32 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
33 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.class.php';
34 require_once DOL_DOCUMENT_ROOT.
'/product/class/productfournisseurprice.class.php';
52 public $product_fourn_price_id;
65 public $delivery_time_days;
67 public $desc_supplier;
68 public $vatrate_supplier;
78 public $fourn_remise_percent;
81 public $product_fourn_id;
82 public $product_fourn_entity;
92 public $fk_availability;
94 public $fourn_unitprice;
95 public $fourn_unitprice_with_discount;
97 public $fourn_tva_npr;
102 public $fk_supplier_price_expression;
104 public $supplier_reputation;
105 public $reputations = array();
108 public $fourn_multicurrency_id;
109 public $fourn_multicurrency_code;
110 public $fourn_multicurrency_tx;
111 public $fourn_multicurrency_price;
112 public $fourn_multicurrency_unitprice;
123 public $supplier_barcode;
134 public $supplier_fk_barcode_type;
149 $langs->load(
"suppliers");
150 $this->reputations = array(
'-1'=>
'',
'FAVORITE'=>$langs->trans(
'Favorite'),
'NOTTHGOOD'=>$langs->trans(
'NotTheGoodQualitySupplier'),
'DONOTORDER'=>$langs->trans(
'DoNotOrderThisProductToThisSupplier'));
167 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"product_fournisseur_price";
168 $sql .=
" WHERE fk_product = ".((int) $this->
id).
" AND fk_soc = ".((int) $id_fourn);
170 dol_syslog(get_class($this).
"::remove_fournisseur", LOG_DEBUG);
171 $resql2 = $this->
db->query($sql);
173 $this->error = $this->
db->lasterror();
181 $this->
db->rollback();
204 $result = $this->
call_trigger(
'SUPPLIER_PRODUCT_BUYPRICE_DELETE', $user);
211 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"product_fournisseur_price";
212 $sql .=
" WHERE rowid = ".((int) $rowid);
214 dol_syslog(get_class($this).
"::remove_product_fournisseur_price", LOG_DEBUG);
217 $this->error = $this->
db->lasterror();
226 $this->
db->rollback();
262 public function 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 =
'', $options = array())
265 global $conf, $langs;
272 if (empty($buyprice)) {
275 if (empty($charges)) {
278 if (empty($availability)) {
284 if (empty($supplier_reputation) || $supplier_reputation == -1) {
285 $supplier_reputation =
'';
287 if ($delivery_time_days !=
'' && !is_numeric($delivery_time_days)) {
288 $delivery_time_days =
'';
290 if ($price_base_type ==
'TTC') {
292 $buyprice = $buyprice / (1 + ($ttx / 100));
296 $multicurrency_unitBuyPrice =
null;
297 $fk_multicurrency =
null;
299 if (empty($multicurrency_tx)) {
300 $multicurrency_tx = 1;
302 if (empty($multicurrency_buyprice)) {
303 $multicurrency_buyprice = 0;
305 if ($multicurrency_price_base_type ==
'TTC') {
307 $multicurrency_buyprice = $multicurrency_buyprice / (1 + ($ttx / 100));
309 $multicurrency_buyprice =
price2num($multicurrency_buyprice,
'MU');
310 $multicurrency_unitBuyPrice =
price2num($multicurrency_buyprice / $qty,
'MU');
312 $buyprice = $multicurrency_buyprice / $multicurrency_tx;
319 $unitBuyPrice =
price2num($buyprice / $qty,
'MU');
323 $packaging =
price2num((empty($this->packaging) ? $qty : $this->packaging),
'MS');
330 if (count($localtaxes_array) > 0) {
331 $localtaxtype1 = $localtaxes_array[
'0'];
332 $localtax1 = $localtaxes_array[
'1'];
333 $localtaxtype2 = $localtaxes_array[
'2'];
334 $localtax2 = $localtaxes_array[
'3'];
336 $localtaxtype1 =
'0';
338 $localtaxtype2 =
'0';
341 if (empty($localtax1)) {
344 if (empty($localtax2)) {
349 if ($buyprice !=
'' && !is_numeric($buyprice)) {
354 if ($this->product_fourn_price_id > 0) {
357 if (is_array($logPrices) && count($logPrices) == 0) {
358 $currentPfp =
new self($this->db);
359 $result = $currentPfp->fetch_product_fournisseur_price($this->product_fourn_price_id);
360 if ($result > 0 && $currentPfp->fourn_price != 0) {
361 $currentPfpUser =
new User($this->
db);
362 $result = $currentPfpUser->fetch($currentPfp->user_id);
364 $currentPfp->logPrice(
366 $currentPfp->date_creation,
367 $currentPfp->fourn_price,
368 $currentPfp->fourn_qty,
369 $currentPfp->fourn_multicurrency_price,
370 $currentPfp->fourn_multicurrency_unitprice,
371 $currentPfp->fourn_multicurrency_tx,
372 $currentPfp->fourn_multicurrency_id,
373 $currentPfp->fourn_multicurrency_code
378 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"product_fournisseur_price";
379 $sql .=
" SET fk_user = ".((int) $user->id).
" ,";
380 $sql .=
" ref_fourn = '".$this->db->escape(
$ref_fourn).
"',";
381 $sql .=
" desc_fourn = '".$this->db->escape($desc_fourn).
"',";
382 $sql .=
" price = ".((float) $buyprice).
",";
383 $sql .=
" quantity = ".((float) $qty).
",";
385 $sql .=
" remise = ".((float) $remise).
",";
386 $sql .=
" unitprice = ".((float) $unitBuyPrice).
",";
387 $sql .=
" fk_availability = ".((int) $availability).
",";
388 $sql .=
" multicurrency_price = ".(isset($multicurrency_buyprice) ?
"'".$this->db->escape(
price2num($multicurrency_buyprice)).
"'" :
'null').
",";
389 $sql .=
" multicurrency_unitprice = ".(isset($multicurrency_unitBuyPrice) ?
"'".$this->db->escape(
price2num($multicurrency_unitBuyPrice)).
"'" :
'null').
",";
390 $sql .=
" multicurrency_tx = ".(isset($multicurrency_tx) ?
"'".$this->db->escape($multicurrency_tx).
"'" :
'1').
",";
391 $sql .=
" fk_multicurrency = ".(isset($fk_multicurrency) ?
"'".$this->db->escape($fk_multicurrency).
"'" :
'null').
",";
392 $sql .=
" multicurrency_code = ".(isset($multicurrency_code) ?
"'".$this->db->escape($multicurrency_code).
"'" :
'null').
",";
393 $sql .=
" entity = ".$conf->entity.
",";
394 $sql .=
" tva_tx = ".price2num(
$tva_tx).
",";
400 $sql .=
" default_vat_code=".($newdefaultvatcode ?
"'".$this->db->escape($newdefaultvatcode).
"'" :
"null").
",";
401 $sql .=
" info_bits = ".((int) $newnpr).
",";
402 $sql .=
" charges = ".((float) $charges).
",";
403 $sql .=
" delivery_time_days = ".($delivery_time_days !=
'' ? ((int) $delivery_time_days) :
'null').
",";
404 $sql .=
" supplier_reputation = ".(empty($supplier_reputation) ?
'NULL' :
"'".$this->db->escape($supplier_reputation).
"'").
",";
405 $sql .=
" barcode = ".(empty($barcode) ?
'NULL' :
"'".$this->db->escape($barcode).
"'").
",";
406 $sql .=
" fk_barcode_type = ".(empty($fk_barcode_type) ?
'NULL' :
"'".$this->db->escape($fk_barcode_type).
"'");
407 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
408 $sql .=
", packaging = ".(empty($packaging) ? 1 : $packaging);
410 $sql .=
" WHERE rowid = ".((int) $this->product_fourn_price_id);
413 if (!empty($options) && is_array($options)) {
415 $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
417 foreach ($options as $key=>$value) {
418 $productfournisseurprice->array_options[$key] = $value;
420 $res = $productfournisseurprice->update($user);
422 $this->error = $productfournisseurprice->error;
423 $this->errors = $productfournisseurprice->errors;
432 dol_syslog(get_class($this).
'::update_buyprice update knowing id of line = product_fourn_price_id = '.$this->product_fourn_price_id, LOG_DEBUG);
436 $result = $this->
call_trigger(
'SUPPLIER_PRODUCT_BUYPRICE_MODIFY', $user);
441 if (!$error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
442 $result = $this->
logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
449 return $this->product_fourn_price_id;
451 $this->
db->rollback();
455 $this->error = $this->
db->error().
" sql=".$sql;
456 $this->
db->rollback();
460 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);
463 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"product_fournisseur_price";
464 $sql .=
" WHERE fk_soc = ".((int) $fourn->id).
" AND ref_fourn = '".$this->
db->escape(
$ref_fourn).
"' AND quantity = ".((
float) $qty).
" AND entity = ".((int) $conf->entity);
468 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"product_fournisseur_price(";
469 $sql .=
" multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
470 $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";
471 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
472 $sql .=
", packaging";
475 $sql .= (isset($multicurrency_buyprice) ?
"'".$this->db->escape(
price2num($multicurrency_buyprice)).
"'" :
'null').
",";
476 $sql .= (isset($multicurrency_unitBuyPrice) ?
"'".$this->db->escape(
price2num($multicurrency_unitBuyPrice)).
"'" :
'null').
",";
477 $sql .= (isset($multicurrency_tx) ?
"'".$this->db->escape($multicurrency_tx).
"'" :
'1').
",";
478 $sql .= (isset($fk_multicurrency) ?
"'".$this->db->escape($fk_multicurrency).
"'" :
'null').
",";
479 $sql .= (isset($multicurrency_code) ?
"'".$this->db->escape($multicurrency_code).
"'" :
'null').
",";
480 $sql .=
" '".$this->db->idate($now).
"',";
481 $sql .=
" ".((int) $this->
id).
",";
482 $sql .=
" ".((int) $fourn->id).
",";
483 $sql .=
" '".$this->db->escape(
$ref_fourn).
"',";
484 $sql .=
" '".$this->db->escape($desc_fourn).
"',";
485 $sql .=
" ".((int) $user->id).
",";
486 $sql .=
" ".price2num($buyprice).
",";
487 $sql .=
" ".((float) $qty).
",";
489 $sql .=
" ".((float) $remise).
",";
490 $sql .=
" ".price2num($unitBuyPrice).
",";
491 $sql .=
" ".price2num(
$tva_tx).
",";
492 $sql .=
" ".price2num($charges).
",";
493 $sql .=
" ".((int) $availability).
",";
494 $sql .=
" ".($newdefaultvatcode ?
"'".$this->db->escape($newdefaultvatcode).
"'" :
"null").
",";
495 $sql .=
" ".((int) $newnpr).
",";
496 $sql .= $conf->entity.
",";
497 $sql .= ($delivery_time_days !=
'' ? ((int) $delivery_time_days) :
'null').
",";
498 $sql .= (empty($supplier_reputation) ?
'NULL' :
"'".$this->db->escape($supplier_reputation).
"'").
",";
499 $sql .= (empty($barcode) ?
'NULL' :
"'".$this->db->escape($barcode).
"'").
",";
500 $sql .= (empty($fk_barcode_type) ?
'NULL' :
"'".$this->db->escape($fk_barcode_type).
"'");
501 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
502 $sql .=
", ".(empty($this->packaging) ?
'1' :
"'".$this->db->escape($this->packaging).
"'");
506 $this->product_fourn_price_id = 0;
510 $this->product_fourn_price_id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"product_fournisseur_price");
512 $this->error = $this->
db->lasterror();
517 if (!empty($options) && is_array($options)) {
519 $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
521 foreach ($options as $key=>$value) {
522 $productfournisseurprice->array_options[$key] = $value;
524 $res = $productfournisseurprice->update($user);
526 $this->error = $productfournisseurprice->error;
527 $this->errors = $productfournisseurprice->errors;
534 if (!$error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
537 $result = $this->
logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
545 $result = $this->
call_trigger(
'SUPPLIER_PRODUCT_BUYPRICE_CREATE', $user);
553 return $this->product_fourn_price_id;
555 $this->
db->rollback();
559 $this->error = $this->
db->lasterror().
" sql=".$sql;
560 $this->
db->rollback();
564 $this->error = $this->
db->lasterror().
" sql=".$sql;
565 $this->
db->rollback();
584 $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,";
585 $sql .=
" pfp.fk_soc, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_product, pfp.charges, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
586 $sql .=
" pfp.supplier_reputation, pfp.fk_user, pfp.datec,";
587 $sql .=
" pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
588 $sql .=
" pfp.barcode, pfp.fk_barcode_type, pfp.packaging,";
589 $sql .=
" p.ref as product_ref, p.tosell as status, p.tobuy as status_buy";
590 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product_fournisseur_price as pfp, ".MAIN_DB_PREFIX.
"product as p";
591 $sql .=
" WHERE pfp.rowid = ".(int) $rowid;
592 $sql .=
" AND pfp.fk_product = p.rowid";
594 dol_syslog(get_class($this).
"::fetch_product_fournisseur_price", LOG_DEBUG);
597 $obj = $this->
db->fetch_object(
$resql);
599 $this->product_fourn_price_id = $rowid;
600 $this->
id = $obj->fk_product;
602 $this->fk_product = $obj->fk_product;
603 $this->product_id = $obj->fk_product;
604 $this->product_ref = $obj->product_ref;
605 $this->status = $obj->status;
606 $this->status_buy = $obj->status_buy;
607 $this->fourn_id = $obj->fk_soc;
608 $this->fourn_ref = $obj->ref_fourn;
609 $this->ref_supplier = $obj->ref_fourn;
610 $this->desc_supplier = $obj->desc_fourn;
611 $this->fourn_price = $obj->price;
612 $this->fourn_charges = $obj->charges;
613 $this->fourn_qty = $obj->quantity;
614 $this->fourn_remise_percent = $obj->remise_percent;
615 $this->fourn_remise = $obj->remise;
616 $this->fourn_unitprice = $obj->unitprice;
617 $this->fourn_tva_tx = $obj->tva_tx;
618 $this->fourn_tva_npr = $obj->fourn_tva_npr;
620 $this->fk_availability = $obj->fk_availability;
621 $this->delivery_time_days = $obj->delivery_time_days;
622 $this->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
623 $this->supplier_reputation = $obj->supplier_reputation;
624 $this->default_vat_code = $obj->default_vat_code;
625 $this->user_id = $obj->fk_user;
626 $this->date_creation = $this->
db->jdate($obj->datec);
627 $this->fourn_multicurrency_price = $obj->multicurrency_price;
628 $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
629 $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
630 $this->fourn_multicurrency_id = $obj->fk_multicurrency;
631 $this->fourn_multicurrency_code = $obj->multicurrency_code;
633 $this->fourn_barcode = $obj->barcode;
634 $this->fourn_fk_barcode_type = $obj->fk_barcode_type;
635 $this->supplier_barcode = $obj->barcode;
636 $this->supplier_fk_barcode_type = $obj->fk_barcode_type;
638 $this->packaging = $obj->packaging;
640 if (
isModEnabled(
'dynamicprices') && empty($ignore_expression) && !empty($this->fk_supplier_price_expression)) {
641 require_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
643 $price_result = $priceparser->parseProductSupplier($this);
644 if ($price_result >= 0) {
645 $this->fourn_price = $price_result;
647 if ($this->fourn_qty != 0) {
648 $this->fourn_unitprice =
price2num($this->fourn_price / $this->fourn_qty,
'MU');
650 $this->fourn_unitprice =
"";
660 $this->error = $this->
db->lasterror();
684 $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, ";
685 $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,";
686 $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,";
687 $sql .=
" pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code, pfp.datec, pfp.tms,";
688 $sql .=
" pfp.barcode, pfp.fk_barcode_type, pfp.packaging";
689 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product_fournisseur_price as pfp, ".MAIN_DB_PREFIX.
"product as p, ".MAIN_DB_PREFIX.
"societe as s";
690 $sql .=
" WHERE pfp.entity IN (".getEntity(
'productsupplierprice').
")";
691 $sql .=
" AND pfp.fk_soc = s.rowid AND pfp.fk_product = p.rowid";
692 $sql .= ($socid > 0 ?
' AND pfp.fk_soc = '.((int) $socid) :
'');
693 $sql .=
" AND s.status = 1";
694 $sql .=
" AND pfp.fk_product = ".((int) $prodid);
695 if (empty($sortfield)) {
696 $sql .=
" ORDER BY s.nom, pfp.quantity, pfp.price";
698 $sql .= $this->
db->order($sortfield, $sortorder);
700 $sql .= $this->
db->plimit($limit, $offset);
701 dol_syslog(get_class($this).
"::list_product_fournisseur_price", LOG_DEBUG);
707 while ($record = $this->
db->fetch_array(
$resql)) {
711 $prodfourn->product_ref = $record[
"product_ref"];
712 $prodfourn->product_fourn_price_id = $record[
"product_fourn_pri_id"];
713 $prodfourn->status = $record[
"status"];
714 $prodfourn->status_buy = $record[
"status_buy"];
715 $prodfourn->product_fourn_id = $record[
"product_fourn_id"];
716 $prodfourn->product_fourn_entity = $record[
"entity"];
717 $prodfourn->ref_supplier = $record[
"ref_fourn"];
718 $prodfourn->fourn_ref = $record[
"ref_fourn"];
719 $prodfourn->desc_supplier = $record[
"desc_fourn"];
720 $prodfourn->fourn_price = $record[
"price"];
721 $prodfourn->fourn_qty = $record[
"quantity"];
722 $prodfourn->fourn_remise_percent = $record[
"remise_percent"];
723 $prodfourn->fourn_remise = $record[
"remise"];
724 $prodfourn->fourn_unitprice = $record[
"unitprice"];
725 $prodfourn->fourn_charges = $record[
"charges"];
726 $prodfourn->fourn_tva_tx = $record[
"tva_tx"];
727 $prodfourn->fourn_id = $record[
"fourn_id"];
728 $prodfourn->fourn_name = $record[
"supplier_name"];
729 $prodfourn->fk_availability = $record[
"fk_availability"];
730 $prodfourn->delivery_time_days = $record[
"delivery_time_days"];
731 $prodfourn->id = $prodid;
732 $prodfourn->fourn_tva_npr = $record[
"info_bits"];
733 $prodfourn->fk_supplier_price_expression = $record[
"fk_supplier_price_expression"];
734 $prodfourn->supplier_reputation = $record[
"supplier_reputation"];
735 $prodfourn->fourn_date_creation = $this->
db->jdate($record[
'datec']);
736 $prodfourn->fourn_date_modification = $this->
db->jdate($record[
'tms']);
738 $prodfourn->fourn_multicurrency_price = $record[
"multicurrency_price"];
739 $prodfourn->fourn_multicurrency_unitprice = $record[
"multicurrency_unitprice"];
740 $prodfourn->fourn_multicurrency_tx = $record[
"multicurrency_tx"];
741 $prodfourn->fourn_multicurrency_id = $record[
"fk_multicurrency"];
742 $prodfourn->fourn_multicurrency_code = $record[
"multicurrency_code"];
744 $prodfourn->packaging = $record[
"packaging"];
747 $prodfourn->supplier_barcode = $record[
"barcode"];
748 $prodfourn->supplier_fk_barcode_type = $record[
"fk_barcode_type"];
751 if (
isModEnabled(
'dynamicprices') && !empty($prodfourn->fk_supplier_price_expression)) {
752 require_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
754 $price_result = $priceparser->parseProductSupplier($prodfourn);
755 if ($price_result >= 0) {
756 $prodfourn->fourn_price = $price_result;
757 $prodfourn->fourn_unitprice =
null;
761 if (!isset($prodfourn->fourn_unitprice)) {
762 if ($prodfourn->fourn_qty != 0) {
763 $prodfourn->fourn_unitprice =
price2num($prodfourn->fourn_price / $prodfourn->fourn_qty,
'MU');
765 $prodfourn->fourn_unitprice =
"";
769 $retarray[] = $prodfourn;
775 $this->error = $this->
db->error();
795 if (empty($prodid)) {
796 dol_syslog(
"Warning function find_min_price_product_fournisseur were called with prodid empty. May be a bug.", LOG_WARNING);
800 $this->product_fourn_price_id =
'';
801 $this->product_fourn_id =
'';
802 $this->fourn_ref =
'';
803 $this->fourn_price =
'';
804 $this->fourn_qty =
'';
805 $this->fourn_remise_percent =
'';
806 $this->fourn_remise =
'';
807 $this->fourn_unitprice =
'';
808 $this->fourn_id =
'';
809 $this->fourn_name =
'';
810 $this->delivery_time_days =
'';
813 $this->fourn_multicurrency_price =
'';
814 $this->fourn_multicurrency_unitprice =
'';
815 $this->fourn_multicurrency_tx =
'';
816 $this->fourn_multicurrency_id =
'';
817 $this->fourn_multicurrency_code =
'';
819 $sql =
"SELECT s.nom as supplier_name, s.rowid as fourn_id,";
820 $sql .=
" pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
821 $sql .=
" pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges,";
822 $sql .=
" pfp.remise, pfp.remise_percent, pfp.fk_supplier_price_expression, pfp.delivery_time_days";
823 $sql .=
" ,pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code";
824 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s, ".MAIN_DB_PREFIX.
"product_fournisseur_price as pfp";
825 $sql .=
" WHERE s.entity IN (".getEntity(
'societe').
")";
826 $sql .=
" AND pfp.entity IN (".getEntity(
'productsupplierprice').
")";
827 $sql .=
" AND pfp.fk_product = ".((int) $prodid);
828 $sql .=
" AND pfp.fk_soc = s.rowid";
829 $sql .=
" AND s.status = 1";
831 $sql .=
" AND pfp.quantity <= ".((float) $qty);
834 $sql .=
' AND pfp.fk_soc = '.((int) $socid);
837 dol_syslog(get_class($this).
"::find_min_price_product_fournisseur", LOG_DEBUG);
841 $record_array = array();
844 while ($record = $this->
db->fetch_array(
$resql)) {
845 $record_array[] = $record;
848 if (count($record_array) == 0) {
853 foreach ($record_array as $record) {
854 $fourn_price = $record[
"price"];
856 $fourn_unitprice = $record[
"unitprice"];
857 $fourn_unitprice_with_discount = $record[
"unitprice"] * (1 - $record[
"remise_percent"] / 100);
859 if (
isModEnabled(
'dynamicprices') && !empty($record[
"fk_supplier_price_expression"])) {
861 $prod_supplier->product_fourn_price_id = $record[
"product_fourn_price_id"];
862 $prod_supplier->id = $prodid;
863 $prod_supplier->fourn_qty = $record[
"quantity"];
864 $prod_supplier->fourn_tva_tx = $record[
"tva_tx"];
865 $prod_supplier->fk_supplier_price_expression = $record[
"fk_supplier_price_expression"];
867 require_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
869 $price_result = $priceparser->parseProductSupplier($prod_supplier);
870 if ($price_result >= 0) {
871 $fourn_price =
price2num($price_result,
'MU');
872 if ($record[
"quantity"] != 0) {
873 $fourn_unitprice =
price2num($fourn_price / $record[
"quantity"],
'MU');
875 $fourn_unitprice = $fourn_price;
877 $fourn_unitprice_with_discount = $fourn_unitprice * (1 - $record[
"remise_percent"] / 100);
880 if ($fourn_unitprice < $min || $min == -1) {
881 $this->product_fourn_price_id = $record[
"product_fourn_price_id"];
882 $this->ref_supplier = $record[
"ref_fourn"];
883 $this->ref_fourn = $record[
"ref_fourn"];
884 $this->fourn_ref = $record[
"ref_fourn"];
885 $this->fourn_price = $fourn_price;
886 $this->fourn_qty = $record[
"quantity"];
887 $this->fourn_remise_percent = $record[
"remise_percent"];
888 $this->fourn_remise = $record[
"remise"];
889 $this->fourn_unitprice = $fourn_unitprice;
890 $this->fourn_unitprice_with_discount = $fourn_unitprice_with_discount;
891 $this->fourn_charges = $record[
"charges"];
892 $this->fourn_tva_tx = $record[
"tva_tx"];
893 $this->fourn_id = $record[
"fourn_id"];
894 $this->fourn_name = $record[
"supplier_name"];
895 $this->delivery_time_days = $record[
"delivery_time_days"];
896 $this->fk_supplier_price_expression = $record[
"fk_supplier_price_expression"];
898 $this->fourn_multicurrency_price = $record[
"multicurrency_price"];
899 $this->fourn_multicurrency_unitprice = $record[
"multicurrency_unitprice"];
900 $this->fourn_multicurrency_tx = $record[
"multicurrency_tx"];
901 $this->fourn_multicurrency_id = $record[
"fk_multicurrency"];
902 $this->fourn_multicurrency_code = $record[
"multicurrency_code"];
903 $min = $fourn_unitprice;
911 $this->error = $this->
db->error();
928 $expression_id = $expression_id != 0 ? $expression_id :
'NULL';
930 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"product_fournisseur_price";
931 $sql .=
" SET fk_supplier_price_expression = ".((int) $expression_id);
932 $sql .=
" WHERE rowid = ".((int) $this->product_fourn_price_id);
934 dol_syslog(get_class($this).
"::setSupplierPriceExpression", LOG_DEBUG);
941 $this->error = $this->
db->error().
" sql=".$sql;
942 $this->
db->rollback();
957 public function getSocNomUrl($withpicto = 0, $option =
'supplier', $maxlen = 0, $notooltip = 0)
960 $thirdparty->fetch($this->fourn_id);
962 return $thirdparty->getNomUrl($withpicto, $option, $maxlen, $notooltip);
980 global $conf, $langs;
983 $langs->load(
"suppliers");
984 if (count($productFournList) > 0) {
985 $out .=
'<table class="nobordernopadding" width="100%">';
986 $out .=
'<tr><td class="liste_titre right">'.($showunitprice ? $langs->trans(
"Price").
' '.$langs->trans(
"HT") :
'').
'</td>';
987 $out .=
'<td class="liste_titre right">'.($showunitprice ? $langs->trans(
"QtyMin") :
'').
'</td>';
988 $out .=
'<td class="liste_titre">'.$langs->trans(
"Supplier").
'</td>';
989 $out .=
'<td class="liste_titre">'.$langs->trans(
"SupplierRef").
'</td></tr>';
990 foreach ($productFournList as $productFourn) {
991 $out .=
'<tr><td class="right">'.($showunitprice ?
price($productFourn->fourn_unitprice * (1 - $productFourn->fourn_remise_percent / 100) - $productFourn->fourn_remise) :
'').
'</td>';
992 $out .=
'<td class="right">'.($showunitprice ? $productFourn->fourn_qty :
'').
'</td>';
993 $out .=
'<td>'.$productFourn->getSocNomUrl(1,
'supplier', $maxlen, $notooltip).
'</td>';
994 $out .=
'<td>'.$productFourn->fourn_ref.
'<td></tr>';
998 $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").
' <span class="opacitymedium">(</span>' :
'');
999 $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;
1000 $out .= ($showunitprice ?
'<span class="opacitymedium">)</span>' :
'');
1016 'product_fournisseur_price'
1033 'product_fournisseur_price'
1052 $sql .=
" u.lastname,";
1053 $sql .=
" pfpl.rowid, pfp.ref_fourn as supplier_ref, pfpl.datec,";
1054 $sql .=
" pfpl.price, pfpl.quantity,";
1055 $sql .=
" pfpl.fk_multicurrency, pfpl.multicurrency_code, pfpl.multicurrency_tx, pfpl.multicurrency_price, pfpl.multicurrency_unitprice";
1056 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product_fournisseur_price_log as pfpl,";
1057 $sql .=
" ".MAIN_DB_PREFIX.
"product_fournisseur_price as pfp,";
1058 $sql .=
" ".MAIN_DB_PREFIX.
"user as u";
1059 $sql .=
" WHERE pfp.entity IN (".getEntity(
'productprice').
")";
1060 $sql .=
" AND pfpl.fk_user = u.rowid";
1061 $sql .=
" AND pfp.rowid = pfpl.fk_product_fournisseur";
1062 $sql .=
" AND pfpl.fk_product_fournisseur = ".((int) $product_fourn_price_id);
1063 if (empty($sortfield)) {
1064 $sql .=
" ORDER BY pfpl.datec";
1066 $sql .= $this->
db->order($sortfield, $sortorder);
1068 $sql .= $this->
db->plimit($limit, $offset);
1069 dol_syslog(get_class($this).
"::list_product_fournisseur_price_log", LOG_DEBUG);
1073 $retarray = array();
1075 while ($obj = $this->
db->fetch_object(
$resql)) {
1076 $tmparray = array();
1077 $tmparray[
'rowid'] = $obj->rowid;
1078 $tmparray[
'supplier_ref'] = $obj->supplier_ref;
1079 $tmparray[
'datec'] = $this->
db->jdate($obj->datec);
1080 $tmparray[
'lastname'] = $obj->lastname;
1081 $tmparray[
'price'] = $obj->price;
1082 $tmparray[
'quantity'] = $obj->quantity;
1083 $tmparray[
'fk_multicurrency'] = $obj->fk_multicurrency;
1084 $tmparray[
'multicurrency_code'] = $obj->multicurrency_code;
1085 $tmparray[
'multicurrency_tx'] = $obj->multicurrency_tx;
1086 $tmparray[
'multicurrency_price'] = $obj->multicurrency_price;
1087 $tmparray[
'multicurrency_unitprice'] = $obj->multicurrency_unitprice;
1089 $retarray[] = $tmparray;
1095 $this->error = $this->
db->error();
1109 global $conf, $langs;
1112 $langs->load(
"suppliers");
1113 if (count($productFournLogList) > 0) {
1114 $out .=
'<table class="noborder centpercent">';
1115 $out .=
'<tr class="liste_titre"><td class="liste_titre">'.$langs->trans(
"Date").
'</td>';
1116 $out .=
'<td class="liste_titre right">'.$langs->trans(
"Price").
'</td>';
1118 $out .=
'<td class="liste_titre">'.$langs->trans(
"User").
'</td></tr>';
1119 foreach ($productFournLogList as $productFournLog) {
1120 $out .=
'<tr><td>'.dol_print_date($productFournLog[
'datec'],
'dayhour',
'tzuser').
'</td>';
1121 $out .=
'<td class="right">'.price($productFournLog[
'price'], 0, $langs, 1, -1, -1, $conf->currency);
1122 if ($productFournLog[
'multicurrency_code'] != $conf->currency) {
1123 $out .=
' ('.price($productFournLog[
'multicurrency_price'], 0, $langs, 1, -1, -1, $productFournLog[
'multicurrency_code']).
')';
1127 $out .=
'<td>'.$productFournLog[
'lastname'].
'</td></tr>';
1149 public function getNomUrl($withpicto = 0, $option =
'', $maxlength = 0, $save_lastsearch_value = -1, $notooltip = 0, $morecss =
'', $add_label = 0, $sep =
' - ')
1151 global $db, $conf, $langs, $hookmanager;
1153 if (!empty($conf->dol_no_mouse_hover)) {
1160 $newref = $this->ref;
1162 $newref =
dol_trunc($newref, $maxlength,
'middle');
1165 if (!empty($this->entity)) {
1166 $tmpphoto = $this->
show_photos(
'product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 80);
1167 if ($this->nbphoto > 0) {
1168 $label .=
'<div class="photointooltip">';
1169 $label .= $tmpphoto;
1170 $label .=
'</div><div style="clear: both;"></div>';
1175 $label .=
img_picto(
'',
'product').
' <u class="paddingrightonly">'.$langs->trans(
"Product").
'</u>';
1177 $label .=
img_picto(
'',
'service').
' <u class="paddingrightonly">'.$langs->trans(
"Service").
'</u>';
1179 if (isset($this->status) && isset($this->status_buy)) {
1180 $label .=
' '.$this->getLibStatut(5, 0);
1181 $label .=
' '.$this->getLibStatut(5, 1);
1184 if (!empty($this->
ref)) {
1185 $label .=
'<br><b>'.$langs->trans(
'ProductRef').
':</b> '.($this->
ref ? $this->
ref : $this->product_ref);
1187 if (!empty($this->label)) {
1188 $label .=
'<br><b>'.$langs->trans(
'ProductLabel').
':</b> '.$this->label;
1190 $label .=
'<br><b>'.$langs->trans(
'RefSupplier').
':</b> '.$this->ref_supplier;
1194 $langs->load(
"productbatch");
1195 $label .=
"<br><b>".$langs->trans(
"ManageLotSerial").
'</b>: '.$this->
getLibStatut(0, 2);
1199 $label .=
'<br><b>'.$langs->trans(
'BarCode').
':</b> '.$this->barcode;
1203 if ($this->weight) {
1204 $label .=
"<br><b>".$langs->trans(
"Weight").
'</b>: '.$this->weight.
' '.
measuringUnitString(0,
"weight", $this->weight_units);
1207 if ($this->length) {
1208 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Length").
'</b>: '.$this->length.
' '.
measuringUnitString(0,
'size', $this->length_units);
1211 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Width").
'</b>: '.$this->width.
' '.
measuringUnitString(0,
'size', $this->width_units);
1213 if ($this->height) {
1214 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Height").
'</b>: '.$this->height.
' '.
measuringUnitString(0,
'size', $this->height_units);
1217 $label .=
"<br>".$labelsize;
1220 $labelsurfacevolume =
"";
1221 if ($this->surface) {
1222 $labelsurfacevolume .= ($labelsurfacevolume ?
" - " :
"").
"<b>".$langs->trans(
"Surface").
'</b>: '.$this->surface.
' '.
measuringUnitString(0,
'surface', $this->surface_units);
1224 if ($this->volume) {
1225 $labelsurfacevolume .= ($labelsurfacevolume ?
" - " :
"").
"<b>".$langs->trans(
"Volume").
'</b>: '.$this->volume.
' '.
measuringUnitString(0,
'volume', $this->volume_units);
1227 if ($labelsurfacevolume) {
1228 $label .=
"<br>".$labelsurfacevolume;
1233 include_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
1234 $label .=
'<br><b>'.$langs->trans(
'ProductAccountancySellCode').
':</b> '.
length_accountg($this->accountancy_code_sell);
1235 $label .=
'<br><b>'.$langs->trans(
'ProductAccountancySellIntraCode').
':</b> '.
length_accountg($this->accountancy_code_sell_intra);
1236 $label .=
'<br><b>'.$langs->trans(
'ProductAccountancySellExportCode').
':</b> '.
length_accountg($this->accountancy_code_sell_export);
1239 include_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
1240 $label .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyCode').
':</b> '.
length_accountg($this->accountancy_code_buy);
1241 $label .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyIntraCode').
':</b> '.
length_accountg($this->accountancy_code_buy_intra);
1242 $label .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyExportCode').
':</b> '.
length_accountg($this->accountancy_code_buy_export);
1246 if (is_array($logPrices) && count($logPrices) > 0) {
1247 $label .=
'<br><br>';
1248 $label .=
'<u>'.$langs->trans(
"History").
'</u>';
1252 $url =
dol_buildpath(
'/product/fournisseurs.php', 1).
'?id='.$this->
id.
'&action=add_price&token='.
newToken().
'&socid='.$this->fourn_id.
'&rowid='.$this->product_fourn_price_id;
1254 if ($option !=
'nolink') {
1256 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1257 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1258 $add_save_lastsearch_values = 1;
1260 if ($add_save_lastsearch_values) {
1261 $url .=
'&save_lastsearch_values=1';
1266 if (empty($notooltip)) {
1267 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1268 $label = $langs->trans(
"SupplierRef");
1269 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1271 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1272 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1274 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1277 $linkstart =
'<a href="'.$url.
'"';
1278 $linkstart .= $linkclose.
'>';
1281 $result .= $linkstart;
1283 $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);
1285 if ($withpicto != 2) {
1286 $result .= $newref.($this->ref_supplier ?
' ('.$this->ref_supplier.
')' :
'');
1288 $result .= $linkend;
1289 if ($withpicto != 2) {
1290 $result .= (($add_label && $this->label) ? $sep.dol_trunc($this->label, ($add_label > 1 ? $add_label : 0)) :
'');
1294 $hookmanager->initHooks(array($this->element .
'dao'));
1295 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
1296 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1298 $result = $hookmanager->resPrint;
1300 $result .= $hookmanager->resPrint;
1320 private function logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice =
null, $multicurrency_unitBuyPrice =
null, $multicurrency_tx =
null, $fk_multicurrency =
null, $multicurrency_code =
null)
1323 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"product_fournisseur_price_log(";
1324 $sql .=
" multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
1325 $sql .=
"datec, fk_product_fournisseur,fk_user,price,quantity)";
1327 $sql .= (isset($multicurrency_buyprice) ?
"'".$this->db->escape(
price2num($multicurrency_buyprice)).
"'" :
'null').
",";
1328 $sql .= (isset($multicurrency_unitBuyPrice) ?
"'".$this->db->escape(
price2num($multicurrency_unitBuyPrice)).
"'" :
'null').
",";
1329 $sql .= (isset($multicurrency_tx) ?
"'".$this->db->escape($multicurrency_tx).
"'" :
'1').
",";
1330 $sql .= (isset($fk_multicurrency) ?
"'".$this->db->escape($fk_multicurrency).
"'" :
'null').
",";
1331 $sql .= (isset($multicurrency_code) ?
"'".$this->db->escape($multicurrency_code).
"'" :
'null').
",";
1332 $sql .=
"'".$this->db->idate($datec).
"',";
1333 $sql .=
" ".((int) $this->product_fourn_price_id).
",";
1334 $sql .=
" ".$user->id.
",";
1335 $sql .=
" ".price2num($buyprice).
",";
1336 $sql .=
" ".price2num($qty);