From 3cbbdf5e20aa99f32308c913a583ed452347c8bc Mon Sep 17 00:00:00 2001 From: phf Date: Sat, 4 Jun 2016 18:23:22 +0200 Subject: [PATCH 1/2] Fix #2858 --- htdocs/admin/stock.php | 20 +++++ htdocs/core/modules/modStock.class.php | 4 +- htdocs/langs/en_US/stocks.lang | 2 + htdocs/product/stock/product.php | 117 +++++++++++++++---------- 4 files changed, 94 insertions(+), 49 deletions(-) diff --git a/htdocs/admin/stock.php b/htdocs/admin/stock.php index 87ed5fe64c6..d0a859e1987 100644 --- a/htdocs/admin/stock.php +++ b/htdocs/admin/stock.php @@ -49,6 +49,10 @@ if($action) { $res = dolibarr_set_const($db, "STOCK_USERSTOCK_AUTOCREATE", GETPOST('STOCK_USERSTOCK_AUTOCREATE','alpha'),'chaine',0,'',$conf->entity); } + if ($action == 'STOCK_ALLOW_NEGATIVE_TRANSFER') + { + $res = dolibarr_set_const($db, "STOCK_ALLOW_NEGATIVE_TRANSFER", GETPOST('STOCK_ALLOW_NEGATIVE_TRANSFER','alpha'),'chaine',0,'',$conf->entity); + } // Mode of stock decrease if ($action == 'STOCK_CALCULATE_ON_BILL' || $action == 'STOCK_CALCULATE_ON_VALIDATE_ORDER' @@ -367,6 +371,22 @@ print '' print ''; print "\n"; print "\n"; + +$var=!$var; + +print ""; +print ''.$langs->trans("WarehouseAllowNegativeTransfer").''; + +print ''; +print "
"; +print ''; +print ""; +print $form->selectyesno("STOCK_ALLOW_NEGATIVE_TRANSFER",$conf->global->STOCK_ALLOW_NEGATIVE_TRANSFER,1); +print ''; +print '
'; +print "\n"; +print "\n"; + print '
'; print ''; print '
'; diff --git a/htdocs/core/modules/modStock.class.php b/htdocs/core/modules/modStock.class.php index 8f6840a21b0..5ac13df8ecc 100644 --- a/htdocs/core/modules/modStock.class.php +++ b/htdocs/core/modules/modStock.class.php @@ -70,7 +70,9 @@ class modStock extends DolibarrModules $this->langfiles = array("stocks"); // Constants - $this->const = array(); + $this->const = array( + 0=>array('STOCK_ALLOW_NEGATIVE_TRANSFER','chaine','1','',1) + ); // Boxes $this->boxes = array(); diff --git a/htdocs/langs/en_US/stocks.lang b/htdocs/langs/en_US/stocks.lang index fdd6408accb..fb2a034b9f6 100644 --- a/htdocs/langs/en_US/stocks.lang +++ b/htdocs/langs/en_US/stocks.lang @@ -123,3 +123,5 @@ StockMustBeEnoughForInvoice=Stock level must be enough to add product/service in StockMustBeEnoughForOrder=Stock level must be enough to add product/service into order StockMustBeEnoughForShipment= Stock level must be enough to add product/service into shipment WarehouseMustBeSelectedAtFirstStepWhenProductBatchModuleOn=Source warehouse must be defined here when batch module is on. It will be used to list wich lot/serial is available for product that required lot/serial data for movement. If you want to send products from different warehouses, just make the shipment into several steps. +WarehouseAllowNegativeTransfer=Allow transfer even without stock +qtyToTranferIsNotEnough=You don't have enough stock from your source warehouse \ No newline at end of file diff --git a/htdocs/product/stock/product.php b/htdocs/product/stock/product.php index 83ad3088a11..c9ba20e95f5 100644 --- a/htdocs/product/stock/product.php +++ b/htdocs/product/stock/product.php @@ -204,59 +204,80 @@ if ($action == "transfert_stock" && ! $cancel) //print 'price src='.$pricesrc.', price dest='.$pricedest;exit; - $pdluoid=GETPOST('pdluoid','int'); - - if ($pdluoid>0) + $do_tranfer = true; + + if (empty($conf->global->STOCK_ALLOW_NEGATIVE_TRANSFER)) { - $pdluo = new Productbatch($db); - $result=$pdluo->fetch($pdluoid); - - if ($result>0 && $pdluo->id) - { - // Remove stock - $result1=$product->correct_stock_batch( - $user, - $pdluo->warehouseid, - GETPOST("nbpiece",'int'), - 1, - GETPOST("label",'san_alpha'), - $pricesrc, - $pdluo->eatby,$pdluo->sellby,$pdluo->batch - ); - // Add stock - $result2=$product->correct_stock_batch( - $user, - GETPOST("id_entrepot_destination",'int'), - GETPOST("nbpiece",'int'), - 0, - GETPOST("label",'san_alpha'), - $pricedest, - $pdluo->eatby,$pdluo->sellby,$pdluo->batch - ); - } + $fk_warehouse_source = GETPOST("id_entrepot_source"); + $nb_unit = GETPOST("nbpiece",'int'); + + if (empty($product->stock_warehouse[$fk_warehouse_source]->real) || $product->stock_warehouse[$fk_warehouse_source]->real < $nb_unit) + { + $do_tranfer = false; + $result1 = $result2 = -1; + $action=''; + $product->error = $langs->trans('qtyToTranferIsNotEnough'); + } } - else + + if ($do_tranfer) { - // Remove stock - $result1=$product->correct_stock( - $user, - GETPOST("id_entrepot_source"), - GETPOST("nbpiece"), - 1, - GETPOST("label"), - $pricesrc - ); + $pdluoid=GETPOST('pdluoid','int'); - // Add stock - $result2=$product->correct_stock( - $user, - GETPOST("id_entrepot_destination"), - GETPOST("nbpiece"), - 0, - GETPOST("label"), - $pricedest - ); + if ($pdluoid>0) + { + $pdluo = new Productbatch($db); + $result=$pdluo->fetch($pdluoid); + + if ($result>0 && $pdluo->id) + { + // Remove stock + $result1=$product->correct_stock_batch( + $user, + $pdluo->warehouseid, + GETPOST("nbpiece",'int'), + 1, + GETPOST("label",'san_alpha'), + $pricesrc, + $pdluo->eatby,$pdluo->sellby,$pdluo->batch + ); + // Add stock + $result2=$product->correct_stock_batch( + $user, + GETPOST("id_entrepot_destination",'int'), + GETPOST("nbpiece",'int'), + 0, + GETPOST("label",'san_alpha'), + $pricedest, + $pdluo->eatby,$pdluo->sellby,$pdluo->batch + ); + } + } + else + { + // Remove stock + $result1=$product->correct_stock( + $user, + GETPOST("id_entrepot_source"), + GETPOST("nbpiece"), + 1, + GETPOST("label"), + $pricesrc + ); + + // Add stock + $result2=$product->correct_stock( + $user, + GETPOST("id_entrepot_destination"), + GETPOST("nbpiece"), + 0, + GETPOST("label"), + $pricedest + ); + } + } + if ($result1 >= 0 && $result2 >= 0) { $db->commit(); From 67392fbac5005c4ea1cecd4f045e2312e68e79ab Mon Sep 17 00:00:00 2001 From: phf Date: Tue, 7 Jun 2016 12:27:43 +0200 Subject: [PATCH 2/2] Fix move the code into mouvementstock class --- .../stock/class/mouvementstock.class.php | 13 +- htdocs/product/stock/product.php | 173 ++++++++---------- 2 files changed, 89 insertions(+), 97 deletions(-) diff --git a/htdocs/product/stock/class/mouvementstock.class.php b/htdocs/product/stock/class/mouvementstock.class.php index b9859f9ec9b..accaaba6c30 100644 --- a/htdocs/product/stock/class/mouvementstock.class.php +++ b/htdocs/product/stock/class/mouvementstock.class.php @@ -240,7 +240,7 @@ class MouvementStock extends CommonObject return -1; } } - + // TODO Check qty is ok for stock move. if (! empty($conf->productbatch->enabled) && $product->hasbatch() && ! $skip_batch) { @@ -250,6 +250,17 @@ class MouvementStock extends CommonObject { } + + if (empty($conf->global->STOCK_ALLOW_NEGATIVE_TRANSFER) && in_array($type, array(1, 2))) + { + if (empty($product->stock_warehouse[$entrepot_id]->real) || $product->stock_warehouse[$entrepot_id]->real < abs($qty)) + { + $this->error = $langs->trans('qtyToTranferIsNotEnough'); + $this->errors[] = $langs->trans('qtyToTranferIsNotEnough'); + $this->db->rollback(); + return -8; + } + } // Define if we must make the stock change (If product type is a service or if stock is used also for services) $movestock=0; diff --git a/htdocs/product/stock/product.php b/htdocs/product/stock/product.php index 4c92c353027..b7ccc14fc7e 100644 --- a/htdocs/product/stock/product.php +++ b/htdocs/product/stock/product.php @@ -231,117 +231,98 @@ if ($action == "transfert_stock" && ! $cancel) $pricesrc=0; if (isset($object->pmp)) $pricesrc=$object->pmp; $pricedest=$pricesrc; - - $do_tranfer = true; - if (empty($conf->global->STOCK_ALLOW_NEGATIVE_TRANSFER)) - { - $fk_warehouse_source = GETPOST("id_entrepot"); - $nb_unit = GETPOST("nbpiece",'int'); - - if (empty($object->stock_warehouse[$fk_warehouse_source]->real) || $object->stock_warehouse[$fk_warehouse_source]->real < $nb_unit) - { - $do_tranfer = false; - $result1 = $result2 = -1; - $action=''; - $object->error = $langs->trans('qtyToTranferIsNotEnough'); - } - } - if ($do_tranfer) + if ($object->hasbatch()) { + $pdluo = new Productbatch($db); - if ($object->hasbatch()) + if ($pdluoid > 0) { - $pdluo = new Productbatch($db); - - if ($pdluoid > 0) + $result=$pdluo->fetch($pdluoid); + if ($result) { - $result=$pdluo->fetch($pdluoid); - if ($result) - { - $srcwarehouseid=$pdluo->warehouseid; - $batch=$pdluo->batch; - $eatby=$pdluo->eatby; - $sellby=$pdluo->sellby; - } - else - { - setEventMessages($pdluo->error, $pdluo->errors, 'errors'); - $error++; - } + $srcwarehouseid=$pdluo->warehouseid; + $batch=$pdluo->batch; + $eatby=$pdluo->eatby; + $sellby=$pdluo->sellby; } else { - $srcwarehouseid=GETPOST('id_entrepot','int'); - $batch=GETPOST('batch_number'); - $eatby=$d_eatby; - $sellby=$d_sellby; - } - - if (! $error) - { - // Remove stock - $result1=$object->correct_stock_batch( - $user, - $srcwarehouseid, - GETPOST("nbpiece",'int'), - 1, - GETPOST("label",'san_alpha'), - $pricesrc, - $eatby,$sellby,$batch, - GETPOST('inventorycode') - ); - if ($result1 < 0) $error++; - } - if (! $error) - { - // Add stock - $result2=$object->correct_stock_batch( - $user, - GETPOST("id_entrepot_destination",'int'), - GETPOST("nbpiece",'int'), - 0, - GETPOST("label",'san_alpha'), - $pricedest, - $eatby,$sellby,$batch, - GETPOST('inventorycode') - ); - if ($result2 < 0) $error++; + setEventMessages($pdluo->error, $pdluo->errors, 'errors'); + $error++; } } else { - if (! $error) - { - // Remove stock - $result1=$object->correct_stock( - $user, - GETPOST("id_entrepot"), - GETPOST("nbpiece"), - 1, - GETPOST("label"), - $pricesrc, - GETPOST('inventorycode') - ); - if ($result1 < 0) $error++; - } - if (! $error) - { - // Add stock - $result2=$object->correct_stock( - $user, - GETPOST("id_entrepot_destination"), - GETPOST("nbpiece"), - 0, - GETPOST("label"), - $pricedest, - GETPOST('inventorycode') - ); - if ($result2 < 0) $error++; - } + $srcwarehouseid=GETPOST('id_entrepot','int'); + $batch=GETPOST('batch_number'); + $eatby=$d_eatby; + $sellby=$d_sellby; } + if (! $error) + { + // Remove stock + $result1=$object->correct_stock_batch( + $user, + $srcwarehouseid, + GETPOST("nbpiece",'int'), + 1, + GETPOST("label",'san_alpha'), + $pricesrc, + $eatby,$sellby,$batch, + GETPOST('inventorycode') + ); + if ($result1 < 0) $error++; + } + if (! $error) + { + // Add stock + $result2=$object->correct_stock_batch( + $user, + GETPOST("id_entrepot_destination",'int'), + GETPOST("nbpiece",'int'), + 0, + GETPOST("label",'san_alpha'), + $pricedest, + $eatby,$sellby,$batch, + GETPOST('inventorycode') + ); + if ($result2 < 0) $error++; + } } + else + { + if (! $error) + { + // Remove stock + $result1=$object->correct_stock( + $user, + GETPOST("id_entrepot"), + GETPOST("nbpiece"), + 1, + GETPOST("label"), + $pricesrc, + GETPOST('inventorycode') + ); + if ($result1 < 0) $error++; + } + if (! $error) + { + // Add stock + $result2=$object->correct_stock( + $user, + GETPOST("id_entrepot_destination"), + GETPOST("nbpiece"), + 0, + GETPOST("label"), + $pricedest, + GETPOST('inventorycode') + ); + if ($result2 < 0) $error++; + } + } + if (! $error && $result1 >= 0 && $result2 >= 0) {