dolibarr  13.0.2
supplier_proposal.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-2018 Philippe Grand <philippe.grand@atoo-net.com>
11  * Copyright (C) 2012-2014 Christophe Battarel <christophe.battarel@altairis.fr>
12  * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
13  * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
14  * Copyright (C) 2016 Ferran Marcet <fmarcet@2byte.es>
15  * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
16  * Copyright (C) 2019-2020 Frédéric France <frederic.france@netlogic.fr>
17  * Copyright (C) 2020 Tobias Sekan <tobias.sekan@startmail.com>
18  *
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <https://www.gnu.org/licenses/>.
31  */
32 
38 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
39 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
40 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
41 require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
42 require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php';
43 require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
44 require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php';
45 
50 {
51  use CommonIncoterm;
52 
56  public $element = 'supplier_proposal';
57 
61  public $table_element = 'supplier_proposal';
62 
66  public $table_element_line = 'supplier_proposaldet';
67 
71  public $fk_element = 'fk_supplier_proposal';
72 
76  public $picto = 'supplier_proposal';
77 
82  public $ismultientitymanaged = 1;
83 
88  public $restrictiononfksoc = 1;
89 
93  protected $table_ref_field = 'ref';
94 
95  public $socid; // Id client
96 
101  public $author;
102 
103  public $ref_fourn; //Reference saisie lors de l'ajout d'une ligne à la demande
104  public $ref_supplier; //Reference saisie lors de l'ajout d'une ligne à la demande
105  public $statut; // 0 (draft), 1 (validated), 2 (signed), 3 (not signed), 4 (processed/billed)
106 
110  public $date;
111 
116  public $date_livraison;
117 
121  public $delivery_date;
122 
127  public $datec;
128 
132  public $date_creation;
133 
138  public $datev;
139 
143  public $date_validation;
144 
145 
146  public $user_author_id;
147  public $user_valid_id;
148  public $user_close_id;
149 
154  public $price;
155 
160  public $tva;
161 
166  public $total;
167 
168  public $cond_reglement_code;
169  public $mode_reglement_code;
170  public $remise = 0;
171  public $remise_percent = 0;
172  public $remise_absolue = 0;
173 
174  public $extraparams = array();
175  public $lines = array();
176  public $line;
177 
178  public $labelStatus = array();
179  public $labelStatusShort = array();
180 
181  public $nbtodo;
182  public $nbtodolate;
183 
184  // Multicurrency
188  public $fk_multicurrency;
189 
190  public $multicurrency_code;
191  public $multicurrency_tx;
192  public $multicurrency_total_ht;
193  public $multicurrency_total_tva;
194  public $multicurrency_total_ttc;
195 
199  const STATUS_DRAFT = 0;
200 
204  const STATUS_VALIDATED = 1;
205 
209  const STATUS_SIGNED = 2;
210 
214  const STATUS_NOTSIGNED = 3;
215 
219  const STATUS_CLOSE = 4;
220 
221 
222 
230  public function __construct($db, $socid = "", $supplier_proposalid = 0)
231  {
232  global $conf, $langs;
233 
234  $this->db = $db;
235 
236  $this->socid = $socid;
237  $this->id = $supplier_proposalid;
238  }
239 
240 
241  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
252  public function add_product($idproduct, $qty, $remise_percent = 0)
253  {
254  // phpcs:enable
255  global $conf, $mysoc;
256 
257  if (!$qty) $qty = 1;
258 
259  dol_syslog(get_class($this)."::add_product $idproduct, $qty, $remise_percent");
260  if ($idproduct > 0)
261  {
262  $prod = new Product($this->db);
263  $prod->fetch($idproduct);
264 
265  $productdesc = $prod->description;
266 
267  $tva_tx = get_default_tva($mysoc, $this->thirdparty, $prod->id);
268  $tva_npr = get_default_npr($mysoc, $this->thirdparty, $prod->id);
269  if (empty($tva_tx)) $tva_npr = 0;
270  $localtax1_tx = get_localtax($tva_tx, 1, $mysoc, $this->thirdparty, $tva_npr);
271  $localtax2_tx = get_localtax($tva_tx, 2, $mysoc, $this->thirdparty, $tva_npr);
272 
273  // multiprix
274  if ($conf->global->PRODUIT_MULTIPRICES && $this->thirdparty->price_level)
275  {
276  $price = $prod->multiprices[$this->thirdparty->price_level];
277  } else {
278  $price = $prod->price;
279  }
280 
281  $line = new SupplierProposalLine($this->db);
282 
283  $line->fk_product = $idproduct;
284  $line->desc = $productdesc;
285  $line->qty = $qty;
286  $line->subprice = $price;
287  $line->remise_percent = $remise_percent;
288  $line->tva_tx = $tva_tx;
289 
290  $this->lines[] = $line;
291  }
292  }
293 
294  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
301  public function insert_discount($idremise)
302  {
303  // phpcs:enable
304  global $langs;
305 
306  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
307  include_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
308 
309  $this->db->begin();
310 
311  $remise = new DiscountAbsolute($this->db);
312  $result = $remise->fetch($idremise);
313 
314  if ($result > 0)
315  {
316  if ($remise->fk_facture) // Protection against multiple submission
317  {
318  $this->error = $langs->trans("ErrorDiscountAlreadyUsed");
319  $this->db->rollback();
320  return -5;
321  }
322 
323  $supplier_proposalligne = new SupplierProposalLine($this->db);
324  $supplier_proposalligne->fk_supplier_proposal = $this->id;
325  $supplier_proposalligne->fk_remise_except = $remise->id;
326  $supplier_proposalligne->desc = $remise->description; // Description ligne
327  $supplier_proposalligne->tva_tx = $remise->tva_tx;
328  $supplier_proposalligne->subprice = -$remise->amount_ht;
329  $supplier_proposalligne->fk_product = 0; // Id produit predefini
330  $supplier_proposalligne->qty = 1;
331  $supplier_proposalligne->remise = 0;
332  $supplier_proposalligne->remise_percent = 0;
333  $supplier_proposalligne->rang = -1;
334  $supplier_proposalligne->info_bits = 2;
335 
336  $supplier_proposalligne->total_ht = -$remise->amount_ht;
337  $supplier_proposalligne->total_tva = -$remise->amount_tva;
338  $supplier_proposalligne->total_ttc = -$remise->amount_ttc;
339 
340  $result = $supplier_proposalligne->insert();
341  if ($result > 0)
342  {
343  $result = $this->update_price(1);
344  if ($result > 0)
345  {
346  $this->db->commit();
347  return 1;
348  } else {
349  $this->db->rollback();
350  return -1;
351  }
352  } else {
353  $this->error = $supplier_proposalligne->error;
354  $this->db->rollback();
355  return -2;
356  }
357  } else {
358  $this->db->rollback();
359  return -2;
360  }
361  }
362 
400  public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $fk_product = 0, $remise_percent = 0, $price_base_type = 'HT', $pu_ttc = 0, $info_bits = 0, $type = 0, $rang = -1, $special_code = 0, $fk_parent_line = 0, $fk_fournprice = 0, $pa_ht = 0, $label = '', $array_options = 0, $ref_supplier = '', $fk_unit = '', $origin = '', $origin_id = 0, $pu_ht_devise = 0, $date_start = 0, $date_end = 0)
401  {
402  global $mysoc, $conf, $langs;
403 
404  dol_syslog(get_class($this)."::addline supplier_proposalid=$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");
405  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
406 
407  // Clean parameters
408  if (empty($remise_percent)) $remise_percent = 0;
409  if (empty($qty)) $qty = 0;
410  if (empty($info_bits)) $info_bits = 0;
411  if (empty($rang)) $rang = 0;
412  if (empty($fk_parent_line) || $fk_parent_line < 0) $fk_parent_line = 0;
413  if (empty($pu_ht)) $pu_ht = 0;
414 
415  $remise_percent = price2num($remise_percent);
416  $qty = price2num($qty);
417  $pu_ht = price2num($pu_ht);
418  $pu_ttc = price2num($pu_ttc);
419  if (!preg_match('/\((.*)\)/', $txtva)) {
420  $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
421  }
422  $txlocaltax1 = price2num($txlocaltax1);
423  $txlocaltax2 = price2num($txlocaltax2);
424  $pa_ht = price2num($pa_ht);
425  if ($price_base_type == 'HT')
426  {
427  $pu = $pu_ht;
428  } else {
429  $pu = $pu_ttc;
430  }
431 
432  // Check parameters
433  if ($type < 0) return -1;
434 
435  if ($this->statut == self::STATUS_DRAFT)
436  {
437  $this->db->begin();
438 
439  if ($fk_product > 0)
440  {
441  if (!empty($conf->global->SUPPLIER_PROPOSAL_WITH_PREDEFINED_PRICES_ONLY))
442  {
443  // Check quantity is enough
444  dol_syslog(get_class($this)."::addline we check supplier prices fk_product=".$fk_product." fk_fournprice=".$fk_fournprice." qty=".$qty." ref_supplier=".$ref_supplier);
445  $prod = new Product($this->db);
446  if ($prod->fetch($fk_product) > 0)
447  {
448  $product_type = $prod->type;
449  $label = $prod->label;
450  $fk_prod_fourn_price = $fk_fournprice;
451 
452  // We use 'none' instead of $ref_supplier, because fourn_ref may not exists anymore. So we will take the first supplier price ok.
453  // If we want a dedicated supplier price, we must provide $fk_prod_fourn_price.
454  $result = $prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product, 'none', ($this->fk_soc ? $this->fk_soc : $this->socid)); // Search on couple $fk_prod_fourn_price/$qty first, then on triplet $qty/$fk_product/$ref_supplier/$this->fk_soc
455  if ($result > 0)
456  {
457  $pu = $prod->fourn_pu; // Unit price supplier price set by get_buyprice
458  $ref_supplier = $prod->ref_supplier; // Ref supplier price set by get_buyprice
459  // is remise percent not keyed but present for the product we add it
460  if ($remise_percent == 0 && $prod->remise_percent != 0)
461  $remise_percent = $prod->remise_percent;
462  }
463  if ($result == 0) // If result == 0, we failed to found the supplier reference price
464  {
465  $langs->load("errors");
466  $this->error = "Ref ".$prod->ref." ".$langs->trans("ErrorQtyTooLowForThisSupplier");
467  $this->db->rollback();
468  dol_syslog(get_class($this)."::addline we did not found supplier price, so we can't guess unit price");
469  //$pu = $prod->fourn_pu; // We do not overwrite unit price
470  //$ref = $prod->ref_fourn; // We do not overwrite ref supplier price
471  return -1;
472  }
473  if ($result == -1)
474  {
475  $langs->load("errors");
476  $this->error = "Ref ".$prod->ref." ".$langs->trans("ErrorQtyTooLowForThisSupplier");
477  $this->db->rollback();
478  dol_syslog(get_class($this)."::addline result=".$result." - ".$this->error, LOG_DEBUG);
479  return -1;
480  }
481  if ($result < -1)
482  {
483  $this->error = $prod->error;
484  $this->errors = $prod->errors;
485  $this->db->rollback();
486  dol_syslog(get_class($this)."::addline result=".$result." - ".$this->error, LOG_ERR);
487  return -1;
488  }
489  } else {
490  $this->error = $prod->error;
491  $this->errors = $prod->errors;
492  $this->db->rollback();
493  return -1;
494  }
495  }
496  } else {
497  $product_type = $type;
498  }
499 
500  // Calcul du total TTC et de la TVA pour la ligne a partir de
501  // qty, pu, remise_percent et txtva
502  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
503  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
504 
505  $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
506 
507  // Clean vat code
508  $reg = array();
509  $vat_src_code = '';
510  if (preg_match('/\((.*)\)/', $txtva, $reg))
511  {
512  $vat_src_code = $reg[1];
513  $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
514  }
515 
516  if (!empty($conf->multicurrency->enabled) && $pu_ht_devise > 0) {
517  $pu = 0;
518  }
519 
520  $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
521  $total_ht = $tabprice[0];
522  $total_tva = $tabprice[1];
523  $total_ttc = $tabprice[2];
524  $total_localtax1 = $tabprice[9];
525  $total_localtax2 = $tabprice[10];
526  $pu = $pu_ht = $tabprice[3];
527 
528  // MultiCurrency
529  $multicurrency_total_ht = $tabprice[16];
530  $multicurrency_total_tva = $tabprice[17];
531  $multicurrency_total_ttc = $tabprice[18];
532  $pu_ht_devise = $tabprice[19];
533 
534  // Rang to use
535  $ranktouse = $rang;
536  if ($ranktouse == -1)
537  {
538  $rangmax = $this->line_max($fk_parent_line);
539  $ranktouse = $rangmax + 1;
540  }
541 
542  // TODO A virer
543  // Anciens indicateurs: $price, $remise (a ne plus utiliser)
544  $price = $pu;
545  $remise = 0;
546  if ($remise_percent > 0)
547  {
548  $remise = round(($pu * $remise_percent / 100), 2);
549  $price = $pu - $remise;
550  }
551 
552  // Insert line
553  $this->line = new SupplierProposalLine($this->db);
554 
555  $this->line->fk_supplier_proposal = $this->id;
556  $this->line->label = $label;
557  $this->line->desc = $desc;
558  $this->line->qty = $qty;
559 
560  $this->line->vat_src_code = $vat_src_code;
561  $this->line->tva_tx = $txtva;
562  $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
563  $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
564  $this->line->localtax1_type = empty($localtaxes_type[0]) ? '' : $localtaxes_type[0];
565  $this->line->localtax2_type = empty($localtaxes_type[2]) ? '' : $localtaxes_type[2];
566  $this->line->fk_product = $fk_product;
567  $this->line->remise_percent = $remise_percent;
568  $this->line->subprice = $pu_ht;
569  $this->line->rang = $ranktouse;
570  $this->line->info_bits = $info_bits;
571  $this->line->total_ht = $total_ht;
572  $this->line->total_tva = $total_tva;
573  $this->line->total_localtax1 = $total_localtax1;
574  $this->line->total_localtax2 = $total_localtax2;
575  $this->line->total_ttc = $total_ttc;
576  $this->line->product_type = $type;
577  $this->line->special_code = $special_code;
578  $this->line->fk_parent_line = $fk_parent_line;
579  $this->line->fk_unit = $fk_unit;
580  $this->line->origin = $origin;
581  $this->line->origin_id = $origin_id;
582  $this->line->ref_fourn = $this->db->escape($ref_supplier);
583  $this->line->date_start = $date_start;
584  $this->line->date_end = $date_end;
585 
586  // infos marge
587  if (!empty($fk_product) && empty($fk_fournprice) && empty($pa_ht)) {
588  // When fk_fournprice is 0, we take the lowest buying price
589  include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
590  $productFournisseur = new ProductFournisseur($this->db);
591  $productFournisseur->find_min_price_product_fournisseur($fk_product);
592  $this->line->fk_fournprice = $productFournisseur->product_fourn_price_id;
593  } else {
594  $this->line->fk_fournprice = ($fk_fournprice > 0 ? $fk_fournprice : 0); // If fk_fournprice is -1, we will not use fk_fournprice
595  }
596  $this->line->pa_ht = $pa_ht;
597  //var_dump($this->line->fk_fournprice);exit;
598 
599  // Multicurrency
600  $this->line->fk_multicurrency = $this->fk_multicurrency;
601  $this->line->multicurrency_code = $this->multicurrency_code;
602  $this->line->multicurrency_subprice = $pu_ht_devise;
603  $this->line->multicurrency_total_ht = $multicurrency_total_ht;
604  $this->line->multicurrency_total_tva = $multicurrency_total_tva;
605  $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
606 
607  // Mise en option de la ligne
608  if (empty($qty) && empty($special_code)) $this->line->special_code = 3;
609 
610  if (is_array($array_options) && count($array_options) > 0) {
611  $this->line->array_options = $array_options;
612  }
613 
614  $result = $this->line->insert();
615  if ($result > 0)
616  {
617  // Reorder if child line
618  if (!empty($fk_parent_line)) $this->line_order(true, 'DESC');
619 
620  // Mise a jour informations denormalisees au niveau de la propale meme
621  $result = $this->update_price(1, 'auto', 0, $this->thirdparty); // This method is designed to add line from user input so total calculation must be done using 'auto' mode.
622  if ($result > 0)
623  {
624  $this->db->commit();
625  return $this->line->id;
626  } else {
627  $this->error = $this->error();
628  $this->errors = $this->errors();
629  $this->db->rollback();
630  return -1;
631  }
632  } else {
633  $this->error = $this->line->error;
634  $this->errors = $this->line->errors;
635  $this->db->rollback();
636  return -2;
637  }
638  } else {
639  $this->error = 'BadStatusOfObjectToAddLine';
640  return -5;
641  }
642  }
643 
644 
671  public function updateline($rowid, $pu, $qty, $remise_percent, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 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, $array_options = 0, $ref_supplier = '', $fk_unit = '', $pu_ht_devise = 0)
672  {
673  global $conf, $user, $langs, $mysoc;
674 
675  dol_syslog(get_class($this)."::updateLine $rowid, $pu, $qty, $remise_percent, $txtva, $desc, $price_base_type, $info_bits");
676  include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
677 
678  // Clean parameters
679  $remise_percent = price2num($remise_percent);
680  $qty = price2num($qty);
681  $pu = price2num($pu);
682  if (!preg_match('/\((.*)\)/', $txtva)) {
683  $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
684  }
685  $txlocaltax1 = price2num($txlocaltax1);
686  $txlocaltax2 = price2num($txlocaltax2);
687  $pa_ht = price2num($pa_ht);
688  if (empty($qty) && empty($special_code)) $special_code = 3; // Set option tag
689  if (!empty($qty) && $special_code == 3) $special_code = 0; // Remove option tag
690 
691  if ($this->statut == 0)
692  {
693  $this->db->begin();
694 
695  // Calcul du total TTC et de la TVA pour la ligne a partir de
696  // qty, pu, remise_percent et txtva
697  // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
698  // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
699 
700  $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $mysoc, $this->thirdparty);
701 
702  // Clean vat code
703  $reg = array();
704  $vat_src_code = '';
705  if (preg_match('/\((.*)\)/', $txtva, $reg))
706  {
707  $vat_src_code = $reg[1];
708  $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
709  }
710 
711  if (!empty($conf->multicurrency->enabled) && $pu_ht_devise > 0) {
712  $pu = 0;
713  }
714 
715  $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
716  $total_ht = $tabprice[0];
717  $total_tva = $tabprice[1];
718  $total_ttc = $tabprice[2];
719  $total_localtax1 = $tabprice[9];
720  $total_localtax2 = $tabprice[10];
721 
722  // MultiCurrency
723  $multicurrency_total_ht = $tabprice[16];
724  $multicurrency_total_tva = $tabprice[17];
725  $multicurrency_total_ttc = $tabprice[18];
726  $pu_ht_devise = $tabprice[19];
727 
728  //Fetch current line from the database and then clone the object and set it in $oldline property
729  $line = new SupplierProposalLine($this->db);
730  $line->fetch($rowid);
731  $line->fetch_optionals();
732 
733  // Stock previous line records
734  $staticline = clone $line;
735 
736  $line->oldline = $staticline;
737  $this->line = $line;
738  $this->line->context = $this->context;
739 
740  // Reorder if fk_parent_line change
741  if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line)
742  {
743  $rangmax = $this->line_max($fk_parent_line);
744  $this->line->rang = $rangmax + 1;
745  }
746 
747  $this->line->id = $rowid;
748  $this->line->label = $label;
749  $this->line->desc = $desc;
750  $this->line->qty = $qty;
751  $this->line->product_type = $type;
752 
753  $this->line->vat_src_code = $vat_src_code;
754  $this->line->tva_tx = $txtva;
755  $this->line->localtax1_tx = $txlocaltax1;
756  $this->line->localtax2_tx = $txlocaltax2;
757  $this->line->localtax1_type = empty($localtaxes_type[0]) ? '' : $localtaxes_type[0];
758  $this->line->localtax2_type = empty($localtaxes_type[2]) ? '' : $localtaxes_type[2];
759  $this->line->remise_percent = $remise_percent;
760  $this->line->subprice = $pu;
761  $this->line->info_bits = $info_bits;
762  $this->line->total_ht = $total_ht;
763  $this->line->total_tva = $total_tva;
764  $this->line->total_localtax1 = $total_localtax1;
765  $this->line->total_localtax2 = $total_localtax2;
766  $this->line->total_ttc = $total_ttc;
767  $this->line->special_code = $special_code;
768  $this->line->fk_parent_line = $fk_parent_line;
769  $this->line->skip_update_total = $skip_update_total;
770  $this->line->ref_fourn = $ref_supplier;
771  $this->line->fk_unit = $fk_unit;
772 
773  // infos marge
774  if (!empty($fk_product) && empty($fk_fournprice) && empty($pa_ht)) {
775  // by external module, take lowest buying price
776  include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
777  $productFournisseur = new ProductFournisseur($this->db);
778  $productFournisseur->find_min_price_product_fournisseur($fk_product);
779  $this->line->fk_fournprice = $productFournisseur->product_fourn_price_id;
780  } else {
781  $this->line->fk_fournprice = $fk_fournprice;
782  }
783  $this->line->pa_ht = $pa_ht;
784 
785  if (is_array($array_options) && count($array_options) > 0) {
786  // We replace values in this->line->array_options only for entries defined into $array_options
787  foreach ($array_options as $key => $value) {
788  $this->line->array_options[$key] = $array_options[$key];
789  }
790  }
791 
792  // Multicurrency
793  $this->line->multicurrency_subprice = $pu_ht_devise;
794  $this->line->multicurrency_total_ht = $multicurrency_total_ht;
795  $this->line->multicurrency_total_tva = $multicurrency_total_tva;
796  $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
797 
798  $result = $this->line->update();
799  if ($result > 0)
800  {
801  // Reorder if child line
802  if (!empty($fk_parent_line)) $this->line_order(true, 'DESC');
803 
804  $this->update_price(1);
805 
806  $this->fk_supplier_proposal = $this->id;
807 
808  $this->db->commit();
809  return $result;
810  } else {
811  $this->error = $this->db->error();
812  $this->db->rollback();
813  return -1;
814  }
815  } else {
816  dol_syslog(get_class($this)."::updateline Erreur -2 SupplierProposal en mode incompatible pour cette action");
817  return -2;
818  }
819  }
820 
821 
828  public function deleteline($lineid)
829  {
830  if ($this->statut == 0)
831  {
832  $line = new SupplierProposalLine($this->db);
833 
834  // For triggers
835  $line->fetch($lineid);
836 
837  if ($line->delete() > 0)
838  {
839  $this->update_price(1);
840 
841  return 1;
842  } else {
843  return -1;
844  }
845  } else {
846  return -2;
847  }
848  }
849 
850 
859  public function create($user, $notrigger = 0)
860  {
861  global $langs, $conf, $mysoc, $hookmanager;
862  $error = 0;
863 
864  $now = dol_now();
865 
866  dol_syslog(get_class($this)."::create");
867 
868  // Check parameters
869  $result = $this->fetch_thirdparty();
870  if ($result < 0)
871  {
872  $this->error = "Failed to fetch company";
873  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
874  return -3;
875  }
876  if (!empty($this->ref)) // We check that ref is not already used
877  {
878  $result = self::isExistingObject($this->element, 0, $this->ref); // Check ref is not yet used
879  if ($result > 0)
880  {
881  $this->error = 'ErrorRefAlreadyExists';
882  dol_syslog(get_class($this)."::create ".$this->error, LOG_WARNING);
883  $this->db->rollback();
884  return -1;
885  }
886  }
887 
888  // Set tmp vars
889  $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
890 
891  // Multicurrency
892  if (!empty($this->multicurrency_code)) list($this->fk_multicurrency, $this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code, $now);
893  if (empty($this->fk_multicurrency))
894  {
895  $this->multicurrency_code = $conf->currency;
896  $this->fk_multicurrency = 0;
897  $this->multicurrency_tx = 1;
898  }
899 
900  $this->db->begin();
901 
902  // Insert into database
903  $sql = "INSERT INTO ".MAIN_DB_PREFIX."supplier_proposal (";
904  $sql .= "fk_soc";
905  $sql .= ", price";
906  $sql .= ", remise";
907  $sql .= ", remise_percent";
908  $sql .= ", remise_absolue";
909  $sql .= ", tva";
910  $sql .= ", total";
911  $sql .= ", datec";
912  $sql .= ", ref";
913  $sql .= ", fk_user_author";
914  $sql .= ", note_private";
915  $sql .= ", note_public";
916  $sql .= ", model_pdf";
917  $sql .= ", fk_cond_reglement";
918  $sql .= ", fk_mode_reglement";
919  $sql .= ", fk_account";
920  $sql .= ", date_livraison";
921  $sql .= ", fk_shipping_method";
922  $sql .= ", fk_projet";
923  $sql .= ", entity";
924  $sql .= ", fk_multicurrency";
925  $sql .= ", multicurrency_code";
926  $sql .= ", multicurrency_tx";
927  $sql .= ") ";
928  $sql .= " VALUES (";
929  $sql .= $this->socid;
930  $sql .= ", 0";
931  $sql .= ", ".$this->remise;
932  $sql .= ", ".($this->remise_percent ? $this->db->escape($this->remise_percent) : 'null');
933  $sql .= ", ".($this->remise_absolue ? $this->db->escape($this->remise_absolue) : 'null');
934  $sql .= ", 0";
935  $sql .= ", 0";
936  $sql .= ", '".$this->db->idate($now)."'";
937  $sql .= ", '(PROV)'";
938  $sql .= ", ".($user->id > 0 ? ((int) $user->id) : "null");
939  $sql .= ", '".$this->db->escape($this->note_private)."'";
940  $sql .= ", '".$this->db->escape($this->note_public)."'";
941  $sql .= ", '".$this->db->escape($this->model_pdf)."'";
942  $sql .= ", ".($this->cond_reglement_id > 0 ? $this->cond_reglement_id : 'NULL');
943  $sql .= ", ".($this->mode_reglement_id > 0 ? $this->mode_reglement_id : 'NULL');
944  $sql .= ", ".($this->fk_account > 0 ? $this->fk_account : 'NULL');
945  $sql .= ", ".($delivery_date ? "'".$this->db->idate($delivery_date)."'" : "null");
946  $sql .= ", ".($this->shipping_method_id > 0 ? $this->shipping_method_id : 'NULL');
947  $sql .= ", ".($this->fk_project ? $this->fk_project : "null");
948  $sql .= ", ".$conf->entity;
949  $sql .= ", ".(int) $this->fk_multicurrency;
950  $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
951  $sql .= ", ".(double) $this->multicurrency_tx;
952  $sql .= ")";
953 
954  dol_syslog(get_class($this)."::create", LOG_DEBUG);
955  $resql = $this->db->query($sql);
956  if ($resql)
957  {
958  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."supplier_proposal");
959 
960  if ($this->id)
961  {
962  $this->ref = '(PROV'.$this->id.')';
963  $sql = 'UPDATE '.MAIN_DB_PREFIX."supplier_proposal SET ref='".$this->db->escape($this->ref)."' WHERE rowid=".$this->id;
964 
965  dol_syslog(get_class($this)."::create", LOG_DEBUG);
966  $resql = $this->db->query($sql);
967  if (!$resql) $error++;
968 
969  if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) // To use new linkedObjectsIds instead of old linked_objects
970  {
971  $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
972  }
973 
974  // Add object linked
975  if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects))
976  {
977  foreach ($this->linked_objects as $origin => $tmp_origin_id)
978  {
979  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, ...))
980  {
981  foreach ($tmp_origin_id as $origin_id)
982  {
983  $ret = $this->add_object_linked($origin, $origin_id);
984  if (!$ret)
985  {
986  dol_print_error($this->db);
987  $error++;
988  }
989  }
990  }
991  }
992  }
993 
994  /*
995  * Insertion du detail des produits dans la base
996  */
997  if (!$error)
998  {
999  $fk_parent_line = 0;
1000  $num = count($this->lines);
1001 
1002  for ($i = 0; $i < $num; $i++)
1003  {
1004  // Reset fk_parent_line for no child products and special product
1005  if (($this->lines[$i]->product_type != 9 && empty($this->lines[$i]->fk_parent_line)) || $this->lines[$i]->product_type == 9) {
1006  $fk_parent_line = 0;
1007  }
1008 
1009  $result = $this->addline(
1010  $this->lines[$i]->desc,
1011  $this->lines[$i]->subprice,
1012  $this->lines[$i]->qty,
1013  $this->lines[$i]->tva_tx,
1014  $this->lines[$i]->localtax1_tx,
1015  $this->lines[$i]->localtax2_tx,
1016  $this->lines[$i]->fk_product,
1017  $this->lines[$i]->remise_percent,
1018  'HT',
1019  0,
1020  0,
1021  $this->lines[$i]->product_type,
1022  $this->lines[$i]->rang,
1023  $this->lines[$i]->special_code,
1024  $fk_parent_line,
1025  $this->lines[$i]->fk_fournprice,
1026  $this->lines[$i]->pa_ht,
1027  empty($this->lines[$i]->label) ? '' : $this->lines[$i]->label, // deprecated
1028  $this->lines[$i]->array_options,
1029  $this->lines[$i]->ref_fourn,
1030  $this->lines[$i]->fk_unit,
1031  'supplier_proposal',
1032  $this->lines[$i]->rowid
1033  );
1034 
1035  if ($result < 0)
1036  {
1037  $error++;
1038  $this->error = $this->db->error;
1039  dol_print_error($this->db);
1040  break;
1041  }
1042  // Defined the new fk_parent_line
1043  if ($result > 0 && $this->lines[$i]->product_type == 9) {
1044  $fk_parent_line = $result;
1045  }
1046  }
1047  }
1048 
1049  if (!$error)
1050  {
1051  // Mise a jour infos denormalisees
1052  $resql = $this->update_price(1);
1053  if ($resql)
1054  {
1055  $action = 'update';
1056 
1057  // Actions on extra fields
1058  if (!$error)
1059  {
1060  $result = $this->insertExtraFields();
1061  if ($result < 0)
1062  {
1063  $error++;
1064  }
1065  }
1066 
1067  if (!$error && !$notrigger)
1068  {
1069  // Call trigger
1070  $result = $this->call_trigger('PROPOSAL_SUPPLIER_CREATE', $user);
1071  if ($result < 0) { $error++; }
1072  // End call triggers
1073  }
1074  } else {
1075  $this->error = $this->db->lasterror();
1076  $error++;
1077  }
1078  }
1079  } else {
1080  $this->error = $this->db->lasterror();
1081  $error++;
1082  }
1083 
1084  if (!$error)
1085  {
1086  $this->db->commit();
1087  dol_syslog(get_class($this)."::create done id=".$this->id);
1088  return $this->id;
1089  } else {
1090  $this->db->rollback();
1091  return -2;
1092  }
1093  } else {
1094  $this->error = $this->db->lasterror();
1095  $this->db->rollback();
1096  return -1;
1097  }
1098  }
1099 
1107  public function createFromClone(User $user, $fromid = 0)
1108  {
1109  global $conf, $hookmanager;
1110 
1111  $error = 0;
1112  $now = dol_now();
1113 
1114  $this->db->begin();
1115 
1116  // get extrafields so they will be clone
1117  foreach ($this->lines as $line)
1118  $line->fetch_optionals();
1119 
1120  // Load source object
1121  $objFrom = clone $this;
1122 
1123  $objsoc = new Societe($this->db);
1124 
1125  // Change socid if needed
1126  if (!empty($fromid) && $fromid != $this->socid)
1127  {
1128  if ($objsoc->fetch($fromid) > 0)
1129  {
1130  $this->socid = $objsoc->id;
1131  $this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1132  $this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1133  $this->fk_project = '';
1134  }
1135 
1136  // TODO Change product price if multi-prices
1137  } else {
1138  $objsoc->fetch($this->socid);
1139  }
1140 
1141  $this->id = 0;
1142  $this->statut = 0;
1143 
1144  if (empty($conf->global->SUPPLIER_PROPOSAL_ADDON) || !is_readable(DOL_DOCUMENT_ROOT."/core/modules/supplier_proposal/".$conf->global->SUPPLIER_PROPOSAL_ADDON.".php"))
1145  {
1146  $this->error = 'ErrorSetupNotComplete';
1147  return -1;
1148  }
1149 
1150  // Clear fields
1151  $this->user_author = $user->id;
1152  $this->user_valid = '';
1153  $this->date = $now;
1154 
1155  // Set ref
1156  require_once DOL_DOCUMENT_ROOT."/core/modules/supplier_proposal/".$conf->global->SUPPLIER_PROPOSAL_ADDON.'.php';
1157  $obj = $conf->global->SUPPLIER_PROPOSAL_ADDON;
1158  $modSupplierProposal = new $obj;
1159  $this->ref = $modSupplierProposal->getNextValue($objsoc, $this);
1160 
1161  // Create clone
1162  $this->context['createfromclone'] = 'createfromclone';
1163  $result = $this->create($user);
1164  if ($result < 0) $error++;
1165 
1166  if (!$error)
1167  {
1168  // Hook of thirdparty module
1169  if (is_object($hookmanager))
1170  {
1171  $parameters = array('objFrom'=>$objFrom);
1172  $action = '';
1173  $reshook = $hookmanager->executeHooks('createFrom', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1174  if ($reshook < 0) $error++;
1175  }
1176  }
1177 
1178  unset($this->context['createfromclone']);
1179 
1180  // End
1181  if (!$error)
1182  {
1183  $this->db->commit();
1184  return $this->id;
1185  } else {
1186  $this->db->rollback();
1187  return -1;
1188  }
1189  }
1190 
1198  public function fetch($rowid, $ref = '')
1199  {
1200  global $conf;
1201 
1202  $sql = "SELECT p.rowid, p.entity, p.ref, p.remise, p.remise_percent, p.remise_absolue, p.fk_soc";
1203  $sql .= ", p.total, p.tva, p.localtax1, p.localtax2, p.total_ht";
1204  $sql .= ", p.datec";
1205  $sql .= ", p.date_valid as datev";
1206  $sql .= ", p.date_livraison as delivery_date";
1207  $sql .= ", p.model_pdf, p.extraparams";
1208  $sql .= ", p.note_private, p.note_public";
1209  $sql .= ", p.fk_projet as fk_project, p.fk_statut";
1210  $sql .= ", p.fk_user_author, p.fk_user_valid, p.fk_user_cloture";
1211  $sql .= ", p.fk_cond_reglement";
1212  $sql .= ", p.fk_mode_reglement";
1213  $sql .= ', p.fk_account';
1214  $sql .= ", p.fk_shipping_method";
1215  $sql .= ", p.fk_multicurrency, p.multicurrency_code, p.multicurrency_tx, p.multicurrency_total_ht, p.multicurrency_total_tva, p.multicurrency_total_ttc";
1216  $sql .= ", c.label as statut_label";
1217  $sql .= ", cr.code as cond_reglement_code, cr.libelle as cond_reglement, cr.libelle_facture as cond_reglement_libelle_doc";
1218  $sql .= ", cp.code as mode_reglement_code, cp.libelle as mode_reglement";
1219  $sql .= " FROM ".MAIN_DB_PREFIX."c_propalst as c, ".MAIN_DB_PREFIX."supplier_proposal as p";
1220  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as cp ON p.fk_mode_reglement = cp.id';
1221  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_payment_term as cr ON p.fk_cond_reglement = cr.rowid';
1222  $sql .= " WHERE p.fk_statut = c.id";
1223  $sql .= " AND p.entity IN (".getEntity('supplier_proposal').")";
1224  if ($ref) $sql .= " AND p.ref='".$ref."'";
1225  else $sql .= " AND p.rowid=".$rowid;
1226 
1227  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1228  $resql = $this->db->query($sql);
1229  if ($resql)
1230  {
1231  if ($this->db->num_rows($resql))
1232  {
1233  $obj = $this->db->fetch_object($resql);
1234 
1235  $this->id = $obj->rowid;
1236  $this->entity = $obj->entity;
1237 
1238  $this->ref = $obj->ref;
1239  $this->remise = $obj->remise;
1240  $this->remise_percent = $obj->remise_percent;
1241  $this->remise_absolue = $obj->remise_absolue;
1242  $this->total = $obj->total; // TODO deprecated
1243  $this->total_ht = $obj->total_ht;
1244  $this->total_tva = $obj->tva;
1245  $this->total_localtax1 = $obj->localtax1;
1246  $this->total_localtax2 = $obj->localtax2;
1247  $this->total_ttc = $obj->total;
1248  $this->socid = $obj->fk_soc;
1249  $this->fk_project = $obj->fk_project;
1250  $this->model_pdf = $obj->model_pdf;
1251  $this->modelpdf = $obj->model_pdf; // deprecated
1252  $this->note = $obj->note_private; // TODO deprecated
1253  $this->note_private = $obj->note_private;
1254  $this->note_public = $obj->note_public;
1255  $this->statut = (int) $obj->fk_statut;
1256  $this->statut_libelle = $obj->statut_label;
1257  $this->datec = $this->db->jdate($obj->datec); // TODO deprecated
1258  $this->datev = $this->db->jdate($obj->datev); // TODO deprecated
1259  $this->date_creation = $this->db->jdate($obj->datec); //Creation date
1260  $this->date_validation = $this->db->jdate($obj->datev); //Validation date
1261  $this->date_livraison = $this->db->jdate($obj->delivery_date); // deprecated
1262  $this->delivery_date = $this->db->jdate($obj->delivery_date);
1263  $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method : null;
1264 
1265  $this->mode_reglement_id = $obj->fk_mode_reglement;
1266  $this->mode_reglement_code = $obj->mode_reglement_code;
1267  $this->mode_reglement = $obj->mode_reglement;
1268  $this->fk_account = ($obj->fk_account > 0) ? $obj->fk_account : null;
1269  $this->cond_reglement_id = $obj->fk_cond_reglement;
1270  $this->cond_reglement_code = $obj->cond_reglement_code;
1271  $this->cond_reglement = $obj->cond_reglement;
1272  $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
1273 
1274  $this->extraparams = (array) json_decode($obj->extraparams, true);
1275 
1276  $this->user_author_id = $obj->fk_user_author;
1277  $this->user_valid_id = $obj->fk_user_valid;
1278  $this->user_close_id = $obj->fk_user_cloture;
1279 
1280  // Multicurrency
1281  $this->fk_multicurrency = $obj->fk_multicurrency;
1282  $this->multicurrency_code = $obj->multicurrency_code;
1283  $this->multicurrency_tx = $obj->multicurrency_tx;
1284  $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
1285  $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
1286  $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
1287 
1288  if ($obj->fk_statut == 0)
1289  {
1290  $this->brouillon = 1;
1291  }
1292 
1293  // Retrieve all extrafield
1294  // fetch optionals attributes and labels
1295  $this->fetch_optionals();
1296 
1297  $this->db->free($resql);
1298 
1299  $this->lines = array();
1300 
1301  // Lines of supplier proposals
1302  $sql = "SELECT d.rowid, d.fk_supplier_proposal, d.fk_parent_line, d.label as custom_label, d.description, d.price, d.tva_tx, d.localtax1_tx, d.localtax2_tx, d.qty, d.fk_remise_except, d.remise_percent, d.subprice, d.fk_product,";
1303  $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,";
1304  $sql .= ' p.ref as product_ref, p.description as product_desc, p.fk_product_type, p.label as product_label,';
1305  $sql .= ' d.ref_fourn as ref_produit_fourn,';
1306  $sql .= ' d.fk_multicurrency, d.multicurrency_code, d.multicurrency_subprice, d.multicurrency_total_ht, d.multicurrency_total_tva, d.multicurrency_total_ttc, d.fk_unit';
1307  $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposaldet as d";
1308  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON d.fk_product = p.rowid";
1309  $sql .= " WHERE d.fk_supplier_proposal = ".$this->id;
1310  $sql .= " ORDER by d.rang";
1311 
1312  $result = $this->db->query($sql);
1313  if ($result)
1314  {
1315  $num = $this->db->num_rows($result);
1316  $i = 0;
1317 
1318  while ($i < $num)
1319  {
1320  $objp = $this->db->fetch_object($result);
1321 
1322  $line = new SupplierProposalLine($this->db);
1323 
1324  $line->rowid = $objp->rowid; // deprecated
1325  $line->id = $objp->rowid;
1326  $line->fk_supplier_proposal = $objp->fk_supplier_proposal;
1327  $line->fk_parent_line = $objp->fk_parent_line;
1328  $line->product_type = $objp->product_type;
1329  $line->label = $objp->custom_label;
1330  $line->desc = $objp->description; // Description ligne
1331  $line->qty = $objp->qty;
1332  $line->tva_tx = $objp->tva_tx;
1333  $line->localtax1_tx = $objp->localtax1_tx;
1334  $line->localtax2_tx = $objp->localtax2_tx;
1335  $line->subprice = $objp->subprice;
1336  $line->fk_remise_except = $objp->fk_remise_except;
1337  $line->remise_percent = $objp->remise_percent;
1338 
1339  $line->info_bits = $objp->info_bits;
1340  $line->total_ht = $objp->total_ht;
1341  $line->total_tva = $objp->total_tva;
1342  $line->total_localtax1 = $objp->total_localtax1;
1343  $line->total_localtax2 = $objp->total_localtax2;
1344  $line->total_ttc = $objp->total_ttc;
1345  $line->fk_fournprice = $objp->fk_fournprice;
1346  $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
1347  $line->pa_ht = $marginInfos[0];
1348  $line->marge_tx = $marginInfos[1];
1349  $line->marque_tx = $marginInfos[2];
1350  $line->special_code = $objp->special_code;
1351  $line->rang = $objp->rang;
1352 
1353  $line->fk_product = $objp->fk_product;
1354 
1355  $line->ref = $objp->product_ref; // deprecated
1356  $line->product_ref = $objp->product_ref;
1357  $line->libelle = $objp->product_label; // deprecated
1358  $line->product_label = $objp->product_label;
1359  $line->product_desc = $objp->product_desc; // Description produit
1360  $line->fk_product_type = $objp->fk_product_type;
1361 
1362  $line->ref_fourn = $objp->ref_produit_fourn;
1363 
1364  // Multicurrency
1365  $line->fk_multicurrency = $objp->fk_multicurrency;
1366  $line->multicurrency_code = $objp->multicurrency_code;
1367  $line->multicurrency_subprice = $objp->multicurrency_subprice;
1368  $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
1369  $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
1370  $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
1371  $line->fk_unit = $objp->fk_unit;
1372 
1373  $this->lines[$i] = $line;
1374 
1375  $i++;
1376  }
1377  $this->db->free($result);
1378  } else {
1379  $this->error = $this->db->error();
1380  return -1;
1381  }
1382 
1383  // Retrieve all extrafield
1384  // fetch optionals attributes and labels
1385  $this->fetch_optionals();
1386 
1387  return 1;
1388  }
1389 
1390  $this->error = "Record Not Found";
1391  return 0;
1392  } else {
1393  $this->error = $this->db->error();
1394  return -1;
1395  }
1396  }
1397 
1405  public function valid($user, $notrigger = 0)
1406  {
1407  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1408 
1409  global $conf, $langs;
1410 
1411  $error = 0;
1412  $now = dol_now();
1413 
1414  if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->supplier_proposal->creer))
1415  || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->supplier_proposal->validate_advance)))
1416  {
1417  $this->db->begin();
1418 
1419  // Numbering module definition
1420  $soc = new Societe($this->db);
1421  $soc->fetch($this->socid);
1422 
1423  // Define new ref
1424  if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) // empty should not happened, but when it occurs, the test save life
1425  {
1426  $num = $this->getNextNumRef($soc);
1427  } else {
1428  $num = $this->ref;
1429  }
1430  $this->newref = dol_sanitizeFileName($num);
1431 
1432  $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal";
1433  $sql .= " SET ref = '".$this->db->escape($num)."',";
1434  $sql .= " fk_statut = 1, date_valid='".$this->db->idate($now)."', fk_user_valid=".$user->id;
1435  $sql .= " WHERE rowid = ".$this->id." AND fk_statut = 0";
1436 
1437  dol_syslog(get_class($this)."::valid", LOG_DEBUG);
1438  $resql = $this->db->query($sql);
1439  if (!$resql)
1440  {
1441  dol_print_error($this->db);
1442  $error++;
1443  }
1444 
1445  // Trigger calls
1446  if (!$error && !$notrigger)
1447  {
1448  // Call trigger
1449  $result = $this->call_trigger('PROPOSAL_SUPPLIER_VALIDATE', $user);
1450  if ($result < 0) { $error++; }
1451  // End call triggers
1452  }
1453 
1454  if (!$error)
1455  {
1456  $this->oldref = $this->ref;
1457 
1458  // Rename directory if dir was a temporary ref
1459  if (preg_match('/^[\(]?PROV/i', $this->ref))
1460  {
1461  // Now we rename also files into index
1462  $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'supplier_proposal/".$this->db->escape($this->newref)."'";
1463  $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'supplier_proposal/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
1464  $resql = $this->db->query($sql);
1465  if (!$resql) { $error++; $this->error = $this->db->lasterror(); }
1466 
1467  // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
1468  $oldref = dol_sanitizeFileName($this->ref);
1469  $newref = dol_sanitizeFileName($num);
1470  $dirsource = $conf->supplier_proposal->dir_output.'/'.$oldref;
1471  $dirdest = $conf->supplier_proposal->dir_output.'/'.$newref;
1472  if (!$error && file_exists($dirsource))
1473  {
1474  dol_syslog(get_class($this)."::valid rename dir ".$dirsource." into ".$dirdest);
1475  if (@rename($dirsource, $dirdest))
1476  {
1477  dol_syslog("Rename ok");
1478  // Rename docs starting with $oldref with $newref
1479  $listoffiles = dol_dir_list($conf->supplier_proposal->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
1480  foreach ($listoffiles as $fileentry)
1481  {
1482  $dirsource = $fileentry['name'];
1483  $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
1484  $dirsource = $fileentry['path'].'/'.$dirsource;
1485  $dirdest = $fileentry['path'].'/'.$dirdest;
1486  @rename($dirsource, $dirdest);
1487  }
1488  }
1489  }
1490  }
1491 
1492  $this->ref = $num;
1493  $this->brouillon = 0;
1494  $this->statut = 1;
1495  $this->user_valid_id = $user->id;
1496  $this->datev = $now;
1497 
1498  $this->db->commit();
1499  return 1;
1500  } else {
1501  $this->db->rollback();
1502  return -1;
1503  }
1504  } else {
1505  dol_syslog("You don't have permission to validate supplier proposal", LOG_WARNING);
1506  return -2;
1507  }
1508  }
1509 
1510  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1519  public function set_date_livraison($user, $delivery_date)
1520  {
1521  // phpcs:enable
1522  return $this->setDeliveryDate($user, $delivery_date);
1523  }
1524 
1532  public function setDeliveryDate($user, $delivery_date)
1533  {
1534  if (!empty($user->rights->supplier_proposal->creer))
1535  {
1536  $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal ";
1537  $sql .= " SET date_livraison = ".($delivery_date != '' ? "'".$this->db->idate($delivery_date)."'" : 'null');
1538  $sql .= " WHERE rowid = ".$this->id;
1539 
1540  if ($this->db->query($sql))
1541  {
1542  $this->date_livraison = $delivery_date;
1543  $this->delivery_date = $delivery_date;
1544  return 1;
1545  } else {
1546  $this->error = $this->db->error();
1547  dol_syslog(get_class($this)."::setDeliveryDate Erreur SQL");
1548  return -1;
1549  }
1550  }
1551  }
1552 
1553  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1561  public function set_remise_percent($user, $remise)
1562  {
1563  // phpcs:enable
1564  $remise = trim($remise) ?trim($remise) : 0;
1565 
1566  if (!empty($user->rights->supplier_proposal->creer))
1567  {
1568  $remise = price2num($remise);
1569 
1570  $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal SET remise_percent = ".$remise;
1571  $sql .= " WHERE rowid = ".$this->id." AND fk_statut = 0";
1572 
1573  if ($this->db->query($sql))
1574  {
1575  $this->remise_percent = $remise;
1576  $this->update_price(1);
1577  return 1;
1578  } else {
1579  $this->error = $this->db->error();
1580  return -1;
1581  }
1582  }
1583  }
1584 
1585 
1586  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1594  public function set_remise_absolue($user, $remise)
1595  {
1596  // phpcs:enable
1597  $remise = trim($remise) ?trim($remise) : 0;
1598 
1599  if (!empty($user->rights->supplier_proposal->creer))
1600  {
1601  $remise = price2num($remise);
1602 
1603  $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal ";
1604  $sql .= " SET remise_absolue = ".$remise;
1605  $sql .= " WHERE rowid = ".$this->id." AND fk_statut = 0";
1606 
1607  if ($this->db->query($sql))
1608  {
1609  $this->remise_absolue = $remise;
1610  $this->update_price(1);
1611  return 1;
1612  } else {
1613  $this->error = $this->db->error();
1614  return -1;
1615  }
1616  }
1617  }
1618 
1619 
1620 
1630  public function reopen($user, $statut, $note = '', $notrigger = 0)
1631  {
1632  global $langs, $conf;
1633 
1634  $this->statut = $statut;
1635  $error = 0;
1636 
1637  $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal";
1638  $sql .= " SET fk_statut = ".$this->statut.",";
1639  if (!empty($note)) $sql .= " note_private = '".$this->db->escape($note)."',";
1640  $sql .= " date_cloture=NULL, fk_user_cloture=NULL";
1641  $sql .= " WHERE rowid = ".$this->id;
1642 
1643  $this->db->begin();
1644 
1645  dol_syslog(get_class($this)."::reopen", LOG_DEBUG);
1646  $resql = $this->db->query($sql);
1647  if (!$resql) {
1648  $error++; $this->errors[] = "Error ".$this->db->lasterror();
1649  }
1650  if (!$error)
1651  {
1652  if (!$notrigger)
1653  {
1654  // Call trigger
1655  $result = $this->call_trigger('PROPOSAL_SUPPLIER_REOPEN', $user);
1656  if ($result < 0) { $error++; }
1657  // End call triggers
1658  }
1659  }
1660 
1661  // Commit or rollback
1662  if ($error)
1663  {
1664  if (!empty($this->errors))
1665  {
1666  foreach ($this->errors as $errmsg)
1667  {
1668  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
1669  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1670  }
1671  }
1672  $this->db->rollback();
1673  return -1 * $error;
1674  } else {
1675  $this->db->commit();
1676  return 1;
1677  }
1678  }
1679 
1680 
1689  public function cloture($user, $status, $note)
1690  {
1691  global $langs, $conf;
1692 
1693  $this->statut = $status;
1694  $error = 0;
1695  $now = dol_now();
1696 
1697  $this->db->begin();
1698 
1699  $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal";
1700  $sql .= " SET fk_statut = ".$status.", note_private = '".$this->db->escape($note)."', date_cloture='".$this->db->idate($now)."', fk_user_cloture=".$user->id;
1701  $sql .= " WHERE rowid = ".$this->id;
1702 
1703  $resql = $this->db->query($sql);
1704  if ($resql)
1705  {
1706  $modelpdf = $conf->global->SUPPLIER_PROPOSAL_ADDON_PDF_ODT_CLOSED ? $conf->global->SUPPLIER_PROPOSAL_ADDON_PDF_ODT_CLOSED : (empty($this->modelpdf) ? '' : $this->modelpdf);
1707  $triggerName = 'PROPOSAL_SUPPLIER_CLOSE_REFUSED';
1708 
1709  if ($status == 2)
1710  {
1711  $triggerName = 'PROPOSAL_SUPPLIER_CLOSE_SIGNED';
1712  $modelpdf = $conf->global->SUPPLIER_PROPOSAL_ADDON_PDF_ODT_TOBILL ? $conf->global->SUPPLIER_PROPOSAL_ADDON_PDF_ODT_TOBILL : (empty($this->modelpdf) ? '' : $this->modelpdf);
1713 
1714  if (!empty($conf->global->SUPPLIER_PROPOSAL_UPDATE_PRICE_ON_SUPPlIER_PROPOSAL)) // TODO This option was not tested correctly. Error if product ref does not exists
1715  {
1716  $result = $this->updateOrCreatePriceFournisseur($user);
1717  }
1718  }
1719  if ($status == 4)
1720  {
1721  $triggerName = 'PROPOSAL_SUPPLIER_CLASSIFY_BILLED';
1722  }
1723 
1724  if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
1725  {
1726  // Define output language
1727  $outputlangs = $langs;
1728  if (!empty($conf->global->MAIN_MULTILANGS)) {
1729  $outputlangs = new Translate("", $conf);
1730  $newlang = (GETPOST('lang_id', 'aZ09') ? GETPOST('lang_id', 'aZ09') : $this->thirdparty->default_lang);
1731  $outputlangs->setDefaultLang($newlang);
1732  }
1733  //$ret=$object->fetch($id); // Reload to get new records
1734  $this->generateDocument($modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
1735  }
1736 
1737  // Call trigger
1738  $result = $this->call_trigger($triggerName, $user);
1739  if ($result < 0) { $error++; }
1740  // End call triggers
1741 
1742  if (!$error)
1743  {
1744  $this->db->commit();
1745  return 1;
1746  } else {
1747  $this->db->rollback();
1748  return -1;
1749  }
1750  } else {
1751  $this->error = $this->db->lasterror();
1752  $this->errors[] = $this->db->lasterror();
1753  $this->db->rollback();
1754  return -1;
1755  }
1756  }
1757 
1764  public function updateOrCreatePriceFournisseur($user)
1765  {
1766  global $conf;
1767 
1768  dol_syslog(get_class($this)."::updateOrCreatePriceFournisseur", LOG_DEBUG);
1769  foreach ($this->lines as $product)
1770  {
1771  if ($product->subprice <= 0) continue;
1772  $productsupplier = new ProductFournisseur($this->db);
1773 
1774  $multicurrency_tx = 1;
1775  $fk_multicurrency = 0;
1776 
1777  if (empty($this->thirdparty)) $this->fetch_thirdparty();
1778 
1779  $ref_fourn = $product->ref_fourn;
1780  if (empty($ref_fourn)) $ref_fourn = $product->ref_supplier;
1781  if (!empty($conf->multicurrency->enabled) && !empty($product->multicurrency_code)) list($fk_multicurrency, $multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $product->multicurrency_code);
1782  $productsupplier->id = $product->fk_product;
1783 
1784  $productsupplier->update_buyprice($product->qty, $product->total_ht, $user, 'HT', $this->thirdparty, '', $ref_fourn, $product->tva_tx, 0, 0, 0, $product->info_bits, '', '', array(), '', $product->multicurrency_total_ht, 'HT', $multicurrency_tx, $product->multicurrency_code, '', '', '');
1785  }
1786 
1787  return 1;
1788  }
1789 
1798  public function updatePriceFournisseur($idProductFournPrice, $product, $user)
1799  {
1800  $price = price2num($product->subprice * $product->qty, 'MU');
1801  $unitPrice = price2num($product->subprice, 'MU');
1802 
1803  $sql = 'UPDATE '.MAIN_DB_PREFIX.'product_fournisseur_price SET '.(!empty($product->ref_fourn) ? 'ref_fourn = "'.$product->ref_fourn.'", ' : '').' price ='.$price.', unitprice ='.$unitPrice.' WHERE rowid = '.$idProductFournPrice;
1804 
1805  $resql = $this->db->query($sql);
1806  if (!$resql) {
1807  $this->error = $this->db->error();
1808  $this->db->rollback();
1809  return -1;
1810  }
1811  }
1812 
1820  public function createPriceFournisseur($product, $user)
1821  {
1822  global $conf;
1823 
1824  $price = price2num($product->subprice * $product->qty, 'MU');
1825  $qty = price2num($product->qty);
1826  $unitPrice = price2num($product->subprice, 'MU');
1827 
1828  $now = dol_now();
1829 
1830  $values = array(
1831  "'".$this->db->idate($now)."'",
1832  $product->fk_product,
1833  $this->thirdparty->id,
1834  "'".$product->ref_fourn."'",
1835  $price,
1836  $qty,
1837  $unitPrice,
1838  $product->tva_tx,
1839  $user->id
1840  );
1841  if (!empty($conf->multicurrency->enabled)) {
1842  if (!empty($product->multicurrency_code)) {
1843  include_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
1844  $multicurrency = new MultiCurrency($this->db); //need to fetch because empty fk_multicurrency and rate
1845  $multicurrency->fetch(0, $product->multicurrency_code);
1846  if (!empty($multicurrency->id)) {
1847  $values[] = $multicurrency->id;
1848  $values[] = "'".$product->multicurrency_code."'";
1849  $values[] = $product->multicurrency_subprice;
1850  $values[] = $product->multicurrency_total_ht;
1851  $values[] = $multicurrency->rate->rate;
1852  } else {
1853  for ($i = 0; $i < 5; $i++) $values[] = 'NULL';
1854  }
1855  }
1856  }
1857 
1858  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'product_fournisseur_price ';
1859  $sql .= '(datec, fk_product, fk_soc, ref_fourn, price, quantity, unitprice, tva_tx, fk_user';
1860  if (!empty($conf->multicurrency->enabled) && !empty($product->multicurrency_code)) $sql .= ',fk_multicurrency, multicurrency_code, multicurrency_unitprice, multicurrency_price, multicurrency_tx';
1861  $sql .= ') VALUES ('.implode(',', $values).')';
1862 
1863  $resql = $this->db->query($sql);
1864  if (!$resql) {
1865  $this->error = $this->db->error();
1866  $this->db->rollback();
1867  return -1;
1868  }
1869  }
1870 
1871  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1878  public function setDraft($user)
1879  {
1880  // phpcs:enable
1881  global $conf, $langs;
1882 
1883  $error = 0;
1884 
1885  if ($this->statut == self::STATUS_DRAFT)
1886  {
1887  dol_syslog(get_class($this)."::setDraft already draft status", LOG_WARNING);
1888  return 0;
1889  }
1890 
1891  $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal";
1892  $sql .= " SET fk_statut = ".self::STATUS_DRAFT;
1893  $sql .= " WHERE rowid = ".$this->id;
1894 
1895  if ($this->db->query($sql))
1896  {
1897  if (!$error) {
1898  $this->oldcopy = clone $this;
1899  }
1900 
1901  if (!$error) {
1902  // Call trigger
1903  $result = $this->call_trigger('PROPOSAL_SUPPLIER_UNVALIDATE', $user);
1904  if ($result < 0) $error++;
1905  }
1906 
1907  if (!$error) {
1908  $this->statut = self::STATUS_DRAFT;
1909  $this->brouillon = 1;
1910  $this->db->commit();
1911  return 1;
1912  } else {
1913  $this->db->rollback();
1914  return -1;
1915  }
1916  } else {
1917  return -1;
1918  }
1919  }
1920 
1921 
1922  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1936  public function liste_array($shortlist = 0, $draft = 0, $notcurrentuser = 0, $socid = 0, $limit = 0, $offset = 0, $sortfield = 'p.datec', $sortorder = 'DESC')
1937  {
1938  // phpcs:enable
1939  global $conf, $user;
1940 
1941  $ga = array();
1942 
1943  $sql = "SELECT s.rowid, s.nom as name, s.client,";
1944  $sql .= " p.rowid as supplier_proposalid, p.fk_statut, p.total_ht, p.ref, p.remise, ";
1945  $sql .= " p.datep as dp, p.fin_validite as datelimite";
1946  if (!$user->rights->societe->client->voir && !$socid) $sql .= ", sc.fk_soc, sc.fk_user";
1947  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."supplier_proposal as p, ".MAIN_DB_PREFIX."c_propalst as c";
1948  if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
1949  $sql .= " WHERE p.entity IN (".getEntity('supplier_proposal').")";
1950  $sql .= " AND p.fk_soc = s.rowid";
1951  $sql .= " AND p.fk_statut = c.id";
1952  if (!$user->rights->societe->client->voir && !$socid) //restriction
1953  {
1954  $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id;
1955  }
1956  if ($socid) $sql .= " AND s.rowid = ".$socid;
1957  if ($draft) $sql .= " AND p.fk_statut = 0";
1958  if ($notcurrentuser > 0) $sql .= " AND p.fk_user_author <> ".$user->id;
1959  $sql .= $this->db->order($sortfield, $sortorder);
1960  $sql .= $this->db->plimit($limit, $offset);
1961 
1962  $result = $this->db->query($sql);
1963  if ($result)
1964  {
1965  $num = $this->db->num_rows($result);
1966  if ($num)
1967  {
1968  $i = 0;
1969  while ($i < $num)
1970  {
1971  $obj = $this->db->fetch_object($result);
1972 
1973  if ($shortlist == 1)
1974  {
1975  $ga[$obj->supplier_proposalid] = $obj->ref;
1976  } elseif ($shortlist == 2)
1977  {
1978  $ga[$obj->supplier_proposalid] = $obj->ref.' ('.$obj->name.')';
1979  } else {
1980  $ga[$i]['id'] = $obj->supplier_proposalid;
1981  $ga[$i]['ref'] = $obj->ref;
1982  $ga[$i]['name'] = $obj->name;
1983  }
1984 
1985  $i++;
1986  }
1987  }
1988  return $ga;
1989  } else {
1990  dol_print_error($this->db);
1991  return -1;
1992  }
1993  }
1994 
2002  public function delete($user, $notrigger = 0)
2003  {
2004  global $conf, $langs;
2005  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
2006 
2007  $error = 0;
2008 
2009  $this->db->begin();
2010 
2011  if (!$notrigger)
2012  {
2013  // Call trigger
2014  $result = $this->call_trigger('PROPOSAL_SUPPLIER_DELETE', $user);
2015  if ($result < 0) { $error++; }
2016  // End call triggers
2017  }
2018 
2019  if (!$error)
2020  {
2021  $main = MAIN_DB_PREFIX.'supplier_proposaldet';
2022  $ef = $main."_extrafields";
2023  $sqlef = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_supplier_proposal = ".$this->id.")";
2024  $sql = "DELETE FROM ".MAIN_DB_PREFIX."supplier_proposaldet WHERE fk_supplier_proposal = ".$this->id;
2025  if ($this->db->query($sql))
2026  {
2027  $sql = "DELETE FROM ".MAIN_DB_PREFIX."supplier_proposal WHERE rowid = ".$this->id;
2028  if ($this->db->query($sqlef) && $this->db->query($sql))
2029  {
2030  // Delete linked object
2031  $res = $this->deleteObjectLinked();
2032  if ($res < 0) $error++;
2033 
2034  if (!$error)
2035  {
2036  // Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive
2037  $this->deleteEcmFiles();
2038 
2039  // We remove directory
2040  $ref = dol_sanitizeFileName($this->ref);
2041  if ($conf->supplier_proposal->dir_output && !empty($this->ref))
2042  {
2043  $dir = $conf->supplier_proposal->dir_output."/".$ref;
2044  $file = $dir."/".$ref.".pdf";
2045  if (file_exists($file))
2046  {
2047  dol_delete_preview($this);
2048 
2049  if (!dol_delete_file($file, 0, 0, 0, $this)) // For triggers
2050  {
2051  $this->error = 'ErrorFailToDeleteFile';
2052  $this->errors = array('ErrorFailToDeleteFile');
2053  $this->db->rollback();
2054  return 0;
2055  }
2056  }
2057  if (file_exists($dir))
2058  {
2059  $res = @dol_delete_dir_recursive($dir);
2060  if (!$res)
2061  {
2062  $this->error = 'ErrorFailToDeleteDir';
2063  $this->errors = array('ErrorFailToDeleteDir');
2064  $this->db->rollback();
2065  return 0;
2066  }
2067  }
2068  }
2069  }
2070 
2071  // Removed extrafields
2072  if (!$error)
2073  {
2074  $result = $this->deleteExtraFields();
2075  if ($result < 0)
2076  {
2077  $error++;
2078  $errorflag = -4;
2079  dol_syslog(get_class($this)."::delete erreur ".$errorflag." ".$this->error, LOG_ERR);
2080  }
2081  }
2082 
2083  if (!$error)
2084  {
2085  dol_syslog(get_class($this)."::delete ".$this->id." by ".$user->id, LOG_DEBUG);
2086  $this->db->commit();
2087  return 1;
2088  } else {
2089  $this->error = $this->db->lasterror();
2090  $this->db->rollback();
2091  return 0;
2092  }
2093  } else {
2094  $this->error = $this->db->lasterror();
2095  $this->db->rollback();
2096  return -3;
2097  }
2098  } else {
2099  $this->error = $this->db->lasterror();
2100  $this->db->rollback();
2101  return -2;
2102  }
2103  } else {
2104  $this->db->rollback();
2105  return -1;
2106  }
2107  }
2108 
2115  public function info($id)
2116  {
2117  $sql = "SELECT c.rowid, ";
2118  $sql .= " c.datec, c.date_valid as datev, c.date_cloture as dateo,";
2119  $sql .= " c.fk_user_author, c.fk_user_valid, c.fk_user_cloture";
2120  $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal as c";
2121  $sql .= " WHERE c.rowid = ".((int) $id);
2122 
2123  $result = $this->db->query($sql);
2124 
2125  if ($result)
2126  {
2127  if ($this->db->num_rows($result))
2128  {
2129  $obj = $this->db->fetch_object($result);
2130 
2131  $this->id = $obj->rowid;
2132 
2133  $this->date_creation = $this->db->jdate($obj->datec);
2134  $this->date_validation = $this->db->jdate($obj->datev);
2135  $this->date_cloture = $this->db->jdate($obj->dateo);
2136 
2137  $cuser = new User($this->db);
2138  $cuser->fetch($obj->fk_user_author);
2139  $this->user_creation = $cuser;
2140 
2141  if ($obj->fk_user_valid)
2142  {
2143  $vuser = new User($this->db);
2144  $vuser->fetch($obj->fk_user_valid);
2145  $this->user_validation = $vuser;
2146  }
2147 
2148  if ($obj->fk_user_cloture)
2149  {
2150  $cluser = new User($this->db);
2151  $cluser->fetch($obj->fk_user_cloture);
2152  $this->user_cloture = $cluser;
2153  }
2154  }
2155  $this->db->free($result);
2156  } else {
2157  dol_print_error($this->db);
2158  }
2159  }
2160 
2161 
2168  public function getLibStatut($mode = 0)
2169  {
2170  return $this->LibStatut((isset($this->statut) ? $this->statut : $this->status), $mode);
2171  }
2172 
2173  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2181  public function LibStatut($status, $mode = 1)
2182  {
2183  // phpcs:enable
2184 
2185  // Init/load array of translation of status
2186  if (empty($this->labelStatus) || empty($this->labelStatusShort))
2187  {
2188  global $langs;
2189  $langs->load("supplier_proposal");
2190  $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv("SupplierProposalStatusDraft");
2191  $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv("SupplierProposalStatusValidated");
2192  $this->labelStatus[self::STATUS_SIGNED] = $langs->transnoentitiesnoconv("SupplierProposalStatusSigned");
2193  $this->labelStatus[self::STATUS_NOTSIGNED] = $langs->transnoentitiesnoconv("SupplierProposalStatusNotSigned");
2194  $this->labelStatus[self::STATUS_CLOSE] = $langs->transnoentitiesnoconv("SupplierProposalStatusClosed");
2195  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv("SupplierProposalStatusDraftShort");
2196  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv("Opened");
2197  $this->labelStatusShort[self::STATUS_SIGNED] = $langs->transnoentitiesnoconv("SupplierProposalStatusSignedShort");
2198  $this->labelStatusShort[self::STATUS_NOTSIGNED] = $langs->transnoentitiesnoconv("SupplierProposalStatusNotSignedShort");
2199  $this->labelStatusShort[self::STATUS_CLOSE] = $langs->transnoentitiesnoconv("SupplierProposalStatusClosedShort");
2200  }
2201 
2202  $statusnew = '';
2203  if ($status == self::STATUS_DRAFT) $statusnew = 'status0';
2204  elseif ($status == self::STATUS_VALIDATED) $statusnew = 'status1';
2205  elseif ($status == self::STATUS_SIGNED) $statusnew = 'status4';
2206  elseif ($status == self::STATUS_NOTSIGNED) $statusnew = 'status9';
2207  elseif ($status == self::STATUS_CLOSE) $statusnew = 'status6';
2208 
2209  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusnew, $mode);
2210  }
2211 
2212 
2213  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2221  public function load_board($user, $mode)
2222  {
2223  // phpcs:enable
2224  global $conf, $user, $langs;
2225 
2226  $now = dol_now();
2227 
2228  $this->nbtodo = $this->nbtodolate = 0;
2229  $clause = " WHERE";
2230 
2231  $sql = "SELECT p.rowid, p.ref, p.datec as datec";
2232  $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal as p";
2233  if (!$user->rights->societe->client->voir && !$user->socid)
2234  {
2235  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc";
2236  $sql .= " WHERE sc.fk_user = ".$user->id;
2237  $clause = " AND";
2238  }
2239  $sql .= $clause." p.entity IN (".getEntity('supplier_proposal').")";
2240  if ($mode == 'opened') $sql .= " AND p.fk_statut = 1";
2241  if ($mode == 'signed') $sql .= " AND p.fk_statut = 2";
2242  if ($user->socid) $sql .= " AND p.fk_soc = ".$user->socid;
2243 
2244  $resql = $this->db->query($sql);
2245  if ($resql)
2246  {
2247  $label = $labelShort = '';
2248  $status = '';
2249  if ($mode == 'opened') {
2250  $delay_warning = $conf->supplier_proposal->cloture->warning_delay;
2251  $status = self::STATUS_VALIDATED;
2252  $label = $langs->trans("SupplierProposalsToClose");
2253  $labelShort = $langs->trans("ToAcceptRefuse");
2254  }
2255  if ($mode == 'signed') {
2256  $delay_warning = $conf->supplier_proposal->facturation->warning_delay;
2257  $status = self::STATUS_SIGNED;
2258  $label = $langs->trans("SupplierProposalsToProcess"); // May be billed or ordered
2259  $labelShort = $langs->trans("ToClose");
2260  }
2261 
2262  $response = new WorkboardResponse();
2263  $response->warning_delay = $delay_warning / 60 / 60 / 24;
2264  $response->label = $label;
2265  $response->labelShort = $labelShort;
2266  $response->url = DOL_URL_ROOT.'/supplier_proposal/list.php?search_status='.$status;
2267  $response->img = img_object('', "propal");
2268 
2269  // This assignment in condition is not a bug. It allows walking the results.
2270  while ($obj = $this->db->fetch_object($resql))
2271  {
2272  $response->nbtodo++;
2273  if ($mode == 'opened')
2274  {
2275  $datelimit = $this->db->jdate($obj->datefin);
2276  if ($datelimit < ($now - $delay_warning))
2277  {
2278  $response->nbtodolate++;
2279  }
2280  }
2281  // TODO Definir regle des propales a facturer en retard
2282  // if ($mode == 'signed' && ! count($this->FactureListeArray($obj->rowid))) $this->nbtodolate++;
2283  }
2284  return $response;
2285  } else {
2286  $this->error = $this->db->lasterror();
2287  return -1;
2288  }
2289  }
2290 
2291 
2299  public function initAsSpecimen()
2300  {
2301  global $user, $langs, $conf;
2302 
2303  // Load array of products prodids
2304  $num_prods = 0;
2305  $prodids = array();
2306  $sql = "SELECT rowid";
2307  $sql .= " FROM ".MAIN_DB_PREFIX."product";
2308  $sql .= " WHERE entity IN (".getEntity('product').")";
2309  $sql .= $this->db->plimit(100);
2310 
2311  $resql = $this->db->query($sql);
2312  if ($resql)
2313  {
2314  $num_prods = $this->db->num_rows($resql);
2315  $i = 0;
2316  while ($i < $num_prods)
2317  {
2318  $i++;
2319  $row = $this->db->fetch_row($resql);
2320  $prodids[$i] = $row[0];
2321  }
2322  }
2323 
2324  // Initialise parametres
2325  $this->id = 0;
2326  $this->ref = 'SPECIMEN';
2327  $this->specimen = 1;
2328  $this->socid = 1;
2329  $this->date = time();
2330  $this->cond_reglement_id = 1;
2331  $this->cond_reglement_code = 'RECEP';
2332  $this->mode_reglement_id = 7;
2333  $this->mode_reglement_code = 'CHQ';
2334  $this->note_public = 'This is a comment (public)';
2335  $this->note_private = 'This is a comment (private)';
2336  // Lines
2337  $nbp = 5;
2338  $xnbp = 0;
2339  while ($xnbp < $nbp)
2340  {
2341  $line = new SupplierProposalLine($this->db);
2342  $line->desc = $langs->trans("Description")." ".$xnbp;
2343  $line->qty = 1;
2344  $line->subprice = 100;
2345  $line->tva_tx = 19.6;
2346  $line->localtax1_tx = 0;
2347  $line->localtax2_tx = 0;
2348  if ($xnbp == 2)
2349  {
2350  $line->total_ht = 50;
2351  $line->total_ttc = 59.8;
2352  $line->total_tva = 9.8;
2353  $line->remise_percent = 50;
2354  } else {
2355  $line->total_ht = 100;
2356  $line->total_ttc = 119.6;
2357  $line->total_tva = 19.6;
2358  $line->remise_percent = 00;
2359  }
2360 
2361  if ($num_prods > 0)
2362  {
2363  $prodid = mt_rand(1, $num_prods);
2364  $line->fk_product = $prodids[$prodid];
2365  }
2366 
2367  $this->lines[$xnbp] = $line;
2368 
2369  $this->total_ht += $line->total_ht;
2370  $this->total_tva += $line->total_tva;
2371  $this->total_ttc += $line->total_ttc;
2372 
2373  $xnbp++;
2374  }
2375  }
2376 
2377  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2383  public function load_state_board()
2384  {
2385  // phpcs:enable
2386  global $conf, $user;
2387 
2388  $this->nb = array();
2389  $clause = "WHERE";
2390 
2391  $sql = "SELECT count(p.rowid) as nb";
2392  $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal as p";
2393  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid";
2394  if (!$user->rights->societe->client->voir && !$user->socid)
2395  {
2396  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
2397  $sql .= " WHERE sc.fk_user = ".$user->id;
2398  $clause = "AND";
2399  }
2400  $sql .= " ".$clause." p.entity IN (".getEntity('supplier_proposal').")";
2401 
2402  $resql = $this->db->query($sql);
2403  if ($resql)
2404  {
2405  // This assignment in condition is not a bug. It allows walking the results.
2406  while ($obj = $this->db->fetch_object($resql))
2407  {
2408  $this->nb["supplier_proposals"] = $obj->nb;
2409  }
2410  $this->db->free($resql);
2411  return 1;
2412  } else {
2413  dol_print_error($this->db);
2414  $this->error = $this->db->lasterror();
2415  return -1;
2416  }
2417  }
2418 
2419 
2427  public function getNextNumRef($soc)
2428  {
2429  global $conf, $db, $langs;
2430  $langs->load("supplier_proposal");
2431 
2432  if (!empty($conf->global->SUPPLIER_PROPOSAL_ADDON))
2433  {
2434  $mybool = false;
2435 
2436  $file = $conf->global->SUPPLIER_PROPOSAL_ADDON.".php";
2437  $classname = $conf->global->SUPPLIER_PROPOSAL_ADDON;
2438 
2439  // Include file with class
2440  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
2441  foreach ($dirmodels as $reldir) {
2442  $dir = dol_buildpath($reldir."core/modules/supplier_proposal/");
2443 
2444  // Load file with numbering class (if found)
2445  $mybool |= @include_once $dir.$file;
2446  }
2447 
2448  if (!$mybool)
2449  {
2450  dol_print_error('', "Failed to include file ".$file);
2451  return '';
2452  }
2453 
2454  $obj = new $classname();
2455  $numref = "";
2456  $numref = $obj->getNextValue($soc, $this);
2457 
2458  if ($numref != "")
2459  {
2460  return $numref;
2461  } else {
2462  $this->error = $obj->error;
2463  return "";
2464  }
2465  } else {
2466  $langs->load("errors");
2467  print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv("SupplierProposal"));
2468  return "";
2469  }
2470  }
2471 
2483  public function getNomUrl($withpicto = 0, $option = '', $get_params = '', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0)
2484  {
2485  global $langs, $conf, $user;
2486 
2487  if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
2488 
2489  $url = '';
2490  $result = '';
2491 
2492  $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("SupplierProposal").'</u>';
2493  if (isset($this->status)) {
2494  $label .= ' '.$this->getLibStatut(5);
2495  }
2496  if (!empty($this->ref)) $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
2497  if (!empty($this->ref_fourn)) $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_fourn;
2498  if (!empty($this->total_ht)) $label .= '<br><b>'.$langs->trans('AmountHT').':</b> '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
2499  if (!empty($this->total_tva)) $label .= '<br><b>'.$langs->trans('VAT').':</b> '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
2500  if (!empty($this->total_ttc)) $label .= '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
2501 
2502  if ($option == '') {
2503  $url = DOL_URL_ROOT.'/supplier_proposal/card.php?id='.$this->id.$get_params;
2504  }
2505  if ($option == 'document') {
2506  $url = DOL_URL_ROOT.'/supplier_proposal/document.php?id='.$this->id.$get_params;
2507  }
2508 
2509  if ($option !== 'nolink')
2510  {
2511  // Add param to save lastsearch_values or not
2512  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
2513  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
2514  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
2515  }
2516 
2517  $linkclose = '';
2518  if (empty($notooltip) && $user->rights->propal->lire)
2519  {
2520  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
2521  {
2522  $label = $langs->trans("ShowSupplierProposal");
2523  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
2524  }
2525  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
2526  $linkclose .= ' class="classfortooltip"';
2527  }
2528 
2529  $linkstart = '<a href="'.$url.'"';
2530  $linkstart .= $linkclose.'>';
2531  $linkend = '</a>';
2532 
2533  $result .= $linkstart;
2534  if ($withpicto) $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
2535  if ($withpicto != 2) $result .= $this->ref;
2536  $result .= $linkend;
2537 
2538  if ($addlinktonotes)
2539  {
2540  $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
2541  if ($txttoshow)
2542  {
2543  $notetoshow = $langs->trans("ViewPrivateNote").':<br>'.dol_string_nohtmltag($txttoshow, 1);
2544  $result .= ' <span class="note inline-block">';
2545  $result .= '<a href="'.DOL_URL_ROOT.'/supplier_proposal/note.php?id='.$this->id.'" class="classfortooltip" title="'.dol_escape_htmltag($notetoshow).'">';
2546  $result .= img_picto('', 'note');
2547  $result .= '</a>';
2548  //$result.=img_picto($langs->trans("ViewNote"),'object_generic');
2549  //$result.='</a>';
2550  $result .= '</span>';
2551  }
2552  }
2553 
2554  return $result;
2555  }
2556 
2562  public function getLinesArray()
2563  {
2564  // For other object, here we call fetch_lines. But fetch_lines does not exists on supplier proposal
2565 
2566  $sql = 'SELECT pt.rowid, pt.label as custom_label, pt.description, pt.fk_product, pt.fk_remise_except,';
2567  $sql .= ' pt.qty, pt.tva_tx, pt.remise_percent, pt.subprice, pt.info_bits,';
2568  $sql .= ' pt.total_ht, pt.total_tva, pt.total_ttc, pt.fk_product_fournisseur_price as fk_fournprice, pt.buy_price_ht as pa_ht, pt.special_code, pt.localtax1_tx, pt.localtax2_tx,';
2569  $sql .= ' pt.product_type, pt.rang, pt.fk_parent_line,';
2570  $sql .= ' p.label as product_label, p.ref, p.fk_product_type, p.rowid as prodid,';
2571  $sql .= ' p.description as product_desc, pt.ref_fourn as ref_supplier,';
2572  $sql .= ' pt.fk_multicurrency, pt.multicurrency_code, pt.multicurrency_subprice, pt.multicurrency_total_ht, pt.multicurrency_total_tva, pt.multicurrency_total_ttc, pt.fk_unit';
2573  $sql .= ' FROM '.MAIN_DB_PREFIX.'supplier_proposaldet as pt';
2574  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pt.fk_product=p.rowid';
2575  $sql .= ' WHERE pt.fk_supplier_proposal = '.$this->id;
2576  $sql .= ' ORDER BY pt.rang ASC, pt.rowid';
2577 
2578  dol_syslog(get_class($this).'::getLinesArray', LOG_DEBUG);
2579  $resql = $this->db->query($sql);
2580  if ($resql)
2581  {
2582  $num = $this->db->num_rows($resql);
2583  $i = 0;
2584 
2585  while ($i < $num)
2586  {
2587  $obj = $this->db->fetch_object($resql);
2588 
2589  $this->lines[$i] = new SupplierProposalLine($this->db);
2590  $this->lines[$i]->id = $obj->rowid; // for backward compatibility
2591  $this->lines[$i]->rowid = $obj->rowid;
2592  $this->lines[$i]->label = $obj->custom_label;
2593  $this->lines[$i]->description = $obj->description;
2594  $this->lines[$i]->fk_product = $obj->fk_product;
2595  $this->lines[$i]->ref = $obj->ref;
2596  $this->lines[$i]->product_label = $obj->product_label;
2597  $this->lines[$i]->product_desc = $obj->product_desc;
2598  $this->lines[$i]->fk_product_type = $obj->fk_product_type; // deprecated
2599  $this->lines[$i]->product_type = $obj->product_type;
2600  $this->lines[$i]->qty = $obj->qty;
2601  $this->lines[$i]->subprice = $obj->subprice;
2602  $this->lines[$i]->fk_remise_except = $obj->fk_remise_except;
2603  $this->lines[$i]->remise_percent = $obj->remise_percent;
2604  $this->lines[$i]->tva_tx = $obj->tva_tx;
2605  $this->lines[$i]->info_bits = $obj->info_bits;
2606  $this->lines[$i]->total_ht = $obj->total_ht;
2607  $this->lines[$i]->total_tva = $obj->total_tva;
2608  $this->lines[$i]->total_ttc = $obj->total_ttc;
2609  $this->lines[$i]->fk_fournprice = $obj->fk_fournprice;
2610  $marginInfos = getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->lines[$i]->fk_fournprice, $obj->pa_ht);
2611  $this->lines[$i]->pa_ht = $marginInfos[0];
2612  $this->lines[$i]->marge_tx = $marginInfos[1];
2613  $this->lines[$i]->marque_tx = $marginInfos[2];
2614  $this->lines[$i]->fk_parent_line = $obj->fk_parent_line;
2615  $this->lines[$i]->special_code = $obj->special_code;
2616  $this->lines[$i]->rang = $obj->rang;
2617 
2618  $this->lines[$i]->ref_fourn = $obj->ref_supplier; // deprecated
2619  $this->lines[$i]->ref_supplier = $obj->ref_supplier;
2620 
2621  // Multicurrency
2622  $this->lines[$i]->fk_multicurrency = $obj->fk_multicurrency;
2623  $this->lines[$i]->multicurrency_code = $obj->multicurrency_code;
2624  $this->lines[$i]->multicurrency_subprice = $obj->multicurrency_subprice;
2625  $this->lines[$i]->multicurrency_total_ht = $obj->multicurrency_total_ht;
2626  $this->lines[$i]->multicurrency_total_tva = $obj->multicurrency_total_tva;
2627  $this->lines[$i]->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
2628  $this->lines[$i]->fk_unit = $obj->fk_unit;
2629 
2630  $i++;
2631  }
2632  $this->db->free($resql);
2633 
2634  return 1;
2635  } else {
2636  $this->error = $this->db->error();
2637  return -1;
2638  }
2639  }
2640 
2652  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
2653  {
2654  global $conf, $langs;
2655 
2656  $langs->load("supplier_proposal");
2657  $outputlangs->load("products");
2658 
2659  if (!dol_strlen($modele)) {
2660  $modele = 'aurore';
2661 
2662  if ($this->model_pdf) {
2663  $modele = $this->model_pdf;
2664  } elseif (!empty($conf->global->SUPPLIER_PROPOSAL_ADDON_PDF)) {
2665  $modele = $conf->global->SUPPLIER_PROPOSAL_ADDON_PDF;
2666  }
2667  }
2668 
2669  $modelpath = "core/modules/supplier_proposal/doc/";
2670 
2671  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2672  }
2673 
2674 
2683  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
2684  {
2685  $tables = array(
2686  'supplier_proposal'
2687  );
2688 
2689  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
2690  }
2691 }
2692 
2693 
2698 {
2702  public $db;
2703 
2707  public $error = '';
2708 
2712  public $element = 'supplier_proposaldet';
2713 
2717  public $table_element = 'supplier_proposaldet';
2718 
2719  public $oldline;
2720 
2724  public $id;
2725 
2729  public $fk_supplier_proposal;
2730 
2734  public $fk_parent_line;
2735 
2736  public $desc; // Description ligne
2737 
2741  public $fk_product; // Id produit predefini
2742 
2753  public $product_type = Product::TYPE_PRODUCT;
2754 
2755  public $qty;
2756  public $tva_tx;
2757  public $vat_src_code;
2758 
2759  public $subprice;
2760  public $remise_percent;
2761 
2765  public $fk_remise_except;
2766 
2767  public $rang = 0;
2768 
2772  public $fk_fournprice;
2773 
2774  public $pa_ht;
2775  public $marge_tx;
2776  public $marque_tx;
2777 
2778  public $special_code; // Tag for special lines (exlusive tags)
2779  // 1: frais de port
2780  // 2: ecotaxe
2781  // 3: option line (when qty = 0)
2782 
2783  public $info_bits = 0; // Liste d'options cumulables:
2784  // Bit 0: 0 si TVA normal - 1 si TVA NPR
2785  // Bit 1: 0 ligne normale - 1 si ligne de remise fixe
2786 
2787  public $total_ht; // Total HT de la ligne toute quantite et incluant la remise ligne
2788  public $total_tva; // Total TVA de la ligne toute quantite et incluant la remise ligne
2789  public $total_ttc; // Total TTC de la ligne toute quantite et incluant la remise ligne
2790 
2791  public $date_start;
2792  public $date_end;
2793 
2794  // From llx_product
2799  public $ref;
2800 
2805  public $product_ref;
2806 
2811  public $libelle;
2812 
2817  public $product_label;
2818 
2823  public $product_desc;
2824 
2825  public $localtax1_tx; // Local tax 1
2826  public $localtax2_tx; // Local tax 2
2827  public $localtax1_type; // Local tax 1 type
2828  public $localtax2_type; // Local tax 2 type
2829  public $total_localtax1; // Line total local tax 1
2830  public $total_localtax2; // Line total local tax 2
2831 
2832  public $skip_update_total; // Skip update price total for special lines
2833 
2834  public $ref_fourn;
2835  public $ref_supplier;
2836 
2837  // Multicurrency
2841  public $fk_multicurrency;
2842 
2843  public $multicurrency_code;
2844  public $multicurrency_subprice;
2845  public $multicurrency_total_ht;
2846  public $multicurrency_total_tva;
2847  public $multicurrency_total_ttc;
2848 
2854  public function __construct($db)
2855  {
2856  $this->db = $db;
2857  }
2858 
2865  public function fetch($rowid)
2866  {
2867  $sql = 'SELECT pd.rowid, pd.fk_supplier_proposal, pd.fk_parent_line, pd.fk_product, pd.label as custom_label, pd.description, pd.price, pd.qty, pd.tva_tx,';
2868  $sql .= ' pd.date_start, pd.date_end,';
2869  $sql .= ' pd.remise, pd.remise_percent, pd.fk_remise_except, pd.subprice,';
2870  $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,';
2871  $sql .= ' pd.localtax1_tx, pd.localtax2_tx, pd.total_localtax1, pd.total_localtax2,';
2872  $sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
2873  $sql .= ' pd.product_type, pd.ref_fourn as ref_produit_fourn,';
2874  $sql .= ' pd.fk_multicurrency, pd.multicurrency_code, pd.multicurrency_subprice, pd.multicurrency_total_ht, pd.multicurrency_total_tva, pd.multicurrency_total_ttc, pd.fk_unit';
2875  $sql .= ' FROM '.MAIN_DB_PREFIX.'supplier_proposaldet as pd';
2876  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pd.fk_product = p.rowid';
2877  $sql .= ' WHERE pd.rowid = '.$rowid;
2878 
2879  $result = $this->db->query($sql);
2880  if ($result)
2881  {
2882  $objp = $this->db->fetch_object($result);
2883 
2884  $this->id = $objp->rowid;
2885  $this->fk_supplier_proposal = $objp->fk_supplier_proposal;
2886  $this->fk_parent_line = $objp->fk_parent_line;
2887  $this->label = $objp->custom_label;
2888  $this->desc = $objp->description;
2889  $this->qty = $objp->qty;
2890  $this->subprice = $objp->subprice;
2891  $this->tva_tx = $objp->tva_tx;
2892  $this->remise_percent = $objp->remise_percent;
2893  $this->fk_remise_except = $objp->fk_remise_except;
2894  $this->fk_product = $objp->fk_product;
2895  $this->info_bits = $objp->info_bits;
2896  $this->date_start = $this->db->jdate($objp->date_start);
2897  $this->date_end = $this->db->jdate($objp->date_end);
2898 
2899  $this->total_ht = $objp->total_ht;
2900  $this->total_tva = $objp->total_tva;
2901  $this->total_ttc = $objp->total_ttc;
2902 
2903  $this->fk_fournprice = $objp->fk_fournprice;
2904 
2905  $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
2906  $this->pa_ht = $marginInfos[0];
2907  $this->marge_tx = $marginInfos[1];
2908  $this->marque_tx = $marginInfos[2];
2909 
2910  $this->special_code = $objp->special_code;
2911  $this->product_type = $objp->product_type;
2912  $this->rang = $objp->rang;
2913 
2914  $this->ref = $objp->product_ref; // deprecated
2915  $this->product_ref = $objp->product_ref;
2916  $this->libelle = $objp->product_label; // deprecated
2917  $this->product_label = $objp->product_label;
2918  $this->product_desc = $objp->product_desc;
2919 
2920  $this->ref_fourn = $objp->ref_produit_forun;
2921 
2922  // Multicurrency
2923  $this->fk_multicurrency = $objp->fk_multicurrency;
2924  $this->multicurrency_code = $objp->multicurrency_code;
2925  $this->multicurrency_subprice = $objp->multicurrency_subprice;
2926  $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
2927  $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
2928  $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2929  $this->fk_unit = $objp->fk_unit;
2930 
2931  $this->db->free($result);
2932  } else {
2933  dol_print_error($this->db);
2934  }
2935  }
2936 
2943  public function insert($notrigger = 0)
2944  {
2945  global $conf, $langs, $user;
2946 
2947  $error = 0;
2948 
2949  dol_syslog(get_class($this)."::insert rang=".$this->rang);
2950 
2951  // Clean parameters
2952  if (empty($this->tva_tx)) $this->tva_tx = 0;
2953  if (empty($this->localtax1_tx)) $this->localtax1_tx = 0;
2954  if (empty($this->localtax2_tx)) $this->localtax2_tx = 0;
2955  if (empty($this->localtax1_type)) $this->localtax1_type = 0;
2956  if (empty($this->localtax2_type)) $this->localtax2_type = 0;
2957  if (empty($this->total_localtax1)) $this->total_localtax1 = 0;
2958  if (empty($this->total_localtax2)) $this->total_localtax2 = 0;
2959  if (empty($this->rang)) $this->rang = 0;
2960  if (empty($this->remise)) $this->remise = 0;
2961  if (empty($this->remise_percent)) $this->remise_percent = 0;
2962  if (empty($this->info_bits)) $this->info_bits = 0;
2963  if (empty($this->special_code)) $this->special_code = 0;
2964  if (empty($this->fk_parent_line)) $this->fk_parent_line = 0;
2965  if (empty($this->fk_fournprice)) $this->fk_fournprice = 0;
2966  if (empty($this->fk_unit)) $this->fk_unit = 0;
2967  if (empty($this->subprice)) $this->subprice = 0;
2968 
2969  if (empty($this->pa_ht)) $this->pa_ht = 0;
2970 
2971  // if buy price not defined, define buyprice as configured in margin admin
2972  if ($this->pa_ht == 0)
2973  {
2974  if (($result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0)
2975  {
2976  return $result;
2977  } else {
2978  $this->pa_ht = $result;
2979  }
2980  }
2981 
2982  // Check parameters
2983  if ($this->product_type < 0) return -1;
2984 
2985  $this->db->begin();
2986 
2987  // Insert line into database
2988  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'supplier_proposaldet';
2989  $sql .= ' (fk_supplier_proposal, fk_parent_line, label, description, fk_product, product_type,';
2990  $sql .= ' date_start, date_end,';
2991  $sql .= ' fk_remise_except, qty, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
2992  $sql .= ' subprice, remise_percent, ';
2993  $sql .= ' info_bits, ';
2994  $sql .= ' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_product_fournisseur_price, buy_price_ht, special_code, rang,';
2995  $sql .= ' ref_fourn,';
2996  $sql .= ' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc, fk_unit)';
2997  $sql .= " VALUES (".$this->fk_supplier_proposal.",";
2998  $sql .= " ".($this->fk_parent_line > 0 ? "'".$this->db->escape($this->fk_parent_line)."'" : "null").",";
2999  $sql .= " ".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
3000  $sql .= " '".$this->db->escape($this->desc)."',";
3001  $sql .= " ".($this->fk_product ? "'".$this->db->escape($this->fk_product)."'" : "null").",";
3002  $sql .= " '".$this->db->escape($this->product_type)."',";
3003  $sql .= " ".($this->date_start ? "'".$this->db->idate($this->date_start)."'" : "null").",";
3004  $sql .= " ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null").",";
3005  $sql .= " ".($this->fk_remise_except ? "'".$this->db->escape($this->fk_remise_except)."'" : "null").",";
3006  $sql .= " ".price2num($this->qty).",";
3007  $sql .= " ".price2num($this->tva_tx).",";
3008  $sql .= " ".price2num($this->localtax1_tx).",";
3009  $sql .= " ".price2num($this->localtax2_tx).",";
3010  $sql .= " '".$this->db->escape($this->localtax1_type)."',";
3011  $sql .= " '".$this->db->escape($this->localtax2_type)."',";
3012  $sql .= " ".(!empty($this->subprice) ?price2num($this->subprice) : "null").",";
3013  $sql .= " ".price2num($this->remise_percent).",";
3014  $sql .= " ".(isset($this->info_bits) ? "'".$this->db->escape($this->info_bits)."'" : "null").",";
3015  $sql .= " ".price2num($this->total_ht).",";
3016  $sql .= " ".price2num($this->total_tva).",";
3017  $sql .= " ".price2num($this->total_localtax1).",";
3018  $sql .= " ".price2num($this->total_localtax2).",";
3019  $sql .= " ".price2num($this->total_ttc).",";
3020  $sql .= " ".(!empty($this->fk_fournprice) ? "'".$this->db->escape($this->fk_fournprice)."'" : "null").",";
3021  $sql .= " ".(isset($this->pa_ht) ? "'".price2num($this->pa_ht)."'" : "null").",";
3022  $sql .= ' '.$this->special_code.',';
3023  $sql .= ' '.$this->rang.',';
3024  $sql .= " '".$this->db->escape($this->ref_fourn)."'";
3025  $sql .= ", ".($this->fk_multicurrency > 0 ? $this->fk_multicurrency : 'null');
3026  $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
3027  $sql .= ", ".$this->multicurrency_subprice;
3028  $sql .= ", ".$this->multicurrency_total_ht;
3029  $sql .= ", ".$this->multicurrency_total_tva;
3030  $sql .= ", ".$this->multicurrency_total_ttc;
3031  $sql .= ", ".($this->fk_unit ? $this->fk_unit : 'null');
3032  $sql .= ')';
3033 
3034  dol_syslog(get_class($this).'::insert', LOG_DEBUG);
3035  $resql = $this->db->query($sql);
3036  if ($resql)
3037  {
3038  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'supplier_proposaldet');
3039 
3040  if (!$error)
3041  {
3042  $result = $this->insertExtraFields();
3043  if ($result < 0)
3044  {
3045  $error++;
3046  }
3047  }
3048 
3049  if (!$error && !$notrigger)
3050  {
3051  // Call trigger
3052  $result = $this->call_trigger('LINESUPPLIER_PROPOSAL_INSERT', $user);
3053  if ($result < 0)
3054  {
3055  $this->db->rollback();
3056  return -1;
3057  }
3058  // End call triggers
3059  }
3060 
3061  $this->db->commit();
3062  return 1;
3063  } else {
3064  $this->error = $this->db->error()." sql=".$sql;
3065  $this->db->rollback();
3066  return -1;
3067  }
3068  }
3069 
3075  public function delete()
3076  {
3077  global $conf, $langs, $user;
3078 
3079  $error = 0;
3080  $this->db->begin();
3081 
3082  $sql = "DELETE FROM ".MAIN_DB_PREFIX."supplier_proposaldet WHERE rowid = ".$this->id;
3083  dol_syslog("SupplierProposalLine::delete", LOG_DEBUG);
3084  if ($this->db->query($sql))
3085  {
3086  // Remove extrafields
3087  if (!$error)
3088  {
3089  $result = $this->deleteExtraFields();
3090  if ($result < 0)
3091  {
3092  $error++;
3093  dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
3094  }
3095  }
3096 
3097  // Call trigger
3098  $result = $this->call_trigger('LINESUPPLIER_PROPOSAL_DELETE', $user);
3099  if ($result < 0)
3100  {
3101  $this->db->rollback();
3102  return -1;
3103  }
3104  // End call triggers
3105 
3106  $this->db->commit();
3107 
3108  return 1;
3109  } else {
3110  $this->error = $this->db->error()." sql=".$sql;
3111  $this->db->rollback();
3112  return -1;
3113  }
3114  }
3115 
3122  public function update($notrigger = 0)
3123  {
3124  global $conf, $langs, $user;
3125 
3126  $error = 0;
3127 
3128  // Clean parameters
3129  if (empty($this->tva_tx)) $this->tva_tx = 0;
3130  if (empty($this->localtax1_tx)) $this->localtax1_tx = 0;
3131  if (empty($this->localtax2_tx)) $this->localtax2_tx = 0;
3132  if (empty($this->total_localtax1)) $this->total_localtax1 = 0;
3133  if (empty($this->total_localtax2)) $this->total_localtax2 = 0;
3134  if (empty($this->localtax1_type)) $this->localtax1_type = 0;
3135  if (empty($this->localtax2_type)) $this->localtax2_type = 0;
3136  if (empty($this->marque_tx)) $this->marque_tx = 0;
3137  if (empty($this->marge_tx)) $this->marge_tx = 0;
3138  if (empty($this->remise_percent)) $this->remise_percent = 0;
3139  if (empty($this->info_bits)) $this->info_bits = 0;
3140  if (empty($this->special_code)) $this->special_code = 0;
3141  if (empty($this->fk_parent_line)) $this->fk_parent_line = 0;
3142  if (empty($this->fk_fournprice)) $this->fk_fournprice = 0;
3143  if (empty($this->fk_unit)) $this->fk_unit = 0;
3144  if (empty($this->subprice)) $this->subprice = 0;
3145 
3146  if (empty($this->pa_ht)) $this->pa_ht = 0;
3147 
3148  // if buy price not defined, define buyprice as configured in margin admin
3149  if ($this->pa_ht == 0)
3150  {
3151  if (($result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0)
3152  {
3153  return $result;
3154  } else {
3155  $this->pa_ht = $result;
3156  }
3157  }
3158 
3159  $this->db->begin();
3160 
3161  // Mise a jour ligne en base
3162  $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposaldet SET";
3163  $sql .= " description='".$this->db->escape($this->desc)."'";
3164  $sql .= " , label=".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null");
3165  $sql .= " , product_type=".$this->product_type;
3166  $sql .= " , date_start=".($this->date_start ? "'".$this->db->idate($this->date_start)."'" : "null");
3167  $sql .= " , date_end=".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
3168  $sql .= " , tva_tx='".price2num($this->tva_tx)."'";
3169  $sql .= " , localtax1_tx=".price2num($this->localtax1_tx);
3170  $sql .= " , localtax2_tx=".price2num($this->localtax2_tx);
3171  $sql .= " , localtax1_type='".$this->db->escape($this->localtax1_type)."'";
3172  $sql .= " , localtax2_type='".$this->db->escape($this->localtax2_type)."'";
3173  $sql .= " , qty='".price2num($this->qty)."'";
3174  $sql .= " , subprice=".price2num($this->subprice)."";
3175  $sql .= " , remise_percent=".price2num($this->remise_percent)."";
3176  $sql .= " , info_bits='".$this->db->escape($this->info_bits)."'";
3177  if (empty($this->skip_update_total))
3178  {
3179  $sql .= " , total_ht=".price2num($this->total_ht)."";
3180  $sql .= " , total_tva=".price2num($this->total_tva)."";
3181  $sql .= " , total_ttc=".price2num($this->total_ttc)."";
3182  $sql .= " , total_localtax1=".price2num($this->total_localtax1)."";
3183  $sql .= " , total_localtax2=".price2num($this->total_localtax2)."";
3184  }
3185  $sql .= " , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? "'".$this->db->escape($this->fk_fournprice)."'" : "null");
3186  $sql .= " , buy_price_ht=".price2num($this->pa_ht);
3187  if (strlen($this->special_code)) $sql .= " , special_code=".$this->special_code;
3188  $sql .= " , fk_parent_line=".($this->fk_parent_line > 0 ? $this->fk_parent_line : "null");
3189  if (!empty($this->rang)) $sql .= ", rang=".$this->rang;
3190  $sql .= " , ref_fourn=".(!empty($this->ref_fourn) ? "'".$this->db->escape($this->ref_fourn)."'" : "null");
3191  $sql .= " , fk_unit=".($this->fk_unit ? $this->fk_unit : 'null');
3192 
3193  // Multicurrency
3194  $sql .= " , multicurrency_subprice=".price2num($this->multicurrency_subprice)."";
3195  $sql .= " , multicurrency_total_ht=".price2num($this->multicurrency_total_ht)."";
3196  $sql .= " , multicurrency_total_tva=".price2num($this->multicurrency_total_tva)."";
3197  $sql .= " , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc)."";
3198 
3199  $sql .= " WHERE rowid = ".$this->id;
3200 
3201  dol_syslog(get_class($this)."::update", LOG_DEBUG);
3202  $resql = $this->db->query($sql);
3203  if ($resql)
3204  {
3205  if (!$error)
3206  {
3207  $result = $this->insertExtraFields();
3208  if ($result < 0)
3209  {
3210  $error++;
3211  }
3212  }
3213 
3214  if (!$error && !$notrigger)
3215  {
3216  // Call trigger
3217  $result = $this->call_trigger('LINESUPPLIER_PROPOSAL_UPDATE', $user);
3218  if ($result < 0)
3219  {
3220  $this->db->rollback();
3221  return -1;
3222  }
3223  // End call triggers
3224  }
3225 
3226  $this->db->commit();
3227  return 1;
3228  } else {
3229  $this->error = $this->db->error();
3230  $this->db->rollback();
3231  return -2;
3232  }
3233  }
3234 
3235  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
3242  public function update_total()
3243  {
3244  // phpcs:enable
3245  $this->db->begin();
3246 
3247  // Mise a jour ligne en base
3248  $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposaldet SET";
3249  $sql .= " total_ht=".price2num($this->total_ht, 'MT');
3250  $sql .= ",total_tva=".price2num($this->total_tva, 'MT');
3251  $sql .= ",total_ttc=".price2num($this->total_ttc, 'MT');
3252  $sql .= " WHERE rowid = ".$this->id;
3253 
3254  dol_syslog("SupplierProposalLine::update_total", LOG_DEBUG);
3255 
3256  $resql = $this->db->query($sql);
3257  if ($resql)
3258  {
3259  $this->db->commit();
3260  return 1;
3261  } else {
3262  $this->error = $this->db->error();
3263  $this->db->rollback();
3264  return -2;
3265  }
3266  }
3267 }
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
fetch($rowid)
Retrieve the propal line object.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto= 'UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1=0, $txlocaltax2=0, $fk_product=0, $remise_percent=0, $price_base_type= 'HT', $pu_ttc=0, $info_bits=0, $type=0, $rang=-1, $special_code=0, $fk_parent_line=0, $fk_fournprice=0, $pa_ht=0, $label= '', $array_options=0, $ref_supplier= '', $fk_unit= '', $origin= '', $origin_id=0, $pu_ht_devise=0, $date_start=0, $date_end=0)
Add a proposal line into database (linked to product/service or not) Les parametres sont deja cense e...
LibStatut($status, $mode=1)
Return label of a status (draft, validated, ...)
const STATUS_NOTSIGNED
Not signed quote, canceled.
if(!empty($arrayfields['u.datec']['checked'])) print_liste_field_titre("DateCreationShort"u if(!empty($arrayfields['u.tms']['checked'])) print_liste_field_titre("DateModificationShort"u if(!empty($arrayfields['u.statut']['checked'])) print_liste_field_titre("Status"u statut
Definition: list.php:632
getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
Return an array with margins information of a line.
setDraft($user)
Set draft status.
static getIdAndTxFromCode($db, $code, $date_document= '')
Get id and rate of currency from code.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
updatePriceFournisseur($idProductFournPrice, $product, $user)
Upate ProductFournisseur.
</td >< tdcolspan="3">< spanclass="opacitymedium"></span ></td ></tr >< trclass="liste_total"> CREANCES DETTES< tdcolspan="3"class="right"></td >< tdcolspan="3"class="right"></td ></tr > CREANCES DETTES RECETTES DEPENSES trips CREANCES DETTES Y m expensereport p date_valid Y m expensereport pe datep $db idate($date_start)."' AND $column < p rowid
liste_array($shortlist=0, $draft=0, $notcurrentuser=0, $socid=0, $limit=0, $offset=0, $sortfield= 'p.datec', $sortorder= 'DESC')
Return list of askprice (eventually filtered on user) into an array.
Class to manage products or services.
dol_now($mode= 'auto')
Return date for now.
line_order($renum=false, $rowidorder= 'ASC', $fk_parent_line=true)
Save a new position (field rang) for details lines.
dol_delete_preview($object)
Delete all preview files linked to object instance.
Definition: files.lib.php:1335
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
Class to manage Dolibarr users.
Definition: user.class.php:44
__construct($db)
Class line Contructor.
Class to manage Dolibarr database access.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
__construct($db, $socid="", $supplier_proposalid=0)
Constructor.
const STATUS_CLOSE
Billed or closed/processed quote.
info($id)
Object SupplierProposal Information.
const TYPE_PRODUCT
Regular product.
get_default_npr(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Fonction qui renvoie si tva doit etre tva percue recuperable.
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id $this-&gt;socid or $this-&gt;fk_soc, into this-&gt;thirdparty.
update($notrigger=0)
Update propal line object into DB.
load_state_board()
Load indicator this-&gt;nb of global stats widget.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
get_localtax($vatrate, $local, $thirdparty_buyer="", $thirdparty_seller="", $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate, from a $thirdparty_buyer to a $thirdparty_seller Note: This function applies same rules than get_default_tva.
reopen($user, $statut, $note= '', $notrigger=0)
Reopen the commercial proposal.
create($user, $notrigger=0)
Create commercial proposal into database this-&gt;ref can be set or empty.
$conf db
API class for accounts.
Definition: inc.php:54
getNomUrl($withpicto=0, $option= '', $get_params= '', $notooltip=0, $save_lastsearch_value=-1, $addlinktonotes=0)
Return clicable link of object (with eventually picto)
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...
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
insertExtraFields($trigger= '', $userused=null)
Add/Update all extra fields values for the current object.
getLinesArray()
Retrieve an array of supplier proposal lines.
const STATUS_SIGNED
Signed quote.
insert($notrigger=0)
Insert object line propal in database.
Class to manage third parties objects (customers, suppliers, prospects...)
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
createFromClone(User $user, $fromid=0)
Load an object from its id and create a new one in database.
update_total()
Update DB line fields total_xxx Used by migration.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
createPriceFournisseur($product, $user)
Create ProductFournisseur.
add_product($idproduct, $qty, $remise_percent=0)
Add line into array -&gt;lines.
const STATUS_VALIDATED
Validated status.
deleteEcmFiles($mode=0)
Delete related files of object in database.
insert_discount($idremise)
Adding line of fixed discount in the proposal in DB.
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
load_board($user, $mode)
Load indicators for dashboard (this-&gt;nbtodo and this-&gt;nbtodolate)
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories) ...
Definition: files.lib.php:1286
valid($user, $notrigger=0)
Set status to validated.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1)
Remove a file or several files with a mask.
Definition: files.lib.php:1144
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
deleteExtraFields()
Delete all extra fields values for the current object.
Class to manage translations.
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
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
set_remise_absolue($user, $remise)
Set an absolute overall discount on the proposal.
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this-&gt;array_options This method is in most cases call...
deleteObjectLinked($sourceid=null, $sourcetype= '', $targetid=null, $targettype= '', $rowid= '')
Delete all links between an object $this.
set_date_livraison($user, $delivery_date)
Set delivery date.
defineBuyPrice($unitPrice=0.0, $discountPercent=0.0, $fk_product=0)
Get buy price to use for margin calculation.
Class to manage supplier_proposal lines.
print $_SERVER["PHP_SELF"]
Edit parameters.
trait CommonIncoterm
Superclass for incoterm classes.
setDeliveryDate($user, $delivery_date)
Set delivery date.
print
Draft customers invoices.
Definition: index.php:89
call_trigger($triggerName, $user)
Call trigger based on this instance.
const STATUS_DRAFT
Draft status.
getLibStatut($mode=0)
Return label of status of proposal (draft, validated, ...)
Class to manage price ask supplier.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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) Si ...
Class to manage absolute discounts.
initAsSpecimen()
Initialise an instance with random values.
Class Currency.
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
cloture($user, $status, $note)
Close the askprice.
updateOrCreatePriceFournisseur($user)
Add or update supplier price according to result of proposal.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
getNextNumRef($soc)
Returns the reference to the following non used Proposal used depending on the active numbering modul...
add_object_linked($origin=null, $origin_id=null)
Add objects linked in llx_element_element.
deleteline($lineid)
Delete detail line.
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)...
updateline($rowid, $pu, $qty, $remise_percent, $txtva, $txlocaltax1=0, $txlocaltax2=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, $array_options=0, $ref_supplier= '', $fk_unit= '', $pu_ht_devise=0)
Update a proposal line.
if(!defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN'
Draft customers invoices.
fetch($rowid, $ref= '')
Load a proposal from database and its ligne array.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
Class to manage predefined suppliers products.
if(!empty($search_group)) natural_search(array("g.nom"g note
Definition: list.php:122
set_remise_percent($user, $remise)
Set an overall discount on the proposal.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $keepmoretags= '', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...