balance.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. /**
  21. * \file htdocs/accountancy/bookkeeping/balance.php
  22. * \ingroup Advanced accountancy
  23. * \brief Balance of book keeping
  24. */
  25. require '../../main.inc.php';
  26. // Class
  27. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  28. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.class.php';
  29. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php';
  30. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formother.class.php';
  31. // Langs
  32. $langs->load("accountancy");
  33. $page = GETPOST("page");
  34. $sortorder = GETPOST("sortorder");
  35. $sortfield = GETPOST("sortfield");
  36. $action = GETPOST('action', 'alpha');
  37. $search_date_start = dol_mktime(0, 0, 0, GETPOST('date_startmonth', 'int'), GETPOST('date_startday', 'int'), GETPOST('date_startyear', 'int'));
  38. $search_date_end = dol_mktime(0, 0, 0, GETPOST('date_endmonth', 'int'), GETPOST('date_endday', 'int'), GETPOST('date_endyear', 'int'));
  39. $search_accountancy_code_start = GETPOST('search_accountancy_code_start', 'alpha');
  40. if ($search_accountancy_code_start == - 1) {
  41. $search_accountancy_code_start = '';
  42. }
  43. $search_accountancy_code_end = GETPOST('search_accountancy_code_end', 'alpha');
  44. if ($search_accountancy_code_end == - 1) {
  45. $search_accountancy_code_end = '';
  46. }
  47. if (GETPOST("button_export_csv_x") || GETPOST("button_export_csv")) {
  48. $action = 'export_csv';
  49. }
  50. $limit = GETPOST('limit') ? GETPOST('limit', 'int') : $conf->liste_limit;
  51. $offset = $limit * $page;
  52. $object = new BookKeeping($db);
  53. $formaccounting = new FormAccounting($db);
  54. $formother = new FormOther($db);
  55. $form = new Form($db);
  56. if (empty($search_date_start)) {
  57. $search_date_start = dol_mktime(0, 0, 0, 1, 1, dol_print_date(dol_now(), '%Y'));
  58. $search_date_end = dol_mktime(0, 0, 0, 12, 31, dol_print_date(dol_now(), '%Y'));
  59. }
  60. if ($sortorder == "")
  61. $sortorder = "ASC";
  62. if ($sortfield == "")
  63. $sortfield = "t.numero_compte";
  64. $options = '';
  65. $filter = array ();
  66. if (! empty($search_date_start)) {
  67. $filter['t.doc_date>='] = $search_date_start;
  68. $options .= '&amp;date_startmonth=' . GETPOST('date_startmonth', 'int') . '&amp;date_startday=' . GETPOST('date_startday', 'int') . '&amp;date_startyear=' . GETPOST('date_startyear', 'int');
  69. }
  70. if (! empty($search_date_end)) {
  71. $filter['t.doc_date<='] = $search_date_end;
  72. $options .= '&amp;date_endmonth=' . GETPOST('date_endmonth', 'int') . '&amp;date_endday=' . GETPOST('date_endday', 'int') . '&amp;date_endyear=' . GETPOST('date_endyear', 'int');
  73. }
  74. if (! empty($search_accountancy_code_start)) {
  75. $filter['t.numero_compte>='] = $search_accountancy_code_start;
  76. $options .= '&amp;search_accountancy_code_start=' . $search_accountancy_code_start;
  77. }
  78. if (! empty($search_accountancy_code_end)) {
  79. $filter['t.numero_compte<='] = $search_accountancy_code_end;
  80. $options .= '&amp;search_accountancy_code_end=' . $search_accountancy_code_end;
  81. }
  82. /*
  83. * Action
  84. */
  85. if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") || GETPOST("button_removefilter")) // Both test are required to be compatible with all browsers
  86. {
  87. $search_accountancy_code_start = '';
  88. $search_accountancy_code_end = '';
  89. $search_date_start = '';
  90. $search_date_end = '';
  91. }
  92. /*
  93. * View
  94. */
  95. if ($action == 'export_csv') {
  96. $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
  97. $journal = 'bookkepping';
  98. include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php';
  99. $result = $object->fetchAllBalance($sortorder, $sortfield, 0, 0, $filter);
  100. if ($result < 0) {
  101. setEventMessages($object->error, $object->errors, 'errors');
  102. }
  103. foreach ( $object->lines as $line ) {
  104. if ($conf->global->ACCOUNTING_EXPORT_MODELCSV == 2) {
  105. $sep = ";";
  106. }
  107. print length_accountg($line->numero_compte) . $sep;
  108. print $line->debit . $sep;
  109. print $line->credit . $sep;
  110. print $line->debit . $sep;
  111. print $line->credit - $line->debit . $sep;
  112. print "\n";
  113. }
  114. }
  115. else {
  116. $title_page = $langs->trans("AccountBalance") . ' ' . dol_print_date($search_date_start) . '-' . dol_print_date($search_date_end);
  117. llxHeader('', $title_page);
  118. // List
  119. $nbtotalofrecords = '';
  120. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  121. $nbtotalofrecords = $object->fetchAllBalance($sortorder, $sortfield, 0, 0, $filter);
  122. if ($nbtotalofrecords < 0) {
  123. setEventMessages($object->error, $object->errors, 'errors');
  124. }
  125. }
  126. $result = $object->fetchAllBalance($sortorder, $sortfield, $limit, $offset, $filter);
  127. if ($result < 0) {
  128. setEventMessages($object->error, $object->errors, 'errors');
  129. }
  130. print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
  131. $button = '<input type="submit" name="button_export_csv" class="butAction" value="' . $langs->trans("Export") . '" />';
  132. print_barre_liste($title_page, $page, $_SERVER["PHP_SELF"], $options, $sortfield, $sortorder, '', $result, 0, 'title_accountancy', 0, $button);
  133. $moreforfilter = '';
  134. $moreforfilter .= '<div class="divsearchfield">';
  135. $moreforfilter .= $langs->trans('DateStart') . ': ';
  136. $moreforfilter .= $form->select_date($search_date_start, 'date_start', 0, 0, 1, '', 1, 0, 1);
  137. $moreforfilter .= $langs->trans('DateEnd') . ': ';
  138. $moreforfilter .= $form->select_date($search_date_end, 'date_end', 0, 0, 1, '', 1, 0, 1);
  139. $moreforfilter .= '</div>';
  140. if (! empty($moreforfilter)) {
  141. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  142. print $moreforfilter;
  143. $parameters = array();
  144. $reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters); // Note that $action and $object may have been modified by hook
  145. print $hookmanager->resPrint;
  146. print '</div>';
  147. }
  148. print '<table class="liste ' . ($moreforfilter ? "listwithfilterbefore" : "") . '">';
  149. print '<tr class="liste_titre_filter">';
  150. print '<td class="liste_titre" colspan="5">';
  151. print $langs->trans('From');
  152. print $formaccounting->select_account($search_accountancy_code_start, 'search_accountancy_code_start', 1, array(), 1, 1, '');
  153. print ' ';
  154. print $langs->trans('to');
  155. print $formaccounting->select_account($search_accountancy_code_end, 'search_accountancy_code_end', 1, array(), 1, 1, '');
  156. print '</td>';
  157. print '<td align="right" class="liste_titre">';
  158. $searchpicto=$form->showFilterAndCheckAddButtons(0);
  159. print $searchpicto;
  160. print '</td>';
  161. print '</tr>';
  162. print '<tr class="liste_titre">';
  163. print_liste_field_titre($langs->trans("AccountAccounting"), $_SERVER['PHP_SELF'], "t.numero_compte", "", $options, "", $sortfield, $sortorder);
  164. print_liste_field_titre($langs->trans("Label"), $_SERVER['PHP_SELF'], "t.label_compte", "", $options, "", $sortfield, $sortorder);
  165. print_liste_field_titre($langs->trans("Debit"), $_SERVER['PHP_SELF'], "t.debit", "", $options, 'align="right"', $sortfield, $sortorder);
  166. print_liste_field_titre($langs->trans("Credit"), $_SERVER['PHP_SELF'], "t.credit", "", $options, 'align="right"', $sortfield, $sortorder);
  167. print_liste_field_titre($langs->trans("Solde"), $_SERVER["PHP_SELF"], "", $options, "", 'align="right"', $sortfield, $sortorder);
  168. print_liste_field_titre('', $_SERVER["PHP_SELF"], "", $options, "", 'width="60" align="center"', $sortfield, $sortorder);
  169. print "</tr>\n";
  170. $total_debit = 0;
  171. $total_credit = 0;
  172. $sous_total_debit = 0;
  173. $sous_total_credit = 0;
  174. $displayed_account = "";
  175. foreach ($object->lines as $line) {
  176. $link = '';
  177. $total_debit += $line->debit;
  178. $total_credit += $line->credit;
  179. $description = $object->get_compte_desc($line->numero_compte); // Search description of the account
  180. $root_account_description = $object->get_compte_racine($line->numero_compte);
  181. if (empty($description)) {
  182. $link = '<a href="../admin/card.php?action=create&compte=' . length_accountg($line->numero_compte) . '">' . img_edit_add() . '</a>';
  183. }
  184. print '<tr class="oddeven">';
  185. // Permet d'afficher le compte comptable
  186. if ($root_account_description != $displayed_account) {
  187. // Affiche un Sous-Total par compte comptable
  188. if ($displayed_account != "") {
  189. 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($sous_total_credit - $sous_total_debit) . '</td>';
  190. print "<td>&nbsp;</td>\n";
  191. print '</tr>';
  192. }
  193. // Affiche le compte comptable en d�but de ligne
  194. print "<tr>";
  195. print '<td colspan="6" style="font-weight:bold; border-bottom: 1pt solid black;">' . $root_account_description . '</td>';
  196. print '</tr>';
  197. $displayed_account = $root_account_description;
  198. $sous_total_debit = 0;
  199. $sous_total_credit = 0;
  200. }
  201. // $object->get_compte_racine($line->numero_compte);
  202. print '<td>' . length_accountg($line->numero_compte) . '</td>';
  203. print '<td>' . $description . '</td>';
  204. print '<td align="right">' . price($line->debit) . '</td>';
  205. print '<td align="right">' . price($line->credit) . '</td>';
  206. print '<td align="right">' . price($line->credit - $line->debit) . '</td>';
  207. print '<td align="center">' . $link;
  208. print '</td>';
  209. print "</tr>\n";
  210. // Comptabilise le sous-total
  211. $sous_total_debit += $line->debit;
  212. $sous_total_credit += $line->credit;
  213. }
  214. 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($sous_total_credit - $sous_total_debit) . '</td>';
  215. print "<td>&nbsp;</td>\n";
  216. print '</tr>';
  217. 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($total_credit - $total_debit) . '</td>';
  218. print "<td>&nbsp;</td>\n";
  219. print '</tr>';
  220. print "</table>";
  221. print '</form>';
  222. llxFooter();
  223. }
  224. $db->close();