dolibarr  9.0.0
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-2018 Alexandre Spangaro <aspangaro@zendsi.com>
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 
28 require '../../main.inc.php';
29 
30 // Class
31 require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
32 require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
33 require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.class.php';
34 require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountancyexport.class.php';
35 require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php';
36 require_once DOL_DOCUMENT_ROOT . '/core/class/html.formother.class.php';
37 
38 // Load translation files required by the page
39 $langs->loadLangs(array("accountancy"));
40 
41 $page = GETPOST("page");
42 $sortorder = GETPOST("sortorder", 'alpha');
43 $sortfield = GETPOST("sortfield", 'alpha');
44 $action = GETPOST('action', 'aZ09');
45 if (GETPOST("exportcsv",'alpha')) $action = 'export_csv';
46 
47 // Load variable for pagination
48 $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
49 $sortfield = GETPOST('sortfield','alpha');
50 $sortorder = GETPOST('sortorder','alpha');
51 $page = GETPOST('page','int');
52 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
53 $offset = $limit * $page;
54 $pageprev = $page - 1;
55 $pagenext = $page + 1;
56 //if (! $sortfield) $sortfield="p.date_fin";
57 //if (! $sortorder) $sortorder="DESC";
58 
59 
60 $search_date_start = dol_mktime(0, 0, 0, GETPOST('date_startmonth', 'int'), GETPOST('date_startday', 'int'), GETPOST('date_startyear', 'int'));
61 $search_date_end = dol_mktime(23, 59, 59, GETPOST('date_endmonth', 'int'), GETPOST('date_endday', 'int'), GETPOST('date_endyear', 'int'));
62 
63 $search_accountancy_code_start = GETPOST('search_accountancy_code_start', 'alpha');
64 if ($search_accountancy_code_start == - 1) {
65  $search_accountancy_code_start = '';
66 }
67 $search_accountancy_code_end = GETPOST('search_accountancy_code_end', 'alpha');
68 if ($search_accountancy_code_end == - 1) {
69  $search_accountancy_code_end = '';
70 }
71 
72 $object = new BookKeeping($db);
73 
74 $formaccounting = new FormAccounting($db);
75 $formother = new FormOther($db);
76 $form = new Form($db);
77 
78 if (empty($search_date_start) && ! GETPOSTISSET('formfilteraction'))
79 {
80  $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear ";
81  $sql.= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'";
82  $sql.= $db->plimit(1);
83  $res = $db->query($sql);
84  if ($res->num_rows > 0) {
85  $fiscalYear = $db->fetch_object($res);
86  $search_date_start = strtotime($fiscalYear->date_start);
87  $search_date_end = strtotime($fiscalYear->date_end);
88  } else {
89  $month_start= ($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
90  $year_start = dol_print_date(dol_now(), '%Y');
91  $year_end = $year_start + 1;
92  $month_end = $month_start - 1;
93  if ($month_end < 1)
94  {
95  $month_end = 12;
96  $year_end--;
97  }
98  $search_date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
99  $search_date_end = dol_get_last_day($year_end, $month_end);
100  }
101 }
102 if ($sortorder == "")
103  $sortorder = "ASC";
104 if ($sortfield == "")
105  $sortfield = "t.numero_compte";
106 
107 
108 $param='';
109 if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.urlencode($contextpage);
110 if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.urlencode($limit);
111 
112 $filter = array ();
113 if (! empty($search_date_start)) {
114  $filter['t.doc_date>='] = $search_date_start;
115  $param .= '&amp;date_startmonth=' . GETPOST('date_startmonth', 'int') . '&amp;date_startday=' . GETPOST('date_startday', 'int') . '&amp;date_startyear=' . GETPOST('date_startyear', 'int');
116 }
117 if (! empty($search_date_end)) {
118  $filter['t.doc_date<='] = $search_date_end;
119  $param .= '&amp;date_endmonth=' . GETPOST('date_endmonth', 'int') . '&amp;date_endday=' . GETPOST('date_endday', 'int') . '&amp;date_endyear=' . GETPOST('date_endyear', 'int');
120 }
121 if (! empty($search_accountancy_code_start)) {
122  $filter['t.numero_compte>='] = $search_accountancy_code_start;
123  $param .= '&amp;search_accountancy_code_start=' . $search_accountancy_code_start;
124 }
125 if (! empty($search_accountancy_code_end)) {
126  $filter['t.numero_compte<='] = $search_accountancy_code_end;
127  $param .= '&amp;search_accountancy_code_end=' . $search_accountancy_code_end;
128 }
129 
130 /*
131  * Action
132  */
133 
134 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
135 {
136  $search_accountancy_code_start = '';
137  $search_accountancy_code_end = '';
138  $search_date_start = '';
139  $search_date_end = '';
140  $filter = array();
141 }
142 
143 
144 /*
145  * View
146  */
147 
148 if ($action == 'export_csv')
149 {
150  $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
151 
152  $filename = 'balance';
153  $type_export = 'balance';
154  include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php';
155 
156  $result = $object->fetchAllBalance($sortorder, $sortfield, $limit, 0, $filter);
157  if ($result < 0) {
158  setEventMessages($object->error, $object->errors, 'errors');
159  }
160 
161  foreach ($object->lines as $line)
162  {
163  print length_accountg($line->numero_compte) . $sep;
164  print $object->get_compte_desc($line->numero_compte) . $sep;
165  print price($line->debit) . $sep;
166  print price($line->credit) . $sep;
167  print price($line->credit - $line->debit) . $sep;
168  print "\n";
169  }
170 
171  exit;
172 }
173 
174 
175 $title_page = $langs->trans("AccountBalance");
176 
177 llxHeader('', $title_page);
178 
179 
180 if ($action != 'export_csv')
181 {
182  // List
183  $nbtotalofrecords = '';
184  if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
185  {
186  $nbtotalofrecords = $object->fetchAllBalance($sortorder, $sortfield, 0, 0, $filter);
187  if ($nbtotalofrecords < 0) {
188  setEventMessages($object->error, $object->errors, 'errors');
189  }
190  }
191 
192  $result = $object->fetchAllBalance($sortorder, $sortfield, $limit, $offset, $filter);
193  if ($result < 0) {
194  setEventMessages($object->error, $object->errors, 'errors');
195  }
196 
197  print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
198  if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
199  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
200  print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
201  print '<input type="hidden" name="action" value="list">';
202  print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
203  print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
204  print '<input type="hidden" name="page" value="'.$page.'">';
205 
206  $button = '<input type="submit" name="exportcsv" class="butAction" value="' . $langs->trans("Export") . ' ('.$conf->global->ACCOUNTING_EXPORT_FORMAT.')" />';
207 
208  print_barre_liste($title_page, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $button, $result, $nbtotalofrecords, 'title_accountancy', 0, '', '', $limit);
209 
210  $moreforfilter = '';
211 
212  $moreforfilter .= '<div class="divsearchfield">';
213  $moreforfilter .= $langs->trans('DateStart') . ': ';
214  $moreforfilter .= $form->selectDate($search_date_start?$search_date_start:-1, 'date_start', 0, 0, 1, '', 1, 0);
215  $moreforfilter .= $langs->trans('DateEnd') . ': ';
216  $moreforfilter .= $form->selectDate($search_date_end?$search_date_end:-1, 'date_end', 0, 0, 1, '', 1, 0);
217  $moreforfilter .= '</div>';
218 
219  if (! empty($moreforfilter)) {
220  print '<div class="liste_titre liste_titre_bydiv centpercent">';
221  print $moreforfilter;
222  $parameters = array();
223  $reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters); // Note that $action and $object may have been modified by hook
224  print $hookmanager->resPrint;
225  print '</div>';
226  }
227 
228  print '<table class="liste ' . ($moreforfilter ? "listwithfilterbefore" : "") . '">';
229 
230  print '<tr class="liste_titre_filter">';
231  print '<td class="liste_titre" colspan="5">';
232  print $langs->trans('From');
233  print $formaccounting->select_account($search_accountancy_code_start, 'search_accountancy_code_start', 1, array(), 1, 1, '');
234  print ' ';
235  print $langs->trans('to');
236  print $formaccounting->select_account($search_accountancy_code_end, 'search_accountancy_code_end', 1, array(), 1, 1, '');
237  print '</td>';
238  print '<td align="right" class="liste_titre">';
239  $searchpicto=$form->showFilterAndCheckAddButtons(0);
240  print $searchpicto;
241  print '</td>';
242 
243  print '</tr>';
244 
245  print '<tr class="liste_titre">';
246  print_liste_field_titre("AccountAccounting", $_SERVER['PHP_SELF'], "t.numero_compte", "", $param, "", $sortfield, $sortorder);
247  print_liste_field_titre("Label", $_SERVER['PHP_SELF'], "t.label_operation", "", $param, "", $sortfield, $sortorder);
248  print_liste_field_titre("Debit", $_SERVER['PHP_SELF'], "t.debit", "", $param, 'align="right"', $sortfield, $sortorder);
249  print_liste_field_titre("Credit", $_SERVER['PHP_SELF'], "t.credit", "", $param, 'align="right"', $sortfield, $sortorder);
250  print_liste_field_titre("Balance", $_SERVER["PHP_SELF"], "", $param, "", 'align="right"', $sortfield, $sortorder);
251  print_liste_field_titre('', $_SERVER["PHP_SELF"], "", $param, "", 'width="60" align="center"', $sortfield, $sortorder);
252  print "</tr>\n";
253 
254  $total_debit = 0;
255  $total_credit = 0;
256  $sous_total_debit = 0;
257  $sous_total_credit = 0;
258  $displayed_account = "";
259 
260  foreach ($object->lines as $line)
261  {
262  $link = '';
263  $total_debit += $line->debit;
264  $total_credit += $line->credit;
265  $description = $object->get_compte_desc($line->numero_compte); // Search description of the account
266  $root_account_description = $object->get_compte_racine($line->numero_compte);
267  if (empty($description)) {
268  $link = '<a href="../admin/card.php?action=create&accountingaccount=' . length_accountg($line->numero_compte) . '">' . img_edit_add() . '</a>';
269  }
270  print '<tr class="oddeven">';
271 
272  // Permet d'afficher le compte comptable
273  if (empty($displayed_account) || $root_account_description != $displayed_account)
274  {
275  // Affiche un Sous-Total par compte comptable
276  if ($displayed_account != "") {
277  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>';
278  print "<td>&nbsp;</td>\n";
279  print '</tr>';
280  }
281 
282  // Affiche le compte comptable en debut de ligne
283  print "<tr>";
284  print '<td colspan="6" style="font-weight:bold; border-bottom: 1pt solid black;">' . $line->numero_compte . ($root_account_description ? ' - ' . $root_account_description : '') . '</td>';
285  print '</tr>';
286 
287  $displayed_account = $root_account_description;
288  $sous_total_debit = 0;
289  $sous_total_credit = 0;
290  }
291 
292  // $object->get_compte_racine($line->numero_compte);
293 
294  print '<td>' . length_accountg($line->numero_compte) . '</td>';
295  print '<td>' . $description . '</td>';
296  print '<td align="right">' . price($line->debit) . '</td>';
297  print '<td align="right">' . price($line->credit) . '</td>';
298  print '<td align="right">' . price($line->credit - $line->debit) . '</td>';
299  print '<td align="center">' . $link;
300  print '</td>';
301  print "</tr>\n";
302 
303  // Comptabilise le sous-total
304  $sous_total_debit += $line->debit;
305  $sous_total_credit += $line->credit;
306  }
307 
308  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>';
309  print "<td>&nbsp;</td>\n";
310  print '</tr>';
311 
312  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>';
313  print "<td>&nbsp;</td>\n";
314  print '</tr>';
315 
316  print "</table>";
317  print '</form>';
318 }
319 
320 // End of page
321 llxFooter();
322 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
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...
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:44
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).
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:467
Class to manage generation of HTML components for accounting management.
img_edit_add($titlealt='default', $other='')
Show logo +.
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) ...
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...