dolibarr 21.0.0-alpha
mod_asset_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 * Copyright (C) 2022-2024 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2022 Frédéric France <frederic.france@netlogic.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * or see https://www.gnu.org/
21 */
22
29require_once DOL_DOCUMENT_ROOT.'/core/modules/asset/modules_asset.php';
30
35{
40 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
41
42 public $prefix = 'ASSET';
43
47 public $error = '';
48
52 public $name = 'standard';
53
54
61 public function info($langs)
62 {
63 global $langs;
64 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
65 }
66
67
73 public function getExample()
74 {
75 return $this->prefix."0501-0001";
76 }
77
78
86 public function canBeActivated($object)
87 {
88 global $conf, $langs, $db;
89
90 $coyymm = '';
91 $max = '';
92
93 $posindice = strlen($this->prefix) + 6;
94 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
95 $sql .= " FROM ".MAIN_DB_PREFIX."asset_asset";
96 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
97 if ($object->ismultientitymanaged == 1) {
98 $sql .= " AND entity = ".$conf->entity;
99 } elseif ($object->ismultientitymanaged == 2) {
100 // TODO
101 }
102
103 $resql = $db->query($sql);
104 if ($resql) {
105 $row = $db->fetch_row($resql);
106 if ($row) {
107 $coyymm = substr($row[0], 0, 6);
108 $max = $row[0];
109 }
110 }
111 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
112 $langs->load("errors");
113 $this->error = $langs->trans('ErrorNumRefModel', $max);
114 return false;
115 }
116
117 return true;
118 }
119
120
127 public function getNextValue($object)
128 {
129 global $db, $conf;
130
131 // first we get the max value
132 $posindice = strlen($this->prefix) + 6;
133 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
134 $sql .= " FROM ".MAIN_DB_PREFIX."asset";
135 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
136 if ($object->ismultientitymanaged == 1) {
137 $sql .= " AND entity = ".$conf->entity;
138 } elseif ($object->ismultientitymanaged == 2) {
139 // TODO
140 }
141
142 $resql = $db->query($sql);
143 if ($resql) {
144 $obj = $db->fetch_object($resql);
145 if ($obj) {
146 $max = intval($obj->max);
147 } else {
148 $max = 0;
149 }
150 } else {
151 dol_syslog("mod_asset_standard::getNextValue", LOG_DEBUG);
152 return -1;
153 }
154
155 //$date=time();
156 $date = $object->date_creation;
157 $yymm = dol_print_date($date, "%y%m");
158
159 if ($max >= (pow(10, 4) - 1)) {
160 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
161 } else {
162 $num = sprintf("%04d", $max + 1);
163 }
164
165 dol_syslog("mod_asset_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
166 return $this->prefix.$yymm."-".$num;
167 }
168}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Parent class to manage numbering of Asset.
Class to manage the Standard numbering rule for Asset.
getNextValue($object)
Return next free value.
canBeActivated($object)
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
info($langs)
Return description of numbering module.
getExample()
Return an example of numbering.
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.