Passed
Branch develop (42ca3b)
by
unknown
27:32
created

mod_codeclient_elephant::verif()   C

Complexity

Conditions 13
Paths 29

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 21
nc 29
nop 4
dl 0
loc 35
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* Copyright (C) 2004      Rodolphe Quiedeville <[email protected]>
3
 * Copyright (C) 2006-2009 Laurent Destailleur  <[email protected]>
4
 * Copyright (C) 2007-2012 Regis Houssin        <[email protected]>
5
 * Copyright (C) 2011      Juanjo Menent	    <[email protected]>
6
 * Copyright (C) 2013-2018 Philippe Grand      	<[email protected]>
7
 * Copyright (C) 2020		Frédéric France		<[email protected]>
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21
 * or see https://www.gnu.org/
22
 */
23
24
/**
25
 *       \file       htdocs/core/modules/societe/mod_codeclient_elephant.php
26
 *       \ingroup    societe
27
 *       \brief      File of class to manage third party code with elephant rule
28
 */
29
30
require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
31
32
33
/**
34
 *	Class to manage third party code with elephant rule
35
 */
36
class mod_codeclient_elephant extends ModeleThirdPartyCode
37
{
38
	/**
39
	 * @var string model name
40
	 */
41
	public $name = 'Elephant';
42
43
	/**
44
	 * @var int Code modifiable
45
	 */
46
	public $code_modifiable;
47
48
	/**
49
	 * @var int Code modifiable si il est invalide
50
	 */
51
	public $code_modifiable_invalide;
52
53
	/**
54
	 * @var int Code modifiables si il est null
55
	 */
56
	public $code_modifiable_null;
57
58
	/**
59
	 * @var int Code facultatif
60
	 */
61
	public $code_null;
62
63
	/**
64
	 * Dolibarr version of the loaded document
65
	 * @var string
66
	 */
67
	public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
68
69
	/**
70
	 * @var int Automatic numbering
71
	 */
72
	public $code_auto;
73
74
	/**
75
	 * @var string search string
76
	 */
77
	public $searchcode;
78
79
	/**
80
	 * @var int Nombre de chiffres du compteur
81
	 */
82
	public $numbitcounter;
83
84
	/**
85
	 * @var int thirdparty prefix is required when using {pre}
86
	 */
87
	public $prefixIsRequired;
88
89
90
	/**
91
	 *	Constructor
92
	 */
93
	public function __construct()
94
	{
95
		$this->code_null = 0;
96
		$this->code_modifiable = 1;
97
		$this->code_modifiable_invalide = 1;
98
		$this->code_modifiable_null = 1;
99
		$this->code_auto = 1;
100
		$this->prefixIsRequired = 0;
101
	}
102
103
104
	/**
105
	 *  Return description of module
106
	 *
107
	 *  @param	Translate	$langs		Object langs
108
	 *  @return string      			Description of module
109
	 */
110
	public function info($langs)
111
	{
112
		global $conf, $mc;
113
		global $form;
114
115
		$langs->load("companies");
116
117
		$disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
118
119
		$texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
120
		$texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
121
		$texte .= '<input type="hidden" name="token" value="'.newToken().'">';
122
		$texte .= '<input type="hidden" name="action" value="setModuleOptions">';
123
		$texte .= '<input type="hidden" name="param1" value="COMPANY_ELEPHANT_MASK_CUSTOMER">';
124
		$texte .= '<input type="hidden" name="param2" value="COMPANY_ELEPHANT_MASK_SUPPLIER">';
125
		$texte .= '<table class="nobordernopadding" width="100%">';
126
127
		$tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("ThirdParty"), $langs->transnoentities("ThirdParty"));
128
		//$tooltip.=$langs->trans("GenericMaskCodes2");	Not required for third party numbering
129
		$tooltip .= $langs->trans("GenericMaskCodes3");
130
		$tooltip .= $langs->trans("GenericMaskCodes4b");
131
		$tooltip .= $langs->trans("GenericMaskCodes5");
132
133
		// Parametrage du prefix customers
134
		$texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("CustomerCodeModel").'):</td>';
135
		$texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat" size="24" name="value1" value="'.$conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER.'"'.$disabled.'>', $tooltip, 1, 1).'</td>';
136
137
		$texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button"'.$disabled.'></td>';
138
139
		$texte .= '</tr>';
140
141
		// Parametrage du prefix suppliers
142
		$texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("SupplierCodeModel").'):</td>';
143
		$texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat" size="24" name="value2" value="'.$conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER.'"'.$disabled.'>', $tooltip, 1, 1).'</td>';
144
		$texte .= '</tr>';
145
146
		$texte .= '</table>';
147
		$texte .= '</form>';
148
149
		return $texte;
150
	}
151
152
153
	/**
154
	 * Return an example of result returned by getNextValue
155
	 *
156
	 * @param	Translate	$langs		Object langs
157
	 * @param	societe		$objsoc		Object thirdparty
158
	 * @param	int			$type		Type of third party (1:customer, 2:supplier, -1:autodetect)
159
	 * @return	string					Return string example
160
	 */
161
	public function getExample($langs, $objsoc = 0, $type = -1)
162
	{
163
		$examplecust = '';
164
		$examplesup = '';
165
		$errmsg = array(
166
			"ErrorBadMask",
167
			"ErrorCantUseRazIfNoYearInMask",
168
			"ErrorCantUseRazInStartedYearIfNoYearMonthInMask",
169
		);
170
		if ($type != 1) {
171
			$examplecust = $this->getNextValue($objsoc, 0);
172
			if (!$examplecust) {
173
				$examplecust = $langs->trans('NotConfigured');
174
			}
175
			if (in_array($examplecust, $errmsg)) {
176
				$langs->load("errors");
177
				$examplecust = $langs->trans($examplecust);
178
			}
179
		}
180
		if ($type != 0) {
181
			$examplesup = $this->getNextValue($objsoc, 1);
182
			if (!$examplesup) {
183
				$examplesup = $langs->trans('NotConfigured');
184
			}
185
			if (in_array($examplesup, $errmsg)) {
186
				$langs->load("errors");
187
				$examplesup = $langs->trans($examplesup);
188
			}
189
		}
190
191
		if ($type == 0) {
192
			return $examplecust;
193
		} elseif ($type == 1) {
194
			return $examplesup;
195
		} else {
196
			return $examplecust.'<br>'.$examplesup;
197
		}
198
	}
199
200
	/**
201
	 * Return next value
202
	 *
203
	 * @param	Societe		$objsoc     Object third party
204
	 * @param  	int		    $type       Client ou fournisseur (0:customer, 1:supplier)
205
	 * @return 	string      			Value if OK, '' if module not configured, <0 if KO
206
	 */
207
	public function getNextValue($objsoc = 0, $type = -1)
208
	{
209
		global $db, $conf;
210
211
		require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
212
213
		// Get Mask value
214
		$mask = '';
215
		if ($type == 0) $mask = empty($conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER) ? '' : $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
216
		if ($type == 1) $mask = empty($conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER) ? '' : $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
217
		if (!$mask)
218
		{
219
			$this->error = 'NotConfigured';
220
			return '';
221
		}
222
223
		$field = ''; $where = '';
224
		if ($type == 0)
225
		{
226
			$field = 'code_client';
227
			//$where = ' AND client in (1,2)';
228
		} elseif ($type == 1)
229
		{
230
			$field = 'code_fournisseur';
231
			//$where = ' AND fournisseur = 1';
232
		} else return -1;
233
234
		$now = dol_now();
235
236
		$numFinal = get_next_value($db, $mask, 'societe', $field, $where, '', $now);
237
238
		return  $numFinal;
239
	}
240
241
242
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
243
	/**
244
	 *   Check if mask/numbering use prefix
245
	 *
246
	 *   @return	int			0 or 1
247
	 */
248
	public function verif_prefixIsUsed()
249
	{
250
		// phpcs:enable
251
		global $conf;
252
253
		$mask = $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
254
		if (preg_match('/\{pre\}/i', $mask)) return 1;
255
256
		$mask = $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
257
		if (preg_match('/\{pre\}/i', $mask)) return 1;
258
259
		return 0;
260
	}
261
262
263
	/**
264
	 * 	Check validity of code according to its rules
265
	 *
266
	 *	@param	DoliDB		$db		Database handler
267
	 *	@param	string		$code	Code to check/correct
268
	 *	@param	Societe		$soc	Object third party
269
	 *  @param  int		  	$type   0 = customer/prospect , 1 = supplier
270
	 *  @return int					0 if OK
271
	 * 								-1 ErrorBadCustomerCodeSyntax
272
	 * 								-2 ErrorCustomerCodeRequired
273
	 * 								-3 ErrorCustomerCodeAlreadyUsed
274
	 * 								-4 ErrorPrefixRequired
275
	 * 								-5 NotConfigured - Setup empty so any value may be ok or not
276
	 * 								-6 Other (see this->error)
277
	 */
278
	public function verif($db, &$code, $soc, $type)
279
	{
280
		global $conf;
281
282
		require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
283
284
		$result = 0;
285
		$code = strtoupper(trim($code));
286
287
		if (empty($code) && $this->code_null && empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED))
288
		{
289
			$result = 0;
290
		} elseif (empty($code) && (!$this->code_null || !empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED)))
291
		{
292
			$result = -2;
293
		} else {
294
			// Get Mask value
295
			$mask = '';
296
			if ($type == 0) $mask = empty($conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER) ? '' : $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
297
			if ($type == 1) $mask = empty($conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER) ? '' : $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
298
			if (!$mask)
299
			{
300
				$this->error = 'NotConfigured';
301
				return -5;
302
			}
303
			$result = check_value($mask, $code);
304
			if (is_string($result))
305
			{
306
				$this->error = $result;
307
				return -6;
308
			}
309
		}
310
311
		dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
312
		return $result;
313
	}
314
315
316
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
317
	/**
318
	 *		Renvoi si un code est pris ou non (par autre tiers)
319
	 *
320
	 *		@param	DoliDB		$db			Handler acces base
321
	 *		@param	string		$code		Code a verifier
322
	 *		@param	Societe		$soc		Objet societe
323
	 *		@param  int		  	$type   	0 = customer/prospect , 1 = supplier
324
	 *		@return	int						0 if available, <0 if KO
325
	 */
326
	public function verif_dispo($db, $code, $soc, $type = 0)
327
	{
328
		// phpcs:enable
329
		$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
330
		if ($type == 1) $sql .= " WHERE code_fournisseur = '".$db->escape($code)."'";
331
		else $sql .= " WHERE code_client = '".$db->escape($code)."'";
332
		if ($soc->id > 0) $sql .= " AND rowid <> ".$soc->id;
333
334
		$resql = $db->query($sql);
335
		if ($resql)
336
		{
337
			if ($db->num_rows($resql) == 0)
338
			{
339
				return 0;
340
			} else {
341
				return -1;
342
			}
343
		} else {
344
			return -2;
345
		}
346
	}
347
}
348