dolibarr 21.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 * Copyright (C) 2024 Eric Seigne <eric.seigne@cap-rel.fr>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 * or see https://www.gnu.org/
24 */
25
32require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
33
34
39{
40 // variables inherited from ModeleThirdPartyCode class
41 public $name = 'Elephant';
42 public $version = 'dolibarr';
43
44 // variables not inherited
45
49 public $searchcode;
50
54 public $numbitcounter;
55
56
62 public function __construct($db)
63 {
64 $this->db = $db;
65
66 $this->code_null = 0;
67 $this->code_modifiable = 1;
68 $this->code_modifiable_invalide = 1;
69 $this->code_modifiable_null = 1;
70 $this->code_auto = 1;
71 $this->prefixIsRequired = 0;
72 }
73
74
81 public function info($langs)
82 {
83 global $conf, $mc;
84 global $form;
85
86 $langs->load("companies");
87
88 $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
89
90 $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
91 $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
92 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
93 $texte .= '<input type="hidden" name="page_y" value="">';
94 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
95 $texte .= '<input type="hidden" name="param1" value="COMPANY_ELEPHANT_MASK_CUSTOMER">';
96 $texte .= '<input type="hidden" name="param2" value="COMPANY_ELEPHANT_MASK_SUPPLIER">';
97 $texte .= '<input type="hidden" name="param3" value="COMPANY_ELEPHANT_DATE_START">';
98 $texte .= '<input type="hidden" name="param4" value="COMPANY_ELEPHANT_DATE_START_ENABLE">';
99 $texte .= '<table class="nobordernopadding" width="100%">';
100
101 $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("ThirdParty"), $langs->transnoentities("ThirdParty"));
102 //$tooltip.=$langs->trans("GenericMaskCodes2"); Not required for third party numbering
103 $tooltip .= $langs->trans("GenericMaskCodes2b").'<br>';
104 $tooltip .= '<br>';
105 $tooltip .= $langs->trans("GenericMaskCodes3");
106 $tooltip .= '<br>';
107 $tooltip .= $langs->trans("GenericMaskCodes4b");
108 $tooltip .= $langs->trans("GenericMaskCodes5");
109 //$tooltip .= '<br>'.$langs->trans("GenericMaskCodes5b");
110
111 // Parametrage du prefix customers
112 $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("CustomerCodeModel").'):</td>';
113 $texte .= '<td class="right nowraponall">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value1" value="'.getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER').'"'.$disabled.'>', $tooltip, 1, 1, '', 0, 3, 'tooltipelephantcutomer').'</td>';
114
115 $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
116
117 $texte .= '</tr>';
118
119 // Parametrage du prefix suppliers
120 $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("SupplierCodeModel").'):</td>';
121 $texte .= '<td class="right nowraponall">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value2" value="'.getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER').'"'.$disabled.'>', $tooltip, 1, 1, '', 0, 3, 'tooltipelephantsupplier').'</td>';
122 $texte .= '</tr>';
123
124 // Date of switch to that numbering model
125 $datedb = getDolGlobalString('COMPANY_ELEPHANT_DATE_START');
126 // After save, default dolibarr store data like displayed : 20/05/2024 and we need a timestamp -> override data
127 if (!empty($datedb)) {
128 if (!is_numeric($datedb) && GETPOSTISSET('value3')) {
129 if (GETPOST('value4') == 1) {
130 $dateinput = GETPOSTDATE('value3');
131 $res = dolibarr_set_const($this->db, 'COMPANY_ELEPHANT_DATE_START', $dateinput, 'chaine', 0, '', $conf->entity);
132 } else {
133 $res = dolibarr_set_const($this->db, 'COMPANY_ELEPHANT_DATE_START', '', 'chaine', 0, '', $conf->entity);
134 }
135 } else {
136 $dateinput = $datedb;
137 }
138 }
139 if (empty($dateinput)) {
140 $dateinput = dol_now();
141 }
142 $texte .= '<tr><td>';
143 $texte .= $form->textwithpicto($langs->trans("DateStartThatModel"), $langs->trans("DateStartThatModelHelp")).'</td>';
144 $texte .= '<td class="nowraponall right">';
145 $texte .= '<input type="checkbox" onclick="let d=document.getElementById(\'elephantchoosedate\'); if(this.checked){d.style.cssText = \'display: block;\'}else{{d.style.cssText = \'display: none;\'}}" name="value4" value="1" style="float: left;"/>';
146 $texte .= '<div style="display: none;" id="elephantchoosedate">';
147 $texte .= $form->selectDate($dateinput, 'value3', 0, 0, 1, '', 1, 0, $disabled ? 1 : 0);
148 $texte .= '</div>';
149 $texte .= '</td>';
150
151 $texte .= '</tr>';
152
153 $texte .= '</table>';
154 $texte .= '</form>';
155
156 return $texte;
157 }
158
159
168 public function getExample($langs = null, $objsoc = '', $type = -1)
169 {
170 if (!$langs instanceof Translate) {
171 $langs = $GLOBALS['langs'];
172 '@phan-var-force Translate $langs';
173 }
174 $examplecust = '';
175 $examplesup = '';
176 $errmsg = array(
177 "ErrorBadMask",
178 "ErrorCantUseRazIfNoYearInMask",
179 "ErrorCantUseRazInStartedYearIfNoYearMonthInMask",
180 "ErrorCounterMustHaveMoreThan3Digits",
181 "ErrorBadMaskBadRazMonth",
182 "ErrorCantUseRazWithYearOnOneDigit",
183 );
184
185 $cssforerror = (getDolGlobalString('SOCIETE_CODECLIENT_ADDON') == 'mod_codeclient_elephant' ? 'error' : 'opacitymedium');
186
187 if ($type != 1) {
188 $examplecust = $this->getNextValue($objsoc, 0);
189 if (!$examplecust && ($cssforerror == 'error' || $this->error != 'NotConfigured')) {
190 $langs->load("errors");
191 $examplecust = '<span class="'.$cssforerror.'">'.$langs->trans('ErrorBadMask').'</span>';
192 }
193 if (in_array($examplecust, $errmsg)) {
194 $langs->load("errors");
195 $examplecust = '<span class="'.$cssforerror.'">'.$langs->trans($examplecust).'</span>';
196 }
197 }
198 if ($type != 0) {
199 $examplesup = $this->getNextValue($objsoc, 1);
200 if (!$examplesup && ($cssforerror == 'error' || $this->error != 'NotConfigured')) {
201 $langs->load("errors");
202 $examplesup = '<span class="'.$cssforerror.'">'.$langs->trans('ErrorBadMask').'</span>';
203 }
204 if (in_array($examplesup, $errmsg)) {
205 $langs->load("errors");
206 $examplesup = '<span class="'.$cssforerror.'">'.$langs->trans($examplesup).'</span>';
207 }
208 }
209
210 if ($type == 0) {
211 return $examplecust;
212 } elseif ($type == 1) {
213 return $examplesup;
214 } else {
215 return $examplecust.'<br>'.$examplesup;
216 }
217 }
218
226 public function getNextValue($objsoc = '', $type = -1)
227 {
228 global $db;
229
230 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
231
232 // Get Mask value
233 $mask = '';
234 if ($type == 0) {
235 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER');
236 }
237 if ($type == 1) {
238 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER');
239 }
240 if (!$mask) {
241 $this->error = 'NotConfigured';
242 return '';
243 }
244
245 $field = '';
246 $where = '';
247 if ($type == 0) {
248 $field = 'code_client';
249 //$where = ' AND client in (1,2)';
250 } elseif ($type == 1) {
251 $field = 'code_fournisseur';
252 //$where = ' AND fournisseur = 1';
253 } else {
254 return -1;
255 }
256
257 $now = dol_now();
258
259 $numFinal = get_next_value($db, $mask, 'societe', $field, $where, '', $now);
260
261 return $numFinal;
262 }
263
264
265 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
271 public function verif_prefixIsUsed()
272 {
273 // phpcs:enable
274 global $conf;
275
276 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER');
277 if (preg_match('/\{pre\}/i', $mask)) {
278 return 1;
279 }
280
281 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER');
282 if (preg_match('/\{pre\}/i', $mask)) {
283 return 1;
284 }
285
286 return 0;
287 }
288
289
305 public function verif($db, &$code, $soc, $type)
306 {
307 global $conf;
308
309 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
310
311 $result = 0;
312 $code = strtoupper(trim($code));
313
314 if (getDolGlobalString('COMPANY_ELEPHANT_DATE_START_ENABLE') && $soc->date_creation < getDolGlobalString('COMPANY_ELEPHANT_DATE_START')) {
315 return -5;
316 }
317 if (empty($code) && $this->code_null && !getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED')) {
318 $result = 0;
319 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED'))) {
320 $result = -2;
321 } else {
322 // Get Mask value
323 $mask = '';
324 if ($type == 0) {
325 $mask = !getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER') ? '' : $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
326 }
327 if ($type == 1) {
328 $mask = !getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER') ? '' : $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
329 }
330 if (!$mask) {
331 $this->error = 'NotConfigured';
332 return -5;
333 }
334 $result = check_value($mask, $code);
335 if (is_string($result)) {
336 $this->error = $result;
337 return -6;
338 } else {
339 $is_dispo = $this->verif_dispo($db, $code, $soc, $type);
340 if ($is_dispo != 0) {
341 $result = -3; // Code $code duplicate
342 }
343 }
344 }
345
346 dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
347 return $result;
348 }
349
350
351 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
361 public function verif_dispo($db, $code, $soc, $type = 0)
362 {
363 // phpcs:enable
364 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
365 if ($type == 1) {
366 $sql .= " WHERE code_fournisseur = '".$db->escape($code)."'";
367 } else {
368 $sql .= " WHERE code_client = '".$db->escape($code)."'";
369 }
370 if ($soc->id > 0) {
371 $sql .= " AND rowid <> ".((int) $soc->id);
372 }
373 $sql .= " AND entity IN (".getEntity('societe').")";
374
375 $resql = $db->query($sql);
376 if ($resql) {
377 if ($db->num_rows($resql) == 0) {
378 return 0;
379 } else {
380 // Code $code duplicate
381 return -1;
382 }
383 } else {
384 return -2;
385 }
386 }
387}
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
Parent class for third parties code generators.
Class to manage translations.
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=null, $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)
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)
GETPOSTDATE($prefix, $hourTime='', $gm='auto')
Helper function that combines values of a dolibarr DatePicker (such as Form\selectDate) for year,...
dol_now($mode='auto')
Return date for now.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79