dolibarr  16.0.5
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 <http://www.gnu.org/licenses/>.
19  * or see http://www.gnu.org/
20  */
21 
27 dol_include_once('/core/modules/takepos/modules_takepos.php');
28 
33 {
38  public $version = 'dolibarr';
39 
44  public $prefix = 'TC';
45 
49  public $error = '';
50 
55  public $nom = 'Simple';
56 
62  public function info()
63  {
64  global $langs;
65 
66  $textinfo = $langs->trans('SimpleNumRefModelDesc', $this->prefix.'0-');
67  $textinfo .= '<br>'.$langs->trans('EachTerminalHasItsOwnCounter');
68 
69  return $textinfo;
70  }
71 
77  public function getExample()
78  {
79  return $this->prefix.'0-0501-0001'; // TC0-0501-0001
80  }
81 
88  public function canBeActivated()
89  {
90  global $conf, $langs, $db;
91 
92  $pryymm = '';
93  $max = '';
94 
95  $pos_source = 0; // POS source = Terminal ID
96 
97  // First, we get the max value
98  $posindice = strlen($this->prefix.$pos_source.'-____-') + 1; // So posindice is position after TCX-YYMM-
99 
100  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
101  $sql .= " FROM ".MAIN_DB_PREFIX."facture";
102  $sql .= " WHERE ref LIKE '".$db->escape($this->prefix.$pos_source."-____-%")."'";
103  $sql .= " AND entity = ".$conf->entity;
104 
105  $resql = $db->query($sql);
106  if ($resql) {
107  $row = $db->fetch_row($resql);
108  if ($row) {
109  $pryymm = substr($row[0], 0, 6);
110  $max = $row[0];
111  }
112  }
113 
114  if (!$pryymm || preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $pryymm)) {
115  return true;
116  } else {
117  $langs->load("errors");
118  $this->error = $langs->trans('ErrorNumRefModel', $max);
119  return false;
120  }
121  }
122 
134  public function getNextValue($objsoc = null, $invoice = null, $mode = 'next')
135  {
136  global $db;
137 
138  $pos_source = is_object($invoice) && $invoice->pos_source > 0 ? $invoice->pos_source : 0; // POS source = Terminal ID
139 
140  // First, we get the max value
141  $posindice = strlen($this->prefix.$pos_source.'-____-') + 1; // So posindice is position after TCX-YYMM-
142  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
143  $sql .= " FROM ".MAIN_DB_PREFIX."facture";
144  $sql .= " WHERE ref LIKE '".$db->escape($this->prefix.$pos_source."-____-%")."'";
145  $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
146  //$sql .= " and module_source = 'takepos'";
147 
148  $resql = $db->query($sql);
149  if ($resql) {
150  $obj = $db->fetch_object($resql);
151  if ($obj) {
152  $max = intval($obj->max);
153  } else {
154  $max = 0;
155  }
156  } else {
157  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
158  return -1;
159  }
160 
161  if ($mode == 'last') {
162  if ($max >= (pow(10, 4) - 1)) {
163  $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
164  } else {
165  $num = sprintf("%04s", $max);
166  }
167 
168  $ref = '';
169  $sql = "SELECT ref as ref";
170  $sql .= " FROM ".MAIN_DB_PREFIX."facture";
171  $sql .= " WHERE ref LIKE '".$db->escape($this->prefix.$pos_source."-____-".$num)."'";
172  $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
173  $sql .= " ORDER BY ref DESC";
174 
175  $resql = $db->query($sql);
176  if ($resql) {
177  $obj = $db->fetch_object($resql);
178  if ($obj) {
179  $ref = $obj->ref;
180  }
181  } else {
182  dol_print_error($db);
183  }
184 
185  return $ref;
186  } elseif ($mode == 'next') {
187  $date = $invoice->date; // This is invoice date (not creation date)
188  $yymm = strftime("%y%m", $date);
189 
190  if ($max >= (pow(10, 4) - 1)) {
191  $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
192  } else {
193  $num = sprintf("%04s", $max + 1);
194  }
195 
196  dol_syslog(get_class($this)."::getNextValue return ".$this->prefix.$pos_source.'-'.$yymm.'-'.$num);
197  return $this->prefix.$pos_source.'-'.$yymm.'-'.$num;
198  } else {
199  dol_print_error('', 'Bad parameter for getNextValue');
200  }
201  }
202 
210  public function getNumRef($objsoc, $objforref)
211  {
212  return $this->getNextValue($objsoc, $objforref);
213  }
214 }
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
dol_include_once
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
Definition: functions.lib.php:1033
mod_takepos_ref_simple\canBeActivated
canBeActivated()
Test si les numeros deja en vigueur dans la base ne provoquent pas de de conflits qui empechera cette...
Definition: mod_takepos_ref_simple.php:88
mod_takepos_ref_simple\getExample
getExample()
Return an example of numbering module values.
Definition: mod_takepos_ref_simple.php:77
mod_takepos_ref_simple\info
info()
Return description of numbering module.
Definition: mod_takepos_ref_simple.php:62
mod_takepos_ref_simple\getNumRef
getNumRef($objsoc, $objforref)
Return next free value.
Definition: mod_takepos_ref_simple.php:210
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
mod_takepos_ref_simple\getNextValue
getNextValue($objsoc=null, $invoice=null, $mode='next')
Return next value.
Definition: mod_takepos_ref_simple.php:134
ModeleNumRefTakepos
Classe mere des modeles de numerotation des tickets de caisse.
Definition: modules_takepos.php:34
mod_takepos_ref_simple
Class to manage ref numbering of takepos cards with rule Simple.
Definition: mod_takepos_ref_simple.php:32
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742