dolibarr 23.0.3
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-2025 Frédéric France <frederic.france@free.fr>
10 * Copyright (C) 2020 Pierre Ardoin <mapiolca@me.com>
11 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25 */
26
33require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
34require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
35require_once DOL_DOCUMENT_ROOT.'/product/class/productfournisseurprice.class.php';
36
37
42{
46 public $db;
47
52 public $TRIGGER_PREFIX = 'PRODUCT'; // We use parent because parent method can still have the parent prefix
53
57 public $error = '';
58
62 public $product_fourn_price_id;
63
67 public $id;
68
74 public $fourn_ref;
75
79 public $delivery_time_days;
80
84 public $ref_supplier;
85
89 public $desc_supplier;
90
94 public $vatrate_supplier;
95
101 public $fk_product;
102
106 public $product_id;
107
111 public $product_ref;
112
116 public $fourn_id;
117
121 public $fourn_name;
122
126 public $fourn_qty;
127
131 public $fourn_pu;
132
136 public $fourn_price;
137
141 public $fourn_remise_percent;
142
146 public $fourn_remise;
147
151 public $fourn_charges; // when getDolGlobalString('PRODUCT_CHARGES') is set
152
153
154 /* Properties found into product_fournisseur_price */
155
159 public $product_fourn_id;
160
164 public $product_fourn_entity;
165
169 public $product_fourn_packaging;
170
171
175 public $user_id;
176
180 public $fk_availability;
181
185 public $fourn_unitprice;
189 public $fourn_unitprice_with_discount; // not saved into database
193 public $fourn_tva_tx;
197 public $fourn_tva_npr;
198
202 public $fk_supplier_price_expression;
203
207 public $supplier_reputation;
208
212 public $reputations = array();
213
214 // Multicurrency
215
219 public $fourn_multicurrency_id;
220
224 public $fourn_multicurrency_code;
228 public $fourn_multicurrency_tx;
232 public $fourn_multicurrency_price;
236 public $fourn_multicurrency_unitprice;
237
243 public $fourn_barcode;
244
248 public $supplier_barcode;
249
255 public $fourn_fk_barcode_type;
256
260 public $supplier_fk_barcode_type;
261
265 public $labelStatusShort;
269 public $labelStatus;
270
271 const STATUS_OPEN = 1;
272 const STATUS_CANCELED = 0;
273
274
280 public function __construct($db)
281 {
282 global $langs;
283
284 $this->db = $db;
285 $langs->load("suppliers");
286 $this->reputations = array('-1' => '', 'FAVORITE' => $langs->trans('Favorite'), 'NOTTHGOOD' => $langs->trans('NotTheGoodQualitySupplier'), 'DONOTORDER' => $langs->trans('DoNotOrderThisProductToThisSupplier'));
287 }
288
289 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
296 public function remove_fournisseur($id_fourn)
297 {
298 // phpcs:enable
299 $ok = 1;
300
301 $this->db->begin();
302
303 $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
304 $sql .= " WHERE fk_product = ".((int) $this->id)." AND fk_soc = ".((int) $id_fourn);
305
306 dol_syslog(get_class($this)."::remove_fournisseur", LOG_DEBUG);
307 $resql2 = $this->db->query($sql);
308 if (!$resql2) {
309 $this->error = $this->db->lasterror();
310 $ok = 0;
311 }
312
313 if ($ok) {
314 $this->db->commit();
315 return 1;
316 } else {
317 $this->db->rollback();
318 return -1;
319 }
320 }
321
322
323 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
330 public function remove_product_fournisseur_price($rowid)
331 {
332 // phpcs:enable
333 global $user;
334
335 $error = 0;
336
337 $this->db->begin();
338
339 // Call trigger
340 $result = $this->call_trigger('PRODUCT_BUYPRICE_DELETE', $user);
341 if ($result < 0) {
342 $error++;
343 }
344 // End call triggers
345
346 if (empty($error)) {
347 $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
348 $sql .= " WHERE rowid = ".((int) $rowid);
349
350 dol_syslog(get_class($this)."::remove_product_fournisseur_price", LOG_DEBUG);
351 $resql = $this->db->query($sql);
352 if (!$resql) {
353 $this->error = $this->db->lasterror();
354 $error++;
355 }
356 }
357
358 if (empty($error)) {
359 $this->db->commit();
360 return 1;
361 } else {
362 $this->db->rollback();
363 return -1;
364 }
365 }
366
367
368 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
398 public function update_buyprice(
399 $qty,
400 $buyprice,
401 $user,
402 $price_base_type,
403 $fourn,
404 $availability,
405 $ref_fourn,
406 $tva_tx,
407 $charges = 0,
408 $remise_percent = 0,
409 $remise = 0,
410 $newnpr = 0,
411 $delivery_time_days = 0,
412 $supplier_reputation = '',
413 $localtaxes_array = array(),
414 $newdefaultvatcode = '',
415 $multicurrency_buyprice = 0,
416 $multicurrency_price_base_type = 'HT',
417 $multicurrency_tx = 1,
418 $multicurrency_code = '',
419 $desc_fourn = '',
420 $barcode = '',
421 $fk_barcode_type = 0,
422 $options = array()
423 ) {
424 // phpcs:enable
425 global $conf;
426 //global $mysoc;
427
428 // Clean parameter
429 if (empty($qty)) {
430 $qty = 0;
431 }
432 if (empty($buyprice)) {
433 $buyprice = 0;
434 }
435 if (empty($charges)) {
436 $charges = 0;
437 }
438 if (empty($availability)) {
439 $availability = 0;
440 }
441 if (empty($remise_percent)) {
442 $remise_percent = 0;
443 }
444 if (empty($supplier_reputation) || $supplier_reputation == -1) {
445 $supplier_reputation = '';
446 }
447 if ($delivery_time_days != '' && !is_numeric($delivery_time_days)) {
448 $delivery_time_days = 0;
449 }
450 if ($price_base_type == 'TTC') {
451 $ttx = $tva_tx;
452 $buyprice /= (1 + ($ttx / 100));
453 }
454
455 // Multicurrency
456 $multicurrency_unitBuyPrice = null;
457 $fk_multicurrency = null;
458 if (isModEnabled("multicurrency")) {
459 if (empty($multicurrency_tx)) {
460 $multicurrency_tx = 1;
461 }
462 if (empty($multicurrency_buyprice)) {
463 $multicurrency_buyprice = 0;
464 }
465 if ($multicurrency_price_base_type == 'TTC') {
466 $ttx = $tva_tx;
467 $multicurrency_buyprice /= (1 + ($ttx / 100));
468 }
469 $multicurrency_buyprice = (float) price2num($multicurrency_buyprice, 'MU');
470 $multicurrency_unitBuyPrice = (float) price2num((float) $multicurrency_buyprice / $qty, 'MU');
471
472 $buyprice = (float) $multicurrency_buyprice / $multicurrency_tx;
473 $fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $multicurrency_code);
474 }
475
476 $buyprice = (float) price2num($buyprice, 'MU');
477 $charges = (float) price2num($charges, 'MU');
478 $qty = (float) price2num($qty, 'MS');
479 $unitBuyPrice = (float) price2num($buyprice / $qty, 'MU');
480
481 // We can have a purchase ref that need to buy 100 min for a given price and with a packaging of 50.
482 $packaging = price2num((empty($this->product_fourn_packaging) ? $qty : $this->product_fourn_packaging), 'MS');
483
484 $error = 0;
485 $now = dol_now();
486
487 $newvat = $tva_tx;
488
489 if (count($localtaxes_array) > 0) {
490 $localtaxtype1 = $localtaxes_array['0'];
491 $localtax1 = $localtaxes_array['1'];
492 $localtaxtype2 = $localtaxes_array['2'];
493 $localtax2 = $localtaxes_array['3'];
494 } else { // old method. deprecated because it can't retrieve type
495 $localtaxtype1 = '0';
496 $localtax1 = get_localtax($newvat, 1);
497 $localtaxtype2 = '0';
498 $localtax2 = get_localtax($newvat, 2);
499 }
500 if (empty($localtax1)) {
501 $localtax1 = 0; // If = '' then = 0
502 }
503 if (empty($localtax2)) {
504 $localtax2 = 0; // If = '' then = 0
505 }
506
507 $this->db->begin();
508
509 if ($this->product_fourn_price_id > 0) {
510 // check if price already logged, if not first log current price
511 $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id);
512 if (is_array($logPrices) && count($logPrices) == 0) {
513 $currentPfp = new self($this->db);
514 $result = $currentPfp->fetch_product_fournisseur_price($this->product_fourn_price_id);
515 if ($result > 0 && $currentPfp->fourn_price != 0) {
516 $currentPfpUser = new User($this->db);
517 $result = $currentPfpUser->fetch($currentPfp->user_id);
518 if ($result > 0) {
519 $currentPfp->logPrice(
520 $currentPfpUser,
521 $currentPfp->date_creation,
522 (float) $currentPfp->fourn_price,
523 $currentPfp->fourn_qty,
524 $currentPfp->fourn_multicurrency_price,
525 $currentPfp->fourn_multicurrency_unitprice,
526 $currentPfp->fourn_multicurrency_tx,
527 $currentPfp->fourn_multicurrency_id,
528 $currentPfp->fourn_multicurrency_code
529 );
530 }
531 }
532 }
533 $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
534 $sql .= " SET fk_user = ".((int) $user->id)." ,";
535 $sql .= " datec = '".$this->db->idate($now)."' ,"; // Note: Even if this is an update, we update the creation date as the log of each change is tracked into product_fournisseur_log.
536 $sql .= " ref_fourn = '".$this->db->escape($ref_fourn)."',";
537 $sql .= " desc_fourn = '".$this->db->escape($desc_fourn)."',";
538 $sql .= " price = ".((float) $buyprice).",";
539 $sql .= " quantity = ".((float) $qty).",";
540 $sql .= " remise_percent = ".((float) $remise_percent).",";
541 $sql .= " remise = ".((float) $remise).",";
542 $sql .= " unitprice = ".((float) $unitBuyPrice).",";
543 $sql .= " fk_availability = ".((int) $availability).",";
544 $sql .= " multicurrency_price = ".(isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
545 $sql .= " multicurrency_unitprice = ".(isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
546 $sql .= " multicurrency_tx = ".(isset($multicurrency_tx) ? "'".$this->db->escape((string) $multicurrency_tx)."'" : '1').",";
547 $sql .= " fk_multicurrency = ".(isset($fk_multicurrency) ? (int) $fk_multicurrency : 'null').",";
548 $sql .= " multicurrency_code = ".(isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
549 $sql .= " entity = ".((int) $conf->entity).",";
550 $sql .= " tva_tx = ".((float) price2num($tva_tx)).",";
551 // TODO Add localtax1 and localtax2
552 //$sql.= " localtax1_tx=".($localtax1>=0?$localtax1:'NULL').",";
553 //$sql.= " localtax2_tx=".($localtax2>=0?$localtax2:'NULL').",";
554 //$sql.= " localtax1_type=".($localtaxtype1!=''?"'".$this->db->escape($localtaxtype1)."'":"'0'").",";
555 //$sql.= " localtax2_type=".($localtaxtype2!=''?"'".$this->db->escape($localtaxtype2)."'":"'0'").",";
556 $sql .= " default_vat_code = ".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
557 $sql .= " info_bits = ".((int) $newnpr).",";
558 $sql .= " charges = ".((float) $charges).","; // deprecated
559 $sql .= " delivery_time_days = ".($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
560 $sql .= " supplier_reputation = ".(empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
561 $sql .= " barcode = ".(empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
562 $sql .= " fk_barcode_type = ".(empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape((string) $fk_barcode_type)."'");
563 if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
564 $sql .= ", packaging = ".(empty($packaging) ? 1 : $packaging);
565 }
566 $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
567
568 if (!$error) {
569 if (!empty($options) && is_array($options)) {
570 $productfournisseurprice = new ProductFournisseurPrice($this->db);
571 $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
572 if ($res > 0) {
573 foreach ($options as $key => $value) {
574 $productfournisseurprice->array_options[$key] = $value;
575 }
576 $res = $productfournisseurprice->update($user);
577 if ($res < 0) {
578 $this->setErrorsFromObject($productfournisseurprice);
579 $error++;
580 }
581 }
582 }
583 }
584
585 // TODO Add price_base_type and price_ttc
586
587 dol_syslog(get_class($this).'::update_buyprice update knowing id of line = product_fourn_price_id = '.$this->product_fourn_price_id, LOG_DEBUG);
588 $resql = $this->db->query($sql);
589 if ($resql) {
590 // Call trigger
591 $result = $this->call_trigger('PRODUCT_BUYPRICE_MODIFY', $user);
592 if ($result < 0) {
593 $error++;
594 }
595 // End call triggers
596 if (!$error && !getDolGlobalString('PRODUCT_PRICE_SUPPLIER_NO_LOG')) {
597 $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, (float) $multicurrency_unitBuyPrice, $multicurrency_tx, (int) $fk_multicurrency, $multicurrency_code);
598 if ($result < 0) {
599 $error++;
600 }
601 }
602 if (empty($error)) {
603 $this->db->commit();
604 return $this->product_fourn_price_id;
605 } else {
606 $this->db->rollback();
607 return -1;
608 }
609 } else {
610 $this->error = $this->db->error()." sql=".$sql;
611 $this->db->rollback();
612 return -2;
613 }
614 } else {
615 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);
616
617 // Delete price for this quantity
618 $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
619 $sql .= " WHERE fk_soc = ".((int) $fourn->id)." AND ref_fourn = '".$this->db->escape($ref_fourn)."' AND quantity = ".((float) $qty)." AND entity = ".((int) $conf->entity);
620 $resql = $this->db->query($sql);
621 if ($resql) {
622 // Add price for this quantity to supplier
623 $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price(";
624 $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
625 $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";
626 if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
627 $sql .= ", packaging";
628 }
629 $sql .= ") values(";
630 $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
631 $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
632 $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape((string) $multicurrency_tx)."'" : '1').",";
633 $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape((string) $fk_multicurrency)."'" : 'null').",";
634 $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
635 $sql .= " '".$this->db->idate($now)."',";
636 $sql .= " ".((int) $this->id).",";
637 $sql .= " ".((int) $fourn->id).",";
638 $sql .= " '".$this->db->escape($ref_fourn)."',";
639 $sql .= " '".$this->db->escape($desc_fourn)."',";
640 $sql .= " ".((int) $user->id).",";
641 $sql .= " ".price2num($buyprice).",";
642 $sql .= " ".((float) $qty).",";
643 $sql .= " ".((float) $remise_percent).",";
644 $sql .= " ".((float) $remise).",";
645 $sql .= " ".price2num($unitBuyPrice).",";
646 $sql .= " ".price2num($tva_tx).",";
647 $sql .= " ".price2num($charges).",";
648 $sql .= " ".((int) $availability).",";
649 $sql .= " ".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
650 $sql .= " ".((int) $newnpr).",";
651 $sql .= $conf->entity.",";
652 $sql .= ($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
653 $sql .= (empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
654 $sql .= (empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
655 $sql .= (empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape((string) $fk_barcode_type)."'");
656 if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
657 $sql .= ", ".(empty($this->packaging) ? '1' : "'".$this->db->escape((string) $this->packaging)."'");
658 }
659 $sql .= ")";
660
661 $this->product_fourn_price_id = 0;
662
663 $resql = $this->db->query($sql);
664 if ($resql) {
665 $this->product_fourn_price_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur_price");
666 } else {
667 $this->error = $this->db->lasterror();
668 $error++;
669 }
670
671 if (!$error) {
672 if (!empty($options) && is_array($options)) {
673 $productfournisseurprice = new ProductFournisseurPrice($this->db);
674 $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
675 if ($res > 0) {
676 foreach ($options as $key => $value) {
677 $productfournisseurprice->array_options[$key] = $value;
678 }
679 $res = $productfournisseurprice->update($user);
680 if ($res < 0) {
681 $this->error = $productfournisseurprice->error;
682 $this->errors = $productfournisseurprice->errors;
683 $error++;
684 }
685 }
686 }
687 }
688
689 if (!$error && !getDolGlobalString('PRODUCT_PRICE_SUPPLIER_NO_LOG')) {
690 // Add record into log table
691 // $this->product_fourn_price_id must be set
692 $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, (float) $multicurrency_unitBuyPrice, $multicurrency_tx, (int) $fk_multicurrency, $multicurrency_code);
693 if ($result < 0) {
694 $error++;
695 }
696 }
697
698 if (!$error) {
699 // Call trigger
700 $result = $this->call_trigger('PRODUCT_BUYPRICE_CREATE', $user);
701 if ($result < 0) {
702 $error++;
703 }
704 // End call triggers
705
706 if (empty($error)) {
707 $this->db->commit();
708 return $this->product_fourn_price_id;
709 } else {
710 $this->db->rollback();
711 return -1;
712 }
713 } else {
714 $this->error = $this->db->lasterror()." sql=".$sql;
715 $this->db->rollback();
716 return -2;
717 }
718 } else {
719 $this->error = $this->db->lasterror()." sql=".$sql;
720 $this->db->rollback();
721 return -1;
722 }
723 }
724 }
725
726 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
734 public function fetch_product_fournisseur_price($rowid, $ignore_expression = 0)
735 {
736 // phpcs:enable
737 $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,";
738 $sql .= " pfp.fk_soc, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_product, pfp.charges, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
739 $sql .= " pfp.supplier_reputation, pfp.fk_user, pfp.datec,";
740 $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
741 $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging,";
742 $sql .= " p.ref as product_ref, p.tosell as status, p.tobuy as status_buy";
743 $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p";
744 $sql .= " WHERE pfp.rowid = ".(int) $rowid;
745 $sql .= " AND pfp.fk_product = p.rowid";
746
747 dol_syslog(get_class($this)."::fetch_product_fournisseur_price", LOG_DEBUG);
748 $resql = $this->db->query($sql);
749 if ($resql) {
750 $obj = $this->db->fetch_object($resql);
751 if ($obj) {
752 $this->product_fourn_price_id = $rowid;
753 $this->id = $obj->fk_product;
754
755 $this->fk_product = $obj->fk_product;
756 $this->product_id = $obj->fk_product;
757 $this->product_ref = $obj->product_ref;
758 $this->status = $obj->status;
759 $this->status_buy = $obj->status_buy;
760 $this->fourn_id = $obj->fk_soc;
761 $this->fourn_ref = $obj->ref_fourn; // deprecated
762 $this->ref_supplier = $obj->ref_fourn;
763 $this->desc_supplier = $obj->desc_fourn;
764 $this->fourn_price = $obj->price;
765 $this->fourn_charges = $obj->charges; // when getDolGlobalString('PRODUCT_CHARGES') is set
766 $this->fourn_qty = $obj->quantity;
767 $this->fourn_remise_percent = $obj->remise_percent;
768 $this->fourn_remise = $obj->remise;
769 $this->fourn_unitprice = $obj->unitprice;
770 $this->fourn_tva_tx = $obj->tva_tx;
771 $this->fourn_tva_npr = $obj->fourn_tva_npr;
772 // Add also localtaxes
773 $this->fk_availability = $obj->fk_availability;
774 $this->delivery_time_days = $obj->delivery_time_days;
775 $this->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
776 $this->supplier_reputation = $obj->supplier_reputation;
777 $this->default_vat_code = $obj->default_vat_code;
778 $this->user_id = $obj->fk_user;
779 $this->date_creation = $this->db->jdate($obj->datec);
780 $this->fourn_multicurrency_price = $obj->multicurrency_price;
781 $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
782 $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
783 $this->fourn_multicurrency_id = $obj->fk_multicurrency;
784 $this->fourn_multicurrency_code = $obj->multicurrency_code;
785 if (isModEnabled('barcode')) {
786 $this->fourn_barcode = $obj->barcode; // deprecated
787 $this->fourn_fk_barcode_type = $obj->fk_barcode_type; // deprecated
788 $this->supplier_barcode = $obj->barcode;
789 $this->supplier_fk_barcode_type = $obj->fk_barcode_type;
790 }
791 $this->packaging = (float) $obj->packaging;
792
793 if (isModEnabled('dynamicprices') && empty($ignore_expression) && !empty($this->fk_supplier_price_expression)) {
794 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
795 $priceparser = new PriceParser($this->db);
796 $price_result = $priceparser->parseProductSupplier($this);
797 if ($price_result >= 0) {
798 $this->fourn_price = $price_result;
799 //recalculation of unitprice, as probably the price changed...
800 if ($this->fourn_qty != 0) {
801 $this->fourn_unitprice = price2num($this->fourn_price / $this->fourn_qty, 'MU');
802 } else {
803 $this->fourn_unitprice = "";
804 }
805 }
806 }
807
808 return 1;
809 } else {
810 return 0;
811 }
812 } else {
813 $this->error = $this->db->lasterror();
814 return -1;
815 }
816 }
817
818
819 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
832 public function list_product_fournisseur_price($prodid, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0, $socid = 0)
833 {
834 // phpcs:enable
835 $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, p.fk_unit as product_fk_unit, ";
836 $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,";
837 $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,";
838 $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code, pfp.datec, pfp.tms,";
839 $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging, pfp.status as pfstatus";
840 $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."societe as s";
841 $sql .= " WHERE pfp.entity IN (".getEntity('productsupplierprice').")";
842 $sql .= " AND pfp.fk_soc = s.rowid AND pfp.fk_product = p.rowid";
843 $sql .= ($socid > 0 ? ' AND pfp.fk_soc = '.((int) $socid) : '');
844 $sql .= " AND s.status = 1"; // only enabled company selected
845 $sql .= " AND pfp.fk_product = ".((int) $prodid);
846 if (empty($sortfield)) {
847 $sql .= " ORDER BY s.nom, pfp.quantity, pfp.price";
848 } else {
849 $sql .= $this->db->order($sortfield, $sortorder);
850 }
851 $sql .= $this->db->plimit($limit, $offset);
852 dol_syslog(get_class($this)."::list_product_fournisseur_price", LOG_DEBUG);
853
854 $resql = $this->db->query($sql);
855 if ($resql) {
856 $retarray = array();
857
858 while ($record = $this->db->fetch_array($resql)) {
859 //define base attribute
860 $prodfourn = new ProductFournisseur($this->db);
861
862 $prodfourn->product_ref = $record["product_ref"];
863 $prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"];
864 $prodfourn->status = $record["status"];
865 $prodfourn->status_buy = $record["status_buy"];
866 $prodfourn->product_fourn_id = $record["product_fourn_id"];
867 $prodfourn->product_fourn_entity = $record["entity"];
868 $prodfourn->ref_supplier = $record["ref_fourn"];
869 $prodfourn->fourn_ref = $record["ref_fourn"];
870 $prodfourn->desc_supplier = $record["desc_fourn"];
871 $prodfourn->fourn_price = $record["price"];
872 $prodfourn->fourn_qty = $record["quantity"];
873 $prodfourn->fourn_remise_percent = $record["remise_percent"];
874 $prodfourn->fourn_remise = $record["remise"];
875 $prodfourn->fourn_unitprice = $record["unitprice"];
876 $prodfourn->fourn_charges = $record["charges"]; // when getDolGlobalString('PRODUCT_CHARGES') is set
877 $prodfourn->fourn_tva_tx = $record["tva_tx"];
878 $prodfourn->fourn_id = $record["fourn_id"];
879 $prodfourn->fourn_name = $record["supplier_name"];
880 $prodfourn->fk_availability = $record["fk_availability"];
881 $prodfourn->delivery_time_days = $record["delivery_time_days"];
882 $prodfourn->id = $prodid;
883 $prodfourn->fourn_tva_npr = $record["info_bits"];
884 $prodfourn->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
885 $prodfourn->supplier_reputation = $record["supplier_reputation"];
886 $prodfourn->fourn_date_creation = $this->db->jdate($record['datec']);
887 $prodfourn->fourn_date_modification = $this->db->jdate($record['tms']);
888 // Carry the product's default measuring unit so the AJAX caller
889 // (getSupplierPrices.php) can return it to the line form, which
890 // then preselects #units like the customer side already does for
891 // idprod (see issues #34610 client-side and #38636 supplier-side).
892 $prodfourn->fk_unit = $record["product_fk_unit"];
893
894 $prodfourn->fourn_multicurrency_price = $record["multicurrency_price"];
895 $prodfourn->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
896 $prodfourn->fourn_multicurrency_tx = $record["multicurrency_tx"];
897 $prodfourn->fourn_multicurrency_id = $record["fk_multicurrency"];
898 $prodfourn->fourn_multicurrency_code = $record["multicurrency_code"];
899
900 $prodfourn->packaging = (float) $record["packaging"];
901 $prodfourn->status = $record["pfstatus"];
902
903 if (isModEnabled('barcode')) {
904 $prodfourn->supplier_barcode = $record["barcode"];
905 $prodfourn->supplier_fk_barcode_type = $record["fk_barcode_type"];
906 }
907
908 if (isModEnabled('dynamicprices') && !empty($prodfourn->fk_supplier_price_expression)) {
909 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
910 $priceparser = new PriceParser($this->db);
911 $price_result = $priceparser->parseProductSupplier($prodfourn);
912 if ($price_result >= 0) {
913 $prodfourn->fourn_price = $price_result;
914 // Set to null on purpose an will have non-null value before method return @phan-suppress-next-line PhanTypeMismatchPropertyProbablyReal
915 $prodfourn->fourn_unitprice = null; //force recalculation of unitprice, as probably the price changed... @phpstan-ignore-line
916 }
917 }
918
919 if (!isset($prodfourn->fourn_unitprice)) {
920 if ($prodfourn->fourn_qty != 0) {
921 $prodfourn->fourn_unitprice = price2num($prodfourn->fourn_price / $prodfourn->fourn_qty, 'MU');
922 } else {
923 $prodfourn->fourn_unitprice = "";
924 }
925 }
926
927 $retarray[] = $prodfourn;
928 }
929
930 $this->db->free($resql);
931 return $retarray;
932 } else {
933 $this->error = $this->db->error();
934 return -1;
935 }
936 }
937
938 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
948 public function find_min_price_product_fournisseur($prodid, $qty = 0, $socid = 0)
949 {
950 // phpcs:enable
951 if (empty($prodid)) {
952 dol_syslog("Warning function find_min_price_product_fournisseur were called with prodid empty. May be a bug.", LOG_WARNING);
953 return 0;
954 }
955
956 $this->product_fourn_price_id = 0;
957 $this->product_fourn_id = 0;
958 $this->fourn_ref = '';
959 $this->fourn_price = 0;
960 $this->fourn_qty = 0;
961 $this->fourn_remise_percent = 0;
962 $this->fourn_remise = 0;
963 $this->fourn_unitprice = 0;
964 $this->fourn_id = 0;
965 $this->fourn_name = '';
966 $this->delivery_time_days = 0;
967 $this->id = 0;
968
969 $this->fourn_multicurrency_price = 0;
970 $this->fourn_multicurrency_unitprice = 0;
971 $this->fourn_multicurrency_tx = 0;
972 $this->fourn_multicurrency_id = 0;
973 $this->fourn_multicurrency_code = '';
974
975 $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
976 $sql .= " pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
977 $sql .= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges,";
978 $sql .= " pfp.remise, pfp.remise_percent, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
979 $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code";
980 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
981 $sql .= " WHERE s.entity IN (".getEntity('societe').")";
982 $sql .= " AND pfp.entity IN (".getEntity('productsupplierprice').")";
983 $sql .= " AND pfp.fk_product = ".((int) $prodid);
984 $sql .= " AND pfp.fk_soc = s.rowid";
985 $sql .= " AND s.status = 1"; // only enabled society
986 if ($qty > 0) {
987 $sql .= " AND pfp.quantity <= ".((float) $qty);
988 }
989 if ($socid > 0) {
990 $sql .= ' AND pfp.fk_soc = '.((int) $socid);
991 }
992
993 dol_syslog(get_class($this)."::find_min_price_product_fournisseur", LOG_DEBUG);
994
995 $resql = $this->db->query($sql);
996 if ($resql) {
997 $record_array = array();
998
999 //Store each record to array for later search of min
1000 while ($record = $this->db->fetch_array($resql)) {
1001 $record_array[] = $record;
1002 }
1003
1004 if (count($record_array) == 0) {
1005 $this->db->free($resql);
1006 return 0;
1007 } else {
1008 $min = -1;
1009 foreach ($record_array as $record) {
1010 $fourn_price = $record["price"];
1011 // calculate unit price for quantity 1
1012 $fourn_unitprice = $record["unitprice"];
1013 $fourn_unitprice_with_discount = $record["unitprice"] * (1 - $record["remise_percent"] / 100);
1014
1015 if (isModEnabled('dynamicprices') && !empty($record["fk_supplier_price_expression"])) {
1016 $prod_supplier = new ProductFournisseur($this->db);
1017 $prod_supplier->product_fourn_price_id = $record["product_fourn_price_id"];
1018 $prod_supplier->id = $prodid;
1019 $prod_supplier->fourn_qty = $record["quantity"];
1020 $prod_supplier->fourn_tva_tx = $record["tva_tx"];
1021 $prod_supplier->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
1022
1023 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
1024 $priceparser = new PriceParser($this->db);
1025 $price_result = $priceparser->parseProductSupplier($prod_supplier);
1026 if ($price_result >= 0) {
1027 $fourn_price = price2num($price_result, 'MU');
1028 if ($record["quantity"] != 0) {
1029 $fourn_unitprice = price2num((float) $fourn_price / $record["quantity"], 'MU');
1030 } else {
1031 $fourn_unitprice = $fourn_price;
1032 }
1033 $fourn_unitprice_with_discount = (float) $fourn_unitprice * (1 - $record["remise_percent"] / 100);
1034 }
1035 }
1036
1037 if ($fourn_unitprice_with_discount < $min || $min == -1) {
1038 $this->id = $prodid;
1039 $this->product_fourn_price_id = $record["product_fourn_price_id"];
1040 $this->ref_supplier = $record["ref_fourn"];
1041 $this->ref_fourn = $record["ref_fourn"]; // deprecated
1042 $this->fourn_ref = $record["ref_fourn"]; // deprecated
1043 $this->fourn_price = $fourn_price;
1044 $this->fourn_qty = $record["quantity"];
1045 $this->fourn_remise_percent = $record["remise_percent"];
1046 $this->fourn_remise = $record["remise"];
1047 $this->fourn_unitprice = $fourn_unitprice;
1048 $this->fourn_unitprice_with_discount = $fourn_unitprice_with_discount;
1049 $this->fourn_charges = $record["charges"]; // when getDolGlobalString('PRODUCT_CHARGES') is set
1050 $this->fourn_tva_tx = $record["tva_tx"];
1051 $this->fourn_id = $record["fourn_id"]; // thirdparty id
1052 $this->fourn_name = $record["supplier_name"];
1053 $this->delivery_time_days = $record["delivery_time_days"];
1054 // False positive @phan-suppress-next-line PhanTypeMismatchProperty
1055 $this->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
1056 $this->fourn_multicurrency_price = $record["multicurrency_price"];
1057 $this->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
1058 $this->fourn_multicurrency_tx = $record["multicurrency_tx"];
1059 $this->fourn_multicurrency_id = $record["fk_multicurrency"];
1060 $this->fourn_multicurrency_code = $record["multicurrency_code"];
1061
1062 $min = $fourn_unitprice_with_discount;
1063 }
1064 }
1065 }
1066
1067 $this->db->free($resql);
1068
1069 return $this->product_fourn_price_id;
1070 } else {
1071 $this->error = $this->db->error();
1072 return -1;
1073 }
1074 }
1075
1082 public function setSupplierPriceExpression($expression_id)
1083 {
1084 // Clean parameters
1085 $this->db->begin();
1086 $expression_id = $expression_id != 0 ? $expression_id : 'NULL';
1087
1088 $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
1089 $sql .= " SET fk_supplier_price_expression = ".((int) $expression_id);
1090 $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
1091
1092 dol_syslog(get_class($this)."::setSupplierPriceExpression", LOG_DEBUG);
1093
1094 $resql = $this->db->query($sql);
1095 if ($resql) {
1096 $this->db->commit();
1097 return 1;
1098 } else {
1099 $this->error = $this->db->error()." sql=".$sql;
1100 $this->db->rollback();
1101 return -1;
1102 }
1103 }
1104
1115 public function getSocNomUrl($withpicto = 0, $option = 'supplier', $maxlen = 0, $notooltip = 0)
1116 {
1117 $thirdparty = new Fournisseur($this->db);
1118 $thirdparty->fetch($this->fourn_id);
1119
1120 return $thirdparty->getNomUrl($withpicto, $option, $maxlen, $notooltip);
1121 }
1122
1123 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1135 public function display_price_product_fournisseur($showunitprice = 1, $showsuptitle = 1, $maxlen = 0, $notooltip = 0, $productFournList = array())
1136 {
1137 // phpcs:enable
1138 global $conf, $langs, $user;
1139
1140 $out = '';
1141 $langs->load("suppliers");
1142 if (count($productFournList) > 0) {
1143 $out .= '<table class="centpercent liste nomarginbottom">';
1144 $out .= '<tr class="liste_titre"><td class="liste_titre right">'.($showunitprice ? $langs->trans("Price").' '.$langs->trans("HT") : '').'</td>';
1145 $out .= '<td class="liste_titre right">'.($showunitprice ? $langs->trans("QtyMin") : '').'</td>';
1146 $out .= '<td class="liste_titre">'.$langs->trans("Supplier").'</td>';
1147 $out .= '<td class="liste_titre">'.$langs->trans("SupplierRef").'</td></tr>';
1148 foreach ($productFournList as $productFourn) {
1149 $out .= '<tr><td class="right">'.($showunitprice ? price($productFourn->fourn_unitprice * (1 - $productFourn->fourn_remise_percent / 100) - $productFourn->fourn_remise) : '').'</td>';
1150 $out .= '<td class="right">'.($showunitprice ? $productFourn->fourn_qty : '').'</td>';
1151 $out .= '<td>'.$productFourn->getSocNomUrl(1, 'supplier', $maxlen, $notooltip).'</td>';
1152 $out .= '<td>'.dolPrintHTML($productFourn->fourn_ref).'</td></tr>';
1153 }
1154 $out .= '</table>';
1155 } else {
1156 $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") : '');
1157 if ($user->hasRight("fournisseur", "read")) { // Without permission, never show the best supplier seller
1158 $out .= ($showunitprice ? ' &nbsp; <span class="opacitymedium">(</span>' : '');
1159 $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;
1160 $out .= ($showunitprice ? '<span class="opacitymedium">)</span>' : '');
1161 }
1162 }
1163 return $out;
1164 }
1165
1174 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1175 {
1176 $tables = array(
1177 'product_fournisseur_price'
1178 );
1179
1180 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1181 }
1182
1191 public static function replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
1192 {
1193 $tables = array(
1194 'product_fournisseur_price'
1195 );
1196
1197 return CommonObject::commonReplaceProduct($dbs, $origin_id, $dest_id, $tables);
1198 }
1199
1210 public function listProductFournisseurPriceLog($product_fourn_price_id, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0)
1211 {
1212 $sql = "SELECT";
1213 $sql .= " u.lastname,";
1214 $sql .= " pfpl.rowid, pfp.ref_fourn as supplier_ref, pfpl.datec,";
1215 $sql .= " pfpl.price, pfpl.quantity,";
1216 $sql .= " pfpl.fk_multicurrency, pfpl.multicurrency_code, pfpl.multicurrency_tx, pfpl.multicurrency_price, pfpl.multicurrency_unitprice";
1217 $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price_log as pfpl,";
1218 $sql .= " ".MAIN_DB_PREFIX."product_fournisseur_price as pfp,";
1219 $sql .= " ".MAIN_DB_PREFIX."user as u";
1220 $sql .= " WHERE pfp.entity IN (".getEntity('productprice').")";
1221 $sql .= " AND pfpl.fk_user = u.rowid";
1222 $sql .= " AND pfp.rowid = pfpl.fk_product_fournisseur";
1223 $sql .= " AND pfpl.fk_product_fournisseur = ".((int) $product_fourn_price_id);
1224 if (empty($sortfield)) {
1225 $sql .= " ORDER BY pfpl.datec";
1226 } else {
1227 $sql .= $this->db->order($sortfield, $sortorder);
1228 }
1229 $sql .= $this->db->plimit($limit, $offset);
1230 dol_syslog(get_class($this)."::list_product_fournisseur_price_log", LOG_DEBUG);
1231
1232 $resql = $this->db->query($sql);
1233 if ($resql) {
1234 $retarray = array();
1235
1236 while ($obj = $this->db->fetch_object($resql)) {
1237 $tmparray = array();
1238 $tmparray['rowid'] = $obj->rowid;
1239 $tmparray['supplier_ref'] = $obj->supplier_ref;
1240 $tmparray['datec'] = $this->db->jdate($obj->datec);
1241 $tmparray['lastname'] = $obj->lastname;
1242 $tmparray['price'] = $obj->price;
1243 $tmparray['quantity'] = $obj->quantity;
1244 $tmparray['fk_multicurrency'] = $obj->fk_multicurrency;
1245 $tmparray['multicurrency_code'] = $obj->multicurrency_code;
1246 $tmparray['multicurrency_tx'] = $obj->multicurrency_tx;
1247 $tmparray['multicurrency_price'] = $obj->multicurrency_price;
1248 $tmparray['multicurrency_unitprice'] = $obj->multicurrency_unitprice;
1249
1250 $retarray[] = $tmparray;
1251 }
1252
1253 $this->db->free($resql);
1254 return $retarray;
1255 } else {
1256 $this->error = $this->db->error();
1257 return -1;
1258 }
1259 }
1260
1268 public function displayPriceProductFournisseurLog($productFournLogList = array())
1269 {
1270 global $conf, $langs;
1271
1272 $out = '';
1273 $langs->load("suppliers");
1274 if (count($productFournLogList) > 0) {
1275 $out .= '<table class="noborder centpercent">';
1276 $out .= '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Date").'</td>';
1277 $out .= '<td class="liste_titre right">'.$langs->trans("Price").'</td>';
1278 //$out .= '<td class="liste_titre right">'.$langs->trans("QtyMin").'</td>';
1279 $out .= '<td class="liste_titre">'.$langs->trans("User").'</td></tr>';
1280 foreach ($productFournLogList as $productFournLog) {
1281 $out .= '<tr><td>'.dol_print_date($productFournLog['datec'], 'dayhour', 'tzuser').'</td>';
1282 $out .= '<td class="right">'.price($productFournLog['price'], 0, $langs, 1, -1, -1, $conf->currency);
1283 if ($productFournLog['multicurrency_code'] != $conf->currency) {
1284 $out .= ' ('.price($productFournLog['multicurrency_price'], 0, $langs, 1, -1, -1, $productFournLog['multicurrency_code']).')';
1285 }
1286 $out .= '</td>';
1287 //$out.= '<td class="right">'.$productFournLog['quantity'].'</td>';
1288 $out .= '<td>'.$productFournLog['lastname'].'</td></tr>';
1289 }
1290 $out .= '</table>';
1291 }
1292 return $out;
1293 }
1294
1295
1310 public function getNomUrl($withpicto = 0, $option = '', $maxlength = 0, $save_lastsearch_value = -1, $notooltip = 0, $morecss = '', $add_label = 0, $sep = ' - ')
1311 {
1312 global $conf, $langs, $hookmanager;
1313
1314 if (!empty($conf->dol_no_mouse_hover)) {
1315 $notooltip = 1; // Force disable tooltips
1316 }
1317
1318 $result = '';
1319 $label = '';
1320
1321 $newref = (string) $this->ref;
1322 if ($maxlength) {
1323 $newref = dol_trunc($newref, $maxlength, 'middle');
1324 }
1325
1326 if (!empty($this->entity)) {
1327 $tmpphoto = $this->show_photos('product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 80);
1328 if ($this->nbphoto > 0) {
1329 $label .= '<div class="photointooltip">';
1330 $label .= $tmpphoto;
1331 $label .= '</div><div style="clear: both;"></div>';
1332 }
1333 }
1334
1335 if ($this->type == Product::TYPE_PRODUCT) {
1336 $label .= img_picto('', 'product').' <u class="paddingrightonly">'.$langs->trans("Product").'</u>';
1337 } elseif ($this->type == Product::TYPE_SERVICE) {
1338 $label .= img_picto('', 'service').' <u class="paddingrightonly">'.$langs->trans("Service").'</u>';
1339 }
1340 if (isset($this->status) && isset($this->status_buy)) {
1341 $label .= ' '.$this->getLibStatut(5, 0);
1342 $label .= ' '.$this->getLibStatut(5, 1);
1343 }
1344
1345 if (!empty($this->ref)) {
1346 $label .= '<br><b>'.$langs->trans('ProductRef').':</b> '.($this->ref ? $this->ref : $this->product_ref);
1347 }
1348 if (!empty($this->label)) {
1349 $label .= '<br><b>'.$langs->trans('ProductLabel').':</b> '.$this->label;
1350 }
1351 $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
1352
1353 if ($this->type == Product::TYPE_PRODUCT || getDolGlobalString('STOCK_SUPPORTS_SERVICES')) {
1354 if (isModEnabled('productbatch')) {
1355 $langs->load("productbatch");
1356 $label .= "<br><b>".$langs->trans("ManageLotSerial").'</b>: '.$this->getLibStatut(0, 2);
1357 }
1358 }
1359 if (isModEnabled('barcode')) {
1360 $label .= '<br><b>'.$langs->trans('BarCode').':</b> '.$this->barcode;
1361 }
1362
1363 if ($this->type == Product::TYPE_PRODUCT) {
1364 if ($this->weight) {
1365 $label .= "<br><b>".$langs->trans("Weight").'</b>: '.$this->weight.' '.measuringUnitString(0, "weight", $this->weight_units);
1366 }
1367 $labelsize = "";
1368 if ($this->length) {
1369 $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Length").'</b>: '.$this->length.' '.measuringUnitString(0, 'size', $this->length_units);
1370 }
1371 if ($this->width) {
1372 $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Width").'</b>: '.$this->width.' '.measuringUnitString(0, 'size', $this->width_units);
1373 }
1374 if ($this->height) {
1375 $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Height").'</b>: '.$this->height.' '.measuringUnitString(0, 'size', $this->height_units);
1376 }
1377 if ($labelsize) {
1378 $label .= "<br>".$labelsize;
1379 }
1380
1381 $labelsurfacevolume = "";
1382 if ($this->surface) {
1383 $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Surface").'</b>: '.$this->surface.' '.measuringUnitString(0, 'surface', $this->surface_units);
1384 }
1385 if ($this->volume) {
1386 $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Volume").'</b>: '.$this->volume.' '.measuringUnitString(0, 'volume', $this->volume_units);
1387 }
1388 if ($labelsurfacevolume) {
1389 $label .= "<br>".$labelsurfacevolume;
1390 }
1391 }
1392
1393 if (isModEnabled('accounting') && $this->status) {
1394 include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1395 $label .= '<br><b>'.$langs->trans('ProductAccountancySellCode').':</b> '.length_accountg($this->accountancy_code_sell);
1396 $label .= '<br><b>'.$langs->trans('ProductAccountancySellIntraCode').':</b> '.length_accountg($this->accountancy_code_sell_intra);
1397 $label .= '<br><b>'.$langs->trans('ProductAccountancySellExportCode').':</b> '.length_accountg($this->accountancy_code_sell_export);
1398 }
1399 if (isModEnabled('accounting') && $this->status_buy) {
1400 include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1401 $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyCode').':</b> '.length_accountg($this->accountancy_code_buy);
1402 $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyIntraCode').':</b> '.length_accountg($this->accountancy_code_buy_intra);
1403 $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyExportCode').':</b> '.length_accountg($this->accountancy_code_buy_export);
1404 }
1405
1406 $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id, 'pfpl.datec', 'DESC', getDolGlobalInt('MAIN_TOOLTIP_PRICELOG_HISTORY_LIMIT', 10)); // set sort order here
1407 if (is_array($logPrices) && count($logPrices) > 0) {
1408 $label .= '<br><br>';
1409 $label .= '<u>'.$langs->trans("History").'</u>';
1410 $label .= $this->displayPriceProductFournisseurLog($logPrices);
1411 }
1412
1413 $url = DOL_URL_ROOT.'/product/price_suppliers.php?id='.((int) $this->id).'&action=create_price&token='.newToken().'&socid='.((int) $this->fourn_id).'&rowid='.((int) $this->product_fourn_price_id);
1414
1415 if ($option != 'nolink') {
1416 // Add param to save lastsearch_values or not
1417 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1418 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1419 $add_save_lastsearch_values = 1;
1420 }
1421 if ($add_save_lastsearch_values) {
1422 $url .= '&save_lastsearch_values=1';
1423 }
1424 }
1425
1426 $allowothertags = array('table', 'tr', 'td');
1427 $linkclose = '';
1428 if (empty($notooltip)) {
1429 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1430 $label = $langs->trans("SupplierRef");
1431 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label, 0, $allowothertags).'"';
1432 }
1433 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label, 0, $allowothertags).'"';
1434 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
1435 } else {
1436 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1437 }
1438
1439 $linkstart = '<a href="'.$url.'"';
1440 $linkstart .= $linkclose.'>';
1441 $linkend = '</a>';
1442
1443 $result .= $linkstart;
1444 if ($withpicto) {
1445 $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, $allowothertags);
1446 }
1447 if ($withpicto != 2) {
1448 $result .= $newref.($this->ref_supplier ? ' ('.$this->ref_supplier.')' : '');
1449 }
1450 $result .= $linkend;
1451 if ($withpicto != 2) {
1452 $result .= (($add_label && $this->label) ? $sep.dol_trunc($this->label, ($add_label > 1 ? $add_label : 0)) : '');
1453 }
1454
1455 global $action;
1456 $hookmanager->initHooks(array($this->element . 'dao'));
1457 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1458 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1459 if ($reshook > 0) {
1460 $result = $hookmanager->resPrint;
1461 } else {
1462 $result .= $hookmanager->resPrint;
1463 }
1464 return $result;
1465 }
1466
1474 public function getLibStatut($mode = 0, $type = 0) // must be compatible with getLibStatut of inherited Product
1475 {
1476 return $this->LibStatut($this->status, $mode);
1477 }
1478
1479 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1488 public function LibStatut($status, $mode = 0, $type = 0)
1489 {
1490 // phpcs:enable
1491 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1492 global $langs;
1493 //$langs->load("mymodule@mymodule");
1494 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Enabled');
1495 $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
1496 $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Enabled');
1497 $this->labelStatusShort[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
1498 }
1499
1500 $statusType = 'status4';
1501 //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
1502 if ($status == self::STATUS_CANCELED) {
1503 $statusType = 'status6';
1504 }
1505
1506 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
1507 }
1508
1524 private function logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice = null, $multicurrency_unitBuyPrice = null, $multicurrency_tx = null, $fk_multicurrency = null, $multicurrency_code = null)
1525 {
1526 // Add record into log table
1527 $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log(";
1528 $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
1529 $sql .= "datec, fk_product_fournisseur,fk_user,price,quantity)";
1530 $sql .= "values(";
1531 $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
1532 $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
1533 $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape((string) $multicurrency_tx)."'" : '1').",";
1534 $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape((string) $fk_multicurrency)."'" : 'null').",";
1535 $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
1536 $sql .= "'".$this->db->idate($datec)."',";
1537 $sql .= " ".((int) $this->product_fourn_price_id).",";
1538 $sql .= " ".((int) $user->id).",";
1539 $sql .= " ".price2num($buyprice).",";
1540 $sql .= " ".price2num($qty);
1541 $sql .= ")";
1542
1543 $resql = $this->db->query($sql);
1544 if (!$resql) {
1545 return -1;
1546 } else {
1547 return 1;
1548 }
1549 }
1550}
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
$object ref
Definition info.php:90
setErrorsFromObject($object)
setErrorsFromObject
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.
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 optionally 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=0, $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.
const TYPE_SERVICE
Service.
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:171
dol_now($mode='gmt')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
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.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
get_localtax($vatrate, $local, $thirdparty_buyer=null, $thirdparty_seller=null, $vatnpr=0)
Return localtax rate for a particular VAT rate, when selling a product with vat $vatrate,...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
measuringUnitString($unitid, $measuring_style='', $unitscale=null, $use_short_label=0, $outputlangs=null)
Return translation label of a unit key.
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition repair.php:125