dolibarr 23.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 * 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
45 public $position = 50;
46
47 // variables not inherited
48
52 public $searchcode;
53
57 public $numbitcounter;
58
59
63 public function __construct()
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("products");
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="PRODUCT_ELEPHANT_MASK_PRODUCT">';
95 $texte .= '<input type="hidden" name="param2" value="PRODUCT_ELEPHANT_MASK_SERVICE">';
96 $texte .= '<table class="nobordernopadding centpercent">';
97
98 $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("Product"), $langs->transnoentities("Product"));
99 $tooltip .= $langs->trans("GenericMaskCodes1");
100 $tooltip .= $langs->trans("GenericMaskCodes3");
101 $tooltip .= $langs->trans("GenericMaskCodes4c");
102 $tooltip .= $langs->trans("GenericMaskCodes5");
103 //$tooltip .= '<br>'.$langs->trans("GenericMaskCodes5b");
104
105 // Parametrage du prefix customers
106 $texte .= '<tr><td>'.$langs->trans("ProductCodeModel").'</td>';
107 $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>';
108
109 $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
110
111 $texte .= '</tr>';
112
113 // Parametrage du prefix suppliers
114 $texte .= '<tr><td>'.$langs->trans("ServiceCodeModel").'</td>';
115 $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>';
116 $texte .= '</tr>';
117
118 $texte .= '</table>';
119 $texte .= '</form>';
120
121 return $texte;
122 }
123
124
133 public function getExample($langs = null, $objproduct = '', $type = -1)
134 {
135 if (!$langs instanceof Translate) {
136 $langs = $GLOBALS['langs'];
137 '@phan-var-force Translate $langs';
138 }
139 $exampleproduct = $exampleservice = '';
140
141 if ($type == 0 || $type == -1) {
142 $exampleproduct = $this->getNextValue($objproduct, 0);
143 if (!$exampleproduct) {
144 $exampleproduct = $langs->trans('NotConfigured');
145 }
146 if ($exampleproduct == "ErrorBadMask") {
147 $langs->load("errors");
148 $exampleproduct = $langs->trans($exampleproduct);
149 }
150 }
151 if ($type == 1 || $type == -1) {
152 $exampleservice = $this->getNextValue($objproduct, 1);
153 if (!$exampleservice) {
154 $exampleservice = $langs->trans('NotConfigured');
155 }
156 if ($exampleservice == "ErrorBadMask") {
157 $langs->load("errors");
158 $exampleservice = $langs->trans($exampleservice);
159 }
160 }
161
162 if ($type == 0) {
163 return $exampleproduct;
164 }
165 if ($type == 1) {
166 return $exampleservice;
167 }
168 return $exampleproduct.'<br>'.$exampleservice;
169 }
170
178 public function getNextValue($objproduct = null, $type = -1)
179 {
180 global $db;
181
182 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
183
184 // Get Mask value
185 $mask = '';
186 if ($type == 0 && getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT')) {
187 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT');
188 } elseif ($type == 1 && getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE')) {
189 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE');
190 }
191
192 if (empty($mask)) {
193 $this->error = 'NotConfigured';
194 return '';
195 }
196
197 $field = '';
198 $where = '';
199 if ($type == 0) {
200 $field = 'ref';
201 //$where = ' AND client in (1,2)';
202 } elseif ($type == 1) {
203 $field = 'ref';
204 //$where = ' AND fournisseur = 1';
205 } else {
206 return -1;
207 }
208
209 $now = dol_now();
210
211 if (getDolGlobalString('PRODUCT_ELEPHANT_ADD_WHERE')) {
212 $where = ' AND ('.dol_string_nospecial(dol_string_unaccent(getDolGlobalString('PRODUCT_ELEPHANT_ADD_WHERE')), '_', array(',', '@', '"', "|", ";", ":")).')';
213 }
214
215 $numFinal = get_next_value($db, $mask, 'product', $field, $where, '', $now);
216
217 return $numFinal;
218 }
219
220
221 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
227 public function verif_prefixIsUsed()
228 {
229 // phpcs:enable
230 global $conf;
231
232 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT');
233 if (preg_match('/\{pre\}/i', $mask)) {
234 return 1;
235 }
236
237 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE');
238 if (preg_match('/\{pre\}/i', $mask)) {
239 return 1;
240 }
241
242 return 0;
243 }
244
245
260 public function verif($db, &$code, $product, $type)
261 {
262 global $conf;
263
264 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
265
266 $result = 0;
267 $code = strtoupper(trim($code));
268
269 if (empty($code) && $this->code_null && !getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED')) {
270 $result = 0;
271 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED'))) {
272 $result = -2;
273 } else {
274 // Get Mask value
275 $mask = '';
276 if ($type == 0) {
277 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT');
278 }
279 if ($type == 1) {
280 $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE');
281 }
282 if (!$mask) {
283 $this->error = 'NotConfigured';
284 return -5;
285 }
286
287 $result = check_value($mask, $code);
288 if (is_string($result)) {
289 $this->error = $result;
290 return -5;
291 }
292 }
293
294 dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
295 return $result;
296 }
297
298
299 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
308 public function verif_dispo($db, $code, $product)
309 {
310 // phpcs:enable
311 $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
312 $sql .= " WHERE ref = '".$db->escape($code)."'";
313 if ($product->id > 0) {
314 $sql .= " AND rowid <> ".$product->id;
315 }
316
317 $resql = $db->query($sql);
318 if ($resql) {
319 if ($db->num_rows($resql) == 0) {
320 return 0;
321 } else {
322 return -1;
323 }
324 } else {
325 return -2;
326 }
327 }
328}
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='gmt')
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.