balance.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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-2018 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 <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. /**
  22. * \file htdocs/accountancy/bookkeeping/balance.php
  23. * \ingroup Advanced accountancy
  24. * \brief Balance of book keeping
  25. */
  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/accountancyexport.class.php';
  32. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php';
  33. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formother.class.php';
  34. // Load translation files required by the page
  35. $langs->loadLangs(array("accountancy"));
  36. $page = GETPOST("page");
  37. $sortorder = GETPOST("sortorder", 'alpha');
  38. $sortfield = GETPOST("sortfield", 'alpha');
  39. $action = GETPOST('action', 'aZ09');
  40. if (GETPOST("exportcsv",'alpha')) $action = 'export_csv';
  41. // Load variable for pagination
  42. $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
  43. $sortfield = GETPOST('sortfield','alpha');
  44. $sortorder = GETPOST('sortorder','alpha');
  45. $page = GETPOST('page','int');
  46. if (empty($page) || $page == -1 || GETPOST('button_search','alpha') || GETPOST('button_removefilter','alpha') || (empty($toselect) && $massaction === '0')) { $page = 0; } // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
  47. $offset = $limit * $page;
  48. $pageprev = $page - 1;
  49. $pagenext = $page + 1;
  50. //if (! $sortfield) $sortfield="p.date_fin";
  51. //if (! $sortorder) $sortorder="DESC";
  52. $search_date_start = dol_mktime(0, 0, 0, GETPOST('date_startmonth', 'int'), GETPOST('date_startday', 'int'), GETPOST('date_startyear', 'int'));
  53. $search_date_end = dol_mktime(23, 59, 59, GETPOST('date_endmonth', 'int'), GETPOST('date_endday', 'int'), GETPOST('date_endyear', 'int'));
  54. $search_accountancy_code_start = GETPOST('search_accountancy_code_start', 'alpha');
  55. if ($search_accountancy_code_start == - 1) {
  56. $search_accountancy_code_start = '';
  57. }
  58. $search_accountancy_code_end = GETPOST('search_accountancy_code_end', 'alpha');
  59. if ($search_accountancy_code_end == - 1) {
  60. $search_accountancy_code_end = '';
  61. }
  62. $object = new BookKeeping($db);
  63. $formaccounting = new FormAccounting($db);
  64. $formother = new FormOther($db);
  65. $form = new Form($db);
  66. if (empty($search_date_start) && ! GETPOSTISSET('formfilteraction'))
  67. {
  68. $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear ";
  69. $sql.= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'";
  70. $sql.= $db->plimit(1);
  71. $res = $db->query($sql);
  72. if ($res->num_rows > 0) {
  73. $fiscalYear = $db->fetch_object($res);
  74. $search_date_start = strtotime($fiscalYear->date_start);
  75. $search_date_end = strtotime($fiscalYear->date_end);
  76. } else {
  77. $month_start= ($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
  78. $year_start = dol_print_date(dol_now(), '%Y');
  79. $year_end = $year_start + 1;
  80. $month_end = $month_start - 1;
  81. if ($month_end < 1)
  82. {
  83. $month_end = 12;
  84. $year_end--;
  85. }
  86. $search_date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
  87. $search_date_end = dol_get_last_day($year_end, $month_end);
  88. }
  89. }
  90. if ($sortorder == "")
  91. $sortorder = "ASC";
  92. if ($sortfield == "")
  93. $sortfield = "t.numero_compte";
  94. $param='';
  95. if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.urlencode($contextpage);
  96. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.urlencode($limit);
  97. $filter = array ();
  98. if (! empty($search_date_start)) {
  99. $filter['t.doc_date>='] = $search_date_start;
  100. $param .= '&amp;date_startmonth=' . GETPOST('date_startmonth', 'int') . '&amp;date_startday=' . GETPOST('date_startday', 'int') . '&amp;date_startyear=' . GETPOST('date_startyear', 'int');
  101. }
  102. if (! empty($search_date_end)) {
  103. $filter['t.doc_date<='] = $search_date_end;
  104. $param .= '&amp;date_endmonth=' . GETPOST('date_endmonth', 'int') . '&amp;date_endday=' . GETPOST('date_endday', 'int') . '&amp;date_endyear=' . GETPOST('date_endyear', 'int');
  105. }
  106. if (! empty($search_accountancy_code_start)) {
  107. $filter['t.numero_compte>='] = $search_accountancy_code_start;
  108. $param .= '&amp;search_accountancy_code_start=' . $search_accountancy_code_start;
  109. }
  110. if (! empty($search_accountancy_code_end)) {
  111. $filter['t.numero_compte<='] = $search_accountancy_code_end;
  112. $param .= '&amp;search_accountancy_code_end=' . $search_accountancy_code_end;
  113. }
  114. /*
  115. * Action
  116. */
  117. 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
  118. {
  119. $search_accountancy_code_start = '';
  120. $search_accountancy_code_end = '';
  121. $search_date_start = '';
  122. $search_date_end = '';
  123. $filter = array();
  124. }
  125. /*
  126. * View
  127. */
  128. if ($action == 'export_csv')
  129. {
  130. $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
  131. $filename = 'balance';
  132. $type_export = 'balance';
  133. include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php';
  134. $result = $object->fetchAllBalance($sortorder, $sortfield, $limit, 0, $filter);
  135. if ($result < 0) {
  136. setEventMessages($object->error, $object->errors, 'errors');
  137. }
  138. foreach ($object->lines as $line)
  139. {
  140. print length_accountg($line->numero_compte) . $sep;
  141. print $object->get_compte_desc($line->numero_compte) . $sep;
  142. print price($line->debit) . $sep;
  143. print price($line->credit) . $sep;
  144. print price($line->credit - $line->debit) . $sep;
  145. print "\n";
  146. }
  147. exit;
  148. }
  149. $title_page = $langs->trans("AccountBalance");
  150. llxHeader('', $title_page);
  151. if ($action != 'export_csv')
  152. {
  153. // List
  154. $nbtotalofrecords = '';
  155. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  156. {
  157. $nbtotalofrecords = $object->fetchAllBalance($sortorder, $sortfield, 0, 0, $filter);
  158. if ($nbtotalofrecords < 0) {
  159. setEventMessages($object->error, $object->errors, 'errors');
  160. }
  161. }
  162. $result = $object->fetchAllBalance($sortorder, $sortfield, $limit, $offset, $filter);
  163. if ($result < 0) {
  164. setEventMessages($object->error, $object->errors, 'errors');
  165. }
  166. print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
  167. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  168. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  169. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  170. print '<input type="hidden" name="action" value="list">';
  171. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  172. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  173. print '<input type="hidden" name="page" value="'.$page.'">';
  174. $button = '<input type="submit" name="exportcsv" class="butAction" value="' . $langs->trans("Export") . ' ('.$conf->global->ACCOUNTING_EXPORT_FORMAT.')" />';
  175. print_barre_liste($title_page, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $button, $result, $nbtotalofrecords, 'title_accountancy', 0, '', '', $limit);
  176. $moreforfilter = '';
  177. $moreforfilter .= '<div class="divsearchfield">';
  178. $moreforfilter .= $langs->trans('DateStart') . ': ';
  179. $moreforfilter .= $form->selectDate($search_date_start?$search_date_start:-1, 'date_start', 0, 0, 1, '', 1, 0);
  180. $moreforfilter .= $langs->trans('DateEnd') . ': ';
  181. $moreforfilter .= $form->selectDate($search_date_end?$search_date_end:-1, 'date_end', 0, 0, 1, '', 1, 0);
  182. $moreforfilter .= '</div>';
  183. if (! empty($moreforfilter)) {
  184. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  185. print $moreforfilter;
  186. $parameters = array();
  187. $reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters); // Note that $action and $object may have been modified by hook
  188. print $hookmanager->resPrint;
  189. print '</div>';
  190. }
  191. print '<table class="liste ' . ($moreforfilter ? "listwithfilterbefore" : "") . '">';
  192. print '<tr class="liste_titre_filter">';
  193. print '<td class="liste_titre" colspan="5">';
  194. print $langs->trans('From');
  195. print $formaccounting->select_account($search_accountancy_code_start, 'search_accountancy_code_start', 1, array(), 1, 1, '');
  196. print ' ';
  197. print $langs->trans('to');
  198. print $formaccounting->select_account($search_accountancy_code_end, 'search_accountancy_code_end', 1, array(), 1, 1, '');
  199. print '</td>';
  200. print '<td align="right" class="liste_titre">';
  201. $searchpicto=$form->showFilterAndCheckAddButtons(0);
  202. print $searchpicto;
  203. print '</td>';
  204. print '</tr>';
  205. print '<tr class="liste_titre">';
  206. print_liste_field_titre("AccountAccounting", $_SERVER['PHP_SELF'], "t.numero_compte", "", $param, "", $sortfield, $sortorder);
  207. print_liste_field_titre("Label", $_SERVER['PHP_SELF'], "t.label_operation", "", $param, "", $sortfield, $sortorder);
  208. print_liste_field_titre("Debit", $_SERVER['PHP_SELF'], "t.debit", "", $param, 'align="right"', $sortfield, $sortorder);
  209. print_liste_field_titre("Credit", $_SERVER['PHP_SELF'], "t.credit", "", $param, 'align="right"', $sortfield, $sortorder);
  210. print_liste_field_titre("Balance", $_SERVER["PHP_SELF"], "", $param, "", 'align="right"', $sortfield, $sortorder);
  211. print_liste_field_titre('', $_SERVER["PHP_SELF"], "", $param, "", 'width="60" align="center"', $sortfield, $sortorder);
  212. print "</tr>\n";
  213. $total_debit = 0;
  214. $total_credit = 0;
  215. $sous_total_debit = 0;
  216. $sous_total_credit = 0;
  217. $displayed_account = "";
  218. foreach ($object->lines as $line)
  219. {
  220. $link = '';
  221. $total_debit += $line->debit;
  222. $total_credit += $line->credit;
  223. $description = $object->get_compte_desc($line->numero_compte); // Search description of the account
  224. $root_account_description = $object->get_compte_racine($line->numero_compte);
  225. if (empty($description)) {
  226. $link = '<a href="../admin/card.php?action=create&accountingaccount=' . length_accountg($line->numero_compte) . '">' . img_edit_add() . '</a>';
  227. }
  228. print '<tr class="oddeven">';
  229. // Permet d'afficher le compte comptable
  230. if (empty($displayed_account) || $root_account_description != $displayed_account)
  231. {
  232. // Affiche un Sous-Total par compte comptable
  233. if ($displayed_account != "") {
  234. print '<tr class="liste_total"><td align="right" colspan="2">' . $langs->trans("SubTotal") . ':</td><td class="nowrap" align="right">' . price($sous_total_debit) . '</td><td class="nowrap" align="right">' . price($sous_total_credit) . '</td><td class="nowrap" align="right">' . price(price2num($sous_total_credit - $sous_total_debit)) . '</td>';
  235. print "<td>&nbsp;</td>\n";
  236. print '</tr>';
  237. }
  238. // Affiche le compte comptable en debut de ligne
  239. print "<tr>";
  240. print '<td colspan="6" style="font-weight:bold; border-bottom: 1pt solid black;">' . $line->numero_compte . ($root_account_description ? ' - ' . $root_account_description : '') . '</td>';
  241. print '</tr>';
  242. $displayed_account = $root_account_description;
  243. $sous_total_debit = 0;
  244. $sous_total_credit = 0;
  245. }
  246. // $object->get_compte_racine($line->numero_compte);
  247. print '<td>' . length_accountg($line->numero_compte) . '</td>';
  248. print '<td>' . $description . '</td>';
  249. print '<td class="right">' . price($line->debit) . '</td>';
  250. print '<td class="right">' . price($line->credit) . '</td>';
  251. print '<td class="right">' . price($line->credit - $line->debit) . '</td>';
  252. print '<td align="center">' . $link;
  253. print '</td>';
  254. print "</tr>\n";
  255. // Comptabilise le sous-total
  256. $sous_total_debit += $line->debit;
  257. $sous_total_credit += $line->credit;
  258. }
  259. print '<tr class="liste_total"><td align="right" colspan="2">' . $langs->trans("SubTotal") . ':</td><td class="nowrap" align="right">' . price($sous_total_debit) . '</td><td class="nowrap" align="right">' . price($sous_total_credit) . '</td><td class="nowrap" align="right">' . price(price2num($sous_total_credit - $sous_total_debit)) . '</td>';
  260. print "<td>&nbsp;</td>\n";
  261. print '</tr>';
  262. print '<tr class="liste_total"><td align="right" colspan="2">' . $langs->trans("AccountBalance") . ':</td><td class="nowrap" align="right">' . price($total_debit) . '</td><td class="nowrap" align="right">' . price($total_credit) . '</td><td class="nowrap" align="right">' . price(price2num($total_credit - $total_debit)) . '</td>';
  263. print "<td>&nbsp;</td>\n";
  264. print '</tr>';
  265. print "</table>";
  266. print '</form>';
  267. }
  268. // End of page
  269. llxFooter();
  270. $db->close();