From 757aec629f2fbbbea55ec8819b8fa5911bc8f7ef Mon Sep 17 00:00:00 2001 From: Amael-PE Date: Fri, 14 Feb 2025 20:54:03 +0100 Subject: [PATCH] New Create isInSEPA function --- htdocs/core/lib/company.lib.php | 55 ++++++++++++++++++++++++++ htdocs/societe/class/societe.class.php | 11 ++++++ 2 files changed, 66 insertions(+) diff --git a/htdocs/core/lib/company.lib.php b/htdocs/core/lib/company.lib.php index 30a8242a7ec..64cd74bf75b 100644 --- a/htdocs/core/lib/company.lib.php +++ b/htdocs/core/lib/company.lib.php @@ -812,6 +812,61 @@ function isInEEC($object) return in_array($object->country_code, $country_code_in_EEC); } +/** + * Return list of countries that are inside the SEPA zone (Single Euro Payment Area) + * Note: Try to keep this function as a "memory only" function for performance reasons. + * + * @return array Array of countries code in SEPA + */ +function getCountriesInSEPA() +{ + // List of all country codes that are in Europe agreement for bank transferts + // List found on https://www.ecb.europa.eu/paym/integration/retail/sepa/html/index.en.html + global $conf, $db; + $country_code_in_SEPA = array(); + + if (!empty($conf->cache['country_code_in_SEPA'])) { + // Use of cache to reduce number of database requests + $country_code_in_SEPA = $conf->cache['country_code_in_SEPA']; + } else { + $sql = "SELECT cc.code FROM ".MAIN_DB_PREFIX."c_country as cc"; + $sql .= " WHERE cc.sepa = 1"; + + $resql = $db->query($sql); + if ($resql) { + $num = $db->num_rows($resql); + $i = 0; + while ($i < $num) { + $objp = $db->fetch_object($resql); + $country_code_in_SEPA[] = $objp->code; + $i++; + } + } else { + dol_print_error($db); + } + $conf->cache['country_code_in_SEPA'] = $country_code_in_SEPA; + } + return $country_code_in_SEPA; +} + +/** + * Return if a country of an object is inside the SEPA zone (Single Euro Payment Area) + * + * @param Object $object Object + * @return boolean true = ccountry inside SEPA, false = country outside SEPA + */ +function isInSEPA($object) +{ + if (empty($object->country_code)) { + return false; + } + + $country_code_in_SEPA = getCountriesInSEPA(); // This make a database call but there is a cache done into $conf->cache['country_code_in_SEPA'] + + //print "dd".$object->country_code; + return in_array($object->country_code, $country_code_in_SEPA); +} + /** * Show html area for list of projects diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 9ffd25427be..a3ea1ac943f 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -4185,6 +4185,17 @@ class Societe extends CommonObject return isInEEC($this); } + /** + * Return if a company is inside the SEPA zone (Single Euro Payment Area) + * + * @return boolean true = country inside SEPA, false = country outside SEPA + */ + public function isInSEPA() + { + require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php'; + return isInSEPA($this); + } + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Load the list of provider categories