dolibarr  9.0.0
fournisseur.product.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2009-2014 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
6  * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
7  * Copyright (C) 2015 Marcos GarcĂ­a <marcosgdf@gmail.com>
8  * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
30 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
33 
34 
39 {
43  public $db;
44 
48  public $error='';
49 
50  public $product_fourn_price_id; // id of ligne product-supplier
51 
55  public $id;
56 
61  public $fourn_ref;
62  public $delivery_time_days;
63  public $ref_supplier; // ref supplier (can be set by get_buyprice)
64  public $desc_supplier;
65  public $vatrate_supplier; // default vat rate for this supplier/qty/product (can be set by get_buyprice)
66 
67  public $fourn_id; //supplier id
68  public $fourn_qty; // quantity for price (can be set by get_buyprice)
69  public $fourn_pu; // unit price for quantity (can be set by get_buyprice)
70 
71  public $fourn_price; // price for quantity
72  public $fourn_remise_percent; // discount for quantity (percent)
73  public $fourn_remise; // discount for quantity (amount)
74  public $product_fourn_id; // supplier id
75 
79  public $fk_availability;
80 
81  public $fourn_unitprice;
82  public $fourn_tva_tx;
83  public $fourn_tva_npr;
84 
88  public $fk_supplier_price_expression;
89 
90  public $supplier_reputation; // reputation of supplier
91  public $reputations=array(); // list of available supplier reputations
92 
93  // Multicurreny
94  public $fourn_multicurrency_id;
95  public $fourn_multicurrency_code;
96  public $fourn_multicurrency_tx;
97  public $fourn_multicurrency_price;
98  public $fourn_multicurrency_unitprice;
99 
105  function __construct($db)
106  {
107  global $langs;
108 
109  $this->db = $db;
110  $langs->load("suppliers");
111  $this->reputations= array('-1'=>'', 'FAVORITE'=>$langs->trans('Favorite'),'NOTTHGOOD'=>$langs->trans('NotTheGoodQualitySupplier'), 'DONOTORDER'=>$langs->trans('DoNotOrderThisProductToThisSupplier'));
112  }
113 
114 
115 
116  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
123  function remove_fournisseur($id_fourn)
124  {
125  // phpcs:enable
126  $ok=1;
127 
128  $this->db->begin();
129 
130  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
131  $sql.= " WHERE fk_product = ".$this->id." AND fk_soc = ".$id_fourn;
132 
133  dol_syslog(get_class($this)."::remove_fournisseur", LOG_DEBUG);
134  $resql2=$this->db->query($sql);
135  if (! $resql2)
136  {
137  $this->error=$this->db->lasterror();
138  $ok=0;
139  }
140 
141  if ($ok)
142  {
143  $this->db->commit();
144  return 1;
145  }
146  else
147  {
148  $this->db->rollback();
149  return -1;
150  }
151  }
152 
153 
154  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
162  {
163  // phpcs:enable
164  global $conf, $user;
165 
166  $error=0;
167 
168  $this->db->begin();
169 
170  // Call trigger
171  $result=$this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_DELETE',$user);
172  if ($result < 0) $error++;
173  // End call triggers
174 
175  if (empty($error))
176  {
177 
178  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
179  $sql.= " WHERE rowid = ".$rowid;
180 
181  dol_syslog(get_class($this)."::remove_product_fournisseur_price", LOG_DEBUG);
182  $resql = $this->db->query($sql);
183  if (!$resql)
184  {
185  $this->error=$this->db->lasterror();
186  $error++;
187  }
188  }
189 
190  if (empty($error)) {
191  $this->db->commit();
192  return 1;
193  } else {
194  $this->db->rollback();
195  return -1;
196  }
197  }
198 
199 
200  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
227  function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx, $charges=0, $remise_percent=0, $remise=0, $newnpr=0, $delivery_time_days=0, $supplier_reputation='', $localtaxes_array=array(), $newdefaultvatcode='', $multicurrency_buyprice=0, $multicurrency_price_base_type='HT',$multicurrency_tx=1,$multicurrency_code='', $desc_fourn='')
228  {
229  // phpcs:enable
230  global $conf, $langs;
231  //global $mysoc;
232 
233  // Clean parameter
234  if (empty($qty)) $qty=0;
235  if (empty($buyprice)) $buyprice=0;
236  if (empty($charges)) $charges=0;
237  if (empty($availability)) $availability=0;
238  if (empty($remise_percent)) $remise_percent=0;
239  if (empty($supplier_reputation) || $supplier_reputation == -1) $supplier_reputation='';
240  if ($delivery_time_days != '' && ! is_numeric($delivery_time_days)) $delivery_time_days = '';
241  if ($price_base_type == 'TTC')
242  {
243  $ttx = $tva_tx;
244  $buyprice = $buyprice/(1+($ttx/100));
245  }
246 
247  // Multicurrency
248  if ($conf->multicurrency->enabled) {
249  if (empty($multicurrency_tx)) $multicurrency_tx=1;
250  if (empty($multicurrency_buyprice)) $multicurrency_buyprice=0;
251 
252  if (empty($multicurrency_buyprice)) $multicurrency_buyprice=0;
253  if ($multicurrency_price_base_type == 'TTC')
254  {
255  $ttx = $tva_tx;
256  $multicurrency_buyprice = $multicurrency_buyprice/(1+($ttx/100));
257  }
258  $multicurrency_buyprice=price2num($multicurrency_buyprice,'MU');
259  $multicurrency_unitBuyPrice=price2num($multicurrency_buyprice/$qty,'MU');
260 
261  $buyprice=$multicurrency_buyprice/$multicurrency_tx;
262  $fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $multicurrency_code);
263  }
264 
265  $buyprice=price2num($buyprice,'MU');
266  $charges=price2num($charges,'MU');
267  $qty=price2num($qty);
268  $error=0;
269 
270  $unitBuyPrice = price2num($buyprice/$qty,'MU');
271 
272  $now=dol_now();
273 
274  $newvat = $tva_tx;
275 
276  if (count($localtaxes_array) > 0)
277  {
278  $localtaxtype1=$localtaxes_array['0'];
279  $localtax1=$localtaxes_array['1'];
280  $localtaxtype2=$localtaxes_array['2'];
281  $localtax2=$localtaxes_array['3'];
282  }
283  else // old method. deprecated because ot can't retreive type
284  {
285  $localtaxtype1='0';
286  $localtax1=get_localtax($newvat,1);
287  $localtaxtype2='0';
288  $localtax2=get_localtax($newvat,2);
289  }
290  if (empty($localtax1)) $localtax1=0; // If = '' then = 0
291  if (empty($localtax2)) $localtax2=0; // If = '' then = 0
292 
293  $this->db->begin();
294 
295  if ($this->product_fourn_price_id > 0)
296  {
297  $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
298  $sql.= " SET fk_user = " . $user->id." ,";
299  $sql.= " ref_fourn = '" . $this->db->escape($ref_fourn) . "',";
300  $sql.= " desc_fourn = '" . $this->db->escape($desc_fourn) . "',";
301  $sql.= " price = ".price2num($buyprice).",";
302  $sql.= " quantity = ".$qty.",";
303  $sql.= " remise_percent = ".$remise_percent.",";
304  $sql.= " remise = ".$remise.",";
305  $sql.= " unitprice = ".$unitBuyPrice.",";
306  $sql.= " fk_availability = ".$availability.",";
307  $sql.= " multicurrency_price = ".(isset($multicurrency_buyprice)?"'".$this->db->escape(price2num($multicurrency_buyprice))."'":'null').",";
308  $sql.= " multicurrency_unitprice = ".(isset($multicurrency_unitBuyPrice)?"'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'":'null').",";
309  $sql.= " multicurrency_tx = ".(isset($multicurrency_tx)?"'".$this->db->escape($multicurrency_tx)."'":'1').",";
310  $sql.= " fk_multicurrency = ".(isset($fk_multicurrency)?"'".$this->db->escape($fk_multicurrency)."'":'null').",";
311  $sql.= " multicurrency_code = ".(isset($multicurrency_code)?"'".$this->db->escape($multicurrency_code)."'":'null').",";
312  $sql.= " entity = ".$conf->entity.",";
313  $sql.= " tva_tx = ".price2num($tva_tx).",";
314  // TODO Add localtax1 and localtax2
315  //$sql.= " localtax1_tx=".($localtax1>=0?$localtax1:'NULL').",";
316  //$sql.= " localtax2_tx=".($localtax2>=0?$localtax2:'NULL').",";
317  //$sql.= " localtax1_type=".($localtaxtype1!=''?"'".$localtaxtype1."'":"'0'").",";
318  //$sql.= " localtax2_type=".($localtaxtype2!=''?"'".$localtaxtype2."'":"'0'").",";
319  $sql.= " default_vat_code=".($newdefaultvatcode?"'".$this->db->escape($newdefaultvatcode)."'":"null").",";
320  $sql.= " info_bits = ".$newnpr.",";
321  $sql.= " charges = ".$charges.","; // deprecated
322  $sql.= " delivery_time_days = ".($delivery_time_days != '' ? $delivery_time_days : 'null').",";
323  $sql.= " supplier_reputation = ".(empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'");
324  $sql.= " WHERE rowid = ".$this->product_fourn_price_id;
325  // TODO Add price_base_type and price_ttc
326 
327  dol_syslog(get_class($this).'::update_buyprice update knowing id of line = product_fourn_price_id = '.$this->product_fourn_price_id, LOG_DEBUG);
328  $resql = $this->db->query($sql);
329  if ($resql)
330  {
331  // Call trigger
332  $result=$this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_UPDATE',$user);
333  if ($result < 0) $error++;
334  // End call triggers
335 
336  if (empty($error))
337  {
338  $this->db->commit();
339  return $this->product_fourn_price_id;
340  }
341  else
342  {
343  $this->db->rollback();
344  return -1;
345  }
346  }
347  else
348  {
349  $this->error=$this->db->error()." sql=".$sql;
350  $this->db->rollback();
351  return -2;
352  }
353  }
354 
355  else
356  {
357  dol_syslog(get_class($this) . '::update_buyprice without knowing id of line, so we delete from company, quantity and supplier_ref and insert again', LOG_DEBUG);
358 
359  // Delete price for this quantity
360  $sql = "DELETE FROM " . MAIN_DB_PREFIX . "product_fournisseur_price";
361  $sql .= " WHERE fk_soc = " . $fourn->id . " AND ref_fourn = '" . $this->db->escape($ref_fourn) . "' AND quantity = " . $qty . " AND entity = " . $conf->entity;
362  $resql = $this->db->query($sql);
363  if ($resql) {
364  // Add price for this quantity to supplier
365  $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_fournisseur_price(";
366  $sql.= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
367  $sql .= "datec, fk_product, fk_soc, ref_fourn, desc_fourn, fk_user, price, quantity, remise_percent, remise, unitprice, tva_tx, charges, fk_availability, default_vat_code, info_bits, entity, delivery_time_days, supplier_reputation)";
368  $sql .= " values(";
369  $sql.= (isset($multicurrency_buyprice)?"'".$this->db->escape(price2num($multicurrency_buyprice))."'":'null').",";
370  $sql.= (isset($multicurrency_unitBuyPrice)?"'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'":'null').",";
371  $sql.= (isset($multicurrency_tx)?"'".$this->db->escape($multicurrency_tx)."'":'1').",";
372  $sql.= (isset($fk_multicurrency)?"'".$this->db->escape($fk_multicurrency)."'":'null').",";
373  $sql.= (isset($multicurrency_code)?"'".$this->db->escape($multicurrency_code)."'":'null').",";
374  $sql .= " '" . $this->db->idate($now) . "',";
375  $sql .= " " . $this->id . ",";
376  $sql .= " " . $fourn->id . ",";
377  $sql .= " '" . $this->db->escape($ref_fourn) . "',";
378  $sql .= " '" . $this->db->escape($desc_fourn) . "',";
379  $sql .= " " . $user->id . ",";
380  $sql .= " " . $buyprice . ",";
381  $sql .= " " . $qty . ",";
382  $sql .= " " . $remise_percent . ",";
383  $sql .= " " . $remise . ",";
384  $sql .= " " . $unitBuyPrice . ",";
385  $sql .= " " . $tva_tx . ",";
386  $sql .= " " . $charges . ",";
387  $sql .= " " . $availability . ",";
388  $sql .= " ".($newdefaultvatcode?"'".$this->db->escape($newdefaultvatcode)."'":"null").",";
389  $sql .= " " . $newnpr . ",";
390  $sql .= $conf->entity . ",";
391  $sql .= $delivery_time_days . ",";
392  $sql .= (empty($supplier_reputation) ? 'NULL' : "'" . $this->db->escape($supplier_reputation) . "'");
393  $sql .= ")";
394 
395  $idinserted = 0;
396 
397  $resql = $this->db->query($sql);
398  if ($resql) {
399  $idinserted = $this->db->last_insert_id(MAIN_DB_PREFIX . "product_fournisseur_price");
400  }
401  else {
402  $error++;
403  }
404 
405  if (! $error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
406  // Add record into log table
407  $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_fournisseur_price_log(";
408  $sql.= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
409  $sql .= "datec, fk_product_fournisseur,fk_user,price,quantity)";
410  $sql .= "values(";
411  $sql.= (isset($multicurrency_buyprice)?"'".$this->db->escape(price2num($multicurrency_buyprice))."'":'null').",";
412  $sql.= (isset($multicurrency_unitBuyPrice)?"'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'":'null').",";
413  $sql.= (isset($multicurrency_tx)?"'".$this->db->escape($multicurrency_tx)."'":'1').",";
414  $sql.= (isset($fk_multicurrency)?"'".$this->db->escape($fk_multicurrency)."'":'null').",";
415  $sql.= (isset($multicurrency_code)?"'".$this->db->escape($multicurrency_code)."'":'null').",";
416  $sql .= " '" . $this->db->idate($now) . "',";
417  $sql .= " " . $this->product_fourn_id . ",";
418  $sql .= " " . $user->id . ",";
419  $sql .= " " . price2num($buyprice) . ",";
420  $sql .= " " . $qty;
421  $sql .= ")";
422 
423  $resql = $this->db->query($sql);
424  if (! $resql) {
425  $error++;
426  }
427  }
428 
429  if (! $error) {
430  // Call trigger
431  $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_CREATE', $user);
432  if ($result < 0)
433  $error++;
434  // End call triggers
435 
436  if (empty($error)) {
437  $this->db->commit();
438  return $idinserted;
439  } else {
440  $this->db->rollback();
441  return -1;
442  }
443  } else {
444  $this->error = $this->db->lasterror() . " sql=" . $sql;
445  $this->db->rollback();
446  return -2;
447  }
448  } else {
449  $this->error = $this->db->lasterror() . " sql=" . $sql;
450  $this->db->rollback();
451  return - 1;
452  }
453  }
454  }
455 
456  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
464  function fetch_product_fournisseur_price($rowid, $ignore_expression = 0)
465  {
466  // phpcs:enable
467  global $conf;
468 
469  $sql = "SELECT pfp.rowid, pfp.price, pfp.quantity, pfp.unitprice, pfp.remise_percent, pfp.remise, pfp.tva_tx, pfp.default_vat_code, pfp.info_bits as fourn_tva_npr, pfp.fk_availability,";
470  $sql.= " pfp.fk_soc, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_product, pfp.charges, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
471  $sql.= " pfp.supplier_reputation";
472  $sql.= " ,pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code";
473  $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
474  $sql.= " WHERE pfp.rowid = ".$rowid;
475 
476  dol_syslog(get_class($this)."::fetch_product_fournisseur_price", LOG_DEBUG);
477  $resql = $this->db->query($sql);
478  if ($resql)
479  {
480  $obj = $this->db->fetch_object($resql);
481  if ($obj)
482  {
483  $this->product_fourn_price_id = $rowid;
484  $this->id = $obj->fk_product;
485  $this->fk_product = $obj->fk_product;
486  $this->product_id = $obj->fk_product; // deprecated
487  $this->fourn_id = $obj->fk_soc;
488  $this->fourn_ref = $obj->ref_fourn; // deprecated
489  $this->ref_supplier = $obj->ref_fourn;
490  $this->desc_supplier = $obj->desc_fourn;
491  $this->fourn_price = $obj->price;
492  $this->fourn_charges = $obj->charges; // deprecated
493  $this->fourn_qty = $obj->quantity;
494  $this->fourn_remise_percent = $obj->remise_percent;
495  $this->fourn_remise = $obj->remise;
496  $this->fourn_unitprice = $obj->unitprice;
497  $this->fourn_tva_tx = $obj->tva_tx;
498  $this->fourn_tva_npr = $obj->fourn_tva_npr;
499  // Add also localtaxes
500  $this->fk_availability = $obj->fk_availability;
501  $this->delivery_time_days = $obj->delivery_time_days;
502  $this->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
503  $this->supplier_reputation = $obj->supplier_reputation;
504  $this->default_vat_code = $obj->default_vat_code;
505 
506  $this->fourn_multicurrency_price = $obj->multicurrency_price;
507  $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
508  $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
509  $this->fourn_multicurrency_id = $obj->fk_multicurrency;
510  $this->fourn_multicurrency_code = $obj->multicurrency_code;
511 
512  if (empty($ignore_expression) && !empty($this->fk_supplier_price_expression))
513  {
514  $priceparser = new PriceParser($this->db);
515  $price_result = $priceparser->parseProductSupplier($this);
516  if ($price_result >= 0) {
517  $this->fourn_price = $price_result;
518  //recalculation of unitprice, as probably the price changed...
519  if ($this->fourn_qty!=0)
520  {
521  $this->fourn_unitprice = price2num($this->fourn_price/$this->fourn_qty,'MU');
522  }
523  else
524  {
525  $this->fourn_unitprice="";
526  }
527  }
528  }
529 
530  return 1;
531  }
532  else
533  {
534  return 0;
535  }
536  }
537  else
538  {
539  $this->error=$this->db->error();
540  return -1;
541  }
542  }
543 
544 
545  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
556  function list_product_fournisseur_price($prodid, $sortfield='', $sortorder='', $limit=0, $offset=0)
557  {
558  // phpcs:enable
559  global $conf;
560 
561  $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
562  $sql.= " pfp.rowid as product_fourn_pri_id, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_product as product_fourn_id, pfp.fk_supplier_price_expression,";
563  $sql.= " pfp.price, pfp.quantity, pfp.unitprice, pfp.remise_percent, pfp.remise, pfp.tva_tx, pfp.fk_availability, pfp.charges, pfp.info_bits, pfp.delivery_time_days, pfp.supplier_reputation,";
564  $sql.= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code, pfp.datec, pfp.tms";
565  $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."societe as s";
566  $sql.= " WHERE pfp.entity IN (".getEntity('productsupplierprice').")";
567  $sql.= " AND pfp.fk_soc = s.rowid";
568  $sql.= " AND s.status=1"; // only enabled company selected
569  $sql.= " AND pfp.fk_product = ".$prodid;
570  if (empty($sortfield)) $sql.= " ORDER BY s.nom, pfp.quantity, pfp.price";
571  else $sql.= $this->db->order($sortfield, $sortorder);
572  $sql.=$this->db->plimit($limit, $offset);
573  dol_syslog(get_class($this)."::list_product_fournisseur_price", LOG_DEBUG);
574 
575  $resql = $this->db->query($sql);
576  if ($resql)
577  {
578  $retarray = array();
579 
580  while ($record = $this->db->fetch_array($resql))
581  {
582  //define base attribute
583  $prodfourn = new ProductFournisseur($this->db);
584 
585  $prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"];
586  $prodfourn->product_fourn_id = $record["product_fourn_id"];
587  $prodfourn->fourn_ref = $record["ref_fourn"];
588  $prodfourn->ref_supplier = $record["ref_fourn"];
589  $prodfourn->desc_supplier = $record["desc_fourn"];
590  $prodfourn->fourn_price = $record["price"];
591  $prodfourn->fourn_qty = $record["quantity"];
592  $prodfourn->fourn_remise_percent = $record["remise_percent"];
593  $prodfourn->fourn_remise = $record["remise"];
594  $prodfourn->fourn_unitprice = $record["unitprice"];
595  $prodfourn->fourn_charges = $record["charges"]; // deprecated
596  $prodfourn->fourn_tva_tx = $record["tva_tx"];
597  $prodfourn->fourn_id = $record["fourn_id"];
598  $prodfourn->fourn_name = $record["supplier_name"];
599  $prodfourn->fk_availability = $record["fk_availability"];
600  $prodfourn->delivery_time_days = $record["delivery_time_days"];
601  $prodfourn->id = $prodid;
602  $prodfourn->fourn_tva_npr = $record["info_bits"];
603  $prodfourn->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
604  $prodfourn->supplier_reputation = $record["supplier_reputation"];
605  $prodfourn->date_creation = $this->db->jdate($record['datec']);
606  $prodfourn->date_modification = $this->db->jdate($record['tms']);
607 
608  $prodfourn->fourn_multicurrency_price = $record["multicurrency_price"];
609  $prodfourn->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
610  $prodfourn->fourn_multicurrency_tx = $record["multicurrency_tx"];
611  $prodfourn->fourn_multicurrency_id = $record["fk_multicurrency"];
612  $prodfourn->fourn_multicurrency_code = $record["multicurrency_code"];
613 
614  if (!empty($conf->dynamicprices->enabled) && !empty($prodfourn->fk_supplier_price_expression)) {
615  $priceparser = new PriceParser($this->db);
616  $price_result = $priceparser->parseProductSupplier($prodfourn);
617  if ($price_result >= 0) {
618  $prodfourn->fourn_price = $price_result;
619  $prodfourn->fourn_unitprice = null; //force recalculation of unitprice, as probably the price changed...
620  }
621  }
622 
623  if (!isset($prodfourn->fourn_unitprice))
624  {
625  if ($prodfourn->fourn_qty!=0)
626  {
627  $prodfourn->fourn_unitprice = price2num($prodfourn->fourn_price/$prodfourn->fourn_qty,'MU');
628  }
629  else
630  {
631  $prodfourn->fourn_unitprice="";
632  }
633  }
634 
635  $retarray[]=$prodfourn;
636  }
637 
638  $this->db->free($resql);
639  return $retarray;
640  }
641  else
642  {
643  $this->error=$this->db->error();
644  return -1;
645  }
646  }
647 
648  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
657  function find_min_price_product_fournisseur($prodid, $qty=0, $socid=0)
658  {
659  // phpcs:enable
660  global $conf;
661 
662  if (empty($prodid))
663  {
664  dol_syslog("Warning function find_min_price_product_fournisseur were called with prodid empty. May be a bug.", LOG_WARNING);
665  return 0;
666  }
667 
668  $this->product_fourn_price_id = '';
669  $this->product_fourn_id = '';
670  $this->fourn_ref = '';
671  $this->fourn_price = '';
672  $this->fourn_qty = '';
673  $this->fourn_remise_percent = '';
674  $this->fourn_remise = '';
675  $this->fourn_unitprice = '';
676  $this->fourn_id = '';
677  $this->fourn_name = '';
678  $this->delivery_time_days = '';
679  $this->id = '';
680 
681  $this->fourn_multicurrency_price = '';
682  $this->fourn_multicurrency_unitprice = '';
683  $this->fourn_multicurrency_tx = '';
684  $this->fourn_multicurrency_id = '';
685  $this->fourn_multicurrency_code = '';
686 
687  $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
688  $sql.= " pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
689  $sql.= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges,";
690  $sql.= " pfp.remise, pfp.remise_percent, pfp.fk_supplier_price_expression, pfp.delivery_time_days";
691  $sql.= " ,pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code";
692  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
693  $sql.= " WHERE s.entity IN (".getEntity('societe').")";
694  $sql.= " AND pfp.entity = ".$conf->entity; // only current entity
695  $sql.= " AND pfp.fk_product = ".$prodid;
696  $sql.= " AND pfp.fk_soc = s.rowid";
697  $sql.= " AND s.status = 1"; // only enabled society
698  if ($qty > 0) $sql.= " AND pfp.quantity <= ".$qty;
699  if ($socid > 0) $sql.= ' AND pfp.fk_soc = '.$socid;
700 
701  dol_syslog(get_class($this)."::find_min_price_product_fournisseur", LOG_DEBUG);
702 
703  $resql = $this->db->query($sql);
704  if ($resql)
705  {
706  $record_array = array();
707 
708  //Store each record to array for later search of min
709  while ($record = $this->db->fetch_array($resql))
710  {
711  $record_array[]=$record;
712  }
713 
714  if (count($record_array) == 0)
715  {
716  $this->db->free($resql);
717  return 0;
718  }
719  else
720  {
721  $min = -1;
722  foreach($record_array as $record)
723  {
724  $fourn_price = $record["price"];
725  // discount calculated buy price
726  $fourn_unitprice = $record["unitprice"] * (1 - $record["remise_percent"] / 100) - $record["remise"];
727  if (!empty($conf->dynamicprices->enabled) && !empty($record["fk_supplier_price_expression"])) {
728  $prod_supplier = new ProductFournisseur($this->db);
729  $prod_supplier->product_fourn_price_id = $record["product_fourn_price_id"];
730  $prod_supplier->id = $prodid;
731  $prod_supplier->fourn_qty = $record["quantity"];
732  $prod_supplier->fourn_tva_tx = $record["tva_tx"];
733  $prod_supplier->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
734  $priceparser = new PriceParser($this->db);
735  $price_result = $priceparser->parseProductSupplier($prod_supplier);
736  if ($price_result >= 0) {
737  $fourn_price = price2num($price_result,'MU');
738  if ($record["quantity"] != 0)
739  {
740  $fourn_unitprice = price2num($fourn_price/$record["quantity"],'MU');
741  }
742  else
743  {
744  $fourn_unitprice = $fourn_price;
745  }
746  }
747  }
748  if ($fourn_unitprice < $min || $min == -1)
749  {
750  $this->product_fourn_price_id = $record["product_fourn_price_id"];
751  $this->ref_supplier = $record["ref_fourn"];
752  $this->ref_fourn = $record["ref_fourn"]; // deprecated
753  $this->fourn_ref = $record["ref_fourn"]; // deprecated
754  $this->fourn_price = $fourn_price;
755  $this->fourn_qty = $record["quantity"];
756  $this->fourn_remise_percent = $record["remise_percent"];
757  $this->fourn_remise = $record["remise"];
758  $this->fourn_unitprice = $record["unitprice"];
759  $this->fourn_charges = $record["charges"]; // deprecated
760  $this->fourn_tva_tx = $record["tva_tx"];
761  $this->fourn_id = $record["fourn_id"];
762  $this->fourn_name = $record["supplier_name"];
763  $this->delivery_time_days = $record["delivery_time_days"];
764  $this->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
765  $this->id = $prodid;
766  $this->fourn_multicurrency_price = $record["multicurrency_price"];
767  $this->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
768  $this->fourn_multicurrency_tx = $record["multicurrency_tx"];
769  $this->fourn_multicurrency_id = $record["fk_multicurrency"];
770  $this->fourn_multicurrency_code = $record["multicurrency_code"];
771  $min = $fourn_unitprice;
772  }
773  }
774  }
775 
776  $this->db->free($resql);
777  return 1;
778  }
779  else
780  {
781  $this->error=$this->db->error();
782  return -1;
783  }
784  }
785 
792  function setSupplierPriceExpression($expression_id)
793  {
794  global $conf;
795 
796  // Clean parameters
797  $this->db->begin();
798  $expression_id = $expression_id != 0 ? $expression_id : 'NULL';
799 
800  $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
801  $sql.= " SET fk_supplier_price_expression = ".$expression_id;
802  $sql.= " WHERE rowid = ".$this->product_fourn_price_id;
803 
804  dol_syslog(get_class($this)."::setSupplierPriceExpression", LOG_DEBUG);
805 
806  $resql = $this->db->query($sql);
807  if ($resql)
808  {
809  $this->db->commit();
810  return 1;
811  }
812  else
813  {
814  $this->error=$this->db->error()." sql=".$sql;
815  $this->db->rollback();
816  return -1;
817  }
818  }
819 
830  function getSocNomUrl($withpicto=0,$option='supplier',$maxlen=0,$notooltip=0)
831  {
832  $thirdparty = new Fournisseur($this->db);
833  $thirdparty->fetch($this->fourn_id);
834 
835  return $thirdparty->getNomUrl($withpicto,$option,$maxlen,$notooltip);
836  }
837 
838  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
850  function display_price_product_fournisseur($showunitprice=1,$showsuptitle=1,$maxlen=0,$notooltip=0, $productFournList=array())
851  {
852  // phpcs:enable
853  global $langs;
854 
855  $out = '';
856  $langs->load("suppliers");
857  if (count($productFournList) > 0) {
858  $out .= '<table class="nobordernopadding" width="100%">';
859  $out .= '<tr><td class="liste_titre" align="right">'.($showunitprice?$langs->trans("Price").' '.$langs->trans("HT"):'').'</td>';
860  $out .= '<td class="liste_titre" align="right">'.($showunitprice?$langs->trans("QtyMin"):'').'</td>';
861  $out .= '<td class="liste_titre">'.$langs->trans("Supplier").'</td>';
862  $out .= '<td class="liste_titre">'.$langs->trans("SupplierRef").'</td></tr>';
863  foreach ($productFournList as $productFourn) {
864  $out.= '<tr><td align="right">'.($showunitprice?price($productFourn->fourn_unitprice * (1 -$productFourn->fourn_remise_percent/100) - $productFourn->fourn_remise):'').'</td>';
865  $out.= '<td align="right">'.($showunitprice?$productFourn->fourn_qty:'').'</td>';
866  $out.= '<td>'.$productFourn->getSocNomUrl(1, 'supplier', $maxlen, $notooltip).'</td>';
867  $out.= '<td>'.$productFourn->fourn_ref.'<td></tr>';
868  }
869  $out .= '</table>';
870  } else {
871  $out=($showunitprice?price($this->fourn_unitprice * (1 - $this->fourn_remise_percent/100) + $this->fourn_remise).' '.$langs->trans("HT").' &nbsp; (':'').($showsuptitle?$langs->trans("Supplier").': ':'').$this->getSocNomUrl(1, 'supplier', $maxlen, $notooltip).' / '.$langs->trans("SupplierRef").': '.$this->fourn_ref.($showunitprice?')':'');
872  }
873  return $out;
874  }
875 
884  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
885  {
886  $tables = array(
887  'product_fournisseur_price'
888  );
889 
890  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
891  }
892 }
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->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) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
static getIdFromCode(&$db, $code)
Get id of currency from code.
Class to parse product price expressions.
list_product_fournisseur_price($prodid, $sortfield='', $sortorder='', $limit=0, $offset=0)
List all supplier prices of a product.
getSocNomUrl($withpicto=0, $option='supplier', $maxlen=0, $notooltip=0)
Display supplier of product.
Class to manage products or services.
Class to manage Dolibarr database access.
setSupplierPriceExpression($expression_id)
Sets the supplier price expression.
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.
Class to manage suppliers.
find_min_price_product_fournisseur($prodid, $qty=0, $socid=0)
Load properties for minimum price.
remove_fournisseur($id_fourn)
Remove all prices for this couple supplier-product.
$tva_tx
Default VAT rate of product.
remove_product_fournisseur_price($rowid)
Remove a price for a couple supplier-product.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
dol_now($mode='gmt')
Return date for now.
display_price_product_fournisseur($showunitprice=1, $showsuptitle=1, $maxlen=0, $notooltip=0, $productFournList=array())
Display price of product.
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
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...
fetch_product_fournisseur_price($rowid, $ignore_expression=0)
Loads the price information of a provider.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx, $charges=0, $remise_percent=0, $remise=0, $newnpr=0, $delivery_time_days=0, $supplier_reputation='', $localtaxes_array=array(), $newdefaultvatcode='', $multicurrency_buyprice=0, $multicurrency_price_base_type='HT', $multicurrency_tx=1, $multicurrency_code='', $desc_fourn='')
Modify the purchase price for a supplier.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
Class to manage predefined suppliers products.