PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms 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
Possible License(s): LGPL-2.0
  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. * \file htdocs/core/modules/societe/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."/core/modules/societe/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 mod_codeclient_elephant()
  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 $langs Object langs
  58. * @return string Description of module
  59. */
  60. function info($langs)
  61. {
  62. global $conf;
  63. $langs->load("companies");
  64. $form = new Form($db);
  65. $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
  66. $texte.= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  67. $texte.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  68. $texte.= '<input type="hidden" name="action" value="setModuleOptions">';
  69. $texte.= '<input type="hidden" name="param1" value="COMPANY_ELEPHANT_MASK_CUSTOMER">';
  70. $texte.= '<input type="hidden" name="param2" value="COMPANY_ELEPHANT_MASK_SUPPLIER">';
  71. $texte.= '<table class="nobordernopadding" width="100%">';
  72. $tooltip=$langs->trans("GenericMaskCodes",$langs->transnoentities("ThirdParty"));
  73. //$tooltip.=$langs->trans("GenericMaskCodes2"); Not required for third party numbering
  74. $tooltip.=$langs->trans("GenericMaskCodes3");
  75. $tooltip.=$langs->trans("GenericMaskCodes4b");
  76. $tooltip.=$langs->trans("GenericMaskCodes5");
  77. // Parametrage du prefix customers
  78. $texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("CustomerCodeModel").'):</td>';
  79. $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>';
  80. $texte.= '<td align="left" rowspan="2">&nbsp; <input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button"></td>';
  81. $texte.= '</tr>';
  82. // Parametrage du prefix suppliers
  83. $texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("SupplierCodeModel").'):</td>';
  84. $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>';
  85. $texte.= '</tr>';
  86. $texte.= '</table>';
  87. $texte.= '</form>';
  88. return $texte;
  89. }
  90. /** Return an example of result returned by getNextValue
  91. *
  92. * @param $langs Object langs
  93. * @param $objsoc Object thirdparty
  94. * @param $type Type of third party (1:customer, 2:supplier, -1:autodetect)
  95. */
  96. function getExample($langs,$objsoc=0,$type=-1)
  97. {
  98. if ($type == 0 || $type == -1)
  99. {
  100. $examplecust = $this->getNextValue($objsoc,0);
  101. if (! $examplecust)
  102. {
  103. $examplecust = $langs->trans('NotConfigured');
  104. }
  105. if($examplecust=="ErrorBadMask")
  106. {
  107. $langs->load("errors");
  108. $examplecust=$langs->trans($examplecust);
  109. }
  110. }
  111. if ($type == 1 || $type == -1)
  112. {
  113. $examplesup = $this->getNextValue($objsoc,1);
  114. if (! $examplesup)
  115. {
  116. $examplesup = $langs->trans('NotConfigured');
  117. }
  118. if($examplesup=="ErrorBadMask")
  119. {
  120. $langs->load("errors");
  121. $examplesup=$langs->trans($examplesup);
  122. }
  123. }
  124. if ($type == 0) return $examplecust;
  125. if ($type == 1) return $examplesup;
  126. return $examplecust.'<br>'.$examplesup;
  127. }
  128. /** Return next value
  129. *
  130. * @param objsoc Object third party
  131. * @param $type Client ou fournisseur (1:client, 2:fournisseur)
  132. * @return string Value if OK, '' if module not configured, <0 if KO
  133. */
  134. function getNextValue($objsoc=0,$type=-1)
  135. {
  136. global $db,$conf;
  137. require_once(DOL_DOCUMENT_ROOT ."/core/lib/functions2.lib.php");
  138. // Get Mask value
  139. $mask = '';
  140. if ($type==0) $mask = $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
  141. if ($type==1) $mask = $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
  142. if (! $mask)
  143. {
  144. $this->error='NotConfigured';
  145. return '';
  146. }
  147. $field='';$where='';
  148. if ($type == 0)
  149. {
  150. $field = 'code_client';
  151. //$where = ' AND client in (1,2)';
  152. }
  153. else if ($type == 1)
  154. {
  155. $field = 'code_fournisseur';
  156. //$where = ' AND fournisseur = 1';
  157. }
  158. else return -1;
  159. $now=dol_now();
  160. $numFinal=get_next_value($db,$mask,'societe',$field,$where,'',$now);
  161. return $numFinal;
  162. }
  163. /**
  164. * Check if mask/numbering use prefix
  165. *
  166. * @param int 0=no, 1=yes
  167. */
  168. function verif_prefixIsUsed()
  169. {
  170. global $conf;
  171. $mask = $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
  172. if (preg_match('/\{pre\}/i',$mask)) return 1;
  173. $mask = $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
  174. if (preg_match('/\{pre\}/i',$mask)) return 1;
  175. return 0;
  176. }
  177. /**
  178. * Check validity of code according to its rules
  179. *
  180. * @param $db Database handler
  181. * @param $code Code to check/correct
  182. * @param $soc Object third party
  183. * @param $type 0 = customer/prospect , 1 = supplier
  184. * @return int 0 if OK
  185. * -1 ErrorBadCustomerCodeSyntax
  186. * -2 ErrorCustomerCodeRequired
  187. * -3 ErrorCustomerCodeAlreadyUsed
  188. * -4 ErrorPrefixRequired
  189. */
  190. function verif($db, &$code, $soc, $type)
  191. {
  192. global $conf;
  193. require_once(DOL_DOCUMENT_ROOT ."/core/lib/functions2.lib.php");
  194. $result=0;
  195. $code = strtoupper(trim($code));
  196. if (empty($code) && $this->code_null && empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED))
  197. {
  198. $result=0;
  199. }
  200. else if (empty($code) && (! $this->code_null || ! empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED)) )
  201. {
  202. $result=-2;
  203. }
  204. else
  205. {
  206. // Get Mask value
  207. $mask = '';
  208. if ($type==0) $mask = empty($conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER)?'':$conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
  209. if ($type==1) $mask = empty($conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER)?'':$conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
  210. if (! $mask)
  211. {
  212. $this->error='NotConfigured';
  213. return '';
  214. }
  215. $result=check_value($mask,$code);
  216. }
  217. dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
  218. return $result;
  219. }
  220. /**
  221. * Renvoi si un code est pris ou non (par autre tiers)
  222. *
  223. * @param $db Handler acces base
  224. * @param $code Code a verifier
  225. * @param $soc Objet societe
  226. * @return int 0 si dispo, <0 si erreur
  227. */
  228. function verif_dispo($db, $code, $soc)
  229. {
  230. $sql = "SELECT code_client FROM ".MAIN_DB_PREFIX."societe";
  231. $sql.= " WHERE code_client = '".$code."'";
  232. if ($soc->id > 0) $sql.= " AND rowid != ".$soc->id;
  233. $resql=$db->query($sql);
  234. if ($resql)
  235. {
  236. if ($db->num_rows($resql) == 0)
  237. {
  238. return 0;
  239. }
  240. else
  241. {
  242. return -1;
  243. }
  244. }
  245. else
  246. {
  247. return -2;
  248. }
  249. }
  250. }
  251. ?>