mod_codecompta_aquarium.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. if (!empty($conf->global->COMPANY_AQUARIUM_NO_PREFIX)) {
  56. $this->prefixcustomeraccountancycode = '';
  57. $this->prefixsupplieraccountancycode = '';
  58. } else {
  59. $this->prefixcustomeraccountancycode = $conf->global->COMPANY_AQUARIUM_MASK_CUSTOMER;
  60. $this->prefixsupplieraccountancycode = $conf->global->COMPANY_AQUARIUM_MASK_SUPPLIER;
  61. }
  62. }
  63. /**
  64. * Return description of module
  65. *
  66. * @param Translate $langs Object langs
  67. * @return string Description of module
  68. */
  69. public function info($langs)
  70. {
  71. global $conf;
  72. global $form;
  73. $langs->load("companies");
  74. $tooltip = '';
  75. $texte = '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  76. $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
  77. $texte .= '<input type="hidden" name="page_y" value="">';
  78. $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
  79. $texte .= '<input type="hidden" name="param1" value="COMPANY_AQUARIUM_MASK_SUPPLIER">';
  80. $texte .= '<input type="hidden" name="param2" value="COMPANY_AQUARIUM_MASK_CUSTOMER">';
  81. $texte .= '<table class="nobordernopadding" width="100%">';
  82. $s1 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value1" value="'.$conf->global->COMPANY_AQUARIUM_MASK_SUPPLIER.'">', $tooltip, 1, 1);
  83. $s2 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value2" value="'.$conf->global->COMPANY_AQUARIUM_MASK_CUSTOMER.'">', $tooltip, 1, 1);
  84. $texte .= '<tr><td>';
  85. // trans remove html entities
  86. $texte .= $langs->trans("ModuleCompanyCodeCustomer".$this->name, '{s2}')."<br>\n";
  87. $texte .= $langs->trans("ModuleCompanyCodeSupplier".$this->name, '{s1}')."<br>\n";
  88. $texte = str_replace(array('{s1}', '{s2}'), array($s1, $s2), $texte);
  89. $texte .= "<br>\n";
  90. if (!isset($conf->global->COMPANY_AQUARIUM_REMOVE_SPECIAL) || !empty($conf->global->$conf->global->COMPANY_AQUARIUM_REMOVE_SPECIAL)) {
  91. $texte .= $langs->trans('RemoveSpecialChars').' = '.yn(1)."<br>\n";
  92. }
  93. //if (!empty($conf->global->COMPANY_AQUARIUM_REMOVE_ALPHA)) $texte.=$langs->trans('COMPANY_AQUARIUM_REMOVE_ALPHA').' = '.yn($conf->global->COMPANY_AQUARIUM_REMOVE_ALPHA)."<br>\n";
  94. if (!empty($conf->global->COMPANY_AQUARIUM_CLEAN_REGEX)) {
  95. $texte .= $langs->trans('COMPANY_AQUARIUM_CLEAN_REGEX').' = '.$conf->global->COMPANY_AQUARIUM_CLEAN_REGEX."<br>\n";
  96. }
  97. if (!empty($conf->global->COMPANY_AQUARIUM_NO_PREFIX)) {
  98. $texte .= $langs->trans('COMPANY_AQUARIUM_NO_PREFIX').' = '.$conf->global->COMPANY_AQUARIUM_NO_PREFIX."<br>\n";
  99. }
  100. $texte .= '</td>';
  101. $texte .= '<td class="right"><input type="submit" class="button button-edit reposition" name="modify" value="'.$langs->trans("Modify").'"></td>';
  102. $texte .= '</tr></table>';
  103. $texte .= '</form>';
  104. return $texte;
  105. }
  106. /**
  107. * Return an example of result returned by getNextValue
  108. *
  109. * @param Translate $langs Object langs
  110. * @param societe $objsoc Object thirdparty
  111. * @param int $type Type of third party (1:customer, 2:supplier, -1:autodetect)
  112. * @return string Return string example
  113. */
  114. public function getExample($langs, $objsoc = 0, $type = -1)
  115. {
  116. $s = '';
  117. $s .= $this->prefixcustomeraccountancycode.'CUSTCODE';
  118. $s .= "<br>\n";
  119. $s .= $this->prefixsupplieraccountancycode.'SUPPCODE';
  120. return $s;
  121. }
  122. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  123. /**
  124. * Set accountancy account code for a third party into this->code
  125. *
  126. * @param DoliDB $db Database handler
  127. * @param Societe $societe Third party object
  128. * @param string $type 'customer' or 'supplier'
  129. * @return int >=0 if OK, <0 if KO
  130. */
  131. public function get_code($db, $societe, $type = '')
  132. {
  133. // phpcs:enable
  134. global $conf;
  135. $i = 0;
  136. $this->db = $db;
  137. dol_syslog("mod_codecompta_aquarium::get_code search code for type=".$type." company=".(!empty($societe->name) ? $societe->name : ''));
  138. // Regle gestion compte compta
  139. if ($type == 'customer') {
  140. $codetouse = (!empty($societe->code_client) ? $societe->code_client : 'CUSTCODE');
  141. $prefix = $this->prefixcustomeraccountancycode;
  142. } elseif ($type == 'supplier') {
  143. $codetouse = (!empty($societe->code_fournisseur) ? $societe->code_fournisseur : 'SUPPCODE');
  144. $prefix = $this->prefixsupplieraccountancycode;
  145. } else {
  146. $this->error = 'Bad value for parameter type';
  147. return -1;
  148. }
  149. //$conf->global->COMPANY_AQUARIUM_CLEAN_REGEX='^..(..)..';
  150. // Remove special char if COMPANY_AQUARIUM_REMOVE_SPECIAL is set to 1 or not set (default)
  151. if (!isset($conf->global->COMPANY_AQUARIUM_REMOVE_SPECIAL) || !empty($conf->global->COMPANY_AQUARIUM_REMOVE_SPECIAL)) {
  152. $codetouse = preg_replace('/([^a-z0-9])/i', '', $codetouse);
  153. }
  154. // Remove special alpha if COMPANY_AQUARIUM_REMOVE_ALPHA is set to 1
  155. if (!empty($conf->global->COMPANY_AQUARIUM_REMOVE_ALPHA)) {
  156. $codetouse = preg_replace('/([a-z])/i', '', $codetouse);
  157. }
  158. // 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.
  159. if (!empty($conf->global->COMPANY_AQUARIUM_CLEAN_REGEX)) { // Example: $conf->global->COMPANY_AQUARIUM_CLEAN_REGEX='^..(..)..';
  160. $codetouse = preg_replace('/'.$conf->global->COMPANY_AQUARIUM_CLEAN_REGEX.'/', '\1\2\3', $codetouse);
  161. }
  162. $codetouse = $prefix.strtoupper($codetouse);
  163. $is_dispo = $this->verif($db, $codetouse, $societe, $type);
  164. if (!$is_dispo) {
  165. $this->code = $codetouse;
  166. } else {
  167. // Pour retour
  168. $this->code = $codetouse;
  169. }
  170. dol_syslog("mod_codecompta_aquarium::get_code found code=".$this->code);
  171. return $is_dispo;
  172. }
  173. /**
  174. * Return if a code is available
  175. *
  176. * @param DoliDB $db Database handler
  177. * @param string $code Code of third party
  178. * @param Societe $societe Object third party
  179. * @param string $type 'supplier' or 'customer'
  180. * @return int 0 if OK but not available, >0 if OK and available, <0 if KO
  181. */
  182. public function verif($db, $code, $societe, $type)
  183. {
  184. $sql = "SELECT ";
  185. if ($type == 'customer') {
  186. $sql .= "code_compta";
  187. } elseif ($type == 'supplier') {
  188. $sql .= "code_compta_fournisseur";
  189. }
  190. $sql .= " FROM ".MAIN_DB_PREFIX."societe";
  191. $sql .= " WHERE ";
  192. if ($type == 'customer') {
  193. $sql .= "code_compta";
  194. } elseif ($type == 'supplier') {
  195. $sql .= "code_compta_fournisseur";
  196. }
  197. $sql .= " = '".$db->escape($code)."'";
  198. if (!empty($societe->id)) {
  199. $sql .= " AND rowid <> ".$societe->id;
  200. }
  201. $resql = $db->query($sql);
  202. if ($resql) {
  203. if ($db->num_rows($resql) == 0) {
  204. dol_syslog("mod_codecompta_aquarium::verif code '".$code."' available");
  205. return 1; // Dispo
  206. } else {
  207. dol_syslog("mod_codecompta_aquarium::verif code '".$code."' not available");
  208. return 0; // Non dispo
  209. }
  210. } else {
  211. $this->error = $db->error()." sql=".$sql;
  212. return -1; // Erreur
  213. }
  214. }
  215. }