diff --git a/htdocs/core/lib/price.lib.php b/htdocs/core/lib/price.lib.php index eb332b44611..de5379f46ec 100644 --- a/htdocs/core/lib/price.lib.php +++ b/htdocs/core/lib/price.lib.php @@ -155,11 +155,22 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocalt } // pu calculation from pu_devise if pu empty - if(empty($pu) && !empty($pu_devise)) { - $pu = $pu_devise / $multicurrency_tx; + if (empty($pu) && !empty($pu_devise)) { + if (! empty($multicurrency_tx)) $pu = $pu_devise / $multicurrency_tx; + else + { + dol_syslog('Price.lib::calcul_price_total function called with bad parameters combination (multicurrency_tx empty when pu_devise not) ', LOG_ERR); + return array(); + } } - if(empty($pu_devise) && !empty($multicurrency_tx)) { - $pu_devise = $pu * $multicurrency_tx; + // pu_devise calculation from pu + if (empty($pu_devise) && !empty($multicurrency_tx)) { + if (is_numeric($pu) && is_numeric($multicurrency_tx)) $pu_devise = $pu * $multicurrency_tx; + else + { + dol_syslog('Price.lib::calcul_price_total function called with bad parameters combination (pu or multicurrency_tx are not numeric)', LOG_ERR); + return array(); + } } // initialize total (may be HT or TTC depending on price_base_type) diff --git a/htdocs/fourn/class/fournisseur.commande.class.php b/htdocs/fourn/class/fournisseur.commande.class.php index d2511d2125c..0ae72b8f96d 100644 --- a/htdocs/fourn/class/fournisseur.commande.class.php +++ b/htdocs/fourn/class/fournisseur.commande.class.php @@ -1445,7 +1445,7 @@ class CommandeFournisseur extends CommonOrder $error = 0; - dol_syslog(get_class($this)."::addline $desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $fk_prod_fourn_price, $ref_supplier, $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, $ref_supplier, $remise_percent, $price_base_type, $pu_ttc, $type, $info_bits $notrigger $date_start $date_end $fk_unit $pu_ht_devise $origin $origin_id"); include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; // Clean parameters @@ -1500,6 +1500,7 @@ class CommandeFournisseur extends CommonOrder // We use 'none' instead of $ref_supplier, because fourn_ref may not exists anymore. So we will take the first supplier price ok. // If we want a dedicated supplier price, we must provide $fk_prod_fourn_price. $result=$prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product, 'none', ($this->fk_soc?$this->fk_soc:$this->socid)); // Search on couple $fk_prod_fourn_price/$qty first, then on triplet $qty/$fk_product/$ref_supplier/$this->fk_soc + if ($result > 0) { $pu = $prod->fourn_pu; // Unit price supplier price set by get_buyprice diff --git a/test/phpunit/CommandeFournisseurTest.php b/test/phpunit/CommandeFournisseurTest.php index 33271a07dcc..7b2f3f07e2b 100644 --- a/test/phpunit/CommandeFournisseurTest.php +++ b/test/phpunit/CommandeFournisseurTest.php @@ -142,26 +142,32 @@ class CommandeFournisseurTest extends PHPUnit_Framework_TestCase $ref_fourn='SUPPLIER_REF_PHPUNIT'; $tva_tx=19.6; - // Create supplier price + // Delete existing supplier prices + // TODO + + // Create 1 supplier price with min qty = 10; $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); $result=$product->update_buyprice($quantity, 20, $user, 'HT', $societe, '', $ref_fourn, $tva_tx, 0, 0); $this->assertGreaterThanOrEqual(0, $result); - // Create supplier order with a too low quantity + // Create supplier order with a too low quantity and option SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY is on + $conf->global->SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY = 1; + $localobject=new CommandeFournisseur($db); $localobject->initAsSpecimen(); $localobject->lines=array(); // Overwrite lines of order $line=new CommandeFournisseurLigne($db); - $line->desc=$langs->trans("Description")." specimen line too low"; + $line->desc=$langs->trans("Description")." specimen line with qty too low"; $line->qty=1; // So lower than $quantity + $line->subprice=100; $line->fk_product=$product->id; $line->ref_fourn=$ref_fourn; $localobject->lines[]=$line; $result=$localobject->create($user); print __METHOD__." result=".$result."\n"; - $this->assertEquals(-1, $result); // must be -1 because quantity is lower than minimum of supplier price + $this->assertEquals(-1, $result, 'Creation of too low quantity'); // must be -1 because quantity is lower than minimum of supplier price $sql="DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseur where ref=''"; $db->query($sql); @@ -173,6 +179,7 @@ class CommandeFournisseurTest extends PHPUnit_Framework_TestCase $line=new CommandeFournisseurLigne($db); $line->desc=$langs->trans("Description")." specimen line ok"; $line->qty=10; // So enough quantity + $line->subprice=100; $line->fk_product=$product->id; $line->ref_fourn=$ref_fourn; $localobject2->lines[]=$line; @@ -181,6 +188,45 @@ class CommandeFournisseurTest extends PHPUnit_Framework_TestCase print __METHOD__." result=".$result."\n"; $this->assertGreaterThan(0, $result); + + // Create supplier order with a too low quantity but option SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY is off + $conf->global->SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY = 0; + + $localobject3=new CommandeFournisseur($db); + $localobject3->initAsSpecimen(); + $localobject3->lines=array(); // Overwrite lines of order + $line=new CommandeFournisseurLigne($db); + $line->desc=$langs->trans("Description")." specimen line with qty too low"; + $line->qty=1; // So lower than $quantity + $line->subprice=100; + $line->fk_product=$product->id; + $line->ref_fourn=$ref_fourn; + $localobject3->lines[]=$line; + + $result=$localobject3->create($user); + print __METHOD__." result=".$result."\n"; + $this->assertGreaterThan(0, $result, 'Creation of too low quantity should be ok'); // must be id of line because there is no test on minimum quantity + + $sql="DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseur where ref=''"; + $db->query($sql); + + // Create supplier order + $localobject4=new CommandeFournisseur($db); + $localobject4->initAsSpecimen(); // This create 5 lines of first product found for socid 1 + $localobject4->lines=array(); // Overwrite lines of order + $line=new CommandeFournisseurLigne($db); + $line->desc=$langs->trans("Description")." specimen line ok"; + $line->qty=10; // So enough quantity + $line->subprice=100; + $line->fk_product=$product->id; + $line->ref_fourn=$ref_fourn; + $localobject4->lines[]=$line; + + $result=$localobject4->create($user); + print __METHOD__." result=".$result."\n"; + $this->assertGreaterThan(0, $result); + + return $result; }