dolibarr 18.0.6
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-2023 Charlene Benke <charlene@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
32require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
33require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
34require_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_name; // supplier name
75 public $fourn_qty; // quantity for price (can be set by get_buyprice)
76 public $fourn_pu; // unit price for quantity (can be set by get_buyprice)
77
78 public $fourn_price; // price for quantity
79 public $fourn_remise_percent; // discount for quantity (percent)
80 public $fourn_remise; // discount for quantity (amount)
81
82 public $fourn_charges; // when getDolGlobalString('PRODUCT_CHARGES') is set
83
84 public $product_fourn_id; // product-supplier id
85 public $product_fourn_entity;
86
90 public $user_id;
91
95 public $fk_availability;
96
97 public $fourn_unitprice;
98 public $fourn_unitprice_with_discount; // not saved into database
99 public $fourn_tva_tx;
100 public $fourn_tva_npr;
101
105 public $fk_supplier_price_expression;
106
107 public $supplier_reputation; // reputation of supplier
108 public $reputations = array(); // list of available supplier reputations
109
110 // Multicurreny
111 public $fourn_multicurrency_id;
112 public $fourn_multicurrency_code;
113 public $fourn_multicurrency_tx;
114 public $fourn_multicurrency_price;
115 public $fourn_multicurrency_unitprice;
116
122
126 public $supplier_barcode;
127
133
137 public $supplier_fk_barcode_type;
138
139 public $packaging;
140
141 public $labelStatusShort;
142 public $labelStatus;
143
144 const STATUS_OPEN = 1;
145 const STATUS_CANCELED = 0;
146
147
153 public function __construct($db)
154 {
155 global $langs;
156
157 $this->db = $db;
158 $langs->load("suppliers");
159 $this->reputations = array('-1'=>'', 'FAVORITE'=>$langs->trans('Favorite'), 'NOTTHGOOD'=>$langs->trans('NotTheGoodQualitySupplier'), 'DONOTORDER'=>$langs->trans('DoNotOrderThisProductToThisSupplier'));
160 }
161
162 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
169 public function remove_fournisseur($id_fourn)
170 {
171 // phpcs:enable
172 $ok = 1;
173
174 $this->db->begin();
175
176 $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
177 $sql .= " WHERE fk_product = ".((int) $this->id)." AND fk_soc = ".((int) $id_fourn);
178
179 dol_syslog(get_class($this)."::remove_fournisseur", LOG_DEBUG);
180 $resql2 = $this->db->query($sql);
181 if (!$resql2) {
182 $this->error = $this->db->lasterror();
183 $ok = 0;
184 }
185
186 if ($ok) {
187 $this->db->commit();
188 return 1;
189 } else {
190 $this->db->rollback();
191 return -1;
192 }
193 }
194
195
196 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
203 public function remove_product_fournisseur_price($rowid)
204 {
205 // phpcs:enable
206 global $conf, $user;
207
208 $error = 0;
209
210 $this->db->begin();
211
212 // Call trigger
213 $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_DELETE', $user);
214 if ($result < 0) {
215 $error++;
216 }
217 // End call triggers
218
219 if (empty($error)) {
220 $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
221 $sql .= " WHERE rowid = ".((int) $rowid);
222
223 dol_syslog(get_class($this)."::remove_product_fournisseur_price", LOG_DEBUG);
224 $resql = $this->db->query($sql);
225 if (!$resql) {
226 $this->error = $this->db->lasterror();
227 $error++;
228 }
229 }
230
231 if (empty($error)) {
232 $this->db->commit();
233 return 1;
234 } else {
235 $this->db->rollback();
236 return -1;
237 }
238 }
239
240
241 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
271 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())
272 {
273 // phpcs:enable
274 global $conf, $langs;
275 //global $mysoc;
276
277 // Clean parameter
278 if (empty($qty)) {
279 $qty = 0;
280 }
281 if (empty($buyprice)) {
282 $buyprice = 0;
283 }
284 if (empty($charges)) {
285 $charges = 0;
286 }
287 if (empty($availability)) {
288 $availability = 0;
289 }
290 if (empty($remise_percent)) {
291 $remise_percent = 0;
292 }
293 if (empty($supplier_reputation) || $supplier_reputation == -1) {
294 $supplier_reputation = '';
295 }
296 if ($delivery_time_days != '' && !is_numeric($delivery_time_days)) {
297 $delivery_time_days = '';
298 }
299 if ($price_base_type == 'TTC') {
300 $ttx = $tva_tx;
301 $buyprice = $buyprice / (1 + ($ttx / 100));
302 }
303
304 // Multicurrency
305 $multicurrency_unitBuyPrice = null;
306 $fk_multicurrency = null;
307 if (isModEnabled("multicurrency")) {
308 if (empty($multicurrency_tx)) {
309 $multicurrency_tx = 1;
310 }
311 if (empty($multicurrency_buyprice)) {
312 $multicurrency_buyprice = 0;
313 }
314 if ($multicurrency_price_base_type == 'TTC') {
315 $ttx = $tva_tx;
316 $multicurrency_buyprice = $multicurrency_buyprice / (1 + ($ttx / 100));
317 }
318 $multicurrency_buyprice = price2num($multicurrency_buyprice, 'MU');
319 $multicurrency_unitBuyPrice = price2num($multicurrency_buyprice / $qty, 'MU');
320
321 $buyprice = $multicurrency_buyprice / $multicurrency_tx;
322 $fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $multicurrency_code);
323 }
324
325 $buyprice = price2num($buyprice, 'MU');
326 $charges = price2num($charges, 'MU');
327 $qty = price2num($qty, 'MS');
328 $unitBuyPrice = price2num($buyprice / $qty, 'MU');
329
330 // We can have a puchase ref that need to buy 100 min for a given price and with a packaging of 50.
331 //$packaging = price2num(((empty($this->packaging) || $this->packaging < $qty) ? $qty : $this->packaging), 'MS');
332 $packaging = price2num((empty($this->packaging) ? $qty : $this->packaging), 'MS');
333
334 $error = 0;
335 $now = dol_now();
336
337 $newvat = $tva_tx;
338
339 if (count($localtaxes_array) > 0) {
340 $localtaxtype1 = $localtaxes_array['0'];
341 $localtax1 = $localtaxes_array['1'];
342 $localtaxtype2 = $localtaxes_array['2'];
343 $localtax2 = $localtaxes_array['3'];
344 } else { // old method. deprecated because ot can't retrieve type
345 $localtaxtype1 = '0';
346 $localtax1 = get_localtax($newvat, 1);
347 $localtaxtype2 = '0';
348 $localtax2 = get_localtax($newvat, 2);
349 }
350 if (empty($localtax1)) {
351 $localtax1 = 0; // If = '' then = 0
352 }
353 if (empty($localtax2)) {
354 $localtax2 = 0; // If = '' then = 0
355 }
356
357 // Check parameters
358 if ($buyprice != '' && !is_numeric($buyprice)) {
359 }
360
361 $this->db->begin();
362
363 if ($this->product_fourn_price_id > 0) {
364 // check if price already logged, if not first log current price
365 $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id);
366 if (is_array($logPrices) && count($logPrices) == 0) {
367 $currentPfp = new self($this->db);
368 $result = $currentPfp->fetch_product_fournisseur_price($this->product_fourn_price_id);
369 if ($result > 0 && $currentPfp->fourn_price != 0) {
370 $currentPfpUser = new User($this->db);
371 $result = $currentPfpUser->fetch($currentPfp->user_id);
372 if ($result > 0) {
373 $currentPfp->logPrice(
374 $currentPfpUser,
375 $currentPfp->date_creation,
376 $currentPfp->fourn_price,
377 $currentPfp->fourn_qty,
378 $currentPfp->fourn_multicurrency_price,
379 $currentPfp->fourn_multicurrency_unitprice,
380 $currentPfp->fourn_multicurrency_tx,
381 $currentPfp->fourn_multicurrency_id,
382 $currentPfp->fourn_multicurrency_code
383 );
384 }
385 }
386 }
387 $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
388 $sql .= " SET fk_user = ".((int) $user->id)." ,";
389 $sql .= " ref_fourn = '".$this->db->escape($ref_fourn)."',";
390 $sql .= " desc_fourn = '".$this->db->escape($desc_fourn)."',";
391 $sql .= " price = ".((float) $buyprice).",";
392 $sql .= " quantity = ".((float) $qty).",";
393 $sql .= " remise_percent = ".((float) $remise_percent).",";
394 $sql .= " remise = ".((float) $remise).",";
395 $sql .= " unitprice = ".((float) $unitBuyPrice).",";
396 $sql .= " fk_availability = ".((int) $availability).",";
397 $sql .= " multicurrency_price = ".(isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
398 $sql .= " multicurrency_unitprice = ".(isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
399 $sql .= " multicurrency_tx = ".(isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
400 $sql .= " fk_multicurrency = ".(isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
401 $sql .= " multicurrency_code = ".(isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
402 $sql .= " entity = ".$conf->entity.",";
403 $sql .= " tva_tx = ".price2num($tva_tx).",";
404 // TODO Add localtax1 and localtax2
405 //$sql.= " localtax1_tx=".($localtax1>=0?$localtax1:'NULL').",";
406 //$sql.= " localtax2_tx=".($localtax2>=0?$localtax2:'NULL').",";
407 //$sql.= " localtax1_type=".($localtaxtype1!=''?"'".$this->db->escape($localtaxtype1)."'":"'0'").",";
408 //$sql.= " localtax2_type=".($localtaxtype2!=''?"'".$this->db->escape($localtaxtype2)."'":"'0'").",";
409 $sql .= " default_vat_code=".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
410 $sql .= " info_bits = ".((int) $newnpr).",";
411 $sql .= " charges = ".((float) $charges).","; // deprecated
412 $sql .= " delivery_time_days = ".($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
413 $sql .= " supplier_reputation = ".(empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
414 $sql .= " barcode = ".(empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
415 $sql .= " fk_barcode_type = ".(empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape($fk_barcode_type)."'");
416 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
417 $sql .= ", packaging = ".(empty($packaging) ? 1 : $packaging);
418 }
419 $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
420
421 if (!$error) {
422 if (!empty($options) && is_array($options)) {
423 $productfournisseurprice = new ProductFournisseurPrice($this->db);
424 $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
425 if ($res > 0) {
426 foreach ($options as $key=>$value) {
427 $productfournisseurprice->array_options[$key] = $value;
428 }
429 $res = $productfournisseurprice->update($user);
430 if ($res < 0) {
431 $this->error = $productfournisseurprice->error;
432 $this->errors = $productfournisseurprice->errors;
433 $error++;
434 }
435 }
436 }
437 }
438
439 // TODO Add price_base_type and price_ttc
440
441 dol_syslog(get_class($this).'::update_buyprice update knowing id of line = product_fourn_price_id = '.$this->product_fourn_price_id, LOG_DEBUG);
442 $resql = $this->db->query($sql);
443 if ($resql) {
444 // Call trigger
445 $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_MODIFY', $user);
446 if ($result < 0) {
447 $error++;
448 }
449 // End call triggers
450 if (!$error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
451 $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
452 if ($result < 0) {
453 $error++;
454 }
455 }
456 if (empty($error)) {
457 $this->db->commit();
458 return $this->product_fourn_price_id;
459 } else {
460 $this->db->rollback();
461 return -1;
462 }
463 } else {
464 $this->error = $this->db->error()." sql=".$sql;
465 $this->db->rollback();
466 return -2;
467 }
468 } else {
469 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);
470
471 // Delete price for this quantity
472 $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
473 $sql .= " WHERE fk_soc = ".((int) $fourn->id)." AND ref_fourn = '".$this->db->escape($ref_fourn)."' AND quantity = ".((float) $qty)." AND entity = ".((int) $conf->entity);
474 $resql = $this->db->query($sql);
475 if ($resql) {
476 // Add price for this quantity to supplier
477 $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price(";
478 $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
479 $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";
480 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
481 $sql .= ", packaging";
482 }
483 $sql .= ") values(";
484 $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
485 $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
486 $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
487 $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
488 $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
489 $sql .= " '".$this->db->idate($now)."',";
490 $sql .= " ".((int) $this->id).",";
491 $sql .= " ".((int) $fourn->id).",";
492 $sql .= " '".$this->db->escape($ref_fourn)."',";
493 $sql .= " '".$this->db->escape($desc_fourn)."',";
494 $sql .= " ".((int) $user->id).",";
495 $sql .= " ".price2num($buyprice).",";
496 $sql .= " ".((float) $qty).",";
497 $sql .= " ".((float) $remise_percent).",";
498 $sql .= " ".((float) $remise).",";
499 $sql .= " ".price2num($unitBuyPrice).",";
500 $sql .= " ".price2num($tva_tx).",";
501 $sql .= " ".price2num($charges).",";
502 $sql .= " ".((int) $availability).",";
503 $sql .= " ".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
504 $sql .= " ".((int) $newnpr).",";
505 $sql .= $conf->entity.",";
506 $sql .= ($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
507 $sql .= (empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
508 $sql .= (empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
509 $sql .= (empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape($fk_barcode_type)."'");
510 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
511 $sql .= ", ".(empty($this->packaging) ? '1' : "'".$this->db->escape($this->packaging)."'");
512 }
513 $sql .= ")";
514
515 $this->product_fourn_price_id = 0;
516
517 $resql = $this->db->query($sql);
518 if ($resql) {
519 $this->product_fourn_price_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur_price");
520 } else {
521 $this->error = $this->db->lasterror();
522 $error++;
523 }
524
525 if (!$error) {
526 if (!empty($options) && is_array($options)) {
527 $productfournisseurprice = new ProductFournisseurPrice($this->db);
528 $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
529 if ($res > 0) {
530 foreach ($options as $key=>$value) {
531 $productfournisseurprice->array_options[$key] = $value;
532 }
533 $res = $productfournisseurprice->update($user);
534 if ($res < 0) {
535 $this->error = $productfournisseurprice->error;
536 $this->errors = $productfournisseurprice->errors;
537 $error++;
538 }
539 }
540 }
541 }
542
543 if (!$error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
544 // Add record into log table
545 // $this->product_fourn_price_id must be set
546 $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
547 if ($result < 0) {
548 $error++;
549 }
550 }
551
552 if (!$error) {
553 // Call trigger
554 $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_CREATE', $user);
555 if ($result < 0) {
556 $error++;
557 }
558 // End call triggers
559
560 if (empty($error)) {
561 $this->db->commit();
562 return $this->product_fourn_price_id;
563 } else {
564 $this->db->rollback();
565 return -1;
566 }
567 } else {
568 $this->error = $this->db->lasterror()." sql=".$sql;
569 $this->db->rollback();
570 return -2;
571 }
572 } else {
573 $this->error = $this->db->lasterror()." sql=".$sql;
574 $this->db->rollback();
575 return -1;
576 }
577 }
578 }
579
580 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
588 public function fetch_product_fournisseur_price($rowid, $ignore_expression = 0)
589 {
590 // phpcs:enable
591 global $conf;
592
593 $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,";
594 $sql .= " pfp.fk_soc, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_product, pfp.charges, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
595 $sql .= " pfp.supplier_reputation, pfp.fk_user, pfp.datec,";
596 $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
597 $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging,";
598 $sql .= " p.ref as product_ref, p.tosell as status, p.tobuy as status_buy";
599 $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p";
600 $sql .= " WHERE pfp.rowid = ".(int) $rowid;
601 $sql .= " AND pfp.fk_product = p.rowid";
602
603 dol_syslog(get_class($this)."::fetch_product_fournisseur_price", LOG_DEBUG);
604 $resql = $this->db->query($sql);
605 if ($resql) {
606 $obj = $this->db->fetch_object($resql);
607 if ($obj) {
608 $this->product_fourn_price_id = $rowid;
609 $this->id = $obj->fk_product;
610
611 $this->fk_product = $obj->fk_product;
612 $this->product_id = $obj->fk_product;
613 $this->product_ref = $obj->product_ref;
614 $this->status = $obj->status;
615 $this->status_buy = $obj->status_buy;
616 $this->fourn_id = $obj->fk_soc;
617 $this->fourn_ref = $obj->ref_fourn; // deprecated
618 $this->ref_supplier = $obj->ref_fourn;
619 $this->desc_supplier = $obj->desc_fourn;
620 $this->fourn_price = $obj->price;
621 $this->fourn_charges = $obj->charges; // when getDolGlobalString('PRODUCT_CHARGES') is set
622 $this->fourn_qty = $obj->quantity;
623 $this->fourn_remise_percent = $obj->remise_percent;
624 $this->fourn_remise = $obj->remise;
625 $this->fourn_unitprice = $obj->unitprice;
626 $this->fourn_tva_tx = $obj->tva_tx;
627 $this->fourn_tva_npr = $obj->fourn_tva_npr;
628 // Add also localtaxes
629 $this->fk_availability = $obj->fk_availability;
630 $this->delivery_time_days = $obj->delivery_time_days;
631 $this->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
632 $this->supplier_reputation = $obj->supplier_reputation;
633 $this->default_vat_code = $obj->default_vat_code;
634 $this->user_id = $obj->fk_user;
635 $this->date_creation = $this->db->jdate($obj->datec);
636 $this->fourn_multicurrency_price = $obj->multicurrency_price;
637 $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
638 $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
639 $this->fourn_multicurrency_id = $obj->fk_multicurrency;
640 $this->fourn_multicurrency_code = $obj->multicurrency_code;
641 if (isModEnabled('barcode')) {
642 $this->fourn_barcode = $obj->barcode; // deprecated
643 $this->fourn_fk_barcode_type = $obj->fk_barcode_type; // deprecated
644 $this->supplier_barcode = $obj->barcode;
645 $this->supplier_fk_barcode_type = $obj->fk_barcode_type;
646 }
647 $this->packaging = $obj->packaging;
648
649 if (isModEnabled('dynamicprices') && empty($ignore_expression) && !empty($this->fk_supplier_price_expression)) {
650 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
651 $priceparser = new PriceParser($this->db);
652 $price_result = $priceparser->parseProductSupplier($this);
653 if ($price_result >= 0) {
654 $this->fourn_price = $price_result;
655 //recalculation of unitprice, as probably the price changed...
656 if ($this->fourn_qty != 0) {
657 $this->fourn_unitprice = price2num($this->fourn_price / $this->fourn_qty, 'MU');
658 } else {
659 $this->fourn_unitprice = "";
660 }
661 }
662 }
663
664 return 1;
665 } else {
666 return 0;
667 }
668 } else {
669 $this->error = $this->db->lasterror();
670 return -1;
671 }
672 }
673
674
675 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
688 public function list_product_fournisseur_price($prodid, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0, $socid = 0)
689 {
690 // phpcs:enable
691 global $conf;
692
693 $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, ";
694 $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,";
695 $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,";
696 $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code, pfp.datec, pfp.tms,";
697 $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging, pfp.status as pfstatus";
698 $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."societe as s";
699 $sql .= " WHERE pfp.entity IN (".getEntity('productsupplierprice').")";
700 $sql .= " AND pfp.fk_soc = s.rowid AND pfp.fk_product = p.rowid";
701 $sql .= ($socid > 0 ? ' AND pfp.fk_soc = '.((int) $socid) : '');
702 $sql .= " AND s.status = 1"; // only enabled company selected
703 $sql .= " AND pfp.fk_product = ".((int) $prodid);
704 if (empty($sortfield)) {
705 $sql .= " ORDER BY s.nom, pfp.quantity, pfp.price";
706 } else {
707 $sql .= $this->db->order($sortfield, $sortorder);
708 }
709 $sql .= $this->db->plimit($limit, $offset);
710 dol_syslog(get_class($this)."::list_product_fournisseur_price", LOG_DEBUG);
711
712 $resql = $this->db->query($sql);
713 if ($resql) {
714 $retarray = array();
715
716 while ($record = $this->db->fetch_array($resql)) {
717 //define base attribute
718 $prodfourn = new ProductFournisseur($this->db);
719
720 $prodfourn->product_ref = $record["product_ref"];
721 $prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"];
722 $prodfourn->status = $record["status"];
723 $prodfourn->status_buy = $record["status_buy"];
724 $prodfourn->product_fourn_id = $record["product_fourn_id"];
725 $prodfourn->product_fourn_entity = $record["entity"];
726 $prodfourn->ref_supplier = $record["ref_fourn"];
727 $prodfourn->fourn_ref = $record["ref_fourn"];
728 $prodfourn->desc_supplier = $record["desc_fourn"];
729 $prodfourn->fourn_price = $record["price"];
730 $prodfourn->fourn_qty = $record["quantity"];
731 $prodfourn->fourn_remise_percent = $record["remise_percent"];
732 $prodfourn->fourn_remise = $record["remise"];
733 $prodfourn->fourn_unitprice = $record["unitprice"];
734 $prodfourn->fourn_charges = $record["charges"]; // when getDolGlobalString('PRODUCT_CHARGES') is set
735 $prodfourn->fourn_tva_tx = $record["tva_tx"];
736 $prodfourn->fourn_id = $record["fourn_id"];
737 $prodfourn->fourn_name = $record["supplier_name"];
738 $prodfourn->fk_availability = $record["fk_availability"];
739 $prodfourn->delivery_time_days = $record["delivery_time_days"];
740 $prodfourn->id = $prodid;
741 $prodfourn->fourn_tva_npr = $record["info_bits"];
742 $prodfourn->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
743 $prodfourn->supplier_reputation = $record["supplier_reputation"];
744 $prodfourn->fourn_date_creation = $this->db->jdate($record['datec']);
745 $prodfourn->fourn_date_modification = $this->db->jdate($record['tms']);
746
747 $prodfourn->fourn_multicurrency_price = $record["multicurrency_price"];
748 $prodfourn->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
749 $prodfourn->fourn_multicurrency_tx = $record["multicurrency_tx"];
750 $prodfourn->fourn_multicurrency_id = $record["fk_multicurrency"];
751 $prodfourn->fourn_multicurrency_code = $record["multicurrency_code"];
752
753 $prodfourn->packaging = $record["packaging"];
754 $prodfourn->status = $record["pfstatus"];
755
756 if (isModEnabled('barcode')) {
757 $prodfourn->supplier_barcode = $record["barcode"];
758 $prodfourn->supplier_fk_barcode_type = $record["fk_barcode_type"];
759 }
760
761 if (isModEnabled('dynamicprices') && !empty($prodfourn->fk_supplier_price_expression)) {
762 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
763 $priceparser = new PriceParser($this->db);
764 $price_result = $priceparser->parseProductSupplier($prodfourn);
765 if ($price_result >= 0) {
766 $prodfourn->fourn_price = $price_result;
767 $prodfourn->fourn_unitprice = null; //force recalculation of unitprice, as probably the price changed...
768 }
769 }
770
771 if (!isset($prodfourn->fourn_unitprice)) {
772 if ($prodfourn->fourn_qty != 0) {
773 $prodfourn->fourn_unitprice = price2num($prodfourn->fourn_price / $prodfourn->fourn_qty, 'MU');
774 } else {
775 $prodfourn->fourn_unitprice = "";
776 }
777 }
778
779 $retarray[] = $prodfourn;
780 }
781
782 $this->db->free($resql);
783 return $retarray;
784 } else {
785 $this->error = $this->db->error();
786 return -1;
787 }
788 }
789
790 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
800 public function find_min_price_product_fournisseur($prodid, $qty = 0, $socid = 0)
801 {
802 // phpcs:enable
803 global $conf;
804
805 if (empty($prodid)) {
806 dol_syslog("Warning function find_min_price_product_fournisseur were called with prodid empty. May be a bug.", LOG_WARNING);
807 return 0;
808 }
809
810 $this->product_fourn_price_id = '';
811 $this->product_fourn_id = '';
812 $this->fourn_ref = '';
813 $this->fourn_price = '';
814 $this->fourn_qty = '';
815 $this->fourn_remise_percent = '';
816 $this->fourn_remise = '';
817 $this->fourn_unitprice = '';
818 $this->fourn_id = '';
819 $this->fourn_name = '';
820 $this->delivery_time_days = '';
821 $this->id = '';
822
823 $this->fourn_multicurrency_price = '';
824 $this->fourn_multicurrency_unitprice = '';
825 $this->fourn_multicurrency_tx = '';
826 $this->fourn_multicurrency_id = '';
827 $this->fourn_multicurrency_code = '';
828
829 $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
830 $sql .= " pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
831 $sql .= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges,";
832 $sql .= " pfp.remise, pfp.remise_percent, pfp.fk_supplier_price_expression, pfp.delivery_time_days";
833 $sql .= " ,pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code";
834 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
835 $sql .= " WHERE s.entity IN (".getEntity('societe').")";
836 $sql .= " AND pfp.entity IN (".getEntity('productsupplierprice').")";
837 $sql .= " AND pfp.fk_product = ".((int) $prodid);
838 $sql .= " AND pfp.fk_soc = s.rowid";
839 $sql .= " AND s.status = 1"; // only enabled society
840 if ($qty > 0) {
841 $sql .= " AND pfp.quantity <= ".((float) $qty);
842 }
843 if ($socid > 0) {
844 $sql .= ' AND pfp.fk_soc = '.((int) $socid);
845 }
846
847 dol_syslog(get_class($this)."::find_min_price_product_fournisseur", LOG_DEBUG);
848
849 $resql = $this->db->query($sql);
850 if ($resql) {
851 $record_array = array();
852
853 //Store each record to array for later search of min
854 while ($record = $this->db->fetch_array($resql)) {
855 $record_array[] = $record;
856 }
857
858 if (count($record_array) == 0) {
859 $this->db->free($resql);
860 return 0;
861 } else {
862 $min = -1;
863 foreach ($record_array as $record) {
864 $fourn_price = $record["price"];
865 // calculate unit price for quantity 1
866 $fourn_unitprice = $record["unitprice"];
867 $fourn_unitprice_with_discount = $record["unitprice"] * (1 - $record["remise_percent"] / 100);
868
869 if (isModEnabled('dynamicprices') && !empty($record["fk_supplier_price_expression"])) {
870 $prod_supplier = new ProductFournisseur($this->db);
871 $prod_supplier->product_fourn_price_id = $record["product_fourn_price_id"];
872 $prod_supplier->id = $prodid;
873 $prod_supplier->fourn_qty = $record["quantity"];
874 $prod_supplier->fourn_tva_tx = $record["tva_tx"];
875 $prod_supplier->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
876
877 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
878 $priceparser = new PriceParser($this->db);
879 $price_result = $priceparser->parseProductSupplier($prod_supplier);
880 if ($price_result >= 0) {
881 $fourn_price = price2num($price_result, 'MU');
882 if ($record["quantity"] != 0) {
883 $fourn_unitprice = price2num($fourn_price / $record["quantity"], 'MU');
884 } else {
885 $fourn_unitprice = $fourn_price;
886 }
887 $fourn_unitprice_with_discount = $fourn_unitprice * (1 - $record["remise_percent"] / 100);
888 }
889 }
890 if ($fourn_unitprice < $min || $min == -1) {
891 $this->product_fourn_price_id = $record["product_fourn_price_id"];
892 $this->ref_supplier = $record["ref_fourn"];
893 $this->ref_fourn = $record["ref_fourn"]; // deprecated
894 $this->fourn_ref = $record["ref_fourn"]; // deprecated
895 $this->fourn_price = $fourn_price;
896 $this->fourn_qty = $record["quantity"];
897 $this->fourn_remise_percent = $record["remise_percent"];
898 $this->fourn_remise = $record["remise"];
899 $this->fourn_unitprice = $fourn_unitprice;
900 $this->fourn_unitprice_with_discount = $fourn_unitprice_with_discount;
901 $this->fourn_charges = $record["charges"]; // when getDolGlobalString('PRODUCT_CHARGES') is set
902 $this->fourn_tva_tx = $record["tva_tx"];
903 $this->fourn_id = $record["fourn_id"];
904 $this->fourn_name = $record["supplier_name"];
905 $this->delivery_time_days = $record["delivery_time_days"];
906 $this->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
907 $this->id = $prodid;
908 $this->fourn_multicurrency_price = $record["multicurrency_price"];
909 $this->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
910 $this->fourn_multicurrency_tx = $record["multicurrency_tx"];
911 $this->fourn_multicurrency_id = $record["fk_multicurrency"];
912 $this->fourn_multicurrency_code = $record["multicurrency_code"];
913 $min = $fourn_unitprice;
914 }
915 }
916 }
917
918 $this->db->free($resql);
919 return 1;
920 } else {
921 $this->error = $this->db->error();
922 return -1;
923 }
924 }
925
932 public function setSupplierPriceExpression($expression_id)
933 {
934 global $conf;
935
936 // Clean parameters
937 $this->db->begin();
938 $expression_id = $expression_id != 0 ? $expression_id : 'NULL';
939
940 $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
941 $sql .= " SET fk_supplier_price_expression = ".((int) $expression_id);
942 $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
943
944 dol_syslog(get_class($this)."::setSupplierPriceExpression", LOG_DEBUG);
945
946 $resql = $this->db->query($sql);
947 if ($resql) {
948 $this->db->commit();
949 return 1;
950 } else {
951 $this->error = $this->db->error()." sql=".$sql;
952 $this->db->rollback();
953 return -1;
954 }
955 }
956
967 public function getSocNomUrl($withpicto = 0, $option = 'supplier', $maxlen = 0, $notooltip = 0)
968 {
969 $thirdparty = new Fournisseur($this->db);
970 $thirdparty->fetch($this->fourn_id);
971
972 return $thirdparty->getNomUrl($withpicto, $option, $maxlen, $notooltip);
973 }
974
975 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
987 public function display_price_product_fournisseur($showunitprice = 1, $showsuptitle = 1, $maxlen = 0, $notooltip = 0, $productFournList = array())
988 {
989 // phpcs:enable
990 global $conf, $langs;
991
992 $out = '';
993 $langs->load("suppliers");
994 if (count($productFournList) > 0) {
995 $out .= '<table class="nobordernopadding" width="100%">';
996 $out .= '<tr><td class="liste_titre right">'.($showunitprice ? $langs->trans("Price").' '.$langs->trans("HT") : '').'</td>';
997 $out .= '<td class="liste_titre right">'.($showunitprice ? $langs->trans("QtyMin") : '').'</td>';
998 $out .= '<td class="liste_titre">'.$langs->trans("Supplier").'</td>';
999 $out .= '<td class="liste_titre">'.$langs->trans("SupplierRef").'</td></tr>';
1000 foreach ($productFournList as $productFourn) {
1001 $out .= '<tr><td class="right">'.($showunitprice ?price($productFourn->fourn_unitprice * (1 - $productFourn->fourn_remise_percent / 100) - $productFourn->fourn_remise) : '').'</td>';
1002 $out .= '<td class="right">'.($showunitprice ? $productFourn->fourn_qty : '').'</td>';
1003 $out .= '<td>'.$productFourn->getSocNomUrl(1, 'supplier', $maxlen, $notooltip).'</td>';
1004 $out .= '<td>'.$productFourn->fourn_ref.'<td></tr>';
1005 }
1006 $out .= '</table>';
1007 } else {
1008 $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>' : '');
1009 $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;
1010 $out .= ($showunitprice ? '<span class="opacitymedium">)</span>' : '');
1011 }
1012 return $out;
1013 }
1014
1023 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1024 {
1025 $tables = array(
1026 'product_fournisseur_price'
1027 );
1028
1029 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1030 }
1031
1040 public static function replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
1041 {
1042 $tables = array(
1043 'product_fournisseur_price'
1044 );
1045
1046 return CommonObject::commonReplaceProduct($dbs, $origin_id, $dest_id, $tables);
1047 }
1048
1059 public function listProductFournisseurPriceLog($product_fourn_price_id, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0)
1060 {
1061 $sql = "SELECT";
1062 $sql .= " u.lastname,";
1063 $sql .= " pfpl.rowid, pfp.ref_fourn as supplier_ref, pfpl.datec,";
1064 $sql .= " pfpl.price, pfpl.quantity,";
1065 $sql .= " pfpl.fk_multicurrency, pfpl.multicurrency_code, pfpl.multicurrency_tx, pfpl.multicurrency_price, pfpl.multicurrency_unitprice";
1066 $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price_log as pfpl,";
1067 $sql .= " ".MAIN_DB_PREFIX."product_fournisseur_price as pfp,";
1068 $sql .= " ".MAIN_DB_PREFIX."user as u";
1069 $sql .= " WHERE pfp.entity IN (".getEntity('productprice').")";
1070 $sql .= " AND pfpl.fk_user = u.rowid";
1071 $sql .= " AND pfp.rowid = pfpl.fk_product_fournisseur";
1072 $sql .= " AND pfpl.fk_product_fournisseur = ".((int) $product_fourn_price_id);
1073 if (empty($sortfield)) {
1074 $sql .= " ORDER BY pfpl.datec";
1075 } else {
1076 $sql .= $this->db->order($sortfield, $sortorder);
1077 }
1078 $sql .= $this->db->plimit($limit, $offset);
1079 dol_syslog(get_class($this)."::list_product_fournisseur_price_log", LOG_DEBUG);
1080
1081 $resql = $this->db->query($sql);
1082 if ($resql) {
1083 $retarray = array();
1084
1085 while ($obj = $this->db->fetch_object($resql)) {
1086 $tmparray = array();
1087 $tmparray['rowid'] = $obj->rowid;
1088 $tmparray['supplier_ref'] = $obj->supplier_ref;
1089 $tmparray['datec'] = $this->db->jdate($obj->datec);
1090 $tmparray['lastname'] = $obj->lastname;
1091 $tmparray['price'] = $obj->price;
1092 $tmparray['quantity'] = $obj->quantity;
1093 $tmparray['fk_multicurrency'] = $obj->fk_multicurrency;
1094 $tmparray['multicurrency_code'] = $obj->multicurrency_code;
1095 $tmparray['multicurrency_tx'] = $obj->multicurrency_tx;
1096 $tmparray['multicurrency_price'] = $obj->multicurrency_price;
1097 $tmparray['multicurrency_unitprice'] = $obj->multicurrency_unitprice;
1098
1099 $retarray[] = $tmparray;
1100 }
1101
1102 $this->db->free($resql);
1103 return $retarray;
1104 } else {
1105 $this->error = $this->db->error();
1106 return -1;
1107 }
1108 }
1109
1117 public function displayPriceProductFournisseurLog($productFournLogList = array())
1118 {
1119 global $conf, $langs;
1120
1121 $out = '';
1122 $langs->load("suppliers");
1123 if (count($productFournLogList) > 0) {
1124 $out .= '<table class="noborder centpercent">';
1125 $out .= '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Date").'</td>';
1126 $out .= '<td class="liste_titre right">'.$langs->trans("Price").'</td>';
1127 //$out .= '<td class="liste_titre right">'.$langs->trans("QtyMin").'</td>';
1128 $out .= '<td class="liste_titre">'.$langs->trans("User").'</td></tr>';
1129 foreach ($productFournLogList as $productFournLog) {
1130 $out .= '<tr><td>'.dol_print_date($productFournLog['datec'], 'dayhour', 'tzuser').'</td>';
1131 $out .= '<td class="right">'.price($productFournLog['price'], 0, $langs, 1, -1, -1, $conf->currency);
1132 if ($productFournLog['multicurrency_code'] != $conf->currency) {
1133 $out .= ' ('.price($productFournLog['multicurrency_price'], 0, $langs, 1, -1, -1, $productFournLog['multicurrency_code']).')';
1134 }
1135 $out .= '</td>';
1136 //$out.= '<td class="right">'.$productFournLog['quantity'].'</td>';
1137 $out .= '<td>'.$productFournLog['lastname'].'</td></tr>';
1138 }
1139 $out .= '</table>';
1140 }
1141 return $out;
1142 }
1143
1144
1159 public function getNomUrl($withpicto = 0, $option = '', $maxlength = 0, $save_lastsearch_value = -1, $notooltip = 0, $morecss = '', $add_label = 0, $sep = ' - ')
1160 {
1161 global $db, $conf, $langs, $hookmanager;
1162
1163 if (!empty($conf->dol_no_mouse_hover)) {
1164 $notooltip = 1; // Force disable tooltips
1165 }
1166
1167 $result = '';
1168 $label = '';
1169
1170 $newref = $this->ref;
1171 if ($maxlength) {
1172 $newref = dol_trunc($newref, $maxlength, 'middle');
1173 }
1174
1175 if (!empty($this->entity)) {
1176 $tmpphoto = $this->show_photos('product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 80);
1177 if ($this->nbphoto > 0) {
1178 $label .= '<div class="photointooltip">';
1179 $label .= $tmpphoto;
1180 $label .= '</div><div style="clear: both;"></div>';
1181 }
1182 }
1183
1184 if ($this->type == Product::TYPE_PRODUCT) {
1185 $label .= img_picto('', 'product').' <u class="paddingrightonly">'.$langs->trans("Product").'</u>';
1186 } elseif ($this->type == Product::TYPE_SERVICE) {
1187 $label .= img_picto('', 'service').' <u class="paddingrightonly">'.$langs->trans("Service").'</u>';
1188 }
1189 if (isset($this->status) && isset($this->status_buy)) {
1190 $label .= ' '.$this->getLibStatut(5, 0);
1191 $label .= ' '.$this->getLibStatut(5, 1);
1192 }
1193
1194 if (!empty($this->ref)) {
1195 $label .= '<br><b>'.$langs->trans('ProductRef').':</b> '.($this->ref ? $this->ref : $this->product_ref);
1196 }
1197 if (!empty($this->label)) {
1198 $label .= '<br><b>'.$langs->trans('ProductLabel').':</b> '.$this->label;
1199 }
1200 $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
1201
1202 if ($this->type == Product::TYPE_PRODUCT || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
1203 if (isModEnabled('productbatch')) {
1204 $langs->load("productbatch");
1205 $label .= "<br><b>".$langs->trans("ManageLotSerial").'</b>: '.$this->getLibStatut(0, 2);
1206 }
1207 }
1208 if (isModEnabled('barcode')) {
1209 $label .= '<br><b>'.$langs->trans('BarCode').':</b> '.$this->barcode;
1210 }
1211
1212 if ($this->type == Product::TYPE_PRODUCT) {
1213 if ($this->weight) {
1214 $label .= "<br><b>".$langs->trans("Weight").'</b>: '.$this->weight.' '.measuringUnitString(0, "weight", $this->weight_units);
1215 }
1216 $labelsize = "";
1217 if ($this->length) {
1218 $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Length").'</b>: '.$this->length.' '.measuringUnitString(0, 'size', $this->length_units);
1219 }
1220 if ($this->width) {
1221 $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Width").'</b>: '.$this->width.' '.measuringUnitString(0, 'size', $this->width_units);
1222 }
1223 if ($this->height) {
1224 $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Height").'</b>: '.$this->height.' '.measuringUnitString(0, 'size', $this->height_units);
1225 }
1226 if ($labelsize) {
1227 $label .= "<br>".$labelsize;
1228 }
1229
1230 $labelsurfacevolume = "";
1231 if ($this->surface) {
1232 $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Surface").'</b>: '.$this->surface.' '.measuringUnitString(0, 'surface', $this->surface_units);
1233 }
1234 if ($this->volume) {
1235 $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Volume").'</b>: '.$this->volume.' '.measuringUnitString(0, 'volume', $this->volume_units);
1236 }
1237 if ($labelsurfacevolume) {
1238 $label .= "<br>".$labelsurfacevolume;
1239 }
1240 }
1241
1242 if (isModEnabled('accounting') && $this->status) {
1243 include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1244 $label .= '<br><b>'.$langs->trans('ProductAccountancySellCode').':</b> '.length_accountg($this->accountancy_code_sell);
1245 $label .= '<br><b>'.$langs->trans('ProductAccountancySellIntraCode').':</b> '.length_accountg($this->accountancy_code_sell_intra);
1246 $label .= '<br><b>'.$langs->trans('ProductAccountancySellExportCode').':</b> '.length_accountg($this->accountancy_code_sell_export);
1247 }
1248 if (isModEnabled('accounting') && $this->status_buy) {
1249 include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1250 $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyCode').':</b> '.length_accountg($this->accountancy_code_buy);
1251 $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyIntraCode').':</b> '.length_accountg($this->accountancy_code_buy_intra);
1252 $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyExportCode').':</b> '.length_accountg($this->accountancy_code_buy_export);
1253 }
1254
1255 $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id, 'pfpl.datec', 'DESC'); // set sort order here
1256 if (is_array($logPrices) && count($logPrices) > 0) {
1257 $label .= '<br><br>';
1258 $label .= '<u>'.$langs->trans("History").'</u>';
1259 $label .= $this->displayPriceProductFournisseurLog($logPrices);
1260 }
1261
1262 $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);
1263
1264 if ($option != 'nolink') {
1265 // Add param to save lastsearch_values or not
1266 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1267 if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1268 $add_save_lastsearch_values = 1;
1269 }
1270 if ($add_save_lastsearch_values) {
1271 $url .= '&save_lastsearch_values=1';
1272 }
1273 }
1274
1275 $linkclose = '';
1276 if (empty($notooltip)) {
1277 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1278 $label = $langs->trans("SupplierRef");
1279 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1280 }
1281 $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
1282 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
1283 } else {
1284 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1285 }
1286
1287 $linkstart = '<a href="'.$url.'"';
1288 $linkstart .= $linkclose.'>';
1289 $linkend = '</a>';
1290
1291 $result .= $linkstart;
1292 if ($withpicto) {
1293 $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);
1294 }
1295 if ($withpicto != 2) {
1296 $result .= $newref.($this->ref_supplier ? ' ('.$this->ref_supplier.')' : '');
1297 }
1298 $result .= $linkend;
1299 if ($withpicto != 2) {
1300 $result .= (($add_label && $this->label) ? $sep.dol_trunc($this->label, ($add_label > 1 ? $add_label : 0)) : '');
1301 }
1302
1303 global $action;
1304 $hookmanager->initHooks(array($this->element . 'dao'));
1305 $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
1306 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1307 if ($reshook > 0) {
1308 $result = $hookmanager->resPrint;
1309 } else {
1310 $result .= $hookmanager->resPrint;
1311 }
1312 return $result;
1313 }
1314
1322 public function getLibStatut($mode = 0, $type = 0) // must be compatible with getLibStatut of inherited Product
1323 {
1324 return $this->LibStatut($this->status, $mode);
1325 }
1326
1327 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1336 public function LibStatut($status, $mode = 0, $type = 0)
1337 {
1338 // phpcs:enable
1339 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1340 global $langs;
1341 //$langs->load("mymodule@mymodule");
1342 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Enabled');
1343 $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
1344 $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Enabled');
1345 $this->labelStatusShort[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
1346 }
1347
1348 $statusType = 'status4';
1349 //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
1350 if ($status == self::STATUS_CANCELED) {
1351 $statusType = 'status6';
1352 }
1353
1354 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
1355 }
1356
1372 private function logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice = null, $multicurrency_unitBuyPrice = null, $multicurrency_tx = null, $fk_multicurrency = null, $multicurrency_code = null)
1373 {
1374 // Add record into log table
1375 $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log(";
1376 $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
1377 $sql .= "datec, fk_product_fournisseur,fk_user,price,quantity)";
1378 $sql .= "values(";
1379 $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
1380 $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
1381 $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
1382 $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
1383 $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
1384 $sql .= "'".$this->db->idate($datec)."',";
1385 $sql .= " ".((int) $this->product_fourn_price_id).",";
1386 $sql .= " ".$user->id.",";
1387 $sql .= " ".price2num($buyprice).",";
1388 $sql .= " ".price2num($qty);
1389 $sql .= ")";
1390
1391 $resql = $this->db->query($sql);
1392 if (!$resql) {
1393 return -1;
1394 } else {
1395 return 1;
1396 }
1397 }
1398}
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.
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.
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
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.
LibStatut($status, $mode=0, $type=0)
Return the status.
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).
getLibStatut($mode=0, $type=0)
Return the label of the status.
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.
$res
Path of subproducts.
const TYPE_SERVICE
Service.
Class to manage Dolibarr users.
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)
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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:120