dolibarr 22.0.5
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 centpercent">';
92
93 $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("Product"), $langs->transnoentities("Product"));
94 $tooltip .= $langs->trans("GenericMaskCodes1");
95 $tooltip .= $langs->trans("GenericMaskCodes3");
96 $tooltip .= $langs->trans("GenericMaskCodes4c");
97 $tooltip .= $langs->trans("GenericMaskCodes5");
98 //$tooltip .= '<br>'.$langs->trans("GenericMaskCodes5b");
99
100 // Parametrage du prefix customers
101 $texte .= '<tr><td>'.$langs->trans("ProductCodeModel").'</td>';
102 $texte .= '<td class="right nowraponall">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value1" placeholder="'.$langs->trans("Mask").'" value="'.getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT').'"'.$disabled.'>', $tooltip, 1, 'help', 'valignmiddle', 0, 3, $this->name).'</td>';
103
104 $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
105
106 $texte .= '</tr>';
107
108 // Parametrage du prefix suppliers
109 $texte .= '<tr><td>'.$langs->trans("ServiceCodeModel").'</td>';
110 $texte .= '<td class="right nowraponall">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value2" placeholder="'.$langs->trans("Mask").'" value="'.getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE').'"'.$disabled.'>', $tooltip, 1, 'help', 'valignmiddle', 0, 3, $this->name).'</td>';
111 $texte .= '</tr>';
112
113 $texte .= '</table>';
114 $texte .= '</form>';
115
116 return $texte;
117 }
118
119
128 public function getExample($langs = null, $objproduct = '', $type = -1)
129 {
130 if (!$langs instanceof Translate) {
131 $langs = $GLOBALS['langs'];
132 '@phan-var-force Translate $langs';
133 }
134 $exampleproduct = $exampleservice = '';
135
136 if ($type == 0 || $type == -1) {
137 $exampleproduct = $this->getNextValue($objproduct, 0);
138 if (!$exampleproduct) {
139 $exampleproduct = $langs->trans('NotConfigured');
140 }
141 if ($exampleproduct == "ErrorBadMask") {
142 $langs->load("errors");
143 $exampleproduct = $langs->trans($exampleproduct);
144 }
145 }
146 if ($type == 1 || $type == -1) {
147 $exampleservice = $this->getNextValue($objproduct, 1);
148 if (!$exampleservice) {
149 $exampleservice = $langs->trans('NotConfigured');
150 }
151 if ($exampleservice == "ErrorBadMask") {
152 $langs->load("errors");
153 $exampleservice = $langs->trans($exampleservice);
154 }
155 }
156
157 if ($type == 0) {
158 return $exampleproduct;
159 }
160 if ($type == 1) {
161 return $exampleservice;
162 }
163 return $exampleproduct.'<br>'.$exampleservice;
164 }
165
173 public function getNextValue($objproduct = null, $type = -1)
174 {
175 global $db;
176
177 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
178
179 // Get Mask value
180 $mask = '';
181 if ($type == 0 && getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT')) {
182 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT');
183 } elseif ($type == 1 && getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE')) {
184 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE');
185 }
186
187 if (empty($mask)) {
188 $this->error = 'NotConfigured';
189 return '';
190 }
191
192 $field = '';
193 $where = '';
194 if ($type == 0) {
195 $field = 'ref';
196 //$where = ' AND client in (1,2)';
197 } elseif ($type == 1) {
198 $field = 'ref';
199 //$where = ' AND fournisseur = 1';
200 } else {
201 return -1;
202 }
203
204 $now = dol_now();
205
206 if (getDolGlobalString('PRODUCT_ELEPHANT_ADD_WHERE')) {
207 $where = ' AND ('.dol_string_nospecial(dol_string_unaccent(getDolGlobalString('PRODUCT_ELEPHANT_ADD_WHERE')), '_', array(',', '@', '"', "|", ";", ":")).')';
208 }
209
210 $numFinal = get_next_value($db, $mask, 'product', $field, $where, '', $now);
211
212 return $numFinal;
213 }
214
215
216 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
222 public function verif_prefixIsUsed()
223 {
224 // phpcs:enable
225 global $conf;
226
227 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT');
228 if (preg_match('/\{pre\}/i', $mask)) {
229 return 1;
230 }
231
232 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE');
233 if (preg_match('/\{pre\}/i', $mask)) {
234 return 1;
235 }
236
237 return 0;
238 }
239
240
255 public function verif($db, &$code, $product, $type)
256 {
257 global $conf;
258
259 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
260
261 $result = 0;
262 $code = strtoupper(trim($code));
263
264 if (empty($code) && $this->code_null && !getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED')) {
265 $result = 0;
266 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED'))) {
267 $result = -2;
268 } else {
269 // Get Mask value
270 $mask = '';
271 if ($type == 0) {
272 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT');
273 }
274 if ($type == 1) {
275 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE');
276 }
277 if (!$mask) {
278 $this->error = 'NotConfigured';
279 return -5;
280 }
281
282 $result = check_value($mask, $code);
283 if (is_string($result)) {
284 $this->error = $result;
285 return -5;
286 }
287 }
288
289 dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
290 return $result;
291 }
292
293
294 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
303 public function verif_dispo($db, $code, $product)
304 {
305 // phpcs:enable
306 $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
307 $sql .= " WHERE ref = '".$db->escape($code)."'";
308 if ($product->id > 0) {
309 $sql .= " AND rowid <> ".$product->id;
310 }
311
312 $resql = $db->query($sql);
313 if ($resql) {
314 if ($db->num_rows($resql) == 0) {
315 return 0;
316 } else {
317 return -1;
318 }
319 } else {
320 return -2;
321 }
322 }
323}
Class template for classes of numbering product.
Class to manage translations.
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=null, $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, $objbookkeeping=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 a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79