dolibarr 18.0.6
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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 * or see https://www.gnu.org/
18 */
19
25require_once DOL_DOCUMENT_ROOT.'/core/modules/bom/modules_bom.php';
26
31{
36 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
37
38 public $prefix = 'BOM';
39
43 public $error = '';
44
48 public $name = 'standard';
49
50
56 public function info()
57 {
58 global $langs;
59 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
60 }
61
62
68 public function getExample()
69 {
70 return $this->prefix."0501-0001";
71 }
72
73
80 public function canBeActivated()
81 {
82 global $conf, $langs, $db;
83
84 $coyymm = '';
85 $max = '';
86
87 $posindice = strlen($this->prefix) + 6;
88 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
89 $sql .= " FROM ".MAIN_DB_PREFIX."bom";
90 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
91 $sql .= " AND entity = ".$conf->entity;
92
93 $resql = $db->query($sql);
94 if ($resql) {
95 $row = $db->fetch_row($resql);
96 if ($row) {
97 $coyymm = substr($row[0], 0, 6);
98 $max = $row[0];
99 }
100 }
101 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
102 $langs->load("errors");
103 $this->error = $langs->trans('ErrorNumRefModel', $max);
104 return false;
105 }
106
107 return true;
108 }
109
117 public function getNextValue($objprod, $object)
118 {
119 global $db, $conf;
120
121 // First we get the max value
122 $posindice = strlen($this->prefix) + 6;
123 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
124 $sql .= " FROM ".MAIN_DB_PREFIX."bom_bom";
125 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
126 $sql .= " AND entity = ".$conf->entity;
127
128 $resql = $db->query($sql);
129 if ($resql) {
130 $obj = $db->fetch_object($resql);
131 if ($obj) {
132 $max = intval($obj->max);
133 } else {
134 $max = 0;
135 }
136 } else {
137 dol_syslog("mod_bom_standard::getNextValue", LOG_DEBUG);
138 return -1;
139 }
140
141 //$date=time();
142 $date = $object->date_creation;
143 $yymm = strftime("%y%m", $date);
144
145 if ($max >= (pow(10, 4) - 1)) {
146 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
147 } else {
148 $num = sprintf("%04s", $max + 1);
149 }
150
151 dol_syslog("mod_bom_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
152 return $this->prefix.$yymm."-".$num;
153 }
154}
Parent class to manage numbering of BOMs.
Class to manage the Standard numbering rule for BOM.
getExample()
Return an example of numbering.
getNextValue($objprod, $object)
Return next free value.
canBeActivated()
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
info()
Return description of numbering module.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.