dolibarr 25.0.0-alpha
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-2026 Frédéric France <frederic.france@free.fr>
10 * Copyright (C) 2020 Pierre Ardoin <mapiolca@me.com>
11 * Copyright (C) 2024-2026 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 // When called on a loaded product, the price line must be one of its own
338 if ($this->id > 0) {
339 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
340 $sql .= " WHERE rowid = ".((int) $rowid)." AND fk_product = ".((int) $this->id);
341 $resql = $this->db->query($sql);
342 if (!$resql || !$this->db->num_rows($resql)) {
343 $this->error = 'ErrorRecordNotFound';
344 return -1;
345 }
346 }
347
348 $this->db->begin();
349
350 // Call trigger
351 $result = $this->call_trigger('PRODUCT_BUYPRICE_DELETE', $user);
352 if ($result < 0) {
353 $error++;
354 }
355 // End call triggers
356
357 if (empty($error)) {
358 $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
359 $sql .= " WHERE rowid = ".((int) $rowid);
360
361 dol_syslog(get_class($this)."::remove_product_fournisseur_price", LOG_DEBUG);
362 $resql = $this->db->query($sql);
363 if (!$resql) {
364 $this->error = $this->db->lasterror();
365 $error++;
366 }
367 }
368
369 if (empty($error)) {
370 $this->db->commit();
371 return 1;
372 } else {
373 $this->db->rollback();
374 return -1;
375 }
376 }
377
378
379 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
409 public function update_buyprice(
410 $qty,
411 $buyprice,
412 $user,
413 $price_base_type,
414 $fourn,
415 $availability,
416 $ref_fourn,
417 $tva_tx,
418 $charges = 0,
419 $remise_percent = 0,
420 $remise = 0,
421 $newnpr = 0,
422 $delivery_time_days = 0,
423 $supplier_reputation = '',
424 $localtaxes_array = array(),
425 $newdefaultvatcode = '',
426 $multicurrency_buyprice = 0,
427 $multicurrency_price_base_type = 'HT',
428 $multicurrency_tx = 1,
429 $multicurrency_code = '',
430 $desc_fourn = '',
431 $barcode = '',
432 $fk_barcode_type = 0,
433 $options = array()
434 ) {
435 // phpcs:enable
436 global $conf;
437 //global $mysoc;
438
439 // Clean parameter
440 if (empty($qty)) {
441 $qty = 0;
442 }
443 if (empty($buyprice)) {
444 $buyprice = 0;
445 }
446 if (empty($charges)) {
447 $charges = 0;
448 }
449 if (empty($availability)) {
450 $availability = 0;
451 }
452 if (empty($remise_percent)) {
453 $remise_percent = 0;
454 }
455 if (empty($supplier_reputation) || $supplier_reputation == -1) {
456 $supplier_reputation = '';
457 }
458 if ($delivery_time_days != '' && !is_numeric($delivery_time_days)) {
459 $delivery_time_days = 0;
460 }
461 if ($price_base_type == 'TTC') {
462 $ttx = $tva_tx;
463 $buyprice /= (1 + ($ttx / 100));
464 }
465
466 // Multicurrency
467 $multicurrency_unitBuyPrice = null;
468 $fk_multicurrency = null;
469 if (isModEnabled("multicurrency")) {
470 if (empty($multicurrency_tx)) {
471 $multicurrency_tx = 1;
472 }
473 if (empty($multicurrency_buyprice)) {
474 $multicurrency_buyprice = 0;
475 }
476 if ($multicurrency_price_base_type == 'TTC') {
477 $ttx = $tva_tx;
478 $multicurrency_buyprice /= (1 + ($ttx / 100));
479 }
480 $multicurrency_buyprice = (float) price2num($multicurrency_buyprice, 'MU');
481 $multicurrency_unitBuyPrice = (float) price2num((float) $multicurrency_buyprice / $qty, 'MU');
482
483 $buyprice = (float) $multicurrency_buyprice / $multicurrency_tx;
484 $fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $multicurrency_code);
485 }
486
487 $buyprice = (float) price2num($buyprice, 'MU');
488 $charges = (float) price2num($charges, 'MU');
489 $qty = (float) price2num($qty, 'MS');
490 $unitBuyPrice = (float) price2num($buyprice / $qty, 'MU');
491
492 // We can have a purchase ref that need to buy 100 min for a given price and with a packaging of 50.
493 $packaging = price2num((empty($this->product_fourn_packaging) ? $qty : $this->product_fourn_packaging), 'MS');
494
495 $error = 0;
496 $now = dol_now();
497
498 $newvat = $tva_tx;
499
500 if (count($localtaxes_array) > 0) {
501 $localtaxtype1 = $localtaxes_array['0'];
502 $localtax1 = $localtaxes_array['1'];
503 $localtaxtype2 = $localtaxes_array['2'];
504 $localtax2 = $localtaxes_array['3'];
505 } else { // old method. deprecated because it can't retrieve type
506 $localtaxtype1 = '0';
507 $localtax1 = get_localtax($newvat, 1);
508 $localtaxtype2 = '0';
509 $localtax2 = get_localtax($newvat, 2);
510 }
511 if (empty($localtax1)) {
512 $localtax1 = 0; // If = '' then = 0
513 }
514 if (empty($localtax2)) {
515 $localtax2 = 0; // If = '' then = 0
516 }
517
518 $this->db->begin();
519
520 if ($this->product_fourn_price_id > 0) {
521 // check if price already logged, if not first log current price
522 $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id);
523 if (is_array($logPrices) && count($logPrices) == 0) {
524 $currentPfp = new self($this->db);
525 $result = $currentPfp->fetch_product_fournisseur_price($this->product_fourn_price_id);
526 if ($result > 0 && $currentPfp->fourn_price != 0) {
527 $currentPfpUser = new User($this->db);
528 $result = $currentPfpUser->fetch($currentPfp->user_id);
529 if ($result > 0) {
530 $currentPfp->logPrice(
531 $currentPfpUser,
532 $currentPfp->date_creation,
533 (float) $currentPfp->fourn_price,
534 $currentPfp->fourn_qty,
535 $currentPfp->fourn_multicurrency_price,
536 $currentPfp->fourn_multicurrency_unitprice,
537 $currentPfp->fourn_multicurrency_tx,
538 $currentPfp->fourn_multicurrency_id,
539 $currentPfp->fourn_multicurrency_code
540 );
541 }
542 }
543 }
544 $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
545 $sql .= " SET fk_user = ".((int) $user->id)." ,";
546 $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.
547 $sql .= " ref_fourn = '".$this->db->escape($ref_fourn)."',";
548 $sql .= " desc_fourn = '".$this->db->escape($desc_fourn)."',";
549 $sql .= " price = ".((float) $buyprice).",";
550 $sql .= " quantity = ".((float) $qty).",";
551 $sql .= " remise_percent = ".((float) $remise_percent).",";
552 $sql .= " remise = ".((float) $remise).",";
553 $sql .= " unitprice = ".((float) $unitBuyPrice).",";
554 $sql .= " fk_availability = ".((int) $availability).",";
555 $sql .= " multicurrency_price = ".(isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
556 $sql .= " multicurrency_unitprice = ".(isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
557 $sql .= " multicurrency_tx = ".(isset($multicurrency_tx) ? "'".$this->db->escape((string) $multicurrency_tx)."'" : '1').",";
558 $sql .= " fk_multicurrency = ".(isset($fk_multicurrency) ? (int) $fk_multicurrency : 'null').",";
559 $sql .= " multicurrency_code = ".(isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
560 $sql .= " entity = ".((int) $conf->entity).",";
561 $sql .= " tva_tx = ".((float) price2num($tva_tx)).",";
562 // TODO Add localtax1 and localtax2
563 //$sql.= " localtax1_tx=".($localtax1>=0?$localtax1:'NULL').",";
564 //$sql.= " localtax2_tx=".($localtax2>=0?$localtax2:'NULL').",";
565 //$sql.= " localtax1_type=".($localtaxtype1!=''?"'".$this->db->escape($localtaxtype1)."'":"'0'").",";
566 //$sql.= " localtax2_type=".($localtaxtype2!=''?"'".$this->db->escape($localtaxtype2)."'":"'0'").",";
567 $sql .= " default_vat_code = ".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
568 $sql .= " info_bits = ".((int) $newnpr).",";
569 $sql .= " charges = ".((float) $charges).","; // deprecated
570 $sql .= " delivery_time_days = ".($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
571 $sql .= " supplier_reputation = ".(empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
572 $sql .= " barcode = ".(empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
573 $sql .= " fk_barcode_type = ".(empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape((string) $fk_barcode_type)."'");
574 if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
575 $sql .= ", packaging = ".(empty($packaging) ? 1 : ((float) $packaging));
576 }
577 $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
578
579 if (!$error) {
580 if (!empty($options) && is_array($options)) {
581 $productfournisseurprice = new ProductFournisseurPrice($this->db);
582 $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
583 if ($res > 0) {
584 foreach ($options as $key => $value) {
585 $productfournisseurprice->array_options[$key] = $value;
586 }
587 $res = $productfournisseurprice->update($user);
588 if ($res < 0) {
589 $this->setErrorsFromObject($productfournisseurprice);
590 $error++;
591 }
592 }
593 }
594 }
595
596 // TODO Add price_base_type and price_ttc
597
598 dol_syslog(get_class($this).'::update_buyprice update knowing id of line = product_fourn_price_id = '.$this->product_fourn_price_id, LOG_DEBUG);
599 $resql = $this->db->query($sql);
600 if ($resql) {
601 // Call trigger
602 $result = $this->call_trigger('PRODUCT_BUYPRICE_MODIFY', $user);
603 if ($result < 0) {
604 $error++;
605 }
606 // End call triggers
607 if (!$error && !getDolGlobalString('PRODUCT_PRICE_SUPPLIER_NO_LOG')) {
608 $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, (float) $multicurrency_unitBuyPrice, $multicurrency_tx, (int) $fk_multicurrency, $multicurrency_code);
609 if ($result < 0) {
610 $error++;
611 }
612 }
613 if (empty($error)) {
614 $this->db->commit();
615 return $this->product_fourn_price_id;
616 } else {
617 $this->db->rollback();
618 return -1;
619 }
620 } else {
621 $this->error = $this->db->error()." sql=".$sql;
622 $this->db->rollback();
623 return -2;
624 }
625 } else {
626 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);
627
628 // Delete price for this quantity
629 $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
630 $sql .= " WHERE fk_soc = ".((int) $fourn->id)." AND ref_fourn = '".$this->db->escape($ref_fourn)."' AND quantity = ".((float) $qty)." AND entity = ".((int) $conf->entity);
631 $resql = $this->db->query($sql);
632 if ($resql) {
633 // Add price for this quantity to supplier
634 $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price(";
635 $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
636 $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";
637 if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
638 $sql .= ", packaging";
639 }
640 $sql .= ") values(";
641 $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
642 $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
643 $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape((string) $multicurrency_tx)."'" : '1').",";
644 $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape((string) $fk_multicurrency)."'" : 'null').",";
645 $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
646 $sql .= " '".$this->db->idate($now)."',";
647 $sql .= " ".((int) $this->id).",";
648 $sql .= " ".((int) $fourn->id).",";
649 $sql .= " '".$this->db->escape($ref_fourn)."',";
650 $sql .= " '".$this->db->escape($desc_fourn)."',";
651 $sql .= " ".((int) $user->id).",";
652 $sql .= " ".price2num($buyprice).",";
653 $sql .= " ".((float) $qty).",";
654 $sql .= " ".((float) $remise_percent).",";
655 $sql .= " ".((float) $remise).",";
656 $sql .= " ".price2num($unitBuyPrice).",";
657 $sql .= " ".price2num($tva_tx).",";
658 $sql .= " ".price2num($charges).",";
659 $sql .= " ".((int) $availability).",";
660 $sql .= " ".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
661 $sql .= " ".((int) $newnpr).",";
662 $sql .= ((int) $conf->entity).",";
663 $sql .= ($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
664 $sql .= (empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
665 $sql .= (empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
666 $sql .= (empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape((string) $fk_barcode_type)."'");
667 if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
668 $sql .= ", ".(empty($this->packaging) ? '1' : "'".$this->db->escape((string) $this->packaging)."'");
669 }
670 $sql .= ")";
671
672 $this->product_fourn_price_id = 0;
673
674 $resql = $this->db->query($sql);
675 if ($resql) {
676 $this->product_fourn_price_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur_price");
677 } else {
678 $this->error = $this->db->lasterror();
679 $error++;
680 }
681
682 if (!$error) {
683 if (!empty($options) && is_array($options)) {
684 $productfournisseurprice = new ProductFournisseurPrice($this->db);
685 $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
686 if ($res > 0) {
687 foreach ($options as $key => $value) {
688 $productfournisseurprice->array_options[$key] = $value;
689 }
690 $res = $productfournisseurprice->update($user);
691 if ($res < 0) {
692 $this->error = $productfournisseurprice->error;
693 $this->errors = $productfournisseurprice->errors;
694 $error++;
695 }
696 }
697 }
698 }
699
700 if (!$error && !getDolGlobalString('PRODUCT_PRICE_SUPPLIER_NO_LOG')) {
701 // Add record into log table
702 // $this->product_fourn_price_id must be set
703 $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, (float) $multicurrency_unitBuyPrice, $multicurrency_tx, (int) $fk_multicurrency, $multicurrency_code);
704 if ($result < 0) {
705 $error++;
706 }
707 }
708
709 if (!$error) {
710 // Call trigger
711 $result = $this->call_trigger('PRODUCT_BUYPRICE_CREATE', $user);
712 if ($result < 0) {
713 $error++;
714 }
715 // End call triggers
716
717 if (empty($error)) {
718 $this->db->commit();
719 return $this->product_fourn_price_id;
720 } else {
721 $this->db->rollback();
722 return -1;
723 }
724 } else {
725 $this->error = $this->db->lasterror()." sql=".$sql;
726 $this->db->rollback();
727 return -2;
728 }
729 } else {
730 $this->error = $this->db->lasterror()." sql=".$sql;
731 $this->db->rollback();
732 return -1;
733 }
734 }
735 }
736
737 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
745 public function fetch_product_fournisseur_price($rowid, $ignore_expression = 0)
746 {
747 // phpcs:enable
748 $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,";
749 $sql .= " pfp.fk_soc, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_product, pfp.charges, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
750 $sql .= " pfp.supplier_reputation, pfp.fk_user, pfp.datec,";
751 $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
752 $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging,";
753 $sql .= " p.ref as product_ref, p.tosell as status, p.tobuy as status_buy";
754 $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p";
755 $sql .= " WHERE pfp.rowid = ".(int) $rowid;
756 $sql .= " AND pfp.fk_product = p.rowid";
757
758 dol_syslog(get_class($this)."::fetch_product_fournisseur_price", LOG_DEBUG);
759 $resql = $this->db->query($sql);
760 if ($resql) {
761 $obj = $this->db->fetch_object($resql);
762 if ($obj) {
763 $this->product_fourn_price_id = $rowid;
764 $this->id = $obj->fk_product;
765
766 $this->fk_product = $obj->fk_product;
767 $this->product_id = $obj->fk_product;
768 $this->product_ref = $obj->product_ref;
769 $this->status = $obj->status;
770 $this->status_buy = $obj->status_buy;
771 $this->fourn_id = $obj->fk_soc;
772 $this->fourn_ref = $obj->ref_fourn; // deprecated
773 $this->ref_supplier = $obj->ref_fourn;
774 $this->desc_supplier = $obj->desc_fourn;
775 $this->fourn_price = $obj->price;
776 $this->fourn_charges = $obj->charges; // when getDolGlobalString('PRODUCT_CHARGES') is set
777 $this->fourn_qty = $obj->quantity;
778 $this->fourn_remise_percent = $obj->remise_percent;
779 $this->fourn_remise = $obj->remise;
780 $this->fourn_unitprice = $obj->unitprice;
781 $this->fourn_tva_tx = $obj->tva_tx;
782 $this->fourn_tva_npr = $obj->fourn_tva_npr;
783 // Add also localtaxes
784 $this->fk_availability = $obj->fk_availability;
785 $this->delivery_time_days = $obj->delivery_time_days;
786 $this->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
787 $this->supplier_reputation = $obj->supplier_reputation;
788 $this->default_vat_code = $obj->default_vat_code;
789 $this->user_id = $obj->fk_user;
790 $this->date_creation = $this->db->jdate($obj->datec);
791 $this->fourn_multicurrency_price = $obj->multicurrency_price;
792 $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
793 $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
794 $this->fourn_multicurrency_id = $obj->fk_multicurrency;
795 $this->fourn_multicurrency_code = $obj->multicurrency_code;
796 if (isModEnabled('barcode')) {
797 $this->fourn_barcode = $obj->barcode; // deprecated
798 $this->fourn_fk_barcode_type = $obj->fk_barcode_type; // deprecated
799 $this->supplier_barcode = $obj->barcode;
800 $this->supplier_fk_barcode_type = $obj->fk_barcode_type;
801 }
802 $this->packaging = (float) $obj->packaging;
803
804 if (isModEnabled('dynamicprices') && empty($ignore_expression) && !empty($this->fk_supplier_price_expression)) {
805 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
806 $priceparser = new PriceParser($this->db);
807 $price_result = $priceparser->parseProductSupplier($this);
808 if ($price_result >= 0) {
809 $this->fourn_price = $price_result;
810 //recalculation of unitprice, as probably the price changed...
811 if ($this->fourn_qty != 0) {
812 $this->fourn_unitprice = price2num($this->fourn_price / $this->fourn_qty, 'MU');
813 } else {
814 $this->fourn_unitprice = "";
815 }
816 }
817 }
818
819 return 1;
820 } else {
821 return 0;
822 }
823 } else {
824 $this->error = $this->db->lasterror();
825 return -1;
826 }
827 }
828
829
830 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
843 public function list_product_fournisseur_price($prodid, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0, $socid = 0)
844 {
845 // phpcs:enable
846 $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.tobatch as status_batch, p.fk_unit as product_fk_unit,";
847 $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,";
848 $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,";
849 $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code, pfp.datec, pfp.tms,";
850 $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging, pfp.status as pfstatus";
851 $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."societe as s";
852 $sql .= " WHERE pfp.entity IN (".getEntity('productsupplierprice').")";
853 $sql .= " AND pfp.fk_soc = s.rowid AND pfp.fk_product = p.rowid";
854 $sql .= ($socid > 0 ? ' AND pfp.fk_soc = '.((int) $socid) : '');
855 $sql .= " AND s.status = 1"; // only enabled company selected
856 $sql .= " AND pfp.fk_product = ".((int) $prodid);
857 if (empty($sortfield)) {
858 $sql .= " ORDER BY s.nom, pfp.quantity, pfp.price";
859 } else {
860 $sql .= $this->db->order($sortfield, $sortorder);
861 }
862 $sql .= $this->db->plimit($limit, $offset);
863 dol_syslog(get_class($this)."::list_product_fournisseur_price", LOG_DEBUG);
864
865 $resql = $this->db->query($sql);
866 if ($resql) {
867 $retarray = array();
868
869 while ($record = $this->db->fetch_array($resql)) {
870 //define base attribute
871 $prodfourn = new ProductFournisseur($this->db);
872
873 $prodfourn->product_ref = $record["product_ref"];
874 $prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"];
875 $prodfourn->status = $record["status"];
876 $prodfourn->status_buy = $record["status_buy"];
877 $prodfourn->status_batch = $record["status_batch"];
878 $prodfourn->product_fourn_id = $record["product_fourn_id"];
879 $prodfourn->product_fourn_entity = $record["entity"];
880 $prodfourn->ref_supplier = $record["ref_fourn"];
881 $prodfourn->fourn_ref = $record["ref_fourn"];
882 $prodfourn->desc_supplier = $record["desc_fourn"];
883 $prodfourn->fourn_price = $record["price"];
884 $prodfourn->fourn_qty = $record["quantity"];
885 $prodfourn->fourn_remise_percent = $record["remise_percent"];
886 $prodfourn->fourn_remise = $record["remise"];
887 $prodfourn->fourn_unitprice = $record["unitprice"];
888 $prodfourn->fourn_charges = $record["charges"]; // when getDolGlobalString('PRODUCT_CHARGES') is set
889 $prodfourn->fourn_tva_tx = $record["tva_tx"];
890 $prodfourn->fourn_id = $record["fourn_id"];
891 $prodfourn->fourn_name = $record["supplier_name"];
892 $prodfourn->fk_availability = $record["fk_availability"];
893 $prodfourn->delivery_time_days = $record["delivery_time_days"];
894 $prodfourn->id = $prodid;
895 $prodfourn->fourn_tva_npr = $record["info_bits"];
896 $prodfourn->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
897 $prodfourn->supplier_reputation = $record["supplier_reputation"];
898 $prodfourn->fourn_date_creation = $this->db->jdate($record['datec']);
899 $prodfourn->fourn_date_modification = $this->db->jdate($record['tms']);
900 // Carry the product's default measuring unit so the AJAX caller
901 // (getSupplierPrices.php) can return it to the line form, which
902 // then preselects #units like the customer side already does for
903 // idprod (see issues #34610 client-side and #38636 supplier-side).
904 $prodfourn->fk_unit = $record["product_fk_unit"];
905
906 $prodfourn->fourn_multicurrency_price = $record["multicurrency_price"];
907 $prodfourn->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
908 $prodfourn->fourn_multicurrency_tx = $record["multicurrency_tx"];
909 $prodfourn->fourn_multicurrency_id = $record["fk_multicurrency"];
910 $prodfourn->fourn_multicurrency_code = $record["multicurrency_code"];
911
912 $prodfourn->packaging = (float) $record["packaging"];
913 $prodfourn->status = $record["pfstatus"];
914
915 if (isModEnabled('barcode')) {
916 $prodfourn->supplier_barcode = $record["barcode"];
917 $prodfourn->supplier_fk_barcode_type = $record["fk_barcode_type"];
918 }
919
920 if (isModEnabled('dynamicprices') && !empty($prodfourn->fk_supplier_price_expression)) {
921 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
922 $priceparser = new PriceParser($this->db);
923 $price_result = $priceparser->parseProductSupplier($prodfourn);
924 if ($price_result >= 0) {
925 $prodfourn->fourn_price = $price_result;
926 // Set to null on purpose an will have non-null value before method return @phan-suppress-next-line PhanTypeMismatchPropertyProbablyReal
927 $prodfourn->fourn_unitprice = null; //force recalculation of unitprice, as probably the price changed... @phpstan-ignore-line
928 }
929 }
930
931 if (!isset($prodfourn->fourn_unitprice)) {
932 if ($prodfourn->fourn_qty != 0) {
933 $prodfourn->fourn_unitprice = price2num($prodfourn->fourn_price / $prodfourn->fourn_qty, 'MU');
934 } else {
935 $prodfourn->fourn_unitprice = "";
936 }
937 }
938
939 $retarray[] = $prodfourn;
940 }
941
942 $this->db->free($resql);
943 return $retarray;
944 } else {
945 $this->error = $this->db->error();
946 return -1;
947 }
948 }
949
950 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
960 public function find_min_price_product_fournisseur($prodid, $qty = 0, $socid = 0)
961 {
962 // phpcs:enable
963 if (empty($prodid)) {
964 dol_syslog("Warning function find_min_price_product_fournisseur were called with prodid empty. May be a bug.", LOG_WARNING);
965 return 0;
966 }
967
968 $this->product_fourn_price_id = 0;
969 $this->product_fourn_id = 0;
970 $this->fourn_ref = '';
971 $this->fourn_price = 0;
972 $this->fourn_qty = 0;
973 $this->fourn_remise_percent = 0;
974 $this->fourn_remise = 0;
975 $this->fourn_unitprice = 0;
976 $this->fourn_id = 0;
977 $this->fourn_name = '';
978 $this->delivery_time_days = 0;
979 $this->id = 0;
980
981 $this->fourn_multicurrency_price = 0;
982 $this->fourn_multicurrency_unitprice = 0;
983 $this->fourn_multicurrency_tx = 0;
984 $this->fourn_multicurrency_id = 0;
985 $this->fourn_multicurrency_code = '';
986
987 $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
988 $sql .= " pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
989 $sql .= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges,";
990 $sql .= " pfp.remise, pfp.remise_percent, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
991 $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code";
992 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
993 $sql .= " WHERE s.entity IN (".getEntity('societe').")";
994 $sql .= " AND pfp.entity IN (".getEntity('productsupplierprice').")";
995 $sql .= " AND pfp.fk_product = ".((int) $prodid);
996 $sql .= " AND pfp.fk_soc = s.rowid";
997 $sql .= " AND s.status = 1"; // only enabled society
998 if ($qty > 0) {
999 $sql .= " AND pfp.quantity <= ".((float) $qty);
1000 }
1001 if ($socid > 0) {
1002 $sql .= ' AND pfp.fk_soc = '.((int) $socid);
1003 }
1004
1005 dol_syslog(get_class($this)."::find_min_price_product_fournisseur", LOG_DEBUG);
1006
1007 $resql = $this->db->query($sql);
1008 if ($resql) {
1009 $record_array = array();
1010
1011 //Store each record to array for later search of min
1012 while ($record = $this->db->fetch_array($resql)) {
1013 $record_array[] = $record;
1014 }
1015
1016 if (count($record_array) == 0) {
1017 $this->db->free($resql);
1018 return 0;
1019 } else {
1020 $min = -1;
1021 foreach ($record_array as $record) {
1022 $fourn_price = $record["price"];
1023 // calculate unit price for quantity 1
1024 $fourn_unitprice = $record["unitprice"];
1025 $fourn_unitprice_with_discount = $record["unitprice"] * (1 - $record["remise_percent"] / 100);
1026
1027 if (isModEnabled('dynamicprices') && !empty($record["fk_supplier_price_expression"])) {
1028 $prod_supplier = new ProductFournisseur($this->db);
1029 $prod_supplier->product_fourn_price_id = $record["product_fourn_price_id"];
1030 $prod_supplier->id = $prodid;
1031 $prod_supplier->fourn_qty = $record["quantity"];
1032 $prod_supplier->fourn_tva_tx = $record["tva_tx"];
1033 $prod_supplier->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
1034
1035 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
1036 $priceparser = new PriceParser($this->db);
1037 $price_result = $priceparser->parseProductSupplier($prod_supplier);
1038 if ($price_result >= 0) {
1039 $fourn_price = price2num($price_result, 'MU');
1040 if ($record["quantity"] != 0) {
1041 $fourn_unitprice = price2num((float) $fourn_price / $record["quantity"], 'MU');
1042 } else {
1043 $fourn_unitprice = $fourn_price;
1044 }
1045 $fourn_unitprice_with_discount = (float) $fourn_unitprice * (1 - $record["remise_percent"] / 100);
1046 }
1047 }
1048
1049 if ($fourn_unitprice_with_discount < $min || $min == -1) {
1050 $this->id = $prodid;
1051 $this->product_fourn_price_id = $record["product_fourn_price_id"];
1052 $this->ref_supplier = $record["ref_fourn"];
1053 $this->ref_fourn = $record["ref_fourn"]; // deprecated
1054 $this->fourn_ref = $record["ref_fourn"]; // deprecated
1055 $this->fourn_price = $fourn_price;
1056 $this->fourn_qty = $record["quantity"];
1057 $this->fourn_remise_percent = $record["remise_percent"];
1058 $this->fourn_remise = $record["remise"];
1059 $this->fourn_unitprice = $fourn_unitprice;
1060 $this->fourn_unitprice_with_discount = $fourn_unitprice_with_discount;
1061 $this->fourn_charges = $record["charges"]; // when getDolGlobalString('PRODUCT_CHARGES') is set
1062 $this->fourn_tva_tx = $record["tva_tx"];
1063 $this->fourn_id = $record["fourn_id"]; // thirdparty id
1064 $this->fourn_name = $record["supplier_name"];
1065 $this->delivery_time_days = $record["delivery_time_days"];
1066 // False positive @phan-suppress-next-line PhanTypeMismatchProperty
1067 $this->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
1068 $this->fourn_multicurrency_price = $record["multicurrency_price"];
1069 $this->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
1070 $this->fourn_multicurrency_tx = $record["multicurrency_tx"];
1071 $this->fourn_multicurrency_id = $record["fk_multicurrency"];
1072 $this->fourn_multicurrency_code = $record["multicurrency_code"];
1073
1074 $min = $fourn_unitprice_with_discount;
1075 }
1076 }
1077 }
1078
1079 $this->db->free($resql);
1080
1081 return $this->product_fourn_price_id;
1082 } else {
1083 $this->error = $this->db->error();
1084 return -1;
1085 }
1086 }
1087
1094 public function setSupplierPriceExpression($expression_id)
1095 {
1096 // Clean parameters
1097 $this->db->begin();
1098 $expression_id = $expression_id != 0 ? $expression_id : 'NULL';
1099
1100 $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
1101 $sql .= " SET fk_supplier_price_expression = ".((int) $expression_id);
1102 $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
1103
1104 dol_syslog(get_class($this)."::setSupplierPriceExpression", LOG_DEBUG);
1105
1106 $resql = $this->db->query($sql);
1107 if ($resql) {
1108 $this->db->commit();
1109 return 1;
1110 } else {
1111 $this->error = $this->db->error()." sql=".$sql;
1112 $this->db->rollback();
1113 return -1;
1114 }
1115 }
1116
1127 public function getSocNomUrl($withpicto = 0, $option = 'supplier', $maxlen = 0, $notooltip = 0)
1128 {
1129 $thirdparty = new Fournisseur($this->db);
1130 $thirdparty->fetch($this->fourn_id);
1131
1132 return $thirdparty->getNomUrl($withpicto, $option, $maxlen, $notooltip);
1133 }
1134
1135 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1147 public function display_price_product_fournisseur($showunitprice = 1, $showsuptitle = 1, $maxlen = 0, $notooltip = 0, $productFournList = array())
1148 {
1149 // phpcs:enable
1150 global $conf, $langs, $user;
1151
1152 $out = '';
1153 $langs->load("suppliers");
1154 if (count($productFournList) > 0) {
1155 $out .= '<table class="centpercent liste nomarginbottom">';
1156 $out .= '<tr class="liste_titre"><td class="liste_titre right">'.($showunitprice ? $langs->trans("Price").' '.$langs->trans("HT") : '').'</td>';
1157 $out .= '<td class="liste_titre right">'.($showunitprice ? $langs->trans("QtyMin") : '').'</td>';
1158 $out .= '<td class="liste_titre">'.$langs->trans("Supplier").'</td>';
1159 $out .= '<td class="liste_titre">'.$langs->trans("SupplierRef").'</td></tr>';
1160 foreach ($productFournList as $productFourn) {
1161 $out .= '<tr><td class="right">'.($showunitprice ? price($productFourn->fourn_unitprice * (1 - $productFourn->fourn_remise_percent / 100) - $productFourn->fourn_remise) : '').'</td>';
1162 $out .= '<td class="right">'.($showunitprice ? $productFourn->fourn_qty : '').'</td>';
1163 $out .= '<td>'.$productFourn->getSocNomUrl(1, 'supplier', $maxlen, $notooltip).'</td>';
1164 $out .= '<td>'.dolPrintHTML($productFourn->fourn_ref).'</td></tr>';
1165 }
1166 $out .= '</table>';
1167 } else {
1168 $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") : '');
1169 if ($user->hasRight("fournisseur", "read")) { // Without permission, never show the best supplier seller
1170 $out .= ($showunitprice ? ' &nbsp; <span class="opacitymedium">(</span>' : '');
1171 $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;
1172 $out .= ($showunitprice ? '<span class="opacitymedium">)</span>' : '');
1173 }
1174 }
1175 return $out;
1176 }
1177
1186 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1187 {
1188 // llx_product_fournisseur_price has a UNIQUE INDEX uk_product_fournisseur_price_ref
1189 // on (ref_fourn, fk_soc, quantity, entity). A blind UPDATE fk_soc = dest on every
1190 // origin row fails on rows where dest already has the same (ref_fourn, quantity, entity),
1191 // blocking the whole merge with a 1062 Duplicate entry error (see #38456).
1192 // Drop the colliding origin rows first - they would become exact duplicates of dest
1193 // rows so the merge has nothing to lose by keeping the dest version.
1194 $sqlselect = "SELECT pfp_origin.rowid";
1195 $sqlselect .= " FROM ".$dbs->prefix()."product_fournisseur_price AS pfp_origin";
1196 $sqlselect .= " INNER JOIN ".$dbs->prefix()."product_fournisseur_price AS pfp_dest";
1197 $sqlselect .= " ON pfp_dest.fk_soc = ".((int) $dest_id);
1198 $sqlselect .= " AND pfp_dest.ref_fourn = pfp_origin.ref_fourn";
1199 $sqlselect .= " AND pfp_dest.quantity = pfp_origin.quantity";
1200 $sqlselect .= " AND pfp_dest.entity = pfp_origin.entity";
1201 $sqlselect .= " WHERE pfp_origin.fk_soc = ".((int) $origin_id);
1202
1203 $resql = $dbs->query($sqlselect);
1204 if (!$resql) {
1205 return false;
1206 }
1207 $colliding = array();
1208 while ($obj = $dbs->fetch_object($resql)) {
1209 $colliding[] = (int) $obj->rowid;
1210 }
1211 $dbs->free($resql);
1212
1213 foreach ($colliding as $rowid) {
1214 $sqldel = "DELETE FROM ".$dbs->prefix()."product_fournisseur_price";
1215 $sqldel .= " WHERE rowid = ".((int) $rowid);
1216 if (!$dbs->query($sqldel)) {
1217 return false;
1218 }
1219 }
1220
1221 $tables = array(
1222 'product_fournisseur_price'
1223 );
1224
1225 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1226 }
1227
1236 public static function replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
1237 {
1238 $tables = array(
1239 'product_fournisseur_price'
1240 );
1241
1242 return CommonObject::commonReplaceProduct($dbs, $origin_id, $dest_id, $tables);
1243 }
1244
1255 public function listProductFournisseurPriceLog($product_fourn_price_id, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0)
1256 {
1257 $sql = "SELECT";
1258 $sql .= " u.lastname,";
1259 $sql .= " pfpl.rowid, pfp.ref_fourn as supplier_ref, pfpl.datec,";
1260 $sql .= " pfpl.price, pfpl.quantity,";
1261 $sql .= " pfpl.fk_multicurrency, pfpl.multicurrency_code, pfpl.multicurrency_tx, pfpl.multicurrency_price, pfpl.multicurrency_unitprice";
1262 $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price_log as pfpl,";
1263 $sql .= " ".MAIN_DB_PREFIX."product_fournisseur_price as pfp,";
1264 $sql .= " ".MAIN_DB_PREFIX."user as u";
1265 $sql .= " WHERE pfp.entity IN (".getEntity('productprice').")";
1266 $sql .= " AND pfpl.fk_user = u.rowid";
1267 $sql .= " AND pfp.rowid = pfpl.fk_product_fournisseur";
1268 $sql .= " AND pfpl.fk_product_fournisseur = ".((int) $product_fourn_price_id);
1269 if (empty($sortfield)) {
1270 $sql .= " ORDER BY pfpl.datec";
1271 } else {
1272 $sql .= $this->db->order($sortfield, $sortorder);
1273 }
1274 $sql .= $this->db->plimit($limit, $offset);
1275 dol_syslog(get_class($this)."::list_product_fournisseur_price_log", LOG_DEBUG);
1276
1277 $resql = $this->db->query($sql);
1278 if ($resql) {
1279 $retarray = array();
1280
1281 while ($obj = $this->db->fetch_object($resql)) {
1282 $tmparray = array();
1283 $tmparray['rowid'] = $obj->rowid;
1284 $tmparray['supplier_ref'] = $obj->supplier_ref;
1285 $tmparray['datec'] = $this->db->jdate($obj->datec);
1286 $tmparray['lastname'] = $obj->lastname;
1287 $tmparray['price'] = $obj->price;
1288 $tmparray['quantity'] = $obj->quantity;
1289 $tmparray['fk_multicurrency'] = $obj->fk_multicurrency;
1290 $tmparray['multicurrency_code'] = $obj->multicurrency_code;
1291 $tmparray['multicurrency_tx'] = $obj->multicurrency_tx;
1292 $tmparray['multicurrency_price'] = $obj->multicurrency_price;
1293 $tmparray['multicurrency_unitprice'] = $obj->multicurrency_unitprice;
1294
1295 $retarray[] = $tmparray;
1296 }
1297
1298 $this->db->free($resql);
1299 return $retarray;
1300 } else {
1301 $this->error = $this->db->error();
1302 return -1;
1303 }
1304 }
1305
1313 public function displayPriceProductFournisseurLog($productFournLogList = array())
1314 {
1315 global $conf, $langs;
1316
1317 $out = '';
1318 $langs->load("suppliers");
1319 if (count($productFournLogList) > 0) {
1320 $out .= '<table class="noborder centpercent">';
1321 $out .= '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Date").'</td>';
1322 $out .= '<td class="liste_titre right">'.$langs->trans("Price").'</td>';
1323 //$out .= '<td class="liste_titre right">'.$langs->trans("QtyMin").'</td>';
1324 $out .= '<td class="liste_titre">'.$langs->trans("User").'</td></tr>';
1325 foreach ($productFournLogList as $productFournLog) {
1326 $out .= '<tr><td>'.dol_print_date($productFournLog['datec'], 'dayhour', 'tzuser').'</td>';
1327 $out .= '<td class="right">'.price($productFournLog['price'], 0, $langs, 1, -1, -1, $conf->currency);
1328 if ($productFournLog['multicurrency_code'] != $conf->currency) {
1329 $out .= ' ('.price($productFournLog['multicurrency_price'], 0, $langs, 1, -1, -1, $productFournLog['multicurrency_code']).')';
1330 }
1331 $out .= '</td>';
1332 //$out.= '<td class="right">'.$productFournLog['quantity'].'</td>';
1333 $out .= '<td>'.$productFournLog['lastname'].'</td></tr>';
1334 }
1335 $out .= '</table>';
1336 }
1337 return $out;
1338 }
1339
1340
1356 public function getNomUrl($withpicto = 0, $option = '', $maxlength = 0, $save_lastsearch_value = -1, $notooltip = 0, $morecss = '', $add_label = 0, $sep = ' - ', $addlinktonotes = 0)
1357 {
1358 global $conf, $langs, $hookmanager, $user;
1359
1360 if (!empty($conf->dol_no_mouse_hover)) {
1361 $notooltip = 1; // Force disable tooltips
1362 }
1363
1364 $result = '';
1365 $label = '';
1366
1367 $newref = (string) $this->ref;
1368 if ($maxlength) {
1369 $newref = dol_trunc($newref, $maxlength, 'middle');
1370 }
1371
1372 if (!empty($this->entity)) {
1373 $tmpphoto = $this->show_photos('product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 80);
1374 if ($this->nbphoto > 0) {
1375 $label .= '<div class="photointooltip">';
1376 $label .= $tmpphoto;
1377 $label .= '</div><div style="clear: both;"></div>';
1378 }
1379 }
1380
1381 if ($this->type == Product::TYPE_PRODUCT) {
1382 $label .= img_picto('', 'product').' <u class="paddingrightonly">'.$langs->trans("Product").'</u>';
1383 } elseif ($this->type == Product::TYPE_SERVICE) {
1384 $label .= img_picto('', 'service').' <u class="paddingrightonly">'.$langs->trans("Service").'</u>';
1385 }
1386 if (isset($this->status) && isset($this->status_buy)) {
1387 $label .= ' '.$this->getLibStatut(5, 0);
1388 $label .= ' '.$this->getLibStatut(5, 1);
1389 }
1390
1391 if (!empty($this->ref)) {
1392 $label .= '<br><b>'.$langs->trans('ProductRef').':</b> '.($this->ref ? $this->ref : $this->product_ref);
1393 }
1394 if (!empty($this->label)) {
1395 $label .= '<br><b>'.$langs->trans('ProductLabel').':</b> '.$this->label;
1396 }
1397 $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
1398
1399 if ($this->type == Product::TYPE_PRODUCT || getDolGlobalString('STOCK_SUPPORTS_SERVICES')) {
1400 if (isModEnabled('productbatch')) {
1401 $langs->load("productbatch");
1402 $label .= "<br><b>".$langs->trans("ManageLotSerial").'</b>: '.$this->getLibStatut(0, 2);
1403 }
1404 }
1405 if (isModEnabled('barcode')) {
1406 $label .= '<br><b>'.$langs->trans('BarCode').':</b> '.$this->barcode;
1407 }
1408
1409 if ($this->type == Product::TYPE_PRODUCT) {
1410 if ($this->weight) {
1411 $label .= "<br><b>".$langs->trans("Weight").'</b>: '.$this->weight.' '.measuringUnitString(0, "weight", $this->weight_units);
1412 }
1413 $labelsize = "";
1414 if ($this->length) {
1415 $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Length").'</b>: '.$this->length.' '.measuringUnitString(0, 'size', $this->length_units);
1416 }
1417 if ($this->width) {
1418 $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Width").'</b>: '.$this->width.' '.measuringUnitString(0, 'size', $this->width_units);
1419 }
1420 if ($this->height) {
1421 $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Height").'</b>: '.$this->height.' '.measuringUnitString(0, 'size', $this->height_units);
1422 }
1423 if ($labelsize) {
1424 $label .= "<br>".$labelsize;
1425 }
1426
1427 $labelsurfacevolume = "";
1428 if ($this->surface) {
1429 $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Surface").'</b>: '.$this->surface.' '.measuringUnitString(0, 'surface', $this->surface_units);
1430 }
1431 if ($this->volume) {
1432 $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Volume").'</b>: '.$this->volume.' '.measuringUnitString(0, 'volume', $this->volume_units);
1433 }
1434 if ($labelsurfacevolume) {
1435 $label .= "<br>".$labelsurfacevolume;
1436 }
1437 }
1438
1439 if (isModEnabled('accounting') && $this->status) {
1440 include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1441 $label .= '<br><b>'.$langs->trans('ProductAccountancySellCode').':</b> '.length_accountg($this->accountancy_code_sell);
1442 $label .= '<br><b>'.$langs->trans('ProductAccountancySellIntraCode').':</b> '.length_accountg($this->accountancy_code_sell_intra);
1443 $label .= '<br><b>'.$langs->trans('ProductAccountancySellExportCode').':</b> '.length_accountg($this->accountancy_code_sell_export);
1444 }
1445 if (isModEnabled('accounting') && $this->status_buy) {
1446 include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1447 $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyCode').':</b> '.length_accountg($this->accountancy_code_buy);
1448 $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyIntraCode').':</b> '.length_accountg($this->accountancy_code_buy_intra);
1449 $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyExportCode').':</b> '.length_accountg($this->accountancy_code_buy_export);
1450 }
1451
1452 $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id, 'pfpl.datec', 'DESC', getDolGlobalInt('MAIN_TOOLTIP_PRICELOG_HISTORY_LIMIT', 10)); // set sort order here
1453 if (is_array($logPrices) && count($logPrices) > 0) {
1454 $label .= '<br><br>';
1455 $label .= '<u>'.$langs->trans("History").'</u>';
1456 $label .= $this->displayPriceProductFournisseurLog($logPrices);
1457 }
1458
1459 $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);
1460
1461 if ($option != 'nolink') {
1462 // Add param to save lastsearch_values or not
1463 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1464 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1465 $add_save_lastsearch_values = 1;
1466 }
1467 if ($add_save_lastsearch_values) {
1468 $url .= '&save_lastsearch_values=1';
1469 }
1470 }
1471
1472 $allowothertags = array('table', 'tr', 'td');
1473 $linkclose = '';
1474 if (empty($notooltip)) {
1475 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1476 $label = $langs->trans("SupplierRef");
1477 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label, 0, $allowothertags).'"';
1478 }
1479 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label, 0, $allowothertags).'"';
1480 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
1481 } else {
1482 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1483 }
1484
1485 $linkstart = '<a href="'.$url.'"';
1486 $linkstart .= $linkclose.'>';
1487 $linkend = '</a>';
1488
1489 $result .= $linkstart;
1490 if ($withpicto) {
1491 $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);
1492 }
1493 if ($withpicto != 2) {
1494 $result .= $newref.($this->ref_supplier ? ' ('.$this->ref_supplier.')' : '');
1495 }
1496 $result .= $linkend;
1497 if ($withpicto != 2) {
1498 $result .= (($add_label && $this->label) ? $sep.dol_trunc($this->label, ($add_label > 1 ? $add_label : 0)) : '');
1499 }
1500
1501 if ($addlinktonotes) {
1502 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
1503 if ($txttoshow) {
1504 $notetoshow = $langs->trans("ViewPrivateNote").':<br>'.dol_string_nohtmltag($txttoshow, 1);
1505 $result .= ' <span class="note inline-block">';
1506 $result .= '<a href="'.DOL_URL_ROOT.'/product/note.php?id='.$this->id.'" class="classfortooltip" title="'.dol_escape_htmltag($notetoshow).'">';
1507 $result .= img_picto('', 'note');
1508 $result .= '</a>';
1509 $result .= '</span>';
1510 }
1511 }
1512
1513 global $action;
1514 $hookmanager->initHooks(array($this->element . 'dao'));
1515 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1516 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1517 if ($reshook > 0) {
1518 $result = $hookmanager->resPrint;
1519 } else {
1520 $result .= $hookmanager->resPrint;
1521 }
1522 return $result;
1523 }
1524
1532 /* public function getLibStatut($mode = 0, $type = 0) // must be compatible with getLibStatut of inherited Product
1533 {
1534 return $this->LibStatut($this->status, $mode);
1535 }
1536 */
1537 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1546 /*
1547 public function LibStatut($status, $mode = 0, $type = 0)
1548 {
1549 // phpcs:enable
1550 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1551 global $langs;
1552 //$langs->load("mymodule@mymodule");
1553 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Enabled');
1554 $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
1555 $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Enabled');
1556 $this->labelStatusShort[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
1557 }
1558
1559 $statusType = 'status4';
1560 //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
1561 if ($status == self::STATUS_CANCELED) {
1562 $statusType = 'status6';
1563 }
1564
1565 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
1566 }
1567 */
1568
1584 private function logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice = null, $multicurrency_unitBuyPrice = null, $multicurrency_tx = null, $fk_multicurrency = null, $multicurrency_code = null)
1585 {
1586 // Add record into log table
1587 $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log(";
1588 $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
1589 $sql .= "datec, fk_product_fournisseur,fk_user,price,quantity)";
1590 $sql .= "values(";
1591 $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
1592 $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
1593 $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape((string) $multicurrency_tx)."'" : '1').",";
1594 $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape((string) $fk_multicurrency)."'" : 'null').",";
1595 $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
1596 $sql .= "'".$this->db->idate($datec)."',";
1597 $sql .= " ".((int) $this->product_fourn_price_id).",";
1598 $sql .= " ".((int) $user->id).",";
1599 $sql .= " ".price2num($buyprice).",";
1600 $sql .= " ".price2num($qty);
1601 $sql .= ")";
1602
1603 $resql = $this->db->query($sql);
1604 if (!$resql) {
1605 return -1;
1606 } else {
1607 return 1;
1608 }
1609 }
1610}
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition propal.php:280
$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.
getNomUrl($withpicto=0, $option='', $maxlength=0, $save_lastsearch_value=-1, $notooltip=0, $morecss='', $add_label=0, $sep=' - ', $addlinktonotes=0)
Return a link to the object card (with optionally the picto).
logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice=null, $multicurrency_unitBuyPrice=null, $multicurrency_tx=null, $fk_multicurrency=null, $multicurrency_code=null)
Return the label of the status.
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.
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.
getLibStatut($mode=0, $type=0)
Return label of status of object.
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:169
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_now($mode='gmt')
Return date for now.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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'.
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.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Definition html.lib.php:181
query($query, $usesavepoint=0, $type='auto', $result_mode=0)
Execute a SQL request and return the resultset.
fetch_object($resultset)
Returns the current line (as an object) for the resultset cursor.
free($resultset=null)
Free last resultset used.
measuringUnitString($unitid, $measuring_style='', $unitscale=null, $use_short_label=0, $outputlangs=null)
Return translation label of a unit key.