dolibarr  20.0.0-beta
mod_codeclient_elephant.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2006-2009 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2007-2012 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
6  * Copyright (C) 2013-2018 Philippe Grand <philippe.grand@atoo-net.com>
7  * Copyright (C) 2020-2024 Frédéric France <frederic.france@free.fr>
8  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <https://www.gnu.org/licenses/>.
22  * or see https://www.gnu.org/
23  */
24 
31 require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
32 
33 
38 {
39  // variables inherited from ModeleThirdPartyCode class
40  public $name = 'Elephant';
41  public $version = 'dolibarr';
42 
43  // variables not inherited
44 
48  public $searchcode;
49 
53  public $numbitcounter;
54 
55 
61  public function __construct($db)
62  {
63  $this->db = $db;
64 
65  $this->code_null = 0;
66  $this->code_modifiable = 1;
67  $this->code_modifiable_invalide = 1;
68  $this->code_modifiable_null = 1;
69  $this->code_auto = 1;
70  $this->prefixIsRequired = 0;
71  }
72 
73 
80  public function info($langs)
81  {
82  global $conf, $mc;
83  global $form;
84 
85  $langs->load("companies");
86 
87  $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
88 
89  $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
90  $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
91  $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
92  $texte .= '<input type="hidden" name="page_y" value="">';
93  $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
94  $texte .= '<input type="hidden" name="param1" value="COMPANY_ELEPHANT_MASK_CUSTOMER">';
95  $texte .= '<input type="hidden" name="param2" value="COMPANY_ELEPHANT_MASK_SUPPLIER">';
96  $texte .= '<table class="nobordernopadding" width="100%">';
97 
98  $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("ThirdParty"), $langs->transnoentities("ThirdParty"));
99  //$tooltip.=$langs->trans("GenericMaskCodes2"); Not required for third party numbering
100  $tooltip .= $langs->trans("GenericMaskCodes3");
101  $tooltip .= $langs->trans("GenericMaskCodes4b");
102  $tooltip .= $langs->trans("GenericMaskCodes5");
103 
104  // Parametrage du prefix customers
105  $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("CustomerCodeModel").'):</td>';
106  $texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value1" value="'.getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER').'"'.$disabled.'>', $tooltip, 1, 1).'</td>';
107 
108  $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
109 
110  $texte .= '</tr>';
111 
112  // Parametrage du prefix suppliers
113  $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("SupplierCodeModel").'):</td>';
114  $texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value2" value="'.getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER').'"'.$disabled.'>', $tooltip, 1, 1).'</td>';
115  $texte .= '</tr>';
116 
117  $texte .= '</table>';
118  $texte .= '</form>';
119 
120  return $texte;
121  }
122 
123 
132  public function getExample($langs, $objsoc = '', $type = -1)
133  {
134  $examplecust = '';
135  $examplesup = '';
136  $errmsg = array(
137  "ErrorBadMask",
138  "ErrorCantUseRazIfNoYearInMask",
139  "ErrorCantUseRazInStartedYearIfNoYearMonthInMask",
140  "ErrorCounterMustHaveMoreThan3Digits",
141  "ErrorBadMaskBadRazMonth",
142  "ErrorCantUseRazWithYearOnOneDigit",
143  );
144 
145  $cssforerror = (getDolGlobalString('SOCIETE_CODECLIENT_ADDON') == 'mod_codeclient_elephant' ? 'error' : 'opacitymedium');
146 
147  if ($type != 1) {
148  $examplecust = $this->getNextValue($objsoc, 0);
149  if (!$examplecust && ($cssforerror == 'error' || $this->error != 'NotConfigured')) {
150  $langs->load("errors");
151  $examplecust = '<span class="'.$cssforerror.'">'.$langs->trans('ErrorBadMask').'</span>';
152  }
153  if (in_array($examplecust, $errmsg)) {
154  $langs->load("errors");
155  $examplecust = '<span class="'.$cssforerror.'">'.$langs->trans($examplecust).'</span>';
156  }
157  }
158  if ($type != 0) {
159  $examplesup = $this->getNextValue($objsoc, 1);
160  if (!$examplesup && ($cssforerror == 'error' || $this->error != 'NotConfigured')) {
161  $langs->load("errors");
162  $examplesup = '<span class="'.$cssforerror.'">'.$langs->trans('ErrorBadMask').'</span>';
163  }
164  if (in_array($examplesup, $errmsg)) {
165  $langs->load("errors");
166  $examplesup = '<span class="'.$cssforerror.'">'.$langs->trans($examplesup).'</span>';
167  }
168  }
169 
170  if ($type == 0) {
171  return $examplecust;
172  } elseif ($type == 1) {
173  return $examplesup;
174  } else {
175  return $examplecust.'<br>'.$examplesup;
176  }
177  }
178 
186  public function getNextValue($objsoc = '', $type = -1)
187  {
188  global $db;
189 
190  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
191 
192  // Get Mask value
193  $mask = '';
194  if ($type == 0) {
195  $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER');
196  }
197  if ($type == 1) {
198  $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER');
199  }
200  if (!$mask) {
201  $this->error = 'NotConfigured';
202  return '';
203  }
204 
205  $field = '';
206  $where = '';
207  if ($type == 0) {
208  $field = 'code_client';
209  //$where = ' AND client in (1,2)';
210  } elseif ($type == 1) {
211  $field = 'code_fournisseur';
212  //$where = ' AND fournisseur = 1';
213  } else {
214  return -1;
215  }
216 
217  $now = dol_now();
218 
219  $numFinal = get_next_value($db, $mask, 'societe', $field, $where, '', $now);
220 
221  return $numFinal;
222  }
223 
224 
225  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
231  public function verif_prefixIsUsed()
232  {
233  // phpcs:enable
234  global $conf;
235 
236  $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER');
237  if (preg_match('/\{pre\}/i', $mask)) {
238  return 1;
239  }
240 
241  $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER');
242  if (preg_match('/\{pre\}/i', $mask)) {
243  return 1;
244  }
245 
246  return 0;
247  }
248 
249 
265  public function verif($db, &$code, $soc, $type)
266  {
267  global $conf;
268 
269  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
270 
271  $result = 0;
272  $code = strtoupper(trim($code));
273 
274  if (empty($code) && $this->code_null && !getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED')) {
275  $result = 0;
276  } elseif (empty($code) && (!$this->code_null || getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED'))) {
277  $result = -2;
278  } else {
279  // Get Mask value
280  $mask = '';
281  if ($type == 0) {
282  $mask = !getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER') ? '' : $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
283  }
284  if ($type == 1) {
285  $mask = !getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER') ? '' : $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
286  }
287  if (!$mask) {
288  $this->error = 'NotConfigured';
289  return -5;
290  }
291  $result = check_value($mask, $code);
292  if (is_string($result)) {
293  $this->error = $result;
294  return -6;
295  } else {
296  $is_dispo = $this->verif_dispo($db, $code, $soc, $type);
297  if ($is_dispo != 0) {
298  $result = -3;
299  }
300  }
301  }
302 
303  dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
304  return $result;
305  }
306 
307 
308  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
318  public function verif_dispo($db, $code, $soc, $type = 0)
319  {
320  // phpcs:enable
321  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
322  if ($type == 1) {
323  $sql .= " WHERE code_fournisseur = '".$db->escape($code)."'";
324  } else {
325  $sql .= " WHERE code_client = '".$db->escape($code)."'";
326  }
327  if ($soc->id > 0) {
328  $sql .= " AND rowid <> ".$soc->id;
329  }
330  $sql .= " AND entity IN (".getEntity('societe').")";
331 
332  $resql = $db->query($sql);
333  if ($resql) {
334  if ($db->num_rows($resql) == 0) {
335  return 0;
336  } else {
337  return -1;
338  }
339  } else {
340  return -2;
341  }
342  }
343 }
Parent class for third parties code generators.
Class to manage third party code with elephant rule.
verif_prefixIsUsed()
Check if mask/numbering use prefix.
verif($db, &$code, $soc, $type)
Check validity of code according to its rules.
getExample($langs, $objsoc='', $type=-1)
Return an example of result returned by getNextValue.
getNextValue($objsoc='', $type=-1)
Return next value.
info($langs)
Return description of module.
verif_dispo($db, $code, $soc, $type=0)
Indicate if the code is available or not (by another third party)
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
check_value($mask, $value)
Check value.
get_next_value($db, $mask, $table, $field, $where='', $objsoc='', $date='', $mode='next', $bentityon=true, $objuser=null, $forceentity=null)
Return last or next value for a mask (according to area we should not reset)
dol_now($mode='auto')
Return date for now.
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.