dolibarr  7.0.0-beta
mod_codeclient_monkey.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2006-2007 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2006-2012 Regis Houssin <regis.houssin@capnetworks.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * or see http://www.gnu.org/
19  */
20 
27 require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
28 
29 
34 {
35  var $nom='Monkey'; // Nom du modele
36  var $name='Monkey'; // Nom du modele
37  var $code_modifiable; // Code modifiable
38  var $code_modifiable_invalide; // Code modifiable si il est invalide
39  var $code_modifiable_null; // Code modifiables si il est null
40  var $code_null; // Code facultatif
41  var $version='dolibarr'; // 'development', 'experimental', 'dolibarr'
42  var $code_auto; // Numerotation automatique
43 
44  var $prefixcustomer='CU';
45  var $prefixsupplier='SU';
46  var $prefixIsRequired; // Le champ prefix du tiers doit etre renseigne quand on utilise {pre}
47 
48 
52  function __construct()
53  {
54  $this->nom = "Monkey";
55  $this->name = "Monkey";
56  $this->version = "dolibarr";
57  $this->code_null = 1;
58  $this->code_modifiable = 1;
59  $this->code_modifiable_invalide = 1;
60  $this->code_modifiable_null = 1;
61  $this->code_auto = 1;
62  $this->prefixIsRequired = 0;
63  }
64 
65 
71  function info($langs)
72  {
73  return $langs->trans("MonkeyNumRefModelDesc",$this->prefixcustomer,$this->prefixsupplier);
74  }
75 
76 
85  function getExample($langs,$objsoc=0,$type=-1)
86  {
87  return $this->prefixcustomer.'0901-0001<br>'.$this->prefixsupplier.'0901-0001';
88  }
89 
90 
98  function getNextValue($objsoc=0,$type=-1)
99  {
100  global $db, $conf, $mc;
101 
102  $return='000001';
103 
104  $field='';$where='';
105  if ($type == 0)
106  {
107  $field = 'code_client';
108  //$where = ' AND client in (1,2)';
109  }
110  else if ($type == 1)
111  {
112  $field = 'code_fournisseur';
113  //$where = ' AND fournisseur = 1';
114  }
115  else return -1;
116 
117 
118  if ($type == 0) $prefix=$this->prefixcustomer;
119  if ($type == 1) $prefix=$this->prefixsupplier;
120 
121  // D'abord on recupere la valeur max (reponse immediate car champ indexe)
122  $posindice=8;
123  $sql = "SELECT MAX(CAST(SUBSTRING(".$field." FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
124  $sql.= " FROM ".MAIN_DB_PREFIX."societe";
125  $sql.= " WHERE ".$field." LIKE '".$prefix."____-%'";
126  $sql.= " AND entity IN (".getEntity('societe').")";
127 
128  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
129 
130  $resql=$db->query($sql);
131  if ($resql)
132  {
133  $obj = $db->fetch_object($resql);
134  if ($obj) $max = intval($obj->max);
135  else $max=0;
136  }
137  else
138  {
139  return -1;
140  }
141 
142  $date = dol_now();
143  $yymm = strftime("%y%m",$date);
144 
145  if ($max >= (pow(10, 4) - 1)) $num=$max+1; // If counter > 9999, we do not format on 4 chars, we take number as it is
146  else $num = sprintf("%04s",$max+1);
147 
148  dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
149  return $prefix.$yymm."-".$num;
150  }
151 
152 
166  function verif($db, &$code, $soc, $type)
167  {
168  global $conf;
169 
170  $result=0;
171  $code = strtoupper(trim($code));
172 
173  if (empty($code) && $this->code_null && empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED))
174  {
175  $result=0;
176  }
177  else if (empty($code) && (! $this->code_null || ! empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED)) )
178  {
179  $result=-2;
180  }
181  else
182  {
183  if ($this->verif_syntax($code) >= 0)
184  {
185  $is_dispo = $this->verif_dispo($db, $code, $soc, $type);
186  if ($is_dispo <> 0)
187  {
188  $result=-3;
189  }
190  else
191  {
192  $result=0;
193  }
194  }
195  else
196  {
197  if (dol_strlen($code) == 0)
198  {
199  $result=-2;
200  }
201  else
202  {
203  $result=-1;
204  }
205  }
206  }
207 
208  dol_syslog(get_class($this)."::verif code=".$code." type=".$type." result=".$result);
209  return $result;
210  }
211 
212 
222  function verif_dispo($db, $code, $soc, $type=0)
223  {
224  global $conf, $mc;
225 
226  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
227  if ($type == 1) $sql.= " WHERE code_fournisseur = '".$code."'";
228  else $sql.= " WHERE code_client = '".$code."'";
229  $sql.= " AND entity IN (".getEntity('societe').")";
230  if ($soc->id > 0) $sql.= " AND rowid <> ".$soc->id;
231 
232  dol_syslog(get_class($this)."::verif_dispo", LOG_DEBUG);
233  $resql=$db->query($sql);
234  if ($resql)
235  {
236  if ($db->num_rows($resql) == 0)
237  {
238  return 0;
239  }
240  else
241  {
242  return -1;
243  }
244  }
245  else
246  {
247  return -2;
248  }
249  }
250 
251 
258  function verif_syntax($code)
259  {
260  $res = 0;
261 
262  if (dol_strlen($code) < 11)
263  {
264  $res = -1;
265  }
266  else
267  {
268  $res = 0;
269  }
270  return $res;
271  }
272 
273 }
274 
verif($db, &$code, $soc, $type)
Check validity of code according to its rules.
info($langs)
Return description of module.
getNextValue($objsoc=0, $type=-1)
Return next value.
Parent class for third parties code generators.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
verif_dispo($db, $code, $soc, $type=0)
Renvoi si un code est pris ou non (par autre tiers)
dol_now($mode='gmt')
Return date for now.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:104
Classe permettant la gestion monkey des codes tiers.
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
getExample($langs, $objsoc=0, $type=-1)
Return an example of result returned by getNextValue.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
verif_syntax($code)
Renvoi si un code respecte la syntaxe.