PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/societe/core/models/mod_codeclient_elephant.php

https://bitbucket.org/speedealing/speedealing
PHP | 302 lines | 171 code | 47 blank | 84 comment | 43 complexity | bc385973651e4da2bb8157a3a47b0191 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  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-2012 Regis Houssin <regis.houssin@capnetworks.com>
  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 3 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. * \file htdocs/core/models/mod_codeclient_elephant.php
  23. * \ingroup societe
  24. * \brief File of class to manage third party code with elephant rule
  25. */
  26. require_once DOL_DOCUMENT_ROOT . '/societe/core/models/modules_societe.class.php';
  27. /**
  28. * \class mod_codeclient_elephant
  29. * \brief Class to manage third party code with elephant rule
  30. */
  31. class mod_codeclient_elephant extends ModeleThirdPartyCode
  32. {
  33. var $nom='Elephant'; // Nom du modele
  34. var $code_modifiable; // Code modifiable
  35. var $code_modifiable_invalide; // Code modifiable si il est invalide
  36. var $code_modifiable_null; // Code modifiables si il est null
  37. var $code_null; // Code facultatif
  38. var $version='dolibarr'; // 'development', 'experimental', 'dolibarr'
  39. var $code_auto; // Numerotation automatique
  40. var $searchcode; // String de recherche
  41. var $numbitcounter; // Nombre de chiffres du compteur
  42. var $prefixIsRequired; // Le champ prefix du tiers doit etre renseigne quand on utilise {pre}
  43. /**
  44. * Constructor
  45. */
  46. function __construct()
  47. {
  48. $this->code_null = 0;
  49. $this->code_modifiable = 1;
  50. $this->code_modifiable_invalide = 1;
  51. $this->code_modifiable_null = 1;
  52. $this->code_auto = 1;
  53. $this->prefixIsRequired = 0;
  54. }
  55. /** Return description of module
  56. *
  57. * @param string $langs Object langs
  58. * @return string Description of module
  59. */
  60. function info($langs)
  61. {
  62. global $conf, $mc;
  63. global $form;
  64. $langs->load("companies");
  65. $disabled = ((! empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled="disabled"' : '');
  66. $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
  67. $texte.= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  68. $texte.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  69. $texte.= '<input type="hidden" name="action" value="setModuleOptions">';
  70. $texte.= '<input type="hidden" name="param1" value="COMPANY_ELEPHANT_MASK_CUSTOMER">';
  71. $texte.= '<input type="hidden" name="param2" value="COMPANY_ELEPHANT_MASK_SUPPLIER">';
  72. $texte.= '<table class="nobordernopadding" width="100%">';
  73. $tooltip=$langs->trans("GenericMaskCodes",$langs->transnoentities("ThirdParty"),$langs->transnoentities("ThirdParty"));
  74. //$tooltip.=$langs->trans("GenericMaskCodes2"); Not required for third party numbering
  75. $tooltip.=$langs->trans("GenericMaskCodes3");
  76. $tooltip.=$langs->trans("GenericMaskCodes4b");
  77. $tooltip.=$langs->trans("GenericMaskCodes5");
  78. // Parametrage du prefix customers
  79. $texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("CustomerCodeModel").'):</td>';
  80. $texte.= '<td align="right">'.$form->textwithpicto('<input type="text" class="flat" size="24" name="value1" value="'.$conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER.'"'.$disabled.'>',$tooltip,1,1).'</td>';
  81. $texte.= '<td align="left" rowspan="2">&nbsp; <input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button"'.$disabled.'></td>';
  82. $texte.= '</tr>';
  83. // Parametrage du prefix suppliers
  84. $texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("SupplierCodeModel").'):</td>';
  85. $texte.= '<td align="right">'.$form->textwithpicto('<input type="text" class="flat" size="24" name="value2" value="'.$conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER.'"'.$disabled.'>',$tooltip,1,1).'</td>';
  86. $texte.= '</tr>';
  87. $texte.= '</table>';
  88. $texte.= '</form>';
  89. return $texte;
  90. }
  91. /**
  92. * Return an example of result returned by getNextValue
  93. *
  94. * @param Translate $langs Object langs
  95. * @param societe $objsoc Object thirdparty
  96. * @param int $type Type of third party (1:customer, 2:supplier, -1:autodetect)
  97. * @return string Return string example
  98. */
  99. function getExample($langs,$objsoc=0,$type=-1)
  100. {
  101. if ($type == 0 || $type == -1)
  102. {
  103. $examplecust = $this->getNextValue($objsoc,0);
  104. if (! $examplecust)
  105. {
  106. $examplecust = $langs->trans('NotConfigured');
  107. }
  108. if($examplecust=="ErrorBadMask")
  109. {
  110. $langs->load("errors");
  111. $examplecust=$langs->trans($examplecust);
  112. }
  113. }
  114. if ($type == 1 || $type == -1)
  115. {
  116. $examplesup = $this->getNextValue($objsoc,1);
  117. if (! $examplesup)
  118. {
  119. $examplesup = $langs->trans('NotConfigured');
  120. }
  121. if($examplesup=="ErrorBadMask")
  122. {
  123. $langs->load("errors");
  124. $examplesup=$langs->trans($examplesup);
  125. }
  126. }
  127. if ($type == 0) return $examplecust;
  128. if ($type == 1) return $examplesup;
  129. return $examplecust.'<br>'.$examplesup;
  130. }
  131. /**
  132. * Return next value
  133. *
  134. * @param Societe $objsoc Object third party
  135. * @param int $type Client ou fournisseur (0:customer, 1:supplier)
  136. * @return string Value if OK, '' if module not configured, <0 if KO
  137. */
  138. function getNextValue($objsoc=0,$type=-1)
  139. {
  140. global $db,$conf;
  141. require_once DOL_DOCUMENT_ROOT .'/core/lib/functions2.lib.php';
  142. // Get Mask value
  143. $mask = '';
  144. if ($type==0) $mask = $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
  145. if ($type==1) $mask = $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
  146. if (! $mask)
  147. {
  148. $this->error='NotConfigured';
  149. return '';
  150. }
  151. $field='';$where='';
  152. if ($type == 0)
  153. {
  154. $field = 'code_client';
  155. //$where = ' AND client in (1,2)';
  156. }
  157. else if ($type == 1)
  158. {
  159. $field = 'code_fournisseur';
  160. //$where = ' AND fournisseur = 1';
  161. }
  162. else return -1;
  163. $now=dol_now();
  164. $numFinal=get_next_value($db,$mask,'societe',$field,$where,'',$now);
  165. return $numFinal;
  166. }
  167. /**
  168. * Check if mask/numbering use prefix
  169. *
  170. * @return int 0 or 1
  171. */
  172. function verif_prefixIsUsed()
  173. {
  174. global $conf;
  175. $mask = $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
  176. if (preg_match('/\{pre\}/i',$mask)) return 1;
  177. $mask = $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
  178. if (preg_match('/\{pre\}/i',$mask)) return 1;
  179. return 0;
  180. }
  181. /**
  182. * Check validity of code according to its rules
  183. *
  184. * @param DoliDB $db Database handler
  185. * @param string &$code Code to check/correct
  186. * @param Societe $soc Object third party
  187. * @param int $type 0 = customer/prospect , 1 = supplier
  188. * @return int 0 if OK
  189. * -1 ErrorBadCustomerCodeSyntax
  190. * -2 ErrorCustomerCodeRequired
  191. * -3 ErrorCustomerCodeAlreadyUsed
  192. * -4 ErrorPrefixRequired
  193. */
  194. function verif($db, &$code, $soc, $type)
  195. {
  196. global $conf;
  197. require_once DOL_DOCUMENT_ROOT .'/core/lib/functions2.lib.php';
  198. $result=0;
  199. $code = strtoupper(trim($code));
  200. if (empty($code) && $this->code_null && empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED))
  201. {
  202. $result=0;
  203. }
  204. else if (empty($code) && (! $this->code_null || ! empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED)) )
  205. {
  206. $result=-2;
  207. }
  208. else
  209. {
  210. // Get Mask value
  211. $mask = '';
  212. if ($type==0) $mask = empty($conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER)?'':$conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
  213. if ($type==1) $mask = empty($conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER)?'':$conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
  214. if (! $mask)
  215. {
  216. $this->error='NotConfigured';
  217. return '';
  218. }
  219. $result=check_value($mask,$code);
  220. }
  221. dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
  222. return $result;
  223. }
  224. /**
  225. * Renvoi si un code est pris ou non (par autre tiers)
  226. *
  227. * @param DoliDB $db Handler acces base
  228. * @param string $code Code a verifier
  229. * @param Societe $soc Objet societe
  230. * @return int 0 if available, <0 if KO
  231. */
  232. function verif_dispo($db, $code, $soc)
  233. {
  234. $sql = "SELECT code_client FROM ".MAIN_DB_PREFIX."societe";
  235. $sql.= " WHERE code_client = '".$code."'";
  236. if ($soc->id > 0) $sql.= " AND rowid <> ".$soc->id;
  237. $resql=$db->query($sql);
  238. if ($resql)
  239. {
  240. if ($db->num_rows($resql) == 0)
  241. {
  242. return 0;
  243. }
  244. else
  245. {
  246. return -1;
  247. }
  248. }
  249. else
  250. {
  251. return -2;
  252. }
  253. }
  254. }
  255. ?>