<?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;
	if($partyId > 0)
	{
		$k = 0;
		$clientDetail = array();
		$toDate   = $_REQUEST['toYear'].'-'.$_REQUEST['toMonth'].'-'.$_REQUEST['toDay'];
		$fromDate   = $_REQUEST['fromYear'].'-'.$_REQUEST['fromMonth'].'-'.$_REQUEST['fromDay'];
		
		$selectClient = "SELECT * FROM client
						 WHERE partyId = ".$partyId."
						 AND  cDate BETWEEN '".$toDate."'
						 AND '".$fromDate."'
						 AND  cstatus = 'A'
						 ORDER BY clientId ASC";
		$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']   	= round($clientRow['totalAmt'],3);
		  $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  cDate BETWEEN '".$toDate."'
						 AND '".$fromDate."'
					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  cDate BETWEEN '".$toDate."'
						 AND '".$fromDate."'
					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  cDate BETWEEN '".$toDate."'
						 AND '".$fromDate."'
					AND partyId = ".$partyId;
		$selectJavakAmountRes = mysql_query($selectJavakAmount);
		if($jrow = mysql_fetch_assoc($selectJavakAmountRes))
		{
		  $jKg          = $jrow['jKg'];
		  $jtotalAmt    = $jrow['jtotalAmt'];  
		}
		
		$selectOpening = "SELECT SUM(openingAmt) AS openingAmt,SUM(openingFine) AS openingFine
						   FROM client
					WHERE partyId = ".$partyId;
		$selectOpeningRes = mysql_query($selectOpening);
		if($oprow = mysql_fetch_assoc($selectOpeningRes))
		{
		  $opFine          = $oprow['openingFine'];
		  $openingAmt      = $oprow['openingAmt'];  
		}
		 
		
	     if($jKg < 0) 
		 {
		  $finalKg     = $jKg - $aKg + $opFine;
		 }
		 else
		 {
			$finalKg     = $aKg - $jKg + $opFine; 
		 }
   	    
		if($atotalAmt > 0)
		{
		  $finalAmount = $atotalAmt - $jtotalAmt + $openingAmt;
		}
		else
		{
		  $finalAmount = $jtotalAmt - $atotalAmt + $openingAmt;	
		}
	 
		 
		
		$slectOpening = "SELECT openingFine,openingAmt 
		                       FROM client
		                    WHERE partyId = ".$partyId."
							GROUP BY partyId";
		$slectOpeningRes = mysql_query($slectOpening);
		while($row = mysql_fetch_array($slectOpeningRes))
		{
	      $openingAmt     = $row['openingAmt'];
	      $openingFine    = $row['openingFine'];
		}
	
	include("./bottom.php");
	$smarty->assign("openingFine",$openingFine);
	$smarty->assign("fromDate",$fromDate);
	$smarty->assign("toDate",$toDate);
	$smarty->assign("openingAmt",$openingAmt);
	$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("clientList.tpl");
}
?>