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;
138 const STATUS_OPEN = 1;
139 const STATUS_CANCELED = 0;
152 $langs->load(
"suppliers");
153 $this->reputations = array(
'-1'=>
'',
'FAVORITE'=>$langs->trans(
'Favorite'),
'NOTTHGOOD'=>$langs->trans(
'NotTheGoodQualitySupplier'),
'DONOTORDER'=>$langs->trans(
'DoNotOrderThisProductToThisSupplier'));
170 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"product_fournisseur_price";
171 $sql .=
" WHERE fk_product = ".((int) $this->
id).
" AND fk_soc = ".((int) $id_fourn);
173 dol_syslog(get_class($this).
"::remove_fournisseur", LOG_DEBUG);
174 $resql2 = $this->db->query(
$sql);
176 $this->error = $this->db->lasterror();
184 $this->db->rollback();
207 $result = $this->
call_trigger(
'SUPPLIER_PRODUCT_BUYPRICE_DELETE', $user);
214 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"product_fournisseur_price";
215 $sql .=
" WHERE rowid = ".((int) $rowid);
217 dol_syslog(get_class($this).
"::remove_product_fournisseur_price", LOG_DEBUG);
218 $resql = $this->db->query(
$sql);
220 $this->error = $this->db->lasterror();
229 $this->db->rollback();
265 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())
268 global $conf, $langs;
275 if (empty($buyprice)) {
278 if (empty($charges)) {
281 if (empty($availability)) {
287 if (empty($supplier_reputation) || $supplier_reputation == -1) {
288 $supplier_reputation =
'';
290 if ($delivery_time_days !=
'' && !is_numeric($delivery_time_days)) {
291 $delivery_time_days =
'';
293 if ($price_base_type ==
'TTC') {
295 $buyprice = $buyprice / (1 + ($ttx / 100));
299 $multicurrency_unitBuyPrice =
null;
300 $fk_multicurrency =
null;
302 if (empty($multicurrency_tx)) {
303 $multicurrency_tx = 1;
305 if (empty($multicurrency_buyprice)) {
306 $multicurrency_buyprice = 0;
308 if ($multicurrency_price_base_type ==
'TTC') {
310 $multicurrency_buyprice = $multicurrency_buyprice / (1 + ($ttx / 100));
312 $multicurrency_buyprice =
price2num($multicurrency_buyprice,
'MU');
313 $multicurrency_unitBuyPrice =
price2num($multicurrency_buyprice / $qty,
'MU');
315 $buyprice = $multicurrency_buyprice / $multicurrency_tx;
322 $unitBuyPrice =
price2num($buyprice / $qty,
'MU');
326 $packaging =
price2num((empty($this->packaging) ? $qty : $this->packaging),
'MS');
333 if (count($localtaxes_array) > 0) {
334 $localtaxtype1 = $localtaxes_array[
'0'];
335 $localtax1 = $localtaxes_array[
'1'];
336 $localtaxtype2 = $localtaxes_array[
'2'];
337 $localtax2 = $localtaxes_array[
'3'];
339 $localtaxtype1 =
'0';
341 $localtaxtype2 =
'0';
344 if (empty($localtax1)) {
347 if (empty($localtax2)) {
352 if ($buyprice !=
'' && !is_numeric($buyprice)) {
357 if ($this->product_fourn_price_id > 0) {
360 if (is_array($logPrices) && count($logPrices) == 0) {
361 $currentPfp =
new self($this->db);
362 $result = $currentPfp->fetch_product_fournisseur_price($this->product_fourn_price_id);
363 if ($result > 0 && $currentPfp->fourn_price != 0) {
364 $currentPfpUser =
new User($this->db);
365 $result = $currentPfpUser->fetch($currentPfp->user_id);
367 $currentPfp->logPrice(
369 $currentPfp->date_creation,
370 $currentPfp->fourn_price,
371 $currentPfp->fourn_qty,
372 $currentPfp->fourn_multicurrency_price,
373 $currentPfp->fourn_multicurrency_unitprice,
374 $currentPfp->fourn_multicurrency_tx,
375 $currentPfp->fourn_multicurrency_id,
376 $currentPfp->fourn_multicurrency_code
381 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"product_fournisseur_price";
382 $sql .=
" SET fk_user = ".((int) $user->id).
" ,";
384 $sql .=
" desc_fourn = '".$this->db->escape($desc_fourn).
"',";
385 $sql .=
" price = ".((float) $buyprice).
",";
386 $sql .=
" quantity = ".((float) $qty).
",";
388 $sql .=
" remise = ".((float) $remise).
",";
389 $sql .=
" unitprice = ".((float) $unitBuyPrice).
",";
390 $sql .=
" fk_availability = ".((int) $availability).
",";
391 $sql .=
" multicurrency_price = ".(isset($multicurrency_buyprice) ?
"'".$this->db->escape(
price2num($multicurrency_buyprice)).
"'" :
'null').
",";
392 $sql .=
" multicurrency_unitprice = ".(isset($multicurrency_unitBuyPrice) ?
"'".$this->db->escape(
price2num($multicurrency_unitBuyPrice)).
"'" :
'null').
",";
393 $sql .=
" multicurrency_tx = ".(isset($multicurrency_tx) ?
"'".$this->db->escape($multicurrency_tx).
"'" :
'1').
",";
394 $sql .=
" fk_multicurrency = ".(isset($fk_multicurrency) ?
"'".$this->db->escape($fk_multicurrency).
"'" :
'null').
",";
395 $sql .=
" multicurrency_code = ".(isset($multicurrency_code) ?
"'".$this->db->escape($multicurrency_code).
"'" :
'null').
",";
396 $sql .=
" entity = ".$conf->entity.
",";
403 $sql .=
" default_vat_code=".($newdefaultvatcode ?
"'".$this->db->escape($newdefaultvatcode).
"'" :
"null").
",";
404 $sql .=
" info_bits = ".((int) $newnpr).
",";
405 $sql .=
" charges = ".((float) $charges).
",";
406 $sql .=
" delivery_time_days = ".($delivery_time_days !=
'' ? ((int) $delivery_time_days) :
'null').
",";
407 $sql .=
" supplier_reputation = ".(empty($supplier_reputation) ?
'NULL' :
"'".$this->db->escape($supplier_reputation).
"'").
",";
408 $sql .=
" barcode = ".(empty($barcode) ?
'NULL' :
"'".$this->db->escape($barcode).
"'").
",";
409 $sql .=
" fk_barcode_type = ".(empty($fk_barcode_type) ?
'NULL' :
"'".$this->db->escape($fk_barcode_type).
"'");
410 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
411 $sql .=
", packaging = ".(empty($packaging) ? 1 : $packaging);
413 $sql .=
" WHERE rowid = ".((int) $this->product_fourn_price_id);
416 if (!empty($options) && is_array($options)) {
418 $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
420 foreach ($options as $key=>$value) {
421 $productfournisseurprice->array_options[$key] = $value;
423 $res = $productfournisseurprice->update($user);
425 $this->error = $productfournisseurprice->error;
426 $this->errors = $productfournisseurprice->errors;
435 dol_syslog(get_class($this).
'::update_buyprice update knowing id of line = product_fourn_price_id = '.$this->product_fourn_price_id, LOG_DEBUG);
436 $resql = $this->db->query(
$sql);
439 $result = $this->
call_trigger(
'SUPPLIER_PRODUCT_BUYPRICE_MODIFY', $user);
444 if (!$error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
445 $result = $this->
logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
452 return $this->product_fourn_price_id;
454 $this->db->rollback();
458 $this->error = $this->db->error().
" sql=".
$sql;
459 $this->db->rollback();
463 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);
466 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"product_fournisseur_price";
467 $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 $resql = $this->db->query(
$sql);
471 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"product_fournisseur_price(";
472 $sql .=
" multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
473 $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";
474 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
475 $sql .=
", packaging";
478 $sql .= (isset($multicurrency_buyprice) ?
"'".$this->db->escape(
price2num($multicurrency_buyprice)).
"'" :
'null').
",";
479 $sql .= (isset($multicurrency_unitBuyPrice) ?
"'".$this->db->escape(
price2num($multicurrency_unitBuyPrice)).
"'" :
'null').
",";
480 $sql .= (isset($multicurrency_tx) ?
"'".$this->db->escape($multicurrency_tx).
"'" :
'1').
",";
481 $sql .= (isset($fk_multicurrency) ?
"'".$this->db->escape($fk_multicurrency).
"'" :
'null').
",";
482 $sql .= (isset($multicurrency_code) ?
"'".$this->db->escape($multicurrency_code).
"'" :
'null').
",";
483 $sql .=
" '".$this->db->idate($now).
"',";
484 $sql .=
" ".((int) $this->
id).
",";
485 $sql .=
" ".((int) $fourn->id).
",";
487 $sql .=
" '".$this->db->escape($desc_fourn).
"',";
488 $sql .=
" ".((int) $user->id).
",";
489 $sql .=
" ".price2num($buyprice).
",";
490 $sql .=
" ".((float) $qty).
",";
492 $sql .=
" ".((float) $remise).
",";
493 $sql .=
" ".price2num($unitBuyPrice).
",";
495 $sql .=
" ".price2num($charges).
",";
496 $sql .=
" ".((int) $availability).
",";
497 $sql .=
" ".($newdefaultvatcode ?
"'".$this->db->escape($newdefaultvatcode).
"'" :
"null").
",";
498 $sql .=
" ".((int) $newnpr).
",";
499 $sql .= $conf->entity.
",";
500 $sql .= ($delivery_time_days !=
'' ? ((int) $delivery_time_days) :
'null').
",";
501 $sql .= (empty($supplier_reputation) ?
'NULL' :
"'".$this->db->escape($supplier_reputation).
"'").
",";
502 $sql .= (empty($barcode) ?
'NULL' :
"'".$this->db->escape($barcode).
"'").
",";
503 $sql .= (empty($fk_barcode_type) ?
'NULL' :
"'".$this->db->escape($fk_barcode_type).
"'");
504 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
505 $sql .=
", ".(empty($this->packaging) ?
'1' :
"'".$this->db->escape($this->packaging).
"'");
509 $this->product_fourn_price_id = 0;
511 $resql = $this->db->query(
$sql);
513 $this->product_fourn_price_id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"product_fournisseur_price");
515 $this->error = $this->db->lasterror();
520 if (!empty($options) && is_array($options)) {
522 $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
524 foreach ($options as $key=>$value) {
525 $productfournisseurprice->array_options[$key] = $value;
527 $res = $productfournisseurprice->update($user);
529 $this->error = $productfournisseurprice->error;
530 $this->errors = $productfournisseurprice->errors;
537 if (!$error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
540 $result = $this->
logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
548 $result = $this->
call_trigger(
'SUPPLIER_PRODUCT_BUYPRICE_CREATE', $user);
556 return $this->product_fourn_price_id;
558 $this->db->rollback();
562 $this->error = $this->db->lasterror().
" sql=".
$sql;
563 $this->db->rollback();
567 $this->error = $this->db->lasterror().
" sql=".
$sql;
568 $this->db->rollback();
587 $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,";
588 $sql .=
" pfp.fk_soc, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_product, pfp.charges, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
589 $sql .=
" pfp.supplier_reputation, pfp.fk_user, pfp.datec,";
590 $sql .=
" pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
591 $sql .=
" pfp.barcode, pfp.fk_barcode_type, pfp.packaging,";
592 $sql .=
" p.ref as product_ref, p.tosell as status, p.tobuy as status_buy";
593 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product_fournisseur_price as pfp, ".MAIN_DB_PREFIX.
"product as p";
594 $sql .=
" WHERE pfp.rowid = ".(int) $rowid;
595 $sql .=
" AND pfp.fk_product = p.rowid";
597 dol_syslog(get_class($this).
"::fetch_product_fournisseur_price", LOG_DEBUG);
598 $resql = $this->db->query(
$sql);
600 $obj = $this->db->fetch_object($resql);
602 $this->product_fourn_price_id = $rowid;
603 $this->
id = $obj->fk_product;
605 $this->fk_product = $obj->fk_product;
606 $this->product_id = $obj->fk_product;
607 $this->product_ref = $obj->product_ref;
608 $this->status = $obj->status;
609 $this->status_buy = $obj->status_buy;
610 $this->fourn_id = $obj->fk_soc;
611 $this->fourn_ref = $obj->ref_fourn;
612 $this->ref_supplier = $obj->ref_fourn;
613 $this->desc_supplier = $obj->desc_fourn;
614 $this->fourn_price = $obj->price;
615 $this->fourn_charges = $obj->charges;
616 $this->fourn_qty = $obj->quantity;
617 $this->fourn_remise_percent = $obj->remise_percent;
618 $this->fourn_remise = $obj->remise;
619 $this->fourn_unitprice = $obj->unitprice;
620 $this->fourn_tva_tx = $obj->tva_tx;
621 $this->fourn_tva_npr = $obj->fourn_tva_npr;
623 $this->fk_availability = $obj->fk_availability;
624 $this->delivery_time_days = $obj->delivery_time_days;
625 $this->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
626 $this->supplier_reputation = $obj->supplier_reputation;
627 $this->default_vat_code = $obj->default_vat_code;
628 $this->user_id = $obj->fk_user;
629 $this->date_creation = $this->db->jdate($obj->datec);
630 $this->fourn_multicurrency_price = $obj->multicurrency_price;
631 $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
632 $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
633 $this->fourn_multicurrency_id = $obj->fk_multicurrency;
634 $this->fourn_multicurrency_code = $obj->multicurrency_code;
636 $this->fourn_barcode = $obj->barcode;
637 $this->fourn_fk_barcode_type = $obj->fk_barcode_type;
638 $this->supplier_barcode = $obj->barcode;
639 $this->supplier_fk_barcode_type = $obj->fk_barcode_type;
641 $this->packaging = $obj->packaging;
643 if (
isModEnabled(
'dynamicprices') && empty($ignore_expression) && !empty($this->fk_supplier_price_expression)) {
644 require_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
646 $price_result = $priceparser->parseProductSupplier($this);
647 if ($price_result >= 0) {
648 $this->fourn_price = $price_result;
650 if ($this->fourn_qty != 0) {
651 $this->fourn_unitprice =
price2num($this->fourn_price / $this->fourn_qty,
'MU');
653 $this->fourn_unitprice =
"";
663 $this->error = $this->db->lasterror();
687 $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, ";
688 $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,";
689 $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,";
690 $sql .=
" pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code, pfp.datec, pfp.tms,";
691 $sql .=
" pfp.barcode, pfp.fk_barcode_type, pfp.packaging, pfp.status as pfstatus";
692 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product_fournisseur_price as pfp, ".MAIN_DB_PREFIX.
"product as p, ".MAIN_DB_PREFIX.
"societe as s";
693 $sql .=
" WHERE pfp.entity IN (".getEntity(
'productsupplierprice').
")";
694 $sql .=
" AND pfp.fk_soc = s.rowid AND pfp.fk_product = p.rowid";
695 $sql .= ($socid > 0 ?
' AND pfp.fk_soc = '.((int) $socid) :
'');
696 $sql .=
" AND s.status = 1";
697 $sql .=
" AND pfp.fk_product = ".((int) $prodid);
698 if (empty($sortfield)) {
699 $sql .=
" ORDER BY s.nom, pfp.quantity, pfp.price";
701 $sql .= $this->db->order($sortfield, $sortorder);
703 $sql .= $this->db->plimit($limit, $offset);
704 dol_syslog(get_class($this).
"::list_product_fournisseur_price", LOG_DEBUG);
706 $resql = $this->db->query(
$sql);
710 while ($record = $this->db->fetch_array($resql)) {
714 $prodfourn->product_ref = $record[
"product_ref"];
715 $prodfourn->product_fourn_price_id = $record[
"product_fourn_pri_id"];
716 $prodfourn->status = $record[
"status"];
717 $prodfourn->status_buy = $record[
"status_buy"];
718 $prodfourn->product_fourn_id = $record[
"product_fourn_id"];
719 $prodfourn->product_fourn_entity = $record[
"entity"];
720 $prodfourn->ref_supplier = $record[
"ref_fourn"];
721 $prodfourn->fourn_ref = $record[
"ref_fourn"];
722 $prodfourn->desc_supplier = $record[
"desc_fourn"];
723 $prodfourn->fourn_price = $record[
"price"];
724 $prodfourn->fourn_qty = $record[
"quantity"];
725 $prodfourn->fourn_remise_percent = $record[
"remise_percent"];
726 $prodfourn->fourn_remise = $record[
"remise"];
727 $prodfourn->fourn_unitprice = $record[
"unitprice"];
728 $prodfourn->fourn_charges = $record[
"charges"];
729 $prodfourn->fourn_tva_tx = $record[
"tva_tx"];
730 $prodfourn->fourn_id = $record[
"fourn_id"];
731 $prodfourn->fourn_name = $record[
"supplier_name"];
732 $prodfourn->fk_availability = $record[
"fk_availability"];
733 $prodfourn->delivery_time_days = $record[
"delivery_time_days"];
734 $prodfourn->id = $prodid;
735 $prodfourn->fourn_tva_npr = $record[
"info_bits"];
736 $prodfourn->fk_supplier_price_expression = $record[
"fk_supplier_price_expression"];
737 $prodfourn->supplier_reputation = $record[
"supplier_reputation"];
738 $prodfourn->fourn_date_creation = $this->db->jdate($record[
'datec']);
739 $prodfourn->fourn_date_modification = $this->db->jdate($record[
'tms']);
741 $prodfourn->fourn_multicurrency_price = $record[
"multicurrency_price"];
742 $prodfourn->fourn_multicurrency_unitprice = $record[
"multicurrency_unitprice"];
743 $prodfourn->fourn_multicurrency_tx = $record[
"multicurrency_tx"];
744 $prodfourn->fourn_multicurrency_id = $record[
"fk_multicurrency"];
745 $prodfourn->fourn_multicurrency_code = $record[
"multicurrency_code"];
747 $prodfourn->packaging = $record[
"packaging"];
748 $prodfourn->status = $record[
"pfstatus"];
751 $prodfourn->supplier_barcode = $record[
"barcode"];
752 $prodfourn->supplier_fk_barcode_type = $record[
"fk_barcode_type"];
755 if (
isModEnabled(
'dynamicprices') && !empty($prodfourn->fk_supplier_price_expression)) {
756 require_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
758 $price_result = $priceparser->parseProductSupplier($prodfourn);
759 if ($price_result >= 0) {
760 $prodfourn->fourn_price = $price_result;
761 $prodfourn->fourn_unitprice =
null;
765 if (!isset($prodfourn->fourn_unitprice)) {
766 if ($prodfourn->fourn_qty != 0) {
767 $prodfourn->fourn_unitprice =
price2num($prodfourn->fourn_price / $prodfourn->fourn_qty,
'MU');
769 $prodfourn->fourn_unitprice =
"";
773 $retarray[] = $prodfourn;
776 $this->db->free($resql);
779 $this->error = $this->db->error();
799 if (empty($prodid)) {
800 dol_syslog(
"Warning function find_min_price_product_fournisseur were called with prodid empty. May be a bug.", LOG_WARNING);
804 $this->product_fourn_price_id =
'';
805 $this->product_fourn_id =
'';
806 $this->fourn_ref =
'';
807 $this->fourn_price =
'';
808 $this->fourn_qty =
'';
809 $this->fourn_remise_percent =
'';
810 $this->fourn_remise =
'';
811 $this->fourn_unitprice =
'';
812 $this->fourn_id =
'';
813 $this->fourn_name =
'';
814 $this->delivery_time_days =
'';
817 $this->fourn_multicurrency_price =
'';
818 $this->fourn_multicurrency_unitprice =
'';
819 $this->fourn_multicurrency_tx =
'';
820 $this->fourn_multicurrency_id =
'';
821 $this->fourn_multicurrency_code =
'';
823 $sql =
"SELECT s.nom as supplier_name, s.rowid as fourn_id,";
824 $sql .=
" pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
825 $sql .=
" pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges,";
826 $sql .=
" pfp.remise, pfp.remise_percent, pfp.fk_supplier_price_expression, pfp.delivery_time_days";
827 $sql .=
" ,pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code";
828 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s, ".MAIN_DB_PREFIX.
"product_fournisseur_price as pfp";
829 $sql .=
" WHERE s.entity IN (".getEntity(
'societe').
")";
830 $sql .=
" AND pfp.entity IN (".getEntity(
'productsupplierprice').
")";
831 $sql .=
" AND pfp.fk_product = ".((int) $prodid);
832 $sql .=
" AND pfp.fk_soc = s.rowid";
833 $sql .=
" AND s.status = 1";
835 $sql .=
" AND pfp.quantity <= ".((float) $qty);
838 $sql .=
' AND pfp.fk_soc = '.((int) $socid);
841 dol_syslog(get_class($this).
"::find_min_price_product_fournisseur", LOG_DEBUG);
843 $resql = $this->db->query(
$sql);
845 $record_array = array();
848 while ($record = $this->db->fetch_array($resql)) {
849 $record_array[] = $record;
852 if (count($record_array) == 0) {
853 $this->db->free($resql);
857 foreach ($record_array as $record) {
858 $fourn_price = $record[
"price"];
860 $fourn_unitprice = $record[
"unitprice"];
861 $fourn_unitprice_with_discount = $record[
"unitprice"] * (1 - $record[
"remise_percent"] / 100);
863 if (
isModEnabled(
'dynamicprices') && !empty($record[
"fk_supplier_price_expression"])) {
865 $prod_supplier->product_fourn_price_id = $record[
"product_fourn_price_id"];
866 $prod_supplier->id = $prodid;
867 $prod_supplier->fourn_qty = $record[
"quantity"];
868 $prod_supplier->fourn_tva_tx = $record[
"tva_tx"];
869 $prod_supplier->fk_supplier_price_expression = $record[
"fk_supplier_price_expression"];
871 require_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
873 $price_result = $priceparser->parseProductSupplier($prod_supplier);
874 if ($price_result >= 0) {
875 $fourn_price =
price2num($price_result,
'MU');
876 if ($record[
"quantity"] != 0) {
877 $fourn_unitprice =
price2num($fourn_price / $record[
"quantity"],
'MU');
879 $fourn_unitprice = $fourn_price;
881 $fourn_unitprice_with_discount = $fourn_unitprice * (1 - $record[
"remise_percent"] / 100);
884 if ($fourn_unitprice < $min || $min == -1) {
885 $this->product_fourn_price_id = $record[
"product_fourn_price_id"];
886 $this->ref_supplier = $record[
"ref_fourn"];
887 $this->ref_fourn = $record[
"ref_fourn"];
888 $this->fourn_ref = $record[
"ref_fourn"];
889 $this->fourn_price = $fourn_price;
890 $this->fourn_qty = $record[
"quantity"];
891 $this->fourn_remise_percent = $record[
"remise_percent"];
892 $this->fourn_remise = $record[
"remise"];
893 $this->fourn_unitprice = $fourn_unitprice;
894 $this->fourn_unitprice_with_discount = $fourn_unitprice_with_discount;
895 $this->fourn_charges = $record[
"charges"];
896 $this->fourn_tva_tx = $record[
"tva_tx"];
897 $this->fourn_id = $record[
"fourn_id"];
898 $this->fourn_name = $record[
"supplier_name"];
899 $this->delivery_time_days = $record[
"delivery_time_days"];
900 $this->fk_supplier_price_expression = $record[
"fk_supplier_price_expression"];
902 $this->fourn_multicurrency_price = $record[
"multicurrency_price"];
903 $this->fourn_multicurrency_unitprice = $record[
"multicurrency_unitprice"];
904 $this->fourn_multicurrency_tx = $record[
"multicurrency_tx"];
905 $this->fourn_multicurrency_id = $record[
"fk_multicurrency"];
906 $this->fourn_multicurrency_code = $record[
"multicurrency_code"];
907 $min = $fourn_unitprice;
912 $this->db->free($resql);
915 $this->error = $this->db->error();
932 $expression_id = $expression_id != 0 ? $expression_id :
'NULL';
934 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"product_fournisseur_price";
935 $sql .=
" SET fk_supplier_price_expression = ".((int) $expression_id);
936 $sql .=
" WHERE rowid = ".((int) $this->product_fourn_price_id);
938 dol_syslog(get_class($this).
"::setSupplierPriceExpression", LOG_DEBUG);
940 $resql = $this->db->query(
$sql);
945 $this->error = $this->db->error().
" sql=".
$sql;
946 $this->db->rollback();
961 public function getSocNomUrl($withpicto = 0, $option =
'supplier', $maxlen = 0, $notooltip = 0)
964 $thirdparty->fetch($this->fourn_id);
966 return $thirdparty->getNomUrl($withpicto, $option, $maxlen, $notooltip);
984 global $conf, $langs;
987 $langs->load(
"suppliers");
988 if (count($productFournList) > 0) {
989 $out .=
'<table class="nobordernopadding" width="100%">';
990 $out .=
'<tr><td class="liste_titre right">'.($showunitprice ? $langs->trans(
"Price").
' '.$langs->trans(
"HT") :
'').
'</td>';
991 $out .=
'<td class="liste_titre right">'.($showunitprice ? $langs->trans(
"QtyMin") :
'').
'</td>';
992 $out .=
'<td class="liste_titre">'.$langs->trans(
"Supplier").
'</td>';
993 $out .=
'<td class="liste_titre">'.$langs->trans(
"SupplierRef").
'</td></tr>';
994 foreach ($productFournList as $productFourn) {
995 $out .=
'<tr><td class="right">'.($showunitprice ?
price($productFourn->fourn_unitprice * (1 - $productFourn->fourn_remise_percent / 100) - $productFourn->fourn_remise) :
'').
'</td>';
996 $out .=
'<td class="right">'.($showunitprice ? $productFourn->fourn_qty :
'').
'</td>';
997 $out .=
'<td>'.$productFourn->getSocNomUrl(1,
'supplier', $maxlen, $notooltip).
'</td>';
998 $out .=
'<td>'.$productFourn->fourn_ref.
'<td></tr>';
1002 $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>' :
'');
1003 $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;
1004 $out .= ($showunitprice ?
'<span class="opacitymedium">)</span>' :
'');
1020 'product_fournisseur_price'
1037 'product_fournisseur_price'
1056 $sql .=
" u.lastname,";
1057 $sql .=
" pfpl.rowid, pfp.ref_fourn as supplier_ref, pfpl.datec,";
1058 $sql .=
" pfpl.price, pfpl.quantity,";
1059 $sql .=
" pfpl.fk_multicurrency, pfpl.multicurrency_code, pfpl.multicurrency_tx, pfpl.multicurrency_price, pfpl.multicurrency_unitprice";
1060 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product_fournisseur_price_log as pfpl,";
1061 $sql .=
" ".MAIN_DB_PREFIX.
"product_fournisseur_price as pfp,";
1062 $sql .=
" ".MAIN_DB_PREFIX.
"user as u";
1063 $sql .=
" WHERE pfp.entity IN (".getEntity(
'productprice').
")";
1064 $sql .=
" AND pfpl.fk_user = u.rowid";
1065 $sql .=
" AND pfp.rowid = pfpl.fk_product_fournisseur";
1066 $sql .=
" AND pfpl.fk_product_fournisseur = ".((int) $product_fourn_price_id);
1067 if (empty($sortfield)) {
1068 $sql .=
" ORDER BY pfpl.datec";
1070 $sql .= $this->db->order($sortfield, $sortorder);
1072 $sql .= $this->db->plimit($limit, $offset);
1073 dol_syslog(get_class($this).
"::list_product_fournisseur_price_log", LOG_DEBUG);
1075 $resql = $this->db->query(
$sql);
1077 $retarray = array();
1079 while ($obj = $this->db->fetch_object($resql)) {
1080 $tmparray = array();
1081 $tmparray[
'rowid'] = $obj->rowid;
1082 $tmparray[
'supplier_ref'] = $obj->supplier_ref;
1083 $tmparray[
'datec'] = $this->db->jdate($obj->datec);
1084 $tmparray[
'lastname'] = $obj->lastname;
1085 $tmparray[
'price'] = $obj->price;
1086 $tmparray[
'quantity'] = $obj->quantity;
1087 $tmparray[
'fk_multicurrency'] = $obj->fk_multicurrency;
1088 $tmparray[
'multicurrency_code'] = $obj->multicurrency_code;
1089 $tmparray[
'multicurrency_tx'] = $obj->multicurrency_tx;
1090 $tmparray[
'multicurrency_price'] = $obj->multicurrency_price;
1091 $tmparray[
'multicurrency_unitprice'] = $obj->multicurrency_unitprice;
1093 $retarray[] = $tmparray;
1096 $this->db->free($resql);
1099 $this->error = $this->db->error();
1113 global $conf, $langs;
1116 $langs->load(
"suppliers");
1117 if (count($productFournLogList) > 0) {
1118 $out .=
'<table class="noborder centpercent">';
1119 $out .=
'<tr class="liste_titre"><td class="liste_titre">'.$langs->trans(
"Date").
'</td>';
1120 $out .=
'<td class="liste_titre right">'.$langs->trans(
"Price").
'</td>';
1122 $out .=
'<td class="liste_titre">'.$langs->trans(
"User").
'</td></tr>';
1123 foreach ($productFournLogList as $productFournLog) {
1124 $out .=
'<tr><td>'.dol_print_date($productFournLog[
'datec'],
'dayhour',
'tzuser').
'</td>';
1125 $out .=
'<td class="right">'.price($productFournLog[
'price'], 0, $langs, 1, -1, -1, $conf->currency);
1126 if ($productFournLog[
'multicurrency_code'] != $conf->currency) {
1127 $out .=
' ('.price($productFournLog[
'multicurrency_price'], 0, $langs, 1, -1, -1, $productFournLog[
'multicurrency_code']).
')';
1131 $out .=
'<td>'.$productFournLog[
'lastname'].
'</td></tr>';
1153 public function getNomUrl($withpicto = 0, $option =
'', $maxlength = 0, $save_lastsearch_value = -1, $notooltip = 0, $morecss =
'', $add_label = 0, $sep =
' - ')
1155 global $db, $conf, $langs, $hookmanager;
1157 if (!empty($conf->dol_no_mouse_hover)) {
1164 $newref = $this->ref;
1166 $newref =
dol_trunc($newref, $maxlength,
'middle');
1169 if (!empty($this->entity)) {
1170 $tmpphoto = $this->
show_photos(
'product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 80);
1171 if ($this->nbphoto > 0) {
1172 $label .=
'<div class="photointooltip">';
1173 $label .= $tmpphoto;
1174 $label .=
'</div><div style="clear: both;"></div>';
1179 $label .=
img_picto(
'',
'product').
' <u class="paddingrightonly">'.$langs->trans(
"Product").
'</u>';
1181 $label .=
img_picto(
'',
'service').
' <u class="paddingrightonly">'.$langs->trans(
"Service").
'</u>';
1183 if (isset($this->status) && isset($this->status_buy)) {
1184 $label .=
' '.$this->getLibStatut(5, 0);
1185 $label .=
' '.$this->getLibStatut(5, 1);
1188 if (!empty($this->
ref)) {
1189 $label .=
'<br><b>'.$langs->trans(
'ProductRef').
':</b> '.($this->
ref ? $this->
ref : $this->product_ref);
1191 if (!empty($this->label)) {
1192 $label .=
'<br><b>'.$langs->trans(
'ProductLabel').
':</b> '.$this->label;
1194 $label .=
'<br><b>'.$langs->trans(
'RefSupplier').
':</b> '.$this->ref_supplier;
1198 $langs->load(
"productbatch");
1199 $label .=
"<br><b>".$langs->trans(
"ManageLotSerial").
'</b>: '.$this->
getLibStatut(0, 2);
1203 $label .=
'<br><b>'.$langs->trans(
'BarCode').
':</b> '.$this->barcode;
1207 if ($this->weight) {
1208 $label .=
"<br><b>".$langs->trans(
"Weight").
'</b>: '.$this->weight.
' '.
measuringUnitString(0,
"weight", $this->weight_units);
1211 if ($this->length) {
1212 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Length").
'</b>: '.$this->length.
' '.
measuringUnitString(0,
'size', $this->length_units);
1215 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Width").
'</b>: '.$this->width.
' '.
measuringUnitString(0,
'size', $this->width_units);
1217 if ($this->height) {
1218 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Height").
'</b>: '.$this->height.
' '.
measuringUnitString(0,
'size', $this->height_units);
1221 $label .=
"<br>".$labelsize;
1224 $labelsurfacevolume =
"";
1225 if ($this->surface) {
1226 $labelsurfacevolume .= ($labelsurfacevolume ?
" - " :
"").
"<b>".$langs->trans(
"Surface").
'</b>: '.$this->surface.
' '.
measuringUnitString(0,
'surface', $this->surface_units);
1228 if ($this->volume) {
1229 $labelsurfacevolume .= ($labelsurfacevolume ?
" - " :
"").
"<b>".$langs->trans(
"Volume").
'</b>: '.$this->volume.
' '.
measuringUnitString(0,
'volume', $this->volume_units);
1231 if ($labelsurfacevolume) {
1232 $label .=
"<br>".$labelsurfacevolume;
1237 include_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
1238 $label .=
'<br><b>'.$langs->trans(
'ProductAccountancySellCode').
':</b> '.
length_accountg($this->accountancy_code_sell);
1239 $label .=
'<br><b>'.$langs->trans(
'ProductAccountancySellIntraCode').
':</b> '.
length_accountg($this->accountancy_code_sell_intra);
1240 $label .=
'<br><b>'.$langs->trans(
'ProductAccountancySellExportCode').
':</b> '.
length_accountg($this->accountancy_code_sell_export);
1243 include_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
1244 $label .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyCode').
':</b> '.
length_accountg($this->accountancy_code_buy);
1245 $label .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyIntraCode').
':</b> '.
length_accountg($this->accountancy_code_buy_intra);
1246 $label .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyExportCode').
':</b> '.
length_accountg($this->accountancy_code_buy_export);
1250 if (is_array($logPrices) && count($logPrices) > 0) {
1251 $label .=
'<br><br>';
1252 $label .=
'<u>'.$langs->trans(
"History").
'</u>';
1256 $url = DOL_URL_ROOT.
'/product/fournisseurs.php?id='.((int) $this->
id).
'&action=create_price&token='.
newToken().
'&socid='.((int) $this->fourn_id).
'&rowid='.((int) $this->product_fourn_price_id);
1258 if ($option !=
'nolink') {
1260 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1261 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1262 $add_save_lastsearch_values = 1;
1264 if ($add_save_lastsearch_values) {
1265 $url .=
'&save_lastsearch_values=1';
1270 if (empty($notooltip)) {
1271 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1272 $label = $langs->trans(
"SupplierRef");
1273 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1275 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1276 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1278 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1281 $linkstart =
'<a href="'.$url.
'"';
1282 $linkstart .= $linkclose.
'>';
1285 $result .= $linkstart;
1287 $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);
1289 if ($withpicto != 2) {
1290 $result .= $newref.($this->ref_supplier ?
' ('.$this->ref_supplier.
')' :
'');
1292 $result .= $linkend;
1293 if ($withpicto != 2) {
1294 $result .= (($add_label && $this->label) ? $sep.dol_trunc($this->label, ($add_label > 1 ? $add_label : 0)) :
'');
1298 $hookmanager->initHooks(array($this->element .
'dao'));
1299 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
1300 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1302 $result = $hookmanager->resPrint;
1304 $result .= $hookmanager->resPrint;
1318 return $this->
LibStatut($this->status, $mode);
1333 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1336 $this->labelStatus[self::STATUS_OPEN] = $langs->transnoentitiesnoconv(
'Enabled');
1337 $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv(
'Disabled');
1340 $statusType =
'status4';
1342 if ($status == self::STATUS_CANCELED) {
1343 $statusType =
'status6';
1346 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
1364 private function logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice =
null, $multicurrency_unitBuyPrice =
null, $multicurrency_tx =
null, $fk_multicurrency =
null, $multicurrency_code =
null)
1367 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"product_fournisseur_price_log(";
1368 $sql .=
" multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
1369 $sql .=
"datec, fk_product_fournisseur,fk_user,price,quantity)";
1371 $sql .= (isset($multicurrency_buyprice) ?
"'".$this->db->escape(
price2num($multicurrency_buyprice)).
"'" :
'null').
",";
1372 $sql .= (isset($multicurrency_unitBuyPrice) ?
"'".$this->db->escape(
price2num($multicurrency_unitBuyPrice)).
"'" :
'null').
",";
1373 $sql .= (isset($multicurrency_tx) ?
"'".$this->db->escape($multicurrency_tx).
"'" :
'1').
",";
1374 $sql .= (isset($fk_multicurrency) ?
"'".$this->db->escape($fk_multicurrency).
"'" :
'null').
",";
1375 $sql .= (isset($multicurrency_code) ?
"'".$this->db->escape($multicurrency_code).
"'" :
'null').
",";
1376 $sql .=
"'".$this->db->idate($datec).
"',";
1377 $sql .=
" ".((int) $this->product_fourn_price_id).
",";
1378 $sql .=
" ".$user->id.
",";
1379 $sql .=
" ".price2num($buyprice).
",";
1380 $sql .=
" ".price2num($qty);
1383 $resql = $this->db->query(
$sql);