From 7981bd08b7c511db2cf2028590ceeefa17813a4b Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 16 Jun 2018 14:47:33 +0200 Subject: [PATCH] Fix price+sql error when adding product when no supplier price defined --- htdocs/fourn/commande/card.php | 31 +++++++++++++++------- htdocs/fourn/facture/card.php | 36 +++++++++++++++++++------- htdocs/product/class/product.class.php | 10 +++---- htdocs/supplier_proposal/card.php | 29 +++++++++++++++------ 4 files changed, 73 insertions(+), 33 deletions(-) diff --git a/htdocs/fourn/commande/card.php b/htdocs/fourn/commande/card.php index 9b635f05418..6a073fb305d 100644 --- a/htdocs/fourn/commande/card.php +++ b/htdocs/fourn/commande/card.php @@ -365,20 +365,33 @@ if (empty($reshook)) $productsupplier = new ProductFournisseur($db); $idprod=0; - if (GETPOST('idprodfournprice') == -1 || GETPOST('idprodfournprice') == '') $idprod=-99; // Same behaviour than with combolist. When not select idprodfournprice is now -99 (to avoid conflict with next action that may return -1, -2, ...) - if (preg_match('/^idprod_([0-9]+)$/', GETPOST('idprodfournprice'), $reg)) + if (GETPOST('idprodfournprice','alpha') == -1 || GETPOST('idprodfournprice','alpha') == '') $idprod=-99; // Same behaviour than with combolist. When not select idprodfournprice is now -99 (to avoid conflict with next action that may return -1, -2, ...) + if (preg_match('/^idprod_([0-9]+)$/', GETPOST('idprodfournprice','alpha'), $reg)) { $idprod=$reg[1]; - $res=$productsupplier->fetch($idprod); - // Call to init properties of $productsupplier + $res=$productsupplier->fetch($idprod); // Load product from its ID + // Call to init some price properties of $productsupplier // So if a supplier price already exists for another thirdparty (first one found), we use it as reference price - $productsupplier->get_buyprice(0, -1, $idprod, 'none'); // We force qty to -1 to be sure to find if a supplier price exist + if (! empty($conf->global->SUPPLIER_TAKE_FIRST_PRICE_IF_NO_PRICE_FOR_CURRENT_SUPPLIER)) + { + $fksoctosearch = 0; + $productsupplier->get_buyprice(0, -1, $idprod, 'none', $fksoctosearch); // We force qty to -1 to be sure to find if a supplier price exist + if ($productsupplier->fourn_socid != $socid) // The price we found is for another supplier, so we clear supplier price + { + $productsupplier->ref_supplier = ''; + } + } + else + { + $fksoctosearch = $object->thirdparty->id; + $productsupplier->get_buyprice(0, -1, $idprod, 'none', $fksoctosearch); // We force qty to -1 to be sure to find if a supplier price exist + } } - elseif (GETPOST('idprodfournprice') > 0) + elseif (GETPOST('idprodfournprice','alpha') > 0) { $qtytosearch=$qty; // Just to see if a price exists for the quantity. Not used to found vat. //$qtytosearch=-1; // We force qty to -1 to be sure to find if a supplier price exist - $idprod=$productsupplier->get_buyprice(GETPOST('idprodfournprice'), $qtytosearch); + $idprod=$productsupplier->get_buyprice(GETPOST('idprodfournprice','alpha'), $qtytosearch); $res=$productsupplier->fetch($idprod); } @@ -394,8 +407,8 @@ if (empty($reshook)) $ref_supplier = $productsupplier->ref_supplier; - $tva_tx = get_default_tva($object->thirdparty, $mysoc, $productsupplier->id, GETPOST('idprodfournprice')); - $tva_npr = get_default_npr($object->thirdparty, $mysoc, $productsupplier->id, GETPOST('idprodfournprice')); + $tva_tx = get_default_tva($object->thirdparty, $mysoc, $productsupplier->id, GETPOST('idprodfournprice','alpha')); + $tva_npr = get_default_npr($object->thirdparty, $mysoc, $productsupplier->id, GETPOST('idprodfournprice','alpha')); if (empty($tva_tx)) $tva_npr=0; $localtax1_tx= get_localtax($tva_tx, 1, $mysoc, $object->thirdparty, $tva_npr); $localtax2_tx= get_localtax($tva_tx, 2, $mysoc, $object->thirdparty, $tva_npr); diff --git a/htdocs/fourn/facture/card.php b/htdocs/fourn/facture/card.php index 2a0d212788f..f171aa272ff 100644 --- a/htdocs/fourn/facture/card.php +++ b/htdocs/fourn/facture/card.php @@ -1172,21 +1172,34 @@ if (empty($reshook)) $productsupplier=new ProductFournisseur($db); $idprod=0; - if (GETPOST('idprodfournprice') == -1 || GETPOST('idprodfournprice') == '') $idprod=-99; // Same behaviour than with combolist. When not select idprodfournprice is now -99 (to avoid conflict with next action that may return -1, -2, ...) + if (GETPOST('idprodfournprice','alpha') == -1 || GETPOST('idprodfournprice','alpha') == '') $idprod=-99; // Same behaviour than with combolist. When not select idprodfournprice is now -99 (to avoid conflict with next action that may return -1, -2, ...) - if (preg_match('/^idprod_([0-9]+)$/', GETPOST('idprodfournprice'), $reg)) + if (preg_match('/^idprod_([0-9]+)$/', GETPOST('idprodfournprice','alpha'), $reg)) { $idprod=$reg[1]; - $res=$productsupplier->fetch($idprod); - // Call to init properties of $productsupplier + $res=$productsupplier->fetch($idprod); // Load product from its id + // Call to init some price properties of $productsupplier // So if a supplier price already exists for another thirdparty (first one found), we use it as reference price - $productsupplier->get_buyprice(0, -1, $idprod, 'none'); // We force qty to -1 to be sure to find if a supplier price exist + if (! empty($conf->global->SUPPLIER_TAKE_FIRST_PRICE_IF_NO_PRICE_FOR_CURRENT_SUPPLIER)) + { + $fksoctosearch = 0; + $productsupplier->get_buyprice(0, -1, $idprod, 'none', $fksoctosearch); // We force qty to -1 to be sure to find if a supplier price exist + if ($productsupplier->fourn_socid != $socid) // The price we found is for another supplier, so we clear supplier price + { + $productsupplier->ref_supplier = ''; + } + } + else + { + $fksoctosearch = $object->thirdparty->id; + $productsupplier->get_buyprice(0, -1, $idprod, 'none', $fksoctosearch); // We force qty to -1 to be sure to find if a supplier price exist + } } - elseif (GETPOST('idprodfournprice') > 0) + elseif (GETPOST('idprodfournprice','alpha') > 0) { $qtytosearch=$qty; // Just to see if a price exists for the quantity. Not used to found vat. //$qtytosearch=-1; // We force qty to -1 to be sure to find if a supplier price exist - $idprod=$productsupplier->get_buyprice(GETPOST('idprodfournprice'), $qtytosearch); + $idprod=$productsupplier->get_buyprice(GETPOST('idprodfournprice','alpha'), $qtytosearch); $res=$productsupplier->fetch($idprod); } @@ -1202,15 +1215,18 @@ if (empty($reshook)) $ref_supplier = $productsupplier->ref_supplier; - $tva_tx=get_default_tva($object->thirdparty, $mysoc, $productsupplier->id, GETPOST('idprodfournprice')); - $tva_npr = get_default_npr($object->thirdparty, $mysoc, $productsupplier->id, GETPOST('idprodfournprice')); + $tva_tx=get_default_tva($object->thirdparty, $mysoc, $productsupplier->id, GETPOST('idprodfournprice','alpha')); + $tva_npr = get_default_npr($object->thirdparty, $mysoc, $productsupplier->id, GETPOST('idprodfournprice','alpha')); if (empty($tva_tx)) $tva_npr=0; $localtax1_tx= get_localtax($tva_tx, 1, $mysoc, $object->thirdparty, $tva_npr); $localtax2_tx= get_localtax($tva_tx, 2, $mysoc, $object->thirdparty, $tva_npr); + $pu = $productsupplier->fourn_pu; + if (empty($pu)) $pu = 0; // If pu is '' or null, we force to have a numeric value + $result=$object->addline( $desc, - $productsupplier->fourn_pu, + $pu, $tva_tx, $localtax1_tx, $localtax2_tx, diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index ae2d3a4e8b1..7d7cbf7c282 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -275,14 +275,10 @@ class Product extends CommonObject public $fk_price_expression; - /** - * @deprecated - * @see fourn_pu - */ - public $buyprice; + /* To store supplier price found */ public $fourn_pu; - public $fourn_price_base_type; + public $fourn_socid; /** * @deprecated @@ -1544,6 +1540,7 @@ class Product extends CommonObject $this->buyprice = $obj->price; // deprecated $this->fourn_pu = $obj->price / $obj->quantity; // Unit price of product of supplier $this->fourn_price_base_type = 'HT'; // Price base type + $this->fourn_socid = $obj->fk_soc; // Company that offer this price $this->ref_fourn = $obj->ref_fourn; // deprecated $this->ref_supplier = $obj->ref_fourn; // Ref supplier $this->remise_percent = $obj->remise_percent; // remise percent if present and not typed @@ -1599,6 +1596,7 @@ class Product extends CommonObject $this->fourn_qty = $obj->quantity; // min quantity for price for a virtual supplier $this->fourn_pu = $obj->price / $obj->quantity; // Unit price of product for a virtual supplier $this->fourn_price_base_type = 'HT'; // Price base type for a virtual supplier + $this->fourn_socid = $obj->fk_soc; // Company that offer this price $this->ref_fourn = $obj->ref_supplier; // deprecated $this->ref_supplier = $obj->ref_supplier; // Ref supplier $this->remise_percent = $obj->remise_percent; // remise percent if present and not typed diff --git a/htdocs/supplier_proposal/card.php b/htdocs/supplier_proposal/card.php index 113ef3db88c..e3e91b6c38f 100644 --- a/htdocs/supplier_proposal/card.php +++ b/htdocs/supplier_proposal/card.php @@ -589,21 +589,34 @@ if (empty($reshook)) $productsupplier = new ProductFournisseur($db); $idprod=0; - if (GETPOST('idprodfournprice') == -1 || GETPOST('idprodfournprice') == '') $idprod=-99; // Same behaviour than with combolist. When not select idprodfournprice is now -99 (to avoid conflict with next action that may return -1, -2, ...) + if (GETPOST('idprodfournprice','alpha') == -1 || GETPOST('idprodfournprice','alpha') == '') $idprod=-99; // Same behaviour than with combolist. When not select idprodfournprice is now -99 (to avoid conflict with next action that may return -1, -2, ...) - if (preg_match('/^idprod_([0-9]+)$/', GETPOST('idprodfournprice'), $reg)) + if (preg_match('/^idprod_([0-9]+)$/', GETPOST('idprodfournprice','alpha'), $reg)) { $idprod=$reg[1]; $res=$productsupplier->fetch($idprod); - // Call to init properties of $productsupplier + // Call to init some price properties of $productsupplier // So if a supplier price already exists for another thirdparty (first one found), we use it as reference price - $productsupplier->get_buyprice(0, -1, $idprod, 'none'); // We force qty to -1 to be sure to find if a supplier price exist + if (! empty($conf->global->SUPPLIER_TAKE_FIRST_PRICE_IF_NO_PRICE_FOR_CURRENT_SUPPLIER)) + { + $fksoctosearch = 0; + $productsupplier->get_buyprice(0, -1, $idprod, 'none', $fksoctosearch); // We force qty to -1 to be sure to find if a supplier price exist + if ($productsupplier->fourn_socid != $socid) // The price we found is for another supplier, so we clear supplier price + { + $productsupplier->ref_supplier = ''; + } + } + else + { + $fksoctosearch = $object->thirdparty->id; + $productsupplier->get_buyprice(0, -1, $idprod, 'none', $fksoctosearch); // We force qty to -1 to be sure to find if a supplier price exist + } } - elseif (GETPOST('idprodfournprice') > 0) + elseif (GETPOST('idprodfournprice','alpha') > 0) { //$qtytosearch=$qty; // Just to see if a price exists for the quantity. Not used to found vat. $qtytosearch=-1; // We force qty to -1 to be sure to find if a supplier price exist - $idprod=$productsupplier->get_buyprice(GETPOST('idprodfournprice'), $qtytosearch); + $idprod=$productsupplier->get_buyprice(GETPOST('idprodfournprice','alpha'), $qtytosearch); $res=$productsupplier->fetch($idprod); } @@ -621,8 +634,8 @@ if (empty($reshook)) $ref_supplier = $productsupplier->ref_supplier; - $tva_tx = get_default_tva($object->thirdparty, $mysoc, $productsupplier->id, GETPOST('idprodfournprice')); - $tva_npr = get_default_npr($object->thirdparty, $mysoc, $productsupplier->id, GETPOST('idprodfournprice')); + $tva_tx = get_default_tva($object->thirdparty, $mysoc, $productsupplier->id, GETPOST('idprodfournprice','alpha')); + $tva_npr = get_default_npr($object->thirdparty, $mysoc, $productsupplier->id, GETPOST('idprodfournprice','alpha')); if (empty($tva_tx)) $tva_npr=0; $localtax1_tx= get_localtax($tva_tx, 1, $mysoc, $object->thirdparty, $tva_npr); $localtax2_tx= get_localtax($tva_tx, 2, $mysoc, $object->thirdparty, $tva_npr);