dolibarr 23.0.3
mod_bom_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) 2024-2025 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * or see https://www.gnu.org/
20 */
21
27require_once DOL_DOCUMENT_ROOT.'/core/modules/bom/modules_bom.php';
28
33{
38 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
39
43 public $prefix = 'BOM';
44
48 public $error = '';
49
53 public $name = 'standard';
54
55
62 public function info($langs)
63 {
64 global $langs;
65 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
66 }
67
68
74 public function getExample()
75 {
76 return $this->prefix."0501-0001";
77 }
78
79
87 public function canBeActivated($object)
88 {
89 global $conf, $langs, $db;
90
91 $coyymm = '';
92 $max = '';
93
94 $posindice = strlen($this->prefix) + 6;
95 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
96 $sql .= " FROM ".MAIN_DB_PREFIX."bom";
97 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
98 $sql .= " AND entity = ".$conf->entity;
99
100 $resql = $db->query($sql);
101 if ($resql) {
102 $row = $db->fetch_row($resql);
103 if ($row) {
104 $coyymm = substr($row[0], 0, 6);
105 $max = $row[0];
106 }
107 }
108 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
109 $langs->load("errors");
110 $this->error = $langs->trans('ErrorNumRefModel', $max);
111 return false;
112 }
113
114 return true;
115 }
116
124 public function getNextValue($objprod, $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."bom_bom";
132 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
133 $sql .= " AND entity = ".$conf->entity;
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_bom_standard::getNextValue", LOG_DEBUG);
145 return -1;
146 }
147
148 //$date=time();
149 $date = $object->date_creation;
150 $yymm = dol_print_date($date, "%y%m");
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("%04d", $max + 1);
156 }
157
158 dol_syslog("mod_bom_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
159 return $this->prefix.$yymm."-".$num;
160 }
161}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Parent class to manage numbering of BOMs.
Class to manage the Standard numbering rule for BOM.
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($objprod, $object)
Return next free value.
info($langs)
Return description of numbering module.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
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.