dolibarr  16.0.5
propal.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
4  * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
5  * Copyright (C) 2005 Marc Barilley <marc@ocebo.com>
6  * Copyright (C) 2005-2013 Regis Houssin <regis.houssin@inodbox.com>
7  * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
8  * Copyright (C) 2008 Raphael Bertrand <raphael.bertrand@resultic.fr>
9  * Copyright (C) 2010-2020 Juanjo Menent <jmenent@2byte.es>
10  * Copyright (C) 2010-2017 Philippe Grand <philippe.grand@atoo-net.com>
11  * Copyright (C) 2012-2014 Christophe Battarel <christophe.battarel@altairis.fr>
12  * Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
13  * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
14  * Copyright (C) 2014-2015 Marcos García <marcosgdf@gmail.com>
15  * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
16  * Copyright (C) 2018-2021 Frédéric France <frederic.france@netlogic.fr>
17  * Copyright (C) 2018 Ferran Marcet <fmarcet@2byte.es>
18  * Copyright (C) 2022 ATM Consulting <contact@atm-consulting.fr>
19  * Copyright (C) 2022 OpenDSI <support@open-dsi.fr>
20  * Copyright (C) 2022 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
21  *
22  * This program is free software; you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation; either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program. If not, see <https://www.gnu.org/licenses/>.
34  */
35 
41 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
42 require_once DOL_DOCUMENT_ROOT."/core/class/commonobjectline.class.php";
43 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
44 require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
45 require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php';
46 require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
47 require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php';
48 
52 class Propal extends CommonObject
53 {
54  use CommonIncoterm;
55 
59  public $element = 'propal';
60 
64  public $table_element = 'propal';
65 
69  public $table_element_line = 'propaldet';
70 
74  public $fk_element = 'fk_propal';
75 
79  public $picto = 'propal';
80 
85  public $ismultientitymanaged = 1;
86 
91  public $restrictiononfksoc = 1;
92 
96  protected $table_ref_field = 'ref';
97 
102  public $socid;
103 
108  public $contactid;
109  public $author;
110 
115  public $ref_client;
116 
122  public $statut;
123 
129  public $status;
130 
135  public $datec;
136 
140  public $date_creation;
141 
146  public $datev;
147 
151  public $date_validation;
152 
156  public $date_signature;
157 
161  public $user_signature;
162 
166  public $date;
167 
172  public $datep;
173 
178  public $date_livraison; // deprecated; Use delivery_date instead.
179 
183  public $delivery_date; // Date expected of shipment (date starting shipment, not the reception that occurs some days after)
184 
185 
186  public $fin_validite;
187 
188  public $user_author_id;
189  public $user_valid_id;
190  public $user_close_id;
191 
196  public $price;
201  public $tva;
206  public $total;
207 
208  public $cond_reglement_code;
209  public $deposit_percent;
210  public $mode_reglement_code;
211  public $remise_percent;
212 
216  public $remise;
221 
226  public $fk_address;
227 
228  public $address_type;
229  public $address;
230 
231  public $availability_id;
232  public $availability_code;
233 
234  public $duree_validite;
235 
236  public $demand_reason_id;
237  public $demand_reason_code;
238 
239  public $warehouse_id;
240 
241  public $extraparams = array();
242 
246  public $lines = array();
247  public $line;
248 
249  public $labelStatus = array();
250  public $labelStatusShort = array();
251 
252  // Multicurrency
256  public $fk_multicurrency;
257 
258  public $multicurrency_code;
259  public $multicurrency_tx;
260  public $multicurrency_total_ht;
261  public $multicurrency_total_tva;
262  public $multicurrency_total_ttc;
263 
264 
289  // BEGIN MODULEBUILDER PROPERTIES
293  public $fields = array(
294  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
295  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>15, 'index'=>1),
296  'ref' =>array('type'=>'varchar(30)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'showoncombobox'=>1, 'position'=>20),
297  'ref_client' =>array('type'=>'varchar(255)', 'label'=>'RefCustomer', 'enabled'=>1, 'visible'=>-1, 'position'=>22),
298  'ref_ext' =>array('type'=>'varchar(255)', 'label'=>'RefExt', 'enabled'=>1, 'visible'=>0, 'position'=>40),
299  'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>'$conf->societe->enabled', 'visible'=>-1, 'position'=>23),
300  'fk_projet' =>array('type'=>'integer:Project:projet/class/project.class.php:1:fk_statut=1', 'label'=>'Fk projet', 'enabled'=>'$conf->project->enabled', 'visible'=>-1, 'position'=>24),
301  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>25),
302  'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
303  'datep' =>array('type'=>'date', 'label'=>'Date', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
304  'fin_validite' =>array('type'=>'datetime', 'label'=>'DateEnd', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
305  'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>70),
306  'date_cloture' =>array('type'=>'datetime', 'label'=>'DateClosing', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
307  'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'position'=>80),
308  'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>85),
309  'fk_user_valid' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
310  'fk_user_cloture' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user cloture', 'enabled'=>1, 'visible'=>-1, 'position'=>95),
311  'price' =>array('type'=>'double', 'label'=>'Price', 'enabled'=>1, 'visible'=>-1, 'position'=>105),
312  'remise_percent' =>array('type'=>'double', 'label'=>'RelativeDiscount', 'enabled'=>1, 'visible'=>-1, 'position'=>110),
313  //'remise_absolue' =>array('type'=>'double', 'label'=>'CustomerRelativeDiscount', 'enabled'=>1, 'visible'=>-1, 'position'=>115),
314  //'remise' =>array('type'=>'double', 'label'=>'Remise', 'enabled'=>1, 'visible'=>-1, 'position'=>120),
315  'total_ht' =>array('type'=>'double(24,8)', 'label'=>'TotalHT', 'enabled'=>1, 'visible'=>-1, 'position'=>125, 'isameasure'=>1),
316  'total_tva' =>array('type'=>'double(24,8)', 'label'=>'VAT', 'enabled'=>1, 'visible'=>-1, 'position'=>130, 'isameasure'=>1),
317  'localtax1' =>array('type'=>'double(24,8)', 'label'=>'LocalTax1', 'enabled'=>1, 'visible'=>-1, 'position'=>135, 'isameasure'=>1),
318  'localtax2' =>array('type'=>'double(24,8)', 'label'=>'LocalTax2', 'enabled'=>1, 'visible'=>-1, 'position'=>140, 'isameasure'=>1),
319  'total_ttc' =>array('type'=>'double(24,8)', 'label'=>'TotalTTC', 'enabled'=>1, 'visible'=>-1, 'position'=>145, 'isameasure'=>1),
320  'fk_account' =>array('type'=>'integer', 'label'=>'BankAccount', 'enabled'=>'$conf->banque->enabled', 'visible'=>-1, 'position'=>150),
321  'fk_currency' =>array('type'=>'varchar(3)', 'label'=>'Currency', 'enabled'=>1, 'visible'=>-1, 'position'=>155),
322  'fk_cond_reglement' =>array('type'=>'integer', 'label'=>'PaymentTerm', 'enabled'=>1, 'visible'=>-1, 'position'=>160),
323  'deposit_percent' =>array('type'=>'varchar(63)', 'label'=>'DepositPercent', 'enabled'=>1, 'visible'=>-1, 'position'=>161),
324  'fk_mode_reglement' =>array('type'=>'integer', 'label'=>'PaymentMode', 'enabled'=>1, 'visible'=>-1, 'position'=>165),
325  'note_private' =>array('type'=>'text', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>170),
326  'note_public' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>175),
327  'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'PDFTemplate', 'enabled'=>1, 'visible'=>0, 'position'=>180),
328  'date_livraison' =>array('type'=>'date', 'label'=>'DateDeliveryPlanned', 'enabled'=>1, 'visible'=>-1, 'position'=>185),
329  'fk_shipping_method' =>array('type'=>'integer', 'label'=>'ShippingMethod', 'enabled'=>1, 'visible'=>-1, 'position'=>190),
330  'fk_warehouse' =>array('type'=>'integer:Entrepot:product/stock/class/entrepot.class.php', 'label'=>'Fk warehouse', 'enabled'=>'$conf->stock->enabled', 'visible'=>-1, 'position'=>191),
331  'fk_availability' =>array('type'=>'integer', 'label'=>'Availability', 'enabled'=>1, 'visible'=>-1, 'position'=>195),
332  'fk_delivery_address' =>array('type'=>'integer', 'label'=>'DeliveryAddress', 'enabled'=>1, 'visible'=>0, 'position'=>200), // deprecated
333  'fk_input_reason' =>array('type'=>'integer', 'label'=>'InputReason', 'enabled'=>1, 'visible'=>-1, 'position'=>205),
334  'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>215),
335  'fk_incoterms' =>array('type'=>'integer', 'label'=>'IncotermCode', 'enabled'=>'$conf->incoterm->enabled', 'visible'=>-1, 'position'=>220),
336  'location_incoterms' =>array('type'=>'varchar(255)', 'label'=>'IncotermLabel', 'enabled'=>'$conf->incoterm->enabled', 'visible'=>-1, 'position'=>225),
337  'fk_multicurrency' =>array('type'=>'integer', 'label'=>'MulticurrencyID', 'enabled'=>1, 'visible'=>-1, 'position'=>230),
338  'multicurrency_code' =>array('type'=>'varchar(255)', 'label'=>'MulticurrencyCurrency', 'enabled'=>'$conf->multicurrency->enabled', 'visible'=>-1, 'position'=>235),
339  'multicurrency_tx' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyRate', 'enabled'=>'$conf->multicurrency->enabled', 'visible'=>-1, 'position'=>240, 'isameasure'=>1),
340  'multicurrency_total_ht' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyAmountHT', 'enabled'=>'$conf->multicurrency->enabled', 'visible'=>-1, 'position'=>245, 'isameasure'=>1),
341  'multicurrency_total_tva' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyAmountVAT', 'enabled'=>'$conf->multicurrency->enabled', 'visible'=>-1, 'position'=>250, 'isameasure'=>1),
342  'multicurrency_total_ttc' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyAmountTTC', 'enabled'=>'$conf->multicurrency->enabled', 'visible'=>-1, 'position'=>255, 'isameasure'=>1),
343  'last_main_doc' =>array('type'=>'varchar(255)', 'label'=>'LastMainDoc', 'enabled'=>1, 'visible'=>-1, 'position'=>260),
344  'fk_statut' =>array('type'=>'smallint(6)', 'label'=>'Status', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500),
345  'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>900),
346  );
347  // END MODULEBUILDER PROPERTIES
348 
352  const STATUS_DRAFT = 0;
356  const STATUS_VALIDATED = 1;
360  const STATUS_SIGNED = 2;
364  const STATUS_NOTSIGNED = 3;
368  const STATUS_BILLED = 4; // Todo rename into STATUS_CLOSE ?
369 
370 
378  public function __construct($db, $socid = 0, $propalid = 0)
379  {
380  global $conf, $langs;
381 
382  $this->db = $db;
383 
384  $this->socid = $socid;
385  $this->id = $propalid;
386 
387  $this->duree_validite = getDolGlobalInt('PROPALE_VALIDITY_DURATION', 0);
388  }
389 
390 
391  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
403  public function add_product($idproduct, $qty, $remise_percent = 0)
404  {
405  // phpcs:enable
406  global $conf, $mysoc;
407 
408  if (!$qty) {
409  $qty = 1;
410  }
411 
412  dol_syslog(get_class($this)."::add_product $idproduct, $qty, $remise_percent");
413  if ($idproduct > 0) {
414  $prod = new Product($this->db);
415  $prod->fetch($idproduct);
416 
417  $productdesc = $prod->description;
418 
419  $tva_tx = get_default_tva($mysoc, $this->thirdparty, $prod->id);
420  $tva_npr = get_default_npr($mysoc, $this->thirdparty, $prod->id);
421  if (empty($tva_tx)) {
422  $tva_npr = 0;
423  }
424  $vat_src_code = ''; // May be defined into tva_tx
425 
426  $localtax1_tx = get_localtax($tva_tx, 1, $mysoc, $this->thirdparty, $tva_npr);
427  $localtax2_tx = get_localtax($tva_tx, 2, $mysoc, $this->thirdparty, $tva_npr);
428 
429  // multiprices
430  if ($conf->global->PRODUIT_MULTIPRICES && $this->thirdparty->price_level) {
431  $price = $prod->multiprices[$this->thirdparty->price_level];
432  } else {
433  $price = $prod->price;
434  }
435 
436  $line = new PropaleLigne($this->db);
437 
438  $line->fk_product = $idproduct;
439  $line->desc = $productdesc;
440  $line->qty = $qty;
441  $line->subprice = $price;
442  $line->remise_percent = $remise_percent;
443  $line->vat_src_code = $vat_src_code;
444  $line->tva_tx = $tva_tx;
445  $line->fk_unit = $prod->fk_unit;
446  if ($tva_npr) {
447  $line->info_bits = 1;
448  }
449 
450  $this->lines[] = $line;
451  }
452  }
453 
454  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
461  public function insert_discount($idremise)
462  {
463  // phpcs:enable
464  global $langs;
465 
466  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
467  include_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
468 
469  $this->db->begin();
470 
471  $remise = new DiscountAbsolute($this->db);
472  $result = $remise->fetch($idremise);
473 
474  if ($result > 0) {
475  if ($remise->fk_facture) { // Protection against multiple submission
476  $this->error = $langs->trans("ErrorDiscountAlreadyUsed");
477  $this->db->rollback();
478  return -5;
479  }
480 
481  $line = new PropaleLigne($this->db);
482 
483  $this->line->context = $this->context;
484 
485  $line->fk_propal = $this->id;
486  $line->fk_remise_except = $remise->id;
487  $line->desc = $remise->description; // Description ligne
488  $line->vat_src_code = $remise->vat_src_code;
489  $line->tva_tx = $remise->tva_tx;
490  $line->subprice = -$remise->amount_ht;
491  $line->fk_product = 0; // Id produit predefined
492  $line->qty = 1;
493  $line->remise_percent = 0;
494  $line->rang = -1;
495  $line->info_bits = 2;
496 
497  // TODO deprecated
498  $line->price = -$remise->amount_ht;
499 
500  $line->total_ht = -$remise->amount_ht;
501  $line->total_tva = -$remise->amount_tva;
502  $line->total_ttc = -$remise->amount_ttc;
503 
504  $result = $line->insert();
505  if ($result > 0) {
506  $result = $this->update_price(1);
507  if ($result > 0) {
508  $this->db->commit();
509  return 1;
510  } else {
511  $this->db->rollback();
512  return -1;
513  }
514  } else {
515  $this->error = $line->error;
516  $this->errors = $line->errors;
517  $this->db->rollback();
518  return -2;
519  }
520  } else {
521  $this->db->rollback();
522  return -2;
523  }
524  }
525 
563  public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0.0, $txlocaltax2 = 0.0, $fk_product = 0, $remise_percent = 0.0, $price_base_type = 'HT', $pu_ttc = 0.0, $info_bits = 0, $type = 0, $rang = -1, $special_code = 0, $fk_parent_line = 0, $fk_fournprice = 0, $pa_ht = 0, $label = '', $date_start = '', $date_end = '', $array_options = 0, $fk_unit = null, $origin = '', $origin_id = 0, $pu_ht_devise = 0, $fk_remise_except = 0, $noupdateafterinsertline = 0)
564  {
565  global $mysoc, $conf, $langs;
566 
567  dol_syslog(get_class($this)."::addline propalid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_except=$remise_percent, price_base_type=$price_base_type, pu_ttc=$pu_ttc, info_bits=$info_bits, type=$type, fk_remise_except=".$fk_remise_except);
568 
569  if ($this->statut == self::STATUS_DRAFT) {
570  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
571 
572  // Clean parameters
573  if (empty($remise_percent)) {
574  $remise_percent = 0;
575  }
576  if (empty($qty)) {
577  $qty = 0;
578  }
579  if (empty($info_bits)) {
580  $info_bits = 0;
581  }
582  if (empty($rang)) {
583  $rang = 0;
584  }
585  if (empty($fk_parent_line) || $fk_parent_line < 0) {
586  $fk_parent_line = 0;
587  }
588 
589  $remise_percent = price2num($remise_percent);
590  $qty = price2num($qty);
591  $pu_ht = price2num($pu_ht);
592  $pu_ht_devise = price2num($pu_ht_devise);
593  $pu_ttc = price2num($pu_ttc);
594  if (!preg_match('/\((.*)\)/', $txtva)) {
595  $txtva = price2num($txtva); // $txtva can have format '5,1' or '5.1' or '5.1(XXX)', we must clean only if '5,1'
596  }
597  $txlocaltax1 = price2num($txlocaltax1);
598  $txlocaltax2 = price2num($txlocaltax2);
599  $pa_ht = price2num($pa_ht);
600  if ($price_base_type == 'HT') {
601  $pu = $pu_ht;
602  } else {
603  $pu = $pu_ttc;
604  }
605 
606  // Check parameters
607  if ($type < 0) {
608  return -1;
609  }
610 
611  if ($date_start && $date_end && $date_start > $date_end) {
612  $langs->load("errors");
613  $this->error = $langs->trans('ErrorStartDateGreaterEnd');
614  return -1;
615  }
616 
617  $this->db->begin();
618 
619  $product_type = $type;
620  if (!empty($fk_product) && $fk_product > 0) {
621  $product = new Product($this->db);
622  $result = $product->fetch($fk_product);
623  $product_type = $product->type;
624 
625  if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_PROPOSAL) && $product_type == 0 && $product->stock_reel < $qty) {
626  $langs->load("errors");
627  $this->error = $langs->trans('ErrorStockIsNotEnoughToAddProductOnProposal', $product->ref);
628  $this->db->rollback();
629  return -3;
630  }
631  }
632 
633  // Calcul du total TTC et de la TVA pour la ligne a partir de
634  // qty, pu, remise_percent et txtva
635  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
636  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
637 
638  $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
639 
640  // Clean vat code
641  $reg = array();
642  $vat_src_code = '';
643  $reg = array();
644  if (preg_match('/\((.*)\)/', $txtva, $reg)) {
645  $vat_src_code = $reg[1];
646  $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
647  }
648 
649  $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
650 
651  $total_ht = $tabprice[0];
652  $total_tva = $tabprice[1];
653  $total_ttc = $tabprice[2];
654  $total_localtax1 = $tabprice[9];
655  $total_localtax2 = $tabprice[10];
656  $pu_ht = $tabprice[3];
657  $pu_tva = $tabprice[4];
658  $pu_ttc = $tabprice[5];
659 
660  // MultiCurrency
661  $multicurrency_total_ht = $tabprice[16];
662  $multicurrency_total_tva = $tabprice[17];
663  $multicurrency_total_ttc = $tabprice[18];
664  $pu_ht_devise = $tabprice[19];
665 
666  // Rang to use
667  $ranktouse = $rang;
668  if ($ranktouse == -1) {
669  $rangmax = $this->line_max($fk_parent_line);
670  $ranktouse = $rangmax + 1;
671  }
672 
673  // TODO A virer
674  // Anciens indicateurs: $price, $remise (a ne plus utiliser)
675  $price = $pu;
676  $remise = 0;
677  if ($remise_percent > 0) {
678  $remise = round(($pu * $remise_percent / 100), 2);
679  $price = $pu - $remise;
680  }
681 
682  // Insert line
683  $this->line = new PropaleLigne($this->db);
684 
685  $this->line->context = $this->context;
686 
687  $this->line->fk_propal = $this->id;
688  $this->line->label = $label;
689  $this->line->desc = $desc;
690  $this->line->qty = $qty;
691 
692  $this->line->vat_src_code = $vat_src_code;
693  $this->line->tva_tx = $txtva;
694  $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
695  $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
696  $this->line->localtax1_type = empty($localtaxes_type[0]) ? '' : $localtaxes_type[0];
697  $this->line->localtax2_type = empty($localtaxes_type[2]) ? '' : $localtaxes_type[2];
698  $this->line->fk_product = $fk_product;
699  $this->line->product_type = $type;
700  $this->line->fk_remise_except = $fk_remise_except;
701  $this->line->remise_percent = $remise_percent;
702  $this->line->subprice = $pu_ht;
703  $this->line->rang = $ranktouse;
704  $this->line->info_bits = $info_bits;
705  $this->line->total_ht = $total_ht;
706  $this->line->total_tva = $total_tva;
707  $this->line->total_localtax1 = $total_localtax1;
708  $this->line->total_localtax2 = $total_localtax2;
709  $this->line->total_ttc = $total_ttc;
710  $this->line->special_code = $special_code;
711  $this->line->fk_parent_line = $fk_parent_line;
712  $this->line->fk_unit = $fk_unit;
713 
714  $this->line->date_start = $date_start;
715  $this->line->date_end = $date_end;
716 
717  $this->line->fk_fournprice = $fk_fournprice;
718  $this->line->pa_ht = $pa_ht;
719 
720  $this->line->origin_id = $origin_id;
721  $this->line->origin = $origin;
722 
723  // Multicurrency
724  $this->line->fk_multicurrency = $this->fk_multicurrency;
725  $this->line->multicurrency_code = $this->multicurrency_code;
726  $this->line->multicurrency_subprice = $pu_ht_devise;
727  $this->line->multicurrency_total_ht = $multicurrency_total_ht;
728  $this->line->multicurrency_total_tva = $multicurrency_total_tva;
729  $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
730 
731  // Mise en option de la ligne
732  if (empty($qty) && empty($special_code)) {
733  $this->line->special_code = 3;
734  }
735 
736  // TODO deprecated
737  $this->line->price = $price;
738 
739  if (is_array($array_options) && count($array_options) > 0) {
740  $this->line->array_options = $array_options;
741  }
742 
743  $result = $this->line->insert();
744  if ($result > 0) {
745  // Reorder if child line
746  if (!empty($fk_parent_line)) {
747  $this->line_order(true, 'DESC');
748  } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) { // Update all rank of all other lines
749  $linecount = count($this->lines);
750  for ($ii = $ranktouse; $ii <= $linecount; $ii++) {
751  $this->updateRangOfLine($this->lines[$ii - 1]->id, $ii + 1);
752  }
753  }
754 
755  // Mise a jour informations denormalisees au niveau de la propale meme
756  if (empty($noupdateafterinsertline)) {
757  $result = $this->update_price(1, 'auto', 0, $mysoc); // This method is designed to add line from user input so total calculation must be done using 'auto' mode.
758  }
759 
760  if ($result > 0) {
761  $this->db->commit();
762  return $this->line->id;
763  } else {
764  $this->error = $this->db->error();
765  $this->db->rollback();
766  return -1;
767  }
768  } else {
769  $this->error = $this->line->error;
770  $this->errors = $this->line->errors;
771  $this->db->rollback();
772  return -2;
773  }
774  } else {
775  dol_syslog(get_class($this)."::addline status of proposal must be Draft to allow use of ->addline()", LOG_ERR);
776  return -3;
777  }
778  }
779 
780 
810  public function updateline($rowid, $pu, $qty, $remise_percent, $txtva, $txlocaltax1 = 0.0, $txlocaltax2 = 0.0, $desc = '', $price_base_type = 'HT', $info_bits = 0, $special_code = 0, $fk_parent_line = 0, $skip_update_total = 0, $fk_fournprice = 0, $pa_ht = 0, $label = '', $type = 0, $date_start = '', $date_end = '', $array_options = 0, $fk_unit = null, $pu_ht_devise = 0, $notrigger = 0, $rang = 0)
811  {
812  global $mysoc, $langs;
813 
814  dol_syslog(get_class($this)."::updateLine rowid=$rowid, pu=$pu, qty=$qty, remise_percent=$remise_percent,
815  txtva=$txtva, desc=$desc, price_base_type=$price_base_type, info_bits=$info_bits, special_code=$special_code, fk_parent_line=$fk_parent_line, pa_ht=$pa_ht, type=$type, date_start=$date_start, date_end=$date_end");
816  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
817 
818  // Clean parameters
819  $remise_percent = price2num($remise_percent);
820  $qty = price2num($qty);
821  $pu = price2num($pu);
822  $pu_ht_devise = price2num($pu_ht_devise);
823  if (!preg_match('/\((.*)\)/', $txtva)) {
824  $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
825  }
826  $txlocaltax1 = price2num($txlocaltax1);
827  $txlocaltax2 = price2num($txlocaltax2);
828  $pa_ht = price2num($pa_ht);
829  if (empty($qty) && empty($special_code)) {
830  $special_code = 3; // Set option tag
831  }
832  if (!empty($qty) && $special_code == 3) {
833  $special_code = 0; // Remove option tag
834  }
835  if (empty($type)) {
836  $type = 0;
837  }
838 
839  if ($date_start && $date_end && $date_start > $date_end) {
840  $langs->load("errors");
841  $this->error = $langs->trans('ErrorStartDateGreaterEnd');
842  return -1;
843  }
844 
845  if ($this->statut == self::STATUS_DRAFT) {
846  $this->db->begin();
847 
848  // Calcul du total TTC et de la TVA pour la ligne a partir de
849  // qty, pu, remise_percent et txtva
850  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
851  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
852 
853  $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
854 
855  // Clean vat code
856  $reg = array();
857  $vat_src_code = '';
858  if (preg_match('/\((.*)\)/', $txtva, $reg)) {
859  $vat_src_code = $reg[1];
860  $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
861  }
862 
863  $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
864  $total_ht = $tabprice[0];
865  $total_tva = $tabprice[1];
866  $total_ttc = $tabprice[2];
867  $total_localtax1 = $tabprice[9];
868  $total_localtax2 = $tabprice[10];
869  $pu_ht = $tabprice[3];
870  $pu_tva = $tabprice[4];
871  $pu_ttc = $tabprice[5];
872 
873  // MultiCurrency
874  $multicurrency_total_ht = $tabprice[16];
875  $multicurrency_total_tva = $tabprice[17];
876  $multicurrency_total_ttc = $tabprice[18];
877  $pu_ht_devise = $tabprice[19];
878 
879  // Anciens indicateurs: $price, $remise (a ne plus utiliser)
880  $price = $pu;
881  $remise = 0;
882  if ($remise_percent > 0) {
883  $remise = round(($pu * $remise_percent / 100), 2);
884  $price = $pu - $remise;
885  }
886 
887  //Fetch current line from the database and then clone the object and set it in $oldline property
888  $line = new PropaleLigne($this->db);
889  $line->fetch($rowid);
890 
891  $staticline = clone $line;
892 
893  $line->oldline = $staticline;
894  $this->line = $line;
895  $this->line->context = $this->context;
896  $this->line->rang = $rang;
897 
898  // Reorder if fk_parent_line change
899  if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
900  $rangmax = $this->line_max($fk_parent_line);
901  $this->line->rang = $rangmax + 1;
902  }
903 
904  $this->line->id = $rowid;
905  $this->line->label = $label;
906  $this->line->desc = $desc;
907  $this->line->qty = $qty;
908  $this->line->product_type = $type;
909  $this->line->vat_src_code = $vat_src_code;
910  $this->line->tva_tx = $txtva;
911  $this->line->localtax1_tx = $txlocaltax1;
912  $this->line->localtax2_tx = $txlocaltax2;
913  $this->line->localtax1_type = $localtaxes_type[0];
914  $this->line->localtax2_type = $localtaxes_type[2];
915  $this->line->remise_percent = $remise_percent;
916  $this->line->subprice = $pu_ht;
917  $this->line->info_bits = $info_bits;
918 
919  $this->line->total_ht = $total_ht;
920  $this->line->total_tva = $total_tva;
921  $this->line->total_localtax1 = $total_localtax1;
922  $this->line->total_localtax2 = $total_localtax2;
923  $this->line->total_ttc = $total_ttc;
924  $this->line->special_code = $special_code;
925  $this->line->fk_parent_line = $fk_parent_line;
926  $this->line->skip_update_total = $skip_update_total;
927  $this->line->fk_unit = $fk_unit;
928 
929  $this->line->fk_fournprice = $fk_fournprice;
930  $this->line->pa_ht = $pa_ht;
931 
932  $this->line->date_start = $date_start;
933  $this->line->date_end = $date_end;
934 
935  // TODO deprecated
936  $this->line->price = $price;
937 
938  if (is_array($array_options) && count($array_options) > 0) {
939  // We replace values in this->line->array_options only for entries defined into $array_options
940  foreach ($array_options as $key => $value) {
941  $this->line->array_options[$key] = $array_options[$key];
942  }
943  }
944 
945  // Multicurrency
946  $this->line->multicurrency_subprice = $pu_ht_devise;
947  $this->line->multicurrency_total_ht = $multicurrency_total_ht;
948  $this->line->multicurrency_total_tva = $multicurrency_total_tva;
949  $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
950 
951  $result = $this->line->update($notrigger);
952  if ($result > 0) {
953  // Reorder if child line
954  if (!empty($fk_parent_line)) {
955  $this->line_order(true, 'DESC');
956  }
957 
958  $this->update_price(1, 'auto');
959 
960  $this->fk_propal = $this->id;
961  $this->rowid = $rowid;
962 
963  $this->db->commit();
964  return $result;
965  } else {
966  $this->error = $this->line->error;
967  $this->errors = $this->line->errors;
968  $this->db->rollback();
969  return -1;
970  }
971  } else {
972  dol_syslog(get_class($this)."::updateline Erreur -2 Propal en mode incompatible pour cette action");
973  return -2;
974  }
975  }
976 
977 
984  public function deleteline($lineid)
985  {
986  global $user;
987 
988  if ($this->statut == self::STATUS_DRAFT) {
989  $this->db->begin();
990 
991  $line = new PropaleLigne($this->db);
992 
993  // For triggers
994  $line->fetch($lineid);
995 
996  if ($line->delete($user) > 0) {
997  $this->update_price(1);
998 
999  $this->db->commit();
1000  return 1;
1001  } else {
1002  $this->error = $line->error;
1003  $this->errors = $line->errors;
1004  $this->db->rollback();
1005  return -1;
1006  }
1007  } else {
1008  $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
1009  return -2;
1010  }
1011  }
1012 
1013 
1022  public function create($user, $notrigger = 0)
1023  {
1024  global $conf, $hookmanager, $mysoc;
1025  $error = 0;
1026 
1027  $now = dol_now();
1028 
1029  // Clean parameters
1030  if (empty($this->date)) {
1031  $this->date = $this->datep;
1032  }
1033  $this->fin_validite = $this->date + ($this->duree_validite * 24 * 3600);
1034  if (empty($this->availability_id)) {
1035  $this->availability_id = 0;
1036  }
1037  if (empty($this->demand_reason_id)) {
1038  $this->demand_reason_id = 0;
1039  }
1040 
1041  // Multicurrency (test on $this->multicurrency_tx because we should take the default rate only if not using origin rate)
1042  if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
1043  list($this->fk_multicurrency, $this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code, $this->date);
1044  } else {
1045  $this->fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $this->multicurrency_code);
1046  }
1047  if (empty($this->fk_multicurrency)) {
1048  $this->multicurrency_code = $conf->currency;
1049  $this->fk_multicurrency = 0;
1050  $this->multicurrency_tx = 1;
1051  }
1052 
1053  // Set tmp vars
1054  $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
1055 
1056  dol_syslog(get_class($this)."::create");
1057 
1058  // Check parameters
1059  $result = $this->fetch_thirdparty();
1060  if ($result < 0) {
1061  $this->error = "Failed to fetch company";
1062  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
1063  return -3;
1064  }
1065 
1066  // Check parameters
1067  if (!empty($this->ref)) { // We check that ref is not already used
1068  $result = self::isExistingObject($this->element, 0, $this->ref); // Check ref is not yet used
1069  if ($result > 0) {
1070  $this->error = 'ErrorRefAlreadyExists';
1071  dol_syslog(get_class($this)."::create ".$this->error, LOG_WARNING);
1072  $this->db->rollback();
1073  return -1;
1074  }
1075  }
1076 
1077  if (empty($this->date)) {
1078  $this->error = "Date of proposal is required";
1079  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
1080  return -4;
1081  }
1082 
1083 
1084  $this->db->begin();
1085 
1086  // Insert into database
1087  $sql = "INSERT INTO ".MAIN_DB_PREFIX."propal (";
1088  $sql .= "fk_soc";
1089  $sql .= ", price";
1090  $sql .= ", remise";
1091  $sql .= ", remise_percent";
1092  $sql .= ", remise_absolue";
1093  $sql .= ", total_tva";
1094  $sql .= ", total_ttc";
1095  $sql .= ", datep";
1096  $sql .= ", datec";
1097  $sql .= ", ref";
1098  $sql .= ", fk_user_author";
1099  $sql .= ", note_private";
1100  $sql .= ", note_public";
1101  $sql .= ", model_pdf";
1102  $sql .= ", fin_validite";
1103  $sql .= ", fk_cond_reglement";
1104  $sql .= ", deposit_percent";
1105  $sql .= ", fk_mode_reglement";
1106  $sql .= ", fk_account";
1107  $sql .= ", ref_client";
1108  $sql .= ", date_livraison";
1109  $sql .= ", fk_shipping_method";
1110  $sql .= ", fk_warehouse";
1111  $sql .= ", fk_availability";
1112  $sql .= ", fk_input_reason";
1113  $sql .= ", fk_projet";
1114  $sql .= ", fk_incoterms";
1115  $sql .= ", location_incoterms";
1116  $sql .= ", entity";
1117  $sql .= ", fk_multicurrency";
1118  $sql .= ", multicurrency_code";
1119  $sql .= ", multicurrency_tx";
1120  $sql .= ") ";
1121  $sql .= " VALUES (";
1122  $sql .= $this->socid;
1123  $sql .= ", 0";
1124  $sql .= ", ".((float) $this->remise); // deprecated
1125  $sql .= ", ".($this->remise_percent ? ((float) $this->remise_percent) : 'NULL');
1126  $sql .= ", ".($this->remise_absolue ? ((float) $this->remise_absolue) : 'NULL'); // deprecated
1127  $sql .= ", 0";
1128  $sql .= ", 0";
1129  $sql .= ", '".$this->db->idate($this->date)."'";
1130  $sql .= ", '".$this->db->idate($now)."'";
1131  $sql .= ", '(PROV)'";
1132  $sql .= ", ".($user->id > 0 ? ((int) $user->id) : "NULL");
1133  $sql .= ", '".$this->db->escape($this->note_private)."'";
1134  $sql .= ", '".$this->db->escape($this->note_public)."'";
1135  $sql .= ", '".$this->db->escape($this->model_pdf)."'";
1136  $sql .= ", ".($this->fin_validite != '' ? "'".$this->db->idate($this->fin_validite)."'" : "NULL");
1137  $sql .= ", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) : 'NULL');
1138  $sql .= ", ".(! empty($this->deposit_percent) ? "'".$this->db->escape($this->deposit_percent)."'" : 'NULL');
1139  $sql .= ", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) : 'NULL');
1140  $sql .= ", ".($this->fk_account > 0 ? ((int) $this->fk_account) : 'NULL');
1141  $sql .= ", '".$this->db->escape($this->ref_client)."'";
1142  $sql .= ", ".(empty($delivery_date) ? "NULL" : "'".$this->db->idate($delivery_date)."'");
1143  $sql .= ", ".($this->shipping_method_id > 0 ? $this->shipping_method_id : 'NULL');
1144  $sql .= ", ".($this->warehouse_id > 0 ? $this->warehouse_id : 'NULL');
1145  $sql .= ", ".$this->availability_id;
1146  $sql .= ", ".$this->demand_reason_id;
1147  $sql .= ", ".($this->fk_project ? $this->fk_project : "null");
1148  $sql .= ", ".(int) $this->fk_incoterms;
1149  $sql .= ", '".$this->db->escape($this->location_incoterms)."'";
1150  $sql .= ", ".setEntity($this);
1151  $sql .= ", ".(int) $this->fk_multicurrency;
1152  $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
1153  $sql .= ", ".(double) $this->multicurrency_tx;
1154  $sql .= ")";
1155 
1156  dol_syslog(get_class($this)."::create", LOG_DEBUG);
1157  $resql = $this->db->query($sql);
1158  if ($resql) {
1159  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."propal");
1160 
1161  if ($this->id) {
1162  $this->ref = '(PROV'.$this->id.')';
1163  $sql = 'UPDATE '.MAIN_DB_PREFIX."propal SET ref='".$this->db->escape($this->ref)."' WHERE rowid=".((int) $this->id);
1164 
1165  dol_syslog(get_class($this)."::create", LOG_DEBUG);
1166  $resql = $this->db->query($sql);
1167  if (!$resql) {
1168  $error++;
1169  }
1170 
1171  if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
1172  $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
1173  }
1174 
1175  // Add object linked
1176  if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1177  foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1178  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, ...))
1179  foreach ($tmp_origin_id as $origin_id) {
1180  $ret = $this->add_object_linked($origin, $origin_id);
1181  if (!$ret) {
1182  $this->error = $this->db->lasterror();
1183  $error++;
1184  }
1185  }
1186  } else // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
1187  {
1188  $origin_id = $tmp_origin_id;
1189  $ret = $this->add_object_linked($origin, $origin_id);
1190  if (!$ret) {
1191  $this->error = $this->db->lasterror();
1192  $error++;
1193  }
1194  }
1195  }
1196  }
1197 
1198  /*
1199  * Insertion du detail des produits dans la base
1200  * Insert products detail in database
1201  */
1202  if (!$error) {
1203  $fk_parent_line = 0;
1204  $num = count($this->lines);
1205 
1206  for ($i = 0; $i < $num; $i++) {
1207  if (!is_object($this->lines[$i])) { // If this->lines is not array of objects, coming from REST API
1208  // Convert into object this->lines[$i].
1209  $line = (object) $this->lines[$i];
1210  } else {
1211  $line = $this->lines[$i];
1212  }
1213  // Reset fk_parent_line for line that are not child lines or special product
1214  if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
1215  $fk_parent_line = 0;
1216  }
1217  // Complete vat rate with code
1218  $vatrate = $line->tva_tx;
1219  if ($line->vat_src_code && !preg_match('/\(.*\)/', $vatrate)) {
1220  $vatrate .= ' ('.$line->vat_src_code.')';
1221  }
1222 
1223  if (!empty($conf->global->MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION)) {
1224  $originid = $line->origin_id;
1225  $origintype = $line->origin;
1226  } else {
1227  $originid = $line->id;
1228  $origintype = $this->element;
1229  }
1230 
1231  $result = $this->addline(
1232  $line->desc,
1233  $line->subprice,
1234  $line->qty,
1235  $vatrate,
1236  $line->localtax1_tx,
1237  $line->localtax2_tx,
1238  $line->fk_product,
1239  $line->remise_percent,
1240  'HT',
1241  0,
1242  $line->info_bits,
1243  $line->product_type,
1244  $line->rang,
1245  $line->special_code,
1246  $fk_parent_line,
1247  $line->fk_fournprice,
1248  $line->pa_ht,
1249  $line->label,
1250  $line->date_start,
1251  $line->date_end,
1252  $line->array_options,
1253  $line->fk_unit,
1254  $origintype,
1255  $originid,
1256  0,
1257  0,
1258  1
1259  );
1260 
1261  if ($result < 0) {
1262  $error++;
1263  $this->error = $this->db->error;
1264  dol_print_error($this->db);
1265  break;
1266  }
1267 
1268  // Set the id on created row
1269  $line->id = $result;
1270 
1271  // Defined the new fk_parent_line
1272  if ($result > 0 && $line->product_type == 9) {
1273  $fk_parent_line = $result;
1274  }
1275  }
1276  }
1277 
1278  // Set delivery address
1279  /*if (! $error && $this->fk_delivery_address)
1280  {
1281  $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
1282  $sql.= " SET fk_delivery_address = ".((int) $this->fk_delivery_address);
1283  $sql.= " WHERE ref = '".$this->db->escape($this->ref)."'";
1284  $sql.= " AND entity = ".setEntity($this);
1285 
1286  $result=$this->db->query($sql);
1287  }*/
1288 
1289  if (!$error) {
1290  // Mise a jour infos denormalisees
1291  $resql = $this->update_price(1, 'auto', 0, $mysoc);
1292  if ($resql) {
1293  $action = 'update';
1294 
1295  // Actions on extra fields
1296  if (!$error) {
1297  $result = $this->insertExtraFields();
1298  if ($result < 0) {
1299  $error++;
1300  }
1301  }
1302 
1303  if (!$error && !$notrigger) {
1304  // Call trigger
1305  $result = $this->call_trigger('PROPAL_CREATE', $user);
1306  if ($result < 0) {
1307  $error++;
1308  }
1309  // End call triggers
1310  }
1311  } else {
1312  $this->error = $this->db->lasterror();
1313  $error++;
1314  }
1315  }
1316  } else {
1317  $this->error = $this->db->lasterror();
1318  $error++;
1319  }
1320 
1321  if (!$error) {
1322  $this->db->commit();
1323  dol_syslog(get_class($this)."::create done id=".$this->id);
1324  return $this->id;
1325  } else {
1326  $this->db->rollback();
1327  return -2;
1328  }
1329  } else {
1330  $this->error = $this->db->lasterror();
1331  $this->db->rollback();
1332  return -1;
1333  }
1334  }
1335 
1345  public function createFromClone(User $user, $socid = 0, $forceentity = null, $update_prices = false)
1346  {
1347  global $conf, $hookmanager, $mysoc;
1348 
1349  dol_include_once('/projet/class/project.class.php');
1350 
1351  $error = 0;
1352  $now = dol_now();
1353 
1354  dol_syslog(__METHOD__, LOG_DEBUG);
1355 
1356  $object = new self($this->db);
1357 
1358  $this->db->begin();
1359 
1360  // Load source object
1361  $object->fetch($this->id);
1362 
1363  $objsoc = new Societe($this->db);
1364 
1365  // Change socid if needed
1366  if (!empty($socid) && $socid != $object->socid) {
1367  if ($objsoc->fetch($socid) > 0) {
1368  $object->socid = $objsoc->id;
1369  $object->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1370  $object->deposit_percent = (!empty($objsoc->deposit_percent) ? $objsoc->deposit_percent : null);
1371  $object->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1372  $object->fk_delivery_address = '';
1373 
1374  /*if (!empty($conf->project->enabled))
1375  {
1376  $project = new Project($db);
1377  if ($this->fk_project > 0 && $project->fetch($this->fk_project)) {
1378  if ($project->socid <= 0) $clonedObj->fk_project = $this->fk_project;
1379  else $clonedObj->fk_project = '';
1380  } else {
1381  $clonedObj->fk_project = '';
1382  }
1383  }*/
1384  $object->fk_project = ''; // A cloned proposal is set by default to no project.
1385  }
1386 
1387  // reset ref_client
1388  $object->ref_client = '';
1389 
1390  // TODO Change product price if multi-prices
1391  } else {
1392  $objsoc->fetch($object->socid);
1393  }
1394 
1395  // update prices
1396  if ($update_prices === true) {
1397  if ($objsoc->id > 0 && !empty($object->lines)) {
1398  if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
1399  // If price per customer
1400  require_once DOL_DOCUMENT_ROOT . '/product/class/productcustomerprice.class.php';
1401  }
1402 
1403  foreach ($object->lines as $line) {
1404  $line->id = 0;
1405 
1406  if ($line->fk_product > 0) {
1407  $prod = new Product($this->db);
1408  $res = $prod->fetch($line->fk_product);
1409  if ($res > 0) {
1410  $pu_ht = $prod->price;
1411  $tva_tx = get_default_tva($mysoc, $objsoc, $prod->id);
1412  $remise_percent = $objsoc->remise_percent;
1413 
1414  if (!empty($conf->global->PRODUIT_MULTIPRICES) && $objsoc->price_level > 0) {
1415  $pu_ht = $prod->multiprices[$objsoc->price_level];
1416  if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) { // using this option is a bug. kept for backward compatibility
1417  if (isset($prod->multiprices_tva_tx[$objsoc->price_level])) {
1418  $tva_tx = $prod->multiprices_tva_tx[$objsoc->price_level];
1419  }
1420  }
1421  } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
1422  $prodcustprice = new Productcustomerprice($this->db);
1423  $filter = array('t.fk_product' => $prod->id, 't.fk_soc' => $objsoc->id);
1424  $result = $prodcustprice->fetch_all('', '', 0, 0, $filter);
1425  if ($result) {
1426  // If there is some prices specific to the customer
1427  if (count($prodcustprice->lines) > 0) {
1428  $pu_ht = price($prodcustprice->lines[0]->price);
1429  $tva_tx = ($prodcustprice->lines[0]->default_vat_code ? $prodcustprice->lines[0]->tva_tx.' ('.$prodcustprice->lines[0]->default_vat_code.' )' : $prodcustprice->lines[0]->tva_tx);
1430  if ($prodcustprice->lines[0]->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) {
1431  $tva_tx .= ' ('.$prodcustprice->lines[0]->default_vat_code.')';
1432  }
1433  }
1434  }
1435  }
1436 
1437  $line->subprice = $pu_ht;
1438  $line->tva_tx = $tva_tx;
1439  $line->remise_percent = $remise_percent;
1440  }
1441  }
1442  }
1443  }
1444  }
1445 
1446  $object->id = 0;
1447  $object->ref = '';
1448  $object->entity = (!empty($forceentity) ? $forceentity : $object->entity);
1449  $object->statut = self::STATUS_DRAFT;
1450 
1451  // Clear fields
1452  $object->user_author = $user->id;
1453  $object->user_valid = 0;
1454  $object->date = $now;
1455  $object->datep = $now; // deprecated
1456  $object->fin_validite = $object->date + ($object->duree_validite * 24 * 3600);
1457  if (empty($conf->global->MAIN_KEEP_REF_CUSTOMER_ON_CLONING)) {
1458  $object->ref_client = '';
1459  }
1460  if ($conf->global->MAIN_DONT_KEEP_NOTE_ON_CLONING == 1) {
1461  $object->note_private = '';
1462  $object->note_public = '';
1463  }
1464  // Create clone
1465  $object->context['createfromclone'] = 'createfromclone';
1466  $result = $object->create($user);
1467  if ($result < 0) {
1468  $this->error = $object->error;
1469  $this->errors = array_merge($this->errors, $object->errors);
1470  $error++;
1471  }
1472 
1473  if (!$error) {
1474  // copy internal contacts
1475  if ($object->copy_linked_contact($this, 'internal') < 0) {
1476  $error++;
1477  }
1478  }
1479 
1480  if (!$error) {
1481  // copy external contacts if same company
1482  if ($this->socid == $object->socid) {
1483  if ($object->copy_linked_contact($this, 'external') < 0) {
1484  $error++;
1485  }
1486  }
1487  }
1488 
1489  if (!$error) {
1490  // Hook of thirdparty module
1491  if (is_object($hookmanager)) {
1492  $parameters = array('objFrom'=>$this, 'clonedObj'=>$object);
1493  $action = '';
1494  $reshook = $hookmanager->executeHooks('createFrom', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
1495  if ($reshook < 0) {
1496  $this->errors += $hookmanager->errors;
1497  $this->error = $hookmanager->error;
1498  $error++;
1499  }
1500  }
1501  }
1502 
1503  unset($object->context['createfromclone']);
1504 
1505  // End
1506  if (!$error) {
1507  $this->db->commit();
1508  return $object->id;
1509  } else {
1510  $this->db->rollback();
1511  return -1;
1512  }
1513  }
1514 
1524  public function fetch($rowid, $ref = '', $ref_ext = '', $forceentity = 0)
1525  {
1526  $sql = "SELECT p.rowid, p.ref, p.entity, p.remise, p.remise_percent, p.remise_absolue, p.fk_soc";
1527  $sql .= ", p.total_ttc, p.total_tva, p.localtax1, p.localtax2, p.total_ht";
1528  $sql .= ", p.datec";
1529  $sql .= ", p.date_signature as dates";
1530  $sql .= ", p.date_valid as datev";
1531  $sql .= ", p.datep as dp";
1532  $sql .= ", p.fin_validite as dfv";
1533  $sql .= ", p.date_livraison as delivery_date";
1534  $sql .= ", p.model_pdf, p.last_main_doc, p.ref_client, p.extraparams";
1535  $sql .= ", p.note_private, p.note_public";
1536  $sql .= ", p.fk_projet as fk_project, p.fk_statut";
1537  $sql .= ", p.fk_user_author, p.fk_user_valid, p.fk_user_cloture";
1538  $sql .= ", p.fk_delivery_address";
1539  $sql .= ", p.fk_availability";
1540  $sql .= ", p.fk_input_reason";
1541  $sql .= ", p.fk_cond_reglement";
1542  $sql .= ", p.fk_mode_reglement";
1543  $sql .= ', p.fk_account';
1544  $sql .= ", p.fk_shipping_method";
1545  $sql .= ", p.fk_warehouse";
1546  $sql .= ", p.fk_incoterms, p.location_incoterms";
1547  $sql .= ", p.fk_multicurrency, p.multicurrency_code, p.multicurrency_tx, p.multicurrency_total_ht, p.multicurrency_total_tva, p.multicurrency_total_ttc";
1548  $sql .= ", p.tms as date_modification";
1549  $sql .= ", i.libelle as label_incoterms";
1550  $sql .= ", c.label as statut_label";
1551  $sql .= ", ca.code as availability_code, ca.label as availability";
1552  $sql .= ", dr.code as demand_reason_code, dr.label as demand_reason";
1553  $sql .= ", cr.code as cond_reglement_code, cr.libelle as cond_reglement, cr.libelle_facture as cond_reglement_libelle_doc, p.deposit_percent";
1554  $sql .= ", cp.code as mode_reglement_code, cp.libelle as mode_reglement";
1555  $sql .= " FROM ".MAIN_DB_PREFIX."propal as p";
1556  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_propalst as c ON p.fk_statut = c.id';
1557  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as cp ON p.fk_mode_reglement = cp.id AND cp.entity IN ('.getEntity('c_paiement').')';
1558  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_payment_term as cr ON p.fk_cond_reglement = cr.rowid AND cr.entity IN ('.getEntity('c_payment_term').')';
1559  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_availability as ca ON p.fk_availability = ca.rowid';
1560  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_input_reason as dr ON p.fk_input_reason = dr.rowid';
1561  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_incoterms as i ON p.fk_incoterms = i.rowid';
1562 
1563  if (!empty($ref)) {
1564  if (!empty($forceentity)) {
1565  $sql .= " WHERE p.entity = ".(int) $forceentity; // Check only the current entity because we may have the same reference in several entities
1566  } else {
1567  $sql .= " WHERE p.entity IN (".getEntity('propal').")";
1568  }
1569  $sql .= " AND p.ref='".$this->db->escape($ref)."'";
1570  } else {
1571  // Dont't use entity if you use rowid
1572  $sql .= " WHERE p.rowid = ".((int) $rowid);
1573  }
1574 
1575  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1576  $resql = $this->db->query($sql);
1577  if ($resql) {
1578  if ($this->db->num_rows($resql)) {
1579  $obj = $this->db->fetch_object($resql);
1580 
1581  $this->id = $obj->rowid;
1582  $this->entity = $obj->entity;
1583 
1584  $this->ref = $obj->ref;
1585  $this->ref_client = $obj->ref_client;
1586  $this->remise = $obj->remise;
1587  $this->remise_percent = $obj->remise_percent;
1588  $this->remise_absolue = $obj->remise_absolue;
1589  $this->total = $obj->total_ttc; // TODO deprecated
1590  $this->total_ttc = $obj->total_ttc;
1591  $this->total_ht = $obj->total_ht;
1592  $this->total_tva = $obj->total_tva;
1593  $this->total_localtax1 = $obj->localtax1;
1594  $this->total_localtax2 = $obj->localtax2;
1595 
1596  $this->socid = $obj->fk_soc;
1597  $this->thirdparty = null; // Clear if another value was already set by fetch_thirdparty
1598 
1599  $this->fk_project = $obj->fk_project;
1600  $this->project = null; // Clear if another value was already set by fetch_projet
1601 
1602  $this->model_pdf = $obj->model_pdf;
1603  $this->modelpdf = $obj->model_pdf; // deprecated
1604  $this->last_main_doc = $obj->last_main_doc;
1605  $this->note = $obj->note_private; // TODO deprecated
1606  $this->note_private = $obj->note_private;
1607  $this->note_public = $obj->note_public;
1608 
1609  $this->status = (int) $obj->fk_statut;
1610  $this->statut = $this->status; // deprecated
1611  $this->statut_libelle = $obj->statut_label;
1612 
1613  $this->datec = $this->db->jdate($obj->datec); // TODO deprecated
1614  $this->datev = $this->db->jdate($obj->datev); // TODO deprecated
1615  $this->date_creation = $this->db->jdate($obj->datec); //Creation date
1616  $this->date_validation = $this->db->jdate($obj->datev); //Validation date
1617  $this->date_modification = $this->db->jdate($obj->date_modification); // tms
1618  $this->date_signature = $this->db->jdate($obj->dates); // Signature date
1619  $this->date = $this->db->jdate($obj->dp); // Proposal date
1620  $this->datep = $this->db->jdate($obj->dp); // deprecated
1621  $this->fin_validite = $this->db->jdate($obj->dfv);
1622  $this->date_livraison = $this->db->jdate($obj->delivery_date); // deprecated
1623  $this->delivery_date = $this->db->jdate($obj->delivery_date);
1624  $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method : null;
1625  $this->warehouse_id = ($obj->fk_warehouse > 0) ? $obj->fk_warehouse : null;
1626  $this->availability_id = $obj->fk_availability;
1627  $this->availability_code = $obj->availability_code;
1628  $this->availability = $obj->availability;
1629  $this->demand_reason_id = $obj->fk_input_reason;
1630  $this->demand_reason_code = $obj->demand_reason_code;
1631  $this->demand_reason = $obj->demand_reason;
1632  $this->fk_address = $obj->fk_delivery_address;
1633 
1634  $this->mode_reglement_id = $obj->fk_mode_reglement;
1635  $this->mode_reglement_code = $obj->mode_reglement_code;
1636  $this->mode_reglement = $obj->mode_reglement;
1637  $this->fk_account = ($obj->fk_account > 0) ? $obj->fk_account : null;
1638  $this->cond_reglement_id = $obj->fk_cond_reglement;
1639  $this->cond_reglement_code = $obj->cond_reglement_code;
1640  $this->cond_reglement = $obj->cond_reglement;
1641  $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
1642  $this->deposit_percent = $obj->deposit_percent;
1643 
1644  $this->extraparams = (array) json_decode($obj->extraparams, true);
1645 
1646  $this->user_author_id = $obj->fk_user_author;
1647  $this->user_valid_id = $obj->fk_user_valid;
1648  $this->user_close_id = $obj->fk_user_cloture;
1649 
1650  //Incoterms
1651  $this->fk_incoterms = $obj->fk_incoterms;
1652  $this->location_incoterms = $obj->location_incoterms;
1653  $this->label_incoterms = $obj->label_incoterms;
1654 
1655  // Multicurrency
1656  $this->fk_multicurrency = $obj->fk_multicurrency;
1657  $this->multicurrency_code = $obj->multicurrency_code;
1658  $this->multicurrency_tx = $obj->multicurrency_tx;
1659  $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1660  $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1661  $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1662 
1663  if ($obj->fk_statut == self::STATUS_DRAFT) {
1664  $this->brouillon = 1;
1665  }
1666 
1667  // Retrieve all extrafield
1668  // fetch optionals attributes and labels
1669  $this->fetch_optionals();
1670 
1671  $this->db->free($resql);
1672 
1673  $this->lines = array();
1674 
1675  // Lines
1676  $result = $this->fetch_lines();
1677  if ($result < 0) {
1678  return -3;
1679  }
1680 
1681  return 1;
1682  }
1683 
1684  $this->error = "Record Not Found";
1685  return 0;
1686  } else {
1687  $this->error = $this->db->lasterror();
1688  return -1;
1689  }
1690  }
1691 
1699  public function update(User $user, $notrigger = 0)
1700  {
1701  global $conf;
1702 
1703  $error = 0;
1704 
1705  // Clean parameters
1706  if (isset($this->ref)) {
1707  $this->ref = trim($this->ref);
1708  }
1709  if (isset($this->ref_client)) {
1710  $this->ref_client = trim($this->ref_client);
1711  }
1712  if (isset($this->note) || isset($this->note_private)) {
1713  $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note));
1714  }
1715  if (isset($this->note_public)) {
1716  $this->note_public = trim($this->note_public);
1717  }
1718  if (isset($this->model_pdf)) {
1719  $this->model_pdf = trim($this->model_pdf);
1720  }
1721  if (isset($this->import_key)) {
1722  $this->import_key = trim($this->import_key);
1723  }
1724  if (!empty($this->duree_validite) && is_numeric($this->duree_validite)) {
1725  $this->fin_validite = $this->date + ($this->duree_validite * 24 * 3600);
1726  }
1727 
1728  // Check parameters
1729  // Put here code to add control on parameters values
1730 
1731  // Update request
1732  $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET";
1733  $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").",";
1734  $sql .= " ref_client=".(isset($this->ref_client) ? "'".$this->db->escape($this->ref_client)."'" : "null").",";
1735  $sql .= " ref_ext=".(isset($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "null").",";
1736  $sql .= " fk_soc=".(isset($this->socid) ? $this->socid : "null").",";
1737  $sql .= " datep=".(strval($this->date) != '' ? "'".$this->db->idate($this->date)."'" : 'null').",";
1738  if (!empty($this->fin_validite)) {
1739  $sql .= " fin_validite=".(strval($this->fin_validite) != '' ? "'".$this->db->idate($this->fin_validite)."'" : 'null').",";
1740  }
1741  $sql .= " date_valid=".(strval($this->date_validation) != '' ? "'".$this->db->idate($this->date_validation)."'" : 'null').",";
1742  $sql .= " total_tva=".(isset($this->total_tva) ? $this->total_tva : "null").",";
1743  $sql .= " localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 : "null").",";
1744  $sql .= " localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 : "null").",";
1745  $sql .= " total_ht=".(isset($this->total_ht) ? $this->total_ht : "null").",";
1746  $sql .= " total_ttc=".(isset($this->total_ttc) ? $this->total_ttc : "null").",";
1747  $sql .= " fk_statut=".(isset($this->statut) ? $this->statut : "null").",";
1748  $sql .= " fk_user_author=".(isset($this->user_author_id) ? $this->user_author_id : "null").",";
1749  $sql .= " fk_user_valid=".(isset($this->user_valid) ? $this->user_valid : "null").",";
1750  $sql .= " fk_projet=".(isset($this->fk_project) ? $this->fk_project : "null").",";
1751  $sql .= " fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id : "null").",";
1752  $sql .= " deposit_percent=".(! empty($this->deposit_percent) ? "'".$this->db->escape($this->deposit_percent)."'" : "null").",";
1753  $sql .= " fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id : "null").",";
1754  $sql .= " fk_input_reason=".(isset($this->demand_reason_id) ? $this->demand_reason_id : "null").",";
1755  $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
1756  $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
1757  $sql .= " model_pdf=".(isset($this->model_pdf) ? "'".$this->db->escape($this->model_pdf)."'" : "null").",";
1758  $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null")."";
1759  $sql .= " WHERE rowid=".((int) $this->id);
1760 
1761  $this->db->begin();
1762 
1763  dol_syslog(get_class($this)."::update", LOG_DEBUG);
1764  $resql = $this->db->query($sql);
1765  if (!$resql) {
1766  $error++;
1767  $this->errors[] = "Error ".$this->db->lasterror();
1768  }
1769 
1770  if (!$error) {
1771  $result = $this->insertExtraFields();
1772  if ($result < 0) {
1773  $error++;
1774  }
1775  }
1776 
1777  if (!$error && !$notrigger) {
1778  // Call trigger
1779  $result = $this->call_trigger('PROPAL_MODIFY', $user);
1780  if ($result < 0) {
1781  $error++;
1782  }
1783  // End call triggers
1784  }
1785 
1786  // Commit or rollback
1787  if ($error) {
1788  foreach ($this->errors as $errmsg) {
1789  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
1790  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1791  }
1792  $this->db->rollback();
1793  return -1 * $error;
1794  } else {
1795  $this->db->commit();
1796  return 1;
1797  }
1798  }
1799 
1800 
1801  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1811  public function fetch_lines($only_product = 0, $loadalsotranslation = 0, $filters = '')
1812  {
1813  // phpcs:enable
1814  global $langs, $conf;
1815 
1816  $this->lines = array();
1817 
1818  $sql = 'SELECT d.rowid, d.fk_propal, d.fk_parent_line, d.label as custom_label, d.description, d.price, d.vat_src_code, d.tva_tx, d.localtax1_tx, d.localtax2_tx, d.localtax1_type, d.localtax2_type, d.qty, d.fk_remise_except, d.remise_percent, d.subprice, d.fk_product,';
1819  $sql .= ' d.info_bits, d.total_ht, d.total_tva, d.total_localtax1, d.total_localtax2, d.total_ttc, d.fk_product_fournisseur_price as fk_fournprice, d.buy_price_ht as pa_ht, d.special_code, d.rang, d.product_type,';
1820  $sql .= ' d.fk_unit,';
1821  $sql .= ' p.ref as product_ref, p.description as product_desc, p.fk_product_type, p.label as product_label, p.tobatch as product_tobatch, p.barcode as product_barcode,';
1822  $sql .= ' p.weight, p.weight_units, p.volume, p.volume_units,';
1823  $sql .= ' d.date_start, d.date_end,';
1824  $sql .= ' d.fk_multicurrency, d.multicurrency_code, d.multicurrency_subprice, d.multicurrency_total_ht, d.multicurrency_total_tva, d.multicurrency_total_ttc';
1825  $sql .= ' FROM '.MAIN_DB_PREFIX.'propaldet as d';
1826  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON (d.fk_product = p.rowid)';
1827  $sql .= ' WHERE d.fk_propal = '.((int) $this->id);
1828  if ($only_product) {
1829  $sql .= ' AND p.fk_product_type = 0';
1830  }
1831  if ($filters) {
1832  $sql .= $filters;
1833  }
1834  $sql .= ' ORDER by d.rang';
1835 
1836  dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
1837  $result = $this->db->query($sql);
1838  if ($result) {
1839  require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
1840 
1841  $num = $this->db->num_rows($result);
1842 
1843  $i = 0;
1844  while ($i < $num) {
1845  $objp = $this->db->fetch_object($result);
1846 
1847  $line = new PropaleLigne($this->db);
1848 
1849  $line->rowid = $objp->rowid; //Deprecated
1850  $line->id = $objp->rowid;
1851  $line->fk_propal = $objp->fk_propal;
1852  $line->fk_parent_line = $objp->fk_parent_line;
1853  $line->product_type = $objp->product_type;
1854  $line->label = $objp->custom_label;
1855  $line->desc = $objp->description; // Description ligne
1856  $line->description = $objp->description; // Description ligne
1857  $line->qty = $objp->qty;
1858  $line->vat_src_code = $objp->vat_src_code;
1859  $line->tva_tx = $objp->tva_tx;
1860  $line->localtax1_tx = $objp->localtax1_tx;
1861  $line->localtax2_tx = $objp->localtax2_tx;
1862  $line->localtax1_type = $objp->localtax1_type;
1863  $line->localtax2_type = $objp->localtax2_type;
1864  $line->subprice = $objp->subprice;
1865  $line->fk_remise_except = $objp->fk_remise_except;
1866  $line->remise_percent = $objp->remise_percent;
1867  $line->price = $objp->price; // TODO deprecated
1868 
1869  $line->info_bits = $objp->info_bits;
1870  $line->total_ht = $objp->total_ht;
1871  $line->total_tva = $objp->total_tva;
1872  $line->total_localtax1 = $objp->total_localtax1;
1873  $line->total_localtax2 = $objp->total_localtax2;
1874  $line->total_ttc = $objp->total_ttc;
1875  $line->fk_fournprice = $objp->fk_fournprice;
1876  $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
1877  $line->pa_ht = $marginInfos[0];
1878  $line->marge_tx = $marginInfos[1];
1879  $line->marque_tx = $marginInfos[2];
1880  $line->special_code = $objp->special_code;
1881  $line->rang = $objp->rang;
1882 
1883  $line->fk_product = $objp->fk_product;
1884 
1885  $line->ref = $objp->product_ref; // deprecated
1886  $line->libelle = $objp->product_label; // deprecated
1887 
1888  $line->product_ref = $objp->product_ref;
1889  $line->product_label = $objp->product_label;
1890  $line->product_desc = $objp->product_desc; // Description produit
1891  $line->product_tobatch = $objp->product_tobatch;
1892  $line->product_barcode = $objp->product_barcode;
1893 
1894  $line->fk_product_type = $objp->fk_product_type; // deprecated
1895  $line->fk_unit = $objp->fk_unit;
1896  $line->weight = $objp->weight;
1897  $line->weight_units = $objp->weight_units;
1898  $line->volume = $objp->volume;
1899  $line->volume_units = $objp->volume_units;
1900 
1901  $line->date_start = $this->db->jdate($objp->date_start);
1902  $line->date_end = $this->db->jdate($objp->date_end);
1903 
1904  // Multicurrency
1905  $line->fk_multicurrency = $objp->fk_multicurrency;
1906  $line->multicurrency_code = $objp->multicurrency_code;
1907  $line->multicurrency_subprice = $objp->multicurrency_subprice;
1908  $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
1909  $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
1910  $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
1911 
1912  $line->fetch_optionals();
1913 
1914  // multilangs
1915  if (!empty($conf->global->MAIN_MULTILANGS) && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
1916  $tmpproduct = new Product($this->db);
1917  $tmpproduct->fetch($objp->fk_product);
1918  $tmpproduct->getMultiLangs();
1919 
1920  $line->multilangs = $tmpproduct->multilangs;
1921  }
1922 
1923  $this->lines[$i] = $line;
1924 
1925  $i++;
1926  }
1927 
1928  $this->db->free($result);
1929 
1930  return $num;
1931  } else {
1932  $this->error = $this->db->lasterror();
1933  return -3;
1934  }
1935  }
1936 
1944  public function valid($user, $notrigger = 0)
1945  {
1946  global $conf;
1947 
1948  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1949 
1950  $error = 0;
1951 
1952  // Protection
1953  if ($this->statut == self::STATUS_VALIDATED) {
1954  dol_syslog(get_class($this)."::valid action abandonned: already validated", LOG_WARNING);
1955  return 0;
1956  }
1957 
1958  if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->propal->creer))
1959  || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->propal->propal_advance->validate)))) {
1960  $this->error = 'ErrorPermissionDenied';
1961  dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
1962  return -1;
1963  }
1964 
1965  $now = dol_now();
1966 
1967  $this->db->begin();
1968 
1969  // Numbering module definition
1970  $soc = new Societe($this->db);
1971  $soc->fetch($this->socid);
1972 
1973  // Define new ref
1974  if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
1975  $num = $this->getNextNumRef($soc);
1976  } else {
1977  $num = $this->ref;
1978  }
1979  $this->newref = dol_sanitizeFileName($num);
1980 
1981  $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
1982  $sql .= " SET ref = '".$this->db->escape($num)."',";
1983  $sql .= " fk_statut = ".self::STATUS_VALIDATED.", date_valid='".$this->db->idate($now)."', fk_user_valid=".((int) $user->id);
1984  $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = ".self::STATUS_DRAFT;
1985 
1986  dol_syslog(get_class($this)."::valid", LOG_DEBUG);
1987  $resql = $this->db->query($sql);
1988  if (!$resql) {
1989  dol_print_error($this->db);
1990  $error++;
1991  }
1992 
1993  // Trigger calls
1994  if (!$error && !$notrigger) {
1995  // Call trigger
1996  $result = $this->call_trigger('PROPAL_VALIDATE', $user);
1997  if ($result < 0) {
1998  $error++;
1999  }
2000  // End call triggers
2001  }
2002 
2003  if (!$error) {
2004  $this->oldref = $this->ref;
2005 
2006  // Rename directory if dir was a temporary ref
2007  if (preg_match('/^[\(]?PROV/i', $this->ref)) {
2008  // Now we rename also files into index
2009  $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'propale/".$this->db->escape($this->newref)."'";
2010  $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'propale/".$this->db->escape($this->ref)."' and entity = ".((int) $conf->entity);
2011  $resql = $this->db->query($sql);
2012  if (!$resql) {
2013  $error++;
2014  $this->error = $this->db->lasterror();
2015  }
2016 
2017  // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
2018  $oldref = dol_sanitizeFileName($this->ref);
2019  $newref = dol_sanitizeFileName($num);
2020  $dirsource = $conf->propal->multidir_output[$this->entity].'/'.$oldref;
2021  $dirdest = $conf->propal->multidir_output[$this->entity].'/'.$newref;
2022  if (!$error && file_exists($dirsource)) {
2023  dol_syslog(get_class($this)."::validate rename dir ".$dirsource." into ".$dirdest);
2024  if (@rename($dirsource, $dirdest)) {
2025  dol_syslog("Rename ok");
2026  // Rename docs starting with $oldref with $newref
2027  $listoffiles = dol_dir_list($dirdest, 'files', 1, '^'.preg_quote($oldref, '/'));
2028  foreach ($listoffiles as $fileentry) {
2029  $dirsource = $fileentry['name'];
2030  $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
2031  $dirsource = $fileentry['path'].'/'.$dirsource;
2032  $dirdest = $fileentry['path'].'/'.$dirdest;
2033  @rename($dirsource, $dirdest);
2034  }
2035  }
2036  }
2037  }
2038 
2039  $this->ref = $num;
2040  $this->brouillon = 0;
2041  $this->statut = self::STATUS_VALIDATED;
2042  $this->user_valid_id = $user->id;
2043  $this->datev = $now;
2044 
2045  $this->db->commit();
2046  return 1;
2047  } else {
2048  $this->db->rollback();
2049  return -1;
2050  }
2051  }
2052 
2053 
2054  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2063  public function set_date($user, $date, $notrigger = 0)
2064  {
2065  // phpcs:enable
2066  if (empty($date)) {
2067  $this->error = 'ErrorBadParameter';
2068  dol_syslog(get_class($this)."::set_date ".$this->error, LOG_ERR);
2069  return -1;
2070  }
2071 
2072  if (!empty($user->rights->propal->creer)) {
2073  $error = 0;
2074 
2075  $this->db->begin();
2076 
2077  $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET datep = '".$this->db->idate($date)."'";
2078  $sql .= " WHERE rowid = ".((int) $this->id);
2079 
2080  dol_syslog(__METHOD__, LOG_DEBUG);
2081  $resql = $this->db->query($sql);
2082  if (!$resql) {
2083  $this->errors[] = $this->db->error();
2084  $error++;
2085  }
2086 
2087  if (!$error) {
2088  $this->oldcopy = clone $this;
2089  $this->date = $date;
2090  $this->datep = $date; // deprecated
2091  }
2092 
2093  if (!$notrigger && empty($error)) {
2094  // Call trigger
2095  $result = $this->call_trigger('PROPAL_MODIFY', $user);
2096  if ($result < 0) {
2097  $error++;
2098  }
2099  // End call triggers
2100  }
2101 
2102  if (!$error) {
2103  $this->db->commit();
2104  return 1;
2105  } else {
2106  foreach ($this->errors as $errmsg) {
2107  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
2108  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2109  }
2110  $this->db->rollback();
2111  return -1 * $error;
2112  }
2113  }
2114  }
2115 
2116  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2125  public function set_echeance($user, $date_fin_validite, $notrigger = 0)
2126  {
2127  // phpcs:enable
2128  if (!empty($user->rights->propal->creer)) {
2129  $error = 0;
2130 
2131  $this->db->begin();
2132 
2133  $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fin_validite = ".($date_fin_validite != '' ? "'".$this->db->idate($date_fin_validite)."'" : 'null');
2134  $sql .= " WHERE rowid = ".((int) $this->id);
2135 
2136  dol_syslog(__METHOD__, LOG_DEBUG);
2137  $resql = $this->db->query($sql);
2138  if (!$resql) {
2139  $this->errors[] = $this->db->error();
2140  $error++;
2141  }
2142 
2143 
2144  if (!$error) {
2145  $this->oldcopy = clone $this;
2146  $this->fin_validite = $date_fin_validite;
2147  }
2148 
2149  if (!$notrigger && empty($error)) {
2150  // Call trigger
2151  $result = $this->call_trigger('PROPAL_MODIFY', $user);
2152  if ($result < 0) {
2153  $error++;
2154  }
2155  // End call triggers
2156  }
2157 
2158  if (!$error) {
2159  $this->db->commit();
2160  return 1;
2161  } else {
2162  foreach ($this->errors as $errmsg) {
2163  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
2164  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2165  }
2166  $this->db->rollback();
2167  return -1 * $error;
2168  }
2169  }
2170  }
2171 
2172  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2182  public function set_date_livraison($user, $delivery_date, $notrigger = 0)
2183  {
2184  // phpcs:enable
2185  return $this->setDeliveryDate($user, $delivery_date, $notrigger);
2186  }
2187 
2196  public function setDeliveryDate($user, $delivery_date, $notrigger = 0)
2197  {
2198  if (!empty($user->rights->propal->creer)) {
2199  $error = 0;
2200 
2201  $this->db->begin();
2202 
2203  $sql = "UPDATE ".MAIN_DB_PREFIX."propal ";
2204  $sql .= " SET date_livraison = ".($delivery_date != '' ? "'".$this->db->idate($delivery_date)."'" : 'null');
2205  $sql .= " WHERE rowid = ".((int) $this->id);
2206 
2207  dol_syslog(__METHOD__, LOG_DEBUG);
2208  $resql = $this->db->query($sql);
2209  if (!$resql) {
2210  $this->errors[] = $this->db->error();
2211  $error++;
2212  }
2213 
2214  if (!$error) {
2215  $this->oldcopy = clone $this;
2216  $this->date_livraison = $delivery_date;
2217  $this->delivery_date = $delivery_date;
2218  }
2219 
2220  if (!$notrigger && empty($error)) {
2221  // Call trigger
2222  $result = $this->call_trigger('PROPAL_MODIFY', $user);
2223  if ($result < 0) {
2224  $error++;
2225  }
2226  // End call triggers
2227  }
2228 
2229  if (!$error) {
2230  $this->db->commit();
2231  return 1;
2232  } else {
2233  foreach ($this->errors as $errmsg) {
2234  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
2235  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2236  }
2237  $this->db->rollback();
2238  return -1 * $error;
2239  }
2240  }
2241  }
2242 
2243  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2252  public function set_availability($user, $id, $notrigger = 0)
2253  {
2254  // phpcs:enable
2255  if (!empty($user->rights->propal->creer) && $this->statut >= self::STATUS_DRAFT) {
2256  $error = 0;
2257 
2258  $this->db->begin();
2259 
2260  $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
2261  $sql .= " SET fk_availability = ".((int) $id);
2262  $sql .= " WHERE rowid = ".((int) $this->id);
2263 
2264  dol_syslog(__METHOD__.' availability('.$id.')', LOG_DEBUG);
2265  $resql = $this->db->query($sql);
2266  if (!$resql) {
2267  $this->errors[] = $this->db->error();
2268  $error++;
2269  }
2270 
2271  if (!$error) {
2272  $this->oldcopy = clone $this;
2273  $this->fk_availability = $id;
2274  $this->availability_id = $id;
2275  }
2276 
2277  if (!$notrigger && empty($error)) {
2278  // Call trigger
2279  $result = $this->call_trigger('PROPAL_MODIFY', $user);
2280  if ($result < 0) {
2281  $error++;
2282  }
2283  // End call triggers
2284  }
2285 
2286  if (!$error) {
2287  $this->db->commit();
2288  return 1;
2289  } else {
2290  foreach ($this->errors as $errmsg) {
2291  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
2292  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2293  }
2294  $this->db->rollback();
2295  return -1 * $error;
2296  }
2297  } else {
2298  $error_str = 'Propal status do not meet requirement '.$this->statut;
2299  dol_syslog(__METHOD__.$error_str, LOG_ERR);
2300  $this->error = $error_str;
2301  $this->errors[] = $this->error;
2302  return -2;
2303  }
2304  }
2305 
2306  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2315  public function set_demand_reason($user, $id, $notrigger = 0)
2316  {
2317  // phpcs:enable
2318  if (!empty($user->rights->propal->creer) && $this->statut >= self::STATUS_DRAFT) {
2319  $error = 0;
2320 
2321  $this->db->begin();
2322 
2323  $sql = "UPDATE ".MAIN_DB_PREFIX."propal ";
2324  $sql .= " SET fk_input_reason = ".((int) $id);
2325  $sql .= " WHERE rowid = ".((int) $this->id);
2326 
2327  dol_syslog(__METHOD__, LOG_DEBUG);
2328  $resql = $this->db->query($sql);
2329  if (!$resql) {
2330  $this->errors[] = $this->db->error();
2331  $error++;
2332  }
2333 
2334 
2335  if (!$error) {
2336  $this->oldcopy = clone $this;
2337  $this->fk_input_reason = $id;
2338  $this->demand_reason_id = $id;
2339  }
2340 
2341 
2342  if (!$notrigger && empty($error)) {
2343  // Call trigger
2344  $result = $this->call_trigger('PROPAL_MODIFY', $user);
2345  if ($result < 0) {
2346  $error++;
2347  }
2348  // End call triggers
2349  }
2350 
2351  if (!$error) {
2352  $this->db->commit();
2353  return 1;
2354  } else {
2355  foreach ($this->errors as $errmsg) {
2356  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
2357  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2358  }
2359  $this->db->rollback();
2360  return -1 * $error;
2361  }
2362  } else {
2363  $error_str = 'Propal status do not meet requirement '.$this->statut;
2364  dol_syslog(__METHOD__.$error_str, LOG_ERR);
2365  $this->error = $error_str;
2366  $this->errors[] = $this->error;
2367  return -2;
2368  }
2369  }
2370 
2371  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2380  public function set_ref_client($user, $ref_client, $notrigger = 0)
2381  {
2382  // phpcs:enable
2383  if (!empty($user->rights->propal->creer)) {
2384  $error = 0;
2385 
2386  $this->db->begin();
2387 
2388  $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET ref_client = ".(empty($ref_client) ? 'NULL' : "'".$this->db->escape($ref_client)."'");
2389  $sql .= " WHERE rowid = ".((int) $this->id);
2390 
2391  dol_syslog(__METHOD__.' $this->id='.$this->id.', ref_client='.$ref_client, LOG_DEBUG);
2392  $resql = $this->db->query($sql);
2393  if (!$resql) {
2394  $this->errors[] = $this->db->error();
2395  $error++;
2396  }
2397 
2398  if (!$error) {
2399  $this->oldcopy = clone $this;
2400  $this->ref_client = $ref_client;
2401  }
2402 
2403  if (!$notrigger && empty($error)) {
2404  // Call trigger
2405  $result = $this->call_trigger('PROPAL_MODIFY', $user);
2406  if ($result < 0) {
2407  $error++;
2408  }
2409  // End call triggers
2410  }
2411 
2412  if (!$error) {
2413  $this->db->commit();
2414  return 1;
2415  } else {
2416  foreach ($this->errors as $errmsg) {
2417  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
2418  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2419  }
2420  $this->db->rollback();
2421  return -1 * $error;
2422  }
2423  } else {
2424  return -1;
2425  }
2426  }
2427 
2428  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2437  public function set_remise_percent($user, $remise, $notrigger = 0)
2438  {
2439  // phpcs:enable
2440  $remise = trim($remise) ?trim($remise) : 0;
2441 
2442  if (!empty($user->rights->propal->creer)) {
2443  $remise = price2num($remise, 2);
2444 
2445  $error = 0;
2446 
2447  $this->db->begin();
2448 
2449  $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET remise_percent = ".((float) $remise);
2450  $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = ".self::STATUS_DRAFT;
2451 
2452  dol_syslog(__METHOD__, LOG_DEBUG);
2453  $resql = $this->db->query($sql);
2454  if (!$resql) {
2455  $this->errors[] = $this->db->error();
2456  $error++;
2457  }
2458 
2459  if (!$error) {
2460  $this->oldcopy = clone $this;
2461  $this->remise_percent = $remise;
2462  $this->update_price(1);
2463  }
2464 
2465  if (!$notrigger && empty($error)) {
2466  // Call trigger
2467  $result = $this->call_trigger('PROPAL_MODIFY', $user);
2468  if ($result < 0) {
2469  $error++;
2470  }
2471  // End call triggers
2472  }
2473 
2474  if (!$error) {
2475  $this->db->commit();
2476  return 1;
2477  } else {
2478  foreach ($this->errors as $errmsg) {
2479  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
2480  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2481  }
2482  $this->db->rollback();
2483  return -1 * $error;
2484  }
2485  }
2486  }
2487 
2488 
2489  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2498  public function set_remise_absolue($user, $remise, $notrigger = 0)
2499  {
2500  // phpcs:enable
2501  if (empty($remise)) {
2502  $remise = 0;
2503  }
2505 
2506  if (!empty($user->rights->propal->creer)) {
2507  $error = 0;
2508 
2509  $this->db->begin();
2510 
2511  $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
2512  $sql .= " SET remise_absolue = ".((float) $remise);
2513  $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = ".self::STATUS_DRAFT;
2514 
2515  dol_syslog(__METHOD__, LOG_DEBUG);
2516  $resql = $this->db->query($sql);
2517  if (!$resql) {
2518  $this->errors[] = $this->db->error();
2519  $error++;
2520  }
2521 
2522  if (!$error) {
2523  $this->oldcopy = clone $this;
2524  $this->update_price(1);
2525  }
2526 
2527  if (!$notrigger && empty($error)) {
2528  // Call trigger
2529  $result = $this->call_trigger('PROPAL_MODIFY', $user);
2530  if ($result < 0) {
2531  $error++;
2532  }
2533  // End call triggers
2534  }
2535 
2536  if (!$error) {
2537  $this->db->commit();
2538  return 1;
2539  } else {
2540  foreach ($this->errors as $errmsg) {
2541  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
2542  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2543  }
2544  $this->db->rollback();
2545  return -1 * $error;
2546  }
2547  }
2548  }
2549 
2550 
2551 
2561  public function reopen($user, $status, $note = '', $notrigger = 0)
2562  {
2563  $error = 0;
2564 
2565  $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
2566  $sql .= " SET fk_statut = ".((int) $status).",";
2567  if (!empty($note)) {
2568  $sql .= " note_private = '".$this->db->escape($note)."',";
2569  }
2570  $sql .= " date_cloture=NULL, fk_user_cloture=NULL";
2571  $sql .= " WHERE rowid = ".((int) $this->id);
2572 
2573  $this->db->begin();
2574 
2575  dol_syslog(get_class($this)."::reopen", LOG_DEBUG);
2576  $resql = $this->db->query($sql);
2577  if (!$resql) {
2578  $error++;
2579  $this->errors[] = "Error ".$this->db->lasterror();
2580  }
2581  if (!$error) {
2582  if (!$notrigger) {
2583  // Call trigger
2584  $result = $this->call_trigger('PROPAL_REOPEN', $user);
2585  if ($result < 0) {
2586  $error++;
2587  }
2588  // End call triggers
2589  }
2590  }
2591 
2592  // Commit or rollback
2593  if ($error) {
2594  if (!empty($this->errors)) {
2595  foreach ($this->errors as $errmsg) {
2596  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
2597  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2598  }
2599  }
2600  $this->db->rollback();
2601  return -1 * $error;
2602  } else {
2603  $this->statut = $status;
2604  $this->status = $status;
2605 
2606  $this->db->commit();
2607  return 1;
2608  }
2609  }
2610 
2620  public function closeProposal($user, $status, $note = '', $notrigger = 0)
2621  {
2622  global $langs,$conf;
2623 
2624  $error = 0;
2625  $now = dol_now();
2626 
2627  $this->db->begin();
2628 
2629  $newprivatenote = dol_concatdesc($this->note_private, $note);
2630 
2631  $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
2632  $sql .= " SET fk_statut = ".((int) $status).", note_private = '".$this->db->escape($newprivatenote)."', date_signature='".$this->db->idate($now)."', fk_user_signature=".$user->id;
2633  $sql .= " WHERE rowid = ".((int) $this->id);
2634 
2635  $resql = $this->db->query($sql);
2636  if ($resql) {
2637  // Status self::STATUS_REFUSED by default
2638  $modelpdf = !empty($conf->global->PROPALE_ADDON_PDF_ODT_CLOSED) ? $conf->global->PROPALE_ADDON_PDF_ODT_CLOSED : $this->model_pdf;
2639  $trigger_name = 'PROPAL_CLOSE_REFUSED';
2640 
2641  if ($status == self::STATUS_SIGNED) { // Status self::STATUS_SIGNED
2642  $trigger_name = 'PROPAL_CLOSE_SIGNED';
2643  $modelpdf = !empty($conf->global->PROPALE_ADDON_PDF_ODT_TOBILL) ? $conf->global->PROPALE_ADDON_PDF_ODT_TOBILL : $this->model_pdf;
2644 
2645  // The connected company is classified as a client
2646  $soc=new Societe($this->db);
2647  $soc->id = $this->socid;
2648  $result = $soc->set_as_client();
2649 
2650  if ($result < 0) {
2651  $this->error=$this->db->lasterror();
2652  $this->db->rollback();
2653  return -2;
2654  }
2655  }
2656 
2657  if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
2658  // Define output language
2659  $outputlangs = $langs;
2660  if (!empty($conf->global->MAIN_MULTILANGS)) {
2661  $outputlangs = new Translate("", $conf);
2662  $newlang = (GETPOST('lang_id', 'aZ09') ? GETPOST('lang_id', 'aZ09') : $this->thirdparty->default_lang);
2663  $outputlangs->setDefaultLang($newlang);
2664  }
2665 
2666  // PDF
2667  $hidedetails = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0);
2668  $hidedesc = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0);
2669  $hideref = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0);
2670 
2671  //$ret=$object->fetch($id); // Reload to get new records
2672  $this->generateDocument($modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
2673  }
2674 
2675  if (!$error) {
2676  $this->oldcopy= clone $this;
2677  $this->statut = $status;
2678  $this->status = $status;
2679  $this->date_signature = $now;
2680  $this->note_private = $newprivatenote;
2681  }
2682 
2683  if (!$notrigger && empty($error)) {
2684  // Call trigger
2685  $result=$this->call_trigger($trigger_name, $user);
2686  if ($result < 0) {
2687  $error++;
2688  }
2689  // End call triggers
2690  }
2691 
2692  if (!$error ) {
2693  $this->db->commit();
2694  return 1;
2695  } else {
2696  $this->statut = $this->oldcopy->statut;
2697  $this->status = $this->oldcopy->statut;
2698  $this->date_signature = $this->oldcopy->date_signature;
2699  $this->note_private = $this->oldcopy->note_private;
2700 
2701  $this->db->rollback();
2702  return -1;
2703  }
2704  } else {
2705  $this->error = $this->db->lasterror();
2706  $this->db->rollback();
2707  return -1;
2708  }
2709  }
2710 
2719  public function classifyBilled(User $user, $notrigger = 0, $note = '')
2720  {
2721  global $conf, $langs;
2722 
2723  $error = 0;
2724 
2725  $now = dol_now();
2726  $num = 0;
2727 
2728  $triggerName = 'PROPAL_CLASSIFY_BILLED';
2729 
2730  $this->db->begin();
2731 
2732  $newprivatenote = dol_concatdesc($this->note_private, $note);
2733 
2734  $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal SET fk_statut = '.self::STATUS_BILLED.", ";
2735  $sql .= " note_private = '".$this->db->escape($newprivatenote)."', date_cloture='".$this->db->idate($now)."', fk_user_cloture=".((int) $user->id);
2736  $sql .= ' WHERE rowid = '.((int) $this->id).' AND fk_statut = '.((int) self::STATUS_SIGNED);
2737 
2738  dol_syslog(__METHOD__, LOG_DEBUG);
2739  $resql = $this->db->query($sql);
2740  if (!$resql) {
2741  $this->errors[] = $this->db->error();
2742  $error++;
2743  } else {
2744  $num = $this->db->affected_rows($resql);
2745  }
2746 
2747  if (!$error) {
2748  $modelpdf = $conf->global->PROPALE_ADDON_PDF_ODT_CLOSED ? $conf->global->PROPALE_ADDON_PDF_ODT_CLOSED : $this->model_pdf;
2749 
2750  if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
2751  // Define output language
2752  $outputlangs = $langs;
2753  if (!empty($conf->global->MAIN_MULTILANGS)) {
2754  $outputlangs = new Translate("", $conf);
2755  $newlang = (GETPOST('lang_id', 'aZ09') ? GETPOST('lang_id', 'aZ09') : $this->thirdparty->default_lang);
2756  $outputlangs->setDefaultLang($newlang);
2757  }
2758 
2759  // PDF
2760  $hidedetails = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0);
2761  $hidedesc = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0);
2762  $hideref = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0);
2763 
2764  //$ret=$object->fetch($id); // Reload to get new records
2765  $this->generateDocument($modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
2766  }
2767 
2768  $this->oldcopy = clone $this;
2769  $this->statut = self::STATUS_BILLED;
2770  $this->date_cloture = $now;
2771  $this->note_private = $newprivatenote;
2772  }
2773 
2774  if (!$notrigger && empty($error)) {
2775  // Call trigger
2776  $result = $this->call_trigger($triggerName, $user);
2777  if ($result < 0) {
2778  $error++;
2779  }
2780  // End call triggers
2781  }
2782 
2783  if (!$error) {
2784  $this->db->commit();
2785  return $num;
2786  } else {
2787  foreach ($this->errors as $errmsg) {
2788  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
2789  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2790  }
2791  $this->db->rollback();
2792  return -1 * $error;
2793  }
2794  }
2795 
2796  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2804  public function setDraft($user, $notrigger = 0)
2805  {
2806  // phpcs:enable
2807  $error = 0;
2808 
2809  // Protection
2810  if ($this->statut <= self::STATUS_DRAFT) {
2811  return 0;
2812  }
2813 
2814  dol_syslog(get_class($this)."::setDraft", LOG_DEBUG);
2815 
2816  $this->db->begin();
2817 
2818  $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
2819  $sql .= " SET fk_statut = ".self::STATUS_DRAFT;
2820  $sql .= " WHERE rowid = ".((int) $this->id);
2821 
2822  $resql = $this->db->query($sql);
2823  if (!$resql) {
2824  $this->errors[] = $this->db->error();
2825  $error++;
2826  }
2827 
2828  if (!$error) {
2829  $this->oldcopy = clone $this;
2830  }
2831 
2832  if (!$notrigger && empty($error)) {
2833  // Call trigger
2834  $result = $this->call_trigger('PROPAL_MODIFY', $user);
2835  if ($result < 0) {
2836  $error++;
2837  }
2838  // End call triggers
2839  }
2840 
2841  if (!$error) {
2842  $this->statut = self::STATUS_DRAFT;
2843  $this->brouillon = 1;
2844 
2845  $this->db->commit();
2846  return 1;
2847  } else {
2848  foreach ($this->errors as $errmsg) {
2849  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
2850  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2851  }
2852  $this->db->rollback();
2853  return -1 * $error;
2854  }
2855  }
2856 
2857 
2858  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2872  public function liste_array($shortlist = 0, $draft = 0, $notcurrentuser = 0, $socid = 0, $limit = 0, $offset = 0, $sortfield = 'p.datep', $sortorder = 'DESC')
2873  {
2874  // phpcs:enable
2875  global $user;
2876 
2877  $ga = array();
2878 
2879  $sql = "SELECT s.rowid, s.nom as name, s.client,";
2880  $sql .= " p.rowid as propalid, p.fk_statut, p.total_ht, p.ref, p.remise, ";
2881  $sql .= " p.datep as dp, p.fin_validite as datelimite";
2882  if (empty($user->rights->societe->client->voir) && !$socid) {
2883  $sql .= ", sc.fk_soc, sc.fk_user";
2884  }
2885  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."propal as p, ".MAIN_DB_PREFIX."c_propalst as c";
2886  if (empty($user->rights->societe->client->voir) && !$socid) {
2887  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
2888  }
2889  $sql .= " WHERE p.entity IN (".getEntity('propal').")";
2890  $sql .= " AND p.fk_soc = s.rowid";
2891  $sql .= " AND p.fk_statut = c.id";
2892  if (empty($user->rights->societe->client->voir) && !$socid) { //restriction
2893  $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2894  }
2895  if ($socid) {
2896  $sql .= " AND s.rowid = ".((int) $socid);
2897  }
2898  if ($draft) {
2899  $sql .= " AND p.fk_statut = ".self::STATUS_DRAFT;
2900  }
2901  if ($notcurrentuser > 0) {
2902  $sql .= " AND p.fk_user_author <> ".((int) $user->id);
2903  }
2904  $sql .= $this->db->order($sortfield, $sortorder);
2905  $sql .= $this->db->plimit($limit, $offset);
2906 
2907  $result = $this->db->query($sql);
2908  if ($result) {
2909  $num = $this->db->num_rows($result);
2910  if ($num) {
2911  $i = 0;
2912  while ($i < $num) {
2913  $obj = $this->db->fetch_object($result);
2914 
2915  if ($shortlist == 1) {
2916  $ga[$obj->propalid] = $obj->ref;
2917  } elseif ($shortlist == 2) {
2918  $ga[$obj->propalid] = $obj->ref.' ('.$obj->name.')';
2919  } else {
2920  $ga[$i]['id'] = $obj->propalid;
2921  $ga[$i]['ref'] = $obj->ref;
2922  $ga[$i]['name'] = $obj->name;
2923  }
2924 
2925  $i++;
2926  }
2927  }
2928  return $ga;
2929  } else {
2930  dol_print_error($this->db);
2931  return -1;
2932  }
2933  }
2934 
2940  public function getInvoiceArrayList()
2941  {
2942  return $this->InvoiceArrayList($this->id);
2943  }
2944 
2945  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2952  public function InvoiceArrayList($id)
2953  {
2954  // phpcs:enable
2955  $ga = array();
2956  $linkedInvoices = array();
2957 
2958  $this->fetchObjectLinked($id, $this->element);
2959  foreach ($this->linkedObjectsIds as $objecttype => $objectid) {
2960  // Nouveau système du comon object renvoi des rowid et non un id linéaire de 1 à n
2961  // On parcourt donc une liste d'objets en tant qu'objet unique
2962  foreach ($objectid as $key => $object) {
2963  // Cas des factures liees directement
2964  if ($objecttype == 'facture') {
2965  $linkedInvoices[] = $object;
2966  } else {
2967  // Cas des factures liees par un autre objet (ex: commande)
2968  $this->fetchObjectLinked($object, $objecttype);
2969  foreach ($this->linkedObjectsIds as $subobjecttype => $subobjectid) {
2970  foreach ($subobjectid as $subkey => $subobject) {
2971  if ($subobjecttype == 'facture') {
2972  $linkedInvoices[] = $subobject;
2973  }
2974  }
2975  }
2976  }
2977  }
2978  }
2979 
2980  if (count($linkedInvoices) > 0) {
2981  $sql = "SELECT rowid as facid, ref, total_ht as total, datef as df, fk_user_author, fk_statut, paye";
2982  $sql .= " FROM ".MAIN_DB_PREFIX."facture";
2983  $sql .= " WHERE rowid IN (".$this->db->sanitize(implode(',', $linkedInvoices)).")";
2984 
2985  dol_syslog(get_class($this)."::InvoiceArrayList", LOG_DEBUG);
2986  $resql = $this->db->query($sql);
2987 
2988  if ($resql) {
2989  $tab_sqlobj = array();
2990  $nump = $this->db->num_rows($resql);
2991  for ($i = 0; $i < $nump; $i++) {
2992  $sqlobj = $this->db->fetch_object($resql);
2993  $tab_sqlobj[] = $sqlobj;
2994  }
2995  $this->db->free($resql);
2996 
2997  $nump = count($tab_sqlobj);
2998 
2999  if ($nump) {
3000  $i = 0;
3001  while ($i < $nump) {
3002  $obj = array_shift($tab_sqlobj);
3003 
3004  $ga[$i] = $obj;
3005 
3006  $i++;
3007  }
3008  }
3009  return $ga;
3010  } else {
3011  return -1;
3012  }
3013  } else {
3014  return $ga;
3015  }
3016  }
3017 
3025  public function delete($user, $notrigger = 0)
3026  {
3027  global $conf;
3028  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
3029 
3030  $error = 0;
3031 
3032  $this->db->begin();
3033 
3034  if (!$notrigger) {
3035  // Call trigger
3036  $result = $this->call_trigger('PROPAL_DELETE', $user);
3037  if ($result < 0) {
3038  $error++;
3039  }
3040  // End call triggers
3041  }
3042 
3043  // Delete extrafields of lines and lines
3044  if (!$error && !empty($this->table_element_line)) {
3045  $tabletodelete = $this->table_element_line;
3046  $sqlef = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete."_extrafields WHERE fk_object IN (SELECT rowid FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE ".$this->fk_element." = ".((int) $this->id).")";
3047  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE ".$this->fk_element." = ".((int) $this->id);
3048  if (!$this->db->query($sqlef) || !$this->db->query($sql)) {
3049  $error++;
3050  $this->error = $this->db->lasterror();
3051  $this->errors[] = $this->error;
3052  dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
3053  }
3054  }
3055 
3056  if (!$error) {
3057  // Delete linked object
3058  $res = $this->deleteObjectLinked();
3059  if ($res < 0) {
3060  $error++;
3061  }
3062  }
3063 
3064  if (!$error) {
3065  // Delete linked contacts
3066  $res = $this->delete_linked_contact();
3067  if ($res < 0) {
3068  $error++;
3069  }
3070  }
3071 
3072  // Removed extrafields of object
3073  if (!$error) {
3074  $result = $this->deleteExtraFields();
3075  if ($result < 0) {
3076  $error++;
3077  dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
3078  }
3079  }
3080 
3081  // Delete main record
3082  if (!$error) {
3083  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element." WHERE rowid = ".((int) $this->id);
3084  $res = $this->db->query($sql);
3085  if (!$res) {
3086  $error++;
3087  $this->error = $this->db->lasterror();
3088  $this->errors[] = $this->error;
3089  dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
3090  }
3091  }
3092 
3093  // Delete record into ECM index and physically
3094  if (!$error) {
3095  $res = $this->deleteEcmFiles(0); // Deleting files physically is done later with the dol_delete_dir_recursive
3096  if (!$res) {
3097  $error++;
3098  }
3099  }
3100 
3101  if (!$error) {
3102  // We remove directory
3103  $ref = dol_sanitizeFileName($this->ref);
3104  if ($conf->propal->multidir_output[$this->entity] && !empty($this->ref)) {
3105  $dir = $conf->propal->multidir_output[$this->entity]."/".$ref;
3106  $file = $dir."/".$ref.".pdf";
3107  if (file_exists($file)) {
3108  dol_delete_preview($this);
3109 
3110  if (!dol_delete_file($file, 0, 0, 0, $this)) {
3111  $this->error = 'ErrorFailToDeleteFile';
3112  $this->errors[] = $this->error;
3113  $this->db->rollback();
3114  return 0;
3115  }
3116  }
3117  if (file_exists($dir)) {
3118  $res = @dol_delete_dir_recursive($dir);
3119  if (!$res) {
3120  $this->error = 'ErrorFailToDeleteDir';
3121  $this->errors[] = $this->error;
3122  $this->db->rollback();
3123  return 0;
3124  }
3125  }
3126  }
3127  }
3128 
3129  if (!$error) {
3130  dol_syslog(get_class($this)."::delete ".$this->id." by ".$user->id, LOG_DEBUG);
3131  $this->db->commit();
3132  return 1;
3133  } else {
3134  $this->db->rollback();
3135  return -1;
3136  }
3137  }
3138 
3147  public function availability($availability_id, $notrigger = 0)
3148  {
3149  global $user;
3150 
3151  if ($this->statut >= self::STATUS_DRAFT) {
3152  $error = 0;
3153 
3154  $this->db->begin();
3155 
3156  $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal';
3157  $sql .= ' SET fk_availability = '.((int) $availability_id);
3158  $sql .= ' WHERE rowid='.((int) $this->id);
3159 
3160  dol_syslog(__METHOD__.' availability('.$availability_id.')', LOG_DEBUG);
3161  $resql = $this->db->query($sql);
3162  if (!$resql) {
3163  $this->errors[] = $this->db->error();
3164  $error++;
3165  }
3166 
3167  if (!$error) {
3168  $this->oldcopy = clone $this;
3169  $this->availability_id = $availability_id;
3170  }
3171 
3172  if (!$notrigger && empty($error)) {
3173  // Call trigger
3174  $result = $this->call_trigger('PROPAL_MODIFY', $user);
3175  if ($result < 0) {
3176  $error++;
3177  }
3178  // End call triggers
3179  }
3180 
3181  if (!$error) {
3182  $this->db->commit();
3183  return 1;
3184  } else {
3185  foreach ($this->errors as $errmsg) {
3186  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
3187  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
3188  }
3189  $this->db->rollback();
3190  return -1 * $error;
3191  }
3192  } else {
3193  $error_str = 'Propal status do not meet requirement '.$this->statut;
3194  dol_syslog(__METHOD__.$error_str, LOG_ERR);
3195  $this->error = $error_str;
3196  $this->errors[] = $this->error;
3197  return -2;
3198  }
3199  }
3200 
3201  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3210  public function demand_reason($demand_reason_id, $notrigger = 0)
3211  {
3212  // phpcs:enable
3213  global $user;
3214 
3215  if ($this->statut >= self::STATUS_DRAFT) {
3216  $error = 0;
3217 
3218  $this->db->begin();
3219 
3220  $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal';
3221  $sql .= ' SET fk_input_reason = '.((int) $demand_reason_id);
3222  $sql .= ' WHERE rowid='.((int) $this->id);
3223 
3224  dol_syslog(__METHOD__.' demand_reason('.$demand_reason_id.')', LOG_DEBUG);
3225  $resql = $this->db->query($sql);
3226  if (!$resql) {
3227  $this->errors[] = $this->db->error();
3228  $error++;
3229  }
3230 
3231  if (!$error) {
3232  $this->oldcopy = clone $this;
3233  $this->demand_reason_id = $demand_reason_id;
3234  }
3235 
3236  if (!$notrigger && empty($error)) {
3237  // Call trigger
3238  $result = $this->call_trigger('PROPAL_MODIFY', $user);
3239  if ($result < 0) {
3240  $error++;
3241  }
3242  // End call triggers
3243  }
3244 
3245  if (!$error) {
3246  $this->db->commit();
3247  return 1;
3248  } else {
3249  foreach ($this->errors as $errmsg) {
3250  dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
3251  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
3252  }
3253  $this->db->rollback();
3254  return -1 * $error;
3255  }
3256  } else {
3257  $error_str = 'Propal status do not meet requirement '.$this->statut;
3258  dol_syslog(__METHOD__.$error_str, LOG_ERR);
3259  $this->error = $error_str;
3260  $this->errors[] = $this->error;
3261  return -2;
3262  }
3263  }
3264 
3265 
3272  public function info($id)
3273  {
3274  $sql = "SELECT c.rowid, ";
3275  $sql .= " c.datec, c.date_valid as datev, c.date_signature, c.date_cloture,";
3276  $sql .= " c.fk_user_author, c.fk_user_valid, c.fk_user_signature, c.fk_user_cloture";
3277  $sql .= " FROM ".MAIN_DB_PREFIX."propal as c";
3278  $sql .= " WHERE c.rowid = ".((int) $id);
3279 
3280  $result = $this->db->query($sql);
3281 
3282  if ($result) {
3283  if ($this->db->num_rows($result)) {
3284  $obj = $this->db->fetch_object($result);
3285 
3286  $this->id = $obj->rowid;
3287 
3288  $this->date_creation = $this->db->jdate($obj->datec);
3289  $this->date_validation = $this->db->jdate($obj->datev);
3290  $this->date_signature = $this->db->jdate($obj->date_signature);
3291  $this->date_cloture = $this->db->jdate($obj->date_cloture);
3292 
3293  $cuser = new User($this->db);
3294  $cuser->fetch($obj->fk_user_author);
3295  $this->user_creation = $cuser;
3296 
3297  if ($obj->fk_user_valid) {
3298  $vuser = new User($this->db);
3299  $vuser->fetch($obj->fk_user_valid);
3300  $this->user_validation = $vuser;
3301  }
3302 
3303  if ($obj->fk_user_signature) {
3304  $user_signature = new User($this->db);
3305  $user_signature->fetch($obj->fk_user_signature);
3306  $this->user_signature = $user_signature;
3307  }
3308 
3309  if ($obj->fk_user_cloture) {
3310  $cluser = new User($this->db);
3311  $cluser->fetch($obj->fk_user_cloture);
3312  $this->user_cloture = $cluser;
3313  }
3314  }
3315  $this->db->free($result);
3316  } else {
3317  dol_print_error($this->db);
3318  }
3319  }
3320 
3321 
3328  public function getLibStatut($mode = 0)
3329  {
3330  return $this->LibStatut($this->statut, $mode);
3331  }
3332 
3333  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3341  public function LibStatut($status, $mode = 1)
3342  {
3343  // phpcs:enable
3344  global $conf;
3345 
3346  // Init/load array of translation of status
3347  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
3348  global $langs;
3349  $langs->load("propal");
3350  $this->labelStatus[0] = $langs->transnoentitiesnoconv("PropalStatusDraft");
3351  $this->labelStatus[1] = $langs->transnoentitiesnoconv("PropalStatusValidated");
3352  $this->labelStatus[2] = $langs->transnoentitiesnoconv("PropalStatusSigned");
3353  $this->labelStatus[3] = $langs->transnoentitiesnoconv("PropalStatusNotSigned");
3354  $this->labelStatus[4] = $langs->transnoentitiesnoconv("PropalStatusBilled");
3355  $this->labelStatusShort[0] = $langs->transnoentitiesnoconv("PropalStatusDraftShort");
3356  $this->labelStatusShort[1] = $langs->transnoentitiesnoconv("PropalStatusValidatedShort");
3357  $this->labelStatusShort[2] = $langs->transnoentitiesnoconv("PropalStatusSignedShort");
3358  $this->labelStatusShort[3] = $langs->transnoentitiesnoconv("PropalStatusNotSignedShort");
3359  $this->labelStatusShort[4] = $langs->transnoentitiesnoconv("PropalStatusBilledShort");
3360  }
3361 
3362  $statusType = '';
3363  if ($status == self::STATUS_DRAFT) {
3364  $statusType = 'status0';
3365  } elseif ($status == self::STATUS_VALIDATED) {
3366  $statusType = 'status1';
3367  } elseif ($status == self::STATUS_SIGNED) {
3368  $statusType = 'status4';
3369  } elseif ($status == self::STATUS_NOTSIGNED) {
3370  $statusType = 'status9';
3371  } elseif ($status == self::STATUS_BILLED) {
3372  $statusType = 'status6';
3373  }
3374 
3375  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
3376  }
3377 
3378 
3379  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3387  public function load_board($user, $mode)
3388  {
3389  // phpcs:enable
3390  global $conf, $langs;
3391 
3392  $clause = " WHERE";
3393 
3394  $sql = "SELECT p.rowid, p.ref, p.datec as datec, p.fin_validite as datefin, p.total_ht";
3395  $sql .= " FROM ".MAIN_DB_PREFIX."propal as p";
3396  if (empty($user->rights->societe->client->voir) && !$user->socid) {
3397  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc";
3398  $sql .= " WHERE sc.fk_user = ".((int) $user->id);
3399  $clause = " AND";
3400  }
3401  $sql .= $clause." p.entity IN (".getEntity('propal').")";
3402  if ($mode == 'opened') {
3403  $sql .= " AND p.fk_statut = ".self::STATUS_VALIDATED;
3404  }
3405  if ($mode == 'signed') {
3406  $sql .= " AND p.fk_statut = ".self::STATUS_SIGNED;
3407  }
3408  if ($user->socid) {
3409  $sql .= " AND p.fk_soc = ".((int) $user->socid);
3410  }
3411 
3412  $resql = $this->db->query($sql);
3413  if ($resql) {
3414  $langs->load("propal");
3415  $now = dol_now();
3416 
3417  $delay_warning = 0;
3418  $status = 0;
3419  $label = $labelShort = '';
3420  if ($mode == 'opened') {
3421  $delay_warning = $conf->propal->cloture->warning_delay;
3422  $status = self::STATUS_VALIDATED;
3423  $label = $langs->transnoentitiesnoconv("PropalsToClose");
3424  $labelShort = $langs->transnoentitiesnoconv("ToAcceptRefuse");
3425  }
3426  if ($mode == 'signed') {
3427  $delay_warning = $conf->propal->facturation->warning_delay;
3428  $status = self::STATUS_SIGNED;
3429  $label = $langs->trans("PropalsToBill"); // We set here bill but may be billed or ordered
3430  $labelShort = $langs->trans("ToBill");
3431  }
3432 
3433  $response = new WorkboardResponse();
3434  $response->warning_delay = $delay_warning / 60 / 60 / 24;
3435  $response->label = $label;
3436  $response->labelShort = $labelShort;
3437  $response->url = DOL_URL_ROOT.'/comm/propal/list.php?search_status='.$status.'&mainmenu=commercial&leftmenu=propals';
3438  $response->url_late = DOL_URL_ROOT.'/comm/propal/list.php?search_status='.$status.'&mainmenu=commercial&leftmenu=propals&sortfield=p.datep&sortorder=asc';
3439  $response->img = img_object('', "propal");
3440 
3441  // This assignment in condition is not a bug. It allows walking the results.
3442  while ($obj = $this->db->fetch_object($resql)) {
3443  $response->nbtodo++;
3444  $response->total += $obj->total_ht;
3445 
3446  if ($mode == 'opened') {
3447  $datelimit = $this->db->jdate($obj->datefin);
3448  if ($datelimit < ($now - $delay_warning)) {
3449  $response->nbtodolate++;
3450  }
3451  }
3452  // TODO Definir regle des propales a facturer en retard
3453  // if ($mode == 'signed' && ! count($this->FactureListeArray($obj->rowid))) $this->nbtodolate++;
3454  }
3455 
3456  return $response;
3457  } else {
3458  $this->error = $this->db->error();
3459  return -1;
3460  }
3461  }
3462 
3463 
3471  public function initAsSpecimen()
3472  {
3473  global $conf, $langs;
3474 
3475  // Load array of products prodids
3476  $num_prods = 0;
3477  $prodids = array();
3478  $sql = "SELECT rowid";
3479  $sql .= " FROM ".MAIN_DB_PREFIX."product";
3480  $sql .= " WHERE entity IN (".getEntity('product').")";
3481  $sql .= $this->db->plimit(100);
3482 
3483  $resql = $this->db->query($sql);
3484  if ($resql) {
3485  $num_prods = $this->db->num_rows($resql);
3486  $i = 0;
3487  while ($i < $num_prods) {
3488  $i++;
3489  $row = $this->db->fetch_row($resql);
3490  $prodids[$i] = $row[0];
3491  }
3492  }
3493 
3494  // Initialise parametres
3495  $this->id = 0;
3496  $this->ref = 'SPECIMEN';
3497  $this->ref_client = 'NEMICEPS';
3498  $this->specimen = 1;
3499  $this->socid = 1;
3500  $this->date = time();
3501  $this->fin_validite = $this->date + 3600 * 24 * 30;
3502  $this->cond_reglement_id = 1;
3503  $this->cond_reglement_code = 'RECEP';
3504  $this->mode_reglement_id = 7;
3505  $this->mode_reglement_code = 'CHQ';
3506  $this->availability_id = 1;
3507  $this->availability_code = 'AV_NOW';
3508  $this->demand_reason_id = 1;
3509  $this->demand_reason_code = 'SRC_00';
3510  $this->note_public = 'This is a comment (public)';
3511  $this->note_private = 'This is a comment (private)';
3512 
3513  $this->multicurrency_tx = 1;
3514  $this->multicurrency_code = $conf->currency;
3515 
3516  // Lines
3517  $nbp = 5;
3518  $xnbp = 0;
3519  while ($xnbp < $nbp) {
3520  $line = new PropaleLigne($this->db);
3521  $line->desc = $langs->trans("Description")." ".$xnbp;
3522  $line->qty = 1;
3523  $line->subprice = 100;
3524  $line->price = 100;
3525  $line->tva_tx = 20;
3526  $line->localtax1_tx = 0;
3527  $line->localtax2_tx = 0;
3528  if ($xnbp == 2) {
3529  $line->total_ht = 50;
3530  $line->total_ttc = 60;
3531  $line->total_tva = 10;
3532  $line->remise_percent = 50;
3533  } else {
3534  $line->total_ht = 100;
3535  $line->total_ttc = 120;
3536  $line->total_tva = 20;
3537  $line->remise_percent = 00;
3538  }
3539 
3540  if ($num_prods > 0) {
3541  $prodid = mt_rand(1, $num_prods);
3542  $line->fk_product = $prodids[$prodid];
3543  $line->product_ref = 'SPECIMEN';
3544  }
3545 
3546  $this->lines[$xnbp] = $line;
3547 
3548  $this->total_ht += $line->total_ht;
3549  $this->total_tva += $line->total_tva;
3550  $this->total_ttc += $line->total_ttc;
3551 
3552  $xnbp++;
3553  }
3554  }
3555 
3556  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3562  public function load_state_board()
3563  {
3564  // phpcs:enable
3565  global $user;
3566 
3567  $this->nb = array();
3568  $clause = "WHERE";
3569 
3570  $sql = "SELECT count(p.rowid) as nb";
3571  $sql .= " FROM ".MAIN_DB_PREFIX."propal as p";
3572  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid";
3573  if (empty($user->rights->societe->client->voir) && !$user->socid) {
3574  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
3575  $sql .= " WHERE sc.fk_user = ".((int) $user->id);
3576  $clause = "AND";
3577  }
3578  $sql .= " ".$clause." p.entity IN (".getEntity('propal').")";
3579 
3580  $resql = $this->db->query($sql);
3581  if ($resql) {
3582  // This assignment in condition is not a bug. It allows walking the results.
3583  while ($obj = $this->db->fetch_object($resql)) {
3584  $this->nb["proposals"] = $obj->nb;
3585  }
3586  $this->db->free($resql);
3587  return 1;
3588  } else {
3589  dol_print_error($this->db);
3590  $this->error = $this->db->error();
3591  return -1;
3592  }
3593  }
3594 
3595 
3603  public function getNextNumRef($soc)
3604  {
3605  global $conf, $langs;
3606  $langs->load("propal");
3607 
3608  $classname = $conf->global->PROPALE_ADDON;
3609 
3610  if (!empty($classname)) {
3611  $mybool = false;
3612 
3613  $file = $classname.".php";
3614 
3615  // Include file with class
3616  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
3617  foreach ($dirmodels as $reldir) {
3618  $dir = dol_buildpath($reldir."core/modules/propale/");
3619 
3620  // Load file with numbering class (if found)
3621  $mybool |= @include_once $dir.$file;
3622  }
3623 
3624  if (!$mybool) {
3625  dol_print_error('', "Failed to include file ".$file);
3626  return '';
3627  }
3628 
3629  $obj = new $classname();
3630  $numref = "";
3631  $numref = $obj->getNextValue($soc, $this);
3632 
3633  if ($numref != "") {
3634  return $numref;
3635  } else {
3636  $this->error = $obj->error;
3637  //dol_print_error($db,"Propale::getNextNumRef ".$obj->error);
3638  return "";
3639  }
3640  } else {
3641  $langs->load("errors");
3642  print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv("Proposal"));
3643  return "";
3644  }
3645  }
3646 
3658  public function getNomUrl($withpicto = 0, $option = '', $get_params = '', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = -1)
3659  {
3660  global $langs, $conf, $user, $hookmanager;
3661 
3662  if (!empty($conf->dol_no_mouse_hover)) {
3663  $notooltip = 1; // Force disable tooltips
3664  }
3665 
3666  $result = '';
3667  $label = '';
3668  $url = '';
3669 
3670  if ($user->rights->propal->lire) {
3671  $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Proposal").'</u>';
3672  if (isset($this->statut)) {
3673  $label .= ' '.$this->getLibStatut(5);
3674  }
3675  if (!empty($this->ref)) {
3676  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
3677  }
3678  if (!empty($this->ref_client)) {
3679  $label .= '<br><b>'.$langs->trans('RefCustomer').':</b> '.$this->ref_client;
3680  }
3681  if (!empty($this->total_ht)) {
3682  $label .= '<br><b>'.$langs->trans('AmountHT').':</b> '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
3683  }
3684  if (!empty($this->total_tva)) {
3685  $label .= '<br><b>'.$langs->trans('VAT').':</b> '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
3686  }
3687  if (!empty($this->total_ttc)) {
3688  $label .= '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
3689  }
3690  if (!empty($this->date)) {
3691  $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->date, 'day');
3692  }
3693  if (!empty($this->delivery_date)) {
3694  $label .= '<br><b>'.$langs->trans('DeliveryDate').':</b> '.dol_print_date($this->delivery_date, 'dayhour');
3695  }
3696 
3697  if ($option == '') {
3698  $url = DOL_URL_ROOT.'/comm/propal/card.php?id='.$this->id.$get_params;
3699  } elseif ($option == 'compta') { // deprecated
3700  $url = DOL_URL_ROOT.'/comm/propal/card.php?id='.$this->id.$get_params;
3701  } elseif ($option == 'expedition') {
3702  $url = DOL_URL_ROOT.'/expedition/propal.php?id='.$this->id.$get_params;
3703  } elseif ($option == 'document') {
3704  $url = DOL_URL_ROOT.'/comm/propal/document.php?id='.$this->id.$get_params;
3705  }
3706 
3707  if ($option != 'nolink') {
3708  // Add param to save lastsearch_values or not
3709  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
3710  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
3711  $add_save_lastsearch_values = 1;
3712  }
3713  if ($add_save_lastsearch_values) {
3714  $url .= '&save_lastsearch_values=1';
3715  }
3716  }
3717  }
3718 
3719  $linkclose = '';
3720  if (empty($notooltip) && $user->rights->propal->lire) {
3721  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
3722  $label = $langs->trans("Proposal");
3723  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
3724  }
3725  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
3726  $linkclose .= ' class="classfortooltip"';
3727  }
3728 
3729  $linkstart = '<a href="'.$url.'"';
3730  $linkstart .= $linkclose.'>';
3731  $linkend = '</a>';
3732 
3733  $result .= $linkstart;
3734  if ($withpicto) {
3735  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
3736  }
3737  if ($withpicto != 2) {
3738  $result .= $this->ref;
3739  }
3740  $result .= $linkend;
3741 
3742  if ($addlinktonotes >= 0) {
3743  $txttoshow = '';
3744 
3745  if ($addlinktonotes == 0) {
3746  if (!empty($this->note_private) || !empty($this->note_public)) {
3747  $txttoshow = $langs->trans('ViewPrivateNote');
3748  }
3749  } elseif ($addlinktonotes == 1) {
3750  if (!empty($this->note_private)) {
3751  $txttoshow .= ($user->socid > 0 ? '' : dol_string_nohtmltag($this->note_private, 1));
3752  }
3753  } elseif ($addlinktonotes == 2) {
3754  if (!empty($this->note_public)) {
3755  $txttoshow .= dol_string_nohtmltag($this->note_public, 1);
3756  }
3757  } elseif ($addlinktonotes == 3) {
3758  if ($user->socid > 0) {
3759  if (!empty($this->note_public)) {
3760  $txttoshow .= dol_string_nohtmltag($this->note_public, 1);
3761  }
3762  } else {
3763  if (!empty($this->note_public)) {
3764  $txttoshow .= dol_string_nohtmltag($this->note_public, 1);
3765  }
3766  if (!empty($this->note_private)) {
3767  if (!empty($txttoshow)) {
3768  $txttoshow .= '<br><br>';
3769  }
3770  $txttoshow .= dol_string_nohtmltag($this->note_private, 1);
3771  }
3772  }
3773  }
3774 
3775  if ($txttoshow) {
3776  $result .= ' <span class="note inline-block">';
3777  $result .= '<a href="'.DOL_URL_ROOT.'/comm/propal/note.php?id='.$this->id.'" class="classfortooltip" title="'.dol_escape_htmltag($txttoshow).'">';
3778  $result .= img_picto('', 'note');
3779  $result .= '</a>';
3780  $result .= '</span>';
3781  }
3782  }
3783 
3784  global $action;
3785  $hookmanager->initHooks(array($this->element . 'dao'));
3786  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
3787  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
3788  if ($reshook > 0) {
3789  $result = $hookmanager->resPrint;
3790  } else {
3791  $result .= $hookmanager->resPrint;
3792  }
3793  return $result;
3794  }
3795 
3802  public function getLinesArray($filters = '')
3803  {
3804  return $this->fetch_lines(0, 0, $filters);
3805  }
3806 
3818  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
3819  {
3820  global $conf, $langs;
3821 
3822  $langs->load("propale");
3823  $outputlangs->load("products");
3824 
3825  if (!dol_strlen($modele)) {
3826  $modele = 'azur';
3827 
3828  if ($this->model_pdf) {
3829  $modele = $this->model_pdf;
3830  } elseif (!empty($conf->global->PROPALE_ADDON_PDF)) {
3831  $modele = $conf->global->PROPALE_ADDON_PDF;
3832  }
3833  }
3834 
3835  $modelpath = "core/modules/propale/doc/";
3836 
3837  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3838  }
3839 
3848  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
3849  {
3850  $tables = array(
3851  'propal'
3852  );
3853 
3854  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
3855  }
3856 
3865  public static function replaceProduct(DoliDB $db, $origin_id, $dest_id)
3866  {
3867  $tables = array(
3868  'propaldet'
3869  );
3870 
3871  return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables);
3872  }
3873 }
3874 
3879 {
3883  public $element = 'propaldet';
3884 
3888  public $table_element = 'propaldet';
3889 
3890  public $oldline;
3891 
3892  // From llx_propaldet
3893  public $fk_propal;
3894  public $fk_parent_line;
3895  public $desc; // Description ligne
3896  public $fk_product; // Id produit predefini
3907  public $product_type = Product::TYPE_PRODUCT;
3908 
3909  public $qty;
3910 
3911  public $tva_tx;
3912  public $vat_src_code;
3913 
3914  public $subprice;
3915  public $remise_percent;
3916  public $fk_remise_except;
3917 
3918  public $rang = 0;
3919 
3920  public $fk_fournprice;
3921  public $pa_ht;
3922  public $marge_tx;
3923  public $marque_tx;
3924 
3925  public $special_code; // Tag for special lines (exlusive tags)
3926  // 1: frais de port
3927  // 2: ecotaxe
3928  // 3: option line (when qty = 0)
3929 
3930  public $info_bits = 0; // Some other info:
3931  // Bit 0: 0 si TVA normal - 1 si TVA NPR
3932  // Bit 1: 0 ligne normale - 1 si ligne de remise fixe
3933 
3934  public $total_ht; // Total HT de la ligne toute quantite et incluant la remise ligne
3935  public $total_tva; // Total TVA de la ligne toute quantite et incluant la remise ligne
3936  public $total_ttc; // Total TTC de la ligne toute quantite et incluant la remise ligne
3937 
3942  public $remise;
3947  public $price;
3948 
3949  // From llx_product
3954  public $ref;
3959  public $product_ref;
3964  public $libelle;
3969  public $label;
3974  public $product_label;
3979  public $product_desc;
3980 
3985  public $product_tobatch;
3986 
3991  public $product_barcode;
3992 
3993  public $localtax1_tx; // Local tax 1
3994  public $localtax2_tx; // Local tax 2
3995  public $localtax1_type; // Local tax 1 type
3996  public $localtax2_type; // Local tax 2 type
3997  public $total_localtax1; // Line total local tax 1
3998  public $total_localtax2; // Line total local tax 2
3999 
4000  public $date_start;
4001  public $date_end;
4002 
4003  public $skip_update_total; // Skip update price total for special lines
4004 
4005  // Multicurrency
4006  public $fk_multicurrency;
4007  public $multicurrency_code;
4008  public $multicurrency_subprice;
4009  public $multicurrency_total_ht;
4010  public $multicurrency_total_tva;
4011  public $multicurrency_total_ttc;
4012 
4013 
4019  public function __construct($db)
4020  {
4021  $this->db = $db;
4022  }
4023 
4030  public function fetch($rowid)
4031  {
4032  $sql = 'SELECT pd.rowid, pd.fk_propal, pd.fk_parent_line, pd.fk_product, pd.label as custom_label, pd.description, pd.price, pd.qty, pd.vat_src_code, pd.tva_tx,';
4033  $sql .= ' pd.remise, pd.remise_percent, pd.fk_remise_except, pd.subprice,';
4034  $sql .= ' pd.info_bits, pd.total_ht, pd.total_tva, pd.total_ttc, pd.fk_product_fournisseur_price as fk_fournprice, pd.buy_price_ht as pa_ht, pd.special_code, pd.rang,';
4035  $sql .= ' pd.fk_unit,';
4036  $sql .= ' pd.localtax1_tx, pd.localtax2_tx, pd.total_localtax1, pd.total_localtax2,';
4037  $sql .= ' pd.fk_multicurrency, pd.multicurrency_code, pd.multicurrency_subprice, pd.multicurrency_total_ht, pd.multicurrency_total_tva, pd.multicurrency_total_ttc,';
4038  $sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
4039  $sql .= ' pd.date_start, pd.date_end, pd.product_type';
4040  $sql .= ' FROM '.MAIN_DB_PREFIX.'propaldet as pd';
4041  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pd.fk_product = p.rowid';
4042  $sql .= ' WHERE pd.rowid = '.((int) $rowid);
4043 
4044  $result = $this->db->query($sql);
4045  if ($result) {
4046  $objp = $this->db->fetch_object($result);
4047 
4048  if ($objp) {
4049  $this->id = $objp->rowid;
4050  $this->rowid = $objp->rowid; // deprecated
4051  $this->fk_propal = $objp->fk_propal;
4052  $this->fk_parent_line = $objp->fk_parent_line;
4053  $this->label = $objp->custom_label;
4054  $this->desc = $objp->description;
4055  $this->qty = $objp->qty;
4056  $this->price = $objp->price; // deprecated
4057  $this->subprice = $objp->subprice;
4058  $this->vat_src_code = $objp->vat_src_code;
4059  $this->tva_tx = $objp->tva_tx;
4060  $this->remise = $objp->remise; // deprecated
4061  $this->remise_percent = $objp->remise_percent;
4062  $this->fk_remise_except = $objp->fk_remise_except;
4063  $this->fk_product = $objp->fk_product;
4064  $this->info_bits = $objp->info_bits;
4065 
4066  $this->total_ht = $objp->total_ht;
4067  $this->total_tva = $objp->total_tva;
4068  $this->total_ttc = $objp->total_ttc;
4069 
4070  $this->fk_fournprice = $objp->fk_fournprice;
4071 
4072  $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
4073  $this->pa_ht = $marginInfos[0];
4074  $this->marge_tx = $marginInfos[1];
4075  $this->marque_tx = $marginInfos[2];
4076 
4077  $this->special_code = $objp->special_code;
4078  $this->product_type = $objp->product_type;
4079  $this->rang = $objp->rang;
4080 
4081  $this->ref = $objp->product_ref; // deprecated
4082  $this->product_ref = $objp->product_ref;
4083  $this->libelle = $objp->product_label; // deprecated
4084  $this->product_label = $objp->product_label;
4085  $this->product_desc = $objp->product_desc;
4086  $this->fk_unit = $objp->fk_unit;
4087 
4088  $this->date_start = $this->db->jdate($objp->date_start);
4089  $this->date_end = $this->db->jdate($objp->date_end);
4090 
4091  // Multicurrency
4092  $this->fk_multicurrency = $objp->fk_multicurrency;
4093  $this->multicurrency_code = $objp->multicurrency_code;
4094  $this->multicurrency_subprice = $objp->multicurrency_subprice;
4095  $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
4096  $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
4097  $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
4098 
4099  $this->fetch_optionals();
4100 
4101  $this->db->free($result);
4102 
4103  return 1;
4104  } else {
4105  return 0;
4106  }
4107  } else {
4108  return -1;
4109  }
4110  }
4111 
4118  public function insert($notrigger = 0)
4119  {
4120  global $conf, $user;
4121 
4122  $error = 0;
4123 
4124  dol_syslog(get_class($this)."::insert rang=".$this->rang);
4125 
4126  $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht == ''); // If true, we can use a default value. If this->pa_ht = '0', we must use '0'.
4127 
4128  // Clean parameters
4129  if (empty($this->tva_tx)) {
4130  $this->tva_tx = 0;
4131  }
4132  if (empty($this->localtax1_tx)) {
4133  $this->localtax1_tx = 0;
4134  }
4135  if (empty($this->localtax2_tx)) {
4136  $this->localtax2_tx = 0;
4137  }
4138  if (empty($this->localtax1_type)) {
4139  $this->localtax1_type = 0;
4140  }
4141  if (empty($this->localtax2_type)) {
4142  $this->localtax2_type = 0;
4143  }
4144  if (empty($this->total_localtax1)) {
4145  $this->total_localtax1 = 0;
4146  }
4147  if (empty($this->total_localtax2)) {
4148  $this->total_localtax2 = 0;
4149  }
4150  if (empty($this->rang)) {
4151  $this->rang = 0;
4152  }
4153  if (empty($this->remise_percent) || !is_numeric($this->remise_percent)) {
4154  $this->remise_percent = 0;
4155  }
4156  if (empty($this->info_bits)) {
4157  $this->info_bits = 0;
4158  }
4159  if (empty($this->special_code)) {
4160  $this->special_code = 0;
4161  }
4162  if (empty($this->fk_parent_line)) {
4163  $this->fk_parent_line = 0;
4164  }
4165  if (empty($this->fk_fournprice)) {
4166  $this->fk_fournprice = 0;
4167  }
4168  if (!is_numeric($this->qty)) {
4169  $this->qty = 0;
4170  }
4171  if (empty($this->pa_ht)) {
4172  $this->pa_ht = 0;
4173  }
4174  if (empty($this->multicurrency_subprice)) {
4175  $this->multicurrency_subprice = 0;
4176  }
4177  if (empty($this->multicurrency_total_ht)) {
4178  $this->multicurrency_total_ht = 0;
4179  }
4180  if (empty($this->multicurrency_total_tva)) {
4181  $this->multicurrency_total_tva = 0;
4182  }
4183  if (empty($this->multicurrency_total_ttc)) {
4184  $this->multicurrency_total_ttc = 0;
4185  }
4186 
4187  // if buy price not defined, define buyprice as configured in margin admin
4188  if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4189  if (($result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
4190  return $result;
4191  } else {
4192  $this->pa_ht = $result;
4193  }
4194  }
4195 
4196  // Check parameters
4197  if ($this->product_type < 0) {
4198  return -1;
4199  }
4200 
4201  $this->db->begin();
4202 
4203  // Insert line into database
4204  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'propaldet';
4205  $sql .= ' (fk_propal, fk_parent_line, label, description, fk_product, product_type,';
4206  $sql .= ' fk_remise_except, qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
4207  $sql .= ' subprice, remise_percent, ';
4208  $sql .= ' info_bits, ';
4209  $sql .= ' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_product_fournisseur_price, buy_price_ht, special_code, rang,';
4210  $sql .= ' fk_unit,';
4211  $sql .= ' date_start, date_end';
4212  $sql .= ', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc)';
4213  $sql .= " VALUES (".$this->fk_propal.",";
4214  $sql .= " ".($this->fk_parent_line > 0 ? "'".$this->db->escape($this->fk_parent_line)."'" : "null").",";
4215  $sql .= " ".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
4216  $sql .= " '".$this->db->escape($this->desc)."',";
4217  $sql .= " ".($this->fk_product ? "'".$this->db->escape($this->fk_product)."'" : "null").",";
4218  $sql .= " '".$this->db->escape($this->product_type)."',";
4219  $sql .= " ".($this->fk_remise_except ? "'".$this->db->escape($this->fk_remise_except)."'" : "null").",";
4220  $sql .= " ".price2num($this->qty, 'MS').",";
4221  $sql .= " ".(empty($this->vat_src_code) ? "''" : "'".$this->db->escape($this->vat_src_code)."'").",";
4222  $sql .= " ".price2num($this->tva_tx).",";
4223  $sql .= " ".price2num($this->localtax1_tx).",";
4224  $sql .= " ".price2num($this->localtax2_tx).",";
4225  $sql .= " '".$this->db->escape($this->localtax1_type)."',";
4226  $sql .= " '".$this->db->escape($this->localtax2_type)."',";
4227  $sql .= " ".(price2num($this->subprice) !== '' ? price2num($this->subprice, 'MU') : "null").",";
4228  $sql .= " ".price2num($this->remise_percent, 3).",";
4229  $sql .= " ".(isset($this->info_bits) ? ((int) $this->info_bits) : "null").",";
4230  $sql .= " ".price2num($this->total_ht, 'MT').",";
4231  $sql .= " ".price2num($this->total_tva, 'MT').",";
4232  $sql .= " ".price2num($this->total_localtax1, 'MT').",";
4233  $sql .= " ".price2num($this->total_localtax2, 'MT').",";
4234  $sql .= " ".price2num($this->total_ttc, 'MT').",";
4235  $sql .= " ".(!empty($this->fk_fournprice) ? "'".$this->db->escape($this->fk_fournprice)."'" : "null").",";
4236  $sql .= " ".(isset($this->pa_ht) ? "'".price2num($this->pa_ht)."'" : "null").",";
4237  $sql .= ' '.((int) $this->special_code).',';
4238  $sql .= ' '.((int) $this->rang).',';
4239  $sql .= ' '.(empty($this->fk_unit) ? 'NULL' : ((int) $this->fk_unit)).',';
4240  $sql .= " ".(!empty($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : "null").',';
4241  $sql .= " ".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null");
4242  $sql .= ", ".($this->fk_multicurrency > 0 ? ((int) $this->fk_multicurrency) : 'null');
4243  $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
4244  $sql .= ", ".price2num($this->multicurrency_subprice, 'CU');
4245  $sql .= ", ".price2num($this->multicurrency_total_ht, 'CT');
4246  $sql .= ", ".price2num($this->multicurrency_total_tva, 'CT');
4247  $sql .= ", ".price2num($this->multicurrency_total_ttc, 'CT');
4248  $sql .= ')';
4249 
4250  dol_syslog(get_class($this).'::insert', LOG_DEBUG);
4251  $resql = $this->db->query($sql);
4252  if ($resql) {
4253  $this->rowid = $this->db->last_insert_id(MAIN_DB_PREFIX.'propaldet');
4254 
4255  if (!$error) {
4256  $this->id = $this->rowid;
4257  $result = $this->insertExtraFields();
4258  if ($result < 0) {
4259  $error++;
4260  }
4261  }
4262 
4263  if (!$error && !$notrigger) {
4264  // Call trigger
4265  $result = $this->call_trigger('LINEPROPAL_INSERT', $user);
4266  if ($result < 0) {
4267  $this->db->rollback();
4268  return -1;
4269  }
4270  // End call triggers
4271  }
4272 
4273  $this->db->commit();
4274  return 1;
4275  } else {
4276  $this->error = $this->db->error()." sql=".$sql;
4277  $this->db->rollback();
4278  return -1;
4279  }
4280  }
4281 
4289  public function delete(User $user, $notrigger = 0)
4290  {
4291  global $conf;
4292 
4293  $error = 0;
4294  $this->db->begin();
4295 
4296  if (!$notrigger) {
4297  // Call trigger
4298  $result = $this->call_trigger('LINEPROPAL_DELETE', $user);
4299  if ($result < 0) {
4300  $error++;
4301  }
4302  }
4303  // End call triggers
4304 
4305  if (!$error) {
4306  $sql = "DELETE FROM " . MAIN_DB_PREFIX . "propaldet WHERE rowid = " . ((int) $this->rowid);
4307  dol_syslog("PropaleLigne::delete", LOG_DEBUG);
4308  if ($this->db->query($sql)) {
4309  // Remove extrafields
4310  if (!$error) {
4311  $this->id = $this->rowid;
4312  $result = $this->deleteExtraFields();
4313  if ($result < 0) {
4314  $error++;
4315  dol_syslog(get_class($this) . "::delete error -4 " . $this->error, LOG_ERR);
4316  }
4317  }
4318  } else {
4319  $this->error = $this->db->error() . " sql=" . $sql;
4320  $error++;
4321  }
4322  }
4323 
4324  if ($error) {
4325  $this->db->rollback();
4326  return -1;
4327  } else {
4328  $this->db->commit();
4329  return 1;
4330  }
4331  }
4332 
4339  public function update($notrigger = 0)
4340  {
4341  global $conf, $user;
4342 
4343  $error = 0;
4344 
4345  $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht == ''); // If true, we can use a default value. If this->pa_ht = '0', we must use '0'.
4346 
4347  if (empty($this->id) && !empty($this->rowid)) {
4348  $this->id = $this->rowid;
4349  }
4350 
4351  // Clean parameters
4352  if (empty($this->tva_tx)) {
4353  $this->tva_tx = 0;
4354  }
4355  if (empty($this->localtax1_tx)) {
4356  $this->localtax1_tx = 0;
4357  }
4358  if (empty($this->localtax2_tx)) {
4359  $this->localtax2_tx = 0;
4360  }
4361  if (empty($this->total_localtax1)) {
4362  $this->total_localtax1 = 0;
4363  }
4364  if (empty($this->total_localtax2)) {
4365  $this->total_localtax2 = 0;
4366  }
4367  if (empty($this->localtax1_type)) {
4368  $this->localtax1_type = 0;
4369  }
4370  if (empty($this->localtax2_type)) {
4371  $this->localtax2_type = 0;
4372  }
4373  if (empty($this->marque_tx)) {
4374  $this->marque_tx = 0;
4375  }
4376  if (empty($this->marge_tx)) {
4377  $this->marge_tx = 0;
4378  }
4379  if (empty($this->price)) {
4380  $this->price = 0; // TODO A virer
4381  }
4382  if (empty($this->remise_percent)) {
4383  $this->remise_percent = 0;
4384  }
4385  if (empty($this->info_bits)) {
4386  $this->info_bits = 0;
4387  }
4388  if (empty($this->special_code)) {
4389  $this->special_code = 0;
4390  }
4391  if (empty($this->fk_parent_line)) {
4392  $this->fk_parent_line = 0;
4393  }
4394  if (empty($this->fk_fournprice)) {
4395  $this->fk_fournprice = 0;
4396  }
4397  if (empty($this->subprice)) {
4398  $this->subprice = 0;
4399  }
4400  if (empty($this->pa_ht)) {
4401  $this->pa_ht = 0;
4402  }
4403 
4404  // if buy price not defined, define buyprice as configured in margin admin
4405  if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
4406  if (($result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
4407  return $result;
4408  } else {
4409  $this->pa_ht = $result;
4410  }
4411  }
4412 
4413  $this->db->begin();
4414 
4415  // Mise a jour ligne en base
4416  $sql = "UPDATE ".MAIN_DB_PREFIX."propaldet SET";
4417  $sql .= " description='".$this->db->escape($this->desc)."'";
4418  $sql .= ", label=".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null");
4419  $sql .= ", product_type=".$this->product_type;
4420  $sql .= ", vat_src_code = '".(empty($this->vat_src_code) ? '' : $this->vat_src_code)."'";
4421  $sql .= ", tva_tx='".price2num($this->tva_tx)."'";
4422  $sql .= ", localtax1_tx=".price2num($this->localtax1_tx);
4423  $sql .= ", localtax2_tx=".price2num($this->localtax2_tx);
4424  $sql .= ", localtax1_type='".$this->db->escape($this->localtax1_type)."'";
4425  $sql .= ", localtax2_type='".$this->db->escape($this->localtax2_type)."'";
4426  $sql .= ", qty='".price2num($this->qty)."'";
4427  $sql .= ", subprice=".price2num($this->subprice)."";
4428  $sql .= ", remise_percent=".price2num($this->remise_percent)."";
4429  $sql .= ", price=".(float) price2num($this->price).""; // TODO A virer
4430  $sql .= ", remise=".(float) price2num($this->remise).""; // TODO A virer
4431  $sql .= ", info_bits='".$this->db->escape($this->info_bits)."'";
4432  if (empty($this->skip_update_total)) {
4433  $sql .= ", total_ht=".price2num($this->total_ht)."";
4434  $sql .= ", total_tva=".price2num($this->total_tva)."";
4435  $sql .= ", total_ttc=".price2num($this->total_ttc)."";
4436  $sql .= ", total_localtax1=".price2num($this->total_localtax1)."";
4437  $sql .= ", total_localtax2=".price2num($this->total_localtax2)."";
4438  }
4439  $sql .= ", fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? "'".$this->db->escape($this->fk_fournprice)."'" : "null");
4440  $sql .= ", buy_price_ht=".price2num($this->pa_ht);
4441  if (strlen($this->special_code)) {
4442  $sql .= ", special_code=".$this->special_code;
4443  }
4444  $sql .= ", fk_parent_line=".($this->fk_parent_line > 0 ? $this->fk_parent_line : "null");
4445  if (!empty($this->rang)) {
4446  $sql .= ", rang=".((int) $this->rang);
4447  }
4448  $sql .= ", date_start=".(!empty($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : "null");
4449  $sql .= ", date_end=".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null");
4450  $sql .= ", fk_unit=".(!$this->fk_unit ? 'NULL' : $this->fk_unit);
4451 
4452  // Multicurrency
4453  $sql .= ", multicurrency_subprice=".price2num($this->multicurrency_subprice)."";
4454  $sql .= ", multicurrency_total_ht=".price2num($this->multicurrency_total_ht)."";
4455  $sql .= ", multicurrency_total_tva=".price2num($this->multicurrency_total_tva)."";
4456  $sql .= ", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc)."";
4457 
4458  $sql .= " WHERE rowid = ".((int) $this->id);
4459 
4460  dol_syslog(get_class($this)."::update", LOG_DEBUG);
4461  $resql = $this->db->query($sql);
4462  if ($resql) {
4463  if (!$error) {
4464  $result = $this->insertExtraFields();
4465  if ($result < 0) {
4466  $error++;
4467  }
4468  }
4469 
4470  if (!$error && !$notrigger) {
4471  // Call trigger
4472  $result = $this->call_trigger('LINEPROPAL_MODIFY', $user);
4473  if ($result < 0) {
4474  $this->db->rollback();
4475  return -1;
4476  }
4477  // End call triggers
4478  }
4479 
4480  $this->db->commit();
4481  return 1;
4482  } else {
4483  $this->error = $this->db->error();
4484  $this->db->rollback();
4485  return -2;
4486  }
4487  }
4488 
4489  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
4496  public function update_total()
4497  {
4498  // phpcs:enable
4499  $this->db->begin();
4500 
4501  // Mise a jour ligne en base
4502  $sql = "UPDATE ".MAIN_DB_PREFIX."propaldet SET";
4503  $sql .= " total_ht=".price2num($this->total_ht, 'MT')."";
4504  $sql .= ",total_tva=".price2num($this->total_tva, 'MT')."";
4505  $sql .= ",total_ttc=".price2num($this->total_ttc, 'MT')."";
4506  $sql .= " WHERE rowid = ".((int) $this->rowid);
4507 
4508  dol_syslog("PropaleLigne::update_total", LOG_DEBUG);
4509 
4510  $resql = $this->db->query($sql);
4511  if ($resql) {
4512  $this->db->commit();
4513  return 1;
4514  } else {
4515  $this->error = $this->db->error();
4516  $this->db->rollback();
4517  return -2;
4518  }
4519  }
4520 }
Propal\closeProposal
closeProposal($user, $status, $note='', $notrigger=0)
Close/set the commercial proposal to status signed or refused (fill also date signature)
Definition: propal.class.php:2620
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
getLocalTaxesFromRate
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
Definition: functions.lib.php:6097
Propal\STATUS_SIGNED
const STATUS_SIGNED
Signed quote.
Definition: propal.class.php:360
db
$conf db
API class for accounts.
Definition: inc.php:41
Propal\setDeliveryDate
setDeliveryDate($user, $delivery_date, $notrigger=0)
Set delivery date.
Definition: propal.class.php:2196
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1468
Propal\getLinesArray
getLinesArray($filters='')
Retrieve an array of proposal lines.
Definition: propal.class.php:3802
Propal\InvoiceArrayList
InvoiceArrayList($id)
Returns an array with id and ref of related invoices.
Definition: propal.class.php:2952
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1226
Propal\demand_reason
demand_reason($demand_reason_id, $notrigger=0)
Change source demand.
Definition: propal.class.php:3210
Propal\LibStatut
LibStatut($status, $mode=1)
Return label of a status (draft, validated, ...)
Definition: propal.class.php:3341
dol_delete_dir_recursive
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
Definition: files.lib.php:1383
Propal\reopen
reopen($user, $status, $note='', $notrigger=0)
Reopen the commercial proposal.
Definition: propal.class.php:2561
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
Productcustomerprice
File of class to manage predefined price products or services by customer.
Definition: productcustomerprice.class.php:29
Propal\set_demand_reason
set_demand_reason($user, $id, $notrigger=0)
Set source of demand.
Definition: propal.class.php:2315
Propal\liste_array
liste_array($shortlist=0, $draft=0, $notcurrentuser=0, $socid=0, $limit=0, $offset=0, $sortfield='p.datep', $sortorder='DESC')
Return list of proposal (eventually filtered on user) into an array.
Definition: propal.class.php:2872
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
PropaleLigne\update_total
update_total()
Update DB line fields total_xxx Used by migration.
Definition: propal.class.php:4496
CommonObject\updateRangOfLine
updateRangOfLine($rowid, $rang)
Update position of line (rang)
Definition: commonobject.class.php:3219
CommonObject\fetchObjectLinked
fetchObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $clause='OR', $alsosametype=1, $orderby='sourcetype', $loadalsoobjects=1)
Fetch array of objects linked to current object (object of enabled modules only).
Definition: commonobject.class.php:3916
PropaleLigne
Class to manage commercial proposal lines.
Definition: propal.class.php:3878
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
Propal\getNextNumRef
getNextNumRef($soc)
Returns the reference to the following non used Proposal used depending on the active numbering modul...
Definition: propal.class.php:3603
Propal\$table_ref_field
$table_ref_field
{}
Definition: propal.class.php:96
dol_include_once
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
Definition: functions.lib.php:1033
Translate
Class to manage translations.
Definition: translate.class.php:30
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1062
MultiCurrency\getIdFromCode
static getIdFromCode($dbs, $code)
Get id of currency from code.
Definition: multicurrency.class.php:501
PropaleLigne\$fk_product_type
$fk_product_type
Definition: propal.class.php:3901
CommonIncoterm
trait CommonIncoterm
Superclass for incoterm classes.
Definition: commonincoterm.class.php:29
Propal\$remise
$remise
Definition: propal.class.php:216
Propal\$price
$price
Definition: propal.class.php:196
CommonObject\deleteObjectLinked
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid='', $f_user=null, $notrigger=0)
Delete all links between an object $this.
Definition: commonobject.class.php:4228
ref
$object ref
Definition: info.php:77
CommonObject\commonReplaceProduct
static commonReplaceProduct(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
Definition: commonobject.class.php:8376
PropaleLigne\__construct
__construct($db)
Class line Contructor.
Definition: propal.class.php:4019
rowid
print *****$script_file(".$version.") pid c cd cd cd description as p label as s rowid
Definition: email_expire_services_to_representatives.php:79
dol_dir_list
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:60
Propal\load_board
load_board($user, $mode)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
Definition: propal.class.php:3387
Propal\$datep
$datep
Definition: propal.class.php:172
Propal\getNomUrl
getNomUrl($withpicto=0, $option='', $get_params='', $notooltip=0, $save_lastsearch_value=-1, $addlinktonotes=-1)
Return clicable link of object (with eventually picto)
Definition: propal.class.php:3658
CommonObjectLine
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Definition: commonobjectline.class.php:32
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
Propal\insert_discount
insert_discount($idremise)
Adding line of fixed discount in the proposal in DB.
Definition: propal.class.php:461
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
WorkboardResponse
Definition: workboardresponse.class.php:25
CommonObject\fetch_thirdparty
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id $this->socid or $this->fk_soc, into this->thirdparty.
Definition: commonobject.class.php:1736
Propal\STATUS_DRAFT
const STATUS_DRAFT
Draft status.
Definition: propal.class.php:352
dol_concatdesc
dol_concatdesc($text1, $text2, $forxml=false, $invert=false)
Concat 2 descriptions with a new line between them (second operand after first one with appropriate n...
Definition: functions.lib.php:7248
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
Propal\updateline
updateline($rowid, $pu, $qty, $remise_percent, $txtva, $txlocaltax1=0.0, $txlocaltax2=0.0, $desc='', $price_base_type='HT', $info_bits=0, $special_code=0, $fk_parent_line=0, $skip_update_total=0, $fk_fournprice=0, $pa_ht=0, $label='', $type=0, $date_start='', $date_end='', $array_options=0, $fk_unit=null, $pu_ht_devise=0, $notrigger=0, $rang=0)
Update a proposal line.
Definition: propal.class.php:810
Propal\__construct
__construct($db, $socid=0, $propalid=0)
Constructor.
Definition: propal.class.php:378
CommonObject\commonGenerateDocument
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
Definition: commonobject.class.php:5406
calcul_price_total
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller='', $localtaxes_array='', $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
Definition: price.lib.php:86
dol_delete_file
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
Definition: files.lib.php:1231
Propal\setDraft
setDraft($user, $notrigger=0)
Set draft status.
Definition: propal.class.php:2804
Propal\getLibStatut
getLibStatut($mode=0)
Return label of status of proposal (draft, validated, ...)
Definition: propal.class.php:3328
CommonObject\update_price
update_price($exclspec=0, $roundingadjust='none', $nodatabaseupdate=0, $seller=null)
Update total_ht, total_ttc, total_vat, total_localtax1, total_localtax2 for an object (sum of lines).
Definition: commonobject.class.php:3525
Propal\set_remise_percent
set_remise_percent($user, $remise, $notrigger=0)
Set an overall discount on the proposal.
Definition: propal.class.php:2437
Propal\$datev
$datev
Definition: propal.class.php:146
Propal\classifyBilled
classifyBilled(User $user, $notrigger=0, $note='')
Classify the proposal to status Billed.
Definition: propal.class.php:2719
Propal\STATUS_BILLED
const STATUS_BILLED
Billed or processed quote.
Definition: propal.class.php:368
get_default_npr
get_default_npr(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Fonction qui renvoie si tva doit etre tva percue recuperable.
Definition: functions.lib.php:6405
getEntity
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
Definition: functions.lib.php:148
Propal\generateDocument
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
Definition: propal.class.php:3818
get_localtax
get_localtax($vatrate, $local, $thirdparty_buyer="", $thirdparty_seller="", $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate,...
Definition: functions.lib.php:5837
PropaleLigne\$libelle
$libelle
Definition: propal.class.php:3964
Propal\fetch
fetch($rowid, $ref='', $ref_ext='', $forceentity=0)
Load a proposal from database.
Definition: propal.class.php:1524
CommonObject\insertExtraFields
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Definition: commonobject.class.php:6156
CommonObject\isExistingObject
static isExistingObject($element, $id, $ref='', $ref_ext='')
Check an object id/ref exists If you don't need/want to instantiate object and just need to know if o...
Definition: commonobject.class.php:617
PropaleLigne\$price
$price
Definition: propal.class.php:3947
Propal\deleteline
deleteline($lineid)
Delete detail line.
Definition: propal.class.php:984
Propal\replaceThirdparty
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: propal.class.php:3848
CommonObject\delete_linked_contact
delete_linked_contact($source='', $code='')
Delete all links between an object $this and all its contacts in llx_element_contact.
Definition: commonobject.class.php:1319
Propal\STATUS_VALIDATED
const STATUS_VALIDATED
Validated status.
Definition: propal.class.php:356
PropaleLigne\$label
$label
Definition: propal.class.php:3969
CommonObject\defineBuyPrice
defineBuyPrice($unitPrice=0.0, $discountPercent=0.0, $fk_product=0)
Get buy price to use for margin calculation.
Definition: commonobject.class.php:8405
dol_string_nohtmltag
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
Definition: functions.lib.php:6694
Propal\fetch_lines
fetch_lines($only_product=0, $loadalsotranslation=0, $filters='')
Load array lines.
Definition: propal.class.php:1811
CommonObject\deleteEcmFiles
deleteEcmFiles($mode=0)
Delete related files of object in database.
Definition: commonobject.class.php:9966
CommonObject\commonReplaceThirdparty
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
Definition: commonobject.class.php:8347
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
DiscountAbsolute
Class to manage absolute discounts.
Definition: discount.class.php:29
Propal\$remise_absolue
$remise_absolue
Definition: propal.class.php:220
MultiCurrency\getIdAndTxFromCode
static getIdAndTxFromCode($dbs, $code, $date_document='')
Get id and rate of currency from code.
Definition: multicurrency.class.php:526
CommonObject\line_max
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
Definition: commonobject.class.php:3373
CommonObject\fetch_optionals
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...
Definition: commonobject.class.php:6007
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
get_default_tva
get_default_tva(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Function that return vat rate of a product line (according to seller, buyer and product vat rate) VAT...
Definition: functions.lib.php:6304
Propal\$total
$total
Definition: propal.class.php:206
Propal\createFromClone
createFromClone(User $user, $socid=0, $forceentity=null, $update_prices=false)
Load an object from its id and create a new one in database.
Definition: propal.class.php:1345
User
Class to manage Dolibarr users.
Definition: user.class.php:44
Propal\create
create($user, $notrigger=0)
Create commercial proposal into database this->ref can be set or empty.
Definition: propal.class.php:1022
Propal\getInvoiceArrayList
getInvoiceArrayList()
Returns an array with the numbers of related invoices.
Definition: propal.class.php:2940
CommonObject\deleteExtraFields
deleteExtraFields()
Delete all extra fields values for the current object.
Definition: commonobject.class.php:6116
Propal\set_availability
set_availability($user, $id, $notrigger=0)
Set delivery.
Definition: propal.class.php:2252
CommonObject\$note
$note
Definition: commonobject.class.php:444
Propal\set_remise_absolue
set_remise_absolue($user, $remise, $notrigger=0)
Set an absolute overall discount on the proposal.
Definition: propal.class.php:2498
Product
Class to manage products or services.
Definition: product.class.php:46
getMarginInfos
getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
Return an array with margins information of a line.
Definition: margins.lib.php:117
PropaleLigne\fetch
fetch($rowid)
Retrieve the propal line object.
Definition: propal.class.php:4030
dol_delete_preview
dol_delete_preview($object)
Delete all preview files linked to object instance.
Definition: files.lib.php:1435
CommonObject\add_object_linked
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
Definition: commonobject.class.php:3818
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10338
Propal\addline
addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1=0.0, $txlocaltax2=0.0, $fk_product=0, $remise_percent=0.0, $price_base_type='HT', $pu_ttc=0.0, $info_bits=0, $type=0, $rang=-1, $special_code=0, $fk_parent_line=0, $fk_fournprice=0, $pa_ht=0, $label='', $date_start='', $date_end='', $array_options=0, $fk_unit=null, $origin='', $origin_id=0, $pu_ht_devise=0, $fk_remise_except=0, $noupdateafterinsertline=0)
Add a proposal line into database (linked to product/service or not) The parameters are already suppo...
Definition: propal.class.php:563
Propal\$tva
$tva
Definition: propal.class.php:201
PropaleLigne\$ref
$ref
Definition: propal.class.php:3954
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
Propal\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: propal.class.php:3471
Propal\info
info($id)
Object Proposal Information.
Definition: propal.class.php:3272
PropaleLigne\$remise
$remise
Definition: propal.class.php:3942
Propal\update
update(User $user, $notrigger=0)
Update database.
Definition: propal.class.php:1699
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
Propal\replaceProduct
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
Definition: propal.class.php:3865
price
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
Definition: functions.lib.php:5541
Propal\valid
valid($user, $notrigger=0)
Set status to validated.
Definition: propal.class.php:1944
Propal\load_state_board
load_state_board()
Charge indicateurs this->nb de tableau de bord.
Definition: propal.class.php:3562
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5791
PropaleLigne\insert
insert($notrigger=0)
Insert object line propal in database.
Definition: propal.class.php:4118
Product\TYPE_PRODUCT
const TYPE_PRODUCT
Regular product.
Definition: product.class.php:500
Propal
Class to manage proposals.
Definition: propal.class.php:52
Propal\add_product
add_product($idproduct, $qty, $remise_percent=0)
Add line into array ->lines $this->thirdparty should be loaded.
Definition: propal.class.php:403
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:93
Propal\set_date_livraison
set_date_livraison($user, $delivery_date, $notrigger=0)
Set delivery date.
Definition: propal.class.php:2182
CommonObject\line_order
line_order($renum=false, $rowidorder='ASC', $fk_parent_line=true)
Save a new position (field rang) for details lines.
Definition: commonobject.class.php:3055
Propal\set_ref_client
set_ref_client($user, $ref_client, $notrigger=0)
Set customer reference number.
Definition: propal.class.php:2380
Propal\$datec
$datec
Definition: propal.class.php:135
Propal\STATUS_NOTSIGNED
const STATUS_NOTSIGNED
Not signed quote.
Definition: propal.class.php:364
Propal\set_echeance
set_echeance($user, $date_fin_validite, $notrigger=0)
Define end validity date.
Definition: propal.class.php:2125
PropaleLigne\update
update($notrigger=0)
Update propal line object into DB.
Definition: propal.class.php:4339
Propal\set_date
set_date($user, $date, $notrigger=0)
Define proposal date.
Definition: propal.class.php:2063
Propal\availability
availability($availability_id, $notrigger=0)
Change the delivery time.
Definition: propal.class.php:3147
float
div float
Buy price without taxes.
Definition: style.css.php:809