From d18097da2825ca8a2655c3f3d9113d48fd50e865 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Fri, 21 Oct 2016 14:09:11 +0200
Subject: [PATCH] Prepare database to manage different vat rate with same rate
 value.

---
 .../install/mysql/migration/4.0.0-5.0.0.sql   | 10 ++++++
 .../tables/llx_commande_fournisseurdet.sql    |  1 +
 .../install/mysql/tables/llx_commandedet.sql  |  3 +-
 .../mysql/tables/llx_facture_fourn_det.sql    |  1 +
 .../install/mysql/tables/llx_facturedet.sql   | 31 ++++++++++---------
 htdocs/install/mysql/tables/llx_propaldet.sql |  3 +-
 .../mysql/tables/llx_supplier_proposaldet.sql |  4 +--
 htdocs/langs/en_US/accountancy.lang           |  3 +-
 8 files changed, 36 insertions(+), 20 deletions(-)

diff --git a/htdocs/install/mysql/migration/4.0.0-5.0.0.sql b/htdocs/install/mysql/migration/4.0.0-5.0.0.sql
index 1b50ae0cc15..cf34086f066 100644
--- a/htdocs/install/mysql/migration/4.0.0-5.0.0.sql
+++ b/htdocs/install/mysql/migration/4.0.0-5.0.0.sql
@@ -140,3 +140,13 @@ ALTER TABLE llx_accounting_bookkeeping ADD COLUMN tms               timestamp;
 ALTER TABLE llx_accounting_account ADD UNIQUE INDEX uk_accounting_account (account_number, entity, fk_pcg_version);
 
 ALTER TABLE llx_c_payment_term change fdm type_cdr tinyint
+
+
+ALTER TABLE llx_facturedet ADD COLUMN vat_src_code varchar(10) DEFAULT '' AFTER tva_tx;
+ALTER TABLE llx_facture_fourn_det ADD COLUMN vat_src_code varchar(10) DEFAULT '' AFTER tva_tx;
+ALTER TABLE llx_commandedet ADD COLUMN vat_src_code varchar(10) DEFAULT '' AFTER tva_tx;
+ALTER TABLE llx_commande_fournisseurdet ADD COLUMN vat_src_code varchar(10) DEFAULT '' AFTER tva_tx;
+ALTER TABLE llx_propaldet ADD COLUMN vat_src_code varchar(10) DEFAULT '' AFTER tva_tx;
+ALTER TABLE llx_supplier_proposaldet ADD COLUMN vat_src_code varchar(10) DEFAULT '' AFTER tva_tx;
+
+  
\ No newline at end of file
diff --git a/htdocs/install/mysql/tables/llx_commande_fournisseurdet.sql b/htdocs/install/mysql/tables/llx_commande_fournisseurdet.sql
index f7b10c8e35e..85cdcbb9a60 100644
--- a/htdocs/install/mysql/tables/llx_commande_fournisseurdet.sql
+++ b/htdocs/install/mysql/tables/llx_commande_fournisseurdet.sql
@@ -28,6 +28,7 @@ create table llx_commande_fournisseurdet
   ref                        varchar(50),  -- supplier product ref
   label                      varchar(255), -- product label
   description                text,
+  vat_src_code				 varchar(10)  DEFAULT '',	-- Vat code used as source of vat fields. Not strict foreign key here.
   tva_tx                     double(6,3)  DEFAULT 0,    -- taux tva
   localtax1_tx               double(6,3)  DEFAULT 0,    -- localtax1 rate
   localtax1_type			 varchar(10)	  NULL, 		-- localtax1 type
diff --git a/htdocs/install/mysql/tables/llx_commandedet.sql b/htdocs/install/mysql/tables/llx_commandedet.sql
index 913351ef2f8..53a1b07c3c7 100644
--- a/htdocs/install/mysql/tables/llx_commandedet.sql
+++ b/htdocs/install/mysql/tables/llx_commandedet.sql
@@ -28,7 +28,8 @@ create table llx_commandedet
   fk_product					integer	 NULL,
   label							varchar(255) DEFAULT NULL,
   description					text,
-  tva_tx						double(6,3),	                 -- vat rate
+  vat_src_code					varchar(10)  DEFAULT '',		 -- Vat code used as source of vat fields. Not strict foreign key here.
+  tva_tx						double(6,3),	                 -- Vat rate
   localtax1_tx               	double(6,3)  DEFAULT 0,    		 -- localtax1 rate
   localtax1_type			 	varchar(10)	  	 NULL, 			 -- localtax1 type
   localtax2_tx               	double(6,3)  DEFAULT 0,    		 -- localtax2 rate
diff --git a/htdocs/install/mysql/tables/llx_facture_fourn_det.sql b/htdocs/install/mysql/tables/llx_facture_fourn_det.sql
index feff72e39b9..2f75f9ff54e 100644
--- a/htdocs/install/mysql/tables/llx_facture_fourn_det.sql
+++ b/htdocs/install/mysql/tables/llx_facture_fourn_det.sql
@@ -31,6 +31,7 @@ create table llx_facture_fourn_det
   pu_ttc            double(24,8), -- unit price with tax
   qty               real,         -- quantity of product/service
   remise_percent	real       DEFAULT 0,				-- % de la remise ligne (exemple 20%)
+  vat_src_code		varchar(10)  DEFAULT '',			-- Vat code used as source of vat fields. Not strict foreign key here.
   tva_tx            double(6,3),  -- TVA taux product/service
   localtax1_tx      double(6,3)  DEFAULT 0,    -- localtax1 rate
   localtax1_type	varchar(10)	  NULL, 		-- localtax1 type
diff --git a/htdocs/install/mysql/tables/llx_facturedet.sql b/htdocs/install/mysql/tables/llx_facturedet.sql
index 42f83e16500..0bf124975ab 100644
--- a/htdocs/install/mysql/tables/llx_facturedet.sql
+++ b/htdocs/install/mysql/tables/llx_facturedet.sql
@@ -26,15 +26,16 @@ create table llx_facturedet
 (
   rowid							integer    AUTO_INCREMENT PRIMARY KEY,
   fk_facture					integer    NOT NULL,
-  fk_parent_line				integer	 NULL,
+  fk_parent_line				integer	   NULL,
   fk_product					integer    NULL,					-- Doit pouvoir etre nul pour ligne detail sans produits
   label							varchar(255) DEFAULT NULL,
   description					text,
+  vat_src_code					varchar(10)  DEFAULT '',			-- Vat code used as source of vat fields. Not strict foreign key here.
   tva_tx						double(6,3),						-- Vat rate (example 20%)
   localtax1_tx               	double(6,3)  DEFAULT 0,    		 	-- localtax1 rate
-  localtax1_type			 	varchar(10)	  	 NULL, 				 	-- localtax1 type
+  localtax1_type			 	varchar(10)	 NULL, 				 	-- localtax1 type
   localtax2_tx               	double(6,3)  DEFAULT 0,    		 	-- localtax2 rate
-  localtax2_type			 	varchar(10)	  	 NULL, 				 	-- localtax2 type
+  localtax2_type			 	varchar(10)	 NULL, 				 	-- localtax2 type
   qty							real,								-- Quantity (exemple 2)
   remise_percent				real       DEFAULT 0,				-- % de la remise ligne (exemple 20%)
   remise						real       DEFAULT 0,				-- Montant calcule de la remise % sur PU HT (exemple 20)
@@ -43,13 +44,13 @@ create table llx_facturedet
   price							double(24,8),						-- Deprecated (Do not use)
   total_ht						double(24,8),						-- Total HT de la ligne toute quantite et incluant remise ligne et globale
   total_tva						double(24,8),						-- Total TVA de la ligne toute quantite et incluant remise ligne et globale
-  total_localtax1				double(24,8) DEFAULT 0,			-- Total LocalTax1 for total quantity of line
-  total_localtax2				double(24,8) DEFAULT 0,			-- total LocalTax2 for total quantity of line
+  total_localtax1				double(24,8) DEFAULT 0,				-- Total LocalTax1 for total quantity of line
+  total_localtax2				double(24,8) DEFAULT 0,				-- Total LocalTax2 for total quantity of line
   total_ttc						double(24,8),						-- Total TTC de la ligne toute quantite et incluant remise ligne et globale
   product_type					integer    DEFAULT 0,
-  date_start					datetime   DEFAULT NULL,			-- date debut si service
-  date_end						datetime   DEFAULT NULL,			-- date fin si service
-  info_bits						integer    DEFAULT 0,				-- TVA NPR ou non
+  date_start					datetime   DEFAULT NULL,			-- date start if service
+  date_end						datetime   DEFAULT NULL,			-- date end if service
+  info_bits						integer    DEFAULT 0,				-- VAT NPR or not (for france only)
 
   buy_price_ht					double(24,8) DEFAULT 0,				-- buying price
   fk_product_fournisseur_price	integer      DEFAULT NULL,			-- reference of supplier price when line was added (may be used to update buy_price_ht current price when future invoice will be created)
@@ -58,19 +59,19 @@ create table llx_facturedet
   
   special_code					integer    DEFAULT 0,			    -- code pour les lignes speciales
   rang							integer    DEFAULT 0,				-- position of line
-  fk_contract_line  integer NULL,									-- id of contract line when invoice comes from contract lines
+  fk_contract_line  			integer NULL,						-- id of contract line when invoice comes from contract lines
   import_key					varchar(14),
 
-  situation_percent real,   -- % progression of lines invoicing
-  fk_prev_id        integer, -- id of the line in the previous situation,
-  fk_unit           integer DEFAULT NULL, -- id of the unit codeĀ”
+  situation_percent real,   										-- % progression of lines invoicing
+  fk_prev_id        integer, 										-- id of the line in the previous situation,
+  fk_unit           integer DEFAULT NULL, 							-- id of the unit codeĀ”
 
-  fk_multicurrency		integer,
+  fk_multicurrency				integer,
   multicurrency_code			varchar(255),
   multicurrency_subprice		double(24,8) DEFAULT 0,
   multicurrency_total_ht		double(24,8) DEFAULT 0,
-  multicurrency_total_tva	double(24,8) DEFAULT 0,
-  multicurrency_total_ttc	double(24,8) DEFAULT 0
+  multicurrency_total_tva		double(24,8) DEFAULT 0,
+  multicurrency_total_ttc		double(24,8) DEFAULT 0
 )ENGINE=innodb;
 
 -- 
diff --git a/htdocs/install/mysql/tables/llx_propaldet.sql b/htdocs/install/mysql/tables/llx_propaldet.sql
index 3f372894182..70029acdb08 100644
--- a/htdocs/install/mysql/tables/llx_propaldet.sql
+++ b/htdocs/install/mysql/tables/llx_propaldet.sql
@@ -28,7 +28,8 @@ create table llx_propaldet
   label								varchar(255) DEFAULT NULL,
   description						text,
   fk_remise_except					integer      NULL,               -- Lien vers table des remises fixes
-  tva_tx							double(6,3)  DEFAULT 0, 	     -- taux tva
+  vat_src_code						varchar(10)  DEFAULT '',		 -- Vat code used as source of vat fields. Not strict foreign key here.
+  tva_tx							double(6,3)  DEFAULT 0, 	     -- Vat rate
   localtax1_tx               		double(6,3)  DEFAULT 0,    		 -- localtax1 rate
   localtax1_type			 		varchar(10)	  	 NULL, 			 -- localtax1 type
   localtax2_tx               		double(6,3)  DEFAULT 0,    		 -- localtax2 rate
diff --git a/htdocs/install/mysql/tables/llx_supplier_proposaldet.sql b/htdocs/install/mysql/tables/llx_supplier_proposaldet.sql
index 22467c63e26..f94df8f2e74 100644
--- a/htdocs/install/mysql/tables/llx_supplier_proposaldet.sql
+++ b/htdocs/install/mysql/tables/llx_supplier_proposaldet.sql
@@ -23,7 +23,8 @@ CREATE TABLE llx_supplier_proposaldet (
   label varchar(255) DEFAULT NULL,
   description text,
   fk_remise_except integer DEFAULT NULL,
-  tva_tx double(6,3) DEFAULT 0,
+  vat_src_code					varchar(10) DEFAULT '',		-- Vat code used as source of vat fields. Not strict foreign key here.
+  tva_tx 						double(6,3) DEFAULT 0,		-- Vat rate
   localtax1_tx double(6,3) DEFAULT 0,
   localtax1_type varchar(10) DEFAULT NULL,
   localtax2_tx double(6,3) DEFAULT 0,
@@ -45,7 +46,6 @@ CREATE TABLE llx_supplier_proposaldet (
   special_code integer DEFAULT 0,
   rang integer DEFAULT 0,
   ref_fourn varchar(30) DEFAULT NULL,
-  
   fk_multicurrency        integer,
   multicurrency_code      varchar(255),
   multicurrency_subprice  double(24,8) DEFAULT 0,
diff --git a/htdocs/langs/en_US/accountancy.lang b/htdocs/langs/en_US/accountancy.lang
index a1859e0ad4b..f41bc17814d 100644
--- a/htdocs/langs/en_US/accountancy.lang
+++ b/htdocs/langs/en_US/accountancy.lang
@@ -24,7 +24,7 @@ AccountancyAreaDescActionFreq=The following actions are usually executed every m
 AccountancyAreaDescChartModel=STEP %s: Create a model of chart of account from menu %s
 AccountancyAreaDescChart=STEP %s: Create or check content of your chart of account from menu %s
 AccountancyAreaDescBank=STEP %s: Check the binding between bank accounts and accounting account is done. Complete missing bindings. This will save you time in future for the next steps by suggesting you the correct default accounting account on your payment lines.<br>For this, go on the card of each financial account. You can start from page %s.
-AccountancyAreaDescVat=STEP %s: Check the binding between vat payment and accounting account is done. Complete missing bindings. This will save you time in future for the next steps by suggesting you the correct default accounting account on record related to VAT payments.<br>You can set accounting accounts to use for each VAT from page %s.    
+AccountancyAreaDescVat=STEP %s: Check the binding between vat rates and accounting account is done. Complete missing bindings. This will save you time in future for the next steps by suggesting you the correct default accounting account on record related to VAT lines.<br>You can set accounting accounts to use for each VAT from page %s.    
 AccountancyAreaDescSal=STEP %s: Check the binding between salaries payment and accounting account is done. Complete missing bindings. This will save you time in future for the next steps by suggesting you the correct default accounting account on record related to payment of salaries.<br>For this you can use the menu entry %s.    
 AccountancyAreaDescContrib=STEP %s: Check the binding between special expences (miscellaneous taxes) and accounting account is done. Complete missing bindings. This will save you time in future for the next steps by suggesting you the correct default accounting account on record related to payments of taxes.<br>For this you can use the menu entry %s.    
 AccountancyAreaDescDonation=STEP %s: Check the binding between donation and accounting account is done. Complete missing bindings. This will save you time in future for the next steps by suggesting you the correct default accounting account on record related to payments of donation.<br>You can set the account dedicated for that from the menu entry %s.
@@ -46,6 +46,7 @@ AccountAccounting=Accounting account
 AccountAccountingShort=Account
 AccountAccountingSuggest=Accounting account suggest
 MenuDefaultAccounts=Default accounts
+MenuVatAccounts=Vat accounts
 MenuProductsAccounts=Product accounts
 ProductsBinding=Products accounts
 Ventilation=Binding to accounts
-- 
GitLab