dolibarr  17.0.4
mod_knowledgerecord_standard.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  * or see https://www.gnu.org/
18  */
19 
25 dol_include_once('/knowledgemanagement/core/modules/knowledgemanagement/modules_knowledgerecord.php');
26 
27 
32 {
37  public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
38 
39  public $prefix = 'KM';
40 
44  public $error = '';
45 
49  public $name = 'standard';
50 
51 
57  public function info()
58  {
59  global $langs;
60  return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
61  }
62 
63 
69  public function getExample()
70  {
71  return $this->prefix."0501-0001";
72  }
73 
74 
82  public function canBeActivated($object)
83  {
84  global $conf, $langs, $db;
85 
86  $coyymm = ''; $max = '';
87 
88  $posindice = strlen($this->prefix) + 6;
89  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
90  $sql .= " FROM ".MAIN_DB_PREFIX."knowledgemanagement_knowledgerecord";
91  $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
92  if ($object->ismultientitymanaged == 1) {
93  $sql .= " AND entity = ".$conf->entity;
94  } elseif ($object->ismultientitymanaged == 2) {
95  // TODO
96  }
97 
98  $resql = $db->query($sql);
99  if ($resql) {
100  $row = $db->fetch_row($resql);
101  if ($row) {
102  $coyymm = substr($row[0], 0, 6); $max = $row[0];
103  }
104  }
105  if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
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  // first we get the max value
125  $posindice = strlen($this->prefix) + 6;
126  $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
127  $sql .= " FROM ".MAIN_DB_PREFIX."knowledgemanagement_knowledgerecord";
128  $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
129  if ($object->ismultientitymanaged == 1) {
130  $sql .= " AND entity = ".$conf->entity;
131  } elseif ($object->ismultientitymanaged == 2) {
132  // TODO
133  }
134 
135  $resql = $db->query($sql);
136  if ($resql) {
137  $obj = $db->fetch_object($resql);
138  if ($obj) {
139  $max = intval($obj->max);
140  } else {
141  $max = 0;
142  }
143  } else {
144  dol_syslog("mod_knowledgerecord_standard::getNextValue", LOG_DEBUG);
145  return -1;
146  }
147 
148  //$date=time();
149  $date = $object->date_creation;
150  $yymm = strftime("%y%m", $date);
151 
152  if ($max >= (pow(10, 4) - 1)) {
153  $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
154  } else {
155  $num = sprintf("%04s", $max + 1);
156  }
157 
158  dol_syslog("mod_knowledgerecord_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
159  return $this->prefix.$yymm."-".$num;
160  }
161 }
Parent class to manage numbering of KnowledgeRecord.
Class to manage the knowledgerecord numbering rules standard.
info()
Return description of numbering module.
canBeActivated($object)
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
getExample()
Return an example of numbering.
getNextValue($object)
Return next free value.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("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->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.