2004-10-19 22:43:23 +02:00
< ? php
2012-03-18 11:13:01 +01:00
/* Copyright ( C ) 2003 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( C ) 2004 - 2012 Destailleur Laurent < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2005 - 2014 Regis Houssin < regis . houssin @ inodbox . com >
2012-03-18 11:13:01 +01:00
* Copyright ( C ) 2006 Andre Cianfarani < acianfa @ free . fr >
* Copyright ( C ) 2008 Raphael Bertrand < raphael . bertrand @ resultic . fr >
2016-08-31 02:47:42 +02:00
* Copyright ( C ) 2010 - 2016 Juanjo Menent < jmenent @ 2 byte . es >
2014-10-06 09:06:25 +02:00
* Copyright ( C ) 2013 Christophe Battarel < christophe . battarel @ altairis . fr >
* Copyright ( C ) 2013 Florian Henry < florian . henry @ open - concept . pro >
2015-02-15 15:07:24 +01:00
* Copyright ( C ) 2014 - 2015 Marcos García < marcosgdf @ gmail . com >
2018-06-15 09:47:28 +02:00
* Copyright ( C ) 2018 Nicolas ZABOURI < info @ inovea - conseil . com >
2021-03-17 18:21:07 +01:00
* Copyright ( C ) 2018 - 2021 Frédéric France < frederic . france @ netlogic . fr >
2018-11-27 11:36:31 +01:00
* Copyright ( C ) 2015 - 2018 Ferran Marcet < fmarcet @ 2 byte . es >
2010-04-20 20:23:55 +02:00
*
2003-08-03 21:15:39 +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-08-03 21:15:39 +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-08-03 21:15:39 +02:00
*/
2004-11-28 17:05:02 +01:00
/**
2010-06-22 15:14:23 +02:00
* \file htdocs / contrat / class / contrat . class . php
2009-02-15 16:04:02 +01:00
* \ingroup contrat
2012-08-05 15:00:24 +02:00
* \brief File of class to manage contracts
2008-10-07 01:21:00 +02:00
*/
2004-09-27 01:02:40 +02:00
2019-11-12 09:46:08 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2015-02-26 14:15:33 +01:00
require_once DOL_DOCUMENT_ROOT . " /core/class/commonobjectline.class.php " ;
2019-11-12 09:46:08 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/price.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/margin/lib/margins.lib.php' ;
2012-08-05 15:00:24 +02:00
2005-04-23 00:39:11 +02:00
/**
2012-08-05 15:00:24 +02:00
* Class to manage contracts
2008-10-07 01:21:00 +02:00
*/
2006-06-18 16:18:41 +02:00
class Contrat extends CommonObject
2003-08-03 21:15:39 +02:00
{
2018-08-23 18:17:09 +02:00
/**
* @ var string ID to identify managed object
*/
2019-11-12 09:46:08 +01:00
public $element = 'contrat' ;
2018-08-29 21:25:22 +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 = 'contrat' ;
2018-08-29 21:25:22 +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 = 'contratdet' ;
2018-09-01 15:13:59 +02:00
/**
2020-12-05 23:53:55 +01:00
* @ var string Fieldname 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_contrat' ;
2018-09-01 15:13:59 +02:00
2020-10-31 14:32:18 +01:00
/**
2018-09-05 12:21:13 +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 = 'contract' ;
2018-08-29 21:25:22 +02:00
2020-10-31 14:32:18 +01:00
/**
* 0 = No test on entity , 1 = Test with field entity , 2 = Test with link by societe
* @ var int
*/
public $ismultientitymanaged = 1 ;
2018-08-29 21:25:22 +02:00
2020-10-31 14:32:18 +01:00
/**
* @ var int Does object support extrafields ? 0 = No , 1 = Yes
*/
public $isextrafieldmanaged = 1 ;
2020-01-25 19:30:12 +01:00
2020-10-31 14:32:18 +01:00
/**
* 0 = Default , 1 = View may be restricted to sales representative only if no permission to see all or to company of external user if external user
* @ var integer
*/
public $restrictiononfksoc = 1 ;
2017-06-01 13:32:20 +02:00
2014-12-25 21:47:39 +01:00
/**
* { @ inheritdoc }
*/
protected $table_ref_field = 'ref' ;
2015-10-17 00:52:35 +02:00
/**
* Customer reference of the contract
* @ var string
*/
2018-09-02 10:42:31 +02:00
public $ref_customer ;
2016-10-14 07:54:44 +02:00
2014-12-28 17:55:21 +01:00
/**
* Supplier reference of the contract
* @ var string
*/
2018-09-02 10:42:31 +02:00
public $ref_supplier ;
2014-12-28 17:55:21 +01:00
2020-10-31 14:32:18 +01:00
/**
* Entity of the contract
* @ var int
*/
public $entity ;
2019-08-20 17:04:41 +02:00
2014-12-28 17:55:21 +01:00
/**
* Client id linked to the contract
* @ var int
*/
2018-09-02 10:42:31 +02:00
public $socid ;
2019-11-12 09:46:08 +01:00
public $societe ; // Objet societe
2014-12-28 17:55:21 +01:00
/**
* Status of the contract
* @ var int
*/
2019-11-12 09:46:08 +01:00
public $statut = 0 ; // 0=Draft,
2018-09-02 10:42:31 +02:00
public $product ;
2006-06-19 22:35:22 +02:00
2014-12-28 17:55:21 +01:00
/**
2015-03-17 11:23:45 +01:00
* @ var int Id of user author of the contract
2014-12-28 17:55:21 +01:00
*/
2015-03-17 11:23:45 +01:00
public $fk_user_author ;
2014-12-28 19:28:25 +01:00
/**
* TODO : Which is the correct one ?
* Author of the contract
* @ var int
*/
2015-03-17 11:23:45 +01:00
public $user_author_id ;
2014-12-28 19:28:25 +01:00
/**
2015-03-17 11:23:45 +01:00
* @ var User Object user that create the contract . Set by the info method .
2014-12-28 19:28:25 +01:00
*/
2015-03-17 11:23:45 +01:00
public $user_creation ;
2014-12-28 19:28:25 +01:00
/**
2015-03-17 11:23:45 +01:00
* @ var User Object user that close the contract . Set by the info method .
2014-12-28 19:28:25 +01:00
*/
public $user_cloture ;
2014-12-28 17:55:21 +01:00
/**
2019-11-10 11:07:47 +01:00
* @ var integer | string Date of creation
2014-12-28 17:55:21 +01:00
*/
2018-09-02 10:42:31 +02:00
public $date_creation ;
2014-12-28 17:55:21 +01:00
/**
2019-11-10 11:07:47 +01:00
* @ var integer | string Date of last modification . Not filled until you call -> info ()
2014-12-28 19:28:25 +01:00
*/
public $date_modification ;
/**
2019-11-10 11:07:47 +01:00
* @ var integer | string Date of validation
2014-12-28 17:55:21 +01:00
*/
2018-09-02 10:42:31 +02:00
public $date_validation ;
2008-02-12 02:33:43 +01:00
2014-12-28 17:55:21 +01:00
/**
2019-11-10 11:07:47 +01:00
* @ var integer | string Date when contract was signed
2014-12-28 17:55:21 +01:00
*/
2018-09-02 10:42:31 +02:00
public $date_contrat ;
2014-12-28 17:55:21 +01:00
2018-09-02 10:42:31 +02:00
public $commercial_signature_id ;
public $commercial_suivi_id ;
2005-07-16 01:28:38 +02:00
2014-12-28 18:10:30 +01:00
/**
* @ deprecated Use fk_project instead
2019-04-08 16:04:15 +02:00
* @ see $fk_project
2014-12-28 18:10:30 +01:00
*/
2018-09-02 10:42:31 +02:00
public $fk_projet ;
2012-03-18 19:23:01 +01:00
2019-11-12 09:46:08 +01:00
public $extraparams = array ();
2012-03-18 19:23:01 +01:00
2014-12-28 19:16:14 +01:00
/**
2015-03-17 11:23:45 +01:00
* @ var ContratLigne [] Contract lines
2014-12-28 19:16:14 +01:00
*/
2019-11-12 09:46:08 +01:00
public $lines = array ();
2009-01-25 18:05:40 +01:00
2021-08-26 21:35:31 +02:00
public $nbofservices ;
public $nbofserviceswait ;
public $nbofservicesopened ;
public $nbofservicesexpired ;
//public $lower_planned_end_date;
//public $higher_planner_end_date;
2016-11-19 19:38:06 +01:00
/**
* Maps ContratLigne IDs to $this -> lines indexes
* @ var int []
*/
2019-11-12 09:46:08 +01:00
protected $lines_id_index_mapper = array ();
2016-11-19 19:38:06 +01:00
2008-10-07 01:21:00 +02: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-25 19:02:36 +01:00
'rowid' => array ( 'type' => 'integer' , 'label' => 'TechnicalID' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 10 ),
'ref' => array ( 'type' => 'varchar(50)' , 'label' => 'Ref' , 'enabled' => 1 , 'visible' =>- 1 , 'showoncombobox' => 1 , 'position' => 15 ),
'ref_ext' => array ( 'type' => 'varchar(255)' , 'label' => 'Ref ext' , 'enabled' => 1 , 'visible' => 0 , 'position' => 20 ),
'ref_supplier' => array ( 'type' => 'varchar(50)' , 'label' => 'Ref supplier' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 25 ),
'entity' => array ( 'type' => 'integer' , 'label' => 'Entity' , 'default' => 1 , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' => 1 , 'position' => 30 , 'index' => 1 ),
'tms' => array ( 'type' => 'timestamp' , 'label' => 'DateModification' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 35 ),
'datec' => array ( 'type' => 'datetime' , 'label' => 'DateCreation' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 40 ),
'date_contrat' => array ( 'type' => 'datetime' , 'label' => 'Date contrat' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 45 ),
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' => 70 ),
2022-06-14 17:53:17 +02:00
'fk_projet' => array ( 'type' => 'integer:Project:projet/class/project.class.php:1:fk_statut=1' , 'label' => 'Project' , 'enabled' => '$conf->project->enabled' , 'visible' =>- 1 , 'position' => 75 ),
2022-03-22 13:23:16 +01:00
'fk_commercial_signature' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'SaleRepresentative Signature' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 80 ),
'fk_commercial_suivi' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'SaleRepresentative follower' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 85 ),
'fk_user_author' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'UserAuthor' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 90 ),
2022-09-09 01:39:52 +02:00
'note_public' => array ( 'type' => 'text' , 'label' => 'NotePublic' , 'enabled' => 1 , 'visible' => 0 , 'position' => 105 ),
'note_private' => array ( 'type' => 'text' , 'label' => 'NotePrivate' , 'enabled' => 1 , 'visible' => 0 , 'position' => 110 ),
2020-01-25 19:02:36 +01:00
'model_pdf' => array ( 'type' => 'varchar(255)' , 'label' => 'Model pdf' , 'enabled' => 1 , 'visible' => 0 , 'position' => 115 ),
'import_key' => array ( 'type' => 'varchar(14)' , 'label' => 'ImportId' , 'enabled' => 1 , 'visible' =>- 2 , 'position' => 120 ),
'extraparams' => array ( 'type' => 'varchar(255)' , 'label' => 'Extraparams' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 125 ),
2020-01-25 19:21:37 +01:00
'ref_customer' => array ( 'type' => 'varchar(50)' , 'label' => 'Ref customer' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 130 ),
'fk_user_modif' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'UserModif' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' =>- 1 , 'position' => 135 ),
2020-01-25 19:02:36 +01:00
'last_main_doc' => array ( 'type' => 'varchar(255)' , 'label' => 'Last main doc' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 140 ),
2022-03-22 13:23:16 +01:00
'statut' => array ( 'type' => 'smallint(6)' , 'label' => 'Statut' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 500 , 'notnull' => 1 , 'arrayofkeyval' => array ( 0 => 'Draft' , 1 => 'Validated' , 2 => 'Closed' ))
2020-01-25 19:02:36 +01:00
);
2020-01-29 22:38:14 +01:00
// END MODULEBUILDER PROPERTIES
2020-01-25 19:02:36 +01:00
2019-11-01 15:53:57 +01:00
const STATUS_DRAFT = 0 ;
const STATUS_VALIDATED = 1 ;
const STATUS_CLOSED = 2 ;
2008-10-07 01:21:00 +02:00
/**
2011-09-11 20:35:38 +02:00
* Constructor
*
2012-01-04 21:47:40 +01:00
* @ param DoliDB $db Database handler
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function __construct ( $db )
2008-10-07 01:21:00 +02:00
{
2012-01-04 21:47:40 +01:00
$this -> db = $db ;
2008-10-07 01:21:00 +02:00
}
2011-02-18 20:55:48 +01:00
/**
* Return next contract ref
2012-01-04 21:47:40 +01:00
*
2014-12-20 15:42:03 +01:00
* @ param Societe $soc Thirdparty object
2012-01-04 21:47:40 +01:00
* @ return string free reference for contract
2011-02-18 20:55:48 +01:00
*/
2019-02-25 20:35:59 +01:00
public function getNextNumRef ( $soc )
2011-02-18 20:55:48 +01:00
{
global $db , $langs , $conf ;
2013-08-26 15:42:01 +02:00
$langs -> load ( " contracts " );
2011-02-18 20:55:48 +01:00
2021-02-23 21:24:38 +01:00
if ( ! empty ( $conf -> global -> CONTRACT_ADDON )) {
2015-01-17 18:58:05 +01:00
$mybool = false ;
2011-02-18 20:55:48 +01:00
2015-01-17 18:58:05 +01:00
$file = $conf -> global -> CONTRACT_ADDON . " .php " ;
$classname = $conf -> global -> CONTRACT_ADDON ;
2011-02-18 20:55:48 +01:00
2015-01-17 18:58:05 +01:00
// Include file with class
$dirmodels = array_merge ( array ( '/' ), ( array ) $conf -> modules_parts [ 'models' ]);
2011-02-18 20:55:48 +01:00
2015-01-17 18:58:05 +01:00
foreach ( $dirmodels as $reldir ) {
$dir = dol_buildpath ( $reldir . " core/modules/contract/ " );
// Load file with numbering class (if found)
2019-11-12 09:46:08 +01:00
$mybool |= @ include_once $dir . $file ;
2015-01-17 18:58:05 +01:00
}
2011-05-03 20:13:07 +02:00
2021-02-23 21:24:38 +01:00
if ( ! $mybool ) {
2019-01-27 11:55:16 +01:00
dol_print_error ( '' , " Failed to include file " . $file );
2015-01-17 18:58:05 +01:00
return '' ;
}
$obj = new $classname ();
2019-01-27 11:55:16 +01:00
$numref = $obj -> getNextValue ( $soc , $this );
2011-02-20 23:53:59 +01:00
2021-02-23 21:24:38 +01:00
if ( $numref != " " ) {
2011-02-20 23:53:59 +01:00
return $numref ;
2020-05-21 15:05:19 +02:00
} else {
2015-01-17 18:58:05 +01:00
$this -> error = $obj -> error ;
2019-01-27 11:55:16 +01:00
dol_print_error ( $db , get_class ( $this ) . " ::getNextValue " . $obj -> error );
2011-02-18 20:55:48 +01:00
return " " ;
}
2020-05-21 15:05:19 +02:00
} else {
2015-01-17 18:58:05 +01:00
$langs -> load ( " errors " );
2019-08-01 12:23:02 +02:00
print $langs -> trans ( " Error " ) . " " . $langs -> trans ( " ErrorModuleSetupNotComplete " , $langs -> transnoentitiesnoconv ( " Contract " ));
2011-02-18 20:55:48 +01:00
return " " ;
2015-01-17 18:58:05 +01:00
}
2011-02-18 20:55:48 +01:00
}
2011-02-20 23:53:59 +01:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2008-10-07 01:21:00 +02:00
/**
2012-01-04 21:47:40 +01:00
* Activate a contract line
*
2015-04-29 19:57:01 +02:00
* @ param User $user Objet User who activate contract
* @ param int $line_id Id of line to activate
2018-08-29 11:06:20 +02:00
* @ param int $date Opening date
* @ param int | string $date_end Expected end date
2012-03-18 19:23:01 +01:00
* @ param string $comment A comment typed by user
* @ return int < 0 if KO , > 0 if OK
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function active_line ( $user , $line_id , $date , $date_end = '' , $comment = '' )
2008-10-07 01:21:00 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2018-04-23 10:35:11 +02:00
$result = $this -> lines [ $this -> lines_id_index_mapper [ $line_id ]] -> active_line ( $user , $date , $date_end , $comment );
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
2018-04-23 10:35:11 +02:00
$this -> error = $this -> lines [ $this -> lines_id_index_mapper [ $line_id ]] -> error ;
$this -> errors = $this -> lines [ $this -> lines_id_index_mapper [ $line_id ]] -> errors ;
}
return $result ;
2008-10-07 01:21:00 +02:00
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2008-10-07 01:21:00 +02:00
/**
2012-01-04 21:47:40 +01:00
* Close a contract line
*
2015-04-29 19:57:01 +02:00
* @ param User $user Objet User who close contract
* @ param int $line_id Id of line to close
2018-08-29 11:06:20 +02:00
* @ param int $date_end End date
2012-03-18 19:23:01 +01:00
* @ param string $comment A comment typed by user
* @ return int < 0 if KO , > 0 if OK
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function close_line ( $user , $line_id , $date_end , $comment = '' )
2008-10-07 01:21:00 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2019-11-12 09:46:08 +01:00
$result = $this -> lines [ $this -> lines_id_index_mapper [ $line_id ]] -> close_line ( $user , $date_end , $comment );
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
2018-04-23 10:35:11 +02:00
$this -> error = $this -> lines [ $this -> lines_id_index_mapper [ $line_id ]] -> error ;
$this -> errors = $this -> lines [ $this -> lines_id_index_mapper [ $line_id ]] -> errors ;
}
return $result ;
2008-10-07 01:21:00 +02:00
}
2017-09-03 14:26:27 +02:00
/**
* Open all lines of a contract
*
* @ param User $user Object User making action
* @ param int | string $date_start Date start ( now if empty )
2020-10-31 14:32:18 +01:00
* @ param int $notrigger 1 = Does not execute triggers , 0 = Execute triggers
* @ param string $comment Comment
2017-09-03 15:40:36 +02:00
* @ return int < 0 if KO , > 0 if OK
2019-04-08 16:04:15 +02:00
* @ see ()
2017-09-03 14:26:27 +02:00
*/
2019-02-25 20:35:59 +01:00
public function activateAll ( $user , $date_start = '' , $notrigger = 0 , $comment = '' )
2017-09-03 14:26:27 +02:00
{
2021-02-23 21:24:38 +01:00
if ( empty ( $date_start )) {
$date_start = dol_now ();
}
2017-09-03 14:26:27 +02:00
$this -> db -> begin ();
2019-11-12 09:46:08 +01:00
$error = 0 ;
2018-10-01 13:54:45 +02:00
2017-09-03 14:26:27 +02:00
// Load lines
$this -> fetch_lines ();
2021-02-23 21:24:38 +01:00
foreach ( $this -> lines as $contratline ) {
2017-09-03 14:26:27 +02:00
// Open lines not already open
2021-02-23 21:24:38 +01:00
if ( $contratline -> statut != ContratLigne :: STATUS_OPEN ) {
2018-02-20 13:20:24 +01:00
$contratline -> context = $this -> context ;
2022-01-27 20:38:38 +01:00
$result = $contratline -> active_line ( $user , $date_start , - 1 , $comment ); // This call trigger LINECONTRACT_ACTIVATE
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
2018-02-16 01:19:31 +01:00
$error ++ ;
2018-02-17 01:16:52 +01:00
$this -> error = $contratline -> error ;
2018-02-16 01:19:31 +01:00
$this -> errors = $contratline -> errors ;
2017-09-03 14:26:27 +02:00
break ;
}
}
}
2021-02-23 21:24:38 +01:00
if ( ! $error && $this -> statut == 0 ) {
2019-11-12 09:46:08 +01:00
$result = $this -> validate ( $user , '' , $notrigger );
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-09-03 14:26:27 +02:00
}
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2017-09-03 14:26:27 +02:00
$this -> db -> commit ();
2017-09-03 16:07:21 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-09-03 14:26:27 +02:00
$this -> db -> rollback ();
2017-09-03 15:40:36 +02:00
return - 1 ;
2017-09-03 14:26:27 +02:00
}
}
2008-10-07 01:21:00 +02:00
/**
2017-10-26 12:32:22 +02:00
* Close all lines of a contract
2012-01-04 21:47:40 +01:00
*
2017-10-26 12:32:22 +02:00
* @ param User $user Object User making action
2020-10-31 14:32:18 +01:00
* @ param int $notrigger 1 = Does not execute triggers , 0 = Execute triggers
* @ param string $comment Comment
2017-10-26 12:32:22 +02:00
* @ return int < 0 if KO , > 0 if OK
2019-04-08 16:04:15 +02:00
* @ see activateAll ()
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function closeAll ( User $user , $notrigger = 0 , $comment = '' )
2012-07-25 11:46:33 +02:00
{
2008-12-31 15:44:10 +01:00
$this -> db -> begin ();
2009-01-25 18:05:40 +01:00
2008-12-31 15:44:10 +01:00
// Load lines
2010-12-15 08:12:28 +01:00
$this -> fetch_lines ();
2008-10-07 01:21:00 +02:00
2017-11-25 12:11:02 +01:00
$now = dol_now ();
2018-02-16 01:19:31 +01:00
$error = 0 ;
2021-02-23 21:24:38 +01:00
foreach ( $this -> lines as $contratline ) {
2017-09-03 14:26:27 +02:00
// Close lines not already closed
2021-02-23 21:24:38 +01:00
if ( $contratline -> statut != ContratLigne :: STATUS_CLOSED ) {
2021-08-26 21:35:31 +02:00
$contratline -> date_end_real = $now ;
$contratline -> date_cloture = $now ; // For backward compatibility
2019-11-12 09:46:08 +01:00
$contratline -> fk_user_cloture = $user -> id ;
$contratline -> statut = ContratLigne :: STATUS_CLOSED ;
$result = $contratline -> close_line ( $user , $now , $comment , $notrigger );
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
2018-02-16 01:19:31 +01:00
$error ++ ;
2018-02-17 01:16:52 +01:00
$this -> error = $contratline -> error ;
2018-02-16 01:19:31 +01:00
$this -> errors = $contratline -> errors ;
2008-12-31 15:44:10 +01:00
break ;
}
2020-10-31 14:32:18 +01:00
}
2008-10-07 01:21:00 +02:00
}
2009-01-25 18:05:40 +01:00
2021-02-23 21:24:38 +01:00
if ( ! $error && $this -> statut == 0 ) {
2019-11-12 09:46:08 +01:00
$result = $this -> validate ( $user , '' , $notrigger );
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2009-07-08 12:10:35 +02:00
}
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2020-10-31 14:32:18 +01:00
$this -> db -> commit ();
return 1 ;
} else {
$this -> db -> rollback ();
return - 1 ;
}
2008-10-07 01:21:00 +02:00
}
/**
2013-10-18 10:17:06 +02:00
* Validate a contract
2012-01-04 21:47:40 +01:00
*
2013-10-18 10:17:06 +02:00
* @ param User $user Objet User
* @ param string $force_number Reference to force on contract ( not implemented yet )
2020-10-31 14:32:18 +01:00
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
2013-10-18 10:17:06 +02:00
* @ return int < 0 if KO , > 0 if OK
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function validate ( User $user , $force_number = '' , $notrigger = 0 )
2008-10-07 01:21:00 +02:00
{
2013-09-12 16:55:49 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2012-06-10 10:29:43 +02:00
global $langs , $conf ;
2012-07-25 11:46:33 +02:00
2019-11-12 09:46:08 +01:00
$now = dol_now ();
2013-10-18 10:17:06 +02:00
2019-11-12 09:46:08 +01:00
$error = 0 ;
2013-10-18 10:17:06 +02:00
dol_syslog ( get_class ( $this ) . '::validate user=' . $user -> id . ', force_number=' . $force_number );
2013-04-23 23:44:49 +02:00
2013-10-18 10:17:06 +02:00
$this -> db -> begin ();
$this -> fetch_thirdparty ();
// A contract is validated so we can move thirdparty to status customer
2021-02-23 21:24:38 +01:00
if ( empty ( $conf -> global -> CONTRACT_DISABLE_AUTOSET_AS_CLIENT_ON_CONTRACT_VALIDATION )) {
2019-11-12 09:46:08 +01:00
$result = $this -> thirdparty -> set_as_client ();
2018-06-27 01:31:53 +02:00
}
2013-04-23 23:44:49 +02:00
2013-03-02 16:50:36 +01:00
// Define new ref
2021-02-23 21:24:38 +01:00
if ( $force_number ) {
2018-01-18 14:07:32 +01:00
$num = $force_number ;
2021-02-23 21:24:38 +01:00
} elseif ( ! $error && ( preg_match ( '/^[\(]?PROV/i' , $this -> ref ) || empty ( $this -> ref ))) { // empty should not happened, but when it occurs, the test save life
2013-10-18 10:17:06 +02:00
$num = $this -> getNextNumRef ( $this -> thirdparty );
2020-05-21 15:05:19 +02:00
} else {
2013-03-02 16:50:36 +01:00
$num = $this -> ref ;
}
2020-10-31 14:32:18 +01:00
$this -> newref = dol_sanitizeFileName ( $num );
2012-01-04 21:47:40 +01:00
2021-02-23 21:24:38 +01:00
if ( $num ) {
2020-09-19 23:30:29 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " contrat SET ref = ' " . $this -> db -> escape ( $num ) . " ', statut = 1 " ;
2013-10-18 10:17:06 +02:00
//$sql.= ", fk_user_valid = ".$user->id.", date_valid = '".$this->db->idate($now)."'";
2021-08-27 18:18:50 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id ) . " AND statut = 0 " ;
2013-10-18 10:17:06 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::validate " , LOG_DEBUG );
2013-10-18 10:17:06 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( ! $resql ) {
2013-10-18 10:17:06 +02:00
dol_print_error ( $this -> db );
$error ++ ;
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> lasterror ();
2013-10-18 10:17:06 +02:00
}
2014-10-27 03:24:45 +01:00
// Trigger calls
2021-02-23 21:24:38 +01:00
if ( ! $error && ! $notrigger ) {
2020-10-31 14:32:18 +01:00
// Call trigger
$result = $this -> call_trigger ( 'CONTRACT_VALIDATE' , $user );
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2020-10-31 14:32:18 +01:00
// End call triggers
2014-10-27 03:24:45 +01:00
}
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2020-10-31 14:32:18 +01:00
$this -> oldref = $this -> ref ;
2013-10-18 10:17:06 +02:00
2013-09-12 16:55:49 +02:00
// Rename directory if dir was a temporary ref
2021-02-23 21:24:38 +01:00
if ( preg_match ( '/^[\(]?PROV/i' , $this -> ref )) {
2019-07-28 22:26:55 +02:00
// Now we rename also files into index
2019-11-12 09:46:08 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . " ecm_files set filename = CONCAT(' " . $this -> db -> escape ( $this -> newref ) . " ', SUBSTR(filename, " . ( strlen ( $this -> ref ) + 1 ) . " )), filepath = 'contract/ " . $this -> db -> escape ( $this -> newref ) . " ' " ;
$sql .= " WHERE filename LIKE ' " . $this -> db -> escape ( $this -> ref ) . " %' AND filepath = 'contract/ " . $this -> db -> escape ( $this -> ref ) . " ' and entity = " . $conf -> entity ;
2019-07-28 22:26:55 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( ! $resql ) {
$error ++ ; $this -> error = $this -> db -> lasterror ();
}
2019-07-28 22:26:55 +02:00
// We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
2014-10-27 03:24:45 +01:00
$oldref = dol_sanitizeFileName ( $this -> ref );
$newref = dol_sanitizeFileName ( $num );
$dirsource = $conf -> contract -> dir_output . '/' . $oldref ;
$dirdest = $conf -> contract -> dir_output . '/' . $newref ;
2021-02-23 21:24:38 +01:00
if ( ! $error && file_exists ( $dirsource )) {
2013-09-12 16:55:49 +02:00
dol_syslog ( get_class ( $this ) . " ::validate rename dir " . $dirsource . " into " . $dirdest );
2013-10-18 10:17:06 +02:00
2021-02-23 21:24:38 +01:00
if ( @ rename ( $dirsource , $dirdest )) {
2013-09-12 16:55:49 +02:00
dol_syslog ( " Rename ok " );
2020-10-31 14:32:18 +01:00
// Rename docs starting with $oldref with $newref
$listoffiles = dol_dir_list ( $conf -> contract -> dir_output . '/' . $newref , 'files' , 1 , '^' . preg_quote ( $oldref , '/' ));
2021-02-23 21:24:38 +01:00
foreach ( $listoffiles as $fileentry ) {
2020-10-31 14:32:18 +01:00
$dirsource = $fileentry [ 'name' ];
$dirdest = preg_replace ( '/^' . preg_quote ( $oldref , '/' ) . '/' , $newref , $dirsource );
$dirsource = $fileentry [ 'path' ] . '/' . $dirsource ;
$dirdest = $fileentry [ 'path' ] . '/' . $dirdest ;
@ rename ( $dirsource , $dirdest );
}
2013-09-12 16:55:49 +02:00
}
}
}
2013-10-18 10:17:06 +02:00
}
2008-10-07 01:21:00 +02:00
2013-10-18 10:17:06 +02:00
// Set new ref and define current statut
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2013-10-18 10:17:06 +02:00
$this -> ref = $num ;
2017-11-25 12:11:02 +01:00
$this -> statut = 1 ;
$this -> brouillon = 0 ;
$this -> date_validation = $now ;
2013-09-12 16:55:49 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2013-10-18 10:17:06 +02:00
$error ++ ;
}
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2013-10-18 10:17:06 +02:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2013-10-18 10:17:06 +02:00
$this -> db -> rollback ();
2008-10-07 01:21:00 +02:00
return - 1 ;
}
}
2016-01-26 18:22:37 +01:00
/**
* Unvalidate a contract
*
2018-08-29 11:06:20 +02:00
* @ param User $user Object User
2020-10-31 14:32:18 +01:00
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
2016-01-26 18:22:37 +01:00
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 20:35:59 +01:00
public function reopen ( $user , $notrigger = 0 )
2016-01-26 18:22:37 +01:00
{
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
global $langs , $conf ;
2019-11-12 09:46:08 +01:00
$now = dol_now ();
2016-01-26 18:22:37 +01:00
2019-11-12 09:46:08 +01:00
$error = 0 ;
2016-01-26 18:22:37 +01:00
dol_syslog ( get_class ( $this ) . '::reopen user=' . $user -> id );
$this -> db -> begin ();
$this -> fetch_thirdparty ();
$sql = " UPDATE " . MAIN_DB_PREFIX . " contrat SET statut = 0 " ;
//$sql.= ", fk_user_valid = null, date_valid = null";
2021-08-27 18:18:50 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id ) . " AND statut = 1 " ;
2008-10-07 01:21:00 +02:00
2016-01-26 18:22:37 +01:00
dol_syslog ( get_class ( $this ) . " ::validate " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( ! $resql ) {
2016-01-26 18:22:37 +01:00
dol_print_error ( $this -> db );
$error ++ ;
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> lasterror ();
2016-01-26 18:22:37 +01:00
}
// Trigger calls
2021-02-23 21:24:38 +01:00
if ( ! $error && ! $notrigger ) {
2016-01-26 18:22:37 +01:00
// Call trigger
2019-11-12 09:46:08 +01:00
$result = $this -> call_trigger ( 'CONTRACT_REOPEN' , $user );
2016-01-26 18:22:37 +01:00
if ( $result < 0 ) {
$error ++ ;
}
// End call triggers
}
2008-10-07 01:21:00 +02:00
2018-08-29 11:06:20 +02:00
// Set new ref and define current status
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2019-11-12 09:46:08 +01:00
$this -> statut = 0 ;
$this -> brouillon = 1 ;
$this -> date_validation = $now ;
2016-01-26 18:22:37 +01:00
}
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2016-01-26 18:22:37 +01:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2016-01-26 18:22:37 +01:00
$this -> db -> rollback ();
return - 1 ;
}
}
2016-10-14 07:54:44 +02:00
2008-10-07 01:21:00 +02:00
/**
2010-11-01 15:35:17 +01:00
* Load a contract from database
2012-01-04 21:47:40 +01:00
*
2017-09-19 22:03:44 +02:00
* @ param int $id Id of contract to load
* @ param string $ref Ref
* @ param string $ref_customer Customer ref
* @ param string $ref_supplier Supplier ref
2019-08-23 13:28:26 +02:00
* @ return int < 0 if KO , 0 if not found or if two records found for same ref , Id of contract if OK
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function fetch ( $id , $ref = '' , $ref_customer = '' , $ref_supplier = '' )
2008-10-07 01:21:00 +02:00
{
2020-02-07 13:15:51 +01:00
$sql = " SELECT rowid, statut, ref, fk_soc, " ;
2019-11-12 09:46:08 +01:00
$sql .= " ref_supplier, ref_customer, " ;
$sql .= " ref_ext, " ;
2020-10-31 14:32:18 +01:00
$sql .= " entity, " ;
2020-02-07 13:15:51 +01:00
$sql .= " date_contrat as datecontrat, " ;
2020-02-07 13:37:37 +01:00
$sql .= " fk_user_author, " ;
2019-11-12 09:46:08 +01:00
$sql .= " fk_projet as fk_project, " ;
$sql .= " fk_commercial_signature, fk_commercial_suivi, " ;
$sql .= " note_private, note_public, model_pdf, extraparams " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " contrat " ;
2021-02-23 21:24:38 +01:00
if ( ! $id ) {
$sql .= " WHERE entity IN ( " . getEntity ( 'contract' ) . " ) " ;
} else {
$sql .= " WHERE rowid= " . ( int ) $id ;
}
if ( $ref_customer ) {
2019-11-12 09:46:08 +01:00
$sql .= " AND ref_customer = ' " . $this -> db -> escape ( $ref_customer ) . " ' " ;
2017-09-19 22:03:44 +02:00
}
2021-02-23 21:24:38 +01:00
if ( $ref_supplier ) {
2019-11-12 09:46:08 +01:00
$sql .= " AND ref_supplier = ' " . $this -> db -> escape ( $ref_supplier ) . " ' " ;
2017-09-19 22:03:44 +02:00
}
2021-02-23 21:24:38 +01:00
if ( $ref ) {
2019-11-12 09:46:08 +01:00
$sql .= " AND ref=' " . $this -> db -> escape ( $ref ) . " ' " ;
2012-05-03 18:40:52 +02:00
}
2008-10-07 01:21:00 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2011-09-20 15:32:16 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( $resql ) {
2019-11-12 09:46:08 +01:00
$num = $this -> db -> num_rows ( $resql );
2021-02-23 21:24:38 +01:00
if ( $num > 1 ) {
2019-11-12 09:46:08 +01:00
$this -> error = 'Fetch found several records.' ;
2019-08-23 13:28:26 +02:00
dol_syslog ( $this -> error , LOG_ERR );
$result = - 2 ;
2021-02-23 21:24:38 +01:00
} elseif ( $num ) { // $num = 1
2019-08-23 13:28:26 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2021-02-23 21:24:38 +01:00
if ( $obj ) {
2019-11-12 09:46:08 +01:00
$this -> id = $obj -> rowid ;
$this -> ref = ( ! isset ( $obj -> ref ) || ! $obj -> ref ) ? $obj -> rowid : $obj -> ref ;
2020-01-22 10:23:31 +01:00
$this -> ref_customer = $obj -> ref_customer ;
$this -> ref_supplier = $obj -> ref_supplier ;
2019-11-12 09:46:08 +01:00
$this -> ref_ext = $obj -> ref_ext ;
2020-10-31 14:32:18 +01:00
$this -> entity = $obj -> entity ;
2019-11-12 09:46:08 +01:00
$this -> statut = $obj -> statut ;
2014-12-28 18:10:30 +01:00
2020-01-22 10:23:31 +01:00
$this -> date_contrat = $this -> db -> jdate ( $obj -> datecontrat );
$this -> date_creation = $this -> db -> jdate ( $obj -> datecontrat );
2008-10-07 01:21:00 +02:00
2019-11-12 09:46:08 +01:00
$this -> user_author_id = $obj -> fk_user_author ;
2008-10-07 01:21:00 +02:00
2019-08-23 13:28:26 +02:00
$this -> commercial_signature_id = $obj -> fk_commercial_signature ;
2019-11-12 09:46:08 +01:00
$this -> commercial_suivi_id = $obj -> fk_commercial_suivi ;
2008-10-07 01:21:00 +02:00
2019-11-12 09:46:08 +01:00
$this -> note_private = $obj -> note_private ;
$this -> note_public = $obj -> note_public ;
2020-08-18 14:48:38 +02:00
$this -> model_pdf = $obj -> model_pdf ;
2020-11-09 15:18:56 +01:00
$this -> modelpdf = $obj -> model_pdf ; // deprecated
2008-10-07 01:21:00 +02:00
2020-01-22 10:23:31 +01:00
$this -> fk_projet = $obj -> fk_project ; // deprecated
2019-11-12 09:46:08 +01:00
$this -> fk_project = $obj -> fk_project ;
2008-10-07 01:21:00 +02:00
2019-11-12 09:46:08 +01:00
$this -> socid = $obj -> fk_soc ;
$this -> fk_soc = $obj -> fk_soc ;
2012-03-18 19:23:01 +01:00
2019-08-23 13:28:26 +02:00
$this -> extraparams = ( array ) json_decode ( $obj -> extraparams , true );
2008-10-07 01:21:00 +02:00
2019-08-23 13:28:26 +02:00
$this -> db -> free ( $resql );
2014-07-19 20:45:02 +02:00
2020-01-22 10:23:31 +01:00
// Retrieve all extrafields
2019-08-23 13:28:26 +02:00
// fetch optionals attributes and labels
$this -> fetch_optionals ();
2018-02-20 19:20:03 +01:00
2019-08-23 13:28:26 +02:00
// Lines
2019-11-12 09:46:08 +01:00
$result = $this -> fetch_lines ();
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> lasterror ();
2019-08-23 13:28:26 +02:00
return - 3 ;
}
2008-10-07 01:21:00 +02:00
2019-08-23 13:28:26 +02:00
return $this -> id ;
}
2020-05-21 15:05:19 +02:00
} else {
2018-06-20 23:50:50 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch Contract not found " );
2019-11-12 09:46:08 +01:00
$this -> error = " Contract not found " ;
2017-09-19 22:03:44 +02:00
return 0 ;
2008-10-07 01:21:00 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2018-06-20 23:50:50 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch Error searching contract " );
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> error ();
2008-10-07 01:21:00 +02:00
return - 1 ;
}
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2008-10-07 01:21:00 +02:00
/**
2018-03-27 23:01:54 +02:00
* Load lines array into this -> lines .
* This set also nbofserviceswait , nbofservicesopened , nbofservicesexpired and nbofservicesclosed
2012-01-04 21:47:40 +01:00
*
2022-03-28 13:50:47 +02:00
* @ param int $only_services 0 = Default , 1 = Force only services ( depending on setup , we may also have physical products in a contract )
* @ param int $loadalsotranslation 0 = Default , 1 = Load also translations of product descriptions
* @ return ContratLigne [] Return array of contract lines
2008-10-07 01:21:00 +02:00
*/
2022-03-28 13:50:47 +02:00
public function fetch_lines ( $only_services = 0 , $loadalsotranslation = 0 )
2008-10-07 01:21:00 +02:00
{
2019-10-06 14:41:52 +02:00
// phpcs:enable
2022-03-28 13:50:47 +02:00
global $langs , $conf ;
2019-10-06 14:41:52 +02:00
2021-08-26 21:35:31 +02:00
$this -> nbofservices = 0 ;
2019-11-12 09:46:08 +01:00
$this -> nbofserviceswait = 0 ;
$this -> nbofservicesopened = 0 ;
$this -> nbofservicesexpired = 0 ;
$this -> nbofservicesclosed = 0 ;
2008-10-07 01:21:00 +02:00
2019-11-12 09:46:08 +01:00
$total_ttc = 0 ;
$total_vat = 0 ;
$total_ht = 0 ;
2010-11-01 15:35:17 +01:00
2019-11-12 09:46:08 +01:00
$now = dol_now ();
2009-02-09 20:31:06 +01:00
2019-11-12 09:46:08 +01:00
$this -> lines = array ();
2020-10-31 14:32:18 +01:00
$pos = 0 ;
2017-06-01 13:32:20 +02:00
2018-08-29 11:06:20 +02:00
// Selects contract lines related to a product
2020-10-15 21:00:49 +02:00
$sql = " SELECT p.label as product_label, p.description as product_desc, p.ref as product_ref, p.fk_product_type as product_type, " ;
2019-11-12 09:46:08 +01:00
$sql .= " d.rowid, d.fk_contrat, d.statut, d.description, d.price_ht, d.vat_src_code, d.tva_tx, d.localtax1_tx, d.localtax2_tx, d.localtax1_type, d.localtax2_type, d.qty, d.remise_percent, d.subprice, d.fk_product_fournisseur_price as fk_fournprice, d.buy_price_ht as pa_ht, " ;
$sql .= " d.total_ht, " ;
$sql .= " d.total_tva, " ;
$sql .= " d.total_localtax1, " ;
$sql .= " d.total_localtax2, " ;
$sql .= " d.total_ttc, " ;
$sql .= " d.info_bits, d.fk_product, " ;
$sql .= " d.date_ouverture_prevue, d.date_ouverture, " ;
$sql .= " d.date_fin_validite, d.date_cloture, " ;
$sql .= " d.fk_user_author, " ;
$sql .= " d.fk_user_ouverture, " ;
$sql .= " d.fk_user_cloture, " ;
2020-10-15 20:38:32 +02:00
$sql .= " d.fk_unit, " ;
2020-10-15 21:00:49 +02:00
$sql .= " d.product_type as type " ;
2019-11-12 09:46:08 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " contratdet as d LEFT JOIN " . MAIN_DB_PREFIX . " product as p ON d.fk_product = p.rowid " ;
2021-08-23 19:33:24 +02:00
$sql .= " WHERE d.fk_contrat = " . (( int ) $this -> id );
2022-03-28 13:50:47 +02:00
if ( $only_services == 1 ) {
$sql .= " AND d.product_type = 1 " ;
}
2019-11-12 09:46:08 +01:00
$sql .= " ORDER by d.rowid ASC " ;
2008-10-07 01:21:00 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch_lines " , LOG_DEBUG );
2008-10-07 01:21:00 +02:00
$result = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( $result ) {
2008-10-07 01:21:00 +02:00
$num = $this -> db -> num_rows ( $result );
$i = 0 ;
2021-02-23 21:24:38 +01:00
while ( $i < $num ) {
2019-02-25 20:35:59 +01:00
$objp = $this -> db -> fetch_object ( $result );
2010-12-15 08:12:28 +01:00
2019-11-12 09:46:08 +01:00
$line = new ContratLigne ( $this -> db );
2022-03-28 13:50:47 +02:00
2019-11-12 09:46:08 +01:00
$line -> id = $objp -> rowid ;
2010-12-15 08:12:28 +01:00
$line -> ref = $objp -> rowid ;
2019-11-12 09:46:08 +01:00
$line -> fk_contrat = $objp -> fk_contrat ;
$line -> desc = $objp -> description ; // Description line
2010-12-15 08:12:28 +01:00
$line -> qty = $objp -> qty ;
2019-11-12 09:46:08 +01:00
$line -> vat_src_code = $objp -> vat_src_code ;
$line -> tva_tx = $objp -> tva_tx ;
2010-12-15 08:12:28 +01:00
$line -> localtax1_tx = $objp -> localtax1_tx ;
$line -> localtax2_tx = $objp -> localtax2_tx ;
2017-05-04 01:05:55 +02:00
$line -> localtax1_type = $objp -> localtax1_type ;
$line -> localtax2_type = $objp -> localtax2_type ;
2010-12-15 08:12:28 +01:00
$line -> subprice = $objp -> subprice ;
2019-11-12 09:46:08 +01:00
$line -> statut = $objp -> statut ;
2010-12-15 08:12:28 +01:00
$line -> remise_percent = $objp -> remise_percent ;
$line -> price_ht = $objp -> price_ht ;
2019-11-12 09:46:08 +01:00
$line -> price = $objp -> price_ht ; // For backward compatibility
2010-12-15 08:12:28 +01:00
$line -> total_ht = $objp -> total_ht ;
$line -> total_tva = $objp -> total_tva ;
$line -> total_localtax1 = $objp -> total_localtax1 ;
$line -> total_localtax2 = $objp -> total_localtax2 ;
$line -> total_ttc = $objp -> total_ttc ;
2019-11-12 09:46:08 +01:00
$line -> fk_product = (( $objp -> fk_product > 0 ) ? $objp -> fk_product : 0 );
2010-12-15 08:12:28 +01:00
$line -> info_bits = $objp -> info_bits ;
2020-10-31 14:32:18 +01:00
$line -> type = $objp -> type ;
2010-12-15 08:12:28 +01:00
2019-11-12 09:46:08 +01:00
$line -> fk_fournprice = $objp -> fk_fournprice ;
2022-03-28 13:50:47 +02:00
$marginInfos = getMarginInfos ( $objp -> subprice , $objp -> remise_percent , $objp -> tva_tx , $objp -> localtax1_tx , $objp -> localtax2_tx , $objp -> fk_fournprice , $objp -> pa_ht );
2019-11-12 09:46:08 +01:00
$line -> pa_ht = $marginInfos [ 0 ];
2013-02-13 09:50:08 +01:00
2019-11-12 09:46:08 +01:00
$line -> fk_user_author = $objp -> fk_user_author ;
$line -> fk_user_ouverture = $objp -> fk_user_ouverture ;
2020-01-30 01:48:28 +01:00
$line -> fk_user_cloture = $objp -> fk_user_cloture ;
2019-11-12 09:46:08 +01:00
$line -> fk_unit = $objp -> fk_unit ;
2010-12-15 08:12:28 +01:00
2019-11-12 09:46:08 +01:00
$line -> ref = $objp -> product_ref ; // deprecated
$line -> product_ref = $objp -> product_ref ; // Product Ref
2020-10-31 14:32:18 +01:00
$line -> product_type = $objp -> product_type ; // Product Type
$line -> product_desc = $objp -> product_desc ; // Product Description
2018-08-29 11:06:20 +02:00
$line -> product_label = $objp -> product_label ; // Product Label
2010-12-15 08:12:28 +01:00
2019-11-12 09:46:08 +01:00
$line -> description = $objp -> description ;
2010-12-15 08:12:28 +01:00
2017-10-26 12:32:22 +02:00
$line -> date_start = $this -> db -> jdate ( $objp -> date_ouverture_prevue );
$line -> date_start_real = $this -> db -> jdate ( $objp -> date_ouverture );
$line -> date_end = $this -> db -> jdate ( $objp -> date_fin_validite );
$line -> date_end_real = $this -> db -> jdate ( $objp -> date_cloture );
// For backward compatibility
2010-12-15 08:12:28 +01:00
$line -> date_ouverture_prevue = $this -> db -> jdate ( $objp -> date_ouverture_prevue );
$line -> date_ouverture = $this -> db -> jdate ( $objp -> date_ouverture );
$line -> date_fin_validite = $this -> db -> jdate ( $objp -> date_fin_validite );
$line -> date_cloture = $this -> db -> jdate ( $objp -> date_cloture );
$line -> date_debut_prevue = $this -> db -> jdate ( $objp -> date_ouverture_prevue );
$line -> date_debut_reel = $this -> db -> jdate ( $objp -> date_ouverture );
$line -> date_fin_prevue = $this -> db -> jdate ( $objp -> date_fin_validite );
$line -> date_fin_reel = $this -> db -> jdate ( $objp -> date_cloture );
2011-02-20 23:53:59 +01:00
2022-05-16 13:05:17 +02:00
// Retrieve all extrafields for contract line
2015-01-31 13:50:18 +01:00
// fetch optionals attributes and labels
2018-02-21 14:48:25 +01:00
$line -> fetch_optionals ();
2019-06-29 16:29:32 +02:00
2019-06-25 15:48:41 +02:00
// multilangs
2020-10-31 14:32:18 +01:00
if ( ! empty ( $conf -> global -> MAIN_MULTILANGS ) && ! empty ( $objp -> fk_product ) && ! empty ( $loadalsotranslation )) {
2022-05-18 23:52:43 +02:00
$tmpproduct = new Product ( $this -> db );
$tmpproduct -> fetch ( $objp -> fk_product );
$tmpproduct -> getMultiLangs ();
$line -> multilangs = $tmpproduct -> multilangs ;
2020-10-31 14:32:18 +01:00
}
2015-03-17 11:23:45 +01:00
2019-02-25 20:35:59 +01:00
$this -> lines [ $pos ] = $line ;
2022-03-28 13:50:47 +02:00
2017-03-03 14:00:38 +01:00
$this -> lines_id_index_mapper [ $line -> id ] = $pos ;
2011-02-20 23:53:59 +01:00
2010-12-15 08:12:28 +01:00
//dol_syslog("1 ".$line->desc);
//dol_syslog("2 ".$line->product_desc);
2021-02-23 21:24:38 +01:00
if ( $line -> statut == ContratLigne :: STATUS_INITIAL ) {
$this -> nbofserviceswait ++ ;
}
if ( $line -> statut == ContratLigne :: STATUS_OPEN && ( empty ( $line -> date_fin_prevue ) || $line -> date_fin_prevue >= $now )) {
$this -> nbofservicesopened ++ ;
}
if ( $line -> statut == ContratLigne :: STATUS_OPEN && ( ! empty ( $line -> date_fin_prevue ) && $line -> date_fin_prevue < $now )) {
$this -> nbofservicesexpired ++ ;
}
if ( $line -> statut == ContratLigne :: STATUS_CLOSED ) {
$this -> nbofservicesclosed ++ ;
}
2008-04-07 19:16:45 +02:00
2019-11-12 09:46:08 +01:00
$total_ttc += $objp -> total_ttc ; // TODO Not saved into database
2020-10-31 14:32:18 +01:00
$total_vat += $objp -> total_tva ;
$total_ht += $objp -> total_ht ;
2010-11-01 15:35:17 +01:00
2008-10-07 01:21:00 +02:00
$i ++ ;
2017-03-03 14:00:38 +01:00
$pos ++ ;
2008-10-07 01:21:00 +02:00
}
$this -> db -> free ( $result );
2020-05-21 15:05:19 +02:00
} else {
2019-07-05 21:50:35 +02:00
dol_syslog ( get_class ( $this ) . " ::Fetch Error when reading lines of contracts linked to products " );
2008-10-07 01:21:00 +02:00
return - 3 ;
}
2021-12-13 16:09:06 +01:00
// Now set the global properties on contract not stored into database.
2019-11-12 09:46:08 +01:00
$this -> nbofservices = count ( $this -> lines );
2021-12-13 16:09:06 +01:00
$this -> total_ttc = price2num ( $total_ttc );
$this -> total_tva = price2num ( $total_vat );
$this -> total_ht = price2num ( $total_ht );
2008-10-07 01:21:00 +02:00
2010-12-15 08:12:28 +01:00
return $this -> lines ;
2008-10-07 01:21:00 +02:00
}
/**
2012-01-04 21:47:40 +01:00
* Create a contract into database
*
* @ param User $user User that create
* @ return int < 0 if KO , id of contract if OK
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function create ( $user )
2008-10-07 01:21:00 +02:00
{
2019-11-12 09:46:08 +01:00
global $conf , $langs , $mysoc ;
2010-04-27 13:08:07 +02:00
2008-10-07 01:21:00 +02:00
// Check parameters
2019-11-12 09:46:08 +01:00
$paramsok = 1 ;
2021-02-23 21:24:38 +01:00
if ( $this -> commercial_signature_id <= 0 ) {
2008-10-07 01:21:00 +02:00
$langs -> load ( " commercial " );
2019-11-12 09:46:08 +01:00
$this -> error .= $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " SalesRepresentativeSignature " ));
$paramsok = 0 ;
2008-10-07 01:21:00 +02:00
}
2021-02-23 21:24:38 +01:00
if ( $this -> commercial_suivi_id <= 0 ) {
2008-10-07 01:21:00 +02:00
$langs -> load ( " commercial " );
2019-11-12 09:46:08 +01:00
$this -> error .= ( $this -> error ? " <br> " : '' );
$this -> error .= $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " SalesRepresentativeFollowUp " ));
$paramsok = 0 ;
2008-10-07 01:21:00 +02:00
}
2021-02-23 21:24:38 +01:00
if ( ! $paramsok ) {
return - 1 ;
}
2008-10-07 01:21:00 +02:00
2016-10-14 07:54:44 +02:00
2008-10-07 01:21:00 +02:00
$this -> db -> begin ();
2012-07-25 11:46:33 +02:00
2019-11-12 09:46:08 +01:00
$now = dol_now ();
2008-10-07 01:21:00 +02:00
// Insert contract
2010-01-12 10:48:19 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " contrat (datec, fk_soc, fk_user_author, date_contrat, " ;
2019-11-12 09:46:08 +01:00
$sql .= " fk_commercial_signature, fk_commercial_suivi, fk_projet, " ;
$sql .= " ref, entity, note_private, note_public, ref_customer, ref_supplier, ref_ext) " ;
2021-08-27 22:42:04 +02:00
$sql .= " VALUES (' " . $this -> db -> idate ( $now ) . " ', " . (( int ) $this -> socid ) . " , " . (( int ) $user -> id );
2019-11-12 09:46:08 +01:00
$sql .= " , " . ( dol_strlen ( $this -> date_contrat ) != 0 ? " ' " . $this -> db -> idate ( $this -> date_contrat ) . " ' " : " NULL " );
2021-08-27 22:42:04 +02:00
$sql .= " , " . ( $this -> commercial_signature_id > 0 ? (( int ) $this -> commercial_signature_id ) : " NULL " );
$sql .= " , " . ( $this -> commercial_suivi_id > 0 ? (( int ) $this -> commercial_suivi_id ) : " NULL " );
$sql .= " , " . ( $this -> fk_project > 0 ? (( int ) $this -> fk_project ) : " NULL " );
2019-11-12 09:46:08 +01:00
$sql .= " , " . ( dol_strlen ( $this -> ref ) <= 0 ? " null " : " ' " . $this -> db -> escape ( $this -> ref ) . " ' " );
2021-08-27 22:42:04 +02:00
$sql .= " , " . (( int ) $conf -> entity );
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 " );
$sql .= " , " . ( ! empty ( $this -> ref_customer ) ? ( " ' " . $this -> db -> escape ( $this -> ref_customer ) . " ' " ) : " NULL " );
$sql .= " , " . ( ! empty ( $this -> ref_supplier ) ? ( " ' " . $this -> db -> escape ( $this -> ref_supplier ) . " ' " ) : " NULL " );
$sql .= " , " . ( ! empty ( $this -> ref_ext ) ? ( " ' " . $this -> db -> escape ( $this -> ref_ext ) . " ' " ) : " NULL " );
$sql .= " ) " ;
$resql = $this -> db -> query ( $sql );
2019-03-08 23:58:04 +01:00
2021-02-23 21:24:38 +01:00
if ( $resql ) {
2019-11-12 09:46:08 +01:00
$error = 0 ;
2008-10-07 01:21:00 +02:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " contrat " );
2013-04-23 23:44:49 +02:00
2014-08-12 13:26:06 +02:00
// Load object modContract
2019-11-12 09:46:08 +01:00
$module = ( ! empty ( $conf -> global -> CONTRACT_ADDON ) ? $conf -> global -> CONTRACT_ADDON : 'mod_contract_serpis' );
2021-02-23 21:24:38 +01:00
if ( substr ( $module , 0 , 13 ) == 'mod_contract_' && substr ( $module , - 3 ) == 'php' ) {
2019-11-12 09:46:08 +01:00
$module = substr ( $module , 0 , dol_strlen ( $module ) - 4 );
2014-08-12 13:26:06 +02:00
}
2019-11-12 09:46:08 +01:00
$result = dol_include_once ( '/core/modules/contract/' . $module . '.php' );
2021-02-23 21:24:38 +01:00
if ( $result > 0 ) {
2014-08-12 13:26:06 +02:00
$modCodeContract = new $module ();
2019-11-12 09:46:08 +01:00
if ( ! empty ( $modCodeContract -> code_auto )) {
2019-03-08 23:58:04 +01:00
// Force the ref to a draft value if numbering module is an automatic numbering
2021-03-14 12:20:23 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . " contrat SET ref='(PROV " . $this -> id . " )' WHERE rowid= " . (( int ) $this -> id );
2021-02-23 21:24:38 +01:00
if ( $this -> db -> query ( $sql )) {
if ( $this -> id ) {
2019-11-12 09:46:08 +01:00
$this -> ref = " (PROV " . $this -> id . " ) " ;
2018-03-10 10:34:11 +01:00
}
2014-08-12 13:26:06 +02:00
}
2013-03-02 16:50:36 +01:00
}
2015-07-21 09:15:35 +02:00
}
2008-10-07 01:21:00 +02:00
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2019-11-12 09:46:08 +01:00
$result = $this -> insertExtraFields ();
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
2018-08-12 16:29:26 +02:00
$error ++ ;
}
2016-07-28 19:02:23 +02:00
}
2016-10-14 07:54:44 +02:00
2018-08-29 11:06:20 +02:00
// Insert business contacts ('SALESREPSIGN','contrat')
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2020-10-31 14:32:18 +01:00
$result = $this -> add_contact ( $this -> commercial_signature_id , 'SALESREPSIGN' , 'internal' );
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2016-07-28 19:02:23 +02:00
}
2008-10-07 01:21:00 +02:00
2018-08-29 11:06:20 +02:00
// Insert business contacts ('SALESREPFOLL','contrat')
2020-12-06 17:30:27 +01:00
if ( ! $error ) {
2020-10-31 14:32:18 +01:00
$result = $this -> add_contact ( $this -> commercial_suivi_id , 'SALESREPFOLL' , 'internal' );
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2014-07-05 11:17:50 +02:00
}
2008-10-07 01:21:00 +02:00
2020-12-06 17:30:27 +01:00
if ( ! $error ) {
2021-02-23 21:24:38 +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:24:38 +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-31 14:32:18 +01:00
$ret = $this -> add_object_linked ( $origin , $origin_id );
2021-02-23 21:24:38 +01:00
if ( ! $ret ) {
2020-10-31 14:32:18 +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:24:38 +01:00
if ( ! $ret ) {
2020-10-31 14:32:18 +01:00
$this -> error = $this -> db -> lasterror ();
$error ++ ;
}
}
}
}
2021-02-23 21:24:38 +01:00
if ( ! $error && $this -> id && ! empty ( $conf -> global -> MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN ) && ! empty ( $this -> origin ) && ! empty ( $this -> origin_id )) { // Get contact from origin object
2020-10-31 14:32:18 +01:00
$originforcontact = $this -> origin ;
$originidforcontact = $this -> origin_id ;
2021-02-23 21:24:38 +01:00
if ( $originforcontact == 'shipping' ) { // shipment and order share the same contacts. If creating from shipment we take data of order
2020-10-31 14:32:18 +01:00
require_once DOL_DOCUMENT_ROOT . '/expedition/class/expedition.class.php' ;
$exp = new Expedition ( $this -> db );
$exp -> fetch ( $this -> origin_id );
$exp -> fetchObjectLinked ();
2021-02-23 21:24:38 +01:00
if ( count ( $exp -> linkedObjectsIds [ 'commande' ]) > 0 ) {
foreach ( $exp -> linkedObjectsIds [ 'commande' ] as $key => $value ) {
2020-10-31 14:32:18 +01:00
$originforcontact = 'commande' ;
2022-06-15 16:41:40 +02:00
$originidforcontact = $value ;
2020-10-31 14:32:18 +01:00
break ; // We take first one
}
}
}
$sqlcontact = " SELECT ctc.code, ctc.source, ec.fk_socpeople FROM " . MAIN_DB_PREFIX . " element_contact as ec, " . MAIN_DB_PREFIX . " c_type_contact as ctc " ;
2021-03-30 17:53:25 +02:00
$sqlcontact .= " WHERE element_id = " . (( int ) $originidforcontact ) . " AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = ' " . $this -> db -> escape ( $originforcontact ) . " ' " ;
2020-10-31 14:32:18 +01:00
$resqlcontact = $this -> db -> query ( $sqlcontact );
2021-02-23 21:24:38 +01:00
if ( $resqlcontact ) {
while ( $objcontact = $this -> db -> fetch_object ( $resqlcontact )) {
if ( $objcontact -> source == 'internal' && in_array ( $objcontact -> code , array ( 'SALESREPSIGN' , 'SALESREPFOLL' ))) {
continue ; // ignore this, already forced previously
}
2020-10-31 14:32:18 +01:00
//print $objcontact->code.'-'.$objcontact->source.'-'.$objcontact->fk_socpeople."\n";
$this -> add_contact ( $objcontact -> fk_socpeople , $objcontact -> code , $objcontact -> source ); // May failed because of duplicate key or because code of contact type does not exists for new object
}
2021-02-23 21:24:38 +01:00
} else {
dol_print_error ( $resqlcontact );
}
2020-10-31 14:32:18 +01:00
}
2016-07-28 19:02:23 +02:00
}
2016-10-14 07:54:44 +02:00
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2020-10-31 14:32:18 +01:00
// Call trigger
$result = $this -> call_trigger ( 'CONTRACT_CREATE' , $user );
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2020-10-31 14:32:18 +01:00
// End call triggers
2008-10-07 01:21:00 +02:00
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2008-10-07 01:21:00 +02:00
$this -> db -> commit ();
return $this -> id ;
2020-05-21 15:05:19 +02:00
} else {
2012-01-04 21:47:40 +01:00
dol_syslog ( get_class ( $this ) . " ::create - 30 - " . $this -> error , LOG_ERR );
2008-10-07 01:21:00 +02:00
$this -> db -> rollback ();
return - 3 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-12 09:46:08 +01:00
$this -> error = " Failed to add contract " ;
2012-01-04 21:47:40 +01:00
dol_syslog ( get_class ( $this ) . " ::create - 20 - " . $this -> error , LOG_ERR );
2008-10-07 01:21:00 +02:00
$this -> db -> rollback ();
return - 2 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-12 09:46:08 +01:00
$this -> error = $langs -> trans ( " UnknownError: " . $this -> db -> error () . " - " , LOG_DEBUG );
2008-10-07 01:21:00 +02:00
$this -> db -> rollback ();
return - 1 ;
}
}
/**
2020-12-06 17:30:27 +01:00
* Delete object
2012-01-04 21:47:40 +01:00
*
2020-12-06 17:30:27 +01:00
* @ param User $user User that deletes
* @ return int < 0 if KO , > 0 if OK
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function delete ( $user )
2008-10-07 01:21:00 +02:00
{
2012-06-10 10:29:43 +02:00
global $conf , $langs ;
2019-11-12 09:46:08 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2012-07-25 11:46:33 +02:00
2019-11-12 09:46:08 +01:00
$error = 0 ;
2008-10-07 01:21:00 +02:00
2007-01-22 23:35:42 +01:00
$this -> db -> begin ();
2014-07-19 20:45:02 +02:00
2020-10-31 14:32:18 +01:00
// Call trigger
$result = $this -> call_trigger ( 'CONTRACT_DELETE' , $user );
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2020-10-31 14:32:18 +01:00
// End call triggers
2008-10-07 01:21:00 +02:00
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2010-04-29 17:57:57 +02:00
// Delete linked contacts
$res = $this -> delete_linked_contact ();
2021-02-23 21:24:38 +01:00
if ( $res < 0 ) {
2012-01-04 21:47:40 +01:00
dol_syslog ( get_class ( $this ) . " ::delete error " , LOG_ERR );
2007-02-22 21:45:16 +01:00
$error ++ ;
}
}
2008-10-07 01:21:00 +02:00
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2015-12-17 12:36:43 +01:00
// Delete linked object
$res = $this -> deleteObjectLinked ();
2021-02-23 21:24:38 +01:00
if ( $res < 0 ) {
$error ++ ;
}
2015-12-17 12:36:43 +01:00
}
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2007-02-22 21:45:16 +01:00
// Delete contratdet_log
2008-06-20 20:41:35 +02:00
/*
2007-02-22 21:45:16 +01:00
$sql = " DELETE cdl " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " contratdet_log as cdl, " . MAIN_DB_PREFIX . " contratdet as cd " ;
2021-08-23 17:41:11 +02:00
$sql .= " WHERE cdl.fk_contratdet=cd.rowid AND cd.fk_contrat= " . (( int ) $this -> id );
2008-06-20 20:41:35 +02:00
*/
$sql = " SELECT cdl.rowid as cdlrowid " ;
2019-11-12 09:46:08 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " contratdet_log as cdl, " . MAIN_DB_PREFIX . " contratdet as cd " ;
2021-08-23 17:41:11 +02:00
$sql .= " WHERE cdl.fk_contratdet=cd.rowid AND cd.fk_contrat= " . (( int ) $this -> id );
2009-01-25 18:05:40 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete contratdet_log " , LOG_DEBUG );
2019-11-12 09:46:08 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( ! $resql ) {
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> error ();
2007-02-22 21:45:16 +01:00
$error ++ ;
}
2019-11-12 09:46:08 +01:00
$numressql = $this -> db -> num_rows ( $resql );
2021-02-23 21:24:38 +01:00
if ( ! $error && $numressql ) {
2019-11-12 09:46:08 +01:00
$tab_resql = array ();
2021-02-23 21:24:38 +01:00
for ( $i = 0 ; $i < $numressql ; $i ++ ) {
2019-11-12 09:46:08 +01:00
$objresql = $this -> db -> fetch_object ( $resql );
$tab_resql [] = $objresql -> cdlrowid ;
2008-06-20 20:41:35 +02:00
}
$this -> db -> free ( $resql );
2008-10-07 01:21:00 +02:00
2019-11-12 09:46:08 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " contratdet_log " ;
2021-03-22 11:30:18 +01:00
$sql .= " WHERE " . MAIN_DB_PREFIX . " contratdet_log.rowid IN ( " . $this -> db -> sanitize ( implode ( " , " , $tab_resql )) . " ) " ;
2009-01-25 18:05:40 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete contratdet_log " , LOG_DEBUG );
2019-11-12 09:46:08 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( ! $resql ) {
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> error ();
2008-06-20 20:41:35 +02:00
$error ++ ;
}
}
2007-02-22 21:45:16 +01:00
}
2008-10-07 01:21:00 +02:00
2020-10-30 14:50:38 +01:00
// Delete lines
if ( ! $error ) {
2020-04-23 19:02:57 +02:00
// Delete contratdet extrafields
2020-05-02 03:39:56 +02:00
$main = MAIN_DB_PREFIX . 'contratdet' ;
$ef = $main . " _extrafields " ;
2021-08-27 18:18:50 +02:00
$sql = " DELETE FROM " . $ef . " WHERE fk_object IN (SELECT rowid FROM " . $main . " WHERE fk_contrat = " . (( int ) $this -> id ) . " ) " ;
2020-04-23 19:02:57 +02:00
dol_syslog ( get_class ( $this ) . " ::delete contratdet_extrafields " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( ! $resql ) {
2020-04-23 19:02:57 +02:00
$this -> error = $this -> db -> error ();
$error ++ ;
}
}
2020-04-23 19:00:47 +02:00
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2007-02-22 21:45:16 +01:00
// Delete contratdet
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " contratdet " ;
2021-08-23 17:41:11 +02:00
$sql .= " WHERE fk_contrat= " . (( int ) $this -> id );
2007-02-22 21:45:16 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete contratdet " , LOG_DEBUG );
2019-11-12 09:46:08 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( ! $resql ) {
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> error ();
2007-02-22 21:45:16 +01:00
$error ++ ;
}
}
2008-10-07 01:21:00 +02:00
2020-10-30 14:50:38 +01:00
// Delete llx_ecm_files
if ( ! $error ) {
2021-08-28 00:55:51 +02:00
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . " ecm_files WHERE src_object_type = ' " . $this -> db -> escape ( $this -> table_element . ( empty ( $this -> module ) ? " " : " @ " . $this -> module )) . " ' AND src_object_id = " . (( int ) $this -> id );
2020-10-30 14:50:38 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( ! $resql ) {
2020-10-30 14:50:38 +01:00
$this -> error = $this -> db -> lasterror ();
$this -> errors [] = $this -> error ;
$error ++ ;
}
}
// Delete contract
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2007-01-22 23:35:42 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " contrat " ;
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid= " . (( int ) $this -> id );
2009-01-25 18:05:40 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete contrat " , LOG_DEBUG );
2019-11-12 09:46:08 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( ! $resql ) {
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> error ();
2007-02-22 21:45:16 +01:00
$error ++ ;
}
2007-01-22 23:35:42 +01:00
}
2012-07-25 11:46:33 +02:00
2016-11-30 16:02:55 +01:00
// Removed extrafields
2019-11-12 09:46:08 +01:00
if ( ! $error ) {
$result = $this -> deleteExtraFields ();
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
2016-11-30 16:02:55 +01:00
$error ++ ;
dol_syslog ( get_class ( $this ) . " ::delete error -3 " . $this -> error , LOG_ERR );
}
}
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2012-07-02 19:30:37 +02:00
// We remove directory
$ref = dol_sanitizeFileName ( $this -> ref );
2021-02-23 21:24:38 +01:00
if ( $conf -> contrat -> dir_output ) {
2021-02-10 13:27:42 +01:00
$dir = $conf -> contrat -> multidir_output [ $this -> entity ] . " / " . $ref ;
2021-02-23 21:24:38 +01:00
if ( file_exists ( $dir )) {
2019-11-12 09:46:08 +01:00
$res = @ dol_delete_dir_recursive ( $dir );
2021-02-23 21:24:38 +01:00
if ( ! $res ) {
2019-11-12 09:46:08 +01:00
$this -> error = 'ErrorFailToDeleteDir' ;
2012-07-02 19:30:37 +02:00
$error ++ ;
}
}
}
2012-06-09 18:37:54 +02:00
}
2008-10-07 01:21:00 +02:00
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2007-02-22 21:45:16 +01:00
$this -> db -> commit ();
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-01-22 23:35:42 +01:00
$this -> db -> rollback ();
2007-02-22 21:45:16 +01:00
return - 1 ;
2007-01-22 23:35:42 +01:00
}
}
2014-07-19 20:45:02 +02:00
2014-07-11 11:26:54 +02:00
/**
* Update object into database
*
* @ param User $user User that modifies
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 20:35:59 +01:00
public function update ( $user , $notrigger = 0 )
2014-07-11 11:26:54 +02:00
{
global $conf , $langs ;
2019-11-12 09:46:08 +01:00
$error = 0 ;
2014-07-19 20:45:02 +02:00
2014-07-11 11:26:54 +02:00
// Clean parameters
2021-02-23 21:24:38 +01:00
if ( empty ( $this -> fk_commercial_signature ) && $this -> commercial_signature_id > 0 ) {
$this -> fk_commercial_signature = $this -> commercial_signature_id ;
}
if ( empty ( $this -> fk_commercial_suivi ) && $this -> commercial_suivi_id > 0 ) {
$this -> fk_commercial_suivi = $this -> commercial_suivi_id ;
}
if ( empty ( $this -> fk_soc ) && $this -> socid > 0 ) {
$this -> fk_soc = ( int ) $this -> socid ;
}
if ( empty ( $this -> fk_project ) && $this -> projet > 0 ) {
$this -> fk_project = ( int ) $this -> projet ;
}
if ( isset ( $this -> ref )) {
$this -> ref = trim ( $this -> ref );
}
if ( isset ( $this -> ref_customer )) {
$this -> ref_customer = trim ( $this -> ref_customer );
}
if ( isset ( $this -> ref_supplier )) {
$this -> ref_supplier = trim ( $this -> ref_supplier );
}
if ( isset ( $this -> ref_ext )) {
$this -> ref_ext = trim ( $this -> ref_ext );
}
if ( isset ( $this -> entity )) {
$this -> entity = ( int ) $this -> entity ;
}
if ( isset ( $this -> statut )) {
$this -> statut = ( int ) $this -> statut ;
}
if ( isset ( $this -> fk_soc )) {
$this -> fk_soc = ( int ) $this -> fk_soc ;
}
if ( isset ( $this -> fk_commercial_signature )) {
$this -> fk_commercial_signature = trim ( $this -> fk_commercial_signature );
}
if ( isset ( $this -> fk_commercial_suivi )) {
$this -> fk_commercial_suivi = trim ( $this -> fk_commercial_suivi );
}
if ( isset ( $this -> note_private )) {
$this -> note_private = trim ( $this -> note_private );
}
if ( isset ( $this -> note_public )) {
$this -> note_public = trim ( $this -> note_public );
}
if ( isset ( $this -> import_key )) {
$this -> import_key = trim ( $this -> import_key );
}
2015-08-07 17:52:21 +02:00
//if (isset($this->extraparams)) $this->extraparams=trim($this->extraparams);
2014-07-19 20:45:02 +02:00
2014-07-11 11:26:54 +02:00
// Check parameters
// Put here code to add a control on parameters values
2014-07-19 20:45:02 +02:00
2014-07-11 11:26:54 +02:00
// Update request
2020-10-31 14:32:18 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " contrat SET " ;
2019-11-12 09:46:08 +01:00
$sql .= " ref= " . ( isset ( $this -> ref ) ? " ' " . $this -> db -> escape ( $this -> ref ) . " ' " : " null " ) . " , " ;
$sql .= " ref_customer= " . ( isset ( $this -> ref_customer ) ? " ' " . $this -> db -> escape ( $this -> ref_customer ) . " ' " : " null " ) . " , " ;
$sql .= " ref_supplier= " . ( isset ( $this -> ref_supplier ) ? " ' " . $this -> db -> escape ( $this -> ref_supplier ) . " ' " : " null " ) . " , " ;
$sql .= " ref_ext= " . ( isset ( $this -> ref_ext ) ? " ' " . $this -> db -> escape ( $this -> ref_ext ) . " ' " : " null " ) . " , " ;
$sql .= " entity= " . $conf -> entity . " , " ;
$sql .= " date_contrat= " . ( dol_strlen ( $this -> date_contrat ) != 0 ? " ' " . $this -> db -> idate ( $this -> date_contrat ) . " ' " : 'null' ) . " , " ;
$sql .= " statut= " . ( isset ( $this -> statut ) ? $this -> statut : " null " ) . " , " ;
$sql .= " fk_soc= " . ( $this -> fk_soc > 0 ? $this -> fk_soc : " null " ) . " , " ;
$sql .= " fk_projet= " . ( $this -> fk_project > 0 ? $this -> fk_project : " null " ) . " , " ;
$sql .= " fk_commercial_signature= " . ( isset ( $this -> fk_commercial_signature ) ? $this -> fk_commercial_signature : " null " ) . " , " ;
$sql .= " fk_commercial_suivi= " . ( isset ( $this -> fk_commercial_suivi ) ? $this -> fk_commercial_suivi : " null " ) . " , " ;
$sql .= " note_private= " . ( isset ( $this -> note_private ) ? " ' " . $this -> db -> escape ( $this -> note_private ) . " ' " : " null " ) . " , " ;
$sql .= " note_public= " . ( isset ( $this -> note_public ) ? " ' " . $this -> db -> escape ( $this -> note_public ) . " ' " : " null " ) . " , " ;
$sql .= " import_key= " . ( isset ( $this -> import_key ) ? " ' " . $this -> db -> escape ( $this -> import_key ) . " ' " : " null " ) . " " ;
2015-08-07 17:52:21 +02:00
//$sql.= " extraparams=".(isset($this->extraparams)?"'".$this->db->escape($this->extraparams)."'":"null")."";
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid= " . (( int ) $this -> id );
2014-07-19 20:45:02 +02:00
2014-07-11 11:26:54 +02:00
$this -> db -> begin ();
2014-07-19 20:45:02 +02:00
2014-07-11 11:26:54 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( ! $resql ) {
$error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror ();
}
2014-07-19 20:45:02 +02:00
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2019-11-12 09:46:08 +01:00
$result = $this -> insertExtraFields ();
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
2018-05-31 17:12:08 +02:00
$error ++ ;
2017-07-20 14:59:24 +02:00
}
2018-05-31 17:12:08 +02:00
}
2014-07-19 20:45:02 +02:00
2021-02-23 21:24:38 +01:00
if ( ! $error && ! $notrigger ) {
2018-08-12 16:29:26 +02:00
// Call triggers
2019-11-12 09:46:08 +01:00
$result = $this -> call_trigger ( 'CONTRACT_MODIFY' , $user );
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2018-08-12 16:29:26 +02:00
// End call triggers
}
2018-05-31 17:12:08 +02:00
// Commit or rollback
2021-02-23 21:24:38 +01:00
if ( $error ) {
foreach ( $this -> errors as $errmsg ) {
2018-05-31 17:12:08 +02:00
dol_syslog ( get_class ( $this ) . " ::update " . $errmsg , LOG_ERR );
2019-11-12 09:46:08 +01:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2014-07-11 11:26:54 +02:00
}
2018-05-31 17:12:08 +02:00
$this -> db -> rollback ();
2019-11-12 09:46:08 +01:00
return - 1 * $error ;
2020-05-21 15:05:19 +02:00
} else {
2018-05-31 17:12:08 +02:00
$this -> db -> commit ();
return 1 ;
}
}
2005-06-11 13:33:21 +02:00
2008-10-07 01:21:00 +02:00
/**
2012-01-04 21:47:40 +01:00
* Ajoute une ligne de contrat en base
*
2020-01-20 12:25:25 +01:00
* @ param string $desc Description of line
* @ param float $pu_ht Unit price net
* @ param int $qty Quantity
* @ param float $txtva Vat rate
2012-03-18 19:23:01 +01:00
* @ param float $txlocaltax1 Local tax 1 rate
* @ param float $txlocaltax2 Local tax 2 rate
* @ param int $fk_product Id produit
2019-03-18 13:45:24 +01:00
* @ param float $remise_percent Percentage discount of the line
2014-12-28 19:16:14 +01:00
* @ param int $date_start Date de debut prevue
* @ param int $date_end Date de fin prevue
* @ param string $price_base_type HT or TTC
2012-03-18 19:23:01 +01:00
* @ param float $pu_ttc Prix unitaire TTC
2019-06-29 16:29:32 +02:00
* @ param int $info_bits Bits of type of lines
2013-04-04 13:58:33 +02:00
* @ param int $fk_fournprice Fourn price id
* @ param int $pa_ht Buying price HT
2015-02-28 04:59:27 +01:00
* @ param array $array_options extrafields array
2015-04-21 15:49:58 +02:00
* @ param string $fk_unit Code of the unit to use . Null to use the default one
2017-10-16 17:56:59 +02:00
* @ param string $rang Position
* @ return int < 0 if KO , > 0 if OK
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function addline ( $desc , $pu_ht , $qty , $txtva , $txlocaltax1 , $txlocaltax2 , $fk_product , $remise_percent , $date_start , $date_end , $price_base_type = 'HT' , $pu_ttc = 0.0 , $info_bits = 0 , $fk_fournprice = null , $pa_ht = 0 , $array_options = 0 , $fk_unit = null , $rang = 0 )
2008-10-07 01:21:00 +02:00
{
2013-09-11 09:19:25 +02:00
global $user , $langs , $conf , $mysoc ;
2019-11-12 09:46:08 +01:00
$error = 0 ;
2008-10-07 01:21:00 +02:00
2017-10-16 17:56:59 +02:00
dol_syslog ( get_class ( $this ) . " ::addline $desc , $pu_ht , $qty , $txtva , $txlocaltax1 , $txlocaltax2 , $fk_product , $remise_percent , $date_start , $date_end , $price_base_type , $pu_ttc , $info_bits , $rang " );
2008-10-07 01:21:00 +02:00
2017-12-21 23:48:33 +01:00
// Check parameters
2021-02-23 21:24:38 +01:00
if ( $fk_product <= 0 && empty ( $desc )) {
2019-11-12 09:46:08 +01:00
$this -> error = " ErrorDescRequiredForFreeProductLines " ;
2017-12-21 23:48:33 +01:00
return - 1 ;
}
2021-02-23 21:24:38 +01:00
if ( $this -> statut >= 0 ) {
2008-10-07 01:21:00 +02:00
// Clean parameters
2019-11-12 09:46:08 +01:00
$pu_ht = price2num ( $pu_ht );
$pu_ttc = price2num ( $pu_ttc );
$pa_ht = price2num ( $pa_ht );
2020-01-13 15:58:03 +01:00
// Clean vat code
$reg = array ();
$vat_src_code = '' ;
2021-02-23 21:24:38 +01:00
if ( preg_match ( '/\((.*)\)/' , $txtva , $reg )) {
2020-01-13 15:58:03 +01:00
$vat_src_code = $reg [ 1 ];
$txtva = preg_replace ( '/\s*\(.*\)/' , '' , $txtva ); // Remove code into vatrate.
2019-11-12 09:46:08 +01:00
}
2020-01-13 15:58:03 +01:00
$txtva = price2num ( $txtva );
2019-11-12 09:46:08 +01:00
$txlocaltax1 = price2num ( $txlocaltax1 );
$txlocaltax2 = price2num ( $txlocaltax2 );
2020-11-27 14:24:15 +01:00
2019-11-12 09:46:08 +01:00
$remise_percent = price2num ( $remise_percent );
$qty = price2num ( $qty );
2021-02-23 21:24:38 +01:00
if ( empty ( $qty )) {
$qty = 1 ;
}
if ( empty ( $info_bits )) {
$info_bits = 0 ;
}
if ( empty ( $pu_ht ) || ! is_numeric ( $pu_ht )) {
$pu_ht = 0 ;
}
if ( empty ( $pu_ttc )) {
$pu_ttc = 0 ;
}
if ( empty ( $txtva ) || ! is_numeric ( $txtva )) {
$txtva = 0 ;
}
if ( empty ( $txlocaltax1 ) || ! is_numeric ( $txlocaltax1 )) {
$txlocaltax1 = 0 ;
}
if ( empty ( $txlocaltax2 ) || ! is_numeric ( $txlocaltax2 )) {
$txlocaltax2 = 0 ;
}
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 ;
2008-03-19 01:31:03 +01:00
}
2009-01-25 18:05:40 +01:00
2013-11-17 23:25:25 +01:00
// Check parameters
2021-02-23 21:24:38 +01:00
if ( empty ( $remise_percent )) {
$remise_percent = 0 ;
}
2019-04-01 22:09:24 +02:00
2019-03-24 14:30:00 +01:00
if ( $date_start && $date_end && $date_start > $date_end ) {
$langs -> load ( " errors " );
2019-11-12 09:46:08 +01:00
$this -> error = $langs -> trans ( 'ErrorStartDateGreaterEnd' );
2019-03-24 14:30:00 +01:00
return - 1 ;
}
2019-04-01 22:09:24 +02:00
2019-03-24 14:30:00 +01:00
$this -> db -> begin ();
2013-11-17 23:25:25 +01:00
2020-01-13 15:58:03 +01:00
$localtaxes_type = getLocalTaxesFromRate ( $txtva . ( $vat_src_code ? ' (' . $vat_src_code . ')' : '' ), 0 , $this -> societe , $mysoc );
2018-12-04 14:58:11 +01:00
2007-07-07 17:51:40 +02:00
// Calcul du total TTC et de la TVA pour la ligne a partir de
// qty, pu, remise_percent et txtva
2008-10-07 01:21:00 +02:00
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
2007-07-07 17:51:40 +02:00
// la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
2013-10-18 10:17:06 +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 , 1 , $mysoc , $localtaxes_type );
2007-07-07 17:51:40 +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 ];
2013-10-18 10:17:06 +02:00
2019-11-12 09:46:08 +01:00
$localtax1_type = $localtaxes_type [ 0 ];
$localtax2_type = $localtaxes_type [ 2 ];
2013-10-18 10:17:06 +02:00
2011-03-08 12:21:39 +01:00
// TODO A virer
2007-07-07 17:51:40 +02:00
// Anciens indicateurs: $price, $remise (a ne plus utiliser)
2008-10-07 01:21:00 +02:00
$remise = 0 ;
2009-01-25 18:05:40 +01:00
$price = price2num ( round ( $pu_ht , 2 ));
2021-02-23 21:24:38 +01:00
if ( dol_strlen ( $remise_percent ) > 0 ) {
2009-01-25 18:05:40 +01:00
$remise = round (( $pu_ht * $remise_percent / 100 ), 2 );
$price = $pu_ht - $remise ;
2008-10-07 01:21:00 +02:00
}
2021-02-23 21:24:38 +01:00
if ( empty ( $pa_ht )) {
$pa_ht = 0 ;
}
2013-02-13 09:50:08 +01:00
2016-10-14 07:54:44 +02:00
2015-10-26 20:33:42 +01:00
// if buy price not defined, define buyprice as configured in margin admin
2021-02-23 21:24:38 +01:00
if ( $this -> pa_ht == 0 ) {
if (( $result = $this -> defineBuyPrice ( $pu_ht , $remise_percent , $fk_product )) < 0 ) {
2015-10-26 20:33:42 +01:00
return $result ;
2020-05-21 15:05:19 +02:00
} else {
2015-10-26 20:33:42 +01:00
$pa_ht = $result ;
}
2013-02-13 09:50:08 +01:00
}
2008-10-07 01:21:00 +02:00
// Insertion dans la base
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " contratdet " ;
2019-11-12 09:46:08 +01:00
$sql .= " (fk_contrat, label, description, fk_product, qty, tva_tx, vat_src_code, " ;
$sql .= " localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, " ;
$sql .= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, " ;
$sql .= " info_bits, " ;
$sql .= " price_ht, remise, fk_product_fournisseur_price, buy_price_ht " ;
2021-02-23 21:24:38 +01:00
if ( $date_start > 0 ) {
$sql .= " ,date_ouverture_prevue " ;
}
if ( $date_end > 0 ) {
$sql .= " ,date_fin_validite " ;
}
2019-11-12 09:46:08 +01:00
$sql .= " , fk_unit " ;
$sql .= " ) VALUES ( " ;
$sql .= $this -> id . " , '', ' " . $this -> db -> escape ( $desc ) . " ', " ;
$sql .= ( $fk_product > 0 ? $fk_product : " null " ) . " , " ;
2021-06-14 13:51:09 +02:00
$sql .= " " . (( float ) $qty ) . " , " ;
$sql .= " " . (( float ) $txtva ) . " , " ;
2020-09-19 23:30:29 +02:00
$sql .= " " . ( $vat_src_code ? " ' " . $this -> db -> escape ( $vat_src_code ) . " ' " : " null " ) . " , " ;
2021-06-14 13:51:09 +02:00
$sql .= " " . (( float ) $txlocaltax1 ) . " , " ;
$sql .= " " . (( float ) $txlocaltax2 ) . " , " ;
2020-09-19 23:30:29 +02:00
$sql .= " ' " . $this -> db -> escape ( $localtax1_type ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $localtax2_type ) . " ', " ;
2019-11-12 09:46:08 +01:00
$sql .= " " . price2num ( $remise_percent ) . " , " ;
$sql .= " " . price2num ( $pu_ht ) . " , " ;
$sql .= " " . price2num ( $total_ht ) . " , " . price2num ( $total_tva ) . " , " . price2num ( $total_localtax1 ) . " , " . price2num ( $total_localtax2 ) . " , " . price2num ( $total_ttc ) . " , " ;
2020-09-19 23:30:29 +02:00
$sql .= " ' " . $this -> db -> escape ( $info_bits ) . " ', " ;
2019-11-12 09:46:08 +01:00
$sql .= " " . price2num ( $price ) . " , " . price2num ( $remise ) . " , " ;
2021-02-23 21:24:38 +01:00
if ( isset ( $fk_fournprice )) {
2021-06-14 13:51:09 +02:00
$sql .= ' ' . (( int ) $fk_fournprice ) . ',' ;
2021-02-23 21:24:38 +01:00
} else {
$sql .= ' null,' ;
}
if ( isset ( $pa_ht )) {
$sql .= ' ' . price2num ( $pa_ht );
} else {
$sql .= ' null' ;
}
if ( $date_start > 0 ) {
$sql .= " ,' " . $this -> db -> idate ( $date_start ) . " ' " ;
}
if ( $date_end > 0 ) {
$sql .= " ,' " . $this -> db -> idate ( $date_end ) . " ' " ;
}
2019-11-12 09:46:08 +01:00
$sql .= " , " . ( $fk_unit ? " ' " . $this -> db -> escape ( $fk_unit ) . " ' " : " null " );
$sql .= " ) " ;
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( $resql ) {
2017-09-04 12:22:03 +02:00
$contractlineid = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " contratdet " );
2017-09-03 12:02:48 +02:00
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2018-02-26 20:12:29 +01:00
$contractline = new ContratLigne ( $this -> db );
2019-11-12 09:46:08 +01:00
$contractline -> array_options = $array_options ;
$contractline -> id = $contractlineid ;
$result = $contractline -> insertExtraFields ();
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
2019-11-12 09:46:08 +01:00
$this -> error [] = $contractline -> error ;
2018-02-26 20:12:29 +01:00
$error ++ ;
2017-09-03 12:02:48 +02:00
}
2018-02-26 20:12:29 +01:00
}
2015-02-28 04:59:27 +01:00
2018-02-26 20:12:29 +01:00
if ( empty ( $error )) {
2020-10-31 14:32:18 +01:00
// Call trigger
$result = $this -> call_trigger ( 'LINECONTRACT_INSERT' , $user );
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
2020-10-31 14:32:18 +01:00
$error ++ ;
}
// End call triggers
2008-03-19 01:31:03 +01:00
}
2018-02-26 20:12:29 +01:00
2021-02-23 21:24:38 +01:00
if ( $error ) {
2008-03-19 01:31:03 +01:00
$this -> db -> rollback ();
return - 1 ;
2020-05-21 15:05:19 +02:00
} else {
2018-02-26 20:12:29 +01:00
$this -> db -> commit ();
return $contractlineid ;
}
2020-05-21 15:05:19 +02:00
} else {
2008-03-19 01:31:03 +01:00
$this -> db -> rollback ();
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> error () . " sql= " . $sql ;
2008-10-07 01:21:00 +02:00
return - 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2012-01-04 21:47:40 +01:00
dol_syslog ( get_class ( $this ) . " ::addline ErrorTryToAddLineOnValidatedContract " , LOG_ERR );
2008-10-07 01:21:00 +02:00
return - 2 ;
}
}
/**
2012-01-04 21:47:40 +01:00
* Mets a jour une ligne de contrat
*
2012-03-18 19:23:01 +01:00
* @ param int $rowid Id de la ligne de facture
* @ param string $desc Description de la ligne
* @ param float $pu Prix unitaire
* @ param int $qty Quantite
2019-03-18 13:45:24 +01:00
* @ param float $remise_percent Percentage discount of the line
2014-12-28 19:16:14 +01:00
* @ param int $date_start Date de debut prevue
* @ param int $date_end Date de fin prevue
2012-03-18 19:23:01 +01:00
* @ param float $tvatx Taux TVA
* @ param float $localtax1tx Local tax 1 rate
* @ param float $localtax2tx Local tax 2 rate
2014-12-28 19:16:14 +01:00
* @ param int | string $date_debut_reel Date de debut reelle
* @ param int | string $date_fin_reel Date de fin reelle
* @ 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
2013-04-04 13:58:33 +02:00
* @ param int $fk_fournprice Fourn price id
* @ param int $pa_ht Buying price HT
2015-02-28 04:59:27 +01:00
* @ param array $array_options extrafields array
2015-04-21 15:49:58 +02:00
* @ param string $fk_unit Code of the unit to use . Null to use the default one
2012-03-18 19:23:01 +01:00
* @ return int < 0 si erreur , > 0 si ok
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function updateline ( $rowid , $desc , $pu , $qty , $remise_percent , $date_start , $date_end , $tvatx , $localtax1tx = 0.0 , $localtax2tx = 0.0 , $date_debut_reel = '' , $date_fin_reel = '' , $price_base_type = 'HT' , $info_bits = 0 , $fk_fournprice = null , $pa_ht = 0 , $array_options = 0 , $fk_unit = null )
2008-10-07 01:21:00 +02:00
{
2013-09-11 09:19:25 +02:00
global $user , $conf , $langs , $mysoc ;
2010-04-27 13:08:07 +02:00
2019-11-12 09:46:08 +01:00
$error = 0 ;
2018-03-10 10:34:11 +01:00
2017-10-16 17:56:59 +02:00
// Clean parameters
2019-11-12 09:46:08 +01:00
$qty = trim ( $qty );
$desc = trim ( $desc );
$desc = trim ( $desc );
2008-10-07 01:21:00 +02:00
$price = price2num ( $pu );
$tvatx = price2num ( $tvatx );
2010-06-22 12:42:37 +02:00
$localtax1tx = price2num ( $localtax1tx );
$localtax2tx = price2num ( $localtax2tx );
2019-11-12 09:46:08 +01:00
$pa_ht = price2num ( $pa_ht );
2021-02-23 21:24:38 +01:00
if ( empty ( $fk_fournprice )) {
$fk_fournprice = 0 ;
}
2012-11-03 16:32:49 +01:00
2008-10-07 01:21:00 +02:00
$subprice = $price ;
$remise = 0 ;
2021-02-23 21:24:38 +01:00
if ( dol_strlen ( $remise_percent ) > 0 ) {
2008-10-07 01:21:00 +02:00
$remise = round (( $pu * $remise_percent / 100 ), 2 );
$price = $pu - $remise ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-12 09:46:08 +01:00
$remise_percent = 0 ;
2008-10-07 01:21:00 +02:00
}
2019-04-01 22:09:24 +02:00
2019-03-24 14:30:00 +01:00
if ( $date_start && $date_end && $date_start > $date_end ) {
$langs -> load ( " errors " );
2019-11-12 09:46:08 +01:00
$this -> error = $langs -> trans ( 'ErrorStartDateGreaterEnd' );
2019-03-24 14:30:00 +01:00
return - 1 ;
}
2008-10-07 01:21:00 +02:00
2012-04-07 02:24:32 +02:00
dol_syslog ( get_class ( $this ) . " ::updateline $rowid , $desc , $pu , $qty , $remise_percent , $date_start , $date_end , $date_debut_reel , $date_fin_reel , $tvatx , $localtax1tx , $localtax2tx , $price_base_type , $info_bits " );
2008-10-07 01:21:00 +02:00
$this -> db -> begin ();
2012-04-07 02:24:32 +02:00
// Calcul du total TTC et de la TVA pour la ligne a partir de
2014-05-14 10:23:03 +02:00
// qty, pu, remise_percent et tvatx
2012-04-07 02:24:32 +02:00
// 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.
2013-10-18 10:17:06 +02:00
2019-11-12 09:46:08 +01:00
$localtaxes_type = getLocalTaxesFromRate ( $tvatx , 0 , $this -> societe , $mysoc );
$tvatx = preg_replace ( '/\s*\(.*\)/' , '' , $tvatx ); // Remove code into vatrate.
2016-10-14 07:54:44 +02:00
2019-11-12 09:46:08 +01:00
$tabprice = calcul_price_total ( $qty , $pu , $remise_percent , $tvatx , $localtax1tx , $localtax2tx , 0 , $price_base_type , $info_bits , 1 , $mysoc , $localtaxes_type );
2012-04-07 02:24:32 +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 ];
2013-10-18 10:17:06 +02:00
2022-10-11 08:00:25 +02:00
$localtax1_type = ( empty ( $localtaxes_type [ 0 ]) ? '' : $localtaxes_type [ 0 ]);
$localtax2_type = ( empty ( $localtaxes_type [ 2 ]) ? '' : $localtaxes_type [ 2 ]);
2013-10-18 10:17:06 +02:00
2012-04-07 02:24:32 +02:00
// TODO A virer
// Anciens indicateurs: $price, $remise (a ne plus utiliser)
$remise = 0 ;
2014-12-28 19:01:39 +01:00
$price = price2num ( round ( $pu , 2 ));
2021-02-23 21:24:38 +01:00
if ( dol_strlen ( $remise_percent ) > 0 ) {
2020-10-31 14:32:18 +01:00
$remise = round (( $pu * $remise_percent / 100 ), 2 );
$price = $pu - $remise ;
2012-04-07 02:24:32 +02:00
}
2021-02-23 21:24:38 +01:00
if ( empty ( $pa_ht )) {
$pa_ht = 0 ;
}
2013-02-13 09:50:08 +01:00
2015-10-26 20:33:42 +01:00
// if buy price not defined, define buyprice as configured in margin admin
2021-02-23 21:24:38 +01:00
if ( $this -> pa_ht == 0 ) {
if (( $result = $this -> defineBuyPrice ( $pu , $remise_percent )) < 0 ) {
2015-10-26 20:33:42 +01:00
return $result ;
2020-05-21 15:05:19 +02:00
} else {
2015-10-26 20:33:42 +01:00
$pa_ht = $result ;
}
2013-02-13 09:50:08 +01:00
}
2021-10-25 18:28:04 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " contratdet set description = ' " . $this -> db -> escape ( $desc ) . " ' " ;
$sql .= " ,price_ht = " . (( float ) price2num ( $price ));
$sql .= " ,subprice = " . (( float ) price2num ( $subprice ));
$sql .= " ,remise = " . (( float ) price2num ( $remise ));
$sql .= " ,remise_percent = " . (( float ) price2num ( $remise_percent ));
$sql .= " ,qty = " . (( float ) $qty );
$sql .= " ,tva_tx = " . (( float ) price2num ( $tvatx ));
$sql .= " ,localtax1_tx = " . (( float ) price2num ( $localtax1tx ));
$sql .= " ,localtax2_tx = " . (( float ) price2num ( $localtax2tx ));
2022-10-11 08:00:25 +02:00
$sql .= " ,localtax1_type=' " . $this -> db -> escape ( $localtax1_type ) . " ' " ;
$sql .= " ,localtax2_type=' " . $this -> db -> escape ( $localtax2_type ) . " ' " ;
2021-10-25 18:28:04 +02:00
$sql .= " , total_ht = " . (( float ) price2num ( $total_ht ));
$sql .= " , total_tva = " . (( float ) price2num ( $total_tva ));
$sql .= " , total_localtax1 = " . (( float ) price2num ( $total_localtax1 ));
$sql .= " , total_localtax2 = " . (( float ) price2num ( $total_localtax2 ));
$sql .= " , total_ttc = " . (( float ) price2num ( $total_ttc ));
2019-11-12 09:46:08 +01:00
$sql .= " , fk_product_fournisseur_price= " . ( $fk_fournprice > 0 ? $fk_fournprice : " null " );
2021-10-25 18:28:04 +02:00
$sql .= " , buy_price_ht = " . (( float ) price2num ( $pa_ht ));
2021-02-23 21:24:38 +01:00
if ( $date_start > 0 ) {
2021-10-25 18:28:04 +02:00
$sql .= " ,date_ouverture_prevue = ' " . $this -> db -> idate ( $date_start ) . " ' " ;
2021-02-23 21:24:38 +01:00
} else {
2021-10-25 18:28:04 +02:00
$sql .= " ,date_ouverture_prevue = null " ;
2021-02-23 21:24:38 +01:00
}
if ( $date_end > 0 ) {
2021-10-25 18:28:04 +02:00
$sql .= " ,date_fin_validite = ' " . $this -> db -> idate ( $date_end ) . " ' " ;
2021-02-23 21:24:38 +01:00
} else {
2021-10-25 18:28:04 +02:00
$sql .= " ,date_fin_validite = null " ;
2021-02-23 21:24:38 +01:00
}
if ( $date_debut_reel > 0 ) {
2021-10-25 18:28:04 +02:00
$sql .= " ,date_ouverture = ' " . $this -> db -> idate ( $date_debut_reel ) . " ' " ;
2021-02-23 21:24:38 +01:00
} else {
2021-10-25 18:28:04 +02:00
$sql .= " ,date_ouverture = null " ;
2021-02-23 21:24:38 +01:00
}
if ( $date_fin_reel > 0 ) {
2021-10-25 18:28:04 +02:00
$sql .= " ,date_cloture = ' " . $this -> db -> idate ( $date_fin_reel ) . " ' " ;
2021-02-23 21:24:38 +01:00
} else {
2021-10-25 18:28:04 +02:00
$sql .= " ,date_cloture = null " ;
2021-02-23 21:24:38 +01:00
}
2021-10-25 18:28:04 +02:00
$sql .= " , fk_unit = " . ( $fk_unit > 0 ? (( int ) $fk_unit ) : " null " );
2021-03-30 17:53:25 +02:00
$sql .= " WHERE rowid = " . (( int ) $rowid );
2005-09-24 03:25:19 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::updateline " , LOG_DEBUG );
2008-10-07 01:21:00 +02:00
$result = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( $result ) {
2021-10-25 18:28:04 +02:00
if ( is_array ( $array_options ) && count ( $array_options ) > 0 ) { // For avoid conflicts if trigger used
$contractline = new ContratLigne ( $this -> db );
$contractline -> fetch ( $rowid );
2020-01-25 14:03:24 +01:00
2021-10-25 18:28:04 +02:00
// We replace values in $contractline->array_options only for entries defined into $array_options
foreach ( $array_options as $key => $value ) {
$contractline -> array_options [ $key ] = $array_options [ $key ];
2015-01-27 12:53:02 +01:00
}
2015-02-28 04:59:27 +01:00
2021-10-25 18:28:04 +02:00
$result = $contractline -> insertExtraFields ();
if ( $result < 0 ) {
$this -> error [] = $contractline -> error ;
$error ++ ;
}
}
2015-02-28 04:59:27 +01:00
2021-10-25 18:28:04 +02:00
if ( empty ( $error )) {
// Call trigger
2022-04-02 10:31:58 +02:00
$result = $this -> call_trigger ( 'LINECONTRACT_MODIFY' , $user );
2021-10-25 18:28:04 +02:00
if ( $result < 0 ) {
$this -> db -> rollback ();
return - 3 ;
2015-01-27 12:53:02 +01:00
}
2021-10-25 18:28:04 +02:00
// End call triggers
$this -> db -> commit ();
return 1 ;
2008-10-07 01:21:00 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2008-10-07 01:21:00 +02:00
$this -> db -> rollback ();
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> error ();
2018-09-28 16:15:17 +02:00
dol_syslog ( get_class ( $this ) . " ::updateline Erreur -1 " );
2008-10-07 01:21:00 +02:00
return - 1 ;
}
}
/**
2012-01-04 21:47:40 +01:00
* Delete a contract line
*
2012-03-18 19:23:01 +01:00
* @ param int $idline Id of line to delete
* @ param User $user User that delete
* @ return int > 0 if OK , < 0 if KO
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function deleteline ( $idline , User $user )
2008-10-07 01:21:00 +02:00
{
2008-04-06 21:34:55 +02:00
global $conf , $langs ;
2008-10-07 01:21:00 +02:00
2019-11-12 09:46:08 +01:00
$error = 0 ;
2012-01-04 21:47:40 +01:00
2021-02-23 21:24:38 +01:00
if ( $this -> statut >= 0 ) {
2020-10-31 14:32:18 +01:00
// Call trigger
$result = $this -> call_trigger ( 'LINECONTRACT_DELETE' , $user );
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
return - 1 ;
}
2020-10-31 14:32:18 +01:00
// End call triggers
2014-07-19 20:45:02 +02:00
2020-10-31 14:32:18 +01:00
$this -> db -> begin ();
2014-07-04 13:43:50 +02:00
2020-10-31 14:32:18 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . $this -> table_element_line ;
2021-04-25 15:55:36 +02:00
$sql .= " WHERE rowid = " . (( int ) $idline );
2008-10-07 01:21:00 +02:00
2019-06-03 20:46:11 +02:00
dol_syslog ( get_class ( $this ) . " ::deleteline " , LOG_DEBUG );
2008-04-06 21:34:55 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( ! $resql ) {
2019-11-12 09:46:08 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2015-01-27 12:53:02 +01:00
$error ++ ;
}
2015-02-28 04:59:27 +01:00
2020-04-23 13:21:39 +02:00
if ( ! $error ) {
2015-01-27 12:53:02 +01:00
// Remove extrafields
2020-04-23 13:21:39 +02:00
$contractline = new ContratLigne ( $this -> db );
$contractline -> id = $idline ;
$result = $contractline -> deleteExtraFields ();
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
2020-04-23 13:21:39 +02:00
$error ++ ;
$this -> error = " Error " . get_class ( $this ) . " ::deleteline deleteExtraFields error -4 " . $contractline -> error ;
2015-01-27 12:53:02 +01:00
}
}
if ( empty ( $error )) {
$this -> db -> commit ();
return 1 ;
} else {
2019-06-03 20:46:11 +02:00
dol_syslog ( get_class ( $this ) . " ::deleteline ERROR: " . $this -> error , LOG_ERR );
2014-07-03 15:21:43 +02:00
$this -> db -> rollback ();
2008-04-06 21:34:55 +02:00
return - 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-12-21 16:50:18 +01:00
$this -> error = 'ErrorDeleteLineNotAllowedByObjectStatus' ;
2008-10-07 01:21:00 +02:00
return - 2 ;
}
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2008-10-07 01:21:00 +02:00
/**
2012-01-04 21:47:40 +01:00
* Update statut of contract according to services
*
2012-03-18 19:23:01 +01:00
* @ param User $user Object user
* @ return int < 0 if KO , > 0 if OK
2015-01-04 19:28:05 +01:00
* @ deprecated This function will never be used . Status of a contract is status of its lines .
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function update_statut ( $user )
2008-10-07 01:21:00 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2019-11-12 09:46:08 +01:00
dol_syslog ( __METHOD__ . " is deprecated " , LOG_WARNING );
2015-04-23 23:21:06 +02:00
2008-04-07 19:16:45 +02:00
// If draft, we keep it (should not happen)
2021-02-23 21:24:38 +01:00
if ( $this -> statut == 0 ) {
return 1 ;
}
2008-04-07 19:16:45 +02:00
2010-12-15 08:12:28 +01:00
// Load $this->lines array
// $this->fetch_lines();
2008-10-07 01:21:00 +02:00
2020-10-31 14:32:18 +01:00
// $newstatut=1;
// foreach($this->lines as $key => $contractline)
// {
// // if ($contractline) // Loop on each service
// }
2008-10-07 01:21:00 +02:00
2008-03-19 01:31:03 +01:00
return 1 ;
2008-10-07 01:21:00 +02:00
}
2005-04-24 04:05:53 +02:00
2006-02-17 00:25:41 +01:00
/**
2012-01-04 21:47:40 +01:00
* Return label of a contract status
*
2019-11-04 11:40:54 +01: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 , 7 = Same than 6 with fixed length
* @ return string Label
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function getLibStatut ( $mode )
2008-10-07 01:21:00 +02:00
{
2019-01-27 11:55:16 +01:00
return $this -> LibStatut ( $this -> statut , $mode );
2008-10-07 01:21:00 +02:00
}
2004-10-30 14:45:08 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2006-02-17 00:25:41 +01:00
/**
2012-01-04 21:47:40 +01:00
* Renvoi label of a given contrat status
*
2019-11-01 15:53:57 +01:00
* @ param int $status Id status
2019-11-04 11:40:54 +01: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 , 7 = Same than 6 with fixed length
2012-03-18 19:23:01 +01:00
* @ return string Label
2008-10-07 01:21:00 +02:00
*/
2019-11-01 15:53:57 +01:00
public function LibStatut ( $status , $mode )
2008-10-07 01:21:00 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2019-11-06 00:42:29 +01:00
global $langs ;
2019-11-01 15:53:57 +01:00
2021-02-23 21:24:38 +01:00
if ( empty ( $this -> labelStatus ) || empty ( $this -> labelStatusShort )) {
2019-11-01 15:53:57 +01:00
global $langs ;
$langs -> load ( " contracts " );
2020-10-25 14:11:09 +01:00
$this -> labelStatus [ self :: STATUS_DRAFT ] = $langs -> transnoentitiesnoconv ( 'ContractStatusDraft' );
$this -> labelStatus [ self :: STATUS_VALIDATED ] = $langs -> transnoentitiesnoconv ( 'ContractStatusValidated' );
$this -> labelStatus [ self :: STATUS_CLOSED ] = $langs -> transnoentitiesnoconv ( 'ContractStatusClosed' );
$this -> labelStatusShort [ self :: STATUS_DRAFT ] = $langs -> transnoentitiesnoconv ( 'ContractStatusDraft' );
$this -> labelStatusShort [ self :: STATUS_VALIDATED ] = $langs -> transnoentitiesnoconv ( 'ContractStatusValidated' );
$this -> labelStatusShort [ self :: STATUS_CLOSED ] = $langs -> transnoentitiesnoconv ( 'ContractStatusClosed' );
2006-03-18 18:41:36 +01:00
}
2019-11-01 15:53:57 +01:00
$statusType = 'status' . $status ;
2021-02-23 21:24:38 +01:00
if ( $status == self :: STATUS_VALIDATED ) {
$statusType = 'status6' ;
}
2019-11-01 15:53:57 +01:00
2021-02-23 21:24:38 +01:00
if ( $mode == 4 || $mode == 6 || $mode == 7 ) {
2019-11-12 09:46:08 +01:00
$text = '' ;
2019-02-25 20:35:59 +01:00
if ( $mode == 4 ) {
2019-11-12 09:46:08 +01:00
$text = '<span class="hideonsmartphone">' ;
$text .= ( $this -> nbofserviceswait + $this -> nbofservicesopened + $this -> nbofservicesexpired + $this -> nbofservicesclosed );
$text .= ' ' . $langs -> trans ( " Services " );
$text .= ': ' ;
$text .= '</span>' ;
}
$text .= ( $mode == 7 ? '<span class="nowraponall">' : '' );
$text .= ( $mode != 7 || $this -> nbofserviceswait > 0 ) ? ( $this -> nbofserviceswait . ContratLigne :: LibStatut ( 0 , 3 , - 1 , 'class="marginleft2"' )) . (( $mode != 7 || $this -> nbofservicesopened || $this -> nbofservicesexpired || $this -> nbofservicesclosed ) ? ' ' : '' ) : '' ;
$text .= ( $mode == 7 ? '</span><span class="nowraponall">' : '' );
$text .= ( $mode != 7 || $this -> nbofservicesopened > 0 ) ? ( $this -> nbofservicesopened . ContratLigne :: LibStatut ( 4 , 3 , 0 , 'class="marginleft2"' )) . (( $mode != 7 || $this -> nbofservicesexpired || $this -> nbofservicesclosed ) ? ' ' : '' ) : '' ;
$text .= ( $mode == 7 ? '</span><span class="nowraponall">' : '' );
$text .= ( $mode != 7 || $this -> nbofservicesexpired > 0 ) ? ( $this -> nbofservicesexpired . ContratLigne :: LibStatut ( 4 , 3 , 1 , 'class="marginleft2"' )) . (( $mode != 7 || $this -> nbofservicesclosed ) ? ' ' : '' ) : '' ;
$text .= ( $mode == 7 ? '</span><span class="nowraponall">' : '' );
$text .= ( $mode != 7 || $this -> nbofservicesclosed > 0 ) ? ( $this -> nbofservicesclosed . ContratLigne :: LibStatut ( 5 , 3 , - 1 , 'class="marginleft2"' )) : '' ;
$text .= ( $mode == 7 ? '</span>' : '' );
2008-04-07 19:16:45 +02:00
return $text ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-01 15:53:57 +01:00
return dolGetStatus ( $this -> labelStatus [ $status ], $this -> labelStatusShort [ $status ], '' , $statusType , $mode );
2006-03-25 13:49:55 +01:00
}
2008-10-07 01:21:00 +02:00
}
2004-10-30 14:45:08 +02:00
2005-07-03 15:07:59 +02:00
2008-04-07 19:16:45 +02:00
/**
2015-03-15 14:04:07 +01:00
* Return clicable name ( with picto eventually )
2012-01-04 21:47:40 +01:00
*
2017-10-03 14:50:52 +02:00
* @ param int $withpicto 0 = No picto , 1 = Include picto into link , 2 = Only picto
* @ param int $maxlength Max length of ref
2020-10-31 14:32:18 +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-10-03 14:50:52 +02:00
* @ return string Chaine avec URL
2009-10-10 18:26:06 +02:00
*/
2020-10-31 14:32:18 +01:00
public function getNomUrl ( $withpicto = 0 , $maxlength = 0 , $notooltip = 0 , $save_lastsearch_value = - 1 )
2008-04-07 19:16:45 +02:00
{
2019-06-24 22:59:00 +02:00
global $conf , $langs , $user , $hookmanager ;
2008-10-07 01:21:00 +02:00
2019-11-12 09:46:08 +01:00
$result = '' ;
2008-04-07 19:16:45 +02:00
2017-03-02 13:24:56 +01:00
$url = DOL_URL_ROOT . '/contrat/card.php?id=' . $this -> id ;
2017-10-03 14:50:52 +02:00
//if ($option !== 'nolink')
//{
// Add param to save lastsearch_values or not
2019-11-12 09:46:08 +01:00
$add_save_lastsearch_values = ( $save_lastsearch_value == 1 ? 1 : 0 );
2021-02-23 21:24:38 +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' ;
}
2017-10-03 14:50:52 +02:00
//}
2020-10-31 14:32:18 +01:00
$label = '' ;
if ( $user -> rights -> contrat -> lire ) {
2020-11-02 11:41:07 +01:00
$label = img_picto ( '' , $this -> picto ) . ' <u class="paddingrightonly">' . $langs -> trans ( " Contract " ) . '</u>' ;
/* Status of a contract is status of all services , so disabled
if ( isset ( $this -> statut )) {
$label .= ' ' . $this -> getLibStatut ( 5 );
} */
2020-10-31 14:32:18 +01:00
$label .= '<br><b>' . $langs -> trans ( 'Ref' ) . ':</b> ' . ( $this -> ref ? $this -> ref : $this -> id );
2021-03-17 18:21:07 +01:00
$ref_customer = ( ! empty ( $this -> ref_customer ) ? $this -> ref_customer : ( empty ( $this -> ref_client ) ? '' : $this -> ref_client ));
$label .= '<br><b>' . $langs -> trans ( 'RefCustomer' ) . ':</b> ' . $ref_customer ;
2020-10-31 14:32:18 +01:00
$label .= '<br><b>' . $langs -> trans ( 'RefSupplier' ) . ':</b> ' . $this -> ref_supplier ;
if ( ! empty ( $this -> total_ht )) {
$label .= '<br><b>' . $langs -> trans ( 'AmountHT' ) . ':</b> ' . price ( $this -> total_ht , 0 , $langs , 0 , - 1 , - 1 , $conf -> currency );
}
if ( ! empty ( $this -> total_tva )) {
$label .= '<br><b>' . $langs -> trans ( 'VAT' ) . ':</b> ' . price ( $this -> total_tva , 0 , $langs , 0 , - 1 , - 1 , $conf -> currency );
}
if ( ! empty ( $this -> total_ttc )) {
$label .= '<br><b>' . $langs -> trans ( 'AmountTTC' ) . ':</b> ' . price ( $this -> total_ttc , 0 , $langs , 0 , - 1 , - 1 , $conf -> currency );
}
}
$linkclose = '' ;
2021-02-23 21:24:38 +01:00
if ( empty ( $notooltip ) && $user -> rights -> contrat -> lire ) {
if ( ! empty ( $conf -> global -> MAIN_OPTIMIZEFORTEXTBROWSER )) {
2020-10-31 14:32:18 +01:00
$label = $langs -> trans ( " ShowOrder " );
$linkclose .= ' alt="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
}
$linkclose .= ' title="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
$linkclose .= ' class="classfortooltip"' ;
}
2008-10-07 01:21:00 +02:00
2017-03-02 13:24:56 +01:00
$linkstart = '<a href="' . $url . '"' ;
2019-11-12 09:46:08 +01:00
$linkstart .= $linkclose . '>' ;
$linkend = '</a>' ;
2017-06-01 13:32:20 +02:00
2017-11-02 15:03:09 +01:00
$result .= $linkstart ;
2021-02-23 21:24:38 +01:00
if ( $withpicto ) {
$result .= img_object (( $notooltip ? '' : $label ), $this -> picto , ( $notooltip ? (( $withpicto != 2 ) ? 'class="paddingright"' : '' ) : 'class="' . (( $withpicto != 2 ) ? 'paddingright ' : '' ) . 'classfortooltip"' ), 0 , 0 , $notooltip ? 0 : 1 );
}
if ( $withpicto != 2 ) {
$result .= ( $this -> ref ? $this -> ref : $this -> id );
}
2017-11-02 15:03:09 +01:00
$result .= $linkend ;
2019-06-24 22:59:00 +02:00
global $action ;
$hookmanager -> initHooks ( array ( 'contractdao' ));
2022-02-15 18:31:05 +01:00
$parameters = array ( 'id' => $this -> id , 'getnomurl' => & $result );
2019-11-12 09:46:08 +01:00
$reshook = $hookmanager -> executeHooks ( 'getNomUrl' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2019-06-24 22:59:00 +02:00
if ( $reshook > 0 ) {
$result = $hookmanager -> resPrint ;
} else {
$result .= $hookmanager -> resPrint ;
}
2008-04-07 19:16:45 +02:00
return $result ;
}
2012-01-04 21:47:40 +01:00
/**
* Charge les informations d 'ordre info dans l' objet contrat
*
* @ param int $id id du contrat a charger
* @ return void
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function info ( $id )
2008-10-07 01:21:00 +02:00
{
2020-02-07 13:37:37 +01:00
$sql = " SELECT c.rowid, c.ref, c.datec, " ;
2019-11-12 09:46:08 +01:00
$sql .= " c.tms as date_modification, " ;
2020-02-07 13:37:37 +01:00
$sql .= " fk_user_author " ;
2019-11-12 09:46:08 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " contrat as c " ;
2020-10-14 01:52:53 +02:00
$sql .= " WHERE c.rowid = " . (( int ) $id );
2008-10-07 01:21:00 +02:00
2019-11-12 09:46:08 +01:00
$result = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( $result ) {
if ( $this -> db -> num_rows ( $result )) {
2008-10-07 01:21:00 +02:00
$obj = $this -> db -> fetch_object ( $result );
$this -> id = $obj -> rowid ;
if ( $obj -> fk_user_author ) {
2010-04-28 09:31:34 +02:00
$cuser = new User ( $this -> db );
$cuser -> fetch ( $obj -> fk_user_author );
2019-11-12 09:46:08 +01:00
$this -> user_creation = $cuser ;
2008-10-07 01:21:00 +02:00
}
2019-11-12 09:46:08 +01:00
$this -> ref = ( ! $obj -> ref ) ? $obj -> rowid : $obj -> ref ;
2010-01-13 19:51:19 +01:00
$this -> date_creation = $this -> db -> jdate ( $obj -> datec );
$this -> date_modification = $this -> db -> jdate ( $obj -> date_modification );
2008-10-07 01:21:00 +02:00
}
$this -> db -> free ( $result );
2020-05-21 15:05:19 +02:00
} else {
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2008-10-07 01:21:00 +02:00
}
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2008-10-07 01:21:00 +02:00
/**
2012-01-04 21:47:40 +01:00
* Return list of line rowid
*
2019-11-02 11:36:48 +01:00
* @ param int $status Status of lines to get
2019-10-07 08:11:23 +02:00
* @ return array | int Array of line ' s rowid or < 0 if error
2008-10-07 01:21:00 +02:00
*/
2019-11-02 11:36:48 +01:00
public function array_detail ( $status = - 1 )
2008-10-07 01:21:00 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2019-11-12 09:46:08 +01:00
$tab = array ();
2008-10-07 01:21:00 +02:00
$sql = " SELECT cd.rowid " ;
2019-11-12 09:46:08 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " contratdet as cd " ;
2021-08-27 18:18:50 +02:00
$sql .= " WHERE fk_contrat = " . (( int ) $this -> id );
2021-02-23 21:24:38 +01:00
if ( $status >= 0 ) {
2021-08-23 17:41:11 +02:00
$sql .= " AND statut = " . (( int ) $status );
2021-02-23 21:24:38 +01:00
}
2009-01-25 18:05:40 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::array_detail() " , LOG_DEBUG );
2019-11-12 09:46:08 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( $resql ) {
2019-11-12 09:46:08 +01:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-23 21:24:38 +01:00
while ( $i < $num ) {
2008-10-07 01:21:00 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2019-11-12 09:46:08 +01:00
$tab [ $i ] = $obj -> rowid ;
2008-10-07 01:21:00 +02:00
$i ++ ;
}
return $tab ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> error ();
2008-10-07 01:21:00 +02:00
return - 1 ;
}
}
/**
2022-06-21 02:15:22 +02:00
* Return list of other contracts for the same company than current contract
2012-01-04 21:47:40 +01:00
*
2022-02-03 10:01:20 +01:00
* @ param string $option 'all' or 'others'
* @ param array $status sort contracts having these status
* @ param array $product_categories sort contracts containing these product categories
* @ param array $line_status sort contracts where lines have these status
* @ return array | int Array of contracts id or < 0 if error
2008-10-07 01:21:00 +02:00
*/
2022-02-08 15:23:56 +01:00
public function getListOfContracts ( $option = 'all' , $status = [], $product_categories = [], $line_status = [])
2008-10-07 01:21:00 +02:00
{
2019-11-12 09:46:08 +01:00
$tab = array ();
2008-10-07 01:21:00 +02:00
2022-06-21 02:15:22 +02:00
$sql = " SELECT c.rowid " ;
2019-11-12 09:46:08 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " contrat as c " ;
2022-02-03 10:01:20 +01:00
if ( ! empty ( $product_categories )) {
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " contratdet as cd ON cd.fk_contrat = c.rowid " ;
2022-03-01 09:17:56 +01:00
$sql .= " INNER JOIN " . MAIN_DB_PREFIX . " categorie_product as cp ON cp.fk_product = cd.fk_product AND cp.fk_categorie IN ( " . $this -> db -> sanitize ( implode ( ', ' , $product_categories )) . " ) " ;
2022-02-03 10:01:20 +01:00
}
$sql .= " WHERE c.fk_soc = " . (( int ) $this -> socid );
$sql .= ( $option == 'others' ) ? " AND c.rowid <> " . (( int ) $this -> id ) : " " ;
2022-03-01 09:17:56 +01:00
$sql .= ( ! empty ( $status )) ? " AND c.statut IN ( " . $this -> db -> sanitize ( implode ( ', ' , $status )) . " ) " : " " ;
$sql .= ( ! empty ( $line_status )) ? " AND cd.statut IN ( " . $this -> db -> sanitize ( implode ( ', ' , $line_status )) . " ) " : " " ;
2022-02-03 10:01:20 +01:00
$sql .= " GROUP BY c.rowid " ;
2008-10-07 01:21:00 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::getOtherContracts() " , LOG_DEBUG );
2019-11-12 09:46:08 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( $resql ) {
2019-11-12 09:46:08 +01:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-23 21:24:38 +01:00
while ( $i < $num ) {
2008-10-07 01:21:00 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2019-11-12 09:46:08 +01:00
$contrat = new Contrat ( $this -> db );
2008-10-07 01:21:00 +02:00
$contrat -> fetch ( $obj -> rowid );
2022-06-21 02:15:22 +02:00
$tab [ $contrat -> id ] = $contrat ;
2008-10-07 01:21:00 +02:00
$i ++ ;
}
return $tab ;
2020-05-21 15:05:19 +02:00
} else {
2022-06-21 02:15:22 +02:00
$this -> error = $this -> db -> lasterror ();
2008-10-07 01:21:00 +02:00
return - 1 ;
}
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2008-10-07 01:21:00 +02:00
/**
2020-10-31 14:32:18 +01:00
* Load indicators for dashboard ( this -> nbtodo and this -> nbtodolate )
*
* @ param User $user Objet user
* @ param string $mode " inactive " pour services a activer , " expired " pour services expires
* @ return WorkboardResponse | int < 0 if KO , WorkboardResponse if OK
2008-10-07 01:21:00 +02:00
*/
2020-10-31 14:32:18 +01:00
public function load_board ( $user , $mode )
2008-10-07 01:21:00 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2016-01-18 15:56:51 +01:00
global $conf , $langs ;
2009-05-06 20:07:34 +02:00
2009-04-27 22:37:50 +02:00
$this -> from = " FROM " . MAIN_DB_PREFIX . " contrat as c " ;
2019-11-12 09:46:08 +01:00
$this -> from .= " , " . MAIN_DB_PREFIX . " contratdet as cd " ;
$this -> from .= " , " . MAIN_DB_PREFIX . " societe as s " ;
2021-10-22 22:15:59 +02:00
if ( empty ( $user -> rights -> societe -> client -> voir ) && ! $user -> socid ) {
2021-02-23 21:24:38 +01:00
$this -> from .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
}
2009-05-06 20:07:34 +02:00
2021-02-23 21:24:38 +01:00
if ( $mode == 'inactive' ) {
2009-01-07 16:21:16 +01:00
$sql = " SELECT cd.rowid, cd.date_ouverture_prevue as datefin " ;
2019-11-12 09:46:08 +01:00
$sql .= $this -> from ;
$sql .= " WHERE c.statut = 1 " ;
$sql .= " AND c.rowid = cd.fk_contrat " ;
$sql .= " AND cd.statut = 0 " ;
2021-02-23 21:24:38 +01:00
} elseif ( $mode == 'expired' ) {
2009-01-07 16:21:16 +01:00
$sql = " SELECT cd.rowid, cd.date_fin_validite as datefin " ;
2019-11-12 09:46:08 +01:00
$sql .= $this -> from ;
$sql .= " WHERE c.statut = 1 " ;
$sql .= " AND c.rowid = cd.fk_contrat " ;
$sql .= " AND cd.statut = 4 " ;
$sql .= " AND cd.date_fin_validite < ' " . $this -> db -> idate ( dol_now ()) . " ' " ;
2021-02-23 21:24:38 +01:00
} elseif ( $mode == 'active' ) {
2020-10-31 14:32:18 +01:00
$sql = " SELECT cd.rowid, cd.date_fin_validite as datefin " ;
$sql .= $this -> from ;
$sql .= " WHERE c.statut = 1 " ;
$sql .= " AND c.rowid = cd.fk_contrat " ;
$sql .= " AND cd.statut = 4 " ;
//$datetouse = dol_now();
//$sql.= " AND cd.date_fin_validite < '".$this->db->idate($datetouse)."'";
2008-10-07 01:21:00 +02:00
}
2019-11-12 09:46:08 +01:00
$sql .= " AND c.fk_soc = s.rowid " ;
2021-08-23 17:41:11 +02:00
$sql .= " AND c.entity = " . (( int ) $conf -> entity );
2021-02-23 21:24:38 +01:00
if ( $user -> socid ) {
2021-08-23 17:41:11 +02:00
$sql .= " AND c.fk_soc = " . (( int ) $user -> socid );
2021-02-23 21:24:38 +01:00
}
2021-10-22 22:15:59 +02:00
if ( empty ( $user -> rights -> societe -> client -> voir ) && ! $user -> socid ) {
2021-08-23 17:41:11 +02:00
$sql .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " . (( int ) $user -> id );
2021-02-23 21:24:38 +01:00
}
2019-05-02 17:15:24 +02:00
2019-11-12 09:46:08 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( $resql ) {
2015-02-15 15:01:28 +01:00
$langs -> load ( " contracts " );
2019-11-12 09:46:08 +01:00
$now = dol_now ();
2015-02-15 15:01:28 +01:00
2019-05-02 17:15:24 +02:00
if ( $mode == 'inactive' ) {
2015-02-15 15:01:28 +01:00
$warning_delay = $conf -> contrat -> services -> inactifs -> warning_delay ;
$label = $langs -> trans ( " BoardNotActivatedServices " );
2019-08-01 12:33:19 +02:00
$labelShort = $langs -> trans ( " BoardNotActivatedServicesShort " );
2019-05-02 17:24:05 +02:00
$url = DOL_URL_ROOT . '/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&mode=0&sortfield=cd.date_fin_validite&sortorder=asc' ;
2020-05-21 15:05:19 +02:00
} elseif ( $mode == 'expired' ) {
2020-10-31 14:32:18 +01:00
$warning_delay = $conf -> contrat -> services -> expires -> warning_delay ;
$url = DOL_URL_ROOT . '/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&mode=4&filter=expired&sortfield=cd.date_fin_validite&sortorder=asc' ;
$label = $langs -> trans ( " BoardExpiredServices " );
$labelShort = $langs -> trans ( " BoardExpiredServicesShort " );
2015-02-15 15:01:28 +01:00
} else {
$warning_delay = $conf -> contrat -> services -> expires -> warning_delay ;
2019-05-02 17:24:05 +02:00
$url = DOL_URL_ROOT . '/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&mode=4&sortfield=cd.date_fin_validite&sortorder=asc' ;
2019-05-02 17:15:24 +02:00
//$url.= '&op2day='.$arraydatetouse['mday'].'&op2month='.$arraydatetouse['mon'].'&op2year='.$arraydatetouse['year'];
//if ($warning_delay >= 0) $url.='&filter=expired';
2015-02-15 15:01:28 +01:00
$label = $langs -> trans ( " BoardRunningServices " );
2019-08-01 12:33:19 +02:00
$labelShort = $langs -> trans ( " BoardRunningServicesShort " );
2015-02-15 15:01:28 +01:00
}
2015-02-16 23:49:07 +01:00
$response = new WorkboardResponse ();
2019-11-12 09:46:08 +01:00
$response -> warning_delay = $warning_delay / 60 / 60 / 24 ;
2015-02-15 15:01:28 +01:00
$response -> label = $label ;
2019-08-01 12:33:19 +02:00
$response -> labelShort = $labelShort ;
2015-02-15 15:01:28 +01:00
$response -> url = $url ;
2019-01-27 11:55:16 +01:00
$response -> img = img_object ( '' , " contract " );
2015-02-15 15:01:28 +01:00
2021-02-23 21:24:38 +01:00
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2015-02-15 15:01:28 +01:00
$response -> nbtodo ++ ;
if ( $obj -> datefin && $this -> db -> jdate ( $obj -> datefin ) < ( $now - $warning_delay )) {
$response -> nbtodolate ++ ;
}
2008-10-07 01:21:00 +02:00
}
2015-02-15 15:01:28 +01:00
return $response ;
2020-05-21 15:05:19 +02:00
} else {
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> error ();
2008-10-07 01:21:00 +02:00
return - 1 ;
}
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2008-10-07 01:21:00 +02:00
/**
2012-01-04 21:47:40 +01:00
* Charge indicateurs this -> nb de tableau de bord
*
* @ return int < 0 si ko , > 0 si ok
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function load_state_board ()
2008-10-07 01:21:00 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2008-10-07 01:21:00 +02:00
global $conf , $user ;
2019-11-12 09:46:08 +01:00
$this -> nb = array ();
2009-04-27 22:37:50 +02:00
$clause = " WHERE " ;
2008-10-07 01:21:00 +02:00
$sql = " SELECT count(c.rowid) as nb " ;
2019-11-12 09:46:08 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " contrat as c " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s ON c.fk_soc = s.rowid " ;
2021-10-22 22:15:59 +02:00
if ( empty ( $user -> rights -> societe -> client -> voir ) && ! $user -> socid ) {
2019-11-12 09:46:08 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe_commerciaux as sc ON s.rowid = sc.fk_soc " ;
2021-08-23 17:41:11 +02:00
$sql .= " WHERE sc.fk_user = " . (( int ) $user -> id );
2009-04-27 22:37:50 +02:00
$clause = " AND " ;
2008-10-07 01:21:00 +02:00
}
2019-11-12 09:46:08 +01:00
$sql .= " " . $clause . " c.entity = " . $conf -> entity ;
2009-05-06 20:07:34 +02:00
2019-11-12 09:46:08 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( $resql ) {
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2020-09-28 08:12:12 +02:00
$this -> nb [ " contracts " ] = $obj -> nb ;
2008-10-07 01:21:00 +02:00
}
2020-10-31 14:32:18 +01:00
$this -> db -> free ( $resql );
2008-10-07 01:21:00 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2009-02-20 23:53:15 +01:00
dol_print_error ( $this -> db );
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> error ();
2008-10-07 01:21:00 +02:00
return - 1 ;
}
}
/* gestion des contacts d'un contrat */
/**
2012-01-04 21:47:40 +01:00
* Return id des contacts clients de facturation
*
* @ return array Liste des id contacts facturation
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function getIdBillingContact ()
2008-10-07 01:21:00 +02:00
{
2019-01-27 11:55:16 +01:00
return $this -> getIdContact ( 'external' , 'BILLING' );
2008-10-07 01:21:00 +02:00
}
/**
2012-01-04 21:47:40 +01:00
* Return id des contacts clients de prestation
*
* @ return array Liste des id contacts prestation
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function getIdServiceContact ()
2008-10-07 01:21:00 +02:00
{
2019-01-27 11:55:16 +01:00
return $this -> getIdContact ( 'external' , 'SERVICE' );
2008-10-07 01:21:00 +02:00
}
2010-04-27 13:08:07 +02:00
/**
2020-10-31 14:32:18 +01:00
* Initialise an instance with random values .
* Used to build previews or test instances .
* id must be 0 if object instance is a specimen .
*
* @ return void
*/
public function initAsSpecimen ()
{
2019-11-12 09:46:08 +01:00
global $user , $langs , $conf ;
2010-04-27 13:08:07 +02:00
2020-10-31 14:32:18 +01:00
// Load array of products prodids
2016-06-28 02:36:40 +02:00
$num_prods = 0 ;
2010-04-27 13:08:07 +02:00
$prodids = array ();
$sql = " SELECT rowid " ;
2019-11-12 09:46:08 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " product " ;
$sql .= " WHERE entity IN ( " . getEntity ( 'product' ) . " ) " ;
$sql .= " AND tosell = 1 " ;
2020-08-19 01:43:48 +02:00
$sql .= $this -> db -> plimit ( 100 );
2010-04-27 13:08:07 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( $resql ) {
2010-04-27 13:08:07 +02:00
$num_prods = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-23 21:24:38 +01:00
while ( $i < $num_prods ) {
2010-04-27 13:08:07 +02:00
$i ++ ;
$row = $this -> db -> fetch_row ( $resql );
$prodids [ $i ] = $row [ 0 ];
}
}
// Initialise parametres
2019-11-12 09:46:08 +01:00
$this -> id = 0 ;
$this -> specimen = 1 ;
2010-04-27 13:08:07 +02:00
2014-02-26 15:57:43 +01:00
$this -> ref = 'SPECIMEN' ;
2015-10-17 00:52:35 +02:00
$this -> ref_customer = 'SPECIMENCUST' ;
$this -> ref_supplier = 'SPECIMENSUPP' ;
2010-04-27 13:08:07 +02:00
$this -> socid = 1 ;
2019-11-12 09:46:08 +01:00
$this -> statut = 0 ;
2016-08-20 00:30:09 +02:00
$this -> date_creation = ( dol_now () - 3600 * 24 * 7 );
2010-04-27 13:08:07 +02:00
$this -> date_contrat = dol_now ();
$this -> commercial_signature_id = 1 ;
$this -> commercial_suivi_id = 1 ;
2019-11-12 09:46:08 +01:00
$this -> note_private = 'This is a comment (private)' ;
$this -> note_public = 'This is a comment (public)' ;
2010-04-27 13:08:07 +02:00
$this -> fk_projet = 0 ;
// Lines
$nbp = 5 ;
$xnbp = 0 ;
2021-02-23 21:24:38 +01:00
while ( $xnbp < $nbp ) {
2019-11-12 09:46:08 +01:00
$line = new ContratLigne ( $this -> db );
$line -> qty = 1 ;
$line -> subprice = 100 ;
$line -> price = 100 ;
$line -> tva_tx = 19.6 ;
$line -> remise_percent = 10 ;
$line -> total_ht = 90 ;
$line -> total_ttc = 107.64 ; // 90 * 1.196
$line -> total_tva = 17.64 ;
2017-10-26 12:32:22 +02:00
$line -> date_start = dol_now () - 500000 ;
$line -> date_start_real = dol_now () - 200000 ;
$line -> date_end = dol_now () + 500000 ;
$line -> date_end_real = dol_now () - 100000 ;
2021-02-23 21:24:38 +01:00
if ( $num_prods > 0 ) {
2016-06-03 22:20:58 +02:00
$prodid = mt_rand ( 1 , $num_prods );
2019-11-12 09:46:08 +01:00
$line -> fk_product = $prodids [ $prodid ];
2020-10-31 14:32:18 +01:00
}
2019-11-12 09:46:08 +01:00
$this -> lines [ $xnbp ] = $line ;
2010-04-27 13:08:07 +02:00
$xnbp ++ ;
}
}
2014-09-28 20:39:02 +02:00
2017-10-23 14:44:06 +02:00
/**
2017-10-16 17:56:59 +02:00
* Create an array of order lines
*
* @ return int > 0 if OK , < 0 if KO
2017-10-23 14:44:06 +02:00
*/
2019-02-25 20:35:59 +01:00
public function getLinesArray ()
2017-10-23 14:44:06 +02:00
{
return $this -> fetch_lines ();
}
2017-10-16 17:56:59 +02:00
2014-09-28 20:39:02 +02:00
/**
* Create a document onto disk according to template module .
*
* @ param string $modele Force model to use ( '' to not force )
* @ param Translate $outputlangs Object langs to use for output
* @ param int $hidedetails Hide details of lines
* @ param int $hidedesc Hide description
* @ param int $hideref Hide ref
2020-10-31 14:32:18 +01:00
* @ param null | array $moreparams Array to provide more information
2014-09-28 20:39:02 +02:00
* @ return int 0 if KO , 1 if OK
*/
2019-01-27 15:20:16 +01:00
public function generateDocument ( $modele , $outputlangs , $hidedetails = 0 , $hidedesc = 0 , $hideref = 0 , $moreparams = null )
2014-09-28 20:39:02 +02:00
{
2019-11-12 09:46:08 +01:00
global $conf , $langs ;
2014-09-28 20:39:02 +02:00
$langs -> load ( " contracts " );
2020-08-04 11:02:16 +02:00
$outputlangs -> load ( " products " );
2014-09-28 20:39:02 +02:00
2019-11-12 09:46:08 +01:00
if ( ! dol_strlen ( $modele )) {
2017-01-16 21:16:05 +01:00
$modele = 'strato' ;
2020-12-13 13:34:21 +01:00
if ( ! empty ( $this -> model_pdf )) {
$modele = $this -> model_pdf ;
} elseif ( ! empty ( $this -> modelpdf )) { // deprecated
2017-01-16 21:16:05 +01:00
$modele = $this -> modelpdf ;
2019-11-12 09:46:08 +01:00
} elseif ( ! empty ( $conf -> global -> CONTRACT_ADDON_PDF )) {
2014-09-28 20:39:02 +02:00
$modele = $conf -> global -> CONTRACT_ADDON_PDF ;
}
}
$modelpath = " core/modules/contract/doc/ " ;
2018-06-15 09:47:28 +02:00
return $this -> commonGenerateDocument ( $modelpath , $modele , $outputlangs , $hidedetails , $hidedesc , $hideref , $moreparams );
2014-09-28 20:39:02 +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 (
'contrat'
);
return CommonObject :: commonReplaceThirdparty ( $db , $origin_id , $dest_id , $tables );
}
2016-10-14 07:54:44 +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 (
'contratdet'
);
return CommonObject :: commonReplaceProduct ( $db , $origin_id , $dest_id , $tables );
}
2016-10-14 07:54:44 +02:00
/**
* Load an object from its id and create a new one in database
*
2019-04-25 12:11:32 +02:00
* @ param User $user User making the clone
* @ param int $socid Id of thirdparty
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
* @ return int New id of clone
2016-10-14 07:54:44 +02:00
*/
2020-10-31 14:32:18 +01:00
public function createFromClone ( User $user , $socid = 0 , $notrigger = 0 )
{
2019-04-25 12:11:32 +02:00
global $db , $langs , $conf , $hookmanager , $extrafields ;
2016-10-14 07:54:44 +02:00
dol_include_once ( '/projet/class/project.class.php' );
$error = 0 ;
$this -> fetch ( $this -> id );
2018-09-16 22:13:29 +02:00
2016-10-14 07:54:44 +02:00
// Load dest object
$clonedObj = clone $this ;
2020-10-31 14:32:18 +01:00
$clonedObj -> socid = $socid ;
2016-10-14 07:54:44 +02:00
$this -> db -> begin ();
$objsoc = new Societe ( $this -> db );
$objsoc -> fetch ( $clonedObj -> socid );
2018-09-16 22:13:29 +02:00
// Clean data
2016-10-14 07:54:44 +02:00
$clonedObj -> statut = 0 ;
2018-09-16 22:13:29 +02:00
// Clean extrafields
2021-02-23 21:24:38 +01:00
if ( is_array ( $clonedObj -> array_options ) && count ( $clonedObj -> array_options ) > 0 ) {
2019-09-28 10:55:09 +02:00
$extrafields -> fetch_name_optionals_label ( $this -> table_element );
2021-02-23 21:24:38 +01:00
foreach ( $clonedObj -> array_options as $key => $option ) {
2018-09-16 22:13:29 +02:00
$shortkey = preg_replace ( '/options_/' , '' , $key );
//var_dump($shortkey); var_dump($extrafields->attributes[$this->element]['unique'][$shortkey]);
2021-02-23 21:24:38 +01:00
if ( ! empty ( $extrafields -> attributes [ $this -> element ][ 'unique' ][ $shortkey ])) {
2018-09-16 22:13:29 +02:00
//var_dump($key); var_dump($clonedObj->array_options[$key]); exit;
unset ( $clonedObj -> array_options [ $key ]);
}
}
}
2016-10-14 07:54:44 +02:00
2019-11-12 09:46:08 +01:00
if ( empty ( $conf -> global -> CONTRACT_ADDON ) || ! is_readable ( DOL_DOCUMENT_ROOT . " /core/modules/contract/ " . $conf -> global -> CONTRACT_ADDON . " .php " )) {
2016-10-14 07:54:44 +02:00
$this -> error = 'ErrorSetupNotComplete' ;
dol_syslog ( $this -> error );
2019-11-12 09:46:08 +01:00
return - 1 ;
2016-10-14 07:54:44 +02:00
}
// Set ref
2019-11-12 09:46:08 +01:00
require_once DOL_DOCUMENT_ROOT . " /core/modules/contract/ " . $conf -> global -> CONTRACT_ADDON . '.php' ;
2016-10-14 07:54:44 +02:00
$obj = $conf -> global -> CONTRACT_ADDON ;
$modContract = new $obj ();
$clonedObj -> ref = $modContract -> getNextValue ( $objsoc , $clonedObj );
// get extrafields so they will be clone
2018-09-16 22:13:29 +02:00
foreach ( $this -> lines as $line ) {
$line -> fetch_optionals ( $line -> id );
2016-10-14 07:54:44 +02:00
}
// Create clone
2019-01-19 12:46:38 +01:00
$clonedObj -> context [ 'createfromclone' ] = 'createfromclone' ;
2016-10-14 07:54:44 +02:00
$result = $clonedObj -> create ( $user );
if ( $result < 0 ) {
2019-11-12 09:46:08 +01:00
$error ++ ;
2016-10-14 07:54:44 +02:00
$this -> error = $clonedObj -> error ;
$this -> errors [] = $clonedObj -> error ;
} else {
2020-10-31 14:32:18 +01:00
// copy external contacts if same company
if ( $this -> socid == $clonedObj -> socid ) {
if ( $clonedObj -> copy_linked_contact ( $this , 'external' ) < 0 ) {
$error ++ ;
}
}
}
2016-10-14 07:54:44 +02:00
2019-11-12 09:46:08 +01:00
if ( ! $error ) {
2019-02-02 18:25:01 +01:00
foreach ( $this -> lines as $line ) {
2021-10-22 15:32:24 +02:00
$result = $clonedObj -> addline ( $line -> description , $line -> subprice , $line -> qty , $line -> tva_tx , $line -> localtax1_tx , $line -> localtax2_tx , $line -> fk_product , $line -> remise_percent , $line -> date_ouverture , $line -> date_cloture , 'HT' , 0 , $line -> info_bits , $line -> fk_fournprice , $line -> pa_ht , $line -> array_options , $line -> fk_unit );
2016-10-14 07:54:44 +02:00
if ( $result < 0 ) {
2019-11-12 09:46:08 +01:00
$error ++ ;
2016-10-14 07:54:44 +02:00
$this -> error = $clonedObj -> error ;
$this -> errors [] = $clonedObj -> error ;
}
}
}
2019-11-12 09:46:08 +01:00
if ( ! $error ) {
2016-10-14 07:54:44 +02:00
// Hook of thirdparty module
if ( is_object ( $hookmanager )) {
2019-11-12 09:46:08 +01:00
$parameters = array (
2016-10-14 07:54:44 +02:00
'objFrom' => $this ,
'clonedObj' => $clonedObj
);
$action = '' ;
$reshook = $hookmanager -> executeHooks ( 'createFrom' , $parameters , $clonedObj , $action ); // Note that $action and $object may have been modified by some hooks
2021-02-23 21:24:38 +01:00
if ( $reshook < 0 ) {
2023-01-18 09:53:28 +01:00
$this -> errors += $hookmanager -> errors ;
$this -> error = $hookmanager -> error ;
2019-11-12 09:46:08 +01:00
$error ++ ;
2021-02-23 21:24:38 +01:00
}
2016-10-14 07:54:44 +02:00
}
}
2019-01-19 12:46:38 +01:00
unset ( $clonedObj -> context [ 'createfromclone' ]);
2016-10-14 07:54:44 +02:00
// End
2019-11-12 09:46:08 +01:00
if ( ! $error ) {
2016-10-14 07:54:44 +02:00
$this -> db -> commit ();
return $clonedObj -> id ;
} else {
$this -> db -> rollback ();
2019-11-12 09:46:08 +01:00
return - 1 ;
2016-10-14 07:54:44 +02:00
}
}
2003-08-03 21:15:39 +02:00
}
2005-09-04 16:52:36 +02:00
/**
2019-06-29 16:29:32 +02:00
* Class to manage lines of contracts
2008-10-07 01:21:00 +02:00
*/
2015-02-26 14:15:33 +01:00
class ContratLigne extends CommonObjectLine
2005-09-04 16:52:36 +02:00
{
2020-10-31 14:32:18 +01:00
/**
2018-08-23 18:17:09 +02:00
* @ var string ID to identify managed object
*/
2019-11-12 09:46:08 +01:00
public $element = 'contratdet' ;
2018-08-29 21:25:22 +02:00
2020-10-31 14:32:18 +01: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 = 'contratdet' ;
2016-10-14 07:54:44 +02:00
2022-02-15 11:03:48 +01:00
/**
* @ var string Name to use for 'features' parameter to check module permissions with restrictedArea ()
*/
public $element_for_permission = 'contrat' ;
2018-08-22 11:24:31 +02:00
/**
* @ var int ID
*/
public $id ;
2018-08-29 21:25:22 +02:00
2018-08-31 18:36:15 +02:00
/**
* @ var string Ref
*/
public $ref ;
2018-09-02 10:42:31 +02:00
public $tms ;
2018-10-10 08:50:23 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int ID
*/
2018-09-02 10:42:31 +02:00
public $fk_contrat ;
2018-10-10 08:50:23 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int ID
*/
2018-09-02 10:42:31 +02:00
public $fk_product ;
2018-10-10 08:50:23 +02:00
2019-11-12 09:46:08 +01:00
public $statut ; // 0 inactive, 4 active, 5 closed
public $type ; // 0 for product, 1 for service
2016-06-06 17:18:31 +02:00
2018-04-21 13:14:58 +02:00
/**
* @ var string
* @ deprecated
*/
2018-09-02 10:42:31 +02:00
public $label ;
2014-12-28 19:36:43 +01:00
/**
* @ var string
2018-04-21 13:14:58 +02:00
* @ deprecated
2014-12-28 19:36:43 +01:00
*/
public $libelle ;
2018-08-31 19:26:08 +02:00
/**
* @ var string description
*/
public $description ;
2016-10-14 07:54:44 +02:00
2020-10-31 14:32:18 +01:00
public $product_type ; // 0 for product, 1 for service
2018-09-02 10:42:31 +02:00
public $product_ref ;
public $product_label ;
2016-10-14 07:54:44 +02:00
2018-09-02 10:42:31 +02:00
public $date_commande ;
2017-10-26 12:32:22 +02:00
2019-11-12 09:46:08 +01:00
public $date_start ; // date start planned
public $date_start_real ; // date start real
public $date_end ; // date end planned
public $date_end_real ; // date end real
2017-10-26 12:32:22 +02:00
// For backward compatibility
2021-08-26 21:35:31 +02:00
/**
* @ deprecated Use date_start
*/
2019-11-12 09:46:08 +01:00
public $date_ouverture_prevue ; // date start planned
2021-08-26 21:35:31 +02:00
/**
* @ deprecated Use date_start_real
*/
2019-11-12 09:46:08 +01:00
public $date_ouverture ; // date start real
2021-08-26 21:35:31 +02:00
/**
* @ deprecated Use date_end
*/
2019-11-12 09:46:08 +01:00
public $date_fin_validite ; // date end planned
2021-08-26 21:35:31 +02:00
/**
* @ deprecated Use date_end_real
*/
2019-11-12 09:46:08 +01:00
public $date_cloture ; // date end real
2021-08-26 21:35:31 +02:00
2018-09-02 10:42:31 +02:00
public $tva_tx ;
2022-06-11 21:53:14 +02:00
public $vat_src_code ;
2018-09-02 10:42:31 +02:00
public $localtax1_tx ;
public $localtax2_tx ;
2019-11-12 09:46:08 +01:00
public $localtax1_type ; // Local tax 1 type
public $localtax2_type ; // Local tax 2 type
2018-09-02 10:42:31 +02:00
public $qty ;
public $remise_percent ;
public $remise ;
2018-10-10 08:50:23 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int ID
*/
2018-09-02 10:42:31 +02:00
public $fk_remise_except ;
2019-11-12 09:46:08 +01:00
public $subprice ; // Unit price HT
2014-12-28 19:36:43 +01:00
/**
* @ var float
* @ deprecated Use $price_ht instead
2018-11-05 09:55:00 +01:00
* @ see $price_ht
2014-12-28 19:36:43 +01:00
*/
public $price ;
2018-09-02 10:42:31 +02:00
public $price_ht ;
2009-01-25 18:05:40 +01:00
2018-09-02 10:42:31 +02:00
public $total_ht ;
public $total_tva ;
public $total_localtax1 ;
public $total_localtax2 ;
public $total_ttc ;
2009-01-25 18:05:40 +01:00
2018-10-10 08:50:23 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int ID
*/
2018-09-02 10:42:31 +02:00
public $fk_fournprice ;
2018-10-10 08:50:23 +02:00
2018-09-02 10:42:31 +02:00
public $pa_ht ;
2013-02-13 09:50:08 +01:00
2018-09-02 10:42:31 +02:00
public $info_bits ;
2018-10-10 08:50:23 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int ID
*/
2018-09-02 10:42:31 +02:00
public $fk_user_author ;
2018-10-10 08:50:23 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int ID
*/
2018-09-02 10:42:31 +02:00
public $fk_user_ouverture ;
2018-10-10 08:50:23 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int ID
*/
2018-09-02 10:42:31 +02:00
public $fk_user_cloture ;
2018-10-10 08:50:23 +02:00
2018-09-02 10:42:31 +02:00
public $commentaire ;
2008-10-07 01:21:00 +02:00
2022-05-18 23:52:43 +02:00
2018-03-30 15:46:08 +02:00
const STATUS_INITIAL = 0 ;
const STATUS_OPEN = 4 ;
const STATUS_CLOSED = 5 ;
2022-02-15 11:03:48 +01:00
// 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 .
*/
public $fields = array (
'rowid' => array ( 'type' => 'integer' , 'label' => 'TechnicalID' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 10 ),
'entity' => array ( 'type' => 'integer' , 'label' => 'Entity' , 'default' => 1 , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' => 1 , 'position' => 30 , 'index' => 1 ),
'tms' => array ( 'type' => 'timestamp' , 'label' => 'DateModification' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 35 ),
'qty' => array ( 'type' => 'integer' , 'label' => 'Quantity' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 35 , 'isameasure' => 1 ),
'total_ht' => array ( 'type' => 'integer' , 'label' => 'AmountHT' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 36 , 'isameasure' => 1 ),
'total_tva' => array ( 'type' => 'integer' , 'label' => 'AmountVAT' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 37 , 'isameasure' => 1 ),
'total_ttc' => array ( 'type' => 'integer' , 'label' => 'AmountTTC' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 38 , 'isameasure' => 1 ),
//'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
//'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>70),
'fk_contrat' => array ( 'type' => 'integer:Contrat:contrat/class/contrat.class.php' , 'label' => 'Contract' , 'enabled' => 1 , 'visible' =>- 1 , 'notnull' => 1 , 'position' => 70 ),
'fk_product' => array ( 'type' => 'integer:Product:product/class/product.class.php:1' , 'label' => 'Product' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 75 ),
//'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>90),
'note_private' => array ( 'type' => 'text' , 'label' => 'NotePublic' , 'enabled' => 1 , 'visible' => 0 , 'position' => 105 ),
'note_public' => array ( 'type' => 'text' , 'label' => 'NotePrivate' , 'enabled' => 1 , 'visible' => 0 , 'position' => 110 ),
//'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>115),
//'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>120),
//'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
2022-03-21 22:02:34 +01:00
'fk_user_ouverture' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'UserStartingService' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' =>- 1 , 'position' => 135 ),
'fk_user_cloture' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'UserClosingService' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' =>- 1 , 'position' => 135 ),
2022-02-15 11:03:48 +01:00
'statut' => array ( 'type' => 'smallint(6)' , 'label' => 'Statut' , 'enabled' => 1 , 'visible' =>- 1 , 'position' => 500 , 'arrayofkeyval' => array ( 0 => 'Draft' , 4 => 'Open' , 5 => 'Closed' ))
);
// END MODULEBUILDER PROPERTIES
2007-07-07 18:38:58 +02:00
/**
2020-10-31 14:32:18 +01:00
* Constructor
*
* @ param DoliDb $db Database handler
2007-07-07 18:38:58 +02:00
*/
2019-02-25 20:35:59 +01:00
public function __construct ( $db )
2008-10-07 01:21:00 +02:00
{
2012-01-04 21:47:40 +01:00
$this -> db = $db ;
2008-10-07 01:21:00 +02:00
}
2007-07-07 18:38:58 +02:00
2006-03-18 18:41:36 +01:00
/**
2012-03-18 19:23:01 +01:00
* Return label of this contract line status
2012-01-04 21:47:40 +01:00
*
2020-02-10 21:28:49 +01: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
* @ return string Label of status
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function getLibStatut ( $mode )
2008-10-07 01:21:00 +02:00
{
2019-11-12 09:46:08 +01:00
return $this -> LibStatut ( $this -> statut , $mode , (( ! empty ( $this -> date_fin_validite )) ? ( $this -> date_fin_validite < dol_now () ? 1 : 0 ) : - 1 ));
2008-10-07 01:21:00 +02:00
}
2006-03-18 18:41:36 +01:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2006-03-18 18:41:36 +01:00
/**
2012-03-18 19:23:01 +01:00
* Return label of a contract line status
2012-01-04 21:47:40 +01:00
*
2019-11-01 15:53:57 +01:00
* @ param int $status Id status
2020-02-10 21:28:49 +01: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
2012-03-18 19:23:01 +01:00
* @ param int $expired 0 = Not expired , 1 = Expired , - 1 = Both or unknown
2017-08-24 14:54:32 +02:00
* @ param string $moreatt More attribute
2020-02-10 21:28:49 +01:00
* @ return string Label of status
2008-10-07 01:21:00 +02:00
*/
2019-11-01 15:53:57 +01:00
public static function LibStatut ( $status , $mode , $expired = - 1 , $moreatt = '' )
2008-10-07 01:21:00 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2008-10-07 01:21:00 +02:00
global $langs ;
$langs -> load ( " contracts " );
2019-11-01 15:53:57 +01:00
2020-10-31 14:32:18 +01:00
if ( $status == self :: STATUS_INITIAL ) {
2021-10-13 14:43:42 +02:00
$labelStatus = $langs -> transnoentities ( " ServiceStatusInitial " );
$labelStatusShort = $langs -> transnoentities ( " ServiceStatusInitial " );
2020-10-31 14:32:18 +01:00
} elseif ( $status == self :: STATUS_OPEN && $expired == - 1 ) {
2021-10-13 14:43:42 +02:00
$labelStatus = $langs -> transnoentities ( " ServiceStatusRunning " );
$labelStatusShort = $langs -> transnoentities ( " ServiceStatusRunning " );
2020-10-31 14:32:18 +01:00
} elseif ( $status == self :: STATUS_OPEN && $expired == 0 ) {
2021-10-13 14:43:42 +02:00
$labelStatus = $langs -> transnoentities ( " ServiceStatusNotLate " );
$labelStatusShort = $langs -> transnoentities ( " ServiceStatusNotLateShort " );
2020-10-31 14:32:18 +01:00
} elseif ( $status == self :: STATUS_OPEN && $expired == 1 ) {
2021-10-13 14:43:42 +02:00
$labelStatus = $langs -> transnoentities ( " ServiceStatusLate " );
$labelStatusShort = $langs -> transnoentities ( " ServiceStatusLateShort " );
2020-10-31 14:32:18 +01:00
} elseif ( $status == self :: STATUS_CLOSED ) {
2021-10-13 14:43:42 +02:00
$labelStatus = $langs -> transnoentities ( " ServiceStatusClosed " );
$labelStatusShort = $langs -> transnoentities ( " ServiceStatusClosed " );
2020-10-31 14:32:18 +01:00
}
2019-11-01 15:53:57 +01:00
$statusType = 'status' . $status ;
2021-02-23 21:24:38 +01:00
if ( $status == self :: STATUS_OPEN && $expired == 1 ) {
$statusType = 'status1' ;
}
if ( $status == self :: STATUS_CLOSED ) {
$statusType = 'status6' ;
}
2019-11-01 15:53:57 +01:00
$params = array (); $reg = array ();
2021-02-23 21:24:38 +01:00
if ( preg_match ( '/class="(.*)"/' , $moreatt , $reg )) {
2019-11-01 15:53:57 +01:00
$params = array ( 'badgeParams' => array ( 'css' => $reg [ 1 ]));
2006-04-07 23:57:41 +02:00
}
2019-11-01 15:53:57 +01:00
return dolGetStatus ( $labelStatus , $labelStatusShort , '' , $statusType , $mode , '' , $params );
2008-10-07 01:21:00 +02:00
}
2006-03-18 18:41:36 +01:00
2008-04-13 21:53:39 +02:00
/**
2015-03-15 14:04:07 +01:00
* Return clicable name ( with picto eventually )
2012-01-04 21:47:40 +01:00
*
2015-03-15 14:04:07 +01:00
* @ param int $withpicto 0 = No picto , 1 = Include picto into link , 2 = Only picto
2012-03-18 19:23:01 +01:00
* @ param int $maxlength Max length
* @ return string Chaine avec URL
2020-10-31 14:32:18 +01:00
*/
public function getNomUrl ( $withpicto = 0 , $maxlength = 0 )
2008-04-13 21:53:39 +02:00
{
global $langs ;
2008-10-07 01:21:00 +02:00
2019-11-12 09:46:08 +01:00
$result = '' ;
2020-10-31 14:32:18 +01:00
$label = $langs -> trans ( " ShowContractOfService " ) . ': ' . $this -> label ;
2021-02-23 21:24:38 +01:00
if ( empty ( $label )) {
$label = $this -> description ;
}
2016-10-14 07:54:44 +02:00
2020-10-31 14:32:18 +01:00
$link = '<a href="' . DOL_URL_ROOT . '/contrat/card.php?id=' . $this -> fk_contrat . '" title="' . dol_escape_htmltag ( $label , 1 ) . '" class="classfortooltip">' ;
2019-11-12 09:46:08 +01:00
$linkend = '</a>' ;
2008-10-07 01:21:00 +02:00
2019-11-12 09:46:08 +01:00
$picto = 'service' ;
2021-02-23 21:24:38 +01:00
if ( $this -> type == 0 ) {
$picto = 'product' ;
}
2008-10-07 01:21:00 +02:00
2021-02-23 21:24:38 +01:00
if ( $withpicto ) {
$result .= ( $link . img_object ( $label , $picto , 'class="classfortooltip"' ) . $linkend );
}
if ( $withpicto && $withpicto != 2 ) {
$result .= ' ' ;
}
if ( $withpicto != 2 ) {
$result .= $link . ( $this -> product_ref ? $this -> product_ref . ' ' : '' ) . ( $this -> label ? $this -> label : $this -> description ) . $linkend ;
}
2008-04-13 21:53:39 +02:00
return $result ;
}
2009-02-15 16:04:02 +01:00
/**
2019-02-25 20:35:59 +01:00
* Load object in memory from database
2012-01-04 21:47:40 +01:00
*
2019-02-25 20:35:59 +01:00
* @ param int $id Id object
2020-07-20 00:24:14 +02:00
* @ param string $ref Ref of contract line
2019-02-25 20:35:59 +01:00
* @ return int < 0 if KO , > 0 if OK
2008-10-07 01:21:00 +02:00
*/
2020-10-31 14:32:18 +01:00
public function fetch ( $id , $ref = '' )
2008-10-07 01:21:00 +02:00
{
2009-02-18 21:48:49 +01:00
// Check parameters
2021-02-23 21:24:38 +01:00
if ( empty ( $id ) && empty ( $ref )) {
return - 1 ;
}
2009-02-18 21:48:49 +01:00
2008-10-07 01:21:00 +02:00
$sql = " SELECT " ;
2019-11-12 09:46:08 +01:00
$sql .= " t.rowid, " ;
$sql .= " t.tms, " ;
$sql .= " t.fk_contrat, " ;
$sql .= " t.fk_product, " ;
$sql .= " t.statut, " ;
$sql .= " t.label, " ; // This field is not used. Only label of product
$sql .= " p.ref as product_ref, " ;
$sql .= " p.label as product_label, " ;
$sql .= " p.description as product_desc, " ;
$sql .= " p.fk_product_type as product_type, " ;
$sql .= " t.description, " ;
$sql .= " t.date_commande, " ;
$sql .= " t.date_ouverture_prevue as date_ouverture_prevue, " ;
$sql .= " t.date_ouverture as date_ouverture, " ;
$sql .= " t.date_fin_validite as date_fin_validite, " ;
$sql .= " t.date_cloture as date_cloture, " ;
$sql .= " t.tva_tx, " ;
$sql .= " t.vat_src_code, " ;
$sql .= " t.localtax1_tx, " ;
$sql .= " t.localtax2_tx, " ;
$sql .= " t.localtax1_type, " ;
$sql .= " t.localtax2_type, " ;
$sql .= " t.qty, " ;
$sql .= " t.remise_percent, " ;
$sql .= " t.remise, " ;
$sql .= " t.fk_remise_except, " ;
$sql .= " t.subprice, " ;
$sql .= " t.price_ht, " ;
$sql .= " t.total_ht, " ;
$sql .= " t.total_tva, " ;
$sql .= " t.total_localtax1, " ;
$sql .= " t.total_localtax2, " ;
$sql .= " t.total_ttc, " ;
$sql .= " t.fk_product_fournisseur_price as fk_fournprice, " ;
$sql .= " t.buy_price_ht as pa_ht, " ;
$sql .= " t.info_bits, " ;
$sql .= " t.fk_user_author, " ;
$sql .= " t.fk_user_ouverture, " ;
$sql .= " t.fk_user_cloture, " ;
$sql .= " t.commentaire, " ;
$sql .= " t.fk_unit " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " contratdet as t LEFT JOIN " . MAIN_DB_PREFIX . " product as p ON p.rowid = t.fk_product " ;
2021-02-23 21:24:38 +01:00
if ( $id ) {
2021-03-14 11:48:39 +01:00
$sql .= " WHERE t.rowid = " . (( int ) $id );
2021-02-23 21:24:38 +01:00
}
if ( $ref ) {
$sql .= " WHERE t.rowid = ' " . $this -> db -> escape ( $ref ) . " ' " ;
}
2008-10-07 01:21:00 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2019-11-12 09:46:08 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( $resql ) {
if ( $this -> db -> num_rows ( $resql )) {
2008-10-07 01:21:00 +02:00
$obj = $this -> db -> fetch_object ( $resql );
$this -> id = $obj -> rowid ;
2009-02-15 16:04:02 +01:00
$this -> ref = $obj -> rowid ;
2009-02-15 18:33:02 +01:00
2010-01-13 19:51:19 +01:00
$this -> tms = $this -> db -> jdate ( $obj -> tms );
2008-04-06 21:34:55 +02:00
$this -> fk_contrat = $obj -> fk_contrat ;
$this -> fk_product = $obj -> fk_product ;
$this -> statut = $obj -> statut ;
2015-04-29 19:57:01 +02:00
$this -> product_ref = $obj -> product_ref ;
$this -> product_label = $obj -> product_label ;
$this -> product_description = $obj -> product_description ;
2016-06-06 17:18:31 +02:00
$this -> product_type = $obj -> product_type ;
2019-11-12 09:46:08 +01:00
$this -> label = $obj -> label ; // deprecated. We do not use this field. Only ref and label of product, and description of contract line
2008-04-06 21:34:55 +02:00
$this -> description = $obj -> description ;
2010-01-13 19:51:19 +01:00
$this -> date_commande = $this -> db -> jdate ( $obj -> date_commande );
2017-10-26 12:32:22 +02:00
$this -> date_start = $this -> db -> jdate ( $obj -> date_ouverture_prevue );
$this -> date_start_real = $this -> db -> jdate ( $obj -> date_ouverture );
$this -> date_end = $this -> db -> jdate ( $obj -> date_fin_validite );
$this -> date_end_real = $this -> db -> jdate ( $obj -> date_cloture );
// For backward compatibility
2010-01-13 19:51:19 +01:00
$this -> date_ouverture_prevue = $this -> db -> jdate ( $obj -> date_ouverture_prevue );
$this -> date_ouverture = $this -> db -> jdate ( $obj -> date_ouverture );
$this -> date_fin_validite = $this -> db -> jdate ( $obj -> date_fin_validite );
$this -> date_cloture = $this -> db -> jdate ( $obj -> date_cloture );
2017-10-26 12:32:22 +02:00
2008-04-06 21:34:55 +02:00
$this -> tva_tx = $obj -> tva_tx ;
2017-03-04 21:45:19 +01:00
$this -> vat_src_code = $obj -> vat_src_code ;
2010-06-22 12:42:37 +02:00
$this -> localtax1_tx = $obj -> localtax1_tx ;
$this -> localtax2_tx = $obj -> localtax2_tx ;
2017-09-24 23:55:44 +02:00
$this -> localtax1_type = $obj -> localtax1_type ;
$this -> localtax2_type = $obj -> localtax2_type ;
2008-04-06 21:34:55 +02:00
$this -> qty = $obj -> qty ;
$this -> remise_percent = $obj -> remise_percent ;
$this -> fk_remise_except = $obj -> fk_remise_except ;
$this -> subprice = $obj -> subprice ;
$this -> price_ht = $obj -> price_ht ;
$this -> total_ht = $obj -> total_ht ;
$this -> total_tva = $obj -> total_tva ;
2010-06-22 12:42:37 +02:00
$this -> total_localtax1 = $obj -> total_localtax1 ;
$this -> total_localtax2 = $obj -> total_localtax2 ;
2008-04-06 21:34:55 +02:00
$this -> total_ttc = $obj -> total_ttc ;
$this -> info_bits = $obj -> info_bits ;
$this -> fk_user_author = $obj -> fk_user_author ;
$this -> fk_user_ouverture = $obj -> fk_user_ouverture ;
$this -> fk_user_cloture = $obj -> fk_user_cloture ;
$this -> commentaire = $obj -> commentaire ;
2013-02-13 09:50:08 +01:00
$this -> fk_fournprice = $obj -> fk_fournprice ;
2020-07-20 00:24:14 +02:00
2013-02-13 09:50:08 +01:00
$marginInfos = getMarginInfos ( $obj -> subprice , $obj -> remise_percent , $obj -> tva_tx , $obj -> localtax1_tx , $obj -> localtax2_tx , $this -> fk_fournprice , $obj -> pa_ht );
$this -> pa_ht = $marginInfos [ 0 ];
2019-11-12 09:46:08 +01:00
$this -> fk_unit = $obj -> fk_unit ;
2020-07-20 00:24:14 +02:00
$this -> fetch_optionals ();
2008-10-07 01:21:00 +02:00
}
2020-07-20 00:24:14 +02:00
2008-10-07 01:21:00 +02:00
$this -> db -> free ( $resql );
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-12 09:46:08 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2008-10-07 01:21:00 +02:00
return - 1 ;
}
}
2011-08-08 16:25:44 +02:00
/**
* Update database for contract line
2012-01-04 21:47:40 +01:00
*
* @ param User $user User that modify
* @ param int $notrigger 0 = no , 1 = yes ( no update trigger )
* @ return int < 0 if KO , > 0 if OK
2008-10-07 01:21:00 +02:00
*/
2019-02-25 20:35:59 +01:00
public function update ( $user , $notrigger = 0 )
2008-10-07 01:21:00 +02:00
{
2015-10-06 14:52:06 +02:00
global $conf , $langs , $mysoc ;
2010-11-01 15:35:17 +01:00
2019-11-12 09:46:08 +01:00
$error = 0 ;
2012-01-04 21:47:40 +01:00
2008-04-06 21:34:55 +02:00
// Clean parameters
2018-10-16 23:20:13 +02:00
$this -> fk_contrat = ( int ) $this -> fk_contrat ;
$this -> fk_product = ( int ) $this -> fk_product ;
$this -> statut = ( int ) $this -> statut ;
2019-11-12 09:46:08 +01:00
$this -> label = trim ( $this -> label );
$this -> description = trim ( $this -> description );
$this -> vat_src_code = trim ( $this -> vat_src_code );
$this -> tva_tx = trim ( $this -> tva_tx );
$this -> localtax1_tx = trim ( $this -> localtax1_tx );
$this -> localtax2_tx = trim ( $this -> localtax2_tx );
$this -> qty = trim ( $this -> qty );
$this -> remise_percent = trim ( $this -> remise_percent );
2018-10-16 23:20:13 +02:00
$this -> fk_remise_except = ( int ) $this -> fk_remise_except ;
2019-11-12 09:46:08 +01:00
$this -> subprice = price2num ( $this -> subprice );
$this -> price_ht = price2num ( $this -> price_ht );
$this -> total_ht = trim ( $this -> total_ht );
$this -> total_tva = trim ( $this -> total_tva );
$this -> total_localtax1 = trim ( $this -> total_localtax1 );
$this -> total_localtax2 = trim ( $this -> total_localtax2 );
$this -> total_ttc = trim ( $this -> total_ttc );
$this -> info_bits = trim ( $this -> info_bits );
2018-10-16 23:20:13 +02:00
$this -> fk_user_author = ( int ) $this -> fk_user_author ;
$this -> fk_user_ouverture = ( int ) $this -> fk_user_ouverture ;
$this -> fk_user_cloture = ( int ) $this -> fk_user_cloture ;
2019-11-12 09:46:08 +01:00
$this -> commentaire = trim ( $this -> commentaire );
2015-04-10 10:17:20 +02:00
//if (empty($this->subprice)) $this->subprice = 0;
2021-02-23 21:24:38 +01:00
if ( empty ( $this -> price_ht )) {
$this -> price_ht = 0 ;
}
if ( empty ( $this -> total_ht )) {
$this -> total_ht = 0 ;
}
if ( empty ( $this -> total_tva )) {
$this -> total_tva = 0 ;
}
if ( empty ( $this -> total_ttc )) {
$this -> total_ttc = 0 ;
}
if ( empty ( $this -> localtax1_tx )) {
$this -> localtax1_tx = 0 ;
}
if ( empty ( $this -> localtax2_tx )) {
$this -> localtax2_tx = 0 ;
}
if ( empty ( $this -> remise_percent )) {
$this -> remise_percent = 0 ;
}
2017-10-26 12:32:22 +02:00
// For backward compatibility
2021-02-23 21:24:38 +01:00
if ( empty ( $this -> date_start )) {
$this -> date_start = $this -> date_ouverture_prevue ;
}
if ( empty ( $this -> date_start_real )) {
$this -> date_start = $this -> date_ouverture ;
}
if ( empty ( $this -> date_end )) {
$this -> date_start = $this -> date_fin_validite ;
}
if ( empty ( $this -> date_end_real )) {
$this -> date_start = $this -> date_cloture ;
}
2017-10-26 12:32:22 +02:00
2015-04-21 15:49:58 +02:00
2008-04-06 21:34:55 +02:00
// Check parameters
// Put here code to add control on parameters values
2012-04-07 02:24:32 +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.
2015-10-06 14:52:06 +02:00
$localtaxes_type = getLocalTaxesFromRate ( $this -> txtva , 0 , $this -> societe , $mysoc );
2019-11-12 09:46:08 +01:00
$tabprice = calcul_price_total ( $this -> qty , $this -> price_ht , $this -> remise_percent , $this -> tva_tx , $this -> localtax1_tx , $this -> localtax2_tx , 0 , 'HT' , 0 , 1 , $mysoc , $localtaxes_type );
2012-04-07 02:24:32 +02:00
$this -> total_ht = $tabprice [ 0 ];
$this -> total_tva = $tabprice [ 1 ];
$this -> total_ttc = $tabprice [ 2 ];
2019-11-12 09:46:08 +01:00
$this -> total_localtax1 = $tabprice [ 9 ];
$this -> total_localtax2 = $tabprice [ 10 ];
2012-04-07 02:24:32 +02:00
2021-02-23 21:24:38 +01:00
if ( empty ( $this -> pa_ht )) {
$this -> pa_ht = 0 ;
}
2013-02-13 09:50:08 +01:00
2015-10-26 20:33:42 +01:00
// if buy price not defined, define buyprice as configured in margin admin
2021-02-23 21:24:38 +01:00
if ( $this -> pa_ht == 0 ) {
if (( $result = $this -> defineBuyPrice ( $this -> subprice , $this -> remise_percent , $this -> fk_product )) < 0 ) {
2015-10-26 20:33:42 +01:00
return $result ;
2020-05-21 15:05:19 +02:00
} else {
2015-10-26 20:33:42 +01:00
$this -> pa_ht = $result ;
}
2013-02-13 09:50:08 +01:00
}
2015-10-26 20:33:42 +01:00
2014-07-03 15:21:43 +02:00
$this -> db -> begin ();
2014-07-04 13:43:50 +02:00
2018-02-04 10:50:58 +01:00
$this -> oldcopy = new ContratLigne ( $this -> db );
$this -> oldcopy -> fetch ( $this -> id );
2008-10-07 01:21:00 +02:00
// Update request
$sql = " UPDATE " . MAIN_DB_PREFIX . " contratdet SET " ;
2021-06-09 15:36:47 +02:00
$sql .= " fk_contrat= " . (( int ) $this -> fk_contrat ) . " , " ;
2019-11-12 09:46:08 +01:00
$sql .= " fk_product= " . ( $this -> fk_product ? " ' " . $this -> db -> escape ( $this -> fk_product ) . " ' " : 'null' ) . " , " ;
2021-06-09 15:36:47 +02:00
$sql .= " statut= " . (( int ) $this -> statut ) . " , " ;
2019-11-12 09:46:08 +01:00
$sql .= " label=' " . $this -> db -> escape ( $this -> label ) . " ', " ;
$sql .= " description=' " . $this -> db -> escape ( $this -> description ) . " ', " ;
$sql .= " date_commande= " . ( $this -> date_commande != '' ? " ' " . $this -> db -> idate ( $this -> date_commande ) . " ' " : " null " ) . " , " ;
$sql .= " date_ouverture_prevue= " . ( $this -> date_ouverture_prevue != '' ? " ' " . $this -> db -> idate ( $this -> date_ouverture_prevue ) . " ' " : " null " ) . " , " ;
$sql .= " date_ouverture= " . ( $this -> date_ouverture != '' ? " ' " . $this -> db -> idate ( $this -> date_ouverture ) . " ' " : " null " ) . " , " ;
$sql .= " date_fin_validite= " . ( $this -> date_fin_validite != '' ? " ' " . $this -> db -> idate ( $this -> date_fin_validite ) . " ' " : " null " ) . " , " ;
$sql .= " date_cloture= " . ( $this -> date_cloture != '' ? " ' " . $this -> db -> idate ( $this -> date_cloture ) . " ' " : " null " ) . " , " ;
$sql .= " vat_src_code=' " . $this -> db -> escape ( $this -> vat_src_code ) . " ', " ;
$sql .= " tva_tx= " . price2num ( $this -> tva_tx ) . " , " ;
$sql .= " localtax1_tx= " . price2num ( $this -> localtax1_tx ) . " , " ;
$sql .= " localtax2_tx= " . price2num ( $this -> localtax2_tx ) . " , " ;
$sql .= " qty= " . price2num ( $this -> qty ) . " , " ;
$sql .= " remise_percent= " . price2num ( $this -> remise_percent ) . " , " ;
$sql .= " remise= " . ( $this -> remise ? price2num ( $this -> remise ) : " null " ) . " , " ;
$sql .= " fk_remise_except= " . ( $this -> fk_remise_except > 0 ? $this -> fk_remise_except : " null " ) . " , " ;
$sql .= " subprice= " . ( $this -> subprice != '' ? $this -> subprice : " null " ) . " , " ;
$sql .= " price_ht= " . ( $this -> price_ht != '' ? $this -> price_ht : " null " ) . " , " ;
$sql .= " total_ht= " . $this -> total_ht . " , " ;
$sql .= " total_tva= " . $this -> total_tva . " , " ;
$sql .= " total_localtax1= " . $this -> total_localtax1 . " , " ;
$sql .= " total_localtax2= " . $this -> total_localtax2 . " , " ;
$sql .= " total_ttc= " . $this -> total_ttc . " , " ;
$sql .= " fk_product_fournisseur_price= " . ( ! empty ( $this -> fk_fournprice ) ? $this -> fk_fournprice : " NULL " ) . " , " ;
$sql .= " buy_price_ht=' " . price2num ( $this -> pa_ht ) . " ', " ;
$sql .= " info_bits=' " . $this -> db -> escape ( $this -> info_bits ) . " ', " ;
$sql .= " fk_user_author= " . ( $this -> fk_user_author >= 0 ? $this -> fk_user_author : " NULL " ) . " , " ;
$sql .= " fk_user_ouverture= " . ( $this -> fk_user_ouverture > 0 ? $this -> fk_user_ouverture : " NULL " ) . " , " ;
$sql .= " fk_user_cloture= " . ( $this -> fk_user_cloture > 0 ? $this -> fk_user_cloture : " NULL " ) . " , " ;
$sql .= " commentaire=' " . $this -> db -> escape ( $this -> commentaire ) . " ', " ;
$sql .= " fk_unit= " . ( ! $this -> fk_unit ? 'NULL' : $this -> fk_unit );
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid= " . (( int ) $this -> id );
2008-04-06 21:34:55 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
2008-10-07 01:21:00 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( ! $resql ) {
2019-11-12 09:46:08 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2015-01-27 12:53:02 +01:00
$error ++ ;
}
2015-02-28 04:59:27 +01:00
2021-02-23 21:24:38 +01:00
if ( ! $error ) { // For avoid conflicts if trigger used
2019-11-12 09:46:08 +01:00
$result = $this -> insertExtraFields ();
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
2015-01-27 12:53:02 +01:00
$error ++ ;
}
2008-10-07 01:21:00 +02:00
}
2008-04-06 21:34:55 +02:00
2018-02-04 10:50:58 +01:00
// If we change a planned date (start or end), sync dates for all services
2021-02-23 21:24:38 +01:00
if ( ! $error && ! empty ( $conf -> global -> CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES )) {
if ( $this -> date_ouverture_prevue != $this -> oldcopy -> date_ouverture_prevue ) {
2019-11-12 09:46:08 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'contratdet SET' ;
$sql .= " date_ouverture_prevue = " . ( $this -> date_ouverture_prevue != '' ? " ' " . $this -> db -> idate ( $this -> date_ouverture_prevue ) . " ' " : " null " );
2021-08-23 19:33:24 +02:00
$sql .= " WHERE fk_contrat = " . (( int ) $this -> fk_contrat );
2018-02-03 18:50:20 +01:00
2018-02-04 10:50:58 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( ! $resql ) {
2018-02-04 10:50:58 +01:00
$error ++ ;
2019-11-12 09:46:08 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2018-02-04 10:50:58 +01:00
}
}
2021-02-23 21:24:38 +01:00
if ( $this -> date_fin_validite != $this -> oldcopy -> date_fin_validite ) {
2019-11-12 09:46:08 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'contratdet SET' ;
$sql .= " date_fin_validite = " . ( $this -> date_fin_validite != '' ? " ' " . $this -> db -> idate ( $this -> date_fin_validite ) . " ' " : " null " );
2021-08-23 19:33:24 +02:00
$sql .= " WHERE fk_contrat = " . (( int ) $this -> fk_contrat );
2018-02-04 10:50:58 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( ! $resql ) {
2018-02-04 10:50:58 +01:00
$error ++ ;
2019-11-12 09:46:08 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2018-02-04 10:50:58 +01:00
}
2018-02-03 18:50:20 +01:00
}
2008-10-07 01:21:00 +02:00
}
2018-02-03 18:50:20 +01:00
2019-11-12 09:46:08 +01:00
if ( ! $error && ! $notrigger ) {
2020-10-31 14:32:18 +01:00
// Call trigger
2022-04-02 10:31:58 +02:00
$result = $this -> call_trigger ( 'LINECONTRACT_MODIFY' , $user );
2020-10-31 14:32:18 +01:00
if ( $result < 0 ) {
$error ++ ;
$this -> db -> rollback ();
}
// End call triggers
2015-01-27 12:53:02 +01:00
}
2008-04-06 21:34:55 +02:00
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2020-10-31 14:32:18 +01:00
$this -> db -> commit ();
2018-02-03 18:50:20 +01:00
return 1 ;
2015-01-27 12:53:02 +01:00
} else {
$this -> db -> rollback ();
2019-11-12 09:46:08 +01:00
$this -> errors [] = $this -> error ;
2015-01-27 12:53:02 +01:00
return - 1 ;
}
2008-10-07 01:21:00 +02:00
}
2007-09-04 01:18:04 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2007-09-04 01:18:04 +02:00
/**
2011-08-08 16:25:44 +02:00
* Mise a jour en base des champs total_xxx de ligne
* Used by migration process
2012-01-04 21:47:40 +01:00
*
2011-08-08 16:25:44 +02:00
* @ return int < 0 if KO , > 0 if OK
2007-07-07 18:38:58 +02:00
*/
2019-02-25 20:35:59 +01:00
public function update_total ()
2007-07-07 18:38:58 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2007-07-07 18:38:58 +02:00
$this -> db -> begin ();
// Mise a jour ligne en base
$sql = " UPDATE " . MAIN_DB_PREFIX . " contratdet SET " ;
2019-11-12 09:46:08 +01:00
$sql .= " total_ht= " . price2num ( $this -> total_ht , 'MT' ) . " " ;
$sql .= " ,total_tva= " . price2num ( $this -> total_tva , 'MT' ) . " " ;
$sql .= " ,total_localtax1= " . price2num ( $this -> total_localtax1 , 'MT' ) . " " ;
$sql .= " ,total_localtax2= " . price2num ( $this -> total_localtax2 , 'MT' ) . " " ;
$sql .= " ,total_ttc= " . price2num ( $this -> total_ttc , 'MT' ) . " " ;
2021-08-27 16:33:03 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2007-07-07 18:38:58 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::update_total " , LOG_DEBUG );
2007-07-07 18:38:58 +02:00
2019-11-12 09:46:08 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( $resql ) {
2007-07-07 18:38:58 +02:00
$this -> db -> commit ();
2008-10-07 01:21:00 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> error ();
2007-07-07 18:38:58 +02:00
$this -> db -> rollback ();
2008-10-07 01:21:00 +02:00
return - 2 ;
2007-07-07 18:38:58 +02:00
}
}
2013-06-16 21:31:21 +02:00
2015-07-20 11:19:40 +02:00
/**
* Inserts a contrat line into database
*
* @ param int $notrigger Set to 1 if you don ' t want triggers to be fired
* @ return int < 0 if KO , > 0 if OK
*/
public function insert ( $notrigger = 0 )
{
2018-02-15 15:32:43 +01:00
global $conf , $user ;
2015-07-20 11:19:40 +02:00
2020-04-23 13:21:39 +02:00
$error = 0 ;
2015-07-20 11:19:40 +02:00
// Insertion dans la base
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " contratdet " ;
2019-11-12 09:46:08 +01:00
$sql .= " (fk_contrat, label, description, fk_product, qty, vat_src_code, tva_tx, " ;
$sql .= " localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, " ;
$sql .= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, " ;
$sql .= " info_bits, " ;
$sql .= " price_ht, remise, fk_product_fournisseur_price, buy_price_ht " ;
2021-02-23 21:24:38 +01:00
if ( $this -> date_ouverture_prevue > 0 ) {
$sql .= " ,date_ouverture_prevue " ;
}
if ( $this -> date_fin_validite > 0 ) {
$sql .= " ,date_fin_validite " ;
}
2019-11-12 09:46:08 +01:00
$sql .= " ) VALUES ( $this->fk_contrat , '', ' " . $this -> db -> escape ( $this -> description ) . " ', " ;
$sql .= ( $this -> fk_product > 0 ? $this -> fk_product : " null " ) . " , " ;
$sql .= " ' " . $this -> db -> escape ( $this -> qty ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> vat_src_code ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> tva_tx ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> localtax1_tx ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> localtax2_tx ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> localtax1_type ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> localtax2_type ) . " ', " ;
$sql .= " " . price2num ( $this -> remise_percent ) . " , " . price2num ( $this -> subprice ) . " , " ;
$sql .= " " . price2num ( $this -> total_ht ) . " , " . price2num ( $this -> total_tva ) . " , " . price2num ( $this -> total_localtax1 ) . " , " . price2num ( $this -> total_localtax2 ) . " , " . price2num ( $this -> total_ttc ) . " , " ;
$sql .= " ' " . $this -> db -> escape ( $this -> info_bits ) . " ', " ;
$sql .= " " . price2num ( $this -> price_ht ) . " , " . price2num ( $this -> remise ) . " , " ;
2021-02-23 21:24:38 +01:00
if ( $this -> fk_fournprice > 0 ) {
2021-08-28 00:55:51 +02:00
$sql .= ' ' . (( int ) $this -> fk_fournprice ) . ',' ;
2021-02-23 21:24:38 +01:00
} else {
$sql .= ' null,' ;
}
if ( $this -> pa_ht > 0 ) {
2021-08-28 00:55:51 +02:00
$sql .= ' ' . (( float ) price2num ( $this -> pa_ht ));
2021-02-23 21:24:38 +01:00
} else {
$sql .= ' null' ;
}
if ( $this -> date_ouverture > 0 ) {
$sql .= " ,' " . $this -> db -> idate ( $this -> date_ouverture ) . " ' " ;
}
if ( $this -> date_cloture > 0 ) {
$sql .= " ,' " . $this -> db -> idate ( $this -> date_cloture ) . " ' " ;
}
2019-11-12 09:46:08 +01:00
$sql .= " ) " ;
2015-07-20 11:19:40 +02:00
dol_syslog ( get_class ( $this ) . " ::insert " , LOG_DEBUG );
2019-11-12 09:46:08 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( $resql ) {
2015-07-20 11:19:40 +02:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . 'contratdet' );
2018-02-09 15:47:18 +01:00
// Insert of extrafields
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2018-02-09 15:47:18 +01:00
$result = $this -> insertExtraFields ();
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
2018-02-09 15:47:18 +01:00
$this -> db -> rollback ();
return - 1 ;
}
}
2015-07-28 18:57:13 +02:00
2021-02-23 21:24:38 +01:00
if ( ! $notrigger ) {
2015-07-20 11:19:40 +02:00
// Call trigger
2015-10-11 13:01:28 +02:00
$result = $this -> call_trigger ( 'LINECONTRACT_INSERT' , $user );
2015-07-20 11:19:40 +02:00
if ( $result < 0 ) {
$this -> db -> rollback ();
return - 1 ;
}
// End call triggers
}
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2015-07-20 11:19:40 +02:00
$this -> db -> rollback ();
2019-11-12 09:46:08 +01:00
$this -> error = $this -> db -> error () . " sql= " . $sql ;
2015-07-20 11:19:40 +02:00
return - 1 ;
}
}
2016-11-19 19:38:06 +01:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2016-11-19 19:38:06 +01:00
/**
* Activate a contract line
*
2017-09-03 14:26:27 +02:00
* @ param User $user Objet User who activate contract
* @ param int $date Date activation
* @ param int | string $date_end Date planned end . Use '-1' to keep it unchanged .
* @ param string $comment A comment typed by user
* @ return int < 0 if KO , > 0 if OK
2016-11-19 19:38:06 +01:00
*/
2019-02-25 20:35:59 +01:00
public function active_line ( $user , $date , $date_end = '' , $comment = '' )
2016-11-19 19:38:06 +01:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2016-11-19 19:38:06 +01:00
global $langs , $conf ;
$error = 0 ;
$this -> db -> begin ();
2019-11-12 09:46:08 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " contratdet SET statut = " . ContratLigne :: STATUS_OPEN . " , " ;
$sql .= " date_ouverture = " . ( dol_strlen ( $date ) != 0 ? " ' " . $this -> db -> idate ( $date ) . " ' " : " null " ) . " , " ;
2021-02-23 21:24:38 +01:00
if ( $date_end >= 0 ) {
$sql .= " date_fin_validite = " . ( dol_strlen ( $date_end ) != 0 ? " ' " . $this -> db -> idate ( $date_end ) . " ' " : " null " ) . " , " ;
}
2021-08-27 18:18:50 +02:00
$sql .= " fk_user_ouverture = " . (( int ) $user -> id ) . " , " ;
2016-11-19 19:38:06 +01:00
$sql .= " date_cloture = null, " ;
2019-11-12 09:46:08 +01:00
$sql .= " commentaire = ' " . $this -> db -> escape ( $comment ) . " ' " ;
2021-08-27 18:18:50 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id ) . " AND (statut = " . ContratLigne :: STATUS_INITIAL . " OR statut = " . ContratLigne :: STATUS_CLOSED . " ) " ;
2016-11-19 19:38:06 +01:00
2019-11-12 09:46:08 +01:00
dol_syslog ( get_class ( $this ) . " ::active_line " , LOG_DEBUG );
2016-11-19 19:38:06 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
// Call trigger
2018-02-16 01:19:31 +01:00
$result = $this -> call_trigger ( 'LINECONTRACT_ACTIVATE' , $user );
2021-02-23 21:24:38 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2018-02-26 20:12:29 +01:00
// End call triggers
2021-02-23 21:24:38 +01:00
if ( ! $error ) {
2018-03-30 15:46:08 +02:00
$this -> statut = ContratLigne :: STATUS_OPEN ;
2018-02-26 20:12:29 +01:00
$this -> date_ouverture = $date ;
$this -> date_fin_validite = $date_end ;
$this -> fk_user_ouverture = $user -> id ;
$this -> date_cloture = null ;
$this -> commentaire = $comment ;
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2016-11-19 19:38:06 +01:00
$this -> db -> rollback ();
return - 1 ;
}
} else {
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2016-11-19 19:38:06 +01:00
/**
* Close a contract line
*
2018-02-16 01:19:31 +01:00
* @ param User $user Objet User who close contract
* @ param int $date_end Date end
* @ param string $comment A comment typed by user
2020-10-31 14:32:18 +01:00
* @ param int $notrigger 1 = Does not execute triggers , 0 = Execute triggers
2018-02-16 01:19:31 +01:00
* @ return int < 0 if KO , > 0 if OK
2016-11-19 19:38:06 +01:00
*/
2019-02-25 20:35:59 +01:00
public function close_line ( $user , $date_end , $comment = '' , $notrigger = 0 )
2016-11-19 19:38:06 +01:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2016-11-19 19:38:06 +01:00
global $langs , $conf ;
// Update object
$this -> date_cloture = $date_end ;
$this -> fk_user_cloture = $user -> id ;
$this -> commentaire = $comment ;
$error = 0 ;
// statut actif : 4
$this -> db -> begin ();
2021-06-09 15:36:47 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " contratdet SET statut = " . (( int ) ContratLigne :: STATUS_CLOSED ) . " , " ;
2019-11-12 09:46:08 +01:00
$sql .= " date_cloture = ' " . $this -> db -> idate ( $date_end ) . " ', " ;
2021-08-27 18:18:50 +02:00
$sql .= " fk_user_cloture = " . (( int ) $user -> id ) . " , " ;
2019-11-12 09:46:08 +01:00
$sql .= " commentaire = ' " . $this -> db -> escape ( $comment ) . " ' " ;
2021-08-27 18:18:50 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id ) . " AND statut = " . (( int ) ContratLigne :: STATUS_OPEN );
2016-11-19 19:38:06 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 21:24:38 +01:00
if ( $resql ) {
if ( ! $notrigger ) {
2018-02-25 10:55:02 +01:00
// Call trigger
$result = $this -> call_trigger ( 'LINECONTRACT_CLOSE' , $user );
if ( $result < 0 ) {
$error ++ ;
$this -> db -> rollback ();
return - 1 ;
}
// End call triggers
2016-11-19 19:38:06 +01:00
}
$this -> db -> commit ();
return 1 ;
} else {
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return - 1 ;
}
}
2005-09-04 16:52:36 +02:00
}