dolibarr 19.0.3
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
64 public function info($langs)
65 {
66 global $langs;
67
68 $textinfo = $langs->trans('SimpleNumRefModelDesc', $this->prefix.'0-');
69 $textinfo .= '<br>'.$langs->trans('EachTerminalHasItsOwnCounter');
70
71 return $textinfo;
72 }
73
79 public function getExample()
80 {
81 return $this->prefix.'0-0501-0001'; // TC0-0501-0001
82 }
83
91 public function canBeActivated($object)
92 {
93 global $conf, $langs, $db;
94
95 $pryymm = '';
96 $max = '';
97
98 $pos_source = 0; // POS source = Terminal ID
99
100 // First, we get the max value
101 $posindice = strlen($this->prefix.$pos_source.'-____-') + 1; // So posindice is position after TCX-YYMM-
102
103 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
104 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
105 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix.$pos_source."-____-%")."'";
106 $sql .= " AND entity = ".$conf->entity;
107
108 $resql = $db->query($sql);
109 if ($resql) {
110 $row = $db->fetch_row($resql);
111 if ($row) {
112 $pryymm = substr($row[0], 0, 6);
113 $max = $row[0];
114 }
115 }
116
117 if (!$pryymm || preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $pryymm)) {
118 return true;
119 } else {
120 $langs->load("errors");
121 $this->error = $langs->trans('ErrorNumRefModel', $max);
122 return false;
123 }
124 }
125
137 public function getNextValue($objsoc = null, $invoice = null, $mode = 'next')
138 {
139 global $db;
140
141 $pos_source = is_object($invoice) && $invoice->pos_source > 0 ? $invoice->pos_source : 0; // POS source = Terminal ID
142
143 // First, we get the max value
144 $posindice = strlen($this->prefix.$pos_source.'-____-') + 1; // So posindice is position after TCX-YYMM-
145 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
146 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
147 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix.$pos_source."-____-%")."'";
148 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
149 //$sql .= " and module_source = 'takepos'";
150
151 $resql = $db->query($sql);
152 if ($resql) {
153 $obj = $db->fetch_object($resql);
154 if ($obj) {
155 $max = intval($obj->max);
156 } else {
157 $max = 0;
158 }
159 } else {
160 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
161 return -1;
162 }
163
164 if ($mode == 'last') {
165 if ($max >= (pow(10, 4) - 1)) {
166 $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
167 } else {
168 $num = sprintf("%04s", $max);
169 }
170
171 $ref = '';
172 $sql = "SELECT ref as ref";
173 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
174 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix.$pos_source."-____-".$num)."'";
175 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
176 $sql .= " ORDER BY ref DESC";
177
178 $resql = $db->query($sql);
179 if ($resql) {
180 $obj = $db->fetch_object($resql);
181 if ($obj) {
182 $ref = $obj->ref;
183 }
184 } else {
185 dol_print_error($db);
186 }
187
188 return $ref;
189 } elseif ($mode == 'next') {
190 $date = $invoice->date; // This is invoice date (not creation date)
191 $yymm = dol_print_date($date, "%y%m");
192
193 if ($max >= (pow(10, 4) - 1)) {
194 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
195 } else {
196 $num = sprintf("%04s", $max + 1);
197 }
198
199 dol_syslog(get_class($this)."::getNextValue return ".$this->prefix.$pos_source.'-'.$yymm.'-'.$num);
200 return $this->prefix.$pos_source.'-'.$yymm.'-'.$num;
201 } else {
202 dol_print_error('', 'Bad parameter for getNextValue');
203 return -1;
204 }
205 }
206
214 public function getNumRef($objsoc, $objforref)
215 {
216 return $this->getNextValue($objsoc, $objforref);
217 }
218}
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.
info($langs)
Return description of numbering module.
canBeActivated($object)
Test if the numbers already in the database do not cause any conflicts that will prevent this of conf...
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...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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.