dolibarr 22.0.5
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-2024 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
51 public $error = '';
52
56 public $product_fourn_price_id;
57
61 public $id;
62
68 public $fourn_ref;
69
73 public $delivery_time_days;
74
78 public $ref_supplier;
79
83 public $desc_supplier;
84
88 public $vatrate_supplier;
89
95 public $fk_product;
96
100 public $product_id;
101
105 public $product_ref;
106
110 public $fourn_id;
111
115 public $fourn_name;
116
120 public $fourn_qty;
121
125 public $fourn_pu;
126
130 public $fourn_price;
131
135 public $fourn_remise_percent;
136
140 public $fourn_remise;
141
145 public $fourn_charges; // when getDolGlobalString('PRODUCT_CHARGES') is set
146
147
148 /* Properties found into product_fournisseur_price */
149
153 public $product_fourn_id;
154
158 public $product_fourn_entity;
159
163 public $product_fourn_packaging;
164
165
169 public $user_id;
170
174 public $fk_availability;
175
179 public $fourn_unitprice;
183 public $fourn_unitprice_with_discount; // not saved into database
187 public $fourn_tva_tx;
191 public $fourn_tva_npr;
192
196 public $fk_supplier_price_expression;
197
201 public $supplier_reputation;
202
206 public $reputations = array();
207
208 // Multicurrency
209
213 public $fourn_multicurrency_id;
214
218 public $fourn_multicurrency_code;
222 public $fourn_multicurrency_tx;
226 public $fourn_multicurrency_price;
230 public $fourn_multicurrency_unitprice;
231
237 public $fourn_barcode;
238
242 public $supplier_barcode;
243
249 public $fourn_fk_barcode_type;
250
254 public $supplier_fk_barcode_type;
255
259 public $labelStatusShort;
263 public $labelStatus;
264
265 const STATUS_OPEN = 1;
266 const STATUS_CANCELED = 0;
267
268
274 public function __construct($db)
275 {
276 global $langs;
277
278 $this->db = $db;
279 $langs->load("suppliers");
280 $this->reputations = array('-1' => '', 'FAVORITE' => $langs->trans('Favorite'), 'NOTTHGOOD' => $langs->trans('NotTheGoodQualitySupplier'), 'DONOTORDER' => $langs->trans('DoNotOrderThisProductToThisSupplier'));
281 }
282
283 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
290 public function remove_fournisseur($id_fourn)
291 {
292 // phpcs:enable
293 $ok = 1;
294
295 $this->db->begin();
296
297 $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
298 $sql .= " WHERE fk_product = ".((int) $this->id)." AND fk_soc = ".((int) $id_fourn);
299
300 dol_syslog(get_class($this)."::remove_fournisseur", LOG_DEBUG);
301 $resql2 = $this->db->query($sql);
302 if (!$resql2) {
303 $this->error = $this->db->lasterror();
304 $ok = 0;
305 }
306
307 if ($ok) {
308 $this->db->commit();
309 return 1;
310 } else {
311 $this->db->rollback();
312 return -1;
313 }
314 }
315
316
317 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
324 public function remove_product_fournisseur_price($rowid)
325 {
326 // phpcs:enable
327 global $conf, $user;
328
329 $error = 0;
330
331 $this->db->begin();
332
333 // Call trigger
334 $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_DELETE', $user);
335 if ($result < 0) {
336 $error++;
337 }
338 // End call triggers
339
340 if (empty($error)) {
341 $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
342 $sql .= " WHERE rowid = ".((int) $rowid);
343
344 dol_syslog(get_class($this)."::remove_product_fournisseur_price", LOG_DEBUG);
345 $resql = $this->db->query($sql);
346 if (!$resql) {
347 $this->error = $this->db->lasterror();
348 $error++;
349 }
350 }
351
352 if (empty($error)) {
353 $this->db->commit();
354 return 1;
355 } else {
356 $this->db->rollback();
357 return -1;
358 }
359 }
360
361
362 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
392 public function update_buyprice(
393 $qty,
394 $buyprice,
395 $user,
396 $price_base_type,
397 $fourn,
398 $availability,
399 $ref_fourn,
400 $tva_tx,
401 $charges = 0,
402 $remise_percent = 0,
403 $remise = 0,
404 $newnpr = 0,
405 $delivery_time_days = 0,
406 $supplier_reputation = '',
407 $localtaxes_array = array(),
408 $newdefaultvatcode = '',
409 $multicurrency_buyprice = 0,
410 $multicurrency_price_base_type = 'HT',
411 $multicurrency_tx = 1,
412 $multicurrency_code = '',
413 $desc_fourn = '',
414 $barcode = '',
415 $fk_barcode_type = 0,
416 $options = array()
417 ) {
418 // phpcs:enable
419 global $conf, $langs;
420 //global $mysoc;
421
422 // Clean parameter
423 if (empty($qty)) {
424 $qty = 0;
425 }
426 if (empty($buyprice)) {
427 $buyprice = 0;
428 }
429 if (empty($charges)) {
430 $charges = 0;
431 }
432 if (empty($availability)) {
433 $availability = 0;
434 }
435 if (empty($remise_percent)) {
436 $remise_percent = 0;
437 }
438 if (empty($supplier_reputation) || $supplier_reputation == -1) {
439 $supplier_reputation = '';
440 }
441 if ($delivery_time_days != '' && !is_numeric($delivery_time_days)) {
442 $delivery_time_days = 0;
443 }
444 if ($price_base_type == 'TTC') {
445 $ttx = $tva_tx;
446 $buyprice /= (1 + ($ttx / 100));
447 }
448
449 // Multicurrency
450 $multicurrency_unitBuyPrice = null;
451 $fk_multicurrency = null;
452 if (isModEnabled("multicurrency")) {
453 if (empty($multicurrency_tx)) {
454 $multicurrency_tx = 1;
455 }
456 if (empty($multicurrency_buyprice)) {
457 $multicurrency_buyprice = 0;
458 }
459 if ($multicurrency_price_base_type == 'TTC') {
460 $ttx = $tva_tx;
461 $multicurrency_buyprice /= (1 + ($ttx / 100));
462 }
463 $multicurrency_buyprice = (float) price2num($multicurrency_buyprice, 'MU');
464 $multicurrency_unitBuyPrice = (float) price2num((float) $multicurrency_buyprice / $qty, 'MU');
465
466 $buyprice = (float) $multicurrency_buyprice / $multicurrency_tx;
467 $fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $multicurrency_code);
468 }
469
470 $buyprice = (float) price2num($buyprice, 'MU');
471 $charges = (float) price2num($charges, 'MU');
472 $qty = (float) price2num($qty, 'MS');
473 $unitBuyPrice = (float) price2num($buyprice / $qty, 'MU');
474
475 // We can have a purchase ref that need to buy 100 min for a given price and with a packaging of 50.
476 $packaging = price2num((empty($this->product_fourn_packaging) ? $qty : $this->product_fourn_packaging), 'MS');
477
478 $error = 0;
479 $now = dol_now();
480
481 $newvat = $tva_tx;
482
483 if (count($localtaxes_array) > 0) {
484 $localtaxtype1 = $localtaxes_array['0'];
485 $localtax1 = $localtaxes_array['1'];
486 $localtaxtype2 = $localtaxes_array['2'];
487 $localtax2 = $localtaxes_array['3'];
488 } else { // old method. deprecated because it can't retrieve type
489 $localtaxtype1 = '0';
490 $localtax1 = get_localtax($newvat, 1);
491 $localtaxtype2 = '0';
492 $localtax2 = get_localtax($newvat, 2);
493 }
494 if (empty($localtax1)) {
495 $localtax1 = 0; // If = '' then = 0
496 }
497 if (empty($localtax2)) {
498 $localtax2 = 0; // If = '' then = 0
499 }
500
501 $this->db->begin();
502
503 if ($this->product_fourn_price_id > 0) {
504 // check if price already logged, if not first log current price
505 $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id);
506 if (is_array($logPrices) && count($logPrices) == 0) {
507 $currentPfp = new self($this->db);
508 $result = $currentPfp->fetch_product_fournisseur_price($this->product_fourn_price_id);
509 if ($result > 0 && $currentPfp->fourn_price != 0) {
510 $currentPfpUser = new User($this->db);
511 $result = $currentPfpUser->fetch($currentPfp->user_id);
512 if ($result > 0) {
513 $currentPfp->logPrice(
514 $currentPfpUser,
515 $currentPfp->date_creation,
516 $currentPfp->fourn_price,
517 $currentPfp->fourn_qty,
518 $currentPfp->fourn_multicurrency_price,
519 $currentPfp->fourn_multicurrency_unitprice,
520 $currentPfp->fourn_multicurrency_tx,
521 $currentPfp->fourn_multicurrency_id,
522 $currentPfp->fourn_multicurrency_code
523 );
524 }
525 }
526 }
527 $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
528 $sql .= " SET fk_user = ".((int) $user->id)." ,";
529 $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.
530 $sql .= " ref_fourn = '".$this->db->escape($ref_fourn)."',";
531 $sql .= " desc_fourn = '".$this->db->escape($desc_fourn)."',";
532 $sql .= " price = ".((float) $buyprice).",";
533 $sql .= " quantity = ".((float) $qty).",";
534 $sql .= " remise_percent = ".((float) $remise_percent).",";
535 $sql .= " remise = ".((float) $remise).",";
536 $sql .= " unitprice = ".((float) $unitBuyPrice).",";
537 $sql .= " fk_availability = ".((int) $availability).",";
538 $sql .= " multicurrency_price = ".(isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
539 $sql .= " multicurrency_unitprice = ".(isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
540 $sql .= " multicurrency_tx = ".(isset($multicurrency_tx) ? "'".$this->db->escape((string) $multicurrency_tx)."'" : '1').",";
541 $sql .= " fk_multicurrency = ".(isset($fk_multicurrency) ? (int) $fk_multicurrency : 'null').",";
542 $sql .= " multicurrency_code = ".(isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
543 $sql .= " entity = ".((int) $conf->entity).",";
544 $sql .= " tva_tx = ".((float) price2num($tva_tx)).",";
545 // TODO Add localtax1 and localtax2
546 //$sql.= " localtax1_tx=".($localtax1>=0?$localtax1:'NULL').",";
547 //$sql.= " localtax2_tx=".($localtax2>=0?$localtax2:'NULL').",";
548 //$sql.= " localtax1_type=".($localtaxtype1!=''?"'".$this->db->escape($localtaxtype1)."'":"'0'").",";
549 //$sql.= " localtax2_type=".($localtaxtype2!=''?"'".$this->db->escape($localtaxtype2)."'":"'0'").",";
550 $sql .= " default_vat_code = ".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
551 $sql .= " info_bits = ".((int) $newnpr).",";
552 $sql .= " charges = ".((float) $charges).","; // deprecated
553 $sql .= " delivery_time_days = ".($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
554 $sql .= " supplier_reputation = ".(empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
555 $sql .= " barcode = ".(empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
556 $sql .= " fk_barcode_type = ".(empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape((string) $fk_barcode_type)."'");
557 if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
558 $sql .= ", packaging = ".(empty($packaging) ? 1 : $packaging);
559 }
560 $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
561
562 if (!$error) {
563 if (!empty($options) && is_array($options)) {
564 $productfournisseurprice = new ProductFournisseurPrice($this->db);
565 $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
566 if ($res > 0) {
567 foreach ($options as $key => $value) {
568 $productfournisseurprice->array_options[$key] = $value;
569 }
570 $res = $productfournisseurprice->update($user);
571 if ($res < 0) {
572 $this->error = $productfournisseurprice->error;
573 $this->errors = $productfournisseurprice->errors;
574 $error++;
575 }
576 }
577 }
578 }
579
580 // TODO Add price_base_type and price_ttc
581
582 dol_syslog(get_class($this).'::update_buyprice update knowing id of line = product_fourn_price_id = '.$this->product_fourn_price_id, LOG_DEBUG);
583 $resql = $this->db->query($sql);
584 if ($resql) {
585 // Call trigger
586 $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_MODIFY', $user);
587 if ($result < 0) {
588 $error++;
589 }
590 // End call triggers
591 if (!$error && !getDolGlobalString('PRODUCT_PRICE_SUPPLIER_NO_LOG')) {
592 $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, (float) $multicurrency_unitBuyPrice, $multicurrency_tx, (int) $fk_multicurrency, $multicurrency_code);
593 if ($result < 0) {
594 $error++;
595 }
596 }
597 if (empty($error)) {
598 $this->db->commit();
599 return $this->product_fourn_price_id;
600 } else {
601 $this->db->rollback();
602 return -1;
603 }
604 } else {
605 $this->error = $this->db->error()." sql=".$sql;
606 $this->db->rollback();
607 return -2;
608 }
609 } else {
610 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);
611
612 // Delete price for this quantity
613 $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
614 $sql .= " WHERE fk_soc = ".((int) $fourn->id)." AND ref_fourn = '".$this->db->escape($ref_fourn)."' AND quantity = ".((float) $qty)." AND entity = ".((int) $conf->entity);
615 $resql = $this->db->query($sql);
616 if ($resql) {
617 // Add price for this quantity to supplier
618 $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price(";
619 $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
620 $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";
621 if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
622 $sql .= ", packaging";
623 }
624 $sql .= ") values(";
625 $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
626 $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
627 $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape((string) $multicurrency_tx)."'" : '1').",";
628 $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape((string) $fk_multicurrency)."'" : 'null').",";
629 $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
630 $sql .= " '".$this->db->idate($now)."',";
631 $sql .= " ".((int) $this->id).",";
632 $sql .= " ".((int) $fourn->id).",";
633 $sql .= " '".$this->db->escape($ref_fourn)."',";
634 $sql .= " '".$this->db->escape($desc_fourn)."',";
635 $sql .= " ".((int) $user->id).",";
636 $sql .= " ".price2num($buyprice).",";
637 $sql .= " ".((float) $qty).",";
638 $sql .= " ".((float) $remise_percent).",";
639 $sql .= " ".((float) $remise).",";
640 $sql .= " ".price2num($unitBuyPrice).",";
641 $sql .= " ".price2num($tva_tx).",";
642 $sql .= " ".price2num($charges).",";
643 $sql .= " ".((int) $availability).",";
644 $sql .= " ".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
645 $sql .= " ".((int) $newnpr).",";
646 $sql .= $conf->entity.",";
647 $sql .= ($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
648 $sql .= (empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
649 $sql .= (empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
650 $sql .= (empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape((string) $fk_barcode_type)."'");
651 if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
652 $sql .= ", ".(empty($this->packaging) ? '1' : "'".$this->db->escape((string) $this->packaging)."'");
653 }
654 $sql .= ")";
655
656 $this->product_fourn_price_id = 0;
657
658 $resql = $this->db->query($sql);
659 if ($resql) {
660 $this->product_fourn_price_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur_price");
661 } else {
662 $this->error = $this->db->lasterror();
663 $error++;
664 }
665
666 if (!$error) {
667 if (!empty($options) && is_array($options)) {
668 $productfournisseurprice = new ProductFournisseurPrice($this->db);
669 $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
670 if ($res > 0) {
671 foreach ($options as $key => $value) {
672 $productfournisseurprice->array_options[$key] = $value;
673 }
674 $res = $productfournisseurprice->update($user);
675 if ($res < 0) {
676 $this->error = $productfournisseurprice->error;
677 $this->errors = $productfournisseurprice->errors;
678 $error++;
679 }
680 }
681 }
682 }
683
684 if (!$error && !getDolGlobalString('PRODUCT_PRICE_SUPPLIER_NO_LOG')) {
685 // Add record into log table
686 // $this->product_fourn_price_id must be set
687 $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, (float) $multicurrency_unitBuyPrice, $multicurrency_tx, (int) $fk_multicurrency, $multicurrency_code);
688 if ($result < 0) {
689 $error++;
690 }
691 }
692
693 if (!$error) {
694 // Call trigger
695 $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_CREATE', $user);
696 if ($result < 0) {
697 $error++;
698 }
699 // End call triggers
700
701 if (empty($error)) {
702 $this->db->commit();
703 return $this->product_fourn_price_id;
704 } else {
705 $this->db->rollback();
706 return -1;
707 }
708 } else {
709 $this->error = $this->db->lasterror()." sql=".$sql;
710 $this->db->rollback();
711 return -2;
712 }
713 } else {
714 $this->error = $this->db->lasterror()." sql=".$sql;
715 $this->db->rollback();
716 return -1;
717 }
718 }
719 }
720
721 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
729 public function fetch_product_fournisseur_price($rowid, $ignore_expression = 0)
730 {
731 // phpcs:enable
732 global $conf;
733
734 $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,";
735 $sql .= " pfp.fk_soc, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_product, pfp.charges, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
736 $sql .= " pfp.supplier_reputation, pfp.fk_user, pfp.datec,";
737 $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
738 $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging,";
739 $sql .= " p.ref as product_ref, p.tosell as status, p.tobuy as status_buy";
740 $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p";
741 $sql .= " WHERE pfp.rowid = ".(int) $rowid;
742 $sql .= " AND pfp.fk_product = p.rowid";
743
744 dol_syslog(get_class($this)."::fetch_product_fournisseur_price", LOG_DEBUG);
745 $resql = $this->db->query($sql);
746 if ($resql) {
747 $obj = $this->db->fetch_object($resql);
748 if ($obj) {
749 $this->product_fourn_price_id = $rowid;
750 $this->id = $obj->fk_product;
751
752 $this->fk_product = $obj->fk_product;
753 $this->product_id = $obj->fk_product;
754 $this->product_ref = $obj->product_ref;
755 $this->status = $obj->status;
756 $this->status_buy = $obj->status_buy;
757 $this->fourn_id = $obj->fk_soc;
758 $this->fourn_ref = $obj->ref_fourn; // deprecated
759 $this->ref_supplier = $obj->ref_fourn;
760 $this->desc_supplier = $obj->desc_fourn;
761 $this->fourn_price = $obj->price;
762 $this->fourn_charges = $obj->charges; // when getDolGlobalString('PRODUCT_CHARGES') is set
763 $this->fourn_qty = $obj->quantity;
764 $this->fourn_remise_percent = $obj->remise_percent;
765 $this->fourn_remise = $obj->remise;
766 $this->fourn_unitprice = $obj->unitprice;
767 $this->fourn_tva_tx = $obj->tva_tx;
768 $this->fourn_tva_npr = $obj->fourn_tva_npr;
769 // Add also localtaxes
770 $this->fk_availability = $obj->fk_availability;
771 $this->delivery_time_days = $obj->delivery_time_days;
772 $this->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
773 $this->supplier_reputation = $obj->supplier_reputation;
774 $this->default_vat_code = $obj->default_vat_code;
775 $this->user_id = $obj->fk_user;
776 $this->date_creation = $this->db->jdate($obj->datec);
777 $this->fourn_multicurrency_price = $obj->multicurrency_price;
778 $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
779 $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
780 $this->fourn_multicurrency_id = $obj->fk_multicurrency;
781 $this->fourn_multicurrency_code = $obj->multicurrency_code;
782 if (isModEnabled('barcode')) {
783 $this->fourn_barcode = $obj->barcode; // deprecated
784 $this->fourn_fk_barcode_type = $obj->fk_barcode_type; // deprecated
785 $this->supplier_barcode = $obj->barcode;
786 $this->supplier_fk_barcode_type = $obj->fk_barcode_type;
787 }
788 $this->packaging = (float) $obj->packaging;
789
790 if (isModEnabled('dynamicprices') && empty($ignore_expression) && !empty($this->fk_supplier_price_expression)) {
791 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
792 $priceparser = new PriceParser($this->db);
793 $price_result = $priceparser->parseProductSupplier($this);
794 if ($price_result >= 0) {
795 $this->fourn_price = $price_result;
796 //recalculation of unitprice, as probably the price changed...
797 if ($this->fourn_qty != 0) {
798 $this->fourn_unitprice = price2num($this->fourn_price / $this->fourn_qty, 'MU');
799 } else {
800 $this->fourn_unitprice = "";
801 }
802 }
803 }
804
805 return 1;
806 } else {
807 return 0;
808 }
809 } else {
810 $this->error = $this->db->lasterror();
811 return -1;
812 }
813 }
814
815
816 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
829 public function list_product_fournisseur_price($prodid, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0, $socid = 0)
830 {
831 // phpcs:enable
832 global $conf;
833
834 $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, ";
835 $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,";
836 $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,";
837 $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code, pfp.datec, pfp.tms,";
838 $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging, pfp.status as pfstatus";
839 $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."societe as s";
840 $sql .= " WHERE pfp.entity IN (".getEntity('productsupplierprice').")";
841 $sql .= " AND pfp.fk_soc = s.rowid AND pfp.fk_product = p.rowid";
842 $sql .= ($socid > 0 ? ' AND pfp.fk_soc = '.((int) $socid) : '');
843 $sql .= " AND s.status = 1"; // only enabled company selected
844 $sql .= " AND pfp.fk_product = ".((int) $prodid);
845 if (empty($sortfield)) {
846 $sql .= " ORDER BY s.nom, pfp.quantity, pfp.price";
847 } else {
848 $sql .= $this->db->order($sortfield, $sortorder);
849 }
850 $sql .= $this->db->plimit($limit, $offset);
851 dol_syslog(get_class($this)."::list_product_fournisseur_price", LOG_DEBUG);
852
853 $resql = $this->db->query($sql);
854 if ($resql) {
855 $retarray = array();
856
857 while ($record = $this->db->fetch_array($resql)) {
858 //define base attribute
859 $prodfourn = new ProductFournisseur($this->db);
860
861 $prodfourn->product_ref = $record["product_ref"];
862 $prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"];
863 $prodfourn->status = $record["status"];
864 $prodfourn->status_buy = $record["status_buy"];
865 $prodfourn->product_fourn_id = $record["product_fourn_id"];
866 $prodfourn->product_fourn_entity = $record["entity"];
867 $prodfourn->ref_supplier = $record["ref_fourn"];
868 $prodfourn->fourn_ref = $record["ref_fourn"];
869 $prodfourn->desc_supplier = $record["desc_fourn"];
870 $prodfourn->fourn_price = $record["price"];
871 $prodfourn->fourn_qty = $record["quantity"];
872 $prodfourn->fourn_remise_percent = $record["remise_percent"];
873 $prodfourn->fourn_remise = $record["remise"];
874 $prodfourn->fourn_unitprice = $record["unitprice"];
875 $prodfourn->fourn_charges = $record["charges"]; // when getDolGlobalString('PRODUCT_CHARGES') is set
876 $prodfourn->fourn_tva_tx = $record["tva_tx"];
877 $prodfourn->fourn_id = $record["fourn_id"];
878 $prodfourn->fourn_name = $record["supplier_name"];
879 $prodfourn->fk_availability = $record["fk_availability"];
880 $prodfourn->delivery_time_days = $record["delivery_time_days"];
881 $prodfourn->id = $prodid;
882 $prodfourn->fourn_tva_npr = $record["info_bits"];
883 $prodfourn->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
884 $prodfourn->supplier_reputation = $record["supplier_reputation"];
885 $prodfourn->fourn_date_creation = $this->db->jdate($record['datec']);
886 $prodfourn->fourn_date_modification = $this->db->jdate($record['tms']);
887
888 $prodfourn->fourn_multicurrency_price = $record["multicurrency_price"];
889 $prodfourn->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
890 $prodfourn->fourn_multicurrency_tx = $record["multicurrency_tx"];
891 $prodfourn->fourn_multicurrency_id = $record["fk_multicurrency"];
892 $prodfourn->fourn_multicurrency_code = $record["multicurrency_code"];
893
894 $prodfourn->packaging = (float) $record["packaging"];
895 $prodfourn->status = $record["pfstatus"];
896
897 if (isModEnabled('barcode')) {
898 $prodfourn->supplier_barcode = $record["barcode"];
899 $prodfourn->supplier_fk_barcode_type = $record["fk_barcode_type"];
900 }
901
902 if (isModEnabled('dynamicprices') && !empty($prodfourn->fk_supplier_price_expression)) {
903 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
904 $priceparser = new PriceParser($this->db);
905 $price_result = $priceparser->parseProductSupplier($prodfourn);
906 if ($price_result >= 0) {
907 $prodfourn->fourn_price = $price_result;
908 // Set to null on purpose an will have non-null value before method return @phan-suppress-next-line PhanTypeMismatchPropertyProbablyReal
909 $prodfourn->fourn_unitprice = null; //force recalculation of unitprice, as probably the price changed... @phpstan-ignore-line
910 }
911 }
912
913 if (!isset($prodfourn->fourn_unitprice)) {
914 if ($prodfourn->fourn_qty != 0) {
915 $prodfourn->fourn_unitprice = price2num($prodfourn->fourn_price / $prodfourn->fourn_qty, 'MU');
916 } else {
917 $prodfourn->fourn_unitprice = "";
918 }
919 }
920
921 $retarray[] = $prodfourn;
922 }
923
924 $this->db->free($resql);
925 return $retarray;
926 } else {
927 $this->error = $this->db->error();
928 return -1;
929 }
930 }
931
932 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
942 public function find_min_price_product_fournisseur($prodid, $qty = 0, $socid = 0)
943 {
944 // phpcs:enable
945 if (empty($prodid)) {
946 dol_syslog("Warning function find_min_price_product_fournisseur were called with prodid empty. May be a bug.", LOG_WARNING);
947 return 0;
948 }
949
950 $this->product_fourn_price_id = 0;
951 $this->product_fourn_id = 0;
952 $this->fourn_ref = '';
953 $this->fourn_price = 0;
954 $this->fourn_qty = 0;
955 $this->fourn_remise_percent = 0;
956 $this->fourn_remise = 0;
957 $this->fourn_unitprice = 0;
958 $this->fourn_id = 0;
959 $this->fourn_name = '';
960 $this->delivery_time_days = 0;
961 $this->id = 0;
962
963 $this->fourn_multicurrency_price = 0;
964 $this->fourn_multicurrency_unitprice = 0;
965 $this->fourn_multicurrency_tx = 0;
966 $this->fourn_multicurrency_id = 0;
967 $this->fourn_multicurrency_code = '';
968
969 $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
970 $sql .= " pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
971 $sql .= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges,";
972 $sql .= " pfp.remise, pfp.remise_percent, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
973 $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code";
974 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
975 $sql .= " WHERE s.entity IN (".getEntity('societe').")";
976 $sql .= " AND pfp.entity IN (".getEntity('productsupplierprice').")";
977 $sql .= " AND pfp.fk_product = ".((int) $prodid);
978 $sql .= " AND pfp.fk_soc = s.rowid";
979 $sql .= " AND s.status = 1"; // only enabled society
980 if ($qty > 0) {
981 $sql .= " AND pfp.quantity <= ".((float) $qty);
982 }
983 if ($socid > 0) {
984 $sql .= ' AND pfp.fk_soc = '.((int) $socid);
985 }
986
987 dol_syslog(get_class($this)."::find_min_price_product_fournisseur", LOG_DEBUG);
988
989 $resql = $this->db->query($sql);
990 if ($resql) {
991 $record_array = array();
992
993 //Store each record to array for later search of min
994 while ($record = $this->db->fetch_array($resql)) {
995 $record_array[] = $record;
996 }
997
998 if (count($record_array) == 0) {
999 $this->db->free($resql);
1000 return 0;
1001 } else {
1002 $min = -1;
1003 foreach ($record_array as $record) {
1004 $fourn_price = $record["price"];
1005 // calculate unit price for quantity 1
1006 $fourn_unitprice = $record["unitprice"];
1007 $fourn_unitprice_with_discount = $record["unitprice"] * (1 - $record["remise_percent"] / 100);
1008
1009 if (isModEnabled('dynamicprices') && !empty($record["fk_supplier_price_expression"])) {
1010 $prod_supplier = new ProductFournisseur($this->db);
1011 $prod_supplier->product_fourn_price_id = $record["product_fourn_price_id"];
1012 $prod_supplier->id = $prodid;
1013 $prod_supplier->fourn_qty = $record["quantity"];
1014 $prod_supplier->fourn_tva_tx = $record["tva_tx"];
1015 $prod_supplier->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
1016
1017 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
1018 $priceparser = new PriceParser($this->db);
1019 $price_result = $priceparser->parseProductSupplier($prod_supplier);
1020 if ($price_result >= 0) {
1021 $fourn_price = price2num($price_result, 'MU');
1022 if ($record["quantity"] != 0) {
1023 $fourn_unitprice = price2num((float) $fourn_price / $record["quantity"], 'MU');
1024 } else {
1025 $fourn_unitprice = $fourn_price;
1026 }
1027 $fourn_unitprice_with_discount = (float) $fourn_unitprice * (1 - $record["remise_percent"] / 100);
1028 }
1029 }
1030
1031 if ($fourn_unitprice_with_discount < $min || $min == -1) {
1032 $this->id = $prodid;
1033 $this->product_fourn_price_id = $record["product_fourn_price_id"];
1034 $this->ref_supplier = $record["ref_fourn"];
1035 $this->ref_fourn = $record["ref_fourn"]; // deprecated
1036 $this->fourn_ref = $record["ref_fourn"]; // deprecated
1037 $this->fourn_price = $fourn_price;
1038 $this->fourn_qty = $record["quantity"];
1039 $this->fourn_remise_percent = $record["remise_percent"];
1040 $this->fourn_remise = $record["remise"];
1041 $this->fourn_unitprice = $fourn_unitprice;
1042 $this->fourn_unitprice_with_discount = $fourn_unitprice_with_discount;
1043 $this->fourn_charges = $record["charges"]; // when getDolGlobalString('PRODUCT_CHARGES') is set
1044 $this->fourn_tva_tx = $record["tva_tx"];
1045 $this->fourn_id = $record["fourn_id"]; // thirdparty id
1046 $this->fourn_name = $record["supplier_name"];
1047 $this->delivery_time_days = $record["delivery_time_days"];
1048 // False positive @phan-suppress-next-line PhanTypeMismatchProperty
1049 $this->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
1050 $this->fourn_multicurrency_price = $record["multicurrency_price"];
1051 $this->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
1052 $this->fourn_multicurrency_tx = $record["multicurrency_tx"];
1053 $this->fourn_multicurrency_id = $record["fk_multicurrency"];
1054 $this->fourn_multicurrency_code = $record["multicurrency_code"];
1055
1056 $min = $fourn_unitprice_with_discount;
1057 }
1058 }
1059 }
1060
1061 $this->db->free($resql);
1062
1063 return $this->product_fourn_price_id;
1064 } else {
1065 $this->error = $this->db->error();
1066 return -1;
1067 }
1068 }
1069
1076 public function setSupplierPriceExpression($expression_id)
1077 {
1078 global $conf;
1079
1080 // Clean parameters
1081 $this->db->begin();
1082 $expression_id = $expression_id != 0 ? $expression_id : 'NULL';
1083
1084 $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
1085 $sql .= " SET fk_supplier_price_expression = ".((int) $expression_id);
1086 $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
1087
1088 dol_syslog(get_class($this)."::setSupplierPriceExpression", LOG_DEBUG);
1089
1090 $resql = $this->db->query($sql);
1091 if ($resql) {
1092 $this->db->commit();
1093 return 1;
1094 } else {
1095 $this->error = $this->db->error()." sql=".$sql;
1096 $this->db->rollback();
1097 return -1;
1098 }
1099 }
1100
1111 public function getSocNomUrl($withpicto = 0, $option = 'supplier', $maxlen = 0, $notooltip = 0)
1112 {
1113 $thirdparty = new Fournisseur($this->db);
1114 $thirdparty->fetch($this->fourn_id);
1115
1116 return $thirdparty->getNomUrl($withpicto, $option, $maxlen, $notooltip);
1117 }
1118
1119 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1131 public function display_price_product_fournisseur($showunitprice = 1, $showsuptitle = 1, $maxlen = 0, $notooltip = 0, $productFournList = array())
1132 {
1133 // phpcs:enable
1134 global $conf, $langs, $user;
1135
1136 $out = '';
1137 $langs->load("suppliers");
1138 if (count($productFournList) > 0) {
1139 $out .= '<table class="centpercent liste nomarginbottom">';
1140 $out .= '<tr class="liste_titre"><td class="liste_titre right">'.($showunitprice ? $langs->trans("Price").' '.$langs->trans("HT") : '').'</td>';
1141 $out .= '<td class="liste_titre right">'.($showunitprice ? $langs->trans("QtyMin") : '').'</td>';
1142 $out .= '<td class="liste_titre">'.$langs->trans("Supplier").'</td>';
1143 $out .= '<td class="liste_titre">'.$langs->trans("SupplierRef").'</td></tr>';
1144 foreach ($productFournList as $productFourn) {
1145 $out .= '<tr><td class="right">'.($showunitprice ? price($productFourn->fourn_unitprice * (1 - $productFourn->fourn_remise_percent / 100) - $productFourn->fourn_remise) : '').'</td>';
1146 $out .= '<td class="right">'.($showunitprice ? $productFourn->fourn_qty : '').'</td>';
1147 $out .= '<td>'.$productFourn->getSocNomUrl(1, 'supplier', $maxlen, $notooltip).'</td>';
1148 $out .= '<td>'.dolPrintHTML($productFourn->fourn_ref).'</td></tr>';
1149 }
1150 $out .= '</table>';
1151 } else {
1152 $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") : '');
1153 if ($user->hasRight("fournisseur", "read")) { // Without permission, never show the best supplier seller
1154 $out .= ($showunitprice ? ' &nbsp; <span class="opacitymedium">(</span>' : '');
1155 $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;
1156 $out .= ($showunitprice ? '<span class="opacitymedium">)</span>' : '');
1157 }
1158 }
1159 return $out;
1160 }
1161
1170 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1171 {
1172 $tables = array(
1173 'product_fournisseur_price'
1174 );
1175
1176 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1177 }
1178
1187 public static function replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
1188 {
1189 $tables = array(
1190 'product_fournisseur_price'
1191 );
1192
1193 return CommonObject::commonReplaceProduct($dbs, $origin_id, $dest_id, $tables);
1194 }
1195
1206 public function listProductFournisseurPriceLog($product_fourn_price_id, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0)
1207 {
1208 $sql = "SELECT";
1209 $sql .= " u.lastname,";
1210 $sql .= " pfpl.rowid, pfp.ref_fourn as supplier_ref, pfpl.datec,";
1211 $sql .= " pfpl.price, pfpl.quantity,";
1212 $sql .= " pfpl.fk_multicurrency, pfpl.multicurrency_code, pfpl.multicurrency_tx, pfpl.multicurrency_price, pfpl.multicurrency_unitprice";
1213 $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price_log as pfpl,";
1214 $sql .= " ".MAIN_DB_PREFIX."product_fournisseur_price as pfp,";
1215 $sql .= " ".MAIN_DB_PREFIX."user as u";
1216 $sql .= " WHERE pfp.entity IN (".getEntity('productprice').")";
1217 $sql .= " AND pfpl.fk_user = u.rowid";
1218 $sql .= " AND pfp.rowid = pfpl.fk_product_fournisseur";
1219 $sql .= " AND pfpl.fk_product_fournisseur = ".((int) $product_fourn_price_id);
1220 if (empty($sortfield)) {
1221 $sql .= " ORDER BY pfpl.datec";
1222 } else {
1223 $sql .= $this->db->order($sortfield, $sortorder);
1224 }
1225 $sql .= $this->db->plimit($limit, $offset);
1226 dol_syslog(get_class($this)."::list_product_fournisseur_price_log", LOG_DEBUG);
1227
1228 $resql = $this->db->query($sql);
1229 if ($resql) {
1230 $retarray = array();
1231
1232 while ($obj = $this->db->fetch_object($resql)) {
1233 $tmparray = array();
1234 $tmparray['rowid'] = $obj->rowid;
1235 $tmparray['supplier_ref'] = $obj->supplier_ref;
1236 $tmparray['datec'] = $this->db->jdate($obj->datec);
1237 $tmparray['lastname'] = $obj->lastname;
1238 $tmparray['price'] = $obj->price;
1239 $tmparray['quantity'] = $obj->quantity;
1240 $tmparray['fk_multicurrency'] = $obj->fk_multicurrency;
1241 $tmparray['multicurrency_code'] = $obj->multicurrency_code;
1242 $tmparray['multicurrency_tx'] = $obj->multicurrency_tx;
1243 $tmparray['multicurrency_price'] = $obj->multicurrency_price;
1244 $tmparray['multicurrency_unitprice'] = $obj->multicurrency_unitprice;
1245
1246 $retarray[] = $tmparray;
1247 }
1248
1249 $this->db->free($resql);
1250 return $retarray;
1251 } else {
1252 $this->error = $this->db->error();
1253 return -1;
1254 }
1255 }
1256
1264 public function displayPriceProductFournisseurLog($productFournLogList = array())
1265 {
1266 global $conf, $langs;
1267
1268 $out = '';
1269 $langs->load("suppliers");
1270 if (count($productFournLogList) > 0) {
1271 $out .= '<table class="noborder centpercent">';
1272 $out .= '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Date").'</td>';
1273 $out .= '<td class="liste_titre right">'.$langs->trans("Price").'</td>';
1274 //$out .= '<td class="liste_titre right">'.$langs->trans("QtyMin").'</td>';
1275 $out .= '<td class="liste_titre">'.$langs->trans("User").'</td></tr>';
1276 foreach ($productFournLogList as $productFournLog) {
1277 $out .= '<tr><td>'.dol_print_date($productFournLog['datec'], 'dayhour', 'tzuser').'</td>';
1278 $out .= '<td class="right">'.price($productFournLog['price'], 0, $langs, 1, -1, -1, $conf->currency);
1279 if ($productFournLog['multicurrency_code'] != $conf->currency) {
1280 $out .= ' ('.price($productFournLog['multicurrency_price'], 0, $langs, 1, -1, -1, $productFournLog['multicurrency_code']).')';
1281 }
1282 $out .= '</td>';
1283 //$out.= '<td class="right">'.$productFournLog['quantity'].'</td>';
1284 $out .= '<td>'.$productFournLog['lastname'].'</td></tr>';
1285 }
1286 $out .= '</table>';
1287 }
1288 return $out;
1289 }
1290
1291
1306 public function getNomUrl($withpicto = 0, $option = '', $maxlength = 0, $save_lastsearch_value = -1, $notooltip = 0, $morecss = '', $add_label = 0, $sep = ' - ')
1307 {
1308 global $db, $conf, $langs, $hookmanager;
1309
1310 if (!empty($conf->dol_no_mouse_hover)) {
1311 $notooltip = 1; // Force disable tooltips
1312 }
1313
1314 $result = '';
1315 $label = '';
1316
1317 $newref = $this->ref;
1318 if ($maxlength) {
1319 $newref = dol_trunc($newref, $maxlength, 'middle');
1320 }
1321
1322 if (!empty($this->entity)) {
1323 $tmpphoto = $this->show_photos('product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 80);
1324 if ($this->nbphoto > 0) {
1325 $label .= '<div class="photointooltip">';
1326 $label .= $tmpphoto;
1327 $label .= '</div><div style="clear: both;"></div>';
1328 }
1329 }
1330
1331 if ($this->type == Product::TYPE_PRODUCT) {
1332 $label .= img_picto('', 'product').' <u class="paddingrightonly">'.$langs->trans("Product").'</u>';
1333 } elseif ($this->type == Product::TYPE_SERVICE) {
1334 $label .= img_picto('', 'service').' <u class="paddingrightonly">'.$langs->trans("Service").'</u>';
1335 }
1336 if (isset($this->status) && isset($this->status_buy)) {
1337 $label .= ' '.$this->getLibStatut(5, 0);
1338 $label .= ' '.$this->getLibStatut(5, 1);
1339 }
1340
1341 if (!empty($this->ref)) {
1342 $label .= '<br><b>'.$langs->trans('ProductRef').':</b> '.($this->ref ? $this->ref : $this->product_ref);
1343 }
1344 if (!empty($this->label)) {
1345 $label .= '<br><b>'.$langs->trans('ProductLabel').':</b> '.$this->label;
1346 }
1347 $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
1348
1349 if ($this->type == Product::TYPE_PRODUCT || getDolGlobalString('STOCK_SUPPORTS_SERVICES')) {
1350 if (isModEnabled('productbatch')) {
1351 $langs->load("productbatch");
1352 $label .= "<br><b>".$langs->trans("ManageLotSerial").'</b>: '.$this->getLibStatut(0, 2);
1353 }
1354 }
1355 if (isModEnabled('barcode')) {
1356 $label .= '<br><b>'.$langs->trans('BarCode').':</b> '.$this->barcode;
1357 }
1358
1359 if ($this->type == Product::TYPE_PRODUCT) {
1360 if ($this->weight) {
1361 $label .= "<br><b>".$langs->trans("Weight").'</b>: '.$this->weight.' '.measuringUnitString(0, "weight", $this->weight_units);
1362 }
1363 $labelsize = "";
1364 if ($this->length) {
1365 $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Length").'</b>: '.$this->length.' '.measuringUnitString(0, 'size', $this->length_units);
1366 }
1367 if ($this->width) {
1368 $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Width").'</b>: '.$this->width.' '.measuringUnitString(0, 'size', $this->width_units);
1369 }
1370 if ($this->height) {
1371 $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Height").'</b>: '.$this->height.' '.measuringUnitString(0, 'size', $this->height_units);
1372 }
1373 if ($labelsize) {
1374 $label .= "<br>".$labelsize;
1375 }
1376
1377 $labelsurfacevolume = "";
1378 if ($this->surface) {
1379 $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Surface").'</b>: '.$this->surface.' '.measuringUnitString(0, 'surface', $this->surface_units);
1380 }
1381 if ($this->volume) {
1382 $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Volume").'</b>: '.$this->volume.' '.measuringUnitString(0, 'volume', $this->volume_units);
1383 }
1384 if ($labelsurfacevolume) {
1385 $label .= "<br>".$labelsurfacevolume;
1386 }
1387 }
1388
1389 if (isModEnabled('accounting') && $this->status) {
1390 include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1391 $label .= '<br><b>'.$langs->trans('ProductAccountancySellCode').':</b> '.length_accountg($this->accountancy_code_sell);
1392 $label .= '<br><b>'.$langs->trans('ProductAccountancySellIntraCode').':</b> '.length_accountg($this->accountancy_code_sell_intra);
1393 $label .= '<br><b>'.$langs->trans('ProductAccountancySellExportCode').':</b> '.length_accountg($this->accountancy_code_sell_export);
1394 }
1395 if (isModEnabled('accounting') && $this->status_buy) {
1396 include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1397 $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyCode').':</b> '.length_accountg($this->accountancy_code_buy);
1398 $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyIntraCode').':</b> '.length_accountg($this->accountancy_code_buy_intra);
1399 $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyExportCode').':</b> '.length_accountg($this->accountancy_code_buy_export);
1400 }
1401
1402 $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id, 'pfpl.datec', 'DESC', getDolGlobalInt('MAIN_TOOLTIP_PRICELOG_HISTORY_LIMIT', 10)); // set sort order here
1403 if (is_array($logPrices) && count($logPrices) > 0) {
1404 $label .= '<br><br>';
1405 $label .= '<u>'.$langs->trans("History").'</u>';
1406 $label .= $this->displayPriceProductFournisseurLog($logPrices);
1407 }
1408
1409 $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);
1410
1411 if ($option != 'nolink') {
1412 // Add param to save lastsearch_values or not
1413 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1414 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1415 $add_save_lastsearch_values = 1;
1416 }
1417 if ($add_save_lastsearch_values) {
1418 $url .= '&save_lastsearch_values=1';
1419 }
1420 }
1421
1422 $allowothertags = array('table', 'tr', 'td');
1423 $linkclose = '';
1424 if (empty($notooltip)) {
1425 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1426 $label = $langs->trans("SupplierRef");
1427 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label, 0, $allowothertags).'"';
1428 }
1429 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label, 0, $allowothertags).'"';
1430 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
1431 } else {
1432 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1433 }
1434
1435 $linkstart = '<a href="'.$url.'"';
1436 $linkstart .= $linkclose.'>';
1437 $linkend = '</a>';
1438
1439 $result .= $linkstart;
1440 if ($withpicto) {
1441 $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);
1442 }
1443 if ($withpicto != 2) {
1444 $result .= $newref.($this->ref_supplier ? ' ('.$this->ref_supplier.')' : '');
1445 }
1446 $result .= $linkend;
1447 if ($withpicto != 2) {
1448 $result .= (($add_label && $this->label) ? $sep.dol_trunc($this->label, ($add_label > 1 ? $add_label : 0)) : '');
1449 }
1450
1451 global $action;
1452 $hookmanager->initHooks(array($this->element . 'dao'));
1453 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1454 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1455 if ($reshook > 0) {
1456 $result = $hookmanager->resPrint;
1457 } else {
1458 $result .= $hookmanager->resPrint;
1459 }
1460 return $result;
1461 }
1462
1470 public function getLibStatut($mode = 0, $type = 0) // must be compatible with getLibStatut of inherited Product
1471 {
1472 return $this->LibStatut($this->status, $mode);
1473 }
1474
1475 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1484 public function LibStatut($status, $mode = 0, $type = 0)
1485 {
1486 // phpcs:enable
1487 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1488 global $langs;
1489 //$langs->load("mymodule@mymodule");
1490 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Enabled');
1491 $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
1492 $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Enabled');
1493 $this->labelStatusShort[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
1494 }
1495
1496 $statusType = 'status4';
1497 //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
1498 if ($status == self::STATUS_CANCELED) {
1499 $statusType = 'status6';
1500 }
1501
1502 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
1503 }
1504
1520 private function logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice = null, $multicurrency_unitBuyPrice = null, $multicurrency_tx = null, $fk_multicurrency = null, $multicurrency_code = null)
1521 {
1522 // Add record into log table
1523 $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log(";
1524 $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
1525 $sql .= "datec, fk_product_fournisseur,fk_user,price,quantity)";
1526 $sql .= "values(";
1527 $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
1528 $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
1529 $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape((string) $multicurrency_tx)."'" : '1').",";
1530 $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape((string) $fk_multicurrency)."'" : 'null').",";
1531 $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
1532 $sql .= "'".$this->db->idate($datec)."',";
1533 $sql .= " ".((int) $this->product_fourn_price_id).",";
1534 $sql .= " ".((int) $user->id).",";
1535 $sql .= " ".price2num($buyprice).",";
1536 $sql .= " ".price2num($qty);
1537 $sql .= ")";
1538
1539 $resql = $this->db->query($sql);
1540 if (!$resql) {
1541 return -1;
1542 } else {
1543 return 1;
1544 }
1545 }
1546}
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
$object ref
Definition info.php:90
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
show_photos($modulepart, $sdir, $size=0, $nbmax=0, $nbbyrow=5, $showfilename=0, $showaction=0, $maxHeight=120, $maxWidth=160, $nolink=0, $overwritetitle=0, $usesharelink=0, $cache='', $addphotorefcss='photoref')
Show photos of an object (nbmax maximum), into several columns.
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr database access.
Class to manage suppliers.
static getIdFromCode($dbs, $code)
Get id of currency from code.
Class to parse product price expressions.
Class to manage predefined suppliers products.
displayPriceProductFournisseurLog($productFournLogList=array())
Display log price of product supplier price.
listProductFournisseurPriceLog($product_fourn_price_id, $sortfield='', $sortorder='', $limit=0, $offset=0)
List supplier prices log of a supplier price.
getSocNomUrl($withpicto=0, $option='supplier', $maxlen=0, $notooltip=0)
Display supplier of product.
LibStatut($status, $mode=0, $type=0)
Return the status.
logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice=null, $multicurrency_unitBuyPrice=null, $multicurrency_tx=null, $fk_multicurrency=null, $multicurrency_code=null)
Private function to log price history.
setSupplierPriceExpression($expression_id)
Sets the supplier price expression.
find_min_price_product_fournisseur($prodid, $qty=0, $socid=0)
Load properties for minimum price.
list_product_fournisseur_price($prodid, $sortfield='', $sortorder='', $limit=0, $offset=0, $socid=0)
List all supplier prices of a product.
fetch_product_fournisseur_price($rowid, $ignore_expression=0)
Loads the price information of a provider.
remove_fournisseur($id_fourn)
Remove all prices for this couple supplier-product.
getNomUrl($withpicto=0, $option='', $maxlength=0, $save_lastsearch_value=-1, $notooltip=0, $morecss='', $add_label=0, $sep=' - ')
Return a link to the object card (with 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
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.
dol_now($mode='auto')
Return date for now.
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.
get_localtax($vatrate, $local, $thirdparty_buyer=null, $thirdparty_seller=null, $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate,...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
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
Definition repair.php:158