<? 
include_once("modules/EcmPaymentStates/helper.php");
?>
<table cellspacing="0" cellpadding="0" border="0"><tr><td></td><td><h2>Rozrachunki: Salda</h2></td></tr></table>
<br />
<input type="button" class="button" value="Utwórz XLS" name="create_xls" onclick="location.href='index.php?module=EcmPaymentStates&action=CreateXLSsummary&to_pdf=1';" />
<?php
$w=$GLOBALS['db']->query("select distinct account_id from ecmpayments_ecminvoiceouts where account_id!='' and account_id is not null and deleted='0'");
while($r=$GLOBALS['db']->fetchByAssoc($w))$acc[$r['account_id']]="id='".$r['account_id']."'"; 

$tds1='<td class="listViewThS1">';

$trs='<tr>';
$trs_grey='<tr style="background-color: #e6e6e6;">';
$tre='</tr>';

$tds='<td class="oddListRowS1">';
$tde='</td>';

$tbs='<table cellpadding="0" cellspacing="0" border="0" class="ListView" style="width:100%;">';
$tbe='</table>';

$t.=$tbs;
$t.=$trs;
	$t.=$tds1;
		$t.="Kontrahent";
	$t.=$tde;
	$t.=$tds1;
		$t.="Saldo";
	$t.=$tde;
	$t.=$tds1;
		$t.="Po terminie";
	$t.=$tde;
	
	$t.=$tds1;
		$t.="1-30";
	$t.=$tde;
	$t.=$tds1;
		$t.="30-60";
	$t.=$tde;
	$t.=$tds1;
		$t.="60-90";
	$t.=$tde;
	$t.=$tds1;
		$t.="90-180";
	$t.=$tde;
	$t.=$tds1;
		$t.="180-...";
	$t.=$tde;
$t.=$tre;

$z="select * from accounts where deleted='0' and (".implode(" or ",$acc).") order by name asc";
$w=$GLOBALS['db']->query($z);


while($r=$GLOBALS['db']->fetchByAssoc($w)){
	if ($i % 2 == 1)
		$t.=$trs;
	else
		$t.=$trs_grey;
	
	$l_summary=getSummary($r['id']);
	$l_ballance=getBallance($r['id']);
	$l_actual_sum=getActualSum($r['id']);
	$l_sum_1_30=getSum($r['id'],1,30);
	$l_sum_30_60=getSum($r['id'],30,60);
	$l_sum_60_90=getSum($r['id'],60,90);
	$l_sum_90_180=getSum($r['id'],90,180);
	$l_sum_180=getSum($r['id'],180);
	if(round($l_summary+$l_actual_sum+$l_sum_1_30+$l_sum_30_60+$l_sum_60_90+$l_sum_90_180+$l_sum_180,2)==0.00)continue; 
	
	$summary+=$l_summary;
	$ballance+=$l_ballance;
	$actual_sum+=$l_actual_sum;
	$sum_1_30+=$l_sum_1_30;
	$sum_30_60+=$l_sum_30_60;
	$sum_60_90+=$l_sum_60_90;
	$sum_90_180+=$l_sum_90_180;
	$sum_180+=$l_sum_180;
	
	//$t.=$trs;
		$t.=$tds;
			$t.='<a href="index.php?action=index&module=EcmPaymentStates&nott_matched=0&account_name='.$r['name'].'&account_id='.$r['id'].'&button=Search">'.str_replace("<","&lt;",str_replace(">","&gt;",$r['name'])).'</a>';
		$t.=$tde;
		$t.=$tds;
			$t.=number_format($l_ballance,2,",",".");
		$t.=$tde;
		$t.=$tds;
			$t.=number_format($l_summary,2,",",".");
		$t.=$tde;
	
		$t.=$tds;
			$t.='<a href="index.php?module=EcmPaymentStates&action=summary_inv&account_id='.$r['id'].'&start=1&end=30">'.number_format($l_sum_1_30,2,",",".").'</a>';
		$t.=$tde;
		$t.=$tds;
			$t.='<a href="index.php?module=EcmPaymentStates&action=summary_inv&account_id='.$r['id'].'&start=30&end=60">'.number_format($l_sum_30_60,2,",",".").'</a>';
		$t.=$tde;
		$t.=$tds;
			$t.='<a href="index.php?module=EcmPaymentStates&action=summary_inv&account_id='.$r['id'].'&start=60&end=90">'.number_format($l_sum_60_90,2,",",".").'</a>';
		$t.=$tde;
		$t.=$tds;
			$t.='<a href="index.php?module=EcmPaymentStates&action=summary_inv&account_id='.$r['id'].'&start=90&end=180">'.number_format($l_sum_90_180,2,",",".").'</a>';
		$t.=$tde;
		$t.=$tds;
			$t.='<a href="index.php?module=EcmPaymentStates&action=summary_inv&account_id='.$r['id'].'&start=180&end=...">'.number_format($l_sum_180,2,",",".").'</a>';
		$t.=$tde;
	$t.=$tre;
	
	$i++;
}


	$t.=$trs;
		$t.=$tds;
		$t.=$tde;
		$t.=$tds;
			$t.='<strong>'.number_format($ballance,2,",",".").'</strong>';
		$t.=$tde;
		$t.=$tds;
			$t.='<strong>'.number_format($summary,2,",",".").'</strong>';
		$t.=$tde;
		$t.=$tds;
			$t.='<strong>'.number_format($sum_1_30,2,",",".").'</strong>';
		$t.=$tde;
		$t.=$tds;
			$t.='<strong>'.number_format($sum_30_60,2,",",".").'</strong>';
		$t.=$tde;
		$t.=$tds;
			$t.='<strong>'.number_format($sum_60_90,2,",",".").'</strong>';
		$t.=$tde;
		$t.=$tds;
			$t.='<strong>'.number_format($sum_90_180,2,",",".").'</strong>';
		$t.=$tde;
		$t.=$tds;
			$t.='<strong>'.number_format($sum_180,2,",",".").'</strong>';
		$t.=$tde;
	$t.=$tre;

$t.=$tbe;
echo $t;
?>