<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
    $partyId = isset($_REQUEST['partyId']) ? $_REQUEST['partyId'] : 0;
	$to        = $_REQUEST['toDtYear']."-".$_REQUEST['toDtMonth']."-".$_REQUEST['toDtDay'];  
	$from      = $_REQUEST['fromDtYear']."-".$_REQUEST['fromDtMonth']."-".$_REQUEST['fromDtDay'];  
	
    $k = 0;
    $clientDetail = array();
	if(isset($_REQUEST['submit']))
	{
	    $selectClient = "SELECT * FROM client
						 WHERE partyId = ".$partyId."
						AND cDate between '".$to."' AND '".$from."'";
		$selectClientRes = mysql_query($selectClient);
		while($clientRow = mysql_fetch_array($selectClientRes))
		{
		  $clientDetail[$k]['clientId']     = $clientRow['clientId'];
		  $clientDetail[$k]['particular']   = $clientRow['particular'];
		  $clientDetail[$k]['cliTransType'] = $clientRow['cliTransType'];
		  $clientDetail[$k]['cDate']        = date('d-m-Y',strtotime($clientRow['cDate']));
		  $clientDetail[$k]['rate']       	= $clientRow['rate'];
		  $clientDetail[$k]['diff']     	= $clientRow['diff'];
		  $clientDetail[$k]['diffAmt']    	= $clientRow['diffAmt'];
		  $clientDetail[$k]['total']      	= $clientRow['total'];
		  $clientDetail[$k]['kg']         	= $clientRow['kg'];
		  $clientDetail[$k]['totalAmt']   	= $clientRow['totalAmt'];
		  $k++;
		} 
    }
	$z=0;
	$clientArray = array();
	$selectClient = "SELECT partyId,partyName
	                   FROM party
					WHERE partyType = 'C'
				   ORDER BY partyName";
	$selectClientRes = mysql_query($selectClient);
	while($clientRow = mysql_fetch_array($selectClientRes))
	{
	  $clientArray['partyId'][$z]   = $clientRow['partyId'];
	  $clientArray['partyName'][$z] = $clientRow['partyName'];
	  $z++;
	}
	
	$selectAmount = "SELECT SUM(kg) AS jKg,SUM(totalAmt) AS jtotalAmt
	                   FROM client
					  WHERE cliTransType = 'J'
				AND partyId = ".$partyId;
	$selectAmountRes = mysql_query($selectAmount);
	if($jrow = mysql_fetch_assoc($selectAmountRes))
	{
	  $jKg          = $jrow['jKg'];
	  $jtotalAmt    = $jrow['jtotalAmt'];
	  
	}
	
	$selectAavkAmount = "SELECT SUM(kg) AS aKg,SUM(totalAmt) AS atotalAmt
	                   FROM client
					  WHERE cliTransType = 'A'
				AND partyId = ".$partyId;
	$selectAavkAmountRes = mysql_query($selectAavkAmount);
	if($arow = mysql_fetch_assoc($selectAavkAmountRes))
	{
	  $aKg          = $arow['aKg'];
	  $atotalAmt    = $arow['atotalAmt'];  
	}
	
	$selectJavakAmount = "SELECT SUM(kg) AS jKg,SUM(totalAmt) AS jtotalAmt
	                   FROM client
					  WHERE cliTransType = 'J'
				AND partyId = ".$partyId;
	$selectJavakAmountRes = mysql_query($selectJavakAmount);
	if($jrow = mysql_fetch_assoc($selectJavakAmountRes))
	{
	  $jKg          = $jrow['jKg'];
	  $jtotalAmt    = $jrow['jtotalAmt'];  
	}
	
	if($aKg > 0)
	{
	  $finalKg     = $aKg - $jKg;
	  
	}
	else
	{
	  $finalKg     = $jKg - $aKg;
	}
	
	$finalAmount = $atotalAmt - $jtotalAmt;
	
	
	include("./bottom.php");
	$smarty->assign("crArray",$crArray);
	$smarty->assign("finalAmount",$finalAmount);
	$smarty->assign("finalKg",$finalKg);
	$smarty->assign("partyId",$partyId);
	$smarty->assign("clientDetail",$clientDetail);
	$smarty->assign("clientArray",$clientArray);
	$smarty->display("clientListAll.tpl");
}
?>