dolibarr 18.0.6
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 Frédéric France <frederic.france@netlogic.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * or see https://www.gnu.org/
20 */
21
27require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
28
29
34{
38 public $name = 'Digitaria';
39
44 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
45
50 public $prefixcustomeraccountancycode;
51
56 public $prefixsupplieraccountancycode;
57
58 public $position = 30;
59
60
64 public function __construct()
65 {
66 global $conf, $langs;
67 if (!isset($conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER) || trim($conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER) == '') {
68 $conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER = '411';
69 }
70 if (!isset($conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER) || trim($conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER) == '') {
71 $conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER = '401';
72 }
73 $this->prefixcustomeraccountancycode = $conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER;
74 $this->prefixsupplieraccountancycode = $conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER;
75
76 if (!isset($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER) || trim($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER) == '') {
77 $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER = '5';
78 }
79 if (!isset($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER) || trim($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER) == '') {
80 $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER = '5';
81 }
82 $this->customeraccountancycodecharacternumber = $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER;
83 $this->supplieraccountancycodecharacternumber = $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER;
84 }
85
92 public function info($langs)
93 {
94 global $conf, $form;
95
96 $tooltip = '';
97 $texte = '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
98 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
99 $texte .= '<input type="hidden" name="page_y" value="">';
100 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
101 $texte .= '<input type="hidden" name="param1" value="COMPANY_DIGITARIA_MASK_SUPPLIER">';
102 $texte .= '<input type="hidden" name="param2" value="COMPANY_DIGITARIA_MASK_CUSTOMER">';
103 $texte .= '<input type="hidden" name="param3" value="COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER">';
104 $texte .= '<input type="hidden" name="param4" value="COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER">';
105 $texte .= '<input type="hidden" name="param5" value="COMPANY_DIGITARIA_CLEAN_WORDS">';
106 $texte .= '<table class="nobordernopadding centpercent">';
107 $s1 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value1" value="'.$conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER.'">', $tooltip, 1, 1);
108 $s2 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value2" value="'.$conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER.'">', $tooltip, 1, 1);
109 $s3 = $form->textwithpicto('<input type="text" class="flat" size="2" name="value3" value="'.$conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER.'">', $tooltip, 1, 1);
110 $s4 = $form->textwithpicto('<input type="text" class="flat" size="2" name="value4" value="'.$conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER.'">', $tooltip, 1, 1);
111 $texte .= '<tr><td>';
112 // trans remove html entities
113 $texte .= $langs->trans("ModuleCompanyCodeCustomer".$this->name, '{s2}', '{s4}')."<br>\n";
114 $texte .= $langs->trans("ModuleCompanyCodeSupplier".$this->name, '{s1}', '{s3}')."<br>\n";
115 $texte = str_replace(array('{s1}', '{s2}', '{s3}', '{s4}'), array($s1, $s2, $s3, $s4), $texte);
116 $texte .= "<br>\n";
117 // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default)
118 if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || !empty($conf->global->$conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) {
119 $texte .= $langs->trans('RemoveSpecialChars').' = '.yn(1)."<br>\n";
120 }
121 // 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.
122 if (!empty($conf->global->COMPANY_DIGITARIA_CLEAN_REGEX)) {
123 $texte .= $langs->trans('COMPANY_DIGITARIA_CLEAN_REGEX').' = '.$conf->global->COMPANY_DIGITARIA_CLEAN_REGEX."<br>\n";
124 }
125 // Unique index on code if COMPANY_DIGITARIA_UNIQUE_CODE is set to 1 or not set (default)
126 if (!isset($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE) || !empty($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE)) {
127 $texte .= $langs->trans('COMPANY_DIGITARIA_UNIQUE_CODE').' = '.yn(1)."<br>\n";
128 }
129 $texte .= '</td>';
130 $texte .= '<td class="right"><input type="submit" class="button button-edit reposition" name="modify" value="'.$langs->trans("Modify").'"></td>';
131 $texte .= '</tr>';
132
133 $texte .= '<tr><td>';
134 $texte .= "<br>\n";
135
136 $texthelp = $langs->trans("RemoveSpecialWordsHelp");
137 $texttitle = $langs->trans("RemoveSpecialWords");
138
139 $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1);
140 $texte .= "<br>\n";
141 $texte .= '<textarea class="flat" cols="60" name="value5">';
142 if (!empty($conf->global->COMPANY_DIGITARIA_CLEAN_WORDS)) {
143 $texte .= $conf->global->COMPANY_DIGITARIA_CLEAN_WORDS;
144 }
145 $texte .= '</textarea>';
146 $texte .= '</tr></table>';
147
148 $texte .= '</form>';
149
150 return $texte;
151 }
152
161 public function getExample($langs, $objsoc = 0, $type = -1)
162 {
163 global $conf, $mysoc;
164
165 $s = $langs->trans("ThirdPartyName").": ".$mysoc->name;
166 $s .= "<br>\n";
167
168 if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) {
169 $thirdpartylabelexample = preg_replace('/([^a-z0-9])/i', '', $mysoc->name);
170 }
171 $s .= "<br>\n";
172 $s .= $this->prefixcustomeraccountancycode.strtoupper(substr($thirdpartylabelexample, 0, $this->customeraccountancycodecharacternumber));
173 $s .= "<br>\n";
174 $s .= $this->prefixsupplieraccountancycode.strtoupper(substr($thirdpartylabelexample, 0, $this->supplieraccountancycodecharacternumber));
175 return $s;
176 }
177
178 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
187 public function get_code($db, $societe, $type = '')
188 {
189 // phpcs:enable
190 global $conf;
191 $i = 0;
192 $this->code = '';
193
194 $disponibility = 0;
195
196 if (is_object($societe)) {
197 dol_syslog("mod_codecompta_digitaria::get_code search code for type=".$type." & company=".(!empty($societe->name) ? $societe->name : ''));
198
199 if ($type == 'supplier') {
200 $codetouse = $societe->name;
201 $prefix = $this->prefixsupplieraccountancycode;
202 $width = $this->supplieraccountancycodecharacternumber;
203 } elseif ($type == 'customer') {
204 $codetouse = $societe->name;
205 $prefix = $this->prefixcustomeraccountancycode;
206 $width = $this->customeraccountancycodecharacternumber;
207 } else {
208 $this->error = 'Bad value for parameter type';
209 return -1;
210 }
211
212 // Clean declared words
213 if (!empty($conf->global->COMPANY_DIGITARIA_CLEAN_WORDS)) {
214 $cleanWords = explode(";", $conf->global->COMPANY_DIGITARIA_CLEAN_WORDS);
215 $codetouse = str_replace($cleanWords, "", $codetouse);
216 }
217 // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default)
218 if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || !empty($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) {
219 $codetouse = preg_replace('/([^a-z0-9])/i', '', $codetouse);
220 }
221 // 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.
222 if (!empty($conf->global->COMPANY_DIGITARIA_CLEAN_REGEX)) { // Example: $conf->global->COMPANY_DIGITARIA_CLEAN_REGEX='^..(..)..';
223 $codetouse = preg_replace('/'.$conf->global->COMPANY_DIGITARIA_CLEAN_REGEX.'/', '\1\2\3', $codetouse);
224 }
225
226 $this->code = $prefix.strtoupper(substr($codetouse, 0, $width));
227 dol_syslog("mod_codecompta_digitaria::get_code search code proposed=".$this->code, LOG_DEBUG);
228
229 // Unique index on code if COMPANY_DIGITARIA_UNIQUE_CODE is set to 1 or not set (default)
230 if (!isset($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE) || !empty($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE)) {
231 $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type);
232
233 while ($disponibility <> 0 && $i < 1000) {
234 $widthsupplier = $this->supplieraccountancycodecharacternumber;
235 $widthcustomer = $this->customeraccountancycodecharacternumber;
236
237 if ($i <= 9) {
238 $a = 1;
239 }
240 if ($i >= 10 && $i <= 99) {
241 $a = 2;
242 }
243 if ($i >= 100 && $i <= 999) {
244 $a = 3;
245 }
246
247 if ($type == 'supplier') {
248 $this->code = $prefix.strtoupper(substr($codetouse, 0, $widthsupplier - $a)).$i;
249 } elseif ($type == 'customer') {
250 $this->code = $prefix.strtoupper(substr($codetouse, 0, $widthcustomer - $a)).$i;
251 }
252 $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type);
253
254 $i++;
255 }
256 } else {
257 $disponibility == 0;
258 }
259 }
260
261 if ($disponibility == 0) {
262 return 0; // return ok
263 } else {
264 return -1; // return ko
265 }
266 }
267
276 public function checkIfAccountancyCodeIsAlreadyUsed($db, $code, $type = '')
277 {
278 global $conf;
279
280 if ($type == 'supplier') {
281 if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
282 $typethirdparty = 'accountancy_code_supplier';
283 } else {
284 $typethirdparty = 'code_compta_fournisseur';
285 }
286 } elseif ($type == 'customer') {
287 if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
288 $typethirdparty = 'accountancy_code_customer';
289 } else {
290 $typethirdparty = 'code_compta';
291 }
292 } else {
293 $this->error = 'Bad value for parameter type';
294 return -1;
295 }
296
297 if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
298 $sql = "SELECT " . $typethirdparty . " FROM " . MAIN_DB_PREFIX . "societe_perentity";
299 $sql .= " WHERE " . $typethirdparty . " = '" . $db->escape($code) . "'";
300 } else {
301 $sql = "SELECT " . $typethirdparty . " FROM " . MAIN_DB_PREFIX . "societe";
302 $sql .= " WHERE " . $typethirdparty . " = '" . $db->escape($code) . "'";
303 }
304 $sql .= " AND entity IN (".getEntity('societe').")";
305
306 $resql = $db->query($sql);
307 if ($resql) {
308 if ($db->num_rows($resql) == 0) {
309 dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' available");
310 return 0; // Available
311 } else {
312 dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' not available");
313 return -1; // Not available
314 }
315 } else {
316 $this->error = $db->error()." sql=".$sql;
317 return -2; // Error
318 }
319 }
320}
Parent class for third parties accountancy code generators.
Class to manage accountancy code of thirdparties with Digitaria rules.
get_code($db, $societe, $type='')
Set accountancy account code for a third party into this->code.
info($langs)
Return description of module.
getExample($langs, $objsoc=0, $type=-1)
Return an example of result returned by getNextValue.
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.
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:123