<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	$pendingArray = array();
	$finetotal = 0;
	$fineamount = 0;
	$motiAmount = 0;
	$rhodium = 0;
	$grosstotal = 0;
	$selectParty = "SELECT * FROM party 
	                   WHERE partyType = 'C'
					  ORDER BY partyName";
	$selectPartyRes = mysql_query($selectParty);
	while($row = mysql_fetch_array($selectPartyRes))
	{
	  $pendingArray[$row['partyId']]['partyId']   = $row['partyId'];
	  $pendingArray[$row['partyId']]['partyName'] = $row['partyName'];
	  $pendingArray[$row['partyId']]['kg'] = 0;
	  $pendingArray[$row['partyId']]['totalAmt'] = 0;
	  $pendingArray[$row['partyId']]['openingAmt']     = 0;
	  $pendingArray[$row['partyId']]['openingFine']   = 0;
	  
	   $slectIssue = "SELECT SUM(kg) AS ckg , SUM(totalAmt) AS ctotalAmt 
	                   FROM client
	                WHERE  cliTransType = 'J'
					AND partyId = ".$row['partyId']."
					GROUP BY clientId";
	  $slectIssueRes = mysql_query($slectIssue);
	  while($irow = mysql_fetch_array($slectIssueRes))
	  {
	    $pendingArray[$row['partyId']]['kg']             -= $irow['ckg'];
	    $pendingArray[$row['partyId']]['totalAmt']       -= $irow['ctotalAmt'];
	  }

	  $slectIssue = "SELECT SUM(kg) AS jkg , SUM(totalAmt) AS jtotalAmt 
	                     FROM client
					WHERE  cliTransType = 'A'
					AND partyId = ".$row['partyId']."
					GROUP BY clientId";
	  $slectIssueResi = mysql_query($slectIssue);
	  while($irow = mysql_fetch_array($slectIssueResi))
	  {
	    $pendingArray[$row['partyId']]['kg']           += $irow['jkg'];
		$pendingArray[$row['partyId']]['totalAmt']     += $irow['jtotalAmt'];
	  }
	  
	 
	  
	  $kg        += $pendingArray[$row['partyId']]['kg'];
	  $totalAmt  += $pendingArray[$row['partyId']]['totalAmt'];
	  
	  
	}
}
include("./bottom.php");
$smarty->assign("totalAmt",$totalAmt);
$smarty->assign("kg",$kg);
$smarty->assign("pendingArray",$pendingArray);
$smarty->display("summary.tpl");
?>