dolibarr  20.0.0-beta
mod_ticket_simple.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2010-2012 Regis Houssin <regis.houssin@inodbox.com>
3  * Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
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 
28 require_once DOL_DOCUMENT_ROOT.'/core/modules/ticket/modules_ticket.php';
29 
34 {
39  public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
40 
41  public $prefix = 'TS';
42 
46  public $error = '';
47 
53  public $nom = 'Simple';
54 
58  public $name = 'Simple';
59 
66  public function info($langs)
67  {
68  global $langs;
69  return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
70  }
71 
77  public function getExample()
78  {
79  return $this->prefix."0501-0001";
80  }
81 
89  public function canBeActivated($object)
90  {
91  global $conf, $langs, $db;
92 
93  $coyymm = '';
94  $max = '';
95 
96  $posindice = strlen($this->prefix) + 6;
97  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
98  $sql .= " FROM ".MAIN_DB_PREFIX."ticket";
99  $search = $this->prefix."____-%";
100  $sql .= " WHERE ref LIKE '".$db->escape($search)."'";
101  $sql .= " AND entity = ".$conf->entity;
102  $resql = $db->query($sql);
103  if ($resql) {
104  $row = $db->fetch_row($resql);
105  if ($row) {
106  $coyymm = substr($row[0], 0, 6);
107  $max = $row[0];
108  }
109  }
110  if (!$coyymm || preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
111  return true;
112  } else {
113  $langs->load("errors");
114  $this->error = $langs->trans('ErrorNumRefModel', $max);
115  return false;
116  }
117  }
118 
126  public function getNextValue($objsoc, $ticket)
127  {
128  global $db;
129 
130  // First, we get the max value
131  $posindice = strlen($this->prefix) + 6;
132  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
133  $sql .= " FROM ".MAIN_DB_PREFIX."ticket";
134  $search = $this->prefix."____-%";
135  $sql .= " WHERE ref LIKE '".$db->escape($search)."'";
136  $sql .= " AND entity IN (".getEntity('ticketnumber', 1, $ticket).")";
137 
138  $resql = $db->query($sql);
139  if ($resql) {
140  $obj = $db->fetch_object($resql);
141  if ($obj) {
142  $max = intval($obj->max);
143  } else {
144  $max = 0;
145  }
146  } else {
147  dol_syslog("mod_ticket_simple::getNextValue", LOG_DEBUG);
148  return -1;
149  }
150 
151  $date = empty($ticket->datec) ? dol_now() : $ticket->datec;
152 
153  //$yymm = strftime("%y%m",time());
154  $yymm = dol_print_date($date, "%y%m");
155 
156  if ($max >= (pow(10, 4) - 1)) {
157  $num = $max + 1;
158  } else {
159  // If counter > 9999, we do not format on 4 chars, we take number as it is
160  $num = sprintf("%04d", $max + 1);
161  }
162 
163  dol_syslog("mod_ticket_simple::getNextValue return ".$this->prefix.$yymm."-".$num);
164  return $this->prefix.$yymm."-".$num;
165  }
166 }
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
Parent Class of the project reference numbering model classes.
Class to manage the numbering module Simple for ticket references.
getExample()
Return an example of numbering module values.
getNextValue($objsoc, $ticket)
Return next value.
info($langs)
Return description of numbering module.
canBeActivated($object)
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
dol_now($mode='auto')
Return date for now.
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).
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.