dolibarr 21.0.0-alpha
mod_stocktransfer_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) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.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
28require_once DOL_DOCUMENT_ROOT . '/core/modules/stocktransfer/modules_stocktransfer.php';
29
30
35{
40 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
41
42 public $prefix = 'ST';
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."stocktransfer_stocktransfer";
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
126 public function getNextValue($object)
127 {
128 global $db, $conf;
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."stocktransfer_stocktransfer";
134 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
135 if ($object->ismultientitymanaged == 1) {
136 $sql .= " AND entity = ".$conf->entity;
137 } elseif ($object->ismultientitymanaged == 2) {
138 // TODO
139 }
140
141 $resql = $db->query($sql);
142 if ($resql) {
143 $obj = $db->fetch_object($resql);
144 if ($obj) {
145 $max = intval($obj->max);
146 } else {
147 $max = 0;
148 }
149 } else {
150 dol_syslog("mod_stocktransfer_standard::getNextValue", LOG_DEBUG);
151 return -1;
152 }
153
154 //$date=time();
155 $date = $object->date_creation;
156 $yymm = dol_print_date($date, "%y%m");
157
158 if ($max >= (pow(10, 4) - 1)) {
159 $num = $max + 1;
160 } else { // If counter > 9999, we do not format on 4 chars, we take number as it is
161 $num = sprintf("%04d", $max + 1);
162 }
163
164 dol_syslog("mod_stocktransfer_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
165 return $this->prefix.$yymm."-".$num;
166 }
167}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Parent class to manage numbering of StockTransfer.
Class to manage the Standard numbering rule for Stock.
getNextValue($object)
Return next free 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...
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.