diff --git a/htdocs/core/modules/societe/mod_codecompta_digitaria.php b/htdocs/core/modules/societe/mod_codecompta_digitaria.php new file mode 100644 index 00000000000..8044610a914 --- /dev/null +++ b/htdocs/core/modules/societe/mod_codecompta_digitaria.php @@ -0,0 +1,272 @@ + + * Copyright (C) 2010 Laurent Destailleur + * Copyright (C) 2019 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 + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * or see http://www.gnu.org/ + */ + +/** + * \file htdocs/core/modules/societe/mod_codecompta_digitaria.php + * \ingroup societe + * \brief File of class to manage accountancy code of thirdparties with Digitaria rules + */ +require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php'; + + +/** + * Class to manage accountancy code of thirdparties with Digitaria rules + */ +class mod_codecompta_digitaria extends ModeleAccountancyCode +{ + /** + * @var string Nom du modele + * @deprecated + * @see name + */ + public $nom='Digitaria'; + + /** + * @var string model name + */ + public $name='Digitaria'; + + /** + * Dolibarr version of the loaded document + * @var string + */ + public $version='dolibarr'; // 'development', 'experimental', 'dolibarr' + + public $prefixcustomeraccountancycode; + + public $prefixsupplieraccountancycode; + + /** + * Constructor + */ + public function __construct() + { + global $conf, $langs; + if (! isset($conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER) || trim($conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER) == '') $conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER='411'; + if (! isset($conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER) || trim($conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER) == '') $conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER='401'; + $this->prefixcustomeraccountancycode=$conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER; + $this->prefixsupplieraccountancycode=$conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER; + + if (! isset($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER) || trim($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER) == '') $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER='5'; + if (! isset($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER) || trim($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER) == '') $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER='5'; + $this->customeraccountancycodecharacternumber=$conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER; + $this->supplieraccountancycodecharacternumber=$conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER; + } + + /** + * Return description of module + * + * @param Translate $langs Object langs + * @return string Description of module + */ + public function info($langs) + { + global $conf, $form; + + $tooltip=''; + $texte = '
'; + $texte.= ''; + $texte.= ''; + $texte.= ''; + $texte.= ''; + $texte.= ''; + $texte.= ''; + $texte.= ''; + $s1= $form->textwithpicto('', $tooltip, 1, 1); + $s2= $form->textwithpicto('', $tooltip, 1, 1); + $s3= $form->textwithpicto('', $tooltip, 1, 1); + $s4= $form->textwithpicto('', $tooltip, 1, 1); + $texte.= ''; + $texte.= ''; + $texte.= '
'; + $texte.= $langs->trans("ModuleCompanyCodeCustomer".$this->name, $s2, $s4)."
\n"; + $texte.= $langs->trans("ModuleCompanyCodeSupplier".$this->name, $s1, $s3)."
\n"; + $texte.= "
\n"; + // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default) + if (! isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || ! empty($conf->global->$conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) $texte.= $langs->trans('COMPANY_DIGITARIA_REMOVE_SPECIAL').' = '.yn(1)."
\n"; + // Apply a regex replacement pattern on code if COMPANY_DIGITARIA_CLEAN_REGEX is set. Value must be a regex with parenthesis. The part into parenthesis is kept, the rest removed. + if (! empty($conf->global->COMPANY_DIGITARIA_CLEAN_REGEX)) $texte.= $langs->trans('COMPANY_DIGITARIA_CLEAN_REGEX').' = '.$conf->global->COMPANY_DIGITARIA_CLEAN_REGEX."
\n"; + // Unique index on code if COMPANY_DIGITARIA_UNIQUE_CODE is set to 1 or not set (default) + if (! isset($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE) || ! empty($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE)) $texte.= $langs->trans('COMPANY_DIGITARIA_UNIQUE_CODE').' = '.yn(1)."
\n"; + $texte.= '
 
'; + $texte.= '
'; + + return $texte; + } + + /** + * Return an example of result returned by getNextValue + * + * @param Translate $langs Object langs + * @param Societe $objsoc Object thirdparty + * @param int $type Type of third party (1:customer, 2:supplier, -1:autodetect) + * @return string Example + */ + public function getExample($langs, $objsoc=0, $type=-1) + { + global $mysoc; + + $s= $langs->trans("ThirdPartyName").": ".$mysoc->name; + $s.="
\n"; + + if (! isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) $thirdpartylabelexample = preg_replace('/([^a-z0-9])/i', '', $mysoc->name); + $s.="
\n"; + $s.=$this->prefixcustomeraccountancycode.strtoupper(substr($thirdpartylabelexample,0, $this->customeraccountancycodecharacternumber)); + $s.="
\n"; + $s.=$this->prefixsupplieraccountancycode.strtoupper(substr($thirdpartylabelexample,0, $this->supplieraccountancycodecharacternumber)); + return $s; + } + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * Set accountancy account code for a third party into this->code + * + * @param DoliDB $db Database handler + * @param Societe $societe Third party object + * @param int $type 'customer' or 'supplier' + * @return int >=0 if OK, <0 if KO + */ + public function get_code($db, $societe, $type='') + { + // phpcs:enable + $i = 0; + $this->code=''; + + if (is_object($societe)) + { + dol_syslog("mod_codecompta_digitaria::get_code search code for type=".$type." & company=".(! empty($societe->name)?$societe->name:'')); + + if ($type == 'supplier') { + $codetouse = $societe->name; + $prefix = $this->prefixsupplieraccountancycode; + $width = $this->supplieraccountancycodecharacternumber; + } + elseif ($type == 'customer') + { + $codetouse = $societe->name; + $prefix = $this->prefixcustomeraccountancycode; + $width = $this->customeraccountancycodecharacternumber; + } + else + { + $this->error = 'Bad value for parameter type'; + return -1; + } + + // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default) + if (! isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || ! empty($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) $codetouse=preg_replace('/([^a-z0-9])/i', '', $codetouse); + // Apply a regex replacement pattern on code if COMPANY_DIGITARIA_CLEAN_REGEX is set. Value must be a regex with parenthesis. The part into parenthesis is kept, the rest removed. + if (! empty($conf->global->COMPANY_DIGITARIA_CLEAN_REGEX)) // Example: $conf->global->COMPANY_DIGITARIA_CLEAN_REGEX='^..(..)..'; + { + $codetouse=preg_replace('/'.$conf->global->COMPANY_DIGITARIA_CLEAN_REGEX.'/', '\1\2\3', $codetouse); + } + + $this->code = $prefix.strtoupper(substr($codetouse, 0, $width)); + dol_syslog("mod_codecompta_digitaria::get_code search code proposed=".$this->code); + + // Unique index on code if COMPANY_DIGITARIA_UNIQUE_CODE is set to 1 or not set (default) + if (! isset($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE) || ! empty($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE)) + { + $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type); + + while ($disponibility <> 0 && $i < 100) { + $widthsupplier = $this->supplieraccountancycodecharacternumber; + $widthcustomer = $this->customeraccountancycodecharacternumber; + + if ($i <= 9) { + $a = 1; + } + if ($i >= 10 && $i <= 99) { + $a = 2; + } + + if ($type == 'supplier') { + $this->code = $prefix . strtoupper(substr($codetouse, 0, $widthsupplier - $a)) . $i; + } elseif ($type == 'customer') { + $this->code = $prefix . strtoupper(substr($codetouse, 0, $widthcustomer - $a)) . $i; + } + $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type); + + $i++; + } + } else { + $disponibility == 0; + } + } + + if ($disponibility == 0) + { + return 0; // return ok + } + else + { + return -1; // return ko + } + } + + /** + * Check accountancy account code for a third party into this->code + * + * @param DoliDB $db Database handler + * @param string $code Code of third party + * @param int $type 'customer' or 'supplier' + * @return int >=0 if OK, <0 if KO + */ + public function checkIfAccountancyCodeIsAlreadyUsed($db, $code, $type='') + { + if ($type == 'supplier') + { + $typethirdparty = 'code_compta_fournisseur'; + } + elseif ($type == 'customer') + { + $typethirdparty = 'code_compta'; + } + else + { + $this->error = 'Bad value for parameter type'; + return -1; + } + + $sql = "SELECT ".$typethirdparty." FROM ".MAIN_DB_PREFIX."societe"; + $sql.= " WHERE ".$typethirdparty." = '".$code."'"; + + $resql=$db->query($sql); + if ($resql) + { + if ($db->num_rows($resql) == 0) + { + dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' available"); + return 0; // Available + } + else + { + dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' not available"); + return -1; // Not available + } + } + else + { + $this->error=$db->error()." sql=".$sql; + return -2; // Error + } + } +} + diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index 1b4badc39b5..0ae5239642d 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -462,7 +462,9 @@ EnableAndSetupModuleCron=If you want to have this recurring invoice generated au ModuleCompanyCodeCustomerAquarium=%s followed by customer code for a customer accounting code ModuleCompanyCodeSupplierAquarium=%s followed by vendor code for a vendor accounting code ModuleCompanyCodePanicum=Return an empty accounting code. -ModuleCompanyCodeDigitaria=Accounting code depends on third-party code. The code is composed of the character "C" in the first position followed by the first 5 characters of the third-party code. +ModuleCompanyCodeDigitaria=Returns a compound accounting code according to the name of the third party. The code consists of a prefix that can be defined in the first position followed by the number of characters defined in the third party code. +ModuleCompanyCodeCustomerDigitaria=%s followed by the truncated customer name by the number of characters: %s for the customer accounting code. +ModuleCompanyCodeSupplierDigitaria=%s followed by the truncated supplier name by the number of characters: %s for the supplier accounting code. Use3StepsApproval=By default, Purchase Orders need to be created and approved by 2 different users (one step/user to create and one step/user to approve. Note that if user has both permission to create and approve, one step/user will be enough). You can ask with this option to introduce a third step/user approval, if amount is higher than a dedicated value (so 3 steps will be necessary: 1=validation, 2=first approval and 3=second approval if amount is enough).
Set this to empty if one approval (2 steps) is enough, set it to a very low value (0.1) if a second approval (3 steps) is always required. UseDoubleApproval=Use a 3 steps approval when amount (without tax) is higher than... WarningPHPMail=WARNING: It is often better to setup outgoing emails to use the email server of your provider instead of the default setup. Some email providers (like Yahoo) do not allow you to send an email from another server than their own server. Your current setup uses the server of the application to send email and not the server of your email provider, so some recipients (the one compatible with the restrictive DMARC protocol), will ask your email provider if they can accept your email and some email providers (like Yahoo) may respond "no" because the server is not theirs, so few of your sent Emails may not be accepted (be careful also of your email provider's sending quota).
If your Email provider (like Yahoo) has this restriction, you must change Email setup to choose the other method "SMTP server" and enter the SMTP server and credentials provided by your Email provider. @@ -1848,6 +1850,9 @@ EnterCalculationRuleIfPreviousFieldIsYes=Enter calculation rule if previous fiel SeveralLangugeVariatFound=Several language variants found COMPANY_AQUARIUM_REMOVE_SPECIAL=Remove special characters COMPANY_AQUARIUM_CLEAN_REGEX=Regex filter to clean value (COMPANY_AQUARIUM_CLEAN_REGEX) +COMPANY_DIGITARIA_REMOVE_SPECIAL=Remove special characters +COMPANY_DIGITARIA_CLEAN_REGEX=Regex filter to clean value (COMPANY_DIGITARIA_CLEAN_REGEX) +COMPANY_DIGITARIA_UNIQUE_CODE=Unauthorized double GDPRContact=Data Protection Officer (DPO, Data Privacy or GDPR contact) GDPRContactDesc=If you store data about European companies/citizens, you can name the contact who is responsible for the General Data Protection Regulation here HelpOnTooltip=Help text to show on tooltip