dolibarr  13.0.2
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 <https://www.gnu.org/licenses/>.
18  * or see https://www.gnu.org/
19  */
20 
27 require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
28 
29 
34 {
38  public $name = 'Monkey';
39 
40  public $code_modifiable; // Code modifiable
41 
42  public $code_modifiable_invalide; // Code modifiable si il est invalide
43 
44  public $code_modifiable_null; // Code modifiables si il est null
45 
46  public $code_null; // Code facultatif
47 
52  public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
53 
57  public $code_auto;
58 
59  public $prefixcustomer = 'CU';
60 
61  public $prefixsupplier = 'SU';
62 
63  public $prefixIsRequired; // Le champ prefix du tiers doit etre renseigne quand on utilise {pre}
64 
65 
69  public function __construct()
70  {
71  $this->nom = "Monkey";
72  $this->name = "Monkey";
73  $this->version = "dolibarr";
74  $this->code_null = 1;
75  $this->code_modifiable = 1;
76  $this->code_modifiable_invalide = 1;
77  $this->code_modifiable_null = 1;
78  $this->code_auto = 1;
79  $this->prefixIsRequired = 0;
80  }
81 
82 
89  public function info($langs)
90  {
91  return $langs->trans("MonkeyNumRefModelDesc", $this->prefixcustomer, $this->prefixsupplier);
92  }
93 
94 
103  public function getExample($langs, $objsoc = 0, $type = -1)
104  {
105  return $this->prefixcustomer.'0901-00001<br>'.$this->prefixsupplier.'0901-00001';
106  }
107 
108 
116  public function getNextValue($objsoc = 0, $type = -1)
117  {
118  global $db, $conf, $mc;
119 
120  $field = '';
121  $prefix = '';
122  if ($type == 0) {
123  $field = 'code_client';
124  $prefix = $this->prefixcustomer;
125  } elseif ($type == 1) {
126  $field = 'code_fournisseur';
127  $prefix = $this->prefixsupplier;
128  } else {
129  return -1;
130  }
131 
132  // First, we get the max value (reponse immediate car champ indexe)
133  $posindice = strlen($prefix) + 6;
134  $sql = "SELECT MAX(CAST(SUBSTRING(".$field." FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
135  $sql .= " FROM ".MAIN_DB_PREFIX."societe";
136  $sql .= " WHERE ".$field." LIKE '".$db->escape($prefix)."____-%'";
137  $sql .= " AND entity IN (".getEntity('societe').")";
138 
139  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
140 
141  $resql = $db->query($sql);
142  if ($resql)
143  {
144  $obj = $db->fetch_object($resql);
145  if ($obj) $max = intval($obj->max);
146  else $max = 0;
147  } else {
148  return -1;
149  }
150 
151  $date = dol_now();
152  $yymm = strftime("%y%m", $date);
153 
154  if ($max >= (pow(10, 5) - 1)) $num = $max + 1; // If counter > 99999, we do not format on 5 chars, we take number as it is
155  else $num = sprintf("%05s", $max + 1);
156 
157  dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
158  return $prefix.$yymm."-".$num;
159  }
160 
161 
175  public function verif($db, &$code, $soc, $type)
176  {
177  global $conf;
178 
179  $result = 0;
180  $code = strtoupper(trim($code));
181 
182  if (empty($code) && $this->code_null && empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED))
183  {
184  $result = 0;
185  } elseif (empty($code) && (!$this->code_null || !empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED)))
186  {
187  $result = -2;
188  } else {
189  if ($this->verif_syntax($code) >= 0)
190  {
191  $is_dispo = $this->verif_dispo($db, $code, $soc, $type);
192  if ($is_dispo <> 0)
193  {
194  $result = -3;
195  } else {
196  $result = 0;
197  }
198  } else {
199  if (dol_strlen($code) == 0)
200  {
201  $result = -2;
202  } else {
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 
213  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
223  public function verif_dispo($db, $code, $soc, $type = 0)
224  {
225  // phpcs:enable
226  global $conf, $mc;
227 
228  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
229  if ($type == 1) $sql .= " WHERE code_fournisseur = '".$db->escape($code)."'";
230  else $sql .= " WHERE code_client = '".$db->escape($code)."'";
231  $sql .= " AND entity IN (".getEntity('societe').")";
232  if ($soc->id > 0) $sql .= " AND rowid <> ".$soc->id;
233 
234  dol_syslog(get_class($this)."::verif_dispo", LOG_DEBUG);
235  $resql = $db->query($sql);
236  if ($resql)
237  {
238  if ($db->num_rows($resql) == 0)
239  {
240  return 0;
241  } else {
242  return -1;
243  }
244  } else {
245  return -2;
246  }
247  }
248 
249 
250  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
257  public function verif_syntax($code)
258  {
259  // phpcs:enable
260  $res = 0;
261 
262  if (dol_strlen($code) < 11)
263  {
264  $res = -1;
265  } else {
266  $res = 0;
267  }
268  return $res;
269  }
270 }
verif($db, &$code, $soc, $type)
Check validity of code according to its rules.
info($langs)
Return description of module.
dol_now($mode= 'auto')
Return date for now.
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:108
Parent class for third parties code generators.
verif_dispo($db, $code, $soc, $type=0)
Renvoi si un code est pris ou non (par autre tiers)
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
if(!empty($arrayfields['s.nom']['checked'])) print_liste_field_titre($arrayfields['s.nom']['label'] s nom
Definition: list.php:560
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
Classe permettant la gestion monkey des codes tiers.
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
getExample($langs, $objsoc=0, $type=-1)
Return an example of result returned by getNextValue.
verif_syntax($code)
Renvoi si un code respecte la syntaxe.