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