1<?php
2/* Copyright (C) 2003-2007  Rodolphe Quiedeville        <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2007  Laurent Destailleur         <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2009  Regis Houssin               <regis.houssin@inodbox.com>
5 * Copyright (C) 2008       Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
6 * Copyright (C) 2019       Frédéric France             <frederic.france@netlogic.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * or see https://www.gnu.org/
21 */
22
23/**
24 * \file       htdocs/core/modules/mymodule/mod_myobject_advanced.php
25 * \ingroup    mymodule
26 * \brief      File containing class for advanced numbering model of MyObject
27 */
28
29dol_include_once('/mymodule/core/modules/mymodule/modules_myobject.php');
30
31
32/**
33 *	Class to manage customer Bom numbering rules advanced
34 */
35class mod_myobject_advanced extends ModeleNumRefMyObject
36{
37	/**
38	 * Dolibarr version of the loaded document
39	 * @var string
40	 */
41	public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
42
43	/**
44	 * @var string Error message
45	 */
46	public $error = '';
47
48	/**
49	 * @var string name
50	 */
51	public $name = 'advanced';
52
53
54	/**
55	 *  Returns the description of the numbering model
56	 *
57	 *  @return     string      Texte descripif
58	 */
59	public function info()
60	{
61		global $conf, $langs, $db;
62
63		$langs->load("bills");
64
65		$form = new Form($db);
66
67		$texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
68		$texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
69		$texte .= '<input type="hidden" name="token" value="'.newToken().'">';
70		$texte .= '<input type="hidden" name="action" value="updateMask">';
71		$texte .= '<input type="hidden" name="maskconstBom" value="MYMODULE_MYOBJECT_ADVANCED_MASK">';
72		$texte .= '<table class="nobordernopadding" width="100%">';
73
74		$tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("MyObject"), $langs->transnoentities("MyObject"));
75		$tooltip .= $langs->trans("GenericMaskCodes2");
76		$tooltip .= $langs->trans("GenericMaskCodes3");
77		$tooltip .= $langs->trans("GenericMaskCodes4a", $langs->transnoentities("MyObject"), $langs->transnoentities("MyObject"));
78		$tooltip .= $langs->trans("GenericMaskCodes5");
79
80		// Parametrage du prefix
81		$texte .= '<tr><td>'.$langs->trans("Mask").':</td>';
82		$texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="maskMyObject" value="'.$conf->global->MYMODULE_MYOBJECT_ADVANCED_MASK.'">', $tooltip, 1, 1).'</td>';
83
84		$texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button"></td>';
85
86		$texte .= '</tr>';
87
88		$texte .= '</table>';
89		$texte .= '</form>';
90
91		return $texte;
92	}
93
94	/**
95	 *  Return an example of numbering
96	 *
97	 *  @return     string      Example
98	 */
99	public function getExample()
100	{
101		global $conf, $db, $langs, $mysoc;
102
103		$object = new MyObject($db);
104		$object->initAsSpecimen();
105
106		/*$old_code_client = $mysoc->code_client;
107		$old_code_type = $mysoc->typent_code;
108		$mysoc->code_client = 'CCCCCCCCCC';
109		$mysoc->typent_code = 'TTTTTTTTTT';*/
110
111		$numExample = $this->getNextValue($object);
112
113		/*$mysoc->code_client = $old_code_client;
114		$mysoc->typent_code = $old_code_type;*/
115
116		if (!$numExample) {
117			$numExample = $langs->trans('NotConfigured');
118		}
119		return $numExample;
120	}
121
122	/**
123	 * 	Return next free value
124	 *
125	 *  @param  Object		$object		Object we need next value for
126	 *  @return string      			Value if KO, <0 if KO
127	 */
128	public function getNextValue($object)
129	{
130		global $db, $conf;
131
132		require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
133
134		// We get cursor rule
135		$mask = $conf->global->MYMODULE_MYOBJECT_ADVANCED_MASK;
136
137		if (!$mask) {
138			$this->error = 'NotConfigured';
139			return 0;
140		}
141
142		$date = $object->date;
143
144		$numFinal = get_next_value($db, $mask, 'mymodule_myobject', 'ref', '', null, $date);
145
146		return  $numFinal;
147	}
148}
149