dolibarr  7.0.0-beta
balance.php
Go to the documentation of this file.
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-2017 Alexandre Spangaro <aspangaro@zendsi.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 
27 require '../../main.inc.php';
28 
29 // Class
30 require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
31 require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
32 require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.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 
37 // Langs
38 $langs->load("accountancy");
39 
40 $page = GETPOST("page");
41 $sortorder = GETPOST("sortorder");
42 $sortfield = GETPOST("sortfield");
43 $action = GETPOST('action', 'alpha');
44 $search_date_start = dol_mktime(0, 0, 0, GETPOST('date_startmonth', 'int'), GETPOST('date_startday', 'int'), GETPOST('date_startyear', 'int'));
45 $search_date_end = dol_mktime(0, 0, 0, GETPOST('date_endmonth', 'int'), GETPOST('date_endday', 'int'), GETPOST('date_endyear', 'int'));
46 
47 $search_accountancy_code_start = GETPOST('search_accountancy_code_start', 'alpha');
48 if ($search_accountancy_code_start == - 1) {
49  $search_accountancy_code_start = '';
50 }
51 $search_accountancy_code_end = GETPOST('search_accountancy_code_end', 'alpha');
52 if ($search_accountancy_code_end == - 1) {
53  $search_accountancy_code_end = '';
54 }
55 
56 if (GETPOST("exportcsv")) $action = 'export_csv';
57 
58 
59 $limit = GETPOST('limit','int')?GETPOST('limit', 'int'):$conf->liste_limit;
60 if (empty($page) || $page < 0) { $page = 0; }
61 
62 $offset = $limit * $page;
63 
64 $object = new BookKeeping($db);
65 
66 $formaccounting = new FormAccounting($db);
67 $formother = new FormOther($db);
68 $form = new Form($db);
69 
70 if (empty($search_date_start) && ! GETPOSTISSET('formfilteraction'))
71 {
72  $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear ";
73  $sql.= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'";
74  $sql.= $db->plimit(1);
75  $res = $db->query($sql);
76  if ($res->num_rows > 0) {
77  $fiscalYear = $db->fetch_object($res);
78  $search_date_start = strtotime($fiscalYear->date_start);
79  $search_date_end = strtotime($fiscalYear->date_end);
80  } else {
81  $month_start= ($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
82  $year_start = dol_print_date(dol_now(), '%Y');
83  $year_end = $year_start + 1;
84  $month_end = $month_start - 1;
85  if ($month_end < 1)
86  {
87  $month_end = 12;
88  $year_end--;
89  }
90  $search_date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
91  $search_date_end = dol_get_last_day($year_end, $month_end);
92  }
93 }
94 if ($sortorder == "")
95  $sortorder = "ASC";
96 if ($sortfield == "")
97  $sortfield = "t.numero_compte";
98 
99 $options = '';
100 $filter = array ();
101 if (! empty($search_date_start)) {
102  $filter['t.doc_date>='] = $search_date_start;
103  $options .= '&amp;date_startmonth=' . GETPOST('date_startmonth', 'int') . '&amp;date_startday=' . GETPOST('date_startday', 'int') . '&amp;date_startyear=' . GETPOST('date_startyear', 'int');
104 }
105 if (! empty($search_date_end)) {
106  $filter['t.doc_date<='] = $search_date_end;
107  $options .= '&amp;date_endmonth=' . GETPOST('date_endmonth', 'int') . '&amp;date_endday=' . GETPOST('date_endday', 'int') . '&amp;date_endyear=' . GETPOST('date_endyear', 'int');
108 }
109 if (! empty($search_accountancy_code_start)) {
110  $filter['t.numero_compte>='] = $search_accountancy_code_start;
111  $options .= '&amp;search_accountancy_code_start=' . $search_accountancy_code_start;
112 }
113 if (! empty($search_accountancy_code_end)) {
114  $filter['t.numero_compte<='] = $search_accountancy_code_end;
115  $options .= '&amp;search_accountancy_code_end=' . $search_accountancy_code_end;
116 }
117 
118 
119 /*
120  * Action
121  */
122 
123 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
124 {
125  $search_accountancy_code_start = '';
126  $search_accountancy_code_end = '';
127  $search_date_start = '';
128  $search_date_end = '';
129  $filter = array();
130 }
131 
132 
133 /*
134  * View
135  */
136 
137 if ($action == 'export_csv') {
138 
139  $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
140 
141  $journal = 'balance';
142 
143  include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php';
144 
145  $result = $object->fetchAllBalance($sortorder, $sortfield, 0, 0, $filter);
146  if ($result < 0) {
147  setEventMessages($object->error, $object->errors, 'errors');
148  }
149 
150  foreach ($object->lines as $line)
151  {
152  print length_accountg($line->numero_compte) . $sep;
153  print $object->get_compte_desc($line->numero_compte) . $sep;
154  print price($line->debit) . $sep;
155  print price($line->credit) . $sep;
156  print price($line->debit) . $sep;
157  print price($line->credit - $line->debit) . $sep;
158  print "\n";
159  }
160 }
161 
162 else {
163  $title_page = $langs->trans("AccountBalance");
164 
165  llxHeader('', $title_page);
166 
167  // List
168  $nbtotalofrecords = '';
169  if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
170  {
171  $nbtotalofrecords = $object->fetchAllBalance($sortorder, $sortfield, 0, 0, $filter);
172  if ($nbtotalofrecords < 0) {
173  setEventMessages($object->error, $object->errors, 'errors');
174  }
175  }
176 
177  $result = $object->fetchAllBalance($sortorder, $sortfield, $limit, $offset, $filter);
178  if ($result < 0) {
179  setEventMessages($object->error, $object->errors, 'errors');
180  }
181 
182  print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
183  if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
184  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
185  print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
186  print '<input type="hidden" name="action" value="list">';
187  print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
188  print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
189  print '<input type="hidden" name="page" value="'.$page.'">';
190 
191  $button = '<input type="submit" name="exportcsv" class="butAction" value="' . $langs->trans("Export") . ' ('.$conf->global->ACCOUNTING_EXPORT_FORMAT.')" />';
192  print_barre_liste($title_page, $page, $_SERVER["PHP_SELF"], $options, $sortfield, $sortorder, '', $result, $result, 'title_accountancy', 0, $button);
193 
194  $moreforfilter = '';
195 
196  $moreforfilter .= '<div class="divsearchfield">';
197  $moreforfilter .= $langs->trans('DateStart') . ': ';
198  $moreforfilter .= $form->select_date($search_date_start?$search_date_start:-1, 'date_start', 0, 0, 1, '', 1, 0, 1);
199  $moreforfilter .= $langs->trans('DateEnd') . ': ';
200  $moreforfilter .= $form->select_date($search_date_end?$search_date_end:-1, 'date_end', 0, 0, 1, '', 1, 0, 1);
201  $moreforfilter .= '</div>';
202 
203  if (! empty($moreforfilter)) {
204  print '<div class="liste_titre liste_titre_bydiv centpercent">';
205  print $moreforfilter;
206  $parameters = array();
207  $reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters); // Note that $action and $object may have been modified by hook
208  print $hookmanager->resPrint;
209  print '</div>';
210  }
211 
212  print '<table class="liste ' . ($moreforfilter ? "listwithfilterbefore" : "") . '">';
213 
214  print '<tr class="liste_titre_filter">';
215  print '<td class="liste_titre" colspan="5">';
216  print $langs->trans('From');
217  print $formaccounting->select_account($search_accountancy_code_start, 'search_accountancy_code_start', 1, array(), 1, 1, '');
218  print ' ';
219  print $langs->trans('to');
220  print $formaccounting->select_account($search_accountancy_code_end, 'search_accountancy_code_end', 1, array(), 1, 1, '');
221  print '</td>';
222  print '<td align="right" class="liste_titre">';
223  $searchpicto=$form->showFilterAndCheckAddButtons(0);
224  print $searchpicto;
225  print '</td>';
226 
227  print '</tr>';
228 
229  print '<tr class="liste_titre">';
230  print_liste_field_titre("AccountAccounting", $_SERVER['PHP_SELF'], "t.numero_compte", "", $options, "", $sortfield, $sortorder);
231  print_liste_field_titre("Label", $_SERVER['PHP_SELF'], "t.label_operation", "", $options, "", $sortfield, $sortorder);
232  print_liste_field_titre("Debit", $_SERVER['PHP_SELF'], "t.debit", "", $options, 'align="right"', $sortfield, $sortorder);
233  print_liste_field_titre("Credit", $_SERVER['PHP_SELF'], "t.credit", "", $options, 'align="right"', $sortfield, $sortorder);
234  print_liste_field_titre("Balance", $_SERVER["PHP_SELF"], "", $options, "", 'align="right"', $sortfield, $sortorder);
235  print_liste_field_titre('', $_SERVER["PHP_SELF"], "", $options, "", 'width="60" align="center"', $sortfield, $sortorder);
236  print "</tr>\n";
237 
238  $total_debit = 0;
239  $total_credit = 0;
240  $sous_total_debit = 0;
241  $sous_total_credit = 0;
242  $displayed_account = "";
243 
244  foreach ($object->lines as $line)
245  {
246  $link = '';
247  $total_debit += $line->debit;
248  $total_credit += $line->credit;
249  $description = $object->get_compte_desc($line->numero_compte); // Search description of the account
250  $root_account_description = $object->get_compte_racine($line->numero_compte);
251  if (empty($description)) {
252  $link = '<a href="../admin/card.php?action=create&compte=' . length_accountg($line->numero_compte) . '">' . img_edit_add() . '</a>';
253  }
254  print '<tr class="oddeven">';
255 
256  // Permet d'afficher le compte comptable
257  if ($root_account_description != $displayed_account) {
258 
259  // Affiche un Sous-Total par compte comptable
260  if ($displayed_account != "") {
261  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>';
262  print "<td>&nbsp;</td>\n";
263  print '</tr>';
264  }
265 
266  // Affiche le compte comptable en d´┐Żbut de ligne
267  print "<tr>";
268  print '<td colspan="6" style="font-weight:bold; border-bottom: 1pt solid black;">' . $root_account_description . '</td>';
269  print '</tr>';
270 
271  $displayed_account = $root_account_description;
272  $sous_total_debit = 0;
273  $sous_total_credit = 0;
274  }
275 
276  // $object->get_compte_racine($line->numero_compte);
277 
278  print '<td>' . length_accountg($line->numero_compte) . '</td>';
279  print '<td>' . $description . '</td>';
280  print '<td align="right">' . price($line->debit) . '</td>';
281  print '<td align="right">' . price($line->credit) . '</td>';
282  print '<td align="right">' . price($line->credit - $line->debit) . '</td>';
283  print '<td align="center">' . $link;
284  print '</td>';
285  print "</tr>\n";
286 
287  // Comptabilise le sous-total
288  $sous_total_debit += $line->debit;
289  $sous_total_credit += $line->credit;
290  }
291 
292  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>';
293  print "<td>&nbsp;</td>\n";
294  print '</tr>';
295 
296  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>';
297  print "<td>&nbsp;</td>\n";
298  print '</tr>';
299 
300  print "</table>";
301  print '</form>';
302 
303  llxFooter();
304 }
305 
306 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
if(empty($reshook)) $form
View.
Definition: perms.php:103
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage Ledger (General Ledger and Subledger)
Class to manage generation of HTML components Only common components must be here.
print_barre_liste($titre, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='title_generic.png', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $hideselectlimit=0, $hidenavigation=0)
Print a title with navigation controls for pagination.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting a parameter.
Classe permettant la generation de composants html autre Only common components are here...
llxHeader()
Empty header.
Definition: wrapper.php:46
dol_now($mode='gmt')
Return date for now.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
img_edit_add($titlealt= 'default', $other= '')
Show logo +.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:459
print
Draft customers invoices.
Definition: index.php:91
Class to manage generation of HTML components for accounting management.
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="")
Show title line of an array.
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous) ...