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