dolibarr 21.0.0-alpha
fournisseur.product.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2009-2014 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
6 * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
7 * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
8 * Copyright (C) 2016-2023 Charlene Benke <charlene@patas-monkey.com>
9 * Copyright (C) 2019-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
142 public $fourn_charges; // when getDolGlobalString('PRODUCT_CHARGES') is set
143
147 public $product_fourn_id;
148
149 public $product_fourn_entity;
150
154 public $user_id;
155
159 public $fk_availability;
160
161 public $fourn_unitprice;
162 public $fourn_unitprice_with_discount; // not saved into database
163 public $fourn_tva_tx;
164 public $fourn_tva_npr;
165
169 public $fk_supplier_price_expression;
170
174 public $supplier_reputation;
175
179 public $reputations = array();
180
181 // Multicurreny
182
186 public $fourn_multicurrency_id;
187
188 public $fourn_multicurrency_code;
189 public $fourn_multicurrency_tx;
190 public $fourn_multicurrency_price;
191 public $fourn_multicurrency_unitprice;
192
198
202 public $supplier_barcode;
203
209
213 public $supplier_fk_barcode_type;
214
215 public $packaging;
216
217 public $labelStatusShort;
218 public $labelStatus;
219
220 const STATUS_OPEN = 1;
221 const STATUS_CANCELED = 0;
222
223
229 public function __construct($db)
230 {
231 global $langs;
232
233 $this->db = $db;
234 $langs->load("suppliers");
235 $this->reputations = array('-1' => '', 'FAVORITE' => $langs->trans('Favorite'), 'NOTTHGOOD' => $langs->trans('NotTheGoodQualitySupplier'), 'DONOTORDER' => $langs->trans('DoNotOrderThisProductToThisSupplier'));
236 }
237
238 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
245 public function remove_fournisseur($id_fourn)
246 {
247 // phpcs:enable
248 $ok = 1;
249
250 $this->db->begin();
251
252 $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
253 $sql .= " WHERE fk_product = ".((int) $this->id)." AND fk_soc = ".((int) $id_fourn);
254
255 dol_syslog(get_class($this)."::remove_fournisseur", LOG_DEBUG);
256 $resql2 = $this->db->query($sql);
257 if (!$resql2) {
258 $this->error = $this->db->lasterror();
259 $ok = 0;
260 }
261
262 if ($ok) {
263 $this->db->commit();
264 return 1;
265 } else {
266 $this->db->rollback();
267 return -1;
268 }
269 }
270
271
272 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
279 public function remove_product_fournisseur_price($rowid)
280 {
281 // phpcs:enable
282 global $conf, $user;
283
284 $error = 0;
285
286 $this->db->begin();
287
288 // Call trigger
289 $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_DELETE', $user);
290 if ($result < 0) {
291 $error++;
292 }
293 // End call triggers
294
295 if (empty($error)) {
296 $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
297 $sql .= " WHERE rowid = ".((int) $rowid);
298
299 dol_syslog(get_class($this)."::remove_product_fournisseur_price", LOG_DEBUG);
300 $resql = $this->db->query($sql);
301 if (!$resql) {
302 $this->error = $this->db->lasterror();
303 $error++;
304 }
305 }
306
307 if (empty($error)) {
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
347 public function update_buyprice(
348 $qty,
349 $buyprice,
350 $user,
351 $price_base_type,
352 $fourn,
353 $availability,
354 $ref_fourn,
355 $tva_tx,
356 $charges = 0,
357 $remise_percent = 0,
358 $remise = 0,
359 $newnpr = 0,
360 $delivery_time_days = 0,
361 $supplier_reputation = '',
362 $localtaxes_array = array(),
363 $newdefaultvatcode = '',
364 $multicurrency_buyprice = 0,
365 $multicurrency_price_base_type = 'HT',
366 $multicurrency_tx = 1,
367 $multicurrency_code = '',
368 $desc_fourn = '',
369 $barcode = '',
370 $fk_barcode_type = 0,
371 $options = array()
372 ) {
373 // phpcs:enable
374 global $conf, $langs;
375 //global $mysoc;
376
377 // Clean parameter
378 if (empty($qty)) {
379 $qty = 0;
380 }
381 if (empty($buyprice)) {
382 $buyprice = 0;
383 }
384 if (empty($charges)) {
385 $charges = 0;
386 }
387 if (empty($availability)) {
388 $availability = 0;
389 }
390 if (empty($remise_percent)) {
391 $remise_percent = 0;
392 }
393 if (empty($supplier_reputation) || $supplier_reputation == -1) {
394 $supplier_reputation = '';
395 }
396 if ($delivery_time_days != '' && !is_numeric($delivery_time_days)) {
397 $delivery_time_days = 0;
398 }
399 if ($price_base_type == 'TTC') {
400 $ttx = $tva_tx;
401 $buyprice /= (1 + ($ttx / 100));
402 }
403
404 // Multicurrency
405 $multicurrency_unitBuyPrice = null;
406 $fk_multicurrency = null;
407 if (isModEnabled("multicurrency")) {
408 if (empty($multicurrency_tx)) {
409 $multicurrency_tx = 1;
410 }
411 if (empty($multicurrency_buyprice)) {
412 $multicurrency_buyprice = 0;
413 }
414 if ($multicurrency_price_base_type == 'TTC') {
415 $ttx = $tva_tx;
416 $multicurrency_buyprice /= (1 + ($ttx / 100));
417 }
418 $multicurrency_buyprice = price2num($multicurrency_buyprice, 'MU');
419 $multicurrency_unitBuyPrice = price2num((float) $multicurrency_buyprice / $qty, 'MU');
420
421 $buyprice = (float) $multicurrency_buyprice / $multicurrency_tx;
422 $fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $multicurrency_code);
423 }
424
425 $buyprice = (float) price2num($buyprice, 'MU');
426 $charges = (float) price2num($charges, 'MU');
427 $qty = (float) price2num($qty, 'MS');
428 $unitBuyPrice = (float) price2num($buyprice / $qty, 'MU');
429
430 // We can have a purchase ref that need to buy 100 min for a given price and with a packaging of 50.
431 //$packaging = price2num(((empty($this->packaging) || $this->packaging < $qty) ? $qty : $this->packaging), 'MS');
432 $packaging = price2num((empty($this->packaging) ? $qty : $this->packaging), 'MS');
433
434 $error = 0;
435 $now = dol_now();
436
437 $newvat = $tva_tx;
438
439 if (count($localtaxes_array) > 0) {
440 $localtaxtype1 = $localtaxes_array['0'];
441 $localtax1 = $localtaxes_array['1'];
442 $localtaxtype2 = $localtaxes_array['2'];
443 $localtax2 = $localtaxes_array['3'];
444 } else { // old method. deprecated because it can't retrieve type
445 $localtaxtype1 = '0';
446 $localtax1 = get_localtax($newvat, 1);
447 $localtaxtype2 = '0';
448 $localtax2 = get_localtax($newvat, 2);
449 }
450 if (empty($localtax1)) {
451 $localtax1 = 0; // If = '' then = 0
452 }
453 if (empty($localtax2)) {
454 $localtax2 = 0; // If = '' then = 0
455 }
456
457 $this->db->begin();
458
459 if ($this->product_fourn_price_id > 0) {
460 // check if price already logged, if not first log current price
461 $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id);
462 if (is_array($logPrices) && count($logPrices) == 0) {
463 $currentPfp = new self($this->db);
464 $result = $currentPfp->fetch_product_fournisseur_price($this->product_fourn_price_id);
465 if ($result > 0 && $currentPfp->fourn_price != 0) {
466 $currentPfpUser = new User($this->db);
467 $result = $currentPfpUser->fetch($currentPfp->user_id);
468 if ($result > 0) {
469 $currentPfp->logPrice(
470 $currentPfpUser,
471 $currentPfp->date_creation,
472 $currentPfp->fourn_price,
473 $currentPfp->fourn_qty,
474 $currentPfp->fourn_multicurrency_price,
475 $currentPfp->fourn_multicurrency_unitprice,
476 $currentPfp->fourn_multicurrency_tx,
477 $currentPfp->fourn_multicurrency_id,
478 $currentPfp->fourn_multicurrency_code
479 );
480 }
481 }
482 }
483 $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
484 $sql .= " SET fk_user = ".((int) $user->id)." ,";
485 $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.
486 $sql .= " ref_fourn = '".$this->db->escape($ref_fourn)."',";
487 $sql .= " desc_fourn = '".$this->db->escape($desc_fourn)."',";
488 $sql .= " price = ".((float) $buyprice).",";
489 $sql .= " quantity = ".((float) $qty).",";
490 $sql .= " remise_percent = ".((float) $remise_percent).",";
491 $sql .= " remise = ".((float) $remise).",";
492 $sql .= " unitprice = ".((float) $unitBuyPrice).",";
493 $sql .= " fk_availability = ".((int) $availability).",";
494 $sql .= " multicurrency_price = ".(isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
495 $sql .= " multicurrency_unitprice = ".(isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
496 $sql .= " multicurrency_tx = ".(isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
497 $sql .= " fk_multicurrency = ".(isset($fk_multicurrency) ? (int) $fk_multicurrency : 'null').",";
498 $sql .= " multicurrency_code = ".(isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
499 $sql .= " entity = ".$conf->entity.",";
500 $sql .= " tva_tx = ".price2num($tva_tx).",";
501 // TODO Add localtax1 and localtax2
502 //$sql.= " localtax1_tx=".($localtax1>=0?$localtax1:'NULL').",";
503 //$sql.= " localtax2_tx=".($localtax2>=0?$localtax2:'NULL').",";
504 //$sql.= " localtax1_type=".($localtaxtype1!=''?"'".$this->db->escape($localtaxtype1)."'":"'0'").",";
505 //$sql.= " localtax2_type=".($localtaxtype2!=''?"'".$this->db->escape($localtaxtype2)."'":"'0'").",";
506 $sql .= " default_vat_code=".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
507 $sql .= " info_bits = ".((int) $newnpr).",";
508 $sql .= " charges = ".((float) $charges).","; // deprecated
509 $sql .= " delivery_time_days = ".($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
510 $sql .= " supplier_reputation = ".(empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
511 $sql .= " barcode = ".(empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
512 $sql .= " fk_barcode_type = ".(empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape($fk_barcode_type)."'");
513 if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
514 $sql .= ", packaging = ".(empty($packaging) ? 1 : $packaging);
515 }
516 $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
517
518 if (!$error) {
519 if (!empty($options) && is_array($options)) {
520 $productfournisseurprice = new ProductFournisseurPrice($this->db);
521 $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
522 if ($res > 0) {
523 foreach ($options as $key => $value) {
524 $productfournisseurprice->array_options[$key] = $value;
525 }
526 $res = $productfournisseurprice->update($user);
527 if ($res < 0) {
528 $this->error = $productfournisseurprice->error;
529 $this->errors = $productfournisseurprice->errors;
530 $error++;
531 }
532 }
533 }
534 }
535
536 // TODO Add price_base_type and price_ttc
537
538 dol_syslog(get_class($this).'::update_buyprice update knowing id of line = product_fourn_price_id = '.$this->product_fourn_price_id, LOG_DEBUG);
539 $resql = $this->db->query($sql);
540 if ($resql) {
541 // Call trigger
542 $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_MODIFY', $user);
543 if ($result < 0) {
544 $error++;
545 }
546 // End call triggers
547 if (!$error && !getDolGlobalString('PRODUCT_PRICE_SUPPLIER_NO_LOG')) {
548 $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
549 if ($result < 0) {
550 $error++;
551 }
552 }
553 if (empty($error)) {
554 $this->db->commit();
555 return $this->product_fourn_price_id;
556 } else {
557 $this->db->rollback();
558 return -1;
559 }
560 } else {
561 $this->error = $this->db->error()." sql=".$sql;
562 $this->db->rollback();
563 return -2;
564 }
565 } else {
566 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);
567
568 // Delete price for this quantity
569 $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
570 $sql .= " WHERE fk_soc = ".((int) $fourn->id)." AND ref_fourn = '".$this->db->escape($ref_fourn)."' AND quantity = ".((float) $qty)." AND entity = ".((int) $conf->entity);
571 $resql = $this->db->query($sql);
572 if ($resql) {
573 // Add price for this quantity to supplier
574 $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price(";
575 $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
576 $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";
577 if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
578 $sql .= ", packaging";
579 }
580 $sql .= ") values(";
581 $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
582 $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
583 $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
584 $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
585 $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
586 $sql .= " '".$this->db->idate($now)."',";
587 $sql .= " ".((int) $this->id).",";
588 $sql .= " ".((int) $fourn->id).",";
589 $sql .= " '".$this->db->escape($ref_fourn)."',";
590 $sql .= " '".$this->db->escape($desc_fourn)."',";
591 $sql .= " ".((int) $user->id).",";
592 $sql .= " ".price2num($buyprice).",";
593 $sql .= " ".((float) $qty).",";
594 $sql .= " ".((float) $remise_percent).",";
595 $sql .= " ".((float) $remise).",";
596 $sql .= " ".price2num($unitBuyPrice).",";
597 $sql .= " ".price2num($tva_tx).",";
598 $sql .= " ".price2num($charges).",";
599 $sql .= " ".((int) $availability).",";
600 $sql .= " ".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
601 $sql .= " ".((int) $newnpr).",";
602 $sql .= $conf->entity.",";
603 $sql .= ($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
604 $sql .= (empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
605 $sql .= (empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
606 $sql .= (empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape($fk_barcode_type)."'");
607 if (getDolGlobalString('PRODUCT_USE_SUPPLIER_PACKAGING')) {
608 $sql .= ", ".(empty($this->packaging) ? '1' : "'".$this->db->escape($this->packaging)."'");
609 }
610 $sql .= ")";
611
612 $this->product_fourn_price_id = 0;
613
614 $resql = $this->db->query($sql);
615 if ($resql) {
616 $this->product_fourn_price_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur_price");
617 } else {
618 $this->error = $this->db->lasterror();
619 $error++;
620 }
621
622 if (!$error) {
623 if (!empty($options) && is_array($options)) {
624 $productfournisseurprice = new ProductFournisseurPrice($this->db);
625 $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
626 if ($res > 0) {
627 foreach ($options as $key => $value) {
628 $productfournisseurprice->array_options[$key] = $value;
629 }
630 $res = $productfournisseurprice->update($user);
631 if ($res < 0) {
632 $this->error = $productfournisseurprice->error;
633 $this->errors = $productfournisseurprice->errors;
634 $error++;
635 }
636 }
637 }
638 }
639
640 if (!$error && !getDolGlobalString('PRODUCT_PRICE_SUPPLIER_NO_LOG')) {
641 // Add record into log table
642 // $this->product_fourn_price_id must be set
643 $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
644 if ($result < 0) {
645 $error++;
646 }
647 }
648
649 if (!$error) {
650 // Call trigger
651 $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_CREATE', $user);
652 if ($result < 0) {
653 $error++;
654 }
655 // End call triggers
656
657 if (empty($error)) {
658 $this->db->commit();
659 return $this->product_fourn_price_id;
660 } else {
661 $this->db->rollback();
662 return -1;
663 }
664 } else {
665 $this->error = $this->db->lasterror()." sql=".$sql;
666 $this->db->rollback();
667 return -2;
668 }
669 } else {
670 $this->error = $this->db->lasterror()." sql=".$sql;
671 $this->db->rollback();
672 return -1;
673 }
674 }
675 }
676
677 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
685 public function fetch_product_fournisseur_price($rowid, $ignore_expression = 0)
686 {
687 // phpcs:enable
688 global $conf;
689
690 $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,";
691 $sql .= " pfp.fk_soc, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_product, pfp.charges, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
692 $sql .= " pfp.supplier_reputation, pfp.fk_user, pfp.datec,";
693 $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
694 $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging,";
695 $sql .= " p.ref as product_ref, p.tosell as status, p.tobuy as status_buy";
696 $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p";
697 $sql .= " WHERE pfp.rowid = ".(int) $rowid;
698 $sql .= " AND pfp.fk_product = p.rowid";
699
700 dol_syslog(get_class($this)."::fetch_product_fournisseur_price", LOG_DEBUG);
701 $resql = $this->db->query($sql);
702 if ($resql) {
703 $obj = $this->db->fetch_object($resql);
704 if ($obj) {
705 $this->product_fourn_price_id = $rowid;
706 $this->id = $obj->fk_product;
707
708 $this->fk_product = $obj->fk_product;
709 $this->product_id = $obj->fk_product;
710 $this->product_ref = $obj->product_ref;
711 $this->status = $obj->status;
712 $this->status_buy = $obj->status_buy;
713 $this->fourn_id = $obj->fk_soc;
714 $this->fourn_ref = $obj->ref_fourn; // deprecated
715 $this->ref_supplier = $obj->ref_fourn;
716 $this->desc_supplier = $obj->desc_fourn;
717 $this->fourn_price = $obj->price;
718 $this->fourn_charges = $obj->charges; // when getDolGlobalString('PRODUCT_CHARGES') is set
719 $this->fourn_qty = $obj->quantity;
720 $this->fourn_remise_percent = $obj->remise_percent;
721 $this->fourn_remise = $obj->remise;
722 $this->fourn_unitprice = $obj->unitprice;
723 $this->fourn_tva_tx = $obj->tva_tx;
724 $this->fourn_tva_npr = $obj->fourn_tva_npr;
725 // Add also localtaxes
726 $this->fk_availability = $obj->fk_availability;
727 $this->delivery_time_days = $obj->delivery_time_days;
728 $this->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
729 $this->supplier_reputation = $obj->supplier_reputation;
730 $this->default_vat_code = $obj->default_vat_code;
731 $this->user_id = $obj->fk_user;
732 $this->date_creation = $this->db->jdate($obj->datec);
733 $this->fourn_multicurrency_price = $obj->multicurrency_price;
734 $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
735 $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
736 $this->fourn_multicurrency_id = $obj->fk_multicurrency;
737 $this->fourn_multicurrency_code = $obj->multicurrency_code;
738 if (isModEnabled('barcode')) {
739 $this->fourn_barcode = $obj->barcode; // deprecated
740 $this->fourn_fk_barcode_type = $obj->fk_barcode_type; // deprecated
741 $this->supplier_barcode = $obj->barcode;
742 $this->supplier_fk_barcode_type = $obj->fk_barcode_type;
743 }
744 $this->packaging = $obj->packaging;
745
746 if (isModEnabled('dynamicprices') && empty($ignore_expression) && !empty($this->fk_supplier_price_expression)) {
747 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
748 $priceparser = new PriceParser($this->db);
749 $price_result = $priceparser->parseProductSupplier($this);
750 if ($price_result >= 0) {
751 $this->fourn_price = $price_result;
752 //recalculation of unitprice, as probably the price changed...
753 if ($this->fourn_qty != 0) {
754 $this->fourn_unitprice = price2num($this->fourn_price / $this->fourn_qty, 'MU');
755 } else {
756 $this->fourn_unitprice = "";
757 }
758 }
759 }
760
761 return 1;
762 } else {
763 return 0;
764 }
765 } else {
766 $this->error = $this->db->lasterror();
767 return -1;
768 }
769 }
770
771
772 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
785 public function list_product_fournisseur_price($prodid, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0, $socid = 0)
786 {
787 // phpcs:enable
788 global $conf;
789
790 $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, ";
791 $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,";
792 $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,";
793 $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code, pfp.datec, pfp.tms,";
794 $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging, pfp.status as pfstatus";
795 $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."societe as s";
796 $sql .= " WHERE pfp.entity IN (".getEntity('productsupplierprice').")";
797 $sql .= " AND pfp.fk_soc = s.rowid AND pfp.fk_product = p.rowid";
798 $sql .= ($socid > 0 ? ' AND pfp.fk_soc = '.((int) $socid) : '');
799 $sql .= " AND s.status = 1"; // only enabled company selected
800 $sql .= " AND pfp.fk_product = ".((int) $prodid);
801 if (empty($sortfield)) {
802 $sql .= " ORDER BY s.nom, pfp.quantity, pfp.price";
803 } else {
804 $sql .= $this->db->order($sortfield, $sortorder);
805 }
806 $sql .= $this->db->plimit($limit, $offset);
807 dol_syslog(get_class($this)."::list_product_fournisseur_price", LOG_DEBUG);
808
809 $resql = $this->db->query($sql);
810 if ($resql) {
811 $retarray = array();
812
813 while ($record = $this->db->fetch_array($resql)) {
814 //define base attribute
815 $prodfourn = new ProductFournisseur($this->db);
816
817 $prodfourn->product_ref = $record["product_ref"];
818 $prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"];
819 $prodfourn->status = $record["status"];
820 $prodfourn->status_buy = $record["status_buy"];
821 $prodfourn->product_fourn_id = $record["product_fourn_id"];
822 $prodfourn->product_fourn_entity = $record["entity"];
823 $prodfourn->ref_supplier = $record["ref_fourn"];
824 $prodfourn->fourn_ref = $record["ref_fourn"];
825 $prodfourn->desc_supplier = $record["desc_fourn"];
826 $prodfourn->fourn_price = $record["price"];
827 $prodfourn->fourn_qty = $record["quantity"];
828 $prodfourn->fourn_remise_percent = $record["remise_percent"];
829 $prodfourn->fourn_remise = $record["remise"];
830 $prodfourn->fourn_unitprice = $record["unitprice"];
831 $prodfourn->fourn_charges = $record["charges"]; // when getDolGlobalString('PRODUCT_CHARGES') is set
832 $prodfourn->fourn_tva_tx = $record["tva_tx"];
833 $prodfourn->fourn_id = $record["fourn_id"];
834 $prodfourn->fourn_name = $record["supplier_name"];
835 $prodfourn->fk_availability = $record["fk_availability"];
836 $prodfourn->delivery_time_days = $record["delivery_time_days"];
837 $prodfourn->id = $prodid;
838 $prodfourn->fourn_tva_npr = $record["info_bits"];
839 $prodfourn->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
840 $prodfourn->supplier_reputation = $record["supplier_reputation"];
841 $prodfourn->fourn_date_creation = $this->db->jdate($record['datec']);
842 $prodfourn->fourn_date_modification = $this->db->jdate($record['tms']);
843
844 $prodfourn->fourn_multicurrency_price = $record["multicurrency_price"];
845 $prodfourn->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
846 $prodfourn->fourn_multicurrency_tx = $record["multicurrency_tx"];
847 $prodfourn->fourn_multicurrency_id = $record["fk_multicurrency"];
848 $prodfourn->fourn_multicurrency_code = $record["multicurrency_code"];
849
850 $prodfourn->packaging = $record["packaging"];
851 $prodfourn->status = $record["pfstatus"];
852
853 if (isModEnabled('barcode')) {
854 $prodfourn->supplier_barcode = $record["barcode"];
855 $prodfourn->supplier_fk_barcode_type = $record["fk_barcode_type"];
856 }
857
858 if (isModEnabled('dynamicprices') && !empty($prodfourn->fk_supplier_price_expression)) {
859 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
860 $priceparser = new PriceParser($this->db);
861 $price_result = $priceparser->parseProductSupplier($prodfourn);
862 if ($price_result >= 0) {
863 $prodfourn->fourn_price = $price_result;
864 $prodfourn->fourn_unitprice = null; //force recalculation of unitprice, as probably the price changed...
865 }
866 }
867
868 if (!isset($prodfourn->fourn_unitprice)) {
869 if ($prodfourn->fourn_qty != 0) {
870 $prodfourn->fourn_unitprice = price2num($prodfourn->fourn_price / $prodfourn->fourn_qty, 'MU');
871 } else {
872 $prodfourn->fourn_unitprice = "";
873 }
874 }
875
876 $retarray[] = $prodfourn;
877 }
878
879 $this->db->free($resql);
880 return $retarray;
881 } else {
882 $this->error = $this->db->error();
883 return -1;
884 }
885 }
886
887 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
897 public function find_min_price_product_fournisseur($prodid, $qty = 0, $socid = 0)
898 {
899 // phpcs:enable
900 if (empty($prodid)) {
901 dol_syslog("Warning function find_min_price_product_fournisseur were called with prodid empty. May be a bug.", LOG_WARNING);
902 return 0;
903 }
904
905 $this->product_fourn_price_id = 0;
906 $this->product_fourn_id = 0;
907 $this->fourn_ref = '';
908 $this->fourn_price = 0;
909 $this->fourn_qty = 0;
910 $this->fourn_remise_percent = 0;
911 $this->fourn_remise = 0;
912 $this->fourn_unitprice = 0;
913 $this->fourn_id = 0;
914 $this->fourn_name = '';
915 $this->delivery_time_days = 0;
916 $this->id = 0;
917
918 $this->fourn_multicurrency_price = 0;
919 $this->fourn_multicurrency_unitprice = 0;
920 $this->fourn_multicurrency_tx = 0;
921 $this->fourn_multicurrency_id = 0;
922 $this->fourn_multicurrency_code = '';
923
924 $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
925 $sql .= " pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
926 $sql .= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges,";
927 $sql .= " pfp.remise, pfp.remise_percent, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
928 $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code";
929 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
930 $sql .= " WHERE s.entity IN (".getEntity('societe').")";
931 $sql .= " AND pfp.entity IN (".getEntity('productsupplierprice').")";
932 $sql .= " AND pfp.fk_product = ".((int) $prodid);
933 $sql .= " AND pfp.fk_soc = s.rowid";
934 $sql .= " AND s.status = 1"; // only enabled society
935 if ($qty > 0) {
936 $sql .= " AND pfp.quantity <= ".((float) $qty);
937 }
938 if ($socid > 0) {
939 $sql .= ' AND pfp.fk_soc = '.((int) $socid);
940 }
941
942 dol_syslog(get_class($this)."::find_min_price_product_fournisseur", LOG_DEBUG);
943
944 $resql = $this->db->query($sql);
945 if ($resql) {
946 $record_array = array();
947
948 //Store each record to array for later search of min
949 while ($record = $this->db->fetch_array($resql)) {
950 $record_array[] = $record;
951 }
952
953 if (count($record_array) == 0) {
954 $this->db->free($resql);
955 return 0;
956 } else {
957 $min = -1;
958 foreach ($record_array as $record) {
959 $fourn_price = $record["price"];
960 // calculate unit price for quantity 1
961 $fourn_unitprice = $record["unitprice"];
962 $fourn_unitprice_with_discount = $record["unitprice"] * (1 - $record["remise_percent"] / 100);
963
964 if (isModEnabled('dynamicprices') && !empty($record["fk_supplier_price_expression"])) {
965 $prod_supplier = new ProductFournisseur($this->db);
966 $prod_supplier->product_fourn_price_id = $record["product_fourn_price_id"];
967 $prod_supplier->id = $prodid;
968 $prod_supplier->fourn_qty = $record["quantity"];
969 $prod_supplier->fourn_tva_tx = $record["tva_tx"];
970 $prod_supplier->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
971
972 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
973 $priceparser = new PriceParser($this->db);
974 $price_result = $priceparser->parseProductSupplier($prod_supplier);
975 if ($price_result >= 0) {
976 $fourn_price = price2num($price_result, 'MU');
977 if ($record["quantity"] != 0) {
978 $fourn_unitprice = price2num((float) $fourn_price / $record["quantity"], 'MU');
979 } else {
980 $fourn_unitprice = $fourn_price;
981 }
982 $fourn_unitprice_with_discount = (float) $fourn_unitprice * (1 - $record["remise_percent"] / 100);
983 }
984 }
985
986 if ($fourn_unitprice < $min || $min == -1) {
987 $this->id = $prodid;
988 $this->product_fourn_price_id = $record["product_fourn_price_id"];
989 $this->ref_supplier = $record["ref_fourn"];
990 $this->ref_fourn = $record["ref_fourn"]; // deprecated
991 $this->fourn_ref = $record["ref_fourn"]; // deprecated
992 $this->fourn_price = $fourn_price;
993 $this->fourn_qty = $record["quantity"];
994 $this->fourn_remise_percent = $record["remise_percent"];
995 $this->fourn_remise = $record["remise"];
996 $this->fourn_unitprice = $fourn_unitprice;
997 $this->fourn_unitprice_with_discount = $fourn_unitprice_with_discount;
998 $this->fourn_charges = $record["charges"]; // when getDolGlobalString('PRODUCT_CHARGES') is set
999 $this->fourn_tva_tx = $record["tva_tx"];
1000 $this->fourn_id = $record["fourn_id"]; // thirdparty id
1001 $this->fourn_name = $record["supplier_name"];
1002 $this->delivery_time_days = $record["delivery_time_days"];
1003 $this->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
1004 $this->fourn_multicurrency_price = $record["multicurrency_price"];
1005 $this->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
1006 $this->fourn_multicurrency_tx = $record["multicurrency_tx"];
1007 $this->fourn_multicurrency_id = $record["fk_multicurrency"];
1008 $this->fourn_multicurrency_code = $record["multicurrency_code"];
1009
1010 $min = $fourn_unitprice;
1011 }
1012 }
1013 }
1014
1015 $this->db->free($resql);
1016
1017 return $this->product_fourn_price_id;
1018 } else {
1019 $this->error = $this->db->error();
1020 return -1;
1021 }
1022 }
1023
1030 public function setSupplierPriceExpression($expression_id)
1031 {
1032 global $conf;
1033
1034 // Clean parameters
1035 $this->db->begin();
1036 $expression_id = $expression_id != 0 ? $expression_id : 'NULL';
1037
1038 $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
1039 $sql .= " SET fk_supplier_price_expression = ".((int) $expression_id);
1040 $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
1041
1042 dol_syslog(get_class($this)."::setSupplierPriceExpression", LOG_DEBUG);
1043
1044 $resql = $this->db->query($sql);
1045 if ($resql) {
1046 $this->db->commit();
1047 return 1;
1048 } else {
1049 $this->error = $this->db->error()." sql=".$sql;
1050 $this->db->rollback();
1051 return -1;
1052 }
1053 }
1054
1065 public function getSocNomUrl($withpicto = 0, $option = 'supplier', $maxlen = 0, $notooltip = 0)
1066 {
1067 $thirdparty = new Fournisseur($this->db);
1068 $thirdparty->fetch($this->fourn_id);
1069
1070 return $thirdparty->getNomUrl($withpicto, $option, $maxlen, $notooltip);
1071 }
1072
1073 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1085 public function display_price_product_fournisseur($showunitprice = 1, $showsuptitle = 1, $maxlen = 0, $notooltip = 0, $productFournList = array())
1086 {
1087 // phpcs:enable
1088 global $conf, $langs;
1089
1090 $out = '';
1091 $langs->load("suppliers");
1092 if (count($productFournList) > 0) {
1093 $out .= '<table class="nobordernopadding" width="100%">';
1094 $out .= '<tr><td class="liste_titre right">'.($showunitprice ? $langs->trans("Price").' '.$langs->trans("HT") : '').'</td>';
1095 $out .= '<td class="liste_titre right">'.($showunitprice ? $langs->trans("QtyMin") : '').'</td>';
1096 $out .= '<td class="liste_titre">'.$langs->trans("Supplier").'</td>';
1097 $out .= '<td class="liste_titre">'.$langs->trans("SupplierRef").'</td></tr>';
1098 foreach ($productFournList as $productFourn) {
1099 $out .= '<tr><td class="right">'.($showunitprice ? price($productFourn->fourn_unitprice * (1 - $productFourn->fourn_remise_percent / 100) - $productFourn->fourn_remise) : '').'</td>';
1100 $out .= '<td class="right">'.($showunitprice ? $productFourn->fourn_qty : '').'</td>';
1101 $out .= '<td>'.$productFourn->getSocNomUrl(1, 'supplier', $maxlen, $notooltip).'</td>';
1102 $out .= '<td>'.$productFourn->fourn_ref.'<td></tr>';
1103 }
1104 $out .= '</table>';
1105 } else {
1106 $out = ($showunitprice ? price($this->fourn_unitprice * (1 - $this->fourn_remise_percent / 100) + $this->fourn_remise, 0, $langs, 1, -1, -1, $conf->currency).' '.$langs->trans("HT").' &nbsp; <span class="opacitymedium">(</span>' : '');
1107 $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;
1108 $out .= ($showunitprice ? '<span class="opacitymedium">)</span>' : '');
1109 }
1110 return $out;
1111 }
1112
1121 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1122 {
1123 $tables = array(
1124 'product_fournisseur_price'
1125 );
1126
1127 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1128 }
1129
1138 public static function replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
1139 {
1140 $tables = array(
1141 'product_fournisseur_price'
1142 );
1143
1144 return CommonObject::commonReplaceProduct($dbs, $origin_id, $dest_id, $tables);
1145 }
1146
1157 public function listProductFournisseurPriceLog($product_fourn_price_id, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0)
1158 {
1159 $sql = "SELECT";
1160 $sql .= " u.lastname,";
1161 $sql .= " pfpl.rowid, pfp.ref_fourn as supplier_ref, pfpl.datec,";
1162 $sql .= " pfpl.price, pfpl.quantity,";
1163 $sql .= " pfpl.fk_multicurrency, pfpl.multicurrency_code, pfpl.multicurrency_tx, pfpl.multicurrency_price, pfpl.multicurrency_unitprice";
1164 $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price_log as pfpl,";
1165 $sql .= " ".MAIN_DB_PREFIX."product_fournisseur_price as pfp,";
1166 $sql .= " ".MAIN_DB_PREFIX."user as u";
1167 $sql .= " WHERE pfp.entity IN (".getEntity('productprice').")";
1168 $sql .= " AND pfpl.fk_user = u.rowid";
1169 $sql .= " AND pfp.rowid = pfpl.fk_product_fournisseur";
1170 $sql .= " AND pfpl.fk_product_fournisseur = ".((int) $product_fourn_price_id);
1171 if (empty($sortfield)) {
1172 $sql .= " ORDER BY pfpl.datec";
1173 } else {
1174 $sql .= $this->db->order($sortfield, $sortorder);
1175 }
1176 $sql .= $this->db->plimit($limit, $offset);
1177 dol_syslog(get_class($this)."::list_product_fournisseur_price_log", LOG_DEBUG);
1178
1179 $resql = $this->db->query($sql);
1180 if ($resql) {
1181 $retarray = array();
1182
1183 while ($obj = $this->db->fetch_object($resql)) {
1184 $tmparray = array();
1185 $tmparray['rowid'] = $obj->rowid;
1186 $tmparray['supplier_ref'] = $obj->supplier_ref;
1187 $tmparray['datec'] = $this->db->jdate($obj->datec);
1188 $tmparray['lastname'] = $obj->lastname;
1189 $tmparray['price'] = $obj->price;
1190 $tmparray['quantity'] = $obj->quantity;
1191 $tmparray['fk_multicurrency'] = $obj->fk_multicurrency;
1192 $tmparray['multicurrency_code'] = $obj->multicurrency_code;
1193 $tmparray['multicurrency_tx'] = $obj->multicurrency_tx;
1194 $tmparray['multicurrency_price'] = $obj->multicurrency_price;
1195 $tmparray['multicurrency_unitprice'] = $obj->multicurrency_unitprice;
1196
1197 $retarray[] = $tmparray;
1198 }
1199
1200 $this->db->free($resql);
1201 return $retarray;
1202 } else {
1203 $this->error = $this->db->error();
1204 return -1;
1205 }
1206 }
1207
1215 public function displayPriceProductFournisseurLog($productFournLogList = array())
1216 {
1217 global $conf, $langs;
1218
1219 $out = '';
1220 $langs->load("suppliers");
1221 if (count($productFournLogList) > 0) {
1222 $out .= '<table class="noborder centpercent">';
1223 $out .= '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Date").'</td>';
1224 $out .= '<td class="liste_titre right">'.$langs->trans("Price").'</td>';
1225 //$out .= '<td class="liste_titre right">'.$langs->trans("QtyMin").'</td>';
1226 $out .= '<td class="liste_titre">'.$langs->trans("User").'</td></tr>';
1227 foreach ($productFournLogList as $productFournLog) {
1228 $out .= '<tr><td>'.dol_print_date($productFournLog['datec'], 'dayhour', 'tzuser').'</td>';
1229 $out .= '<td class="right">'.price($productFournLog['price'], 0, $langs, 1, -1, -1, $conf->currency);
1230 if ($productFournLog['multicurrency_code'] != $conf->currency) {
1231 $out .= ' ('.price($productFournLog['multicurrency_price'], 0, $langs, 1, -1, -1, $productFournLog['multicurrency_code']).')';
1232 }
1233 $out .= '</td>';
1234 //$out.= '<td class="right">'.$productFournLog['quantity'].'</td>';
1235 $out .= '<td>'.$productFournLog['lastname'].'</td></tr>';
1236 }
1237 $out .= '</table>';
1238 }
1239 return $out;
1240 }
1241
1242
1257 public function getNomUrl($withpicto = 0, $option = '', $maxlength = 0, $save_lastsearch_value = -1, $notooltip = 0, $morecss = '', $add_label = 0, $sep = ' - ')
1258 {
1259 global $db, $conf, $langs, $hookmanager;
1260
1261 if (!empty($conf->dol_no_mouse_hover)) {
1262 $notooltip = 1; // Force disable tooltips
1263 }
1264
1265 $result = '';
1266 $label = '';
1267
1268 $newref = $this->ref;
1269 if ($maxlength) {
1270 $newref = dol_trunc($newref, $maxlength, 'middle');
1271 }
1272
1273 if (!empty($this->entity)) {
1274 $tmpphoto = $this->show_photos('product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 80);
1275 if ($this->nbphoto > 0) {
1276 $label .= '<div class="photointooltip">';
1277 $label .= $tmpphoto;
1278 $label .= '</div><div style="clear: both;"></div>';
1279 }
1280 }
1281
1282 if ($this->type == Product::TYPE_PRODUCT) {
1283 $label .= img_picto('', 'product').' <u class="paddingrightonly">'.$langs->trans("Product").'</u>';
1284 } elseif ($this->type == Product::TYPE_SERVICE) {
1285 $label .= img_picto('', 'service').' <u class="paddingrightonly">'.$langs->trans("Service").'</u>';
1286 }
1287 if (isset($this->status) && isset($this->status_buy)) {
1288 $label .= ' '.$this->getLibStatut(5, 0);
1289 $label .= ' '.$this->getLibStatut(5, 1);
1290 }
1291
1292 if (!empty($this->ref)) {
1293 $label .= '<br><b>'.$langs->trans('ProductRef').':</b> '.($this->ref ? $this->ref : $this->product_ref);
1294 }
1295 if (!empty($this->label)) {
1296 $label .= '<br><b>'.$langs->trans('ProductLabel').':</b> '.$this->label;
1297 }
1298 $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
1299
1300 if ($this->type == Product::TYPE_PRODUCT || getDolGlobalString('STOCK_SUPPORTS_SERVICES')) {
1301 if (isModEnabled('productbatch')) {
1302 $langs->load("productbatch");
1303 $label .= "<br><b>".$langs->trans("ManageLotSerial").'</b>: '.$this->getLibStatut(0, 2);
1304 }
1305 }
1306 if (isModEnabled('barcode')) {
1307 $label .= '<br><b>'.$langs->trans('BarCode').':</b> '.$this->barcode;
1308 }
1309
1310 if ($this->type == Product::TYPE_PRODUCT) {
1311 if ($this->weight) {
1312 $label .= "<br><b>".$langs->trans("Weight").'</b>: '.$this->weight.' '.measuringUnitString(0, "weight", $this->weight_units);
1313 }
1314 $labelsize = "";
1315 if ($this->length) {
1316 $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Length").'</b>: '.$this->length.' '.measuringUnitString(0, 'size', $this->length_units);
1317 }
1318 if ($this->width) {
1319 $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Width").'</b>: '.$this->width.' '.measuringUnitString(0, 'size', $this->width_units);
1320 }
1321 if ($this->height) {
1322 $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Height").'</b>: '.$this->height.' '.measuringUnitString(0, 'size', $this->height_units);
1323 }
1324 if ($labelsize) {
1325 $label .= "<br>".$labelsize;
1326 }
1327
1328 $labelsurfacevolume = "";
1329 if ($this->surface) {
1330 $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Surface").'</b>: '.$this->surface.' '.measuringUnitString(0, 'surface', $this->surface_units);
1331 }
1332 if ($this->volume) {
1333 $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Volume").'</b>: '.$this->volume.' '.measuringUnitString(0, 'volume', $this->volume_units);
1334 }
1335 if ($labelsurfacevolume) {
1336 $label .= "<br>".$labelsurfacevolume;
1337 }
1338 }
1339
1340 if (isModEnabled('accounting') && $this->status) {
1341 include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1342 $label .= '<br><b>'.$langs->trans('ProductAccountancySellCode').':</b> '.length_accountg($this->accountancy_code_sell);
1343 $label .= '<br><b>'.$langs->trans('ProductAccountancySellIntraCode').':</b> '.length_accountg($this->accountancy_code_sell_intra);
1344 $label .= '<br><b>'.$langs->trans('ProductAccountancySellExportCode').':</b> '.length_accountg($this->accountancy_code_sell_export);
1345 }
1346 if (isModEnabled('accounting') && $this->status_buy) {
1347 include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
1348 $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyCode').':</b> '.length_accountg($this->accountancy_code_buy);
1349 $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyIntraCode').':</b> '.length_accountg($this->accountancy_code_buy_intra);
1350 $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyExportCode').':</b> '.length_accountg($this->accountancy_code_buy_export);
1351 }
1352
1353 $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id, 'pfpl.datec', 'DESC'); // set sort order here
1354 if (is_array($logPrices) && count($logPrices) > 0) {
1355 $label .= '<br><br>';
1356 $label .= '<u>'.$langs->trans("History").'</u>';
1357 $label .= $this->displayPriceProductFournisseurLog($logPrices);
1358 }
1359
1360 $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);
1361
1362 if ($option != 'nolink') {
1363 // Add param to save lastsearch_values or not
1364 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1365 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1366 $add_save_lastsearch_values = 1;
1367 }
1368 if ($add_save_lastsearch_values) {
1369 $url .= '&save_lastsearch_values=1';
1370 }
1371 }
1372
1373 $linkclose = '';
1374 if (empty($notooltip)) {
1375 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1376 $label = $langs->trans("SupplierRef");
1377 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1378 }
1379 $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
1380 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
1381 } else {
1382 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1383 }
1384
1385 $linkstart = '<a href="'.$url.'"';
1386 $linkstart .= $linkclose.'>';
1387 $linkend = '</a>';
1388
1389 $result .= $linkstart;
1390 if ($withpicto) {
1391 $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);
1392 }
1393 if ($withpicto != 2) {
1394 $result .= $newref.($this->ref_supplier ? ' ('.$this->ref_supplier.')' : '');
1395 }
1396 $result .= $linkend;
1397 if ($withpicto != 2) {
1398 $result .= (($add_label && $this->label) ? $sep.dol_trunc($this->label, ($add_label > 1 ? $add_label : 0)) : '');
1399 }
1400
1401 global $action;
1402 $hookmanager->initHooks(array($this->element . 'dao'));
1403 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1404 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1405 if ($reshook > 0) {
1406 $result = $hookmanager->resPrint;
1407 } else {
1408 $result .= $hookmanager->resPrint;
1409 }
1410 return $result;
1411 }
1412
1420 public function getLibStatut($mode = 0, $type = 0) // must be compatible with getLibStatut of inherited Product
1421 {
1422 return $this->LibStatut($this->status, $mode);
1423 }
1424
1425 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1434 public function LibStatut($status, $mode = 0, $type = 0)
1435 {
1436 // phpcs:enable
1437 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1438 global $langs;
1439 //$langs->load("mymodule@mymodule");
1440 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Enabled');
1441 $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
1442 $this->labelStatusShort[self::STATUS_OPEN] = $langs->transnoentitiesnoconv('Enabled');
1443 $this->labelStatusShort[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
1444 }
1445
1446 $statusType = 'status4';
1447 //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
1448 if ($status == self::STATUS_CANCELED) {
1449 $statusType = 'status6';
1450 }
1451
1452 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
1453 }
1454
1470 private function logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice = null, $multicurrency_unitBuyPrice = null, $multicurrency_tx = null, $fk_multicurrency = null, $multicurrency_code = null)
1471 {
1472 // Add record into log table
1473 $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log(";
1474 $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
1475 $sql .= "datec, fk_product_fournisseur,fk_user,price,quantity)";
1476 $sql .= "values(";
1477 $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
1478 $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
1479 $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
1480 $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
1481 $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
1482 $sql .= "'".$this->db->idate($datec)."',";
1483 $sql .= " ".((int) $this->product_fourn_price_id).",";
1484 $sql .= " ".$user->id.",";
1485 $sql .= " ".price2num($buyprice).",";
1486 $sql .= " ".price2num($qty);
1487 $sql .= ")";
1488
1489 $resql = $this->db->query($sql);
1490 if (!$resql) {
1491 return -1;
1492 } else {
1493 return 1;
1494 }
1495 }
1496}
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition security.php:640
$object ref
Definition info.php:79
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.
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'.
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.
measuringUnitString($unit, $measuring_style='', $scale='', $use_short_label=0, $outputlangs=null)
Return translation label of a unit key.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:137