dolibarr  16.0.5
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-2021 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 require_once DOL_DOCUMENT_ROOT.'/product/class/productfournisseurprice.class.php';
36 
37 
42 {
46  public $db;
47 
51  public $error = '';
52 
53  public $product_fourn_price_id; // id of ligne product-supplier
54 
58  public $id;
59 
64  public $fourn_ref;
65 
66  public $delivery_time_days;
67  public $ref_supplier; // ref supplier (can be set by get_buyprice)
68  public $desc_supplier;
69  public $vatrate_supplier; // default vat rate for this supplier/qty/product (can be set by get_buyprice)
70 
71  public $product_id;
72  public $product_ref;
73 
74  public $fourn_id; //supplier id
75  public $fourn_qty; // quantity for price (can be set by get_buyprice)
76  public $fourn_pu; // unit price for quantity (can be set by get_buyprice)
77 
78  public $fourn_price; // price for quantity
79  public $fourn_remise_percent; // discount for quantity (percent)
80  public $fourn_remise; // discount for quantity (amount)
81 
82  public $product_fourn_id; // product-supplier id
83  public $product_fourn_entity;
84 
88  public $user_id;
89 
93  public $fk_availability;
94 
95  public $fourn_unitprice;
96  public $fourn_unitprice_with_discount; // not saved into database
97  public $fourn_tva_tx;
98  public $fourn_tva_npr;
99 
103  public $fk_supplier_price_expression;
104 
105  public $supplier_reputation; // reputation of supplier
106  public $reputations = array(); // list of available supplier reputations
107 
108  // Multicurreny
109  public $fourn_multicurrency_id;
110  public $fourn_multicurrency_code;
111  public $fourn_multicurrency_tx;
112  public $fourn_multicurrency_price;
113  public $fourn_multicurrency_unitprice;
114 
120 
124  public $supplier_barcode;
125 
131 
135  public $supplier_fk_barcode_type;
136 
137  public $packaging;
138 
139 
145  public function __construct($db)
146  {
147  global $langs;
148 
149  $this->db = $db;
150  $langs->load("suppliers");
151  $this->reputations = array('-1'=>'', 'FAVORITE'=>$langs->trans('Favorite'), 'NOTTHGOOD'=>$langs->trans('NotTheGoodQualitySupplier'), 'DONOTORDER'=>$langs->trans('DoNotOrderThisProductToThisSupplier'));
152  }
153 
154  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
161  public function remove_fournisseur($id_fourn)
162  {
163  // phpcs:enable
164  $ok = 1;
165 
166  $this->db->begin();
167 
168  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
169  $sql .= " WHERE fk_product = ".((int) $this->id)." AND fk_soc = ".((int) $id_fourn);
170 
171  dol_syslog(get_class($this)."::remove_fournisseur", LOG_DEBUG);
172  $resql2 = $this->db->query($sql);
173  if (!$resql2) {
174  $this->error = $this->db->lasterror();
175  $ok = 0;
176  }
177 
178  if ($ok) {
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) {
207  $error++;
208  }
209  // End call triggers
210 
211  if (empty($error)) {
212  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
213  $sql .= " WHERE rowid = ".((int) $rowid);
214 
215  dol_syslog(get_class($this)."::remove_product_fournisseur_price", LOG_DEBUG);
216  $resql = $this->db->query($sql);
217  if (!$resql) {
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
263  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 = '', $options = array())
264  {
265  // phpcs:enable
266  global $conf, $langs;
267  //global $mysoc;
268 
269  // Clean parameter
270  if (empty($qty)) {
271  $qty = 0;
272  }
273  if (empty($buyprice)) {
274  $buyprice = 0;
275  }
276  if (empty($charges)) {
277  $charges = 0;
278  }
279  if (empty($availability)) {
280  $availability = 0;
281  }
282  if (empty($remise_percent)) {
283  $remise_percent = 0;
284  }
285  if (empty($supplier_reputation) || $supplier_reputation == -1) {
286  $supplier_reputation = '';
287  }
288  if ($delivery_time_days != '' && !is_numeric($delivery_time_days)) {
289  $delivery_time_days = '';
290  }
291  if ($price_base_type == 'TTC') {
292  $ttx = $tva_tx;
293  $buyprice = $buyprice / (1 + ($ttx / 100));
294  }
295 
296  // Multicurrency
297  $multicurrency_unitBuyPrice = null;
298  $fk_multicurrency = null;
299  if (!empty($conf->multicurrency->enabled)) {
300  if (empty($multicurrency_tx)) {
301  $multicurrency_tx = 1;
302  }
303  if (empty($multicurrency_buyprice)) {
304  $multicurrency_buyprice = 0;
305  }
306  if ($multicurrency_price_base_type == 'TTC') {
307  $ttx = $tva_tx;
308  $multicurrency_buyprice = $multicurrency_buyprice / (1 + ($ttx / 100));
309  }
310  $multicurrency_buyprice = price2num($multicurrency_buyprice, 'MU');
311  $multicurrency_unitBuyPrice = price2num($multicurrency_buyprice / $qty, 'MU');
312 
313  $buyprice = $multicurrency_buyprice / $multicurrency_tx;
314  $fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $multicurrency_code);
315  }
316 
317  $buyprice = price2num($buyprice, 'MU');
318  $charges = price2num($charges, 'MU');
319  $qty = price2num($qty, 'MS');
320  $unitBuyPrice = price2num($buyprice / $qty, 'MU');
321 
322  // We can have a puchase ref that need to buy 100 min for a given price and with a packaging of 50.
323  //$packaging = price2num(((empty($this->packaging) || $this->packaging < $qty) ? $qty : $this->packaging), 'MS');
324  $packaging = price2num((empty($this->packaging) ? $qty : $this->packaging), 'MS');
325 
326  $error = 0;
327  $now = dol_now();
328 
329  $newvat = $tva_tx;
330 
331  if (count($localtaxes_array) > 0) {
332  $localtaxtype1 = $localtaxes_array['0'];
333  $localtax1 = $localtaxes_array['1'];
334  $localtaxtype2 = $localtaxes_array['2'];
335  $localtax2 = $localtaxes_array['3'];
336  } else { // old method. deprecated because ot can't retrieve type
337  $localtaxtype1 = '0';
338  $localtax1 = get_localtax($newvat, 1);
339  $localtaxtype2 = '0';
340  $localtax2 = get_localtax($newvat, 2);
341  }
342  if (empty($localtax1)) {
343  $localtax1 = 0; // If = '' then = 0
344  }
345  if (empty($localtax2)) {
346  $localtax2 = 0; // If = '' then = 0
347  }
348 
349  // Check parameters
350  if ($buyprice != '' && !is_numeric($buyprice)) {
351  }
352 
353  $this->db->begin();
354 
355  if ($this->product_fourn_price_id > 0) {
356  // check if price already logged, if not first log current price
357  $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id);
358  if (is_array($logPrices) && count($logPrices) == 0) {
359  $currentPfp = new self($this->db);
360  $result = $currentPfp->fetch_product_fournisseur_price($this->product_fourn_price_id);
361  if ($result > 0 && $currentPfp->fourn_price != 0) {
362  $currentPfpUser = new User($this->db);
363  $result = $currentPfpUser->fetch($currentPfp->user_id);
364  if ($result > 0) {
365  $currentPfp->logPrice(
366  $currentPfpUser,
367  $currentPfp->date_creation,
368  $currentPfp->fourn_price,
369  $currentPfp->fourn_qty,
370  $currentPfp->fourn_multicurrency_price,
371  $currentPfp->fourn_multicurrency_unitprice,
372  $currentPfp->fourn_multicurrency_tx,
373  $currentPfp->fourn_multicurrency_id,
374  $currentPfp->fourn_multicurrency_code
375  );
376  }
377  }
378  }
379  $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
380  $sql .= " SET fk_user = ".((int) $user->id)." ,";
381  $sql .= " ref_fourn = '".$this->db->escape($ref_fourn)."',";
382  $sql .= " desc_fourn = '".$this->db->escape($desc_fourn)."',";
383  $sql .= " price = ".((float) $buyprice).",";
384  $sql .= " quantity = ".((float) $qty).",";
385  $sql .= " remise_percent = ".((float) $remise_percent).",";
386  $sql .= " remise = ".((float) $remise).",";
387  $sql .= " unitprice = ".((float) $unitBuyPrice).",";
388  $sql .= " fk_availability = ".((int) $availability).",";
389  $sql .= " multicurrency_price = ".(isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
390  $sql .= " multicurrency_unitprice = ".(isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
391  $sql .= " multicurrency_tx = ".(isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
392  $sql .= " fk_multicurrency = ".(isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
393  $sql .= " multicurrency_code = ".(isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
394  $sql .= " entity = ".$conf->entity.",";
395  $sql .= " tva_tx = ".price2num($tva_tx).",";
396  // TODO Add localtax1 and localtax2
397  //$sql.= " localtax1_tx=".($localtax1>=0?$localtax1:'NULL').",";
398  //$sql.= " localtax2_tx=".($localtax2>=0?$localtax2:'NULL').",";
399  //$sql.= " localtax1_type=".($localtaxtype1!=''?"'".$this->db->escape($localtaxtype1)."'":"'0'").",";
400  //$sql.= " localtax2_type=".($localtaxtype2!=''?"'".$this->db->escape($localtaxtype2)."'":"'0'").",";
401  $sql .= " default_vat_code=".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
402  $sql .= " info_bits = ".((int) $newnpr).",";
403  $sql .= " charges = ".((float) $charges).","; // deprecated
404  $sql .= " delivery_time_days = ".($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
405  $sql .= " supplier_reputation = ".(empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
406  $sql .= " barcode = ".(empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
407  $sql .= " fk_barcode_type = ".(empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape($fk_barcode_type)."'");
408  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
409  $sql .= ", packaging = ".(empty($packaging) ? 1 : $packaging);
410  }
411  $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
412 
413  if (!$error) {
414  if (!empty($options) && is_array($options)) {
415  $productfournisseurprice = new ProductFournisseurPrice($this->db);
416  $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
417  if ($res > 0) {
418  foreach ($options as $key=>$value) {
419  $productfournisseurprice->array_options[$key] = $value;
420  }
421  $res = $productfournisseurprice->update($user);
422  if ($res < 0) {
423  $this->error = $productfournisseurprice->error;
424  $this->errors = $productfournisseurprice->errors;
425  $error++;
426  }
427  }
428  }
429  }
430 
431  // TODO Add price_base_type and price_ttc
432 
433  dol_syslog(get_class($this).'::update_buyprice update knowing id of line = product_fourn_price_id = '.$this->product_fourn_price_id, LOG_DEBUG);
434  $resql = $this->db->query($sql);
435  if ($resql) {
436  // Call trigger
437  $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_MODIFY', $user);
438  if ($result < 0) {
439  $error++;
440  }
441  // End call triggers
442  if (!$error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
443  $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
444  if ($result < 0) {
445  $error++;
446  }
447  }
448  if (empty($error)) {
449  $this->db->commit();
450  return $this->product_fourn_price_id;
451  } else {
452  $this->db->rollback();
453  return -1;
454  }
455  } else {
456  $this->error = $this->db->error()." sql=".$sql;
457  $this->db->rollback();
458  return -2;
459  }
460  } else {
461  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);
462 
463  // Delete price for this quantity
464  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
465  $sql .= " WHERE fk_soc = ".((int) $fourn->id)." AND ref_fourn = '".$this->db->escape($ref_fourn)."' AND quantity = ".((float) $qty)." AND entity = ".((int) $conf->entity);
466  $resql = $this->db->query($sql);
467  if ($resql) {
468  // Add price for this quantity to supplier
469  $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price(";
470  $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
471  $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";
472  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
473  $sql .= ", packaging";
474  }
475  $sql .= ") values(";
476  $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
477  $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
478  $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
479  $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
480  $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
481  $sql .= " '".$this->db->idate($now)."',";
482  $sql .= " ".((int) $this->id).",";
483  $sql .= " ".((int) $fourn->id).",";
484  $sql .= " '".$this->db->escape($ref_fourn)."',";
485  $sql .= " '".$this->db->escape($desc_fourn)."',";
486  $sql .= " ".((int) $user->id).",";
487  $sql .= " ".price2num($buyprice).",";
488  $sql .= " ".((float) $qty).",";
489  $sql .= " ".((float) $remise_percent).",";
490  $sql .= " ".((float) $remise).",";
491  $sql .= " ".price2num($unitBuyPrice).",";
492  $sql .= " ".price2num($tva_tx).",";
493  $sql .= " ".price2num($charges).",";
494  $sql .= " ".((int) $availability).",";
495  $sql .= " ".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
496  $sql .= " ".((int) $newnpr).",";
497  $sql .= $conf->entity.",";
498  $sql .= ($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
499  $sql .= (empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
500  $sql .= (empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
501  $sql .= (empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape($fk_barcode_type)."'");
502  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
503  $sql .= ", ".(empty($this->packaging) ? '1' : "'".$this->db->escape($this->packaging)."'");
504  }
505  $sql .= ")";
506 
507  $this->product_fourn_price_id = 0;
508 
509  $resql = $this->db->query($sql);
510  if ($resql) {
511  $this->product_fourn_price_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur_price");
512  } else {
513  $this->error = $this->db->lasterror();
514  $error++;
515  }
516 
517  if (!$error) {
518  if (!empty($options) && is_array($options)) {
519  $productfournisseurprice = new ProductFournisseurPrice($this->db);
520  $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
521  if ($res > 0) {
522  foreach ($options as $key=>$value) {
523  $productfournisseurprice->array_options[$key] = $value;
524  }
525  $res = $productfournisseurprice->update($user);
526  if ($res < 0) {
527  $this->error = $productfournisseurprice->error;
528  $this->errors = $productfournisseurprice->errors;
529  $error++;
530  }
531  }
532  }
533  }
534 
535  if (!$error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
536  // Add record into log table
537  // $this->product_fourn_price_id must be set
538  $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
539  if ($result < 0) {
540  $error++;
541  }
542  }
543 
544  if (!$error) {
545  // Call trigger
546  $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_CREATE', $user);
547  if ($result < 0) {
548  $error++;
549  }
550  // End call triggers
551 
552  if (empty($error)) {
553  $this->db->commit();
554  return $this->product_fourn_price_id;
555  } else {
556  $this->db->rollback();
557  return -1;
558  }
559  } else {
560  $this->error = $this->db->lasterror()." sql=".$sql;
561  $this->db->rollback();
562  return -2;
563  }
564  } else {
565  $this->error = $this->db->lasterror()." sql=".$sql;
566  $this->db->rollback();
567  return -1;
568  }
569  }
570  }
571 
572  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
580  public function fetch_product_fournisseur_price($rowid, $ignore_expression = 0)
581  {
582  // phpcs:enable
583  global $conf;
584 
585  $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,";
586  $sql .= " pfp.fk_soc, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_product, pfp.charges, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
587  $sql .= " pfp.supplier_reputation, pfp.fk_user, pfp.datec,";
588  $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
589  $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging,";
590  $sql .= " p.ref as product_ref, p.tosell as status, p.tobuy as status_buy";
591  $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p";
592  $sql .= " WHERE pfp.rowid = ".(int) $rowid;
593  $sql .= " AND pfp.fk_product = p.rowid";
594 
595  dol_syslog(get_class($this)."::fetch_product_fournisseur_price", LOG_DEBUG);
596  $resql = $this->db->query($sql);
597  if ($resql) {
598  $obj = $this->db->fetch_object($resql);
599  if ($obj) {
600  $this->product_fourn_price_id = $rowid;
601  $this->id = $obj->fk_product;
602 
603  $this->fk_product = $obj->fk_product;
604  $this->product_id = $obj->fk_product;
605  $this->product_ref = $obj->product_ref;
606  $this->status = $obj->status;
607  $this->status_buy = $obj->status_buy;
608  $this->fourn_id = $obj->fk_soc;
609  $this->fourn_ref = $obj->ref_fourn; // deprecated
610  $this->ref_supplier = $obj->ref_fourn;
611  $this->desc_supplier = $obj->desc_fourn;
612  $this->fourn_price = $obj->price;
613  $this->fourn_charges = $obj->charges; // deprecated
614  $this->fourn_qty = $obj->quantity;
615  $this->fourn_remise_percent = $obj->remise_percent;
616  $this->fourn_remise = $obj->remise;
617  $this->fourn_unitprice = $obj->unitprice;
618  $this->fourn_tva_tx = $obj->tva_tx;
619  $this->fourn_tva_npr = $obj->fourn_tva_npr;
620  // Add also localtaxes
621  $this->fk_availability = $obj->fk_availability;
622  $this->delivery_time_days = $obj->delivery_time_days;
623  $this->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
624  $this->supplier_reputation = $obj->supplier_reputation;
625  $this->default_vat_code = $obj->default_vat_code;
626  $this->user_id = $obj->fk_user;
627  $this->date_creation = $this->db->jdate($obj->datec);
628  $this->fourn_multicurrency_price = $obj->multicurrency_price;
629  $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
630  $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
631  $this->fourn_multicurrency_id = $obj->fk_multicurrency;
632  $this->fourn_multicurrency_code = $obj->multicurrency_code;
633  if (!empty($conf->barcode->enabled)) {
634  $this->fourn_barcode = $obj->barcode; // deprecated
635  $this->fourn_fk_barcode_type = $obj->fk_barcode_type; // deprecated
636  $this->supplier_barcode = $obj->barcode;
637  $this->supplier_fk_barcode_type = $obj->fk_barcode_type;
638  }
639  $this->packaging = $obj->packaging;
640 
641  if (empty($ignore_expression) && !empty($this->fk_supplier_price_expression)) {
642  $priceparser = new PriceParser($this->db);
643  $price_result = $priceparser->parseProductSupplier($this);
644  if ($price_result >= 0) {
645  $this->fourn_price = $price_result;
646  //recalculation of unitprice, as probably the price changed...
647  if ($this->fourn_qty != 0) {
648  $this->fourn_unitprice = price2num($this->fourn_price / $this->fourn_qty, 'MU');
649  } else {
650  $this->fourn_unitprice = "";
651  }
652  }
653  }
654 
655  return 1;
656  } else {
657  return 0;
658  }
659  } else {
660  $this->error = $this->db->lasterror();
661  return -1;
662  }
663  }
664 
665 
666  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
678  public function list_product_fournisseur_price($prodid, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0, $socid = 0)
679  {
680  // phpcs:enable
681  global $conf;
682 
683  $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id, p.ref as product_ref, p.tosell as status, p.tobuy as status_buy, ";
684  $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,";
685  $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,";
686  $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code, pfp.datec, pfp.tms,";
687  $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging";
688  $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."societe as s";
689  $sql .= " WHERE pfp.entity IN (".getEntity('productsupplierprice').")";
690  $sql .= " AND pfp.fk_soc = s.rowid AND pfp.fk_product = p.rowid";
691  $sql .= ($socid > 0 ? ' AND pfp.fk_soc = '.((int) $socid) : '');
692  $sql .= " AND s.status = 1"; // only enabled company selected
693  $sql .= " AND pfp.fk_product = ".((int) $prodid);
694  if (empty($sortfield)) {
695  $sql .= " ORDER BY s.nom, pfp.quantity, pfp.price";
696  } else {
697  $sql .= $this->db->order($sortfield, $sortorder);
698  }
699  $sql .= $this->db->plimit($limit, $offset);
700  dol_syslog(get_class($this)."::list_product_fournisseur_price", LOG_DEBUG);
701 
702  $resql = $this->db->query($sql);
703  if ($resql) {
704  $retarray = array();
705 
706  while ($record = $this->db->fetch_array($resql)) {
707  //define base attribute
708  $prodfourn = new ProductFournisseur($this->db);
709 
710  $prodfourn->product_ref = $record["product_ref"];
711  $prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"];
712  $prodfourn->status = $record["status"];
713  $prodfourn->status_buy = $record["status_buy"];
714  $prodfourn->product_fourn_id = $record["product_fourn_id"];
715  $prodfourn->product_fourn_entity = $record["entity"];
716  $prodfourn->ref_supplier = $record["ref_fourn"];
717  $prodfourn->fourn_ref = $record["ref_fourn"];
718  $prodfourn->desc_supplier = $record["desc_fourn"];
719  $prodfourn->fourn_price = $record["price"];
720  $prodfourn->fourn_qty = $record["quantity"];
721  $prodfourn->fourn_remise_percent = $record["remise_percent"];
722  $prodfourn->fourn_remise = $record["remise"];
723  $prodfourn->fourn_unitprice = $record["unitprice"];
724  $prodfourn->fourn_charges = $record["charges"]; // deprecated
725  $prodfourn->fourn_tva_tx = $record["tva_tx"];
726  $prodfourn->fourn_id = $record["fourn_id"];
727  $prodfourn->fourn_name = $record["supplier_name"];
728  $prodfourn->fk_availability = $record["fk_availability"];
729  $prodfourn->delivery_time_days = $record["delivery_time_days"];
730  $prodfourn->id = $prodid;
731  $prodfourn->fourn_tva_npr = $record["info_bits"];
732  $prodfourn->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
733  $prodfourn->supplier_reputation = $record["supplier_reputation"];
734  $prodfourn->fourn_date_creation = $this->db->jdate($record['datec']);
735  $prodfourn->fourn_date_modification = $this->db->jdate($record['tms']);
736 
737  $prodfourn->fourn_multicurrency_price = $record["multicurrency_price"];
738  $prodfourn->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
739  $prodfourn->fourn_multicurrency_tx = $record["multicurrency_tx"];
740  $prodfourn->fourn_multicurrency_id = $record["fk_multicurrency"];
741  $prodfourn->fourn_multicurrency_code = $record["multicurrency_code"];
742 
743  $prodfourn->packaging = $record["packaging"];
744 
745  if (!empty($conf->barcode->enabled)) {
746  $prodfourn->supplier_barcode = $record["barcode"];
747  $prodfourn->supplier_fk_barcode_type = $record["fk_barcode_type"];
748  }
749 
750  if (!empty($conf->dynamicprices->enabled) && !empty($prodfourn->fk_supplier_price_expression)) {
751  $priceparser = new PriceParser($this->db);
752  $price_result = $priceparser->parseProductSupplier($prodfourn);
753  if ($price_result >= 0) {
754  $prodfourn->fourn_price = $price_result;
755  $prodfourn->fourn_unitprice = null; //force recalculation of unitprice, as probably the price changed...
756  }
757  }
758 
759  if (!isset($prodfourn->fourn_unitprice)) {
760  if ($prodfourn->fourn_qty != 0) {
761  $prodfourn->fourn_unitprice = price2num($prodfourn->fourn_price / $prodfourn->fourn_qty, 'MU');
762  } else {
763  $prodfourn->fourn_unitprice = "";
764  }
765  }
766 
767  $retarray[] = $prodfourn;
768  }
769 
770  $this->db->free($resql);
771  return $retarray;
772  } else {
773  $this->error = $this->db->error();
774  return -1;
775  }
776  }
777 
778  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
787  public function find_min_price_product_fournisseur($prodid, $qty = 0, $socid = 0)
788  {
789  // phpcs:enable
790  global $conf;
791 
792  if (empty($prodid)) {
793  dol_syslog("Warning function find_min_price_product_fournisseur were called with prodid empty. May be a bug.", LOG_WARNING);
794  return 0;
795  }
796 
797  $this->product_fourn_price_id = '';
798  $this->product_fourn_id = '';
799  $this->fourn_ref = '';
800  $this->fourn_price = '';
801  $this->fourn_qty = '';
802  $this->fourn_remise_percent = '';
803  $this->fourn_remise = '';
804  $this->fourn_unitprice = '';
805  $this->fourn_id = '';
806  $this->fourn_name = '';
807  $this->delivery_time_days = '';
808  $this->id = '';
809 
810  $this->fourn_multicurrency_price = '';
811  $this->fourn_multicurrency_unitprice = '';
812  $this->fourn_multicurrency_tx = '';
813  $this->fourn_multicurrency_id = '';
814  $this->fourn_multicurrency_code = '';
815 
816  $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
817  $sql .= " pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
818  $sql .= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges,";
819  $sql .= " pfp.remise, pfp.remise_percent, pfp.fk_supplier_price_expression, pfp.delivery_time_days";
820  $sql .= " ,pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code";
821  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
822  $sql .= " WHERE s.entity IN (".getEntity('societe').")";
823  $sql .= " AND pfp.entity IN (".getEntity('productsupplierprice').")";
824  $sql .= " AND pfp.fk_product = ".((int) $prodid);
825  $sql .= " AND pfp.fk_soc = s.rowid";
826  $sql .= " AND s.status = 1"; // only enabled society
827  if ($qty > 0) {
828  $sql .= " AND pfp.quantity <= ".((float) $qty);
829  }
830  if ($socid > 0) {
831  $sql .= ' AND pfp.fk_soc = '.((int) $socid);
832  }
833 
834  dol_syslog(get_class($this)."::find_min_price_product_fournisseur", LOG_DEBUG);
835 
836  $resql = $this->db->query($sql);
837  if ($resql) {
838  $record_array = array();
839 
840  //Store each record to array for later search of min
841  while ($record = $this->db->fetch_array($resql)) {
842  $record_array[] = $record;
843  }
844 
845  if (count($record_array) == 0) {
846  $this->db->free($resql);
847  return 0;
848  } else {
849  $min = -1;
850  foreach ($record_array as $record) {
851  $fourn_price = $record["price"];
852  // calculate unit price for quantity 1
853  $fourn_unitprice = $record["unitprice"];
854  $fourn_unitprice_with_discount = $record["unitprice"] * (1 - $record["remise_percent"] / 100);
855 
856  if (!empty($conf->dynamicprices->enabled) && !empty($record["fk_supplier_price_expression"])) {
857  $prod_supplier = new ProductFournisseur($this->db);
858  $prod_supplier->product_fourn_price_id = $record["product_fourn_price_id"];
859  $prod_supplier->id = $prodid;
860  $prod_supplier->fourn_qty = $record["quantity"];
861  $prod_supplier->fourn_tva_tx = $record["tva_tx"];
862  $prod_supplier->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
863  $priceparser = new PriceParser($this->db);
864  $price_result = $priceparser->parseProductSupplier($prod_supplier);
865  if ($price_result >= 0) {
866  $fourn_price = price2num($price_result, 'MU');
867  if ($record["quantity"] != 0) {
868  $fourn_unitprice = price2num($fourn_price / $record["quantity"], 'MU');
869  } else {
870  $fourn_unitprice = $fourn_price;
871  }
872  $fourn_unitprice_with_discount = $fourn_unitprice * (1 - $record["remise_percent"] / 100);
873  }
874  }
875  if ($fourn_unitprice < $min || $min == -1) {
876  $this->product_fourn_price_id = $record["product_fourn_price_id"];
877  $this->ref_supplier = $record["ref_fourn"];
878  $this->ref_fourn = $record["ref_fourn"]; // deprecated
879  $this->fourn_ref = $record["ref_fourn"]; // deprecated
880  $this->fourn_price = $fourn_price;
881  $this->fourn_qty = $record["quantity"];
882  $this->fourn_remise_percent = $record["remise_percent"];
883  $this->fourn_remise = $record["remise"];
884  $this->fourn_unitprice = $fourn_unitprice;
885  $this->fourn_unitprice_with_discount = $fourn_unitprice_with_discount;
886  $this->fourn_charges = $record["charges"]; // deprecated
887  $this->fourn_tva_tx = $record["tva_tx"];
888  $this->fourn_id = $record["fourn_id"];
889  $this->fourn_name = $record["supplier_name"];
890  $this->delivery_time_days = $record["delivery_time_days"];
891  $this->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
892  $this->id = $prodid;
893  $this->fourn_multicurrency_price = $record["multicurrency_price"];
894  $this->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
895  $this->fourn_multicurrency_tx = $record["multicurrency_tx"];
896  $this->fourn_multicurrency_id = $record["fk_multicurrency"];
897  $this->fourn_multicurrency_code = $record["multicurrency_code"];
898  $min = $fourn_unitprice;
899  }
900  }
901  }
902 
903  $this->db->free($resql);
904  return 1;
905  } else {
906  $this->error = $this->db->error();
907  return -1;
908  }
909  }
910 
917  public function setSupplierPriceExpression($expression_id)
918  {
919  global $conf;
920 
921  // Clean parameters
922  $this->db->begin();
923  $expression_id = $expression_id != 0 ? $expression_id : 'NULL';
924 
925  $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
926  $sql .= " SET fk_supplier_price_expression = ".((int) $expression_id);
927  $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
928 
929  dol_syslog(get_class($this)."::setSupplierPriceExpression", LOG_DEBUG);
930 
931  $resql = $this->db->query($sql);
932  if ($resql) {
933  $this->db->commit();
934  return 1;
935  } else {
936  $this->error = $this->db->error()." sql=".$sql;
937  $this->db->rollback();
938  return -1;
939  }
940  }
941 
952  public function getSocNomUrl($withpicto = 0, $option = 'supplier', $maxlen = 0, $notooltip = 0)
953  {
954  $thirdparty = new Fournisseur($this->db);
955  $thirdparty->fetch($this->fourn_id);
956 
957  return $thirdparty->getNomUrl($withpicto, $option, $maxlen, $notooltip);
958  }
959 
960  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
972  public function display_price_product_fournisseur($showunitprice = 1, $showsuptitle = 1, $maxlen = 0, $notooltip = 0, $productFournList = array())
973  {
974  // phpcs:enable
975  global $conf, $langs;
976 
977  $out = '';
978  $langs->load("suppliers");
979  if (count($productFournList) > 0) {
980  $out .= '<table class="nobordernopadding" width="100%">';
981  $out .= '<tr><td class="liste_titre right">'.($showunitprice ? $langs->trans("Price").' '.$langs->trans("HT") : '').'</td>';
982  $out .= '<td class="liste_titre right">'.($showunitprice ? $langs->trans("QtyMin") : '').'</td>';
983  $out .= '<td class="liste_titre">'.$langs->trans("Supplier").'</td>';
984  $out .= '<td class="liste_titre">'.$langs->trans("SupplierRef").'</td></tr>';
985  foreach ($productFournList as $productFourn) {
986  $out .= '<tr><td class="right">'.($showunitprice ?price($productFourn->fourn_unitprice * (1 - $productFourn->fourn_remise_percent / 100) - $productFourn->fourn_remise) : '').'</td>';
987  $out .= '<td class="right">'.($showunitprice ? $productFourn->fourn_qty : '').'</td>';
988  $out .= '<td>'.$productFourn->getSocNomUrl(1, 'supplier', $maxlen, $notooltip).'</td>';
989  $out .= '<td>'.$productFourn->fourn_ref.'<td></tr>';
990  }
991  $out .= '</table>';
992  } else {
993  $out = ($showunitprice ? price($this->fourn_unitprice * (1 - $this->fourn_remise_percent / 100) + $this->fourn_remise, 0, $langs, 1, -1, -1, $conf->currency).' '.$langs->trans("HT").' &nbsp; <span class="opacitymedium">(</span>' : '');
994  $out .= ($showsuptitle ? '<span class="opacitymedium">'.$langs->trans("Supplier").'</span>: ' : '').$this->getSocNomUrl(1, 'supplier', $maxlen, $notooltip).' / <span class="opacitymedium">'.$langs->trans("SupplierRef").'</span>: '.$this->ref_supplier;
995  $out .= ($showunitprice ? '<span class="opacitymedium">)</span>' : '');
996  }
997  return $out;
998  }
999 
1008  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
1009  {
1010  $tables = array(
1011  'product_fournisseur_price'
1012  );
1013 
1014  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
1015  }
1016 
1025  public static function replaceProduct(DoliDB $db, $origin_id, $dest_id)
1026  {
1027  $tables = array(
1028  'product_fournisseur_price'
1029  );
1030 
1031  return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables);
1032  }
1033 
1044  public function listProductFournisseurPriceLog($product_fourn_price_id, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0)
1045  {
1046  $sql = "SELECT";
1047  $sql .= " u.lastname,";
1048  $sql .= " pfpl.rowid, pfp.ref_fourn as supplier_ref, pfpl.datec,";
1049  $sql .= " pfpl.price, pfpl.quantity,";
1050  $sql .= " pfpl.fk_multicurrency, pfpl.multicurrency_code, pfpl.multicurrency_tx, pfpl.multicurrency_price, pfpl.multicurrency_unitprice";
1051  $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price_log as pfpl,";
1052  $sql .= " ".MAIN_DB_PREFIX."product_fournisseur_price as pfp,";
1053  $sql .= " ".MAIN_DB_PREFIX."user as u";
1054  $sql .= " WHERE pfp.entity IN (".getEntity('productprice').")";
1055  $sql .= " AND pfpl.fk_user = u.rowid";
1056  $sql .= " AND pfp.rowid = pfpl.fk_product_fournisseur";
1057  $sql .= " AND pfpl.fk_product_fournisseur = ".((int) $product_fourn_price_id);
1058  if (empty($sortfield)) {
1059  $sql .= " ORDER BY pfpl.datec";
1060  } else {
1061  $sql .= $this->db->order($sortfield, $sortorder);
1062  }
1063  $sql .= $this->db->plimit($limit, $offset);
1064  dol_syslog(get_class($this)."::list_product_fournisseur_price_log", LOG_DEBUG);
1065 
1066  $resql = $this->db->query($sql);
1067  if ($resql) {
1068  $retarray = array();
1069 
1070  while ($obj = $this->db->fetch_object($resql)) {
1071  $tmparray = array();
1072  $tmparray['rowid'] = $obj->rowid;
1073  $tmparray['supplier_ref'] = $obj->supplier_ref;
1074  $tmparray['datec'] = $this->db->jdate($obj->datec);
1075  $tmparray['lastname'] = $obj->lastname;
1076  $tmparray['price'] = $obj->price;
1077  $tmparray['quantity'] = $obj->quantity;
1078  $tmparray['fk_multicurrency'] = $obj->fk_multicurrency;
1079  $tmparray['multicurrency_code'] = $obj->multicurrency_code;
1080  $tmparray['multicurrency_tx'] = $obj->multicurrency_tx;
1081  $tmparray['multicurrency_price'] = $obj->multicurrency_price;
1082  $tmparray['multicurrency_unitprice'] = $obj->multicurrency_unitprice;
1083 
1084  $retarray[] = $tmparray;
1085  }
1086 
1087  $this->db->free($resql);
1088  return $retarray;
1089  } else {
1090  $this->error = $this->db->error();
1091  return -1;
1092  }
1093  }
1094 
1102  public function displayPriceProductFournisseurLog($productFournLogList = array())
1103  {
1104  global $conf, $langs;
1105 
1106  $out = '';
1107  $langs->load("suppliers");
1108  if (count($productFournLogList) > 0) {
1109  $out .= '<table class="noborder centpercent">';
1110  $out .= '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Date").'</td>';
1111  $out .= '<td class="liste_titre right">'.$langs->trans("Price").'</td>';
1112  //$out .= '<td class="liste_titre right">'.$langs->trans("QtyMin").'</td>';
1113  $out .= '<td class="liste_titre">'.$langs->trans("User").'</td></tr>';
1114  foreach ($productFournLogList as $productFournLog) {
1115  $out .= '<tr><td>'.dol_print_date($productFournLog['datec'], 'dayhour', 'tzuser').'</td>';
1116  $out .= '<td class="right">'.price($productFournLog['price'], 0, $langs, 1, -1, -1, $conf->currency);
1117  if ($productFournLog['multicurrency_code'] != $conf->currency) {
1118  $out .= ' ('.price($productFournLog['multicurrency_price'], 0, $langs, 1, -1, -1, $productFournLog['multicurrency_code']).')';
1119  }
1120  $out .= '</td>';
1121  //$out.= '<td class="right">'.$productFournLog['quantity'].'</td>';
1122  $out .= '<td>'.$productFournLog['lastname'].'</td></tr>';
1123  }
1124  $out .= '</table>';
1125  }
1126  return $out;
1127  }
1128 
1129 
1141  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
1142  {
1143  global $db, $conf, $langs, $hookmanager;
1144 
1145  if (!empty($conf->dol_no_mouse_hover)) {
1146  $notooltip = 1; // Force disable tooltips
1147  }
1148 
1149  $result = '';
1150  $label = '';
1151 
1152  if (!empty($this->entity)) {
1153  $tmpphoto = $this->show_photos('product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 80);
1154  if ($this->nbphoto > 0) {
1155  $label .= '<div class="photointooltip">';
1156  $label .= $tmpphoto;
1157  $label .= '</div><div style="clear: both;"></div>';
1158  }
1159  }
1160 
1161  if ($this->type == Product::TYPE_PRODUCT) {
1162  $label .= img_picto('', 'product').' <u class="paddingrightonly">'.$langs->trans("Product").'</u>';
1163  } elseif ($this->type == Product::TYPE_SERVICE) {
1164  $label .= img_picto('', 'service').' <u class="paddingrightonly">'.$langs->trans("Service").'</u>';
1165  }
1166  if (isset($this->status) && isset($this->status_buy)) {
1167  $label .= ' '.$this->getLibStatut(5, 0);
1168  $label .= ' '.$this->getLibStatut(5, 1);
1169  }
1170 
1171  if (!empty($this->ref)) {
1172  $label .= '<br><b>'.$langs->trans('ProductRef').':</b> '.($this->ref ? $this->ref : $this->product_ref);
1173  }
1174  if (!empty($this->label)) {
1175  $label .= '<br><b>'.$langs->trans('ProductLabel').':</b> '.$this->label;
1176  }
1177  $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
1178 
1179  if ($this->type == Product::TYPE_PRODUCT || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
1180  if (!empty($conf->productbatch->enabled)) {
1181  $langs->load("productbatch");
1182  $label .= "<br><b>".$langs->trans("ManageLotSerial").'</b>: '.$this->getLibStatut(0, 2);
1183  }
1184  }
1185  if (!empty($conf->barcode->enabled)) {
1186  $label .= '<br><b>'.$langs->trans('BarCode').':</b> '.$this->barcode;
1187  }
1188 
1189  if ($this->type == Product::TYPE_PRODUCT) {
1190  if ($this->weight) {
1191  $label .= "<br><b>".$langs->trans("Weight").'</b>: '.$this->weight.' '.measuringUnitString(0, "weight", $this->weight_units);
1192  }
1193  $labelsize = "";
1194  if ($this->length) {
1195  $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Length").'</b>: '.$this->length.' '.measuringUnitString(0, 'size', $this->length_units);
1196  }
1197  if ($this->width) {
1198  $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Width").'</b>: '.$this->width.' '.measuringUnitString(0, 'size', $this->width_units);
1199  }
1200  if ($this->height) {
1201  $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Height").'</b>: '.$this->height.' '.measuringUnitString(0, 'size', $this->height_units);
1202  }
1203  if ($labelsize) {
1204  $label .= "<br>".$labelsize;
1205  }
1206 
1207  $labelsurfacevolume = "";
1208  if ($this->surface) {
1209  $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Surface").'</b>: '.$this->surface.' '.measuringUnitString(0, 'surface', $this->surface_units);
1210  }
1211  if ($this->volume) {
1212  $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Volume").'</b>: '.$this->volume.' '.measuringUnitString(0, 'volume', $this->volume_units);
1213  }
1214  if ($labelsurfacevolume) {
1215  $label .= "<br>".$labelsurfacevolume;
1216  }
1217  }
1218 
1219  if (!empty($conf->accounting->enabled) && $this->status) {
1220  include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1221  $label .= '<br><b>'.$langs->trans('ProductAccountancySellCode').':</b> '.length_accountg($this->accountancy_code_sell);
1222  $label .= '<br><b>'.$langs->trans('ProductAccountancySellIntraCode').':</b> '.length_accountg($this->accountancy_code_sell_intra);
1223  $label .= '<br><b>'.$langs->trans('ProductAccountancySellExportCode').':</b> '.length_accountg($this->accountancy_code_sell_export);
1224  }
1225  if (!empty($conf->accounting->enabled) && $this->status_buy) {
1226  include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1227  $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyCode').':</b> '.length_accountg($this->accountancy_code_buy);
1228  $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyIntraCode').':</b> '.length_accountg($this->accountancy_code_buy_intra);
1229  $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyExportCode').':</b> '.length_accountg($this->accountancy_code_buy_export);
1230  }
1231 
1232  $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id, 'pfpl.datec', 'DESC'); // set sort order here
1233  if (is_array($logPrices) && count($logPrices) > 0) {
1234  $label .= '<br><br>';
1235  $label .= '<u>'.$langs->trans("History").'</u>';
1236  $label .= $this->displayPriceProductFournisseurLog($logPrices);
1237  }
1238 
1239  $url = dol_buildpath('/product/fournisseurs.php', 1).'?id='.$this->id.'&action=add_price&token='.newToken().'&socid='.$this->fourn_id.'&rowid='.$this->product_fourn_price_id;
1240 
1241  if ($option != 'nolink') {
1242  // Add param to save lastsearch_values or not
1243  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1244  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1245  $add_save_lastsearch_values = 1;
1246  }
1247  if ($add_save_lastsearch_values) {
1248  $url .= '&save_lastsearch_values=1';
1249  }
1250  }
1251 
1252  $linkclose = '';
1253  if (empty($notooltip)) {
1254  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1255  $label = $langs->trans("SupplierRef");
1256  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1257  }
1258  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
1259  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
1260  } else {
1261  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1262  }
1263 
1264  $linkstart = '<a href="'.$url.'"';
1265  $linkstart .= $linkclose.'>';
1266  $linkend = '</a>';
1267 
1268  $result .= $linkstart;
1269  if ($withpicto) {
1270  $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);
1271  }
1272  if ($withpicto != 2) {
1273  $result .= $this->ref.($this->ref_supplier ? ' ('.$this->ref_supplier.')' : '');
1274  }
1275  $result .= $linkend;
1276  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
1277 
1278  global $action;
1279  $hookmanager->initHooks(array($this->element . 'dao'));
1280  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
1281  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1282  if ($reshook > 0) {
1283  $result = $hookmanager->resPrint;
1284  } else {
1285  $result .= $hookmanager->resPrint;
1286  }
1287  return $result;
1288  }
1289 
1305  private function logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice = null, $multicurrency_unitBuyPrice = null, $multicurrency_tx = null, $fk_multicurrency = null, $multicurrency_code = null)
1306  {
1307  // Add record into log table
1308  $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log(";
1309  $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
1310  $sql .= "datec, fk_product_fournisseur,fk_user,price,quantity)";
1311  $sql .= "values(";
1312  $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
1313  $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
1314  $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
1315  $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
1316  $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
1317  $sql .= "'".$this->db->idate($datec)."',";
1318  $sql .= " ".((int) $this->product_fourn_price_id).",";
1319  $sql .= " ".$user->id.",";
1320  $sql .= " ".price2num($buyprice).",";
1321  $sql .= " ".price2num($qty);
1322  $sql .= ")";
1323 
1324  $resql = $this->db->query($sql);
1325  if (!$resql) {
1326  return -1;
1327  } else {
1328  return 1;
1329  }
1330  }
1331 }
db
$conf db
API class for accounts.
Definition: inc.php:41
Product\$ref_fourn
$ref_fourn
Definition: product.class.php:401
ProductFournisseur\$fourn_fk_barcode_type
$fourn_fk_barcode_type
Definition: fournisseur.product.class.php:130
ProductFournisseur\remove_product_fournisseur_price
remove_product_fournisseur_price($rowid)
Remove a price for a couple supplier-product.
Definition: fournisseur.product.class.php:195
ProductFournisseur\update_buyprice
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='', $options=array())
Modify the purchase price for a supplier.
Definition: fournisseur.product.class.php:263
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
ProductFournisseur
Class to manage predefined suppliers products.
Definition: fournisseur.product.class.php:41
ProductFournisseur\fetch_product_fournisseur_price
fetch_product_fournisseur_price($rowid, $ignore_expression=0)
Loads the price information of a provider.
Definition: fournisseur.product.class.php:580
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1062
MultiCurrency\getIdFromCode
static getIdFromCode($dbs, $code)
Get id of currency from code.
Definition: multicurrency.class.php:501
ProductFournisseurPrice
Class for ProductFournisseurPrice.
Definition: productfournisseurprice.class.php:31
ref
$object ref
Definition: info.php:77
ProductFournisseur\getSocNomUrl
getSocNomUrl($withpicto=0, $option='supplier', $maxlen=0, $notooltip=0)
Display supplier of product.
Definition: fournisseur.product.class.php:952
ProductFournisseur\logPrice
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.
Definition: fournisseur.product.class.php:1305
CommonObject\commonReplaceProduct
static commonReplaceProduct(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
Definition: commonobject.class.php:8376
ProductFournisseur\setSupplierPriceExpression
setSupplierPriceExpression($expression_id)
Sets the supplier price expression.
Definition: fournisseur.product.class.php:917
Product\getLibStatut
getLibStatut($mode=0, $type=0)
Return label of status of object.
Definition: product.class.php:5106
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
measuringUnitString
measuringUnitString($unit, $measuring_style='', $scale='', $use_short_label=0, $outputlangs=null)
Return translation label of a unit key.
Definition: product.lib.php:619
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
ProductFournisseur\getNomUrl
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto).
Definition: fournisseur.product.class.php:1141
PriceParser
Class to parse product price expressions.
Definition: price_parser.class.php:33
ProductFournisseur\listProductFournisseurPriceLog
listProductFournisseurPriceLog($product_fourn_price_id, $sortfield='', $sortorder='', $limit=0, $offset=0)
List supplier prices log of a supplier price.
Definition: fournisseur.product.class.php:1044
ProductFournisseur\$fourn_ref
$fourn_ref
Definition: fournisseur.product.class.php:64
get_localtax
get_localtax($vatrate, $local, $thirdparty_buyer="", $thirdparty_seller="", $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate,...
Definition: functions.lib.php:5837
ProductFournisseur\replaceThirdparty
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: fournisseur.product.class.php:1008
Fournisseur
Class to manage suppliers.
Definition: fournisseur.class.php:34
length_accountg
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
Definition: accounting.lib.php:94
ProductFournisseur\displayPriceProductFournisseurLog
displayPriceProductFournisseurLog($productFournLogList=array())
Display log price of product supplier price.
Definition: fournisseur.product.class.php:1102
CommonObject\commonReplaceThirdparty
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
Definition: commonobject.class.php:8347
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
ProductFournisseur\__construct
__construct($db)
Constructor.
Definition: fournisseur.product.class.php:145
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:10878
ProductFournisseur\list_product_fournisseur_price
list_product_fournisseur_price($prodid, $sortfield='', $sortorder='', $limit=0, $offset=0, $socid=0)
List all supplier prices of a product.
Definition: fournisseur.product.class.php:678
User
Class to manage Dolibarr users.
Definition: user.class.php:44
CommonObject\show_photos
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.
Definition: commonobject.class.php:8476
ProductFournisseur\replaceProduct
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
Definition: fournisseur.product.class.php:1025
ProductFournisseur\display_price_product_fournisseur
display_price_product_fournisseur($showunitprice=1, $showsuptitle=1, $maxlen=0, $notooltip=0, $productFournList=array())
Display price of product.
Definition: fournisseur.product.class.php:972
Product
Class to manage products or services.
Definition: product.class.php:46
Product\$tva_tx
$tva_tx
Default VAT rate of product.
Definition: product.class.php:185
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
ProductFournisseur\remove_fournisseur
remove_fournisseur($id_fourn)
Remove all prices for this couple supplier-product.
Definition: fournisseur.product.class.php:161
price
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
Definition: functions.lib.php:5541
Product\TYPE_SERVICE
const TYPE_SERVICE
Service.
Definition: product.class.php:504
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5791
Product\TYPE_PRODUCT
const TYPE_PRODUCT
Regular product.
Definition: product.class.php:500
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
ProductFournisseur\find_min_price_product_fournisseur
find_min_price_product_fournisseur($prodid, $qty=0, $socid=0)
Load properties for minimum price.
Definition: fournisseur.product.class.php:787
ProductFournisseur\$fourn_barcode
$fourn_barcode
Definition: fournisseur.product.class.php:119
float
div float
Buy price without taxes.
Definition: style.css.php:809