PageRenderTime 56ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/societe/core/models/mod_codeclient_monkey.php

https://bitbucket.org/speedealing/speedealing
PHP | 267 lines | 118 code | 31 blank | 118 comment | 22 complexity | 2150c938afc0ddaebf4cbfc43992d428 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-2007 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2006-2013 Regis Houssin <regis.houssin@capnetworks.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * or see http://www.gnu.org/
  19. */
  20. /**
  21. * \file htdocs/core/models/mod_codeclient_monkey.php
  22. * \ingroup societe
  23. * \brief Fichier de la classe des gestion lion des codes clients
  24. */
  25. require_once DOL_DOCUMENT_ROOT . '/societe/core/models/modules_societe.class.php';
  26. /**
  27. * \class mod_codeclient_monkey
  28. * \brief Classe permettant la gestion monkey des codes tiers
  29. */
  30. class mod_codeclient_monkey extends ModeleThirdPartyCode
  31. {
  32. var $nom='Monkey'; // Nom du modele
  33. var $code_modifiable; // Code modifiable
  34. var $code_modifiable_invalide; // Code modifiable si il est invalide
  35. var $code_modifiable_null; // Code modifiables si il est null
  36. var $code_null; // Code facultatif
  37. var $version='dolibarr'; // 'development', 'experimental', 'dolibarr'
  38. var $code_auto; // Numerotation automatique
  39. var $prefixcustomer='CU';
  40. var $prefixsupplier='SU';
  41. /**
  42. * Constructor
  43. */
  44. function __construct()
  45. {
  46. $this->nom = "Monkey";
  47. $this->version = "dolibarr";
  48. $this->code_null = 1;
  49. $this->code_modifiable = 1;
  50. $this->code_modifiable_invalide = 1;
  51. $this->code_modifiable_null = 1;
  52. $this->code_auto = 1;
  53. }
  54. /** Return description of module
  55. *
  56. * @param string $langs Object langs
  57. * @return string Description of module
  58. */
  59. function info($langs)
  60. {
  61. return $langs->trans("MonkeyNumRefModelDesc",$this->prefixcustomer,$this->prefixsupplier);
  62. }
  63. /**
  64. * Return an example of result returned by getNextValue
  65. *
  66. * @param Translate $langs Object langs
  67. * @param societe $objsoc Object thirdparty
  68. * @param int $type Type of third party (1:customer, 2:supplier, -1:autodetect)
  69. * @return string Return string example
  70. */
  71. function getExample($langs,$objsoc=0,$type=-1)
  72. {
  73. return $this->prefixcustomer.'0901-0001<br>'.$this->prefixsupplier.'0901-0001';
  74. }
  75. /**
  76. * Return next value
  77. *
  78. * @param Societe $objsoc Object third party
  79. * @param int $type Client ou fournisseur (1:client, 2:fournisseur)
  80. * @return string Value if OK, '' if module not configured, <0 if KO
  81. */
  82. function getNextValue($objsoc=0,$type=-1)
  83. {
  84. global $db, $conf, $mc;
  85. $return='000001';
  86. $field='';$where='';
  87. if ($type == 0)
  88. {
  89. $field = 'code_client';
  90. //$where = ' AND client in (1,2)';
  91. }
  92. else if ($type == 1)
  93. {
  94. $field = 'code_fournisseur';
  95. //$where = ' AND fournisseur = 1';
  96. }
  97. else return -1;
  98. if ($type == 0) $prefix=$this->prefixcustomer;
  99. if ($type == 1) $prefix=$this->prefixsupplier;
  100. // D'abord on recupere la valeur max (reponse immediate car champ indexe)
  101. $posindice=8;
  102. $sql = "SELECT MAX(SUBSTRING(".$field." FROM ".$posindice.")) as max"; // This is standard SQL
  103. $sql.= " FROM ".MAIN_DB_PREFIX."societe";
  104. $sql.= " WHERE ".$field." LIKE '".$prefix."____-%'";
  105. $sql.= " AND entity IN (".getEntity('societe', 1).")";
  106. /*
  107. $resql=$db->query($sql);
  108. if ($resql)
  109. {
  110. $obj = $db->fetch_object($resql);
  111. if ($obj) $max = intval($obj->max);
  112. else $max=0;
  113. }
  114. else
  115. {
  116. dol_syslog(get_class($this)."::getNextValue sql=".$sql, LOG_ERR);
  117. return -1;
  118. }
  119. */
  120. $date = dol_now();
  121. $yymm = strftime("%y%m",$date);
  122. $num = sprintf("%04s",$max+1);
  123. dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
  124. return $prefix.$yymm."-".$num;
  125. }
  126. /**
  127. * Check validity of code according to its rules
  128. *
  129. * @param DoliDB $db Database handler
  130. * @param string &$code Code to check/correct
  131. * @param Societe $soc Object third party
  132. * @param int $type 0 = customer/prospect , 1 = supplier
  133. * @return int 0 if OK
  134. * -1 ErrorBadCustomerCodeSyntax
  135. * -2 ErrorCustomerCodeRequired
  136. * -3 ErrorCustomerCodeAlreadyUsed
  137. * -4 ErrorPrefixRequired
  138. */
  139. function verif($db, &$code, $soc, $type)
  140. {
  141. global $conf;
  142. $result=0;
  143. $code = strtoupper(trim($code));
  144. if (empty($code) && $this->code_null && empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED))
  145. {
  146. $result=0;
  147. }
  148. else if (empty($code) && (! $this->code_null || ! empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED)) )
  149. {
  150. $result=-2;
  151. }
  152. else
  153. {
  154. if ($this->verif_syntax($code) >= 0)
  155. {
  156. $is_dispo = $this->verif_dispo($db, $code, $soc);
  157. if ($is_dispo <> 0)
  158. {
  159. $result=-3;
  160. }
  161. else
  162. {
  163. $result=0;
  164. }
  165. }
  166. else
  167. {
  168. if (dol_strlen($code) == 0)
  169. {
  170. $result=-2;
  171. }
  172. else
  173. {
  174. $result=-1;
  175. }
  176. }
  177. }
  178. dol_syslog(get_class($this)."::verif type=".$type." result=".$result);
  179. return $result;
  180. }
  181. /**
  182. * Renvoi si un code est pris ou non (par autre tiers)
  183. *
  184. * @param DoliDB $db Handler acces base
  185. * @param string $code Code a verifier
  186. * @param Societe $soc Objet societe
  187. * @return int 0 if available, <0 if KO
  188. */
  189. function verif_dispo($db, $code, $soc)
  190. {
  191. /*
  192. $sql = "SELECT code_client FROM ".MAIN_DB_PREFIX."societe";
  193. $sql.= " WHERE code_client = '".$code."'";
  194. $sql.= " AND entity IN (".getEntity('societe', 1).")";
  195. if ($soc->id > 0) $sql.= " AND rowid <> ".$soc->id;
  196. dol_syslog(get_class($this)."::verif_dispo sql=".$sql, LOG_DEBUG);
  197. $resql=$db->query($sql);
  198. if ($resql)
  199. {
  200. if ($db->num_rows($resql) == 0)
  201. {
  202. return 0;
  203. }
  204. else
  205. {
  206. return -1;
  207. }
  208. }
  209. else
  210. {
  211. return -2;
  212. }
  213. */
  214. }
  215. /**
  216. * Renvoi si un code respecte la syntaxe
  217. *
  218. * @param string $code Code a verifier
  219. * @return int 0 si OK, <0 si KO
  220. */
  221. function verif_syntax($code)
  222. {
  223. $res = 0;
  224. if (dol_strlen($code) < 11)
  225. {
  226. $res = -1;
  227. }
  228. else
  229. {
  230. $res = 0;
  231. }
  232. return $res;
  233. }
  234. }
  235. ?>