Passed
Branch develop (aaddda)
by
unknown
27:46
created

mod_workstation_standard::info()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2005-2010 Laurent Destailleur  <[email protected]>
3
 * Copyright (C) 2005-2009 Regis Houssin        <[email protected]>
4
 * Copyright (C) 2020 	   Gauthier VERDOL <[email protected]>
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
21
/**
22
 *  \file       htdocs/core/modules/workstation/mod_workstation_standard.php
23
 *  \ingroup    workstation
24
 *  \brief      File of class to manage Workstation numbering rules standard
25
 */
26
require_once DOL_DOCUMENT_ROOT . '/core/modules/workstation/modules_workstation.php';
27
28
/**
29
 *	Class to manage customer order numbering rules standard
30
 */
31
class mod_workstation_standard extends ModeleNumRefWorkstation
32
{
33
	/**
34
	 * Dolibarr version of the loaded document
35
	 * @var string
36
	 */
37
	public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
38
39
	public $prefix = 'WORKSTATION';
40
41
	/**
42
	 * @var string Error code (or message)
43
	 */
44
	public $error = '';
45
46
	/**
47
	 * @var string name
48
	 */
49
	public $name = 'standard';
50
51
52
	/**
53
	 *  Return description of numbering module
54
	 *
55
	 *  @return     string      Text with description
56
	 */
57
	public function info()
58
	{
59
		global $langs;
60
		return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
61
	}
62
63
64
	/**
65
	 *  Return an example of numbering
66
	 *
67
	 *  @return     string      Example
68
	 */
69
	public function getExample()
70
	{
71
		return $this->prefix."0501-0001";
72
	}
73
74
75
	/**
76
	 *  Checks if the numbers already in the database do not
77
	 *  cause conflicts that would prevent this numbering working.
78
	 *
79
	 *  @param  Object		$object		Object we need next value for
80
	 *  @return boolean     			false if conflict, true if ok
81
	 */
82
	public function canBeActivated($object)
83
	{
84
		global $conf, $langs, $db;
85
86
		$coyymm = ''; $max = '';
87
88
		$posindice = strlen($this->prefix) + 6;
89
		$sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
90
		$sql .= " FROM ".MAIN_DB_PREFIX."workstation_workstation";
91
		$sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
92
		if ($object->ismultientitymanaged == 1) {
93
			$sql .= " AND entity = ".$conf->entity;
94
		} elseif ($object->ismultientitymanaged == 2) {
95
			// TODO
96
		}
97
98
		$resql = $db->query($sql);
99
		if ($resql)
100
		{
101
			$row = $db->fetch_row($resql);
102
			if ($row) { $coyymm = substr($row[0], 0, 6); $max = $row[0]; }
103
		}
104
		if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm))
105
		{
106
			$langs->load("errors");
107
			$this->error = $langs->trans('ErrorNumRefModel', $max);
108
			return false;
109
		}
110
111
		return true;
112
	}
113
114
	/**
115
	 * 	Return next free value
116
	 *
117
	 *  @param  Object		$object		Object we need next value for
118
	 *  @return string      			Value if KO, <0 if KO
119
	 */
120
	public function getNextValue($object)
121
	{
122
		global $db, $conf;
123
124
		// First we get the max value
125
		$posindice = strlen($this->prefix) + 6;
126
		$sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
127
		$sql .= " FROM ".MAIN_DB_PREFIX."workstation_workstation";
128
		$sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
129
		//$sql .= " AND entity = ".$conf->entity;
130
131
		$resql = $db->query($sql);
132
		if ($resql)
133
		{
134
			$obj = $db->fetch_object($resql);
135
			if ($obj) $max = intval($obj->max);
136
			else $max = 0;
137
		} else {
138
			dol_syslog("mod_workstation_standard::getNextValue", LOG_DEBUG);
139
			return -1;
140
		}
141
142
		//$date=time();
143
		$date = dol_now();
144
		$yymm = strftime("%y%m", $date);
145
146
		if ($max >= (pow(10, 4) - 1)) $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
147
		else $num = sprintf("%04s", $max + 1);
148
149
		dol_syslog("mod_workstation_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
150
		return $this->prefix.$yymm."-".$num;
151
	}
152
}
153