dolibarr  20.0.0-beta
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 
30 require_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 
98  // Parametrage du prefix customers
99  $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("ProductCodeModel").'):</td>';
100  $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>';
101 
102  $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
103 
104  $texte .= '</tr>';
105 
106  // Parametrage du prefix suppliers
107  $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("ServiceCodeModel").'):</td>';
108  $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>';
109  $texte .= '</tr>';
110 
111  $texte .= '</table>';
112  $texte .= '</form>';
113 
114  return $texte;
115  }
116 
117 
126  public function getExample($langs, $objproduct = '', $type = -1)
127  {
128  $exampleproduct = $exampleservice = '';
129 
130  if ($type == 0 || $type == -1) {
131  $exampleproduct = $this->getNextValue($objproduct, 0);
132  if (!$exampleproduct) {
133  $exampleproduct = $langs->trans('NotConfigured');
134  }
135  if ($exampleproduct == "ErrorBadMask") {
136  $langs->load("errors");
137  $exampleproduct = $langs->trans($exampleproduct);
138  }
139  }
140  if ($type == 1 || $type == -1) {
141  $exampleservice = $this->getNextValue($objproduct, 1);
142  if (!$exampleservice) {
143  $exampleservice = $langs->trans('NotConfigured');
144  }
145  if ($exampleservice == "ErrorBadMask") {
146  $langs->load("errors");
147  $exampleservice = $langs->trans($exampleservice);
148  }
149  }
150 
151  if ($type == 0) {
152  return $exampleproduct;
153  }
154  if ($type == 1) {
155  return $exampleservice;
156  }
157  return $exampleproduct.'<br>'.$exampleservice;
158  }
159 
167  public function getNextValue($objproduct = null, $type = -1)
168  {
169  global $db, $conf;
170 
171  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
172 
173  // Get Mask value
174  $mask = '';
175  if ($type == 0 && getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT')) {
176  $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT');
177  } elseif ($type == 1 && getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE')) {
178  $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE');
179  }
180 
181  if (empty($mask)) {
182  $this->error = 'NotConfigured';
183  return '';
184  }
185 
186  $field = '';
187  $where = '';
188  if ($type == 0) {
189  $field = 'ref';
190  //$where = ' AND client in (1,2)';
191  } elseif ($type == 1) {
192  $field = 'ref';
193  //$where = ' AND fournisseur = 1';
194  } else {
195  return -1;
196  }
197 
198  $now = dol_now();
199 
200  if (getDolGlobalString('PRODUCT_ELEPHANT_ADD_WHERE')) {
201  $where = ' AND ('.dol_string_nospecial(dol_string_unaccent($conf->global->PRODUCT_ELEPHANT_ADD_WHERE), '_', array(',', '@', '"', "|", ";", ":")).')';
202  }
203 
204  $numFinal = get_next_value($db, $mask, 'product', $field, $where, '', $now);
205 
206  return $numFinal;
207  }
208 
209 
210  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
216  public function verif_prefixIsUsed()
217  {
218  // phpcs:enable
219  global $conf;
220 
221  $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT');
222  if (preg_match('/\{pre\}/i', $mask)) {
223  return 1;
224  }
225 
226  $mask = getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE');
227  if (preg_match('/\{pre\}/i', $mask)) {
228  return 1;
229  }
230 
231  return 0;
232  }
233 
234 
249  public function verif($db, &$code, $product, $type)
250  {
251  global $conf;
252 
253  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
254 
255  $result = 0;
256  $code = strtoupper(trim($code));
257 
258  if (empty($code) && $this->code_null && !getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED')) {
259  $result = 0;
260  } elseif (empty($code) && (!$this->code_null || getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED'))) {
261  $result = -2;
262  } else {
263  // Get Mask value
264  $mask = '';
265  if ($type == 0) {
266  $mask = !getDolGlobalString('PRODUCT_ELEPHANT_MASK_PRODUCT') ? '' : $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
267  }
268  if ($type == 1) {
269  $mask = !getDolGlobalString('PRODUCT_ELEPHANT_MASK_SERVICE') ? '' : $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
270  }
271  if (!$mask) {
272  $this->error = 'NotConfigured';
273  return -5;
274  }
275 
276  $result = check_value($mask, $code);
277  if (is_string($result)) {
278  $this->error = $result;
279  return -5;
280  }
281  }
282 
283  dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
284  return $result;
285  }
286 
287 
288  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
297  public function verif_dispo($db, $code, $product)
298  {
299  // phpcs:enable
300  $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
301  $sql .= " WHERE ref = '".$db->escape($code)."'";
302  if ($product->id > 0) {
303  $sql .= " AND rowid <> ".$product->id;
304  }
305 
306  $resql = $db->query($sql);
307  if ($resql) {
308  if ($db->num_rows($resql) == 0) {
309  return 0;
310  } else {
311  return -1;
312  }
313  } else {
314  return -2;
315  }
316  }
317 }
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.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
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.