dolibarr 18.0.6
mod_takepos_ref_simple.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2005-2008 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
4 * Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
5 * Copyright (C) 2020 Open-DSI <support@open-dsi.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * or see https://www.gnu.org/
20 */
21
28dol_include_once('/core/modules/takepos/modules_takepos.php');
29
34{
39 public $version = 'dolibarr';
40
45 public $prefix = 'TC';
46
50 public $error = '';
51
56 public $nom = 'Simple';
57
63 public function info()
64 {
65 global $langs;
66
67 $textinfo = $langs->trans('SimpleNumRefModelDesc', $this->prefix.'0-');
68 $textinfo .= '<br>'.$langs->trans('EachTerminalHasItsOwnCounter');
69
70 return $textinfo;
71 }
72
78 public function getExample()
79 {
80 return $this->prefix.'0-0501-0001'; // TC0-0501-0001
81 }
82
89 public function canBeActivated()
90 {
91 global $conf, $langs, $db;
92
93 $pryymm = '';
94 $max = '';
95
96 $pos_source = 0; // POS source = Terminal ID
97
98 // First, we get the max value
99 $posindice = strlen($this->prefix.$pos_source.'-____-') + 1; // So posindice is position after TCX-YYMM-
100
101 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
102 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
103 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix.$pos_source."-____-%")."'";
104 $sql .= " AND entity = ".$conf->entity;
105
106 $resql = $db->query($sql);
107 if ($resql) {
108 $row = $db->fetch_row($resql);
109 if ($row) {
110 $pryymm = substr($row[0], 0, 6);
111 $max = $row[0];
112 }
113 }
114
115 if (!$pryymm || preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $pryymm)) {
116 return true;
117 } else {
118 $langs->load("errors");
119 $this->error = $langs->trans('ErrorNumRefModel', $max);
120 return false;
121 }
122 }
123
135 public function getNextValue($objsoc = null, $invoice = null, $mode = 'next')
136 {
137 global $db;
138
139 $pos_source = is_object($invoice) && $invoice->pos_source > 0 ? $invoice->pos_source : 0; // POS source = Terminal ID
140
141 // First, we get the max value
142 $posindice = strlen($this->prefix.$pos_source.'-____-') + 1; // So posindice is position after TCX-YYMM-
143 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
144 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
145 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix.$pos_source."-____-%")."'";
146 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
147 //$sql .= " and module_source = 'takepos'";
148
149 $resql = $db->query($sql);
150 if ($resql) {
151 $obj = $db->fetch_object($resql);
152 if ($obj) {
153 $max = intval($obj->max);
154 } else {
155 $max = 0;
156 }
157 } else {
158 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
159 return -1;
160 }
161
162 if ($mode == 'last') {
163 if ($max >= (pow(10, 4) - 1)) {
164 $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
165 } else {
166 $num = sprintf("%04s", $max);
167 }
168
169 $ref = '';
170 $sql = "SELECT ref as ref";
171 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
172 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix.$pos_source."-____-".$num)."'";
173 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
174 $sql .= " ORDER BY ref DESC";
175
176 $resql = $db->query($sql);
177 if ($resql) {
178 $obj = $db->fetch_object($resql);
179 if ($obj) {
180 $ref = $obj->ref;
181 }
182 } else {
183 dol_print_error($db);
184 }
185
186 return $ref;
187 } elseif ($mode == 'next') {
188 $date = $invoice->date; // This is invoice date (not creation date)
189 $yymm = strftime("%y%m", $date);
190
191 if ($max >= (pow(10, 4) - 1)) {
192 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
193 } else {
194 $num = sprintf("%04s", $max + 1);
195 }
196
197 dol_syslog(get_class($this)."::getNextValue return ".$this->prefix.$pos_source.'-'.$yymm.'-'.$num);
198 return $this->prefix.$pos_source.'-'.$yymm.'-'.$num;
199 } else {
200 dol_print_error('', 'Bad parameter for getNextValue');
201 return -1;
202 }
203 }
204
212 public function getNumRef($objsoc, $objforref)
213 {
214 return $this->getNextValue($objsoc, $objforref);
215 }
216}
Classe mere des modeles de numerotation des tickets de caisse.
Class to manage ref numbering of takepos cards with rule Simple.
getNextValue($objsoc=null, $invoice=null, $mode='next')
Return next value.
canBeActivated()
Test if the numbers already in the database do not cause any conflicts that will prevent this of conf...
info()
Return description of numbering module.
getNumRef($objsoc, $objforref)
Return next free value.
getExample()
Return an example of numbering module values.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.