dolibarr 19.0.3
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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * or see https://www.gnu.org/
20 */
21
28require_once DOL_DOCUMENT_ROOT.'/core/modules/product/modules_product.class.php';
29
30
35{
41 public $nom = 'Elephant';
42
46 public $name = 'Elephant';
47
48 public $code_modifiable; // Code modifiable
49
50 public $code_modifiable_invalide; // Code modifiable si il est invalide
51
52 public $code_modifiable_null; // Code modifiables si il est null
53
54 public $code_null; // Code facultatif
55
60 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
61
65 public $code_auto;
66
67 public $searchcode; // String de recherche
68
69 public $numbitcounter; // Nombre de chiffres du compteur
70
71 public $prefixIsRequired; // Le champ prefix du tiers doit etre renseigne quand on utilise {pre}
72
73
77 public function __construct()
78 {
79 $this->code_null = 0;
80 $this->code_modifiable = 1;
81 $this->code_modifiable_invalide = 1;
82 $this->code_modifiable_null = 1;
83 $this->code_auto = 1;
84 $this->prefixIsRequired = 0;
85 }
86
87
94 public function info($langs)
95 {
96 global $conf, $mc;
97 global $form;
98
99 $langs->load("products");
100
101 $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
102
103 $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
104 $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
105 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
106 $texte .= '<input type="hidden" name="page_y" value="">';
107 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
108 $texte .= '<input type="hidden" name="param1" value="PRODUCT_ELEPHANT_MASK_PRODUCT">';
109 $texte .= '<input type="hidden" name="param2" value="PRODUCT_ELEPHANT_MASK_SERVICE">';
110 $texte .= '<table class="nobordernopadding" width="100%">';
111
112 $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("Product"), $langs->transnoentities("Product"));
113 $tooltip .= $langs->trans("GenericMaskCodes3");
114 $tooltip .= $langs->trans("GenericMaskCodes4c");
115 $tooltip .= $langs->trans("GenericMaskCodes5");
116
117 // Parametrage du prefix customers
118 $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("ProductCodeModel").'):</td>';
119 $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>';
120
121 $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
122
123 $texte .= '</tr>';
124
125 // Parametrage du prefix suppliers
126 $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("ServiceCodeModel").'):</td>';
127 $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>';
128 $texte .= '</tr>';
129
130 $texte .= '</table>';
131 $texte .= '</form>';
132
133 return $texte;
134 }
135
136
145 public function getExample($langs, $objproduct = 0, $type = -1)
146 {
147 $exampleproduct = $exampleservice = '';
148
149 if ($type == 0 || $type == -1) {
150 $exampleproduct = $this->getNextValue($objproduct, 0);
151 if (!$exampleproduct) {
152 $exampleproduct = $langs->trans('NotConfigured');
153 }
154 if ($exampleproduct == "ErrorBadMask") {
155 $langs->load("errors");
156 $exampleproduct = $langs->trans($exampleproduct);
157 }
158 }
159 if ($type == 1 || $type == -1) {
160 $exampleservice = $this->getNextValue($objproduct, 1);
161 if (!$exampleservice) {
162 $exampleservice = $langs->trans('NotConfigured');
163 }
164 if ($exampleservice == "ErrorBadMask") {
165 $langs->load("errors");
166 $exampleservice = $langs->trans($exampleservice);
167 }
168 }
169
170 if ($type == 0) {
171 return $exampleproduct;
172 }
173 if ($type == 1) {
174 return $exampleservice;
175 }
176 return $exampleproduct.'<br>'.$exampleservice;
177 }
178
186 public function getNextValue($objproduct = 0, $type = -1)
187 {
188 global $db, $conf;
189
190 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
191
192 // Get Mask value
193 $mask = '';
194 if ($type == 0 && getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT')) {
195 $mask = $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
196 } elseif ($type == 1 && getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE')) {
197 $mask = $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
198 }
199
200 if (empty($mask)) {
201 $this->error = 'NotConfigured';
202 return '';
203 }
204
205 $field = '';
206 $where = '';
207 if ($type == 0) {
208 $field = 'ref';
209 //$where = ' AND client in (1,2)';
210 } elseif ($type == 1) {
211 $field = 'ref';
212 //$where = ' AND fournisseur = 1';
213 } else {
214 return -1;
215 }
216
217 $now = dol_now();
218
219 if (getDolGlobalString('PRODUCT_ELEPHANT_ADD_WHERE')) {
220 $where = ' AND ('.dol_string_nospecial(dol_string_unaccent($conf->global->PRODUCT_ELEPHANT_ADD_WHERE), '_', array(',', '@', '"', "|", ";", ":")).')';
221 }
222
223 $numFinal = get_next_value($db, $mask, 'product', $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 = $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
241 if (preg_match('/\{pre\}/i', $mask)) {
242 return 1;
243 }
244
245 $mask = $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
246 if (preg_match('/\{pre\}/i', $mask)) {
247 return 1;
248 }
249
250 return 0;
251 }
252
253
268 public function verif($db, &$code, $product, $type)
269 {
270 global $conf;
271
272 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
273
274 $result = 0;
275 $code = strtoupper(trim($code));
276
277 if (empty($code) && $this->code_null && !getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED')) {
278 $result = 0;
279 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED'))) {
280 $result = -2;
281 } else {
282 // Get Mask value
283 $mask = '';
284 if ($type == 0) {
285 $mask = !getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT') ? '' : $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
286 }
287 if ($type == 1) {
288 $mask = !getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE') ? '' : $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
289 }
290 if (!$mask) {
291 $this->error = 'NotConfigured';
292 return -5;
293 }
294
295 $result = check_value($mask, $code);
296 if (is_string($result)) {
297 $this->error = $result;
298 return -5;
299 }
300 }
301
302 dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
303 return $result;
304 }
305
306
307 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
316 public function verif_dispo($db, $code, $product)
317 {
318 // phpcs:enable
319 $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
320 $sql .= " WHERE ref = '".$db->escape($code)."'";
321 if ($product->id > 0) {
322 $sql .= " AND rowid <> ".$product->id;
323 }
324
325 $resql = $db->query($sql);
326 if ($resql) {
327 if ($db->num_rows($resql) == 0) {
328 return 0;
329 } else {
330 return -1;
331 }
332 } else {
333 return -2;
334 }
335 }
336}
Class template for classes of numbering product.
Class to manage product code with elephant rule.
getExample($langs, $objproduct=0, $type=-1)
Return an example of result returned by getNextValue.
verif_dispo($db, $code, $product)
Renvoi si un code est pris ou non (par autre tiers)
verif_prefixIsUsed()
Check if mask/numbering use prefix.
info($langs)
Return description of module.
getNextValue($objproduct=0, $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.