dolibarr 19.0.3
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
27require_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->code_null = 1;
72 $this->code_modifiable = 1;
73 $this->code_modifiable_invalide = 1;
74 $this->code_modifiable_null = 1;
75 $this->code_auto = 1;
76 $this->prefixIsRequired = 0;
77 }
78
79
86 public function info($langs)
87 {
88 return $langs->trans("MonkeyNumRefModelDesc", $this->prefixcustomer, $this->prefixsupplier);
89 }
90
91
100 public function getExample($langs, $objsoc = 0, $type = -1)
101 {
102 return $this->prefixcustomer.'0901-00001<br>'.$this->prefixsupplier.'0901-00001';
103 }
104
105
113 public function getNextValue($objsoc = 0, $type = -1)
114 {
115 global $db, $conf, $mc;
116
117 $field = '';
118 $prefix = '';
119 if ($type == 0) {
120 $field = 'code_client';
121 $prefix = $this->prefixcustomer;
122 } elseif ($type == 1) {
123 $field = 'code_fournisseur';
124 $prefix = $this->prefixsupplier;
125 } else {
126 return -1;
127 }
128
129 // First, we get the max value (reponse immediate car champ indexe)
130 $posindice = strlen($prefix) + 6;
131 $sql = "SELECT MAX(CAST(SUBSTRING(".$field." FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
132 $sql .= " FROM ".MAIN_DB_PREFIX."societe";
133 $sql .= " WHERE ".$field." LIKE '".$db->escape($prefix)."____-%'";
134 $sql .= " AND entity IN (".getEntity('societe').")";
135
136 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
137
138 $resql = $db->query($sql);
139 if ($resql) {
140 $obj = $db->fetch_object($resql);
141 if ($obj) {
142 $max = intval($obj->max);
143 } else {
144 $max = 0;
145 }
146 } else {
147 return -1;
148 }
149
150 $date = dol_now();
151 $yymm = dol_print_date($date, "%y%m", 'tzuserrel');
152
153 if ($max >= (pow(10, 5) - 1)) {
154 $num = $max + 1; // If counter > 99999, we do not format on 5 chars, we take number as it is
155 } else {
156 $num = sprintf("%05s", $max + 1);
157 }
158
159 dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
160 return $prefix.$yymm."-".$num;
161 }
162
163
177 public function verif($db, &$code, $soc, $type)
178 {
179 global $conf;
180
181 $result = 0;
182 $code = strtoupper(trim($code));
183
184 if (empty($code) && $this->code_null && !getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED')) {
185 $result = 0;
186 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED'))) {
187 $result = -2;
188 } else {
189 if ($this->verif_syntax($code) >= 0) {
190 $is_dispo = $this->verif_dispo($db, $code, $soc, $type);
191 if ($is_dispo != 0) {
192 $result = -3;
193 } else {
194 $result = 0;
195 }
196 } else {
197 if (dol_strlen($code) == 0) {
198 $result = -2;
199 } else {
200 $result = -1;
201 }
202 }
203 }
204
205 dol_syslog(get_class($this)."::verif code=".$code." type=".$type." result=".$result);
206 return $result;
207 }
208
209
210 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
220 public function verif_dispo($db, $code, $soc, $type = 0)
221 {
222 // phpcs:enable
223 global $conf, $mc;
224
225 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
226 if ($type == 1) {
227 $sql .= " WHERE code_fournisseur = '".$db->escape($code)."'";
228 } else {
229 $sql .= " WHERE code_client = '".$db->escape($code)."'";
230 }
231 $sql .= " AND entity IN (".getEntity('societe').")";
232 if ($soc->id > 0) {
233 $sql .= " AND rowid <> ".$soc->id;
234 }
235
236 dol_syslog(get_class($this)."::verif_dispo", LOG_DEBUG);
237 $resql = $db->query($sql);
238 if ($resql) {
239 if ($db->num_rows($resql) == 0) {
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 $res = -1;
264 } else {
265 $res = 0;
266 }
267 return $res;
268 }
269}
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.
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.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.