Fix balance calculation

This commit is contained in:
Laurent Destailleur 2016-10-22 17:56:12 +02:00
parent 35601775e0
commit 8fa44e9363

View File

@ -99,12 +99,12 @@ if ($page == -1) { $page = 0; }
$offset = $limit * $page; $offset = $limit * $page;
$pageprev = $page - 1; $pageprev = $page - 1;
$pagenext = $page + 1; $pagenext = $page + 1;
if (! $sortorder) $sortorder='DESC'; if (! $sortorder) $sortorder='ASC';
if (! $sortfield) $sortfield='b.dateo'; if (! $sortfield) $sortfield='b.datev';
$mode_balance_ok=false; $mode_balance_ok=false;
//if (($sortfield == 'b.datev' || $sortfield == 'b.datev, b.dateo, b.rowid')) // TODO Manage balance when account not selected //if (($sortfield == 'b.datev' || $sortfield == 'b.datev, b.dateo, b.rowid')) // TODO Manage balance when account not selected
if (($sortfield == 'b.datev' || $sortfield == 'b.datev, b.dateo, b.rowid') && $account > 0) if (($sortfield == 'b.datev' || $sortfield == 'b.datev, b.dateo, b.rowid') && ($id > 0 || ! empty($ref)))
{ {
$sortfield = 'b.datev, b.dateo, b.rowid'; $sortfield = 'b.datev, b.dateo, b.rowid';
$mode_balance_ok = true; $mode_balance_ok = true;
@ -178,13 +178,15 @@ if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") || GETP
$type=""; $type="";
$debit=""; $debit="";
$credit=""; $credit="";
$account="";
$bid=""; $bid="";
$search_ref=""; $search_ref="";
$search_req_nb=''; $search_req_nb='';
$search_thirdparty=''; $search_thirdparty='';
$search_num_releve=''; $search_num_releve='';
$thirdparty=''; $thirdparty='';
$account="";
if ($id > 0 || ! empty($ref)) $account=$object->id;
} }
if (empty($reshook)) if (empty($reshook))
@ -413,13 +415,38 @@ $sql.=$hookmanager->resPrint;
$sql.= $db->order($sortfield,$sortorder); $sql.= $db->order($sortfield,$sortorder);
$nbtotalofrecords = 0; $nbtotalofrecords = 0;
if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST) || ! empty($arrayfields['balance']['checked']))
{ {
$result = $db->query($sql); $result = $db->query($sql);
$nbtotalofrecords = $db->num_rows($result); $nbtotalofrecords = $db->num_rows($result);
$nbtotalofpages = ceil($nbtotalofrecords/$limit); $nbtotalofpages = ceil($nbtotalofrecords/$limit);
} }
if (($id > 0 || ! empty($ref)) && GETPOST("page",'int') === '')
{
// We open a list of transaction of a dedicated account and no page was set by defaut
// We force on last page.
$page = ($nbtotalofpages - 1);
$offset = $limit * $page;
if ($page < 0) $page = 0;
}
// Calculate balance start
$balance = 0; // For balance
if (! empty($arrayfields['balance']['checked']))
{
//Loop on each record
$sign = 1;
$i = 0;
while ($i < $offset)
{
$objp = $db->fetch_object($result);
$balance = price2num($balance + ($sign * $objp->amount),'MT');
$i++;
}
}
$sql.= $db->plimit($limit+1,$offset); $sql.= $db->plimit($limit+1,$offset);
dol_syslog('compta/bank/bankentries.php', LOG_DEBUG); dol_syslog('compta/bank/bankentries.php', LOG_DEBUG);
@ -555,6 +582,10 @@ if ($resql)
print_barre_liste($langs->trans("BankTransactions"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, $picto, 0, $morehtml, '', $limit); print_barre_liste($langs->trans("BankTransactions"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, $picto, 0, $morehtml, '', $limit);
} }
// We can add page now to param
if ($page != '') $param.='&page='.urlencode($page);
$moreforfilter = ''; $moreforfilter = '';
$moreforfilter.='<div class="divsearchfield">'; $moreforfilter.='<div class="divsearchfield">';
@ -688,7 +719,6 @@ if ($resql)
print "</tr>\n"; print "</tr>\n";
// Loop on each record // Loop on each record
$total = 0; // For balance
$sign = 1; $sign = 1;
$totalarray=array(); $totalarray=array();
@ -696,7 +726,7 @@ if ($resql)
{ {
$objp = $db->fetch_object($resql); $objp = $db->fetch_object($resql);
$total = price2num($total + ($sign * $objp->amount),'MT'); $balance = price2num($balance + ($sign * $objp->amount),'MT');
if (empty($cachebankaccount[$objp->bankid])) if (empty($cachebankaccount[$objp->bankid]))
{ {
@ -957,13 +987,13 @@ if ($resql)
{ {
if ($mode_balance_ok) if ($mode_balance_ok)
{ {
if ($total >= 0) if ($balance >= 0)
{ {
print '<td align="right" class="nowrap">&nbsp;'.price($total).'</td>'; print '<td align="right" class="nowrap">&nbsp;'.price($balance).'</td>';
} }
else else
{ {
print '<td align="right" class="error nowrap">&nbsp;'.price($total).'</td>'; print '<td align="right" class="error nowrap">&nbsp;'.price($balance).'</td>';
} }
} }
else else