dolibarr  13.0.2
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) == '') $conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER = '411';
68  if (!isset($conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER) || trim($conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER) == '') $conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER = '401';
69  $this->prefixcustomeraccountancycode = $conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER;
70  $this->prefixsupplieraccountancycode = $conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER;
71 
72  if (!isset($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER) || trim($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER) == '') $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER = '5';
73  if (!isset($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER) || trim($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER) == '') $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER = '5';
74  $this->customeraccountancycodecharacternumber = $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER;
75  $this->supplieraccountancycodecharacternumber = $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER;
76  }
77 
84  public function info($langs)
85  {
86  global $conf, $form;
87 
88  $tooltip = '';
89  $texte = '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
90  $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
91  $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
92  $texte .= '<input type="hidden" name="param1" value="COMPANY_DIGITARIA_MASK_SUPPLIER">';
93  $texte .= '<input type="hidden" name="param2" value="COMPANY_DIGITARIA_MASK_CUSTOMER">';
94  $texte .= '<input type="hidden" name="param3" value="COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER">';
95  $texte .= '<input type="hidden" name="param4" value="COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER">';
96  $texte .= '<table class="nobordernopadding" width="100%">';
97  $s1 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value1" value="'.$conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER.'">', $tooltip, 1, 1);
98  $s2 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value2" value="'.$conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER.'">', $tooltip, 1, 1);
99  $s3 = $form->textwithpicto('<input type="text" class="flat" size="2" name="value3" value="'.$conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER.'">', $tooltip, 1, 1);
100  $s4 = $form->textwithpicto('<input type="text" class="flat" size="2" name="value4" value="'.$conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER.'">', $tooltip, 1, 1);
101  $texte .= '<tr><td>';
102  // trans remove html entities
103  $texte .= $langs->trans("ModuleCompanyCodeCustomer".$this->name, '{s2}', '{s4}')."<br>\n";
104  $texte .= $langs->trans("ModuleCompanyCodeSupplier".$this->name, '{s1}', '{s3}')."<br>\n";
105  $texte = str_replace(array('{s1}', '{s2}', '{s3}', '{s4}'), array($s1, $s2, $s3, $s4), $texte);
106  $texte .= "<br>\n";
107  // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default)
108  if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || !empty($conf->global->$conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) $texte .= $langs->trans('RemoveSpecialChars').' = '.yn(1)."<br>\n";
109  // 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.
110  if (!empty($conf->global->COMPANY_DIGITARIA_CLEAN_REGEX)) $texte .= $langs->trans('COMPANY_DIGITARIA_CLEAN_REGEX').' = '.$conf->global->COMPANY_DIGITARIA_CLEAN_REGEX."<br>\n";
111  // Unique index on code if COMPANY_DIGITARIA_UNIQUE_CODE is set to 1 or not set (default)
112  if (!isset($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE) || !empty($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE)) $texte .= $langs->trans('COMPANY_DIGITARIA_UNIQUE_CODE').' = '.yn(1)."<br>\n";
113  $texte .= '</td>';
114  $texte .= '<td class="right"><input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button"></td>';
115  $texte .= '</tr></table>';
116  $texte .= '</form>';
117 
118  return $texte;
119  }
120 
129  public function getExample($langs, $objsoc = 0, $type = -1)
130  {
131  global $conf, $mysoc;
132 
133  $s = $langs->trans("ThirdPartyName").": ".$mysoc->name;
134  $s .= "<br>\n";
135 
136  if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) $thirdpartylabelexample = preg_replace('/([^a-z0-9])/i', '', $mysoc->name);
137  $s .= "<br>\n";
138  $s .= $this->prefixcustomeraccountancycode.strtoupper(substr($thirdpartylabelexample, 0, $this->customeraccountancycodecharacternumber));
139  $s .= "<br>\n";
140  $s .= $this->prefixsupplieraccountancycode.strtoupper(substr($thirdpartylabelexample, 0, $this->supplieraccountancycodecharacternumber));
141  return $s;
142  }
143 
144  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
153  public function get_code($db, $societe, $type = '')
154  {
155  // phpcs:enable
156  global $conf;
157  $i = 0;
158  $this->code = '';
159 
160  $disponibility = 0;
161 
162  if (is_object($societe))
163  {
164  dol_syslog("mod_codecompta_digitaria::get_code search code for type=".$type." & company=".(!empty($societe->name) ? $societe->name : ''));
165 
166  if ($type == 'supplier') {
167  $codetouse = $societe->name;
168  $prefix = $this->prefixsupplieraccountancycode;
169  $width = $this->supplieraccountancycodecharacternumber;
170  } elseif ($type == 'customer')
171  {
172  $codetouse = $societe->name;
173  $prefix = $this->prefixcustomeraccountancycode;
174  $width = $this->customeraccountancycodecharacternumber;
175  } else {
176  $this->error = 'Bad value for parameter type';
177  return -1;
178  }
179 
180  // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default)
181  if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || !empty($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) $codetouse = preg_replace('/([^a-z0-9])/i', '', $codetouse);
182  // 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.
183  if (!empty($conf->global->COMPANY_DIGITARIA_CLEAN_REGEX)) // Example: $conf->global->COMPANY_DIGITARIA_CLEAN_REGEX='^..(..)..';
184  {
185  $codetouse = preg_replace('/'.$conf->global->COMPANY_DIGITARIA_CLEAN_REGEX.'/', '\1\2\3', $codetouse);
186  }
187 
188  $this->code = $prefix.strtoupper(substr($codetouse, 0, $width));
189  dol_syslog("mod_codecompta_digitaria::get_code search code proposed=".$this->code);
190 
191  // Unique index on code if COMPANY_DIGITARIA_UNIQUE_CODE is set to 1 or not set (default)
192  if (!isset($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE) || !empty($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE))
193  {
194  $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type);
195 
196  while ($disponibility <> 0 && $i < 100) {
197  $widthsupplier = $this->supplieraccountancycodecharacternumber;
198  $widthcustomer = $this->customeraccountancycodecharacternumber;
199 
200  if ($i <= 9) {
201  $a = 1;
202  }
203  if ($i >= 10 && $i <= 99) {
204  $a = 2;
205  }
206 
207  if ($type == 'supplier') {
208  $this->code = $prefix.strtoupper(substr($codetouse, 0, $widthsupplier - $a)).$i;
209  } elseif ($type == 'customer') {
210  $this->code = $prefix.strtoupper(substr($codetouse, 0, $widthcustomer - $a)).$i;
211  }
212  $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type);
213 
214  $i++;
215  }
216  } else {
217  $disponibility == 0;
218  }
219  }
220 
221  if ($disponibility == 0) {
222  return 0; // return ok
223  } else {
224  return -1; // return ko
225  }
226  }
227 
236  public function checkIfAccountancyCodeIsAlreadyUsed($db, $code, $type = '')
237  {
238  if ($type == 'supplier')
239  {
240  $typethirdparty = 'code_compta_fournisseur';
241  } elseif ($type == 'customer')
242  {
243  $typethirdparty = 'code_compta';
244  } else {
245  $this->error = 'Bad value for parameter type';
246  return -1;
247  }
248 
249  $sql = "SELECT ".$typethirdparty." FROM ".MAIN_DB_PREFIX."societe";
250  $sql .= " WHERE ".$typethirdparty." = '".$db->escape($code)."'";
251 
252  $resql = $db->query($sql);
253  if ($resql)
254  {
255  if ($db->num_rows($resql) == 0)
256  {
257  dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' available");
258  return 0; // Available
259  } else {
260  dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' not available");
261  return -1; // Not available
262  }
263  } else {
264  $this->error = $db->error()." sql=".$sql;
265  return -2; // Error
266  }
267  }
268 }
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
if(!empty($arrayfields['country.code_iso']['checked'])) print_liste_field_titre($arrayfields['country.code_iso']['label'] country if(!empty($arrayfields['typent.code']['checked'])) print_liste_field_titre($arrayfields['typent.code']['label'] typent code
Definition: list.php:566
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:108
Parent class for third parties accountancy code generators.
getExample($langs, $objsoc=0, $type=-1)
Return an example of result returned by getNextValue.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
get_code($db, $societe, $type= '')
Set accountancy account code for a third party into this-&gt;code.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
checkIfAccountancyCodeIsAlreadyUsed($db, $code, $type= '')
Check accountancy account code for a third party into this-&gt;code.
Class to manage accountancy code of thirdparties with Digitaria rules.
info($langs)
Return description of module.