dolibarr 24.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-2025 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 global $conf;
320
321 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
322
323 $result = 0;
324 $code = strtoupper(trim($code));
325
326 if (getDolGlobalString('COMPANY_ELEPHANT_DATE_START_ENABLE') && $soc->date_creation < getDolGlobalString('COMPANY_ELEPHANT_DATE_START')) {
327 return -5;
328 }
329 if (empty($code) && $this->code_null && !getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED')) {
330 $result = 0;
331 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED'))) {
332 $result = -2;
333 } else {
334 // Get Mask value
335 $mask = '';
336 if ($type == 0) {
337 $mask = !getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER') ? '' : $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
338 }
339 if ($type == 1) {
340 $mask = !getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER') ? '' : $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
341 }
342 if (!$mask) {
343 $this->error = 'NotConfigured';
344 return -5;
345 }
346 $result = check_value($mask, $code);
347 if (is_string($result)) {
348 $this->error = $result;
349 return -6;
350 } else {
351 $is_dispo = $this->verif_dispo($db, $code, $soc, $type);
352 if ($is_dispo != 0) {
353 $result = -3; // Code $code duplicate
354 }
355 }
356 }
357
358 dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
359 return $result;
360 }
361
362
363 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
373 public function verif_dispo($db, $code, $soc, $type = 0)
374 {
375 // phpcs:enable
376 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
377 if ($type == 1) {
378 $sql .= " WHERE code_fournisseur = '".$db->escape($code)."'";
379 } else {
380 $sql .= " WHERE code_client = '".$db->escape($code)."'";
381 }
382 if ($soc->id > 0) {
383 $sql .= " AND rowid <> ".((int) $soc->id);
384 }
385 $sql .= " AND entity IN (".getEntity('societe').")";
386
387 $resql = $db->query($sql);
388 if ($resql) {
389 if ($db->num_rows($resql) == 0) {
390 return 0;
391 } else {
392 // Code $code duplicate
393 return -1;
394 }
395 } else {
396 return -2;
397 }
398 }
399}
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)
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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)
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.