dolibarr  9.0.0
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@inodbox.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 {
40  public $nom='Monkey';
41 
45  public $name='Monkey';
46 
47  public $code_modifiable; // Code modifiable
48 
49  public $code_modifiable_invalide; // Code modifiable si il est invalide
50 
51  public $code_modifiable_null; // Code modifiables si il est null
52 
53  public $code_null; // Code facultatif
54 
59  public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
60 
61  public $code_auto; // Numerotation automatique
62 
63  public $prefixcustomer='CU';
64 
65  public $prefixsupplier='SU';
66 
67  public $prefixIsRequired; // Le champ prefix du tiers doit etre renseigne quand on utilise {pre}
68 
69 
73  function __construct()
74  {
75  $this->nom = "Monkey";
76  $this->name = "Monkey";
77  $this->version = "dolibarr";
78  $this->code_null = 1;
79  $this->code_modifiable = 1;
80  $this->code_modifiable_invalide = 1;
81  $this->code_modifiable_null = 1;
82  $this->code_auto = 1;
83  $this->prefixIsRequired = 0;
84  }
85 
86 
92  function info($langs)
93  {
94  return $langs->trans("MonkeyNumRefModelDesc",$this->prefixcustomer,$this->prefixsupplier);
95  }
96 
97 
106  function getExample($langs,$objsoc=0,$type=-1)
107  {
108  return $this->prefixcustomer.'0901-0001<br>'.$this->prefixsupplier.'0901-0001';
109  }
110 
111 
119  function getNextValue($objsoc=0,$type=-1)
120  {
121  global $db, $conf, $mc;
122 
123  $return='000001';
124 
125  $field='';
126  $where='';
127  $prefix = '';
128  if ($type == 0) {
129  $field = 'code_client';
130  $prefix = $this->prefixcustomer;
131  //$where = ' AND client in (1,2)';
132  } elseif ($type == 1) {
133  $field = 'code_fournisseur';
134  $prefix = $this->prefixsupplier;
135  //$where = ' AND fournisseur = 1';
136  } else {
137  return -1;
138  }
139 
140  // D'abord on recupere la valeur max (reponse immediate car champ indexe)
141  $posindice=8;
142  $sql = "SELECT MAX(CAST(SUBSTRING(".$field." FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
143  $sql.= " FROM ".MAIN_DB_PREFIX."societe";
144  $sql.= " WHERE ".$field." LIKE '".$prefix."____-%'";
145  $sql.= " AND entity IN (".getEntity('societe').")";
146 
147  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
148 
149  $resql=$db->query($sql);
150  if ($resql)
151  {
152  $obj = $db->fetch_object($resql);
153  if ($obj) $max = intval($obj->max);
154  else $max=0;
155  }
156  else
157  {
158  return -1;
159  }
160 
161  $date = dol_now();
162  $yymm = strftime("%y%m",$date);
163 
164  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
165  else $num = sprintf("%04s",$max+1);
166 
167  dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
168  return $prefix.$yymm."-".$num;
169  }
170 
171 
185  function verif($db, &$code, $soc, $type)
186  {
187  global $conf;
188 
189  $result=0;
190  $code = strtoupper(trim($code));
191 
192  if (empty($code) && $this->code_null && empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED))
193  {
194  $result=0;
195  }
196  else if (empty($code) && (! $this->code_null || ! empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED)) )
197  {
198  $result=-2;
199  }
200  else
201  {
202  if ($this->verif_syntax($code) >= 0)
203  {
204  $is_dispo = $this->verif_dispo($db, $code, $soc, $type);
205  if ($is_dispo <> 0)
206  {
207  $result=-3;
208  }
209  else
210  {
211  $result=0;
212  }
213  }
214  else
215  {
216  if (dol_strlen($code) == 0)
217  {
218  $result=-2;
219  }
220  else
221  {
222  $result=-1;
223  }
224  }
225  }
226 
227  dol_syslog(get_class($this)."::verif code=".$code." type=".$type." result=".$result);
228  return $result;
229  }
230 
231 
232  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
242  function verif_dispo($db, $code, $soc, $type=0)
243  {
244  // phpcs:enable
245  global $conf, $mc;
246 
247  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
248  if ($type == 1) $sql.= " WHERE code_fournisseur = '".$code."'";
249  else $sql.= " WHERE code_client = '".$code."'";
250  $sql.= " AND entity IN (".getEntity('societe').")";
251  if ($soc->id > 0) $sql.= " AND rowid <> ".$soc->id;
252 
253  dol_syslog(get_class($this)."::verif_dispo", LOG_DEBUG);
254  $resql=$db->query($sql);
255  if ($resql)
256  {
257  if ($db->num_rows($resql) == 0)
258  {
259  return 0;
260  }
261  else
262  {
263  return -1;
264  }
265  }
266  else
267  {
268  return -2;
269  }
270  }
271 
272 
273  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
280  function verif_syntax($code)
281  {
282  // phpcs:enable
283  $res = 0;
284 
285  if (dol_strlen($code) < 11)
286  {
287  $res = -1;
288  }
289  else
290  {
291  $res = 0;
292  }
293  return $res;
294  }
295 }
$version
Dolibarr version of the loaded document string.
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, $soc, $type)
Check validity of code according to its rules.
info($langs)
Return description of module.
getNextValue($objsoc=0, $type=-1)
Return next value.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:103
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)
if(! empty($arrayfields['s.nom']['checked'])) print_liste_field_titre($arrayfields['s.nom']['label'] s nom
Definition: list.php:573
dol_now($mode='gmt')
Return date for now.
Classe permettant la gestion monkey des codes tiers.
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.