Fix regression introduced with option SUPPLIERORDER_WITH_NOPRICEDEFINED

This commit is contained in:
Laurent Destailleur 2016-05-17 22:59:46 +02:00
parent 89d37ccc62
commit 4e46a01d51
4 changed files with 127 additions and 130 deletions

View File

@ -1126,7 +1126,7 @@ class CommandeFournisseur extends CommonOrder
// insert products details into database // insert products details into database
for ($i=0;$i<$num;$i++) for ($i=0;$i<$num;$i++)
{ {
$result = $this->addline( $result = $this->addline( // This include test on qty if option SUPPLIERORDER_WITH_NOPRICEDEFINED is not set
$this->lines[$i]->desc, $this->lines[$i]->desc,
$this->lines[$i]->subprice, $this->lines[$i]->subprice,
$this->lines[$i]->qty, $this->lines[$i]->qty,
@ -1310,7 +1310,7 @@ class CommandeFournisseur extends CommonOrder
$error = 0; $error = 0;
dol_syslog(get_class($this)."::addline $desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2. $fk_product, $fk_prod_fourn_price, $fourn_ref, $remise_percent, $price_base_type, $pu_ttc, $type, $fk_unit"); dol_syslog(get_class($this)."::addline $desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $fk_prod_fourn_price, $fourn_ref, $remise_percent, $price_base_type, $pu_ttc, $type, $fk_unit");
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
// Clean parameters // Clean parameters
@ -1350,12 +1350,16 @@ class CommandeFournisseur extends CommonOrder
{ {
$this->db->begin(); $this->db->begin();
if ($fk_prod_fourn_price > 0) if ($fk_product > 0)
{ {
if (empty($conf->global->SUPPLIERORDER_WITH_NOPRICEDEFINED))
{
// Check quantity is enough
dol_syslog(get_class($this)."::addline we check supplier prices fk_product=".$fk_product." fk_prod_fourn_price=".$fk_prod_fourn_price." qty=".$qty." fourn_ref=".$fourn_ref);
$prod = new Product($this->db, $fk_product); $prod = new Product($this->db, $fk_product);
if ($prod->fetch($fk_product) > 0) if ($prod->fetch($fk_product) > 0)
{ {
$result=$prod->get_buyprice($fk_prod_fourn_price,$qty,$fk_product,$fourn_ref); $result=$prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product, $fourn_ref); // Search on couple $fk_prod_fourn_price/$qty first, then on triplet $qty/$fk_product/$fourn_ref
if ($result > 0) if ($result > 0)
{ {
$label = $prod->libelle; $label = $prod->libelle;
@ -1385,6 +1389,7 @@ class CommandeFournisseur extends CommonOrder
return -1; return -1;
} }
} }
}
else else
{ {
$product_type = $type; $product_type = $type;
@ -1449,7 +1454,6 @@ class CommandeFournisseur extends CommonOrder
$sql.= ", ".$multicurrency_total_ttc; $sql.= ", ".$multicurrency_total_ttc;
$sql.= ")"; $sql.= ")";
dol_syslog(get_class($this)."::addline", LOG_DEBUG);
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
//print $sql; //print $sql;
if ($resql) if ($resql)

View File

@ -216,7 +216,7 @@ class ProductFournisseur extends Product
$this->db->begin(); $this->db->begin();
if ($this->product_fourn_price_id) if ($this->product_fourn_price_id > 0)
{ {
$sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price"; $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
$sql.= " SET fk_user = " . $user->id." ,"; $sql.= " SET fk_user = " . $user->id." ,";
@ -237,7 +237,7 @@ class ProductFournisseur extends Product
$sql.= " WHERE rowid = ".$this->product_fourn_price_id; $sql.= " WHERE rowid = ".$this->product_fourn_price_id;
// TODO Add price_base_type and price_ttc // TODO Add price_base_type and price_ttc
dol_syslog(get_class($this).'::update_buyprice', LOG_DEBUG); dol_syslog(get_class($this).'::update_buyprice update knowing id of line = product_fourn_price_id = '.$this->product_fourn_price_id, LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql)
{ {
@ -249,7 +249,7 @@ class ProductFournisseur extends Product
if (empty($error)) if (empty($error))
{ {
$this->db->commit(); $this->db->commit();
return 0; return $this->product_fourn_price_id;
} }
else else
{ {
@ -267,13 +267,13 @@ class ProductFournisseur extends Product
else else
{ {
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);
// Delete price for this quantity // Delete price for this quantity
$sql = "DELETE FROM " . MAIN_DB_PREFIX . "product_fournisseur_price"; $sql = "DELETE FROM " . MAIN_DB_PREFIX . "product_fournisseur_price";
$sql .= " WHERE fk_soc = " . $fourn->id . " AND ref_fourn = '" . $this->db->escape($ref_fourn) . "' AND quantity = " . $qty . " AND entity = " . $conf->entity; $sql .= " WHERE fk_soc = " . $fourn->id . " AND ref_fourn = '" . $this->db->escape($ref_fourn) . "' AND quantity = " . $qty . " AND entity = " . $conf->entity;
dol_syslog(get_class($this).'::update_buyprice', LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql) {
{
// Add price for this quantity to supplier // Add price for this quantity to supplier
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_fournisseur_price("; $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_fournisseur_price(";
$sql .= "datec, fk_product, fk_soc, ref_fourn, fk_user, price, quantity, remise_percent, remise, unitprice, tva_tx, charges, unitcharges, fk_availability, info_bits, entity, delivery_time_days,supplier_reputation)"; $sql .= "datec, fk_product, fk_soc, ref_fourn, fk_user, price, quantity, remise_percent, remise, unitprice, tva_tx, charges, unitcharges, fk_availability, info_bits, entity, delivery_time_days,supplier_reputation)";
@ -297,14 +297,17 @@ class ProductFournisseur extends Product
$sql .= (empty($supplier_reputation) ? 'NULL' : "'" . $this->db->escape($supplier_reputation) . "'"); $sql .= (empty($supplier_reputation) ? 'NULL' : "'" . $this->db->escape($supplier_reputation) . "'");
$sql .= ")"; $sql .= ")";
dol_syslog(get_class($this)."::update_buyprice", LOG_DEBUG); $idinserted = 0;
if (! $this->db->query($sql))
{ $resql = $this->db->query($sql);
if ($resql) {
$idinserted = $this->db->last_insert_id(MAIN_DB_PREFIX . "product_fournisseur_price");
}
else {
$error++; $error++;
} }
if (! $error && !empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) if (! $error && ! empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
{
// Add record into log table // Add record into log table
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_fournisseur_price_log("; $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_fournisseur_price_log(";
$sql .= "datec, fk_product_fournisseur,fk_user,price,quantity)"; $sql .= "datec, fk_product_fournisseur,fk_user,price,quantity)";
@ -316,41 +319,32 @@ class ProductFournisseur extends Product
$sql .= ")"; $sql .= ")";
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if (! $resql) if (! $resql) {
{
$error++; $error++;
} }
} }
if (! $error) {
if (! $error)
{
// Call trigger // Call trigger
$result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_CREATE', $user); $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_CREATE', $user);
if ($result < 0) $error++; if ($result < 0)
$error++;
// End call triggers // End call triggers
if (empty($error)) if (empty($error)) {
{
$this->db->commit(); $this->db->commit();
return 0; return $idinserted;
} } else {
else
{
$this->db->rollback(); $this->db->rollback();
return -1; return -1;
} }
} } else {
else $this->error = $this->db->lasterror() . " sql=" . $sql;
{
$this->error=$this->db->error()." sql=".$sql;
$this->db->rollback(); $this->db->rollback();
return -2; return -2;
} }
} } else {
else $this->error = $this->db->lasterror() . " sql=" . $sql;
{
$this->error=$this->db->error()." sql=".$sql;
$this->db->rollback(); $this->db->rollback();
return - 1; return - 1;
} }

View File

@ -1315,21 +1315,21 @@ class Product extends CommonObject
* @param double $qty Quantity asked * @param double $qty Quantity asked
* @param int $product_id Filter on a particular product id * @param int $product_id Filter on a particular product id
* @param string $fourn_ref Filter on a supplier ref * @param string $fourn_ref Filter on a supplier ref
* @return int <-1 if KO, -1 if qty not enough, 0 si ok mais rien trouve, id_product si ok et trouve. May also initialize some properties like (->ref_supplier, buyprice, fourn_pu, vatrate_supplier...) * @return int <-1 if KO, -1 if qty not enough, 0 if OK but nothing found, id_product if OK and found. May also initialize some properties like (->ref_supplier, buyprice, fourn_pu, vatrate_supplier...)
*/ */
function get_buyprice($prodfournprice,$qty,$product_id=0,$fourn_ref=0) function get_buyprice($prodfournprice,$qty,$product_id=0,$fourn_ref=0)
{ {
global $conf; global $conf;
$result = 0; $result = 0;
// We do select by searching with qty and prodfournprice // We do a first seach with a select by searching with couple prodfournprice and qty only (later we will search on triplet qty/product_id/fourn_ref)
$sql = "SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity,"; $sql = "SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity,";
$sql.= " pfp.fk_product, pfp.ref_fourn, pfp.fk_soc, pfp.tva_tx, pfp.fk_supplier_price_expression"; $sql.= " pfp.fk_product, pfp.ref_fourn, pfp.fk_soc, pfp.tva_tx, pfp.fk_supplier_price_expression";
$sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
$sql.= " WHERE pfp.rowid = ".$prodfournprice; $sql.= " WHERE pfp.rowid = ".$prodfournprice;
if ($qty) $sql.= " AND pfp.quantity <= ".$qty; if ($qty) $sql.= " AND pfp.quantity <= ".$qty;
dol_syslog(get_class($this)."::get_buyprice", LOG_DEBUG); dol_syslog(get_class($this)."::get_buyprice first search by prodfournprice/qty", LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql)
{ {
@ -1358,9 +1358,9 @@ class Product extends CommonObject
$result=$obj->fk_product; $result=$obj->fk_product;
return $result; return $result;
} }
else else // If not found
{ {
// We do same select again but searching with qty, ref and id product // We do a second search by doing a select again but searching with qty, ref and id product
$sql = "SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, pfp.fk_soc,"; $sql = "SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, pfp.fk_soc,";
$sql.= " pfp.fk_product, pfp.ref_fourn as ref_supplier, pfp.tva_tx, pfp.fk_supplier_price_expression"; $sql.= " pfp.fk_product, pfp.ref_fourn as ref_supplier, pfp.tva_tx, pfp.fk_supplier_price_expression";
$sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
@ -1370,7 +1370,7 @@ class Product extends CommonObject
$sql.= " ORDER BY pfp.quantity DESC"; $sql.= " ORDER BY pfp.quantity DESC";
$sql.= " LIMIT 1"; $sql.= " LIMIT 1";
dol_syslog(get_class($this)."::get_buyprice", LOG_DEBUG); dol_syslog(get_class($this)."::get_buyprice second search from qty/ref/product_id", LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql)
{ {
@ -2656,6 +2656,8 @@ class Product extends CommonObject
$now=dol_now(); $now=dol_now();
dol_syslog(get_class($this)."::add_fournisseur id_fourn = ".$id_fourn." ref_fourn=".$ref_fourn." quantity=".$quantity, LOG_DEBUG);
if ($ref_fourn) if ($ref_fourn)
{ {
$sql = "SELECT rowid, fk_product"; $sql = "SELECT rowid, fk_product";
@ -2665,7 +2667,6 @@ class Product extends CommonObject
$sql.= " AND fk_product != ".$this->id; $sql.= " AND fk_product != ".$this->id;
$sql.= " AND entity = ".$conf->entity; $sql.= " AND entity = ".$conf->entity;
dol_syslog(get_class($this)."::add_fournisseur", LOG_DEBUG);
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
if ($resql) if ($resql)
{ {
@ -2689,7 +2690,6 @@ class Product extends CommonObject
$sql.= " AND fk_product = ".$this->id; $sql.= " AND fk_product = ".$this->id;
$sql.= " AND entity = ".$conf->entity; $sql.= " AND entity = ".$conf->entity;
dol_syslog(get_class($this)."::add_fournisseur", LOG_DEBUG);
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
if ($resql) if ($resql)
{ {
@ -2718,7 +2718,6 @@ class Product extends CommonObject
$sql.= ", 0"; $sql.= ", 0";
$sql.= ")"; $sql.= ")";
dol_syslog(get_class($this)."::add_fournisseur", LOG_DEBUG);
if ($this->db->query($sql)) if ($this->db->query($sql))
{ {
$this->product_fourn_price_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur_price"); $this->product_fourn_price_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur_price");

View File

@ -145,7 +145,7 @@ class CommandeFournisseurTest extends PHPUnit_Framework_TestCase
// Create supplier price // Create supplier price
$result=$product->add_fournisseur($user, $societe->id, $ref_fourn, $quantity); // This insert record with no value for price. Values are update later with update_buyprice $result=$product->add_fournisseur($user, $societe->id, $ref_fourn, $quantity); // This insert record with no value for price. Values are update later with update_buyprice
$this->assertGreaterThanOrEqual(1, $result); $this->assertGreaterThanOrEqual(1, $result);
$result=$product->update_buyprice($quantity, 10, $user, 'HT', $societe, '', $ref_fourn, $tva_tx, 0, 0); $result=$product->update_buyprice($quantity, 20, $user, 'HT', $societe, '', $ref_fourn, $tva_tx, 0, 0);
$this->assertGreaterThanOrEqual(0, $result); $this->assertGreaterThanOrEqual(0, $result);
// Create supplier order with a too low quantity // Create supplier order with a too low quantity