dolibarr  9.0.0
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 <http://www.gnu.org/licenses/>.
19  * or see http://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 
63  public $code_auto; // Numerotation automatique
64 
65  public $searchcode; // String de recherche
66 
67  public $numbitcounter; // Nombre de chiffres du compteur
68 
69  public $prefixIsRequired; // Le champ prefix du tiers doit etre renseigne quand on utilise {pre}
70 
71 
75  function __construct()
76  {
77  $this->code_null = 0;
78  $this->code_modifiable = 1;
79  $this->code_modifiable_invalide = 1;
80  $this->code_modifiable_null = 1;
81  $this->code_auto = 1;
82  $this->prefixIsRequired = 0;
83  }
84 
85 
91  function info($langs)
92  {
93  global $conf, $mc;
94  global $form;
95 
96  $langs->load("products");
97 
98  $disabled = ((! empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
99 
100  $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
101  $texte.= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
102  $texte.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
103  $texte.= '<input type="hidden" name="action" value="setModuleOptions">';
104  $texte.= '<input type="hidden" name="param1" value="PRODUCT_ELEPHANT_MASK_PRODUCT">';
105  $texte.= '<input type="hidden" name="param2" value="PRODUCT_ELEPHANT_MASK_SERVICE">';
106  $texte.= '<table class="nobordernopadding" width="100%">';
107 
108  $tooltip=$langs->trans("GenericMaskCodes",$langs->transnoentities("Product"),$langs->transnoentities("Product"));
109  $tooltip.=$langs->trans("GenericMaskCodes3");
110  $tooltip.=$langs->trans("GenericMaskCodes4c");
111  $tooltip.=$langs->trans("GenericMaskCodes5");
112 
113  // Parametrage du prefix customers
114  $texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("ProductCodeModel").'):</td>';
115  $texte.= '<td align="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>';
116 
117  $texte.= '<td align="left" rowspan="2">&nbsp; <input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button"'.$disabled.'></td>';
118 
119  $texte.= '</tr>';
120 
121  // Parametrage du prefix suppliers
122  $texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("ServiceCodeModel").'):</td>';
123  $texte.= '<td align="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>';
124  $texte.= '</tr>';
125 
126  $texte.= '</table>';
127  $texte.= '</form>';
128 
129  return $texte;
130  }
131 
132 
141  function getExample($langs,$objproduct=0,$type=-1)
142  {
143  if ($type == 0 || $type == -1)
144  {
145  $exampleproduct = $this->getNextValue($objproduct,0);
146  if (! $exampleproduct)
147  {
148  $exampleproduct = $langs->trans('NotConfigured');
149  }
150  if($exampleproduct=="ErrorBadMask")
151  {
152  $langs->load("errors");
153  $exampleproduct=$langs->trans($exampleproduct);
154  }
155  }
156  if ($type == 1 || $type == -1)
157  {
158  $exampleservice = $this->getNextValue($objproduct,1);
159  if (! $exampleservice)
160  {
161  $exampleservice = $langs->trans('NotConfigured');
162  }
163  if($exampleservice=="ErrorBadMask")
164  {
165  $langs->load("errors");
166  $exampleservice=$langs->trans($exampleservice);
167  }
168  }
169 
170  if ($type == 0) return $exampleproduct;
171  if ($type == 1) return $exampleservice;
172  return $exampleproduct.'<br>'.$exampleservice;
173  }
174 
182  function getNextValue($objproduct=0,$type=-1)
183  {
184  global $db,$conf;
185 
186  require_once DOL_DOCUMENT_ROOT .'/core/lib/functions2.lib.php';
187 
188  // Get Mask value
189  $mask = '';
190  if ($type == 0 && ! empty($conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT))
191  $mask = $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
192  else if ($type == 1 && ! empty($conf->global->PRODUCT_ELEPHANT_MASK_SERVICE))
193  $mask = $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
194 
195  if (empty($mask))
196  {
197  $this->error='NotConfigured';
198  return '';
199  }
200 
201  $field='';$where='';
202  if ($type == 0)
203  {
204  $field = 'ref';
205  //$where = ' AND client in (1,2)';
206  }
207  else if ($type == 1)
208  {
209  $field = 'ref';
210  //$where = ' AND fournisseur = 1';
211  }
212  else return -1;
213 
214  $now=dol_now();
215 
216  $numFinal=get_next_value($db,$mask,'product',$field,$where,'',$now);
217 
218  return $numFinal;
219  }
220 
221 
222  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
229  {
230  // phpcs:enable
231  global $conf;
232 
233  $mask = $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
234  if (preg_match('/\{pre\}/i',$mask)) return 1;
235 
236  $mask = $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
237  if (preg_match('/\{pre\}/i',$mask)) return 1;
238 
239  return 0;
240  }
241 
242 
257  function verif($db, &$code, $product, $type)
258  {
259  global $conf;
260 
261  require_once DOL_DOCUMENT_ROOT .'/core/lib/functions2.lib.php';
262 
263  $result=0;
264  $code = strtoupper(trim($code));
265 
266  if (empty($code) && $this->code_null && empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED))
267  {
268  $result=0;
269  }
270  else if (empty($code) && (! $this->code_null || ! empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED)) )
271  {
272  $result=-2;
273  }
274  else
275  {
276  // Get Mask value
277  $mask = '';
278  if ($type==0) $mask = empty($conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT)?'':$conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
279  if ($type==1) $mask = empty($conf->global->PRODUCT_ELEPHANT_MASK_SERVICE)?'':$conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
280  if (! $mask)
281  {
282  $this->error='NotConfigured';
283  return -5;
284  }
285 
286  $result=check_value($mask,$code);
287  if (is_string($result))
288  {
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.NotCamelCaps
308  function verif_dispo($db, $code, $product)
309  {
310  // phpcs:enable
311  $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
312  $sql.= " WHERE ref = '".$code."'";
313  if ($product->id > 0) $sql.= " AND rowid <> ".$product->id;
314 
315  $resql=$db->query($sql);
316  if ($resql)
317  {
318  if ($db->num_rows($resql) == 0)
319  {
320  return 0;
321  }
322  else
323  {
324  return -1;
325  }
326  }
327  else
328  {
329  return -2;
330  }
331  }
332 }
getExample($langs, $objproduct=0, $type=-1)
Return an example of result returned by getNextValue.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->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) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
verif($db, &$code, $product, $type)
Check validity of code according to its rules.
$version
Dolibarr version of the loaded document string.
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)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage product code with elephant rule.
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='gmt')
Return date for now.
Parent class for product code generators.