dolibarr  13.0.2
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 
28 require_once DOL_DOCUMENT_ROOT.'/core/modules/product/modules_product.class.php';
29 
30 
36 {
42  public $nom = 'Elephant';
43 
47  public $name = 'Elephant';
48 
49  public $code_modifiable; // Code modifiable
50 
51  public $code_modifiable_invalide; // Code modifiable si il est invalide
52 
53  public $code_modifiable_null; // Code modifiables si il est null
54 
55  public $code_null; // Code facultatif
56 
61  public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
62 
66  public $code_auto;
67 
68  public $searchcode; // String de recherche
69 
70  public $numbitcounter; // Nombre de chiffres du compteur
71 
72  public $prefixIsRequired; // Le champ prefix du tiers doit etre renseigne quand on utilise {pre}
73 
74 
78  public function __construct()
79  {
80  $this->code_null = 0;
81  $this->code_modifiable = 1;
82  $this->code_modifiable_invalide = 1;
83  $this->code_modifiable_null = 1;
84  $this->code_auto = 1;
85  $this->prefixIsRequired = 0;
86  }
87 
88 
95  public function info($langs)
96  {
97  global $conf, $mc;
98  global $form;
99 
100  $langs->load("products");
101 
102  $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
103 
104  $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
105  $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
106  $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
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" size="24" name="value1" value="'.(!empty($conf->global->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" value="'.$langs->trans("Modify").'" name="Button"'.$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" size="24" name="value2" value="'.(!empty($conf->global->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  if ($type == 0 || $type == -1)
148  {
149  $exampleproduct = $this->getNextValue($objproduct, 0);
150  if (!$exampleproduct)
151  {
152  $exampleproduct = $langs->trans('NotConfigured');
153  }
154  if ($exampleproduct == "ErrorBadMask")
155  {
156  $langs->load("errors");
157  $exampleproduct = $langs->trans($exampleproduct);
158  }
159  }
160  if ($type == 1 || $type == -1)
161  {
162  $exampleservice = $this->getNextValue($objproduct, 1);
163  if (!$exampleservice)
164  {
165  $exampleservice = $langs->trans('NotConfigured');
166  }
167  if ($exampleservice == "ErrorBadMask")
168  {
169  $langs->load("errors");
170  $exampleservice = $langs->trans($exampleservice);
171  }
172  }
173 
174  if ($type == 0) return $exampleproduct;
175  if ($type == 1) return $exampleservice;
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 && !empty($conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT))
195  $mask = $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
196  elseif ($type == 1 && !empty($conf->global->PRODUCT_ELEPHANT_MASK_SERVICE))
197  $mask = $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
198 
199  if (empty($mask))
200  {
201  $this->error = 'NotConfigured';
202  return '';
203  }
204 
205  $field = ''; $where = '';
206  if ($type == 0)
207  {
208  $field = 'ref';
209  //$where = ' AND client in (1,2)';
210  } elseif ($type == 1)
211  {
212  $field = 'ref';
213  //$where = ' AND fournisseur = 1';
214  } else return -1;
215 
216  $now = dol_now();
217 
218  if (!empty($conf->global->PRODUCT_ELEPHANT_ADD_WHERE))
219  {
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)) return 1;
242 
243  $mask = $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
244  if (preg_match('/\{pre\}/i', $mask)) return 1;
245 
246  return 0;
247  }
248 
249 
264  public function verif($db, &$code, $product, $type)
265  {
266  global $conf;
267 
268  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
269 
270  $result = 0;
271  $code = strtoupper(trim($code));
272 
273  if (empty($code) && $this->code_null && empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED))
274  {
275  $result = 0;
276  } elseif (empty($code) && (!$this->code_null || !empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED)))
277  {
278  $result = -2;
279  } else {
280  // Get Mask value
281  $mask = '';
282  if ($type == 0) $mask = empty($conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT) ? '' : $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
283  if ($type == 1) $mask = empty($conf->global->PRODUCT_ELEPHANT_MASK_SERVICE) ? '' : $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
284  if (!$mask)
285  {
286  $this->error = 'NotConfigured';
287  return -5;
288  }
289 
290  $result = check_value($mask, $code);
291  if (is_string($result))
292  {
293  $this->error = $result;
294  return -5;
295  }
296  }
297 
298  dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
299  return $result;
300  }
301 
302 
303  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
312  public function verif_dispo($db, $code, $product)
313  {
314  // phpcs:enable
315  $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
316  $sql .= " WHERE ref = '".$db->escape($code)."'";
317  if ($product->id > 0) $sql .= " AND rowid <> ".$product->id;
318 
319  $resql = $db->query($sql);
320  if ($resql)
321  {
322  if ($db->num_rows($resql) == 0)
323  {
324  return 0;
325  } else {
326  return -1;
327  }
328  } else {
329  return -2;
330  }
331  }
332 }
getExample($langs, $objproduct=0, $type=-1)
Return an example of result returned by getNextValue.
verif($db, &$code, $product, $type)
Check validity of code according to its rules.
dol_now($mode= 'auto')
Return date for now.
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)
check_value($mask, $value)
Check value.
info($langs)
Return description of module.
getNextValue($objproduct=0, $type=-1)
Return next value.
verif_prefixIsUsed()
Check if mask/numbering use prefix.
verif_dispo($db, $code, $product)
Renvoi si un code est pris ou non (par autre tiers)
Class to manage product code with elephant rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
dol_string_unaccent($str)
Clean a string from all accent characters to be used as ref, login or by dol_sanitizeFileName.
Class template for classes of numbering product.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232