<?php

$ss = new Sugar_Smarty ();
global $mod_strings;

$db = $GLOBALS ['db'];

$tabela = array ();

# saldo z rozrachunków
$query = "
SELECT 
    SUM(CASE
        WHEN type = 1 THEN value
        ELSE - value
    END) as saldo,
    parent_name,
	parent_id
FROM
    ecmtransactions
WHERE
    deleted = 0
        AND register_date > '2011-12-31'
GROUP BY parent_id
ORDER by parent_name ASC";

$res = $db->query ( $query );

while ( $dane = $db->fetchByAssoc ( $res ) ) {
	if ($dane ['parent_id'] == "")
		continue;
	$a = new Account();
	$a->retrieve($dane ['parent_id']);
	$tabela [$dane ['parent_id']] ['parent_name'] = $a->name;
	$tabela [$dane ['parent_id']] ['parent_id'] = $dane ['parent_id'];
	$tabela [$dane ['parent_id']] ['saldo'] = $dane ['saldo'];
	unset($a);
}



# saldo z rozrachunków nierozliczonych
$query = "
SELECT
    SUM(CASE
        WHEN type = 1 THEN value
        ELSE - value
    END) as saldo,
    parent_name,
	parent_id
FROM
    ecmtransactions
WHERE
    deleted = 0
        AND register_date > '2011-12-31' and settled!='1'
GROUP BY parent_id
ORDER by parent_name ASC";

$res = $db->query ( $query );

while ( $dane = $db->fetchByAssoc ( $res ) ) {
	if ($dane ['parent_id'] == "")
		continue;
		$a = new Account();
		$a->retrieve($dane ['parent_id']);
		$tabela [$dane ['parent_id']] ['parent_name'] = $a->name;
		$tabela [$dane ['parent_id']] ['parent_id'] = $dane ['parent_id'];
		$tabela [$dane ['parent_id']] ['saldo_nierozliczone'] = $dane ['saldo'];
		unset($a);
}

# transakcje rozliczone winien
$query = "
SELECT
	sum(t.value) as saldo,
	t.parent_name,
	t.parent_id
FROM
    ecmtransactions t
WHERE
    t.deleted = 0
        AND t.register_date > '2011-12-31'
		AND t.type=0 and t.settled=1
group by t.parent_id";

$res = $db->query ( $query );

while ( $dane = $db->fetchByAssoc ( $res ) ) {
	if ($dane ['parent_id'] == "")
		continue;

		$tabela [$dane ['parent_id']] ['saldo_roz_a'] = $dane ['saldo'];
}

# transakcje rozliczone ma
$query = "
SELECT
	sum(t.value) as saldo,
	t.parent_name,
	t.parent_id
FROM
    ecmtransactions t
WHERE
    t.deleted = 0
        AND t.register_date > '2011-12-31'
		AND t.type=1 and t.settled=1
group by t.parent_id";

$res = $db->query ( $query );

while ( $dane = $db->fetchByAssoc ( $res ) ) {
	if ($dane ['parent_id'] == "")
		continue;

		if($tabela [$dane ['parent_id']] ['saldo_roz_a']!=""){
			$tabela [$dane ['parent_id']] ['saldo_roz'] = bcsub($tabela [$dane ['parent_id']] ['saldo_roz_a'],$dane ['saldo'],2);
			unset($tabela [$dane ['parent_id']] ['saldo_roz_a']);
		} else {
			$tabela [$dane ['parent_id']] ['saldo_roz'] =$dane ['saldo'];
		}
		
		
}
// ukryj zerowe
foreach ($tabela as $key=>$val){
	if($tabela[$key]['saldo']==""){
		$tabela[$key]['saldo']=0;
	}
	if($tabela[$key]['saldo_roz']==""){
		$tabela[$key]['saldo_roz']=0;
	}
	if($tabela[$key]['saldo_nierozliczone']==""){
		$tabela[$key]['saldo_nierozliczone']=0;
	}
	if(round($tabela[$key]['saldo'],2)==0 && round($tabela[$key]['saldo_roz'],2)==0 && round($tabela[$key]['saldo_nierozliczone'],2)==0){
		unset($tabela[$key]);
	}
}
$ss->assign ( 'tabela', $tabela );

echo $ss->display ( 'modules/EcmPaymentStates/tpls/CompareSummary.tpl' );

?>