dolibarr 21.0.0-beta
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * or see https://www.gnu.org/
21 */
22
28require_once DOL_DOCUMENT_ROOT.'/core/modules/societe/modules_societe.class.php';
29
30
35{
39 public $name = 'Aquarium';
40
44 public $code;
45
50 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
51
52 public $prefixcustomeraccountancycode;
53
54 public $prefixsupplieraccountancycode;
55
56 public $position = 20;
57
58
62 public function __construct()
63 {
64 global $conf;
65 if (!isset($conf->global->COMPANY_AQUARIUM_MASK_CUSTOMER) || trim($conf->global->COMPANY_AQUARIUM_MASK_CUSTOMER) == '') {
66 $conf->global->COMPANY_AQUARIUM_MASK_CUSTOMER = '411';
67 }
68 if (!isset($conf->global->COMPANY_AQUARIUM_MASK_SUPPLIER) || trim($conf->global->COMPANY_AQUARIUM_MASK_SUPPLIER) == '') {
69 $conf->global->COMPANY_AQUARIUM_MASK_SUPPLIER = '401';
70 }
71
72 if (getDolGlobalString('COMPANY_AQUARIUM_NO_PREFIX')) {
73 $this->prefixcustomeraccountancycode = '';
74 $this->prefixsupplieraccountancycode = '';
75 } else {
76 $this->prefixcustomeraccountancycode = getDolGlobalString('COMPANY_AQUARIUM_MASK_CUSTOMER');
77 $this->prefixsupplieraccountancycode = getDolGlobalString('COMPANY_AQUARIUM_MASK_SUPPLIER');
78 }
79 }
80
81
88 public function info($langs)
89 {
90 global $form;
91
92 $langs->load("companies");
93
94 $tooltip = '';
95 $texte = '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
96 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
97 $texte .= '<input type="hidden" name="page_y" value="">';
98 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
99 $texte .= '<input type="hidden" name="param1" value="COMPANY_AQUARIUM_MASK_SUPPLIER">';
100 $texte .= '<input type="hidden" name="param2" value="COMPANY_AQUARIUM_MASK_CUSTOMER">';
101 $texte .= '<table class="nobordernopadding" width="100%">';
102 $s1 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value1" value="' . getDolGlobalString('COMPANY_AQUARIUM_MASK_SUPPLIER').'">', $tooltip, 1, 'help', 'valignmiddle', 0, 3, $this->name);
103 $s2 = $form->textwithpicto('<input type="text" class="flat" size="4" name="value2" value="' . getDolGlobalString('COMPANY_AQUARIUM_MASK_CUSTOMER').'">', $tooltip, 1, 'help', 'valignmiddle', 0, 3, $this->name);
104 $texte .= '<tr><td>';
105 // trans remove html entities
106 $texte .= $langs->trans("ModuleCompanyCodeCustomer".$this->name, '{s2}')."<br>\n";
107 $texte .= $langs->trans("ModuleCompanyCodeSupplier".$this->name, '{s1}')."<br>\n";
108 $texte = str_replace(array('{s1}', '{s2}'), array($s1, $s2), $texte);
109 $texte .= "<br>\n";
110 if (getDolGlobalInt('COMPANY_AQUARIUM_REMOVE_SPECIAL')) {
111 $texte .= $langs->trans('RemoveSpecialChars').' = '.yn(1)."<br>\n";
112 }
113 //if (!empty($conf->global->COMPANY_AQUARIUM_REMOVE_ALPHA)) $texte.=$langs->trans('COMPANY_AQUARIUM_REMOVE_ALPHA').' = '.yn($conf->global->COMPANY_AQUARIUM_REMOVE_ALPHA)."<br>\n";
114 if (getDolGlobalString('COMPANY_AQUARIUM_CLEAN_REGEX')) {
115 $texte .= $langs->trans('COMPANY_AQUARIUM_CLEAN_REGEX').' = ' . getDolGlobalString('COMPANY_AQUARIUM_CLEAN_REGEX')."<br>\n";
116 }
117
118 if (getDolGlobalString('COMPANY_AQUARIUM_NO_PREFIX')) {
119 $texte .= $langs->trans('COMPANY_AQUARIUM_NO_PREFIX').' = ' . getDolGlobalString('COMPANY_AQUARIUM_NO_PREFIX')."<br>\n";
120 }
121 $texte .= '</td>';
122 $texte .= '<td class="right"><input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"></td>';
123 $texte .= '</tr></table>';
124 $texte .= '</form>';
125
126 return $texte;
127 }
128
137 public function getExample($langs = null, $objsoc = '', $type = -1)
138 {
139 $s = '';
140 $s .= $this->prefixcustomeraccountancycode.'CUSTCODE';
141 $s .= "<br>\n";
142 $s .= $this->prefixsupplieraccountancycode.'SUPPCODE';
143 return $s;
144 }
145
146
147 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
156 public function get_code($db, $societe, $type = '')
157 {
158 // phpcs:enable
159 global $conf;
160
161 $i = 0;
162 $this->db = $db;
163
164 dol_syslog("mod_codecompta_aquarium::get_code search code for type=".$type." company=".(!empty($societe->name) ? $societe->name : ''));
165
166 // Regle gestion compte compta
167 if ($type == 'customer') {
168 $codetouse = (!empty($societe->code_client) ? $societe->code_client : 'CUSTCODE');
169 $prefix = $this->prefixcustomeraccountancycode;
170 } elseif ($type == 'supplier') {
171 $codetouse = (!empty($societe->code_fournisseur) ? $societe->code_fournisseur : 'SUPPCODE');
172 $prefix = $this->prefixsupplieraccountancycode;
173 } else {
174 $this->error = 'Bad value for parameter type';
175 return -1;
176 }
177
178 //$conf->global->COMPANY_AQUARIUM_CLEAN_REGEX='^..(..)..';
179
180 // Remove special char if COMPANY_AQUARIUM_REMOVE_SPECIAL is set to 1 or not set (default)
181 if (!isset($conf->global->COMPANY_AQUARIUM_REMOVE_SPECIAL) || getDolGlobalString('COMPANY_AQUARIUM_REMOVE_SPECIAL')) {
182 $codetouse = preg_replace('/([^a-z0-9])/i', '', $codetouse);
183 }
184 // Remove special alpha if COMPANY_AQUARIUM_REMOVE_ALPHA is set to 1
185 if (getDolGlobalString('COMPANY_AQUARIUM_REMOVE_ALPHA')) {
186 $codetouse = preg_replace('/([a-z])/i', '', $codetouse);
187 }
188 // 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.
189 if (getDolGlobalString('COMPANY_AQUARIUM_CLEAN_REGEX')) { // Example: $conf->global->COMPANY_AQUARIUM_CLEAN_REGEX='^..(..)..';
190 $codetouse = preg_replace('/' . getDolGlobalString('COMPANY_AQUARIUM_CLEAN_REGEX').'/', '\1\2\3', $codetouse);
191 }
192
193 $codetouse = $prefix.strtoupper($codetouse);
194
195 $is_dispo = $this->verif($db, $codetouse, $societe, $type);
196 $this->code = $codetouse;
197 dol_syslog("mod_codecompta_aquarium::get_code found code=".$this->code);
198 return $is_dispo;
199 }
200
201
211 public function verif($db, $code, $societe, $type)
212 {
213 $sql = "SELECT ";
214 if ($type == 'customer') {
215 $sql .= "code_compta";
216 } elseif ($type == 'supplier') {
217 $sql .= "code_compta_fournisseur";
218 }
219 $sql .= " FROM ".MAIN_DB_PREFIX."societe";
220 $sql .= " WHERE ";
221 if ($type == 'customer') {
222 $sql .= "code_compta";
223 } elseif ($type == 'supplier') {
224 $sql .= "code_compta_fournisseur";
225 }
226 $sql .= " = '".$db->escape($code)."'";
227 if (!empty($societe->id)) {
228 $sql .= " AND rowid <> ".$societe->id;
229 }
230
231 $resql = $db->query($sql);
232 if ($resql) {
233 if ($db->num_rows($resql) == 0) {
234 dol_syslog("mod_codecompta_aquarium::verif code '".$code."' available");
235 return 1; // Dispo
236 } else {
237 dol_syslog("mod_codecompta_aquarium::verif code '".$code."' not available");
238 return 0; // Non dispo
239 }
240 } else {
241 $this->error = $db->error()." sql=".$sql;
242 return -1; // Erreur
243 }
244 }
245}
Parent class for third parties accountancy code generators.
Class to manage accountancy code of thirdparties with Aquarium rules.
getExample($langs=null, $objsoc='', $type=-1)
Return an example of result returned by getNextValue.
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
yn($yesno, $format=1, $color=0)
Return yes or no in current language.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:152