dolibarr  16.0.5
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 
27 require_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 .= '<table class="nobordernopadding" width="100%">';
106  $s1 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value1" value="'.$conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER.'">', $tooltip, 1, 1);
107  $s2 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value2" value="'.$conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER.'">', $tooltip, 1, 1);
108  $s3 = $form->textwithpicto('<input type="text" class="flat" size="2" name="value3" value="'.$conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER.'">', $tooltip, 1, 1);
109  $s4 = $form->textwithpicto('<input type="text" class="flat" size="2" name="value4" value="'.$conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER.'">', $tooltip, 1, 1);
110  $texte .= '<tr><td>';
111  // trans remove html entities
112  $texte .= $langs->trans("ModuleCompanyCodeCustomer".$this->name, '{s2}', '{s4}')."<br>\n";
113  $texte .= $langs->trans("ModuleCompanyCodeSupplier".$this->name, '{s1}', '{s3}')."<br>\n";
114  $texte = str_replace(array('{s1}', '{s2}', '{s3}', '{s4}'), array($s1, $s2, $s3, $s4), $texte);
115  $texte .= "<br>\n";
116  // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default)
117  if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || !empty($conf->global->$conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) {
118  $texte .= $langs->trans('RemoveSpecialChars').' = '.yn(1)."<br>\n";
119  }
120  // 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.
121  if (!empty($conf->global->COMPANY_DIGITARIA_CLEAN_REGEX)) {
122  $texte .= $langs->trans('COMPANY_DIGITARIA_CLEAN_REGEX').' = '.$conf->global->COMPANY_DIGITARIA_CLEAN_REGEX."<br>\n";
123  }
124  // Unique index on code if COMPANY_DIGITARIA_UNIQUE_CODE is set to 1 or not set (default)
125  if (!isset($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE) || !empty($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE)) {
126  $texte .= $langs->trans('COMPANY_DIGITARIA_UNIQUE_CODE').' = '.yn(1)."<br>\n";
127  }
128  $texte .= '</td>';
129  $texte .= '<td class="right"><input type="submit" class="button button-edit reposition" name="modify" value="'.$langs->trans("Modify").'"></td>';
130  $texte .= '</tr></table>';
131  $texte .= '</form>';
132 
133  return $texte;
134  }
135 
144  public function getExample($langs, $objsoc = 0, $type = -1)
145  {
146  global $conf, $mysoc;
147 
148  $s = $langs->trans("ThirdPartyName").": ".$mysoc->name;
149  $s .= "<br>\n";
150 
151  if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) {
152  $thirdpartylabelexample = preg_replace('/([^a-z0-9])/i', '', $mysoc->name);
153  }
154  $s .= "<br>\n";
155  $s .= $this->prefixcustomeraccountancycode.strtoupper(substr($thirdpartylabelexample, 0, $this->customeraccountancycodecharacternumber));
156  $s .= "<br>\n";
157  $s .= $this->prefixsupplieraccountancycode.strtoupper(substr($thirdpartylabelexample, 0, $this->supplieraccountancycodecharacternumber));
158  return $s;
159  }
160 
161  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
170  public function get_code($db, $societe, $type = '')
171  {
172  // phpcs:enable
173  global $conf;
174  $i = 0;
175  $this->code = '';
176 
177  $disponibility = 0;
178 
179  if (is_object($societe)) {
180  dol_syslog("mod_codecompta_digitaria::get_code search code for type=".$type." & company=".(!empty($societe->name) ? $societe->name : ''));
181 
182  if ($type == 'supplier') {
183  $codetouse = $societe->name;
184  $prefix = $this->prefixsupplieraccountancycode;
185  $width = $this->supplieraccountancycodecharacternumber;
186  } elseif ($type == 'customer') {
187  $codetouse = $societe->name;
188  $prefix = $this->prefixcustomeraccountancycode;
189  $width = $this->customeraccountancycodecharacternumber;
190  } else {
191  $this->error = 'Bad value for parameter type';
192  return -1;
193  }
194 
195  // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default)
196  if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || !empty($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) {
197  $codetouse = preg_replace('/([^a-z0-9])/i', '', $codetouse);
198  }
199  // 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.
200  if (!empty($conf->global->COMPANY_DIGITARIA_CLEAN_REGEX)) { // Example: $conf->global->COMPANY_DIGITARIA_CLEAN_REGEX='^..(..)..';
201  $codetouse = preg_replace('/'.$conf->global->COMPANY_DIGITARIA_CLEAN_REGEX.'/', '\1\2\3', $codetouse);
202  }
203 
204  $this->code = $prefix.strtoupper(substr($codetouse, 0, $width));
205  dol_syslog("mod_codecompta_digitaria::get_code search code proposed=".$this->code);
206 
207  // Unique index on code if COMPANY_DIGITARIA_UNIQUE_CODE is set to 1 or not set (default)
208  if (!isset($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE) || !empty($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE)) {
209  $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type);
210 
211  while ($disponibility <> 0 && $i < 1000) {
212  $widthsupplier = $this->supplieraccountancycodecharacternumber;
213  $widthcustomer = $this->customeraccountancycodecharacternumber;
214 
215  if ($i <= 9) {
216  $a = 1;
217  }
218  if ($i >= 10 && $i <= 99) {
219  $a = 2;
220  }
221  if ($i >= 100 && $i <= 999) {
222  $a = 3;
223  }
224 
225  if ($type == 'supplier') {
226  $this->code = $prefix.strtoupper(substr($codetouse, 0, $widthsupplier - $a)).$i;
227  } elseif ($type == 'customer') {
228  $this->code = $prefix.strtoupper(substr($codetouse, 0, $widthcustomer - $a)).$i;
229  }
230  $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type);
231 
232  $i++;
233  }
234  } else {
235  $disponibility == 0;
236  }
237  }
238 
239  if ($disponibility == 0) {
240  return 0; // return ok
241  } else {
242  return -1; // return ko
243  }
244  }
245 
254  public function checkIfAccountancyCodeIsAlreadyUsed($db, $code, $type = '')
255  {
256  global $conf;
257 
258  if ($type == 'supplier') {
259  if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
260  $typethirdparty = 'accountancy_code_supplier';
261  } else {
262  $typethirdparty = 'code_compta_fournisseur';
263  }
264  } elseif ($type == 'customer') {
265  if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
266  $typethirdparty = 'accountancy_code_customer';
267  } else {
268  $typethirdparty = 'code_compta';
269  }
270  } else {
271  $this->error = 'Bad value for parameter type';
272  return -1;
273  }
274 
275  if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
276  $sql = "SELECT " . $typethirdparty . " FROM " . MAIN_DB_PREFIX . "societe_perentity";
277  $sql .= " WHERE " . $typethirdparty . " = '" . $db->escape($code) . "'";
278  } else {
279  $sql = "SELECT " . $typethirdparty . " FROM " . MAIN_DB_PREFIX . "societe";
280  $sql .= " WHERE " . $typethirdparty . " = '" . $db->escape($code) . "'";
281  }
282 
283  $resql = $db->query($sql);
284  if ($resql) {
285  if ($db->num_rows($resql) == 0) {
286  dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' available");
287  return 0; // Available
288  } else {
289  dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' not available");
290  return -1; // Not available
291  }
292  } else {
293  $this->error = $db->error()." sql=".$sql;
294  return -2; // Error
295  }
296  }
297 }
yn
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
Definition: functions.lib.php:6477
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
name
$conf db name
Definition: repair.php:122
ModeleAccountancyCode
Parent class for third parties accountancy code generators.
Definition: modules_societe.class.php:277
mod_codecompta_digitaria\getExample
getExample($langs, $objsoc=0, $type=-1)
Return an example of result returned by getNextValue.
Definition: mod_codecompta_digitaria.php:144
mod_codecompta_digitaria\get_code
get_code($db, $societe, $type='')
Set accountancy account code for a third party into this->code.
Definition: mod_codecompta_digitaria.php:170
mod_codecompta_digitaria\__construct
__construct()
Constructor.
Definition: mod_codecompta_digitaria.php:64
code
print *****$script_file(".$version.") pid code
! Closing after partial payment: discount_vat, badcustomer or badsupplier, bankcharge,...
Definition: sync_members_ldap2dolibarr.php:60
mod_codecompta_digitaria
Class to manage accountancy code of thirdparties with Digitaria rules.
Definition: mod_codecompta_digitaria.php:33
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1589
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
mod_codecompta_digitaria\info
info($langs)
Return description of module.
Definition: mod_codecompta_digitaria.php:92
mod_codecompta_digitaria\checkIfAccountancyCodeIsAlreadyUsed
checkIfAccountancyCodeIsAlreadyUsed($db, $code, $type='')
Check accountancy account code for a third party into this->code.
Definition: mod_codecompta_digitaria.php:254