balance.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. <?php
  2. /* Copyright (C) 2016 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2016 Florian Henry <florian.henry@open-concept.pro>
  4. * Copyright (C) 2016-2023 Alexandre Spangaro <aspangaro@open-dsi.fr>
  5. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/accountancy/bookkeeping/balance.php
  22. * \ingroup Accountancy (Double entries)
  23. * \brief Balance of book keeping
  24. */
  25. // Load Dolibarr environment
  26. require '../../main.inc.php';
  27. // Class
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/bookkeeping.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancyexport.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  36. // Load translation files required by the page
  37. $langs->loadLangs(array("accountancy", "compta"));
  38. $action = GETPOST('action', 'aZ09');
  39. $optioncss = GETPOST('optioncss', 'alpha');
  40. $type = GETPOST('type', 'alpha');
  41. if ($type == 'sub') {
  42. $context_default = 'balancesubaccountlist';
  43. } else {
  44. $context_default = 'balancelist';
  45. }
  46. $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : $context_default;
  47. $show_subgroup = GETPOST('show_subgroup', 'alpha');
  48. $search_date_start = dol_mktime(0, 0, 0, GETPOST('date_startmonth', 'int'), GETPOST('date_startday', 'int'), GETPOST('date_startyear', 'int'));
  49. $search_date_end = dol_mktime(23, 59, 59, GETPOST('date_endmonth', 'int'), GETPOST('date_endday', 'int'), GETPOST('date_endyear', 'int'));
  50. $search_ledger_code = GETPOST('search_ledger_code', 'array');
  51. $search_accountancy_code_start = GETPOST('search_accountancy_code_start', 'alpha');
  52. if ($search_accountancy_code_start == - 1) {
  53. $search_accountancy_code_start = '';
  54. }
  55. $search_accountancy_code_end = GETPOST('search_accountancy_code_end', 'alpha');
  56. if ($search_accountancy_code_end == - 1) {
  57. $search_accountancy_code_end = '';
  58. }
  59. $search_not_reconciled = GETPOST('search_not_reconciled', 'alpha');
  60. // Load variable for pagination
  61. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  62. $sortfield = GETPOST('sortfield', 'aZ09comma');
  63. $sortorder = GETPOST('sortorder', 'aZ09comma');
  64. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  65. if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
  66. $page = 0;
  67. } // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
  68. $offset = $limit * $page;
  69. $pageprev = $page - 1;
  70. $pagenext = $page + 1;
  71. if ($sortorder == "") {
  72. $sortorder = "ASC";
  73. }
  74. if ($sortfield == "") {
  75. $sortfield = "t.numero_compte";
  76. }
  77. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  78. $object = new BookKeeping($db);
  79. $hookmanager->initHooks(array($contextpage)); // Note that conf->hooks_modules contains array
  80. $formaccounting = new FormAccounting($db);
  81. $formother = new FormOther($db);
  82. $form = new Form($db);
  83. if (empty($search_date_start) && !GETPOSTISSET('formfilteraction')) {
  84. $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear ";
  85. $sql .= " WHERE date_start < '".$db->idate(dol_now())."' AND date_end > '".$db->idate(dol_now())."'";
  86. $sql .= $db->plimit(1);
  87. $res = $db->query($sql);
  88. if ($res->num_rows > 0) {
  89. $fiscalYear = $db->fetch_object($res);
  90. $search_date_start = strtotime($fiscalYear->date_start);
  91. $search_date_end = strtotime($fiscalYear->date_end);
  92. } else {
  93. $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
  94. $year_start = dol_print_date(dol_now(), '%Y');
  95. if (dol_print_date(dol_now(), '%m') < $month_start) {
  96. $year_start--; // If current month is lower that starting fiscal month, we start last year
  97. }
  98. $year_end = $year_start + 1;
  99. $month_end = $month_start - 1;
  100. if ($month_end < 1) {
  101. $month_end = 12;
  102. $year_end--;
  103. }
  104. $search_date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
  105. $search_date_end = dol_get_last_day($year_end, $month_end);
  106. }
  107. }
  108. if (!isModEnabled('accounting')) {
  109. accessforbidden();
  110. }
  111. if ($user->socid > 0) {
  112. accessforbidden();
  113. }
  114. if (!$user->hasRight('accounting', 'mouvements', 'lire')) {
  115. accessforbidden();
  116. }
  117. /*
  118. * Action
  119. */
  120. $param = '';
  121. $parameters = array('socid'=>$socid);
  122. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  123. if ($reshook < 0) {
  124. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  125. }
  126. if (empty($reshook)) {
  127. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
  128. $show_subgroup = '';
  129. $search_date_start = '';
  130. $search_date_end = '';
  131. $search_date_startyear = '';
  132. $search_date_startmonth = '';
  133. $search_date_startday = '';
  134. $search_date_endyear = '';
  135. $search_date_endmonth = '';
  136. $search_date_endday = '';
  137. $search_accountancy_code_start = '';
  138. $search_accountancy_code_end = '';
  139. $search_not_reconciled = '';
  140. $search_ledger_code = array();
  141. $filter = array();
  142. }
  143. // Must be after the remove filter action, before the export.
  144. $filter = array();
  145. if (!empty($search_date_start)) {
  146. $filter['t.doc_date>='] = $search_date_start;
  147. $param .= '&amp;date_startmonth=' . GETPOST('date_startmonth', 'int') . '&amp;date_startday=' . GETPOST('date_startday', 'int') . '&amp;date_startyear=' . GETPOST('date_startyear', 'int');
  148. }
  149. if (!empty($search_date_end)) {
  150. $filter['t.doc_date<='] = $search_date_end;
  151. $param .= '&amp;date_endmonth=' . GETPOST('date_endmonth', 'int') . '&amp;date_endday=' . GETPOST('date_endday', 'int') . '&amp;date_endyear=' . GETPOST('date_endyear', 'int');
  152. }
  153. if (!empty($search_doc_date)) {
  154. $filter['t.doc_date'] = $search_doc_date;
  155. $param .= '&doc_datemonth=' . GETPOST('doc_datemonth', 'int') . '&doc_dateday=' . GETPOST('doc_dateday', 'int') . '&doc_dateyear=' . GETPOST('doc_dateyear', 'int');
  156. }
  157. if (!empty($search_accountancy_code_start)) {
  158. if ($type == 'sub') {
  159. $filter['t.subledger_account>='] = $search_accountancy_code_start;
  160. } else {
  161. $filter['t.numero_compte>='] = $search_accountancy_code_start;
  162. }
  163. $param .= '&amp;search_accountancy_code_start=' . urlencode($search_accountancy_code_start);
  164. }
  165. if (!empty($search_accountancy_code_end)) {
  166. if ($type == 'sub') {
  167. $filter['t.subledger_account<='] = $search_accountancy_code_end;
  168. } else {
  169. $filter['t.numero_compte<='] = $search_accountancy_code_end;
  170. }
  171. $param .= '&amp;search_accountancy_code_end=' . urlencode($search_accountancy_code_end);
  172. }
  173. if (!empty($search_ledger_code)) {
  174. $filter['t.code_journal'] = $search_ledger_code;
  175. foreach ($search_ledger_code as $code) {
  176. $param .= '&search_ledger_code[]=' . urlencode($code);
  177. }
  178. }
  179. if (!empty($search_not_reconciled)) {
  180. $filter['t.reconciled_option'] = $search_not_reconciled;
  181. $param .= '&search_not_reconciled='.urlencode($search_not_reconciled);
  182. }
  183. // param with type of list
  184. $url_param = substr($param, 1); // remove first "&"
  185. if (!empty($type)) {
  186. $param = '&type=' . $type . $param;
  187. }
  188. }
  189. if ($action == 'export_csv') {
  190. $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
  191. $filename = 'balance';
  192. $type_export = 'balance';
  193. include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';
  194. if ($type == 'sub') {
  195. $result = $object->fetchAllBalance($sortorder, $sortfield, $limit, 0, $filter, 'AND', 1);
  196. } else {
  197. $result = $object->fetchAllBalance($sortorder, $sortfield, $limit, 0, $filter);
  198. }
  199. if ($result < 0) {
  200. setEventMessages($object->error, $object->errors, 'errors');
  201. }
  202. foreach ($object->lines as $line) {
  203. if ($type == 'sub') {
  204. print '"' . length_accounta($line->subledger_account) . '"' . $sep;
  205. print '"' . $line->subledger_label . '"' . $sep;
  206. } else {
  207. print '"' . length_accountg($line->numero_compte) . '"' . $sep;
  208. print '"' . $object->get_compte_desc($line->numero_compte) . '"' . $sep;
  209. }
  210. print '"'.price($line->debit).'"'.$sep;
  211. print '"'.price($line->credit).'"'.$sep;
  212. print '"'.price($line->debit - $line->credit).'"'.$sep;
  213. print "\n";
  214. }
  215. exit;
  216. }
  217. /*
  218. * View
  219. */
  220. if ($type == 'sub') {
  221. $title_page = $langs->trans("AccountBalanceSubAccount");
  222. } else {
  223. $title_page = $langs->trans("AccountBalance");
  224. }
  225. llxHeader('', $title_page);
  226. if ($action != 'export_csv') {
  227. // List
  228. $nbtotalofrecords = '';
  229. if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
  230. if ($type == 'sub') {
  231. $nbtotalofrecords = $object->fetchAllBalance($sortorder, $sortfield, 0, 0, $filter, 'AND', 1);
  232. } else {
  233. $nbtotalofrecords = $object->fetchAllBalance($sortorder, $sortfield, 0, 0, $filter);
  234. }
  235. if ($nbtotalofrecords < 0) {
  236. setEventMessages($object->error, $object->errors, 'errors');
  237. }
  238. }
  239. if ($type == 'sub') {
  240. $result = $object->fetchAllBalance($sortorder, $sortfield, $limit, $offset, $filter, 'AND', 1);
  241. } else {
  242. $result = $object->fetchAllBalance($sortorder, $sortfield, $limit, $offset, $filter);
  243. }
  244. if ($result < 0) {
  245. setEventMessages($object->error, $object->errors, 'errors');
  246. }
  247. print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
  248. print '<input type="hidden" name="token" value="'.newToken().'">';
  249. print '<input type="hidden" name="action" id="action" value="list">';
  250. if ($optioncss != '') {
  251. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  252. }
  253. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  254. print '<input type="hidden" name="type" value="'.$type.'">';
  255. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  256. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  257. print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
  258. print '<input type="hidden" name="page" value="'.$page.'">';
  259. $parameters = array();
  260. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  261. if ($reshook < 0) {
  262. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  263. }
  264. $newcardbutton = empty($hookmanager->resPrint) ? '' : $hookmanager->resPrint;
  265. if (empty($reshook)) {
  266. $newcardbutton = '<input type="button" id="exportcsvbutton" name="exportcsvbutton" class="butAction" value="'.$langs->trans("Export").' ('.$conf->global->ACCOUNTING_EXPORT_FORMAT.')" />';
  267. print '<script type="text/javascript">
  268. jQuery(document).ready(function() {
  269. jQuery("#exportcsvbutton").click(function(event) {
  270. event.preventDefault();
  271. console.log("Set action to export_csv");
  272. jQuery("#action").val("export_csv");
  273. jQuery("#searchFormList").submit();
  274. jQuery("#action").val("list");
  275. });
  276. });
  277. </script>';
  278. if ($type == 'sub') {
  279. $newcardbutton .= dolGetButtonTitle($langs->trans('AccountBalance')." - ".$langs->trans('GroupByAccountAccounting'), '', 'fa fa-stream paddingleft imgforviewmode', DOL_URL_ROOT . '/accountancy/bookkeeping/balance.php?' . $url_param, '', 1, array('morecss' => 'marginleftonly'));
  280. $newcardbutton .= dolGetButtonTitle($langs->trans('AccountBalance')." - ".$langs->trans('GroupBySubAccountAccounting'), '', 'fa fa-align-left vmirror paddingleft imgforviewmode', DOL_URL_ROOT . '/accountancy/bookkeeping/balance.php?type=sub&' . $url_param, '', 1, array('morecss' => 'marginleftonly btnTitleSelected'));
  281. } else {
  282. $newcardbutton .= dolGetButtonTitle($langs->trans('AccountBalance')." - ".$langs->trans('GroupByAccountAccounting'), '', 'fa fa-stream paddingleft imgforviewmode', DOL_URL_ROOT . '/accountancy/bookkeeping/balance.php?' . $url_param, '', 1, array('morecss' => 'marginleftonly btnTitleSelected'));
  283. $newcardbutton .= dolGetButtonTitle($langs->trans('AccountBalance')." - ".$langs->trans('GroupBySubAccountAccounting'), '', 'fa fa-align-left vmirror paddingleft imgforviewmode', DOL_URL_ROOT . '/accountancy/bookkeeping/balance.php?type=sub&' . $url_param, '', 1, array('morecss' => 'marginleftonly'));
  284. }
  285. $newcardbutton .= dolGetButtonTitle($langs->trans('NewAccountingMvt'), '', 'fa fa-plus-circle paddingleft', DOL_URL_ROOT.'/accountancy/bookkeeping/card.php?action=create');
  286. }
  287. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
  288. $param .= '&contextpage='.urlencode($contextpage);
  289. }
  290. if ($limit > 0 && $limit != $conf->liste_limit) {
  291. $param .= '&limit='.((int) $limit);
  292. }
  293. print_barre_liste($title_page, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $result, $nbtotalofrecords, 'title_accountancy', 0, $newcardbutton, '', $limit, 0, 0, 1);
  294. $selectedfields = '';
  295. // Warning to explain why list of record is not consistent with the other list view (missing a lot of lines)
  296. if ($type == 'sub') {
  297. print info_admin($langs->trans("WarningRecordWithoutSubledgerAreExcluded"));
  298. }
  299. $moreforfilter = '';
  300. $moreforfilter .= '<div class="divsearchfield">';
  301. $moreforfilter .= $langs->trans('DateStart').': ';
  302. $moreforfilter .= $form->selectDate($search_date_start ? $search_date_start : -1, 'date_start', 0, 0, 1, '', 1, 0);
  303. $moreforfilter .= $langs->trans('DateEnd').': ';
  304. $moreforfilter .= $form->selectDate($search_date_end ? $search_date_end : -1, 'date_end', 0, 0, 1, '', 1, 0);
  305. $moreforfilter .= '</div>';
  306. $moreforfilter .= '<div class="divsearchfield">';
  307. $moreforfilter .= '<label for="show_subgroup">'.$langs->trans('ShowSubtotalByGroup').'</label>: ';
  308. $moreforfilter .= '<input type="checkbox" name="show_subgroup" id="show_subgroup" value="show_subgroup"'.($show_subgroup == 'show_subgroup' ? ' checked' : '').'>';
  309. $moreforfilter .= '</div>';
  310. $moreforfilter .= '<div class="divsearchfield">';
  311. $moreforfilter .= $langs->trans("Journals").': ';
  312. $moreforfilter .= $formaccounting->multi_select_journal($search_ledger_code, 'search_ledger_code', 0, 1, 1, 1);
  313. $moreforfilter .= '</div>';
  314. $moreforfilter .= '</br>';
  315. $moreforfilter .= '<div class="divsearchfield">';
  316. // Accountancy account
  317. $moreforfilter .= $langs->trans('AccountAccounting').': ';
  318. if ($type == 'sub') {
  319. $moreforfilter .= $formaccounting->select_auxaccount($search_accountancy_code_start, 'search_accountancy_code_start', $langs->trans('From'), 'maxwidth200');
  320. } else {
  321. $moreforfilter .= $formaccounting->select_account($search_accountancy_code_start, 'search_accountancy_code_start', $langs->trans('From'), array(), 1, 1, 'maxwidth200', 'accounts');
  322. }
  323. $moreforfilter .= ' ';
  324. if ($type == 'sub') {
  325. $moreforfilter .= $formaccounting->select_auxaccount($search_accountancy_code_end, 'search_accountancy_code_end', $langs->trans('to'), 'maxwidth200');
  326. } else {
  327. $moreforfilter .= $formaccounting->select_account($search_accountancy_code_end, 'search_accountancy_code_end', $langs->trans('to'), array(), 1, 1, 'maxwidth200', 'accounts');
  328. }
  329. $moreforfilter .= '</div>';
  330. $moreforfilter .= '<div class="divsearchfield">';
  331. $moreforfilter .= '<label for="notreconciled">'.$langs->trans('NotReconciled').'</label>: ';
  332. $moreforfilter .= '<input type="checkbox" name="search_not_reconciled" id="notreconciled" value="notreconciled"'.($search_not_reconciled == 'notreconciled' ? ' checked' : '').'>';
  333. $moreforfilter .= '</div>';
  334. if (!empty($moreforfilter)) {
  335. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  336. print $moreforfilter;
  337. $parameters = array();
  338. $reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters); // Note that $action and $object may have been modified by hook
  339. print $hookmanager->resPrint;
  340. print '</div>';
  341. }
  342. $colspan = (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE) ? 5 : 4);
  343. print '<table class="liste '.($moreforfilter ? "listwithfilterbefore" : "").'">';
  344. print '<tr class="liste_titre_filter">';
  345. print '<td class="liste_titre" colspan="'.$colspan.'">';
  346. print '</td>';
  347. // Fields from hook
  348. $parameters = array('arrayfields'=>$arrayfields);
  349. $reshook = $hookmanager->executeHooks('printFieldListOption', $parameters, $object); // Note that $action and $object may have been modified by hook
  350. print $hookmanager->resPrint;
  351. // Action column
  352. print '<td class="liste_titre maxwidthsearch">';
  353. $searchpicto = $form->showFilterButtons();
  354. print $searchpicto;
  355. print '</td>';
  356. print '</tr>'."\n";
  357. print '<tr class="liste_titre">';
  358. print_liste_field_titre("AccountAccounting", $_SERVER['PHP_SELF'], "t.numero_compte", "", $param, "", $sortfield, $sortorder);
  359. // TODO : Retrieve the type of third party: Customer / Supplier / Employee
  360. //if ($type == 'sub') {
  361. // print_liste_field_titre("Type", $_SERVER['PHP_SELF'], "t.type", "", $param, "", $sortfield, $sortorder);
  362. //}
  363. if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
  364. print_liste_field_titre("OpeningBalance", $_SERVER['PHP_SELF'], "", $param, "", 'class="right"', $sortfield, $sortorder);
  365. }
  366. print_liste_field_titre("Debit", $_SERVER['PHP_SELF'], "t.debit", "", $param, 'class="right"', $sortfield, $sortorder);
  367. print_liste_field_titre("Credit", $_SERVER['PHP_SELF'], "t.credit", "", $param, 'class="right"', $sortfield, $sortorder);
  368. print_liste_field_titre("Balance", $_SERVER["PHP_SELF"], "", $param, "", 'class="right"', $sortfield, $sortorder);
  369. // Hook fields
  370. $parameters = array('arrayfields'=>$arrayfields, 'param'=>$param, 'sortfield'=>$sortfield, 'sortorder'=>$sortorder);
  371. $reshook = $hookmanager->executeHooks('printFieldListTitle', $parameters, $object); // Note that $action and $object may have been modified by hook
  372. print $hookmanager->resPrint;
  373. // Action column
  374. print getTitleFieldOfList($selectedfields, 0, $_SERVER["PHP_SELF"], '', '', '', '', $sortfield, $sortorder, 'center maxwidthsearch ')."\n";
  375. print '</tr>'."\n";
  376. $total_debit = 0;
  377. $total_credit = 0;
  378. $sous_total_debit = 0;
  379. $sous_total_credit = 0;
  380. $total_opening_balance = 0;
  381. $sous_total_opening_balance = 0;
  382. $displayed_account = "";
  383. $accountingaccountstatic = new AccountingAccount($db);
  384. // TODO Debug - This feature is dangerous, it takes all the entries and adds all the accounts
  385. // without time and class limits (Class 6 and 7 accounts ???) and does not take into account the "a-nouveau" journal.
  386. if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
  387. $sql = "SELECT t.numero_compte, (SUM(t.debit) - SUM(t.credit)) as opening_balance";
  388. $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as t";
  389. $sql .= " WHERE t.entity = " . $conf->entity; // Never do sharing into accounting features
  390. $sql .= " AND t.doc_date < '" . $db->idate($search_date_start) . "'";
  391. $sql .= " GROUP BY t.numero_compte";
  392. $resql = $db->query($sql);
  393. $nrows = $resql->num_rows;
  394. $opening_balances = array();
  395. for ($i = 0; $i < $nrows; $i++) {
  396. $arr = $resql->fetch_array();
  397. $opening_balances["'" . $arr['numero_compte'] . "'"] = $arr['opening_balance'];
  398. }
  399. }
  400. foreach ($object->lines as $line) {
  401. // reset before the fetch (in case of the fetch fails)
  402. $accountingaccountstatic->id = 0;
  403. $accountingaccountstatic->account_number = '';
  404. if ($type != 'sub') {
  405. $accountingaccountstatic->fetch(null, $line->numero_compte, true);
  406. if (!empty($accountingaccountstatic->account_number)) {
  407. $accounting_account = $accountingaccountstatic->getNomUrl(0, 1, 1);
  408. } else {
  409. $accounting_account = length_accountg($line->numero_compte);
  410. }
  411. }
  412. $link = '';
  413. $total_debit += $line->debit;
  414. $total_credit += $line->credit;
  415. $opening_balance = isset($opening_balances["'".$line->numero_compte."'"]) ? $opening_balances["'".$line->numero_compte."'"] : 0;
  416. $total_opening_balance += $opening_balance;
  417. $tmparrayforrootaccount = $object->getRootAccount($line->numero_compte);
  418. $root_account_description = $tmparrayforrootaccount['label'];
  419. $root_account_number = $tmparrayforrootaccount['account_number'];
  420. //var_dump($tmparrayforrootaccount);
  421. //var_dump($accounting_account);
  422. //var_dump($accountingaccountstatic);
  423. if (empty($accountingaccountstatic->label) && $accountingaccountstatic->id > 0) {
  424. $link = '<a class="editfielda reposition" href="' . DOL_URL_ROOT . '/accountancy/admin/card.php?action=update&token=' . newToken() . '&id=' . $accountingaccountstatic->id . '">' . img_edit() . '</a>';
  425. } elseif ($accounting_account == 'NotDefined') {
  426. $link = '<a href="' . DOL_URL_ROOT . '/accountancy/admin/card.php?action=create&token=' . newToken() . '&accountingaccount=' . length_accountg($line->numero_compte) . '">' . img_edit_add() . '</a>';
  427. } elseif (empty($tmparrayforrootaccount['label'])) {
  428. // $tmparrayforrootaccount['label'] not defined = the account has not parent with a parent.
  429. // This is useless, we should not create a new account when an account has no parent, we must edit it to fix its parent.
  430. // BUG 1: Accounts on level root or level 1 must not have a parent 2 level higher, so should not show a link to create another account.
  431. // BUG 2: Adding a link to create a new accounting account here is useless because it is not add as parent of the orphelin.
  432. //$link = '<a href="' . DOL_URL_ROOT . '/accountancy/admin/card.php?action=create&token=' . newToken() . '&accountingaccount=' . length_accountg($line->numero_compte) . '">' . img_edit_add() . '</a>';
  433. }
  434. if (!empty($show_subgroup)) {
  435. // Show accounting account
  436. if (empty($displayed_account) || $root_account_number != $displayed_account) {
  437. // Show subtotal per accounting account
  438. if ($displayed_account != "") {
  439. print '<tr class="liste_total">';
  440. print '<td class="right">'.$langs->trans("SubTotal").':</td>';
  441. if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
  442. print '<td class="right nowraponall amount">'.price($sous_total_opening_balance).'</td>';
  443. }
  444. print '<td class="right nowraponall amount">'.price($sous_total_debit).'</td>';
  445. print '<td class="right nowraponall amount">'.price($sous_total_credit).'</td>';
  446. if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
  447. print '<td class="right nowraponall amount">'.price(price2num($sous_total_opening_balance + $sous_total_debit - $sous_total_credit)).'</td>';
  448. } else {
  449. print '<td class="right nowraponall amount">'.price(price2num($sous_total_debit - $sous_total_credit)).'</td>';
  450. }
  451. print "<td></td>\n";
  452. print '</tr>';
  453. }
  454. // Show first line of a break
  455. print '<tr class="trforbreak">';
  456. print '<td colspan="'.($colspan+1).'" style="font-weight:bold; border-bottom: 1pt solid black;">'.$line->numero_compte.($root_account_description ? ' - '.$root_account_description : '').'</td>';
  457. print '</tr>';
  458. $displayed_account = $root_account_number;
  459. $sous_total_debit = 0;
  460. $sous_total_credit = 0;
  461. $sous_total_opening_balance = 0;
  462. }
  463. }
  464. print '<tr class="oddeven">';
  465. // Accounting account
  466. if ($type == 'sub') {
  467. print '<td>'.$line->subledger_account.' <span class="opacitymedium">('.$line->subledger_label.')</span></td>';
  468. } else {
  469. print '<td>'.$accounting_account.'</td>';
  470. }
  471. // Type
  472. // TODO Retrieve the type of third party: Customer / Supplier / Employee
  473. //if ($type == 'sub') {
  474. // print '<td></td>';
  475. //}
  476. if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
  477. print '<td class="right nowraponall amount">'.price(price2num($opening_balance, 'MT')).'</td>';
  478. }
  479. $urlzoom = '';
  480. if ($type == 'sub') {
  481. if ($line->subledger_account) {
  482. $urlzoom = DOL_URL_ROOT . '/accountancy/bookkeeping/listbyaccount.php?type=sub&search_accountancy_code_start=' . urlencode($line->subledger_account) . '&search_accountancy_code_end=' . urlencode($line->subledger_account);
  483. if (GETPOSTISSET('date_startmonth')) {
  484. $urlzoom .= '&search_date_startmonth=' . GETPOST('date_startmonth', 'int') . '&search_date_startday=' . GETPOST('date_startday', 'int') . '&search_date_startyear=' . GETPOST('date_startyear', 'int');
  485. }
  486. if (GETPOSTISSET('date_endmonth')) {
  487. $urlzoom .= '&search_date_endmonth=' . GETPOST('date_endmonth', 'int') . '&search_date_endday=' . GETPOST('date_endday', 'int') . '&search_date_endyear=' . GETPOST('date_endyear', 'int');
  488. }
  489. }
  490. } else {
  491. if ($line->numero_compte) {
  492. $urlzoom = DOL_URL_ROOT . '/accountancy/bookkeeping/listbyaccount.php?search_accountancy_code_start=' . urlencode($line->numero_compte) . '&search_accountancy_code_end=' . urlencode($line->numero_compte);
  493. if (GETPOSTISSET('date_startmonth')) {
  494. $urlzoom .= '&search_date_startmonth=' . GETPOST('date_startmonth', 'int') . '&search_date_startday=' . GETPOST('date_startday', 'int') . '&search_date_startyear=' . GETPOST('date_startyear', 'int');
  495. }
  496. if (GETPOSTISSET('date_endmonth')) {
  497. $urlzoom .= '&search_date_endmonth=' . GETPOST('date_endmonth', 'int') . '&search_date_endday=' . GETPOST('date_endday', 'int') . '&search_date_endyear=' . GETPOST('date_endyear', 'int');
  498. }
  499. }
  500. }
  501. // Debit
  502. print '<td class="right nowraponall amount"><a href="'.$urlzoom.'">'.price(price2num($line->debit, 'MT')).'</a></td>';
  503. // Credit
  504. print '<td class="right nowraponall amount"><a href="'.$urlzoom.'">'.price(price2num($line->credit, 'MT')).'</a></td>';
  505. if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
  506. print '<td class="right nowraponall amount">'.price(price2num($opening_balance + $line->debit - $line->credit, 'MT')).'</td>';
  507. } else {
  508. print '<td class="right nowraponall amount">'.price(price2num($line->debit - $line->credit, 'MT')).'</td>';
  509. }
  510. print '<td class="center">';
  511. print $link;
  512. print '</td>';
  513. print "</tr>\n";
  514. // Records the sub-total
  515. $sous_total_debit += $line->debit;
  516. $sous_total_credit += $line->credit;
  517. $sous_total_opening_balance += $opening_balance;
  518. }
  519. if (!empty($show_subgroup)) {
  520. print '<tr class="liste_total"><td class="right">'.$langs->trans("SubTotal").':</td>';
  521. if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
  522. print '<td class="right nowraponall amount">'.price(price2num($sous_total_opening_balance, 'MT')).'</td>';
  523. }
  524. print '<td class="right nowraponall amount">'.price(price2num($sous_total_debit, 'MT')).'</td>';
  525. print '<td class="right nowraponall amount">'.price(price2num($sous_total_credit, 'MT')).'</td>';
  526. if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
  527. print '<td class="right nowraponall amount">' . price(price2num($sous_total_opening_balance + $sous_total_debit - $sous_total_credit, 'MT')) . '</td>';
  528. } else {
  529. print '<td class="right nowraponall amount">' . price(price2num($sous_total_debit - $sous_total_credit, 'MT')) . '</td>';
  530. }
  531. print "<td></td>\n";
  532. print '</tr>';
  533. }
  534. print '<tr class="liste_total"><td class="right">'.$langs->trans("AccountBalance").':</td>';
  535. if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
  536. print '<td class="nowrap right">'.price(price2num($total_opening_balance, 'MT')).'</td>';
  537. }
  538. print '<td class="right nowraponall amount">'.price(price2num($total_debit, 'MT')).'</td>';
  539. print '<td class="right nowraponall amount">'.price(price2num($total_credit, 'MT')).'</td>';
  540. if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
  541. print '<td class="right nowraponall amount">' . price(price2num($total_opening_balance + $total_debit - $total_credit, 'MT')) . '</td>';
  542. } else {
  543. print '<td class="right nowraponall amount">' . price(price2num($total_debit - $total_credit, 'MT')) . '</td>';
  544. }
  545. print "<td></td>\n";
  546. print '</tr>';
  547. $parameters = array('arrayfields'=>$arrayfields, 'sql'=>$sql);
  548. $reshook = $hookmanager->executeHooks('printFieldListFooter', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  549. print $hookmanager->resPrint;
  550. print "</table>";
  551. print '</form>';
  552. }
  553. // End of page
  554. llxFooter();
  555. $db->close();