| Total Complexity | 109 | 
| Total Lines | 897 | 
| Duplicated Lines | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like ContratLigne often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContratLigne, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 49 | class ContratLigne extends CommonObjectLine  | 
            ||
| 50 | { | 
            ||
| 51 | /**  | 
            ||
| 52 | * @var string ID to identify managed object  | 
            ||
| 53 | */  | 
            ||
| 54 | public $element = 'contratdet';  | 
            ||
| 55 | |||
| 56 | /**  | 
            ||
| 57 | * @var string Name of table without prefix where object is stored  | 
            ||
| 58 | */  | 
            ||
| 59 | public $table_element = 'contratdet';  | 
            ||
| 60 | |||
| 61 | /**  | 
            ||
| 62 | * @see CommonObjectLine  | 
            ||
| 63 | */  | 
            ||
| 64 | public $parent_element = 'contrat';  | 
            ||
| 65 | |||
| 66 | /**  | 
            ||
| 67 | * @see CommonObjectLine  | 
            ||
| 68 | */  | 
            ||
| 69 | public $fk_parent_attribute = 'fk_contrat';  | 
            ||
| 70 | |||
| 71 | /**  | 
            ||
| 72 | * @var string Name to use for 'features' parameter to check module permissions user->rights->feature with restrictedArea().  | 
            ||
| 73 | * Undefined means same value than $element. Can be use to force a check on another element for example for class of line, we mention here the parent element.  | 
            ||
| 74 | */  | 
            ||
| 75 | public $element_for_permission = 'contrat';  | 
            ||
| 76 | |||
| 77 | /**  | 
            ||
| 78 | * @var int ID  | 
            ||
| 79 | */  | 
            ||
| 80 | public $id;  | 
            ||
| 81 | |||
| 82 | /**  | 
            ||
| 83 | * @var string Ref  | 
            ||
| 84 | */  | 
            ||
| 85 | public $ref;  | 
            ||
| 86 | |||
| 87 | /**  | 
            ||
| 88 | * @var int ID  | 
            ||
| 89 | */  | 
            ||
| 90 | public $fk_contrat;  | 
            ||
| 91 | |||
| 92 | /**  | 
            ||
| 93 | * @var int ID  | 
            ||
| 94 | */  | 
            ||
| 95 | public $fk_product;  | 
            ||
| 96 | |||
| 97 | public $statut; // 0 inactive, 4 active, 5 closed  | 
            ||
| 98 | public $type; // 0 for product, 1 for service  | 
            ||
| 99 | |||
| 100 | /**  | 
            ||
| 101 | * @var string  | 
            ||
| 102 | * @deprecated  | 
            ||
| 103 | */  | 
            ||
| 104 | public $label;  | 
            ||
| 105 | |||
| 106 | /**  | 
            ||
| 107 | * @var string  | 
            ||
| 108 | * @deprecated  | 
            ||
| 109 | */  | 
            ||
| 110 | public $libelle;  | 
            ||
| 111 | |||
| 112 | /**  | 
            ||
| 113 | * @var string description  | 
            ||
| 114 | */  | 
            ||
| 115 | public $description;  | 
            ||
| 116 | |||
| 117 | public $product_type; // 0 for product, 1 for service  | 
            ||
| 118 | public $product_ref;  | 
            ||
| 119 | public $product_label;  | 
            ||
| 120 | |||
| 121 | public $date_commande;  | 
            ||
| 122 | |||
| 123 | public $date_start; // date start planned  | 
            ||
| 124 | public $date_start_real; // date start real  | 
            ||
| 125 | public $date_end; // date end planned  | 
            ||
| 126 | public $date_end_real; // date end real  | 
            ||
| 127 | |||
| 128 | public $tva_tx;  | 
            ||
| 129 | public $vat_src_code;  | 
            ||
| 130 | public $localtax1_tx;  | 
            ||
| 131 | public $localtax2_tx;  | 
            ||
| 132 | public $localtax1_type; // Local tax 1 type  | 
            ||
| 133 | public $localtax2_type; // Local tax 2 type  | 
            ||
| 134 | public $qty;  | 
            ||
| 135 | public $remise_percent;  | 
            ||
| 136 | public $remise;  | 
            ||
| 137 | |||
| 138 | /**  | 
            ||
| 139 | * @var int ID  | 
            ||
| 140 | */  | 
            ||
| 141 | public $fk_remise_except;  | 
            ||
| 142 | |||
| 143 | /**  | 
            ||
| 144 | * Unit price before taxes  | 
            ||
| 145 | * @var float  | 
            ||
| 146 | */  | 
            ||
| 147 | public $subprice;  | 
            ||
| 148 | |||
| 149 | /**  | 
            ||
| 150 | * @var float  | 
            ||
| 151 | * @deprecated Use $price_ht instead  | 
            ||
| 152 | * @see $price_ht  | 
            ||
| 153 | */  | 
            ||
| 154 | public $price;  | 
            ||
| 155 | |||
| 156 | /**  | 
            ||
| 157 | * @var float price without tax  | 
            ||
| 158 | */  | 
            ||
| 159 | public $price_ht;  | 
            ||
| 160 | |||
| 161 | public $total_ht;  | 
            ||
| 162 | public $total_tva;  | 
            ||
| 163 | public $total_localtax1;  | 
            ||
| 164 | public $total_localtax2;  | 
            ||
| 165 | public $total_ttc;  | 
            ||
| 166 | |||
| 167 | /**  | 
            ||
| 168 | * @var int ID  | 
            ||
| 169 | */  | 
            ||
| 170 | public $fk_fournprice;  | 
            ||
| 171 | |||
| 172 | public $pa_ht;  | 
            ||
| 173 | |||
| 174 | /**  | 
            ||
| 175 | * @var int Info bits  | 
            ||
| 176 | */  | 
            ||
| 177 | public $info_bits;  | 
            ||
| 178 | |||
| 179 | /**  | 
            ||
| 180 | * @var int ID of user that insert the service  | 
            ||
| 181 | */  | 
            ||
| 182 | public $fk_user_author;  | 
            ||
| 183 | |||
| 184 | /**  | 
            ||
| 185 | * @var int ID of user opening the service  | 
            ||
| 186 | */  | 
            ||
| 187 | public $fk_user_ouverture;  | 
            ||
| 188 | |||
| 189 | /**  | 
            ||
| 190 | * @var int ID of user closing the service  | 
            ||
| 191 | */  | 
            ||
| 192 | public $fk_user_cloture;  | 
            ||
| 193 | |||
| 194 | /**  | 
            ||
| 195 | * @var string Comment  | 
            ||
| 196 | */  | 
            ||
| 197 | public $commentaire;  | 
            ||
| 198 | |||
| 199 | |||
| 200 | /**  | 
            ||
| 201 | * @var int line rank  | 
            ||
| 202 | */  | 
            ||
| 203 | public $rang = 0;  | 
            ||
| 204 | |||
| 205 | |||
| 206 | const STATUS_INITIAL = 0;  | 
            ||
| 207 | const STATUS_OPEN = 4;  | 
            ||
| 208 | const STATUS_CLOSED = 5;  | 
            ||
| 209 | |||
| 210 | |||
| 211 | // BEGIN MODULEBUILDER PROPERTIES  | 
            ||
| 212 | /**  | 
            ||
| 213 |      * @var array<string,array{type:string,label:string,enabled:int<0,2>|string,position:int,notnull?:int,visible:int,noteditable?:int,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int,css?:string,csslist?:string,help?:string,showoncombobox?:int,disabled?:int,arrayofkeyval?:array<int,string>,comment?:string}>  Array with all fields and their property. Do not use it as a static var. It may be modified by constructor. | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 214 | */  | 
            ||
| 215 | public $fields = array(  | 
            ||
| 216 |         'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10), | 
            ||
| 217 |         'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 30, 'index' => 1), | 
            ||
| 218 |         'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 35), | 
            ||
| 219 |         'qty' => array('type' => 'integer', 'label' => 'Quantity', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 35, 'isameasure' => 1), | 
            ||
| 220 |         'total_ht' => array('type' => 'integer', 'label' => 'AmountHT', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 36, 'isameasure' => 1), | 
            ||
| 221 |         'total_tva' => array('type' => 'integer', 'label' => 'AmountVAT', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 37, 'isameasure' => 1), | 
            ||
| 222 |         'total_ttc' => array('type' => 'integer', 'label' => 'AmountTTC', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 38, 'isameasure' => 1), | 
            ||
| 223 |         //'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>40), | 
            ||
| 224 |         //'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>70), | 
            ||
| 225 |         'fk_contrat' => array('type' => 'integer:Contrat:contrat/class/contrat.class.php', 'label' => 'Contract', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 70), | 
            ||
| 226 |         'fk_product' => array('type' => 'integer:Product:product/class/product.class.php:1', 'label' => 'Product', 'enabled' => 1, 'visible' => -1, 'position' => 75), | 
            ||
| 227 |         //'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>90), | 
            ||
| 228 |         'note_private' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 105), | 
            ||
| 229 |         'note_public' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 110), | 
            ||
| 230 |         //'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>115), | 
            ||
| 231 |         //'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>120), | 
            ||
| 232 |         //'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>125), | 
            ||
| 233 |         'fk_user_ouverture' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserStartingService', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 135), | 
            ||
| 234 |         'fk_user_cloture' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserClosingService', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 135), | 
            ||
| 235 |         'statut' => array('type' => 'smallint(6)', 'label' => 'Statut', 'enabled' => 1, 'visible' => -1, 'position' => 500, 'arrayofkeyval' => array(0 => 'Draft', 4 => 'Open', 5 => 'Closed')), | 
            ||
| 236 |         'rang' => array('type' => 'integer', 'label' => 'Rank', 'enabled' => 1, 'visible' => 0, 'position' => 500, 'default' => '0') | 
            ||
| 237 | );  | 
            ||
| 238 | // END MODULEBUILDER PROPERTIES  | 
            ||
| 239 | |||
| 240 | |||
| 241 | /**  | 
            ||
| 242 | * Constructor  | 
            ||
| 243 | *  | 
            ||
| 244 | * @param DoliDB $db Database handler  | 
            ||
| 245 | */  | 
            ||
| 246 | public function __construct($db)  | 
            ||
| 247 |     { | 
            ||
| 248 | $this->db = $db;  | 
            ||
| 249 | }  | 
            ||
| 250 | |||
| 251 | |||
| 252 | /**  | 
            ||
| 253 | * Return label of this contract line status  | 
            ||
| 254 | *  | 
            ||
| 255 | * @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  | 
            ||
| 256 | * @return string Label of status  | 
            ||
| 257 | */  | 
            ||
| 258 | public function getLibStatut($mode)  | 
            ||
| 259 |     { | 
            ||
| 260 | return $this->LibStatut($this->statut, $mode, ((!empty($this->date_end)) ? ($this->date_end < dol_now() ? 1 : 0) : -1));  | 
            ||
| 261 | }  | 
            ||
| 262 | |||
| 263 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps  | 
            ||
| 264 | /**  | 
            ||
| 265 | * Return label of a contract line status  | 
            ||
| 266 | *  | 
            ||
| 267 | * @param int $status Id status  | 
            ||
| 268 | * @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  | 
            ||
| 269 | * @param int $expired 0=Not expired, 1=Expired, -1=Both or unknown  | 
            ||
| 270 | * @param string $moreatt More attribute  | 
            ||
| 271 | * @param string $morelabel More label  | 
            ||
| 272 | * @return string Label of status  | 
            ||
| 273 | */  | 
            ||
| 274 | public static function LibStatut($status, $mode, $expired = -1, $moreatt = '', $morelabel = '')  | 
            ||
| 275 |     { | 
            ||
| 276 | // phpcs:enable  | 
            ||
| 277 | global $langs;  | 
            ||
| 278 |         $langs->load("contracts"); | 
            ||
| 279 | |||
| 280 |         if ($status == self::STATUS_INITIAL) { | 
            ||
| 281 |             $labelStatus = $langs->transnoentities("ServiceStatusInitial"); | 
            ||
| 282 |             $labelStatusShort = $langs->transnoentities("ServiceStatusInitial"); | 
            ||
| 283 |         } elseif ($status == self::STATUS_OPEN && $expired == -1) { | 
            ||
| 284 |             $labelStatus = $langs->transnoentities("ServiceStatusRunning"); | 
            ||
| 285 |             $labelStatusShort = $langs->transnoentities("ServiceStatusRunning"); | 
            ||
| 286 |         } elseif ($status == self::STATUS_OPEN && $expired == 0) { | 
            ||
| 287 |             $labelStatus = $langs->transnoentities("ServiceStatusNotLate"); | 
            ||
| 288 |             $labelStatusShort = $langs->transnoentities("ServiceStatusNotLateShort"); | 
            ||
| 289 |         } elseif ($status == self::STATUS_OPEN && $expired == 1) { | 
            ||
| 290 |             $labelStatus = $langs->transnoentities("ServiceStatusLate"); | 
            ||
| 291 |             $labelStatusShort = $langs->transnoentities("ServiceStatusLateShort"); | 
            ||
| 292 |         } elseif ($status == self::STATUS_CLOSED) { | 
            ||
| 293 |             $labelStatus = $langs->transnoentities("ServiceStatusClosed"); | 
            ||
| 294 |             $labelStatusShort = $langs->transnoentities("ServiceStatusClosed"); | 
            ||
| 295 | }  | 
            ||
| 296 | |||
| 297 | $statusType = 'status' . $status;  | 
            ||
| 298 |         if ($status == self::STATUS_OPEN && $expired == 1) { | 
            ||
| 299 | $statusType = 'status1';  | 
            ||
| 300 | }  | 
            ||
| 301 |         if ($status == self::STATUS_CLOSED) { | 
            ||
| 302 | $statusType = 'status6';  | 
            ||
| 303 | }  | 
            ||
| 304 | |||
| 305 | $params = array();  | 
            ||
| 306 | $reg = array();  | 
            ||
| 307 |         if (preg_match('/class="(.*)"/', $moreatt, $reg)) { | 
            ||
| 308 |             $params = array('badgeParams' => array('css' => $reg[1])); | 
            ||
| 309 | }  | 
            ||
| 310 | return dolGetStatus($labelStatus . ($morelabel ? ' ' . $morelabel : ''), $labelStatusShort . ($morelabel ? ' ' . $morelabel : ''), '', $statusType, $mode, '', $params);  | 
            ||
| 311 | }  | 
            ||
| 312 | |||
| 313 | /**  | 
            ||
| 314 | * getTooltipContentArray  | 
            ||
| 315 | * @param array $params params to construct tooltip data  | 
            ||
| 316 | * @since v18  | 
            ||
| 317 | * @return array  | 
            ||
| 318 | */  | 
            ||
| 319 | public function getTooltipContentArray($params)  | 
            ||
| 320 |     { | 
            ||
| 321 | global $conf, $langs, $user;  | 
            ||
| 322 | |||
| 323 | $datas = [];  | 
            ||
| 324 |         $datas['label'] = $langs->trans("ShowContractOfService") . ': ' . $this->label; | 
            ||
| 325 |         if (empty($datas['label'])) { | 
            ||
| 326 | $datas['label'] = $this->description;  | 
            ||
| 327 | }  | 
            ||
| 328 | |||
| 329 | return $datas;  | 
            ||
| 330 | }  | 
            ||
| 331 | |||
| 332 | /**  | 
            ||
| 333 | * Return clicable name (with picto eventually) for ContratLigne  | 
            ||
| 334 | *  | 
            ||
| 335 | * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto  | 
            ||
| 336 | * @param int $maxlength Max length  | 
            ||
| 337 | * @return string Chaine avec URL  | 
            ||
| 338 | */  | 
            ||
| 339 | public function getNomUrl($withpicto = 0, $maxlength = 0)  | 
            ||
| 340 |     { | 
            ||
| 341 | global $langs;  | 
            ||
| 342 | |||
| 343 | $result = '';  | 
            ||
| 344 |         $label = $langs->trans("ShowContractOfService") . ': ' . $this->label; | 
            ||
| 345 |         if (empty($label)) { | 
            ||
| 346 | $label = $this->description;  | 
            ||
| 347 | }  | 
            ||
| 348 | $classfortooltip = 'classfortooltip';  | 
            ||
| 349 | $dataparams = '';  | 
            ||
| 350 |         if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) { | 
            ||
| 351 | $params = [  | 
            ||
| 352 | 'id' => $this->fk_contrat,  | 
            ||
| 353 | 'objecttype' => $this->element,  | 
            ||
| 354 | ];  | 
            ||
| 355 | $classfortooltip = 'classforajaxtooltip';  | 
            ||
| 356 | $dataparams = ' data-params="' . dol_escape_htmltag(json_encode($params)) . '"';  | 
            ||
| 357 | $label = '';  | 
            ||
| 358 | }  | 
            ||
| 359 | |||
| 360 |         $link = '<a href="' . constant('BASE_URL') . '/contrat/card.php?id=' . $this->fk_contrat . '"'; | 
            ||
| 361 | $link .= ($label ? ' title="' . dol_escape_htmltag($label, 1) . '"' : ' title="tocomplete"');  | 
            ||
| 362 | $link .= $dataparams . ' class="' . $classfortooltip . '">';  | 
            ||
| 363 | $linkend = '</a>';  | 
            ||
| 364 | |||
| 365 | $picto = 'service';  | 
            ||
| 366 |         if ($this->type == 0) { | 
            ||
| 367 | $picto = 'product';  | 
            ||
| 368 | }  | 
            ||
| 369 | |||
| 370 |         if ($withpicto) { | 
            ||
| 371 | $result .= ($link . img_object($label, $picto, $dataparams . ' class="' . $classfortooltip . '"') . $linkend);  | 
            ||
| 372 | }  | 
            ||
| 373 |         if ($withpicto && $withpicto != 2) { | 
            ||
| 374 | $result .= ' ';  | 
            ||
| 375 | }  | 
            ||
| 376 |         if ($withpicto != 2) { | 
            ||
| 377 | $result .= $link . ($this->product_ref ? $this->product_ref . ' ' : '') . ($this->label ? $this->label : $this->description) . $linkend;  | 
            ||
| 378 | }  | 
            ||
| 379 | return $result;  | 
            ||
| 380 | }  | 
            ||
| 381 | |||
| 382 | /**  | 
            ||
| 383 | * Load object in memory from database  | 
            ||
| 384 | *  | 
            ||
| 385 | * @param int $id Id object  | 
            ||
| 386 | * @param string $ref Ref of contract line  | 
            ||
| 387 | * @return int Return integer <0 if KO, >0 if OK  | 
            ||
| 388 | */  | 
            ||
| 389 | public function fetch($id, $ref = '')  | 
            ||
| 390 |     { | 
            ||
| 391 | // Check parameters  | 
            ||
| 392 |         if (empty($id) && empty($ref)) { | 
            ||
| 393 | return -1;  | 
            ||
| 394 | }  | 
            ||
| 395 | |||
| 396 | $sql = "SELECT";  | 
            ||
| 397 | $sql .= " t.rowid,";  | 
            ||
| 398 | $sql .= " t.tms,";  | 
            ||
| 399 | $sql .= " t.fk_contrat,";  | 
            ||
| 400 | $sql .= " t.fk_product,";  | 
            ||
| 401 | $sql .= " t.statut,";  | 
            ||
| 402 | $sql .= " t.label,"; // This field is not used. Only label of product  | 
            ||
| 403 | $sql .= " p.ref as product_ref,";  | 
            ||
| 404 | $sql .= " p.label as product_label,";  | 
            ||
| 405 | $sql .= " p.description as product_desc,";  | 
            ||
| 406 | $sql .= " p.fk_product_type as product_type,";  | 
            ||
| 407 | $sql .= " t.description,";  | 
            ||
| 408 | $sql .= " t.date_commande,";  | 
            ||
| 409 | $sql .= " t.date_ouverture_prevue as date_start,";  | 
            ||
| 410 | $sql .= " t.date_ouverture as date_start_real,";  | 
            ||
| 411 | $sql .= " t.date_fin_validite as date_end,";  | 
            ||
| 412 | $sql .= " t.date_cloture as date_end_real,";  | 
            ||
| 413 | $sql .= " t.tva_tx,";  | 
            ||
| 414 | $sql .= " t.vat_src_code,";  | 
            ||
| 415 | $sql .= " t.localtax1_tx,";  | 
            ||
| 416 | $sql .= " t.localtax2_tx,";  | 
            ||
| 417 | $sql .= " t.localtax1_type,";  | 
            ||
| 418 | $sql .= " t.localtax2_type,";  | 
            ||
| 419 | $sql .= " t.qty,";  | 
            ||
| 420 | $sql .= " t.remise_percent,";  | 
            ||
| 421 | $sql .= " t.remise,";  | 
            ||
| 422 | $sql .= " t.fk_remise_except,";  | 
            ||
| 423 | $sql .= " t.subprice,";  | 
            ||
| 424 | $sql .= " t.price_ht,";  | 
            ||
| 425 | $sql .= " t.total_ht,";  | 
            ||
| 426 | $sql .= " t.total_tva,";  | 
            ||
| 427 | $sql .= " t.total_localtax1,";  | 
            ||
| 428 | $sql .= " t.total_localtax2,";  | 
            ||
| 429 | $sql .= " t.total_ttc,";  | 
            ||
| 430 | $sql .= " t.fk_product_fournisseur_price as fk_fournprice,";  | 
            ||
| 431 | $sql .= " t.buy_price_ht as pa_ht,";  | 
            ||
| 432 | $sql .= " t.info_bits,";  | 
            ||
| 433 | $sql .= " t.fk_user_author,";  | 
            ||
| 434 | $sql .= " t.fk_user_ouverture,";  | 
            ||
| 435 | $sql .= " t.fk_user_cloture,";  | 
            ||
| 436 | $sql .= " t.commentaire,";  | 
            ||
| 437 | $sql .= " t.fk_unit,";  | 
            ||
| 438 | $sql .= " t.rang";  | 
            ||
| 439 | $sql .= " FROM " . MAIN_DB_PREFIX . "contratdet as t LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON p.rowid = t.fk_product";  | 
            ||
| 440 |         if ($id) { | 
            ||
| 441 | $sql .= " WHERE t.rowid = " . ((int) $id);  | 
            ||
| 442 | }  | 
            ||
| 443 |         if ($ref) { | 
            ||
| 444 | $sql .= " WHERE t.rowid = '" . $this->db->escape($ref) . "'";  | 
            ||
| 445 | }  | 
            ||
| 446 | |||
| 447 | dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);  | 
            ||
| 448 | $resql = $this->db->query($sql);  | 
            ||
| 449 |         if ($resql) { | 
            ||
| 450 |             if ($this->db->num_rows($resql)) { | 
            ||
| 451 | $obj = $this->db->fetch_object($resql);  | 
            ||
| 452 | |||
| 453 | $this->id = $obj->rowid;  | 
            ||
| 454 | $this->ref = $obj->rowid;  | 
            ||
| 455 | |||
| 456 | $this->tms = $this->db->jdate($obj->tms);  | 
            ||
| 457 | $this->fk_contrat = $obj->fk_contrat;  | 
            ||
| 458 | $this->fk_product = $obj->fk_product;  | 
            ||
| 459 | $this->statut = $obj->statut;  | 
            ||
| 460 | $this->product_ref = $obj->product_ref;  | 
            ||
| 461 | $this->product_label = $obj->product_label;  | 
            ||
| 462 | $this->product_type = $obj->product_type;  | 
            ||
| 463 | $this->label = $obj->label; // deprecated. We do not use this field. Only ref and label of product, and description of contract line  | 
            ||
| 464 | $this->description = $obj->description;  | 
            ||
| 465 | $this->date_commande = $this->db->jdate($obj->date_commande);  | 
            ||
| 466 | |||
| 467 | $this->date_start = $this->db->jdate($obj->date_start);  | 
            ||
| 468 | $this->date_start_real = $this->db->jdate($obj->date_start_real);  | 
            ||
| 469 | $this->date_end = $this->db->jdate($obj->date_end);  | 
            ||
| 470 | $this->date_end_real = $this->db->jdate($obj->date_end_real);  | 
            ||
| 471 | // For backward compatibility  | 
            ||
| 472 | //$this->date_ouverture_prevue = $this->db->jdate($obj->date_ouverture_prevue);  | 
            ||
| 473 | //$this->date_ouverture = $this->db->jdate($obj->date_ouverture);  | 
            ||
| 474 | //$this->date_fin_validite = $this->db->jdate($obj->date_fin_validite);  | 
            ||
| 475 | //$this->date_cloture = $this->db->jdate($obj->date_cloture);  | 
            ||
| 476 | |||
| 477 | $this->tva_tx = $obj->tva_tx;  | 
            ||
| 478 | $this->vat_src_code = $obj->vat_src_code;  | 
            ||
| 479 | $this->localtax1_tx = $obj->localtax1_tx;  | 
            ||
| 480 | $this->localtax2_tx = $obj->localtax2_tx;  | 
            ||
| 481 | $this->localtax1_type = $obj->localtax1_type;  | 
            ||
| 482 | $this->localtax2_type = $obj->localtax2_type;  | 
            ||
| 483 | $this->qty = $obj->qty;  | 
            ||
| 484 | $this->remise_percent = $obj->remise_percent;  | 
            ||
| 485 | $this->fk_remise_except = $obj->fk_remise_except;  | 
            ||
| 486 | $this->subprice = $obj->subprice;  | 
            ||
| 487 | $this->price_ht = $obj->price_ht;  | 
            ||
| 488 | $this->total_ht = $obj->total_ht;  | 
            ||
| 489 | $this->total_tva = $obj->total_tva;  | 
            ||
| 490 | $this->total_localtax1 = $obj->total_localtax1;  | 
            ||
| 491 | $this->total_localtax2 = $obj->total_localtax2;  | 
            ||
| 492 | $this->total_ttc = $obj->total_ttc;  | 
            ||
| 493 | $this->info_bits = $obj->info_bits;  | 
            ||
| 494 | $this->fk_user_author = $obj->fk_user_author;  | 
            ||
| 495 | $this->fk_user_ouverture = $obj->fk_user_ouverture;  | 
            ||
| 496 | $this->fk_user_cloture = $obj->fk_user_cloture;  | 
            ||
| 497 | $this->commentaire = $obj->commentaire;  | 
            ||
| 498 | $this->fk_fournprice = $obj->fk_fournprice;  | 
            ||
| 499 | |||
| 500 | $marginInfos = getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->fk_fournprice, $obj->pa_ht);  | 
            ||
| 501 | $this->pa_ht = $marginInfos[0];  | 
            ||
| 502 | $this->fk_unit = $obj->fk_unit;  | 
            ||
| 503 | |||
| 504 | $this->rang = $obj->rang;  | 
            ||
| 505 | |||
| 506 | $this->fetch_optionals();  | 
            ||
| 507 | }  | 
            ||
| 508 | |||
| 509 | $this->db->free($resql);  | 
            ||
| 510 | |||
| 511 | return 1;  | 
            ||
| 512 |         } else { | 
            ||
| 513 | $this->error = "Error " . $this->db->lasterror();  | 
            ||
| 514 | return -1;  | 
            ||
| 515 | }  | 
            ||
| 516 | }  | 
            ||
| 517 | |||
| 518 | |||
| 519 | /**  | 
            ||
| 520 | * Update database for contract line  | 
            ||
| 521 | *  | 
            ||
| 522 | * @param User $user User that modify  | 
            ||
| 523 | * @param int $notrigger 0=no, 1=yes (no update trigger)  | 
            ||
| 524 | * @return int Return integer <0 if KO, >0 if OK  | 
            ||
| 525 | */  | 
            ||
| 526 | public function update($user, $notrigger = 0)  | 
            ||
| 527 |     { | 
            ||
| 528 | global $mysoc;  | 
            ||
| 529 | |||
| 530 | $error = 0;  | 
            ||
| 531 | |||
| 532 | // Clean parameters  | 
            ||
| 533 | $this->fk_contrat = (int) $this->fk_contrat;  | 
            ||
| 534 | $this->fk_product = (int) $this->fk_product;  | 
            ||
| 535 | $this->statut = (int) $this->statut;  | 
            ||
| 536 | $this->label = trim($this->label);  | 
            ||
| 537 | $this->description = trim($this->description);  | 
            ||
| 538 | $this->vat_src_code = trim($this->vat_src_code);  | 
            ||
| 539 | $this->tva_tx = trim((string) $this->tva_tx);  | 
            ||
| 540 | $this->localtax1_tx = trim($this->localtax1_tx);  | 
            ||
| 541 | $this->localtax2_tx = trim($this->localtax2_tx);  | 
            ||
| 542 | $this->qty = (float) $this->qty;  | 
            ||
| 543 | $this->remise_percent = trim((string) $this->remise_percent);  | 
            ||
| 544 | $this->fk_remise_except = (int) $this->fk_remise_except;  | 
            ||
| 545 | $this->subprice = (float) price2num($this->subprice);  | 
            ||
| 546 | $this->price_ht = (float) price2num($this->price_ht);  | 
            ||
| 547 | $this->info_bits = (int) $this->info_bits;  | 
            ||
| 548 | $this->fk_user_author = (int) $this->fk_user_author;  | 
            ||
| 549 | $this->fk_user_ouverture = (int) $this->fk_user_ouverture;  | 
            ||
| 550 | $this->fk_user_cloture = (int) $this->fk_user_cloture;  | 
            ||
| 551 | $this->commentaire = trim($this->commentaire);  | 
            ||
| 552 | $this->rang = (int) $this->rang;  | 
            ||
| 553 | //if (empty($this->subprice)) $this->subprice = 0;  | 
            ||
| 554 |         if (empty($this->price_ht)) { | 
            ||
| 555 | $this->price_ht = 0;  | 
            ||
| 556 | }  | 
            ||
| 557 |         if (empty($this->total_ht)) { | 
            ||
| 558 | $this->total_ht = 0;  | 
            ||
| 559 | }  | 
            ||
| 560 |         if (empty($this->total_tva)) { | 
            ||
| 561 | $this->total_tva = 0;  | 
            ||
| 562 | }  | 
            ||
| 563 |         if (empty($this->total_ttc)) { | 
            ||
| 564 | $this->total_ttc = 0;  | 
            ||
| 565 | }  | 
            ||
| 566 |         if (empty($this->localtax1_tx)) { | 
            ||
| 567 | $this->localtax1_tx = 0;  | 
            ||
| 568 | }  | 
            ||
| 569 |         if (empty($this->localtax2_tx)) { | 
            ||
| 570 | $this->localtax2_tx = 0;  | 
            ||
| 571 | }  | 
            ||
| 572 |         if (empty($this->remise_percent)) { | 
            ||
| 573 | $this->remise_percent = 0;  | 
            ||
| 574 | }  | 
            ||
| 575 | |||
| 576 | // Calcul du total TTC et de la TVA pour la ligne a partir de  | 
            ||
| 577 | // qty, pu, remise_percent et txtva  | 
            ||
| 578 | // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker  | 
            ||
| 579 | // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.  | 
            ||
| 580 | $localtaxes_type = getLocalTaxesFromRate($this->tva_tx, 0, $this->thirdparty, $mysoc);  | 
            ||
| 581 | |||
| 582 | $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);  | 
            ||
| 583 | $this->total_ht = $tabprice[0];  | 
            ||
| 584 | $this->total_tva = $tabprice[1];  | 
            ||
| 585 | $this->total_ttc = $tabprice[2];  | 
            ||
| 586 | $this->total_localtax1 = $tabprice[9];  | 
            ||
| 587 | $this->total_localtax2 = $tabprice[10];  | 
            ||
| 588 | |||
| 589 |         if (empty($this->pa_ht)) { | 
            ||
| 590 | $this->pa_ht = 0;  | 
            ||
| 591 | }  | 
            ||
| 592 | |||
| 593 | // if buy price not defined, define buyprice as configured in margin admin  | 
            ||
| 594 |         if ($this->pa_ht == 0) { | 
            ||
| 595 | $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);  | 
            ||
| 596 |             if ($result < 0) { | 
            ||
| 597 | return -1;  | 
            ||
| 598 |             } else { | 
            ||
| 599 | $this->pa_ht = $result;  | 
            ||
| 600 | }  | 
            ||
| 601 | }  | 
            ||
| 602 | |||
| 603 | // $this->oldcopy should have been set by the caller of update (here properties were already modified)  | 
            ||
| 604 |         if (empty($this->oldcopy)) { | 
            ||
| 605 |             dol_syslog("this->oldcopy should have been set by the caller of update (here properties were already modified)", LOG_WARNING); | 
            ||
| 606 | $this->oldcopy = dol_clone($this, 2);  | 
            ||
| 607 | }  | 
            ||
| 608 | |||
| 609 | $this->db->begin();  | 
            ||
| 610 | |||
| 611 | // Update request  | 
            ||
| 612 | $sql = "UPDATE " . MAIN_DB_PREFIX . "contratdet SET";  | 
            ||
| 613 | $sql .= " fk_contrat = " . ((int) $this->fk_contrat) . ",";  | 
            ||
| 614 | $sql .= " fk_product = " . ($this->fk_product ? ((int) $this->fk_product) : 'null') . ",";  | 
            ||
| 615 | $sql .= " statut = " . ((int) $this->statut) . ",";  | 
            ||
| 616 | $sql .= " label = '" . $this->db->escape($this->label) . "',";  | 
            ||
| 617 | $sql .= " description = '" . $this->db->escape($this->description) . "',";  | 
            ||
| 618 | $sql .= " date_commande = " . ($this->date_commande != '' ? "'" . $this->db->idate($this->date_commande) . "'" : "null") . ",";  | 
            ||
| 619 | $sql .= " date_ouverture_prevue = " . ($this->date_start != '' ? "'" . $this->db->idate($this->date_start) . "'" : "null") . ",";  | 
            ||
| 620 | $sql .= " date_ouverture = " . ($this->date_start_real != '' ? "'" . $this->db->idate($this->date_start_real) . "'" : "null") . ",";  | 
            ||
| 621 | $sql .= " date_fin_validite = " . ($this->date_end != '' ? "'" . $this->db->idate($this->date_end) . "'" : "null") . ",";  | 
            ||
| 622 | $sql .= " date_cloture = " . ($this->date_end_real != '' ? "'" . $this->db->idate($this->date_end_real) . "'" : "null") . ",";  | 
            ||
| 623 | $sql .= " vat_src_code = '" . $this->db->escape($this->vat_src_code) . "',";  | 
            ||
| 624 | $sql .= " tva_tx = " . price2num($this->tva_tx) . ",";  | 
            ||
| 625 | $sql .= " localtax1_tx = " . price2num($this->localtax1_tx) . ",";  | 
            ||
| 626 | $sql .= " localtax2_tx = " . price2num($this->localtax2_tx) . ",";  | 
            ||
| 627 | $sql .= " qty = " . price2num($this->qty) . ",";  | 
            ||
| 628 | $sql .= " remise_percent = " . price2num($this->remise_percent) . ",";  | 
            ||
| 629 | $sql .= " remise = " . ($this->remise ? price2num($this->remise) : "null") . ",";  | 
            ||
| 630 | $sql .= " fk_remise_except = " . ($this->fk_remise_except > 0 ? $this->fk_remise_except : "null") . ",";  | 
            ||
| 631 | $sql .= " subprice = " . ($this->subprice != '' ? $this->subprice : "null") . ",";  | 
            ||
| 632 | $sql .= " price_ht = " . ($this->price_ht != '' ? $this->price_ht : "null") . ",";  | 
            ||
| 633 | $sql .= " total_ht = " . $this->total_ht . ",";  | 
            ||
| 634 | $sql .= " total_tva = " . $this->total_tva . ",";  | 
            ||
| 635 | $sql .= " total_localtax1 = " . $this->total_localtax1 . ",";  | 
            ||
| 636 | $sql .= " total_localtax2 = " . $this->total_localtax2 . ",";  | 
            ||
| 637 | $sql .= " total_ttc = " . $this->total_ttc . ",";  | 
            ||
| 638 | $sql .= " fk_product_fournisseur_price = " . (!empty($this->fk_fournprice) ? $this->fk_fournprice : "NULL") . ",";  | 
            ||
| 639 | $sql .= " buy_price_ht = '" . price2num($this->pa_ht) . "',";  | 
            ||
| 640 | $sql .= " info_bits = '" . $this->db->escape($this->info_bits) . "',";  | 
            ||
| 641 | $sql .= " fk_user_author = " . ($this->fk_user_author >= 0 ? $this->fk_user_author : "NULL") . ",";  | 
            ||
| 642 | $sql .= " fk_user_ouverture = " . ($this->fk_user_ouverture > 0 ? $this->fk_user_ouverture : "NULL") . ",";  | 
            ||
| 643 | $sql .= " fk_user_cloture = " . ($this->fk_user_cloture > 0 ? $this->fk_user_cloture : "NULL") . ",";  | 
            ||
| 644 | $sql .= " commentaire = '" . $this->db->escape($this->commentaire) . "',";  | 
            ||
| 645 | $sql .= " fk_unit = " . (!$this->fk_unit ? 'NULL' : $this->fk_unit) . ",";  | 
            ||
| 646 | $sql .= " rang = " . (empty($this->rang) ? '0' : (int) $this->rang);  | 
            ||
| 647 | $sql .= " WHERE rowid = " . ((int) $this->id);  | 
            ||
| 648 | |||
| 649 | dol_syslog(get_class($this) . "::update", LOG_DEBUG);  | 
            ||
| 650 | $resql = $this->db->query($sql);  | 
            ||
| 651 |         if (!$resql) { | 
            ||
| 652 | $this->error = "Error " . $this->db->lasterror();  | 
            ||
| 653 | $error++;  | 
            ||
| 654 | }  | 
            ||
| 655 | |||
| 656 |         if (!$error) { // For avoid conflicts if trigger used | 
            ||
| 657 | $result = $this->insertExtraFields();  | 
            ||
| 658 |             if ($result < 0) { | 
            ||
| 659 | $error++;  | 
            ||
| 660 | }  | 
            ||
| 661 | }  | 
            ||
| 662 | |||
| 663 | // If we change a planned date (start or end) of one contract line, sync dates for all other services too  | 
            ||
| 664 |         if (!$error && getDolGlobalString('CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES')) { | 
            ||
| 665 | dol_syslog(get_class($this) . "::update CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES is on so we update date for all lines", LOG_DEBUG);  | 
            ||
| 666 | |||
| 667 |             if ($this->date_start != $this->oldcopy->date_start) { | 
            ||
| 668 | $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'contratdet SET';  | 
            ||
| 669 | $sql .= " date_ouverture_prevue = " . ($this->date_start != '' ? "'" . $this->db->idate($this->date_start) . "'" : "null");  | 
            ||
| 670 | $sql .= " WHERE fk_contrat = " . ((int) $this->fk_contrat);  | 
            ||
| 671 | |||
| 672 | $resql = $this->db->query($sql);  | 
            ||
| 673 |                 if (!$resql) { | 
            ||
| 674 | $error++;  | 
            ||
| 675 | $this->error = "Error " . $this->db->lasterror();  | 
            ||
| 676 | }  | 
            ||
| 677 | }  | 
            ||
| 678 |             if ($this->date_end != $this->oldcopy->date_end) { | 
            ||
| 679 | $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'contratdet SET';  | 
            ||
| 680 | $sql .= " date_fin_validite = " . ($this->date_end != '' ? "'" . $this->db->idate($this->date_end) . "'" : "null");  | 
            ||
| 681 | $sql .= " WHERE fk_contrat = " . ((int) $this->fk_contrat);  | 
            ||
| 682 | |||
| 683 | $resql = $this->db->query($sql);  | 
            ||
| 684 |                 if (!$resql) { | 
            ||
| 685 | $error++;  | 
            ||
| 686 | $this->error = "Error " . $this->db->lasterror();  | 
            ||
| 687 | }  | 
            ||
| 688 | }  | 
            ||
| 689 | }  | 
            ||
| 690 | |||
| 691 |         if (!$error && !$notrigger) { | 
            ||
| 692 | // Call trigger  | 
            ||
| 693 |             $result = $this->call_trigger('LINECONTRACT_MODIFY', $user); | 
            ||
| 694 |             if ($result < 0) { | 
            ||
| 695 | $error++;  | 
            ||
| 696 | $this->db->rollback();  | 
            ||
| 697 | }  | 
            ||
| 698 | // End call triggers  | 
            ||
| 699 | }  | 
            ||
| 700 | |||
| 701 |         if (!$error) { | 
            ||
| 702 | $this->db->commit();  | 
            ||
| 703 | return 1;  | 
            ||
| 704 |         } else { | 
            ||
| 705 | $this->db->rollback();  | 
            ||
| 706 | $this->errors[] = $this->error;  | 
            ||
| 707 | return -1;  | 
            ||
| 708 | }  | 
            ||
| 709 | }  | 
            ||
| 710 | |||
| 711 | |||
| 712 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps  | 
            ||
| 713 | /**  | 
            ||
| 714 | * Update in database the fields total_xxx of lines  | 
            ||
| 715 | * Used by migration process  | 
            ||
| 716 | *  | 
            ||
| 717 | * @return int Return integer <0 if KO, >0 if OK  | 
            ||
| 718 | */  | 
            ||
| 719 | public function update_total()  | 
            ||
| 743 | }  | 
            ||
| 744 | }  | 
            ||
| 745 | |||
| 746 | |||
| 747 | /**  | 
            ||
| 748 | * Inserts a contrat line into database  | 
            ||
| 749 | *  | 
            ||
| 750 | * @param int $notrigger Set to 1 if you don't want triggers to be fired  | 
            ||
| 751 | * @return int Return integer <0 if KO, >0 if OK  | 
            ||
| 752 | */  | 
            ||
| 753 | public function insert($notrigger = 0)  | 
            ||
| 754 |     { | 
            ||
| 755 | global $user;  | 
            ||
| 756 | |||
| 757 | $error = 0;  | 
            ||
| 758 | |||
| 759 | // Insertion dans la base  | 
            ||
| 760 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "contratdet";  | 
            ||
| 761 | $sql .= " (fk_contrat, label, description, fk_product, qty, vat_src_code, tva_tx,";  | 
            ||
| 762 | $sql .= " localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";  | 
            ||
| 763 | $sql .= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";  | 
            ||
| 764 | $sql .= " info_bits,";  | 
            ||
| 765 | $sql .= " rang,";  | 
            ||
| 766 | $sql .= " price_ht, remise, fk_product_fournisseur_price, buy_price_ht";  | 
            ||
| 767 |         if ($this->date_start > 0) { | 
            ||
| 768 | $sql .= ",date_ouverture_prevue";  | 
            ||
| 769 | }  | 
            ||
| 770 |         if ($this->date_end > 0) { | 
            ||
| 771 | $sql .= ",date_fin_validite";  | 
            ||
| 772 | }  | 
            ||
| 773 | $sql .= ") VALUES ($this->fk_contrat, '', '" . $this->db->escape($this->description) . "',";  | 
            ||
| 774 | $sql .= ($this->fk_product > 0 ? $this->fk_product : "null") . ",";  | 
            ||
| 775 | $sql .= " '" . $this->db->escape($this->qty) . "',";  | 
            ||
| 776 | $sql .= " '" . $this->db->escape($this->vat_src_code) . "',";  | 
            ||
| 777 | $sql .= " '" . $this->db->escape($this->tva_tx) . "',";  | 
            ||
| 778 | $sql .= " '" . $this->db->escape($this->localtax1_tx) . "',";  | 
            ||
| 779 | $sql .= " '" . $this->db->escape($this->localtax2_tx) . "',";  | 
            ||
| 780 | $sql .= " '" . $this->db->escape($this->localtax1_type) . "',";  | 
            ||
| 781 | $sql .= " '" . $this->db->escape($this->localtax2_type) . "',";  | 
            ||
| 782 | $sql .= " " . price2num($this->remise_percent) . "," . price2num($this->subprice) . ",";  | 
            ||
| 783 | $sql .= " " . price2num($this->total_ht) . "," . price2num($this->total_tva) . "," . price2num($this->total_localtax1) . "," . price2num($this->total_localtax2) . "," . price2num($this->total_ttc) . ",";  | 
            ||
| 784 | $sql .= " '" . $this->db->escape($this->info_bits) . "',";  | 
            ||
| 785 | $sql .= " " . (empty($this->rang) ? '0' : (int) $this->rang) . ",";  | 
            ||
| 786 | $sql .= " " . price2num($this->price_ht) . "," . price2num($this->remise) . ",";  | 
            ||
| 787 |         if ($this->fk_fournprice > 0) { | 
            ||
| 788 | $sql .= ' ' . ((int) $this->fk_fournprice) . ',';  | 
            ||
| 789 |         } else { | 
            ||
| 790 | $sql .= ' null,';  | 
            ||
| 791 | }  | 
            ||
| 792 |         if ($this->pa_ht > 0) { | 
            ||
| 793 | $sql .= ' ' . ((float) price2num($this->pa_ht));  | 
            ||
| 794 |         } else { | 
            ||
| 795 | $sql .= ' null';  | 
            ||
| 796 | }  | 
            ||
| 797 |         if ($this->date_start > 0) { | 
            ||
| 798 | $sql .= ",'" . $this->db->idate($this->date_start) . "'";  | 
            ||
| 799 | }  | 
            ||
| 800 |         if ($this->date_end > 0) { | 
            ||
| 801 | $sql .= ",'" . $this->db->idate($this->date_end) . "'";  | 
            ||
| 802 | }  | 
            ||
| 803 | $sql .= ")";  | 
            ||
| 804 | |||
| 805 | dol_syslog(get_class($this) . "::insert", LOG_DEBUG);  | 
            ||
| 806 | |||
| 807 | $resql = $this->db->query($sql);  | 
            ||
| 808 |         if ($resql) { | 
            ||
| 809 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'contratdet');  | 
            ||
| 810 | |||
| 811 | // Insert of extrafields  | 
            ||
| 812 |             if (!$error) { | 
            ||
| 813 | $result = $this->insertExtraFields();  | 
            ||
| 814 |                 if ($result < 0) { | 
            ||
| 815 | $this->db->rollback();  | 
            ||
| 816 | return -1;  | 
            ||
| 817 | }  | 
            ||
| 818 | }  | 
            ||
| 819 | |||
| 820 |             if (!$notrigger) { | 
            ||
| 821 | // Call trigger  | 
            ||
| 822 |                 $result = $this->call_trigger('LINECONTRACT_INSERT', $user); | 
            ||
| 823 |                 if ($result < 0) { | 
            ||
| 824 | $this->db->rollback();  | 
            ||
| 825 | return -1;  | 
            ||
| 826 | }  | 
            ||
| 827 | // End call triggers  | 
            ||
| 828 | }  | 
            ||
| 829 | |||
| 830 | $this->db->commit();  | 
            ||
| 831 | return 1;  | 
            ||
| 832 |         } else { | 
            ||
| 833 | $this->db->rollback();  | 
            ||
| 834 | $this->error = $this->db->error() . " sql=" . $sql;  | 
            ||
| 835 | return -1;  | 
            ||
| 836 | }  | 
            ||
| 837 | }  | 
            ||
| 838 | |||
| 839 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps  | 
            ||
| 840 | /**  | 
            ||
| 841 | * Activate a contract line  | 
            ||
| 842 | *  | 
            ||
| 843 | * @param User $user Object User who activate contract  | 
            ||
| 844 | * @param int $date Date real activation  | 
            ||
| 845 | * @param int|string $date_end Date planned end. Use '-1' to keep it unchanged.  | 
            ||
| 846 | * @param string $comment A comment typed by user  | 
            ||
| 847 | * @return int Return integer <0 if KO, >0 if OK  | 
            ||
| 848 | */  | 
            ||
| 849 | public function active_line($user, $date, $date_end = '', $comment = '')  | 
            ||
| 850 |     { | 
            ||
| 851 | // phpcs:enable  | 
            ||
| 852 | $error = 0;  | 
            ||
| 853 | |||
| 854 | $this->db->begin();  | 
            ||
| 855 | |||
| 856 | $this->statut = ContratLigne::STATUS_OPEN;  | 
            ||
| 857 | $this->date_start_real = $date;  | 
            ||
| 858 | $this->date_end = $date_end;  | 
            ||
| 859 | $this->fk_user_ouverture = $user->id;  | 
            ||
| 860 | $this->date_end_real = null;  | 
            ||
| 861 | $this->commentaire = $comment;  | 
            ||
| 862 | |||
| 863 | $sql = "UPDATE " . MAIN_DB_PREFIX . "contratdet SET statut = " . ((int) $this->statut) . ",";  | 
            ||
| 864 | $sql .= " date_ouverture = " . (dol_strlen($this->date_start_real) != 0 ? "'" . $this->db->idate($this->date_start_real) . "'" : "null") . ",";  | 
            ||
| 865 |         if ($date_end >= 0) { | 
            ||
| 866 | $sql .= " date_fin_validite = " . (dol_strlen($this->date_end) != 0 ? "'" . $this->db->idate($this->date_end) . "'" : "null") . ",";  | 
            ||
| 867 | }  | 
            ||
| 868 | $sql .= " fk_user_ouverture = " . ((int) $this->fk_user_ouverture) . ",";  | 
            ||
| 869 | $sql .= " date_cloture = null,";  | 
            ||
| 870 | $sql .= " commentaire = '" . $this->db->escape($comment) . "'";  | 
            ||
| 871 | $sql .= " WHERE rowid = " . ((int) $this->id) . " AND (statut = " . ContratLigne::STATUS_INITIAL . " OR statut = " . ContratLigne::STATUS_CLOSED . ")";  | 
            ||
| 872 | |||
| 873 | dol_syslog(get_class($this) . "::active_line", LOG_DEBUG);  | 
            ||
| 874 | $resql = $this->db->query($sql);  | 
            ||
| 875 |         if ($resql) { | 
            ||
| 876 | // Call trigger  | 
            ||
| 877 |             $result = $this->call_trigger('LINECONTRACT_ACTIVATE', $user); | 
            ||
| 878 |             if ($result < 0) { | 
            ||
| 879 | $error++;  | 
            ||
| 880 | }  | 
            ||
| 881 | // End call triggers  | 
            ||
| 882 | |||
| 883 |             if (!$error) { | 
            ||
| 884 | $this->db->commit();  | 
            ||
| 885 | return 1;  | 
            ||
| 886 |             } else { | 
            ||
| 887 | $this->db->rollback();  | 
            ||
| 888 | return -1;  | 
            ||
| 889 | }  | 
            ||
| 890 |         } else { | 
            ||
| 891 | $this->error = $this->db->lasterror();  | 
            ||
| 892 | $this->db->rollback();  | 
            ||
| 893 | return -1;  | 
            ||
| 894 | }  | 
            ||
| 895 | }  | 
            ||
| 896 | |||
| 897 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps  | 
            ||
| 898 | /**  | 
            ||
| 899 | * Close a contract line  | 
            ||
| 900 | *  | 
            ||
| 901 | * @param User $user Object User who close contract  | 
            ||
| 902 | * @param int $date_end_real Date end  | 
            ||
| 903 | * @param string $comment A comment typed by user  | 
            ||
| 904 | * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers  | 
            ||
| 905 | * @return int Return integer <0 if KO, >0 if OK  | 
            ||
| 906 | */  | 
            ||
| 907 | public function close_line($user, $date_end_real, $comment = '', $notrigger = 0)  | 
            ||
| 946 | }  | 
            ||
| 947 | }  | 
            ||
| 948 | }  | 
            ||
| 949 |