balance.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/html.formventilation.class.php';
  29. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.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. $formventilation = new FormVentilation($db);
  54. $formother = new FormOther($db);
  55. $form = new Form($db);
  56. if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter")) // Both test are required to be compatible with all browsers
  57. {
  58. $search_accountancy_code_start = '';
  59. $search_accountancy_code_end = '';
  60. $search_date_start = '';
  61. $search_date_end = '';
  62. }
  63. if (empty($search_date_start)) {
  64. $search_date_start = dol_mktime(0, 0, 0, 1, 1, dol_print_date(dol_now(), '%Y'));
  65. $search_date_end = dol_mktime(0, 0, 0, 12, 31, dol_print_date(dol_now(), '%Y'));
  66. }
  67. if ($sortorder == "")
  68. $sortorder = "ASC";
  69. if ($sortfield == "")
  70. $sortfield = "t.numero_compte";
  71. $options = '';
  72. $filter = array ();
  73. if (! empty($search_date_start)) {
  74. $filter['t.doc_date>='] = $search_date_start;
  75. $options .= '&amp;date_startmonth=' . GETPOST('date_startmonth', 'int') . '&amp;date_startday=' . GETPOST('date_startday', 'int') . '&amp;date_startyear=' . GETPOST('date_startyear', 'int');
  76. }
  77. if (! empty($search_date_end)) {
  78. $filter['t.doc_date<='] = $search_date_end;
  79. $options .= '&amp;date_endmonth=' . GETPOST('date_endmonth', 'int') . '&amp;date_endday=' . GETPOST('date_endday', 'int') . '&amp;date_endyear=' . GETPOST('date_endyear', 'int');
  80. }
  81. if (! empty($search_accountancy_code_start)) {
  82. $filter['t.numero_compte>='] = $search_accountancy_code_start;
  83. $options .= '&amp;search_accountancy_code_start=' . $search_accountancy_code_start;
  84. }
  85. if (! empty($search_accountancy_code_end)) {
  86. $filter['t.numero_compte<='] = $search_accountancy_code_end;
  87. $options .= '&amp;search_accountancy_code_end=' . $search_accountancy_code_end;
  88. }
  89. /*
  90. * Action
  91. */
  92. if ($action == 'export_csv') {
  93. $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
  94. $journal = 'bookkepping';
  95. include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php';
  96. $result = $object->fetchAllBalance($sortorder, $sortfield, 0, 0, $filter);
  97. if ($result < 0) {
  98. setEventMessages($object->error, $object->errors, 'errors');
  99. }
  100. foreach ( $object->lines as $line ) {
  101. if ($conf->global->ACCOUNTING_EXPORT_MODELCSV == 2) {
  102. $sep = ";";
  103. }
  104. print length_accountg($line->numero_compte) . $sep;
  105. print $line->debit . $sep;
  106. print $line->credit . $sep;
  107. print $line->debit . $sep;
  108. print $line->credit - $line->debit . $sep;
  109. print "\n";
  110. }
  111. }
  112. else {
  113. $title_page = $langs->trans("AccountBalance") . ' ' . dol_print_date($search_date_start) . '-' . dol_print_date($search_date_end);
  114. llxHeader('', $title_page);
  115. /*
  116. * List
  117. */
  118. $nbtotalofrecords = 0;
  119. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  120. $nbtotalofrecords = $object->fetchAllBalance($sortorder, $sortfield, 0, 0, $filter);
  121. if ($nbtotalofrecords < 0) {
  122. setEventMessages($object->error, $object->errors, 'errors');
  123. }
  124. }
  125. $result = $object->fetchAllBalance($sortorder, $sortfield, $limit, $offset, $filter);
  126. if ($result < 0) {
  127. setEventMessages($object->error, $object->errors, 'errors');
  128. }
  129. print_barre_liste($title_page, $page, $_SERVER["PHP_SELF"], $options, $sortfield, $sortorder, '', $result);
  130. print '<form method="GET" id="searchFormList" action="' . $_SERVER["PHP_SELF"] . '">';
  131. print '<div class="tabsAction">' . "\n";
  132. print '<div class="inline-block divButAction"><input type="submit" name="button_export_csv" class="butAction" value="' . $langs->trans("Export") . '" /></div>';
  133. print '</div>';
  134. $moreforfilter='';
  135. $moreforfilter.='<div class="divsearchfield">';
  136. $moreforfilter.=$langs->trans('DateStart') . ': ';
  137. $moreforfilter.=$form->select_date($search_date_start, 'date_start', 0, 0, 1, '', 1, 0, 1);
  138. $moreforfilter.=$langs->trans('DateEnd') . ': ';
  139. $moreforfilter.=$form->select_date($search_date_end, 'date_end', 0, 0, 1, '', 1, 0, 1);
  140. $moreforfilter.='</div>';
  141. if (! empty($moreforfilter))
  142. {
  143. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  144. print $moreforfilter;
  145. $parameters=array();
  146. $reshook=$hookmanager->executeHooks('printFieldPreListTitle',$parameters); // Note that $action and $object may have been modified by hook
  147. print $hookmanager->resPrint;
  148. print '</div>';
  149. }
  150. print '<table class="liste '.($moreforfilter?"listwithfilterbefore":"").'">';
  151. print '<tr class="liste_titre">';
  152. print_liste_field_titre($langs->trans("AccountAccountingShort"), $_SERVER['PHP_SELF'], "t.numero_compte", "", $options, "", $sortfield, $sortorder);
  153. print_liste_field_titre($langs->trans("Labelcompte"), $_SERVER['PHP_SELF'], "t.label_compte", "", $options, "", $sortfield, $sortorder);
  154. print_liste_field_titre($langs->trans("Debit"), $_SERVER['PHP_SELF'], "t.debit", "", $options, 'align="right"', $sortfield, $sortorder);
  155. print_liste_field_titre($langs->trans("Credit"), $_SERVER['PHP_SELF'], "t.credit", "", $options, 'align="right"', $sortfield, $sortorder);
  156. print_liste_field_titre($langs->trans("Solde"), $_SERVER["PHP_SELF"], "", $options, "", 'align="right"', $sortfield, $sortorder);
  157. print_liste_field_titre($langs->trans("Action"), $_SERVER["PHP_SELF"], "", $options, "", 'width="60" align="center"', $sortfield, $sortorder);
  158. print "</tr>\n";
  159. print '<tr class="liste_titre">';
  160. print '<td colspan="2">';
  161. print $langs->trans('From');
  162. print $formventilation->select_account($search_accountancy_code_start, 'search_accountancy_code_start', 1, array (), 1, 1, '');
  163. print '<br>';
  164. print $langs->trans('to');
  165. print $formventilation->select_account($search_accountancy_code_end, 'search_accountancy_code_end', 1, array (), 1, 1, '');
  166. print '</td>';
  167. print '<td>&nbsp;</td>';
  168. print '<td>&nbsp;</td>';
  169. print '<td>&nbsp;</td>';
  170. print '<td align="right" class="liste_titre">';
  171. print '<input type="image" class="liste_titre" src="' . img_picto($langs->trans("Search"), 'search.png', '', '', 1) . '" name="button_search" value="' . dol_escape_htmltag($langs->trans("Search")) . '" title="' . dol_escape_htmltag($langs->trans("Search")) . '">';
  172. print '&nbsp;';
  173. print '<input type="image" class="liste_titre" src="' . img_picto($langs->trans("Search"), 'searchclear.png', '', '', 1) . '" name="button_removefilter" value="' . dol_escape_htmltag($langs->trans("RemoveFilter")) . '" title="' . dol_escape_htmltag($langs->trans("RemoveFilter")) . '">';
  174. print '</td>';
  175. print '</tr>';
  176. $var = True;
  177. $total_debit = 0;
  178. $total_credit = 0;
  179. $sous_total_debit = 0;
  180. $sous_total_credit = 0;
  181. $displayed_account = "";
  182. foreach ( $object->lines as $line ) {
  183. $var = ! $var;
  184. $link = '';
  185. $total_debit += $line->debit;
  186. $total_credit += $line->credit;
  187. $description = $object->get_compte_desc($line->numero_compte); // Search description of the account
  188. $root_account_description = $object->get_compte_racine($line->numero_compte);
  189. if(empty($description)){
  190. $link = '<a href="../admin/card.php?action=create&compte=' . length_accountg($line->numero_compte) . '">' . img_edit_add() .'</a>';
  191. }
  192. print '<tr'. $bc[$var].'>';
  193. // Permet d'afficher le compte comptable
  194. if ($root_account_description != $displayed_account) {
  195. // Affiche un Sous-Total par compte comptable
  196. if ($displayed_account != "") {
  197. 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>';
  198. print "<td>&nbsp;</td>\n";
  199. print '</tr>';
  200. }
  201. // Affiche le compte comptable en début de ligne
  202. print "<tr>";
  203. print '<td colspan="6" style="font-weight:bold; border-bottom: 1pt solid black;">'. $root_account_description .'</td>';
  204. print '</tr>';
  205. $displayed_account = $root_account_description;
  206. $sous_total_debit = 0;
  207. $sous_total_credit = 0;
  208. }
  209. // $object->get_compte_racine($line->numero_compte);
  210. print '<td>' . length_accountg($line->numero_compte) . '</td>';
  211. print '<td>' . $description . '</td>';
  212. print '<td align="right">' . number_format($line->debit, 2, ',', ' ') . '</td>';
  213. print '<td align="right">' . number_format($line->credit, 2, ',', ' ') . '</td>';
  214. print '<td align="right">' . number_format($line->credit - $line->debit, 2, ',', ' ') . '</td>';
  215. print '<td align="center">' . $link;
  216. print '</td>';
  217. print "</tr>\n";
  218. // Comptabilise le sous-total
  219. $sous_total_debit += $line->debit;
  220. $sous_total_credit += $line->credit;
  221. }
  222. 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>';
  223. print "<td>&nbsp;</td>\n";
  224. print '</tr>';
  225. 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>';
  226. print "<td>&nbsp;</td>\n";
  227. print '</tr>';
  228. print "</table>";
  229. print '</form>';
  230. llxFooter();
  231. }
  232. $db->close();