dolibarr 18.0.6
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 Frédéric France <frederic.france@netlogic.fr>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * or see https://www.gnu.org/
22 */
23
30require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
31
32
37{
41 public $name = 'Elephant';
42
46 public $code_modifiable;
47
51 public $code_modifiable_invalide;
52
56 public $code_modifiable_null;
57
61 public $code_null;
62
67 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
68
72 public $code_auto;
73
77 public $searchcode;
78
82 public $numbitcounter;
83
87 public $prefixIsRequired;
88
89
93 public function __construct()
94 {
95 $this->code_null = 0;
96 $this->code_modifiable = 1;
97 $this->code_modifiable_invalide = 1;
98 $this->code_modifiable_null = 1;
99 $this->code_auto = 1;
100 $this->prefixIsRequired = 0;
101 }
102
103
110 public function info($langs)
111 {
112 global $conf, $mc;
113 global $form;
114
115 $langs->load("companies");
116
117 $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
118
119 $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
120 $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
121 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
122 $texte .= '<input type="hidden" name="page_y" value="">';
123 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
124 $texte .= '<input type="hidden" name="param1" value="COMPANY_ELEPHANT_MASK_CUSTOMER">';
125 $texte .= '<input type="hidden" name="param2" value="COMPANY_ELEPHANT_MASK_SUPPLIER">';
126 $texte .= '<table class="nobordernopadding" width="100%">';
127
128 $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("ThirdParty"), $langs->transnoentities("ThirdParty"));
129 //$tooltip.=$langs->trans("GenericMaskCodes2"); Not required for third party numbering
130 $tooltip .= $langs->trans("GenericMaskCodes3");
131 $tooltip .= $langs->trans("GenericMaskCodes4b");
132 $tooltip .= $langs->trans("GenericMaskCodes5");
133
134 // Parametrage du prefix customers
135 $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("CustomerCodeModel").'):</td>';
136 $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>';
137
138 $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
139
140 $texte .= '</tr>';
141
142 // Parametrage du prefix suppliers
143 $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("SupplierCodeModel").'):</td>';
144 $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>';
145 $texte .= '</tr>';
146
147 $texte .= '</table>';
148 $texte .= '</form>';
149
150 return $texte;
151 }
152
153
162 public function getExample($langs, $objsoc = 0, $type = -1)
163 {
164 $error = 0;
165 $examplecust = '';
166 $examplesup = '';
167 $errmsg = array(
168 "ErrorBadMask",
169 "ErrorCantUseRazIfNoYearInMask",
170 "ErrorCantUseRazInStartedYearIfNoYearMonthInMask",
171 "ErrorCounterMustHaveMoreThan3Digits",
172 "ErrorBadMaskBadRazMonth",
173 "ErrorCantUseRazWithYearOnOneDigit",
174 );
175
176 $cssforerror = (getDolGlobalString('SOCIETE_CODECLIENT_ADDON') == 'mod_codeclient_elephant' ? 'error' : 'opacitymedium');
177
178 if ($type != 1) {
179 $examplecust = $this->getNextValue($objsoc, 0);
180 if (!$examplecust && ($cssforerror == 'error' || $this->error != 'NotConfigured')) {
181 $langs->load("errors");
182 $examplecust = '<span class="'.$cssforerror.'">'.$langs->trans('ErrorBadMask').'</span>';
183 $error = 1;
184 }
185 if (in_array($examplecust, $errmsg)) {
186 $langs->load("errors");
187 $examplecust = '<span class="'.$cssforerror.'">'.$langs->trans($examplecust).'</span>';
188 $error = 1;
189 }
190 }
191 if ($type != 0) {
192 $examplesup = $this->getNextValue($objsoc, 1);
193 if (!$examplesup && ($cssforerror == 'error' || $this->error != 'NotConfigured')) {
194 $langs->load("errors");
195 $examplesup = '<span class="'.$cssforerror.'">'.$langs->trans('ErrorBadMask').'</span>';
196 $error = 1;
197 }
198 if (in_array($examplesup, $errmsg)) {
199 $langs->load("errors");
200 $examplesup = '<span class="'.$cssforerror.'">'.$langs->trans($examplesup).'</span>';
201 $error = 1;
202 }
203 }
204
205 if ($type == 0) {
206 return $examplecust;
207 } elseif ($type == 1) {
208 return $examplesup;
209 } else {
210 return $examplecust.'<br>'.$examplesup;
211 }
212 }
213
221 public function getNextValue($objsoc = 0, $type = -1)
222 {
223 global $db, $conf;
224
225 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
226
227 // Get Mask value
228 $mask = '';
229 if ($type == 0) {
230 $mask = empty($conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER) ? '' : $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
231 }
232 if ($type == 1) {
233 $mask = empty($conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER) ? '' : $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
234 }
235 if (!$mask) {
236 $this->error = 'NotConfigured';
237 return '';
238 }
239
240 $field = '';
241 $where = '';
242 if ($type == 0) {
243 $field = 'code_client';
244 //$where = ' AND client in (1,2)';
245 } elseif ($type == 1) {
246 $field = 'code_fournisseur';
247 //$where = ' AND fournisseur = 1';
248 } else {
249 return -1;
250 }
251
252 $now = dol_now();
253
254 $numFinal = get_next_value($db, $mask, 'societe', $field, $where, '', $now);
255
256 return $numFinal;
257 }
258
259
260 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
266 public function verif_prefixIsUsed()
267 {
268 // phpcs:enable
269 global $conf;
270
271 $mask = $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
272 if (preg_match('/\{pre\}/i', $mask)) {
273 return 1;
274 }
275
276 $mask = $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
277 if (preg_match('/\{pre\}/i', $mask)) {
278 return 1;
279 }
280
281 return 0;
282 }
283
284
300 public function verif($db, &$code, $soc, $type)
301 {
302 global $conf;
303
304 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
305
306 $result = 0;
307 $code = strtoupper(trim($code));
308
309 if (empty($code) && $this->code_null && empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED)) {
310 $result = 0;
311 } elseif (empty($code) && (!$this->code_null || !empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED))) {
312 $result = -2;
313 } else {
314 // Get Mask value
315 $mask = '';
316 if ($type == 0) {
317 $mask = empty($conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER) ? '' : $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
318 }
319 if ($type == 1) {
320 $mask = empty($conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER) ? '' : $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
321 }
322 if (!$mask) {
323 $this->error = 'NotConfigured';
324 return -5;
325 }
326 $result = check_value($mask, $code);
327 if (is_string($result)) {
328 $this->error = $result;
329 return -6;
330 } else {
331 $is_dispo = $this->verif_dispo($db, $code, $soc, $type);
332 if ($is_dispo <> 0) {
333 $result = -3;
334 }
335 }
336 }
337
338 dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
339 return $result;
340 }
341
342
343 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
353 public function verif_dispo($db, $code, $soc, $type = 0)
354 {
355 // phpcs:enable
356 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
357 if ($type == 1) {
358 $sql .= " WHERE code_fournisseur = '".$db->escape($code)."'";
359 } else {
360 $sql .= " WHERE code_client = '".$db->escape($code)."'";
361 }
362 if ($soc->id > 0) {
363 $sql .= " AND rowid <> ".$soc->id;
364 }
365 $sql .= " AND entity IN (".getEntity('societe').")";
366
367 $resql = $db->query($sql);
368 if ($resql) {
369 if ($db->num_rows($resql) == 0) {
370 return 0;
371 } else {
372 return -1;
373 }
374 } else {
375 return -2;
376 }
377 }
378}
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.
getNextValue($objsoc=0, $type=-1)
Return next value.
info($langs)
Return description of module.
getExample($langs, $objsoc=0, $type=-1)
Return an example of result returned by getNextValue.
verif_dispo($db, $code, $soc, $type=0)
Renvoi si un code est pris ou non (par autre tiers)
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.