mod_codeclient_monkey.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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-2012 Regis Houssin <regis.houssin@inodbox.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 <https://www.gnu.org/licenses/>.
  18. * or see https://www.gnu.org/
  19. */
  20. /**
  21. * \file htdocs/core/modules/societe/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.'/core/modules/societe/modules_societe.class.php';
  26. /**
  27. * Classe permettant la gestion monkey des codes tiers
  28. */
  29. class mod_codeclient_monkey extends ModeleThirdPartyCode
  30. {
  31. /**
  32. * @var string model name
  33. */
  34. public $name = 'Monkey';
  35. public $code_modifiable; // Code modifiable
  36. public $code_modifiable_invalide; // Code modifiable si il est invalide
  37. public $code_modifiable_null; // Code modifiables si il est null
  38. public $code_null; // Code facultatif
  39. /**
  40. * Dolibarr version of the loaded document
  41. * @var string
  42. */
  43. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  44. /**
  45. * @var int Automatic numbering
  46. */
  47. public $code_auto;
  48. public $prefixcustomer = 'CU';
  49. public $prefixsupplier = 'SU';
  50. public $prefixIsRequired; // Le champ prefix du tiers doit etre renseigne quand on utilise {pre}
  51. /**
  52. * Constructor
  53. */
  54. public function __construct()
  55. {
  56. $this->nom = "Monkey";
  57. $this->name = "Monkey";
  58. $this->version = "dolibarr";
  59. $this->code_null = 1;
  60. $this->code_modifiable = 1;
  61. $this->code_modifiable_invalide = 1;
  62. $this->code_modifiable_null = 1;
  63. $this->code_auto = 1;
  64. $this->prefixIsRequired = 0;
  65. }
  66. /**
  67. * Return description of module
  68. *
  69. * @param Translate $langs Object langs
  70. * @return string Description of module
  71. */
  72. public function info($langs)
  73. {
  74. return $langs->trans("MonkeyNumRefModelDesc", $this->prefixcustomer, $this->prefixsupplier);
  75. }
  76. /**
  77. * Return an example of result returned by getNextValue
  78. *
  79. * @param Translate $langs Object langs
  80. * @param societe $objsoc Object thirdparty
  81. * @param int $type Type of third party (1:customer, 2:supplier, -1:autodetect)
  82. * @return string Return string example
  83. */
  84. public function getExample($langs, $objsoc = 0, $type = -1)
  85. {
  86. return $this->prefixcustomer.'0901-00001<br>'.$this->prefixsupplier.'0901-00001';
  87. }
  88. /**
  89. * Return next value
  90. *
  91. * @param Societe $objsoc Object third party
  92. * @param int $type Client ou fournisseur (1:client, 2:fournisseur)
  93. * @return string Value if OK, '' if module not configured, <0 if KO
  94. */
  95. public function getNextValue($objsoc = 0, $type = -1)
  96. {
  97. global $db, $conf, $mc;
  98. $field = '';
  99. $prefix = '';
  100. if ($type == 0) {
  101. $field = 'code_client';
  102. $prefix = $this->prefixcustomer;
  103. } elseif ($type == 1) {
  104. $field = 'code_fournisseur';
  105. $prefix = $this->prefixsupplier;
  106. } else {
  107. return -1;
  108. }
  109. // First, we get the max value (reponse immediate car champ indexe)
  110. $posindice = strlen($prefix) + 6;
  111. $sql = "SELECT MAX(CAST(SUBSTRING(".$field." FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
  112. $sql .= " FROM ".MAIN_DB_PREFIX."societe";
  113. $sql .= " WHERE ".$field." LIKE '".$db->escape($prefix)."____-%'";
  114. $sql .= " AND entity IN (".getEntity('societe').")";
  115. dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
  116. $resql = $db->query($sql);
  117. if ($resql) {
  118. $obj = $db->fetch_object($resql);
  119. if ($obj) {
  120. $max = intval($obj->max);
  121. } else {
  122. $max = 0;
  123. }
  124. } else {
  125. return -1;
  126. }
  127. $date = dol_now();
  128. $yymm = dol_print_date($date, "%y%m", 'tzuserrel');
  129. if ($max >= (pow(10, 5) - 1)) {
  130. $num = $max + 1; // If counter > 99999, we do not format on 5 chars, we take number as it is
  131. } else {
  132. $num = sprintf("%05s", $max + 1);
  133. }
  134. dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
  135. return $prefix.$yymm."-".$num;
  136. }
  137. /**
  138. * Check validity of code according to its rules
  139. *
  140. * @param DoliDB $db Database handler
  141. * @param string $code Code to check/correct
  142. * @param Societe $soc Object third party
  143. * @param int $type 0 = customer/prospect , 1 = supplier
  144. * @return int 0 if OK
  145. * -1 ErrorBadCustomerCodeSyntax
  146. * -2 ErrorCustomerCodeRequired
  147. * -3 ErrorCustomerCodeAlreadyUsed
  148. * -4 ErrorPrefixRequired
  149. */
  150. public function verif($db, &$code, $soc, $type)
  151. {
  152. global $conf;
  153. $result = 0;
  154. $code = strtoupper(trim($code));
  155. if (empty($code) && $this->code_null && empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED)) {
  156. $result = 0;
  157. } elseif (empty($code) && (!$this->code_null || !empty($conf->global->MAIN_COMPANY_CODE_ALWAYS_REQUIRED))) {
  158. $result = -2;
  159. } else {
  160. if ($this->verif_syntax($code) >= 0) {
  161. $is_dispo = $this->verif_dispo($db, $code, $soc, $type);
  162. if ($is_dispo <> 0) {
  163. $result = -3;
  164. } else {
  165. $result = 0;
  166. }
  167. } else {
  168. if (dol_strlen($code) == 0) {
  169. $result = -2;
  170. } else {
  171. $result = -1;
  172. }
  173. }
  174. }
  175. dol_syslog(get_class($this)."::verif code=".$code." type=".$type." result=".$result);
  176. return $result;
  177. }
  178. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  179. /**
  180. * Renvoi si un code est pris ou non (par autre tiers)
  181. *
  182. * @param DoliDB $db Handler acces base
  183. * @param string $code Code a verifier
  184. * @param Societe $soc Objet societe
  185. * @param int $type 0 = customer/prospect , 1 = supplier
  186. * @return int 0 if available, <0 if KO
  187. */
  188. public function verif_dispo($db, $code, $soc, $type = 0)
  189. {
  190. // phpcs:enable
  191. global $conf, $mc;
  192. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
  193. if ($type == 1) {
  194. $sql .= " WHERE code_fournisseur = '".$db->escape($code)."'";
  195. } else {
  196. $sql .= " WHERE code_client = '".$db->escape($code)."'";
  197. }
  198. $sql .= " AND entity IN (".getEntity('societe').")";
  199. if ($soc->id > 0) {
  200. $sql .= " AND rowid <> ".$soc->id;
  201. }
  202. dol_syslog(get_class($this)."::verif_dispo", LOG_DEBUG);
  203. $resql = $db->query($sql);
  204. if ($resql) {
  205. if ($db->num_rows($resql) == 0) {
  206. return 0;
  207. } else {
  208. return -1;
  209. }
  210. } else {
  211. return -2;
  212. }
  213. }
  214. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  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. public function verif_syntax($code)
  222. {
  223. // phpcs:enable
  224. $res = 0;
  225. if (dol_strlen($code) < 11) {
  226. $res = -1;
  227. } else {
  228. $res = 0;
  229. }
  230. return $res;
  231. }
  232. }