PageRenderTime 84ms CodeModel.GetById 40ms app.highlight 12ms RepoModel.GetById 28ms app.codeStats 0ms

/htdocs/core/modules/societe/mod_codeclient_elephant.php

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