dolibarr 19.0.4
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
57 public function info($langs)
58 {
59 global $langs;
60 return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
61 }
62
63
69 public function getExample()
70 {
71 return $this->prefix."0501-0001";
72 }
73
74
82 public function canBeActivated($object)
83 {
84 global $conf, $langs, $db;
85
86 $coyymm = '';
87 $max = '';
88
89 $posindice = strlen($this->prefix) + 6;
90 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
91 $sql .= " FROM ".MAIN_DB_PREFIX."bom";
92 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
93 $sql .= " AND entity = ".$conf->entity;
94
95 $resql = $db->query($sql);
96 if ($resql) {
97 $row = $db->fetch_row($resql);
98 if ($row) {
99 $coyymm = substr($row[0], 0, 6);
100 $max = $row[0];
101 }
102 }
103 if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
104 $langs->load("errors");
105 $this->error = $langs->trans('ErrorNumRefModel', $max);
106 return false;
107 }
108
109 return true;
110 }
111
119 public function getNextValue($objprod, $object)
120 {
121 global $db, $conf;
122
123 // First we get the max value
124 $posindice = strlen($this->prefix) + 6;
125 $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
126 $sql .= " FROM ".MAIN_DB_PREFIX."bom_bom";
127 $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
128 $sql .= " AND entity = ".$conf->entity;
129
130 $resql = $db->query($sql);
131 if ($resql) {
132 $obj = $db->fetch_object($resql);
133 if ($obj) {
134 $max = intval($obj->max);
135 } else {
136 $max = 0;
137 }
138 } else {
139 dol_syslog("mod_bom_standard::getNextValue", LOG_DEBUG);
140 return -1;
141 }
142
143 //$date=time();
144 $date = $object->date_creation;
145 $yymm = dol_print_date($date, "%y%m");
146
147 if ($max >= (pow(10, 4) - 1)) {
148 $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
149 } else {
150 $num = sprintf("%04s", $max + 1);
151 }
152
153 dol_syslog("mod_bom_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
154 return $this->prefix.$yymm."-".$num;
155 }
156}
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='', $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.