dolibarr 24.0.0-beta
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-2025 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024-2025 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
45 public $prefixcustomer = 'CU';
49 public $prefixsupplier = 'SU';
50
51
57 public function __construct($db)
58 {
59 $this->db = $db;
60
61 $this->code_null = 1;
62 $this->code_modifiable = 1;
63 $this->code_modifiable_invalide = 1;
64 $this->code_modifiable_null = 1;
65 $this->code_auto = 1;
66 $this->prefixIsRequired = 0;
67 }
68
69
76 public function info($langs)
77 {
78 return $langs->trans("MonkeyNumRefModelDesc", $this->prefixcustomer, $this->prefixsupplier);
79 }
80
81
90 public function getExample($langs = null, $objsoc = '', $type = -1)
91 {
92 return $this->prefixcustomer.'0901-00001<br>'.$this->prefixsupplier.'0901-00001';
93 }
94
95
103 public function getNextValue($objsoc = '', $type = -1)
104 {
105 global $db;
106
107 $field = '';
108 $prefix = '';
109 if ($type == 0) {
110 $field = 'code_client';
111 $prefix = $this->prefixcustomer;
112 } elseif ($type == 1) {
113 $field = 'code_fournisseur';
114 $prefix = $this->prefixsupplier;
115 } else {
116 return -1;
117 }
118
119 // First, we get the max value (response immediate car champ indexe)
120 $posindice = strlen($prefix) + 6;
121 $sql = "SELECT MAX(CAST(SUBSTRING(".$db->sanitize($field)." FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
122 $sql .= " FROM ".MAIN_DB_PREFIX."societe";
123 $sql .= " WHERE ".$db->sanitize($field)." LIKE '".$db->escape($prefix)."____-%'";
124 $sql .= " AND entity IN (".getEntity('societe').")";
125
126 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
127
128 $resql = $db->query($sql);
129 if ($resql) {
130 $obj = $db->fetch_object($resql);
131 if ($obj) {
132 $max = intval($obj->max);
133 } else {
134 $max = 0;
135 }
136 } else {
137 return -1;
138 }
139
140 $date = dol_now();
141 $yymm = dol_print_date($date, "%y%m", 'tzuserrel');
142
143 if ($max >= (pow(10, 5) - 1)) {
144 $num = $max + 1; // If counter > 99999, we do not format on 5 chars, we take number as it is
145 } else {
146 $num = sprintf("%05d", $max + 1);
147 }
148
149 dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
150 return $prefix.$yymm."-".$num;
151 }
152
153
169 public function verif($db, &$code, $soc, $type)
170 {
171 $result = 0;
172 $code = strtoupper(trim($code));
173
174 if (empty($code) && $this->code_null && !getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED')) {
175 $result = 0;
176 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED'))) {
177 $result = -2;
178 } else {
179 if ($this->verif_syntax($code) >= 0) {
180 $is_dispo = $this->verif_dispo($db, $code, $soc, $type);
181 if ($is_dispo != 0) {
182 $result = -3;
183 } else {
184 $result = 0;
185 }
186 } else {
187 if (dol_strlen($code) == 0) {
188 $result = -2;
189 } else {
190 $result = -1;
191 }
192 }
193 }
194
195 dol_syslog(get_class($this)."::verif code=".$code." type=".$type." result=".$result);
196 return $result;
197 }
198
199
200 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
210 public function verif_dispo($db, $code, $soc, $type = 0)
211 {
212 // phpcs:enable
213 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
214 if ($type == 1) {
215 $sql .= " WHERE code_fournisseur = '".$db->escape($code)."'";
216 } else {
217 $sql .= " WHERE code_client = '".$db->escape($code)."'";
218 }
219 $sql .= " AND entity IN (".getEntity('societe').")";
220 if ($soc->id > 0) {
221 $sql .= " AND rowid <> ".$soc->id;
222 }
223
224 dol_syslog(get_class($this)."::verif_dispo", LOG_DEBUG);
225 $resql = $db->query($sql);
226 if ($resql) {
227 if ($db->num_rows($resql) == 0) {
228 return 0;
229 } else {
230 return -1;
231 }
232 } else {
233 return -2;
234 }
235 }
236
237
238 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
245 public function verif_syntax($code)
246 {
247 // phpcs:enable
248 $res = 0;
249
250 if (dol_strlen($code) < 11) {
251 $res = -1;
252 } else {
253 $res = 0;
254 }
255 return $res;
256 }
257}
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.
getExample($langs=null, $objsoc='', $type=-1)
Return an example of result returned by getNextValue.
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)
verif($db, &$code, $soc, $type)
Check validity of code according to its rules.
getNextValue($objsoc='', $type=-1)
Return next value.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_now($mode='gmt')
Return date for now.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.