Completed
Branch develop (68bb95)
by
unknown
53:54
created

mod_bom_standard::getNextValue()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 2
dl 0
loc 34
rs 9.376
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
 *
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 <http://www.gnu.org/licenses/>.
17
 * or see http://www.gnu.org/
18
 */
19
20
/**
21
 *  \file       htdocs/core/modules/bom/mod_bom_standard.php
22
 *  \ingroup    bom
23
 *  \brief      File of class to manage customer order numbering rules standard
24
 */
25
require_once DOL_DOCUMENT_ROOT .'/core/modules/bom/modules_bom.php';
26
27
/**
28
 *	Class to manage customer order numbering rules standard
29
 */
30
class mod_bom_standard extends ModeleNumRefboms
31
{
32
	/**
33
     * Dolibarr version of the loaded document
34
     * @public string
35
     */
36
	public $version = 'dolibarr';		// 'development', 'experimental', 'dolibarr'
37
38
	public $prefix='BOM';
39
40
	/**
41
	 * @var string Error code (or message)
42
	 */
43
	public $error='';
44
45
	/**
46
	 * @var string name
47
	 */
48
	public $name='standard';
49
50
51
    /**
52
     *  Return description of numbering module
53
     *
54
     *  @return     string      Text with description
55
     */
56
    public function info()
57
    {
58
    	global $langs;
59
      	return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
60
    }
61
62
63
	/**
64
	 *  Renvoi un exemple de numerotation
65
	 *
66
	 *  @return     string      Example
67
	 */
68
	public function getExample()
69
	{
70
		return $this->prefix."0501-0001";
71
	}
72
73
74
	/**
75
	 *  Test si les numeros deje en vigueur dans la base ne provoquent pas de
76
	 *  de conflits qui empechera cette numerotation de fonctionner.
77
	 *
78
	 *  @return     boolean     false si conflit, true si ok
79
	 */
80
	public function canBeActivated()
81
	{
82
		global $conf,$langs,$db;
83
84
		$coyymm=''; $max='';
85
86
		$posindice=8;
87
		$sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
88
		$sql.= " FROM ".MAIN_DB_PREFIX."bom";
89
		$sql.= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
90
		$sql.= " AND entity = ".$conf->entity;
91
92
		$resql=$db->query($sql);
93
		if ($resql)
94
		{
95
			$row = $db->fetch_row($resql);
96
			if ($row) { $coyymm = substr($row[0], 0, 6); $max=$row[0]; }
97
		}
98
		if ($coyymm && ! preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm))
99
		{
100
			$langs->load("errors");
101
			$this->error=$langs->trans('ErrorNumRefModel', $max);
102
			return false;
103
		}
104
105
		return true;
106
	}
107
108
	/**
109
	 * 	Return next free value
110
	 *
111
	 *  @param	Societe		$objsoc     Object thirdparty
112
	 *  @param  Object		$object		Object we need next value for
113
	 *  @return string      			Value if KO, <0 if KO
114
	 */
115
	public function getNextValue($objsoc, $object)
116
	{
117
		global $db,$conf;
118
119
		// D'abord on recupere la valeur max
120
		$posindice=8;
121
		$sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
122
		$sql.= " FROM ".MAIN_DB_PREFIX."bom_bom";
123
		$sql.= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
124
		$sql.= " AND entity = ".$conf->entity;
125
126
		$resql=$db->query($sql);
127
		if ($resql)
128
		{
129
			$obj = $db->fetch_object($resql);
130
			if ($obj) $max = intval($obj->max);
131
			else $max=0;
132
		}
133
		else
134
		{
135
			dol_syslog("mod_bom_standard::getNextValue", LOG_DEBUG);
136
			return -1;
137
		}
138
139
		//$date=time();
140
		$date=$object->date;
141
		$yymm = strftime("%y%m", $date);
142
143
    	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
144
    	else $num = sprintf("%04s", $max+1);
145
146
		dol_syslog("mod_bom_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
147
		return $this->prefix.$yymm."-".$num;
148
	}
149
}
150