From ce3a60b2a3b03a772d06d8a4c8cd5c2f5885d733 Mon Sep 17 00:00:00 2001
From: KreizIT <KreizIT@users.noreply.github.com>
Date: Tue, 6 Jan 2015 13:23:51 +0100
Subject: [PATCH] Allow warehouse transfer for batched product

---
 htdocs/product/class/productbatch.class.php |   7 +-
 htdocs/product/stock/product.php            | 115 +++++++++++++++-----
 2 files changed, 93 insertions(+), 29 deletions(-)

diff --git a/htdocs/product/class/productbatch.class.php b/htdocs/product/class/productbatch.class.php
index a4df7a95829..088dccf6861 100644
--- a/htdocs/product/class/productbatch.class.php
+++ b/htdocs/product/class/productbatch.class.php
@@ -42,6 +42,7 @@ class Productbatch extends CommonObject
 	var $batch='';
 	var $qty;
 	var $import_key;
+	public $warehouseid;
 
 
 
@@ -155,10 +156,11 @@ class Productbatch extends CommonObject
 		$sql.= " t.eatby,";
 		$sql.= " t.batch,";
 		$sql.= " t.qty,";
-		$sql.= " t.import_key";
-
+		$sql.= " t.import_key,";
+		$sql.= " w.fk_entrepot";
 
         $sql.= " FROM ".MAIN_DB_PREFIX.self::$_table_element." as t";
+        $sql.= " INNER JOIN ".MAIN_DB_PREFIX."product_stock w on t.fk_product_stock=w.rowid";
         $sql.= " WHERE t.rowid = ".$id;
 
 		dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
@@ -177,6 +179,7 @@ class Productbatch extends CommonObject
 				$this->batch = $obj->batch;
 				$this->qty = $obj->qty;
 				$this->import_key = $obj->import_key;
+				$this->warehouseid= $obj->fk_entrepot;
 			}
 			$this->db->free($resql);
 
diff --git a/htdocs/product/stock/product.php b/htdocs/product/stock/product.php
index eaa12794688..13a798548d5 100644
--- a/htdocs/product/stock/product.php
+++ b/htdocs/product/stock/product.php
@@ -170,13 +170,13 @@ if ($action == "correct_stock" && ! $cancel)
 // Transfer stock from a warehouse to another warehouse
 if ($action == "transfert_stock" && ! $cancel)
 {
-	if (! (GETPOST("id_entrepot_source") > 0) || ! (GETPOST("id_entrepot_destination") > 0))
+	if (! (GETPOST("id_entrepot_source",'int') > 0) || ! (GETPOST("id_entrepot_destination",'int') > 0))
 	{
 		setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Warehouse")), 'errors');
 		$error++;
 		$action='transfert';
 	}
-	if (! GETPOST("nbpiece"))
+	if (! GETPOST("nbpiece",'int'))
 	{
 		setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("NumberOfUnit")), 'errors');
 		$error++;
@@ -185,9 +185,9 @@ if ($action == "transfert_stock" && ! $cancel)
 
 	if (! $error)
 	{
-		if (GETPOST("id_entrepot_source") <> GETPOST("id_entrepot_destination"))
+		if (GETPOST("id_entrepot_source",'int') <> GETPOST("id_entrepot_destination",'int'))
 		{
-			if (is_numeric(GETPOST("nbpiece")) && $id)
+			if (GETPOST("nbpiece",'int') && $id)
 			{
 				$product = new Product($db);
 				$result=$product->fetch($id);
@@ -201,28 +201,59 @@ if ($action == "transfert_stock" && ! $cancel)
 				if (isset($product->stock_warehouse[GETPOST("id_entrepot_source")]->pmp)) $pricesrc=$product->stock_warehouse[GETPOST("id_entrepot_source")]->pmp;
 				$pricedest=$pricesrc;
 
-				//print 'price src='.$pricesrc.', price dest='.$pricedest;exit;
-
-				// 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
-				);
+				$pdluoid=GETPOST('pdluoid','int');
 
+				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();
@@ -239,6 +270,7 @@ if ($action == "transfert_stock" && ! $cancel)
 	}
 }
 
+// Update batch information
 if ($action == 'updateline' && GETPOST('save') == $langs->trans('Save'))
 {
 
@@ -540,15 +572,43 @@ if ($id > 0 || $ref)
 	 */
 	if ($action == "transfert")
 	{
+	    $pdluoid=GETPOST('pdluoid','int');
+
+	    if ($pdluoid > 0)
+	    {
+	        $pdluo = new Productbatch($db);
+	        $result=$pdluo->fetch($pdluoid);
+
+	        if ($result > 0)
+	        {
+	            $pdluoid=$pdluo->id;
+	        }
+	        else
+	        {
+	            dol_print_error($db);
+	        }
+	    }
+
 		print_titre($langs->trans("StockTransfer"));
 		print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$product->id.'" method="post">'."\n";
 		print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
 		print '<input type="hidden" name="action" value="transfert_stock">';
+		if ($pdluoid)
+		{
+		    print '<input type="hidden" name="pdluoid" value="'.$pdluoid.'">';
+		}
 		print '<table class="border" width="100%">';
 
 		print '<tr>';
 		print '<td width="20%" class="fieldrequired">'.$langs->trans("WarehouseSource").'</td><td width="20%">';
-		print $formproduct->selectWarehouses(($_GET["dwid"]?$_GET["dwid"]:GETPOST('id_entrepot_source')),'id_entrepot_source','',1);
+		if ($pdluoid)
+		{
+		    print $formproduct->selectWarehouses($pdluo->warehouseid,'id_entrepot_source','',1,1);
+		}
+		else
+		{
+            print $formproduct->selectWarehouses(($_GET["dwid"]?$_GET["dwid"]:GETPOST('id_entrepot_source')),'id_entrepot_source','',1);
+		}
 		print '</td>';
 		print '<td width="20%" class="fieldrequired">'.$langs->trans("WarehouseTarget").'</td><td width="20%">';
 		print $formproduct->selectWarehouses(GETPOST('id_entrepot_destination'),'id_entrepot_destination','',1);
@@ -630,7 +690,7 @@ if (empty($action) && $product->id)
 
 
 /*
- * Contenu des stocks
+ * Stock detail
  */
 print '<br><table class="noborder" width="100%">';
 print '<tr class="liste_titre"><td width="40%" colspan="4">'.$langs->trans("Warehouse").'</td>';
@@ -720,6 +780,7 @@ if ($resql)
 			    else
 			    {
                     print "\n".'<tr><td align="right">';
+                    print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$product->id.'&amp;action=transfert&amp;pdluoid='.$pdluo->id.'">'.$langs->trans("StockMovement").'</a>';
                     print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$id.'&amp;action=editline&amp;lineid='.$pdluo->id.'#'.$pdluo->id.'">';
                     print img_edit().'</a></td>';
                     print '<td align="right">'.$pdluo->batch.'</td>';
-- 
GitLab