dolibarr 25.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-2026 Frédéric France <frederic.france@free.fr>
8 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
9 * Copyright (C) 2024 Eric Seigne <eric.seigne@cap-rel.fr>
10 * Copyright (C) 2025 Charlene Benke <charlene@patas-monkey.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 * or see https://www.gnu.org/
25 */
26
33require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
34
35
40{
41 // variables inherited from ModeleThirdPartyCode class
42 public $name = 'Elephant';
43 public $version = 'dolibarr';
44
45 // variables not inherited
46
50 public $searchcode;
51
55 public $numbitcounter;
56
57
63 public function __construct($db)
64 {
65 $this->db = $db;
66
67 $this->code_null = 0;
68 $this->code_modifiable = 1;
69 $this->code_modifiable_invalide = 1;
70 $this->code_modifiable_null = 1;
71 $this->code_auto = 1;
72 $this->prefixIsRequired = 0;
73 }
74
75
82 public function info($langs)
83 {
84 global $conf, $mc;
85 global $form;
86
87 $langs->load("companies");
88
89 $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
90
91 $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
92 $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
93 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
94 $texte .= '<input type="hidden" name="page_y" value="">';
95 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
96 $texte .= '<input type="hidden" name="param1" value="COMPANY_ELEPHANT_MASK_CUSTOMER">';
97 $texte .= '<input type="hidden" name="param2" value="COMPANY_ELEPHANT_MASK_SUPPLIER">';
98 $texte .= '<input type="hidden" name="param3" value="COMPANY_ELEPHANT_DATE_START">';
99 $texte .= '<input type="hidden" name="param4" value="COMPANY_ELEPHANT_DATE_START_ENABLE">';
100 $texte .= '<table class="nobordernopadding" width="100%">';
101
102 $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("ThirdParty"), $langs->transnoentities("ThirdParty"));
103 $tooltip .= $langs->trans("GenericMaskCodes1");
104 //$tooltip.=$langs->trans("GenericMaskCodes2"); Not required for third party numbering
105 $tooltip .= $langs->trans("GenericMaskCodes2b").'<br>';
106 $tooltip .= '<br>';
107 $tooltip .= $langs->trans("GenericMaskCodes3");
108 $tooltip .= '<br>';
109 $tooltip .= $langs->trans("GenericMaskCodes4b");
110 $tooltip .= $langs->trans("GenericMaskCodes5");
111 //$tooltip .= '<br>'.$langs->trans("GenericMaskCodes5b");
112
113 // Setting of prefix customers
114 $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("CustomerCodeModel").'):</td>';
115 $texte .= '<td class="nowraponall">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value1" value="'.getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER').'"'.$disabled.'>', $tooltip, 1, 'help', '', 0, 3, 'tooltipelephantcutomer').'</td>';
116
117 $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
118
119 $texte .= '</tr>';
120
121 // Setting of prefix suppliers
122 $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("SupplierCodeModel").'):</td>';
123 $texte .= '<td class="nowraponall">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value2" value="'.getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER').'"'.$disabled.'>', $tooltip, 1, 'help', '', 0, 3, 'tooltipelephantsupplier').'</td>';
124 $texte .= '</tr>';
125
126 // Date of switch to that numbering model
127 $datedb = getDolGlobalString('COMPANY_ELEPHANT_DATE_START');
128 // After save, default dolibarr store data like displayed : 20/05/2024 and we need a timestamp -> override data
129 if (!empty($datedb)) {
130 if (!is_numeric($datedb) && GETPOSTISSET('value3')) {
131 if (GETPOST('value4') == 1) {
132 $dateinput = GETPOSTDATE('value3');
133 $res = dolibarr_set_const($this->db, 'COMPANY_ELEPHANT_DATE_START', $dateinput, 'chaine', 0, '', $conf->entity);
134 $res = dolibarr_set_const($this->db, 'COMPANY_ELEPHANT_DATE_START_ENABLE', 1, 'chaine', 0, '', $conf->entity);
135 } else {
136 $res = dolibarr_set_const($this->db, 'COMPANY_ELEPHANT_DATE_START', '', 'chaine', 0, '', $conf->entity);
137 $res = dolibarr_set_const($this->db, 'COMPANY_ELEPHANT_DATE_START_ENABLE', 1, 'chaine', 0, '', $conf->entity);
138 }
139 } else {
140 $dateinput = $datedb;
141 }
142 }
143 if (empty($dateinput)) {
144 $dateinput = dol_now();
145 }
146 $isEnabled = getDolGlobalString('COMPANY_ELEPHANT_DATE_START_ENABLE');
147 $texte .= '<tr><td>';
148 $texte .= '<input type="checkbox"';
149 if ($isEnabled) {
150 $texte .= ' checked="checked"';
151 }
152 $texte .= ' onclick="if (this.checked) { jQuery(\'#elephantchoosedate\').show(); } else { jQuery(\'#elephantchoosedate\').hide(); }" id="elephantdisablebefore" name="value4" value="1" class="inline-block"/>';
153 $texte .= '<label for="elephantdisablebefore" class="small">';
154 $texte .= $form->textwithpicto($langs->trans("DateStartThatModel"), $langs->trans("DateStartThatModelHelp"));
155 $texte .= '</label>';
156 $texte .= '</td>';
157 $texte .= '<td class="nowraponall">';
158 $texte .= '<div class="'.($isEnabled ? '' : 'hideobject ').' inline-block" id="elephantchoosedate">';
159 $texte .= $form->selectDate($dateinput, 'value3', 0, 0, 1, '', 1, 0, $disabled ? 1 : 0);
160 $texte .= '</div>';
161 $texte .= '</td>';
162
163 $texte .= '</tr>';
164
165 $texte .= '</table>';
166 $texte .= '</form>';
167
168 return $texte;
169 }
170
171
180 public function getExample($langs = null, $objsoc = '', $type = -1)
181 {
182 if (!$langs instanceof Translate) {
183 $langs = $GLOBALS['langs'];
184 '@phan-var-force Translate $langs';
185 }
186 $examplecust = '';
187 $examplesup = '';
188 $errmsg = array(
189 "ErrorBadMask",
190 "ErrorCantUseRazIfNoYearInMask",
191 "ErrorCantUseRazInStartedYearIfNoYearMonthInMask",
192 "ErrorCounterMustHaveMoreThan3Digits",
193 "ErrorBadMaskBadRazMonth",
194 "ErrorCantUseRazWithYearOnOneDigit",
195 );
196
197 $cssforerror = (getDolGlobalString('SOCIETE_CODECLIENT_ADDON') == 'mod_codeclient_elephant' ? 'error' : 'opacitymedium');
198
199 if ($type != 1) {
200 $examplecust = $this->getNextValue($objsoc, 0);
201 if (!$examplecust && ($cssforerror == 'error' || $this->error != 'NotConfigured')) {
202 $langs->load("errors");
203 $examplecust = '<span class="'.$cssforerror.'">'.$langs->trans('ErrorBadMask').'</span>';
204 }
205 if (in_array($examplecust, $errmsg)) {
206 $langs->load("errors");
207 $examplecust = '<span class="'.$cssforerror.'">'.$langs->trans($examplecust).'</span>';
208 }
209 }
210 if ($type != 0) {
211 $examplesup = $this->getNextValue($objsoc, 1);
212 if (!$examplesup && ($cssforerror == 'error' || $this->error != 'NotConfigured')) {
213 $langs->load("errors");
214 $examplesup = '<span class="'.$cssforerror.'">'.$langs->trans('ErrorBadMask').'</span>';
215 }
216 if (in_array($examplesup, $errmsg)) {
217 $langs->load("errors");
218 $examplesup = '<span class="'.$cssforerror.'">'.$langs->trans($examplesup).'</span>';
219 }
220 }
221
222 if ($type == 0) {
223 return $examplecust;
224 } elseif ($type == 1) {
225 return $examplesup;
226 } else {
227 return $examplecust.'<br>'.$examplesup;
228 }
229 }
230
238 public function getNextValue($objsoc = '', $type = -1)
239 {
240 global $db;
241
242 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
243
244 // Get Mask value
245 $mask = '';
246 if ($type == 0) {
247 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER');
248 }
249 if ($type == 1) {
250 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER');
251 }
252 if (!$mask) {
253 $this->error = 'NotConfigured';
254 return '';
255 }
256
257 $field = '';
258 $where = '';
259 if ($type == 0) {
260 $field = 'code_client';
261 //$where = ' AND client in (1,2)';
262 } elseif ($type == 1) {
263 $field = 'code_fournisseur';
264 //$where = ' AND fournisseur = 1';
265 } else {
266 return -1;
267 }
268
269 $now = dol_now();
270
271 $numFinal = get_next_value($db, $mask, 'societe', $field, $where, '', $now);
272
273 return $numFinal;
274 }
275
276
277 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
283 public function verif_prefixIsUsed()
284 {
285 // phpcs:enable
286 global $conf;
287
288 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER');
289 if (preg_match('/\{pre\}/i', $mask)) {
290 return 1;
291 }
292
293 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER');
294 if (preg_match('/\{pre\}/i', $mask)) {
295 return 1;
296 }
297
298 return 0;
299 }
300
301
317 public function verif($db, &$code, $soc, $type)
318 {
319 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
320
321 $result = 0;
322 $code = strtoupper(trim($code));
323
324 if (getDolGlobalString('COMPANY_ELEPHANT_DATE_START_ENABLE') && $soc->date_creation < getDolGlobalString('COMPANY_ELEPHANT_DATE_START')) {
325 return -5;
326 }
327 if (empty($code) && $this->code_null && !getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED')) {
328 $result = 0;
329 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED'))) {
330 $result = -2;
331 } else {
332 // Get Mask value
333 $mask = '';
334 if ($type == 0) {
335 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER');
336 }
337 if ($type == 1) {
338 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER');
339 }
340 if (!$mask) {
341 $this->error = 'NotConfigured';
342 return -5;
343 }
344 $result = check_value($mask, $code);
345 if (is_string($result)) {
346 $this->error = $result;
347 return -6;
348 } else {
349 $is_dispo = $this->verif_dispo($db, $code, $soc, $type);
350 if ($is_dispo != 0) {
351 $result = -3; // Code $code duplicate
352 }
353 }
354 }
355
356 dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
357 return $result;
358 }
359
360
361 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
371 public function verif_dispo($db, $code, $soc, $type = 0)
372 {
373 // phpcs:enable
374 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
375 if ($type == 1) {
376 $sql .= " WHERE code_fournisseur = '".$db->escape($code)."'";
377 } else {
378 $sql .= " WHERE code_client = '".$db->escape($code)."'";
379 }
380 if ($soc->id > 0) {
381 $sql .= " AND rowid <> ".((int) $soc->id);
382 }
383 $sql .= " AND entity IN (".getEntity('societe').")";
384
385 $resql = $db->query($sql);
386 if ($resql) {
387 if ($db->num_rows($resql) == 0) {
388 return 0;
389 } else {
390 // Code $code duplicate
391 return -1;
392 }
393 } else {
394 return -2;
395 }
396 }
397}
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).
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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, $objbookkeeping=null)
Return last or next value for a mask (according to area we should not reset)
dol_now($mode='gmt')
Return date for now.
GETPOSTDATE($prefix, $hourTime='', $gm='auto', $saverestore='')
Helper function that combines values of a dolibarr DatePicker (such as Form\selectDate) for year,...
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
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.