1<?php
2/* Copyright (C) 2010-2012	Regis Houssin		<regis.houssin@inodbox.com>
3 * Copyright (C) 2010		Laurent Destailleur	<eldy@users.sourceforge.net>
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
20/**
21 *	\file       htdocs/core/modules/project/mod_project_simple.php
22 *	\ingroup    project
23 *	\brief      File with class to manage the numbering module Simple for project references
24 */
25
26require_once DOL_DOCUMENT_ROOT.'/core/modules/project/modules_project.php';
27
28
29/**
30 * 	Class to manage the numbering module Simple for project references
31 */
32class mod_project_simple extends ModeleNumRefProjects
33{
34	/**
35	 * Dolibarr version of the loaded document
36	 * @var string
37	 */
38	public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
39
40	public $prefix = 'PJ';
41
42	/**
43	 * @var string Error code (or message)
44	 */
45	public $error = '';
46
47	/**
48	 * @var string Nom du modele
49	 * @deprecated
50	 * @see $name
51	 */
52	public $nom = 'Simple';
53
54	/**
55	 * @var string model name
56	 */
57	public $name = 'Simple';
58
59
60	/**
61	 *  Return description of numbering module
62	 *
63	 *  @return     string      Text with description
64	 */
65	public function info()
66	{
67		global $langs;
68	  	return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
69	}
70
71
72	/**
73	 *  Return an example of numbering module values
74	 *
75	 * 	@return     string      Example
76	 */
77	public function getExample()
78	{
79		return $this->prefix."0501-0001";
80	}
81
82
83	/**
84	 *  Checks if the numbers already in the database do not
85	 *  cause conflicts that would prevent this numbering working.
86	 *
87	 *   @return     boolean     false if conflict, true if ok
88	 */
89	public function canBeActivated()
90	{
91		global $conf, $langs, $db;
92
93		$coyymm = ''; $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."projet";
98		$sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
99		$sql .= " AND entity = ".$conf->entity;
100		$resql = $db->query($sql);
101		if ($resql)
102		{
103			$row = $db->fetch_row($resql);
104			if ($row) { $coyymm = substr($row[0], 0, 6); $max = $row[0]; }
105		}
106		if (!$coyymm || preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm))
107		{
108			return true;
109		} else {
110			$langs->load("errors");
111			$this->error = $langs->trans('ErrorNumRefModel', $max);
112			return false;
113		}
114	}
115
116
117	/**
118	 *  Return next value
119	 *
120	 *  @param   Societe	$objsoc		Object third party
121	 *  @param   Project	$project	Object project
122	 *  @return	string				Value if OK, 0 if KO
123	 */
124	public function getNextValue($objsoc, $project)
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."projet";
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		{
138			$obj = $db->fetch_object($resql);
139			if ($obj) $max = intval($obj->max);
140			else $max = 0;
141		} else {
142			dol_syslog("mod_project_simple::getNextValue", LOG_DEBUG);
143			return -1;
144		}
145
146		$date = empty($project->date_c) ?dol_now() : $project->date_c;
147
148		//$yymm = strftime("%y%m",time());
149		$yymm = strftime("%y%m", $date);
150
151		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
152		else $num = sprintf("%04s", $max + 1);
153
154		dol_syslog("mod_project_simple::getNextValue return ".$this->prefix.$yymm."-".$num);
155		return $this->prefix.$yymm."-".$num;
156	}
157
158
159	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
160	/**
161	 *  Return next reference not yet used as a reference
162	 *
163	 *  @param	Societe	$objsoc     Object third party
164	 *  @param  Project	$project	Object project
165	 *  @return string      		Next not used reference
166	 */
167	public function project_get_num($objsoc = 0, $project = '')
168	{
169		// phpcs:enable
170		return $this->getNextValue($objsoc, $project);
171	}
172}
173