dolibarr  17.0.4
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  $obj = $db->fetch_object($resql);
144  if ($obj) {
145  $max = intval($obj->max);
146  } else {
147  $max = 0;
148  }
149  } else {
150  return -1;
151  }
152 
153  $date = dol_now();
154  $yymm = dol_print_date($date, "%y%m", 'tzuserrel');
155 
156  if ($max >= (pow(10, 5) - 1)) {
157  $num = $max + 1; // If counter > 99999, we do not format on 5 chars, we take number as it is
158  } else {
159  $num = sprintf("%05s", $max + 1);
160  }
161 
162  dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
163  return $prefix.$yymm."-".$num;
164  }
165 
166 
180  public function verif($db, &$code, $soc, $type)
181  {
182  global $conf;
183 
184  $result = 0;
185  $code = strtoupper(trim($code));
186 
187  if (empty($code) && $this->code_null && empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED)) {
188  $result = 0;
189  } elseif (empty($code) && (!$this->code_null || !empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED))) {
190  $result = -2;
191  } else {
192  if ($this->verif_syntax($code) >= 0) {
193  $is_dispo = $this->verif_dispo($db, $code, $soc, $type);
194  if ($is_dispo <> 0) {
195  $result = -3;
196  } else {
197  $result = 0;
198  }
199  } else {
200  if (dol_strlen($code) == 0) {
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) {
230  $sql .= " WHERE code_fournisseur = '".$db->escape($code)."'";
231  } else {
232  $sql .= " WHERE code_client = '".$db->escape($code)."'";
233  }
234  $sql .= " AND entity IN (".getEntity('societe').")";
235  if ($soc->id > 0) {
236  $sql .= " AND rowid <> ".$soc->id;
237  }
238 
239  dol_syslog(get_class($this)."::verif_dispo", LOG_DEBUG);
240  $resql = $db->query($sql);
241  if ($resql) {
242  if ($db->num_rows($resql) == 0) {
243  return 0;
244  } else {
245  return -1;
246  }
247  } else {
248  return -2;
249  }
250  }
251 
252 
253  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
260  public function verif_syntax($code)
261  {
262  // phpcs:enable
263  $res = 0;
264 
265  if (dol_strlen($code) < 11) {
266  $res = -1;
267  } else {
268  $res = 0;
269  }
270  return $res;
271  }
272 }
Parent class for third parties code generators.
Classe permettant la gestion monkey des codes tiers.
verif_syntax($code)
Renvoi si un code respecte la syntaxe.
getExample($langs, $objsoc=0, $type=-1)
Return an example of result returned by getNextValue.
info($langs)
Return description of module.
verif_dispo($db, $code, $soc, $type=0)
Renvoi si un code est pris ou non (par autre tiers)
verif($db, &$code, $soc, $type)
Check validity of code according to its rules.
getNextValue($objsoc=0, $type=-1)
Return next value.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:122