dolibarr  7.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@capnetworks.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 {
37  var $nom='Elephant'; // Nom du modele
38  var $name='Elephant'; // Nom du modele
39  var $code_modifiable; // Code modifiable
40  var $code_modifiable_invalide; // Code modifiable si il est invalide
41  var $code_modifiable_null; // Code modifiables si il est null
42  var $code_null; // Code facultatif
43  var $version='dolibarr'; // 'development', 'experimental', 'dolibarr'
44  var $code_auto; // Numerotation automatique
45 
46  var $searchcode; // String de recherche
47  var $numbitcounter; // Nombre de chiffres du compteur
48  var $prefixIsRequired; // Le champ prefix du tiers doit etre renseigne quand on utilise {pre}
49 
50 
54  function __construct()
55  {
56  $this->code_null = 0;
57  $this->code_modifiable = 1;
58  $this->code_modifiable_invalide = 1;
59  $this->code_modifiable_null = 1;
60  $this->code_auto = 1;
61  $this->prefixIsRequired = 0;
62  }
63 
64 
70  function info($langs)
71  {
72  global $conf, $mc;
73  global $form;
74 
75  $langs->load("products");
76 
77  $disabled = ((! empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
78 
79  $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
80  $texte.= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
81  $texte.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
82  $texte.= '<input type="hidden" name="action" value="setModuleOptions">';
83  $texte.= '<input type="hidden" name="param1" value="PRODUCT_ELEPHANT_MASK_PRODUCT">';
84  $texte.= '<input type="hidden" name="param2" value="PRODUCT_ELEPHANT_MASK_SERVICE">';
85  $texte.= '<table class="nobordernopadding" width="100%">';
86 
87  $tooltip=$langs->trans("GenericMaskCodes",$langs->transnoentities("Product"),$langs->transnoentities("Product"));
88  $tooltip.=$langs->trans("GenericMaskCodes3");
89  $tooltip.=$langs->trans("GenericMaskCodes4c");
90  $tooltip.=$langs->trans("GenericMaskCodes5");
91 
92  // Parametrage du prefix customers
93  $texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("ProductCodeModel").'):</td>';
94  $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>';
95 
96  $texte.= '<td align="left" rowspan="2">&nbsp; <input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button"'.$disabled.'></td>';
97 
98  $texte.= '</tr>';
99 
100  // Parametrage du prefix suppliers
101  $texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("ServiceCodeModel").'):</td>';
102  $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>';
103  $texte.= '</tr>';
104 
105  $texte.= '</table>';
106  $texte.= '</form>';
107 
108  return $texte;
109  }
110 
111 
120  function getExample($langs,$objproduct=0,$type=-1)
121  {
122  if ($type == 0 || $type == -1)
123  {
124  $exampleproduct = $this->getNextValue($objproduct,0);
125  if (! $exampleproduct)
126  {
127  $exampleproduct = $langs->trans('NotConfigured');
128  }
129  if($exampleproduct=="ErrorBadMask")
130  {
131  $langs->load("errors");
132  $exampleproduct=$langs->trans($exampleproduct);
133  }
134  }
135  if ($type == 1 || $type == -1)
136  {
137  $exampleservice = $this->getNextValue($objproduct,1);
138  if (! $exampleservice)
139  {
140  $exampleservice = $langs->trans('NotConfigured');
141  }
142  if($exampleservice=="ErrorBadMask")
143  {
144  $langs->load("errors");
145  $exampleservice=$langs->trans($exampleservice);
146  }
147  }
148 
149  if ($type == 0) return $exampleproduct;
150  if ($type == 1) return $exampleservice;
151  return $exampleproduct.'<br>'.$exampleservice;
152  }
153 
161  function getNextValue($objproduct=0,$type=-1)
162  {
163  global $db,$conf;
164 
165  require_once DOL_DOCUMENT_ROOT .'/core/lib/functions2.lib.php';
166 
167  // Get Mask value
168  $mask = '';
169  if ($type == 0 && ! empty($conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT))
170  $mask = $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
171  else if ($type == 1 && ! empty($conf->global->PRODUCT_ELEPHANT_MASK_SERVICE))
172  $mask = $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
173 
174  if (empty($mask))
175  {
176  $this->error='NotConfigured';
177  return '';
178  }
179 
180  $field='';$where='';
181  if ($type == 0)
182  {
183  $field = 'ref';
184  //$where = ' AND client in (1,2)';
185  }
186  else if ($type == 1)
187  {
188  $field = 'ref';
189  //$where = ' AND fournisseur = 1';
190  }
191  else return -1;
192 
193  $now=dol_now();
194 
195  $numFinal=get_next_value($db,$mask,'product',$field,$where,'',$now);
196 
197  return $numFinal;
198  }
199 
200 
207  {
208  global $conf;
209 
210  $mask = $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
211  if (preg_match('/\{pre\}/i',$mask)) return 1;
212 
213  $mask = $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
214  if (preg_match('/\{pre\}/i',$mask)) return 1;
215 
216  return 0;
217  }
218 
219 
234  function verif($db, &$code, $product, $type)
235  {
236  global $conf;
237 
238  require_once DOL_DOCUMENT_ROOT .'/core/lib/functions2.lib.php';
239 
240  $result=0;
241  $code = strtoupper(trim($code));
242 
243  if (empty($code) && $this->code_null && empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED))
244  {
245  $result=0;
246  }
247  else if (empty($code) && (! $this->code_null || ! empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED)) )
248  {
249  $result=-2;
250  }
251  else
252  {
253  // Get Mask value
254  $mask = '';
255  if ($type==0) $mask = empty($conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT)?'':$conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
256  if ($type==1) $mask = empty($conf->global->PRODUCT_ELEPHANT_MASK_SERVICE)?'':$conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
257  if (! $mask)
258  {
259  $this->error='NotConfigured';
260  return -5;
261  }
262 
263  $result=check_value($mask,$code);
264  if (is_string($result))
265  {
266  $this->error = $result;
267  return -5;
268  }
269  }
270 
271  dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
272  return $result;
273  }
274 
275 
284  function verif_dispo($db, $code, $product)
285  {
286  $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
287  $sql.= " WHERE ref = '".$code."'";
288  if ($product->id > 0) $sql.= " AND rowid <> ".$product->id;
289 
290  $resql=$db->query($sql);
291  if ($resql)
292  {
293  if ($db->num_rows($resql) == 0)
294  {
295  return 0;
296  }
297  else
298  {
299  return -1;
300  }
301  }
302  else
303  {
304  return -2;
305  }
306 
307  }
308 
309 }
310 
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.
if(empty($reshook)) $form
View.
Definition: perms.php:103
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.
dol_now($mode='gmt')
Return date for now.
Parent class for product code generators.
get_next_value($db, $mask, $table, $field, $where='', $objsoc='', $date='', $mode='next', $bentityon=true, $objuser=null)
Return last or next value for a mask (according to area we should not reset)
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:1013