dolibarr 20.0.0
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * or see https://www.gnu.org/
22 */
23
30dol_include_once('/core/modules/takepos/modules_takepos.php');
31
36{
41 public $version = 'dolibarr';
42
47 public $prefix = 'TC';
48
52 public $error = '';
53
58 public $nom = 'Simple';
59
66 public function info($langs)
67 {
68 global $langs;
69
70 $textinfo = $langs->trans('SimpleNumRefModelDesc', $this->prefix.'0-');
71 $textinfo .= '<br>'.$langs->trans('EachTerminalHasItsOwnCounter');
72
73 return $textinfo;
74 }
75
81 public function getExample()
82 {
83 return $this->prefix.'0-0501-0001'; // TC0-0501-0001
84 }
85
93 public function canBeActivated($object)
94 {
95 global $conf, $langs, $db;
96
97 $pryymm = '';
98 $max = '';
99
100 $pos_source = 0; // POS source = Terminal ID
101
102 // First, we get the max value
103 $posindice = strlen($this->prefix.$pos_source.'-____-') + 1; // So posindice is position after TCX-YYMM-
104
105 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
106 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
107 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix.$pos_source."-____-%")."'";
108 $sql .= " AND entity = ".$conf->entity;
109
110 $resql = $db->query($sql);
111 if ($resql) {
112 $row = $db->fetch_row($resql);
113 if ($row) {
114 $pryymm = substr($row[0], 0, 6);
115 $max = $row[0];
116 }
117 }
118
119 if (!$pryymm || preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $pryymm)) {
120 return true;
121 } else {
122 $langs->load("errors");
123 $this->error = $langs->trans('ErrorNumRefModel', $max);
124 return false;
125 }
126 }
127
139 public function getNextValue($objsoc = null, $invoice = null, $mode = 'next')
140 {
141 global $db;
142
143 $pos_source = is_object($invoice) && $invoice->pos_source > 0 ? $invoice->pos_source : 0; // POS source = Terminal ID
144
145 // First, we get the max value
146 $posindice = strlen($this->prefix.$pos_source.'-____-') + 1; // So posindice is position after TCX-YYMM-
147 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
148 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
149 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix.$pos_source."-____-%")."'";
150 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
151 //$sql .= " and module_source = 'takepos'";
152
153 $resql = $db->query($sql);
154 if ($resql) {
155 $obj = $db->fetch_object($resql);
156 if ($obj) {
157 $max = intval($obj->max);
158 } else {
159 $max = 0;
160 }
161 } else {
162 dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
163 return -1;
164 }
165
166 if ($mode == 'last') {
167 if ($max >= (pow(10, 4) - 1)) {
168 $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
169 } else {
170 $num = sprintf("%04d", $max);
171 }
172
173 $ref = '';
174 $sql = "SELECT ref as ref";
175 $sql .= " FROM ".MAIN_DB_PREFIX."facture";
176 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix.$pos_source."-____-".$num)."'";
177 $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
178 $sql .= " ORDER BY ref DESC";
179
180 $resql = $db->query($sql);
181 if ($resql) {
182 $obj = $db->fetch_object($resql);
183 if ($obj) {
184 $ref = $obj->ref;
185 }
186 } else {
187 dol_print_error($db);
188 }
189
190 return $ref;
191 } elseif ($mode == 'next') {
192 $date = $invoice->date; // This is invoice date (not creation date)
193 $yymm = dol_print_date($date, "%y%m");
194
195 if ($max >= (pow(10, 4) - 1)) {
196 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
197 } else {
198 $num = sprintf("%04d", $max + 1);
199 }
200
201 dol_syslog(get_class($this)."::getNextValue return ".$this->prefix.$pos_source.'-'.$yymm.'-'.$num);
202 return $this->prefix.$pos_source.'-'.$yymm.'-'.$num;
203 } else {
204 dol_print_error(null, 'Bad parameter for getNextValue');
205 return -1;
206 }
207 }
208
217 public function getNumRef($objsoc, $objforref)
218 {
219 return $this->getNextValue($objsoc, $objforref);
220 }
221}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Parent Class of the models to number the cash register receipts.
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_date($time, $format='', $tzoutput='auto', $outputlangs=null, $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_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.