dolibarr 19.0.4
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 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
26require_once DOL_DOCUMENT_ROOT . '/core/modules/stocktransfer/modules_stocktransfer.php';
27
28
33{
38 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
39
40 public $prefix = 'ST';
41
45 public $error = '';
46
50 public $name = 'standard';
51
52
59 public function info($langs)
60 {
61 global $langs;
62 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
63 }
64
65
71 public function getExample()
72 {
73 return $this->prefix."0501-0001";
74 }
75
76
84 public function canBeActivated($object)
85 {
86 global $conf, $langs, $db;
87
88 $coyymm = '';
89 $max = '';
90
91 $posindice = strlen($this->prefix) + 6;
92 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
93 $sql .= " FROM ".MAIN_DB_PREFIX."stocktransfer_stocktransfer";
94 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
95 if ($object->ismultientitymanaged == 1) {
96 $sql .= " AND entity = ".$conf->entity;
97 } elseif ($object->ismultientitymanaged == 2) {
98 // TODO
99 }
100
101 $resql = $db->query($sql);
102 if ($resql) {
103 $row = $db->fetch_row($resql);
104 if ($row) {
105 $coyymm = substr($row[0], 0, 6);
106 $max = $row[0];
107 }
108 }
109 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
110 $langs->load("errors");
111 $this->error = $langs->trans('ErrorNumRefModel', $max);
112 return false;
113 }
114
115 return true;
116 }
117
124 public function getNextValue($object)
125 {
126 global $db, $conf;
127
128 // first we get the max value
129 $posindice = strlen($this->prefix) + 6;
130 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
131 $sql .= " FROM ".MAIN_DB_PREFIX."stocktransfer_stocktransfer";
132 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
133 if ($object->ismultientitymanaged == 1) {
134 $sql .= " AND entity = ".$conf->entity;
135 } elseif ($object->ismultientitymanaged == 2) {
136 // TODO
137 }
138
139 $resql = $db->query($sql);
140 if ($resql) {
141 $obj = $db->fetch_object($resql);
142 if ($obj) {
143 $max = intval($obj->max);
144 } else {
145 $max = 0;
146 }
147 } else {
148 dol_syslog("mod_stocktransfer_standard::getNextValue", LOG_DEBUG);
149 return -1;
150 }
151
152 //$date=time();
153 $date = $object->date_creation;
154 $yymm = dol_print_date($date, "%y%m");
155
156 if ($max >= (pow(10, 4) - 1)) {
157 $num = $max + 1;
158 } else { // If counter > 9999, we do not format on 4 chars, we take number as it is
159 $num = sprintf("%04s", $max + 1);
160 }
161
162 dol_syslog("mod_stocktransfer_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
163 return $this->prefix.$yymm."-".$num;
164 }
165}
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='', $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.