From 7b16482cee32b7c9cb8b2f0702bbad34d18db86b Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sat, 9 Apr 2016 10:01:22 +0200
Subject: [PATCH] Add comment to show directoves to solve conception error on
 lot management (add a table to store all lots and their properties: date,
 note, ...) Removed deprecated field pmp at stock level (pmp is at product
 level)

---
 .../install/mysql/migration/3.9.0-4.0.0.sql   | 19 ++++++++++++
 htdocs/install/mysql/tables/llx_product.sql   |  1 +
 .../mysql/tables/llx_product_batch.sql        |  6 ++--
 .../install/mysql/tables/llx_product_lot.sql  | 30 +++++++++++++++++++
 .../mysql/tables/llx_product_stock.sql        | 11 ++++---
 htdocs/install/mysql/tables/llx_user.sql      |  1 +
 htdocs/product/class/product.class.php        |  3 +-
 htdocs/product/stock/card.php                 |  4 +--
 .../stock/class/mouvementstock.class.php      |  3 --
 htdocs/product/stock/product.php              |  3 +-
 10 files changed, 64 insertions(+), 17 deletions(-)
 create mode 100644 htdocs/install/mysql/tables/llx_product_lot.sql

diff --git a/htdocs/install/mysql/migration/3.9.0-4.0.0.sql b/htdocs/install/mysql/migration/3.9.0-4.0.0.sql
index 6bb498e9c01..19ea469b0e1 100644
--- a/htdocs/install/mysql/migration/3.9.0-4.0.0.sql
+++ b/htdocs/install/mysql/migration/3.9.0-4.0.0.sql
@@ -36,6 +36,21 @@ ALTER TABLE llx_product_customer_price_log ADD COLUMN localtax1_type varchar(10)
 ALTER TABLE llx_product_customer_price_log ADD COLUMN localtax2_type varchar(10)  NOT NULL DEFAULT '0' after localtax2_tx; 
 
 
+CREATE TABLE llx_product_lot (
+  rowid integer AUTO_INCREMENT PRIMARY KEY,
+  tms timestamp,
+  batch varchar(30) NOT NULL,
+  eatby datetime DEFAULT NULL,
+  sellby datetime DEFAULT NULL,
+  note_public  text,
+  note_private text,
+  qty double NOT NULL DEFAULT 0,
+  import_key varchar(14) DEFAULT NULL
+) ENGINE=InnoDB;
+
+ALTER TABLE llx_product ADD COLUMN note_public text;
+ALTER TABLE llx_user ADD COLUMN note_public text;
+
 ALTER TABLE llx_c_type_contact ADD COLUMN position integer NOT NULL DEFAULT 0;
 
 
@@ -362,3 +377,7 @@ insert into llx_c_action_trigger (code,label,description,elementtype,rang) value
 insert into llx_c_action_trigger (code,label,description,elementtype,rang) values ('ORDER_SUPPLIER_RECEIVE','Supplier order request received','Executed when a supplier order is received','order_supplier',12);
 
 ALTER TABLE llx_product_fournisseur_price ADD supplier_reputation varchar(10) NULL;
+
+-- Delete old deprecated field
+ALTER TABLE llx_product_stock DROP COLUMN pmp;
+
diff --git a/htdocs/install/mysql/tables/llx_product.sql b/htdocs/install/mysql/tables/llx_product.sql
index 78620495f61..d9de9da17e9 100755
--- a/htdocs/install/mysql/tables/llx_product.sql
+++ b/htdocs/install/mysql/tables/llx_product.sql
@@ -35,6 +35,7 @@ create table llx_product
 
   label						varchar(255) NOT NULL,
   description				text,
+  note_public				text,
   note						text,
   customcode                varchar(32),                    -- Optionnal custom code
   fk_country                integer DEFAULT NULL,                        -- Optionnal id of original country
diff --git a/htdocs/install/mysql/tables/llx_product_batch.sql b/htdocs/install/mysql/tables/llx_product_batch.sql
index 1cf72151304..f502bb8c23f 100644
--- a/htdocs/install/mysql/tables/llx_product_batch.sql
+++ b/htdocs/install/mysql/tables/llx_product_batch.sql
@@ -1,5 +1,6 @@
 -- ============================================================================
 -- Copyright (C) 2014      Cédric GROSS         <c.gross@kreiz-it.fr>
+-- Copyright (C) 2016      Laurent Destailleur  <eldy@users.sourceforge.net>
 --
 -- This program is free software; you can redistribute it and/or modify
 -- it under the terms of the GNU General Public License as published by
@@ -14,13 +15,14 @@
 -- You should have received a copy of the GNU General Public License
 -- along with this program. If not, see <http://www.gnu.org/licenses/>.
 --
+-- This table is dedicated to store detail (lots/serial) of a stock
 -- ============================================================================
 CREATE TABLE llx_product_batch (
   rowid integer AUTO_INCREMENT PRIMARY KEY,
   tms timestamp,
   fk_product_stock integer NOT NULL,
-  eatby datetime DEFAULT NULL,
-  sellby datetime DEFAULT NULL,
+  eatby datetime DEFAULT NULL,			-- deprecated. should not be used here but should be stored into a table llx_product_lot
+  sellby datetime DEFAULT NULL,			-- deprecated. should not be used here but should be stored into a table llx_product_lot
   batch varchar(30) NOT NULL,
   qty double NOT NULL DEFAULT 0,
   import_key varchar(14) DEFAULT NULL
diff --git a/htdocs/install/mysql/tables/llx_product_lot.sql b/htdocs/install/mysql/tables/llx_product_lot.sql
new file mode 100644
index 00000000000..91796823a8d
--- /dev/null
+++ b/htdocs/install/mysql/tables/llx_product_lot.sql
@@ -0,0 +1,30 @@
+-- ============================================================================
+-- Copyright (C) 2014      Cédric GROSS         <c.gross@kreiz-it.fr>
+-- Copyright (C) 2016      Laurent Destailleur  <eldy@users.sourceforge.net>
+--
+-- This program is free software; you can redistribute it and/or modify
+-- it under the terms of the GNU General Public License as published by
+-- the Free Software Foundation; either version 3 of the License, or
+-- (at your option) any later version.
+--
+-- This program is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+-- GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License
+-- along with this program. If not, see <http://www.gnu.org/licenses/>.
+--
+-- This table is dedicated to store lots with detail of each lot
+-- ============================================================================
+CREATE TABLE llx_product_lot (
+  rowid integer AUTO_INCREMENT PRIMARY KEY,
+  tms timestamp,
+  batch varchar(30) NOT NULL,
+  eatby datetime DEFAULT NULL,
+  sellby datetime DEFAULT NULL,
+  note_public  text,
+  note_private text,
+  qty double NOT NULL DEFAULT 0,
+  import_key varchar(14) DEFAULT NULL
+) ENGINE=InnoDB;
diff --git a/htdocs/install/mysql/tables/llx_product_stock.sql b/htdocs/install/mysql/tables/llx_product_stock.sql
index 99d7d27fb77..a2696a4bc4e 100644
--- a/htdocs/install/mysql/tables/llx_product_stock.sql
+++ b/htdocs/install/mysql/tables/llx_product_stock.sql
@@ -1,7 +1,7 @@
 -- ============================================================================
--- Copyright (C) 2003 Rodolphe Quiedeville	<rodolphe@quiedeville.org>
--- Copyright (C) 2009 Laurent Destailleur	<eldy@users.sourceforge.net>
--- Copyright (C) 2012 Juanjo Menent			<jmenent@2byte.es>
+-- Copyright (C) 2003      Rodolphe Quiedeville	<rodolphe@quiedeville.org>
+-- Copyright (C) 2009-2016 Laurent Destailleur	<eldy@users.sourceforge.net>
+-- Copyright (C) 2012      Juanjo Menent        <jmenent@2byte.es>
 --
 -- This program is free software; you can redistribute it and/or modify
 -- it under the terms of the GNU General Public License as published by
@@ -24,8 +24,7 @@ create table llx_product_stock
   tms             timestamp,
   fk_product      integer NOT NULL,
   fk_entrepot     integer NOT NULL,
-  reel            real,           					-- physical stock
-  pmp             double(24,8) default 0 NOT NULL,	-- PMP value for product in this warehouse (deprecated. no sens for warehouse)
-  import_key      varchar(14)                  -- Import key
+  reel            real,           			-- physical stock
+  import_key      varchar(14)               -- Import key
 )ENGINE=innodb;
 
diff --git a/htdocs/install/mysql/tables/llx_user.sql b/htdocs/install/mysql/tables/llx_user.sql
index e334ae1f802..6b3c5b83f6d 100644
--- a/htdocs/install/mysql/tables/llx_user.sql
+++ b/htdocs/install/mysql/tables/llx_user.sql
@@ -61,6 +61,7 @@ create table llx_user
   fk_socpeople      integer,
   fk_member         integer,
   fk_user           integer,               -- Hierarchic parent
+  note_public		text,
   note              text DEFAULT NULL,
   datelastlogin     datetime,
   datepreviouslogin datetime,
diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php
index ca752c1a1f8..a538e4543ee 100644
--- a/htdocs/product/class/product.class.php
+++ b/htdocs/product/class/product.class.php
@@ -3338,7 +3338,7 @@ class Product extends CommonObject
 		$this->stock_reel = 0;
 		$this->stock_warehouse = array();
 
-		$sql = "SELECT ps.reel, ps.fk_entrepot, ps.pmp, ps.rowid";
+		$sql = "SELECT ps.rowid, ps.reel, ps.fk_entrepot";
 		$sql.= " FROM ".MAIN_DB_PREFIX."product_stock as ps";
 		$sql.= ", ".MAIN_DB_PREFIX."entrepot as w";
 		$sql.= " WHERE w.entity IN (".getEntity('stock', 1).")";
@@ -3358,7 +3358,6 @@ class Product extends CommonObject
 					$row = $this->db->fetch_object($result);
 					$this->stock_warehouse[$row->fk_entrepot] = new stdClass();
 					$this->stock_warehouse[$row->fk_entrepot]->real = $row->reel;
-					$this->stock_warehouse[$row->fk_entrepot]->pmp = $row->pmp;
 					$this->stock_warehouse[$row->fk_entrepot]->id = $row->rowid;
 					if ($this->hasbatch()) $this->stock_warehouse[$row->fk_entrepot]->detail_batch=Productbatch::findAll($this->db,$row->rowid,1);
 					$this->stock_reel+=$row->reel;
diff --git a/htdocs/product/stock/card.php b/htdocs/product/stock/card.php
index 80d8bea62f7..430e042c202 100644
--- a/htdocs/product/stock/card.php
+++ b/htdocs/product/stock/card.php
@@ -384,7 +384,7 @@ else
 			print_liste_field_titre($langs->trans("Product"),"", "p.ref","&amp;id=".$id,"","",$sortfield,$sortorder);
 			print_liste_field_titre($langs->trans("Label"),"", "p.label","&amp;id=".$id,"","",$sortfield,$sortorder);
             print_liste_field_titre($langs->trans("Units"),"", "ps.reel","&amp;id=".$id,"",'align="right"',$sortfield,$sortorder);
-            print_liste_field_titre($langs->trans("AverageUnitPricePMPShort"),"", "ps.pmp","&amp;id=".$id,"",'align="right"',$sortfield,$sortorder);
+            print_liste_field_titre($langs->trans("AverageUnitPricePMPShort"),"", "p.pmp","&amp;id=".$id,"",'align="right"',$sortfield,$sortorder);
 			print_liste_field_titre($langs->trans("EstimatedStockValueShort"),"", "","&amp;id=".$id,"",'align="right"',$sortfield,$sortorder);
             if (empty($conf->global->PRODUIT_MULTIPRICES)) print_liste_field_titre($langs->trans("SellPriceMin"),"", "p.price","&amp;id=".$id,"",'align="right"',$sortfield,$sortorder);
             if (empty($conf->global->PRODUIT_MULTIPRICES)) print_liste_field_titre($langs->trans("EstimatedStockValueSellShort"),"", "","&amp;id=".$id,"",'align="right"',$sortfield,$sortorder);
@@ -396,7 +396,7 @@ else
 			$totalvalue=$totalvaluesell=0;
 
 			$sql = "SELECT p.rowid as rowid, p.ref, p.label as produit, p.fk_product_type as type, p.pmp as ppmp, p.price, p.price_ttc, p.entity,";
-			$sql.= " ps.pmp, ps.reel as value";
+			$sql.= " ps.reel as value";
 			$sql.= " FROM ".MAIN_DB_PREFIX."product_stock as ps, ".MAIN_DB_PREFIX."product as p";
 			$sql.= " WHERE ps.fk_product = p.rowid";
 			$sql.= " AND ps.reel <> 0";	// We do not show if stock is 0 (no product in this warehouse)
diff --git a/htdocs/product/stock/class/mouvementstock.class.php b/htdocs/product/stock/class/mouvementstock.class.php
index dde89bfb6e5..652c6d9dc36 100644
--- a/htdocs/product/stock/class/mouvementstock.class.php
+++ b/htdocs/product/stock/class/mouvementstock.class.php
@@ -304,15 +304,12 @@ class MouvementStock extends CommonObject
 			{
 				if ($num > 0)
 				{
-					//$sql = "UPDATE ".MAIN_DB_PREFIX."product_stock SET pmp = ".$newpmpwarehouse.", reel = reel + ".$qty;
 					$sql = "UPDATE ".MAIN_DB_PREFIX."product_stock SET reel = reel + ".$qty;
 					$sql.= " WHERE fk_entrepot = ".$entrepot_id." AND fk_product = ".$fk_product;
 				}
 				else
 				{
 					$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_stock";
-					//$sql.= " (pmp, reel, fk_entrepot, fk_product) VALUES ";
-					//$sql.= " (".$newpmpwarehouse.", ".$qty.", ".$entrepot_id.", ".$fk_product.")";
 					$sql.= " (reel, fk_entrepot, fk_product) VALUES ";
 					$sql.= " (".$qty.", ".$entrepot_id.", ".$fk_product.")";
 				}
diff --git a/htdocs/product/stock/product.php b/htdocs/product/stock/product.php
index 42fadf54923..de30bc3d0b6 100644
--- a/htdocs/product/stock/product.php
+++ b/htdocs/product/stock/product.php
@@ -229,7 +229,6 @@ if ($action == "transfert_stock" && ! $cancel)
 
 			// Define value of products moved
 			$pricesrc=0;
-			//if (isset($object->stock_warehouse[GETPOST("id_entrepot_source")]->pmp)) $pricesrc=$object->stock_warehouse[GETPOST("id_entrepot_source")]->pmp;
 			if (isset($object->pmp)) $pricesrc=$object->pmp;
 			$pricedest=$pricesrc;
 
@@ -677,7 +676,7 @@ if ((! empty($conf->productbatch->enabled)) && $object->hasbatch())
 	print '</tr>';
 }
 
-$sql = "SELECT e.rowid, e.label, e.lieu, ps.reel, ps.pmp, ps.rowid as product_stock_id";
+$sql = "SELECT e.rowid, e.label, e.lieu, ps.reel, ps.rowid as product_stock_id, p.pmp";
 $sql.= " FROM ".MAIN_DB_PREFIX."entrepot as e,";
 $sql.= " ".MAIN_DB_PREFIX."product_stock as ps";
 $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = ps.fk_product";
-- 
GitLab