dolibarr 21.0.0-alpha
mod_sn_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 Christophe Battarel <christophe@altairis.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/product_batch/modules_product_batch.class.php';
29
34{
39 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
40
41 public $prefix = 'SN';
42
46 public $error = '';
47
51 public $name = 'sn_standard';
52
53
60 public function info($langs)
61 {
62 global $langs;
63 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
64 }
65
66
72 public function getExample()
73 {
74 return $this->prefix."0501-0001";
75 }
76
77
85 public function canBeActivated($object)
86 {
87 global $conf, $langs, $db;
88
89 $coyymm = '';
90 $max = '';
91
92 $posindice = strlen($this->prefix) + 6;
93 $sql = "SELECT MAX(CAST(SUBSTRING(batch FROM ".$posindice.") AS SIGNED)) as max";
94 $sql .= " FROM ".MAIN_DB_PREFIX."product_lot";
95 $sql .= " WHERE batch LIKE '".$db->escape($this->prefix)."____-%'";
96 $sql .= " AND entity = ".$conf->entity;
97
98 $resql = $db->query($sql);
99 if ($resql) {
100 $obj = $db->fetch_object($resql);
101 if ($obj) {
102 $max = intval($obj->max);
103 } else {
104 $max = 0;
105 }
106 }
107 if ($max && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', (string) $max)) {
108 $langs->load("errors");
109 $this->error = $langs->trans('ErrorNumRefModel', $max);
110 return false;
111 }
112
113 return true;
114 }
115
123 public function getNextValue($objsoc, $object)
124 {
125 global $db, $conf;
126
127 // First, we get the max value
128 $posindice = strlen($this->prefix) + 6;
129 $sql = "SELECT MAX(CAST(SUBSTRING(batch FROM ".$posindice.") AS SIGNED)) as max";
130 $sql .= " FROM ".MAIN_DB_PREFIX."product_lot";
131 $sql .= " WHERE batch LIKE '".$db->escape($this->prefix)."____-%'";
132 $sql .= " AND entity = ".$conf->entity;
133
134 $resql = $db->query($sql);
135 if ($resql) {
136 $obj = $db->fetch_object($resql);
137 if ($obj) {
138 $max = intval($obj->max);
139 } else {
140 $max = 0;
141 }
142 } else {
143 dol_syslog("mod_sn_standard::getNextValue", LOG_DEBUG);
144 return -1;
145 }
146
147 //$date=time();
148 $date = dol_now();
149 $yymm = dol_print_date($date, "%y%m");
150
151 if ($max >= (pow(10, 4) - 1)) {
152 $num = $max + 1;
153 } else { // If counter > 9999, we do not format on 4 chars, we take number as it is
154 $num = sprintf("%04d", $max + 1);
155 }
156
157 dol_syslog("mod_sn_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
158 return $this->prefix.$yymm."-".$num;
159 }
160}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Parent class to manage numbering of batch products.
Class to manage MO numbering rules standard.
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.
getNextValue($objsoc, $object)
Return next free value.
getExample()
Return an example of numbering.
dol_now($mode='auto')
Return date for now.
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.