dolibarr 21.0.0-alpha
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 = null, $objproduct = '', $type = -1)
128 {
129 if (!$langs instanceof Translate) {
130 $langs = $GLOBALS['langs'];
131 '@phan-var-force Translate $langs';
132 }
133 $exampleproduct = $exampleservice = '';
134
135 if ($type == 0 || $type == -1) {
136 $exampleproduct = $this->getNextValue($objproduct, 0);
137 if (!$exampleproduct) {
138 $exampleproduct = $langs->trans('NotConfigured');
139 }
140 if ($exampleproduct == "ErrorBadMask") {
141 $langs->load("errors");
142 $exampleproduct = $langs->trans($exampleproduct);
143 }
144 }
145 if ($type == 1 || $type == -1) {
146 $exampleservice = $this->getNextValue($objproduct, 1);
147 if (!$exampleservice) {
148 $exampleservice = $langs->trans('NotConfigured');
149 }
150 if ($exampleservice == "ErrorBadMask") {
151 $langs->load("errors");
152 $exampleservice = $langs->trans($exampleservice);
153 }
154 }
155
156 if ($type == 0) {
157 return $exampleproduct;
158 }
159 if ($type == 1) {
160 return $exampleservice;
161 }
162 return $exampleproduct.'<br>'.$exampleservice;
163 }
164
172 public function getNextValue($objproduct = null, $type = -1)
173 {
174 global $db, $conf;
175
176 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
177
178 // Get Mask value
179 $mask = '';
180 if ($type == 0 && getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT')) {
181 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT');
182 } elseif ($type == 1 && getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE')) {
183 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE');
184 }
185
186 if (empty($mask)) {
187 $this->error = 'NotConfigured';
188 return '';
189 }
190
191 $field = '';
192 $where = '';
193 if ($type == 0) {
194 $field = 'ref';
195 //$where = ' AND client in (1,2)';
196 } elseif ($type == 1) {
197 $field = 'ref';
198 //$where = ' AND fournisseur = 1';
199 } else {
200 return -1;
201 }
202
203 $now = dol_now();
204
205 if (getDolGlobalString('PRODUCT_ELEPHANT_ADD_WHERE')) {
206 $where = ' AND ('.dol_string_nospecial(dol_string_unaccent($conf->global->PRODUCT_ELEPHANT_ADD_WHERE), '_', array(',', '@', '"', "|", ";", ":")).')';
207 }
208
209 $numFinal = get_next_value($db, $mask, 'product', $field, $where, '', $now);
210
211 return $numFinal;
212 }
213
214
215 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
221 public function verif_prefixIsUsed()
222 {
223 // phpcs:enable
224 global $conf;
225
226 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT');
227 if (preg_match('/\{pre\}/i', $mask)) {
228 return 1;
229 }
230
231 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE');
232 if (preg_match('/\{pre\}/i', $mask)) {
233 return 1;
234 }
235
236 return 0;
237 }
238
239
254 public function verif($db, &$code, $product, $type)
255 {
256 global $conf;
257
258 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
259
260 $result = 0;
261 $code = strtoupper(trim($code));
262
263 if (empty($code) && $this->code_null && !getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED')) {
264 $result = 0;
265 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED'))) {
266 $result = -2;
267 } else {
268 // Get Mask value
269 $mask = '';
270 if ($type == 0) {
271 $mask = !getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT') ? '' : $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
272 }
273 if ($type == 1) {
274 $mask = !getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE') ? '' : $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
275 }
276 if (!$mask) {
277 $this->error = 'NotConfigured';
278 return -5;
279 }
280
281 $result = check_value($mask, $code);
282 if (is_string($result)) {
283 $this->error = $result;
284 return -5;
285 }
286 }
287
288 dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
289 return $result;
290 }
291
292
293 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
302 public function verif_dispo($db, $code, $product)
303 {
304 // phpcs:enable
305 $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
306 $sql .= " WHERE ref = '".$db->escape($code)."'";
307 if ($product->id > 0) {
308 $sql .= " AND rowid <> ".$product->id;
309 }
310
311 $resql = $db->query($sql);
312 if ($resql) {
313 if ($db->num_rows($resql) == 0) {
314 return 0;
315 } else {
316 return -1;
317 }
318 } else {
319 return -2;
320 }
321 }
322}
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)
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.