dolibarr 25.0.0-alpha
contratligne.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2012 Destailleur Laurent <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2014 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
6 * Copyright (C) 2008 Raphael Bertrand <raphael.bertrand@resultic.fr>
7 * Copyright (C) 2010-2016 Juanjo Menent <jmenent@2byte.es>
8 * Copyright (C) 2013 Christophe Battarel <christophe.battarel@altairis.fr>
9 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
10 * Copyright (C) 2014-2015 Marcos García <marcosgdf@gmail.com>
11 * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
12 * Copyright (C) 2018-2025 Frédéric France <frederic.france@free.fr>
13 * Copyright (C) 2015-2018 Ferran Marcet <fmarcet@2byte.es>
14 * Copyright (C) 2024 William Mead <william.mead@manchenumerique.fr>
15 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
16 * Copyright (C) 2026 Lionel Vessiller <lvessiller@open-dsi.fr>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <https://www.gnu.org/licenses/>.
30 */
31
38require_once DOL_DOCUMENT_ROOT."/core/class/commonobjectline.class.php";
39require_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
40require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php';
41
46{
50 public $element = 'contratdet';
51
55 public $table_element = 'contratdet';
56
60 public $parent_element = 'contrat';
61
65 public $fk_parent_attribute = 'fk_contrat';
66
71 public $element_for_permission = 'contrat';
72
76 public $id;
77
81 public $ref;
82
86 public $fk_contrat;
87
91 public $fk_product;
92
96 public $fk_parent_line;
97
101 public $statut;
102
106 public $type;
107
112 public $label;
113
118 public $libelle;
119
123 public $description;
124
128 public $product_type;
129
133 public $product_ref;
134
138 public $product_label;
139
143 public $date_commande;
144
148 public $date_start;
149
153 public $date_start_real;
154
158 public $date_end;
159
163 public $date_end_real;
164
168 public $tva_tx;
169
173 public $vat_src_code;
174
178 public $localtax1_tx;
179
183 public $localtax2_tx;
184
188 public $localtax1_type;
189
193 public $localtax2_type;
194
198 public $qty;
199
203 public $remise_percent;
204
208 public $fk_remise_except;
209
213 public $subprice;
214
218 public $total_ht;
219
223 public $total_tva;
224
228 public $total_localtax1;
229
233 public $total_localtax2;
234
238 public $total_ttc;
239
243 public $fk_fournprice;
244
248 public $pa_ht;
249
253 public $info_bits;
254
258 public $fk_user_author;
259
263 public $fk_user_ouverture;
264
268 public $fk_user_cloture;
269
273 public $commentaire;
274
275
279 public $rang = 0;
280
281
282 const STATUS_INITIAL = 0;
283 const STATUS_OPEN = 4;
284 const STATUS_CLOSED = 5;
285
286
287 // BEGIN MODULEBUILDER PROPERTIES
291 public $fields = array(
292 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
293 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 30, 'index' => 1),
294 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 35),
295 'qty' => array('type' => 'integer', 'label' => 'Quantity', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 35, 'isameasure' => 1),
296 'total_ht' => array('type' => 'integer', 'label' => 'AmountHT', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 36, 'isameasure' => 1),
297 'total_tva' => array('type' => 'integer', 'label' => 'AmountVAT', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 37, 'isameasure' => 1),
298 'total_ttc' => array('type' => 'integer', 'label' => 'AmountTTC', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 38, 'isameasure' => 1),
299 //'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
300 //'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>70),
301 'fk_contrat' => array('type' => 'integer:Contrat:contrat/class/contrat.class.php', 'label' => 'Contract', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 70),
302 'fk_product' => array('type' => 'integer:Product:product/class/product.class.php:1', 'label' => 'Product', 'enabled' => 1, 'visible' => -1, 'position' => 75),
303 //'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>90),
304 'note_private' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 105),
305 'note_public' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 110),
306 //'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>115),
307 //'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>120),
308 //'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>125),
309 'fk_user_ouverture' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserStartingService', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 135),
310 'fk_user_cloture' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserClosingService', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 135),
311 'statut' => array('type' => 'smallint(6)', 'label' => 'Statut', 'enabled' => 1, 'visible' => -1, 'position' => 500, 'arrayofkeyval' => array(0 => 'Draft', 4 => 'Open', 5 => 'Closed')),
312 'rang' => array('type' => 'integer', 'label' => 'Rank', 'enabled' => 1, 'visible' => 0, 'position' => 500, 'default' => '0'),
313 'fk_multicurrency' => array('type' => 'integer:MultiCurrency:multicurrency/class/multicurrency.class.php', 'label' => 'Currency', 'enabled' => 1, 'visible' => -1, 'position' => 190),
314 'multicurrency_code' => array('type' => 'varchar(3)', 'label' => 'CurrencyCode', 'enabled' => 1, 'visible' => -1, 'position' => 195),
315 'multicurrency_subprice' => array('type' => 'double(24,8)', 'label' => 'CurrencyUnitPrice', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 200),
316 'multicurrency_subprice_ttc' => array('type' => 'double(24,8)', 'label' => 'CurrencyUnitPriceTTC', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 205),
317 'multicurrency_total_ht' => array('type' => 'double(24,8)', 'label' => 'CurrencyTotalHT', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 210),
318 'multicurrency_total_tva' => array('type' => 'double(24,8)', 'label' => 'CurrencyTotalVAT', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 215),
319 'multicurrency_total_ttc' => array('type' => 'double(24,8)', 'label' => 'CurrencyTotalTTC', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 220)
320 );
321 // END MODULEBUILDER PROPERTIES
322
323
329 public function __construct($db)
330 {
331 $this->db = $db;
332 }
333
334
341 public function getLibStatut($mode)
342 {
343 return $this->LibStatut($this->statut, $mode, ((!empty($this->date_end)) ? ($this->date_end < dol_now() ? 1 : 0) : -1));
344 }
345
346 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
357 public static function LibStatut($status, $mode, $expired = -1, $moreatt = '', $morelabel = '')
358 {
359 // phpcs:enable
360 global $langs;
361 $langs->load("contracts");
362
363 if ($status == self::STATUS_INITIAL) {
364 $labelStatus = $langs->transnoentities("ServiceStatusInitial");
365 $labelStatusShort = $langs->transnoentities("ServiceStatusInitial");
366 } elseif ($status == self::STATUS_OPEN && $expired == -1) {
367 $labelStatus = $langs->transnoentities("ServiceStatusRunning");
368 $labelStatusShort = $langs->transnoentities("ServiceStatusRunning");
369 } elseif ($status == self::STATUS_OPEN && $expired == 0) {
370 $labelStatus = $langs->transnoentities("ServiceStatusNotLate");
371 $labelStatusShort = $langs->transnoentities("ServiceStatusNotLateShort");
372 } elseif ($status == self::STATUS_OPEN && $expired == 1) {
373 $labelStatus = $langs->transnoentities("ServiceStatusLate");
374 $labelStatusShort = $langs->transnoentities("ServiceStatusLateShort");
375 } elseif ($status == self::STATUS_CLOSED) {
376 $labelStatus = $langs->transnoentities("ServiceStatusClosed");
377 $labelStatusShort = $langs->transnoentities("ServiceStatusClosed");
378 } else {
379 $labelStatus = '';
380 $labelStatusShort = '';
381 }
382
383 $statusType = 'status'.$status;
384 if ($status == self::STATUS_OPEN && $expired == 1) {
385 $statusType = 'status1';
386 }
387 if ($status == self::STATUS_CLOSED) {
388 $statusType = 'status6';
389 }
390
391 $params = array();
392 $reg = array();
393 if (preg_match('/class="(.*)"/', $moreatt, $reg)) {
394 $params = array('badgeParams' => array('css' => $reg[1]));
395 }
396 return dolGetStatus($labelStatus.($morelabel ? ' '.$morelabel : ''), $labelStatusShort.($morelabel ? ' '.$morelabel : ''), '', $statusType, $mode, '', $params);
397 }
398
405 public function getTooltipContentArray($params)
406 {
407 global $conf, $langs, $user;
408
409 $datas = [];
410 $datas['label'] = $langs->trans("ShowContractOfService").': '.$this->label;
411 if (empty($this->label)) {
412 $datas['label'] = $this->description;
413 }
414
415 return $datas;
416 }
417
425 public function getNomUrl($withpicto = 0, $maxlength = 0)
426 {
427 global $langs;
428
429 $result = '';
430 $label = $langs->trans("ShowContractOfService").': '.$this->label;
431 if (empty($this->label)) {
432 $label = $this->description;
433 }
434 $classfortooltip = 'classfortooltip';
435 $dataparams = '';
436 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
437 $params = [
438 'id' => $this->fk_contrat,
439 'objecttype' => $this->element,
440 ];
441 $classfortooltip = 'classforajaxtooltip';
442 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
443 $label = '';
444 }
445
446 $link = '<a href="'.DOL_URL_ROOT.'/contrat/card.php?id='.$this->fk_contrat.'"';
447 $link .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
448 $link .= $dataparams.' class="'.$classfortooltip.'">';
449 $linkend = '</a>';
450
451 $picto = 'service';
452 if ($this->type == 0) {
453 $picto = 'product';
454 }
455
456 if ($withpicto) {
457 $result .= ($link.img_object($label, $picto, $dataparams.' class="'.$classfortooltip.'"').$linkend);
458 }
459 if ($withpicto && $withpicto != 2) {
460 $result .= ' ';
461 }
462 if ($withpicto != 2) {
463 $result .= $link.($this->product_ref ? $this->product_ref.' ' : '').($this->label ? $this->label : $this->description).$linkend;
464 }
465
466 global $action, $hookmanager;
467 $hookmanager->initHooks(array($this->element . 'dao'));
468 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
469 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
470 if ($reshook > 0) {
471 $result = $hookmanager->resPrint;
472 } else {
473 $result .= $hookmanager->resPrint;
474 }
475
476 return $result;
477 }
478
486 public function fetch($id, $ref = '')
487 {
488 // Check parameters
489 if (empty($id) && empty($ref)) {
490 return -1;
491 }
492
493 $sql = "SELECT";
494 $sql .= " t.rowid,";
495 $sql .= " t.tms,";
496 $sql .= " t.fk_contrat,";
497 $sql .= " t.fk_product,";
498 $sql .= " t.statut,";
499 $sql .= " t.label,"; // This field is not used. Only label of product
500 $sql .= " p.ref as product_ref,";
501 $sql .= " p.label as product_label,";
502 $sql .= " p.description as product_description,";
503 $sql .= " p.fk_product_type as product_type,";
504 $sql .= " t.description,";
505 $sql .= " t.date_commande,";
506 $sql .= " t.date_ouverture_prevue as date_start,";
507 $sql .= " t.date_ouverture as date_start_real,";
508 $sql .= " t.date_fin_validite as date_end,";
509 $sql .= " t.date_cloture as date_end_real,";
510 $sql .= " t.tva_tx,";
511 $sql .= " t.vat_src_code,";
512 $sql .= " t.localtax1_tx,";
513 $sql .= " t.localtax2_tx,";
514 $sql .= " t.localtax1_type,";
515 $sql .= " t.localtax2_type,";
516 $sql .= " t.qty,";
517 $sql .= " t.remise_percent,";
518 $sql .= " t.fk_remise_except,";
519 $sql .= " t.subprice,";
520 $sql .= " t.subprice_ttc,";
521 $sql .= " t.total_ht,";
522 $sql .= " t.total_tva,";
523 $sql .= " t.total_localtax1,";
524 $sql .= " t.total_localtax2,";
525 $sql .= " t.total_ttc,";
526 $sql .= " t.fk_product_fournisseur_price as fk_fournprice,";
527 $sql .= " t.buy_price_ht as pa_ht,";
528 $sql .= " t.info_bits,";
529 $sql .= " t.fk_user_author,";
530 $sql .= " t.fk_user_ouverture,";
531 $sql .= " t.fk_user_cloture,";
532 $sql .= " t.commentaire,";
533 $sql .= " t.fk_unit,";
534 $sql .= " t.extraparams,";
535 $sql .= " t.rang,";
536 $sql .= " t.fk_multicurrency, t.multicurrency_code, t.multicurrency_subprice, t.multicurrency_subprice_ttc, t.multicurrency_total_ht, t.multicurrency_total_tva, t.multicurrency_total_ttc";
537 $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as t LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = t.fk_product";
538 if ($id) {
539 $sql .= " WHERE t.rowid = ".((int) $id);
540 }
541 if ($ref) {
542 $sql .= " WHERE t.rowid = '".$this->db->escape($ref)."'";
543 }
544
545 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
546 $resql = $this->db->query($sql);
547 if ($resql) {
548 if ($this->db->num_rows($resql)) {
549 $obj = $this->db->fetch_object($resql);
550
551 $this->id = $obj->rowid;
552 $this->ref = $obj->rowid;
553
554 $this->tms = $this->db->jdate($obj->tms);
555 $this->fk_contrat = (int) $obj->fk_contrat;
556 $this->fk_product = (int) $obj->fk_product;
557 $this->statut = $obj->statut;
558 $this->product_ref = $obj->product_ref;
559 $this->product_label = $obj->product_label;
560 $this->product_type = $obj->product_type;
561 $this->label = $obj->label; // deprecated. We do not use this field. Only ref and label of product, and description of contract line
562 $this->description = $obj->description;
563 $this->date_commande = $this->db->jdate($obj->date_commande);
564
565 $this->date_start = $this->db->jdate($obj->date_start);
566 $this->date_start_real = $this->db->jdate($obj->date_start_real);
567 $this->date_end = $this->db->jdate($obj->date_end);
568 $this->date_end_real = $this->db->jdate($obj->date_end_real);
569
570 $this->tva_tx = $obj->tva_tx;
571 $this->vat_src_code = $obj->vat_src_code;
572 $this->localtax1_tx = $obj->localtax1_tx;
573 $this->localtax2_tx = $obj->localtax2_tx;
574 $this->localtax1_type = $obj->localtax1_type;
575 $this->localtax2_type = $obj->localtax2_type;
576 $this->qty = $obj->qty;
577 $this->remise_percent = $obj->remise_percent;
578 $this->fk_remise_except = $obj->fk_remise_except;
579 $this->subprice = $obj->subprice;
580 $this->subprice_ttc = $obj->subprice_ttc;
581 $this->total_ht = $obj->total_ht;
582 $this->total_tva = $obj->total_tva;
583 $this->total_localtax1 = $obj->total_localtax1;
584 $this->total_localtax2 = $obj->total_localtax2;
585 $this->total_ttc = $obj->total_ttc;
586 $this->info_bits = $obj->info_bits;
587 $this->fk_user_author = $obj->fk_user_author;
588 $this->fk_user_ouverture = $obj->fk_user_ouverture;
589 $this->fk_user_cloture = $obj->fk_user_cloture;
590 $this->commentaire = $obj->commentaire;
591 $this->fk_fournprice = $obj->fk_fournprice;
592
593 $marginInfos = getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->fk_fournprice, $obj->pa_ht);
594 $this->pa_ht = $marginInfos[0];
595 $this->fk_unit = $obj->fk_unit;
596
597 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams, true) : array();
598
599 $this->rang = $obj->rang;
600
601 $this->fk_multicurrency = $obj->fk_multicurrency;
602 $this->multicurrency_code = $obj->multicurrency_code;
603 $this->multicurrency_subprice = $obj->multicurrency_subprice;
604 $this->multicurrency_subprice_ttc = $obj->multicurrency_subprice_ttc;
605 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
606 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
607 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
608
609 $this->fetch_optionals();
610 }
611
612 $this->db->free($resql);
613
614 return 1;
615 } else {
616 $this->error = "Error ".$this->db->lasterror();
617 return -1;
618 }
619 }
620
621
629 public function update($user, $notrigger = 0)
630 {
631 global $mysoc;
632
633 $error = 0;
634
635 // Clean parameters
636 $this->fk_contrat = (int) $this->fk_contrat;
637 $this->fk_product = (int) $this->fk_product;
638 $this->statut = (int) $this->statut;
639 $this->label = trim((string) $this->label);
640 $this->description = trim((string) $this->description);
641 $this->vat_src_code = trim((string) $this->vat_src_code);
642 $this->tva_tx = trim((string) $this->tva_tx);
643 $this->localtax1_tx = trim((string) $this->localtax1_tx);
644 $this->localtax2_tx = trim((string) $this->localtax2_tx);
645 $this->qty = (float) $this->qty;
646 $this->remise_percent = trim((string) $this->remise_percent);
647 $this->fk_remise_except = (int) $this->fk_remise_except;
648 $this->subprice = (float) price2num($this->subprice);
649 $this->info_bits = (int) $this->info_bits;
650 $this->fk_user_author = (int) $this->fk_user_author;
651 $this->fk_user_ouverture = (int) $this->fk_user_ouverture;
652 $this->fk_user_cloture = (int) $this->fk_user_cloture;
653 $this->commentaire = trim((string) $this->commentaire);
654 $this->rang = (int) $this->rang;
655 if (empty($this->subprice)) {
656 $this->subprice = 0;
657 }
658 if (empty($this->total_ht)) {
659 $this->total_ht = 0;
660 }
661 if (empty($this->total_tva)) {
662 $this->total_tva = 0;
663 }
664 if (empty($this->total_ttc)) {
665 $this->total_ttc = 0;
666 }
667 if (empty($this->localtax1_tx)) {
668 $this->localtax1_tx = 0;
669 }
670 if (empty($this->localtax2_tx)) {
671 $this->localtax2_tx = 0;
672 }
673 if (empty($this->remise_percent)) {
674 $this->remise_percent = 0;
675 }
676
677 // Calculation of the gross total (TTC) and VAT for the line from qty, pu, remise_percent and txtva
678 // VERY IMPORTANT: It's at the time of line insertion that we must store the net, VAT, and gross amounts,
679 // and this is done at the line level, which has its own VAT rate
680 $localtaxes_type = getLocalTaxesFromRate($this->tva_tx, 0, $this->thirdparty, $mysoc);
681
682 // Compute the total from the value the user actually entered, to avoid a rounding drift.
683 $line_price_base_type = $this->getPriceBaseType();
684 $pu_for_calc = $this->wasEnteredIncludingTax() ? (float) $this->subprice_ttc : (float) $this->subprice;
685
686 // Currency of the line follows its parent contract. The rate is not duplicated on the line,
687 // so it must be read from the contract each time (mirrors how the line has no multicurrency_tx property).
688 $multicurrency_tx = 1;
689 if (isModEnabled('multicurrency') && $this->fk_contrat > 0) {
690 $sql_contract_currency = "SELECT fk_multicurrency, multicurrency_code, multicurrency_tx FROM ".MAIN_DB_PREFIX."contrat WHERE rowid = ".((int) $this->fk_contrat);
691 $resql_contract_currency = $this->db->query($sql_contract_currency);
692 if ($resql_contract_currency) {
693 $obj_contract_currency = $this->db->fetch_object($resql_contract_currency);
694 if ($obj_contract_currency) {
695 $this->fk_multicurrency = $obj_contract_currency->fk_multicurrency;
696 $this->multicurrency_code = $obj_contract_currency->multicurrency_code;
697 $multicurrency_tx = $obj_contract_currency->multicurrency_tx ? $obj_contract_currency->multicurrency_tx : 1;
698 }
699 }
700 }
701
702 $tabprice = calcul_price_total($this->qty, $pu_for_calc, $this->remise_percent, (float) $this->tva_tx, $this->localtax1_tx, $this->localtax2_tx, 0, $line_price_base_type, 0, 1, $mysoc, $localtaxes_type, 100, $multicurrency_tx, (float) $this->multicurrency_subprice);
703 $this->total_ht = (float) $tabprice[0];
704 $this->total_tva = (float) $tabprice[1];
705 $this->total_ttc = (float) $tabprice[2];
706 $this->total_localtax1 = (float) $tabprice[9];
707 $this->total_localtax2 = (float) $tabprice[10];
708
709 // Multicurrency
710 $this->multicurrency_total_ht = (float) $tabprice[16];
711 $this->multicurrency_total_tva = (float) $tabprice[17];
712 $this->multicurrency_total_ttc = (float) $tabprice[18];
713 $this->multicurrency_subprice = (float) $tabprice[19];
714
715 if (empty($this->pa_ht)) {
716 $this->pa_ht = 0;
717 }
718
719 // if buy price not defined, define buyprice as configured in margin admin
720 if ($this->pa_ht == 0) {
721 $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product, $this->qty);
722 if ($result < 0) {
723 return -1;
724 } else {
725 $this->pa_ht = $result;
726 }
727 }
728
729 // $this->oldcopy should have been set by the caller of update (here properties were already modified)
730 if (empty($this->oldcopy)) {
731 dol_syslog("this->oldcopy should have been set by the caller of update (here properties were already modified)", LOG_WARNING);
732 $this->oldcopy = dol_clone($this, 2);
733 }
734
735 $this->db->begin();
736
737 // Update request
738 $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET";
739 $sql .= " fk_contrat = ".((int) $this->fk_contrat).",";
740 $sql .= " fk_product = ".($this->fk_product ? ((int) $this->fk_product) : 'null').",";
741 $sql .= " statut = ".((int) $this->statut).",";
742 $sql .= " label = '".$this->db->escape($this->label)."',";
743 $sql .= " description = '".$this->db->escape($this->description)."',";
744 $sql .= " date_commande = ".($this->date_commande != '' ? "'".$this->db->idate($this->date_commande)."'" : "null").",";
745 $sql .= " date_ouverture_prevue = ".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : "null").",";
746 $sql .= " date_ouverture = ".($this->date_start_real != '' ? "'".$this->db->idate($this->date_start_real)."'" : "null").",";
747 $sql .= " date_fin_validite = ".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : "null").",";
748 $sql .= " date_cloture = ".($this->date_end_real != '' ? "'".$this->db->idate($this->date_end_real)."'" : "null").",";
749 $sql .= " vat_src_code = '".$this->db->escape($this->vat_src_code)."',";
750 $sql .= " tva_tx = ".price2num($this->tva_tx).",";
751 $sql .= " localtax1_tx = ".price2num($this->localtax1_tx).",";
752 $sql .= " localtax2_tx = ".price2num($this->localtax2_tx).",";
753 $sql .= " qty = ".price2num($this->qty).",";
754 $sql .= " remise_percent = ".price2num($this->remise_percent).",";
755 $sql .= " fk_remise_except = ".($this->fk_remise_except > 0 ? ((int) $this->fk_remise_except) : "null").",";
756 $sql .= " subprice = ".($this->subprice != '' ? ((float) $this->subprice) : "null").",";
757 $sql .= " subprice_ttc = ".($this->subprice_ttc != '' ? ((float) $this->subprice_ttc) : "0").",";
758 $sql .= " total_ht = ".((float) $this->total_ht).",";
759 $sql .= " total_tva = ".((float) $this->total_tva).",";
760 $sql .= " total_localtax1 = ".((float) $this->total_localtax1).",";
761 $sql .= " total_localtax2 = ".((float) $this->total_localtax2).",";
762 $sql .= " total_ttc = ".((float) $this->total_ttc).",";
763 $sql .= " fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ? ((int) $this->fk_fournprice) : "NULL").",";
764 $sql .= " buy_price_ht = '".price2num($this->pa_ht)."',";
765 $sql .= " info_bits = '".$this->db->escape((string) $this->info_bits)."',";
766 $sql .= " fk_user_author = ".($this->fk_user_author >= 0 ? ((int) $this->fk_user_author) : "NULL").",";
767 $sql .= " fk_user_ouverture = ".($this->fk_user_ouverture > 0 ? ((int) $this->fk_user_ouverture) : "NULL").",";
768 $sql .= " fk_user_cloture = ".($this->fk_user_cloture > 0 ? ((int) $this->fk_user_cloture) : "NULL").",";
769 $sql .= " commentaire = '".$this->db->escape($this->commentaire)."',";
770 $sql .= " fk_unit = ".(!$this->fk_unit ? 'NULL' : ((int) $this->fk_unit)).",";
771 $sql .= " fk_multicurrency = ".(!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) : "NULL").",";
772 $sql .= " multicurrency_code = ".(!empty($this->multicurrency_code) ? "'".$this->db->escape($this->multicurrency_code)."'" : "NULL").",";
773 $sql .= " multicurrency_subprice = ".((float) $this->multicurrency_subprice).",";
774 $sql .= " multicurrency_total_ht = ".((float) $this->multicurrency_total_ht).",";
775 $sql .= " multicurrency_total_tva = ".((float) $this->multicurrency_total_tva).",";
776 $sql .= " multicurrency_total_ttc = ".((float) $this->multicurrency_total_ttc).",";
777 $sql .= " rang = ".(empty($this->rang) ? '0' : ((int) $this->rang));
778 $sql .= " WHERE rowid = ".((int) $this->id);
779
780 dol_syslog(get_class($this)."::update", LOG_DEBUG);
781 $resql = $this->db->query($sql);
782 if (!$resql) {
783 $this->error = "Error ".$this->db->lasterror();
784 $error++;
785 }
786
787 if (!$error) { // For avoid conflicts if trigger used
788 $result = $this->insertExtraFields();
789 if ($result < 0) {
790 $error++;
791 }
792 }
793
794 // If we change a planned date (start or end) of one contract line, sync dates for all other services too
795 if (!$error && getDolGlobalString('CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES')) {
796 dol_syslog(get_class($this)."::update CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES is on so we update date for all lines", LOG_DEBUG);
797
798 if ($this->date_start != $this->oldcopy->date_start) {
799 $sql = 'UPDATE '.MAIN_DB_PREFIX.'contratdet SET';
800 $sql .= " date_ouverture_prevue = ".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : "null");
801 $sql .= " WHERE fk_contrat = ".((int) $this->fk_contrat);
802
803 $resql = $this->db->query($sql);
804 if (!$resql) {
805 $error++;
806 $this->error = "Error ".$this->db->lasterror();
807 }
808 }
809 if ($this->date_end != $this->oldcopy->date_end) {
810 $sql = 'UPDATE '.MAIN_DB_PREFIX.'contratdet SET';
811 $sql .= " date_fin_validite = ".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : "null");
812 $sql .= " WHERE fk_contrat = ".((int) $this->fk_contrat);
813
814 $resql = $this->db->query($sql);
815 if (!$resql) {
816 $error++;
817 $this->error = "Error ".$this->db->lasterror();
818 }
819 }
820 }
821
822 if (!$error && !$notrigger) {
823 // Call trigger
824 $result = $this->call_trigger('LINECONTRACT_MODIFY', $user);
825 if ($result < 0) {
826 $error++;
827 $this->db->rollback();
828 }
829 // End call triggers
830 }
831
832 if (!$error) {
833 $this->db->commit();
834 return 1;
835 } else {
836 $this->db->rollback();
837 $this->errors[] = $this->error;
838 return -1;
839 }
840 }
841
842
843 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
850 public function update_total()
851 {
852 // phpcs:enable
853 $this->db->begin();
854
855 // Update line in database
856 $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET";
857 $sql .= " total_ht=".price2num($this->total_ht, 'MT');
858 $sql .= ",total_tva=".price2num($this->total_tva, 'MT');
859 $sql .= ",total_localtax1=".price2num($this->total_localtax1, 'MT');
860 $sql .= ",total_localtax2=".price2num($this->total_localtax2, 'MT');
861 $sql .= ",total_ttc=".price2num($this->total_ttc, 'MT');
862 $sql .= " WHERE rowid = ".((int) $this->id);
863
864 dol_syslog(get_class($this)."::update_total", LOG_DEBUG);
865
866 $resql = $this->db->query($sql);
867 if ($resql) {
868 $this->db->commit();
869 return 1;
870 } else {
871 $this->error = $this->db->error();
872 $this->db->rollback();
873 return -2;
874 }
875 }
876
877
884 public function insert($notrigger = 0)
885 {
886 global $user;
887
888 $error = 0;
889
890 // Insert into database
891 $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet";
892 $sql .= " (fk_contrat, label, description, fk_product, qty, vat_src_code, tva_tx,";
893 $sql .= " localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, subprice_ttc,";
894 $sql .= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
895 $sql .= " info_bits,";
896 $sql .= " rang,";
897 $sql .= " fk_product_fournisseur_price, buy_price_ht";
898 if ($this->date_start > 0) {
899 $sql .= ",date_ouverture_prevue";
900 }
901 if ($this->date_end > 0) {
902 $sql .= ",date_fin_validite";
903 }
904 $sql .= ") VALUES (".((int) $this->fk_contrat).", '', '".$this->db->escape($this->description)."',";
905 $sql .= ($this->fk_product > 0 ? ((int) $this->fk_product) : "null").",";
906 $sql .= " '".$this->db->escape((string) $this->qty)."',";
907 $sql .= " '".$this->db->escape($this->vat_src_code)."',";
908 $sql .= " '".$this->db->escape($this->tva_tx)."',";
909 $sql .= " '".$this->db->escape($this->localtax1_tx)."',";
910 $sql .= " '".$this->db->escape($this->localtax2_tx)."',";
911 $sql .= " '".$this->db->escape($this->localtax1_type)."',";
912 $sql .= " '".$this->db->escape($this->localtax2_type)."',";
913 $sql .= " ".price2num($this->remise_percent).",".price2num($this->subprice).",".((float) price2num($this->subprice_ttc)).",";
914 $sql .= " ".price2num($this->total_ht).",".price2num($this->total_tva).",".price2num($this->total_localtax1).",".price2num($this->total_localtax2).",".price2num($this->total_ttc).",";
915 $sql .= " '".$this->db->escape((string) $this->info_bits)."',";
916 $sql .= " ".(empty($this->rang) ? '0' : (int) $this->rang).",";
917 if ($this->fk_fournprice > 0) {
918 $sql .= ' '.((int) $this->fk_fournprice).',';
919 } else {
920 $sql .= ' null,';
921 }
922 if ($this->pa_ht > 0) {
923 $sql .= ' '.((float) price2num($this->pa_ht));
924 } else {
925 $sql .= ' null';
926 }
927 if ($this->date_start > 0) {
928 $sql .= ",'".$this->db->idate($this->date_start)."'";
929 }
930 if ($this->date_end > 0) {
931 $sql .= ",'".$this->db->idate($this->date_end)."'";
932 }
933 $sql .= ")";
934
935 dol_syslog(get_class($this)."::insert", LOG_DEBUG);
936
937 $resql = $this->db->query($sql);
938 if ($resql) {
939 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'contratdet');
940
941 // Insert of extrafields
942 $result = $this->insertExtraFields();
943 if ($result < 0) {
944 $this->db->rollback();
945 return -1;
946 }
947
948 if (!$notrigger) {
949 // Call trigger
950 $result = $this->call_trigger('LINECONTRACT_INSERT', $user);
951 if ($result < 0) {
952 $this->db->rollback();
953 return -1;
954 }
955 // End call triggers
956 }
957
958 $this->db->commit();
959 return 1;
960 } else {
961 $this->db->rollback();
962 $this->error = $this->db->error()." sql=".$sql;
963 return -1;
964 }
965 }
966
967 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
977 public function active_line($user, $date, $date_end = '', $comment = '')
978 {
979 // phpcs:enable
980 $error = 0;
981
982 $this->db->begin();
983
984 $this->statut = ContratLigne::STATUS_OPEN;
985 $this->status = ContratLigne::STATUS_OPEN;
986 $this->date_start_real = $date;
987 $this->date_end = $date_end;
988 $this->fk_user_ouverture = $user->id;
989 $this->date_end_real = null;
990 $this->commentaire = $comment;
991
992 $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET statut = ".((int) $this->status).",";
993 $sql .= " date_ouverture = ".(dol_strlen((string) $this->date_start_real) != 0 ? "'".$this->db->idate($this->date_start_real)."'" : "null").",";
994 if ($date_end >= 0) {
995 $sql .= " date_fin_validite = ".(dol_strlen($this->date_end) != 0 ? "'".$this->db->idate($this->date_end)."'" : "null").",";
996 }
997 $sql .= " fk_user_ouverture = ".((int) $this->fk_user_ouverture).",";
998 $sql .= " date_cloture = null,";
999 $sql .= " fk_user_cloture = null,";
1000 $sql .= " commentaire = '".$this->db->escape($comment)."'";
1001 $sql .= " WHERE rowid = ".((int) $this->id)." AND (statut = ".ContratLigne::STATUS_INITIAL." OR statut = ".ContratLigne::STATUS_CLOSED.")";
1002
1003 dol_syslog(get_class($this)."::active_line", LOG_DEBUG);
1004 $resql = $this->db->query($sql);
1005 if ($resql) {
1006 if ($date_end >= 0) {
1007 // Update column llx_contrat.denormalized_lower_panned_end_date with next expiration date of an open contract
1008 $sqltoupdatecontract = "UPDATE ".MAIN_DB_PREFIX."contrat";
1009 $sqltoupdatecontract .= " SET denormalized_lower_planned_end_date = (SELECT MIN(date_fin_validite) FROM ".MAIN_DB_PREFIX."contratdet as cd WHERE cd.fk_contrat = ".((int) $this->fk_contrat)." AND cd.statut = ".ContratLigne::STATUS_OPEN.")";
1010 $sqltoupdatecontract .= " WHERE rowid = ".((int) $this->fk_contrat);
1011 $resqltoupdatecontract = $this->db->query($sqltoupdatecontract);
1012 if (!$resqltoupdatecontract) {
1013 $this->error = $this->db->lasterror();
1014 $this->db->rollback();
1015 return -1;
1016 }
1017 }
1018
1019 // Call trigger
1020 $result = $this->call_trigger('LINECONTRACT_ACTIVATE', $user);
1021 if ($result < 0) {
1022 $error++;
1023 }
1024 // End call triggers
1025
1026 if (!$error) {
1027 $this->db->commit();
1028 return 1;
1029 } else {
1030 $this->db->rollback();
1031 return -1;
1032 }
1033 } else {
1034 $this->error = $this->db->lasterror();
1035 $this->db->rollback();
1036 return -1;
1037 }
1038 }
1039
1040 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1050 public function close_line($user, $date_end_real, $comment = '', $notrigger = 0)
1051 {
1052 // phpcs:enable
1053 $this->date_cloture = $date_end_real;
1054 $this->date_end_real = $date_end_real;
1055 $this->user_closing_id = $user->id;
1056 $this->commentaire = $comment;
1057
1058 $error = 0;
1059
1060 // statut actif : 4
1061
1062 $this->db->begin();
1063
1064 $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET statut = ".((int) ContratLigne::STATUS_CLOSED).",";
1065 $sql .= " date_cloture = '".$this->db->idate($date_end_real)."',";
1066 $sql .= " fk_user_cloture = ".((int) $user->id).",";
1067 $sql .= " commentaire = '".$this->db->escape($comment)."'";
1068 $sql .= " WHERE rowid = ".((int) $this->id)." AND statut <> ".((int) ContratLigne::STATUS_CLOSED);
1069
1070 $resql = $this->db->query($sql);
1071 if ($resql) {
1072 // Update column llx_contrat.denormalized_lower_panned_end_date with next expiration date of an open contract
1073 $sqltoupdatecontract = "UPDATE ".MAIN_DB_PREFIX."contrat";
1074 $sqltoupdatecontract .= " SET denormalized_lower_planned_end_date = (SELECT MIN(date_fin_validite) FROM ".MAIN_DB_PREFIX."contratdet as cd WHERE cd.fk_contrat = ".((int) $this->fk_contrat)." AND cd.statut = ".ContratLigne::STATUS_OPEN.")";
1075 $sqltoupdatecontract .= " WHERE rowid = ".((int) $this->fk_contrat);
1076 $resqltoupdatecontract = $this->db->query($sqltoupdatecontract);
1077 if (!$resqltoupdatecontract) {
1078 $this->error = $this->db->lasterror();
1079 $this->db->rollback();
1080 return -1;
1081 }
1082
1083 if (!$notrigger) {
1084 // Call trigger
1085 $result = $this->call_trigger('LINECONTRACT_CLOSE', $user);
1086 if ($result < 0) {
1087 $error++;
1088 $this->db->rollback();
1089 return -1;
1090 }
1091 // End call triggers
1092 }
1093
1094 $this->db->commit();
1095 return 1;
1096 } else {
1097 $this->error = $this->db->lasterror();
1098 $this->db->rollback();
1099 return -1;
1100 }
1101 }
1102}
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition propal.php:281
$object ref
Definition info.php:90
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
defineBuyPrice($unitPrice=0.0, $discountPercent=0.0, $fk_product=0, $qty=0)
Get buy price to use for margin calculation.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
wasEnteredIncludingTax()
Return true if the unit price was originally entered including tax (TTC mode).
getPriceBaseType()
Return the price base type ('TTC' or 'HT') matching how the unit price was entered.
Class to manage lines of contracts.
active_line($user, $date, $date_end='', $comment='')
Activate a contract line.
getLibStatut($mode)
Return label of this contract line status.
insert($notrigger=0)
Inserts a contrat line into database.
fetch($id, $ref='')
Load object in memory from database.
close_line($user, $date_end_real, $comment='', $notrigger=0)
Close a contract line.
static LibStatut($status, $mode, $expired=-1, $moreatt='', $morelabel='')
Return label of a contract line status.
update($user, $notrigger=0)
Update database for contract line.
update_total()
Update in database the fields total_xxx of lines Used by migration process.
__construct($db)
Constructor.
getTooltipContentArray($params)
getTooltipContentArray
getNomUrl($withpicto=0, $maxlength=0)
Return clickable name (with picto eventually) for ContratLigne.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:169
global $mysoc
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_now($mode='gmt')
Return date for now.
price2num($amount, $rounding='', $option=0, $currencycode='')
Function that return a number with universal decimal format (decimal separator is '.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
dol_clone($srcobject, $native=2)
Create a clone of instance of object (new instance with same value for each properties) With native =...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getMarginInfos($pv_ht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $pa_ht)
Return an array with margins information of a line.
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $notused, $price_base_type, $info_bits, $type, $seller=null, $localtaxes_array=[], $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
Definition price.lib.php:90