diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index e1b25b9e3b7..a6d676a0f7b 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -4893,7 +4893,7 @@ class Product extends CommonObject $sql .= " WHERE w.entity IN (".getEntity('stock').")"; $sql .= " AND w.rowid = ps.fk_entrepot"; $sql .= " AND ps.fk_product = ".$this->id; - if ($conf->global->ENTREPOT_EXTRA_STATUS && count($warehouseStatus)) { + if (!empty($conf->global->ENTREPOT_EXTRA_STATUS) && count($warehouseStatus)) { $sql .= " AND w.statut IN (".$this->db->sanitize($this->db->escape(implode(',', $warehouseStatus))).")"; } diff --git a/htdocs/product/class/productbatch.class.php b/htdocs/product/class/productbatch.class.php index f5a91ff2839..a97437f6a45 100644 --- a/htdocs/product/class/productbatch.class.php +++ b/htdocs/product/class/productbatch.class.php @@ -89,19 +89,18 @@ class Productbatch extends CommonObject // Insert request $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_batch ("; $sql .= "fk_product_stock,"; - $sql .= "sellby,"; - $sql .= "eatby,"; + $sql .= "sellby,"; // no more used + $sql .= "eatby,"; // no more used $sql .= "batch,"; $sql .= "qty,"; $sql .= "import_key"; $sql .= ") VALUES ("; $sql .= " ".(!isset($this->fk_product_stock) ? 'NULL' : $this->fk_product_stock).","; - $sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").","; - $sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").","; + $sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").","; // no more used + $sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").","; // no more used $sql .= " ".(!isset($this->batch) ? 'NULL' : "'".$this->db->escape($this->batch)."'").","; $sql .= " ".(!isset($this->qty) ? 'NULL' : $this->qty).","; $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'").""; - $sql .= ")"; $this->db->begin(); diff --git a/htdocs/product/stock/class/mouvementstock.class.php b/htdocs/product/stock/class/mouvementstock.class.php index 631cedd0a25..a23846c3e08 100644 --- a/htdocs/product/stock/class/mouvementstock.class.php +++ b/htdocs/product/stock/class/mouvementstock.class.php @@ -140,8 +140,8 @@ class MouvementStock extends CommonObject * @param string $label Label of stock movement * @param string $inventorycode Inventory code * @param string $datem Force date of movement - * @param integer $eatby eat-by date. Will be used if lot does not exists yet and will be created. - * @param integer $sellby sell-by date. Will be used if lot does not exists yet and will be created. + * @param integer|string $eatby eat-by date. Will be used if lot does not exists yet and will be created. + * @param integer|string $sellby sell-by date. Will be used if lot does not exists yet and will be created. * @param string $batch batch number * @param boolean $skip_batch If set to true, stock movement is done without impacting batch record * @param int $id_product_batch Id product_batch (when skip_batch is false and we already know which record of product_batch to use) @@ -166,13 +166,14 @@ class MouvementStock extends CommonObject // Check parameters if (empty($fk_product)) return 0; - if ($eatby < 0) - { + + if (is_numeric($eatby) && $eatby < 0) { + dol_syslog(get_class($this)."::_create start ErrorBadValueForParameterEatBy eatby = ".$eatby); $this->errors[] = 'ErrorBadValueForParameterEatBy'; return -1; } - if ($sellby < 0) - { + if (is_numeric($sellby) && $sellby < 0) { + dol_syslog(get_class($this)."::_create start ErrorBadValueForParameterSellBy sellby = ".$sellby); $this->errors[] = 'ErrorBadValueForParameterSellBy'; return -1; } @@ -786,19 +787,19 @@ class MouvementStock extends CommonObject /** * Increase stock for product and subproducts * - * @param User $user Object user - * @param int $fk_product Id product - * @param int $entrepot_id Warehouse id - * @param int $qty Quantity - * @param int $price Price - * @param string $label Label of stock movement - * @param integer $eatby eat-by date - * @param integer $sellby sell-by date - * @param string $batch batch number - * @param string $datem Force date of movement - * @param int $id_product_batch Id product_batch - * @param string $inventorycode Inventory code - * @return int <0 if KO, >0 if OK + * @param User $user Object user + * @param int $fk_product Id product + * @param int $entrepot_id Warehouse id + * @param int $qty Quantity + * @param int $price Price + * @param string $label Label of stock movement + * @param integer|string $eatby eat-by date + * @param integer|string $sellby sell-by date + * @param string $batch batch number + * @param string $datem Force date of movement + * @param int $id_product_batch Id product_batch + * @param string $inventorycode Inventory code + * @return int <0 if KO, >0 if OK */ public function reception($user, $fk_product, $entrepot_id, $qty, $price = 0, $label = '', $eatby = '', $sellby = '', $batch = '', $datem = '', $id_product_batch = 0, $inventorycode = '') { @@ -918,8 +919,8 @@ class MouvementStock extends CommonObject } else { // product_batch record not found $pdluo->fk_product_stock = $vfk_product_stock; $pdluo->qty = $qty; - $pdluo->eatby = $veatby; - $pdluo->sellby = $vsellby; + $pdluo->eatby = empty($dluo['eatby']) ? '' : $dluo['eatby']; // No more used. Now eatby date is store in table of lot, no more into prouct_batch table. + $pdluo->sellby = empty($dluo['sellby']) ? '' : $dluo['sellby']; // No more used. Now sellby date is store in table of lot, no more into prouct_batch table. $pdluo->batch = $vbatchnumber; $result = $pdluo->create($user, 1); diff --git a/test/phpunit/MouvementStockTest.php b/test/phpunit/MouvementStockTest.php index 23b6b2b0111..4570db5a1d5 100644 --- a/test/phpunit/MouvementStockTest.php +++ b/test/phpunit/MouvementStockTest.php @@ -130,7 +130,7 @@ class MouvementStockTest extends PHPUnit\Framework\TestCase /** * testMouvementCreate * - * @return int + * @return MouvementStock */ public function testMouvementCreate() { @@ -141,6 +141,13 @@ class MouvementStockTest extends PHPUnit\Framework\TestCase $db=$this->savdb; // We create a product for tests + $product0=new Product($db); + $product0->initAsSpecimen(); + $product0->ref.=' 0'; + $product0->label.=' 0'; + $product0->status_batch = 1; + $product0id=$product0->create($user); + $product1=new Product($db); $product1->initAsSpecimen(); $product1->ref.=' 1'; @@ -154,6 +161,13 @@ class MouvementStockTest extends PHPUnit\Framework\TestCase $product2id=$product2->create($user); // We create a product for tests + $warehouse0=new Entrepot($db); + $warehouse0->initAsSpecimen(); + $warehouse0->label.=' 0'; + $warehouse0->description.=' 0'; + $warehouse0->statut = 0; + $warehouse0id=$warehouse0->create($user); + $warehouse1=new Entrepot($db); $warehouse1->initAsSpecimen(); $warehouse1->label.=' 1'; @@ -168,32 +182,49 @@ class MouvementStockTest extends PHPUnit\Framework\TestCase $localobject=new MouvementStock($this->savdb); + $datetest1 = dol_mktime(0, 0, 0, 1, 1, 2000); + $datetest2 = dol_mktime(0, 0, 0, 1, 2, 2000); + + // Create an input movement movement (type = 3) with value for eatby date and a lot + $result=$localobject->reception($user, $product0id, $warehouse0id, 5, 999, 'Movement for unit test with batch', $datetest1, $datetest1, 'anotyetuselotnumberA', '', 0, 'Inventory Code Test with batch'); + print __METHOD__." result=".$result."\n"; + $this->assertGreaterThan(0, $result, 'Failed to create a movement with a lot number of product with status_batch=1'); + + $result=$localobject->reception($user, $product0id, $warehouse0id, 5, 999, 'Movement for unit test with batch', $datetest1, $datetest1, 'anotyetuselotnumberB', '', 0, 'Inventory Code Test with batch'); + print __METHOD__." result=".$result."\n"; + $this->assertGreaterThan(0, $result, 'Test to check we can create a movement a eatby dare different when lot number is different'); + + // Create same input movement movement (type = 3) with same lot but a different value of eatby date + $result=$localobject->reception($user, $product0id, $warehouse0id, 5, 999, 'Movement for unit test with batch', $datetest2, $datetest1, 'anotyetuselotnumberA', '', 0, 'Inventory Code Test with batch'); + print __METHOD__." result=".$result."\n"; + $this->assertEquals(-3, $result, 'Test to check we can t create a movement for a lot with a different eatby date'); + // Do a list of movement into warehouse 1 // Create an input movement (type = 3) of price 9.9 -> should update PMP to 9.9 $result=$localobject->reception($user, $product1id, $warehouse1id, 10, 9.9, 'Movement for unit test 1', '', '', '', '', 0, 'Inventory Code Test'); print __METHOD__." result=".$result."\n"; - $this->assertLessThan($result, 0); + $this->assertGreaterThan(0, $result, 'Return code of 0 was expected for the reception test 1'); // Create an input movement (type = 3) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8 $result=$localobject->reception($user, $product1id, $warehouse1id, 10, 9.7, 'Movement for unit test 2', '', '', '', '', 0, 'Inventory Code Test'); print __METHOD__." result=".$result."\n"; - $this->assertLessThan($result, 0); + $this->assertGreaterThan(0, $result); // Create an output movement (type = 2) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8 $result=$localobject->livraison($user, $product1id, $warehouse1id, 5, 999, 'Movement for unit test 3', '', '', '', '', 0, 'Inventory Code Test'); print __METHOD__." result=".$result."\n"; - $this->assertLessThan($result, 0); + $this->assertGreaterThan(0, $result); // Create an output movement (type = 1) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8 $result=$localobject->_create($user, $product1id, $warehouse1id, 1, 0, 0, 'Input from transfer', 'Transfert X'); print __METHOD__." result=".$result."\n"; - $this->assertLessThan($result, 0); + $this->assertGreaterThan(0, $result); // Create an output movement (type = 1) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8 $result=$localobject->_create($user, $product1id, $warehouse1id, -2, 1, 0, 'Output from transfer', 'Transfert Y'); print __METHOD__." result=".$result."\n"; - $this->assertLessThan($result, 0); + $this->assertGreaterThan(0, $result); // Do same but into warehouse 2 @@ -201,28 +232,27 @@ class MouvementStockTest extends PHPUnit\Framework\TestCase // Create an input movement (type = 3) of price 9.9 -> should update PMP to 9.9 $result=$localobject->reception($user, $product1id, $warehouse2id, 10, 9.9, 'Movement for unit test 1 wh 2', '', '', '', '', 0, 'Inventory Code Test 2'); print __METHOD__." result=".$result."\n"; - $this->assertLessThan($result, 0); + $this->assertGreaterThan(0, $result); // Create an input movement (type = 3) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8 $result=$localobject->reception($user, $product1id, $warehouse2id, 10, 9.7, 'Movement for unit test 2 wh 2', '', '', '', '', 0, 'Inventory Code Test 2'); print __METHOD__." result=".$result."\n"; - $this->assertLessThan($result, 0); + $this->assertGreaterThan(0, $result); // Create an output movement (type = 2) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8 $result=$localobject->livraison($user, $product1id, $warehouse2id, 5, 999, 'Movement for unit test 3 wh 2', '', '', '', '', 0, 'Inventory Code Test 2'); print __METHOD__." result=".$result."\n"; - $this->assertLessThan($result, 0); + $this->assertGreaterThan(0, $result); // Create an output movement (type = 1) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8 $result=$localobject->_create($user, $product1id, $warehouse2id, 1, 0, 0, 'Input from transfer wh 2', 'Transfert X 2'); print __METHOD__." result=".$result."\n"; - $this->assertLessThan($result, 0); + $this->assertGreaterThan(0, $result); // Create an output movement (type = 1) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8 $result=$localobject->_create($user, $product1id, $warehouse2id, -2, 1, 0, 'Output from transfer wh 2', 'Transfert Y 2'); print __METHOD__." result=".$result."\n"; - $this->assertLessThan($result, 0); - + $this->assertGreaterThan(0, $result); return $localobject; } @@ -231,7 +261,7 @@ class MouvementStockTest extends PHPUnit\Framework\TestCase * testMouvementCheck * * @param MouvementStock $localobject Movement object we created - * @return int + * @return MouvementStock * * @depends testMouvementCreate * The depends says test is run only if previous is ok