dolibarr  13.0.2
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  return $langs->trans('SimpleNumRefModelDesc', $this->prefix.'0-');
67  }
68 
74  public function getExample()
75  {
76  return $this->prefix.'0-0501-0001';
77  }
78 
85  public function canBeActivated()
86  {
87  global $conf, $langs, $db;
88 
89  $pryymm = '';
90  $max = '';
91 
92  $pos_source = 0;
93 
94  // First, we get the max value
95  $posindice = strlen($this->prefix.$pos_source.'-____-') + 1;
96 
97  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
98  $sql .= " FROM ".MAIN_DB_PREFIX."facture";
99  $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
100  $sql .= " AND entity = ".$conf->entity;
101 
102  $resql = $db->query($sql);
103  if ($resql) {
104  $row = $db->fetch_row($resql);
105  if ($row) {
106  $pryymm = substr($row[0], 0, 6);
107  $max = $row[0];
108  }
109  }
110 
111  if (!$pryymm || preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $pryymm)) {
112  return true;
113  } else {
114  $langs->load("errors");
115  $this->error = $langs->trans('ErrorNumRefModel', $max);
116  return false;
117  }
118  }
119 
128  public function getNextValue($objsoc = null, $invoice = null, $mode = 'next')
129  {
130  global $db;
131 
132  $pos_source = is_object($invoice) && $invoice->pos_source > 0 ? $invoice->pos_source : 0;
133 
134  // First, we get the max value
135  $posindice = strlen($this->prefix.$pos_source.'-____-') + 1;
136  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
137  $sql .= " FROM ".MAIN_DB_PREFIX."facture";
138  $sql .= " WHERE ref LIKE '".$db->escape($this->prefix.$pos_source)."-____-%'";
139  $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
140 
141  $resql = $db->query($sql);
142  if ($resql) {
143  $obj = $db->fetch_object($resql);
144  if ($obj) $max = intval($obj->max);
145  else $max = 0;
146  } else {
147  dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
148  return -1;
149  }
150 
151  if ($mode == 'last')
152  {
153  if ($max >= (pow(10, 4) - 1)) $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
154  else $num = sprintf("%04s", $max);
155 
156  $ref = '';
157  $sql = "SELECT ref as ref";
158  $sql .= " FROM ".MAIN_DB_PREFIX."facture";
159  $sql .= " WHERE ref LIKE '".$db->escape($this->prefix.$pos_source)."-____-".$num."'";
160  $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
161  $sql .= " ORDER BY ref DESC";
162 
163  $resql = $db->query($sql);
164  if ($resql) {
165  $obj = $db->fetch_object($resql);
166  if ($obj) $ref = $obj->ref;
167  } else dol_print_error($db);
168 
169  return $ref;
170  } elseif ($mode == 'next')
171  {
172  $date = $invoice->date; // This is invoice date (not creation date)
173  $yymm = strftime("%y%m", $date);
174 
175  if ($max >= (pow(10, 4) - 1)) $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
176  else $num = sprintf("%04s", $max + 1);
177 
178  dol_syslog(get_class($this)."::getNextValue return ".$this->prefix.$pos_source.'-'.$yymm.'-'.$num);
179  return $this->prefix.$pos_source.'-'.$yymm.'-'.$num;
180  } else dol_print_error('', 'Bad parameter for getNextValue');
181  }
182 
190  public function getNumRef($objsoc, $objforref)
191  {
192  return $this->getNextValue($objsoc, $objforref);
193  }
194 }
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname= '')
Make an include_once using default root and alternate root if it fails.
getNextValue($objsoc=null, $invoice=null, $mode= 'next')
Return next value.
canBeActivated()
Test si les numeros deja en vigueur dans la base ne provoquent pas de de conflits qui empechera cette...
info()
Return description of numbering module.
Class to manage ref numbering of takepos cards with rule Simple.
Classe mere des modeles de numerotation des tickets de caisse.
getExample()
Return an example of numbering module values.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
getNumRef($objsoc, $objforref)
Return next free value.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getNextValue()
Renvoi prochaine valeur attribuee.