diff --git a/htdocs/admin/dict.php b/htdocs/admin/dict.php index 5d4631cb362..415d3452e00 100644 --- a/htdocs/admin/dict.php +++ b/htdocs/admin/dict.php @@ -8,7 +8,7 @@ * Copyright (C) 2011 Remy Younes * Copyright (C) 2012-2015 Marcos García * Copyright (C) 2012 Christophe Battarel - * Copyright (C) 2011-2022 Alexandre Spangaro + * Copyright (C) 2011-2023 Alexandre Spangaro * Copyright (C) 2015 Ferran Marcet * Copyright (C) 2016 Raphaël Doursenaud * Copyright (C) 2019-2022 Frédéric France @@ -208,7 +208,7 @@ $tabsql[6] = "SELECT a.id as rowid, a.code as code, a.libelle AS libelle, a.t $tabsql[7] = "SELECT a.id as rowid, a.code as code, a.libelle AS libelle, a.accountancy_code as accountancy_code, c.code as country_code, c.label as country, a.fk_pays as country_id, a.active FROM ".MAIN_DB_PREFIX."c_chargesociales AS a, ".MAIN_DB_PREFIX."c_country as c WHERE a.fk_pays = c.rowid and c.active = 1"; $tabsql[8] = "SELECT t.id as rowid, t.code as code, t.libelle, t.fk_country as country_id, c.code as country_code, c.label as country, t.position, t.active FROM ".MAIN_DB_PREFIX."c_typent as t LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON t.fk_country=c.rowid"; $tabsql[9] = "SELECT c.code_iso as code, c.label, c.unicode, c.active FROM ".MAIN_DB_PREFIX."c_currencies AS c"; -$tabsql[10] = "SELECT t.rowid, t.code, t.taux, t.localtax1_type, t.localtax1, t.localtax2_type, t.localtax2, c.label as country, c.code as country_code, t.fk_pays as country_id, t.recuperableonly, t.note, t.active, t.accountancy_code_sell, t.accountancy_code_buy FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c WHERE t.fk_pays = c.rowid"; +$tabsql[10] = "SELECT t.rowid, t.entity, t.code, t.taux, t.localtax1_type, t.localtax1, t.localtax2_type, t.localtax2, c.label as country, c.code as country_code, t.fk_pays as country_id, t.recuperableonly, t.note, t.active, t.accountancy_code_sell, t.accountancy_code_buy FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c WHERE t.fk_pays = c.rowid AND t.entity = ".getEntity($tabname[10]); $tabsql[11] = "SELECT t.rowid as rowid, t.element, t.source, t.code, t.libelle, t.position, t.active FROM ".MAIN_DB_PREFIX."c_type_contact AS t"; $tabsql[12] = "SELECT c.rowid as rowid, c.code, c.libelle, c.libelle_facture, c.deposit_percent, c.nbjour, c.type_cdr, c.decalage, c.active, c.sortorder, c.entity FROM ".MAIN_DB_PREFIX."c_payment_term AS c WHERE c.entity = ".getEntity($tabname[12]); $tabsql[13] = "SELECT c.id as rowid, c.code, c.libelle, c.type, c.active, c.entity FROM ".MAIN_DB_PREFIX."c_paiement AS c WHERE c.entity = ".getEntity($tabname[13]); @@ -396,7 +396,7 @@ $tabfieldinsert[6] = "code,libelle,type,color,position"; $tabfieldinsert[7] = "code,libelle,fk_pays,accountancy_code"; $tabfieldinsert[8] = "code,libelle,fk_country".(!empty($conf->global->SOCIETE_SORT_ON_TYPEENT) ? ',position' : ''); $tabfieldinsert[9] = "code_iso,label,unicode"; -$tabfieldinsert[10] = "fk_pays,code,taux,localtax1_type,localtax1,localtax2_type,localtax2,recuperableonly,accountancy_code_sell,accountancy_code_buy,note"; +$tabfieldinsert[10] = "fk_pays,code,taux,localtax1_type,localtax1,localtax2_type,localtax2,recuperableonly,accountancy_code_sell,accountancy_code_buy,note,entity"; $tabfieldinsert[11] = "element,source,code,libelle,position"; $tabfieldinsert[12] = "code,libelle,libelle_facture,deposit_percent,nbjour,type_cdr,decalage,sortorder,entity"; $tabfieldinsert[13] = "code,libelle,type,entity"; diff --git a/htdocs/admin/limits.php b/htdocs/admin/limits.php index 03efcf7aea5..06dfa06ce1a 100644 --- a/htdocs/admin/limits.php +++ b/htdocs/admin/limits.php @@ -2,6 +2,7 @@ /* Copyright (C) 2007-2022 Laurent Destailleur * Copyright (C) 2009-2018 Regis Houssin * Copyright (C) 2010 Juanjo Menent + * Copyright (C) 2023 Alexandre Spangaro * * 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 @@ -262,6 +263,7 @@ if (empty($mysoc->country_code)) { $sql = "SELECT taux as vat_rate, t.code as vat_code, t.localtax1 as localtax_rate1, t.localtax2 as localtax_rate2"; $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c"; $sql .= " WHERE t.active=1 AND t.fk_pays = c.rowid AND c.code='".$db->escape($mysoc->country_code)."' AND (t.taux <> 0 OR t.localtax1 <> '0' OR t.localtax2 <> '0')"; + $sql .= " AND t.entity IN (".getEntity('c_tva').")"; $sql .= " ORDER BY t.taux ASC"; $resql = $db->query($sql); if ($resql) { diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index e067fbcf56f..051c49f7ae2 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -15,7 +15,7 @@ * Copyright (C) 2012-2016 Marcos García * Copyright (C) 2012 Cedric Salvador * Copyright (C) 2012-2015 Raphaël Doursenaud - * Copyright (C) 2014-2020 Alexandre Spangaro + * Copyright (C) 2014-2023 Alexandre Spangaro * Copyright (C) 2018-2022 Ferran Marcet * Copyright (C) 2018-2021 Frédéric France * Copyright (C) 2018 Nicolas ZABOURI @@ -6326,6 +6326,7 @@ class Form $sql .= " FROM " . $this->db->prefix() . "c_tva as t, " . $this->db->prefix() . "c_country as c"; $sql .= " WHERE t.fk_pays = c.rowid"; $sql .= " AND t.active > 0"; + $sql .= " AND t.entity IN (".getEntity('c_tva').")"; $sql .= " AND c.code IN (" . $this->db->sanitize($country_code, 1) . ")"; $sql .= " ORDER BY t.code ASC, t.taux ASC, t.recuperableonly ASC"; diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 8e1b4d62a45..cff33fee2cc 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -6235,6 +6235,7 @@ function get_localtax($vatrate, $local, $thirdparty_buyer = "", $thirdparty_sell $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c"; $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($thirdparty_seller->country_code)."'"; $sql .= " AND t.taux = ".((float) $vatratecleaned)." AND t.active = 1"; + $sql .= " AND t.entity IN (".getEntity('c_tva').")"; if (!empty($vatratecode)) { $sql .= " AND t.code ='".$db->escape($vatratecode)."'"; // If we have the code, we use it in priority } else { @@ -6291,9 +6292,9 @@ function get_localtax_by_third($local) $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 .= " WHERE c.code = '".$db->escape($mysoc->country_code)."' AND t.active = 1 AND t.entity IN (".getEntity('c_tva').") 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 .= " WHERE c.code = '".$db->escape($mysoc->country_code)."' AND t.entity IN (".getEntity('c_tva').") AND tt.active = 1)"; $sql .= " AND t.localtax".$local."_type <> '0'"; $sql .= " ORDER BY t.rowid DESC"; @@ -6348,6 +6349,7 @@ function getTaxesFromId($vatrate, $buyer = null, $seller = null, $firstparamisid else $sql.= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($seller->country_code)."'";*/ $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($seller->country_code)."'"; $sql .= " AND t.taux = ".((float) $vatratecleaned)." AND t.active = 1"; + $sql .= " AND t.entity IN (".getEntity('c_tva').")"; if ($vatratecode) { $sql .= " AND t.code = '".$db->escape($vatratecode)."'"; } @@ -6504,6 +6506,7 @@ function get_product_vat_for_country($idprod, $thirdpartytouse, $idprodfournpric $sql = "SELECT t.taux as vat_rate, t.code as default_vat_code"; $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c"; $sql .= " WHERE t.active = 1 AND t.fk_pays = c.rowid AND c.code = '".$db->escape($thirdpartytouse->country_code)."'"; + $sql .= " AND t.entity IN (".getEntity('c_tva').")"; $sql .= " ORDER BY t.use_default DESC, t.taux DESC, t.code ASC, t.recuperableonly ASC"; $sql .= $db->plimit(1); @@ -6583,6 +6586,7 @@ function get_product_localtax_for_country($idprod, $local, $thirdpartytouse) $sql = "SELECT taux as vat_rate, localtax1, localtax2"; $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c"; $sql .= " WHERE t.active=1 AND t.fk_pays = c.rowid AND c.code='".$db->escape($thirdpartytouse->country_code)."'"; + $sql .= " AND t.entity IN (".getEntity('c_tva').")"; $sql .= " ORDER BY t.taux DESC, t.recuperableonly ASC"; $sql .= $db->plimit(1); diff --git a/htdocs/core/lib/price.lib.php b/htdocs/core/lib/price.lib.php index 0376b70ecd8..bdf0117ae3c 100644 --- a/htdocs/core/lib/price.lib.php +++ b/htdocs/core/lib/price.lib.php @@ -153,6 +153,7 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocalt $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as cv"; $sql .= " WHERE cv.taux = ".((float) $txtva); $sql .= " AND cv.fk_pays = ".((int) $countryid); + $sql .= " AND cv.entity IN (".getEntity('c_tva').")"; $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); diff --git a/htdocs/install/mysql/migration/18.0.0-19.0.0.sql b/htdocs/install/mysql/migration/18.0.0-19.0.0.sql index 69f1e51e5a5..2ce221c8778 100644 --- a/htdocs/install/mysql/migration/18.0.0-19.0.0.sql +++ b/htdocs/install/mysql/migration/18.0.0-19.0.0.sql @@ -1,4 +1,3 @@ - -- -- Be carefull to requests order. -- This file must be loaded by calling /install/index.php page @@ -33,7 +32,19 @@ -- -- VPGSQL8.2 SELECT dol_util_rebuild_sequences(); --- V19 +-- v18 + + + + +-- v19 + +-- VAT multientity +-- VMYSQL4.1 DROP INDEX uk_c_tva_id on llx_c_tva; +-- VPGSQL8.2 DROP INDEX uk_c_tva_id; +ALTER TABLE llx_c_tva ADD COLUMN entity integer DEFAULT 1 NOT NULL AFTER rowid; +ALTER TABLE llx_c_tva ADD UNIQUE INDEX uk_c_tva_id (entity, fk_pays, code, taux, recuperableonly); + ALTER TABLE llx_ticket ADD COLUMN fk_contract integer DEFAULT 0 after fk_project; UPDATE llx_product_lot SET manufacturing_date = datec WHERE manufacturing_date IS NULL; diff --git a/htdocs/install/mysql/tables/llx_c_tva.key.sql b/htdocs/install/mysql/tables/llx_c_tva.key.sql index 6675bc82db5..fbdd89e1b02 100644 --- a/htdocs/install/mysql/tables/llx_c_tva.key.sql +++ b/htdocs/install/mysql/tables/llx_c_tva.key.sql @@ -1,5 +1,6 @@ -- ======================================================================== -- Copyright (C) 2014-2015 Laurent Destailleur +-- Copyright (C) 2023 Alexandre Spangaro -- -- 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 @@ -16,6 +17,6 @@ -- -- ======================================================================== -ALTER TABLE llx_c_tva ADD UNIQUE INDEX uk_c_tva_id (fk_pays, code, taux, recuperableonly); +ALTER TABLE llx_c_tva ADD UNIQUE INDEX uk_c_tva_id (entity, fk_pays, code, taux, recuperableonly); -- ALTER TABLE llx_c_tva ADD UNIQUE INDEX uk_c_tva_id (fk_pays, code, recuperableonly); -- Not yet possible for compatibility reason, where old code is '' diff --git a/htdocs/install/mysql/tables/llx_c_tva.sql b/htdocs/install/mysql/tables/llx_c_tva.sql index 9bc77bb69ea..496cda4161a 100644 --- a/htdocs/install/mysql/tables/llx_c_tva.sql +++ b/htdocs/install/mysql/tables/llx_c_tva.sql @@ -1,7 +1,7 @@ -- ======================================================================== -- Copyright (C) 2005 Laurent Destailleur -- Copyright (C) 2010-2015 Juanjo Menent --- Copyright (C) 2011-2012 Alexandre Spangaro +-- Copyright (C) 2011-2023 Alexandre Spangaro -- -- 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 @@ -21,6 +21,7 @@ create table llx_c_tva ( rowid integer NOT NULL AUTO_INCREMENT PRIMARY KEY, + entity integer DEFAULT 1 NOT NULL, fk_pays integer NOT NULL, code varchar(10) DEFAULT '', -- a key to describe vat entry, for example FR20 taux double NOT NULL, diff --git a/htdocs/product/card.php b/htdocs/product/card.php index 5e6eefc37be..e25729427b1 100644 --- a/htdocs/product/card.php +++ b/htdocs/product/card.php @@ -8,7 +8,7 @@ * Copyright (C) 2010-2015 Juanjo Menent * Copyright (C) 2013-2016 Marcos García * Copyright (C) 2012-2013 Cédric Salvador - * Copyright (C) 2011-2020 Alexandre Spangaro + * Copyright (C) 2011-2023 Alexandre Spangaro * Copyright (C) 2014 Cédric Gross * Copyright (C) 2014-2015 Ferran Marcet * Copyright (C) 2015 Jean-François Ferry @@ -539,6 +539,7 @@ if (empty($reshook)) { $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'"; $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1"; $sql .= " AND t.code = '".$db->escape($vatratecode)."'"; + $sql .= " AND t.entity IN (".getEntity('c_tva').")"; $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); diff --git a/htdocs/product/price.php b/htdocs/product/price.php index 4f3bae2b3cb..d7384dd6643 100644 --- a/htdocs/product/price.php +++ b/htdocs/product/price.php @@ -8,7 +8,7 @@ * Copyright (C) 2014-2018 Juanjo Menent * Copyright (C) 2014-2019 Philippe Grand * Copyright (C) 2014 Ion agorria - * Copyright (C) 2015 Alexandre Spangaro + * Copyright (C) 2015-2023 Alexandre Spangaro * Copyright (C) 2015 Marcos García * Copyright (C) 2016 Ferran Marcet * Copyright (C) 2018-2020 Frédéric France @@ -146,6 +146,7 @@ if (empty($reshook)) { $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'"; $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1"; $sql .= " AND t.code = '".$db->escape($vatratecode)."'"; + $sql .= " AND t.entity IN (".getEntity('c_tva').")"; $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); @@ -164,6 +165,7 @@ if (empty($reshook)) { $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'"; $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1"; $sql .= " AND t.code = ''"; + $sql .= " AND t.entity IN (".getEntity('c_tva').")"; $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); @@ -318,6 +320,7 @@ if (empty($reshook)) { $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'"; $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1"; $sql .= " AND t.code ='".$db->escape($vatratecode)."'"; + $sql .= " AND t.entity IN (".getEntity('c_tva').")"; $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); @@ -397,6 +400,7 @@ if (empty($reshook)) { $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'"; $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1"; $sql .= " AND t.code ='".$db->escape($vatratecode)."'"; + $sql .= " AND t.entity IN (".getEntity('c_tva').")"; $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); @@ -650,6 +654,7 @@ if (empty($reshook)) { $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'"; $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1"; $sql .= " AND t.code ='".$db->escape($vatratecode)."'"; + $sql .= " AND t.entity IN (".getEntity('c_tva').")"; $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); @@ -769,6 +774,7 @@ if (empty($reshook)) { $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'"; $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1"; $sql .= " AND t.code ='".$db->escape($vatratecode)."'"; + $sql .= " AND t.entity IN (".getEntity('c_tva').")"; $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 56948b6c5c2..97d528e4b1c 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -4556,6 +4556,7 @@ class Societe extends CommonObject $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c"; $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$this->db->escape($this->country_code)."'"; $sql .= " AND t.active = 1"; + $sql .= " AND t.entity IN (".getEntity('c_tva').")"; if (empty($localTaxNum)) { $sql .= " AND (t.localtax1_type <> '0' OR t.localtax2_type <> '0')"; } elseif ($localTaxNum == 1) { @@ -4583,6 +4584,7 @@ class Societe extends CommonObject $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c"; $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$this->db->escape($this->country_code)."'"; $sql .= " AND t.active = 1 AND t.recuperableonly = 1"; + $sql .= " AND t.entity IN (".getEntity('c_tva').")"; dol_syslog("useNPR", LOG_DEBUG); $resql = $this->db->query($sql); diff --git a/htdocs/societe/price.php b/htdocs/societe/price.php index 6ecf351c1ad..8f2990361e2 100644 --- a/htdocs/societe/price.php +++ b/htdocs/societe/price.php @@ -5,6 +5,7 @@ * Copyright (C) 2005-2013 Regis Houssin * Copyright (C) 2006 Andre Cianfarani * Copyright (C) 2015 Marcos García + * Copyright (C) 2023 Alexandre Spangaro * * 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 @@ -118,7 +119,8 @@ if (empty($reshook)) { $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c"; $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'"; $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1"; - $sql .= " AND t.code ='".$db->escape($vatratecode)."'"; + $sql .= " AND t.code = '".$db->escape($vatratecode)."'"; + $sql .= " AND t.entity IN (".getEntity('c_tva').")"; $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql);