dolibarr 21.0.0-alpha
mod_codecompta_digitaria.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2019 Alexandre Spangaro <aspangaro@open-dsi.fr>
5 * Copyright (C) 2019-2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * or see https://www.gnu.org/
21 */
22
28require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
29
30
35{
39 public $name = 'Digitaria';
40
45 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
46
51 public $prefixcustomeraccountancycode;
52
57 public $prefixsupplieraccountancycode;
58
59 public $position = 30;
60
64 public $code;
65 public $customeraccountancycodecharacternumber;
66 public $supplieraccountancycodecharacternumber;
67
68
72 public function __construct()
73 {
74 global $conf, $langs;
75 if (!isset($conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER) || trim($conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER) == '') {
76 $conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER = '411';
77 }
78 if (!isset($conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER) || trim($conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER) == '') {
79 $conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER = '401';
80 }
81 $this->prefixcustomeraccountancycode = getDolGlobalString('COMPANY_DIGITARIA_MASK_CUSTOMER');
82 $this->prefixsupplieraccountancycode = getDolGlobalString('COMPANY_DIGITARIA_MASK_SUPPLIER');
83
84 if (!isset($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER) || trim($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER) == '') {
85 $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER = '5';
86 }
87 if (!isset($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER) || trim($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER) == '') {
88 $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER = '5';
89 }
90 $this->customeraccountancycodecharacternumber = getDolGlobalString('COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER');
91 $this->supplieraccountancycodecharacternumber = getDolGlobalString('COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER');
92 }
93
100 public function info($langs)
101 {
102 global $conf, $form;
103
104 $tooltip = '';
105 $texte = '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
106 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
107 $texte .= '<input type="hidden" name="page_y" value="">';
108 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
109 $texte .= '<input type="hidden" name="param1" value="COMPANY_DIGITARIA_MASK_SUPPLIER">';
110 $texte .= '<input type="hidden" name="param2" value="COMPANY_DIGITARIA_MASK_CUSTOMER">';
111 $texte .= '<input type="hidden" name="param3" value="COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER">';
112 $texte .= '<input type="hidden" name="param4" value="COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER">';
113 $texte .= '<input type="hidden" name="param5" value="COMPANY_DIGITARIA_CLEAN_WORDS">';
114 $texte .= '<table class="nobordernopadding centpercent">';
115 $s1 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value1" value="' . getDolGlobalString('COMPANY_DIGITARIA_MASK_SUPPLIER').'">', $tooltip, 1, 1);
116 $s2 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value2" value="' . getDolGlobalString('COMPANY_DIGITARIA_MASK_CUSTOMER').'">', $tooltip, 1, 1);
117 $s3 = $form->textwithpicto('<input type="text" class="flat" size="2" name="value3" value="' . getDolGlobalString('COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER').'">', $tooltip, 1, 1);
118 $s4 = $form->textwithpicto('<input type="text" class="flat" size="2" name="value4" value="' . getDolGlobalString('COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER').'">', $tooltip, 1, 1);
119 $texte .= '<tr><td>';
120 // trans remove html entities
121 $texte .= $langs->trans("ModuleCompanyCodeCustomer".$this->name, '{s2}', '{s4}')."<br>\n";
122 $texte .= $langs->trans("ModuleCompanyCodeSupplier".$this->name, '{s1}', '{s3}')."<br>\n";
123 $texte = str_replace(array('{s1}', '{s2}', '{s3}', '{s4}'), array($s1, $s2, $s3, $s4), $texte);
124 $texte .= "<br>\n";
125 // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default)
126 if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || !empty($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) {
127 $texte .= $langs->trans('RemoveSpecialChars').' = '.yn(1)."<br>\n";
128 }
129 // 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.
130 if (getDolGlobalString('COMPANY_DIGITARIA_CLEAN_REGEX')) {
131 $texte .= $langs->trans('COMPANY_DIGITARIA_CLEAN_REGEX').' = ' . getDolGlobalString('COMPANY_DIGITARIA_CLEAN_REGEX')."<br>\n";
132 }
133 // If value is not unique (if COMPANY_DIGITARIA_UNIQUE_CODE is set to 0), we show this
134 if (!getDolGlobalString('COMPANY_DIGITARIA_UNIQUE_CODE', '1')) {
135 $texte .= $langs->trans('DuplicateForbidden').' = '.yn(0)."<br>\n";
136 }
137 $texte .= '</td>';
138 $texte .= '<td class="right"><input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"></td>';
139 $texte .= '</tr>';
140
141 $texte .= '<tr><td>';
142 $texte .= "<br>\n";
143
144 $texthelp = $langs->trans("RemoveSpecialWordsHelp");
145 $texttitle = $langs->trans("RemoveSpecialWords");
146
147 $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1);
148 $texte .= "<br>\n";
149 $texte .= '<textarea class="flat" cols="60" name="value5">';
150 if (getDolGlobalString('COMPANY_DIGITARIA_CLEAN_WORDS')) {
151 $texte .= $conf->global->COMPANY_DIGITARIA_CLEAN_WORDS;
152 }
153 $texte .= '</textarea>';
154 $texte .= '</tr></table>';
155
156 $texte .= '</form>';
157
158 return $texte;
159 }
160
169 public function getExample($langs = null, $objsoc = '', $type = -1)
170 {
171 global $conf, $mysoc;
172 if (!$langs instanceof Translate) {
173 $langs = $GLOBALS['langs'];
174 '@phan-var-force Translate $langs';
175 }
176
177 $s = $langs->trans("ThirdPartyName").": ".$mysoc->name;
178 $s .= "<br>\n";
179
180 if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) {
181 $thirdpartylabelexample = preg_replace('/([^a-z0-9])/i', '', $mysoc->name);
182 }
183 $s .= "<br>\n";
184 $s .= $this->prefixcustomeraccountancycode.strtoupper(substr($thirdpartylabelexample, 0, (int) $this->customeraccountancycodecharacternumber));
185 $s .= "<br>\n";
186 $s .= $this->prefixsupplieraccountancycode.strtoupper(substr($thirdpartylabelexample, 0, (int) $this->supplieraccountancycodecharacternumber));
187 return $s;
188 }
189
190 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
199 public function get_code($db, $societe, $type = '')
200 {
201 // phpcs:enable
202 global $conf;
203 $i = 0;
204 $this->code = '';
205
206 $disponibility = 0;
207
208 if (is_object($societe)) {
209 dol_syslog("mod_codecompta_digitaria::get_code search code for type=".$type." & company=".(!empty($societe->name) ? $societe->name : ''));
210
211 if ($type == 'supplier') {
212 $codetouse = $societe->name;
213 $prefix = $this->prefixsupplieraccountancycode;
214 $width = $this->supplieraccountancycodecharacternumber;
215 } elseif ($type == 'customer') {
216 $codetouse = $societe->name;
217 $prefix = $this->prefixcustomeraccountancycode;
218 $width = $this->customeraccountancycodecharacternumber;
219 } else {
220 $this->error = 'Bad value for parameter type';
221 return -1;
222 }
223
224 // Clean declared words
225 if (getDolGlobalString('COMPANY_DIGITARIA_CLEAN_WORDS')) {
226 $cleanWords = explode(";", getDolGlobalString('COMPANY_DIGITARIA_CLEAN_WORDS'));
227 $codetouse = str_replace($cleanWords, "", $codetouse);
228 }
229 // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default)
230 if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || getDolGlobalString('COMPANY_DIGITARIA_REMOVE_SPECIAL')) {
231 $codetouse = preg_replace('/([^a-z0-9])/i', '', $codetouse);
232 }
233 // 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.
234 if (getDolGlobalString('COMPANY_DIGITARIA_CLEAN_REGEX')) { // Example: $conf->global->COMPANY_DIGITARIA_CLEAN_REGEX='^..(..)..';
235 $codetouse = preg_replace('/' . getDolGlobalString('COMPANY_DIGITARIA_CLEAN_REGEX').'/', '\1\2\3', $codetouse);
236 }
237
238 $this->code = $prefix.strtoupper(substr($codetouse, 0, (int) $width));
239 dol_syslog("mod_codecompta_digitaria::get_code search code proposed=".$this->code, LOG_DEBUG);
240
241 // Unique index on code if COMPANY_DIGITARIA_UNIQUE_CODE is set to 1 or not set (default)
242 if (getDolGlobalString('COMPANY_DIGITARIA_UNIQUE_CODE', '1')) {
243 $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type);
244
245 while ($disponibility != 0 && $i < 1000) {
246 $widthsupplier = $this->supplieraccountancycodecharacternumber;
247 $widthcustomer = $this->customeraccountancycodecharacternumber;
248
249 if ($i <= 9) {
250 $a = 1;
251 }
252 if ($i >= 10 && $i <= 99) {
253 $a = 2;
254 }
255 if ($i >= 100 && $i <= 999) {
256 $a = 3;
257 }
258
259 if ($type == 'supplier') {
260 $this->code = $prefix.strtoupper(substr($codetouse, 0, (int) $widthsupplier - $a)).$i;
261 } elseif ($type == 'customer') {
262 $this->code = $prefix.strtoupper(substr($codetouse, 0, (int) $widthcustomer - $a)).$i;
263 }
264 $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type);
265
266 $i++;
267 }
268 } // else { $disponibility = 0; /* Already set */ }
269 }
270
271 if ($disponibility == 0) {
272 return 0; // return ok
273 } else {
274 return -1; // return ko
275 }
276 }
277
286 public function checkIfAccountancyCodeIsAlreadyUsed($db, $code, $type = '')
287 {
288 global $conf;
289
290 if ($type == 'supplier') {
291 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
292 $typethirdparty = 'accountancy_code_supplier';
293 } else {
294 $typethirdparty = 'code_compta_fournisseur';
295 }
296 } elseif ($type == 'customer') {
297 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
298 $typethirdparty = 'accountancy_code_customer';
299 } else {
300 $typethirdparty = 'code_compta';
301 }
302 } else {
303 $this->error = 'Bad value for parameter type';
304 return -1;
305 }
306
307 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
308 $sql = "SELECT " . $typethirdparty . " FROM " . MAIN_DB_PREFIX . "societe_perentity";
309 $sql .= " WHERE " . $typethirdparty . " = '" . $db->escape($code) . "'";
310 } else {
311 $sql = "SELECT " . $typethirdparty . " FROM " . MAIN_DB_PREFIX . "societe";
312 $sql .= " WHERE " . $typethirdparty . " = '" . $db->escape($code) . "'";
313 }
314 $sql .= " AND entity IN (".getEntity('societe').")";
315
316 $resql = $db->query($sql);
317 if ($resql) {
318 if ($db->num_rows($resql) == 0) {
319 dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' available");
320 return 0; // Available
321 } else {
322 dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' not available");
323 return -1; // Not available
324 }
325 } else {
326 $this->error = $db->error()." sql=".$sql;
327 return -2; // Error
328 }
329 }
330}
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.
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:140