dolibarr  13.0.2
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  * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
10  * Copyright (C) 2020 Pierre Ardoin <mapiolca@me.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <https://www.gnu.org/licenses/>.
24  */
25 
32 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
34 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
35 
36 
41 {
45  public $db;
46 
50  public $error = '';
51 
52  public $product_fourn_price_id; // id of ligne product-supplier
53 
57  public $id;
58 
63  public $fourn_ref;
64 
65  public $delivery_time_days;
66  public $ref_supplier; // ref supplier (can be set by get_buyprice)
67  public $desc_supplier;
68  public $vatrate_supplier; // default vat rate for this supplier/qty/product (can be set by get_buyprice)
69 
70  public $product_id;
71  public $product_ref;
72 
73  public $fourn_id; //supplier id
74  public $fourn_qty; // quantity for price (can be set by get_buyprice)
75  public $fourn_pu; // unit price for quantity (can be set by get_buyprice)
76 
77  public $fourn_price; // price for quantity
78  public $fourn_remise_percent; // discount for quantity (percent)
79  public $fourn_remise; // discount for quantity (amount)
80 
81  public $product_fourn_id; // product-supplier id
82  public $product_fourn_entity;
83 
87  public $user_id;
88 
92  public $fk_availability;
93 
94  public $fourn_unitprice;
95  public $fourn_tva_tx;
96  public $fourn_tva_npr;
97 
101  public $fk_supplier_price_expression;
102 
103  public $supplier_reputation; // reputation of supplier
104  public $reputations = array(); // list of available supplier reputations
105 
106  // Multicurreny
107  public $fourn_multicurrency_id;
108  public $fourn_multicurrency_code;
109  public $fourn_multicurrency_tx;
110  public $fourn_multicurrency_price;
111  public $fourn_multicurrency_unitprice;
112 
118 
122  public $supplier_barcode;
123 
129 
133  public $supplier_fk_barcode_type;
134 
135  public $packaging;
136 
137 
143  public function __construct($db)
144  {
145  global $langs;
146 
147  $this->db = $db;
148  $langs->load("suppliers");
149  $this->reputations = array('-1'=>'', 'FAVORITE'=>$langs->trans('Favorite'), 'NOTTHGOOD'=>$langs->trans('NotTheGoodQualitySupplier'), 'DONOTORDER'=>$langs->trans('DoNotOrderThisProductToThisSupplier'));
150  }
151 
152  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
159  public function remove_fournisseur($id_fourn)
160  {
161  // phpcs:enable
162  $ok = 1;
163 
164  $this->db->begin();
165 
166  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
167  $sql .= " WHERE fk_product = ".$this->id." AND fk_soc = ".$id_fourn;
168 
169  dol_syslog(get_class($this)."::remove_fournisseur", LOG_DEBUG);
170  $resql2 = $this->db->query($sql);
171  if (!$resql2)
172  {
173  $this->error = $this->db->lasterror();
174  $ok = 0;
175  }
176 
177  if ($ok)
178  {
179  $this->db->commit();
180  return 1;
181  } else {
182  $this->db->rollback();
183  return -1;
184  }
185  }
186 
187 
188  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
195  public function remove_product_fournisseur_price($rowid)
196  {
197  // phpcs:enable
198  global $conf, $user;
199 
200  $error = 0;
201 
202  $this->db->begin();
203 
204  // Call trigger
205  $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_DELETE', $user);
206  if ($result < 0) $error++;
207  // End call triggers
208 
209  if (empty($error))
210  {
211  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
212  $sql .= " WHERE rowid = ".$rowid;
213 
214  dol_syslog(get_class($this)."::remove_product_fournisseur_price", LOG_DEBUG);
215  $resql = $this->db->query($sql);
216  if (!$resql)
217  {
218  $this->error = $this->db->lasterror();
219  $error++;
220  }
221  }
222 
223  if (empty($error)) {
224  $this->db->commit();
225  return 1;
226  } else {
227  $this->db->rollback();
228  return -1;
229  }
230  }
231 
232 
233  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
262  public 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 = '', $barcode = '', $fk_barcode_type = '')
263  {
264  // phpcs:enable
265  global $conf, $langs;
266  //global $mysoc;
267 
268  // Clean parameter
269  if (empty($qty)) $qty = 0;
270  if (empty($buyprice)) $buyprice = 0;
271  if (empty($charges)) $charges = 0;
272  if (empty($availability)) $availability = 0;
273  if (empty($remise_percent)) $remise_percent = 0;
274  if (empty($supplier_reputation) || $supplier_reputation == -1) $supplier_reputation = '';
275  if ($delivery_time_days != '' && !is_numeric($delivery_time_days)) $delivery_time_days = '';
276  if ($price_base_type == 'TTC')
277  {
278  $ttx = $tva_tx;
279  $buyprice = $buyprice / (1 + ($ttx / 100));
280  }
281 
282  // Multicurrency
283  $multicurrency_unitBuyPrice = null;
284  $fk_multicurrency = null;
285  if (!empty($conf->multicurrency->enabled)) {
286  if (empty($multicurrency_tx)) $multicurrency_tx = 1;
287  if (empty($multicurrency_buyprice)) $multicurrency_buyprice = 0;
288  if ($multicurrency_price_base_type == 'TTC')
289  {
290  $ttx = $tva_tx;
291  $multicurrency_buyprice = $multicurrency_buyprice / (1 + ($ttx / 100));
292  }
293  $multicurrency_buyprice = price2num($multicurrency_buyprice, 'MU');
294  $multicurrency_unitBuyPrice = price2num($multicurrency_buyprice / $qty, 'MU');
295 
296  $buyprice = $multicurrency_buyprice / $multicurrency_tx;
297  $fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $multicurrency_code);
298  }
299 
300  $buyprice = price2num($buyprice, 'MU');
301  $charges = price2num($charges, 'MU');
302  $qty = price2num($qty, 'MS');
303  $unitBuyPrice = price2num($buyprice / $qty, 'MU');
304 
305  $packaging = price2num(((empty($this->packaging) || $this->packaging < $qty) ? $qty : $this->packaging), 'MS');
306 
307  $error = 0;
308  $now = dol_now();
309 
310  $newvat = $tva_tx;
311 
312  if (count($localtaxes_array) > 0)
313  {
314  $localtaxtype1 = $localtaxes_array['0'];
315  $localtax1 = $localtaxes_array['1'];
316  $localtaxtype2 = $localtaxes_array['2'];
317  $localtax2 = $localtaxes_array['3'];
318  } else { // old method. deprecated because ot can't retrieve type
319  $localtaxtype1 = '0';
320  $localtax1 = get_localtax($newvat, 1);
321  $localtaxtype2 = '0';
322  $localtax2 = get_localtax($newvat, 2);
323  }
324  if (empty($localtax1)) $localtax1 = 0; // If = '' then = 0
325  if (empty($localtax2)) $localtax2 = 0; // If = '' then = 0
326 
327  // Check parameters
328  if ($buyprice != '' && !is_numeric($buyprice))
329  {
330  }
331 
332  $this->db->begin();
333 
334  if ($this->product_fourn_price_id > 0)
335  {
336  // check if price already logged, if not first log current price
337  $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id);
338  if (is_array($logPrices) && count($logPrices) == 0)
339  {
340  $currentPfp = new self($this->db);
341  $result = $currentPfp->fetch_product_fournisseur_price($this->product_fourn_price_id);
342  if ($result > 0 && $currentPfp->fourn_price != 0)
343  {
344  $currentPfpUser = new User($this->db);
345  $result = $currentPfpUser->fetch($currentPfp->user_id);
346  if ($result > 0) {
347  $currentPfp->logPrice(
348  $currentPfpUser,
349  $currentPfp->date_creation,
350  $currentPfp->fourn_price,
351  $currentPfp->fourn_qty,
352  $currentPfp->fourn_multicurrency_price,
353  $currentPfp->fourn_multicurrency_unitprice,
354  $currentPfp->fourn_multicurrency_tx,
355  $currentPfp->fourn_multicurrency_id,
356  $currentPfp->fourn_multicurrency_code
357  );
358  }
359  }
360  }
361  $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
362  $sql .= " SET fk_user = ".$user->id." ,";
363  $sql .= " ref_fourn = '".$this->db->escape($ref_fourn)."',";
364  $sql .= " desc_fourn = '".$this->db->escape($desc_fourn)."',";
365  $sql .= " price = ".$buyprice.",";
366  $sql .= " quantity = ".$qty.",";
367  $sql .= " remise_percent = ".$remise_percent.",";
368  $sql .= " remise = ".$remise.",";
369  $sql .= " unitprice = ".$unitBuyPrice.",";
370  $sql .= " fk_availability = ".$availability.",";
371  $sql .= " multicurrency_price = ".(isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
372  $sql .= " multicurrency_unitprice = ".(isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
373  $sql .= " multicurrency_tx = ".(isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
374  $sql .= " fk_multicurrency = ".(isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
375  $sql .= " multicurrency_code = ".(isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
376  $sql .= " entity = ".$conf->entity.",";
377  $sql .= " tva_tx = ".price2num($tva_tx).",";
378  // TODO Add localtax1 and localtax2
379  //$sql.= " localtax1_tx=".($localtax1>=0?$localtax1:'NULL').",";
380  //$sql.= " localtax2_tx=".($localtax2>=0?$localtax2:'NULL').",";
381  //$sql.= " localtax1_type=".($localtaxtype1!=''?"'".$this->db->escape($localtaxtype1)."'":"'0'").",";
382  //$sql.= " localtax2_type=".($localtaxtype2!=''?"'".$this->db->escape($localtaxtype2)."'":"'0'").",";
383  $sql .= " default_vat_code=".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
384  $sql .= " info_bits = ".$newnpr.",";
385  $sql .= " charges = ".$charges.","; // deprecated
386  $sql .= " delivery_time_days = ".($delivery_time_days != '' ? $delivery_time_days : 'null').",";
387  $sql .= " supplier_reputation = ".(empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
388  $sql .= " barcode = ".(empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
389  $sql .= " fk_barcode_type = ".(empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape($fk_barcode_type)."'");
390  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) $sql .= ", packaging = ".(empty($packaging) ? 1 : $packaging);
391  $sql .= " WHERE rowid = ".$this->product_fourn_price_id;
392  // TODO Add price_base_type and price_ttc
393 
394  dol_syslog(get_class($this).'::update_buyprice update knowing id of line = product_fourn_price_id = '.$this->product_fourn_price_id, LOG_DEBUG);
395  $resql = $this->db->query($sql);
396  if ($resql)
397  {
398  // Call trigger
399  $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_UPDATE', $user);
400  if ($result < 0) $error++;
401  // End call triggers
402  if (!$error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG))
403  {
404  $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
405  if ($result < 0) {
406  $error++;
407  }
408  }
409  if (empty($error))
410  {
411  $this->db->commit();
412  return $this->product_fourn_price_id;
413  } else {
414  $this->db->rollback();
415  return -1;
416  }
417  } else {
418  $this->error = $this->db->error()." sql=".$sql;
419  $this->db->rollback();
420  return -2;
421  }
422  } else {
423  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);
424 
425  // Delete price for this quantity
426  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
427  $sql .= " WHERE fk_soc = ".$fourn->id." AND ref_fourn = '".$this->db->escape($ref_fourn)."' AND quantity = ".$qty." AND entity = ".$conf->entity;
428  $resql = $this->db->query($sql);
429  if ($resql) {
430  // Add price for this quantity to supplier
431  $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price(";
432  $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
433  $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, barcode, fk_barcode_type)";
434  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) $sql .= ", packaging";
435  $sql .= " values(";
436  $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
437  $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
438  $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
439  $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
440  $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
441  $sql .= " '".$this->db->idate($now)."',";
442  $sql .= " ".$this->id.",";
443  $sql .= " ".$fourn->id.",";
444  $sql .= " '".$this->db->escape($ref_fourn)."',";
445  $sql .= " '".$this->db->escape($desc_fourn)."',";
446  $sql .= " ".$user->id.",";
447  $sql .= " ".$buyprice.",";
448  $sql .= " ".$qty.",";
449  $sql .= " ".$remise_percent.",";
450  $sql .= " ".$remise.",";
451  $sql .= " ".$unitBuyPrice.",";
452  $sql .= " ".$tva_tx.",";
453  $sql .= " ".$charges.",";
454  $sql .= " ".$availability.",";
455  $sql .= " ".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
456  $sql .= " ".$newnpr.",";
457  $sql .= $conf->entity.",";
458  $sql .= ($delivery_time_days != '' ? $delivery_time_days : 'null').",";
459  $sql .= (empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
460  $sql .= (empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
461  $sql .= (empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape($fk_barcode_type)."'");
462  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) $sql .= ", ".(empty($this->packaging) ? 1 : $this->db->escape($this->packaging));
463  $sql .= ")";
464 
465  $this->product_fourn_price_id = 0;
466 
467  $resql = $this->db->query($sql);
468  if ($resql) {
469  $this->product_fourn_price_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur_price");
470  } else {
471  $error++;
472  }
473 
474  if (!$error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
475  // Add record into log table
476  // $this->product_fourn_price_id must be set
477  $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
478  if ($result < 0) {
479  $error++;
480  }
481  }
482 
483  if (!$error) {
484  // Call trigger
485  $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_CREATE', $user);
486  if ($result < 0)
487  $error++;
488  // End call triggers
489 
490  if (empty($error)) {
491  $this->db->commit();
492  return $this->product_fourn_price_id;
493  } else {
494  $this->db->rollback();
495  return -1;
496  }
497  } else {
498  $this->error = $this->db->lasterror()." sql=".$sql;
499  $this->db->rollback();
500  return -2;
501  }
502  } else {
503  $this->error = $this->db->lasterror()." sql=".$sql;
504  $this->db->rollback();
505  return -1;
506  }
507  }
508  }
509 
510  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
518  public function fetch_product_fournisseur_price($rowid, $ignore_expression = 0)
519  {
520  // phpcs:enable
521  global $conf;
522 
523  $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,";
524  $sql .= " pfp.fk_soc, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_product, pfp.charges, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
525  $sql .= " pfp.supplier_reputation, pfp.fk_user, pfp.datec,";
526  $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
527  $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging,";
528  $sql .= " p.ref as product_ref";
529  $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p";
530  $sql .= " WHERE pfp.rowid = ".(int) $rowid;
531  $sql .= " AND pfp.fk_product = p.rowid";
532 
533  dol_syslog(get_class($this)."::fetch_product_fournisseur_price", LOG_DEBUG);
534  $resql = $this->db->query($sql);
535  if ($resql)
536  {
537  $obj = $this->db->fetch_object($resql);
538  if ($obj)
539  {
540  $this->product_fourn_price_id = $rowid;
541  $this->id = $obj->fk_product;
542 
543  $this->fk_product = $obj->fk_product;
544  $this->product_id = $obj->fk_product;
545  $this->product_ref = $obj->product_ref;
546 
547  $this->fourn_id = $obj->fk_soc;
548  $this->fourn_ref = $obj->ref_fourn; // deprecated
549  $this->ref_supplier = $obj->ref_fourn;
550  $this->desc_supplier = $obj->desc_fourn;
551  $this->fourn_price = $obj->price;
552  $this->fourn_charges = $obj->charges; // deprecated
553  $this->fourn_qty = $obj->quantity;
554  $this->fourn_remise_percent = $obj->remise_percent;
555  $this->fourn_remise = $obj->remise;
556  $this->fourn_unitprice = $obj->unitprice;
557  $this->fourn_tva_tx = $obj->tva_tx;
558  $this->fourn_tva_npr = $obj->fourn_tva_npr;
559  // Add also localtaxes
560  $this->fk_availability = $obj->fk_availability;
561  $this->delivery_time_days = $obj->delivery_time_days;
562  $this->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
563  $this->supplier_reputation = $obj->supplier_reputation;
564  $this->default_vat_code = $obj->default_vat_code;
565  $this->user_id = $obj->fk_user;
566  $this->date_creation = $this->db->jdate($obj->datec);
567  $this->fourn_multicurrency_price = $obj->multicurrency_price;
568  $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
569  $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
570  $this->fourn_multicurrency_id = $obj->fk_multicurrency;
571  $this->fourn_multicurrency_code = $obj->multicurrency_code;
572  if (!empty($conf->barcode->enabled)) {
573  $this->fourn_barcode = $obj->barcode; // deprecated
574  $this->fourn_fk_barcode_type = $obj->fk_barcode_type; // deprecated
575  $this->supplier_barcode = $obj->barcode;
576  $this->supplier_fk_barcode_type = $obj->fk_barcode_type;
577  }
578 
579  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
580  $this->packaging = $obj->packaging;
581  if ($this->packaging < $this->fourn_qty) $this->packaging = $this->fourn_qty;
582  }
583 
584  if (empty($ignore_expression) && !empty($this->fk_supplier_price_expression))
585  {
586  $priceparser = new PriceParser($this->db);
587  $price_result = $priceparser->parseProductSupplier($this);
588  if ($price_result >= 0) {
589  $this->fourn_price = $price_result;
590  //recalculation of unitprice, as probably the price changed...
591  if ($this->fourn_qty != 0)
592  {
593  $this->fourn_unitprice = price2num($this->fourn_price / $this->fourn_qty, 'MU');
594  } else {
595  $this->fourn_unitprice = "";
596  }
597  }
598  }
599 
600  return 1;
601  } else {
602  return 0;
603  }
604  } else {
605  $this->error = $this->db->lasterror();
606  return -1;
607  }
608  }
609 
610 
611  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
622  public function list_product_fournisseur_price($prodid, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0)
623  {
624  // phpcs:enable
625  global $conf;
626 
627  $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id, p.ref as product_ref,";
628  $sql .= " pfp.rowid as product_fourn_pri_id, pfp.entity, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_product as product_fourn_id, pfp.fk_supplier_price_expression,";
629  $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,";
630  $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code, pfp.datec, pfp.tms,";
631  $sql .= " pfp.barcode, pfp.fk_barcode_type";
632  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) $sql .= ", pfp.packaging";
633  $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."societe as s";
634  $sql .= " WHERE pfp.entity IN (".getEntity('productsupplierprice').")";
635  $sql .= " AND pfp.fk_soc = s.rowid AND pfp.fk_product = p.rowid";
636  $sql .= " AND s.status=1"; // only enabled company selected
637  $sql .= " AND pfp.fk_product = ".$prodid;
638  if (empty($sortfield)) $sql .= " ORDER BY s.nom, pfp.quantity, pfp.price";
639  else $sql .= $this->db->order($sortfield, $sortorder);
640  $sql .= $this->db->plimit($limit, $offset);
641  dol_syslog(get_class($this)."::list_product_fournisseur_price", LOG_DEBUG);
642 
643  $resql = $this->db->query($sql);
644  if ($resql)
645  {
646  $retarray = array();
647 
648  while ($record = $this->db->fetch_array($resql))
649  {
650  //define base attribute
651  $prodfourn = new ProductFournisseur($this->db);
652 
653  $prodfourn->product_ref = $record["product_ref"];
654  $prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"];
655  $prodfourn->product_fourn_id = $record["product_fourn_id"];
656  $prodfourn->product_fourn_entity = $record["entity"];
657  $prodfourn->ref_supplier = $record["ref_fourn"];
658  $prodfourn->fourn_ref = $record["ref_fourn"];
659  $prodfourn->desc_supplier = $record["desc_fourn"];
660  $prodfourn->fourn_price = $record["price"];
661  $prodfourn->fourn_qty = $record["quantity"];
662  $prodfourn->fourn_remise_percent = $record["remise_percent"];
663  $prodfourn->fourn_remise = $record["remise"];
664  $prodfourn->fourn_unitprice = $record["unitprice"];
665  $prodfourn->fourn_charges = $record["charges"]; // deprecated
666  $prodfourn->fourn_tva_tx = $record["tva_tx"];
667  $prodfourn->fourn_id = $record["fourn_id"];
668  $prodfourn->fourn_name = $record["supplier_name"];
669  $prodfourn->fk_availability = $record["fk_availability"];
670  $prodfourn->delivery_time_days = $record["delivery_time_days"];
671  $prodfourn->id = $prodid;
672  $prodfourn->fourn_tva_npr = $record["info_bits"];
673  $prodfourn->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
674  $prodfourn->supplier_reputation = $record["supplier_reputation"];
675  $prodfourn->fourn_date_creation = $this->db->jdate($record['datec']);
676  $prodfourn->fourn_date_modification = $this->db->jdate($record['tms']);
677 
678  $prodfourn->fourn_multicurrency_price = $record["multicurrency_price"];
679  $prodfourn->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
680  $prodfourn->fourn_multicurrency_tx = $record["multicurrency_tx"];
681  $prodfourn->fourn_multicurrency_id = $record["fk_multicurrency"];
682  $prodfourn->fourn_multicurrency_code = $record["multicurrency_code"];
683 
684  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
685  $prodfourn->packaging = $record["packaging"];
686  if ($prodfourn->packaging < $prodfourn->fourn_qty) $prodfourn->packaging = $prodfourn->fourn_qty;
687  }
688 
689  if (!empty($conf->barcode->enabled)) {
690  $prodfourn->supplier_barcode = $record["barcode"];
691  $prodfourn->supplier_fk_barcode_type = $record["fk_barcode_type"];
692  }
693 
694  if (!empty($conf->dynamicprices->enabled) && !empty($prodfourn->fk_supplier_price_expression)) {
695  $priceparser = new PriceParser($this->db);
696  $price_result = $priceparser->parseProductSupplier($prodfourn);
697  if ($price_result >= 0) {
698  $prodfourn->fourn_price = $price_result;
699  $prodfourn->fourn_unitprice = null; //force recalculation of unitprice, as probably the price changed...
700  }
701  }
702 
703  if (!isset($prodfourn->fourn_unitprice))
704  {
705  if ($prodfourn->fourn_qty != 0)
706  {
707  $prodfourn->fourn_unitprice = price2num($prodfourn->fourn_price / $prodfourn->fourn_qty, 'MU');
708  } else {
709  $prodfourn->fourn_unitprice = "";
710  }
711  }
712 
713  $retarray[] = $prodfourn;
714  }
715 
716  $this->db->free($resql);
717  return $retarray;
718  } else {
719  $this->error = $this->db->error();
720  return -1;
721  }
722  }
723 
724  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
733  public function find_min_price_product_fournisseur($prodid, $qty = 0, $socid = 0)
734  {
735  // phpcs:enable
736  global $conf;
737 
738  if (empty($prodid))
739  {
740  dol_syslog("Warning function find_min_price_product_fournisseur were called with prodid empty. May be a bug.", LOG_WARNING);
741  return 0;
742  }
743 
744  $this->product_fourn_price_id = '';
745  $this->product_fourn_id = '';
746  $this->fourn_ref = '';
747  $this->fourn_price = '';
748  $this->fourn_qty = '';
749  $this->fourn_remise_percent = '';
750  $this->fourn_remise = '';
751  $this->fourn_unitprice = '';
752  $this->fourn_id = '';
753  $this->fourn_name = '';
754  $this->delivery_time_days = '';
755  $this->id = '';
756 
757  $this->fourn_multicurrency_price = '';
758  $this->fourn_multicurrency_unitprice = '';
759  $this->fourn_multicurrency_tx = '';
760  $this->fourn_multicurrency_id = '';
761  $this->fourn_multicurrency_code = '';
762 
763  $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
764  $sql .= " pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
765  $sql .= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges,";
766  $sql .= " pfp.remise, pfp.remise_percent, pfp.fk_supplier_price_expression, pfp.delivery_time_days";
767  $sql .= " ,pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code";
768  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
769  $sql .= " WHERE s.entity IN (".getEntity('societe').")";
770  $sql .= " AND pfp.entity = ".$conf->entity; // only current entity
771  $sql .= " AND pfp.fk_product = ".$prodid;
772  $sql .= " AND pfp.fk_soc = s.rowid";
773  $sql .= " AND s.status = 1"; // only enabled society
774  if ($qty > 0) $sql .= " AND pfp.quantity <= ".$qty;
775  if ($socid > 0) $sql .= ' AND pfp.fk_soc = '.$socid;
776 
777  dol_syslog(get_class($this)."::find_min_price_product_fournisseur", LOG_DEBUG);
778 
779  $resql = $this->db->query($sql);
780  if ($resql)
781  {
782  $record_array = array();
783 
784  //Store each record to array for later search of min
785  while ($record = $this->db->fetch_array($resql))
786  {
787  $record_array[] = $record;
788  }
789 
790  if (count($record_array) == 0)
791  {
792  $this->db->free($resql);
793  return 0;
794  } else {
795  $min = -1;
796  foreach ($record_array as $record)
797  {
798  $fourn_price = $record["price"];
799  // discount calculated buy price
800  $fourn_unitprice = $record["unitprice"] * (1 - $record["remise_percent"] / 100) - $record["remise"];
801  if (!empty($conf->dynamicprices->enabled) && !empty($record["fk_supplier_price_expression"])) {
802  $prod_supplier = new ProductFournisseur($this->db);
803  $prod_supplier->product_fourn_price_id = $record["product_fourn_price_id"];
804  $prod_supplier->id = $prodid;
805  $prod_supplier->fourn_qty = $record["quantity"];
806  $prod_supplier->fourn_tva_tx = $record["tva_tx"];
807  $prod_supplier->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
808  $priceparser = new PriceParser($this->db);
809  $price_result = $priceparser->parseProductSupplier($prod_supplier);
810  if ($price_result >= 0) {
811  $fourn_price = price2num($price_result, 'MU');
812  if ($record["quantity"] != 0)
813  {
814  $fourn_unitprice = price2num($fourn_price / $record["quantity"], 'MU');
815  } else {
816  $fourn_unitprice = $fourn_price;
817  }
818  }
819  }
820  if ($fourn_unitprice < $min || $min == -1)
821  {
822  $this->product_fourn_price_id = $record["product_fourn_price_id"];
823  $this->ref_supplier = $record["ref_fourn"];
824  $this->ref_fourn = $record["ref_fourn"]; // deprecated
825  $this->fourn_ref = $record["ref_fourn"]; // deprecated
826  $this->fourn_price = $fourn_price;
827  $this->fourn_qty = $record["quantity"];
828  $this->fourn_remise_percent = $record["remise_percent"];
829  $this->fourn_remise = $record["remise"];
830  $this->fourn_unitprice = $record["unitprice"];
831  $this->fourn_charges = $record["charges"]; // deprecated
832  $this->fourn_tva_tx = $record["tva_tx"];
833  $this->fourn_id = $record["fourn_id"];
834  $this->fourn_name = $record["supplier_name"];
835  $this->delivery_time_days = $record["delivery_time_days"];
836  $this->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
837  $this->id = $prodid;
838  $this->fourn_multicurrency_price = $record["multicurrency_price"];
839  $this->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
840  $this->fourn_multicurrency_tx = $record["multicurrency_tx"];
841  $this->fourn_multicurrency_id = $record["fk_multicurrency"];
842  $this->fourn_multicurrency_code = $record["multicurrency_code"];
843  $min = $fourn_unitprice;
844  }
845  }
846  }
847 
848  $this->db->free($resql);
849  return 1;
850  } else {
851  $this->error = $this->db->error();
852  return -1;
853  }
854  }
855 
862  public function setSupplierPriceExpression($expression_id)
863  {
864  global $conf;
865 
866  // Clean parameters
867  $this->db->begin();
868  $expression_id = $expression_id != 0 ? $expression_id : 'NULL';
869 
870  $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
871  $sql .= " SET fk_supplier_price_expression = ".$expression_id;
872  $sql .= " WHERE rowid = ".$this->product_fourn_price_id;
873 
874  dol_syslog(get_class($this)."::setSupplierPriceExpression", LOG_DEBUG);
875 
876  $resql = $this->db->query($sql);
877  if ($resql)
878  {
879  $this->db->commit();
880  return 1;
881  } else {
882  $this->error = $this->db->error()." sql=".$sql;
883  $this->db->rollback();
884  return -1;
885  }
886  }
887 
898  public function getSocNomUrl($withpicto = 0, $option = 'supplier', $maxlen = 0, $notooltip = 0)
899  {
900  $thirdparty = new Fournisseur($this->db);
901  $thirdparty->fetch($this->fourn_id);
902 
903  return $thirdparty->getNomUrl($withpicto, $option, $maxlen, $notooltip);
904  }
905 
906  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
918  public function display_price_product_fournisseur($showunitprice = 1, $showsuptitle = 1, $maxlen = 0, $notooltip = 0, $productFournList = array())
919  {
920  // phpcs:enable
921  global $langs;
922 
923  $out = '';
924  $langs->load("suppliers");
925  if (count($productFournList) > 0) {
926  $out .= '<table class="nobordernopadding" width="100%">';
927  $out .= '<tr><td class="liste_titre right">'.($showunitprice ? $langs->trans("Price").' '.$langs->trans("HT") : '').'</td>';
928  $out .= '<td class="liste_titre right">'.($showunitprice ? $langs->trans("QtyMin") : '').'</td>';
929  $out .= '<td class="liste_titre">'.$langs->trans("Supplier").'</td>';
930  $out .= '<td class="liste_titre">'.$langs->trans("SupplierRef").'</td></tr>';
931  foreach ($productFournList as $productFourn) {
932  $out .= '<tr><td class="right">'.($showunitprice ?price($productFourn->fourn_unitprice * (1 - $productFourn->fourn_remise_percent / 100) - $productFourn->fourn_remise) : '').'</td>';
933  $out .= '<td class="right">'.($showunitprice ? $productFourn->fourn_qty : '').'</td>';
934  $out .= '<td>'.$productFourn->getSocNomUrl(1, 'supplier', $maxlen, $notooltip).'</td>';
935  $out .= '<td>'.$productFourn->fourn_ref.'<td></tr>';
936  }
937  $out .= '</table>';
938  } else {
939  $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 ? ')' : '');
940  }
941  return $out;
942  }
943 
952  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
953  {
954  $tables = array(
955  'product_fournisseur_price'
956  );
957 
958  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
959  }
960 
971  public function listProductFournisseurPriceLog($product_fourn_price_id, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0)
972  {
973  $sql = "SELECT";
974  $sql .= " u.lastname,";
975  $sql .= " pfpl.rowid, pfp.ref_fourn as supplier_ref, pfpl.datec,";
976  $sql .= " pfpl.price, pfpl.quantity,";
977  $sql .= " pfpl.fk_multicurrency, pfpl.multicurrency_code, pfpl.multicurrency_tx, pfpl.multicurrency_price, pfpl.multicurrency_unitprice";
978  $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price_log as pfpl,";
979  $sql .= " ".MAIN_DB_PREFIX."product_fournisseur_price as pfp,";
980  $sql .= " ".MAIN_DB_PREFIX."user as u";
981  $sql .= " WHERE pfp.entity IN (".getEntity('productprice').")";
982  $sql .= " AND pfpl.fk_user = u.rowid";
983  $sql .= " AND pfp.rowid = pfpl.fk_product_fournisseur";
984  $sql .= " AND pfpl.fk_product_fournisseur = ".$product_fourn_price_id;
985  if (empty($sortfield)) $sql .= " ORDER BY pfpl.datec";
986  else $sql .= $this->db->order($sortfield, $sortorder);
987  $sql .= $this->db->plimit($limit, $offset);
988  dol_syslog(get_class($this)."::list_product_fournisseur_price_log", LOG_DEBUG);
989 
990  $resql = $this->db->query($sql);
991  if ($resql)
992  {
993  $retarray = array();
994 
995  while ($obj = $this->db->fetch_object($resql))
996  {
997  $tmparray = array();
998  $tmparray['rowid'] = $obj->rowid;
999  $tmparray['supplier_ref'] = $obj->supplier_ref;
1000  $tmparray['datec'] = $this->db->jdate($obj->datec);
1001  $tmparray['lastname'] = $obj->lastname;
1002  $tmparray['price'] = $obj->price;
1003  $tmparray['quantity'] = $obj->quantity;
1004  $tmparray['fk_multicurrency'] = $obj->fk_multicurrency;
1005  $tmparray['multicurrency_code'] = $obj->multicurrency_code;
1006  $tmparray['multicurrency_tx'] = $obj->multicurrency_tx;
1007  $tmparray['multicurrency_price'] = $obj->multicurrency_price;
1008  $tmparray['multicurrency_unitprice'] = $obj->multicurrency_unitprice;
1009 
1010  $retarray[] = $tmparray;
1011  }
1012 
1013  $this->db->free($resql);
1014  return $retarray;
1015  } else {
1016  $this->error = $this->db->error();
1017  return -1;
1018  }
1019  }
1020 
1028  public function displayPriceProductFournisseurLog($productFournLogList = array())
1029  {
1030  global $conf, $langs;
1031 
1032  $out = '';
1033  $langs->load("suppliers");
1034  if (count($productFournLogList) > 0) {
1035  $out .= '<table class="noborder centpercent">';
1036  $out .= '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Date").'</td>';
1037  $out .= '<td class="liste_titre right">'.$langs->trans("Price").'</td>';
1038  //$out .= '<td class="liste_titre right">'.$langs->trans("QtyMin").'</td>';
1039  $out .= '<td class="liste_titre">'.$langs->trans("User").'</td></tr>';
1040  foreach ($productFournLogList as $productFournLog) {
1041  $out .= '<tr><td class="right">'.dol_print_date($productFournLog['datec'], 'dayhour', 'tzuser').'</td>';
1042  $out .= '<td class="right">'.price($productFournLog['price'], 0, $langs, 1, -1, -1, $conf->currency);
1043  if ($productFournLog['multicurrency_code'] != $conf->currency) {
1044  $out .= ' ('.price($productFournLog['multicurrency_price'], 0, $langs, 1, -1, -1, $productFournLog['multicurrency_code']).')';
1045  }
1046  $out .= '</td>';
1047  //$out.= '<td class="right">'.$productFournLog['quantity'].'</td>';
1048  $out .= '<td>'.$productFournLog['lastname'].'</td></tr>';
1049  }
1050  $out .= '</table>';
1051  }
1052  return $out;
1053  }
1054 
1055 
1067  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
1068  {
1069  global $db, $conf, $langs;
1070 
1071  if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
1072 
1073  $result = '';
1074 
1075  if (!empty($this->entity)) {
1076  $tmpphoto = $this->show_photos('product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 80);
1077  if ($this->nbphoto > 0) {
1078  $label .= '<div class="photointooltip">';
1079  $label .= $tmpphoto;
1080  $label .= '</div><div style="clear: both;"></div>';
1081  }
1082  }
1083 
1084  if ($this->type == Product::TYPE_PRODUCT) {
1085  $label .= img_picto('', 'product').' <u class="paddingrightonly">'.$langs->trans("Product").'</u>';
1086  } elseif ($this->type == Product::TYPE_SERVICE) {
1087  $label .= img_picto('', 'service').' <u class="paddingrightonly">'.$langs->trans("Service").'</u>';
1088  }
1089  if (isset($this->status) && isset($this->status_buy)) {
1090  $label .= ' '.$this->getLibStatut(5, 0);
1091  $label .= ' '.$this->getLibStatut(5, 1);
1092  }
1093 
1094  if (!empty($this->ref)) {
1095  $label .= '<br><b>'.$langs->trans('ProductRef').':</b> '.($this->ref ? $this->ref : $this->product_ref);
1096  }
1097  if (!empty($this->label)) {
1098  $label .= '<br><b>'.$langs->trans('ProductLabel').':</b> '.$this->label;
1099  }
1100  $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
1101 
1102  if ($this->type == Product::TYPE_PRODUCT || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
1103  if (!empty($conf->productbatch->enabled)) {
1104  $langs->load("productbatch");
1105  $label .= "<br><b>".$langs->trans("ManageLotSerial").'</b>: '.$this->getLibStatut(0, 2);
1106  }
1107  }
1108  if (!empty($conf->barcode->enabled)) {
1109  $label .= '<br><b>'.$langs->trans('BarCode').':</b> '.$this->barcode;
1110  }
1111 
1112  if ($this->type == Product::TYPE_PRODUCT)
1113  {
1114  if ($this->weight) {
1115  $label .= "<br><b>".$langs->trans("Weight").'</b>: '.$this->weight.' '.measuringUnitString(0, "weight", $this->weight_units);
1116  }
1117  $labelsize = "";
1118  if ($this->length) {
1119  $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Length").'</b>: '.$this->length.' '.measuringUnitString(0, 'size', $this->length_units);
1120  }
1121  if ($this->width) {
1122  $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Width").'</b>: '.$this->width.' '.measuringUnitString(0, 'size', $this->width_units);
1123  }
1124  if ($this->height) {
1125  $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Height").'</b>: '.$this->height.' '.measuringUnitString(0, 'size', $this->height_units);
1126  }
1127  if ($labelsize) $label .= "<br>".$labelsize;
1128 
1129  $labelsurfacevolume = "";
1130  if ($this->surface) {
1131  $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Surface").'</b>: '.$this->surface.' '.measuringUnitString(0, 'surface', $this->surface_units);
1132  }
1133  if ($this->volume) {
1134  $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Volume").'</b>: '.$this->volume.' '.measuringUnitString(0, 'volume', $this->volume_units);
1135  }
1136  if ($labelsurfacevolume) $label .= "<br>".$labelsurfacevolume;
1137  }
1138 
1139  if (!empty($conf->accounting->enabled) && $this->status) {
1140  include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1141  $label .= '<br><b>'.$langs->trans('ProductAccountancySellCode').':</b> '.length_accountg($this->accountancy_code_sell);
1142  $label .= '<br><b>'.$langs->trans('ProductAccountancySellIntraCode').':</b> '.length_accountg($this->accountancy_code_sell_intra);
1143  $label .= '<br><b>'.$langs->trans('ProductAccountancySellExportCode').':</b> '.length_accountg($this->accountancy_code_sell_export);
1144  }
1145  if (!empty($conf->accounting->enabled) && $this->status_buy) {
1146  include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1147  $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyCode').':</b> '.length_accountg($this->accountancy_code_buy);
1148  $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyIntraCode').':</b> '.length_accountg($this->accountancy_code_buy_intra);
1149  $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyExportCode').':</b> '.length_accountg($this->accountancy_code_buy_export);
1150  }
1151 
1152  $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id, 'pfpl.datec', 'DESC'); // set sort order here
1153  if (is_array($logPrices) && count($logPrices) > 0) {
1154  $label .= '<br><br>';
1155  $label .= '<u>'.$langs->trans("History").'</u>';
1156  $label .= $this->displayPriceProductFournisseurLog($logPrices);
1157  }
1158 
1159  $url = dol_buildpath('/product/fournisseurs.php', 1).'?id='.$this->id.'&action=add_price&socid='.$this->fourn_id.'&rowid='.$this->product_fourn_price_id;
1160 
1161  if ($option != 'nolink')
1162  {
1163  // Add param to save lastsearch_values or not
1164  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1165  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
1166  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
1167  }
1168 
1169  $linkclose = '';
1170  if (empty($notooltip))
1171  {
1172  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
1173  {
1174  $label = $langs->trans("SupplierRef");
1175  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1176  }
1177  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
1178  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
1179  } else $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1180 
1181  $linkstart = '<a href="'.$url.'"';
1182  $linkstart .= $linkclose.'>';
1183  $linkend = '</a>';
1184 
1185  $result .= $linkstart;
1186  if ($withpicto) $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1187  if ($withpicto != 2) $result .= $this->ref.($this->ref_supplier ? ' ('.$this->ref_supplier.')' : '');
1188  $result .= $linkend;
1189  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
1190 
1191  return $result;
1192  }
1193 
1209  private function logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice = null, $multicurrency_unitBuyPrice = null, $multicurrency_tx = null, $fk_multicurrency = null, $multicurrency_code = null)
1210  {
1211  // Add record into log table
1212  $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log(";
1213  $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
1214  $sql .= "datec, fk_product_fournisseur,fk_user,price,quantity)";
1215  $sql .= "values(";
1216  $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
1217  $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
1218  $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
1219  $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
1220  $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
1221  $sql .= "'".$this->db->idate($datec)."',";
1222  $sql .= " ".$this->product_fourn_price_id.",";
1223  $sql .= " ".$user->id.",";
1224  $sql .= " ".price2num($buyprice).",";
1225  $sql .= " ".$qty;
1226  $sql .= ")";
1227 
1228  $resql = $this->db->query($sql);
1229  if (!$resql) {
1230  return -1;
1231  } else {
1232  return 1;
1233  }
1234  }
1235 }
listProductFournisseurPriceLog($product_fourn_price_id, $sortfield= '', $sortorder= '', $limit=0, $offset=0)
List supplier prices log of a supplier price.
Class to parse product price expressions.
Class to manage products or services.
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
Class to manage Dolibarr database access.
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= '', $barcode= '', $fk_barcode_type= '')
Modify the purchase price for a supplier.
const TYPE_SERVICE
Service.
setSupplierPriceExpression($expression_id)
Sets the supplier price expression.
const TYPE_PRODUCT
Regular product.
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.
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage suppliers.
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...
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.
displayPriceProductFournisseurLog($productFournLogList=array())
Display log price of product supplier price.
list_product_fournisseur_price($prodid, $sortfield= '', $sortorder= '', $limit=0, $offset=0)
List all supplier prices of a product.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
getSocNomUrl($withpicto=0, $option= 'supplier', $maxlen=0, $notooltip=0)
Display supplier of product.
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
static getIdFromCode($db, $code)
Get id of currency from code.
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.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
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.
print $_SERVER["PHP_SELF"]
Edit parameters.
getNomUrl($withpicto=0, $option= '', $notooltip=0, $morecss= '', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto).
call_trigger($triggerName, $user)
Call trigger based on this instance.
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
fetch_product_fournisseur_price($rowid, $ignore_expression=0)
Loads the price information of a provider.
show_photos($modulepart, $sdir, $size=0, $nbmax=0, $nbbyrow=5, $showfilename=0, $showaction=0, $maxHeight=120, $maxWidth=160, $nolink=0, $notitle=0, $usesharelink=0)
Show photos of an object (nbmax maximum), into several columns.
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous) ...
logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice=null, $multicurrency_unitBuyPrice=null, $multicurrency_tx=null, $fk_multicurrency=null, $multicurrency_code=null)
Private function to log price history.
getLibStatut($mode=0, $type=0)
Return label of status of object.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105
Class to manage predefined suppliers products.
measuringUnitString($unit, $measuring_style= '', $scale= '', $use_short_label=0, $outputlangs=null)
Return translation label of a unit key.