dolibarr  13.0.2
mod_expensereport_jade.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2017 Maxime Kohlhaas <support@atm-consulting.fr>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  * or see https://www.gnu.org/
17  */
18 
24 require_once DOL_DOCUMENT_ROOT.'/core/modules/expensereport/modules_expensereport.php';
25 
30 {
35  public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
36 
37  public $prefix = 'ER';
38 
42  public $error = '';
43 
49  public $nom = 'Jade';
50 
54  public $name = 'Jade';
55 
56 
62  public function info()
63  {
64  global $langs;
65  return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
66  }
67 
68 
74  public function getExample()
75  {
76  return $this->prefix."0501-0001";
77  }
78 
79 
86  public function canBeActivated()
87  {
88  global $conf, $langs, $db;
89 
90  $coyymm = ''; $max = '';
91 
92  $posindice = strlen($this->prefix) + 6;
93  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
94  $sql .= " FROM ".MAIN_DB_PREFIX."expensereport";
95  $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
96  $sql .= " AND entity = ".$conf->entity;
97 
98  $resql = $db->query($sql);
99  if ($resql)
100  {
101  $row = $db->fetch_row($resql);
102  if ($row) { $coyymm = substr($row[0], 0, 6); $max = $row[0]; }
103  }
104  if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm))
105  {
106  $langs->load("errors");
107  $this->error = $langs->trans('ErrorNumRefModel', $max);
108  return false;
109  }
110 
111  return true;
112  }
113 
120  public function getNextValue($object)
121  {
122  global $db, $conf;
123 
124  // For backward compatibility and restore old behavior to get ref of expense report
125  if (!empty($conf->global->EXPENSEREPORT_USE_OLD_NUMBERING_RULE))
126  {
127  $fuser = null;
128  if ($object->fk_user_author > 0)
129  {
130  $fuser = new User($db);
131  $fuser->fetch($object->fk_user_author);
132  }
133 
134  $expld_car = (empty($conf->global->NDF_EXPLODE_CHAR)) ? "-" : $conf->global->NDF_EXPLODE_CHAR;
135  $num_car = (empty($conf->global->NDF_NUM_CAR_REF)) ? "5" : $conf->global->NDF_NUM_CAR_REF;
136 
137  $sql = 'SELECT MAX(de.ref_number_int) as max';
138  $sql .= ' FROM '.MAIN_DB_PREFIX.'expensereport de';
139 
140  $result = $db->query($sql);
141 
142  if ($db->num_rows($result) > 0) {
143  $objp = $db->fetch_object($result);
144  $newref = $objp->max;
145  $newref++;
146  while (strlen($newref) < $num_car) {
147  $newref = "0".$newref;
148  }
149  } else {
150  $newref = 1;
151  while (strlen($newref) < $num_car) {
152  $newref = "0".$newref;
153  }
154  }
155 
156  $ref_number_int = ($newref + 1) - 1;
157 
158  $user_author_infos = dolGetFirstLastname($fuser->firstname, $fuser->lastname);
159 
160  $prefix = "ER";
161  if (!empty($conf->global->EXPENSE_REPORT_PREFIX)) $prefix = $conf->global->EXPENSE_REPORT_PREFIX;
162  $newref = str_replace(' ', '_', $user_author_infos).$expld_car.$prefix.$newref.$expld_car.dol_print_date($object->date_debut, '%y%m%d');
163 
164  $sqlbis = 'UPDATE '.MAIN_DB_PREFIX.'expensereport SET ref_number_int = '.$ref_number_int.' WHERE rowid = '.$object->id;
165  $resqlbis = $db->query($sqlbis);
166  if (!$resqlbis)
167  {
168  dol_print_error($resqlbis);
169  exit;
170  }
171 
172  dol_syslog("mod_expensereport_jade::getNextValue return ".$newref);
173  return $newref;
174  }
175 
176  // First we get the max value
177  $posindice = strlen($this->prefix) + 6;
178  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
179  $sql .= " FROM ".MAIN_DB_PREFIX."expensereport";
180  $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
181  $sql .= " AND entity = ".$conf->entity;
182 
183  $resql = $db->query($sql);
184  if ($resql)
185  {
186  $obj = $db->fetch_object($resql);
187  if ($obj) $max = intval($obj->max);
188  else $max = 0;
189  } else {
190  dol_syslog("mod_expensereport_jade::getNextValue", LOG_DEBUG);
191  return 0;
192  }
193 
194  $date = $object->date_valid; // $object->date does not exists
195  if (empty($date))
196  {
197  $this->error = 'Date valid not defined';
198  return 0;
199  }
200 
201  $yymm = strftime("%y%m", $date);
202 
203  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
204  else $num = sprintf("%04s", $max + 1);
205 
206  dol_syslog("mod_expensereport_jade::getNextValue return ".$this->prefix.$yymm."-".$num);
207  return $this->prefix.$yymm."-".$num;
208  }
209 }
Class to manage expensereport numbering rules Jade.
Class to manage Dolibarr users.
Definition: user.class.php:44
canBeActivated()
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
Parent class for numbering masks of expense reports.
getNextValue($object)
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...
info()
Return description of numbering model.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
getExample()
Returns an example of numbering.