Passed
Branch develop (5baba6)
by
unknown
30:41
created

mod_member_simple::info()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
/* Copyright (C) 2021		Laurent Destailleur	<[email protected]>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 * or see https://www.gnu.org/
17
 */
18
19
/**
20
 *	\file       htdocs/core/modules/member/mod_member_simple.php
21
 *	\ingroup    member
22
 *	\brief      File with class to manage the numbering module Simple for member references
23
 */
24
25
require_once DOL_DOCUMENT_ROOT.'/core/modules/member/modules_member.class.php';
26
27
28
/**
29
 * 	Class to manage the numbering module Simple for member references
30
 */
31
class mod_member_simple extends ModeleNumRefMembers
32
{
33
	/**
34
	 * Dolibarr version of the loaded document
35
	 * @var string
36
	 */
37
	public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
38
39
	public $prefix = 'MEM';
40
41
	/**
42
	 * @var string Error code (or message)
43
	 */
44
	public $error = '';
45
46
	/**
47
	 * @var string Nom du modele
48
	 * @deprecated
49
	 * @see $name
50
	 */
51
	public $nom = 'Simple';
52
53
	/**
54
	 * @var string model name
55
	 */
56
	public $name = 'Simple';
57
58
59
	/**
60
	 *  Return description of numbering module
61
	 *
62
	 *  @return     string      Text with description
63
	 */
64
	public function info()
65
	{
66
		global $langs;
67
		return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
68
	}
69
70
71
	/**
72
	 *  Return an example of numbering module values
73
	 *
74
	 * 	@return     string      Example
75
	 */
76
	public function getExample()
77
	{
78
		return $this->prefix."0501-0001";
79
	}
80
81
82
	/**
83
	 *  Checks if the numbers already in the database do not
84
	 *  cause conflicts that would prevent this numbering working.
85
	 *
86
	 *   @return     boolean     false if conflict, true if ok
87
	 */
88
	public function canBeActivated()
89
	{
90
		global $conf, $langs, $db;
91
92
		$coyymm = '';
93
		$max = '';
94
95
		$posindice = strlen($this->prefix) + 6;
96
		$sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
97
		$sql .= " FROM ".MAIN_DB_PREFIX."adherent";
98
		$sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
99
		$sql .= " AND entity = ".$conf->entity;
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
			return true;
110
		} else {
111
			$langs->load("errors");
112
			$this->error = $langs->trans('ErrorNumRefModel', $max);
113
			return false;
114
		}
115
	}
116
117
118
	/**
119
	 *  Return next value
120
	 *
121
	 *  @param  Societe		$objsoc		Object third party
122
	 *  @param  Object		$object		Object we need next value for
123
	 *  @return	string					Value if OK, 0 if KO
124
	 */
125
	public function getNextValue($objsoc, $object)
126
	{
127
		global $db, $conf;
128
129
		/*
130
		// First, we get the max value
131
		$posindice = strlen($this->prefix) + 6;
132
		$sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
133
		$sql .= " FROM ".MAIN_DB_PREFIX."adherent";
134
		$sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
135
		$sql .= " AND entity = ".$conf->entity;
136
137
		$resql = $db->query($sql);
138
		if ($resql) {
139
			$obj = $db->fetch_object($resql);
140
			if ($obj) {
141
				$max = intval($obj->max);
142
			} else {
143
				$max = 0;
144
			}
145
		} else {
146
			dol_syslog("mod_member_simple::getNextValue", LOG_DEBUG);
147
			return -1;
148
		}
149
150
		$date = empty($object->date_c) ? dol_now() : $object->date_c;
151
152
		//$yymm = strftime("%y%m",time());
153
		$yymm = strftime("%y%m", $date);
154
155
		if ($max >= (pow(10, 4) - 1)) {
156
			$num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
157
		} else {
158
			$num = sprintf("%04s", $max + 1);
159
		}
160
161
		dol_syslog("mod_member_simple::getNextValue return ".$this->prefix.$yymm."-".$num);
162
		return $this->prefix.$yymm."-".$num;
163
		*/
164
165
		// For the moment, the ref of a member is the rowid
166
		$sql = "SELECT MAX(rowid) as max";
167
		$sql .= " FROM ".MAIN_DB_PREFIX."adherent";
168
169
		$resql = $db->query($sql);
170
		if ($resql) {
171
			$obj = $db->fetch_object($resql);
172
			if ($obj) {
173
				$max = intval($obj->max);
174
			} else {
175
				$max = 0;
176
			}
177
		} else {
178
			dol_syslog("mod_member_simple::getNextValue", LOG_DEBUG);
179
			return -1;
180
		}
181
		return ($max + 1);
182
	}
183
}
184