mod_codeclient_elephant.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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@inodbox.com>
  5. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2013-2018 Philippe Grand <philippe.grand@atoo-net.com>
  7. * Copyright (C) 2020 Frédéric France <frederic.france@netlogic.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. * or see https://www.gnu.org/
  22. */
  23. /**
  24. * \file htdocs/core/modules/societe/mod_codeclient_elephant.php
  25. * \ingroup societe
  26. * \brief File of class to manage third party code with elephant rule
  27. */
  28. require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
  29. /**
  30. * Class to manage third party code with elephant rule
  31. */
  32. class mod_codeclient_elephant extends ModeleThirdPartyCode
  33. {
  34. /**
  35. * @var string model name
  36. */
  37. public $name = 'Elephant';
  38. /**
  39. * @var int Code modifiable
  40. */
  41. public $code_modifiable;
  42. /**
  43. * @var int Code modifiable si il est invalide
  44. */
  45. public $code_modifiable_invalide;
  46. /**
  47. * @var int Code modifiables si il est null
  48. */
  49. public $code_modifiable_null;
  50. /**
  51. * @var int Code facultatif
  52. */
  53. public $code_null;
  54. /**
  55. * Dolibarr version of the loaded document
  56. * @var string
  57. */
  58. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  59. /**
  60. * @var int Automatic numbering
  61. */
  62. public $code_auto;
  63. /**
  64. * @var string search string
  65. */
  66. public $searchcode;
  67. /**
  68. * @var int Nombre de chiffres du compteur
  69. */
  70. public $numbitcounter;
  71. /**
  72. * @var int thirdparty prefix is required when using {pre}
  73. */
  74. public $prefixIsRequired;
  75. /**
  76. * Constructor
  77. */
  78. public function __construct()
  79. {
  80. $this->code_null = 0;
  81. $this->code_modifiable = 1;
  82. $this->code_modifiable_invalide = 1;
  83. $this->code_modifiable_null = 1;
  84. $this->code_auto = 1;
  85. $this->prefixIsRequired = 0;
  86. }
  87. /**
  88. * Return description of module
  89. *
  90. * @param Translate $langs Object langs
  91. * @return string Description of module
  92. */
  93. public function info($langs)
  94. {
  95. global $conf, $mc;
  96. global $form;
  97. $langs->load("companies");
  98. $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
  99. $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
  100. $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  101. $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
  102. $texte .= '<input type="hidden" name="page_y" value="">';
  103. $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
  104. $texte .= '<input type="hidden" name="param1" value="COMPANY_ELEPHANT_MASK_CUSTOMER">';
  105. $texte .= '<input type="hidden" name="param2" value="COMPANY_ELEPHANT_MASK_SUPPLIER">';
  106. $texte .= '<table class="nobordernopadding" width="100%">';
  107. $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("ThirdParty"), $langs->transnoentities("ThirdParty"));
  108. //$tooltip.=$langs->trans("GenericMaskCodes2"); Not required for third party numbering
  109. $tooltip .= $langs->trans("GenericMaskCodes3");
  110. $tooltip .= $langs->trans("GenericMaskCodes4b");
  111. $tooltip .= $langs->trans("GenericMaskCodes5");
  112. // Parametrage du prefix customers
  113. $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("CustomerCodeModel").'):</td>';
  114. $texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value1" value="'.getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER').'"'.$disabled.'>', $tooltip, 1, 1).'</td>';
  115. $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
  116. $texte .= '</tr>';
  117. // Parametrage du prefix suppliers
  118. $texte .= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("SupplierCodeModel").'):</td>';
  119. $texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value2" value="'.getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER').'"'.$disabled.'>', $tooltip, 1, 1).'</td>';
  120. $texte .= '</tr>';
  121. $texte .= '</table>';
  122. $texte .= '</form>';
  123. return $texte;
  124. }
  125. /**
  126. * Return an example of result returned by getNextValue
  127. *
  128. * @param Translate $langs Object langs
  129. * @param societe $objsoc Object thirdparty
  130. * @param int $type Type of third party (1:customer, 2:supplier, -1:autodetect)
  131. * @return string Return string example
  132. */
  133. public function getExample($langs, $objsoc = 0, $type = -1)
  134. {
  135. $error = 0;
  136. $examplecust = '';
  137. $examplesup = '';
  138. $errmsg = array(
  139. "ErrorBadMask",
  140. "ErrorCantUseRazIfNoYearInMask",
  141. "ErrorCantUseRazInStartedYearIfNoYearMonthInMask",
  142. "ErrorCounterMustHaveMoreThan3Digits",
  143. "ErrorBadMaskBadRazMonth",
  144. "ErrorCantUseRazWithYearOnOneDigit",
  145. );
  146. $cssforerror = (getDolGlobalString('SOCIETE_CODECLIENT_ADDON') == 'mod_codeclient_elephant' ? 'error' : 'opacitymedium');
  147. if ($type != 1) {
  148. $examplecust = $this->getNextValue($objsoc, 0);
  149. if (!$examplecust && ($cssforerror == 'error' || $this->error != 'NotConfigured')) {
  150. $langs->load("errors");
  151. $examplecust = '<span class="'.$cssforerror.'">'.$langs->trans('ErrorBadMask').'</span>';
  152. $error = 1;
  153. }
  154. if (in_array($examplecust, $errmsg)) {
  155. $langs->load("errors");
  156. $examplecust = '<span class="'.$cssforerror.'">'.$langs->trans($examplecust).'</span>';
  157. $error = 1;
  158. }
  159. }
  160. if ($type != 0) {
  161. $examplesup = $this->getNextValue($objsoc, 1);
  162. if (!$examplesup && ($cssforerror == 'error' || $this->error != 'NotConfigured')) {
  163. $langs->load("errors");
  164. $examplesup = '<span class="'.$cssforerror.'">'.$langs->trans('ErrorBadMask').'</span>';
  165. $error = 1;
  166. }
  167. if (in_array($examplesup, $errmsg)) {
  168. $langs->load("errors");
  169. $examplesup = '<span class="'.$cssforerror.'">'.$langs->trans($examplesup).'</span>';
  170. $error = 1;
  171. }
  172. }
  173. if ($type == 0) {
  174. return $examplecust;
  175. } elseif ($type == 1) {
  176. return $examplesup;
  177. } else {
  178. return $examplecust.'<br>'.$examplesup;
  179. }
  180. }
  181. /**
  182. * Return next value
  183. *
  184. * @param Societe $objsoc Object third party
  185. * @param int $type Client ou fournisseur (0:customer, 1:supplier)
  186. * @return string Value if OK, '' if module not configured, <0 if KO
  187. */
  188. public function getNextValue($objsoc = 0, $type = -1)
  189. {
  190. global $db, $conf;
  191. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  192. // Get Mask value
  193. $mask = '';
  194. if ($type == 0) {
  195. $mask = !getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER') ? '' : $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
  196. }
  197. if ($type == 1) {
  198. $mask = !getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER') ? '' : $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
  199. }
  200. if (!$mask) {
  201. $this->error = 'NotConfigured';
  202. return '';
  203. }
  204. $field = '';
  205. $where = '';
  206. if ($type == 0) {
  207. $field = 'code_client';
  208. //$where = ' AND client in (1,2)';
  209. } elseif ($type == 1) {
  210. $field = 'code_fournisseur';
  211. //$where = ' AND fournisseur = 1';
  212. } else {
  213. return -1;
  214. }
  215. $now = dol_now();
  216. $numFinal = get_next_value($db, $mask, 'societe', $field, $where, '', $now);
  217. return $numFinal;
  218. }
  219. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  220. /**
  221. * Check if mask/numbering use prefix
  222. *
  223. * @return int 0 or 1
  224. */
  225. public function verif_prefixIsUsed()
  226. {
  227. // phpcs:enable
  228. global $conf;
  229. $mask = $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
  230. if (preg_match('/\{pre\}/i', $mask)) {
  231. return 1;
  232. }
  233. $mask = $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
  234. if (preg_match('/\{pre\}/i', $mask)) {
  235. return 1;
  236. }
  237. return 0;
  238. }
  239. /**
  240. * Check validity of code according to its rules
  241. *
  242. * @param DoliDB $db Database handler
  243. * @param string $code Code to check/correct
  244. * @param Societe $soc Object third party
  245. * @param int $type 0 = customer/prospect , 1 = supplier
  246. * @return int 0 if OK
  247. * -1 ErrorBadCustomerCodeSyntax
  248. * -2 ErrorCustomerCodeRequired
  249. * -3 ErrorCustomerCodeAlreadyUsed
  250. * -4 ErrorPrefixRequired
  251. * -5 NotConfigured - Setup empty so any value may be ok or not
  252. * -6 Other (see this->error)
  253. */
  254. public function verif($db, &$code, $soc, $type)
  255. {
  256. global $conf;
  257. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  258. $result = 0;
  259. $code = strtoupper(trim($code));
  260. if (empty($code) && $this->code_null && !getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED')) {
  261. $result = 0;
  262. } elseif (empty($code) && (!$this->code_null || getDolGlobalString('MAIN_COMPANY_CODE_ALWAYS_REQUIRED'))) {
  263. $result = -2;
  264. } else {
  265. // Get Mask value
  266. $mask = '';
  267. if ($type == 0) {
  268. $mask = !getDolGlobalString('COMPANY_ELEPHANT_MASK_CUSTOMER') ? '' : $conf->global->COMPANY_ELEPHANT_MASK_CUSTOMER;
  269. }
  270. if ($type == 1) {
  271. $mask = !getDolGlobalString('COMPANY_ELEPHANT_MASK_SUPPLIER') ? '' : $conf->global->COMPANY_ELEPHANT_MASK_SUPPLIER;
  272. }
  273. if (!$mask) {
  274. $this->error = 'NotConfigured';
  275. return -5;
  276. }
  277. $result = check_value($mask, $code);
  278. if (is_string($result)) {
  279. $this->error = $result;
  280. return -6;
  281. } else {
  282. $is_dispo = $this->verif_dispo($db, $code, $soc, $type);
  283. if ($is_dispo != 0) {
  284. $result = -3;
  285. }
  286. }
  287. }
  288. dol_syslog("mod_codeclient_elephant::verif type=".$type." result=".$result);
  289. return $result;
  290. }
  291. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  292. /**
  293. * Renvoi si un code est pris ou non (par autre tiers)
  294. *
  295. * @param DoliDB $db Handler acces base
  296. * @param string $code Code a verifier
  297. * @param Societe $soc Objet societe
  298. * @param int $type 0 = customer/prospect , 1 = supplier
  299. * @return int 0 if available, <0 if KO
  300. */
  301. public function verif_dispo($db, $code, $soc, $type = 0)
  302. {
  303. // phpcs:enable
  304. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
  305. if ($type == 1) {
  306. $sql .= " WHERE code_fournisseur = '".$db->escape($code)."'";
  307. } else {
  308. $sql .= " WHERE code_client = '".$db->escape($code)."'";
  309. }
  310. if ($soc->id > 0) {
  311. $sql .= " AND rowid <> ".$soc->id;
  312. }
  313. $sql .= " AND entity IN (".getEntity('societe').")";
  314. $resql = $db->query($sql);
  315. if ($resql) {
  316. if ($db->num_rows($resql) == 0) {
  317. return 0;
  318. } else {
  319. return -1;
  320. }
  321. } else {
  322. return -2;
  323. }
  324. }
  325. }