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, $objsoc = '', $type = -1)
170 {
171 global $conf, $mysoc;
172
173 $s = $langs->trans("ThirdPartyName").": ".$mysoc->name;
174 $s .= "<br>\n";
175
176 if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) {
177 $thirdpartylabelexample = preg_replace('/([^a-z0-9])/i', '', $mysoc->name);
178 }
179 $s .= "<br>\n";
180 $s .= $this->prefixcustomeraccountancycode.strtoupper(substr($thirdpartylabelexample, 0, (int) $this->customeraccountancycodecharacternumber));
181 $s .= "<br>\n";
182 $s .= $this->prefixsupplieraccountancycode.strtoupper(substr($thirdpartylabelexample, 0, (int) $this->supplieraccountancycodecharacternumber));
183 return $s;
184 }
185
186 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
195 public function get_code($db, $societe, $type = '')
196 {
197 // phpcs:enable
198 global $conf;
199 $i = 0;
200 $this->code = '';
201
202 $disponibility = 0;
203
204 if (is_object($societe)) {
205 dol_syslog("mod_codecompta_digitaria::get_code search code for type=".$type." & company=".(!empty($societe->name) ? $societe->name : ''));
206
207 if ($type == 'supplier') {
208 $codetouse = $societe->name;
209 $prefix = $this->prefixsupplieraccountancycode;
210 $width = $this->supplieraccountancycodecharacternumber;
211 } elseif ($type == 'customer') {
212 $codetouse = $societe->name;
213 $prefix = $this->prefixcustomeraccountancycode;
214 $width = $this->customeraccountancycodecharacternumber;
215 } else {
216 $this->error = 'Bad value for parameter type';
217 return -1;
218 }
219
220 // Clean declared words
221 if (getDolGlobalString('COMPANY_DIGITARIA_CLEAN_WORDS')) {
222 $cleanWords = explode(";", getDolGlobalString('COMPANY_DIGITARIA_CLEAN_WORDS'));
223 $codetouse = str_replace($cleanWords, "", $codetouse);
224 }
225 // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default)
226 if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || getDolGlobalString('COMPANY_DIGITARIA_REMOVE_SPECIAL')) {
227 $codetouse = preg_replace('/([^a-z0-9])/i', '', $codetouse);
228 }
229 // 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.
230 if (getDolGlobalString('COMPANY_DIGITARIA_CLEAN_REGEX')) { // Example: $conf->global->COMPANY_DIGITARIA_CLEAN_REGEX='^..(..)..';
231 $codetouse = preg_replace('/' . getDolGlobalString('COMPANY_DIGITARIA_CLEAN_REGEX').'/', '\1\2\3', $codetouse);
232 }
233
234 $this->code = $prefix.strtoupper(substr($codetouse, 0, (int) $width));
235 dol_syslog("mod_codecompta_digitaria::get_code search code proposed=".$this->code, LOG_DEBUG);
236
237 // Unique index on code if COMPANY_DIGITARIA_UNIQUE_CODE is set to 1 or not set (default)
238 if (getDolGlobalString('COMPANY_DIGITARIA_UNIQUE_CODE', '1')) {
239 $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type);
240
241 while ($disponibility != 0 && $i < 1000) {
242 $widthsupplier = $this->supplieraccountancycodecharacternumber;
243 $widthcustomer = $this->customeraccountancycodecharacternumber;
244
245 if ($i <= 9) {
246 $a = 1;
247 }
248 if ($i >= 10 && $i <= 99) {
249 $a = 2;
250 }
251 if ($i >= 100 && $i <= 999) {
252 $a = 3;
253 }
254
255 if ($type == 'supplier') {
256 $this->code = $prefix.strtoupper(substr($codetouse, 0, $widthsupplier - $a)).$i;
257 } elseif ($type == 'customer') {
258 $this->code = $prefix.strtoupper(substr($codetouse, 0, $widthcustomer - $a)).$i;
259 }
260 $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type);
261
262 $i++;
263 }
264 } // else { $disponibility = 0; /* Already set */ }
265 }
266
267 if ($disponibility == 0) {
268 return 0; // return ok
269 } else {
270 return -1; // return ko
271 }
272 }
273
282 public function checkIfAccountancyCodeIsAlreadyUsed($db, $code, $type = '')
283 {
284 global $conf;
285
286 if ($type == 'supplier') {
287 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
288 $typethirdparty = 'accountancy_code_supplier';
289 } else {
290 $typethirdparty = 'code_compta_fournisseur';
291 }
292 } elseif ($type == 'customer') {
293 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
294 $typethirdparty = 'accountancy_code_customer';
295 } else {
296 $typethirdparty = 'code_compta';
297 }
298 } else {
299 $this->error = 'Bad value for parameter type';
300 return -1;
301 }
302
303 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
304 $sql = "SELECT " . $typethirdparty . " FROM " . MAIN_DB_PREFIX . "societe_perentity";
305 $sql .= " WHERE " . $typethirdparty . " = '" . $db->escape($code) . "'";
306 } else {
307 $sql = "SELECT " . $typethirdparty . " FROM " . MAIN_DB_PREFIX . "societe";
308 $sql .= " WHERE " . $typethirdparty . " = '" . $db->escape($code) . "'";
309 }
310 $sql .= " AND entity IN (".getEntity('societe').")";
311
312 $resql = $db->query($sql);
313 if ($resql) {
314 if ($db->num_rows($resql) == 0) {
315 dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' available");
316 return 0; // Available
317 } else {
318 dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' not available");
319 return -1; // Not available
320 }
321 } else {
322 $this->error = $db->error()." sql=".$sql;
323 return -2; // Error
324 }
325 }
326}
Parent class for third parties accountancy code generators.
Class to manage accountancy code of thirdparties with Digitaria rules.
getExample($langs, $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, $case=1, $color=0)
Return yes or no in current language.
getDolGlobalString($key, $default='')
Return 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:142