PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/modules/societe/mod_codeclient_monkey.php

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