Fix price+sql error when adding product when no supplier price defined

This commit is contained in:
Laurent Destailleur 2018-06-16 14:47:33 +02:00
parent 4898f10fa2
commit 7981bd08b7
4 changed files with 73 additions and 33 deletions

View File

@ -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);

View File

@ -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,

View File

@ -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

View File

@ -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);