2004-10-19 22:35:36 +02:00
< ? php
2012-08-25 03:28:30 +02:00
/* Copyright ( C ) 2003 - 2005 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2019-10-13 20:50:03 +02:00
* Copyright ( C ) 2004 - 2019 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2009 - 2012 Regis Houssin < regis . houssin @ inodbox . com >
2012-08-25 03:28:30 +02:00
* Copyright ( C ) 2010 - 2011 Juanjo Menent < jmenent @ 2 byte . es >
2019-03-02 08:43:09 +01:00
* Copyright ( C ) 2012 Cedric Salvador < csalvador @ gpcsolutions . fr >
2013-04-09 17:18:07 +02:00
* Copyright ( C ) 2013 Florian Henry < florian . henry @ open - concept . pro >
2015-04-18 20:50:03 +02:00
* Copyright ( C ) 2015 Marcos García < marcosgdf @ gmail . com >
2020-04-11 22:53:45 +02:00
* Copyright ( C ) 2017 - 2020 Frédéric France < frederic . france @ netlogic . fr >
2003-10-12 20:11:19 +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
2003-10-12 20:11:19 +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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2003-10-12 20:11:19 +02:00
*/
2005-06-26 00:54:02 +02:00
/**
2010-07-21 14:35:56 +02:00
* \file htdocs / compta / facture / class / facture - rec . class . php
2009-05-27 03:15:44 +02:00
* \ingroup facture
* \brief Fichier de la classe des factures recurentes
*/
2004-08-14 15:00:55 +02:00
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/notify.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php' ;
2016-02-11 09:50:35 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
2005-06-26 00:54:02 +02:00
2004-08-14 15:00:55 +02:00
2005-06-26 00:54:02 +02:00
/**
2017-09-11 01:04:53 +02:00
* Class to manage invoice templates
2009-05-27 03:15:44 +02:00
*/
2016-04-03 20:49:57 +02:00
class FactureRec extends CommonInvoice
2003-10-12 20:11:19 +02:00
{
2022-04-01 19:07:17 +02:00
const TRIGGER_PREFIX = 'BILLREC' ;
2018-08-23 17:07:27 +02:00
/**
* @ var string ID to identify managed object
*/
2019-11-12 09:46:08 +01:00
public $element = 'facturerec' ;
2018-09-02 21:08:57 +02:00
2018-08-22 18:34:50 +02:00
/**
* @ var string Name of table without prefix where object is stored
*/
2019-11-12 09:46:08 +01:00
public $table_element = 'facture_rec' ;
2018-09-02 21:08:57 +02:00
2018-09-02 11:08:41 +02:00
/**
2020-12-05 23:53:55 +01:00
* @ var string Name of subtable line
2018-09-02 11:08:41 +02:00
*/
2019-11-12 09:46:08 +01:00
public $table_element_line = 'facturedet_rec' ;
2018-09-01 15:13:59 +02:00
/**
2020-12-05 23:53:55 +01:00
* @ var string Field with ID of parent key if this field has a parent
2018-09-01 15:13:59 +02:00
*/
2019-11-12 09:46:08 +01:00
public $fk_element = 'fk_facture' ;
2018-09-01 15:13:59 +02:00
2018-09-05 10:31:12 +02:00
/**
* @ var string String with name of icon for myobject . Must be the part after the 'object_' into object_myobject . png
*/
2019-11-12 09:46:08 +01:00
public $picto = 'bill' ;
2017-06-09 09:25:15 +02:00
2018-10-05 15:44:57 +02:00
/**
* @ var int Entity
*/
2018-09-02 11:08:41 +02:00
public $entity ;
2018-10-05 15:44:57 +02:00
2020-04-11 21:06:33 +02:00
/**
* { @ inheritdoc }
*/
protected $table_ref_field = 'titre' ;
2020-10-07 17:41:36 +02:00
/**
* @ var string The label of recurring invoice
*/
public $title ;
2021-03-07 19:16:09 +01:00
public $socid ;
2018-09-02 11:08:41 +02:00
public $number ;
public $date ;
public $remise ;
2021-03-07 19:16:09 +01:00
public $remise_absolue ;
public $remise_percent ;
2021-03-01 04:07:31 +01:00
/**
* @ deprecated
* @ see $total_ht
*/
2018-09-02 11:08:41 +02:00
public $total ;
2021-03-01 04:07:31 +01:00
/**
* @ deprecated
* @ see $total_tva
*/
public $tva ;
2011-02-11 20:32:46 +01:00
2018-09-02 11:08:41 +02:00
public $date_last_gen ;
public $date_when ;
public $nb_gen_done ;
public $nb_gen_max ;
2017-06-09 09:25:15 +02:00
2021-03-07 19:47:55 +01:00
public $user_author ;
2020-04-11 21:06:33 +02:00
/**
* @ var int Frequency
*/
2018-09-02 11:08:41 +02:00
public $frequency ;
2020-04-11 21:06:33 +02:00
2020-10-27 19:46:07 +01:00
/**
2020-04-11 21:06:33 +02:00
* @ var string Unit frequency
*/
2018-09-02 11:08:41 +02:00
public $unit_frequency ;
2017-09-11 01:04:53 +02:00
2018-09-02 11:08:41 +02:00
public $rang ;
public $special_code ;
2011-02-11 20:32:46 +01:00
2019-11-12 09:46:08 +01:00
public $usenewprice = 0 ;
2012-09-14 10:09:56 +02:00
2021-03-07 19:16:09 +01:00
public $date_lim_reglement ;
2021-03-08 09:16:33 +01:00
public $cond_reglement_code ; // Code in llx_c_paiement
public $mode_reglement_code ; // Code in llx_c_paiement
2019-11-12 09:46:08 +01:00
public $suspended ; // status
2018-03-22 21:42:19 +01:00
2021-10-25 22:07:31 +02:00
public $auto_validate ; // 0 to create in draft, 1 to create and validate the new invoice
public $generate_pdf ; // 1 to generate PDF on invoice generation (default)
2021-03-06 12:50:38 +01:00
2021-03-08 09:14:23 +01:00
/**
* @ var int 1 if status is draft
* @ deprecated
*/
public $brouillon ;
2021-03-06 12:50:38 +01:00
2020-01-29 22:38:14 +01:00
/**
* 'type' if the field format ( 'integer' , 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]' , 'varchar(x)' , 'double(24,8)' , 'real' , 'price' , 'text' , 'html' , 'date' , 'datetime' , 'timestamp' , 'duration' , 'mail' , 'phone' , 'url' , 'password' )
* Note : Filter can be a string like " (t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL) "
* 'label' the translation key .
* 'enabled' is a condition when the field must be managed .
* 'position' is the sort order of field .
* 'notnull' is set to 1 if not null in database . Set to - 1 if we must set data to null if empty ( '' or 0 ) .
* 'visible' says if field is visible in list ( Examples : 0 = Not visible , 1 = Visible on list and create / update / view forms , 2 = Visible on list only , 3 = Visible on create / update / view form only ( not list ), 4 = Visible on list and update / view form only ( not create ) . 5 = Visible on list and view only ( not create / not update ) . Using a negative value means field is not shown by default on list but can be selected for viewing )
* 'noteditable' says if field is not editable ( 1 or 0 )
* 'default' is a default value for creation ( can still be overwrote by the Setup of Default Values if field is editable in creation form ) . Note : If default is set to '(PROV)' and field is 'ref' , the default value will be set to '(PROVid)' where id is rowid when a new record is created .
* 'index' if we want an index in database .
* 'foreignkey' => 'tablename.field' if the field is a foreign key ( it is recommanded to name the field fk_ ... ) .
* 'searchall' is 1 if we want to search in this field when making a search from the quick search button .
* 'isameasure' must be set to 1 if you want to have a total on list for this field . Field type must be summable like integer or double ( 24 , 8 ) .
* 'css' is the CSS style to use on field . For example : 'maxwidth200'
* 'help' is a string visible as a tooltip on field
* 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
* 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute . In most cases , this is never set into the definition of $fields into class , but is set dynamically by some part of code .
2021-04-29 12:10:55 +02:00
* 'arrayofkeyval' to set list of value if type is a list of predefined values . For example : array ( " 0 " => " Draft " , " 1 " => " Active " , " -1 " => " Cancel " )
2020-01-29 22:38:14 +01:00
* 'comment' is not used . You can store here any text of your choice . It is not used by application .
*
* Note : To have value dynamic , you can set value to 0 in definition and edit the value on the fly into the constructor .
*/
// BEGIN MODULEBUILDER PROPERTIES
/**
* @ var array Array with all fields and their property . Do not use it as a static var . It may be modified by constructor .
*/
2020-04-10 10:59:32 +02:00
public $fields = array (
2020-01-29 22:38:14 +01:00
'rowid' => array ( 'type' => 'integer' , 'label' => 'TechnicalID' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 10 ),
2020-04-11 22:53:45 +02:00
'titre' => array ( 'type' => 'varchar(100)' , 'label' => 'Titre' , 'enabled' => 1 , 'showoncombobox' => 1 , 'visible' =>- 1 , 'position' => 15 ),
2020-01-29 22:38:14 +01:00
'entity' => array ( 'type' => 'integer' , 'label' => 'Entity' , 'default' => 1 , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' => 1 , 'position' => 20 , 'index' => 1 ),
2022-05-21 00:08:18 +02:00
'fk_soc' => array ( 'type' => 'integer:Societe:societe/class/societe.class.php' , 'label' => 'ThirdParty' , 'enabled' => '$conf->societe->enabled' , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 25 ),
2020-01-29 22:38:14 +01:00
'datec' => array ( 'type' => 'datetime' , 'label' => 'DateCreation' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 30 ),
2020-01-31 15:12:17 +01:00
//'amount' =>array('type'=>'double(24,8)', 'label'=>'Amount', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>35),
2020-01-29 22:38:14 +01:00
'remise' => array ( 'type' => 'double' , 'label' => 'Remise' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 40 ),
2020-02-07 15:51:14 +01:00
//'remise_percent' =>array('type'=>'double', 'label'=>'Remise percent', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
//'remise_absolue' =>array('type'=>'double', 'label'=>'Remise absolue', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
2021-02-22 05:58:46 +01:00
'total_tva' => array ( 'type' => 'double(24,8)' , 'label' => 'Tva' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 55 , 'isameasure' => 1 ),
2020-01-29 22:38:14 +01:00
'localtax1' => array ( 'type' => 'double(24,8)' , 'label' => 'Localtax1' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 60 , 'isameasure' => 1 ),
'localtax2' => array ( 'type' => 'double(24,8)' , 'label' => 'Localtax2' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 65 , 'isameasure' => 1 ),
2021-02-22 05:58:46 +01:00
'total_ht' => array ( 'type' => 'double(24,8)' , 'label' => 'Total' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 70 , 'isameasure' => 1 ),
2020-01-29 22:38:14 +01:00
'total_ttc' => array ( 'type' => 'double(24,8)' , 'label' => 'Total ttc' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 75 , 'isameasure' => 1 ),
'fk_user_author' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'Fk user author' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 80 ),
2022-06-14 17:53:17 +02:00
'fk_projet' => array ( 'type' => 'integer:Project:projet/class/project.class.php:1:fk_statut=1' , 'label' => 'Fk projet' , 'enabled' => '$conf->project->enabled' , 'visible' =>- 1 , 'position' => 85 ),
2020-01-29 22:38:14 +01:00
'fk_cond_reglement' => array ( 'type' => 'integer' , 'label' => 'Fk cond reglement' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 90 ),
'fk_mode_reglement' => array ( 'type' => 'integer' , 'label' => 'Fk mode reglement' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 95 ),
'date_lim_reglement' => array ( 'type' => 'date' , 'label' => 'Date lim reglement' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 100 ),
2022-09-06 05:30:35 +02:00
'note_private' => array ( 'type' => 'text' , 'label' => 'NotePrivate' , 'enabled' => 1 , 'visible' => 0 , 'position' => 105 ),
'note_public' => array ( 'type' => 'text' , 'label' => 'NotePublic' , 'enabled' => 1 , 'visible' => 0 , 'position' => 110 ),
2020-01-29 22:38:14 +01:00
'modelpdf' => array ( 'type' => 'varchar(255)' , 'label' => 'Modelpdf' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 115 ),
2020-09-25 14:24:26 +02:00
'date_last_gen' => array ( 'type' => 'varchar(7)' , 'label' => 'Last gen' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 120 ),
2020-01-29 22:38:14 +01:00
'unit_frequency' => array ( 'type' => 'varchar(2)' , 'label' => 'Unit frequency' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 125 ),
'date_when' => array ( 'type' => 'datetime' , 'label' => 'Date when' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 130 ),
'date_last_gen' => array ( 'type' => 'datetime' , 'label' => 'Date last gen' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 135 ),
'nb_gen_done' => array ( 'type' => 'integer' , 'label' => 'Nb gen done' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 140 ),
'nb_gen_max' => array ( 'type' => 'integer' , 'label' => 'Nb gen max' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 145 ),
'frequency' => array ( 'type' => 'integer' , 'label' => 'Frequency' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 150 ),
2020-02-07 15:51:14 +01:00
'usenewprice' => array ( 'type' => 'integer' , 'label' => 'UseNewPrice' , 'enabled' => 1 , 'visible' => 0 , 'position' => 155 ),
'revenuestamp' => array ( 'type' => 'double(24,8)' , 'label' => 'RevenueStamp' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 160 , 'isameasure' => 1 ),
2020-01-29 22:38:14 +01:00
'auto_validate' => array ( 'type' => 'integer' , 'label' => 'Auto validate' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 165 ),
'generate_pdf' => array ( 'type' => 'integer' , 'label' => 'Generate pdf' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 170 ),
2022-05-21 00:08:18 +02:00
'fk_account' => array ( 'type' => 'integer' , 'label' => 'Fk account' , 'enabled' => '$conf->banque->enabled' , 'visible' =>- 1 , 'position' => 175 ),
2020-01-29 22:38:14 +01:00
'fk_multicurrency' => array ( 'type' => 'integer' , 'label' => 'Fk multicurrency' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 180 ),
'multicurrency_code' => array ( 'type' => 'varchar(255)' , 'label' => 'Multicurrency code' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 185 ),
2020-02-07 15:51:14 +01:00
'multicurrency_tx' => array ( 'type' => 'double(24,8)' , 'label' => 'Multicurrency tx' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 190 , 'isameasure' => 1 ),
'multicurrency_total_ht' => array ( 'type' => 'double(24,8)' , 'label' => 'Multicurrency total ht' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 195 , 'isameasure' => 1 ),
'multicurrency_total_tva' => array ( 'type' => 'double(24,8)' , 'label' => 'Multicurrency total tva' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 200 , 'isameasure' => 1 ),
'multicurrency_total_ttc' => array ( 'type' => 'double(24,8)' , 'label' => 'Multicurrency total ttc' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 205 , 'isameasure' => 1 ),
2020-01-29 22:38:14 +01:00
'fk_user_modif' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'UserModif' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' =>- 1 , 'position' => 210 ),
'tms' => array ( 'type' => 'timestamp' , 'label' => 'DateModification' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 215 ),
'suspended' => array ( 'type' => 'integer' , 'label' => 'Suspended' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 225 ),
);
// END MODULEBUILDER PROPERTIES
2018-03-22 21:42:19 +01:00
const STATUS_NOTSUSPENDED = 0 ;
const STATUS_SUSPENDED = 1 ;
2009-05-27 03:15:44 +02:00
/**
2011-09-08 22:02:57 +02:00
* Constructor
*
2012-07-30 17:17:33 +02:00
* @ param DoliDB $db Database handler
2009-05-27 03:15:44 +02:00
*/
2022-05-18 11:00:43 +02:00
public function __construct ( DoliDB $db )
2009-05-27 03:15:44 +02:00
{
2012-07-30 17:17:33 +02:00
$this -> db = $db ;
2009-05-27 03:15:44 +02:00
}
/**
2011-09-08 22:02:57 +02:00
* Create a predefined invoice
*
2012-01-10 09:57:16 +01:00
* @ param User $user User object
2011-09-08 22:02:57 +02:00
* @ param int $facid Id of source invoice
2020-06-25 14:01:38 +02:00
* @ param int $notrigger No trigger
2016-03-03 21:14:20 +01:00
* @ return int < 0 if KO , id of invoice created if OK
2009-05-27 03:15:44 +02:00
*/
2020-06-25 14:01:38 +02:00
public function create ( $user , $facid , $notrigger = 0 )
2009-05-27 03:15:44 +02:00
{
2012-08-25 03:28:30 +02:00
global $conf ;
2009-05-27 03:15:44 +02:00
2019-11-12 09:46:08 +01:00
$error = 0 ;
$now = dol_now ();
2009-05-27 03:15:44 +02:00
// Clean parameters
2020-12-16 11:07:34 +01:00
$this -> titre = trim ( isset ( $this -> titre ) ? $this -> titre : $this -> title ); // deprecated
2019-11-12 09:46:08 +01:00
$this -> title = trim ( $this -> title );
$this -> usenewprice = empty ( $this -> usenewprice ) ? 0 : $this -> usenewprice ;
2021-02-23 21:09:01 +01:00
if ( empty ( $this -> suspended )) {
$this -> suspended = 0 ;
}
2017-06-09 09:25:15 +02:00
2016-02-11 09:50:35 +01:00
// No frequency defined then no next date to execution
2021-02-23 21:09:01 +01:00
if ( empty ( $this -> frequency )) {
2019-11-12 09:46:08 +01:00
$this -> frequency = 0 ;
$this -> date_when = null ;
2016-02-11 09:50:35 +01:00
}
2017-06-09 09:25:15 +02:00
2019-11-12 09:46:08 +01:00
$this -> frequency = abs ( $this -> frequency );
$this -> nb_gen_done = 0 ;
$this -> nb_gen_max = empty ( $this -> nb_gen_max ) ? 0 : $this -> nb_gen_max ;
$this -> auto_validate = empty ( $this -> auto_validate ) ? 0 : $this -> auto_validate ;
$this -> generate_pdf = empty ( $this -> generate_pdf ) ? 0 : $this -> generate_pdf ;
2017-11-18 00:54:23 +01:00
2008-07-26 16:48:22 +02:00
$this -> db -> begin ();
2009-02-04 21:38:07 +01:00
2009-05-27 03:15:44 +02:00
// Charge facture modele
2019-11-12 09:46:08 +01:00
$facsrc = new Facture ( $this -> db );
$result = $facsrc -> fetch ( $facid );
2021-02-23 21:09:01 +01:00
if ( $result > 0 ) {
2009-05-27 03:15:44 +02:00
// On positionne en mode brouillon la facture
$this -> brouillon = 1 ;
2022-05-13 11:23:49 +02:00
$this -> fk_soc = $facsrc -> socid ;
2009-05-27 03:15:44 +02:00
2009-11-19 12:25:08 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " facture_rec ( " ;
2019-11-12 09:46:08 +01:00
$sql .= " titre " ;
$sql .= " , fk_soc " ;
$sql .= " , entity " ;
$sql .= " , datec " ;
$sql .= " , amount " ;
$sql .= " , remise " ;
$sql .= " , note_private " ;
$sql .= " , note_public " ;
$sql .= " , modelpdf " ;
$sql .= " , fk_user_author " ;
$sql .= " , fk_projet " ;
$sql .= " , fk_account " ;
$sql .= " , fk_cond_reglement " ;
$sql .= " , fk_mode_reglement " ;
$sql .= " , usenewprice " ;
$sql .= " , frequency " ;
$sql .= " , unit_frequency " ;
$sql .= " , date_when " ;
$sql .= " , date_last_gen " ;
$sql .= " , nb_gen_done " ;
$sql .= " , nb_gen_max " ;
$sql .= " , auto_validate " ;
$sql .= " , generate_pdf " ;
$sql .= " , fk_multicurrency " ;
$sql .= " , multicurrency_code " ;
$sql .= " , multicurrency_tx " ;
$sql .= " , suspended " ;
$sql .= " ) VALUES ( " ;
$sql .= " ' " . $this -> db -> escape ( $this -> titre ? $this -> titre : $this -> title ) . " ' " ;
2022-05-13 11:23:49 +02:00
$sql .= " , " . (( int ) $this -> fk_soc );
2021-09-03 21:25:17 +02:00
$sql .= " , " . (( int ) $conf -> entity );
2019-11-12 09:46:08 +01:00
$sql .= " , ' " . $this -> db -> idate ( $now ) . " ' " ;
2021-10-21 16:46:04 +02:00
$sql .= " , " . ( ! empty ( $facsrc -> total_ttc ) ? (( float ) $facsrc -> total_ttc ) : '0' );
$sql .= " , " . ( ! empty ( $facsrc -> remise_absolue ) ? (( float ) $this -> remise_absolue ) : '0' );
2019-11-12 09:46:08 +01:00
$sql .= " , " . ( ! empty ( $this -> note_private ) ? ( " ' " . $this -> db -> escape ( $this -> note_private ) . " ' " ) : " NULL " );
$sql .= " , " . ( ! empty ( $this -> note_public ) ? ( " ' " . $this -> db -> escape ( $this -> note_public ) . " ' " ) : " NULL " );
2021-02-08 18:30:09 +01:00
$sql .= " , " . ( ! empty ( $this -> model_pdf ) ? ( " ' " . $this -> db -> escape ( $this -> model_pdf ) . " ' " ) : " NULL " );
2021-09-03 21:25:17 +02:00
$sql .= " , " . (( int ) $user -> id );
2022-03-22 11:47:25 +01:00
$sql .= " , " . ( ! empty ( $this -> fk_project ) ? (( int ) $this -> fk_project ) : " null " );
2021-09-03 21:25:17 +02:00
$sql .= " , " . ( ! empty ( $facsrc -> fk_account ) ? (( int ) $facsrc -> fk_account ) : " null " );
2022-03-22 11:47:25 +01:00
$sql .= " , " . ( $this -> cond_reglement_id > 0 ? (( int ) $this -> cond_reglement_id ) : " null " );
$sql .= " , " . ( $this -> mode_reglement_id > 0 ? (( int ) $this -> mode_reglement_id ) : " null " );
2021-09-03 21:25:17 +02:00
$sql .= " , " . (( int ) $this -> usenewprice );
$sql .= " , " . (( int ) $this -> frequency );
2019-11-12 09:46:08 +01:00
$sql .= " , ' " . $this -> db -> escape ( $this -> unit_frequency ) . " ' " ;
$sql .= " , " . ( ! empty ( $this -> date_when ) ? " ' " . $this -> db -> idate ( $this -> date_when ) . " ' " : 'NULL' );
$sql .= " , " . ( ! empty ( $this -> date_last_gen ) ? " ' " . $this -> db -> idate ( $this -> date_last_gen ) . " ' " : 'NULL' );
2021-09-03 21:25:17 +02:00
$sql .= " , " . (( int ) $this -> nb_gen_done );
$sql .= " , " . (( int ) $this -> nb_gen_max );
$sql .= " , " . (( int ) $this -> auto_validate );
$sql .= " , " . (( int ) $this -> generate_pdf );
$sql .= " , " . (( int ) $facsrc -> fk_multicurrency );
2019-11-12 09:46:08 +01:00
$sql .= " , ' " . $this -> db -> escape ( $facsrc -> multicurrency_code ) . " ' " ;
2021-09-03 21:25:17 +02:00
$sql .= " , " . (( float ) $facsrc -> multicurrency_tx );
$sql .= " , " . (( int ) $this -> suspended );
2019-11-12 09:46:08 +01:00
$sql .= " ) " ;
2010-01-20 20:38:41 +01:00
2021-02-23 21:09:01 +01:00
if ( $this -> db -> query ( $sql )) {
2009-05-27 03:15:44 +02:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " facture_rec " );
2017-12-05 13:38:24 +01:00
// Fields used into addline later
$this -> fk_multicurrency = $facsrc -> fk_multicurrency ;
$this -> multicurrency_code = $facsrc -> multicurrency_code ;
$this -> multicurrency_tx = $facsrc -> multicurrency_tx ;
2016-03-03 13:16:46 +01:00
// Add lines
2019-11-12 09:46:08 +01:00
$num = count ( $facsrc -> lines );
2021-02-23 21:09:01 +01:00
for ( $i = 0 ; $i < $num ; $i ++ ) {
2017-05-09 14:19:16 +02:00
$tva_tx = $facsrc -> lines [ $i ] -> tva_tx ;
2021-02-23 21:09:01 +01:00
if ( ! empty ( $facsrc -> lines [ $i ] -> vat_src_code ) && ! preg_match ( '/\(/' , $tva_tx )) {
$tva_tx .= ' (' . $facsrc -> lines [ $i ] -> vat_src_code . ')' ;
}
2017-05-09 14:19:16 +02:00
2022-03-22 11:47:25 +01:00
$default_start_fill = getDolGlobalInt ( 'INVOICEREC_SET_AUTOFILL_DATE_START' );
$default_end_fill = getDolGlobalInt ( 'INVOICEREC_SET_AUTOFILL_DATE_END' );
2020-10-27 19:46:07 +01:00
$result_insert = $this -> addline (
$facsrc -> lines [ $i ] -> desc ,
$facsrc -> lines [ $i ] -> subprice ,
$facsrc -> lines [ $i ] -> qty ,
2017-05-09 14:19:16 +02:00
$tva_tx ,
2020-10-27 19:46:07 +01:00
$facsrc -> lines [ $i ] -> localtax1_tx ,
$facsrc -> lines [ $i ] -> localtax2_tx ,
$facsrc -> lines [ $i ] -> fk_product ,
$facsrc -> lines [ $i ] -> remise_percent ,
'HT' ,
2018-01-17 19:45:23 +01:00
$facsrc -> lines [ $i ] -> info_bits ,
2020-10-27 19:46:07 +01:00
'' ,
0 ,
$facsrc -> lines [ $i ] -> product_type ,
$facsrc -> lines [ $i ] -> rang ,
$facsrc -> lines [ $i ] -> special_code ,
$facsrc -> lines [ $i ] -> label ,
2017-12-05 13:38:24 +01:00
$facsrc -> lines [ $i ] -> fk_unit ,
2021-03-03 22:52:02 +01:00
$facsrc -> lines [ $i ] -> multicurrency_subprice ,
2022-03-22 11:47:25 +01:00
$default_start_fill ,
$default_end_fill ,
2021-03-03 22:52:02 +01:00
null ,
$facsrc -> lines [ $i ] -> pa_ht
2020-10-27 19:46:07 +01:00
);
2009-05-27 03:15:44 +02:00
2021-02-23 21:09:01 +01:00
if ( $result_insert < 0 ) {
2009-05-27 03:15:44 +02:00
$error ++ ;
2020-05-21 15:05:19 +02:00
} else {
2020-10-27 19:46:07 +01:00
$objectline = new FactureLigneRec ( $this -> db );
2021-03-05 19:29:09 +01:00
2021-03-03 23:55:28 +01:00
$result2 = $objectline -> fetch ( $result_insert );
if ( $result2 > 0 ) {
2020-10-27 19:46:07 +01:00
// Extrafields
if ( method_exists ( $facsrc -> lines [ $i ], 'fetch_optionals' )) {
2021-02-08 18:30:09 +01:00
$facsrc -> lines [ $i ] -> fetch_optionals ( $facsrc -> lines [ $i ] -> id );
2020-10-27 19:46:07 +01:00
$objectline -> array_options = $facsrc -> lines [ $i ] -> array_options ;
}
$result = $objectline -> insertExtraFields ();
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
2020-10-27 19:46:07 +01:00
$error ++ ;
}
2021-03-03 23:55:28 +01:00
} elseif ( $result2 < 0 ) {
$this -> errors [] = $objectline -> error ;
$error ++ ;
2020-10-27 19:46:07 +01:00
}
2020-04-02 01:10:56 +02:00
}
2009-05-27 03:15:44 +02:00
}
2017-06-09 09:25:15 +02:00
2021-02-23 21:09:01 +01:00
if ( ! empty ( $this -> linkedObjectsIds ) && empty ( $this -> linked_objects )) { // To use new linkedObjectsIds instead of old linked_objects
2019-11-12 09:46:08 +01:00
$this -> linked_objects = $this -> linkedObjectsIds ; // TODO Replace linked_objects with linkedObjectsIds
2017-10-19 19:06:48 +02:00
}
// Add object linked
2021-02-23 21:09:01 +01:00
if ( ! $error && $this -> id && ! empty ( $this -> linked_objects ) && is_array ( $this -> linked_objects )) {
foreach ( $this -> linked_objects as $origin => $tmp_origin_id ) {
if ( is_array ( $tmp_origin_id )) { // New behaviour, if linked_object can have several links per type, so is something like array('contract'=>array(id1, id2, ...))
foreach ( $tmp_origin_id as $origin_id ) {
2020-10-27 19:46:07 +01:00
$ret = $this -> add_object_linked ( $origin , $origin_id );
2021-02-23 21:09:01 +01:00
if ( ! $ret ) {
2020-10-27 19:46:07 +01:00
$this -> error = $this -> db -> lasterror ();
$error ++ ;
}
}
} else // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
{
$origin_id = $tmp_origin_id ;
$ret = $this -> add_object_linked ( $origin , $origin_id );
2021-02-23 21:09:01 +01:00
if ( ! $ret ) {
2020-10-27 19:46:07 +01:00
$this -> error = $this -> db -> lasterror ();
$error ++ ;
}
}
2017-10-19 19:06:48 +02:00
}
}
2009-05-27 03:15:44 +02:00
2020-06-25 14:01:38 +02:00
if ( ! $error ) {
$result = $this -> insertExtraFields ();
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
2020-06-25 14:01:38 +02:00
$error ++ ;
}
}
2021-02-23 21:09:01 +01:00
if ( ! $error && ! $notrigger ) {
2020-06-25 14:01:38 +02:00
// Call trigger
$result = $this -> call_trigger ( 'BILLREC_CREATE' , $user );
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
2020-06-25 14:01:38 +02:00
$this -> db -> rollback ();
return - 2 ;
}
// End call triggers
}
2021-02-23 21:09:01 +01:00
if ( $error ) {
2008-07-26 16:48:22 +02:00
$this -> db -> rollback ();
2021-03-03 23:55:28 +01:00
return - 3 ;
2020-05-21 15:05:19 +02:00
} else {
2008-07-26 16:48:22 +02:00
$this -> db -> commit ();
2009-05-27 03:15:44 +02:00
return $this -> id ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-10-27 19:46:07 +01:00
$this -> error = $this -> db -> lasterror ();
2008-07-26 16:48:22 +02:00
$this -> db -> rollback ();
2009-05-27 03:15:44 +02:00
return - 2 ;
}
2020-05-21 15:05:19 +02:00
} else {
2008-07-26 16:48:22 +02:00
$this -> db -> rollback ();
2009-05-27 03:15:44 +02:00
return - 1 ;
}
}
2003-10-12 20:11:19 +02:00
2005-06-26 00:54:02 +02:00
2019-03-01 15:38:15 +01:00
/**
* Update a line to invoice_rec .
*
* @ param User $user User
* @ param int $notrigger No trigger
* @ return int < 0 if KO , Id of line if OK
*/
2019-03-02 00:14:22 +01:00
public function update ( User $user , $notrigger = 0 )
2019-03-01 15:38:15 +01:00
{
2020-10-27 19:46:07 +01:00
global $conf ;
$error = 0 ;
$sql = " UPDATE " . MAIN_DB_PREFIX . " facture_rec SET " ;
2022-01-10 14:27:43 +01:00
$sql .= " entity = " . (( int ) $this -> entity ) . " , " ;
$sql .= " titre = ' " . $this -> db -> escape ( $this -> title ) . " ', " ;
$sql .= " suspended = " . (( int ) $this -> suspended ) . " , " ;
$sql .= " fk_soc = " . (( int ) $this -> socid ) . " , " ;
$sql .= " total_tva = " . (( float ) $this -> total_tva ) . " , " ;
$sql .= " localtax1 = " . (( float ) $this -> localtax1 ) . " , " ;
$sql .= " localtax2 = " . (( float ) $this -> localtax2 ) . " , " ;
$sql .= " total_ht = " . (( float ) $this -> total_ht ) . " , " ;
$sql .= " total_ttc = " . (( float ) $this -> total_ttc ) . " , " ;
$sql .= " remise_percent = " . (( float ) $this -> remise_percent );
2020-10-27 19:46:07 +01:00
// TODO Add missing fields
2021-06-09 15:36:47 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2020-10-27 19:46:07 +01:00
2022-01-10 14:27:43 +01:00
$this -> db -> begin ();
2020-10-27 19:46:07 +01:00
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
2022-01-10 14:27:43 +01:00
2020-10-27 19:46:07 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
if ( ! $error ) {
2020-10-27 19:46:07 +01:00
$result = $this -> insertExtraFields ();
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
2020-10-27 19:46:07 +01:00
$error ++ ;
}
}
2021-02-23 21:09:01 +01:00
if ( ! $error && ! $notrigger ) {
2020-10-27 19:46:07 +01:00
// Call trigger
2022-04-01 19:07:17 +02:00
$result = $this -> call_trigger ( 'BILLREC_MODIFY' , $user );
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
2020-10-27 19:46:07 +01:00
$this -> db -> rollback ();
return - 2 ;
}
// End call triggers
}
$this -> db -> commit ();
return 1 ;
} else {
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
2022-01-10 14:27:43 +01:00
return - 1 ;
2020-10-27 19:46:07 +01:00
}
2019-03-01 15:38:15 +01:00
}
2007-07-09 23:26:51 +02:00
/**
2016-02-16 00:31:05 +01:00
* Load object and lines
2011-12-05 18:36:54 +01:00
*
2016-02-03 02:41:07 +01:00
* @ param int $rowid Id of object to load
2016-02-16 00:31:05 +01:00
* @ param string $ref Reference of recurring invoice
2016-02-03 02:41:07 +01:00
* @ param string $ref_ext External reference of invoice
* @ return int > 0 if OK , < 0 if KO , 0 if not found
2009-05-27 03:15:44 +02:00
*/
2020-04-13 01:10:35 +02:00
public function fetch ( $rowid , $ref = '' , $ref_ext = '' )
2007-07-09 23:26:51 +02:00
{
2022-03-28 21:12:57 +02:00
dol_syslog ( 'FactureRec::fetch' , LOG_DEBUG );
2021-02-22 05:58:46 +01:00
$sql = 'SELECT f.rowid, f.entity, f.titre as title, f.suspended, f.fk_soc, f.total_tva, f.localtax1, f.localtax2, f.total_ht, f.total_ttc' ;
2019-11-12 09:46:08 +01:00
$sql .= ', f.remise_percent, f.remise_absolue, f.remise' ;
$sql .= ', f.date_lim_reglement as dlr' ;
$sql .= ', f.note_private, f.note_public, f.fk_user_author' ;
2020-11-08 00:18:41 +01:00
$sql .= ', f.modelpdf as model_pdf' ;
2019-11-12 09:46:08 +01:00
$sql .= ', f.fk_mode_reglement, f.fk_cond_reglement, f.fk_projet as fk_project' ;
$sql .= ', f.fk_account' ;
$sql .= ', f.frequency, f.unit_frequency, f.date_when, f.date_last_gen, f.nb_gen_done, f.nb_gen_max, f.usenewprice, f.auto_validate' ;
2020-10-27 19:46:07 +01:00
$sql .= ', f.generate_pdf' ;
$sql .= " , f.fk_multicurrency, f.multicurrency_code, f.multicurrency_tx, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc " ;
$sql .= ', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle' ;
2019-11-12 09:46:08 +01:00
$sql .= ', c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc' ;
2016-02-16 00:31:05 +01:00
//$sql.= ', el.fk_source';
2019-11-12 09:46:08 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'facture_rec as f' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_payment_term as c ON f.fk_cond_reglement = c.rowid' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_paiement as p ON f.fk_mode_reglement = p.id' ;
2016-02-16 00:31:05 +01:00
//$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON el.fk_target = f.rowid AND el.targettype = 'facture'";
2019-11-12 09:46:08 +01:00
$sql .= ' WHERE f.entity IN (' . getEntity ( 'invoice' ) . ')' ;
2021-02-23 21:09:01 +01:00
if ( $rowid ) {
2022-01-10 14:27:43 +01:00
$sql .= ' AND f.rowid = ' . (( int ) $rowid );
2021-02-23 21:09:01 +01:00
} elseif ( $ref ) {
2022-01-10 14:27:43 +01:00
$sql .= " AND f.titre = ' " . $this -> db -> escape ( $ref ) . " ' " ;
2021-03-03 23:55:28 +01:00
} else {
$sql .= ' AND f.rowid = 0' ;
2021-02-23 21:09:01 +01:00
}
2016-02-03 02:41:07 +01:00
/* This field are not used for template invoice
2021-03-05 19:29:09 +01:00
if ( $ref_ext ) $sql .= " AND f.ref_ext=' " . $this -> db -> escape ( $ref_ext ) . " ' " ;
*/
2017-06-09 09:25:15 +02:00
2007-07-09 23:26:51 +02:00
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $result ) {
if ( $this -> db -> num_rows ( $result )) {
2007-07-09 23:26:51 +02:00
$obj = $this -> db -> fetch_object ( $result );
2016-02-16 00:31:05 +01:00
$this -> id = $obj -> rowid ;
2016-09-10 04:33:23 +02:00
$this -> entity = $obj -> entity ;
2019-11-12 09:46:08 +01:00
$this -> titre = $obj -> title ; // deprecated
2019-07-19 13:22:54 +02:00
$this -> title = $obj -> title ;
$this -> ref = $obj -> title ;
2017-09-10 20:47:45 +02:00
$this -> suspended = $obj -> suspended ;
2007-07-09 23:26:51 +02:00
$this -> remise_percent = $obj -> remise_percent ;
$this -> remise_absolue = $obj -> remise_absolue ;
$this -> remise = $obj -> remise ;
2021-02-22 05:58:46 +01:00
$this -> total_ht = $obj -> total_ht ;
$this -> total_tva = $obj -> total_tva ;
2017-05-09 14:19:16 +02:00
$this -> total_localtax1 = $obj -> localtax1 ;
$this -> total_localtax2 = $obj -> localtax2 ;
2007-07-09 23:26:51 +02:00
$this -> total_ttc = $obj -> total_ttc ;
$this -> socid = $obj -> fk_soc ;
2010-05-08 21:31:43 +02:00
$this -> date_lim_reglement = $this -> db -> jdate ( $obj -> dlr );
2007-07-09 23:26:51 +02:00
$this -> mode_reglement_id = $obj -> fk_mode_reglement ;
$this -> mode_reglement_code = $obj -> mode_reglement_code ;
$this -> mode_reglement = $obj -> mode_reglement_libelle ;
$this -> cond_reglement_id = $obj -> fk_cond_reglement ;
$this -> cond_reglement_code = $obj -> cond_reglement_code ;
$this -> cond_reglement = $obj -> cond_reglement_libelle ;
2010-01-20 20:38:41 +01:00
$this -> cond_reglement_doc = $obj -> cond_reglement_libelle_doc ;
2019-03-15 15:07:52 +01:00
$this -> fk_project = $obj -> fk_project ;
2016-02-12 12:31:43 +01:00
$this -> fk_account = $obj -> fk_account ;
2013-04-09 17:18:07 +02:00
$this -> note_private = $obj -> note_private ;
2007-07-09 23:26:51 +02:00
$this -> note_public = $obj -> note_public ;
$this -> user_author = $obj -> fk_user_author ;
2021-02-08 18:30:09 +01:00
$this -> modelpdf = $obj -> model_pdf ; // deprecated
2020-11-08 00:18:41 +01:00
$this -> model_pdf = $obj -> model_pdf ;
2021-03-07 20:26:36 +01:00
//$this->special_code = $obj->special_code;
2016-02-11 09:50:35 +01:00
$this -> frequency = $obj -> frequency ;
2019-11-12 09:46:08 +01:00
$this -> unit_frequency = $obj -> unit_frequency ;
2016-02-16 00:31:05 +01:00
$this -> date_when = $this -> db -> jdate ( $obj -> date_when );
2019-11-12 09:46:08 +01:00
$this -> date_last_gen = $this -> db -> jdate ( $obj -> date_last_gen );
2016-02-11 09:50:35 +01:00
$this -> nb_gen_done = $obj -> nb_gen_done ;
2019-11-12 09:46:08 +01:00
$this -> nb_gen_max = $obj -> nb_gen_max ;
2016-02-11 09:50:35 +01:00
$this -> usenewprice = $obj -> usenewprice ;
2019-11-12 09:46:08 +01:00
$this -> auto_validate = $obj -> auto_validate ;
$this -> generate_pdf = $obj -> generate_pdf ;
2017-11-18 00:54:23 +01:00
2017-12-05 13:38:24 +01:00
// Multicurrency
$this -> fk_multicurrency = $obj -> fk_multicurrency ;
2019-11-12 09:46:08 +01:00
$this -> multicurrency_code = $obj -> multicurrency_code ;
2017-12-05 13:38:24 +01:00
$this -> multicurrency_tx = $obj -> multicurrency_tx ;
2019-11-12 09:46:08 +01:00
$this -> multicurrency_total_ht = $obj -> multicurrency_total_ht ;
2017-12-05 13:38:24 +01:00
$this -> multicurrency_total_tva = $obj -> multicurrency_total_tva ;
$this -> multicurrency_total_ttc = $obj -> multicurrency_total_ttc ;
2021-02-23 21:09:01 +01:00
if ( $this -> statut == self :: STATUS_DRAFT ) {
$this -> brouillon = 1 ;
}
2007-07-09 23:26:51 +02:00
2020-10-23 20:08:35 +02:00
// Retrieve all extrafield
2017-07-11 09:26:25 +02:00
// fetch optionals attributes and labels
2018-02-20 19:38:18 +01:00
$this -> fetch_optionals ();
2017-08-27 13:38:42 +02:00
2007-07-09 23:26:51 +02:00
/*
2009-05-27 03:15:44 +02:00
* Lines
*/
2019-11-12 09:46:08 +01:00
$result = $this -> fetch_lines ();
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> lasterror ();
2007-07-09 23:26:51 +02:00
return - 3 ;
}
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2021-06-18 00:59:18 +02:00
$this -> error = 'Bill with id ' . $rowid . ' or ref ' . $ref . ' not found' ;
2009-03-12 23:49:05 +01:00
dol_syslog ( 'Facture::Fetch Error ' . $this -> error , LOG_ERR );
2007-07-09 23:26:51 +02:00
return - 2 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> error ();
2007-07-09 23:26:51 +02:00
return - 1 ;
}
}
2016-04-26 22:00:41 +02:00
/**
* Create an array of invoice lines
*
* @ return int > 0 if OK , < 0 if KO
*/
2019-02-25 23:15:48 +01:00
public function getLinesArray ()
2016-04-26 22:00:41 +02:00
{
2020-10-27 19:46:07 +01:00
return $this -> fetch_lines ();
2016-04-26 22:00:41 +02:00
}
2017-06-09 09:25:15 +02:00
2020-10-27 19:46:07 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2007-07-09 23:26:51 +02:00
/**
2019-06-29 16:29:32 +02:00
* Get lines of template invoices into this -> lines
2011-12-16 16:31:18 +01:00
*
2018-08-13 17:26:32 +02:00
* @ return int 1 if OK , < 0 if KO
2020-10-27 19:46:07 +01:00
*/
2019-02-25 23:15:48 +01:00
public function fetch_lines ()
2007-07-09 23:26:51 +02:00
{
2020-10-27 19:46:07 +01:00
// phpcs:enable
2022-03-28 13:50:47 +02:00
2019-11-12 09:46:08 +01:00
$this -> lines = array ();
2016-04-26 22:00:41 +02:00
2020-10-23 20:08:35 +02:00
// Retrieve all extrafield for line
2017-12-05 13:38:24 +01:00
// fetch optionals attributes and labels
2022-03-28 13:50:47 +02:00
/* if ( ! is_object ( $extrafields )) {
2019-10-06 14:41:52 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php' ;
2019-11-12 09:46:08 +01:00
$extrafields = new ExtraFields ( $this -> db );
2019-10-06 14:41:52 +02:00
}
$extrafields -> fetch_name_optionals_label ( $this -> table_element_line , true );
2022-03-28 13:50:47 +02:00
*/
2017-12-05 13:38:24 +01:00
2022-03-28 21:12:57 +02:00
dol_syslog ( 'FactureRec::fetch_lines' , LOG_DEBUG );
2017-05-09 14:19:16 +02:00
$sql = 'SELECT l.rowid, l.fk_product, l.product_type, l.label as custom_label, l.description, l.product_type, l.price, l.qty, l.vat_src_code, l.tva_tx, ' ;
2019-11-12 09:46:08 +01:00
$sql .= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,' ;
2021-03-03 23:55:28 +01:00
$sql .= ' l.info_bits, l.date_start_fill, l.date_end_fill, l.total_ht, l.total_tva, l.total_ttc, l.fk_product_fournisseur_price, l.buy_price_ht as pa_ht,' ;
2019-11-12 09:46:08 +01:00
$sql .= ' l.rang, l.special_code,' ;
$sql .= ' l.fk_unit, l.fk_contract_line,' ;
$sql .= ' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,' ;
$sql .= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'facturedet_rec as l' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'product as p ON l.fk_product = p.rowid' ;
2021-08-27 23:36:06 +02:00
$sql .= ' WHERE l.fk_facture = ' . (( int ) $this -> id );
2019-11-12 09:46:08 +01:00
$sql .= ' ORDER BY l.rang' ;
2017-06-09 09:25:15 +02:00
2007-07-09 23:26:51 +02:00
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $result ) {
2007-07-09 23:26:51 +02:00
$num = $this -> db -> num_rows ( $result );
$i = 0 ;
2021-02-23 21:09:01 +01:00
while ( $i < $num ) {
2007-07-09 23:26:51 +02:00
$objp = $this -> db -> fetch_object ( $result );
2017-07-11 09:26:25 +02:00
$line = new FactureLigneRec ( $this -> db );
2011-02-11 20:32:46 +01:00
2019-11-12 09:46:08 +01:00
$line -> id = $objp -> rowid ;
$line -> rowid = $objp -> rowid ;
$line -> desc = $objp -> description ; // Description line
$line -> description = $objp -> description ; // Description line
$line -> ref = $objp -> product_ref ; // Ref product
$line -> product_ref = $objp -> product_ref ; // Ref product
$line -> libelle = $objp -> product_label ; // deprecated
$line -> product_label = $objp -> product_label ; // Label product
$line -> product_desc = $objp -> product_desc ; // Description product
2021-03-08 11:33:07 +01:00
$line -> product_type = $objp -> product_type ; // Type of line
2019-11-12 09:46:08 +01:00
$line -> fk_product_type = $objp -> fk_product_type ; // Type of product
2010-09-12 17:04:13 +02:00
$line -> qty = $objp -> qty ;
$line -> subprice = $objp -> subprice ;
2017-05-09 14:19:16 +02:00
2019-11-12 09:46:08 +01:00
$line -> label = $objp -> custom_label ; // @deprecated
2018-04-21 13:14:58 +02:00
2017-05-09 14:19:16 +02:00
$line -> vat_src_code = $objp -> vat_src_code ;
2010-09-12 17:04:13 +02:00
$line -> tva_tx = $objp -> tva_tx ;
2017-05-09 14:19:16 +02:00
$line -> localtax1_tx = $objp -> localtax1_tx ;
$line -> localtax2_tx = $objp -> localtax2_tx ;
$line -> localtax1_type = $objp -> localtax1_type ;
$line -> localtax2_type = $objp -> localtax2_type ;
2010-09-12 17:04:13 +02:00
$line -> remise_percent = $objp -> remise_percent ;
2021-03-08 11:33:07 +01:00
//$line->fk_remise_except = $objp->fk_remise_except;
2010-09-12 17:04:13 +02:00
$line -> fk_product = $objp -> fk_product ;
2018-03-12 12:27:40 +01:00
$line -> date_start_fill = $objp -> date_start_fill ;
$line -> date_end_fill = $objp -> date_end_fill ;
2010-09-12 17:04:13 +02:00
$line -> info_bits = $objp -> info_bits ;
$line -> total_ht = $objp -> total_ht ;
$line -> total_tva = $objp -> total_tva ;
$line -> total_ttc = $objp -> total_ttc ;
2021-03-03 23:55:28 +01:00
2020-12-16 11:07:34 +01:00
//$line->code_ventilation = $objp->fk_code_ventilation;
2021-03-03 23:55:28 +01:00
$line -> fk_product_fournisseur_price = $objp -> fk_product_fournisseur_price ;
2021-10-25 22:07:31 +02:00
$line -> fk_fournprice = $objp -> fk_product_fournisseur_price ; // For backward compatibility
2021-03-03 23:55:28 +01:00
$marginInfos = getMarginInfos ( $objp -> subprice , $objp -> remise_percent , $objp -> tva_tx , $objp -> localtax1_tx , $objp -> localtax2_tx , $objp -> fk_product_fournisseur_price , $objp -> pa_ht );
$line -> buyprice = $marginInfos [ 0 ];
2021-10-25 22:07:31 +02:00
$line -> pa_ht = $marginInfos [ 0 ]; // For backward compatibility
2019-05-20 14:41:47 +02:00
$line -> marge_tx = $marginInfos [ 1 ];
$line -> marque_tx = $marginInfos [ 2 ];
2019-11-12 09:46:08 +01:00
$line -> rang = $objp -> rang ;
$line -> special_code = $objp -> special_code ;
$line -> fk_unit = $objp -> fk_unit ;
2020-10-27 19:46:07 +01:00
$line -> fk_contract_line = $objp -> fk_contract_line ;
2017-06-09 09:25:15 +02:00
2007-07-09 23:26:51 +02:00
// Ne plus utiliser
2010-09-12 17:04:13 +02:00
$line -> price = $objp -> price ;
$line -> remise = $objp -> remise ;
2007-07-09 23:26:51 +02:00
2020-03-27 16:02:58 +01:00
$line -> fetch_optionals ();
2017-08-27 13:38:42 +02:00
2017-12-05 13:38:24 +01:00
// Multicurrency
2019-11-12 09:46:08 +01:00
$line -> fk_multicurrency = $objp -> fk_multicurrency ;
$line -> multicurrency_code = $objp -> multicurrency_code ;
2017-12-05 13:38:24 +01:00
$line -> multicurrency_subprice = $objp -> multicurrency_subprice ;
$line -> multicurrency_total_ht = $objp -> multicurrency_total_ht ;
$line -> multicurrency_total_tva = $objp -> multicurrency_total_tva ;
$line -> multicurrency_total_ttc = $objp -> multicurrency_total_ttc ;
2017-08-27 13:38:42 +02:00
2010-09-12 17:04:13 +02:00
$this -> lines [ $i ] = $line ;
2011-02-11 20:32:46 +01:00
2007-07-09 23:26:51 +02:00
$i ++ ;
}
2009-05-27 03:48:31 +02:00
2007-07-09 23:26:51 +02:00
$this -> db -> free ( $result );
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> lasterror ();
2007-07-09 23:26:51 +02:00
return - 3 ;
}
}
2005-06-26 00:54:02 +02:00
2003-10-12 20:11:19 +02:00
2009-05-27 03:15:44 +02:00
/**
2016-02-03 02:41:07 +01:00
* Delete template invoice
2012-01-10 09:57:16 +01:00
*
2016-09-28 17:56:54 +02:00
* @ param User $user User that delete .
2016-02-03 02:41:07 +01:00
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
* @ param int $idwarehouse Id warehouse to use for stock change .
* @ return int < 0 if KO , > 0 if OK
2009-05-27 03:15:44 +02:00
*/
2019-02-25 23:15:48 +01:00
public function delete ( User $user , $notrigger = 0 , $idwarehouse = - 1 )
2009-05-27 03:15:44 +02:00
{
2020-10-27 19:46:07 +01:00
$rowid = $this -> id ;
2017-06-09 09:25:15 +02:00
2021-04-25 15:55:36 +02:00
dol_syslog ( get_class ( $this ) . " ::delete rowid= " . (( int ) $rowid ), LOG_DEBUG );
2017-06-09 09:25:15 +02:00
2020-10-27 19:46:07 +01:00
$error = 0 ;
2016-02-03 02:41:07 +01:00
$this -> db -> begin ();
2017-06-09 09:25:15 +02:00
2020-05-02 03:39:56 +02:00
$main = MAIN_DB_PREFIX . 'facturedet_rec' ;
2020-10-27 19:46:07 +01:00
$ef = $main . " _extrafields " ;
2021-06-25 23:10:42 +02:00
$sqlef = " DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM " . $main . " WHERE fk_facture = " . (( int ) $rowid ) . " ) " ;
2021-03-30 17:53:25 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " facturedet_rec WHERE fk_facture = " . (( int ) $rowid );
2021-06-25 23:10:42 +02:00
2021-02-23 21:09:01 +01:00
if ( $this -> db -> query ( $sqlef ) && $this -> db -> query ( $sql )) {
2021-03-30 17:53:25 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " facture_rec WHERE rowid = " . (( int ) $rowid );
2011-03-08 12:13:14 +01:00
dol_syslog ( $sql );
2021-02-23 21:09:01 +01:00
if ( $this -> db -> query ( $sql )) {
2017-06-26 15:44:04 +02:00
// Delete linked object
$res = $this -> deleteObjectLinked ();
2021-02-23 21:09:01 +01:00
if ( $res < 0 ) {
$error = - 3 ;
}
2020-04-23 17:01:40 +02:00
// Delete extrafields
2020-10-27 19:46:07 +01:00
$res = $this -> deleteExtraFields ();
2021-02-23 21:09:01 +01:00
if ( $res < 0 ) {
$error = - 4 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> lasterror ();
$error = - 1 ;
2009-05-27 03:15:44 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> lasterror ();
$error = - 2 ;
2016-02-03 02:41:07 +01:00
}
2022-04-01 19:07:17 +02:00
if ( ! $error && ! $notrigger ) {
// Call trigger
$result = $this -> call_trigger ( 'BILLREC_DELETE' , $user );
if ( $result < 0 ) {
$error ++ ;
}
// End call triggers
}
2021-02-23 21:09:01 +01:00
if ( ! $error ) {
2020-10-27 19:46:07 +01:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2020-10-27 19:46:07 +01:00
$this -> db -> rollback ();
return $error ;
2009-05-27 03:15:44 +02:00
}
}
2005-06-26 00:54:02 +02:00
2009-02-04 21:38:07 +01:00
2006-07-02 16:57:43 +02:00
/**
2012-01-10 09:57:16 +01:00
* Add a line to invoice
*
2020-10-27 19:46:07 +01:00
* @ param string $desc Description de la ligne
* @ param double $pu_ht Prix unitaire HT ( > 0 even for credit note )
* @ param double $qty Quantite
* @ param double $txtva Taux de tva force , sinon - 1
2017-05-09 14:59:37 +02:00
* @ param double $txlocaltax1 Local tax 1 rate ( deprecated )
* @ param double $txlocaltax2 Local tax 2 rate ( deprecated )
2020-10-27 19:46:07 +01:00
* @ param int $fk_product Product / Service ID predefined
* @ param double $remise_percent Percentage discount of the line
* @ param string $price_base_type HT or TTC
* @ param int $info_bits VAT npr or not ?
* @ param int $fk_remise_except Id remise
* @ param double $pu_ttc Prix unitaire TTC ( > 0 even for credit note )
* @ param int $type Type of line ( 0 = product , 1 = service )
* @ param int $rang Position of line
* @ param int $special_code Special code
* @ param string $label Label of the line
* @ param string $fk_unit Unit
2017-12-05 13:38:24 +01:00
* @ param double $pu_ht_devise Unit price in currency
2018-03-12 12:27:40 +01:00
* @ param int $date_start_fill 1 = Flag to fill start date when generating invoice
* @ param int $date_end_fill 1 = Flag to fill end date when generating invoice
2019-05-20 14:41:47 +02:00
* @ param int $fk_fournprice Supplier price id ( to calculate margin ) or ''
* @ param int $pa_ht Buying price of line ( to calculate margin ) or ''
2020-10-27 19:46:07 +01:00
* @ return int < 0 if KO , Id of line if OK
2006-07-02 16:57:43 +02:00
*/
2019-05-20 14:41:47 +02:00
public function addline ( $desc , $pu_ht , $qty , $txtva , $txlocaltax1 = 0 , $txlocaltax2 = 0 , $fk_product = 0 , $remise_percent = 0 , $price_base_type = 'HT' , $info_bits = 0 , $fk_remise_except = '' , $pu_ttc = 0 , $type = 0 , $rang = - 1 , $special_code = 0 , $label = '' , $fk_unit = null , $pu_ht_devise = 0 , $date_start_fill = 0 , $date_end_fill = 0 , $fk_fournprice = null , $pa_ht = 0 )
2006-07-02 16:57:43 +02:00
{
2020-10-27 19:46:07 +01:00
global $mysoc ;
2017-06-09 09:25:15 +02:00
2019-11-12 09:46:08 +01:00
$facid = $this -> id ;
2013-07-10 17:29:54 +02:00
2019-05-20 14:41:47 +02:00
dol_syslog ( get_class ( $this ) . " ::addline facid= $facid ,desc= $desc ,pu_ht= $pu_ht ,qty= $qty ,txtva= $txtva ,txlocaltax1= $txlocaltax1 ,txlocaltax2= $txlocaltax2 ,fk_product= $fk_product ,remise_percent= $remise_percent ,info_bits= $info_bits ,fk_remise_except= $fk_remise_except ,price_base_type= $price_base_type ,pu_ttc= $pu_ttc ,type= $type ,fk_unit= $fk_unit ,pu_ht_devise= $pu_ht_devise ,date_start_fill= $date_start_fill ,date_end_fill= $date_end_fill ,pa_ht= $pa_ht " , LOG_DEBUG );
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/price.lib.php' ;
2009-02-04 21:38:07 +01:00
2009-05-27 03:15:44 +02:00
// Check parameters
2021-02-23 21:09:01 +01:00
if ( $type < 0 ) {
return - 1 ;
}
2009-05-27 03:15:44 +02:00
2019-11-12 09:46:08 +01:00
$localtaxes_type = getLocalTaxesFromRate ( $txtva , 0 , $this -> thirdparty , $mysoc );
2017-05-09 14:19:16 +02:00
// Clean vat code
2021-03-08 09:14:23 +01:00
$reg = array ();
2019-11-12 09:46:08 +01:00
$vat_src_code = '' ;
2021-02-23 21:09:01 +01:00
if ( preg_match ( '/\((.*)\)/' , $txtva , $reg )) {
2017-05-09 14:19:16 +02:00
$vat_src_code = $reg [ 1 ];
2019-11-12 09:46:08 +01:00
$txtva = preg_replace ( '/\s*\(.*\)/' , '' , $txtva ); // Remove code into vatrate.
2017-05-09 14:19:16 +02:00
}
2021-02-23 21:09:01 +01:00
if ( $this -> brouillon ) {
2009-05-27 03:15:44 +02:00
// Clean parameters
2019-11-12 09:46:08 +01:00
$remise_percent = price2num ( $remise_percent );
2021-02-23 21:09:01 +01:00
if ( empty ( $remise_percent )) {
$remise_percent = 0 ;
}
2019-11-12 09:46:08 +01:00
$qty = price2num ( $qty );
2017-09-11 01:04:53 +02:00
$pu_ht = price2num ( $pu_ht );
$pu_ttc = price2num ( $pu_ttc );
2021-01-02 16:51:34 +01:00
if ( ! preg_match ( '/\((.*)\)/' , $txtva )) {
$txtva = price2num ( $txtva ); // $txtva can have format '5.0(XXX)' or '5'
}
2017-09-11 01:04:53 +02:00
$txlocaltax1 = price2num ( $txlocaltax1 );
$txlocaltax2 = price2num ( $txlocaltax2 );
2021-02-23 21:09:01 +01:00
if ( empty ( $txtva )) {
$txtva = 0 ;
}
if ( empty ( $txlocaltax1 )) {
$txlocaltax1 = 0 ;
}
if ( empty ( $txlocaltax2 )) {
$txlocaltax2 = 0 ;
}
if ( empty ( $info_bits )) {
$info_bits = 0 ;
}
2009-05-27 03:15:44 +02:00
2021-02-23 21:09:01 +01:00
if ( $price_base_type == 'HT' ) {
2019-11-12 09:46:08 +01:00
$pu = $pu_ht ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-12 09:46:08 +01:00
$pu = $pu_ttc ;
2006-07-02 16:57:43 +02:00
}
2009-02-04 21:38:07 +01:00
2006-09-14 21:34:25 +02:00
// Calcul du total TTC et de la TVA pour la ligne a partir de
// qty, pu, remise_percent et txtva
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
// la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
2017-05-09 14:19:16 +02:00
2019-11-12 09:46:08 +01:00
$tabprice = calcul_price_total ( $qty , $pu , $remise_percent , $txtva , $txlocaltax1 , $txlocaltax2 , 0 , $price_base_type , $info_bits , $type , $mysoc , $localtaxes_type , 100 , $this -> multicurrency_tx , $pu_ht_devise );
2006-09-14 21:34:25 +02:00
$total_ht = $tabprice [ 0 ];
$total_tva = $tabprice [ 1 ];
$total_ttc = $tabprice [ 2 ];
2019-11-12 09:46:08 +01:00
$total_localtax1 = $tabprice [ 9 ];
$total_localtax2 = $tabprice [ 10 ];
2017-12-05 13:38:24 +01:00
$pu_ht = $tabprice [ 3 ];
// MultiCurrency
$multicurrency_total_ht = $tabprice [ 16 ];
$multicurrency_total_tva = $tabprice [ 17 ];
$multicurrency_total_ttc = $tabprice [ 18 ];
$pu_ht_devise = $tabprice [ 19 ];
2017-06-09 09:25:15 +02:00
2019-11-12 09:46:08 +01:00
$product_type = $type ;
2021-02-23 21:09:01 +01:00
if ( $fk_product ) {
2019-11-12 09:46:08 +01:00
$product = new Product ( $this -> db );
$result = $product -> fetch ( $fk_product );
$product_type = $product -> type ;
2009-05-27 03:15:44 +02:00
}
2022-05-03 14:05:02 +02:00
// Rank to use
$ranktouse = $rang ;
if ( $ranktouse == - 1 ) {
$rangmax = $this -> line_max ( 0 );
$ranktouse = $rangmax + 1 ;
}
2009-11-19 12:25:08 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " facturedet_rec ( " ;
2019-11-12 09:46:08 +01:00
$sql .= " fk_facture " ;
$sql .= " , label " ;
$sql .= " , description " ;
$sql .= " , price " ;
$sql .= " , qty " ;
$sql .= " , tva_tx " ;
$sql .= " , vat_src_code " ;
$sql .= " , localtax1_tx " ;
$sql .= " , localtax1_type " ;
$sql .= " , localtax2_tx " ;
$sql .= " , localtax2_type " ;
$sql .= " , fk_product " ;
$sql .= " , product_type " ;
$sql .= " , remise_percent " ;
$sql .= " , subprice " ;
$sql .= " , remise " ;
$sql .= " , total_ht " ;
$sql .= " , total_tva " ;
$sql .= " , total_localtax1 " ;
$sql .= " , total_localtax2 " ;
$sql .= " , total_ttc " ;
$sql .= " , date_start_fill " ;
$sql .= " , date_end_fill " ;
$sql .= " , fk_product_fournisseur_price " ;
$sql .= " , buy_price_ht " ;
$sql .= " , info_bits " ;
$sql .= " , rang " ;
$sql .= " , special_code " ;
$sql .= " , fk_unit " ;
$sql .= ', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc' ;
$sql .= " ) VALUES ( " ;
2020-09-19 23:30:29 +02:00
$sql .= " " . (( int ) $facid );
2019-11-12 09:46:08 +01:00
$sql .= " , " . ( ! empty ( $label ) ? " ' " . $this -> db -> escape ( $label ) . " ' " : " null " );
$sql .= " , ' " . $this -> db -> escape ( $desc ) . " ' " ;
$sql .= " , " . price2num ( $pu_ht );
$sql .= " , " . price2num ( $qty );
$sql .= " , " . price2num ( $txtva );
$sql .= " , ' " . $this -> db -> escape ( $vat_src_code ) . " ' " ;
$sql .= " , " . price2num ( $txlocaltax1 );
2020-12-18 14:40:45 +01:00
$sql .= " , ' " . $this -> db -> escape ( isset ( $localtaxes_type [ 0 ]) ? $localtaxes_type [ 0 ] : '' ) . " ' " ;
2019-11-12 09:46:08 +01:00
$sql .= " , " . price2num ( $txlocaltax2 );
2020-12-18 14:40:45 +01:00
$sql .= " , ' " . $this -> db -> escape ( isset ( $localtaxes_type [ 2 ]) ? $localtaxes_type [ 2 ] : '' ) . " ' " ;
2020-09-19 23:30:29 +02:00
$sql .= " , " . ( ! empty ( $fk_product ) ? " ' " . $this -> db -> escape ( $fk_product ) . " ' " : " null " );
2021-09-03 21:25:17 +02:00
$sql .= " , " . (( int ) $product_type );
2019-11-12 09:46:08 +01:00
$sql .= " , " . price2num ( $remise_percent );
$sql .= " , " . price2num ( $pu_ht );
$sql .= " , null " ;
$sql .= " , " . price2num ( $total_ht );
$sql .= " , " . price2num ( $total_tva );
$sql .= " , " . price2num ( $total_localtax1 );
$sql .= " , " . price2num ( $total_localtax2 );
$sql .= " , " . price2num ( $total_ttc );
$sql .= " , " . ( int ) $date_start_fill ;
$sql .= " , " . ( int ) $date_end_fill ;
$sql .= " , " . ( $fk_fournprice > 0 ? $fk_fournprice : 'null' );
$sql .= " , " . ( $pa_ht ? price2num ( $pa_ht ) : 0 );
2021-09-03 21:25:17 +02:00
$sql .= " , " . (( int ) $info_bits );
2022-05-03 14:05:02 +02:00
$sql .= " , " . (( int ) $ranktouse );
2021-09-03 21:25:17 +02:00
$sql .= " , " . (( int ) $special_code );
$sql .= " , " . ( $fk_unit ? (( int ) $fk_unit ) : " null " );
2019-11-12 09:46:08 +01:00
$sql .= " , " . ( int ) $this -> fk_multicurrency ;
$sql .= " , ' " . $this -> db -> escape ( $this -> multicurrency_code ) . " ' " ;
2021-09-03 21:25:17 +02:00
$sql .= " , " . price2num ( $pu_ht_devise , 'CU' );
$sql .= " , " . price2num ( $multicurrency_total_ht , 'CT' );
$sql .= " , " . price2num ( $multicurrency_total_tva , 'CT' );
$sql .= " , " . price2num ( $multicurrency_total_ttc , 'CT' );
2019-11-12 09:46:08 +01:00
$sql .= " ) " ;
2009-02-04 21:38:07 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::addline " , LOG_DEBUG );
2021-02-23 21:09:01 +01:00
if ( $this -> db -> query ( $sql )) {
2017-07-11 09:26:25 +02:00
$lineId = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " facturedet_rec " );
2019-11-12 09:46:08 +01:00
$this -> id = $facid ;
2008-03-07 11:34:16 +01:00
$this -> update_price ();
2017-07-11 09:26:25 +02:00
return $lineId ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> lasterror ();
2006-07-02 16:57:43 +02:00
return - 1 ;
}
}
}
2009-02-04 21:38:07 +01:00
2016-06-28 17:51:29 +02:00
/**
* Update a line to invoice
*
* @ param int $rowid Id of line to update
* @ param string $desc Description de la ligne
* @ param double $pu_ht Prix unitaire HT ( > 0 even for credit note )
* @ param double $qty Quantite
* @ param double $txtva Taux de tva force , sinon - 1
2017-05-09 14:59:37 +02:00
* @ param double $txlocaltax1 Local tax 1 rate ( deprecated )
* @ param double $txlocaltax2 Local tax 2 rate ( deprecated )
2019-03-18 12:46:32 +01:00
* @ param int $fk_product Product / Service ID predefined
2019-03-18 13:45:24 +01:00
* @ param double $remise_percent Percentage discount of the line
2016-06-28 17:51:29 +02:00
* @ param string $price_base_type HT or TTC
2019-06-29 16:29:32 +02:00
* @ param int $info_bits Bits of type of lines
2016-06-28 17:51:29 +02:00
* @ param int $fk_remise_except Id remise
* @ param double $pu_ttc Prix unitaire TTC ( > 0 even for credit note )
* @ param int $type Type of line ( 0 = product , 1 = service )
* @ param int $rang Position of line
* @ param int $special_code Special code
* @ param string $label Label of the line
* @ param string $fk_unit Unit
2017-12-05 13:38:24 +01:00
* @ param double $pu_ht_devise Unit price in currency
* @ param int $notrigger disable line update trigger
2018-03-12 12:27:40 +01:00
* @ param int $date_start_fill 1 = Flag to fill start date when generating invoice
* @ param int $date_end_fill 1 = Flag to fill end date when generating invoice
2019-05-20 14:41:47 +02:00
* @ param int $fk_fournprice Id of origin supplier price
2019-11-03 18:58:26 +01:00
* @ param int $pa_ht Price ( without tax ) of product for margin calculation
2016-06-28 17:51:29 +02:00
* @ return int < 0 if KO , Id of line if OK
*/
2019-05-20 14:41:47 +02:00
public function updateline ( $rowid , $desc , $pu_ht , $qty , $txtva , $txlocaltax1 = 0 , $txlocaltax2 = 0 , $fk_product = 0 , $remise_percent = 0 , $price_base_type = 'HT' , $info_bits = 0 , $fk_remise_except = '' , $pu_ttc = 0 , $type = 0 , $rang = - 1 , $special_code = 0 , $label = '' , $fk_unit = null , $pu_ht_devise = 0 , $notrigger = 0 , $date_start_fill = 0 , $date_end_fill = 0 , $fk_fournprice = null , $pa_ht = 0 )
2016-06-28 17:51:29 +02:00
{
2020-10-27 19:46:07 +01:00
global $mysoc ;
$facid = $this -> id ;
dol_syslog ( get_class ( $this ) . " ::updateline facid= " . $facid . " rowid= $rowid , desc= $desc , pu_ht= $pu_ht , qty= $qty , txtva= $txtva , txlocaltax1= $txlocaltax1 , txlocaltax2= $txlocaltax2 , fk_product= $fk_product , remise_percent= $remise_percent , info_bits= $info_bits , fk_remise_except= $fk_remise_except , price_base_type= $price_base_type , pu_ttc= $pu_ttc , type= $type , fk_unit= $fk_unit , pu_ht_devise= $pu_ht_devise " , LOG_DEBUG );
include_once DOL_DOCUMENT_ROOT . '/core/lib/price.lib.php' ;
// Clean parameters
2021-02-23 21:09:01 +01:00
if ( empty ( $remise_percent )) {
$remise_percent = 0 ;
}
2020-10-27 19:46:07 +01:00
// Check parameters
2021-02-23 21:09:01 +01:00
if ( $type < 0 ) {
return - 1 ;
}
2020-10-27 19:46:07 +01:00
2021-02-23 21:09:01 +01:00
if ( $this -> brouillon ) {
2020-10-27 19:46:07 +01:00
// Clean parameters
$remise_percent = price2num ( $remise_percent );
$qty = price2num ( $qty );
2021-02-23 21:09:01 +01:00
if ( empty ( $info_bits )) {
$info_bits = 0 ;
}
2020-10-27 19:46:07 +01:00
$pu_ht = price2num ( $pu_ht );
$pu_ttc = price2num ( $pu_ttc );
$pu_ht_devise = price2num ( $pu_ht_devise );
2021-02-23 21:09:01 +01:00
if ( ! preg_match ( '/\((.*)\)/' , $txtva )) {
$txtva = price2num ( $txtva ); // $txtva can have format '5.0(XXX)' or '5'
}
2020-10-27 19:46:07 +01:00
$txlocaltax1 = price2num ( $txlocaltax1 );
$txlocaltax2 = price2num ( $txlocaltax2 );
2021-02-23 21:09:01 +01:00
if ( empty ( $txlocaltax1 )) {
$txlocaltax1 = 0 ;
}
if ( empty ( $txlocaltax2 )) {
$txlocaltax2 = 0 ;
}
2020-10-27 19:46:07 +01:00
2021-02-23 21:09:01 +01:00
if ( empty ( $this -> multicurrency_subprice )) {
$this -> multicurrency_subprice = 0 ;
}
if ( empty ( $this -> multicurrency_total_ht )) {
$this -> multicurrency_total_ht = 0 ;
}
if ( empty ( $this -> multicurrency_total_tva )) {
$this -> multicurrency_total_tva = 0 ;
}
if ( empty ( $this -> multicurrency_total_ttc )) {
$this -> multicurrency_total_ttc = 0 ;
}
2020-10-27 19:46:07 +01:00
2021-02-23 21:09:01 +01:00
if ( $price_base_type == 'HT' ) {
2020-10-27 19:46:07 +01:00
$pu = $pu_ht ;
} else {
$pu = $pu_ttc ;
}
// Calculate total with, without tax and tax from qty, pu, remise_percent and txtva
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
// la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
$localtaxes_type = getLocalTaxesFromRate ( $txtva , 0 , $this -> thirdparty , $mysoc );
// Clean vat code
$vat_src_code = '' ;
2021-02-23 21:09:01 +01:00
$reg = array ();
if ( preg_match ( '/\((.*)\)/' , $txtva , $reg )) {
2020-10-27 19:46:07 +01:00
$vat_src_code = $reg [ 1 ];
$txtva = preg_replace ( '/\s*\(.*\)/' , '' , $txtva ); // Remove code into vatrate.
}
$tabprice = calcul_price_total ( $qty , $pu , $remise_percent , $txtva , $txlocaltax1 , $txlocaltax2 , 0 , $price_base_type , $info_bits , $type , $mysoc , $localtaxes_type , 100 , $this -> multicurrency_tx , $pu_ht_devise );
$total_ht = $tabprice [ 0 ];
$total_tva = $tabprice [ 1 ];
$total_ttc = $tabprice [ 2 ];
$total_localtax1 = $tabprice [ 9 ];
$total_localtax2 = $tabprice [ 10 ];
$pu_ht = $tabprice [ 3 ];
$pu_tva = $tabprice [ 4 ];
$pu_ttc = $tabprice [ 5 ];
// MultiCurrency
$multicurrency_total_ht = $tabprice [ 16 ];
$multicurrency_total_tva = $tabprice [ 17 ];
$multicurrency_total_ttc = $tabprice [ 18 ];
$pu_ht_devise = $tabprice [ 19 ];
$product_type = $type ;
2021-02-23 21:09:01 +01:00
if ( $fk_product ) {
2020-10-27 19:46:07 +01:00
$product = new Product ( $this -> db );
$result = $product -> fetch ( $fk_product );
$product_type = $product -> type ;
}
$sql = " UPDATE " . MAIN_DB_PREFIX . " facturedet_rec SET " ;
$sql .= " fk_facture = " . (( int ) $facid );
$sql .= " , label= " . ( ! empty ( $label ) ? " ' " . $this -> db -> escape ( $label ) . " ' " : " null " );
$sql .= " , description=' " . $this -> db -> escape ( $desc ) . " ' " ;
$sql .= " , price= " . price2num ( $pu_ht );
$sql .= " , qty= " . price2num ( $qty );
$sql .= " , tva_tx= " . price2num ( $txtva );
$sql .= " , vat_src_code=' " . $this -> db -> escape ( $vat_src_code ) . " ' " ;
2021-06-09 15:36:47 +02:00
$sql .= " , localtax1_tx= " . (( float ) $txlocaltax1 );
2020-10-27 19:46:07 +01:00
$sql .= " , localtax1_type=' " . $this -> db -> escape ( $localtaxes_type [ 0 ]) . " ' " ;
2021-06-09 15:36:47 +02:00
$sql .= " , localtax2_tx= " . (( float ) $txlocaltax2 );
2020-10-27 19:46:07 +01:00
$sql .= " , localtax2_type=' " . $this -> db -> escape ( $localtaxes_type [ 2 ]) . " ' " ;
$sql .= " , fk_product= " . ( ! empty ( $fk_product ) ? " ' " . $this -> db -> escape ( $fk_product ) . " ' " : " null " );
2021-06-09 15:36:47 +02:00
$sql .= " , product_type= " . (( int ) $product_type );
2020-10-27 19:46:07 +01:00
$sql .= " , remise_percent=' " . price2num ( $remise_percent ) . " ' " ;
$sql .= " , subprice=' " . price2num ( $pu_ht ) . " ' " ;
$sql .= " , total_ht=' " . price2num ( $total_ht ) . " ' " ;
$sql .= " , total_tva=' " . price2num ( $total_tva ) . " ' " ;
$sql .= " , total_localtax1=' " . price2num ( $total_localtax1 ) . " ' " ;
$sql .= " , total_localtax2=' " . price2num ( $total_localtax2 ) . " ' " ;
$sql .= " , total_ttc=' " . price2num ( $total_ttc ) . " ' " ;
$sql .= " , date_start_fill= " . (( int ) $date_start_fill );
$sql .= " , date_end_fill= " . (( int ) $date_end_fill );
$sql .= " , fk_product_fournisseur_price= " . ( $fk_fournprice > 0 ? $fk_fournprice : 'null' );
$sql .= " , buy_price_ht= " . ( $pa_ht ? price2num ( $pa_ht ) : 0 );
2021-04-26 19:12:23 +02:00
$sql .= " , info_bits= " . (( int ) $info_bits );
$sql .= " , rang= " . (( int ) $rang );
$sql .= " , special_code= " . (( int ) $special_code );
2020-10-27 19:46:07 +01:00
$sql .= " , fk_unit= " . ( $fk_unit ? " ' " . $this -> db -> escape ( $fk_unit ) . " ' " : " null " );
2021-04-26 19:12:23 +02:00
$sql .= ', multicurrency_subprice = ' . price2num ( $pu_ht_devise );
$sql .= ', multicurrency_total_ht = ' . price2num ( $multicurrency_total_ht );
$sql .= ', multicurrency_total_tva = ' . price2num ( $multicurrency_total_tva );
$sql .= ', multicurrency_total_ttc = ' . price2num ( $multicurrency_total_ttc );
2021-03-30 17:53:25 +02:00
$sql .= " WHERE rowid = " . (( int ) $rowid );
2020-10-27 19:46:07 +01:00
dol_syslog ( get_class ( $this ) . " ::updateline " , LOG_DEBUG );
2021-02-23 21:09:01 +01:00
if ( $this -> db -> query ( $sql )) {
2020-10-27 19:46:07 +01:00
$this -> id = $facid ;
$this -> update_price ();
return 1 ;
} else {
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
2017-06-09 09:25:15 +02:00
}
2016-02-11 09:50:35 +01:00
/**
2017-06-09 09:25:15 +02:00
* Return the next date of
*
2019-02-12 23:54:38 +01:00
* @ return int | false false if KO , timestamp if OK
2016-02-11 09:50:35 +01:00
*/
2019-02-25 23:15:48 +01:00
public function getNextDate ()
2016-02-11 09:50:35 +01:00
{
2021-02-23 21:09:01 +01:00
if ( empty ( $this -> date_when )) {
return false ;
}
2016-02-16 00:31:05 +01:00
return dol_time_plus_duree ( $this -> date_when , $this -> frequency , $this -> unit_frequency );
2016-02-11 09:50:35 +01:00
}
2017-06-09 09:25:15 +02:00
2017-10-09 14:16:22 +02:00
/**
* Return if maximum number of generation is reached
*
* @ return boolean False by default , True if maximum number of generation is reached
*/
2019-02-25 23:15:48 +01:00
public function isMaxNbGenReached ()
2017-10-09 14:16:22 +02:00
{
$ret = false ;
2021-02-23 21:09:01 +01:00
if ( $this -> nb_gen_max > 0 && ( $this -> nb_gen_done >= $this -> nb_gen_max )) {
$ret = true ;
}
2017-10-09 14:16:22 +02:00
return $ret ;
}
/**
* Format string to output with by striking the string if max number of generation was reached
*
* @ param string $ret Default value to output
* @ return boolean False by default , True if maximum number of generation is reached
*/
2019-02-25 23:15:48 +01:00
public function strikeIfMaxNbGenReached ( $ret )
2017-10-09 14:16:22 +02:00
{
// Special case to strike the date
2019-11-12 09:46:08 +01:00
return ( $this -> isMaxNbGenReached () ? '<strike>' : '' ) . $ret . ( $this -> isMaxNbGenReached () ? '</strike>' : '' );
2017-10-09 14:16:22 +02:00
}
2016-02-11 09:50:35 +01:00
/**
2016-09-10 05:26:31 +02:00
* Create all recurrents invoices ( for all entities if multicompany is used ) .
* A result may also be provided into this -> output .
2017-06-09 09:25:15 +02:00
*
2018-03-12 12:27:40 +01:00
* WARNING : This method change temporarly context $conf -> entity to be in correct context for each recurring invoice found .
2017-06-09 09:25:15 +02:00
*
2018-08-17 19:56:29 +02:00
* @ param int $restrictioninvoiceid 0 = All qualified template invoices found . > 0 = restrict action on invoice ID
2018-05-16 20:41:01 +02:00
* @ param int $forcevalidation 1 = Force validation of invoice whatever is template auto_validate flag .
2022-05-13 11:29:48 +02:00
* @ param int $notrigger Disable the trigger
2018-05-16 20:41:01 +02:00
* @ return int 0 if OK , < 0 if KO ( this function is used also by cron so only 0 is OK )
2016-02-11 09:50:35 +01:00
*/
2022-05-13 11:29:48 +02:00
public function createRecurringInvoices ( $restrictioninvoiceid = 0 , $forcevalidation = 0 , $notrigger = 0 )
2016-02-11 09:50:35 +01:00
{
2018-08-17 20:39:46 +02:00
global $conf , $langs , $db , $user , $hookmanager ;
2017-06-09 09:25:15 +02:00
2019-11-12 09:46:08 +01:00
$error = 0 ;
$nb_create = 0 ;
2018-05-16 15:23:52 +02:00
2018-09-09 11:10:32 +02:00
// Load translation files required by the page
2019-11-12 09:46:08 +01:00
$langs -> loadLangs ( array ( " main " , " bills " ));
2017-06-09 09:25:15 +02:00
2016-02-16 00:31:05 +01:00
$now = dol_now ();
2019-11-12 09:46:08 +01:00
$tmparray = dol_getdate ( $now );
$today = dol_mktime ( 23 , 59 , 59 , $tmparray [ 'mon' ], $tmparray [ 'mday' ], $tmparray [ 'year' ]); // Today is last second of current day
2017-06-09 09:25:15 +02:00
2022-10-15 13:19:55 +02:00
$this -> output = null ;
2018-08-17 19:56:29 +02:00
dol_syslog ( " createRecurringInvoices restrictioninvoiceid= " . $restrictioninvoiceid . " forcevalidation= " . $forcevalidation );
2018-05-16 20:41:01 +02:00
2016-02-11 09:50:35 +01:00
$sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . 'facture_rec' ;
2019-11-12 09:46:08 +01:00
$sql .= ' WHERE frequency > 0' ; // A recurring invoice is an invoice with a frequency
2020-09-19 23:30:29 +02:00
$sql .= " AND (date_when IS NULL OR date_when <= ' " . $this -> db -> idate ( $today ) . " ') " ;
2019-11-12 09:46:08 +01:00
$sql .= ' AND (nb_gen_done < nb_gen_max OR nb_gen_max = 0)' ;
$sql .= ' AND suspended = 0' ;
$sql .= ' AND entity = ' . $conf -> entity ; // MUST STAY = $conf->entity here
2021-02-23 21:09:01 +01:00
if ( $restrictioninvoiceid > 0 ) {
2021-04-26 19:12:23 +02:00
$sql .= ' AND rowid = ' . (( int ) $restrictioninvoiceid );
2021-02-23 21:09:01 +01:00
}
2020-09-19 23:30:29 +02:00
$sql .= $this -> db -> order ( 'entity' , 'ASC' );
2016-02-16 00:31:05 +01:00
//print $sql;exit;
2018-08-17 20:39:46 +02:00
$parameters = array (
'restrictioninvoiceid' => $restrictioninvoiceid ,
'forcevalidation' => $forcevalidation ,
2019-03-02 08:43:09 +01:00
);
2018-12-14 16:09:12 +01:00
$reshook = $hookmanager -> executeHooks ( 'beforeCreationOfRecurringInvoices' , $parameters , $sql ); // note that $sql might be modified by hooks
2017-06-09 09:25:15 +02:00
2020-09-19 23:30:29 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
2019-11-12 09:46:08 +01:00
$i = 0 ;
2020-09-19 23:30:29 +02:00
$num = $this -> db -> num_rows ( $resql );
2017-06-09 09:25:15 +02:00
2021-02-23 21:09:01 +01:00
if ( $num ) {
2019-11-12 09:46:08 +01:00
$this -> output .= $langs -> trans ( " FoundXQualifiedRecurringInvoiceTemplate " , $num ) . " \n " ;
2021-02-23 21:09:01 +01:00
} else {
$this -> output .= $langs -> trans ( " NoQualifiedRecurringInvoiceTemplateFound " );
}
2017-06-09 09:25:15 +02:00
2018-08-17 20:47:01 +02:00
$saventity = $conf -> entity ;
2017-06-09 09:25:15 +02:00
2021-02-23 21:09:01 +01:00
while ( $i < $num ) { // Loop on each template invoice. If $num = 0, test is false at first pass.
2020-09-19 23:30:29 +02:00
$line = $this -> db -> fetch_object ( $resql );
2016-02-16 00:31:05 +01:00
2020-09-19 23:30:29 +02:00
$this -> db -> begin ();
2017-06-09 09:25:15 +02:00
2018-08-17 20:47:01 +02:00
$invoiceidgenerated = 0 ;
2018-05-16 15:23:52 +02:00
2018-08-17 20:39:46 +02:00
$facture = null ;
2020-09-19 23:30:29 +02:00
$facturerec = new FactureRec ( $this -> db );
2016-02-11 09:50:35 +01:00
$facturerec -> fetch ( $line -> rowid );
2017-06-09 09:25:15 +02:00
2021-02-23 21:09:01 +01:00
if ( $facturerec -> id > 0 ) {
2018-04-16 14:18:38 +02:00
// Set entity context
$conf -> entity = $facturerec -> entity ;
dol_syslog ( " createRecurringInvoices Process invoice template id= " . $facturerec -> id . " , ref= " . $facturerec -> ref . " , entity= " . $facturerec -> entity );
2020-09-19 23:30:29 +02:00
$facture = new Facture ( $this -> db );
2019-11-12 09:46:08 +01:00
$facture -> fac_rec = $facturerec -> id ; // We will create $facture from this recurring invoice
$facture -> fk_fac_rec_source = $facturerec -> id ; // We will create $facture from this recurring invoice
2018-04-16 14:18:38 +02:00
2018-08-17 20:47:01 +02:00
$facture -> type = self :: TYPE_STANDARD ;
$facture -> brouillon = 1 ;
2021-03-08 09:14:23 +01:00
$facture -> statut = self :: STATUS_DRAFT ;
$facture -> status = self :: STATUS_DRAFT ;
2019-11-12 09:46:08 +01:00
$facture -> date = ( empty ( $facturerec -> date_when ) ? $now : $facturerec -> date_when ); // We could also use dol_now here but we prefer date_when so invoice has real date when we would like even if we generate later.
2018-08-17 20:47:01 +02:00
$facture -> socid = $facturerec -> socid ;
2022-05-21 16:43:30 +02:00
if ( ! empty ( $facturerec -> fk_multicurrency )) {
2022-05-21 16:42:27 +02:00
$facture -> fk_multicurrency = $facturerec -> fk_multicurrency ;
$facture -> multicurrency_code = $facturerec -> multicurrency_code ;
$facture -> multicurrency_tx = $facturerec -> multicurrency_tx ;
}
2018-08-17 20:47:01 +02:00
$invoiceidgenerated = $facture -> create ( $user );
2021-02-23 21:09:01 +01:00
if ( $invoiceidgenerated <= 0 ) {
2018-12-03 17:03:28 +01:00
$this -> errors = $facture -> errors ;
2018-08-17 20:47:01 +02:00
$this -> error = $facture -> error ;
$error ++ ;
}
2022-05-13 11:29:48 +02:00
2021-02-23 21:09:01 +01:00
if ( ! $error && ( $facturerec -> auto_validate || $forcevalidation )) {
2018-08-17 20:47:01 +02:00
$result = $facture -> validate ( $user );
2021-02-23 21:09:01 +01:00
if ( $result <= 0 ) {
2018-12-03 17:03:28 +01:00
$this -> errors = $facture -> errors ;
2018-08-17 20:47:01 +02:00
$this -> error = $facture -> error ;
$error ++ ;
}
}
2021-02-23 21:09:01 +01:00
if ( ! $error && $facturerec -> generate_pdf ) {
2018-08-17 20:47:01 +02:00
// We refresh the object in order to have all necessary data (like date_lim_reglement)
$facture -> fetch ( $facture -> id );
2021-02-08 18:30:09 +01:00
$result = $facture -> generateDocument ( $facturerec -> model_pdf , $langs );
if ( $result <= 0 ) {
2018-12-03 17:03:28 +01:00
$this -> errors = $facture -> errors ;
2018-08-17 20:47:01 +02:00
$this -> error = $facture -> error ;
$error ++ ;
}
}
2022-05-13 11:29:48 +02:00
if ( ! $error && ! $notrigger ) {
// Call trigger
$result = $facturerec -> call_trigger ( 'BILLREC_CREATEBILL' , $user );
if ( $result < 0 ) {
$this -> errors = $facturerec -> errors ;
$this -> error = $facturerec -> error ;
$error ++ ;
}
// End call triggers
}
2020-05-21 15:05:19 +02:00
} else {
2018-04-16 14:18:38 +02:00
$error ++ ;
2019-11-12 09:46:08 +01:00
$this -> error = " Failed to load invoice template with id= " . $line -> rowid . " , entity= " . $conf -> entity . " \n " ;
$this -> errors [] = " Failed to load invoice template with id= " . $line -> rowid . " , entity= " . $conf -> entity ;
2018-04-16 14:18:38 +02:00
dol_syslog ( " createRecurringInvoices Failed to load invoice template with id= " . $line -> rowid . " , entity= " . $conf -> entity );
}
2016-02-16 00:31:05 +01:00
2021-02-23 21:09:01 +01:00
if ( ! $error && $invoiceidgenerated >= 0 ) {
2020-09-19 23:30:29 +02:00
$this -> db -> commit ( " createRecurringInvoices Process invoice template id= " . $facturerec -> id . " , ref= " . $facturerec -> ref );
2016-02-16 00:31:05 +01:00
dol_syslog ( " createRecurringInvoices Process invoice template " . $facturerec -> ref . " is finished with a success generation " );
2016-02-12 15:55:55 +01:00
$nb_create ++ ;
2019-11-12 09:46:08 +01:00
$this -> output .= $langs -> trans ( " InvoiceGeneratedFromTemplate " , $facture -> ref , $facturerec -> ref ) . " \n " ;
2020-05-21 15:05:19 +02:00
} else {
2020-09-19 23:30:29 +02:00
$this -> db -> rollback ( " createRecurringInvoices Process invoice template id= " . $facturerec -> id . " , ref= " . $facturerec -> ref );
2016-02-12 15:55:55 +01:00
}
2016-02-11 09:50:35 +01:00
2018-08-17 20:39:46 +02:00
$parameters = array (
'cpt' => $i ,
'total' => $num ,
'errorCount' => $error ,
'invoiceidgenerated' => $invoiceidgenerated ,
2019-11-12 09:46:08 +01:00
'facturerec' => $facturerec , // it's an object which PHP passes by "reference", so modifiable by hooks.
'this' => $this , // it's an object which PHP passes by "reference", so modifiable by hooks.
2019-03-02 08:43:09 +01:00
);
2019-11-12 09:46:08 +01:00
$reshook = $hookmanager -> executeHooks ( 'afterCreationOfRecurringInvoice' , $parameters , $facture ); // note: $facture can be modified by hooks (warning: $facture can be null)
2018-08-17 20:39:46 +02:00
2016-02-16 00:31:05 +01:00
$i ++ ;
2016-02-11 09:50:35 +01:00
}
2017-06-09 09:25:15 +02:00
2019-11-12 09:46:08 +01:00
$conf -> entity = $saventity ; // Restore entity context
2021-02-23 21:09:01 +01:00
} else {
dol_print_error ( $this -> db );
}
2017-06-09 09:25:15 +02:00
2019-11-12 09:46:08 +01:00
$this -> output = trim ( $this -> output );
2017-06-09 09:25:15 +02:00
2019-11-12 09:46:08 +01:00
return $error ? $error : 0 ;
2016-02-11 09:50:35 +01:00
}
2017-06-09 09:25:15 +02:00
2009-02-04 21:38:07 +01:00
/**
2015-03-15 14:04:07 +01:00
* Return clicable name ( with picto eventually )
2012-01-10 09:57:16 +01:00
*
2017-11-25 15:56:36 +01:00
* @ param int $withpicto Add picto into link
* @ param string $option Where point the link
* @ param int $max Maxlength of ref
* @ param int $short 1 = Return just URL
* @ param string $moretitle Add more text to title tooltip
2020-10-27 19:46:07 +01:00
* @ param int $notooltip 1 = Disable tooltip
* @ param int $save_lastsearch_value - 1 = Auto , 0 = No save of lastsearch_values when clicking , 1 = Save lastsearch_values whenclicking
2017-11-25 15:56:36 +01:00
* @ return string String with URL
2009-02-04 21:38:07 +01:00
*/
2019-02-25 23:15:48 +01:00
public function getNomUrl ( $withpicto = 0 , $option = '' , $max = 0 , $short = 0 , $moretitle = '' , $notooltip = '' , $save_lastsearch_value = - 1 )
2009-02-04 21:38:07 +01:00
{
2022-02-15 18:14:44 +01:00
global $langs , $hookmanager ;
2009-02-04 21:38:07 +01:00
2019-11-12 09:46:08 +01:00
$result = '' ;
2018-06-18 16:27:25 +02:00
2020-07-05 23:18:19 +02:00
$label = '<u>' . $langs -> trans ( " RepeatableInvoice " ) . '</u>' ;
2019-11-12 09:46:08 +01:00
if ( ! empty ( $this -> ref )) {
$label .= '<br><b>' . $langs -> trans ( 'Ref' ) . ':</b> ' . $this -> ref ;
2019-10-13 20:50:03 +02:00
}
if ( $this -> frequency > 0 ) {
2019-11-12 09:46:08 +01:00
$label .= '<br><b>' . $langs -> trans ( 'Frequency' ) . ':</b> ' . $langs -> trans ( 'FrequencyPer_' . $this -> unit_frequency , $this -> frequency );
2019-10-13 20:50:03 +02:00
}
2019-11-12 09:46:08 +01:00
if ( ! empty ( $this -> date_last_gen )) {
$label .= '<br><b>' . $langs -> trans ( 'DateLastGeneration' ) . ':</b> ' . dol_print_date ( $this -> date_last_gen , 'dayhour' );
2019-10-13 20:50:03 +02:00
}
if ( $this -> frequency > 0 ) {
2019-11-12 09:46:08 +01:00
if ( ! empty ( $this -> date_when )) {
$label .= '<br><b>' . $langs -> trans ( 'NextDateToExecution' ) . ':</b> ' ;
$label .= ( empty ( $this -> suspended ) ? '' : '<strike>' ) . dol_print_date ( $this -> date_when , 'day' ) . ( empty ( $this -> suspended ) ? '' : '</strike>' ); // No hour for this property
2021-02-23 21:09:01 +01:00
if ( ! empty ( $this -> suspended )) {
$label .= ' (' . $langs -> trans ( " Disabled " ) . ')' ;
}
2018-06-18 16:27:25 +02:00
}
}
2017-06-09 09:25:15 +02:00
2020-10-27 19:46:07 +01:00
$url = DOL_URL_ROOT . '/compta/facture/card-rec.php?facid=' . $this -> id ;
2017-06-09 09:25:15 +02:00
2021-02-23 21:09:01 +01:00
if ( $short ) {
return $url ;
}
2017-06-09 09:25:15 +02:00
2021-02-23 21:09:01 +01:00
if ( $option != 'nolink' ) {
2020-10-27 19:46:07 +01:00
// Add param to save lastsearch_values or not
$add_save_lastsearch_values = ( $save_lastsearch_value == 1 ? 1 : 0 );
2021-02-23 21:09:01 +01:00
if ( $save_lastsearch_value == - 1 && preg_match ( '/list\.php/' , $_SERVER [ " PHP_SELF " ])) {
$add_save_lastsearch_values = 1 ;
}
if ( $add_save_lastsearch_values ) {
$url .= '&save_lastsearch_values=1' ;
}
2020-10-27 19:46:07 +01:00
}
2017-06-09 09:25:15 +02:00
2017-11-25 15:56:36 +01:00
$linkstart = '<a href="' . $url . '" title="' . dol_escape_htmltag ( $label , 1 ) . '" class="classfortooltip">' ;
2019-11-12 09:46:08 +01:00
$linkend = '</a>' ;
2009-02-04 21:38:07 +01:00
2017-11-25 15:56:36 +01:00
$result .= $linkstart ;
2021-02-23 21:09:01 +01:00
if ( $withpicto ) {
$result .= img_object (( $notooltip ? '' : $label ), ( $this -> picto ? $this -> picto : 'generic' ), ( $notooltip ? (( $withpicto != 2 ) ? 'class="paddingright"' : '' ) : 'class="' . (( $withpicto != 2 ) ? 'paddingright ' : '' ) . 'classfortooltip"' ), 0 , 0 , $notooltip ? 0 : 1 );
}
if ( $withpicto != 2 ) {
$result .= $this -> ref ;
}
2017-11-25 15:56:36 +01:00
$result .= $linkend ;
2022-02-15 11:13:22 +01:00
global $action ;
$hookmanager -> initHooks ( array ( $this -> element . 'dao' ));
2022-02-15 19:04:18 +01:00
$parameters = array ( 'id' => $this -> id , 'getnomurl' => & $result );
2022-02-15 11:13:22 +01:00
$reshook = $hookmanager -> executeHooks ( 'getNomUrl' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
if ( $reshook > 0 ) {
$result = $hookmanager -> resPrint ;
} else {
$result .= $hookmanager -> resPrint ;
}
2009-02-04 21:38:07 +01:00
return $result ;
}
2017-09-10 20:47:45 +02:00
/**
* Return label of object status
*
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = short label + picto , 6 = Long label + picto
2017-11-25 11:10:52 +01:00
* @ param integer $alreadypaid Not used on recurring invoices
* @ return string Label of status
2017-09-10 20:47:45 +02:00
*/
2019-02-25 23:15:48 +01:00
public function getLibStatut ( $mode = 0 , $alreadypaid = - 1 )
2017-09-10 20:47:45 +02:00
{
2019-11-12 09:46:08 +01:00
return $this -> LibStatut ( $this -> frequency ? 1 : 0 , $this -> suspended , $mode , $alreadypaid , empty ( $this -> type ) ? 0 : $this -> type );
2017-09-10 20:47:45 +02:00
}
2020-10-27 19:46:07 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-09-10 19:28:20 +02:00
/**
2017-11-25 11:10:52 +01:00
* Return label of a status
2017-09-10 19:28:20 +02:00
*
2017-09-10 20:47:45 +02:00
* @ param int $recur Is it a recurring invoice ?
* @ param int $status Id status ( suspended or not )
2017-09-10 19:28:20 +02:00
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = short label + picto , 6 = long label + picto
2017-11-25 11:10:52 +01:00
* @ param integer $alreadypaid Not used for recurring invoices
2017-09-10 19:28:20 +02:00
* @ param int $type Type invoice
2017-11-25 11:10:52 +01:00
* @ return string Label of status
2017-09-10 19:28:20 +02:00
*/
2019-02-25 23:15:48 +01:00
public function LibStatut ( $recur , $status , $mode = 0 , $alreadypaid = - 1 , $type = 0 )
2017-09-10 19:28:20 +02:00
{
2020-10-27 19:46:07 +01:00
// phpcs:enable
2017-09-10 19:28:20 +02:00
global $langs ;
$langs -> load ( 'bills' );
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Active' );
2019-12-15 19:32:37 +01:00
$statusType = 'status0' ;
2017-11-25 11:10:52 +01:00
//print "$recur,$status,$mode,$alreadypaid,$type";
2021-02-23 21:09:01 +01:00
if ( $mode == 0 ) {
if ( $recur ) {
2019-12-15 19:32:37 +01:00
if ( $status == self :: STATUS_SUSPENDED ) {
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Disabled' );
2020-05-21 15:05:19 +02:00
} else {
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Active' );
2019-12-15 19:32:37 +01:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-12-15 19:32:37 +01:00
if ( $status == self :: STATUS_SUSPENDED ) {
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Disabled' );
2020-05-21 15:05:19 +02:00
} else {
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( " Draft " );
2019-12-15 19:32:37 +01:00
}
2018-03-22 21:42:19 +01:00
}
2021-02-23 21:09:01 +01:00
} elseif ( $mode == 1 ) {
2019-11-12 09:46:08 +01:00
$prefix = 'Short' ;
2021-02-23 21:09:01 +01:00
if ( $recur ) {
2019-12-15 19:32:37 +01:00
if ( $status == self :: STATUS_SUSPENDED ) {
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Disabled' );
2020-05-21 15:05:19 +02:00
} else {
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Active' );
2019-12-15 19:32:37 +01:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-12-15 19:32:37 +01:00
if ( $status == self :: STATUS_SUSPENDED ) {
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Disabled' );
2020-05-21 15:05:19 +02:00
} else {
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( " Draft " );
2019-12-15 19:32:37 +01:00
}
2018-03-22 21:42:19 +01:00
}
2021-02-23 21:09:01 +01:00
} elseif ( $mode == 2 ) {
if ( $recur ) {
2019-12-15 19:32:37 +01:00
if ( $status == self :: STATUS_SUSPENDED ) {
$statusType = 'status6' ;
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Disabled' );
2020-05-21 15:05:19 +02:00
} else {
2019-12-15 19:32:37 +01:00
$statusType = 'status4' ;
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Active' );
2019-12-15 19:32:37 +01:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-12-15 19:32:37 +01:00
if ( $status == self :: STATUS_SUSPENDED ) {
$statusType = 'status6' ;
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Disabled' );
2020-05-21 15:05:19 +02:00
} else {
2019-12-15 19:32:37 +01:00
$statusType = 'status0' ;
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Draft' );
2019-12-15 19:32:37 +01:00
}
2018-03-22 21:42:19 +01:00
}
2021-02-23 21:09:01 +01:00
} elseif ( $mode == 3 ) {
if ( $recur ) {
2019-11-12 09:46:08 +01:00
$prefix = 'Short' ;
2019-12-15 19:32:37 +01:00
if ( $status == self :: STATUS_SUSPENDED ) {
$statusType = 'status6' ;
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Disabled' );
2020-05-21 15:05:19 +02:00
} else {
2019-12-15 19:32:37 +01:00
$statusType = 'status4' ;
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Active' );
2019-12-15 19:32:37 +01:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-12-15 19:32:37 +01:00
if ( $status == self :: STATUS_SUSPENDED ) {
$statusType = 'status6' ;
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Disabled' );
2020-05-21 15:05:19 +02:00
} else {
2019-12-15 19:32:37 +01:00
$statusType = 'status0' ;
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Draft' );
2019-12-15 19:32:37 +01:00
}
2018-03-22 21:42:19 +01:00
}
2021-02-23 21:09:01 +01:00
} elseif ( $mode == 4 ) {
2019-11-12 09:46:08 +01:00
$prefix = '' ;
2021-02-23 21:09:01 +01:00
if ( $recur ) {
2019-12-15 19:32:37 +01:00
if ( $status == self :: STATUS_SUSPENDED ) {
$statusType = 'status6' ;
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Disabled' );
2020-05-21 15:05:19 +02:00
} else {
2019-12-15 19:32:37 +01:00
$statusType = 'status4' ;
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Active' );
2019-12-15 19:32:37 +01:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-12-15 19:32:37 +01:00
if ( $status == self :: STATUS_SUSPENDED ) {
$statusType = 'status6' ;
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Disabled' );
2020-05-21 15:05:19 +02:00
} else {
2019-12-15 19:32:37 +01:00
$statusType = 'status0' ;
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Draft' );
2019-12-15 19:32:37 +01:00
}
2018-03-22 21:42:19 +01:00
}
2021-02-23 21:09:01 +01:00
} elseif ( $mode == 5 || $mode == 6 ) {
2019-11-12 09:46:08 +01:00
$prefix = '' ;
2021-02-23 21:09:01 +01:00
if ( $mode == 5 ) {
$prefix = 'Short' ;
}
if ( $recur ) {
2019-12-15 19:32:37 +01:00
if ( $status == self :: STATUS_SUSPENDED ) {
$statusType = 'status6' ;
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Disabled' );
2020-05-21 15:05:19 +02:00
} else {
2019-12-15 19:32:37 +01:00
$statusType = 'status4' ;
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Active' );
2019-12-15 19:32:37 +01:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-12-15 19:32:37 +01:00
if ( $status == self :: STATUS_SUSPENDED ) {
$statusType = 'status6' ;
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Disabled' );
2020-05-21 15:05:19 +02:00
} else {
2019-12-15 19:32:37 +01:00
$statusType = 'status0' ;
2021-10-16 19:37:57 +02:00
$labelStatus = $langs -> transnoentitiesnoconv ( 'Draft' );
2019-12-15 19:32:37 +01:00
}
2018-03-22 21:42:19 +01:00
}
2017-09-10 19:28:20 +02:00
}
2019-12-15 19:32:37 +01:00
2021-10-16 19:37:57 +02:00
$labelStatusShort = $labelStatus ;
2019-12-15 19:32:37 +01:00
return dolGetStatus ( $labelStatus , $labelStatusShort , '' , $statusType , $mode );
2017-09-10 19:28:20 +02:00
}
2015-04-21 15:49:58 +02:00
2013-09-16 13:42:42 +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 .
*
* @ param string $option '' = Create a specimen invoice with lines , 'nolines' = No lines
* @ return void
*/
2019-02-25 23:15:48 +01:00
public function initAsSpecimen ( $option = '' )
2013-09-16 13:42:42 +02:00
{
2019-11-12 09:46:08 +01:00
global $user , $langs , $conf ;
2013-09-16 13:42:42 +02:00
2019-11-12 09:46:08 +01:00
$now = dol_now ();
$arraynow = dol_getdate ( $now );
$nownotime = dol_mktime ( 0 , 0 , 0 , $arraynow [ 'mon' ], $arraynow [ 'mday' ], $arraynow [ 'year' ]);
2013-09-16 13:42:42 +02:00
2020-10-27 19:46:07 +01:00
// Load array of products prodids
2016-05-29 11:20:27 +02:00
$num_prods = 0 ;
2016-06-28 02:36:40 +02:00
$prodids = array ();
2017-06-09 09:25:15 +02:00
2016-05-17 23:38:07 +02:00
$sql = " SELECT rowid " ;
2019-11-12 09:46:08 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product " ;
$sql .= " WHERE entity IN ( " . getEntity ( 'product' ) . " ) " ;
2020-08-19 01:43:48 +02:00
$sql .= $this -> db -> plimit ( 100 );
2016-05-17 23:38:07 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
2016-05-17 23:38:07 +02:00
$num_prods = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-23 21:09:01 +01:00
while ( $i < $num_prods ) {
2016-05-17 23:38:07 +02:00
$i ++ ;
$row = $this -> db -> fetch_row ( $resql );
$prodids [ $i ] = $row [ 0 ];
}
}
// Initialize parameters
2019-11-12 09:46:08 +01:00
$this -> id = 0 ;
2016-05-17 23:38:07 +02:00
$this -> ref = 'SPECIMEN' ;
2021-03-08 09:14:23 +01:00
$this -> title = 'SPECIMEN' ;
2019-11-12 09:46:08 +01:00
$this -> specimen = 1 ;
2016-05-17 23:38:07 +02:00
$this -> socid = 1 ;
$this -> date = $nownotime ;
2019-11-12 09:46:08 +01:00
$this -> date_lim_reglement = $nownotime + 3600 * 24 * 30 ;
2016-05-17 23:38:07 +02:00
$this -> cond_reglement_id = 1 ;
$this -> cond_reglement_code = 'RECEP' ;
2019-11-12 09:46:08 +01:00
$this -> date_lim_reglement = $this -> calculate_date_lim_reglement ();
$this -> mode_reglement_id = 0 ; // Not forced to show payment mode CHQ + VIR
$this -> mode_reglement_code = '' ; // Not forced to show payment mode CHQ + VIR
$this -> note_public = 'This is a comment (public)' ;
$this -> note_private = 'This is a comment (private)' ;
$this -> note = 'This is a comment (private)' ;
$this -> fk_incoterms = 0 ;
$this -> location_incoterms = '' ;
2016-05-17 23:38:07 +02:00
2021-02-23 21:09:01 +01:00
if ( empty ( $option ) || $option != 'nolines' ) {
2016-05-17 23:38:07 +02:00
// Lines
$nbp = 5 ;
$xnbp = 0 ;
2021-02-23 21:09:01 +01:00
while ( $xnbp < $nbp ) {
2019-11-12 09:46:08 +01:00
$line = new FactureLigne ( $this -> db );
$line -> desc = $langs -> trans ( " Description " ) . " " . $xnbp ;
$line -> qty = 1 ;
$line -> subprice = 100 ;
$line -> tva_tx = 19.6 ;
$line -> localtax1_tx = 0 ;
$line -> localtax2_tx = 0 ;
$line -> remise_percent = 0 ;
2021-02-23 21:09:01 +01:00
if ( $xnbp == 1 ) { // Qty is negative (product line)
2016-05-17 23:38:07 +02:00
$prodid = mt_rand ( 1 , $num_prods );
2019-11-12 09:46:08 +01:00
$line -> fk_product = $prodids [ $prodid ];
$line -> qty = - 1 ;
$line -> total_ht = - 100 ;
$line -> total_ttc = - 119.6 ;
$line -> total_tva = - 19.6 ;
2021-02-23 21:09:01 +01:00
} elseif ( $xnbp == 2 ) { // UP is negative (free line)
2019-11-12 09:46:08 +01:00
$line -> subprice = - 100 ;
$line -> total_ht = - 100 ;
$line -> total_ttc = - 119.6 ;
$line -> total_tva = - 19.6 ;
$line -> remise_percent = 0 ;
2021-02-23 21:09:01 +01:00
} elseif ( $xnbp == 3 ) { // Discount is 50% (product line)
2016-05-17 23:38:07 +02:00
$prodid = mt_rand ( 1 , $num_prods );
2019-11-12 09:46:08 +01:00
$line -> fk_product = $prodids [ $prodid ];
$line -> total_ht = 50 ;
$line -> total_ttc = 59.8 ;
$line -> total_tva = 9.8 ;
$line -> remise_percent = 50 ;
2020-05-21 15:05:19 +02:00
} else // (product line)
2016-05-17 23:38:07 +02:00
{
$prodid = mt_rand ( 1 , $num_prods );
2019-11-12 09:46:08 +01:00
$line -> fk_product = $prodids [ $prodid ];
$line -> total_ht = 100 ;
$line -> total_ttc = 119.6 ;
$line -> total_tva = 19.6 ;
$line -> remise_percent = 00 ;
2016-05-17 23:38:07 +02:00
}
2019-11-12 09:46:08 +01:00
$this -> lines [ $xnbp ] = $line ;
2016-05-17 23:38:07 +02:00
$xnbp ++ ;
2013-09-16 13:42:42 +02:00
2016-05-17 23:38:07 +02:00
$this -> total_ht += $line -> total_ht ;
$this -> total_tva += $line -> total_tva ;
$this -> total_ttc += $line -> total_ttc ;
}
$this -> revenuestamp = 0 ;
// Add a line "offered"
2019-11-12 09:46:08 +01:00
$line = new FactureLigne ( $this -> db );
$line -> desc = $langs -> trans ( " Description " ) . " (offered line) " ;
$line -> qty = 1 ;
$line -> subprice = 100 ;
$line -> tva_tx = 19.6 ;
$line -> localtax1_tx = 0 ;
$line -> localtax2_tx = 0 ;
$line -> remise_percent = 100 ;
$line -> total_ht = 0 ;
$line -> total_ttc = 0 ; // 90 * 1.196
$line -> total_tva = 0 ;
2016-05-17 23:38:07 +02:00
$prodid = mt_rand ( 1 , $num_prods );
2019-11-12 09:46:08 +01:00
$line -> fk_product = $prodids [ $prodid ];
2016-05-17 23:38:07 +02:00
2019-11-12 09:46:08 +01:00
$this -> lines [ $xnbp ] = $line ;
2016-05-17 23:38:07 +02:00
$xnbp ++ ;
}
2017-06-09 09:25:15 +02:00
2020-02-07 15:51:14 +01:00
$this -> usenewprice = 0 ;
2013-09-16 13:42:42 +02:00
}
2015-04-18 18:11:01 +02:00
/**
* Function used to replace a thirdparty id with another one .
*
* @ param DoliDB $db Database handler
* @ param int $origin_id Old thirdparty id
* @ param int $dest_id New thirdparty id
* @ return bool
*/
public static function replaceThirdparty ( DoliDB $db , $origin_id , $dest_id )
{
$tables = array (
'facture_rec'
);
return CommonObject :: commonReplaceThirdparty ( $db , $origin_id , $dest_id , $tables );
}
2017-06-09 09:25:15 +02:00
2021-11-23 21:38:29 +01:00
/**
* Function used to replace a product id with another one .
*
* @ param DoliDB $db Database handler
* @ param int $origin_id Old product id
* @ param int $dest_id New product id
* @ return bool
*/
public static function replaceProduct ( DoliDB $db , $origin_id , $dest_id )
{
$tables = array (
'facturedet_rec'
);
return CommonObject :: commonReplaceProduct ( $db , $origin_id , $dest_id , $tables );
}
2016-02-12 12:31:43 +01:00
/**
2020-10-27 19:46:07 +01:00
* Update frequency and unit
*
* @ param int $frequency value of frequency
* @ param string $unit unit of frequency ( d , m , y )
2022-05-13 11:23:49 +02:00
* @ param int $notrigger Disable the trigger
2020-10-27 19:46:07 +01:00
* @ return int < 0 if KO , > 0 if OK
*/
2022-05-13 11:23:49 +02:00
public function setFrequencyAndUnit ( $frequency , $unit , $notrigger = 0 )
2020-10-27 19:46:07 +01:00
{
2022-05-13 11:23:49 +02:00
global $user ;
2020-10-27 19:46:07 +01:00
if ( ! $this -> table_element ) {
dol_syslog ( get_class ( $this ) . " ::setFrequencyAndUnit was called on objet with property table_element not defined " , LOG_ERR );
return - 1 ;
}
if ( ! empty ( $frequency ) && empty ( $unit )) {
dol_syslog ( get_class ( $this ) . " ::setFrequencyAndUnit was called on objet with params frequency defined but unit not defined " , LOG_ERR );
return - 2 ;
}
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= ' SET frequency = ' . ( $frequency ? $this -> db -> escape ( $frequency ) : 'null' );
if ( ! empty ( $unit )) {
$sql .= ', unit_frequency = \'' . $this -> db -> escape ( $unit ) . '\'' ;
}
2021-08-27 23:36:06 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2020-10-27 19:46:07 +01:00
dol_syslog ( get_class ( $this ) . " ::setFrequencyAndUnit " , LOG_DEBUG );
if ( $this -> db -> query ( $sql )) {
$this -> frequency = $frequency ;
2021-02-23 21:09:01 +01:00
if ( ! empty ( $unit )) {
$this -> unit_frequency = $unit ;
}
2022-05-13 11:23:49 +02:00
if ( ! $notrigger ) {
// Call trigger
$result = $this -> call_trigger ( 'BILLREC_MODIFY' , $user );
if ( $result < 0 ) {
return $result ;
}
// End call triggers
}
2020-10-27 19:46:07 +01:00
return 1 ;
} else {
dol_print_error ( $this -> db );
return - 1 ;
2016-02-12 12:31:43 +01:00
}
2020-10-27 19:46:07 +01:00
}
2017-06-09 09:25:15 +02:00
2016-02-12 12:31:43 +01:00
/**
2020-10-27 19:46:07 +01:00
* Update the next date of execution
*
* @ param datetime $date date of execution
* @ param int $increment_nb_gen_done 0 do nothing more , > 0 increment nb_gen_done
2022-05-13 11:23:49 +02:00
* @ param int $notrigger Disable the trigger
2020-10-27 19:46:07 +01:00
* @ return int < 0 if KO , > 0 if OK
*/
2022-05-13 11:23:49 +02:00
public function setNextDate ( $date , $increment_nb_gen_done = 0 , $notrigger = 0 )
2020-10-27 19:46:07 +01:00
{
2022-05-13 11:23:49 +02:00
global $user ;
2021-02-23 21:09:01 +01:00
if ( ! $this -> table_element ) {
2020-10-27 19:46:07 +01:00
dol_syslog ( get_class ( $this ) . " ::setNextDate was called on objet with property table_element not defined " , LOG_ERR );
return - 1 ;
}
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " SET date_when = " . ( $date ? " ' " . $this -> db -> idate ( $date ) . " ' " : " null " );
2021-02-23 21:09:01 +01:00
if ( $increment_nb_gen_done > 0 ) {
$sql .= ', nb_gen_done = nb_gen_done + 1' ;
}
2021-08-27 23:36:06 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2020-10-27 19:46:07 +01:00
dol_syslog ( get_class ( $this ) . " ::setNextDate " , LOG_DEBUG );
2021-02-23 21:09:01 +01:00
if ( $this -> db -> query ( $sql )) {
2020-10-27 19:46:07 +01:00
$this -> date_when = $date ;
2021-02-23 21:09:01 +01:00
if ( $increment_nb_gen_done > 0 ) {
$this -> nb_gen_done ++ ;
}
2022-05-13 11:23:49 +02:00
if ( ! $notrigger ) {
// Call trigger
$result = $this -> call_trigger ( 'BILLREC_MODIFY' , $user );
if ( $result < 0 ) {
return $result ;
}
// End call triggers
}
2020-10-27 19:46:07 +01:00
return 1 ;
} else {
dol_print_error ( $this -> db );
return - 1 ;
}
}
2017-06-09 09:25:15 +02:00
2016-02-12 12:31:43 +01:00
/**
2020-10-27 19:46:07 +01:00
* Update the maximum period
*
* @ param int $nb number of maximum period
2022-05-13 11:23:49 +02:00
* @ param int $notrigger Disable the trigger
2020-10-27 19:46:07 +01:00
* @ return int < 0 if KO , > 0 if OK
*/
2022-05-13 11:23:49 +02:00
public function setMaxPeriod ( $nb , $notrigger = 0 )
2020-10-27 19:46:07 +01:00
{
2022-05-13 11:23:49 +02:00
global $user ;
2021-02-23 21:09:01 +01:00
if ( ! $this -> table_element ) {
2020-10-27 19:46:07 +01:00
dol_syslog ( get_class ( $this ) . " ::setMaxPeriod was called on objet with property table_element not defined " , LOG_ERR );
return - 1 ;
}
2021-02-23 21:09:01 +01:00
if ( empty ( $nb )) {
$nb = 0 ;
}
2020-10-27 19:46:07 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2021-03-30 11:36:50 +02:00
$sql .= ' SET nb_gen_max = ' . (( int ) $nb );
2021-08-27 23:36:06 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2020-10-27 19:46:07 +01:00
dol_syslog ( get_class ( $this ) . " ::setMaxPeriod " , LOG_DEBUG );
2021-02-23 21:09:01 +01:00
if ( $this -> db -> query ( $sql )) {
2020-10-27 19:46:07 +01:00
$this -> nb_gen_max = $nb ;
2022-05-13 11:23:49 +02:00
if ( ! $notrigger ) {
// Call trigger
$result = $this -> call_trigger ( 'BILLREC_MODIFY' , $user );
if ( $result < 0 ) {
return $result ;
}
// End call triggers
}
2020-10-27 19:46:07 +01:00
return 1 ;
} else {
dol_print_error ( $this -> db );
return - 1 ;
}
}
/**
* Update the auto validate flag of invoice
*
* @ param int $validate 0 to create in draft , 1 to create and validate invoice
2022-05-13 11:23:49 +02:00
* @ param int $notrigger Disable the trigger
2020-10-27 19:46:07 +01:00
* @ return int < 0 if KO , > 0 if OK
*/
2022-05-13 11:23:49 +02:00
public function setAutoValidate ( $validate , $notrigger = 0 )
2020-10-27 19:46:07 +01:00
{
2022-05-13 11:23:49 +02:00
global $user ;
2021-02-23 21:09:01 +01:00
if ( ! $this -> table_element ) {
2020-10-27 19:46:07 +01:00
dol_syslog ( get_class ( $this ) . " ::setAutoValidate was called on objet with property table_element not defined " , LOG_ERR );
return - 1 ;
}
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2021-03-30 11:36:50 +02:00
$sql .= ' SET auto_validate = ' . (( int ) $validate );
2021-08-27 23:36:06 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2020-10-27 19:46:07 +01:00
dol_syslog ( get_class ( $this ) . " ::setAutoValidate " , LOG_DEBUG );
2021-02-23 21:09:01 +01:00
if ( $this -> db -> query ( $sql )) {
2020-10-27 19:46:07 +01:00
$this -> auto_validate = $validate ;
2022-05-13 11:23:49 +02:00
if ( ! $notrigger ) {
// Call trigger
$result = $this -> call_trigger ( 'BILLREC_MODIFY' , $user );
if ( $result < 0 ) {
return $result ;
}
// End call triggers
}
2020-10-27 19:46:07 +01:00
return 1 ;
} else {
dol_print_error ( $this -> db );
return - 1 ;
}
}
/**
* Update the auto generate documents
*
* @ param int $validate 0 no document , 1 to generate document
2022-05-13 11:23:49 +02:00
* @ param int $notrigger Disable the trigger
2020-10-27 19:46:07 +01:00
* @ return int < 0 if KO , > 0 if OK
*/
2022-05-13 11:23:49 +02:00
public function setGeneratePdf ( $validate , $notrigger = 0 )
2020-10-27 19:46:07 +01:00
{
2022-05-13 11:23:49 +02:00
global $user ;
2021-02-23 21:09:01 +01:00
if ( ! $this -> table_element ) {
2020-10-27 19:46:07 +01:00
dol_syslog ( get_class ( $this ) . " ::setGeneratePdf was called on objet with property table_element not defined " , LOG_ERR );
return - 1 ;
}
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2021-03-30 11:36:50 +02:00
$sql .= ' SET generate_pdf = ' . (( int ) $validate );
2021-08-27 23:36:06 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2020-10-27 19:46:07 +01:00
dol_syslog ( get_class ( $this ) . " ::setGeneratePdf " , LOG_DEBUG );
2021-02-23 21:09:01 +01:00
if ( $this -> db -> query ( $sql )) {
2020-10-27 19:46:07 +01:00
$this -> generate_pdf = $validate ;
2022-05-13 11:23:49 +02:00
if ( ! $notrigger ) {
// Call trigger
$result = $this -> call_trigger ( 'BILLREC_MODIFY' , $user );
if ( $result < 0 ) {
return $result ;
}
// End call triggers
}
2020-10-27 19:46:07 +01:00
return 1 ;
} else {
dol_print_error ( $this -> db );
return - 1 ;
}
}
2017-06-09 09:25:15 +02:00
2016-02-12 12:31:43 +01:00
/**
2020-10-27 19:46:07 +01:00
* Update the model for documents
*
* @ param string $model model of document generator
2022-05-13 11:23:49 +02:00
* @ param int $notrigger Disable the trigger
2020-10-27 19:46:07 +01:00
* @ return int < 0 if KO , > 0 if OK
*/
2022-05-13 11:23:49 +02:00
public function setModelPdf ( $model , $notrigger = 0 )
2020-10-27 19:46:07 +01:00
{
2022-05-13 11:23:49 +02:00
global $user ;
2021-02-23 21:09:01 +01:00
if ( ! $this -> table_element ) {
2020-10-27 19:46:07 +01:00
dol_syslog ( get_class ( $this ) . " ::setModelPdf was called on objet with property table_element not defined " , LOG_ERR );
return - 1 ;
}
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2021-08-27 23:36:06 +02:00
$sql .= " SET modelpdf = ' " . $this -> db -> escape ( $model ) . " ' " ;
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2020-10-27 19:46:07 +01:00
dol_syslog ( get_class ( $this ) . " ::setModelPdf " , LOG_DEBUG );
2021-02-23 21:09:01 +01:00
if ( $this -> db -> query ( $sql )) {
2021-02-08 18:30:09 +01:00
$this -> model_pdf = $model ;
2022-05-13 11:23:49 +02:00
if ( ! $notrigger ) {
// Call trigger
$result = $this -> call_trigger ( 'BILLREC_MODIFY' , $user );
if ( $result < 0 ) {
return $result ;
}
// End call triggers
}
2020-10-27 19:46:07 +01:00
return 1 ;
} else {
dol_print_error ( $this -> db );
return - 1 ;
}
}
2003-10-12 20:11:19 +02:00
}
2016-04-24 18:55:19 +02:00
/**
* Class to manage invoice lines of templates .
* Saved into database table llx_facturedet_rec
*/
class FactureLigneRec extends CommonInvoiceLine
{
2018-08-23 17:07:27 +02:00
/**
* @ var string ID to identify managed object
*/
2019-11-12 09:46:08 +01:00
public $element = 'facturedetrec' ;
2018-09-02 21:08:57 +02:00
2018-08-22 18:34:50 +02:00
/**
* @ var string Name of table without prefix where object is stored
*/
2019-11-12 09:46:08 +01:00
public $table_element = 'facturedet_rec' ;
2017-08-27 13:38:42 +02:00
2018-03-12 12:27:40 +01:00
2021-03-08 11:33:07 +01:00
public $fk_product_fournisseur_price ;
2021-10-25 22:07:31 +02:00
public $fk_fournprice ; // For backward compatibility
2021-03-08 11:33:07 +01:00
public $rang ;
public $desc ;
public $description ;
2021-10-25 22:07:31 +02:00
public $fk_product_type ; // Use instead product_type
2021-03-08 11:33:07 +01:00
public $fk_contract_line ;
2018-03-12 12:27:40 +01:00
2020-10-27 19:46:07 +01:00
/**
* Delete line in database
*
* @ param User $user Object user
* @ param int $notrigger Disable triggers
* @ return int < 0 if KO , > 0 if OK
*/
public function delete ( User $user , $notrigger = false )
{
$error = 0 ;
$this -> db -> begin ();
if ( ! $error ) {
if ( ! $notrigger ) {
// Call triggers
$result = $this -> call_trigger ( 'LINEBILLREC_DELETE' , $user );
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
$error ++ ;
} // Do also here what you must do to rollback action if trigger fail
2020-10-27 19:46:07 +01:00
// End call triggers
}
}
2017-06-09 09:25:15 +02:00
2021-02-23 21:09:01 +01:00
if ( ! $error ) {
2020-10-27 19:46:07 +01:00
$result = $this -> deleteExtraFields ();
if ( $result < 0 ) {
$error ++ ;
}
}
2021-02-23 21:09:01 +01:00
if ( ! $error ) {
2021-03-14 12:20:23 +01:00
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this -> table_element . ' WHERE rowid=' . (( int ) $this -> id );
2020-10-27 19:46:07 +01:00
$res = $this -> db -> query ( $sql );
2021-12-21 15:23:56 +01:00
if ( ! $res ) {
2020-10-27 19:46:07 +01:00
$error ++ ;
$this -> errors [] = $this -> db -> lasterror ();
}
}
// Commit or rollback
2017-09-12 13:26:25 +02:00
if ( $error ) {
2020-10-27 19:46:07 +01:00
$this -> db -> rollback ();
return - 1 ;
} else {
$this -> db -> commit ();
return 1 ;
}
}
/**
* Get line of template invoice
*
* @ param int $rowid Id of invoice
* @ return int 1 if OK , < 0 if KO
*/
public function fetch ( $rowid )
{
$sql = 'SELECT l.rowid, l.fk_facture ,l.fk_product, l.product_type, l.label as custom_label, l.description, l.product_type, l.price, l.qty, l.vat_src_code, l.tva_tx,' ;
$sql .= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,' ;
$sql .= ' l.date_start_fill, l.date_end_fill, l.info_bits, l.total_ht, l.total_tva, l.total_ttc,' ;
$sql .= ' l.rang, l.special_code,' ;
$sql .= ' l.fk_unit, l.fk_contract_line,' ;
2021-03-03 23:55:28 +01:00
$sql .= ' l.import_key, l.fk_multicurrency,' ;
$sql .= ' l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,' ;
$sql .= ' l.buy_price_ht, l.fk_product_fournisseur_price,' ;
$sql .= ' l.fk_user_author, l.fk_user_modif,' ;
2020-10-27 19:46:07 +01:00
$sql .= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'facturedet_rec as l' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'product as p ON l.fk_product = p.rowid' ;
2021-03-29 15:32:09 +02:00
$sql .= ' WHERE l.rowid = ' . (( int ) $rowid );
2020-10-27 19:46:07 +01:00
$sql .= ' ORDER BY l.rang' ;
dol_syslog ( 'FactureRec::fetch' , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $result ) {
2020-10-27 19:46:07 +01:00
$objp = $this -> db -> fetch_object ( $result );
$this -> id = $objp -> rowid ;
$this -> label = $objp -> custom_label ; // Label line
$this -> desc = $objp -> description ; // Description line
$this -> description = $objp -> description ; // Description line
$this -> product_type = $objp -> product_type ; // Type of line
$this -> ref = $objp -> product_ref ; // Ref product
$this -> product_ref = $objp -> product_ref ; // Ref product
$this -> libelle = $objp -> product_label ; // deprecated
$this -> product_label = $objp -> product_label ; // Label product
$this -> product_desc = $objp -> product_desc ; // Description product
$this -> fk_product_type = $objp -> fk_product_type ; // Type of product
$this -> qty = $objp -> qty ;
$this -> price = $objp -> price ;
$this -> subprice = $objp -> subprice ;
$this -> fk_facture = $objp -> fk_facture ;
$this -> vat_src_code = $objp -> vat_src_code ;
$this -> tva_tx = $objp -> tva_tx ;
$this -> localtax1_tx = $objp -> localtax1_tx ;
$this -> localtax2_tx = $objp -> localtax2_tx ;
$this -> localtax1_type = $objp -> localtax1_type ;
$this -> localtax2_type = $objp -> localtax2_type ;
$this -> remise_percent = $objp -> remise_percent ;
2020-12-16 11:07:34 +01:00
//$this->fk_remise_except = $objp->fk_remise_except;
2020-10-27 19:46:07 +01:00
$this -> fk_product = $objp -> fk_product ;
$this -> date_start_fill = $objp -> date_start_fill ;
$this -> date_end_fill = $objp -> date_end_fill ;
$this -> info_bits = $objp -> info_bits ;
$this -> total_ht = $objp -> total_ht ;
$this -> total_tva = $objp -> total_tva ;
$this -> total_ttc = $objp -> total_ttc ;
2020-12-16 11:07:34 +01:00
//$this->code_ventilation = $objp->fk_code_ventilation;
2020-10-27 19:46:07 +01:00
$this -> rang = $objp -> rang ;
$this -> special_code = $objp -> special_code ;
$this -> fk_unit = $objp -> fk_unit ;
$this -> fk_contract_line = $objp -> fk_contract_line ;
2021-03-03 23:55:28 +01:00
$this -> import_key = $objp -> import_key ;
$this -> fk_multicurrency = $objp -> fk_multicurrency ;
$this -> multicurrency_code = $objp -> multicurrency_code ;
$this -> multicurrency_subprice = $objp -> multicurrency_subprice ;
$this -> multicurrency_total_ht = $objp -> multicurrency_total_ht ;
$this -> multicurrency_total_tva = $objp -> multicurrency_total_tva ;
$this -> multicurrency_total_ttc = $objp -> multicurrency_total_ttc ;
2021-03-08 11:33:07 +01:00
2021-03-03 23:55:28 +01:00
$this -> buy_price_ht = $objp -> buy_price_ht ;
2021-03-08 11:33:07 +01:00
2021-03-03 23:55:28 +01:00
$this -> fk_product_fournisseur_price = $objp -> fk_product_fournisseur_price ;
$this -> fk_user_author = $objp -> fk_user_author ;
$this -> fk_user_modif = $objp -> fk_user_modif ;
2020-10-27 19:46:07 +01:00
$this -> db -> free ( $result );
return 1 ;
2017-09-12 13:26:25 +02:00
} else {
2020-10-27 19:46:07 +01:00
$this -> error = $this -> db -> lasterror ();
return - 3 ;
2017-09-12 13:26:25 +02:00
}
2020-10-27 19:46:07 +01:00
}
/**
* Update a line to invoice_rec .
*
* @ param User $user User
* @ param int $notrigger No trigger
* @ return int < 0 if KO , Id of line if OK
*/
public function update ( User $user , $notrigger = 0 )
{
global $conf ;
$error = 0 ;
include_once DOL_DOCUMENT_ROOT . '/core/lib/price.lib.php' ;
$sql = " UPDATE " . MAIN_DB_PREFIX . " facturedet_rec SET " ;
2022-04-05 13:36:25 +02:00
$sql .= " fk_facture = " . (( int ) $this -> fk_facture );
2020-10-27 19:46:07 +01:00
$sql .= " , label= " . ( ! empty ( $this -> label ) ? " ' " . $this -> db -> escape ( $this -> label ) . " ' " : " null " );
$sql .= " , description=' " . $this -> db -> escape ( $this -> desc ) . " ' " ;
$sql .= " , price= " . price2num ( $this -> price );
$sql .= " , qty= " . price2num ( $this -> qty );
$sql .= " , tva_tx= " . price2num ( $this -> tva_tx );
$sql .= " , vat_src_code=' " . $this -> db -> escape ( $this -> vat_src_code ) . " ' " ;
$sql .= " , localtax1_tx= " . price2num ( $this -> localtax1_tx );
$sql .= " , localtax1_type=' " . $this -> db -> escape ( $this -> localtax1_type ) . " ' " ;
$sql .= " , localtax2_tx= " . price2num ( $this -> localtax2_tx );
$sql .= " , localtax2_type=' " . $this -> db -> escape ( $this -> localtax2_type ) . " ' " ;
$sql .= " , fk_product= " . ( $this -> fk_product > 0 ? $this -> fk_product : " null " );
2022-04-05 13:36:25 +02:00
$sql .= " , product_type= " . (( int ) $this -> product_type );
$sql .= " , remise_percent= " . price2num ( $this -> remise_percent );
$sql .= " , subprice= " . price2num ( $this -> subprice );
$sql .= " , info_bits= " . price2num ( $this -> info_bits );
2020-10-27 19:46:07 +01:00
$sql .= " , date_start_fill= " . ( int ) $this -> date_start_fill ;
$sql .= " , date_end_fill= " . ( int ) $this -> date_end_fill ;
if ( empty ( $this -> skip_update_total )) {
$sql .= " , total_ht= " . price2num ( $this -> total_ht );
$sql .= " , total_tva= " . price2num ( $this -> total_tva );
$sql .= " , total_localtax1= " . price2num ( $this -> total_localtax1 );
$sql .= " , total_localtax2= " . price2num ( $this -> total_localtax2 );
$sql .= " , total_ttc= " . price2num ( $this -> total_ttc );
}
2021-06-09 15:36:47 +02:00
$sql .= " , rang= " . (( int ) $this -> rang );
$sql .= " , special_code= " . (( int ) $this -> special_code );
2020-10-27 19:46:07 +01:00
$sql .= " , fk_unit= " . ( $this -> fk_unit ? " ' " . $this -> db -> escape ( $this -> fk_unit ) . " ' " : " null " );
$sql .= " , fk_contract_line= " . ( $this -> fk_contract_line ? $this -> fk_contract_line : " null " );
2021-06-09 15:36:47 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2020-10-27 19:46:07 +01:00
2021-01-15 15:33:12 +01:00
$this -> db -> begin ();
2020-10-27 19:46:07 +01:00
dol_syslog ( get_class ( $this ) . " ::updateline " , LOG_DEBUG );
2022-01-10 14:27:43 +01:00
2020-10-27 19:46:07 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:09:01 +01:00
if ( $resql ) {
if ( ! $error ) {
2020-10-27 19:46:07 +01:00
$result = $this -> insertExtraFields ();
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
2020-10-27 19:46:07 +01:00
$error ++ ;
}
}
2021-02-23 21:09:01 +01:00
if ( ! $error && ! $notrigger ) {
2020-10-27 19:46:07 +01:00
// Call trigger
2022-04-02 10:31:58 +02:00
$result = $this -> call_trigger ( 'LINEBILLREC_MODIFY' , $user );
2021-02-23 21:09:01 +01:00
if ( $result < 0 ) {
2021-01-15 15:33:12 +01:00
$error ++ ;
2020-10-27 19:46:07 +01:00
}
// End call triggers
}
2021-01-15 15:33:12 +01:00
if ( $error ) {
$this -> db -> rollback ();
return - 2 ;
} else {
$this -> db -> commit ();
return 1 ;
}
2020-10-27 19:46:07 +01:00
} else {
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 2 ;
}
}
2016-10-04 21:55:37 +02:00
}