dolibarr 23.0.3
mod_codecompta_digitaria.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
4 * Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
5 * Copyright (C) 2019 Alexandre Spangaro <aspangaro@open-dsi.fr>
6 * Copyright (C) 2019-2024 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * or see https://www.gnu.org/
22 */
23
29require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
30
31
36{
40 public $name = 'Digitaria';
41
46 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
47
51 public $prefixcustomeraccountancycode;
52
56 public $prefixsupplieraccountancycode;
57
61 public $position = 30;
62
66 public $code;
70 public $customeraccountancycodecharacternumber;
74 public $supplieraccountancycodecharacternumber;
75
76
80 public function __construct()
81 {
82 global $conf, $langs;
83 if (!isset($conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER) || trim($conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER) == '') {
84 $conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER = '411';
85 }
86 if (!isset($conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER) || trim($conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER) == '') {
87 $conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER = '401';
88 }
89 $this->prefixcustomeraccountancycode = getDolGlobalString('COMPANY_DIGITARIA_MASK_CUSTOMER');
90 $this->prefixsupplieraccountancycode = getDolGlobalString('COMPANY_DIGITARIA_MASK_SUPPLIER');
91
92 if (!isset($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER) || trim($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER) == '') {
93 $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER = '5';
94 }
95 if (!isset($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER) || trim($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER) == '') {
96 $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER = '5';
97 }
98 $this->customeraccountancycodecharacternumber = getDolGlobalString('COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER');
99 $this->supplieraccountancycodecharacternumber = getDolGlobalString('COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER');
100 }
101
108 public function info($langs)
109 {
110 global $conf, $form;
111
112 $tooltip = '';
113 $texte = '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
114 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
115 $texte .= '<input type="hidden" name="page_y" value="">';
116 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
117 $texte .= '<input type="hidden" name="param1" value="COMPANY_DIGITARIA_MASK_SUPPLIER">';
118 $texte .= '<input type="hidden" name="param2" value="COMPANY_DIGITARIA_MASK_CUSTOMER">';
119 $texte .= '<input type="hidden" name="param3" value="COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER">';
120 $texte .= '<input type="hidden" name="param4" value="COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER">';
121 $texte .= '<input type="hidden" name="param5" value="COMPANY_DIGITARIA_CLEAN_WORDS">';
122 $texte .= '<table class="nobordernopadding centpercent">';
123 $s1 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value1" value="' . getDolGlobalString('COMPANY_DIGITARIA_MASK_SUPPLIER').'">', $tooltip, 1, 'help', 'valignmiddle', 0, 3, $this->name);
124 $s2 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value2" value="' . getDolGlobalString('COMPANY_DIGITARIA_MASK_CUSTOMER').'">', $tooltip, 1, 'help', 'valignmiddle', 0, 3, $this->name);
125 $s3 = $form->textwithpicto('<input type="text" class="flat" size="2" name="value3" value="' . getDolGlobalString('COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER').'">', $tooltip, 1, 'help', 'valignmiddle', 0, 3, $this->name);
126 $s4 = $form->textwithpicto('<input type="text" class="flat" size="2" name="value4" value="' . getDolGlobalString('COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER').'">', $tooltip, 1, 'help', 'valignmiddle', 0, 3, $this->name);
127 $texte .= '<tr><td>';
128 // trans remove html entities
129 $texte .= $langs->trans("ModuleCompanyCodeCustomer".$this->name, '{s2}', '{s4}')."<br>\n";
130 $texte .= $langs->trans("ModuleCompanyCodeSupplier".$this->name, '{s1}', '{s3}')."<br>\n";
131 $texte = str_replace(array('{s1}', '{s2}', '{s3}', '{s4}'), array($s1, $s2, $s3, $s4), $texte);
132 $texte .= "<br>\n";
133 // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default)
134 if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || getDolGlobalString('COMPANY_DIGITARIA_REMOVE_SPECIAL')) {
135 $texte .= $langs->trans('RemoveSpecialChars').' = '.yn(1)."<br>\n";
136 }
137 // 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.
138 if (getDolGlobalString('COMPANY_DIGITARIA_CLEAN_REGEX')) {
139 $texte .= $langs->trans('COMPANY_DIGITARIA_CLEAN_REGEX').' = ' . getDolGlobalString('COMPANY_DIGITARIA_CLEAN_REGEX')."<br>\n";
140 }
141 // If value is not unique (if COMPANY_DIGITARIA_UNIQUE_CODE is set to 0), we show this
142 if (!getDolGlobalString('COMPANY_DIGITARIA_UNIQUE_CODE', '1')) {
143 $texte .= $langs->trans('DuplicateForbidden').' = '.yn(0)."<br>\n";
144 }
145 $texte .= '</td>';
146 $texte .= '<td class="right"><input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"></td>';
147 $texte .= '</tr>';
148
149 $texte .= '<tr><td>';
150 $texte .= "<br>\n";
151
152 $texthelp = $langs->trans("RemoveSpecialWordsHelp");
153 $texttitle = $langs->trans("RemoveSpecialWords");
154
155 $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1);
156 $texte .= "<br>\n";
157 $texte .= '<textarea class="flat textareafordir" spellcheck="false" cols="60" name="value5">';
158 if (getDolGlobalString('COMPANY_DIGITARIA_CLEAN_WORDS')) {
159 $texte .= $conf->global->COMPANY_DIGITARIA_CLEAN_WORDS;
160 }
161 $texte .= '</textarea>';
162 $texte .= '</tr></table>';
163
164 $texte .= '</form>';
165
166 return $texte;
167 }
168
177 public function getExample($langs = null, $objsoc = '', $type = -1)
178 {
179 global $conf, $mysoc;
180 if (!$langs instanceof Translate) {
181 $langs = $GLOBALS['langs'];
182 '@phan-var-force Translate $langs';
183 }
184
185 $s = $langs->trans("ThirdPartyName").": ".$mysoc->name;
186 $s .= "<br>\n";
187
188 if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) {
189 $thirdpartylabelexample = (string) preg_replace('/([^a-z0-9])/i', '', $mysoc->name);
190 } else {
191 $thirdpartylabelexample = '';
192 }
193 $s .= "<br>\n";
194 $s .= $this->prefixcustomeraccountancycode.strtoupper(substr($thirdpartylabelexample, 0, (int) $this->customeraccountancycodecharacternumber));
195 $s .= "<br>\n";
196 $s .= $this->prefixsupplieraccountancycode.strtoupper(substr($thirdpartylabelexample, 0, (int) $this->supplieraccountancycodecharacternumber));
197 return $s;
198 }
199
200 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
209 public function get_code($db, $societe, $type = '')
210 {
211 // phpcs:enable
212 global $conf;
213 $i = 0;
214 $this->code = '';
215
216 $disponibility = 0;
217
218 if (is_object($societe)) {
219 dol_syslog("mod_codecompta_digitaria::get_code search code for type=".$type." & company=".(!empty($societe->name) ? $societe->name : ''));
220
221 if ($type == 'supplier') {
222 $codetouse = (string) $societe->name;
223 $prefix = $this->prefixsupplieraccountancycode;
224 $width = $this->supplieraccountancycodecharacternumber;
225 } elseif ($type == 'customer') {
226 $codetouse = (string) $societe->name;
227 $prefix = $this->prefixcustomeraccountancycode;
228 $width = $this->customeraccountancycodecharacternumber;
229 } else {
230 $this->error = 'Bad value for parameter type';
231 return -1;
232 }
233
234 // Clean declared words
235 if (getDolGlobalString('COMPANY_DIGITARIA_CLEAN_WORDS')) {
236 $cleanWords = explode(";", getDolGlobalString('COMPANY_DIGITARIA_CLEAN_WORDS'));
237 $codetouse = str_replace($cleanWords, "", $codetouse);
238 }
239 // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default)
240 if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || getDolGlobalString('COMPANY_DIGITARIA_REMOVE_SPECIAL')) {
241 $codetouse = (string) preg_replace('/([^a-z0-9])/i', '', $codetouse);
242 }
243 // 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.
244 if (getDolGlobalString('COMPANY_DIGITARIA_CLEAN_REGEX')) { // Example: $conf->global->COMPANY_DIGITARIA_CLEAN_REGEX='^..(..)..';
245 $codetouse = (string) preg_replace('/' . getDolGlobalString('COMPANY_DIGITARIA_CLEAN_REGEX').'/', '\1\2\3', $codetouse);
246 }
247
248 $this->code = $prefix.strtoupper(substr($codetouse, 0, (int) $width));
249 dol_syslog("mod_codecompta_digitaria::get_code search code proposed=".$this->code, LOG_DEBUG);
250
251 // Unique index on code if COMPANY_DIGITARIA_UNIQUE_CODE is set to 1 or not set (default)
252 if (getDolGlobalString('COMPANY_DIGITARIA_UNIQUE_CODE', '1')) {
253 $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type);
254
255 while ($disponibility != 0 && $i < 1000) {
256 $widthsupplier = $this->supplieraccountancycodecharacternumber;
257 $widthcustomer = $this->customeraccountancycodecharacternumber;
258
259 if ($i <= 9) {
260 $a = 1;
261 } elseif ($i <= 99) { // Also >= 10
262 $a = 2;
263 } else { // ($i >= 100 && $i <= 999) {
264 $a = 3;
265 }
266
267 if ($type == 'supplier') {
268 $this->code = $prefix.strtoupper(substr($codetouse, 0, (int) $widthsupplier - $a)).$i;
269 } elseif ($type == 'customer') {
270 $this->code = $prefix.strtoupper(substr($codetouse, 0, (int) $widthcustomer - $a)).$i;
271 }
272 $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type);
273
274 $i++;
275 }
276 } // else { $disponibility = 0; /* Already set */ }
277 }
278
279 if ($disponibility == 0) {
280 return 0; // return ok
281 } else {
282 return -1; // return ko
283 }
284 }
285
294 public function checkIfAccountancyCodeIsAlreadyUsed($db, $code, $type = '')
295 {
296 if ($type == 'supplier') {
297 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
298 $typethirdparty = 'accountancy_code_supplier';
299 } else {
300 $typethirdparty = 'code_compta_fournisseur';
301 }
302 } elseif ($type == 'customer') {
303 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
304 $typethirdparty = 'accountancy_code_customer';
305 } else {
306 $typethirdparty = 'code_compta';
307 }
308 } else {
309 $this->error = 'Bad value for parameter type';
310 return -1;
311 }
312
313 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
314 $sql = "SELECT " . $typethirdparty . " FROM " . MAIN_DB_PREFIX . "societe_perentity";
315 $sql .= " WHERE " . $typethirdparty . " = '" . $db->escape($code) . "'";
316 } else {
317 $sql = "SELECT " . $typethirdparty . " FROM " . MAIN_DB_PREFIX . "societe";
318 $sql .= " WHERE " . $typethirdparty . " = '" . $db->escape($code) . "'";
319 }
320 $sql .= " AND entity IN (".getEntity('societe').")";
321
322 $resql = $db->query($sql);
323 if ($resql) {
324 if ($db->num_rows($resql) == 0) {
325 dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' available");
326 return 0; // Available
327 } else {
328 dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' not available");
329 return -1; // Not available
330 }
331 } else {
332 $this->error = $db->error()." sql=".$sql;
333 return -2; // Error
334 }
335 }
336}
Parent class for third parties accountancy code generators.
Class to manage translations.
Class to manage accountancy code of thirdparties with Digitaria rules.
getExample($langs=null, $objsoc='', $type=-1)
Return an example of result returned by getNextValue.
get_code($db, $societe, $type='')
Set accountancy account code for a third party into this->code.
info($langs)
Return description of module.
checkIfAccountancyCodeIsAlreadyUsed($db, $code, $type='')
Check accountancy account code for a third party into this->code.
global $mysoc
yn($yesno, $format=1, $color=0)
Return yes or no in current language.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:128