<?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;
	
    $k = 0;
    $clientDetail = array();
    $selectClient = "SELECT * FROM client
	                 WHERE partyId = ".$partyId;
	$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'];
	  
	  if($clientRow['cliTransType'] == 'J')
	  {
	  
		  $selectJavak = "SELECT DATE_FORMAT(rojmelDate,'%d-%m-%Y') AS javkDate,fine AS javkFine,amount AS javakAmt 
							FROM rojmel
					   WHERE rojTransType = 'J'
					  AND partyId = ".$partyId;
		  $selectJavakRes = mysql_query($selectJavak);
		  while($javakRow = mysql_fetch_array($selectJavakRes))
		  {
			$clientDetail[$k]['javkDate'] = $javakRow['javkDate'];                 
			$clientDetail[$k]['javkFine'] = $javakRow['javkFine'];                 
			$clientDetail[$k]['javakAmt'] = $javakRow['javakAmt'];                 
			$k++;
		  } 
	  }
	  else 
	  {
		
		  $selectAavk = "SELECT DATE_FORMAT(rojmelDate,'%d-%m-%Y') AS avakDate,fine AS avakFine,amount AS avakAmt 
							FROM rojmel
					   WHERE rojTransType = 'A'
					  AND partyId = ".$partyId;
		  $sselectAavkRes = mysql_query($selectAavk);
		  while($aavakRow = mysql_fetch_array($sselectAavkRes))
		  {
			$clientDetail[$k]['avakDate'] = $aavakRow['avakDate'];                 
			$clientDetail[$k]['avakFine'] = $aavakRow['avakFine'];                 
			$clientDetail[$k]['avakAmt']  = $aavakRow['avakAmt'];                 
			$k++;
		  } 
	   }
	} 
 
	$z=0;
	$clientArray = array();
	$selectClient = "SELECT partyId,partyName
	                   FROM party
					WHERE partyType = 'C'";
	$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'];  
	}
	
	$selectClientAavk = "SELECT SUM(fine) AS cavakKg,SUM(amount) AS cavaktotalAmt
	                   FROM rojmel
					  WHERE rojTransType = 'A'
				AND partyId = ".$partyId;
	$selectClientAavkRes = mysql_query($selectClientAavk);
	if($cavakRow = mysql_fetch_assoc($selectClientAavkRes))
	{
      $cavakg            = $cavakRow['cavakKg'];
	  $cavakgtotalAmt    = $cavakRow['cavaktotalAmt'];  
	}
    
	$selectClientJavak = "SELECT SUM(fine) AS cjavakKg,SUM(amount) AS cjavaktotalAmt
	                   FROM rojmel
					  WHERE rojTransType = 'J'
				AND partyId = ".$partyId;
	$selectClientJavakRes = mysql_query($selectClientJavak);
	if($cjavakRow = mysql_fetch_assoc($selectClientJavakRes))
	{
      $cjavakKg          = $cjavakRow['cjavakKg'];
	  $cjavaktotalAmt    = $cjavakRow['cjavaktotalAmt'];  
	}
	
	
	$finalKg     = $cavakg         - $cjavakKg;
	$finalAmount = $cavakgtotalAmt - $cjavaktotalAmt;
	
	
	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("clientList.tpl");
}
?>