<?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'];  
	
	if(isset($_REQUEST['submit']))
	{
		$k = 0;
		$clientDetail = array();
		$selectClient = "SELECT * FROM vepari
						 WHERE partyId = ".$partyId."
						 AND vDate BETWEEN '".$to."' AND '".$from."'";
		$selectClientRes = mysql_query($selectClient);
		while($clientRow = mysql_fetch_array($selectClientRes))
		{ 
		  $clientDetail[$k]['vepariId']     = $clientRow['vepariId'];
		  $clientDetail[$k]['vRate']        = $clientRow['vRate'];
		  $clientDetail[$k]['vParti']       = $clientRow['vParti'];
		  $clientDetail[$k]['veTransType']  = $clientRow['veTransType'];
		  $clientDetail[$k]['vDate']        = date('d-m-Y',strtotime($clientRow['vDate']));
		  $clientDetail[$k]['vDiffAmt']     = $clientRow['vDiffAmt'];
		  $clientDetail[$k]['vTotal']       = $clientRow['vTotal'];
		  $clientDetail[$k]['diffAmt']      = $clientRow['diffAmt'];
		  $clientDetail[$k]['vTaxPer']      = $clientRow['vTaxPer'];
		  $clientDetail[$k]['vTax']         = $clientRow['vTax'];
		  $clientDetail[$k]['vkg']   	    = $clientRow['vkg'];
		  $clientDetail[$k]['vTotalAmt']    = $clientRow['vTotalAmt']; 
		  $k++;
		}
	}
	$z=0;
	$clientArray = array();
	$selectClient = "SELECT partyId,partyName
	                   FROM party
					WHERE partyType = 'V'";
	$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 veTransType = 'A'
				AND partyId = ".$partyId;
	$selectAavkAmountRes = mysql_query($selectAavkAmount);
	if($arow = mysql_fetch_assoc($selectAavkAmountRes))
	{
	  $aKg          = $arow['aKg'];
	  $atotalAmt    = $arow['atotalAmt'];  
	}
	*/
    $selectClientAavk = "SELECT SUM(vkg) AS aKg,SUM(vTotalAmt) AS aTotalAmt
	                      FROM vepari
					   WHERE veTransType = 'A'
				    AND partyId = ".$partyId;
	$selectClientAavkRes = mysql_query($selectClientAavk);
	if($cavakRow = mysql_fetch_assoc($selectClientAavkRes))
	{
      $aKg          = $cavakRow['aKg'];
	  $aTotalAmt    = $cavakRow['aTotalAmt'];  
	}
    
	$selectClientJavak = "SELECT SUM(vkg) AS javakKg,SUM(vTotalAmt) AS javaktotalAmt
	                   FROM vepari
					  WHERE veTransType = 'J'
				AND partyId = ".$partyId;
	$selectClientJavakRes = mysql_query($selectClientJavak);
	if($cjavakRow = mysql_fetch_assoc($selectClientJavakRes))
	{
      $javakKg          = $cjavakRow['javakKg'];
	  $javaktotalAmt    = $cjavakRow['javaktotalAmt'];  
	}
	
	
	if($javakKg > 0)
	{
	  $finalKg     = $javakKg  - $aKg;
	}
	else
	{
	  $finalKg     = $aKg - $javakKg;
	}
	$finalAmount = $javaktotalAmt - $aTotalAmt;
	
	
	include("./bottom.php");
	$smarty->assign("crArray",$crArray);
	$smarty->assign("rojmelArray",$rojmelArray);
	$smarty->assign("finalAmount",$finalAmount);
	$smarty->assign("finalKg",$finalKg);
	$smarty->assign("partyId",$partyId);
	$smarty->assign("clientDetail",$clientDetail);
	$smarty->assign("clientArray",$clientArray);
	$smarty->display("vepariListAll.tpl");
}
?>