dolibarr  16.0.1
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) $error++;
423  }
424  }
425  }
426 
427  // TODO Add price_base_type and price_ttc
428 
429  dol_syslog(get_class($this).'::update_buyprice update knowing id of line = product_fourn_price_id = '.$this->product_fourn_price_id, LOG_DEBUG);
430  $resql = $this->db->query($sql);
431  if ($resql) {
432  // Call trigger
433  $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_MODIFY', $user);
434  if ($result < 0) {
435  $error++;
436  }
437  // End call triggers
438  if (!$error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
439  $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
440  if ($result < 0) {
441  $error++;
442  }
443  }
444  if (empty($error)) {
445  $this->db->commit();
446  return $this->product_fourn_price_id;
447  } else {
448  $this->db->rollback();
449  return -1;
450  }
451  } else {
452  $this->error = $this->db->error()." sql=".$sql;
453  $this->db->rollback();
454  return -2;
455  }
456  } else {
457  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);
458 
459  // Delete price for this quantity
460  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
461  $sql .= " WHERE fk_soc = ".((int) $fourn->id)." AND ref_fourn = '".$this->db->escape($ref_fourn)."' AND quantity = ".((float) $qty)." AND entity = ".((int) $conf->entity);
462  $resql = $this->db->query($sql);
463  if ($resql) {
464  // Add price for this quantity to supplier
465  $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price(";
466  $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
467  $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";
468  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
469  $sql .= ", packaging";
470  }
471  $sql .= ") values(";
472  $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
473  $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
474  $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
475  $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
476  $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
477  $sql .= " '".$this->db->idate($now)."',";
478  $sql .= " ".((int) $this->id).",";
479  $sql .= " ".((int) $fourn->id).",";
480  $sql .= " '".$this->db->escape($ref_fourn)."',";
481  $sql .= " '".$this->db->escape($desc_fourn)."',";
482  $sql .= " ".((int) $user->id).",";
483  $sql .= " ".price2num($buyprice).",";
484  $sql .= " ".((float) $qty).",";
485  $sql .= " ".((float) $remise_percent).",";
486  $sql .= " ".((float) $remise).",";
487  $sql .= " ".price2num($unitBuyPrice).",";
488  $sql .= " ".price2num($tva_tx).",";
489  $sql .= " ".price2num($charges).",";
490  $sql .= " ".((int) $availability).",";
491  $sql .= " ".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
492  $sql .= " ".((int) $newnpr).",";
493  $sql .= $conf->entity.",";
494  $sql .= ($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
495  $sql .= (empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
496  $sql .= (empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
497  $sql .= (empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape($fk_barcode_type)."'");
498  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
499  $sql .= ", ".(empty($this->packaging) ? '1' : "'".$this->db->escape($this->packaging)."'");
500  }
501  $sql .= ")";
502 
503  $this->product_fourn_price_id = 0;
504 
505  $resql = $this->db->query($sql);
506  if ($resql) {
507  $this->product_fourn_price_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur_price");
508  } else {
509  $error++;
510  }
511 
512  if (!$error) {
513  if (!empty($options) && is_array($options)) {
514  $productfournisseurprice = new ProductFournisseurPrice($this->db);
515  $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
516  if ($res > 0) {
517  foreach ($options as $key=>$value) {
518  $productfournisseurprice->array_options[$key] = $value;
519  }
520  $res = $productfournisseurprice->update($user);
521  if ($res < 0) $error++;
522  }
523  }
524  }
525 
526  if (!$error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
527  // Add record into log table
528  // $this->product_fourn_price_id must be set
529  $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
530  if ($result < 0) {
531  $error++;
532  }
533  }
534 
535  if (!$error) {
536  // Call trigger
537  $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_CREATE', $user);
538  if ($result < 0) {
539  $error++;
540  }
541  // End call triggers
542 
543  if (empty($error)) {
544  $this->db->commit();
545  return $this->product_fourn_price_id;
546  } else {
547  $this->db->rollback();
548  return -1;
549  }
550  } else {
551  $this->error = $this->db->lasterror()." sql=".$sql;
552  $this->db->rollback();
553  return -2;
554  }
555  } else {
556  $this->error = $this->db->lasterror()." sql=".$sql;
557  $this->db->rollback();
558  return -1;
559  }
560  }
561  }
562 
563  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
571  public function fetch_product_fournisseur_price($rowid, $ignore_expression = 0)
572  {
573  // phpcs:enable
574  global $conf;
575 
576  $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,";
577  $sql .= " pfp.fk_soc, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_product, pfp.charges, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
578  $sql .= " pfp.supplier_reputation, pfp.fk_user, pfp.datec,";
579  $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
580  $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging,";
581  $sql .= " p.ref as product_ref, p.tosell as status, p.tobuy as status_buy";
582  $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p";
583  $sql .= " WHERE pfp.rowid = ".(int) $rowid;
584  $sql .= " AND pfp.fk_product = p.rowid";
585 
586  dol_syslog(get_class($this)."::fetch_product_fournisseur_price", LOG_DEBUG);
587  $resql = $this->db->query($sql);
588  if ($resql) {
589  $obj = $this->db->fetch_object($resql);
590  if ($obj) {
591  $this->product_fourn_price_id = $rowid;
592  $this->id = $obj->fk_product;
593 
594  $this->fk_product = $obj->fk_product;
595  $this->product_id = $obj->fk_product;
596  $this->product_ref = $obj->product_ref;
597  $this->status = $obj->status;
598  $this->status_buy = $obj->status_buy;
599  $this->fourn_id = $obj->fk_soc;
600  $this->fourn_ref = $obj->ref_fourn; // deprecated
601  $this->ref_supplier = $obj->ref_fourn;
602  $this->desc_supplier = $obj->desc_fourn;
603  $this->fourn_price = $obj->price;
604  $this->fourn_charges = $obj->charges; // deprecated
605  $this->fourn_qty = $obj->quantity;
606  $this->fourn_remise_percent = $obj->remise_percent;
607  $this->fourn_remise = $obj->remise;
608  $this->fourn_unitprice = $obj->unitprice;
609  $this->fourn_tva_tx = $obj->tva_tx;
610  $this->fourn_tva_npr = $obj->fourn_tva_npr;
611  // Add also localtaxes
612  $this->fk_availability = $obj->fk_availability;
613  $this->delivery_time_days = $obj->delivery_time_days;
614  $this->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
615  $this->supplier_reputation = $obj->supplier_reputation;
616  $this->default_vat_code = $obj->default_vat_code;
617  $this->user_id = $obj->fk_user;
618  $this->date_creation = $this->db->jdate($obj->datec);
619  $this->fourn_multicurrency_price = $obj->multicurrency_price;
620  $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
621  $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
622  $this->fourn_multicurrency_id = $obj->fk_multicurrency;
623  $this->fourn_multicurrency_code = $obj->multicurrency_code;
624  if (!empty($conf->barcode->enabled)) {
625  $this->fourn_barcode = $obj->barcode; // deprecated
626  $this->fourn_fk_barcode_type = $obj->fk_barcode_type; // deprecated
627  $this->supplier_barcode = $obj->barcode;
628  $this->supplier_fk_barcode_type = $obj->fk_barcode_type;
629  }
630  $this->packaging = $obj->packaging;
631 
632  if (empty($ignore_expression) && !empty($this->fk_supplier_price_expression)) {
633  $priceparser = new PriceParser($this->db);
634  $price_result = $priceparser->parseProductSupplier($this);
635  if ($price_result >= 0) {
636  $this->fourn_price = $price_result;
637  //recalculation of unitprice, as probably the price changed...
638  if ($this->fourn_qty != 0) {
639  $this->fourn_unitprice = price2num($this->fourn_price / $this->fourn_qty, 'MU');
640  } else {
641  $this->fourn_unitprice = "";
642  }
643  }
644  }
645 
646  return 1;
647  } else {
648  return 0;
649  }
650  } else {
651  $this->error = $this->db->lasterror();
652  return -1;
653  }
654  }
655 
656 
657  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
669  public function list_product_fournisseur_price($prodid, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0, $socid = 0)
670  {
671  // phpcs:enable
672  global $conf;
673 
674  $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, ";
675  $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,";
676  $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,";
677  $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code, pfp.datec, pfp.tms,";
678  $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging";
679  $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."societe as s";
680  $sql .= " WHERE pfp.entity IN (".getEntity('productsupplierprice').")";
681  $sql .= " AND pfp.fk_soc = s.rowid AND pfp.fk_product = p.rowid";
682  $sql .= ($socid > 0 ? ' AND pfp.fk_soc = '.((int) $socid) : '');
683  $sql .= " AND s.status = 1"; // only enabled company selected
684  $sql .= " AND pfp.fk_product = ".((int) $prodid);
685  if (empty($sortfield)) {
686  $sql .= " ORDER BY s.nom, pfp.quantity, pfp.price";
687  } else {
688  $sql .= $this->db->order($sortfield, $sortorder);
689  }
690  $sql .= $this->db->plimit($limit, $offset);
691  dol_syslog(get_class($this)."::list_product_fournisseur_price", LOG_DEBUG);
692 
693  $resql = $this->db->query($sql);
694  if ($resql) {
695  $retarray = array();
696 
697  while ($record = $this->db->fetch_array($resql)) {
698  //define base attribute
699  $prodfourn = new ProductFournisseur($this->db);
700 
701  $prodfourn->product_ref = $record["product_ref"];
702  $prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"];
703  $prodfourn->status = $record["status"];
704  $prodfourn->status_buy = $record["status_buy"];
705  $prodfourn->product_fourn_id = $record["product_fourn_id"];
706  $prodfourn->product_fourn_entity = $record["entity"];
707  $prodfourn->ref_supplier = $record["ref_fourn"];
708  $prodfourn->fourn_ref = $record["ref_fourn"];
709  $prodfourn->desc_supplier = $record["desc_fourn"];
710  $prodfourn->fourn_price = $record["price"];
711  $prodfourn->fourn_qty = $record["quantity"];
712  $prodfourn->fourn_remise_percent = $record["remise_percent"];
713  $prodfourn->fourn_remise = $record["remise"];
714  $prodfourn->fourn_unitprice = $record["unitprice"];
715  $prodfourn->fourn_charges = $record["charges"]; // deprecated
716  $prodfourn->fourn_tva_tx = $record["tva_tx"];
717  $prodfourn->fourn_id = $record["fourn_id"];
718  $prodfourn->fourn_name = $record["supplier_name"];
719  $prodfourn->fk_availability = $record["fk_availability"];
720  $prodfourn->delivery_time_days = $record["delivery_time_days"];
721  $prodfourn->id = $prodid;
722  $prodfourn->fourn_tva_npr = $record["info_bits"];
723  $prodfourn->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
724  $prodfourn->supplier_reputation = $record["supplier_reputation"];
725  $prodfourn->fourn_date_creation = $this->db->jdate($record['datec']);
726  $prodfourn->fourn_date_modification = $this->db->jdate($record['tms']);
727 
728  $prodfourn->fourn_multicurrency_price = $record["multicurrency_price"];
729  $prodfourn->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
730  $prodfourn->fourn_multicurrency_tx = $record["multicurrency_tx"];
731  $prodfourn->fourn_multicurrency_id = $record["fk_multicurrency"];
732  $prodfourn->fourn_multicurrency_code = $record["multicurrency_code"];
733 
734  $prodfourn->packaging = $record["packaging"];
735 
736  if (!empty($conf->barcode->enabled)) {
737  $prodfourn->supplier_barcode = $record["barcode"];
738  $prodfourn->supplier_fk_barcode_type = $record["fk_barcode_type"];
739  }
740 
741  if (!empty($conf->dynamicprices->enabled) && !empty($prodfourn->fk_supplier_price_expression)) {
742  $priceparser = new PriceParser($this->db);
743  $price_result = $priceparser->parseProductSupplier($prodfourn);
744  if ($price_result >= 0) {
745  $prodfourn->fourn_price = $price_result;
746  $prodfourn->fourn_unitprice = null; //force recalculation of unitprice, as probably the price changed...
747  }
748  }
749 
750  if (!isset($prodfourn->fourn_unitprice)) {
751  if ($prodfourn->fourn_qty != 0) {
752  $prodfourn->fourn_unitprice = price2num($prodfourn->fourn_price / $prodfourn->fourn_qty, 'MU');
753  } else {
754  $prodfourn->fourn_unitprice = "";
755  }
756  }
757 
758  $retarray[] = $prodfourn;
759  }
760 
761  $this->db->free($resql);
762  return $retarray;
763  } else {
764  $this->error = $this->db->error();
765  return -1;
766  }
767  }
768 
769  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
778  public function find_min_price_product_fournisseur($prodid, $qty = 0, $socid = 0)
779  {
780  // phpcs:enable
781  global $conf;
782 
783  if (empty($prodid)) {
784  dol_syslog("Warning function find_min_price_product_fournisseur were called with prodid empty. May be a bug.", LOG_WARNING);
785  return 0;
786  }
787 
788  $this->product_fourn_price_id = '';
789  $this->product_fourn_id = '';
790  $this->fourn_ref = '';
791  $this->fourn_price = '';
792  $this->fourn_qty = '';
793  $this->fourn_remise_percent = '';
794  $this->fourn_remise = '';
795  $this->fourn_unitprice = '';
796  $this->fourn_id = '';
797  $this->fourn_name = '';
798  $this->delivery_time_days = '';
799  $this->id = '';
800 
801  $this->fourn_multicurrency_price = '';
802  $this->fourn_multicurrency_unitprice = '';
803  $this->fourn_multicurrency_tx = '';
804  $this->fourn_multicurrency_id = '';
805  $this->fourn_multicurrency_code = '';
806 
807  $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
808  $sql .= " pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
809  $sql .= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges,";
810  $sql .= " pfp.remise, pfp.remise_percent, pfp.fk_supplier_price_expression, pfp.delivery_time_days";
811  $sql .= " ,pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code";
812  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
813  $sql .= " WHERE s.entity IN (".getEntity('societe').")";
814  $sql .= " AND pfp.entity IN (".getEntity('productsupplierprice').")";
815  $sql .= " AND pfp.fk_product = ".((int) $prodid);
816  $sql .= " AND pfp.fk_soc = s.rowid";
817  $sql .= " AND s.status = 1"; // only enabled society
818  if ($qty > 0) {
819  $sql .= " AND pfp.quantity <= ".((float) $qty);
820  }
821  if ($socid > 0) {
822  $sql .= ' AND pfp.fk_soc = '.((int) $socid);
823  }
824 
825  dol_syslog(get_class($this)."::find_min_price_product_fournisseur", LOG_DEBUG);
826 
827  $resql = $this->db->query($sql);
828  if ($resql) {
829  $record_array = array();
830 
831  //Store each record to array for later search of min
832  while ($record = $this->db->fetch_array($resql)) {
833  $record_array[] = $record;
834  }
835 
836  if (count($record_array) == 0) {
837  $this->db->free($resql);
838  return 0;
839  } else {
840  $min = -1;
841  foreach ($record_array as $record) {
842  $fourn_price = $record["price"];
843  // calculate unit price for quantity 1
844  $fourn_unitprice = $record["unitprice"];
845  $fourn_unitprice_with_discount = $record["unitprice"] * (1 - $record["remise_percent"] / 100);
846 
847  if (!empty($conf->dynamicprices->enabled) && !empty($record["fk_supplier_price_expression"])) {
848  $prod_supplier = new ProductFournisseur($this->db);
849  $prod_supplier->product_fourn_price_id = $record["product_fourn_price_id"];
850  $prod_supplier->id = $prodid;
851  $prod_supplier->fourn_qty = $record["quantity"];
852  $prod_supplier->fourn_tva_tx = $record["tva_tx"];
853  $prod_supplier->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
854  $priceparser = new PriceParser($this->db);
855  $price_result = $priceparser->parseProductSupplier($prod_supplier);
856  if ($price_result >= 0) {
857  $fourn_price = price2num($price_result, 'MU');
858  if ($record["quantity"] != 0) {
859  $fourn_unitprice = price2num($fourn_price / $record["quantity"], 'MU');
860  } else {
861  $fourn_unitprice = $fourn_price;
862  }
863  $fourn_unitprice_with_discount = $fourn_unitprice * (1 - $record["remise_percent"] / 100);
864  }
865  }
866  if ($fourn_unitprice < $min || $min == -1) {
867  $this->product_fourn_price_id = $record["product_fourn_price_id"];
868  $this->ref_supplier = $record["ref_fourn"];
869  $this->ref_fourn = $record["ref_fourn"]; // deprecated
870  $this->fourn_ref = $record["ref_fourn"]; // deprecated
871  $this->fourn_price = $fourn_price;
872  $this->fourn_qty = $record["quantity"];
873  $this->fourn_remise_percent = $record["remise_percent"];
874  $this->fourn_remise = $record["remise"];
875  $this->fourn_unitprice = $fourn_unitprice;
876  $this->fourn_unitprice_with_discount = $fourn_unitprice_with_discount;
877  $this->fourn_charges = $record["charges"]; // deprecated
878  $this->fourn_tva_tx = $record["tva_tx"];
879  $this->fourn_id = $record["fourn_id"];
880  $this->fourn_name = $record["supplier_name"];
881  $this->delivery_time_days = $record["delivery_time_days"];
882  $this->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
883  $this->id = $prodid;
884  $this->fourn_multicurrency_price = $record["multicurrency_price"];
885  $this->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
886  $this->fourn_multicurrency_tx = $record["multicurrency_tx"];
887  $this->fourn_multicurrency_id = $record["fk_multicurrency"];
888  $this->fourn_multicurrency_code = $record["multicurrency_code"];
889  $min = $fourn_unitprice;
890  }
891  }
892  }
893 
894  $this->db->free($resql);
895  return 1;
896  } else {
897  $this->error = $this->db->error();
898  return -1;
899  }
900  }
901 
908  public function setSupplierPriceExpression($expression_id)
909  {
910  global $conf;
911 
912  // Clean parameters
913  $this->db->begin();
914  $expression_id = $expression_id != 0 ? $expression_id : 'NULL';
915 
916  $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
917  $sql .= " SET fk_supplier_price_expression = ".((int) $expression_id);
918  $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
919 
920  dol_syslog(get_class($this)."::setSupplierPriceExpression", LOG_DEBUG);
921 
922  $resql = $this->db->query($sql);
923  if ($resql) {
924  $this->db->commit();
925  return 1;
926  } else {
927  $this->error = $this->db->error()." sql=".$sql;
928  $this->db->rollback();
929  return -1;
930  }
931  }
932 
943  public function getSocNomUrl($withpicto = 0, $option = 'supplier', $maxlen = 0, $notooltip = 0)
944  {
945  $thirdparty = new Fournisseur($this->db);
946  $thirdparty->fetch($this->fourn_id);
947 
948  return $thirdparty->getNomUrl($withpicto, $option, $maxlen, $notooltip);
949  }
950 
951  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
963  public function display_price_product_fournisseur($showunitprice = 1, $showsuptitle = 1, $maxlen = 0, $notooltip = 0, $productFournList = array())
964  {
965  // phpcs:enable
966  global $conf, $langs;
967 
968  $out = '';
969  $langs->load("suppliers");
970  if (count($productFournList) > 0) {
971  $out .= '<table class="nobordernopadding" width="100%">';
972  $out .= '<tr><td class="liste_titre right">'.($showunitprice ? $langs->trans("Price").' '.$langs->trans("HT") : '').'</td>';
973  $out .= '<td class="liste_titre right">'.($showunitprice ? $langs->trans("QtyMin") : '').'</td>';
974  $out .= '<td class="liste_titre">'.$langs->trans("Supplier").'</td>';
975  $out .= '<td class="liste_titre">'.$langs->trans("SupplierRef").'</td></tr>';
976  foreach ($productFournList as $productFourn) {
977  $out .= '<tr><td class="right">'.($showunitprice ?price($productFourn->fourn_unitprice * (1 - $productFourn->fourn_remise_percent / 100) - $productFourn->fourn_remise) : '').'</td>';
978  $out .= '<td class="right">'.($showunitprice ? $productFourn->fourn_qty : '').'</td>';
979  $out .= '<td>'.$productFourn->getSocNomUrl(1, 'supplier', $maxlen, $notooltip).'</td>';
980  $out .= '<td>'.$productFourn->fourn_ref.'<td></tr>';
981  }
982  $out .= '</table>';
983  } else {
984  $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>' : '');
985  $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;
986  $out .= ($showunitprice ? '<span class="opacitymedium">)</span>' : '');
987  }
988  return $out;
989  }
990 
999  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
1000  {
1001  $tables = array(
1002  'product_fournisseur_price'
1003  );
1004 
1005  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
1006  }
1007 
1016  public static function replaceProduct(DoliDB $db, $origin_id, $dest_id)
1017  {
1018  $tables = array(
1019  'product_fournisseur_price'
1020  );
1021 
1022  return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables);
1023  }
1024 
1035  public function listProductFournisseurPriceLog($product_fourn_price_id, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0)
1036  {
1037  $sql = "SELECT";
1038  $sql .= " u.lastname,";
1039  $sql .= " pfpl.rowid, pfp.ref_fourn as supplier_ref, pfpl.datec,";
1040  $sql .= " pfpl.price, pfpl.quantity,";
1041  $sql .= " pfpl.fk_multicurrency, pfpl.multicurrency_code, pfpl.multicurrency_tx, pfpl.multicurrency_price, pfpl.multicurrency_unitprice";
1042  $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price_log as pfpl,";
1043  $sql .= " ".MAIN_DB_PREFIX."product_fournisseur_price as pfp,";
1044  $sql .= " ".MAIN_DB_PREFIX."user as u";
1045  $sql .= " WHERE pfp.entity IN (".getEntity('productprice').")";
1046  $sql .= " AND pfpl.fk_user = u.rowid";
1047  $sql .= " AND pfp.rowid = pfpl.fk_product_fournisseur";
1048  $sql .= " AND pfpl.fk_product_fournisseur = ".((int) $product_fourn_price_id);
1049  if (empty($sortfield)) {
1050  $sql .= " ORDER BY pfpl.datec";
1051  } else {
1052  $sql .= $this->db->order($sortfield, $sortorder);
1053  }
1054  $sql .= $this->db->plimit($limit, $offset);
1055  dol_syslog(get_class($this)."::list_product_fournisseur_price_log", LOG_DEBUG);
1056 
1057  $resql = $this->db->query($sql);
1058  if ($resql) {
1059  $retarray = array();
1060 
1061  while ($obj = $this->db->fetch_object($resql)) {
1062  $tmparray = array();
1063  $tmparray['rowid'] = $obj->rowid;
1064  $tmparray['supplier_ref'] = $obj->supplier_ref;
1065  $tmparray['datec'] = $this->db->jdate($obj->datec);
1066  $tmparray['lastname'] = $obj->lastname;
1067  $tmparray['price'] = $obj->price;
1068  $tmparray['quantity'] = $obj->quantity;
1069  $tmparray['fk_multicurrency'] = $obj->fk_multicurrency;
1070  $tmparray['multicurrency_code'] = $obj->multicurrency_code;
1071  $tmparray['multicurrency_tx'] = $obj->multicurrency_tx;
1072  $tmparray['multicurrency_price'] = $obj->multicurrency_price;
1073  $tmparray['multicurrency_unitprice'] = $obj->multicurrency_unitprice;
1074 
1075  $retarray[] = $tmparray;
1076  }
1077 
1078  $this->db->free($resql);
1079  return $retarray;
1080  } else {
1081  $this->error = $this->db->error();
1082  return -1;
1083  }
1084  }
1085 
1093  public function displayPriceProductFournisseurLog($productFournLogList = array())
1094  {
1095  global $conf, $langs;
1096 
1097  $out = '';
1098  $langs->load("suppliers");
1099  if (count($productFournLogList) > 0) {
1100  $out .= '<table class="noborder centpercent">';
1101  $out .= '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Date").'</td>';
1102  $out .= '<td class="liste_titre right">'.$langs->trans("Price").'</td>';
1103  //$out .= '<td class="liste_titre right">'.$langs->trans("QtyMin").'</td>';
1104  $out .= '<td class="liste_titre">'.$langs->trans("User").'</td></tr>';
1105  foreach ($productFournLogList as $productFournLog) {
1106  $out .= '<tr><td>'.dol_print_date($productFournLog['datec'], 'dayhour', 'tzuser').'</td>';
1107  $out .= '<td class="right">'.price($productFournLog['price'], 0, $langs, 1, -1, -1, $conf->currency);
1108  if ($productFournLog['multicurrency_code'] != $conf->currency) {
1109  $out .= ' ('.price($productFournLog['multicurrency_price'], 0, $langs, 1, -1, -1, $productFournLog['multicurrency_code']).')';
1110  }
1111  $out .= '</td>';
1112  //$out.= '<td class="right">'.$productFournLog['quantity'].'</td>';
1113  $out .= '<td>'.$productFournLog['lastname'].'</td></tr>';
1114  }
1115  $out .= '</table>';
1116  }
1117  return $out;
1118  }
1119 
1120 
1132  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
1133  {
1134  global $db, $conf, $langs, $hookmanager;
1135 
1136  if (!empty($conf->dol_no_mouse_hover)) {
1137  $notooltip = 1; // Force disable tooltips
1138  }
1139 
1140  $result = '';
1141  $label = '';
1142 
1143  if (!empty($this->entity)) {
1144  $tmpphoto = $this->show_photos('product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 80);
1145  if ($this->nbphoto > 0) {
1146  $label .= '<div class="photointooltip">';
1147  $label .= $tmpphoto;
1148  $label .= '</div><div style="clear: both;"></div>';
1149  }
1150  }
1151 
1152  if ($this->type == Product::TYPE_PRODUCT) {
1153  $label .= img_picto('', 'product').' <u class="paddingrightonly">'.$langs->trans("Product").'</u>';
1154  } elseif ($this->type == Product::TYPE_SERVICE) {
1155  $label .= img_picto('', 'service').' <u class="paddingrightonly">'.$langs->trans("Service").'</u>';
1156  }
1157  if (isset($this->status) && isset($this->status_buy)) {
1158  $label .= ' '.$this->getLibStatut(5, 0);
1159  $label .= ' '.$this->getLibStatut(5, 1);
1160  }
1161 
1162  if (!empty($this->ref)) {
1163  $label .= '<br><b>'.$langs->trans('ProductRef').':</b> '.($this->ref ? $this->ref : $this->product_ref);
1164  }
1165  if (!empty($this->label)) {
1166  $label .= '<br><b>'.$langs->trans('ProductLabel').':</b> '.$this->label;
1167  }
1168  $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
1169 
1170  if ($this->type == Product::TYPE_PRODUCT || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
1171  if (!empty($conf->productbatch->enabled)) {
1172  $langs->load("productbatch");
1173  $label .= "<br><b>".$langs->trans("ManageLotSerial").'</b>: '.$this->getLibStatut(0, 2);
1174  }
1175  }
1176  if (!empty($conf->barcode->enabled)) {
1177  $label .= '<br><b>'.$langs->trans('BarCode').':</b> '.$this->barcode;
1178  }
1179 
1180  if ($this->type == Product::TYPE_PRODUCT) {
1181  if ($this->weight) {
1182  $label .= "<br><b>".$langs->trans("Weight").'</b>: '.$this->weight.' '.measuringUnitString(0, "weight", $this->weight_units);
1183  }
1184  $labelsize = "";
1185  if ($this->length) {
1186  $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Length").'</b>: '.$this->length.' '.measuringUnitString(0, 'size', $this->length_units);
1187  }
1188  if ($this->width) {
1189  $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Width").'</b>: '.$this->width.' '.measuringUnitString(0, 'size', $this->width_units);
1190  }
1191  if ($this->height) {
1192  $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Height").'</b>: '.$this->height.' '.measuringUnitString(0, 'size', $this->height_units);
1193  }
1194  if ($labelsize) {
1195  $label .= "<br>".$labelsize;
1196  }
1197 
1198  $labelsurfacevolume = "";
1199  if ($this->surface) {
1200  $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Surface").'</b>: '.$this->surface.' '.measuringUnitString(0, 'surface', $this->surface_units);
1201  }
1202  if ($this->volume) {
1203  $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Volume").'</b>: '.$this->volume.' '.measuringUnitString(0, 'volume', $this->volume_units);
1204  }
1205  if ($labelsurfacevolume) {
1206  $label .= "<br>".$labelsurfacevolume;
1207  }
1208  }
1209 
1210  if (!empty($conf->accounting->enabled) && $this->status) {
1211  include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1212  $label .= '<br><b>'.$langs->trans('ProductAccountancySellCode').':</b> '.length_accountg($this->accountancy_code_sell);
1213  $label .= '<br><b>'.$langs->trans('ProductAccountancySellIntraCode').':</b> '.length_accountg($this->accountancy_code_sell_intra);
1214  $label .= '<br><b>'.$langs->trans('ProductAccountancySellExportCode').':</b> '.length_accountg($this->accountancy_code_sell_export);
1215  }
1216  if (!empty($conf->accounting->enabled) && $this->status_buy) {
1217  include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1218  $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyCode').':</b> '.length_accountg($this->accountancy_code_buy);
1219  $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyIntraCode').':</b> '.length_accountg($this->accountancy_code_buy_intra);
1220  $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyExportCode').':</b> '.length_accountg($this->accountancy_code_buy_export);
1221  }
1222 
1223  $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id, 'pfpl.datec', 'DESC'); // set sort order here
1224  if (is_array($logPrices) && count($logPrices) > 0) {
1225  $label .= '<br><br>';
1226  $label .= '<u>'.$langs->trans("History").'</u>';
1227  $label .= $this->displayPriceProductFournisseurLog($logPrices);
1228  }
1229 
1230  $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;
1231 
1232  if ($option != 'nolink') {
1233  // Add param to save lastsearch_values or not
1234  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1235  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1236  $add_save_lastsearch_values = 1;
1237  }
1238  if ($add_save_lastsearch_values) {
1239  $url .= '&save_lastsearch_values=1';
1240  }
1241  }
1242 
1243  $linkclose = '';
1244  if (empty($notooltip)) {
1245  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1246  $label = $langs->trans("SupplierRef");
1247  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1248  }
1249  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
1250  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
1251  } else {
1252  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1253  }
1254 
1255  $linkstart = '<a href="'.$url.'"';
1256  $linkstart .= $linkclose.'>';
1257  $linkend = '</a>';
1258 
1259  $result .= $linkstart;
1260  if ($withpicto) {
1261  $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);
1262  }
1263  if ($withpicto != 2) {
1264  $result .= $this->ref.($this->ref_supplier ? ' ('.$this->ref_supplier.')' : '');
1265  }
1266  $result .= $linkend;
1267  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
1268 
1269  global $action;
1270  $hookmanager->initHooks(array($this->element . 'dao'));
1271  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
1272  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1273  if ($reshook > 0) {
1274  $result = $hookmanager->resPrint;
1275  } else {
1276  $result .= $hookmanager->resPrint;
1277  }
1278  return $result;
1279  }
1280 
1296  private function logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice = null, $multicurrency_unitBuyPrice = null, $multicurrency_tx = null, $fk_multicurrency = null, $multicurrency_code = null)
1297  {
1298  // Add record into log table
1299  $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log(";
1300  $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
1301  $sql .= "datec, fk_product_fournisseur,fk_user,price,quantity)";
1302  $sql .= "values(";
1303  $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
1304  $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
1305  $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
1306  $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
1307  $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
1308  $sql .= "'".$this->db->idate($datec)."',";
1309  $sql .= " ".((int) $this->product_fourn_price_id).",";
1310  $sql .= " ".$user->id.",";
1311  $sql .= " ".price2num($buyprice).",";
1312  $sql .= " ".price2num($qty);
1313  $sql .= ")";
1314 
1315  $resql = $this->db->query($sql);
1316  if (!$resql) {
1317  return -1;
1318  } else {
1319  return 1;
1320  }
1321  }
1322 }
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.
$conf db
API class for accounts.
Definition: inc.php:41
Class to manage products or services.
dol_now($mode= 'auto')
Return date for now.
static commonReplaceProduct(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
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= '', $options=array())
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.
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.
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.
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.
list_product_fournisseur_price($prodid, $sortfield= '', $sortorder= '', $limit=0, $offset=0, $socid=0)
List all supplier prices of a product.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
static getIdFromCode($dbs, $code)
Get id of currency from code.
display_price_product_fournisseur($showunitprice=1, $showsuptitle=1, $maxlen=0, $notooltip=0, $productFournList=array())
Display price of product.
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
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
getNomUrl($withpicto=0, $option= '', $notooltip=0, $morecss= '', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto).
div float
Buy price without taxes.
Definition: style.css.php:809
call_trigger($triggerName, $user)
Call trigger based on this instance.
$object ref
Definition: info.php:77
newToken()
Return the value of token currently saved into session with name &#39;newtoken&#39;.
fetch_product_fournisseur_price($rowid, $ignore_expression=0)
Loads the price information of a provider.
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
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:119
Class to manage predefined suppliers products.
Class for ProductFournisseurPrice.
measuringUnitString($unit, $measuring_style= '', $scale= '', $use_short_label=0, $outputlangs=null)
Return translation label of a unit key.