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

/htdocs/core/modules/product/mod_codeproduct_elephant.php

https://bitbucket.org/speedealing/speedealing
PHP | 304 lines | 173 code | 48 blank | 83 comment | 48 complexity | 3f391638a2705a6aab419af5f16ce5d3 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/modules/product/mod_codeproduct_elephant.php
  23. * \ingroup product
  24. * \brief File of class to manage product code with elephant rule
  25. */
  26. require_once DOL_DOCUMENT_ROOT.'/core/modules/product/modules_product.class.php';
  27. /**
  28. * \class mod_codeproduct_elephant
  29. * \brief Class to manage product code with elephant rule
  30. */
  31. class mod_codeproduct_elephant extends ModeleProductCode
  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("products");
  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="PRODUCT_ELEPHANT_MASK_PRODUCT">';
  71. $texte.= '<input type="hidden" name="param2" value="PRODUCT_ELEPHANT_MASK_SERVICE">';
  72. $texte.= '<table class="nobordernopadding" width="100%">';
  73. $tooltip=$langs->trans("GenericMaskCodes",$langs->transnoentities("Product"),$langs->transnoentities("Product"));
  74. $tooltip.=$langs->trans("GenericMaskCodes3");
  75. $tooltip.=$langs->trans("GenericMaskCodes4c");
  76. $tooltip.=$langs->trans("GenericMaskCodes5");
  77. // Parametrage du prefix customers
  78. $texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("ProductCodeModel").'):</td>';
  79. $texte.= '<td align="right">'.$form->textwithpicto('<input type="text" class="flat" size="24" name="value1" value="'.(! empty($conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT)?$conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT:'').'"'.$disabled.'>',$tooltip,1,1).'</td>';
  80. $texte.= '<td align="left" rowspan="2">&nbsp; <input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button"'.$disabled.'></td>';
  81. $texte.= '</tr>';
  82. // Parametrage du prefix suppliers
  83. $texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("ServiceCodeModel").'):</td>';
  84. $texte.= '<td align="right">'.$form->textwithpicto('<input type="text" class="flat" size="24" name="value2" value="'.(! empty($conf->global->PRODUCT_ELEPHANT_MASK_SERVICE)?$conf->global->PRODUCT_ELEPHANT_MASK_SERVICE:'').'"'.$disabled.'>',$tooltip,1,1).'</td>';
  85. $texte.= '</tr>';
  86. $texte.= '</table>';
  87. $texte.= '</form>';
  88. return $texte;
  89. }
  90. /**
  91. * Return an example of result returned by getNextValue
  92. *
  93. * @param Translate $langs Object langs
  94. * @param product $objproduct Object product
  95. * @param int $type Type of third party (1:customer, 2:supplier, -1:autodetect)
  96. * @return string Return string example
  97. */
  98. function getExample($langs,$objproduct=0,$type=-1)
  99. {
  100. if ($type == 0 || $type == -1)
  101. {
  102. $exampleproduct = $this->getNextValue($objproduct,0);
  103. if (! $exampleproduct)
  104. {
  105. $exampleproduct = $langs->trans('NotConfigured');
  106. }
  107. if($exampleproduct=="ErrorBadMask")
  108. {
  109. $langs->load("errors");
  110. $exampleproduct=$langs->trans($exampleproduct);
  111. }
  112. }
  113. if ($type == 1 || $type == -1)
  114. {
  115. $exampleservice = $this->getNextValue($objproduct,1);
  116. if (! $exampleservice)
  117. {
  118. $exampleservice = $langs->trans('NotConfigured');
  119. }
  120. if($exampleservice=="ErrorBadMask")
  121. {
  122. $langs->load("errors");
  123. $exampleservice=$langs->trans($exampleservice);
  124. }
  125. }
  126. if ($type == 0) return $exampleproduct;
  127. if ($type == 1) return $exampleservice;
  128. return $exampleproduct.'<br>'.$exampleservice;
  129. }
  130. /**
  131. * Return next value
  132. *
  133. * @param Product $objproduct Object product
  134. * @param int $type Produit ou service (0:product, 1:service)
  135. * @return string Value if OK, '' if module not configured, <0 if KO
  136. */
  137. function getNextValue($objproduct=0,$type=-1)
  138. {
  139. global $db,$conf;
  140. require_once DOL_DOCUMENT_ROOT .'/core/lib/functions2.lib.php';
  141. // Get Mask value
  142. $mask = '';
  143. if ($type == 0 && ! empty($conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT))
  144. $mask = $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
  145. else if ($type == 1 && ! empty($conf->global->PRODUCT_ELEPHANT_MASK_SERVICE))
  146. $mask = $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
  147. if (empty($mask))
  148. {
  149. $this->error='NotConfigured';
  150. return '';
  151. }
  152. $field='';$where='';
  153. if ($type == 0)
  154. {
  155. $field = 'ref';
  156. //$where = ' AND client in (1,2)';
  157. }
  158. else if ($type == 1)
  159. {
  160. $field = 'ref';
  161. //$where = ' AND fournisseur = 1';
  162. }
  163. else return -1;
  164. $now=dol_now();
  165. $numFinal=get_next_value($db,$mask,'product',$field,$where,'',$now);
  166. return $numFinal;
  167. }
  168. /**
  169. * Check if mask/numbering use prefix
  170. *
  171. * @return int 0 or 1
  172. */
  173. function verif_prefixIsUsed()
  174. {
  175. global $conf;
  176. $mask = $conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
  177. if (preg_match('/\{pre\}/i',$mask)) return 1;
  178. $mask = $conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
  179. if (preg_match('/\{pre\}/i',$mask)) return 1;
  180. return 0;
  181. }
  182. /**
  183. * Check validity of code according to its rules
  184. *
  185. * @param DoliDB $db Database handler
  186. * @param string &$code Code to check/correct
  187. * @param Product $product Object product
  188. * @param int $type 0 = customer/prospect , 1 = supplier
  189. * @return int 0 if OK
  190. * -1 ErrorBadCustomerCodeSyntax
  191. * -2 ErrorCustomerCodeRequired
  192. * -3 ErrorCustomerCodeAlreadyUsed
  193. * -4 ErrorPrefixRequired
  194. */
  195. function verif($db, &$code, $product, $type)
  196. {
  197. global $conf;
  198. require_once DOL_DOCUMENT_ROOT .'/core/lib/functions2.lib.php';
  199. $result=0;
  200. $code = strtoupper(trim($code));
  201. if (empty($code) && $this->code_null && empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED))
  202. {
  203. $result=0;
  204. }
  205. else if (empty($code) && (! $this->code_null || ! empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED)) )
  206. {
  207. $result=-2;
  208. }
  209. else
  210. {
  211. // Get Mask value
  212. $mask = '';
  213. if ($type==0) $mask = empty($conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT)?'':$conf->global->PRODUCT_ELEPHANT_MASK_PRODUCT;
  214. if ($type==1) $mask = empty($conf->global->PRODUCT_ELEPHANT_MASK_SSERVICE)?'':$conf->global->PRODUCT_ELEPHANT_MASK_SERVICE;
  215. if (! $mask)
  216. {
  217. $this->error='NotConfigured';
  218. return '';
  219. }
  220. $result=check_value($mask,$code);
  221. }
  222. dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
  223. return $result;
  224. }
  225. /**
  226. * Renvoi si un code est pris ou non (par autre tiers)
  227. *
  228. * @param DoliDB $db Handler acces base
  229. * @param string $code Code a verifier
  230. * @param Product $product Objet product
  231. * @return int 0 if available, <0 if KO
  232. */
  233. function verif_dispo($db, $code, $product)
  234. {
  235. $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
  236. $sql.= " WHERE ref = '".$code."'";
  237. if ($product->id > 0) $sql.= " AND rowid <> ".$product->id;
  238. $resql=$db->query($sql);
  239. if ($resql)
  240. {
  241. if ($db->num_rows($resql) == 0)
  242. {
  243. return 0;
  244. }
  245. else
  246. {
  247. return -1;
  248. }
  249. }
  250. else
  251. {
  252. return -2;
  253. }
  254. }
  255. }
  256. ?>