2006-07-01 01:38:12 +02:00
< ? php
/* Copyright ( C ) 2005 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2018-03-27 18:00:17 +02:00
* Copyright ( C ) 2004 - 2018 Laurent Destailleur < eldy @ users . sourceforge . net >
2006-07-01 01:38:12 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2006-07-01 01:38:12 +02:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2011-08-01 01:45:11 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2006-07-01 01:38:12 +02:00
*/
/**
2010-06-01 23:33:48 +02:00
* \file htdocs / core / class / discount . class . php
2011-06-19 16:35:16 +02:00
* \ingroup core propal facture commande
* \brief File of class to manage absolute discounts
2008-08-09 00:12:27 +02:00
*/
2006-07-01 01:38:12 +02:00
/**
2018-03-27 18:00:17 +02:00
* Class to manage absolute discounts
2008-08-09 00:12:27 +02:00
*/
2006-07-01 01:38:12 +02:00
class DiscountAbsolute
{
2016-12-17 07:49:28 +01:00
public $db ;
public $error ;
2011-09-10 19:52:21 +02:00
2016-12-17 07:49:28 +01:00
public $id ; // Id discount
public $fk_soc ;
2018-02-14 18:02:40 +01:00
public $discount_type ; // 0 => customer discount, 1 => supplier discount
2016-12-17 07:49:28 +01:00
public $amount_ht ; //
public $amount_tva ; //
public $amount_ttc ; //
public $tva_tx ; // Vat rate
public $fk_user ; // Id utilisateur qui accorde la remise
public $description ; // Description libre
public $datec ; // Date creation
2017-01-28 12:59:32 +01:00
public $fk_facture_line ; // Id invoice line when a discount is used into an invoice line (for absolute discounts)
public $fk_facture ; // Id invoice when a discount line is used into an invoice (for credit note)
2016-12-17 07:49:28 +01:00
public $fk_facture_source ; // Id facture avoir a l'origine de la remise
2017-01-28 12:59:32 +01:00
public $ref_facture_source ; // Ref facture avoir a l'origine de la remise
2018-03-05 11:30:18 +01:00
public $ref_invoice_supplier_source ;
2011-09-10 19:52:21 +02:00
/**
2011-09-11 12:33:48 +02:00
* Constructor
*
2012-07-30 17:17:33 +02:00
* @ param DoliDB $db Database handler
2011-09-10 19:52:21 +02:00
*/
2012-07-30 17:17:33 +02:00
function __construct ( $db )
2011-09-10 19:52:21 +02:00
{
2012-07-30 17:17:33 +02:00
$this -> db = $db ;
2011-09-10 19:52:21 +02:00
}
2007-04-03 01:45:50 +02:00
2007-03-25 05:07:46 +02:00
/**
2011-09-11 12:33:48 +02:00
* Load object from database into memory
*
2018-02-14 10:47:10 +01:00
* @ param int $rowid id discount to load
* @ param int $fk_facture_source fk_facture_source
* @ param int $fk_invoice_supplier_source fk_invoice_supplier_source
* @ return int < 0 if KO , = 0 if not found , > 0 if OK
2011-09-10 19:52:21 +02:00
*/
2018-02-14 10:47:10 +01:00
function fetch ( $rowid , $fk_facture_source = 0 , $fk_invoice_supplier_source = 0 )
2011-09-10 19:52:21 +02:00
{
2016-07-07 12:19:20 +02:00
global $conf ;
2011-09-10 19:52:21 +02:00
// Check parameters
2018-02-14 10:47:10 +01:00
if ( ! $rowid && ! $fk_facture_source && ! $fk_invoice_supplier_source )
2011-09-10 19:52:21 +02:00
{
$this -> error = 'ErrorBadParameters' ;
return - 1 ;
}
2018-02-14 18:02:40 +01:00
$sql = " SELECT sr.rowid, sr.fk_soc, sr.discount_type, " ;
2011-09-10 19:52:21 +02:00
$sql .= " sr.fk_user, " ;
$sql .= " sr.amount_ht, sr.amount_tva, sr.amount_ttc, sr.tva_tx, " ;
2017-09-19 00:24:52 +02:00
$sql .= " sr.multicurrency_amount_ht, sr.multicurrency_amount_tva, sr.multicurrency_amount_ttc, " ;
2018-02-14 10:47:10 +01:00
$sql .= " sr.fk_facture_line, sr.fk_facture, sr.fk_facture_source, sr.fk_invoice_supplier_line, sr.fk_invoice_supplier, sr.fk_invoice_supplier_source, sr.description, " ;
2011-09-10 19:52:21 +02:00
$sql .= " sr.datec, " ;
2018-02-14 10:47:10 +01:00
$sql .= " f.facnumber as ref_facture_source, fsup.facnumber as ref_invoice_supplier_source " ;
2011-09-10 19:52:21 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_remise_except as sr " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " facture as f ON sr.fk_facture_source = f.rowid " ;
2018-02-14 10:47:10 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " facture as fsup ON sr.fk_invoice_supplier_source = fsup.rowid " ;
2016-07-07 12:19:20 +02:00
$sql .= " WHERE sr.entity = " . $conf -> entity ;
if ( $rowid ) $sql .= " AND sr.rowid= " . $rowid ;
if ( $fk_facture_source ) $sql .= " AND sr.fk_facture_source= " . $fk_facture_source ;
2018-02-14 10:47:10 +01:00
if ( $fk_invoice_supplier_source ) $sql .= " AND sr.fk_invoice_supplier_source= " . $fk_invoice_supplier_source ;
2011-09-10 19:52:21 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2011-09-10 19:52:21 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
if ( $this -> db -> num_rows ( $resql ))
{
$obj = $this -> db -> fetch_object ( $resql );
$this -> id = $obj -> rowid ;
$this -> fk_soc = $obj -> fk_soc ;
2018-02-14 18:02:40 +01:00
$this -> discount_type = $obj -> discount_type ;
2017-09-19 00:24:52 +02:00
2011-09-10 19:52:21 +02:00
$this -> amount_ht = $obj -> amount_ht ;
$this -> amount_tva = $obj -> amount_tva ;
$this -> amount_ttc = $obj -> amount_ttc ;
2017-09-19 00:24:52 +02:00
2018-10-17 15:49:08 +02:00
$this -> multicurrency_amount_ht = $this -> multicurrency_subprice = $obj -> multicurrency_amount_ht ;
2017-09-19 00:24:52 +02:00
$this -> multicurrency_amount_tva = $obj -> multicurrency_amount_tva ;
$this -> multicurrency_amount_ttc = $obj -> multicurrency_amount_ttc ;
2011-09-10 19:52:21 +02:00
$this -> tva_tx = $obj -> tva_tx ;
$this -> fk_user = $obj -> fk_user ;
$this -> fk_facture_line = $obj -> fk_facture_line ;
$this -> fk_facture = $obj -> fk_facture ;
$this -> fk_facture_source = $obj -> fk_facture_source ; // Id avoir source
$this -> ref_facture_source = $obj -> ref_facture_source ; // Ref avoir source
2018-02-14 10:47:10 +01:00
$this -> fk_invoice_supplier_line = $obj -> fk_invoice_supplier_line ;
$this -> fk_invoice_supplier = $obj -> fk_invoice_supplier ;
$this -> fk_invoice_supplier_source = $obj -> fk_invoice_supplier_source ; // Id avoir source
$this -> ref_invoice_supplier_source = $obj -> ref_invoice_supplier_source ; // Ref avoir source
2011-09-10 19:52:21 +02:00
$this -> description = $obj -> description ;
$this -> datec = $this -> db -> jdate ( $obj -> datec );
$this -> db -> free ( $resql );
return 1 ;
}
else
{
$this -> db -> free ( $resql );
return 0 ;
}
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
/**
* Create a discount into database
*
* @ param User $user User that create
* @ return int < 0 if KO , > 0 if OK
2007-03-25 05:07:46 +02:00
*/
function create ( $user )
{
2011-09-10 19:52:21 +02:00
global $conf , $langs ;
// Clean parameters
$this -> amount_ht = price2num ( $this -> amount_ht );
$this -> amount_tva = price2num ( $this -> amount_tva );
$this -> amount_ttc = price2num ( $this -> amount_ttc );
2018-10-19 14:43:10 +02:00
2011-09-10 19:52:21 +02:00
$this -> tva_tx = price2num ( $this -> tva_tx );
2018-10-19 14:43:10 +02:00
$this -> multicurrency_amount_ht = price2num ( $this -> multicurrency_amount_ht );
$this -> multicurrency_amount_tva = price2num ( $this -> multicurrency_amount_tva );
$this -> multicurrency_amount_ttc = price2num ( $this -> multicurrency_amount_ttc );
if ( empty ( $this -> multicurrency_amount_ht )) $this -> multicurrency_amount_ht = 0 ;
if ( empty ( $this -> multicurrency_amount_tva )) $this -> multicurrency_amount_tva = 0 ;
if ( empty ( $this -> multicurrency_amount_ttc )) $this -> multicurrency_amount_ttc = 0 ;
2011-09-10 19:52:21 +02:00
// Check parameters
if ( empty ( $this -> description ))
{
$this -> error = 'BadValueForPropertyDescription' ;
2011-09-11 12:33:48 +02:00
dol_syslog ( get_class ( $this ) . " ::create " . $this -> error , LOG_ERR );
2009-08-07 02:49:43 +02:00
return - 1 ;
2011-09-10 19:52:21 +02:00
}
2009-08-07 02:49:43 +02:00
2007-03-25 05:07:46 +02:00
// Insert request
2011-09-10 19:52:21 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " societe_remise_except " ;
2018-02-14 18:02:40 +01:00
$sql .= " (entity, datec, fk_soc, discount_type, fk_user, description, " ;
2011-09-10 19:52:21 +02:00
$sql .= " amount_ht, amount_tva, amount_ttc, tva_tx, " ;
2018-10-17 15:49:08 +02:00
$sql .= " multicurrency_amount_ht, multicurrency_amount_tva, multicurrency_amount_ttc, " ;
2018-02-14 10:47:10 +01:00
$sql .= " fk_facture_source, fk_invoice_supplier_source " ;
2011-09-10 19:52:21 +02:00
$sql .= " ) " ;
2018-02-14 18:02:40 +01:00
$sql .= " VALUES ( " . $conf -> entity . " , ' " . $this -> db -> idate ( $this -> datec != '' ? $this -> datec : dol_now ()) . " ', " . $this -> fk_soc . " , " . ( empty ( $this -> discount_type ) ? 0 : intval ( $this -> discount_type )) . " , " . $user -> id . " , ' " . $this -> db -> escape ( $this -> description ) . " ', " ;
2011-09-10 19:52:21 +02:00
$sql .= " " . $this -> amount_ht . " , " . $this -> amount_tva . " , " . $this -> amount_ttc . " , " . $this -> tva_tx . " , " ;
2018-10-17 15:49:08 +02:00
$sql .= " " . $this -> multicurrency_amount_ht . " , " . $this -> multicurrency_amount_tva . " , " . $this -> multicurrency_amount_ttc . " , " ;
2018-02-14 10:47:10 +01:00
$sql .= " " . ( $this -> fk_facture_source ? " ' " . $this -> db -> escape ( $this -> fk_facture_source ) . " ' " : " null " ) . " , " ;
$sql .= " " . ( $this -> fk_invoice_supplier_source ? " ' " . $this -> db -> escape ( $this -> fk_invoice_supplier_source ) . " ' " : " null " );
2011-09-10 19:52:21 +02:00
$sql .= " ) " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::create " , LOG_DEBUG );
2011-09-10 19:52:21 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " societe_remise_except " );
return $this -> id ;
}
else
{
2007-04-03 01:45:50 +02:00
$this -> error = $this -> db -> lasterror () . ' - sql=' . $sql ;
2007-03-25 05:07:46 +02:00
return - 1 ;
2011-09-10 19:52:21 +02:00
}
}
/**
* Delete object in database . If fk_facture_source is defined , we delete all familiy with same fk_facture_source . If not , only with id is removed
*
* @ param User $user Object of user asking to delete
* @ return int < 0 if KO , > 0 if OK
*/
function delete ( $user )
{
global $conf , $langs ;
// Check if we can remove the discount
2018-02-14 18:02:40 +01:00
if ( $this -> fk_facture_source )
2011-09-10 19:52:21 +02:00
{
2014-04-23 12:03:01 +02:00
$sql = " SELECT COUNT(rowid) as nb " ;
2011-09-10 19:52:21 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_remise_except " ;
$sql .= " WHERE (fk_facture_line IS NOT NULL " ; // Not used as absolute simple discount
$sql .= " OR fk_facture IS NOT NULL) " ; // Not used as credit note and not used as deposit
$sql .= " AND fk_facture_source = " . $this -> fk_facture_source ;
//$sql.=" AND rowid != ".$this->id;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete Check if we can remove discount " , LOG_DEBUG );
2011-09-10 19:52:21 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj -> nb > 0 )
{
$this -> error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved' ;
return - 2 ;
}
}
else
{
2011-12-05 18:36:54 +01:00
dol_print_error ( $this -> db );
2011-09-10 19:52:21 +02:00
return - 1 ;
}
}
2018-03-27 18:00:17 +02:00
2018-02-14 18:02:40 +01:00
// Check if we can remove the discount
if ( $this -> fk_invoice_supplier_source )
{
$sql = " SELECT COUNT(rowid) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_remise_except " ;
$sql .= " WHERE (fk_invoice_supplier_line IS NOT NULL " ; // Not used as absolute simple discount
$sql .= " OR fk_invoice_supplier IS NOT NULL) " ; // Not used as credit note and not used as deposit
$sql .= " AND fk_invoice_supplier_source = " . $this -> fk_invoice_supplier_source ;
//$sql.=" AND rowid != ".$this->id;
2018-03-27 18:00:17 +02:00
2018-02-14 18:02:40 +01:00
dol_syslog ( get_class ( $this ) . " ::delete Check if we can remove discount " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj -> nb > 0 )
{
$this -> error = 'ErrorThisPartOrAnotherIsAlreadyUsedSoDiscountSerieCantBeRemoved' ;
return - 2 ;
}
}
else
{
dol_print_error ( $this -> db );
return - 1 ;
}
}
2011-09-10 19:52:21 +02:00
$this -> db -> begin ();
// Delete but only if not used
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " societe_remise_except " ;
if ( $this -> fk_facture_source ) $sql .= " WHERE fk_facture_source = " . $this -> fk_facture_source ; // Delete all lines of same serie
2018-02-14 18:02:40 +01:00
elseif ( $this -> fk_invoice_supplier_source ) $sql .= " WHERE fk_invoice_supplier_source = " . $this -> fk_invoice_supplier_source ; // Delete all lines of same serie
2011-09-10 19:52:21 +02:00
else $sql .= " WHERE rowid = " . $this -> id ; // Delete only line
$sql .= " AND (fk_facture_line IS NULL " ; // Not used as absolute simple discount
$sql .= " AND fk_facture IS NULL) " ; // Not used as credit note and not used as deposit
2018-02-14 18:02:40 +01:00
$sql .= " AND (fk_invoice_supplier_line IS NULL " ; // Not used as absolute simple discount
$sql .= " AND fk_invoice_supplier IS NULL) " ; // Not used as credit note and not used as deposit
2011-09-10 19:52:21 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete Delete discount " , LOG_DEBUG );
2011-09-10 19:52:21 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
// If source of discount was a credit note or deposit, we change source statut.
if ( $this -> fk_facture_source )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . " facture " ;
$sql .= " set paye=0, fk_statut=1 " ;
$sql .= " WHERE (type = 2 or type = 3) AND rowid= " . $this -> fk_facture_source ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete Update credit note or deposit invoice statut " , LOG_DEBUG );
2011-09-10 19:52:21 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
}
2018-02-14 18:02:40 +01:00
elseif ( $this -> fk_invoice_supplier_source ) {
2018-03-27 18:00:17 +02:00
2018-02-14 18:02:40 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " facture_fourn " ;
$sql .= " set paye=0, fk_statut=1 " ;
$sql .= " WHERE (type = 2 or type = 3) AND rowid= " . $this -> fk_invoice_supplier_source ;
dol_syslog ( get_class ( $this ) . " ::delete Update credit note or deposit invoice statut " , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
if ( $result )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
}
2011-09-10 19:52:21 +02:00
else
{
$this -> db -> commit ();
return 1 ;
}
}
else
{
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
2007-03-25 05:07:46 +02:00
}
2011-09-10 19:52:21 +02:00
/**
2011-09-11 12:33:48 +02:00
* Link the discount to a particular invoice line or a particular invoice .
* When discount is a global discount used as an invoice line , we link using rowidline .
* When discount is from a credit note used to reduce payment of an invoice , we link using rowidinvoice
2011-09-10 19:52:21 +02:00
*
2011-09-11 12:33:48 +02:00
* @ param int $rowidline Invoice line id ( To use discount into invoice lines )
* @ param int $rowidinvoice Invoice id ( To use discount as a credit note to reduc payment of invoice )
* @ return int < 0 if KO , > 0 if OK
2011-09-10 19:52:21 +02:00
*/
2018-02-14 18:02:40 +01:00
function link_to_invoice ( $rowidline , $rowidinvoice )
2011-09-10 19:52:21 +02:00
{
// Check parameters
if ( ! $rowidline && ! $rowidinvoice )
{
$this -> error = 'ErrorBadParameters' ;
return - 1 ;
}
if ( $rowidline && $rowidinvoice )
{
$this -> error = 'ErrorBadParameters' ;
return - 2 ;
}
$sql = " UPDATE " . MAIN_DB_PREFIX . " societe_remise_except " ;
2018-02-14 18:02:40 +01:00
if ( ! empty ( $this -> discount_type )) {
2018-02-14 10:47:10 +01:00
if ( $rowidline ) $sql .= " SET fk_invoice_supplier_line = " . $rowidline ;
if ( $rowidinvoice ) $sql .= " SET fk_invoice_supplier = " . $rowidinvoice ;
} else {
if ( $rowidline ) $sql .= " SET fk_facture_line = " . $rowidline ;
if ( $rowidinvoice ) $sql .= " SET fk_facture = " . $rowidinvoice ;
}
2011-09-10 19:52:21 +02:00
$sql .= " WHERE rowid = " . $this -> id ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::link_to_invoice " , LOG_DEBUG );
2011-09-10 19:52:21 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2018-02-14 18:02:40 +01:00
if ( ! empty ( $this -> discount_type )) {
2018-02-14 10:47:10 +01:00
$this -> fk_invoice_supplier_line = $rowidline ;
$this -> fk_invoice_supplier = $rowidinvoice ;
} else {
$this -> fk_facture_line = $rowidline ;
$this -> fk_facture = $rowidinvoice ;
}
2011-09-10 19:52:21 +02:00
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
return - 3 ;
}
}
/**
2011-09-11 12:33:48 +02:00
* Link the discount to a particular invoice line or a particular invoice .
* Do not call this if discount is linked to a reconcialiated invoice
*
* @ return int < 0 if KO , > 0 if OK
2011-09-10 19:52:21 +02:00
*/
2018-02-14 18:02:40 +01:00
function unlink_invoice ()
2011-09-10 19:52:21 +02:00
{
$sql = " UPDATE " . MAIN_DB_PREFIX . " societe_remise_except " ;
2018-02-14 18:02:40 +01:00
if ( ! empty ( $this -> discount_type )) {
2018-02-14 10:47:10 +01:00
$sql .= " SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL " ;
} else {
$sql .= " SET fk_facture_line = NULL, fk_facture = NULL " ;
}
2011-09-10 19:52:21 +02:00
$sql .= " WHERE rowid = " . $this -> id ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::unlink_invoice " , LOG_DEBUG );
2011-09-10 19:52:21 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
return - 3 ;
}
}
/**
2017-02-24 15:22:32 +01:00
* Return amount ( with tax ) of discounts currently available for a company , user or other criteria
2011-09-11 12:33:48 +02:00
*
2018-02-14 18:02:40 +01:00
* @ param Societe $company Object third party for filter
* @ param User $user Filtre sur un user auteur des remises
* @ param string $filter Filtre autre
* @ param int $maxvalue Filter on max value for discount
* @ param int $discount_type 0 => customer discount , 1 => supplier discount
2011-09-11 12:33:48 +02:00
* @ return int < 0 if KO , amount otherwise
2011-09-10 19:52:21 +02:00
*/
2018-02-14 18:02:40 +01:00
function getAvailableDiscounts ( $company = '' , $user = '' , $filter = '' , $maxvalue = 0 , $discount_type = 0 )
2011-09-10 19:52:21 +02:00
{
2016-07-07 12:19:20 +02:00
global $conf ;
2007-12-02 20:50:58 +01:00
$sql = " SELECT SUM(rc.amount_ttc) as amount " ;
2017-02-24 15:22:32 +01:00
//$sql = "SELECT rc.amount_ttc as amount";
2011-09-10 19:52:21 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_remise_except as rc " ;
2016-07-07 12:19:20 +02:00
$sql .= " WHERE rc.entity = " . $conf -> entity ;
2018-02-14 18:02:40 +01:00
$sql .= " AND rc.discount_type= " . intval ( $discount_type );
if ( ! empty ( $discount_type )) {
$sql .= " AND (rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line IS NULL) " ; // Available from supplier
} else {
$sql .= " AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL) " ; // Available to customer
}
2011-09-10 19:52:21 +02:00
if ( is_object ( $company )) $sql .= " AND rc.fk_soc = " . $company -> id ;
2007-12-02 20:50:58 +01:00
if ( is_object ( $user )) $sql .= " AND rc.fk_user = " . $user -> id ;
2011-11-16 18:58:11 +01:00
if ( $filter ) $sql .= ' AND (' . $filter . ')' ;
2008-08-09 00:12:27 +02:00
if ( $maxvalue ) $sql .= ' AND rc.amount_ttc <= ' . price2num ( $maxvalue );
2007-12-02 20:50:58 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::getAvailableDiscounts " , LOG_DEBUG );
2007-12-02 20:50:58 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
2011-09-10 19:52:21 +02:00
//while ($obj)
2008-08-09 00:12:27 +02:00
//{
2011-09-10 19:52:21 +02:00
//print 'zz'.$obj->amount;
//$obj = $this->db->fetch_object($resql);
2008-08-09 00:12:27 +02:00
//}
2007-12-02 20:50:58 +01:00
return $obj -> amount ;
}
2011-09-10 19:52:21 +02:00
return - 1 ;
}
2007-12-02 20:50:58 +01:00
2009-03-02 22:21:49 +01:00
2011-09-10 19:52:21 +02:00
/**
2017-02-17 00:19:28 +01:00
* Return amount ( with tax ) of all deposits invoices used by invoice as a payment .
* Should always be empty , except if option FACTURE_DEPOSITS_ARE_JUST_PAYMENTS is on ( not recommended ) .
2011-09-11 12:33:48 +02:00
*
2017-02-24 15:22:32 +01:00
* @ param CommonInvoice $invoice Object invoice ( customer of supplier )
* @ param int $multicurrency Return multicurrency_amount instead of amount
* @ return int < 0 if KO , Sum of credit notes and deposits amount otherwise
2011-09-10 19:52:21 +02:00
*/
2017-02-17 00:19:28 +01:00
function getSumDepositsUsed ( $invoice , $multicurrency = 0 )
2011-09-10 19:52:21 +02:00
{
2017-02-17 00:19:28 +01:00
dol_syslog ( get_class ( $this ) . " ::getSumDepositsUsed " , LOG_DEBUG );
2017-09-11 15:04:29 +02:00
2017-02-24 15:22:32 +01:00
if ( $invoice -> element == 'facture' || $invoice -> element == 'invoice' )
{
$sql = 'SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'societe_remise_except as rc, ' . MAIN_DB_PREFIX . 'facture as f' ;
$sql .= ' WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ' . $invoice -> id ;
$sql .= ' AND f.type = 3' ;
}
else if ( $invoice -> element == 'invoice_supplier' )
{
$sql = 'SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'societe_remise_except as rc, ' . MAIN_DB_PREFIX . 'facture_fourn as f' ;
$sql .= ' WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ' . $invoice -> id ;
$sql .= ' AND f.type = 3' ;
}
else
{
$this -> error = get_class ( $this ) . " ::getSumDepositsUsed was called with a bad object as a first parameter " ;
dol_print_error ( $this -> error );
return - 1 ;
}
2017-09-11 15:04:29 +02:00
2011-09-10 19:52:21 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
2016-02-10 23:16:38 +01:00
if ( $multicurrency ) return $obj -> multicurrency_amount ;
else return $obj -> amount ;
2011-09-10 19:52:21 +02:00
}
else
{
2017-02-24 15:22:32 +01:00
$this -> error = $this -> db -> lasterror ();
2011-09-10 19:52:21 +02:00
return - 1 ;
}
}
/**
2017-02-17 00:19:28 +01:00
* Return amount ( with tax ) of all credit notes and deposits invoices used by invoice as a payment
2011-09-11 12:33:48 +02:00
*
2017-02-24 15:22:32 +01:00
* @ param CommonInvoice $invoice Object invoice
* @ param int $multicurrency Return multicurrency_amount instead of amount
* @ return int < 0 if KO , Sum of credit notes and deposits amount otherwise
2011-09-10 19:52:21 +02:00
*/
2017-02-17 00:19:28 +01:00
function getSumCreditNotesUsed ( $invoice , $multicurrency = 0 )
2011-09-10 19:52:21 +02:00
{
2017-02-17 00:19:28 +01:00
dol_syslog ( get_class ( $this ) . " ::getSumCreditNotesUsed " , LOG_DEBUG );
2017-09-11 15:04:29 +02:00
2017-02-24 15:22:32 +01:00
if ( $invoice -> element == 'facture' || $invoice -> element == 'invoice' )
{
$sql = 'SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'societe_remise_except as rc, ' . MAIN_DB_PREFIX . 'facture as f' ;
$sql .= ' WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = ' . $invoice -> id ;
2017-10-13 01:03:33 +02:00
$sql .= ' AND (f.type = 2 OR f.type = 0)' ; // Find discount coming from credit note or excess received
2017-02-24 15:22:32 +01:00
}
else if ( $invoice -> element == 'invoice_supplier' )
{
$sql = 'SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'societe_remise_except as rc, ' . MAIN_DB_PREFIX . 'facture_fourn as f' ;
$sql .= ' WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = ' . $invoice -> id ;
2018-02-14 10:47:10 +01:00
$sql .= ' AND (f.type = 2 OR f.type = 0)' ; // Find discount coming from credit note or excess paid
2017-02-24 15:22:32 +01:00
}
else
{
$this -> error = get_class ( $this ) . " ::getSumCreditNotesUsed was called with a bad object as a first parameter " ;
dol_print_error ( $this -> error );
return - 1 ;
}
2017-09-11 15:04:29 +02:00
2011-09-10 19:52:21 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
2016-02-10 23:16:38 +01:00
if ( $multicurrency ) return $obj -> multicurrency_amount ;
else return $obj -> amount ;
2011-09-10 19:52:21 +02:00
}
else
{
2017-02-24 15:22:32 +01:00
$this -> error = $this -> db -> lasterror ();
2011-09-10 19:52:21 +02:00
return - 1 ;
}
}
/**
2011-09-11 12:33:48 +02:00
* Return clickable ref of object ( with picto or not )
*
* @ param int $withpicto 0 = No picto , 1 = Include picto into link , 2 = Picto only
* @ param string $option Where to link to ( 'invoice' or 'discount' )
* @ return string String with URL
2011-09-10 19:52:21 +02:00
*/
function getNomUrl ( $withpicto , $option = 'invoice' )
{
global $langs ;
$result = '' ;
2015-01-21 01:06:04 +01:00
if ( $option == 'invoice' ) {
2018-02-19 15:52:07 +01:00
$facid =! empty ( $this -> discount_type ) ? $this -> fk_invoice_supplier_source : $this -> fk_facture_source ;
2018-03-05 11:41:06 +01:00
$link =! empty ( $this -> discount_type ) ? '/fourn/facture/card.php' : '/compta/facture/card.php' ;
2011-09-10 19:52:21 +02:00
$label = $langs -> trans ( " ShowDiscount " ) . ': ' . $this -> ref_facture_source ;
2018-03-05 11:41:06 +01:00
$link = '<a href="' . DOL_URL_ROOT . $link . '?facid=' . $facid . '" title="' . dol_escape_htmltag ( $label , 1 ) . '" class="classfortooltip">' ;
2015-03-15 14:04:07 +01:00
$linkend = '</a>' ;
2018-02-19 15:52:07 +01:00
$ref =! empty ( $this -> discount_type ) ? $this -> ref_invoice_supplier_source : $this -> ref_facture_source ;
2011-09-10 19:52:21 +02:00
$picto = 'bill' ;
}
2015-01-21 01:06:04 +01:00
if ( $option == 'discount' ) {
2011-09-10 19:52:21 +02:00
$label = $langs -> trans ( " Discount " );
2015-03-15 14:04:07 +01:00
$link = '<a href="' . DOL_URL_ROOT . '/comm/remx.php?id=' . $this -> fk_soc . '" title="' . dol_escape_htmltag ( $label , 1 ) . '" class="classfortooltip">' ;
$linkend = '</a>' ;
2011-09-10 19:52:21 +02:00
$ref = $langs -> trans ( " Discount " );
$picto = 'generic' ;
}
2015-03-15 14:04:07 +01:00
if ( $withpicto ) $result .= ( $link . img_object ( $label , $picto , 'class="classfortooltip"' ) . $linkend );
2011-09-10 19:52:21 +02:00
if ( $withpicto && $withpicto != 2 ) $result .= ' ' ;
2015-03-15 14:04:07 +01:00
$result .= $link . $ref . $linkend ;
2011-09-10 19:52:21 +02:00
return $result ;
}
2009-03-02 22:21:49 +01:00
2011-09-11 12:33:48 +02:00
/**
2011-09-20 19:19:46 +02:00
* Initialise an instance with random values .
* Used to build previews or test instances .
* id must be 0 if object instance is a specimen .
*
* @ return void
2011-09-11 12:33:48 +02:00
*/
function initAsSpecimen ()
{
global $user , $langs , $conf ;
$this -> fk_soc = 1 ;
$this -> amount_ht = 10 ;
$this -> amount_tva = 1.96 ;
$this -> amount_ttc = 11.96 ;
$this -> tva_tx = 19.6 ;
$this -> description = 'Specimen discount' ;
}
2006-07-01 01:38:12 +02:00
}