28 require
'../../main.inc.php';
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/accountingjournal.class.php';
35 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingaccount.class.php';
36 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountancyexport.class.php';
37 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formaccounting.class.php';
38 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formother.class.php';
41 $langs->loadLangs(array(
"accountancy",
"compta"));
43 $action =
GETPOST(
'action',
'aZ09');
44 $optioncss =
GETPOST(
'optioncss',
'alpha');
45 $contextpage =
GETPOST(
'contextpage',
'aZ09');
48 $limit =
GETPOST(
'limit',
'int') ?
GETPOST(
'limit',
'int') : $conf->liste_limit;
49 $sortfield =
GETPOST(
'sortfield',
'aZ09comma');
50 $sortorder =
GETPOST(
'sortorder',
'aZ09comma');
52 if (empty($page) || $page == -1 ||
GETPOST(
'button_search',
'alpha') ||
GETPOST(
'button_removefilter',
'alpha') || (empty($toselect) && $massaction ===
'0')) {
55 $offset = $limit * $page;
56 $pageprev = $page - 1;
57 $pagenext = $page + 1;
61 $show_subgroup =
GETPOST(
'show_subgroup',
'alpha');
64 $search_ledger_code =
GETPOST(
'search_ledger_code',
'array');
65 $search_accountancy_code_start =
GETPOST(
'search_accountancy_code_start',
'alpha');
66 if ($search_accountancy_code_start == - 1) {
67 $search_accountancy_code_start =
'';
69 $search_accountancy_code_end =
GETPOST(
'search_accountancy_code_end',
'alpha');
70 if ($search_accountancy_code_end == - 1) {
71 $search_accountancy_code_end =
'';
76 $hookmanager->initHooks(array(
'balancelist'));
82 if (empty($search_date_start) && !
GETPOSTISSET(
'formfilteraction')) {
83 $sql =
"SELECT date_start, date_end from ".MAIN_DB_PREFIX.
"accounting_fiscalyear ";
84 $sql .=
" WHERE date_start < '".$db->idate(
dol_now()).
"' AND date_end > '".$db->idate(
dol_now()).
"'";
85 $sql .= $db->plimit(1);
86 $res = $db->query($sql);
87 if ($res->num_rows > 0) {
88 $fiscalYear = $db->fetch_object($res);
89 $search_date_start = strtotime($fiscalYear->date_start);
90 $search_date_end = strtotime($fiscalYear->date_end);
92 $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
97 $year_end = $year_start + 1;
98 $month_end = $month_start - 1;
103 $search_date_start =
dol_mktime(0, 0, 0, $month_start, 1, $year_start);
107 if ($sortorder ==
"") {
110 if ($sortfield ==
"") {
111 $sortfield =
"t.numero_compte";
116 if (!empty($contextpage) && $contextpage != $_SERVER[
"PHP_SELF"]) {
117 $param .=
'&contextpage='.urlencode($contextpage);
119 if ($limit > 0 && $limit != $conf->liste_limit) {
120 $param .=
'&limit='.urlencode($limit);
124 if (!empty($search_date_start)) {
125 $filter[
't.doc_date>='] = $search_date_start;
126 $param .=
'&date_startmonth='.GETPOST(
'date_startmonth',
'int').
'&date_startday='.
GETPOST(
'date_startday',
'int').
'&date_startyear='.
GETPOST(
'date_startyear',
'int');
128 if (!empty($search_date_end)) {
129 $filter[
't.doc_date<='] = $search_date_end;
130 $param .=
'&date_endmonth='.GETPOST(
'date_endmonth',
'int').
'&date_endday='.
GETPOST(
'date_endday',
'int').
'&date_endyear='.
GETPOST(
'date_endyear',
'int');
132 if (!empty($search_accountancy_code_start)) {
133 $filter[
't.numero_compte>='] = $search_accountancy_code_start;
134 $param .=
'&search_accountancy_code_start='.urlencode($search_accountancy_code_start);
136 if (!empty($search_accountancy_code_end)) {
137 $filter[
't.numero_compte<='] = $search_accountancy_code_end;
138 $param .=
'&search_accountancy_code_end='.urlencode($search_accountancy_code_end);
140 if (!empty($search_ledger_code)) {
141 $filter[
't.code_journal'] = $search_ledger_code;
142 foreach ($search_ledger_code as $code) {
143 $param .=
'&search_ledger_code[]='.urlencode($code);
150 if ($user->socid > 0) {
153 if (!$user->hasRight(
'accounting',
'mouvements',
'lire')) {
163 $parameters = array();
164 $arrayfields = array();
165 $reshook = $hookmanager->executeHooks(
'doActions', $parameters, $object, $action);
170 if (empty($reshook)) {
171 if (
GETPOST(
'button_removefilter_x',
'alpha') ||
GETPOST(
'button_removefilter.x',
'alpha') ||
GETPOST(
'button_removefilter',
'alpha')) {
173 $search_date_start =
'';
174 $search_date_end =
'';
175 $search_accountancy_code_start =
'';
176 $search_accountancy_code_end =
'';
177 $search_ledger_code = array();
186 if ($action ==
'export_csv') {
187 $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
189 $filename =
'balance';
190 $type_export =
'balance';
191 include DOL_DOCUMENT_ROOT.
'/accountancy/tpl/export_journal.tpl.php';
193 $result = $object->fetchAllBalance($sortorder, $sortfield, $limit, 0, $filter);
198 foreach ($object->lines as $line) {
199 print
'"'.length_accountg($line->numero_compte).
'"'.$sep;
200 print
'"'.$object->get_compte_desc($line->numero_compte).
'"'.$sep;
201 print
'"'.price($line->debit).
'"'.$sep;
202 print
'"'.price($line->credit).
'"'.$sep;
203 print
'"'.price($line->debit - $line->credit).
'"'.$sep;
211 $title_page = $langs->trans(
"AccountBalance");
216 if ($action !=
'export_csv') {
219 if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
220 $nbtotalofrecords = $object->fetchAllBalance($sortorder, $sortfield, 0, 0, $filter);
226 $result = $object->fetchAllBalance($sortorder, $sortfield, $limit, $offset, $filter);
231 print
'<form method="POST" id="searchFormList" action="'.$_SERVER[
"PHP_SELF"].
'">';
232 if ($optioncss !=
'') {
233 print
'<input type="hidden" name="optioncss" value="'.$optioncss.
'">';
235 print
'<input type="hidden" name="token" value="'.newToken().
'">';
236 print
'<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
237 print
'<input type="hidden" name="action" id="action" value="list">';
238 print
'<input type="hidden" name="sortfield" value="'.$sortfield.
'">';
239 print
'<input type="hidden" name="sortorder" value="'.$sortorder.
'">';
240 print
'<input type="hidden" name="page" value="'.$page.
'">';
243 $parameters = array();
244 $reshook = $hookmanager->executeHooks(
'addMoreActionsButtonsList', $parameters, $object, $action);
249 $button = empty($hookmanager->resPrint) ?
'' : $hookmanager->resPrint;
251 if (empty($reshook)) {
252 $button .=
'<input type="button" id="exportcsvbutton" name="exportcsvbutton" class="butAction" value="'.$langs->trans(
"Export").
' ('.$conf->global->ACCOUNTING_EXPORT_FORMAT.
')" />';
254 print
'<script type="text/javascript">
255 jQuery(document).ready(function() {
256 jQuery("#exportcsvbutton").click(function() {
257 event.preventDefault();
258 console.log("Set action to export_csv");
259 jQuery("#action").val("export_csv");
260 jQuery("#searchFormList").submit();
261 jQuery("#action").val("list");
267 print_barre_liste($title_page, $page, $_SERVER[
"PHP_SELF"], $param, $sortfield, $sortorder, $button, $result,
$nbtotalofrecords,
'title_accountancy', 0,
'',
'', $limit);
269 $selectedfields =
'';
273 $moreforfilter .=
'<div class="divsearchfield">';
274 $moreforfilter .= $langs->trans(
'DateStart').
': ';
275 $moreforfilter .=
$form->selectDate($search_date_start ? $search_date_start : -1,
'date_start', 0, 0, 1,
'', 1, 0);
276 $moreforfilter .= $langs->trans(
'DateEnd').
': ';
277 $moreforfilter .=
$form->selectDate($search_date_end ? $search_date_end : -1,
'date_end', 0, 0, 1,
'', 1, 0);
279 $moreforfilter .=
' - ';
280 $moreforfilter .=
'<label for="show_subgroup">'.$langs->trans(
'ShowSubtotalByGroup').
'</label>: ';
281 $moreforfilter .=
'<input type="checkbox" name="show_subgroup" id="show_subgroup" value="show_subgroup"'.($show_subgroup ==
'show_subgroup' ?
' checked' :
'').
'>';
283 $moreforfilter .=
'</div>';
285 $moreforfilter .=
'<div class="divsearchfield">';
287 $moreforfilter .= $langs->trans(
"Journal");
288 $moreforfilter .= $formaccounting->multi_select_journal($search_ledger_code,
'search_ledger_code', 0, 1, 1, 1);
290 $moreforfilter .=
'</div>';
292 if (!empty($moreforfilter)) {
293 print
'<div class="liste_titre liste_titre_bydiv centpercent">';
294 print $moreforfilter;
295 $parameters = array();
296 $reshook = $hookmanager->executeHooks(
'printFieldPreListTitle', $parameters);
297 print $hookmanager->resPrint;
302 $colspan = (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE) ? 5 : 4);
304 print
'<table class="liste '.($moreforfilter ?
"listwithfilterbefore" :
"").
'">';
306 print
'<tr class="liste_titre_filter">';
307 print
'<td class="liste_titre" colspan="'.$colspan.
'">';
308 print $formaccounting->select_account($search_accountancy_code_start,
'search_accountancy_code_start', $langs->trans(
'From'), array(), 1, 1,
'',
'accounts');
310 print $formaccounting->select_account($search_accountancy_code_end,
'search_accountancy_code_end', $langs->trans(
'to'), array(), 1, 1,
'',
'accounts');
314 $parameters = array(
'arrayfields'=>$arrayfields);
315 $reshook = $hookmanager->executeHooks(
'printFieldListOption', $parameters, $object);
316 print $hookmanager->resPrint;
319 print
'<td class="liste_titre maxwidthsearch">';
320 $searchpicto =
$form->showFilterButtons();
325 print
'<tr class="liste_titre">';
326 print_liste_field_titre(
"AccountAccounting", $_SERVER[
'PHP_SELF'],
"t.numero_compte",
"", $param,
"", $sortfield, $sortorder);
327 if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
328 print_liste_field_titre(
"OpeningBalance", $_SERVER[
'PHP_SELF'],
"", $param,
"",
'class="right"', $sortfield, $sortorder);
330 print_liste_field_titre(
"Debit", $_SERVER[
'PHP_SELF'],
"t.debit",
"", $param,
'class="right"', $sortfield, $sortorder);
331 print_liste_field_titre(
"Credit", $_SERVER[
'PHP_SELF'],
"t.credit",
"", $param,
'class="right"', $sortfield, $sortorder);
332 print_liste_field_titre(
"Balance", $_SERVER[
"PHP_SELF"],
"", $param,
"",
'class="right"', $sortfield, $sortorder);
335 $parameters = array(
'arrayfields'=>$arrayfields,
'param'=>$param,
'sortfield'=>$sortfield,
'sortorder'=>$sortorder);
336 $reshook = $hookmanager->executeHooks(
'printFieldListTitle', $parameters, $object);
337 print $hookmanager->resPrint;
339 print
getTitleFieldOfList($selectedfields, 0, $_SERVER[
"PHP_SELF"],
'',
'',
'',
'', $sortfield, $sortorder,
'center maxwidthsearch ').
"\n";
344 $sous_total_debit = 0;
345 $sous_total_credit = 0;
346 $total_opening_balance = 0;
347 $sous_total_opening_balance = 0;
348 $displayed_account =
"";
354 if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
355 $sql =
"SELECT t.numero_compte, (SUM(t.debit) - SUM(t.credit)) as opening_balance";
356 $sql .=
" FROM " . MAIN_DB_PREFIX .
"accounting_bookkeeping as t";
357 $sql .=
" WHERE t.entity = " . $conf->entity;
358 $sql .=
" AND t.doc_date < '" . $db->idate($search_date_start) .
"'";
359 $sql .=
" GROUP BY t.numero_compte";
361 $resql = $db->query($sql);
362 $nrows = $db->num_rows(
$resql);
363 $opening_balances = array();
364 for ($i = 0; $i < $nrows; $i++) {
365 $arr =
$resql->fetch_array();
366 $opening_balances[
"'" . $arr[
'numero_compte'] .
"'"] = $arr[
'opening_balance'];
370 foreach ($object->lines as $line) {
372 $accountingaccountstatic->id = 0;
373 $accountingaccountstatic->account_number =
'';
375 $accountingaccountstatic->fetch(
null, $line->numero_compte,
true);
376 if (!empty($accountingaccountstatic->account_number)) {
377 $accounting_account = $accountingaccountstatic->getNomUrl(0, 1, 0,
'', 0, -1, 0,
'accountcard');
383 $total_debit += $line->debit;
384 $total_credit += $line->credit;
385 $opening_balance = isset($opening_balances[
"'".$line->numero_compte.
"'"]) ? $opening_balances[
"'".$line->numero_compte.
"'"] : 0;
386 $total_opening_balance += $opening_balance;
388 $tmparrayforrootaccount = $object->getRootAccount($line->numero_compte);
389 $root_account_description = $tmparrayforrootaccount[
'label'];
390 $root_account_number = $tmparrayforrootaccount[
'account_number'];
395 if (empty($accountingaccountstatic->label) && $accountingaccountstatic->id > 0) {
396 $link =
'<a class="editfielda reposition" href="' . DOL_URL_ROOT .
'/accountancy/admin/card.php?action=update&token=' .
newToken() .
'&id=' . $accountingaccountstatic->id .
'">' .
img_edit() .
'</a>';
397 } elseif ($accounting_account ==
'NotDefined') {
398 $link =
'<a href="' . DOL_URL_ROOT .
'/accountancy/admin/card.php?action=create&token=' .
newToken() .
'&accountingaccount=' .
length_accountg($line->numero_compte) .
'">' .
img_edit_add() .
'</a>';
399 } elseif (empty($tmparrayforrootaccount[
'label'])) {
407 if (!empty($show_subgroup)) {
409 if (empty($displayed_account) || $root_account_number != $displayed_account) {
411 if ($displayed_account !=
"") {
412 print
'<tr class="liste_total">';
413 print
'<td class="right">'.$langs->trans(
"SubTotal").
':</td>';
414 if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
415 print
'<td class="right nowraponall amount">'.price($sous_total_opening_balance).
'</td>';
417 print
'<td class="right nowraponall amount">'.price($sous_total_debit).
'</td>';
418 print
'<td class="right nowraponall amount">'.price($sous_total_credit).
'</td>';
419 if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
420 print
'<td class="right nowraponall amount">'.price(
price2num($sous_total_opening_balance + $sous_total_debit - $sous_total_credit)).
'</td>';
422 print
'<td class="right nowraponall amount">'.price(
price2num($sous_total_debit - $sous_total_credit)).
'</td>';
429 print
'<tr class="trforbreak">';
430 print
'<td colspan="'.($colspan+1).
'" class="tdforbreak">'.$line->numero_compte.($root_account_description ?
' - '.$root_account_description :
'').
'</td>';
433 $displayed_account = $root_account_number;
434 $sous_total_debit = 0;
435 $sous_total_credit = 0;
436 $sous_total_opening_balance = 0;
440 print
'<tr class="oddeven">';
441 print
'<td>'.$accounting_account.
'</td>';
442 if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
443 print
'<td class="right nowraponall amount">'.price(
price2num($opening_balance,
'MT')).
'</td>';
447 if ($line->numero_compte) {
448 $urlzoom = DOL_URL_ROOT.
'/accountancy/bookkeeping/listbyaccount.php?search_accountancy_code_start='.urlencode($line->numero_compte).
'&search_accountancy_code_end='.urlencode($line->numero_compte);
450 $urlzoom .=
'&search_date_startmonth='.GETPOST(
'date_startmonth',
'int').
'&search_date_startday='.
GETPOST(
'date_startday',
'int').
'&search_date_startyear='.
GETPOST(
'date_startyear',
'int');
453 $urlzoom .=
'&search_date_endmonth='.GETPOST(
'date_endmonth',
'int').
'&search_date_endday='.
GETPOST(
'date_endday',
'int').
'&search_date_endyear='.
GETPOST(
'date_endyear',
'int');
457 print
'<td class="right nowraponall amount"><a href="'.$urlzoom.
'">'.
price(
price2num($line->debit,
'MT')).
'</a></td>';
459 print
'<td class="right nowraponall amount"><a href="'.$urlzoom.
'">'.
price(
price2num($line->credit,
'MT')).
'</a></td>';
461 if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
462 print
'<td class="right nowraponall amount">'.price(
price2num($opening_balance + $line->debit - $line->credit,
'MT')).
'</td>';
464 print
'<td class="right nowraponall amount">'.price(
price2num($line->debit - $line->credit,
'MT')).
'</td>';
466 print
'<td class="center">';
472 $sous_total_debit += $line->debit;
473 $sous_total_credit += $line->credit;
474 $sous_total_opening_balance += $opening_balance;
477 if (!empty($show_subgroup)) {
478 print
'<tr class="liste_total"><td class="right">'.$langs->trans(
"SubTotal").
':</td>';
479 if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
480 print
'<td class="right nowraponall amount">'.price(
price2num($sous_total_opening_balance,
'MT')).
'</td>';
482 print
'<td class="right nowraponall amount">'.price(
price2num($sous_total_debit,
'MT')).
'</td>';
483 print
'<td class="right nowraponall amount">'.price(
price2num($sous_total_credit,
'MT')).
'</td>';
484 if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
485 print
'<td class="right nowraponall amount">' .
price(
price2num($sous_total_opening_balance + $sous_total_debit - $sous_total_credit,
'MT')) .
'</td>';
487 print
'<td class="right nowraponall amount">' .
price(
price2num($sous_total_debit - $sous_total_credit,
'MT')) .
'</td>';
493 print
'<tr class="liste_total"><td class="right">'.$langs->trans(
"AccountBalance").
':</td>';
494 if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
495 print
'<td class="nowrap right">'.price(
price2num($total_opening_balance,
'MT')).
'</td>';
497 print
'<td class="right nowraponall amount">'.price(
price2num($total_debit,
'MT')).
'</td>';
498 print
'<td class="right nowraponall amount">'.price(
price2num($total_credit,
'MT')).
'</td>';
499 if (!empty($conf->global->ACCOUNTANCY_SHOW_OPENING_BALANCE)) {
500 print
'<td class="right nowraponall amount">' .
price(
price2num($total_opening_balance + $total_debit - $total_credit,
'MT')) .
'</td>';
502 print
'<td class="right nowraponall amount">' .
price(
price2num($total_debit - $total_credit,
'MT')) .
'</td>';
507 $parameters = array(
'arrayfields'=>$arrayfields,
'sql'=>$sql);
508 $reshook = $hookmanager->executeHooks(
'printFieldListFooter', $parameters, $object, $action);
509 print $hookmanager->resPrint;