dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * or see https://www.gnu.org/
21 */
22
29require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
30
31
36{
37 // variables inherited from ModeleThirdPartyCode class
38 public $name = 'Monkey';
39 public $version = 'dolibarr';
40
41 // variables not inherited
42 public $prefixcustomer = 'CU';
43 public $prefixsupplier = 'SU';
44
45
51 public function __construct($db)
52 {
53 $this->db = $db;
54
55 $this->code_null = 1;
56 $this->code_modifiable = 1;
57 $this->code_modifiable_invalide = 1;
58 $this->code_modifiable_null = 1;
59 $this->code_auto = 1;
60 $this->prefixIsRequired = 0;
61 }
62
63
70 public function info($langs)
71 {
72 return $langs->trans("MonkeyNumRefModelDesc", $this->prefixcustomer, $this->prefixsupplier);
73 }
74
75
84 public function getExample($langs, $objsoc = '', $type = -1)
85 {
86 return $this->prefixcustomer.'0901-00001<br>'.$this->prefixsupplier.'0901-00001';
87 }
88
89
97 public function getNextValue($objsoc = '', $type = -1)
98 {
99 global $db;
100
101 $field = '';
102 $prefix = '';
103 if ($type == 0) {
104 $field = 'code_client';
105 $prefix = $this->prefixcustomer;
106 } elseif ($type == 1) {
107 $field = 'code_fournisseur';
108 $prefix = $this->prefixsupplier;
109 } else {
110 return -1;
111 }
112
113 // First, we get the max value (response immediate car champ indexe)
114 $posindice = strlen($prefix) + 6;
115 $sql = "SELECT MAX(CAST(SUBSTRING(".$field." FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
116 $sql .= " FROM ".MAIN_DB_PREFIX."societe";
117 $sql .= " WHERE ".$field." LIKE '".$db->escape($prefix)."____-%'";
118 $sql .= " AND entity IN (".getEntity('societe').")";
119
120 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
121
122 $resql = $db->query($sql);
123 if ($resql) {
124 $obj = $db->fetch_object($resql);
125 if ($obj) {
126 $max = intval($obj->max);
127 } else {
128 $max = 0;
129 }
130 } else {
131 return -1;
132 }
133
134 $date = dol_now();
135 $yymm = dol_print_date($date, "%y%m", 'tzuserrel');
136
137 if ($max >= (pow(10, 5) - 1)) {
138 $num = $max + 1; // If counter > 99999, we do not format on 5 chars, we take number as it is
139 } else {
140 $num = sprintf("%05d", $max + 1);
141 }
142
143 dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
144 return $prefix.$yymm."-".$num;
145 }
146
147
161 public function verif($db, &$code, $soc, $type)
162 {
163 $result = 0;
164 $code = strtoupper(trim($code));
165
166 if (empty($code) && $this->code_null && !getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED')) {
167 $result = 0;
168 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED'))) {
169 $result = -2;
170 } else {
171 if ($this->verif_syntax($code) >= 0) {
172 $is_dispo = $this->verif_dispo($db, $code, $soc, $type);
173 if ($is_dispo != 0) {
174 $result = -3;
175 } else {
176 $result = 0;
177 }
178 } else {
179 if (dol_strlen($code) == 0) {
180 $result = -2;
181 } else {
182 $result = -1;
183 }
184 }
185 }
186
187 dol_syslog(get_class($this)."::verif code=".$code." type=".$type." result=".$result);
188 return $result;
189 }
190
191
192 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
202 public function verif_dispo($db, $code, $soc, $type = 0)
203 {
204 // phpcs:enable
205 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
206 if ($type == 1) {
207 $sql .= " WHERE code_fournisseur = '".$db->escape($code)."'";
208 } else {
209 $sql .= " WHERE code_client = '".$db->escape($code)."'";
210 }
211 $sql .= " AND entity IN (".getEntity('societe').")";
212 if ($soc->id > 0) {
213 $sql .= " AND rowid <> ".$soc->id;
214 }
215
216 dol_syslog(get_class($this)."::verif_dispo", LOG_DEBUG);
217 $resql = $db->query($sql);
218 if ($resql) {
219 if ($db->num_rows($resql) == 0) {
220 return 0;
221 } else {
222 return -1;
223 }
224 } else {
225 return -2;
226 }
227 }
228
229
230 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
237 public function verif_syntax($code)
238 {
239 // phpcs:enable
240 $res = 0;
241
242 if (dol_strlen($code) < 11) {
243 $res = -1;
244 } else {
245 $res = 0;
246 }
247 return $res;
248 }
249}
Parent class for third parties code generators.
Class permettant la gestion monkey des codes tiers.
verif_syntax($code)
Renvoi si un code respecte la syntax.
info($langs)
Return description of module.
verif_dispo($db, $code, $soc, $type=0)
Indicates if the code is available or not (by another third party)
getExample($langs, $objsoc='', $type=-1)
Return an example of result returned by getNextValue.
verif($db, &$code, $soc, $type)
Check validity of code according to its rules.
getNextValue($objsoc='', $type=-1)
Return next value.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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.