modAccounting.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2021 Alexandre Spangaro <aspangaro@open-dsi.fr>
  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. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/core/modules/modAccounting.class.php
  24. * \ingroup Double entry accounting
  25. * \brief Module to activate the double entry accounting module
  26. */
  27. include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
  28. /**
  29. * Class to describe and enable double entry accounting module
  30. */
  31. class modAccounting extends DolibarrModules
  32. {
  33. /**
  34. * Constructor. Define names, constants, directories, boxes, permissions
  35. *
  36. * @param DoliDB $db Database handler
  37. */
  38. public function __construct($db)
  39. {
  40. global $conf;
  41. $this->db = $db;
  42. $this->numero = 50400;
  43. $this->family = "financial";
  44. $this->module_position = '61';
  45. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  46. $this->name = preg_replace('/^mod/i', '', get_class($this));
  47. $this->description = "Double entry accounting management";
  48. // Possible values for version are: 'development', 'experimental', 'dolibarr' or 'dolibarr_deprecated' or version
  49. $this->version = 'dolibarr';
  50. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  51. $this->picto = 'accountancy';
  52. // Data directories to create when module is enabled
  53. $this->dirs = array('/accounting/temp');
  54. // Config pages
  55. $this->config_page_url = array('accounting.php?mainmenu=accountancy&leftmenu=accountancy_admin');
  56. // Dependencies
  57. $this->depends = array("modFacture", "modBanque", "modTax"); // List of modules id that must be enabled if this module is enabled
  58. $this->requiredby = array(); // List of modules id to disable if this one is disabled
  59. $this->conflictwith = array("modComptabilite"); // List of modules are in conflict with this module
  60. $this->phpmin = array(5, 6); // Minimum version of PHP required by module
  61. $this->need_dolibarr_version = array(3, 9); // Minimum version of Dolibarr required by module
  62. $this->langfiles = array("accountancy", "compta");
  63. // Constants
  64. // List of particular constants to add when module is enabled (key, 'chaine', value, desc, visible, 'current' or 'allentities', deleteonunactive)
  65. // Example: $this->const=array(0=>array('MYMODULE_MYNEWCONST1','chaine','myvalue','This is a constant to add',1),
  66. // 1=>array('MYMODULE_MYNEWCONST2','chaine','myvalue','This is another constant to add',0, 'current', 1)
  67. // );
  68. $this->const = array();
  69. $this->const[1] = array(
  70. "MAIN_COMPANY_CODE_ALWAYS_REQUIRED",
  71. "chaine",
  72. "1",
  73. "With this constants on, third party code is always required whatever is numbering module behaviour", 0, 'current', 1
  74. );
  75. $this->const[2] = array(
  76. "MAIN_BANK_ACCOUNTANCY_CODE_ALWAYS_REQUIRED",
  77. "chaine",
  78. "1",
  79. "With this constants on, bank account number is always required", 0, 'current', 1
  80. );
  81. $this->const[3] = array(
  82. "ACCOUNTING_ACCOUNT_SUSPENSE",
  83. "chaine",
  84. "471",
  85. "", 0, 'current', 0
  86. );
  87. $this->const[4] = array(
  88. "ACCOUNTING_ACCOUNT_TRANSFER_CASH",
  89. "chaine",
  90. "58",
  91. "", 0, 'current', 0
  92. );
  93. $this->const[5] = array(
  94. "CHARTOFACCOUNTS",
  95. "chaine",
  96. "2",
  97. "", 0, 'current', 0
  98. );
  99. $this->const[6] = array(
  100. "ACCOUNTING_EXPORT_MODELCSV",
  101. "chaine",
  102. "1",
  103. "", 0, 'current', 0
  104. );
  105. $this->const[7] = array(
  106. "ACCOUNTING_LENGTH_GACCOUNT",
  107. "chaine",
  108. "",
  109. "", 0, 'current', 0
  110. );
  111. $this->const[8] = array(
  112. "ACCOUNTING_LENGTH_AACCOUNT",
  113. "chaine",
  114. "",
  115. "", 0, 'current', 0
  116. );
  117. $this->const[9] = array(
  118. "ACCOUNTING_LIST_SORT_VENTILATION_TODO",
  119. "yesno",
  120. "1",
  121. "", 0, 'current', 0
  122. );
  123. $this->const[10] = array(
  124. "ACCOUNTING_LIST_SORT_VENTILATION_DONE",
  125. "yesno",
  126. "1",
  127. "", 0, 'current', 0
  128. );
  129. $this->const[11] = array(
  130. "ACCOUNTING_EXPORT_DATE",
  131. "chaine",
  132. "%d%m%Y",
  133. "", 0, 'current', 0
  134. );
  135. $this->const[12] = array(
  136. "ACCOUNTING_EXPORT_SEPARATORCSV",
  137. "string",
  138. ",",
  139. "", 0, 'current', 0
  140. );
  141. $this->const[13] = array(
  142. "ACCOUNTING_EXPORT_FORMAT",
  143. "chaine",
  144. "csv",
  145. "", 0, 'current', 0
  146. );
  147. // Tabs
  148. $this->tabs = array();
  149. // Css
  150. $this->module_parts = array();
  151. // Boxes
  152. $this->boxes = array(
  153. 0=>array('file'=>'box_accountancy_last_manual_entries.php', 'enabledbydefaulton'=>'accountancyindex'),
  154. 1=>array('file'=>'box_accountancy_suspense_account.php', 'enabledbydefaulton'=>'accountancyindex')
  155. );
  156. // Permissions
  157. $this->rights_class = 'accounting';
  158. $this->rights = array(); // Permission array used by this module
  159. $r = 0;
  160. $this->rights[$r][0] = 50440;
  161. $this->rights[$r][1] = 'Manage chart of accounts, setup of accountancy';
  162. $this->rights[$r][2] = 'r';
  163. $this->rights[$r][3] = 0;
  164. $this->rights[$r][4] = 'chartofaccount';
  165. $this->rights[$r][5] = '';
  166. $r++;
  167. $this->rights[$r][0] = 50401;
  168. $this->rights[$r][1] = 'Bind products and invoices with accounting accounts';
  169. $this->rights[$r][2] = 'r';
  170. $this->rights[$r][3] = 0;
  171. $this->rights[$r][4] = 'bind';
  172. $this->rights[$r][5] = 'write';
  173. $r++;
  174. $this->rights[$r][0] = 50411;
  175. $this->rights[$r][1] = 'Read operations in Ledger';
  176. $this->rights[$r][2] = 'r';
  177. $this->rights[$r][3] = 0;
  178. $this->rights[$r][4] = 'mouvements';
  179. $this->rights[$r][5] = 'lire';
  180. $r++;
  181. $this->rights[$r][0] = 50412;
  182. $this->rights[$r][1] = 'Write/Edit operations in Ledger';
  183. $this->rights[$r][2] = 'w';
  184. $this->rights[$r][3] = 0;
  185. $this->rights[$r][4] = 'mouvements';
  186. $this->rights[$r][5] = 'creer';
  187. $r++;
  188. $this->rights[$r][0] = 50414;
  189. $this->rights[$r][1] = 'Delete operations in Ledger';
  190. $this->rights[$r][2] = 'd';
  191. $this->rights[$r][3] = 0;
  192. $this->rights[$r][4] = 'mouvements';
  193. $this->rights[$r][5] = 'supprimer';
  194. $r++;
  195. $this->rights[$r][0] = 50415;
  196. $this->rights[$r][1] = 'Delete all operations by year and journal in Ledger';
  197. $this->rights[$r][2] = 'd';
  198. $this->rights[$r][3] = 0;
  199. $this->rights[$r][4] = 'mouvements';
  200. $this->rights[$r][5] = 'supprimer_tous';
  201. $r++;
  202. $this->rights[$r][0] = 50418;
  203. $this->rights[$r][1] = 'Export operations of the Ledger';
  204. $this->rights[$r][2] = 'r';
  205. $this->rights[$r][3] = 0;
  206. $this->rights[$r][4] = 'mouvements';
  207. $this->rights[$r][5] = 'export';
  208. $r++;
  209. $this->rights[$r][0] = 50420;
  210. $this->rights[$r][1] = 'Report and export reports (turnover, balance, journals, ledger)';
  211. $this->rights[$r][2] = 'r';
  212. $this->rights[$r][3] = 0;
  213. $this->rights[$r][4] = 'comptarapport';
  214. $this->rights[$r][5] = 'lire';
  215. $r++;
  216. $this->rights[$r][0] = 50430;
  217. $this->rights[$r][1] = 'Manage fiscal periods, validate movements and close periods';
  218. $this->rights[$r][2] = 'r';
  219. $this->rights[$r][3] = 0;
  220. $this->rights[$r][4] = 'fiscalyear';
  221. $this->rights[$r][5] = 'write';
  222. $r++;
  223. // Menus
  224. //-------
  225. $this->menu = 1; // This module add menu entries. They are coded into menu manager.
  226. // Exports
  227. //--------
  228. $r = 0;
  229. $r++;
  230. $this->export_code[$r] = $this->rights_class.'_'.$r;
  231. $this->export_label[$r] = 'Chartofaccounts';
  232. $this->export_icon[$r] = 'accounting';
  233. $this->export_permission[$r] = array(array("accounting", "chartofaccount"));
  234. $this->export_fields_array[$r] = array('ac.rowid'=>'ChartofaccountsId', 'ac.pcg_version'=>'Chartofaccounts', 'aa.rowid'=>'ID', 'aa.account_number'=>"AccountAccounting", 'aa.label'=>"Label", 'aa.account_parent'=>"Accountparent", 'aa.pcg_type'=>"Pcgtype", 'aa.active'=>'Status');
  235. $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", 'aa.account_parent'=>"Text", 'aa.pcg_type'=>'Text', 'aa.active'=>'Status');
  236. $this->export_entities_array[$r] = array('ac.rowid'=>"Accounting", 'ac.pcg_version'=>"Accounting", 'aa.rowid'=>'Accounting', 'aa.account_number'=>"Accounting", 'aa.label'=>"Accounting", 'aa.accountparent'=>"Accounting", 'aa.pcg_type'=>"Accounting", 'aa_active'=>"Accounting");
  237. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  238. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'accounting_account as aa';
  239. $this->export_sql_end[$r] .= ' ,'.MAIN_DB_PREFIX.'accounting_system as ac';
  240. $this->export_sql_end[$r] .= ' WHERE ac.pcg_version = aa.fk_pcg_version AND aa.entity IN ('.getEntity('accounting').') ';
  241. // Imports
  242. //--------
  243. $r = 0;
  244. // General ledger
  245. $r++;
  246. $this->import_code[$r] = $this->rights_class.'_'.$r;
  247. $this->import_label[$r] = 'ImportAccountingEntries';
  248. $this->import_icon[$r] = $this->picto;
  249. $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
  250. $this->import_tables_array[$r] = array('b'=>MAIN_DB_PREFIX.'accounting_bookkeeping'); // List of tables to insert into (insert done in same order)
  251. $this->import_fields_array[$r] = array(
  252. 'b.piece_num'=>"TransactionNumShort",
  253. 'b.doc_date'=>"Docdate",
  254. //'b.doc_type'=>'Doctype',
  255. 'b.doc_ref'=>'Piece',
  256. 'b.code_journal'=>'Codejournal',
  257. 'b.journal_label'=>'JournalLabel',
  258. 'b.numero_compte'=>'AccountAccounting',
  259. 'b.label_compte'=>'LabelAccount',
  260. 'b.subledger_account'=>'SubledgerAccount',
  261. 'b.subledger_label'=>'SubledgerAccountLabel',
  262. 'b.label_operation'=>'LabelOperation',
  263. 'b.debit'=>"Debit",
  264. 'b.credit'=>"Credit",
  265. '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
  266. );
  267. $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)
  268. $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]$');
  269. $this->import_convertvalue_array[$r] = array(
  270. 'b.numero_compte' => array('rule' => 'accountingaccount'),
  271. 'b.subledger_account' => array('rule' => 'accountingaccount')
  272. );
  273. $this->import_examplevalues_array[$r] = array(
  274. 'b.piece_num'=>'123 (!!! use next value not already used)',
  275. 'b.doc_date'=>dol_print_date(dol_now(), "%Y-%m-%d"),
  276. //'b.doc_type'=>'import',
  277. 'b.doc_ref'=>'My document ABC',
  278. 'b.code_journal'=>"VTE",
  279. 'b.journal_label'=>"Sale journal",
  280. 'b.numero_compte'=>"707",
  281. 'b.label_compte'=>'Product account 707',
  282. 'b.subledger_account'=>'',
  283. 'b.subledger_label'=>'',
  284. 'b.label_operation'=>"Sale of ABC",
  285. 'b.debit'=>"0",
  286. 'b.credit'=>"100",
  287. '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
  288. );
  289. // General ledger - Fichier FEC
  290. $r++;
  291. $this->import_code[$r] = $this->rights_class.'_'.$r;
  292. $this->import_label[$r] = 'ImportAccountingEntriesFECFormat';
  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.code_journal'=>'FECFormatJournalCode*',
  298. 'b.journal_label'=>'FECFormatJournalLabel',
  299. 'b.piece_num'=>'FECFormatEntryNum*',
  300. 'b.doc_date'=>'FECFormatEntryDate*',
  301. 'b.numero_compte'=>'FECFormatGeneralAccountNumber*',
  302. 'b.label_compte'=>'FECFormatGeneralAccountLabel*',
  303. 'b.subledger_account'=>'FECFormatSubledgerAccountNumber',
  304. 'b.subledger_label'=>'FECFormatSubledgerAccountLabel',
  305. 'b.doc_ref'=>'FECFormatPieceRef*',
  306. 'b.date_creation'=>'FECFormatPieceDate',
  307. 'b.label_operation'=>'FECFormatLabelOperation',
  308. 'b.debit'=>'FECFormatDebit*',
  309. 'b.credit'=>'FECFormatCredit*',
  310. 'b.lettering_code'=>'FECFormatReconcilableCode',
  311. 'b.date_lettering'=>'FECFormatReconcilableDate',
  312. 'b.date_validated'=>'FECFormatValidateDate',
  313. 'b.multicurrency_amount'=>'FECFormatMulticurrencyAmount',
  314. 'b.multicurrency_code'=>'FECFormatMulticurrencyCode'
  315. );
  316. $this->import_fieldshidden_array[$r] = array(
  317. 'b.doc_type'=>'const-import_from_external',
  318. 'b.fk_doc'=>'const-0',
  319. 'b.fk_docdet'=>'const-0',
  320. 'b.fk_user_author'=>'user->id',
  321. 'b.montant'=>'rule-computeMontant',
  322. 'b.sens'=>'rule-computeSens'
  323. ); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent)
  324. $this->import_convertvalue_array[$r]=array(
  325. 'b.numero_compte'=>array('rule'=>'accountingaccount'),
  326. 'b.subledger_account'=>array('rule'=>'accountingaccount'),
  327. 'b.montant' => array('rule' => 'compute', 'classfile' => '/accountancy/class/accountancyimport.class.php', 'class' => 'AccountancyImport', 'method' => 'computeAmount', 'element' => 'Accountancy'),
  328. 'b.sens' => array('rule' => 'compute', 'classfile' => '/accountancy/class/accountancyimport.class.php', 'class' => 'AccountancyImport', 'method' => 'computeDirection', 'element' => 'Accountancy'),
  329. );
  330. $this->import_regex_array[$r] = array(
  331. //'b.doc_date'=>'^\d{4}\d{2}\d{2}$',
  332. 'b.doc_ref'=>'^.{1,300}$',
  333. 'b.numero_compte'=>'^.{1,32}$',
  334. 'b.label_compte'=>'^.{1,255}$',
  335. 'b.subledger_compte'=>'^.{1,32}$',
  336. 'b.subledger_label'=>'^.{1,255}$',
  337. 'b.label_operation'=>'^.{1,255}$',
  338. //'b.sens'=>'^[D|C]$',
  339. );
  340. $this->import_examplevalues_array[$r] = array(
  341. 'b.code_journal'=>"VT",
  342. 'b.journal_label'=>"Sale journal",
  343. 'b.piece_num'=>'123 (!!! use next value not already used)',
  344. 'b.doc_date'=>dol_print_date(dol_now(), "%Y%m%d"),
  345. 'b.numero_compte'=>"707",
  346. 'b.label_compte'=>'Sale',
  347. 'b.subledger_account'=>'',
  348. 'b.subledger_label'=>'',
  349. 'b.doc_ref'=>'My document ABC',
  350. 'b.date_creation'=>dol_print_date(dol_now(), "%Y%m%d"),
  351. 'b.label_operation'=>"Sale of ABC",
  352. 'b.debit'=>"0",
  353. 'b.credit'=>"100",
  354. 'b.lettering_code'=>'ABC',
  355. 'b.date_lettering'=>dol_print_date(dol_now(), "%Y%m%d"),
  356. 'b.date_validated'=>dol_print_date(dol_now(), "%Y%m%d"),
  357. 'b.multicurrency_amount'=>"90 (Necessary if devise is different than EUR)",
  358. 'b.multicurrency_code'=>"US (Necessary if devise is different than EUR)",
  359. );
  360. // Chart of accounts
  361. $r++;
  362. $this->import_code[$r] = $this->rights_class.'_'.$r;
  363. $this->import_label[$r] = "Chartofaccounts"; // Translation key
  364. $this->import_icon[$r] = $this->picto;
  365. $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
  366. $this->import_tables_array[$r] = array('aa'=>MAIN_DB_PREFIX.'accounting_account');
  367. $this->import_tables_creator_array[$r] = array('aa'=>'fk_user_author'); // Fields to store import user id
  368. $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.active'=>'Status*', 'aa.datec'=>"DateCreation");
  369. $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.active'=>'^0|1$', 'aa.datec'=>'^\d{4}-\d{2}-\d{2}$');
  370. $this->import_convertvalue_array[$r] = array(
  371. 'aa.account_number'=>array('rule'=>'accountingaccount'),
  372. 'aa.account_parent'=>array('rule'=>'fetchidfromref', 'classfile'=>'/accountancy/class/accountingaccount.class.php', 'class'=>'AccountingAccount', 'method'=>'fetch', 'element'=>'AccountingAccount'),
  373. 'aa.fk_accounting_category'=>array('rule'=>'fetchidfromcodeorlabel', 'classfile'=>'/accountancy/class/accountancycategory.class.php', 'class'=>'AccountancyCategory', 'method'=>'fetch', 'dict'=>'DictionaryAccountancyCategory'),
  374. );
  375. $this->import_examplevalues_array[$r] = array('aa.fk_pcg_version'=>"PCG99-ABREGE", '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.active'=>'1', 'aa.datec'=>"2017-04-28");
  376. $this->import_updatekeys_array[$r] = array('aa.fk_pcg_version'=>'Chartofaccounts', 'aa.account_number'=>'AccountAccounting');
  377. }
  378. }