dolibarr 24.0.0-beta
modAccounting.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
3 * Copyright (C) 2013-2025 Alexandre Spangaro <alexandre@inovea-conseil.com>
4 * Copyright (C) 2014 Ari Elbaz (elarifr) <github@accedinfo.com>
5 * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
6 * Copyright (C) 2016-2017 Laurent Destailleur <eldy@users.sourceforge.net>
7 * Copyright (C) 2017-2021 Open-DSI <support@open-dsi.fr>
8 * Copyright (C) 2026 Frédéric France <frederic.france@free.fr>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
29include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
30
35{
41 public function __construct($db)
42 {
43 $this->db = $db;
44 $this->numero = 50400;
45
46 $this->family = "financial";
47 $this->module_position = '62';
48 // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
49 $this->name = preg_replace('/^mod/i', '', get_class($this));
50 $this->description = "Double entry accounting management";
51
52 // Possible values for version are: 'development', 'experimental', 'dolibarr' or 'dolibarr_deprecated' or version
53 $this->version = 'dolibarr';
54
55 $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
56 $this->picto = 'accountancy';
57
58 // Data directories to create when module is enabled
59 $this->dirs = array('/accounting/temp', '/accounting/export');
60
61 // Config pages
62 $this->config_page_url = array('accounting.php?mainmenu=accountancy&leftmenu=accountancy_admin');
63
64 // Dependencies
65 $this->depends = array("modFacture", "modBanque", "modTax"); // List of modules id that must be enabled if this module is enabled
66 $this->requiredby = array(); // List of modules id to disable if this one is disabled
67 $this->conflictwith = array("modComptabilite"); // List of modules are in conflict with this module
68 $this->phpmin = array(7, 0); // Minimum version of PHP required by module
69 $this->need_dolibarr_version = array(3, 9); // Minimum version of Dolibarr required by module
70 $this->langfiles = array("accountancy", "compta");
71
72 // Constants
73 // List of particular constants to add when module is enabled (key, 'chaine', value, desc, visible, 'current' or 'allentities', deleteonunactive)
74 // Example: $this->const=array(0=>array('MYMODULE_MYNEWCONST1','chaine','myvalue','This is a constant to add',1),
75 // 1=>array('MYMODULE_MYNEWCONST2','chaine','myvalue','This is another constant to add',0, 'current', 1)
76 // );
77 $this->const = [
78 [
79 "MAIN_COMPANY_CODE_ALWAYS_REQUIRED",
80 "chaine",
81 "1",
82 "With this constants on, third party code is always required whatever is numbering module behaviour", 0, 'current', 1
83 ],
84 [
85 "MAIN_BANK_ACCOUNTANCY_CODE_ALWAYS_REQUIRED",
86 "chaine",
87 "0",
88 "With this constants on, bank account number is always required", 0, 'current', 1
89 ],
90 [
91 "ACCOUNTING_ACCOUNT_SUSPENSE",
92 "chaine",
93 "471",
94 "", 0, 'current', 0
95 ],
96 [
97 "ACCOUNTING_ACCOUNT_TRANSFER_CASH",
98 "chaine",
99 "58",
100 "", 0, 'current', 0
101 ],
102 [
103 "CHARTOFACCOUNTS",
104 "chaine",
105 "2",
106 "", 0, 'current', 0
107 ],
108 [
109 "ACCOUNTING_EXPORT_MODELCSV",
110 "chaine",
111 "1",
112 "", 0, 'current', 0
113 ],
114 [
115 "ACCOUNTING_LENGTH_GACCOUNT",
116 "chaine",
117 "",
118 "", 0, 'current', 0
119 ],
120 [
121 "ACCOUNTING_LENGTH_AACCOUNT",
122 "chaine",
123 "",
124 "", 0, 'current', 0
125 ],
126 [
127 "ACCOUNTING_EXPORT_DATE",
128 "chaine",
129 "%Y-%m-%d",
130 "", 0, 'current', 0
131 ],
132 [
133 "ACCOUNTING_EXPORT_SEPARATORCSV",
134 "string",
135 ",",
136 "", 0, 'current', 0
137 ],
138 [
139 "ACCOUNTING_EXPORT_FORMAT",
140 "chaine",
141 "csv",
142 "", 0, 'current', 0
143 ],
144 [
145 "BOOKKEEPING_ADDON",
146 "chaine",
147 "mod_bookkeeping_argon",
148 "", 0, 'current', 0
149 ],
150 ];
151
152 // Tabs
153 $this->tabs = array();
154
155 // Css
156 $this->module_parts = array();
157
158 // Boxes
159 $this->boxes = [
160 ['file'=>'box_accountancy_last_manual_entries.php', 'enabledbydefaulton'=>'accountancyindex'],
161 ['file'=>'box_accountancy_suspense_account.php', 'enabledbydefaulton'=>'accountancyindex'],
162 ];
163
164 // Permissions
165 $this->rights_class = 'accounting';
166
167 $this->rights = array(); // Permission array used by this module
168 $r = 0;
169
170 $this->rights[$r][0] = 50440;
171 $this->rights[$r][1] = 'Manage chart of accounts, setup of accountancy';
172 $this->rights[$r][2] = 'r';
173 $this->rights[$r][3] = 0;
174 $this->rights[$r][4] = 'chartofaccount';
175 $this->rights[$r][5] = '';
176 $r++;
177
178 $this->rights[$r][0] = 50401;
179 $this->rights[$r][1] = 'Bind products and invoices with accounting accounts';
180 $this->rights[$r][2] = 'r';
181 $this->rights[$r][3] = 0;
182 $this->rights[$r][4] = 'bind';
183 $this->rights[$r][5] = 'write';
184 $r++;
185
186 $this->rights[$r][0] = 50411;
187 $this->rights[$r][1] = 'Read operations in Ledger';
188 $this->rights[$r][2] = 'r';
189 $this->rights[$r][3] = 0;
190 $this->rights[$r][4] = 'mouvements';
191 $this->rights[$r][5] = 'lire';
192 $r++;
193
194 $this->rights[$r][0] = 50412;
195 $this->rights[$r][1] = 'Write/Edit operations in Ledger';
196 $this->rights[$r][2] = 'w';
197 $this->rights[$r][3] = 0;
198 $this->rights[$r][4] = 'mouvements';
199 $this->rights[$r][5] = 'creer';
200 $r++;
201
202 $this->rights[$r][0] = 50414;
203 $this->rights[$r][1] = 'Delete operations in Ledger';
204 $this->rights[$r][2] = 'd';
205 $this->rights[$r][3] = 0;
206 $this->rights[$r][4] = 'mouvements';
207 $this->rights[$r][5] = 'supprimer';
208 $r++;
209
210 $this->rights[$r][0] = 50415;
211 $this->rights[$r][1] = 'Delete all operations by year and journal in Ledger';
212 $this->rights[$r][2] = 'd';
213 $this->rights[$r][3] = 0;
214 $this->rights[$r][4] = 'mouvements';
215 $this->rights[$r][5] = 'supprimer_tous';
216 $r++;
217
218 $this->rights[$r][0] = 50418;
219 $this->rights[$r][1] = 'Export operations of the Ledger';
220 $this->rights[$r][2] = 'r';
221 $this->rights[$r][3] = 0;
222 $this->rights[$r][4] = 'mouvements';
223 $this->rights[$r][5] = 'export';
224 $r++;
225
226 $this->rights[$r][0] = 50420;
227 $this->rights[$r][1] = 'Report and export reports (turnover, balance, journals, ledger)';
228 $this->rights[$r][2] = 'r';
229 $this->rights[$r][3] = 0;
230 $this->rights[$r][4] = 'comptarapport';
231 $this->rights[$r][5] = 'lire';
232 $r++;
233
234 $this->rights[$r][0] = 50430;
235 $this->rights[$r][1] = 'Manage fiscal periods, validate movements and close periods';
236 $this->rights[$r][2] = 'r';
237 $this->rights[$r][3] = 0;
238 $this->rights[$r][4] = 'fiscalyear';
239 $this->rights[$r][5] = 'write';
240 $r++;
241
242 // Menus
243 //-------
244 $this->menu = 1; // This module add menu entries. They are coded into menu manager.
245
246 // Exports
247 //--------
248 $r = 0;
249
250 $r++;
251 $this->export_code[$r] = $this->rights_class.'_'.$r;
252 $this->export_label[$r] = 'Chartofaccounts';
253 $this->export_icon[$r] = $this->picto;
254 $this->export_permission[$r] = array(array("accounting", "chartofaccount"));
255 $this->export_fields_array[$r] = array('ac.rowid'=>'ChartofaccountsId', 'ac.pcg_version'=>'Chartofaccounts', 'aa.rowid'=>'ID', 'aa.account_number'=>"AccountAccounting", 'aa.label'=>"Label", 'aa2.account_number'=>"Accountparent", 'aa.pcg_type'=>"Pcgtype", 'aa.centralized'=>'Centralized', 'aa.active'=>'Status');
256 $this->export_TypeFields_array[$r] = array('ac.rowid'=>'List:accounting_system:pcg_version', 'ac.pcg_version'=>'Text', 'aa.rowid'=>'Numeric', 'aa.account_number'=>"Text", 'aa.label'=>"Text", 'aa2.account_number'=>"Text", 'aa.pcg_type'=>'Text', 'aa.centralized'=>'Status', 'aa.active'=>'Status');
257 $this->export_entities_array[$r] = array(); // We define here only fields that use another picto
258
259 $this->export_sql_start[$r] = 'SELECT DISTINCT ';
260 $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'accounting_account as aa';
261 $this->export_sql_end[$r] .= ' ,'.MAIN_DB_PREFIX.'accounting_system as ac';
262 $this->export_sql_end[$r] .= ' ,'.MAIN_DB_PREFIX.'accounting_account as aa2';
263 $this->export_sql_end[$r] .= ' WHERE ac.pcg_version = aa.fk_pcg_version AND aa.entity IN ('.getEntity('accounting').')';
264 $this->export_sql_end[$r] .= ' AND aa2.rowid = aa.account_parent AND aa2.active = 1 AND ac.pcg_version = aa2.fk_pcg_version AND aa2.entity IN ('.getEntity('accounting').')';
265
266
267 // Imports
268 //--------
269 $r = 0;
270
271 // Chart of accounts
272 $r++;
273 $this->import_code[$r] = $this->rights_class.'_'.$r;
274 $this->import_label[$r] = "Chartofaccounts"; // Translation key
275 $this->import_icon[$r] = $this->picto;
276 $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
277 $this->import_tables_array[$r] = array('aa'=>MAIN_DB_PREFIX.'accounting_account');
278 $this->import_tables_creator_array[$r] = array('aa'=>'fk_user_author'); // Fields to store import user id
279 $this->import_fields_array[$r] = array('aa.fk_pcg_version'=>"Chartofaccounts*", 'aa.account_number'=>"AccountAccounting*", 'aa.label'=>"Label*", 'aa.account_parent'=>"Accountparent", "aa.fk_accounting_category"=>"AccountingCategory", "aa.pcg_type"=>"Pcgtype*", 'aa.centralized'=>'Centralized*', 'aa.active'=>'Status*', 'aa.datec'=>"DateCreation");
280 $this->import_regex_array[$r] = array('aa.fk_pcg_version'=>'pcg_version@'.MAIN_DB_PREFIX.'accounting_system', 'aa.account_number'=>'^.{1,32}$', 'aa.label'=>'^.{1,255}$', 'aa.account_parent'=>'^.{0,32}$', 'aa.fk_accounting_category'=>'rowid@'.MAIN_DB_PREFIX.'c_accounting_category', 'aa.pcg_type'=>'^.{1,20}$', 'aa.centralized'=>'^0|1$', 'aa.active'=>'^0|1$', 'aa.datec'=>'^\d{4}-\d{2}-\d{2}$');
281 $this->import_convertvalue_array[$r] = array(
282 'aa.account_number'=>array('rule'=>'accountingaccount'),
283 'aa.account_parent'=>array('rule'=>'fetchidfromref', 'classfile'=>'/accountancy/class/accountingaccount.class.php', 'class'=>'AccountingAccount', 'method'=>'fetch', 'element'=>'AccountingAccount'),
284 'aa.fk_accounting_category'=>array('rule'=>'fetchidfromcodeorlabel', 'classfile'=>'/accountancy/class/accountancycategory.class.php', 'class'=>'AccountancyCategory', 'method'=>'fetch', 'dict'=>'DictionaryAccountancyCategory'),
285 );
286 $this->import_examplevalues_array[$r] = array('aa.fk_pcg_version'=>"PCG25-DEV", 'aa.account_number'=>"707", 'aa.label'=>"Product sales", 'aa.account_parent'=>"ref:7 or id:1407", "aa.fk_accounting_category"=>"", "aa.pcg_type"=>"PROD", 'aa.centralized'=>'0', 'aa.active'=>'1', 'aa.datec'=>"2017-04-28");
287 $this->import_updatekeys_array[$r] = array('aa.fk_pcg_version'=>'Chartofaccounts', 'aa.account_number'=>'AccountAccounting');
288
289 // General ledger
290 $r++;
291 $this->import_code[$r] = $this->rights_class.'_'.$r;
292 $this->import_label[$r] = 'ImportAccountingEntries';
293 $this->import_icon[$r] = $this->picto;
294 $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
295 $this->import_tables_array[$r] = array('b'=>MAIN_DB_PREFIX.'accounting_bookkeeping'); // List of tables to insert into (insert done in same order)
296 $this->import_fields_array[$r] = array(
297 'b.piece_num'=>"TransactionNumShort",
298 'b.doc_date'=>"Docdate",
299 //'b.doc_type'=>'Doctype',
300 'b.doc_ref'=>'Piece',
301 'b.code_journal'=>'Codejournal',
302 'b.journal_label'=>'JournalLabel',
303 'b.numero_compte'=>'AccountAccounting',
304 'b.label_compte'=>'LabelAccount',
305 'b.subledger_account'=>'SubledgerAccount',
306 'b.subledger_label'=>'SubledgerAccountLabel',
307 'b.label_operation'=>'LabelOperation',
308 'b.debit'=>"Debit",
309 'b.credit'=>"Credit",
310 'b.sens'=>'Direction' // This field is still used by accounting export. We can remove it once it has been replaced into accountancyexport.class.php by a detection using ->debit and ->credit
311 );
312 $this->import_fieldshidden_array[$r] = array('b.doc_type'=>'const-import_from_external', 'b.fk_doc'=>'const-0', 'b.fk_docdet'=>'const-0', 'b.fk_user_author'=>'user->id', 'b.date_creation'=>'const-'.dol_print_date(dol_now(), 'standard')); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent)
313 $this->import_regex_array[$r] = array('b.doc_date'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$');
314 $this->import_convertvalue_array[$r] = array(
315 'b.numero_compte' => array('rule' => 'accountingaccount'),
316 'b.subledger_account' => array('rule' => 'accountingaccount')
317 );
318 $this->import_examplevalues_array[$r] = array(
319 'b.piece_num'=>'123 (!!! use next value not already used)',
320 'b.doc_date'=>dol_print_date(dol_now(), "%Y-%m-%d"),
321 //'b.doc_type'=>'import',
322 'b.doc_ref'=>'My document ABC',
323 'b.code_journal'=>"VTE",
324 'b.journal_label'=>"Sale journal",
325 'b.numero_compte'=>"707",
326 'b.label_compte'=>'Product account 707',
327 'b.subledger_account'=>'',
328 'b.subledger_label'=>'',
329 'b.label_operation'=>"Sale of ABC",
330 'b.debit'=>"0",
331 'b.credit'=>"100",
332 'b.sens'=>'C' // This field is still used by accounting export. We can remove it once it has been replace into accountancyexport.class.php by a detection using ->debit and ->credit
333 );
334
335 // General ledger - File FEC
336 $r++;
337 $this->import_code[$r] = $this->rights_class.'_'.$r;
338 $this->import_label[$r] = 'ImportAccountingEntriesFECFormat';
339 $this->import_icon[$r] = $this->picto;
340 $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
341 $this->import_tables_array[$r] = array('b'=>MAIN_DB_PREFIX.'accounting_bookkeeping'); // List of tables to insert into (insert done in same order)
342 $this->import_fields_array[$r] = array(
343 'b.code_journal'=>'FECFormatJournalCode*',
344 'b.journal_label'=>'FECFormatJournalLabel',
345 'b.piece_num'=>'FECFormatEntryNum', // not mandatory (keep empty to get next value of "piece_num" from "llx_accounting_bookkeeping" table)
346 'b.doc_date'=>'FECFormatEntryDate*',
347 'b.numero_compte'=>'FECFormatGeneralAccountNumber*',
348 'b.label_compte'=>'FECFormatGeneralAccountLabel*',
349 'b.subledger_account'=>'FECFormatSubledgerAccountNumber',
350 'b.subledger_label'=>'FECFormatSubledgerAccountLabel',
351 'b.doc_ref'=>'FECFormatPieceRef*',
352 'b.date_creation'=>'FECFormatPieceDate',
353 'b.label_operation'=>'FECFormatLabelOperation',
354 'b.debit'=>'FECFormatDebit*',
355 'b.credit'=>'FECFormatCredit*',
356 'b.lettering_code'=>'FECFormatReconcilableCode',
357 'b.date_lettering'=>'FECFormatReconcilableDate',
358 'b.date_validated'=>'FECFormatValidateDate',
359 'b.multicurrency_amount'=>'FECFormatMulticurrencyAmount',
360 'b.multicurrency_code'=>'FECFormatMulticurrencyCode'
361 );
362 $this->import_fieldshidden_array[$r] = array(
363 'b.doc_type'=>'const-import_from_external',
364 'b.fk_doc'=>'const-0',
365 'b.fk_docdet'=>'const-0',
366 'b.fk_user_author'=>'user->id',
367 'b.montant'=>'rule-computeAmount',
368 'b.sens'=>'rule-computeDirection'
369 ); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent)
370 $this->import_convertvalue_array[$r]=array(
371 'b.piece_num' => array('rule' => 'compute', 'type' => 'int', 'classfile' => '/accountancy/class/accountancyimport.class.php', 'class' => 'AccountancyImport', 'method' => 'computePieceNum', 'element' => 'Accountancy'),
372 'b.numero_compte'=>array('rule'=>'accountingaccount'),
373 'b.subledger_account'=>array('rule'=>'accountingaccount'),
374 'b.debit' => array('rule' => 'compute', 'type' => 'double', 'classfile' => '/accountancy/class/accountancyimport.class.php', 'class' => 'AccountancyImport', 'method' => 'cleanAmount', 'element' => 'Accountancy'),
375 'b.credit' => array('rule' => 'compute', 'type' => 'double', 'classfile' => '/accountancy/class/accountancyimport.class.php', 'class' => 'AccountancyImport', 'method' => 'cleanAmount', 'element' => 'Accountancy'),
376 'b.multicurrency_amount' => array('rule' => 'compute', 'type' => 'double', 'classfile' => '/accountancy/class/accountancyimport.class.php', 'class' => 'AccountancyImport', 'method' => 'cleanAmount', 'element' => 'Accountancy'),
377 'b.montant' => array('rule' => 'compute', 'type' => 'double', 'classfile' => '/accountancy/class/accountancyimport.class.php', 'class' => 'AccountancyImport', 'method' => 'computeAmount', 'element' => 'Accountancy'),
378 'b.sens' => array('rule' => 'compute', 'type' => 'varchar', 'classfile' => '/accountancy/class/accountancyimport.class.php', 'class' => 'AccountancyImport', 'method' => 'computeDirection', 'element' => 'Accountancy'),
379 );
380 $this->import_regex_array[$r] = array(
381 //'b.doc_date'=>'^\d{4}\d{2}\d{2}$',
382 'b.doc_ref'=>'^.{1,300}$',
383 'b.numero_compte'=>'^.{1,32}$',
384 'b.label_compte'=>'^.{1,255}$',
385 'b.subledger_compte'=>'^.{1,32}$',
386 'b.subledger_label'=>'^.{1,255}$',
387 'b.label_operation'=>'^.{1,255}$',
388 //'b.sens'=>'^[D|C]$',
389 );
390 $this->import_examplevalues_array[$r] = array(
391 'b.code_journal'=>"VT",
392 'b.journal_label'=>"Sale journal",
393 'b.piece_num'=>'123 (!!! use next value not already used)',
394 'b.doc_date'=>dol_print_date(dol_now(), "%Y%m%d"),
395 'b.numero_compte'=>"707",
396 'b.label_compte'=>'Sale',
397 'b.subledger_account'=>'',
398 'b.subledger_label'=>'',
399 'b.doc_ref'=>'My document ABC',
400 'b.date_creation'=>dol_print_date(dol_now(), "%Y%m%d"),
401 'b.label_operation'=>"Sale of ABC",
402 'b.debit'=>"0",
403 'b.credit'=>"100",
404 'b.lettering_code'=>'ABC',
405 'b.date_lettering'=>dol_print_date(dol_now(), "%Y%m%d"),
406 'b.date_validated'=>dol_print_date(dol_now(), "%Y%m%d"),
407 'b.multicurrency_amount'=>"90 (Necessary if devise is different than EUR)",
408 'b.multicurrency_code'=>"US (Necessary if devise is different than EUR)",
409 );
410 }
411
420 public function init($options = '')
421 {
422 $result = $this->_load_tables('/install/mysql/', 'accounting');
423 if ($result < 0) {
424 return -1; // Do not activate module if error 'not allowed' returned when loading module SQL queries (the _load_table run sql with run_sql with the error allowed parameter set to 'default')
425 }
426
427 $sql = array();
428 return $this->_init($sql, $options);
429 }
430
439 public function remove($options = '')
440 {
441 $sql = array();
442 return $this->_remove($sql, $options);
443 }
444}
Class DolibarrModules.
_init($array_sql, $options='')
Enables a module.
_remove($array_sql, $options='')
Disable function.
_load_tables($reldir, $onlywithsuffix='')
Create tables and keys required by module:
Class to describe and enable double entry accounting module.
init($options='')
Function called when module is enabled.
__construct($db)
Constructor.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_now($mode='gmt')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:133