dolibarr 21.0.0-alpha
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("GenericMaskCodes3");
104 $tooltip .= $langs->trans("GenericMaskCodes4b");
105 $tooltip .= $langs->trans("GenericMaskCodes5");
106
107 // Parametrage du prefix customers
108 $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("CustomerCodeModel").'):</td>';
109 $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).'</td>';
110
111 $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
112
113 $texte .= '</tr>';
114
115 // Parametrage du prefix suppliers
116 $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("SupplierCodeModel").'):</td>';
117 $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).'</td>';
118 $texte .= '</tr>';
119
120 // Date of switch to that numbering model
121 $datedb = getDolGlobalString('COMPANY_ELEPHANT_DATE_START');
122 // After save, default dolibarr store data like displayed : 20/05/2024 and we need a timestamp -> override data
123 if (!empty($datedb)) {
124 if (!is_numeric($datedb) && GETPOSTISSET('value3')) {
125 if (GETPOST('value4') == 1) {
126 $dateinput = GETPOSTDATE('value3');
127 $res = dolibarr_set_const($this->db, 'COMPANY_ELEPHANT_DATE_START', $dateinput, 'chaine', 0, '', $conf->entity);
128 } else {
129 $res = dolibarr_set_const($this->db, 'COMPANY_ELEPHANT_DATE_START', '', 'chaine', 0, '', $conf->entity);
130 }
131 } else {
132 $dateinput = $datedb;
133 }
134 }
135 if (empty($dateinput)) {
136 $dateinput = dol_now();
137 }
138 $texte .= '<tr><td>';
139 $texte .= $form->textwithpicto($langs->trans("DateStartThatModel"), $langs->trans("DateStartThatModelHelp")).'</td>';
140 $texte .= '<td class="nowraponall right">';
141 $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;"/>';
142 $texte .= '<div style="display: none;" id="elephantchoosedate">';
143 $texte .= $form->selectDate($dateinput, 'value3', 0, 0, 1, '', 1, 0, $disabled ? 1 : 0);
144 $texte .= '</div>';
145 $texte .= '</td>';
146 $texte .= '</tr>';
147
148 $texte .= '</table>';
149 $texte .= '</form>';
150
151 return $texte;
152 }
153
154
163 public function getExample($langs, $objsoc = '', $type = -1)
164 {
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 }
184 if (in_array($examplecust, $errmsg)) {
185 $langs->load("errors");
186 $examplecust = '<span class="'.$cssforerror.'">'.$langs->trans($examplecust).'</span>';
187 }
188 }
189 if ($type != 0) {
190 $examplesup = $this->getNextValue($objsoc, 1);
191 if (!$examplesup && ($cssforerror == 'error' || $this->error != 'NotConfigured')) {
192 $langs->load("errors");
193 $examplesup = '<span class="'.$cssforerror.'">'.$langs->trans('ErrorBadMask').'</span>';
194 }
195 if (in_array($examplesup, $errmsg)) {
196 $langs->load("errors");
197 $examplesup = '<span class="'.$cssforerror.'">'.$langs->trans($examplesup).'</span>';
198 }
199 }
200
201 if ($type == 0) {
202 return $examplecust;
203 } elseif ($type == 1) {
204 return $examplesup;
205 } else {
206 return $examplecust.'<br>'.$examplesup;
207 }
208 }
209
217 public function getNextValue($objsoc = '', $type = -1)
218 {
219 global $db;
220
221 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
222
223 // Get Mask value
224 $mask = '';
225 if ($type == 0) {
226 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER');
227 }
228 if ($type == 1) {
229 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER');
230 }
231 if (!$mask) {
232 $this->error = 'NotConfigured';
233 return '';
234 }
235
236 $field = '';
237 $where = '';
238 if ($type == 0) {
239 $field = 'code_client';
240 //$where = ' AND client in (1,2)';
241 } elseif ($type == 1) {
242 $field = 'code_fournisseur';
243 //$where = ' AND fournisseur = 1';
244 } else {
245 return -1;
246 }
247
248 $now = dol_now();
249
250 $numFinal = get_next_value($db, $mask, 'societe', $field, $where, '', $now);
251
252 return $numFinal;
253 }
254
255
256 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
262 public function verif_prefixIsUsed()
263 {
264 // phpcs:enable
265 global $conf;
266
267 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER');
268 if (preg_match('/\{pre\}/i', $mask)) {
269 return 1;
270 }
271
272 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER');
273 if (preg_match('/\{pre\}/i', $mask)) {
274 return 1;
275 }
276
277 return 0;
278 }
279
280
296 public function verif($db, &$code, $soc, $type)
297 {
298 global $conf;
299
300 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
301
302 $result = 0;
303 $code = strtoupper(trim($code));
304
305 if (getDolGlobalString('COMPANY_ELEPHANT_DATE_START_ENABLE') && $soc->date_creation < getDolGlobalString('COMPANY_ELEPHANT_DATE_START')) {
306 return -5;
307 }
308 if (empty($code) && $this->code_null && !getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED')) {
309 $result = 0;
310 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED'))) {
311 $result = -2;
312 } else {
313 // Get Mask value
314 $mask = '';
315 if ($type == 0) {
316 $mask = !getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER') ? '' : $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
317 }
318 if ($type == 1) {
319 $mask = !getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER') ? '' : $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
320 }
321 if (!$mask) {
322 $this->error = 'NotConfigured';
323 return -5;
324 }
325 $result = check_value($mask, $code);
326 if (is_string($result)) {
327 $this->error = $result;
328 return -6;
329 } else {
330 $is_dispo = $this->verif_dispo($db, $code, $soc, $type);
331 if ($is_dispo != 0) {
332 $result = -3; // Code $code duplicate
333 }
334 }
335 }
336
337 dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
338 return $result;
339 }
340
341
342 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
352 public function verif_dispo($db, $code, $soc, $type = 0)
353 {
354 // phpcs:enable
355 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
356 if ($type == 1) {
357 $sql .= " WHERE code_fournisseur = '".$db->escape($code)."'";
358 } else {
359 $sql .= " WHERE code_client = '".$db->escape($code)."'";
360 }
361 if ($soc->id > 0) {
362 $sql .= " AND rowid <> ".((int) $soc->id);
363 }
364 $sql .= " AND entity IN (".getEntity('societe').")";
365
366 $resql = $db->query($sql);
367 if ($resql) {
368 if ($db->num_rows($resql) == 0) {
369 return 0;
370 } else {
371 // Code $code duplicate
372 return -1;
373 }
374 } else {
375 return -2;
376 }
377 }
378}
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 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)
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 dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.