From 5581e5da7289803ce19aab3fde0a5368a407634c Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Fri, 12 May 2017 13:12:17 +0200
Subject: [PATCH] FIX New vat code not correctly implemented if "1 price per
 customer".

---
 htdocs/comm/propal/card.php                     |  7 ++++---
 htdocs/install/mysql/migration/4.0.0-5.0.0.sql  |  3 +++
 .../mysql/tables/llx_product_customer_price.sql |  1 +
 .../tables/llx_product_customer_price_log.sql   |  1 +
 .../class/productcustomerprice.class.php        | 17 +++++++++++++----
 htdocs/product/price.php                        | 16 ++++++++++------
 6 files changed, 32 insertions(+), 13 deletions(-)

diff --git a/htdocs/comm/propal/card.php b/htdocs/comm/propal/card.php
index 150abdf5f46..407a461a1a1 100644
--- a/htdocs/comm/propal/card.php
+++ b/htdocs/comm/propal/card.php
@@ -750,7 +750,7 @@ if (empty($reshook))
 				$tva_tx = get_default_tva($mysoc, $object->thirdparty, $prod->id);
 				$tva_npr = get_default_npr($mysoc, $object->thirdparty, $prod->id);
 				if (empty($tva_tx)) $tva_npr=0;
-
+				
 				$pu_ht = $prod->price;
 				$pu_ttc = $prod->price_ttc;
 				$price_min = $prod->price_min;
@@ -775,15 +775,16 @@ if (empty($reshook))
 
 					$prodcustprice = new Productcustomerprice($db);
 
-					$filter = array('t.fk_product' => $prod->id,'t.fk_soc' => $object->thirdparty->id);
+					$filter = array('t.fk_product' => $prod->id, 't.fk_soc' => $object->thirdparty->id);
 
 					$result = $prodcustprice->fetch_all('', '', 0, 0, $filter);
 					if ($result) {
+					    // If there is some prices specific to the customer
 						if (count($prodcustprice->lines) > 0) {
 							$pu_ht = price($prodcustprice->lines[0]->price);
 							$pu_ttc = price($prodcustprice->lines[0]->price_ttc);
 							$price_base_type = $prodcustprice->lines[0]->price_base_type;
-							$tva_tx = $prodcustprice->lines[0]->tva_tx;
+							$tva_tx = ($prodcustprice->lines[0]->default_vat_code ? $prodcustprice->lines[0]->tva_tx . ' ('.$prodcustprice->lines[0]->default_vat_code.' )' : $prodcustprice->lines[0]->tva_tx);
 						}
 					}
 				}
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 dd4ac9fde4b..fa185f2864b 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
@@ -263,6 +263,9 @@ update llx_accounting_account set account_parent = 0 where account_parent = '';
 ALTER TABLE llx_product_price ALTER COLUMN date_price SET DEFAULT NULL;
  
 ALTER TABLE llx_product_price ADD COLUMN default_vat_code	varchar(10) after tva_tx;
+ALTER TABLE llx_product_customer_price ADD COLUMN default_vat_code	varchar(10) after tva_tx;
+ALTER TABLE llx_product_customer_price_log ADD COLUMN default_vat_code	varchar(10) after tva_tx;
 ALTER TABLE llx_product_fournisseur_price ADD COLUMN default_vat_code	varchar(10) after tva_tx;
 
+
 ALTER TABLE llx_events MODIFY COLUMN ip varchar(250);
diff --git a/htdocs/install/mysql/tables/llx_product_customer_price.sql b/htdocs/install/mysql/tables/llx_product_customer_price.sql
index 8098ae5f028..339de2bb4be 100644
--- a/htdocs/install/mysql/tables/llx_product_customer_price.sql
+++ b/htdocs/install/mysql/tables/llx_product_customer_price.sql
@@ -33,6 +33,7 @@ create table llx_product_customer_price
   price_min					double(24,8) DEFAULT 0,
   price_min_ttc				double(24,8) DEFAULT 0,
   price_base_type			varchar(3)   DEFAULT 'HT',
+  default_vat_code			varchar(10),	         		-- Same code than into table llx_c_tva (but no constraints). Should be used in priority to find default vat, npr, localtaxes for product.
   tva_tx					double(6,3),
   recuperableonly           integer NOT NULL DEFAULT '0',   -- Other NPR VAT
   localtax1_tx				double(6,3)  DEFAULT 0,         -- Other local VAT 1 
diff --git a/htdocs/install/mysql/tables/llx_product_customer_price_log.sql b/htdocs/install/mysql/tables/llx_product_customer_price_log.sql
index 699060cdc98..bd4fe54a586 100644
--- a/htdocs/install/mysql/tables/llx_product_customer_price_log.sql
+++ b/htdocs/install/mysql/tables/llx_product_customer_price_log.sql
@@ -30,6 +30,7 @@ create table llx_product_customer_price_log
   price_min					double(24,8) DEFAULT 0,
   price_min_ttc				double(24,8) DEFAULT 0,
   price_base_type			varchar(3)   DEFAULT 'HT',
+  default_vat_code			varchar(10),	         		-- Same code than into table llx_c_tva (but no constraints). Should be used in priority to find default vat, npr, localtaxes for product.
   tva_tx					double(6,3),
   recuperableonly           integer NOT NULL DEFAULT '0',   -- Other NPR VAT
   localtax1_tx				double(6,3)  DEFAULT 0,         -- Other local VAT 1 
diff --git a/htdocs/product/class/productcustomerprice.class.php b/htdocs/product/class/productcustomerprice.class.php
index fd36c55dcf9..5b9050077c9 100644
--- a/htdocs/product/class/productcustomerprice.class.php
+++ b/htdocs/product/class/productcustomerprice.class.php
@@ -140,7 +140,6 @@ class Productcustomerprice extends CommonObject
 
 		// Insert request
 		$sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_customer_price(";
-
 		$sql .= "entity,";
 		$sql .= "datec,";
 		$sql .= "fk_product,";
@@ -150,6 +149,7 @@ class Productcustomerprice extends CommonObject
 		$sql .= "price_min,";
 		$sql .= "price_min_ttc,";
 		$sql .= "price_base_type,";
+		$sql .= "default_vat_code,";
 		$sql .= "tva_tx,";
 		$sql .= "recuperableonly,";
 		$sql .= "localtax1_type,";
@@ -158,9 +158,7 @@ class Productcustomerprice extends CommonObject
 		$sql .= "localtax2_tx,";
 		$sql .= "fk_user,";
 		$sql .= "import_key";
-
 		$sql .= ") VALUES (";
-
 		$sql .= " " . $conf->entity . ",";
 		$sql .= " '" . $this->db->idate(dol_now()) . "',";
 		$sql .= " " . (! isset($this->fk_product) ? 'NULL' : "'" . $this->fk_product . "'") . ",";
@@ -170,6 +168,7 @@ class Productcustomerprice extends CommonObject
 		$sql .= " " . (empty($this->price_min) ? '0' : "'" . $this->price_min . "'") . ",";
 		$sql .= " " . (empty($this->price_min_ttc) ? '0' : "'" . $this->price_min_ttc . "'") . ",";
 		$sql .= " " . (! isset($this->price_base_type) ? 'NULL' : "'" . $this->db->escape($this->price_base_type) . "'") . ",";
+		$sql .= " ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
 		$sql .= " " . (! isset($this->tva_tx) ? 'NULL' : "'" . $this->tva_tx . "'") . ",";
 		$sql .= " " . (! isset($this->recuperableonly) ? 'NULL' : "'" . $this->recuperableonly . "'") . ",";
 		$sql .= " " . (empty($this->localtax1_type) ? "'0'" : "'" . $this->localtax1_type . "'") . ",";
@@ -178,7 +177,6 @@ class Productcustomerprice extends CommonObject
 		$sql .= " " . (! isset($this->localtax2_tx) ? 'NULL' : "'" . $this->localtax2_tx . "'") . ",";
 		$sql .= " " . $user->id . ",";
 		$sql .= " " . (! isset($this->import_key) ? 'NULL' : "'" . $this->db->escape($this->import_key) . "'") . "";
-
 		$sql .= ")";
 
 		$this->db->begin();
@@ -250,6 +248,7 @@ class Productcustomerprice extends CommonObject
 		$sql .= " t.price_min,";
 		$sql .= " t.price_min_ttc,";
 		$sql .= " t.price_base_type,";
+		$sql .= " t.default_vat_code,";
 		$sql .= " t.tva_tx,";
 		$sql .= " t.recuperableonly,";
 		$sql .= " t.localtax1_tx,";
@@ -278,6 +277,7 @@ class Productcustomerprice extends CommonObject
 				$this->price_min = $obj->price_min;
 				$this->price_min_ttc = $obj->price_min_ttc;
 				$this->price_base_type = $obj->price_base_type;
+				$this->default_vat_code = $obj->default_vat_code;
 				$this->tva_tx = $obj->tva_tx;
 				$this->recuperableonly = $obj->recuperableonly;
 				$this->localtax1_tx = $obj->localtax1_tx;
@@ -324,6 +324,7 @@ class Productcustomerprice extends CommonObject
 		$sql .= " t.price_min,";
 		$sql .= " t.price_min_ttc,";
 		$sql .= " t.price_base_type,";
+		$sql .= " t.default_vat_code,";
 		$sql .= " t.tva_tx,";
 		$sql .= " t.recuperableonly,";
 		$sql .= " t.localtax1_tx,";
@@ -380,6 +381,7 @@ class Productcustomerprice extends CommonObject
 				$line->price_min = $obj->price_min;
 				$line->price_min_ttc = $obj->price_min_ttc;
 				$line->price_base_type = $obj->price_base_type;
+				$line->default_vat_code = $obj->default_vat_code;
 				$line->tva_tx = $obj->tva_tx;
 				$line->recuperableonly = $obj->recuperableonly;
 				$line->localtax1_tx = $obj->localtax1_tx;
@@ -429,6 +431,7 @@ class Productcustomerprice extends CommonObject
 		$sql .= " t.price_min,";
 		$sql .= " t.price_min_ttc,";
 		$sql .= " t.price_base_type,";
+		$sql .= " t.default_vat_code,";
 		$sql .= " t.tva_tx,";
 		$sql .= " t.recuperableonly,";
 		$sql .= " t.localtax1_tx,";
@@ -484,6 +487,7 @@ class Productcustomerprice extends CommonObject
 				$line->price_min = $obj->price_min;
 				$line->price_min_ttc = $obj->price_min_ttc;
 				$line->price_base_type = $obj->price_base_type;
+				$line->default_vat_code = $obj->default_vat_code;
 				$line->tva_tx = $obj->tva_tx;
 				$line->recuperableonly = $obj->recuperableonly;
 				$line->localtax1_tx = $obj->localtax1_tx;
@@ -595,6 +599,7 @@ class Productcustomerprice extends CommonObject
 		$sql .= "price_min,";
 		$sql .= "price_min_ttc,";
 		$sql .= "price_base_type,";
+		$sql .= "default_vat_code,";
 		$sql .= "tva_tx,";
 		$sql .= "recuperableonly,";
 		$sql .= "localtax1_tx,";
@@ -616,6 +621,7 @@ class Productcustomerprice extends CommonObject
 		$sql .= " t.price_min,";
 		$sql .= " t.price_min_ttc,";
 		$sql .= " t.price_base_type,";
+		$sql .= " t.default_vat_code,";
 		$sql .= " t.tva_tx,";
 		$sql .= " t.recuperableonly,";
 		$sql .= " t.localtax1_tx,";
@@ -649,6 +655,7 @@ class Productcustomerprice extends CommonObject
 		$sql .= " price_min=" . (isset($this->price_min) ? $this->price_min : "null") . ",";
 		$sql .= " price_min_ttc=" . (isset($this->price_min_ttc) ? $this->price_min_ttc : "null") . ",";
 		$sql .= " price_base_type=" . (isset($this->price_base_type) ? "'" . $this->db->escape($this->price_base_type) . "'" : "null") . ",";
+		$sql .= " default_vat_code = ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
 		$sql .= " tva_tx=" . (isset($this->tva_tx) ? $this->tva_tx : "null") . ",";
 		$sql .= " recuperableonly=" . (isset($this->recuperableonly) ? $this->recuperableonly : "null") . ",";
 		$sql .= " localtax1_tx=" . (isset($this->localtax1_tx) ? $this->localtax1_tx : "null") . ",";
@@ -918,6 +925,7 @@ class Productcustomerprice extends CommonObject
 		$this->price_min = '';
 		$this->price_min_ttc = '';
 		$this->price_base_type = '';
+		$this->default_vat_code = '';
 		$this->tva_tx = '';
 		$this->recuperableonly = '';
 		$this->localtax1_tx = '';
@@ -943,6 +951,7 @@ class PriceByCustomerLine
 	var $price_min;
 	var $price_min_ttc;
 	var $price_base_type;
+	var $default_vat_code;
 	var $tva_tx;
 	var $recuperableonly;
 	var $localtax1_tx;
diff --git a/htdocs/product/price.php b/htdocs/product/price.php
index e6bde28d27e..1e9bcec3d52 100644
--- a/htdocs/product/price.php
+++ b/htdocs/product/price.php
@@ -1518,13 +1518,13 @@ if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES))
 		print '<tr>';
 		print '<td class="fieldrequired">' . $langs->trans('ThirdParty') . '</td>';
 		print '<td>';
-		print $form->select_company('', 'socid', 's.client in (1,2,3) AND s.rowid NOT IN (SELECT fk_soc FROM ' . MAIN_DB_PREFIX . 'product_customer_price WHERE fk_product='.$object->id.')', 'SelectThirdParty', 0, 0, array(), 0, 'minwidth300');
+		print $form->select_company('', 'socid', 's.client in (1,2,3)', 'SelectThirdParty', 0, 0, array(), 0, 'minwidth300');
 		print '</td>';
 		print '</tr>';
 
 		// VAT
 		print '<tr><td class="fieldrequired">' . $langs->trans("VATRate") . '</td><td>';
-		print $form->load_tva("tva_tx", $object->tva_tx, $mysoc, '', $object->id, $object->tva_npr, $object->type, false, 1);
+		print $form->load_tva("tva_tx", $object->default_vat_code ? $object->tva_tx.' ('.$object->default_vat_code.')' : $object->tva_tx, $mysoc, '', $object->id, $object->tva_npr, $object->type, false, 1);
 		print '</td></tr>';
 
 		// Price base
@@ -1610,9 +1610,9 @@ if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES))
 
 		// VAT
 		print '<tr><td>' . $langs->trans("VATRate") . '</td><td colspan="2">';
-		print $form->load_tva("tva_tx", $prodcustprice->tva_tx, $mysoc, '', $object->id, $prodcustprice->recuperableonly, $object->type, false, 1);
+		print $form->load_tva("tva_tx", $prodcustprice->default_vat_code ? $prodcustprice->tva_tx.' ('.$prodcustprice->default_vat_code.')' : $prodcustprice->tva_tx, $mysoc, '', $object->id, $prodcustprice->recuperableonly, $object->type, false, 1);
 		print '</td></tr>';
-
+		
 		// Price base
 		print '<tr><td width="15%">';
 		print $langs->trans('PriceBase');
@@ -1731,8 +1731,10 @@ if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES))
 				print "<td>" . $staticsoc->getNomUrl(1) . "</td>";
 				print "<td>" . dol_print_date($line->datec, "dayhour") . "</td>";
 
+				$tva_tx = $line->default_vat_code ? $line->tva_tx.' ('.$line->default_vat_code.')' : $line->tva_tx;
+				
 				print '<td align="center">' . $langs->trans($line->price_base_type) . "</td>";
-				print '<td align="right">' . vatrate($line->tva_tx, true, $line->recuperableonly) . "</td>";
+				print '<td align="right">' . vatrate($tva_tx, true, $line->recuperableonly) . "</td>";
 				print '<td align="right">' . price($line->price) . "</td>";
 				print '<td align="right">' . price($line->price_ttc) . "</td>";
 				print '<td align="right">' . price($line->price_min) . '</td>';
@@ -1844,11 +1846,13 @@ if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES))
 				$staticsoc = new Societe($db);
 				$staticsoc->fetch($line->fk_soc);
 
+				$tva_tx = $line->default_vat_code ? $line->tva_tx.' ('.$line->default_vat_code.')' : $line->tva_tx;
+				
 				print "<td>" . $staticsoc->getNomUrl(1) . "</td>";
 				print "<td>" . dol_print_date($line->datec, "dayhour") . "</td>";
 
 				print '<td align="center">' . $langs->trans($line->price_base_type) . "</td>";
-				print '<td align="right">' . vatrate($line->tva_tx, true, $line->recuperableonly) . "</td>";
+				print '<td align="right">' . vatrate($tva_tx, true, $line->recuperableonly) . "</td>";
 				print '<td align="right">' . price($line->price) . "</td>";
 				print '<td align="right">' . price($line->price_ttc) . "</td>";
 				print '<td align="right">' . price($line->price_min) . '</td>';
-- 
GitLab