mod_codecompta_aquarium.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /* Copyright (C) 2004-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005 Eric Seigne <eric.seigne@ryxeo.com>
  4. * Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  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_codecompta_aquarium.php
  22. * \ingroup societe
  23. * \brief File of class to manage accountancy code of thirdparties with Panicum rules
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
  26. /**
  27. * Class to manage accountancy code of thirdparties with Aquarium rules
  28. */
  29. class mod_codecompta_aquarium extends ModeleAccountancyCode
  30. {
  31. /**
  32. * @var string model name
  33. */
  34. public $name = 'Aquarium';
  35. /**
  36. * Dolibarr version of the loaded document
  37. * @var string
  38. */
  39. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  40. public $prefixcustomeraccountancycode;
  41. public $prefixsupplieraccountancycode;
  42. public $position = 20;
  43. /**
  44. * Constructor
  45. */
  46. public function __construct()
  47. {
  48. global $conf;
  49. if (!isset($conf->global->COMPANY_AQUARIUM_MASK_CUSTOMER) || trim($conf->global->COMPANY_AQUARIUM_MASK_CUSTOMER) == '') {
  50. $conf->global->COMPANY_AQUARIUM_MASK_CUSTOMER = '411';
  51. }
  52. if (!isset($conf->global->COMPANY_AQUARIUM_MASK_SUPPLIER) || trim($conf->global->COMPANY_AQUARIUM_MASK_SUPPLIER) == '') {
  53. $conf->global->COMPANY_AQUARIUM_MASK_SUPPLIER = '401';
  54. }
  55. $this->prefixcustomeraccountancycode = $conf->global->COMPANY_AQUARIUM_MASK_CUSTOMER;
  56. $this->prefixsupplieraccountancycode = $conf->global->COMPANY_AQUARIUM_MASK_SUPPLIER;
  57. }
  58. /**
  59. * Return description of module
  60. *
  61. * @param Translate $langs Object langs
  62. * @return string Description of module
  63. */
  64. public function info($langs)
  65. {
  66. global $conf;
  67. global $form;
  68. $langs->load("companies");
  69. $tooltip = '';
  70. $texte = '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  71. $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
  72. $texte .= '<input type="hidden" name="page_y" value="">';
  73. $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
  74. $texte .= '<input type="hidden" name="param1" value="COMPANY_AQUARIUM_MASK_SUPPLIER">';
  75. $texte .= '<input type="hidden" name="param2" value="COMPANY_AQUARIUM_MASK_CUSTOMER">';
  76. $texte .= '<table class="nobordernopadding" width="100%">';
  77. $s1 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value1" value="'.$conf->global->COMPANY_AQUARIUM_MASK_SUPPLIER.'">', $tooltip, 1, 1);
  78. $s2 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value2" value="'.$conf->global->COMPANY_AQUARIUM_MASK_CUSTOMER.'">', $tooltip, 1, 1);
  79. $texte .= '<tr><td>';
  80. // trans remove html entities
  81. $texte .= $langs->trans("ModuleCompanyCodeCustomer".$this->name, '{s2}')."<br>\n";
  82. $texte .= $langs->trans("ModuleCompanyCodeSupplier".$this->name, '{s1}')."<br>\n";
  83. $texte = str_replace(array('{s1}', '{s2}'), array($s1, $s2), $texte);
  84. $texte .= "<br>\n";
  85. if (!isset($conf->global->COMPANY_AQUARIUM_REMOVE_SPECIAL) || !empty($conf->global->$conf->global->COMPANY_AQUARIUM_REMOVE_SPECIAL)) {
  86. $texte .= $langs->trans('RemoveSpecialChars').' = '.yn(1)."<br>\n";
  87. }
  88. //if (!empty($conf->global->COMPANY_AQUARIUM_REMOVE_ALPHA)) $texte.=$langs->trans('COMPANY_AQUARIUM_REMOVE_ALPHA').' = '.yn($conf->global->COMPANY_AQUARIUM_REMOVE_ALPHA)."<br>\n";
  89. if (!empty($conf->global->COMPANY_AQUARIUM_CLEAN_REGEX)) {
  90. $texte .= $langs->trans('COMPANY_AQUARIUM_CLEAN_REGEX').' = '.$conf->global->COMPANY_AQUARIUM_CLEAN_REGEX."<br>\n";
  91. }
  92. $texte .= '</td>';
  93. $texte .= '<td class="right"><input type="submit" class="button button-edit reposition" name="modify" value="'.$langs->trans("Modify").'"></td>';
  94. $texte .= '</tr></table>';
  95. $texte .= '</form>';
  96. return $texte;
  97. }
  98. /**
  99. * Return an example of result returned by getNextValue
  100. *
  101. * @param Translate $langs Object langs
  102. * @param societe $objsoc Object thirdparty
  103. * @param int $type Type of third party (1:customer, 2:supplier, -1:autodetect)
  104. * @return string Return string example
  105. */
  106. public function getExample($langs, $objsoc = 0, $type = -1)
  107. {
  108. $s = '';
  109. $s .= $this->prefixcustomeraccountancycode.'CUSTCODE';
  110. $s .= "<br>\n";
  111. $s .= $this->prefixsupplieraccountancycode.'SUPPCODE';
  112. return $s;
  113. }
  114. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  115. /**
  116. * Set accountancy account code for a third party into this->code
  117. *
  118. * @param DoliDB $db Database handler
  119. * @param Societe $societe Third party object
  120. * @param string $type 'customer' or 'supplier'
  121. * @return int >=0 if OK, <0 if KO
  122. */
  123. public function get_code($db, $societe, $type = '')
  124. {
  125. // phpcs:enable
  126. global $conf;
  127. $i = 0;
  128. $this->db = $db;
  129. dol_syslog("mod_codecompta_aquarium::get_code search code for type=".$type." company=".(!empty($societe->name) ? $societe->name : ''));
  130. // Regle gestion compte compta
  131. if ($type == 'customer') {
  132. $codetouse = (!empty($societe->code_client) ? $societe->code_client : 'CUSTCODE');
  133. $prefix = $this->prefixcustomeraccountancycode;
  134. } elseif ($type == 'supplier') {
  135. $codetouse = (!empty($societe->code_fournisseur) ? $societe->code_fournisseur : 'SUPPCODE');
  136. $prefix = $this->prefixsupplieraccountancycode;
  137. } else {
  138. $this->error = 'Bad value for parameter type';
  139. return -1;
  140. }
  141. //$conf->global->COMPANY_AQUARIUM_CLEAN_REGEX='^..(..)..';
  142. // Remove special char if COMPANY_AQUARIUM_REMOVE_SPECIAL is set to 1 or not set (default)
  143. if (!isset($conf->global->COMPANY_AQUARIUM_REMOVE_SPECIAL) || !empty($conf->global->COMPANY_AQUARIUM_REMOVE_SPECIAL)) {
  144. $codetouse = preg_replace('/([^a-z0-9])/i', '', $codetouse);
  145. }
  146. // Remove special alpha if COMPANY_AQUARIUM_REMOVE_ALPHA is set to 1
  147. if (!empty($conf->global->COMPANY_AQUARIUM_REMOVE_ALPHA)) {
  148. $codetouse = preg_replace('/([a-z])/i', '', $codetouse);
  149. }
  150. // Apply a regex replacement pattern on code if COMPANY_AQUARIUM_CLEAN_REGEX is set. Value must be a regex with parenthesis. The part into parenthesis is kept, the rest removed.
  151. if (!empty($conf->global->COMPANY_AQUARIUM_CLEAN_REGEX)) { // Example: $conf->global->COMPANY_AQUARIUM_CLEAN_REGEX='^..(..)..';
  152. $codetouse = preg_replace('/'.$conf->global->COMPANY_AQUARIUM_CLEAN_REGEX.'/', '\1\2\3', $codetouse);
  153. }
  154. $codetouse = $prefix.strtoupper($codetouse);
  155. $is_dispo = $this->verif($db, $codetouse, $societe, $type);
  156. if (!$is_dispo) {
  157. $this->code = $codetouse;
  158. } else {
  159. // Pour retour
  160. $this->code = $codetouse;
  161. }
  162. dol_syslog("mod_codecompta_aquarium::get_code found code=".$this->code);
  163. return $is_dispo;
  164. }
  165. /**
  166. * Return if a code is available
  167. *
  168. * @param DoliDB $db Database handler
  169. * @param string $code Code of third party
  170. * @param Societe $societe Object third party
  171. * @param string $type 'supplier' or 'customer'
  172. * @return int 0 if OK but not available, >0 if OK and available, <0 if KO
  173. */
  174. public function verif($db, $code, $societe, $type)
  175. {
  176. $sql = "SELECT ";
  177. if ($type == 'customer') {
  178. $sql .= "code_compta";
  179. } elseif ($type == 'supplier') {
  180. $sql .= "code_compta_fournisseur";
  181. }
  182. $sql .= " FROM ".MAIN_DB_PREFIX."societe";
  183. $sql .= " WHERE ";
  184. if ($type == 'customer') {
  185. $sql .= "code_compta";
  186. } elseif ($type == 'supplier') {
  187. $sql .= "code_compta_fournisseur";
  188. }
  189. $sql .= " = '".$db->escape($code)."'";
  190. if (!empty($societe->id)) {
  191. $sql .= " AND rowid <> ".$societe->id;
  192. }
  193. $resql = $db->query($sql);
  194. if ($resql) {
  195. if ($db->num_rows($resql) == 0) {
  196. dol_syslog("mod_codecompta_aquarium::verif code '".$code."' available");
  197. return 1; // Dispo
  198. } else {
  199. dol_syslog("mod_codecompta_aquarium::verif code '".$code."' not available");
  200. return 0; // Non dispo
  201. }
  202. } else {
  203. $this->error = $db->error()." sql=".$sql;
  204. return -1; // Erreur
  205. }
  206. }
  207. }