<?php


# STATISZTIKÁK

if ($this->is_id()) {

	# ADOTT STAT MEGJELENÍTÉSE
	# nem lehet rá univerzális megoldást adni, mindegyiknél más kell majd
	# todo: stat_group-ok és stat_value-k

	if (1 == $this->get_id()) {
		/* adatok:
			bevétel kp + utalás
			kiadás kp + utalás
			összebevétel
			összkiadás
			totál balance
		*/
		$in_cash_query = "SELECT sum(mi_sum) FROM money_income WHERE mi_deleted = 0 AND mi_payment_type_pt_id = 1";
		$in_transfer_query = "SELECT sum(mi_sum) FROM money_income WHERE mi_deleted = 0 AND mi_payment_type_pt_id = 2";

		$cash_previous_query = "SELECT setv_int FROM setting_value JOIN setting on set_id = setv_setting_set_id WHERE set_name = 'Áthozat (kp)';";

		$out_cash_query = "SELECT sum(mox_sum) FROM money_expense WHERE mox_deleted = 0 AND mox_payment_type_pt_id = 1";
		$out_transfer_query = "SELECT sum(mox_sum) FROM money_expense WHERE mox_deleted = 0 AND mox_payment_type_pt_id = 2";

		$transfer_previous_query = "SELECT setv_int FROM setting_value JOIN setting on set_id = setv_setting_set_id WHERE set_name = 'Áthozat (utalás)';";

		$in_cash = $sql->single_variable($in_cash_query);
		$out_cash = $sql->single_variable($out_cash_query);

		$in_transfer = $sql->single_variable($in_transfer_query);
		$out_transfer = $sql->single_variable($out_transfer_query);

		$cash_previous = $sql->single_variable($cash_previous_query);
		$transfer_previous = $sql->single_variable($transfer_previous_query);

		$balance_query = "
			select
			    (select
			            sum(mi_sum)
			        from
			            money_income where mi_deleted = 0) - (select
			            sum(mox_sum)
			        from
			            money_expense where mox_deleted = 0) as diff
			from dual;
		";

		$balance = $sql->single_variable($balance_query);

		$smarty->assign('cash_previous',$cash_previous);
		$smarty->assign('transfer_previous',$transfer_previous);
		$smarty->assign('in_cash',$in_cash);
		$smarty->assign('out_cash',$out_cash);
		$smarty->assign('in_transfer',$in_transfer);
		$smarty->assign('out_transfer',$out_transfer);
		$smarty->assign('balance',$balance);
		$smarty->display('stat_balance.tpl');
	}

}

else {

	# STAT LISTA

	$stat_query = "SELECT * FROM statistics WHERE stat_deleted = 0 ORDER BY stat_order ASC;";
	$stat_assoc_array = $sql->assoc_array($stat_query);

	$stats = array();
	foreach ($stat_assoc_array as $stat) {
		$new_stat = new stat();
		$new_stat->set_stat_data_by_id($stat['stat_id']);
		$stats[] = $new_stat;
	}

	$smarty->assign('stats',$stats);
	$smarty->display('stat_list.tpl');

}





?>
