dolibarr 20.0.0
mod_codeproduct_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) 2024 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * or see https://www.gnu.org/
22 */
23
30require_once DOL_DOCUMENT_ROOT.'/core/modules/product/modules_product.class.php';
31
32
37{
38 // variables inherited from ModelProductCode class
39 public $name = 'Elephant';
40 public $version = 'dolibarr';
41
42 // variables not inherited
43
47 public $searchcode;
48
52 public $numbitcounter;
53
54
58 public function __construct()
59 {
60 $this->code_null = 0;
61 $this->code_modifiable = 1;
62 $this->code_modifiable_invalide = 1;
63 $this->code_modifiable_null = 1;
64 $this->code_auto = 1;
65 $this->prefixIsRequired = 0;
66 }
67
68
75 public function info($langs)
76 {
77 global $conf, $mc;
78 global $form;
79
80 $langs->load("products");
81
82 $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
83
84 $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
85 $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
86 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
87 $texte .= '<input type="hidden" name="page_y" value="">';
88 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
89 $texte .= '<input type="hidden" name="param1" value="PRODUCT_ELEPHANT_MASK_PRODUCT">';
90 $texte .= '<input type="hidden" name="param2" value="PRODUCT_ELEPHANT_MASK_SERVICE">';
91 $texte .= '<table class="nobordernopadding" width="100%">';
92
93 $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("Product"), $langs->transnoentities("Product"));
94 $tooltip .= $langs->trans("GenericMaskCodes3");
95 $tooltip .= $langs->trans("GenericMaskCodes4c");
96 $tooltip .= $langs->trans("GenericMaskCodes5");
97 //$tooltip .= '<br>'.$langs->trans("GenericMaskCodes5b");
98
99 // Parametrage du prefix customers
100 $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("ProductCodeModel").'):</td>';
101 $texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value1" value="'.(getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT') ? $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT : '').'"'.$disabled.'>', $tooltip, 1, 1).'</td>';
102
103 $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
104
105 $texte .= '</tr>';
106
107 // Parametrage du prefix suppliers
108 $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("ServiceCodeModel").'):</td>';
109 $texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value2" value="'.(getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE') ? $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE : '').'"'.$disabled.'>', $tooltip, 1, 1).'</td>';
110 $texte .= '</tr>';
111
112 $texte .= '</table>';
113 $texte .= '</form>';
114
115 return $texte;
116 }
117
118
127 public function getExample($langs, $objproduct = '', $type = -1)
128 {
129 $exampleproduct = $exampleservice = '';
130
131 if ($type == 0 || $type == -1) {
132 $exampleproduct = $this->getNextValue($objproduct, 0);
133 if (!$exampleproduct) {
134 $exampleproduct = $langs->trans('NotConfigured');
135 }
136 if ($exampleproduct == "ErrorBadMask") {
137 $langs->load("errors");
138 $exampleproduct = $langs->trans($exampleproduct);
139 }
140 }
141 if ($type == 1 || $type == -1) {
142 $exampleservice = $this->getNextValue($objproduct, 1);
143 if (!$exampleservice) {
144 $exampleservice = $langs->trans('NotConfigured');
145 }
146 if ($exampleservice == "ErrorBadMask") {
147 $langs->load("errors");
148 $exampleservice = $langs->trans($exampleservice);
149 }
150 }
151
152 if ($type == 0) {
153 return $exampleproduct;
154 }
155 if ($type == 1) {
156 return $exampleservice;
157 }
158 return $exampleproduct.'<br>'.$exampleservice;
159 }
160
168 public function getNextValue($objproduct = null, $type = -1)
169 {
170 global $db, $conf;
171
172 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
173
174 // Get Mask value
175 $mask = '';
176 if ($type == 0 && getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT')) {
177 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT');
178 } elseif ($type == 1 && getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE')) {
179 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE');
180 }
181
182 if (empty($mask)) {
183 $this->error = 'NotConfigured';
184 return '';
185 }
186
187 $field = '';
188 $where = '';
189 if ($type == 0) {
190 $field = 'ref';
191 //$where = ' AND client in (1,2)';
192 } elseif ($type == 1) {
193 $field = 'ref';
194 //$where = ' AND fournisseur = 1';
195 } else {
196 return -1;
197 }
198
199 $now = dol_now();
200
201 if (getDolGlobalString('PRODUCT_ELEPHANT_ADD_WHERE')) {
202 $where = ' AND ('.dol_string_nospecial(dol_string_unaccent($conf->global->PRODUCT_ELEPHANT_ADD_WHERE), '_', array(',', '@', '"', "|", ";", ":")).')';
203 }
204
205 $numFinal = get_next_value($db, $mask, 'product', $field, $where, '', $now);
206
207 return $numFinal;
208 }
209
210
211 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
217 public function verif_prefixIsUsed()
218 {
219 // phpcs:enable
220 global $conf;
221
222 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT');
223 if (preg_match('/\{pre\}/i', $mask)) {
224 return 1;
225 }
226
227 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE');
228 if (preg_match('/\{pre\}/i', $mask)) {
229 return 1;
230 }
231
232 return 0;
233 }
234
235
250 public function verif($db, &$code, $product, $type)
251 {
252 global $conf;
253
254 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
255
256 $result = 0;
257 $code = strtoupper(trim($code));
258
259 if (empty($code) && $this->code_null && !getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED')) {
260 $result = 0;
261 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED'))) {
262 $result = -2;
263 } else {
264 // Get Mask value
265 $mask = '';
266 if ($type == 0) {
267 $mask = !getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT') ? '' : $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
268 }
269 if ($type == 1) {
270 $mask = !getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE') ? '' : $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
271 }
272 if (!$mask) {
273 $this->error = 'NotConfigured';
274 return -5;
275 }
276
277 $result = check_value($mask, $code);
278 if (is_string($result)) {
279 $this->error = $result;
280 return -5;
281 }
282 }
283
284 dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
285 return $result;
286 }
287
288
289 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
298 public function verif_dispo($db, $code, $product)
299 {
300 // phpcs:enable
301 $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
302 $sql .= " WHERE ref = '".$db->escape($code)."'";
303 if ($product->id > 0) {
304 $sql .= " AND rowid <> ".$product->id;
305 }
306
307 $resql = $db->query($sql);
308 if ($resql) {
309 if ($db->num_rows($resql) == 0) {
310 return 0;
311 } else {
312 return -1;
313 }
314 } else {
315 return -2;
316 }
317 }
318}
Class template for classes of numbering product.
Class to manage product code with elephant rule.
verif_dispo($db, $code, $product)
Indicate if the code is available or not (by another third party)
verif_prefixIsUsed()
Check if mask/numbering use prefix.
getExample($langs, $objproduct='', $type=-1)
Return an example of result returned by getNextValue.
info($langs)
Return description of module.
getNextValue($objproduct=null, $type=-1)
Return next value.
verif($db, &$code, $product, $type)
Check validity of code according to its rules.
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.
dol_string_unaccent($str)
Clean a string from all accent characters to be used as ref, login or by dol_sanitizeFileName.
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.