From b191330a79011f2c41392373aaeb656e80ec06bb Mon Sep 17 00:00:00 2001 From: David Pareja Rodriguez Date: Tue, 28 Feb 2023 13:24:11 +0100 Subject: [PATCH 01/11] Ignore localtaxes if localtax1_type or localtax2_type is 0 --- htdocs/core/lib/functions.lib.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index c275e4fd310..73bd7419a8c 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -5688,7 +5688,7 @@ function isOnlyOneLocalTax($local) function get_localtax_by_third($local) { global $db, $mysoc; - $sql = "SELECT t.localtax1, t.localtax2 "; + $sql = "SELECT t.localtax1_type, t.localtax2_type, t.localtax1, t.localtax2 "; $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t inner join ".MAIN_DB_PREFIX."c_country as c ON c.rowid=t.fk_pays"; $sql .= " WHERE c.code = '".$db->escape($mysoc->country_code)."' AND t.active = 1 AND t.taux=("; $sql .= " SELECT max(tt.taux) FROM ".MAIN_DB_PREFIX."c_tva as tt inner join ".MAIN_DB_PREFIX."c_country as c ON c.rowid=tt.fk_pays"; @@ -5698,9 +5698,9 @@ function get_localtax_by_third($local) $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); - if ($local == 1) { + if ($local == 1 && $obj->localtax1_type > 0) { return $obj->localtax1; - } elseif ($local == 2) { + } elseif ($local == 2 && $obj->localtax2_type > 0) { return $obj->localtax2; } } From 590373a620495bcaf5561089e99ca03c3ef1babd Mon Sep 17 00:00:00 2001 From: David Pareja Rodriguez Date: Tue, 28 Feb 2023 13:33:52 +0100 Subject: [PATCH 02/11] Forgot to loop on the results --- htdocs/core/lib/functions.lib.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 73bd7419a8c..e2bbb122176 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -5697,11 +5697,12 @@ function get_localtax_by_third($local) $resql = $db->query($sql); if ($resql) { - $obj = $db->fetch_object($resql); - if ($local == 1 && $obj->localtax1_type > 0) { - return $obj->localtax1; - } elseif ($local == 2 && $obj->localtax2_type > 0) { - return $obj->localtax2; + while ($obj = $db->fetch_object($resql)) { + if ($local == 1 && $obj->localtax1_type > 0) { + return $obj->localtax1; + } elseif ($local == 2 && $obj->localtax2_type > 0) { + return $obj->localtax2; + } } } From 927d0a2fa15b61658c58763c0f5c05fed71a0505 Mon Sep 17 00:00:00 2001 From: David Pareja Rodriguez Date: Mon, 13 Mar 2023 11:16:37 +0100 Subject: [PATCH 03/11] ORDER BY rowid --- htdocs/core/lib/functions.lib.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index e2bbb122176..4d0367562df 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -5688,22 +5688,20 @@ function isOnlyOneLocalTax($local) function get_localtax_by_third($local) { global $db, $mysoc; - $sql = "SELECT t.localtax1_type, t.localtax2_type, t.localtax1, t.localtax2 "; + + $sql = " SELECT t.localtax$local as localtax"; $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t inner join ".MAIN_DB_PREFIX."c_country as c ON c.rowid=t.fk_pays"; $sql .= " WHERE c.code = '".$db->escape($mysoc->country_code)."' AND t.active = 1 AND t.taux=("; $sql .= " SELECT max(tt.taux) FROM ".MAIN_DB_PREFIX."c_tva as tt inner join ".MAIN_DB_PREFIX."c_country as c ON c.rowid=tt.fk_pays"; $sql .= " WHERE c.code = '".$db->escape($mysoc->country_code)."' AND tt.active = 1"; - $sql .= " )"; + $sql .= " ) "; + $sql .= " AND t.localtax${local}_type > 0"; + $sql .= " ORDER BY rowid DESC"; $resql = $db->query($sql); if ($resql) { - while ($obj = $db->fetch_object($resql)) { - if ($local == 1 && $obj->localtax1_type > 0) { - return $obj->localtax1; - } elseif ($local == 2 && $obj->localtax2_type > 0) { - return $obj->localtax2; - } - } + $obj = $db->fetch_object($resql); + return $obj->localtax; } return 0; From 91cf865583653ad6e5f79d0c2360b2243cd257df Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 18 Mar 2023 10:41:07 +0100 Subject: [PATCH 04/11] Update functions.lib.php --- htdocs/core/lib/functions.lib.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 4d0367562df..c1459b1206b 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -5689,13 +5689,13 @@ function get_localtax_by_third($local) { global $db, $mysoc; - $sql = " SELECT t.localtax$local as localtax"; + $sql = " SELECT t.localtax".$local." as localtax"; $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t inner join ".MAIN_DB_PREFIX."c_country as c ON c.rowid=t.fk_pays"; $sql .= " WHERE c.code = '".$db->escape($mysoc->country_code)."' AND t.active = 1 AND t.taux=("; $sql .= " SELECT max(tt.taux) FROM ".MAIN_DB_PREFIX."c_tva as tt inner join ".MAIN_DB_PREFIX."c_country as c ON c.rowid=tt.fk_pays"; $sql .= " WHERE c.code = '".$db->escape($mysoc->country_code)."' AND tt.active = 1"; $sql .= " ) "; - $sql .= " AND t.localtax${local}_type > 0"; + $sql .= " AND t.localtax".$local."_type > 0"; $sql .= " ORDER BY rowid DESC"; $resql = $db->query($sql); From 8d2a3a863da8c6ecd97cceff3e164317834e0022 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 18 Mar 2023 10:48:09 +0100 Subject: [PATCH 05/11] Add phpunit for get_localtax_by_third --- test/phpunit/FunctionsLibTest.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index 1bf3b6378fb..3b6a4d44942 100644 --- a/test/phpunit/FunctionsLibTest.php +++ b/test/phpunit/FunctionsLibTest.php @@ -1168,7 +1168,7 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase } /** - * testGetDefaultTva + * testGetDefaultLocalTax * * @return void */ @@ -1255,6 +1255,27 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase } + /** + * testGetLocalTaxByThird + * + * @return void + */ + public function testGetLocalTaxByThird() + { + global $mysoc; + + $mysoc->country_code = 'ES'; + + $result = get_localtax_by_third(1); + print __METHOD__." result=".$result."\n"; + $this->assertEquals('5.2', $result); + + $result = get_localtax_by_third(2); + print __METHOD__." result=".$result."\n"; + $this->assertEquals('-19:-15:-9', $result); + } + + /** * testDolExplodeIntoArray * From 6e17c0c96183bfcca015bc415d658b5079887af7 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 18 Mar 2023 10:51:07 +0100 Subject: [PATCH 06/11] Merge branch '15.0' of git@github.com:Dolibarr/dolibarr.git into 15.0 --- htdocs/core/lib/functions.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index ef5b837587d..f32d983150f 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -5696,7 +5696,7 @@ function get_localtax_by_third($local) $sql .= " WHERE c.code = '".$db->escape($mysoc->country_code)."' AND tt.active = 1"; $sql .= " ) "; $sql .= " AND t.localtax".$local."_type > 0"; - $sql .= " ORDER BY rowid DESC"; + $sql .= " ORDER BY t.rowid DESC"; $resql = $db->query($sql); if ($resql) { From 502badad4b68e850c6954dd13a6e689ae7cc54d9 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 19 Mar 2023 11:25:36 +0100 Subject: [PATCH 07/11] Better error management --- htdocs/core/class/html.formcompany.class.php | 12 ++++++------ htdocs/core/lib/functions.lib.php | 17 +++++++++-------- .../install/mysql/migration/14.0.0-15.0.0.sql | 4 ++++ 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/htdocs/core/class/html.formcompany.class.php b/htdocs/core/class/html.formcompany.class.php index 103db86998c..a42ffc604a3 100644 --- a/htdocs/core/class/html.formcompany.class.php +++ b/htdocs/core/class/html.formcompany.class.php @@ -984,15 +984,15 @@ class FormCompany extends Form // phpcs:enable $tax = get_localtax_by_third($local); - $num = $this->db->num_rows($tax); - $i = 0; - if ($num) { + if ($tax) { $valors = explode(":", $tax); + $nbvalues = count($valors); - if (count($valors) > 1) { + if ($nbvalues > 1) { //montar select print ''; + print ''; } } } diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index f32d983150f..9c3126c3a80 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -5682,19 +5682,18 @@ function isOnlyOneLocalTax($local) /** * Get values of localtaxes (1 or 2) for company country for the common vat with the highest value * - * @param int $local LocalTax to get - * @return number Values of localtax + * @param int $local LocalTax to get + * @return string Values of localtax (Can be '20', '-19:-15:-9') */ function get_localtax_by_third($local) { global $db, $mysoc; $sql = " SELECT t.localtax".$local." as localtax"; - $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t inner join ".MAIN_DB_PREFIX."c_country as c ON c.rowid=t.fk_pays"; - $sql .= " WHERE c.code = '".$db->escape($mysoc->country_code)."' AND t.active = 1 AND t.taux=("; - $sql .= " SELECT max(tt.taux) FROM ".MAIN_DB_PREFIX."c_tva as tt inner join ".MAIN_DB_PREFIX."c_country as c ON c.rowid=tt.fk_pays"; - $sql .= " WHERE c.code = '".$db->escape($mysoc->country_code)."' AND tt.active = 1"; - $sql .= " ) "; + $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t INNER JOIN ".MAIN_DB_PREFIX."c_country as c ON c.rowid = t.fk_pays"; + $sql .= " WHERE c.code = '".$db->escape($mysoc->country_code)."' AND t.active = 1 AND t.taux = ("; + $sql .= "SELECT MAX(tt.taux) FROM ".MAIN_DB_PREFIX."c_tva as tt INNER JOIN ".MAIN_DB_PREFIX."c_country as c ON c.rowid = tt.fk_pays"; + $sql .= " WHERE c.code = '".$db->escape($mysoc->country_code)."' AND tt.active = 1)"; $sql .= " AND t.localtax".$local."_type > 0"; $sql .= " ORDER BY t.rowid DESC"; @@ -5702,9 +5701,11 @@ function get_localtax_by_third($local) if ($resql) { $obj = $db->fetch_object($resql); return $obj->localtax; + } else { + return 'Error'; } - return 0; + return '0'; } diff --git a/htdocs/install/mysql/migration/14.0.0-15.0.0.sql b/htdocs/install/mysql/migration/14.0.0-15.0.0.sql index bef7621d282..4b2655818c1 100644 --- a/htdocs/install/mysql/migration/14.0.0-15.0.0.sql +++ b/htdocs/install/mysql/migration/14.0.0-15.0.0.sql @@ -533,3 +533,7 @@ ALTER TABLE llx_bank ADD COLUMN amount_main_currency double(24,8) NULL; ALTER TABLE llx_commande_fournisseurdet MODIFY COLUMN ref varchar(128); ALTER TABLE llx_facture_fourn_det MODIFY COLUMN ref varchar(128); + +ALTER TABLE llx_c_tva SET localtax2 = '-19:-15:-9' WHERE localtax2 = '-19' AND localtax2_type = '5' AND fk_pays = 4 AND rowid = 44; + + From c895f366a10f4f8449657d3d58b5690cdb78f635 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 19 Mar 2023 11:36:06 +0100 Subject: [PATCH 08/11] Fix sql --- htdocs/install/mysql/migration/14.0.0-15.0.0.sql | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/htdocs/install/mysql/migration/14.0.0-15.0.0.sql b/htdocs/install/mysql/migration/14.0.0-15.0.0.sql index 4b2655818c1..67ff6f1fab3 100644 --- a/htdocs/install/mysql/migration/14.0.0-15.0.0.sql +++ b/htdocs/install/mysql/migration/14.0.0-15.0.0.sql @@ -534,6 +534,5 @@ ALTER TABLE llx_bank ADD COLUMN amount_main_currency double(24,8) NULL; ALTER TABLE llx_commande_fournisseurdet MODIFY COLUMN ref varchar(128); ALTER TABLE llx_facture_fourn_det MODIFY COLUMN ref varchar(128); -ALTER TABLE llx_c_tva SET localtax2 = '-19:-15:-9' WHERE localtax2 = '-19' AND localtax2_type = '5' AND fk_pays = 4 AND rowid = 44; - +UPDATE llx_c_tva SET localtax2 = '-19:-15:-9' WHERE localtax2 = '-19' AND localtax2_type = '5' AND fk_pays = 4 AND taux = 21; From d3f679f2b1923a9ba4ea1d3353eabf0fe3abb6f3 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 19 Mar 2023 12:54:11 +0100 Subject: [PATCH 09/11] Fix sql regression --- htdocs/core/lib/functions.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 9c3126c3a80..4f33a68811c 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -5694,7 +5694,7 @@ function get_localtax_by_third($local) $sql .= " WHERE c.code = '".$db->escape($mysoc->country_code)."' AND t.active = 1 AND t.taux = ("; $sql .= "SELECT MAX(tt.taux) FROM ".MAIN_DB_PREFIX."c_tva as tt INNER JOIN ".MAIN_DB_PREFIX."c_country as c ON c.rowid = tt.fk_pays"; $sql .= " WHERE c.code = '".$db->escape($mysoc->country_code)."' AND tt.active = 1)"; - $sql .= " AND t.localtax".$local."_type > 0"; + $sql .= " AND t.localtax".$local."_type <> '0'"; $sql .= " ORDER BY t.rowid DESC"; $resql = $db->query($sql); From 249a6ea9a9c76cbd1aaaf99487a427c0b5301990 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 19 Mar 2023 14:49:54 +0100 Subject: [PATCH 10/11] Remove warning --- htdocs/core/lib/functions.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 4f33a68811c..3f25d9cdfd0 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -5814,7 +5814,7 @@ function getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisi } $sql .= ", ".MAIN_DB_PREFIX."c_country as c"; - if ($mysoc->country_code == 'ES') { + if (!empty($mysoc) && $mysoc->country_code == 'ES') { $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($buyer->country_code)."'"; // local tax in spain use the buyer country ?? } else { $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape(empty($seller->country_code) ? $mysoc->country_code : $seller->country_code)."'"; From 88353ee5ba3c2bcf7a840d8ce19e1fea38bdd03c Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 19 Mar 2023 17:38:22 +0100 Subject: [PATCH 11/11] Fix tests --- htdocs/compta/facture/class/facture.class.php | 1 + test/phpunit/NumberingModulesTest.php | 2 ++ 2 files changed, 3 insertions(+) diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index 484b44851fd..ec36314c999 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -3192,6 +3192,7 @@ class Facture extends CommonInvoice /** * Add an invoice line into database (linked to product/service or not). + * Note: ->thirdparty must be defined. * Les parametres sont deja cense etre juste et avec valeurs finales a l'appel * de cette methode. Aussi, pour le taux tva, il doit deja avoir ete defini * par l'appelant par la methode get_default_tva(societe_vendeuse,societe_acheteuse,produit) diff --git a/test/phpunit/NumberingModulesTest.php b/test/phpunit/NumberingModulesTest.php index ceab1948732..755eeb68956 100644 --- a/test/phpunit/NumberingModulesTest.php +++ b/test/phpunit/NumberingModulesTest.php @@ -152,6 +152,8 @@ class NumberingModulesTest extends PHPUnit\Framework\TestCase $localobject=new Facture($this->savdb); $localobject->initAsSpecimen(); + $localobject->fetch_thirdparty(); + $localobject->date=dol_mktime(12, 0, 0, 1, 1, 1915); // we use year 1915 to be sure to not have existing invoice for this year (usefull only if numbering is {0000@1} $numbering=new mod_facture_mercure(); $result=$numbering->getNextValue($mysoc, $localobject);