dolibarr 19.0.3
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
63 public $code;
64 public $customeraccountancycodecharacternumber;
65 public $supplieraccountancycodecharacternumber;
66
67
71 public function __construct()
72 {
73 global $conf, $langs;
74 if (!isset($conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER) || trim($conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER) == '') {
75 $conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER = '411';
76 }
77 if (!isset($conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER) || trim($conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER) == '') {
78 $conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER = '401';
79 }
80 $this->prefixcustomeraccountancycode = $conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER;
81 $this->prefixsupplieraccountancycode = $conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER;
82
83 if (!isset($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER) || trim($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER) == '') {
84 $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER = '5';
85 }
86 if (!isset($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER) || trim($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER) == '') {
87 $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER = '5';
88 }
89 $this->customeraccountancycodecharacternumber = $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER;
90 $this->supplieraccountancycodecharacternumber = $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER;
91 }
92
99 public function info($langs)
100 {
101 global $conf, $form;
102
103 $tooltip = '';
104 $texte = '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
105 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
106 $texte .= '<input type="hidden" name="page_y" value="">';
107 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
108 $texte .= '<input type="hidden" name="param1" value="COMPANY_DIGITARIA_MASK_SUPPLIER">';
109 $texte .= '<input type="hidden" name="param2" value="COMPANY_DIGITARIA_MASK_CUSTOMER">';
110 $texte .= '<input type="hidden" name="param3" value="COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER">';
111 $texte .= '<input type="hidden" name="param4" value="COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER">';
112 $texte .= '<input type="hidden" name="param5" value="COMPANY_DIGITARIA_CLEAN_WORDS">';
113 $texte .= '<table class="nobordernopadding centpercent">';
114 $s1 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value1" value="' . getDolGlobalString('COMPANY_DIGITARIA_MASK_SUPPLIER').'">', $tooltip, 1, 1);
115 $s2 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value2" value="' . getDolGlobalString('COMPANY_DIGITARIA_MASK_CUSTOMER').'">', $tooltip, 1, 1);
116 $s3 = $form->textwithpicto('<input type="text" class="flat" size="2" name="value3" value="' . getDolGlobalString('COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER').'">', $tooltip, 1, 1);
117 $s4 = $form->textwithpicto('<input type="text" class="flat" size="2" name="value4" value="' . getDolGlobalString('COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER').'">', $tooltip, 1, 1);
118 $texte .= '<tr><td>';
119 // trans remove html entities
120 $texte .= $langs->trans("ModuleCompanyCodeCustomer".$this->name, '{s2}', '{s4}')."<br>\n";
121 $texte .= $langs->trans("ModuleCompanyCodeSupplier".$this->name, '{s1}', '{s3}')."<br>\n";
122 $texte = str_replace(array('{s1}', '{s2}', '{s3}', '{s4}'), array($s1, $s2, $s3, $s4), $texte);
123 $texte .= "<br>\n";
124 // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default)
125 if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || !empty($conf->global->$conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) {
126 $texte .= $langs->trans('RemoveSpecialChars').' = '.yn(1)."<br>\n";
127 }
128 // 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.
129 if (getDolGlobalString('COMPANY_DIGITARIA_CLEAN_REGEX')) {
130 $texte .= $langs->trans('COMPANY_DIGITARIA_CLEAN_REGEX').' = ' . getDolGlobalString('COMPANY_DIGITARIA_CLEAN_REGEX')."<br>\n";
131 }
132 // Unique index on code if COMPANY_DIGITARIA_UNIQUE_CODE is set to 1 or not set (default)
133 if (!isset($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE) || getDolGlobalString('COMPANY_DIGITARIA_UNIQUE_CODE')) {
134 $texte .= $langs->trans('COMPANY_DIGITARIA_UNIQUE_CODE').' = '.yn(1)."<br>\n";
135 }
136 $texte .= '</td>';
137 $texte .= '<td class="right"><input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"></td>';
138 $texte .= '</tr>';
139
140 $texte .= '<tr><td>';
141 $texte .= "<br>\n";
142
143 $texthelp = $langs->trans("RemoveSpecialWordsHelp");
144 $texttitle = $langs->trans("RemoveSpecialWords");
145
146 $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1);
147 $texte .= "<br>\n";
148 $texte .= '<textarea class="flat" cols="60" name="value5">';
149 if (getDolGlobalString('COMPANY_DIGITARIA_CLEAN_WORDS')) {
150 $texte .= $conf->global->COMPANY_DIGITARIA_CLEAN_WORDS;
151 }
152 $texte .= '</textarea>';
153 $texte .= '</tr></table>';
154
155 $texte .= '</form>';
156
157 return $texte;
158 }
159
168 public function getExample($langs, $objsoc = 0, $type = -1)
169 {
170 global $conf, $mysoc;
171
172 $s = $langs->trans("ThirdPartyName").": ".$mysoc->name;
173 $s .= "<br>\n";
174
175 if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) {
176 $thirdpartylabelexample = preg_replace('/([^a-z0-9])/i', '', $mysoc->name);
177 }
178 $s .= "<br>\n";
179 $s .= $this->prefixcustomeraccountancycode.strtoupper(substr($thirdpartylabelexample, 0, $this->customeraccountancycodecharacternumber));
180 $s .= "<br>\n";
181 $s .= $this->prefixsupplieraccountancycode.strtoupper(substr($thirdpartylabelexample, 0, $this->supplieraccountancycodecharacternumber));
182 return $s;
183 }
184
185 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
194 public function get_code($db, $societe, $type = '')
195 {
196 // phpcs:enable
197 global $conf;
198 $i = 0;
199 $this->code = '';
200
201 $disponibility = 0;
202
203 if (is_object($societe)) {
204 dol_syslog("mod_codecompta_digitaria::get_code search code for type=".$type." & company=".(!empty($societe->name) ? $societe->name : ''));
205
206 if ($type == 'supplier') {
207 $codetouse = $societe->name;
208 $prefix = $this->prefixsupplieraccountancycode;
209 $width = $this->supplieraccountancycodecharacternumber;
210 } elseif ($type == 'customer') {
211 $codetouse = $societe->name;
212 $prefix = $this->prefixcustomeraccountancycode;
213 $width = $this->customeraccountancycodecharacternumber;
214 } else {
215 $this->error = 'Bad value for parameter type';
216 return -1;
217 }
218
219 // Clean declared words
220 if (getDolGlobalString('COMPANY_DIGITARIA_CLEAN_WORDS')) {
221 $cleanWords = explode(";", getDolGlobalString('COMPANY_DIGITARIA_CLEAN_WORDS'));
222 $codetouse = str_replace($cleanWords, "", $codetouse);
223 }
224 // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default)
225 if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || getDolGlobalString('COMPANY_DIGITARIA_REMOVE_SPECIAL')) {
226 $codetouse = preg_replace('/([^a-z0-9])/i', '', $codetouse);
227 }
228 // 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.
229 if (getDolGlobalString('COMPANY_DIGITARIA_CLEAN_REGEX')) { // Example: $conf->global->COMPANY_DIGITARIA_CLEAN_REGEX='^..(..)..';
230 $codetouse = preg_replace('/' . getDolGlobalString('COMPANY_DIGITARIA_CLEAN_REGEX').'/', '\1\2\3', $codetouse);
231 }
232
233 $this->code = $prefix.strtoupper(substr($codetouse, 0, $width));
234 dol_syslog("mod_codecompta_digitaria::get_code search code proposed=".$this->code, LOG_DEBUG);
235
236 // Unique index on code if COMPANY_DIGITARIA_UNIQUE_CODE is set to 1 or not set (default)
237 if (!isset($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE) || getDolGlobalString('COMPANY_DIGITARIA_UNIQUE_CODE')) {
238 $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type);
239
240 while ($disponibility != 0 && $i < 1000) {
241 $widthsupplier = $this->supplieraccountancycodecharacternumber;
242 $widthcustomer = $this->customeraccountancycodecharacternumber;
243
244 if ($i <= 9) {
245 $a = 1;
246 }
247 if ($i >= 10 && $i <= 99) {
248 $a = 2;
249 }
250 if ($i >= 100 && $i <= 999) {
251 $a = 3;
252 }
253
254 if ($type == 'supplier') {
255 $this->code = $prefix.strtoupper(substr($codetouse, 0, $widthsupplier - $a)).$i;
256 } elseif ($type == 'customer') {
257 $this->code = $prefix.strtoupper(substr($codetouse, 0, $widthcustomer - $a)).$i;
258 }
259 $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type);
260
261 $i++;
262 }
263 } else {
264 $disponibility == 0;
265 }
266 }
267
268 if ($disponibility == 0) {
269 return 0; // return ok
270 } else {
271 return -1; // return ko
272 }
273 }
274
283 public function checkIfAccountancyCodeIsAlreadyUsed($db, $code, $type = '')
284 {
285 global $conf;
286
287 if ($type == 'supplier') {
288 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
289 $typethirdparty = 'accountancy_code_supplier';
290 } else {
291 $typethirdparty = 'code_compta_fournisseur';
292 }
293 } elseif ($type == 'customer') {
294 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
295 $typethirdparty = 'accountancy_code_customer';
296 } else {
297 $typethirdparty = 'code_compta';
298 }
299 } else {
300 $this->error = 'Bad value for parameter type';
301 return -1;
302 }
303
304 if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
305 $sql = "SELECT " . $typethirdparty . " FROM " . MAIN_DB_PREFIX . "societe_perentity";
306 $sql .= " WHERE " . $typethirdparty . " = '" . $db->escape($code) . "'";
307 } else {
308 $sql = "SELECT " . $typethirdparty . " FROM " . MAIN_DB_PREFIX . "societe";
309 $sql .= " WHERE " . $typethirdparty . " = '" . $db->escape($code) . "'";
310 }
311 $sql .= " AND entity IN (".getEntity('societe').")";
312
313 $resql = $db->query($sql);
314 if ($resql) {
315 if ($db->num_rows($resql) == 0) {
316 dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' available");
317 return 0; // Available
318 } else {
319 dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' not available");
320 return -1; // Not available
321 }
322 } else {
323 $this->error = $db->error()." sql=".$sql;
324 return -2; // Error
325 }
326 }
327}
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.
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:124