dolibarr 19.0.3
mod_codecompta_aquarium.php
Go to the documentation of this file.
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
26require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
27
28
33{
37 public $name = 'Aquarium';
38
42 public $code;
43
48 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
49
50 public $prefixcustomeraccountancycode;
51
52 public $prefixsupplieraccountancycode;
53
54 public $position = 20;
55
56
60 public function __construct()
61 {
62 global $conf;
63 if (!isset($conf->global->COMPANY_AQUARIUM_MASK_CUSTOMER) || trim($conf->global->COMPANY_AQUARIUM_MASK_CUSTOMER) == '') {
64 $conf->global->COMPANY_AQUARIUM_MASK_CUSTOMER = '411';
65 }
66 if (!isset($conf->global->COMPANY_AQUARIUM_MASK_SUPPLIER) || trim($conf->global->COMPANY_AQUARIUM_MASK_SUPPLIER) == '') {
67 $conf->global->COMPANY_AQUARIUM_MASK_SUPPLIER = '401';
68 }
69
70 if (getDolGlobalString('COMPANY_AQUARIUM_NO_PREFIX')) {
71 $this->prefixcustomeraccountancycode = '';
72 $this->prefixsupplieraccountancycode = '';
73 } else {
74 $this->prefixcustomeraccountancycode = $conf->global->COMPANY_AQUARIUM_MASK_CUSTOMER;
75 $this->prefixsupplieraccountancycode = $conf->global->COMPANY_AQUARIUM_MASK_SUPPLIER;
76 }
77 }
78
79
86 public function info($langs)
87 {
88 global $conf;
89 global $form;
90
91 $langs->load("companies");
92
93 $tooltip = '';
94 $texte = '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
95 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
96 $texte .= '<input type="hidden" name="page_y" value="">';
97 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
98 $texte .= '<input type="hidden" name="param1" value="COMPANY_AQUARIUM_MASK_SUPPLIER">';
99 $texte .= '<input type="hidden" name="param2" value="COMPANY_AQUARIUM_MASK_CUSTOMER">';
100 $texte .= '<table class="nobordernopadding" width="100%">';
101 $s1 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value1" value="' . getDolGlobalString('COMPANY_AQUARIUM_MASK_SUPPLIER').'">', $tooltip, 1, 1);
102 $s2 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value2" value="' . getDolGlobalString('COMPANY_AQUARIUM_MASK_CUSTOMER').'">', $tooltip, 1, 1);
103 $texte .= '<tr><td>';
104 // trans remove html entities
105 $texte .= $langs->trans("ModuleCompanyCodeCustomer".$this->name, '{s2}')."<br>\n";
106 $texte .= $langs->trans("ModuleCompanyCodeSupplier".$this->name, '{s1}')."<br>\n";
107 $texte = str_replace(array('{s1}', '{s2}'), array($s1, $s2), $texte);
108 $texte .= "<br>\n";
109 if (!isset($conf->global->COMPANY_AQUARIUM_REMOVE_SPECIAL) || !empty($conf->global->$conf->global->COMPANY_AQUARIUM_REMOVE_SPECIAL)) {
110 $texte .= $langs->trans('RemoveSpecialChars').' = '.yn(1)."<br>\n";
111 }
112 //if (!empty($conf->global->COMPANY_AQUARIUM_REMOVE_ALPHA)) $texte.=$langs->trans('COMPANY_AQUARIUM_REMOVE_ALPHA').' = '.yn($conf->global->COMPANY_AQUARIUM_REMOVE_ALPHA)."<br>\n";
113 if (getDolGlobalString('COMPANY_AQUARIUM_CLEAN_REGEX')) {
114 $texte .= $langs->trans('COMPANY_AQUARIUM_CLEAN_REGEX').' = ' . getDolGlobalString('COMPANY_AQUARIUM_CLEAN_REGEX')."<br>\n";
115 }
116
117 if (getDolGlobalString('COMPANY_AQUARIUM_NO_PREFIX')) {
118 $texte .= $langs->trans('COMPANY_AQUARIUM_NO_PREFIX').' = ' . getDolGlobalString('COMPANY_AQUARIUM_NO_PREFIX')."<br>\n";
119 }
120 $texte .= '</td>';
121 $texte .= '<td class="right"><input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"></td>';
122 $texte .= '</tr></table>';
123 $texte .= '</form>';
124
125 return $texte;
126 }
127
136 public function getExample($langs, $objsoc = 0, $type = -1)
137 {
138 $s = '';
139 $s .= $this->prefixcustomeraccountancycode.'CUSTCODE';
140 $s .= "<br>\n";
141 $s .= $this->prefixsupplieraccountancycode.'SUPPCODE';
142 return $s;
143 }
144
145
146 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
155 public function get_code($db, $societe, $type = '')
156 {
157 // phpcs:enable
158 global $conf;
159
160 $i = 0;
161 $this->db = $db;
162
163 dol_syslog("mod_codecompta_aquarium::get_code search code for type=".$type." company=".(!empty($societe->name) ? $societe->name : ''));
164
165 // Regle gestion compte compta
166 if ($type == 'customer') {
167 $codetouse = (!empty($societe->code_client) ? $societe->code_client : 'CUSTCODE');
168 $prefix = $this->prefixcustomeraccountancycode;
169 } elseif ($type == 'supplier') {
170 $codetouse = (!empty($societe->code_fournisseur) ? $societe->code_fournisseur : 'SUPPCODE');
171 $prefix = $this->prefixsupplieraccountancycode;
172 } else {
173 $this->error = 'Bad value for parameter type';
174 return -1;
175 }
176
177 //$conf->global->COMPANY_AQUARIUM_CLEAN_REGEX='^..(..)..';
178
179 // Remove special char if COMPANY_AQUARIUM_REMOVE_SPECIAL is set to 1 or not set (default)
180 if (!isset($conf->global->COMPANY_AQUARIUM_REMOVE_SPECIAL) || getDolGlobalString('COMPANY_AQUARIUM_REMOVE_SPECIAL')) {
181 $codetouse = preg_replace('/([^a-z0-9])/i', '', $codetouse);
182 }
183 // Remove special alpha if COMPANY_AQUARIUM_REMOVE_ALPHA is set to 1
184 if (getDolGlobalString('COMPANY_AQUARIUM_REMOVE_ALPHA')) {
185 $codetouse = preg_replace('/([a-z])/i', '', $codetouse);
186 }
187 // 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.
188 if (getDolGlobalString('COMPANY_AQUARIUM_CLEAN_REGEX')) { // Example: $conf->global->COMPANY_AQUARIUM_CLEAN_REGEX='^..(..)..';
189 $codetouse = preg_replace('/' . getDolGlobalString('COMPANY_AQUARIUM_CLEAN_REGEX').'/', '\1\2\3', $codetouse);
190 }
191
192 $codetouse = $prefix.strtoupper($codetouse);
193
194 $is_dispo = $this->verif($db, $codetouse, $societe, $type);
195 if (!$is_dispo) {
196 $this->code = $codetouse;
197 } else {
198 // Pour retour
199 $this->code = $codetouse;
200 }
201 dol_syslog("mod_codecompta_aquarium::get_code found code=".$this->code);
202 return $is_dispo;
203 }
204
205
215 public function verif($db, $code, $societe, $type)
216 {
217 $sql = "SELECT ";
218 if ($type == 'customer') {
219 $sql .= "code_compta";
220 } elseif ($type == 'supplier') {
221 $sql .= "code_compta_fournisseur";
222 }
223 $sql .= " FROM ".MAIN_DB_PREFIX."societe";
224 $sql .= " WHERE ";
225 if ($type == 'customer') {
226 $sql .= "code_compta";
227 } elseif ($type == 'supplier') {
228 $sql .= "code_compta_fournisseur";
229 }
230 $sql .= " = '".$db->escape($code)."'";
231 if (!empty($societe->id)) {
232 $sql .= " AND rowid <> ".$societe->id;
233 }
234
235 $resql = $db->query($sql);
236 if ($resql) {
237 if ($db->num_rows($resql) == 0) {
238 dol_syslog("mod_codecompta_aquarium::verif code '".$code."' available");
239 return 1; // Dispo
240 } else {
241 dol_syslog("mod_codecompta_aquarium::verif code '".$code."' not available");
242 return 0; // Non dispo
243 }
244 } else {
245 $this->error = $db->error()." sql=".$sql;
246 return -1; // Erreur
247 }
248 }
249}
Parent class for third parties accountancy code generators.
Class to manage accountancy code of thirdparties with Aquarium rules.
info($langs)
Return description of module.
verif($db, $code, $societe, $type)
Return if a code is available.
get_code($db, $societe, $type='')
Set accountancy account code for a third party into this->code.
getExample($langs, $objsoc=0, $type=-1)
Return an example of result returned by getNextValue.
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:124