dolibarr 20.0.0
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 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 * or see https://www.gnu.org/
23 */
24
31require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
32
33
38{
39 // variables inherited from ModeleThirdPartyCode class
40 public $name = 'Elephant';
41 public $version = 'dolibarr';
42
43 // variables not inherited
44
48 public $searchcode;
49
53 public $numbitcounter;
54
55
61 public function __construct($db)
62 {
63 $this->db = $db;
64
65 $this->code_null = 0;
66 $this->code_modifiable = 1;
67 $this->code_modifiable_invalide = 1;
68 $this->code_modifiable_null = 1;
69 $this->code_auto = 1;
70 $this->prefixIsRequired = 0;
71 }
72
73
80 public function info($langs)
81 {
82 global $conf, $mc;
83 global $form;
84
85 $langs->load("companies");
86
87 $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
88
89 $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
90 $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
91 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
92 $texte .= '<input type="hidden" name="page_y" value="">';
93 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
94 $texte .= '<input type="hidden" name="param1" value="COMPANY_ELEPHANT_MASK_CUSTOMER">';
95 $texte .= '<input type="hidden" name="param2" value="COMPANY_ELEPHANT_MASK_SUPPLIER">';
96 $texte .= '<table class="nobordernopadding" width="100%">';
97
98 $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("ThirdParty"), $langs->transnoentities("ThirdParty"));
99 //$tooltip.=$langs->trans("GenericMaskCodes2"); Not required for third party numbering
100 $tooltip .= $langs->trans("GenericMaskCodes2b").'<br>';
101 $tooltip .= '<br>';
102 $tooltip .= $langs->trans("GenericMaskCodes3");
103 $tooltip .= '<br>';
104 $tooltip .= $langs->trans("GenericMaskCodes4b");
105 $tooltip .= $langs->trans("GenericMaskCodes5");
106 //$tooltip .= '<br>'.$langs->trans("GenericMaskCodes5b");
107
108 // Parametrage du prefix customers
109 $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("CustomerCodeModel").'):</td>';
110 $texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value1" value="'.getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER').'"'.$disabled.'>', $tooltip, 1, 1, '', 0, 3, 'tooltipelephantcutomer').'</td>';
111
112 $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
113
114 $texte .= '</tr>';
115
116 // Parametrage du prefix suppliers
117 $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("SupplierCodeModel").'):</td>';
118 $texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value2" value="'.getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER').'"'.$disabled.'>', $tooltip, 1, 1, '', 0, 3, 'tooltipelephantsupplier').'</td>';
119 $texte .= '</tr>';
120
121 $texte .= '</table>';
122 $texte .= '</form>';
123
124 return $texte;
125 }
126
127
136 public function getExample($langs, $objsoc = '', $type = -1)
137 {
138 $examplecust = '';
139 $examplesup = '';
140 $errmsg = array(
141 "ErrorBadMask",
142 "ErrorCantUseRazIfNoYearInMask",
143 "ErrorCantUseRazInStartedYearIfNoYearMonthInMask",
144 "ErrorCounterMustHaveMoreThan3Digits",
145 "ErrorBadMaskBadRazMonth",
146 "ErrorCantUseRazWithYearOnOneDigit",
147 );
148
149 $cssforerror = (getDolGlobalString('SOCIETE_CODECLIENT_ADDON') == 'mod_codeclient_elephant' ? 'error' : 'opacitymedium');
150
151 if ($type != 1) {
152 $examplecust = $this->getNextValue($objsoc, 0);
153 if (!$examplecust && ($cssforerror == 'error' || $this->error != 'NotConfigured')) {
154 $langs->load("errors");
155 $examplecust = '<span class="'.$cssforerror.'">'.$langs->trans('ErrorBadMask').'</span>';
156 }
157 if (in_array($examplecust, $errmsg)) {
158 $langs->load("errors");
159 $examplecust = '<span class="'.$cssforerror.'">'.$langs->trans($examplecust).'</span>';
160 }
161 }
162 if ($type != 0) {
163 $examplesup = $this->getNextValue($objsoc, 1);
164 if (!$examplesup && ($cssforerror == 'error' || $this->error != 'NotConfigured')) {
165 $langs->load("errors");
166 $examplesup = '<span class="'.$cssforerror.'">'.$langs->trans('ErrorBadMask').'</span>';
167 }
168 if (in_array($examplesup, $errmsg)) {
169 $langs->load("errors");
170 $examplesup = '<span class="'.$cssforerror.'">'.$langs->trans($examplesup).'</span>';
171 }
172 }
173
174 if ($type == 0) {
175 return $examplecust;
176 } elseif ($type == 1) {
177 return $examplesup;
178 } else {
179 return $examplecust.'<br>'.$examplesup;
180 }
181 }
182
190 public function getNextValue($objsoc = '', $type = -1)
191 {
192 global $db;
193
194 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
195
196 // Get Mask value
197 $mask = '';
198 if ($type == 0) {
199 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER');
200 }
201 if ($type == 1) {
202 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER');
203 }
204 if (!$mask) {
205 $this->error = 'NotConfigured';
206 return '';
207 }
208
209 $field = '';
210 $where = '';
211 if ($type == 0) {
212 $field = 'code_client';
213 //$where = ' AND client in (1,2)';
214 } elseif ($type == 1) {
215 $field = 'code_fournisseur';
216 //$where = ' AND fournisseur = 1';
217 } else {
218 return -1;
219 }
220
221 $now = dol_now();
222
223 $numFinal = get_next_value($db, $mask, 'societe', $field, $where, '', $now);
224
225 return $numFinal;
226 }
227
228
229 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
235 public function verif_prefixIsUsed()
236 {
237 // phpcs:enable
238 global $conf;
239
240 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER');
241 if (preg_match('/\{pre\}/i', $mask)) {
242 return 1;
243 }
244
245 $mask = getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER');
246 if (preg_match('/\{pre\}/i', $mask)) {
247 return 1;
248 }
249
250 return 0;
251 }
252
253
269 public function verif($db, &$code, $soc, $type)
270 {
271 global $conf;
272
273 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
274
275 $result = 0;
276 $code = strtoupper(trim($code));
277
278 if (empty($code) && $this->code_null && !getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED')) {
279 $result = 0;
280 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED'))) {
281 $result = -2;
282 } else {
283 // Get Mask value
284 $mask = '';
285 if ($type == 0) {
286 $mask = !getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER') ? '' : $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
287 }
288 if ($type == 1) {
289 $mask = !getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER') ? '' : $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
290 }
291 if (!$mask) {
292 $this->error = 'NotConfigured';
293 return -5;
294 }
295 $result = check_value($mask, $code);
296 if (is_string($result)) {
297 $this->error = $result;
298 return -6;
299 } else {
300 $is_dispo = $this->verif_dispo($db, $code, $soc, $type);
301 if ($is_dispo != 0) {
302 $result = -3; // Code $code duplicate
303 }
304 }
305 }
306
307 dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
308 return $result;
309 }
310
311
312 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
322 public function verif_dispo($db, $code, $soc, $type = 0)
323 {
324 // phpcs:enable
325 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
326 if ($type == 1) {
327 $sql .= " WHERE code_fournisseur = '".$db->escape($code)."'";
328 } else {
329 $sql .= " WHERE code_client = '".$db->escape($code)."'";
330 }
331 if ($soc->id > 0) {
332 $sql .= " AND rowid <> ".$soc->id;
333 }
334 $sql .= " AND entity IN (".getEntity('societe').")";
335
336 $resql = $db->query($sql);
337 if ($resql) {
338 if ($db->num_rows($resql) == 0) {
339 return 0;
340 } else {
341 // Code $code duplicate
342 return -1;
343 }
344 } else {
345 return -2;
346 }
347 }
348}
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)
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.