mod_codecompta_digitaria.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2019 Alexandre Spangaro <aspangaro@open-dsi.fr>
  5. * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
  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 <https://www.gnu.org/licenses/>.
  19. * or see https://www.gnu.org/
  20. */
  21. /**
  22. * \file htdocs/core/modules/societe/mod_codecompta_digitaria.php
  23. * \ingroup societe
  24. * \brief File of class to manage accountancy code of thirdparties with Digitaria rules
  25. */
  26. require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
  27. /**
  28. * Class to manage accountancy code of thirdparties with Digitaria rules
  29. */
  30. class mod_codecompta_digitaria extends ModeleAccountancyCode
  31. {
  32. /**
  33. * @var string model name
  34. */
  35. public $name = 'Digitaria';
  36. /**
  37. * Dolibarr version of the loaded document
  38. * @var string
  39. */
  40. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  41. /**
  42. * Prefix customer accountancy code
  43. * @var string
  44. */
  45. public $prefixcustomeraccountancycode;
  46. /**
  47. * Prefix supplier accountancy code
  48. * @var string
  49. */
  50. public $prefixsupplieraccountancycode;
  51. public $position = 30;
  52. /**
  53. * Constructor
  54. */
  55. public function __construct()
  56. {
  57. global $conf, $langs;
  58. if (!isset($conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER) || trim($conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER) == '') {
  59. $conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER = '411';
  60. }
  61. if (!isset($conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER) || trim($conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER) == '') {
  62. $conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER = '401';
  63. }
  64. $this->prefixcustomeraccountancycode = $conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER;
  65. $this->prefixsupplieraccountancycode = $conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER;
  66. if (!isset($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER) || trim($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER) == '') {
  67. $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER = '5';
  68. }
  69. if (!isset($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER) || trim($conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER) == '') {
  70. $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER = '5';
  71. }
  72. $this->customeraccountancycodecharacternumber = $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER;
  73. $this->supplieraccountancycodecharacternumber = $conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER;
  74. }
  75. /**
  76. * Return description of module
  77. *
  78. * @param Translate $langs Object langs
  79. * @return string Description of module
  80. */
  81. public function info($langs)
  82. {
  83. global $conf, $form;
  84. $tooltip = '';
  85. $texte = '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  86. $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
  87. $texte .= '<input type="hidden" name="page_y" value="">';
  88. $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
  89. $texte .= '<input type="hidden" name="param1" value="COMPANY_DIGITARIA_MASK_SUPPLIER">';
  90. $texte .= '<input type="hidden" name="param2" value="COMPANY_DIGITARIA_MASK_CUSTOMER">';
  91. $texte .= '<input type="hidden" name="param3" value="COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER">';
  92. $texte .= '<input type="hidden" name="param4" value="COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER">';
  93. $texte .= '<input type="hidden" name="param5" value="COMPANY_DIGITARIA_CLEAN_WORDS">';
  94. $texte .= '<table class="nobordernopadding centpercent">';
  95. $s1 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value1" value="'.$conf->global->COMPANY_DIGITARIA_MASK_SUPPLIER.'">', $tooltip, 1, 1);
  96. $s2 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value2" value="'.$conf->global->COMPANY_DIGITARIA_MASK_CUSTOMER.'">', $tooltip, 1, 1);
  97. $s3 = $form->textwithpicto('<input type="text" class="flat" size="2" name="value3" value="'.$conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_SUPPLIER.'">', $tooltip, 1, 1);
  98. $s4 = $form->textwithpicto('<input type="text" class="flat" size="2" name="value4" value="'.$conf->global->COMPANY_DIGITARIA_MASK_NBCHARACTER_CUSTOMER.'">', $tooltip, 1, 1);
  99. $texte .= '<tr><td>';
  100. // trans remove html entities
  101. $texte .= $langs->trans("ModuleCompanyCodeCustomer".$this->name, '{s2}', '{s4}')."<br>\n";
  102. $texte .= $langs->trans("ModuleCompanyCodeSupplier".$this->name, '{s1}', '{s3}')."<br>\n";
  103. $texte = str_replace(array('{s1}', '{s2}', '{s3}', '{s4}'), array($s1, $s2, $s3, $s4), $texte);
  104. $texte .= "<br>\n";
  105. // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default)
  106. if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || !empty($conf->global->$conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) {
  107. $texte .= $langs->trans('RemoveSpecialChars').' = '.yn(1)."<br>\n";
  108. }
  109. // Apply a regex replacement pattern on code if COMPANY_DIGITARIA_CLEAN_REGEX is set. Value must be a regex with parenthesis. The part into parenthesis is kept, the rest removed.
  110. if (!empty($conf->global->COMPANY_DIGITARIA_CLEAN_REGEX)) {
  111. $texte .= $langs->trans('COMPANY_DIGITARIA_CLEAN_REGEX').' = '.$conf->global->COMPANY_DIGITARIA_CLEAN_REGEX."<br>\n";
  112. }
  113. // Unique index on code if COMPANY_DIGITARIA_UNIQUE_CODE is set to 1 or not set (default)
  114. if (!isset($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE) || !empty($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE)) {
  115. $texte .= $langs->trans('COMPANY_DIGITARIA_UNIQUE_CODE').' = '.yn(1)."<br>\n";
  116. }
  117. $texte .= '</td>';
  118. $texte .= '<td class="right"><input type="submit" class="button button-edit reposition" name="modify" value="'.$langs->trans("Modify").'"></td>';
  119. $texte .= '</tr>';
  120. $texte .= '<tr><td>';
  121. $texte .= "<br>\n";
  122. $texthelp = $langs->trans("RemoveSpecialWordsHelp");
  123. $texttitle = $langs->trans("RemoveSpecialWords");
  124. $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1);
  125. $texte .= "<br>\n";
  126. $texte .= '<textarea class="flat" cols="60" name="value5">';
  127. if (!empty($conf->global->COMPANY_DIGITARIA_CLEAN_WORDS)) {
  128. $texte .= $conf->global->COMPANY_DIGITARIA_CLEAN_WORDS;
  129. }
  130. $texte .= '</textarea>';
  131. $texte .= '</tr></table>';
  132. $texte .= '</form>';
  133. return $texte;
  134. }
  135. /**
  136. * Return an example of result returned by getNextValue
  137. *
  138. * @param Translate $langs Object langs
  139. * @param Societe $objsoc Object thirdparty
  140. * @param int $type Type of third party (1:customer, 2:supplier, -1:autodetect)
  141. * @return string Example
  142. */
  143. public function getExample($langs, $objsoc = 0, $type = -1)
  144. {
  145. global $conf, $mysoc;
  146. $s = $langs->trans("ThirdPartyName").": ".$mysoc->name;
  147. $s .= "<br>\n";
  148. if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) {
  149. $thirdpartylabelexample = preg_replace('/([^a-z0-9])/i', '', $mysoc->name);
  150. }
  151. $s .= "<br>\n";
  152. $s .= $this->prefixcustomeraccountancycode.strtoupper(substr($thirdpartylabelexample, 0, $this->customeraccountancycodecharacternumber));
  153. $s .= "<br>\n";
  154. $s .= $this->prefixsupplieraccountancycode.strtoupper(substr($thirdpartylabelexample, 0, $this->supplieraccountancycodecharacternumber));
  155. return $s;
  156. }
  157. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  158. /**
  159. * Set accountancy account code for a third party into this->code
  160. *
  161. * @param DoliDB $db Database handler
  162. * @param Societe $societe Third party object
  163. * @param int $type 'customer' or 'supplier'
  164. * @return int >=0 if OK, <0 if KO
  165. */
  166. public function get_code($db, $societe, $type = '')
  167. {
  168. // phpcs:enable
  169. global $conf;
  170. $i = 0;
  171. $this->code = '';
  172. $disponibility = 0;
  173. if (is_object($societe)) {
  174. dol_syslog("mod_codecompta_digitaria::get_code search code for type=".$type." & company=".(!empty($societe->name) ? $societe->name : ''));
  175. if ($type == 'supplier') {
  176. $codetouse = $societe->name;
  177. $prefix = $this->prefixsupplieraccountancycode;
  178. $width = $this->supplieraccountancycodecharacternumber;
  179. } elseif ($type == 'customer') {
  180. $codetouse = $societe->name;
  181. $prefix = $this->prefixcustomeraccountancycode;
  182. $width = $this->customeraccountancycodecharacternumber;
  183. } else {
  184. $this->error = 'Bad value for parameter type';
  185. return -1;
  186. }
  187. // Clean declared words
  188. if (!empty($conf->global->COMPANY_DIGITARIA_CLEAN_WORDS)) {
  189. $cleanWords = explode(";", $conf->global->COMPANY_DIGITARIA_CLEAN_WORDS);
  190. $codetouse = str_replace($cleanWords, "", $codetouse);
  191. }
  192. // Remove special char if COMPANY_DIGITARIA_REMOVE_SPECIAL is set to 1 or not set (default)
  193. if (!isset($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL) || !empty($conf->global->COMPANY_DIGITARIA_REMOVE_SPECIAL)) {
  194. $codetouse = preg_replace('/([^a-z0-9])/i', '', $codetouse);
  195. }
  196. // Apply a regex replacement pattern on code if COMPANY_DIGITARIA_CLEAN_REGEX is set. Value must be a regex with parenthesis. The part into parenthesis is kept, the rest removed.
  197. if (!empty($conf->global->COMPANY_DIGITARIA_CLEAN_REGEX)) { // Example: $conf->global->COMPANY_DIGITARIA_CLEAN_REGEX='^..(..)..';
  198. $codetouse = preg_replace('/'.$conf->global->COMPANY_DIGITARIA_CLEAN_REGEX.'/', '\1\2\3', $codetouse);
  199. }
  200. $this->code = $prefix.strtoupper(substr($codetouse, 0, $width));
  201. dol_syslog("mod_codecompta_digitaria::get_code search code proposed=".$this->code, LOG_DEBUG);
  202. // Unique index on code if COMPANY_DIGITARIA_UNIQUE_CODE is set to 1 or not set (default)
  203. if (!isset($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE) || !empty($conf->global->COMPANY_DIGITARIA_UNIQUE_CODE)) {
  204. $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type);
  205. while ($disponibility <> 0 && $i < 1000) {
  206. $widthsupplier = $this->supplieraccountancycodecharacternumber;
  207. $widthcustomer = $this->customeraccountancycodecharacternumber;
  208. if ($i <= 9) {
  209. $a = 1;
  210. }
  211. if ($i >= 10 && $i <= 99) {
  212. $a = 2;
  213. }
  214. if ($i >= 100 && $i <= 999) {
  215. $a = 3;
  216. }
  217. if ($type == 'supplier') {
  218. $this->code = $prefix.strtoupper(substr($codetouse, 0, $widthsupplier - $a)).$i;
  219. } elseif ($type == 'customer') {
  220. $this->code = $prefix.strtoupper(substr($codetouse, 0, $widthcustomer - $a)).$i;
  221. }
  222. $disponibility = $this->checkIfAccountancyCodeIsAlreadyUsed($db, $this->code, $type);
  223. $i++;
  224. }
  225. } else {
  226. $disponibility == 0;
  227. }
  228. }
  229. if ($disponibility == 0) {
  230. return 0; // return ok
  231. } else {
  232. return -1; // return ko
  233. }
  234. }
  235. /**
  236. * Check accountancy account code for a third party into this->code
  237. *
  238. * @param DoliDB $db Database handler
  239. * @param string $code Code of third party
  240. * @param int $type 'customer' or 'supplier'
  241. * @return int >=0 if OK, <0 if KO
  242. */
  243. public function checkIfAccountancyCodeIsAlreadyUsed($db, $code, $type = '')
  244. {
  245. global $conf;
  246. if ($type == 'supplier') {
  247. if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
  248. $typethirdparty = 'accountancy_code_supplier';
  249. } else {
  250. $typethirdparty = 'code_compta_fournisseur';
  251. }
  252. } elseif ($type == 'customer') {
  253. if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
  254. $typethirdparty = 'accountancy_code_customer';
  255. } else {
  256. $typethirdparty = 'code_compta';
  257. }
  258. } else {
  259. $this->error = 'Bad value for parameter type';
  260. return -1;
  261. }
  262. if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
  263. $sql = "SELECT " . $typethirdparty . " FROM " . MAIN_DB_PREFIX . "societe_perentity";
  264. $sql .= " WHERE " . $typethirdparty . " = '" . $db->escape($code) . "'";
  265. } else {
  266. $sql = "SELECT " . $typethirdparty . " FROM " . MAIN_DB_PREFIX . "societe";
  267. $sql .= " WHERE " . $typethirdparty . " = '" . $db->escape($code) . "'";
  268. }
  269. $sql .= " AND entity IN (".getEntity('societe').")";
  270. $resql = $db->query($sql);
  271. if ($resql) {
  272. if ($db->num_rows($resql) == 0) {
  273. dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' available");
  274. return 0; // Available
  275. } else {
  276. dol_syslog("mod_codecompta_digitaria::checkIfAccountancyCodeIsAlreadyUsed '".$code."' not available");
  277. return -1; // Not available
  278. }
  279. } else {
  280. $this->error = $db->error()." sql=".$sql;
  281. return -2; // Error
  282. }
  283. }
  284. }