dolibarr  18.0.0-beta
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  const STATUS_OPEN = 1;
139  const STATUS_CANCELED = 0;
140 
141 
147  public function __construct($db)
148  {
149  global $langs;
150 
151  $this->db = $db;
152  $langs->load("suppliers");
153  $this->reputations = array('-1'=>'', 'FAVORITE'=>$langs->trans('Favorite'), 'NOTTHGOOD'=>$langs->trans('NotTheGoodQualitySupplier'), 'DONOTORDER'=>$langs->trans('DoNotOrderThisProductToThisSupplier'));
154  }
155 
156  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
163  public function remove_fournisseur($id_fourn)
164  {
165  // phpcs:enable
166  $ok = 1;
167 
168  $this->db->begin();
169 
170  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
171  $sql .= " WHERE fk_product = ".((int) $this->id)." AND fk_soc = ".((int) $id_fourn);
172 
173  dol_syslog(get_class($this)."::remove_fournisseur", LOG_DEBUG);
174  $resql2 = $this->db->query($sql);
175  if (!$resql2) {
176  $this->error = $this->db->lasterror();
177  $ok = 0;
178  }
179 
180  if ($ok) {
181  $this->db->commit();
182  return 1;
183  } else {
184  $this->db->rollback();
185  return -1;
186  }
187  }
188 
189 
190  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
197  public function remove_product_fournisseur_price($rowid)
198  {
199  // phpcs:enable
200  global $conf, $user;
201 
202  $error = 0;
203 
204  $this->db->begin();
205 
206  // Call trigger
207  $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_DELETE', $user);
208  if ($result < 0) {
209  $error++;
210  }
211  // End call triggers
212 
213  if (empty($error)) {
214  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
215  $sql .= " WHERE rowid = ".((int) $rowid);
216 
217  dol_syslog(get_class($this)."::remove_product_fournisseur_price", LOG_DEBUG);
218  $resql = $this->db->query($sql);
219  if (!$resql) {
220  $this->error = $this->db->lasterror();
221  $error++;
222  }
223  }
224 
225  if (empty($error)) {
226  $this->db->commit();
227  return 1;
228  } else {
229  $this->db->rollback();
230  return -1;
231  }
232  }
233 
234 
235  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
265  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())
266  {
267  // phpcs:enable
268  global $conf, $langs;
269  //global $mysoc;
270 
271  // Clean parameter
272  if (empty($qty)) {
273  $qty = 0;
274  }
275  if (empty($buyprice)) {
276  $buyprice = 0;
277  }
278  if (empty($charges)) {
279  $charges = 0;
280  }
281  if (empty($availability)) {
282  $availability = 0;
283  }
284  if (empty($remise_percent)) {
285  $remise_percent = 0;
286  }
287  if (empty($supplier_reputation) || $supplier_reputation == -1) {
288  $supplier_reputation = '';
289  }
290  if ($delivery_time_days != '' && !is_numeric($delivery_time_days)) {
291  $delivery_time_days = '';
292  }
293  if ($price_base_type == 'TTC') {
294  $ttx = $tva_tx;
295  $buyprice = $buyprice / (1 + ($ttx / 100));
296  }
297 
298  // Multicurrency
299  $multicurrency_unitBuyPrice = null;
300  $fk_multicurrency = null;
301  if (isModEnabled("multicurrency")) {
302  if (empty($multicurrency_tx)) {
303  $multicurrency_tx = 1;
304  }
305  if (empty($multicurrency_buyprice)) {
306  $multicurrency_buyprice = 0;
307  }
308  if ($multicurrency_price_base_type == 'TTC') {
309  $ttx = $tva_tx;
310  $multicurrency_buyprice = $multicurrency_buyprice / (1 + ($ttx / 100));
311  }
312  $multicurrency_buyprice = price2num($multicurrency_buyprice, 'MU');
313  $multicurrency_unitBuyPrice = price2num($multicurrency_buyprice / $qty, 'MU');
314 
315  $buyprice = $multicurrency_buyprice / $multicurrency_tx;
316  $fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $multicurrency_code);
317  }
318 
319  $buyprice = price2num($buyprice, 'MU');
320  $charges = price2num($charges, 'MU');
321  $qty = price2num($qty, 'MS');
322  $unitBuyPrice = price2num($buyprice / $qty, 'MU');
323 
324  // We can have a puchase ref that need to buy 100 min for a given price and with a packaging of 50.
325  //$packaging = price2num(((empty($this->packaging) || $this->packaging < $qty) ? $qty : $this->packaging), 'MS');
326  $packaging = price2num((empty($this->packaging) ? $qty : $this->packaging), 'MS');
327 
328  $error = 0;
329  $now = dol_now();
330 
331  $newvat = $tva_tx;
332 
333  if (count($localtaxes_array) > 0) {
334  $localtaxtype1 = $localtaxes_array['0'];
335  $localtax1 = $localtaxes_array['1'];
336  $localtaxtype2 = $localtaxes_array['2'];
337  $localtax2 = $localtaxes_array['3'];
338  } else { // old method. deprecated because ot can't retrieve type
339  $localtaxtype1 = '0';
340  $localtax1 = get_localtax($newvat, 1);
341  $localtaxtype2 = '0';
342  $localtax2 = get_localtax($newvat, 2);
343  }
344  if (empty($localtax1)) {
345  $localtax1 = 0; // If = '' then = 0
346  }
347  if (empty($localtax2)) {
348  $localtax2 = 0; // If = '' then = 0
349  }
350 
351  // Check parameters
352  if ($buyprice != '' && !is_numeric($buyprice)) {
353  }
354 
355  $this->db->begin();
356 
357  if ($this->product_fourn_price_id > 0) {
358  // check if price already logged, if not first log current price
359  $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id);
360  if (is_array($logPrices) && count($logPrices) == 0) {
361  $currentPfp = new self($this->db);
362  $result = $currentPfp->fetch_product_fournisseur_price($this->product_fourn_price_id);
363  if ($result > 0 && $currentPfp->fourn_price != 0) {
364  $currentPfpUser = new User($this->db);
365  $result = $currentPfpUser->fetch($currentPfp->user_id);
366  if ($result > 0) {
367  $currentPfp->logPrice(
368  $currentPfpUser,
369  $currentPfp->date_creation,
370  $currentPfp->fourn_price,
371  $currentPfp->fourn_qty,
372  $currentPfp->fourn_multicurrency_price,
373  $currentPfp->fourn_multicurrency_unitprice,
374  $currentPfp->fourn_multicurrency_tx,
375  $currentPfp->fourn_multicurrency_id,
376  $currentPfp->fourn_multicurrency_code
377  );
378  }
379  }
380  }
381  $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
382  $sql .= " SET fk_user = ".((int) $user->id)." ,";
383  $sql .= " ref_fourn = '".$this->db->escape($ref_fourn)."',";
384  $sql .= " desc_fourn = '".$this->db->escape($desc_fourn)."',";
385  $sql .= " price = ".((float) $buyprice).",";
386  $sql .= " quantity = ".((float) $qty).",";
387  $sql .= " remise_percent = ".((float) $remise_percent).",";
388  $sql .= " remise = ".((float) $remise).",";
389  $sql .= " unitprice = ".((float) $unitBuyPrice).",";
390  $sql .= " fk_availability = ".((int) $availability).",";
391  $sql .= " multicurrency_price = ".(isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
392  $sql .= " multicurrency_unitprice = ".(isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
393  $sql .= " multicurrency_tx = ".(isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
394  $sql .= " fk_multicurrency = ".(isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
395  $sql .= " multicurrency_code = ".(isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
396  $sql .= " entity = ".$conf->entity.",";
397  $sql .= " tva_tx = ".price2num($tva_tx).",";
398  // TODO Add localtax1 and localtax2
399  //$sql.= " localtax1_tx=".($localtax1>=0?$localtax1:'NULL').",";
400  //$sql.= " localtax2_tx=".($localtax2>=0?$localtax2:'NULL').",";
401  //$sql.= " localtax1_type=".($localtaxtype1!=''?"'".$this->db->escape($localtaxtype1)."'":"'0'").",";
402  //$sql.= " localtax2_type=".($localtaxtype2!=''?"'".$this->db->escape($localtaxtype2)."'":"'0'").",";
403  $sql .= " default_vat_code=".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
404  $sql .= " info_bits = ".((int) $newnpr).",";
405  $sql .= " charges = ".((float) $charges).","; // deprecated
406  $sql .= " delivery_time_days = ".($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
407  $sql .= " supplier_reputation = ".(empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
408  $sql .= " barcode = ".(empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
409  $sql .= " fk_barcode_type = ".(empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape($fk_barcode_type)."'");
410  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
411  $sql .= ", packaging = ".(empty($packaging) ? 1 : $packaging);
412  }
413  $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
414 
415  if (!$error) {
416  if (!empty($options) && is_array($options)) {
417  $productfournisseurprice = new ProductFournisseurPrice($this->db);
418  $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
419  if ($res > 0) {
420  foreach ($options as $key=>$value) {
421  $productfournisseurprice->array_options[$key] = $value;
422  }
423  $res = $productfournisseurprice->update($user);
424  if ($res < 0) {
425  $this->error = $productfournisseurprice->error;
426  $this->errors = $productfournisseurprice->errors;
427  $error++;
428  }
429  }
430  }
431  }
432 
433  // TODO Add price_base_type and price_ttc
434 
435  dol_syslog(get_class($this).'::update_buyprice update knowing id of line = product_fourn_price_id = '.$this->product_fourn_price_id, LOG_DEBUG);
436  $resql = $this->db->query($sql);
437  if ($resql) {
438  // Call trigger
439  $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_MODIFY', $user);
440  if ($result < 0) {
441  $error++;
442  }
443  // End call triggers
444  if (!$error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
445  $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
446  if ($result < 0) {
447  $error++;
448  }
449  }
450  if (empty($error)) {
451  $this->db->commit();
452  return $this->product_fourn_price_id;
453  } else {
454  $this->db->rollback();
455  return -1;
456  }
457  } else {
458  $this->error = $this->db->error()." sql=".$sql;
459  $this->db->rollback();
460  return -2;
461  }
462  } else {
463  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);
464 
465  // Delete price for this quantity
466  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
467  $sql .= " WHERE fk_soc = ".((int) $fourn->id)." AND ref_fourn = '".$this->db->escape($ref_fourn)."' AND quantity = ".((float) $qty)." AND entity = ".((int) $conf->entity);
468  $resql = $this->db->query($sql);
469  if ($resql) {
470  // Add price for this quantity to supplier
471  $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price(";
472  $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
473  $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";
474  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
475  $sql .= ", packaging";
476  }
477  $sql .= ") values(";
478  $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
479  $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
480  $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
481  $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
482  $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
483  $sql .= " '".$this->db->idate($now)."',";
484  $sql .= " ".((int) $this->id).",";
485  $sql .= " ".((int) $fourn->id).",";
486  $sql .= " '".$this->db->escape($ref_fourn)."',";
487  $sql .= " '".$this->db->escape($desc_fourn)."',";
488  $sql .= " ".((int) $user->id).",";
489  $sql .= " ".price2num($buyprice).",";
490  $sql .= " ".((float) $qty).",";
491  $sql .= " ".((float) $remise_percent).",";
492  $sql .= " ".((float) $remise).",";
493  $sql .= " ".price2num($unitBuyPrice).",";
494  $sql .= " ".price2num($tva_tx).",";
495  $sql .= " ".price2num($charges).",";
496  $sql .= " ".((int) $availability).",";
497  $sql .= " ".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
498  $sql .= " ".((int) $newnpr).",";
499  $sql .= $conf->entity.",";
500  $sql .= ($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
501  $sql .= (empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
502  $sql .= (empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
503  $sql .= (empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape($fk_barcode_type)."'");
504  if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
505  $sql .= ", ".(empty($this->packaging) ? '1' : "'".$this->db->escape($this->packaging)."'");
506  }
507  $sql .= ")";
508 
509  $this->product_fourn_price_id = 0;
510 
511  $resql = $this->db->query($sql);
512  if ($resql) {
513  $this->product_fourn_price_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur_price");
514  } else {
515  $this->error = $this->db->lasterror();
516  $error++;
517  }
518 
519  if (!$error) {
520  if (!empty($options) && is_array($options)) {
521  $productfournisseurprice = new ProductFournisseurPrice($this->db);
522  $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
523  if ($res > 0) {
524  foreach ($options as $key=>$value) {
525  $productfournisseurprice->array_options[$key] = $value;
526  }
527  $res = $productfournisseurprice->update($user);
528  if ($res < 0) {
529  $this->error = $productfournisseurprice->error;
530  $this->errors = $productfournisseurprice->errors;
531  $error++;
532  }
533  }
534  }
535  }
536 
537  if (!$error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
538  // Add record into log table
539  // $this->product_fourn_price_id must be set
540  $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
541  if ($result < 0) {
542  $error++;
543  }
544  }
545 
546  if (!$error) {
547  // Call trigger
548  $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_CREATE', $user);
549  if ($result < 0) {
550  $error++;
551  }
552  // End call triggers
553 
554  if (empty($error)) {
555  $this->db->commit();
556  return $this->product_fourn_price_id;
557  } else {
558  $this->db->rollback();
559  return -1;
560  }
561  } else {
562  $this->error = $this->db->lasterror()." sql=".$sql;
563  $this->db->rollback();
564  return -2;
565  }
566  } else {
567  $this->error = $this->db->lasterror()." sql=".$sql;
568  $this->db->rollback();
569  return -1;
570  }
571  }
572  }
573 
574  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
582  public function fetch_product_fournisseur_price($rowid, $ignore_expression = 0)
583  {
584  // phpcs:enable
585  global $conf;
586 
587  $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,";
588  $sql .= " pfp.fk_soc, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_product, pfp.charges, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
589  $sql .= " pfp.supplier_reputation, pfp.fk_user, pfp.datec,";
590  $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
591  $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging,";
592  $sql .= " p.ref as product_ref, p.tosell as status, p.tobuy as status_buy";
593  $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p";
594  $sql .= " WHERE pfp.rowid = ".(int) $rowid;
595  $sql .= " AND pfp.fk_product = p.rowid";
596 
597  dol_syslog(get_class($this)."::fetch_product_fournisseur_price", LOG_DEBUG);
598  $resql = $this->db->query($sql);
599  if ($resql) {
600  $obj = $this->db->fetch_object($resql);
601  if ($obj) {
602  $this->product_fourn_price_id = $rowid;
603  $this->id = $obj->fk_product;
604 
605  $this->fk_product = $obj->fk_product;
606  $this->product_id = $obj->fk_product;
607  $this->product_ref = $obj->product_ref;
608  $this->status = $obj->status;
609  $this->status_buy = $obj->status_buy;
610  $this->fourn_id = $obj->fk_soc;
611  $this->fourn_ref = $obj->ref_fourn; // deprecated
612  $this->ref_supplier = $obj->ref_fourn;
613  $this->desc_supplier = $obj->desc_fourn;
614  $this->fourn_price = $obj->price;
615  $this->fourn_charges = $obj->charges; // deprecated
616  $this->fourn_qty = $obj->quantity;
617  $this->fourn_remise_percent = $obj->remise_percent;
618  $this->fourn_remise = $obj->remise;
619  $this->fourn_unitprice = $obj->unitprice;
620  $this->fourn_tva_tx = $obj->tva_tx;
621  $this->fourn_tva_npr = $obj->fourn_tva_npr;
622  // Add also localtaxes
623  $this->fk_availability = $obj->fk_availability;
624  $this->delivery_time_days = $obj->delivery_time_days;
625  $this->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
626  $this->supplier_reputation = $obj->supplier_reputation;
627  $this->default_vat_code = $obj->default_vat_code;
628  $this->user_id = $obj->fk_user;
629  $this->date_creation = $this->db->jdate($obj->datec);
630  $this->fourn_multicurrency_price = $obj->multicurrency_price;
631  $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
632  $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
633  $this->fourn_multicurrency_id = $obj->fk_multicurrency;
634  $this->fourn_multicurrency_code = $obj->multicurrency_code;
635  if (isModEnabled('barcode')) {
636  $this->fourn_barcode = $obj->barcode; // deprecated
637  $this->fourn_fk_barcode_type = $obj->fk_barcode_type; // deprecated
638  $this->supplier_barcode = $obj->barcode;
639  $this->supplier_fk_barcode_type = $obj->fk_barcode_type;
640  }
641  $this->packaging = $obj->packaging;
642 
643  if (isModEnabled('dynamicprices') && empty($ignore_expression) && !empty($this->fk_supplier_price_expression)) {
644  require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
645  $priceparser = new PriceParser($this->db);
646  $price_result = $priceparser->parseProductSupplier($this);
647  if ($price_result >= 0) {
648  $this->fourn_price = $price_result;
649  //recalculation of unitprice, as probably the price changed...
650  if ($this->fourn_qty != 0) {
651  $this->fourn_unitprice = price2num($this->fourn_price / $this->fourn_qty, 'MU');
652  } else {
653  $this->fourn_unitprice = "";
654  }
655  }
656  }
657 
658  return 1;
659  } else {
660  return 0;
661  }
662  } else {
663  $this->error = $this->db->lasterror();
664  return -1;
665  }
666  }
667 
668 
669  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
682  public function list_product_fournisseur_price($prodid, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0, $socid = 0)
683  {
684  // phpcs:enable
685  global $conf;
686 
687  $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, ";
688  $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,";
689  $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,";
690  $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code, pfp.datec, pfp.tms,";
691  $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging, pfp.status as pfstatus";
692  $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."societe as s";
693  $sql .= " WHERE pfp.entity IN (".getEntity('productsupplierprice').")";
694  $sql .= " AND pfp.fk_soc = s.rowid AND pfp.fk_product = p.rowid";
695  $sql .= ($socid > 0 ? ' AND pfp.fk_soc = '.((int) $socid) : '');
696  $sql .= " AND s.status = 1"; // only enabled company selected
697  $sql .= " AND pfp.fk_product = ".((int) $prodid);
698  if (empty($sortfield)) {
699  $sql .= " ORDER BY s.nom, pfp.quantity, pfp.price";
700  } else {
701  $sql .= $this->db->order($sortfield, $sortorder);
702  }
703  $sql .= $this->db->plimit($limit, $offset);
704  dol_syslog(get_class($this)."::list_product_fournisseur_price", LOG_DEBUG);
705 
706  $resql = $this->db->query($sql);
707  if ($resql) {
708  $retarray = array();
709 
710  while ($record = $this->db->fetch_array($resql)) {
711  //define base attribute
712  $prodfourn = new ProductFournisseur($this->db);
713 
714  $prodfourn->product_ref = $record["product_ref"];
715  $prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"];
716  $prodfourn->status = $record["status"];
717  $prodfourn->status_buy = $record["status_buy"];
718  $prodfourn->product_fourn_id = $record["product_fourn_id"];
719  $prodfourn->product_fourn_entity = $record["entity"];
720  $prodfourn->ref_supplier = $record["ref_fourn"];
721  $prodfourn->fourn_ref = $record["ref_fourn"];
722  $prodfourn->desc_supplier = $record["desc_fourn"];
723  $prodfourn->fourn_price = $record["price"];
724  $prodfourn->fourn_qty = $record["quantity"];
725  $prodfourn->fourn_remise_percent = $record["remise_percent"];
726  $prodfourn->fourn_remise = $record["remise"];
727  $prodfourn->fourn_unitprice = $record["unitprice"];
728  $prodfourn->fourn_charges = $record["charges"]; // deprecated
729  $prodfourn->fourn_tva_tx = $record["tva_tx"];
730  $prodfourn->fourn_id = $record["fourn_id"];
731  $prodfourn->fourn_name = $record["supplier_name"];
732  $prodfourn->fk_availability = $record["fk_availability"];
733  $prodfourn->delivery_time_days = $record["delivery_time_days"];
734  $prodfourn->id = $prodid;
735  $prodfourn->fourn_tva_npr = $record["info_bits"];
736  $prodfourn->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
737  $prodfourn->supplier_reputation = $record["supplier_reputation"];
738  $prodfourn->fourn_date_creation = $this->db->jdate($record['datec']);
739  $prodfourn->fourn_date_modification = $this->db->jdate($record['tms']);
740 
741  $prodfourn->fourn_multicurrency_price = $record["multicurrency_price"];
742  $prodfourn->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
743  $prodfourn->fourn_multicurrency_tx = $record["multicurrency_tx"];
744  $prodfourn->fourn_multicurrency_id = $record["fk_multicurrency"];
745  $prodfourn->fourn_multicurrency_code = $record["multicurrency_code"];
746 
747  $prodfourn->packaging = $record["packaging"];
748  $prodfourn->status = $record["pfstatus"];
749 
750  if (isModEnabled('barcode')) {
751  $prodfourn->supplier_barcode = $record["barcode"];
752  $prodfourn->supplier_fk_barcode_type = $record["fk_barcode_type"];
753  }
754 
755  if (isModEnabled('dynamicprices') && !empty($prodfourn->fk_supplier_price_expression)) {
756  require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
757  $priceparser = new PriceParser($this->db);
758  $price_result = $priceparser->parseProductSupplier($prodfourn);
759  if ($price_result >= 0) {
760  $prodfourn->fourn_price = $price_result;
761  $prodfourn->fourn_unitprice = null; //force recalculation of unitprice, as probably the price changed...
762  }
763  }
764 
765  if (!isset($prodfourn->fourn_unitprice)) {
766  if ($prodfourn->fourn_qty != 0) {
767  $prodfourn->fourn_unitprice = price2num($prodfourn->fourn_price / $prodfourn->fourn_qty, 'MU');
768  } else {
769  $prodfourn->fourn_unitprice = "";
770  }
771  }
772 
773  $retarray[] = $prodfourn;
774  }
775 
776  $this->db->free($resql);
777  return $retarray;
778  } else {
779  $this->error = $this->db->error();
780  return -1;
781  }
782  }
783 
784  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
794  public function find_min_price_product_fournisseur($prodid, $qty = 0, $socid = 0)
795  {
796  // phpcs:enable
797  global $conf;
798 
799  if (empty($prodid)) {
800  dol_syslog("Warning function find_min_price_product_fournisseur were called with prodid empty. May be a bug.", LOG_WARNING);
801  return 0;
802  }
803 
804  $this->product_fourn_price_id = '';
805  $this->product_fourn_id = '';
806  $this->fourn_ref = '';
807  $this->fourn_price = '';
808  $this->fourn_qty = '';
809  $this->fourn_remise_percent = '';
810  $this->fourn_remise = '';
811  $this->fourn_unitprice = '';
812  $this->fourn_id = '';
813  $this->fourn_name = '';
814  $this->delivery_time_days = '';
815  $this->id = '';
816 
817  $this->fourn_multicurrency_price = '';
818  $this->fourn_multicurrency_unitprice = '';
819  $this->fourn_multicurrency_tx = '';
820  $this->fourn_multicurrency_id = '';
821  $this->fourn_multicurrency_code = '';
822 
823  $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
824  $sql .= " pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
825  $sql .= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges,";
826  $sql .= " pfp.remise, pfp.remise_percent, pfp.fk_supplier_price_expression, pfp.delivery_time_days";
827  $sql .= " ,pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code";
828  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
829  $sql .= " WHERE s.entity IN (".getEntity('societe').")";
830  $sql .= " AND pfp.entity IN (".getEntity('productsupplierprice').")";
831  $sql .= " AND pfp.fk_product = ".((int) $prodid);
832  $sql .= " AND pfp.fk_soc = s.rowid";
833  $sql .= " AND s.status = 1"; // only enabled society
834  if ($qty > 0) {
835  $sql .= " AND pfp.quantity <= ".((float) $qty);
836  }
837  if ($socid > 0) {
838  $sql .= ' AND pfp.fk_soc = '.((int) $socid);
839  }
840 
841  dol_syslog(get_class($this)."::find_min_price_product_fournisseur", LOG_DEBUG);
842 
843  $resql = $this->db->query($sql);
844  if ($resql) {
845  $record_array = array();
846 
847  //Store each record to array for later search of min
848  while ($record = $this->db->fetch_array($resql)) {
849  $record_array[] = $record;
850  }
851 
852  if (count($record_array) == 0) {
853  $this->db->free($resql);
854  return 0;
855  } else {
856  $min = -1;
857  foreach ($record_array as $record) {
858  $fourn_price = $record["price"];
859  // calculate unit price for quantity 1
860  $fourn_unitprice = $record["unitprice"];
861  $fourn_unitprice_with_discount = $record["unitprice"] * (1 - $record["remise_percent"] / 100);
862 
863  if (isModEnabled('dynamicprices') && !empty($record["fk_supplier_price_expression"])) {
864  $prod_supplier = new ProductFournisseur($this->db);
865  $prod_supplier->product_fourn_price_id = $record["product_fourn_price_id"];
866  $prod_supplier->id = $prodid;
867  $prod_supplier->fourn_qty = $record["quantity"];
868  $prod_supplier->fourn_tva_tx = $record["tva_tx"];
869  $prod_supplier->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
870 
871  require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
872  $priceparser = new PriceParser($this->db);
873  $price_result = $priceparser->parseProductSupplier($prod_supplier);
874  if ($price_result >= 0) {
875  $fourn_price = price2num($price_result, 'MU');
876  if ($record["quantity"] != 0) {
877  $fourn_unitprice = price2num($fourn_price / $record["quantity"], 'MU');
878  } else {
879  $fourn_unitprice = $fourn_price;
880  }
881  $fourn_unitprice_with_discount = $fourn_unitprice * (1 - $record["remise_percent"] / 100);
882  }
883  }
884  if ($fourn_unitprice < $min || $min == -1) {
885  $this->product_fourn_price_id = $record["product_fourn_price_id"];
886  $this->ref_supplier = $record["ref_fourn"];
887  $this->ref_fourn = $record["ref_fourn"]; // deprecated
888  $this->fourn_ref = $record["ref_fourn"]; // deprecated
889  $this->fourn_price = $fourn_price;
890  $this->fourn_qty = $record["quantity"];
891  $this->fourn_remise_percent = $record["remise_percent"];
892  $this->fourn_remise = $record["remise"];
893  $this->fourn_unitprice = $fourn_unitprice;
894  $this->fourn_unitprice_with_discount = $fourn_unitprice_with_discount;
895  $this->fourn_charges = $record["charges"]; // deprecated
896  $this->fourn_tva_tx = $record["tva_tx"];
897  $this->fourn_id = $record["fourn_id"];
898  $this->fourn_name = $record["supplier_name"];
899  $this->delivery_time_days = $record["delivery_time_days"];
900  $this->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
901  $this->id = $prodid;
902  $this->fourn_multicurrency_price = $record["multicurrency_price"];
903  $this->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
904  $this->fourn_multicurrency_tx = $record["multicurrency_tx"];
905  $this->fourn_multicurrency_id = $record["fk_multicurrency"];
906  $this->fourn_multicurrency_code = $record["multicurrency_code"];
907  $min = $fourn_unitprice;
908  }
909  }
910  }
911 
912  $this->db->free($resql);
913  return 1;
914  } else {
915  $this->error = $this->db->error();
916  return -1;
917  }
918  }
919 
926  public function setSupplierPriceExpression($expression_id)
927  {
928  global $conf;
929 
930  // Clean parameters
931  $this->db->begin();
932  $expression_id = $expression_id != 0 ? $expression_id : 'NULL';
933 
934  $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
935  $sql .= " SET fk_supplier_price_expression = ".((int) $expression_id);
936  $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
937 
938  dol_syslog(get_class($this)."::setSupplierPriceExpression", LOG_DEBUG);
939 
940  $resql = $this->db->query($sql);
941  if ($resql) {
942  $this->db->commit();
943  return 1;
944  } else {
945  $this->error = $this->db->error()." sql=".$sql;
946  $this->db->rollback();
947  return -1;
948  }
949  }
950 
961  public function getSocNomUrl($withpicto = 0, $option = 'supplier', $maxlen = 0, $notooltip = 0)
962  {
963  $thirdparty = new Fournisseur($this->db);
964  $thirdparty->fetch($this->fourn_id);
965 
966  return $thirdparty->getNomUrl($withpicto, $option, $maxlen, $notooltip);
967  }
968 
969  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
981  public function display_price_product_fournisseur($showunitprice = 1, $showsuptitle = 1, $maxlen = 0, $notooltip = 0, $productFournList = array())
982  {
983  // phpcs:enable
984  global $conf, $langs;
985 
986  $out = '';
987  $langs->load("suppliers");
988  if (count($productFournList) > 0) {
989  $out .= '<table class="nobordernopadding" width="100%">';
990  $out .= '<tr><td class="liste_titre right">'.($showunitprice ? $langs->trans("Price").' '.$langs->trans("HT") : '').'</td>';
991  $out .= '<td class="liste_titre right">'.($showunitprice ? $langs->trans("QtyMin") : '').'</td>';
992  $out .= '<td class="liste_titre">'.$langs->trans("Supplier").'</td>';
993  $out .= '<td class="liste_titre">'.$langs->trans("SupplierRef").'</td></tr>';
994  foreach ($productFournList as $productFourn) {
995  $out .= '<tr><td class="right">'.($showunitprice ?price($productFourn->fourn_unitprice * (1 - $productFourn->fourn_remise_percent / 100) - $productFourn->fourn_remise) : '').'</td>';
996  $out .= '<td class="right">'.($showunitprice ? $productFourn->fourn_qty : '').'</td>';
997  $out .= '<td>'.$productFourn->getSocNomUrl(1, 'supplier', $maxlen, $notooltip).'</td>';
998  $out .= '<td>'.$productFourn->fourn_ref.'<td></tr>';
999  }
1000  $out .= '</table>';
1001  } else {
1002  $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>' : '');
1003  $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;
1004  $out .= ($showunitprice ? '<span class="opacitymedium">)</span>' : '');
1005  }
1006  return $out;
1007  }
1008 
1017  public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1018  {
1019  $tables = array(
1020  'product_fournisseur_price'
1021  );
1022 
1023  return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1024  }
1025 
1034  public static function replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
1035  {
1036  $tables = array(
1037  'product_fournisseur_price'
1038  );
1039 
1040  return CommonObject::commonReplaceProduct($dbs, $origin_id, $dest_id, $tables);
1041  }
1042 
1053  public function listProductFournisseurPriceLog($product_fourn_price_id, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0)
1054  {
1055  $sql = "SELECT";
1056  $sql .= " u.lastname,";
1057  $sql .= " pfpl.rowid, pfp.ref_fourn as supplier_ref, pfpl.datec,";
1058  $sql .= " pfpl.price, pfpl.quantity,";
1059  $sql .= " pfpl.fk_multicurrency, pfpl.multicurrency_code, pfpl.multicurrency_tx, pfpl.multicurrency_price, pfpl.multicurrency_unitprice";
1060  $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price_log as pfpl,";
1061  $sql .= " ".MAIN_DB_PREFIX."product_fournisseur_price as pfp,";
1062  $sql .= " ".MAIN_DB_PREFIX."user as u";
1063  $sql .= " WHERE pfp.entity IN (".getEntity('productprice').")";
1064  $sql .= " AND pfpl.fk_user = u.rowid";
1065  $sql .= " AND pfp.rowid = pfpl.fk_product_fournisseur";
1066  $sql .= " AND pfpl.fk_product_fournisseur = ".((int) $product_fourn_price_id);
1067  if (empty($sortfield)) {
1068  $sql .= " ORDER BY pfpl.datec";
1069  } else {
1070  $sql .= $this->db->order($sortfield, $sortorder);
1071  }
1072  $sql .= $this->db->plimit($limit, $offset);
1073  dol_syslog(get_class($this)."::list_product_fournisseur_price_log", LOG_DEBUG);
1074 
1075  $resql = $this->db->query($sql);
1076  if ($resql) {
1077  $retarray = array();
1078 
1079  while ($obj = $this->db->fetch_object($resql)) {
1080  $tmparray = array();
1081  $tmparray['rowid'] = $obj->rowid;
1082  $tmparray['supplier_ref'] = $obj->supplier_ref;
1083  $tmparray['datec'] = $this->db->jdate($obj->datec);
1084  $tmparray['lastname'] = $obj->lastname;
1085  $tmparray['price'] = $obj->price;
1086  $tmparray['quantity'] = $obj->quantity;
1087  $tmparray['fk_multicurrency'] = $obj->fk_multicurrency;
1088  $tmparray['multicurrency_code'] = $obj->multicurrency_code;
1089  $tmparray['multicurrency_tx'] = $obj->multicurrency_tx;
1090  $tmparray['multicurrency_price'] = $obj->multicurrency_price;
1091  $tmparray['multicurrency_unitprice'] = $obj->multicurrency_unitprice;
1092 
1093  $retarray[] = $tmparray;
1094  }
1095 
1096  $this->db->free($resql);
1097  return $retarray;
1098  } else {
1099  $this->error = $this->db->error();
1100  return -1;
1101  }
1102  }
1103 
1111  public function displayPriceProductFournisseurLog($productFournLogList = array())
1112  {
1113  global $conf, $langs;
1114 
1115  $out = '';
1116  $langs->load("suppliers");
1117  if (count($productFournLogList) > 0) {
1118  $out .= '<table class="noborder centpercent">';
1119  $out .= '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Date").'</td>';
1120  $out .= '<td class="liste_titre right">'.$langs->trans("Price").'</td>';
1121  //$out .= '<td class="liste_titre right">'.$langs->trans("QtyMin").'</td>';
1122  $out .= '<td class="liste_titre">'.$langs->trans("User").'</td></tr>';
1123  foreach ($productFournLogList as $productFournLog) {
1124  $out .= '<tr><td>'.dol_print_date($productFournLog['datec'], 'dayhour', 'tzuser').'</td>';
1125  $out .= '<td class="right">'.price($productFournLog['price'], 0, $langs, 1, -1, -1, $conf->currency);
1126  if ($productFournLog['multicurrency_code'] != $conf->currency) {
1127  $out .= ' ('.price($productFournLog['multicurrency_price'], 0, $langs, 1, -1, -1, $productFournLog['multicurrency_code']).')';
1128  }
1129  $out .= '</td>';
1130  //$out.= '<td class="right">'.$productFournLog['quantity'].'</td>';
1131  $out .= '<td>'.$productFournLog['lastname'].'</td></tr>';
1132  }
1133  $out .= '</table>';
1134  }
1135  return $out;
1136  }
1137 
1138 
1153  public function getNomUrl($withpicto = 0, $option = '', $maxlength = 0, $save_lastsearch_value = -1, $notooltip = 0, $morecss = '', $add_label = 0, $sep = ' - ')
1154  {
1155  global $db, $conf, $langs, $hookmanager;
1156 
1157  if (!empty($conf->dol_no_mouse_hover)) {
1158  $notooltip = 1; // Force disable tooltips
1159  }
1160 
1161  $result = '';
1162  $label = '';
1163 
1164  $newref = $this->ref;
1165  if ($maxlength) {
1166  $newref = dol_trunc($newref, $maxlength, 'middle');
1167  }
1168 
1169  if (!empty($this->entity)) {
1170  $tmpphoto = $this->show_photos('product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 80);
1171  if ($this->nbphoto > 0) {
1172  $label .= '<div class="photointooltip">';
1173  $label .= $tmpphoto;
1174  $label .= '</div><div style="clear: both;"></div>';
1175  }
1176  }
1177 
1178  if ($this->type == Product::TYPE_PRODUCT) {
1179  $label .= img_picto('', 'product').' <u class="paddingrightonly">'.$langs->trans("Product").'</u>';
1180  } elseif ($this->type == Product::TYPE_SERVICE) {
1181  $label .= img_picto('', 'service').' <u class="paddingrightonly">'.$langs->trans("Service").'</u>';
1182  }
1183  if (isset($this->status) && isset($this->status_buy)) {
1184  $label .= ' '.$this->getLibStatut(5, 0);
1185  $label .= ' '.$this->getLibStatut(5, 1);
1186  }
1187 
1188  if (!empty($this->ref)) {
1189  $label .= '<br><b>'.$langs->trans('ProductRef').':</b> '.($this->ref ? $this->ref : $this->product_ref);
1190  }
1191  if (!empty($this->label)) {
1192  $label .= '<br><b>'.$langs->trans('ProductLabel').':</b> '.$this->label;
1193  }
1194  $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
1195 
1196  if ($this->type == Product::TYPE_PRODUCT || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
1197  if (isModEnabled('productbatch')) {
1198  $langs->load("productbatch");
1199  $label .= "<br><b>".$langs->trans("ManageLotSerial").'</b>: '.$this->getLibStatut(0, 2);
1200  }
1201  }
1202  if (isModEnabled('barcode')) {
1203  $label .= '<br><b>'.$langs->trans('BarCode').':</b> '.$this->barcode;
1204  }
1205 
1206  if ($this->type == Product::TYPE_PRODUCT) {
1207  if ($this->weight) {
1208  $label .= "<br><b>".$langs->trans("Weight").'</b>: '.$this->weight.' '.measuringUnitString(0, "weight", $this->weight_units);
1209  }
1210  $labelsize = "";
1211  if ($this->length) {
1212  $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Length").'</b>: '.$this->length.' '.measuringUnitString(0, 'size', $this->length_units);
1213  }
1214  if ($this->width) {
1215  $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Width").'</b>: '.$this->width.' '.measuringUnitString(0, 'size', $this->width_units);
1216  }
1217  if ($this->height) {
1218  $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Height").'</b>: '.$this->height.' '.measuringUnitString(0, 'size', $this->height_units);
1219  }
1220  if ($labelsize) {
1221  $label .= "<br>".$labelsize;
1222  }
1223 
1224  $labelsurfacevolume = "";
1225  if ($this->surface) {
1226  $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Surface").'</b>: '.$this->surface.' '.measuringUnitString(0, 'surface', $this->surface_units);
1227  }
1228  if ($this->volume) {
1229  $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Volume").'</b>: '.$this->volume.' '.measuringUnitString(0, 'volume', $this->volume_units);
1230  }
1231  if ($labelsurfacevolume) {
1232  $label .= "<br>".$labelsurfacevolume;
1233  }
1234  }
1235 
1236  if (isModEnabled('accounting') && $this->status) {
1237  include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1238  $label .= '<br><b>'.$langs->trans('ProductAccountancySellCode').':</b> '.length_accountg($this->accountancy_code_sell);
1239  $label .= '<br><b>'.$langs->trans('ProductAccountancySellIntraCode').':</b> '.length_accountg($this->accountancy_code_sell_intra);
1240  $label .= '<br><b>'.$langs->trans('ProductAccountancySellExportCode').':</b> '.length_accountg($this->accountancy_code_sell_export);
1241  }
1242  if (isModEnabled('accounting') && $this->status_buy) {
1243  include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1244  $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyCode').':</b> '.length_accountg($this->accountancy_code_buy);
1245  $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyIntraCode').':</b> '.length_accountg($this->accountancy_code_buy_intra);
1246  $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyExportCode').':</b> '.length_accountg($this->accountancy_code_buy_export);
1247  }
1248 
1249  $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id, 'pfpl.datec', 'DESC'); // set sort order here
1250  if (is_array($logPrices) && count($logPrices) > 0) {
1251  $label .= '<br><br>';
1252  $label .= '<u>'.$langs->trans("History").'</u>';
1253  $label .= $this->displayPriceProductFournisseurLog($logPrices);
1254  }
1255 
1256  $url = DOL_URL_ROOT.'/product/fournisseurs.php?id='.((int) $this->id).'&action=create_price&token='.newToken().'&socid='.((int) $this->fourn_id).'&rowid='.((int) $this->product_fourn_price_id);
1257 
1258  if ($option != 'nolink') {
1259  // Add param to save lastsearch_values or not
1260  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1261  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1262  $add_save_lastsearch_values = 1;
1263  }
1264  if ($add_save_lastsearch_values) {
1265  $url .= '&save_lastsearch_values=1';
1266  }
1267  }
1268 
1269  $linkclose = '';
1270  if (empty($notooltip)) {
1271  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1272  $label = $langs->trans("SupplierRef");
1273  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1274  }
1275  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
1276  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
1277  } else {
1278  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1279  }
1280 
1281  $linkstart = '<a href="'.$url.'"';
1282  $linkstart .= $linkclose.'>';
1283  $linkend = '</a>';
1284 
1285  $result .= $linkstart;
1286  if ($withpicto) {
1287  $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);
1288  }
1289  if ($withpicto != 2) {
1290  $result .= $newref.($this->ref_supplier ? ' ('.$this->ref_supplier.')' : '');
1291  }
1292  $result .= $linkend;
1293  if ($withpicto != 2) {
1294  $result .= (($add_label && $this->label) ? $sep.dol_trunc($this->label, ($add_label > 1 ? $add_label : 0)) : '');
1295  }
1296 
1297  global $action;
1298  $hookmanager->initHooks(array($this->element . 'dao'));
1299  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
1300  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1301  if ($reshook > 0) {
1302  $result = $hookmanager->resPrint;
1303  } else {
1304  $result .= $hookmanager->resPrint;
1305  }
1306  return $result;
1307  }
1308 
1316  public function getLibStatut($mode = 0, $type = 0) // must be compatible with getLibStatut of inherited Product
1317  {
1318  return $this->LibStatut($this->status, $mode);
1319  }
1320 
1321  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1330  public function LibStatut($status, $mode = 0, $type = 0)
1331  {
1332  // phpcs:enable
1333  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1334  global $langs;
1335  //$langs->load("mymodule@mymodule");
1336  $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Enabled');
1337  $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
1338  }
1339 
1340  $statusType = 'status4';
1341  //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
1342  if ($status == self::STATUS_CANCELED) {
1343  $statusType = 'status6';
1344  }
1345 
1346  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
1347  }
1348 
1364  private function logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice = null, $multicurrency_unitBuyPrice = null, $multicurrency_tx = null, $fk_multicurrency = null, $multicurrency_code = null)
1365  {
1366  // Add record into log table
1367  $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log(";
1368  $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
1369  $sql .= "datec, fk_product_fournisseur,fk_user,price,quantity)";
1370  $sql .= "values(";
1371  $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
1372  $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
1373  $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
1374  $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
1375  $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
1376  $sql .= "'".$this->db->idate($datec)."',";
1377  $sql .= " ".((int) $this->product_fourn_price_id).",";
1378  $sql .= " ".$user->id.",";
1379  $sql .= " ".price2num($buyprice).",";
1380  $sql .= " ".price2num($qty);
1381  $sql .= ")";
1382 
1383  $resql = $this->db->query($sql);
1384  if (!$resql) {
1385  return -1;
1386  } else {
1387  return 1;
1388  }
1389  }
1390 }
Product\$ref_fourn
$ref_fourn
Definition: product.class.php:453
dol_trunc
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.
Definition: functions.lib.php:4049
ProductFournisseur\$fourn_fk_barcode_type
$fourn_fk_barcode_type
Definition: fournisseur.product.class.php:129
ProductFournisseur\remove_product_fournisseur_price
remove_product_fournisseur_price($rowid)
Remove a price for a couple supplier-product.
Definition: fournisseur.product.class.php:197
ProductFournisseur\update_buyprice
update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx, $charges=0, $remise_percent=0, $remise=0, $newnpr=0, $delivery_time_days=0, $supplier_reputation='', $localtaxes_array=array(), $newdefaultvatcode='', $multicurrency_buyprice=0, $multicurrency_price_base_type='HT', $multicurrency_tx=1, $multicurrency_code='', $desc_fourn='', $barcode='', $fk_barcode_type='', $options=array())
Modify the purchase price for a supplier.
Definition: fournisseur.product.class.php:265
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
ProductFournisseur
Class to manage predefined suppliers products.
Definition: fournisseur.product.class.php:40
ProductFournisseur\replaceProduct
static replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a product id with another one.
Definition: fournisseur.product.class.php:1034
ProductFournisseur\fetch_product_fournisseur_price
fetch_product_fournisseur_price($rowid, $ignore_expression=0)
Loads the price information of a provider.
Definition: fournisseur.product.class.php:582
ProductFournisseur\LibStatut
LibStatut($status, $mode=0, $type=0)
Return the status.
Definition: fournisseur.product.class.php:1330
MultiCurrency\getIdFromCode
static getIdFromCode($dbs, $code)
Get id of currency from code.
Definition: multicurrency.class.php:503
ProductFournisseurPrice
Class for ProductFournisseurPrice.
Definition: productfournisseurprice.class.php:31
ProductFournisseur\getSocNomUrl
getSocNomUrl($withpicto=0, $option='supplier', $maxlen=0, $notooltip=0)
Display supplier of product.
Definition: fournisseur.product.class.php:961
ProductFournisseur\logPrice
logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice=null, $multicurrency_unitBuyPrice=null, $multicurrency_tx=null, $fk_multicurrency=null, $multicurrency_code=null)
Private function to log price history.
Definition: fournisseur.product.class.php:1364
ProductFournisseur\setSupplierPriceExpression
setSupplierPriceExpression($expression_id)
Sets the supplier price expression.
Definition: fournisseur.product.class.php:926
CommonObject\show_photos
show_photos($modulepart, $sdir, $size=0, $nbmax=0, $nbbyrow=5, $showfilename=0, $showaction=0, $maxHeight=120, $maxWidth=160, $nolink=0, $overwritetitle=0, $usesharelink=0, $cache='', $addphotorefcss='photoref')
Show photos of an object (nbmax maximum), into several columns.
Definition: commonobject.class.php:8600
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5943
measuringUnitString
measuringUnitString($unit, $measuring_style='', $scale='', $use_short_label=0, $outputlangs=null)
Return translation label of a unit key.
Definition: product.lib.php:805
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:4125
ProductFournisseur\replaceThirdparty
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: fournisseur.product.class.php:1017
PriceParser
Class to parse product price expressions.
Definition: price_parser.class.php:33
ProductFournisseur\listProductFournisseurPriceLog
listProductFournisseurPriceLog($product_fourn_price_id, $sortfield='', $sortorder='', $limit=0, $offset=0)
List supplier prices log of a supplier price.
Definition: fournisseur.product.class.php:1053
ProductFournisseur\$fourn_ref
$fourn_ref
Definition: fournisseur.product.class.php:63
get_localtax
get_localtax($vatrate, $local, $thirdparty_buyer="", $thirdparty_seller="", $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate,...
Definition: functions.lib.php:6124
Fournisseur
Class to manage suppliers.
Definition: fournisseur.class.php:34
Product\$remise_percent
$remise_percent
Default discount percent.
Definition: product.class.php:200
$sql
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') && $user->hasRight('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)) $sql
Social contributions to pay.
Definition: index.php:746
length_accountg
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
Definition: accounting.lib.php:94
ProductFournisseur\displayPriceProductFournisseurLog
displayPriceProductFournisseurLog($productFournLogList=array())
Display log price of product supplier price.
Definition: fournisseur.product.class.php:1111
CommonObject\commonReplaceProduct
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
Definition: commonobject.class.php:8498
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1732
ProductFournisseur\__construct
__construct($db)
Constructor.
Definition: fournisseur.product.class.php:147
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:11620
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:207
ProductFournisseur\list_product_fournisseur_price
list_product_fournisseur_price($prodid, $sortfield='', $sortorder='', $limit=0, $offset=0, $socid=0)
List all supplier prices of a product.
Definition: fournisseur.product.class.php:682
ref
$object ref
Definition: info.php:78
User
Class to manage Dolibarr users.
Definition: user.class.php:47
ProductFournisseur\display_price_product_fournisseur
display_price_product_fournisseur($showunitprice=1, $showsuptitle=1, $maxlen=0, $notooltip=0, $productFournList=array())
Display price of product.
Definition: fournisseur.product.class.php:981
Product
Class to manage products or services.
Definition: product.class.php:46
Product\$tva_tx
$tva_tx
Default VAT rate of product.
Definition: product.class.php:194
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10932
ProductFournisseur\getNomUrl
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).
Definition: fournisseur.product.class.php:1153
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4462
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:3046
ProductFournisseur\remove_fournisseur
remove_fournisseur($id_fourn)
Remove all prices for this couple supplier-product.
Definition: fournisseur.product.class.php:163
price
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
Definition: functions.lib.php:5817
Product\TYPE_SERVICE
const TYPE_SERVICE
Service.
Definition: product.class.php:556
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5743
Product\TYPE_PRODUCT
const TYPE_PRODUCT
Regular product.
Definition: product.class.php:552
ProductFournisseur\getLibStatut
getLibStatut($mode=0, $type=0)
Return the label of the status.
Definition: fournisseur.product.class.php:1316
CommonObject\commonReplaceThirdparty
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
Definition: commonobject.class.php:8469
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:120
ProductFournisseur\find_min_price_product_fournisseur
find_min_price_product_fournisseur($prodid, $qty=0, $socid=0)
Load properties for minimum price.
Definition: fournisseur.product.class.php:794
ProductFournisseur\$fourn_barcode
$fourn_barcode
Definition: fournisseur.product.class.php:118
float
div float
Buy price without taxes.
Definition: style.css.php:921