<?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'] != '' ? $_REQUEST['partyId'] : 0;	 
	$to        = $_REQUEST['toDtYear']."-".$_REQUEST['toDtMonth']."-".$_REQUEST['toDtDay'];  
	$from      = $_REQUEST['fromDtYear']."-".$_REQUEST['fromDtMonth']."-".$_REQUEST['fromDtDay'];  

    if(isset($_POST['btn']))
	{
	  $deleteEntry = "DELETE FROM client
                        	 WHERE cDate BETWEEN   
						  '".$to."' AND '".$from."'
						  AND partyId = ".$partyId; 
	  $insertClientRes = mysql_query($deleteEntry);
	  if(!$insertClientRes)
	  {
	  	echo "Delete Fail";
	  }
	  else
	  {
	     header("Location:delete1.php");
	  }
	  
	}
	
	$k=0;
	$clientArray = array();
	$selectClient = "SELECT partyId,partyName
	                   FROM party
					  WHERE partyType = 'C'";
	$selectClientRes = mysql_query($selectClient);
	while($clientRow = mysql_fetch_array($selectClientRes))
	{
	  $clientArray['partyId'][$k]   = $clientRow['partyId'];
	  $clientArray['partyName'][$k] = $clientRow['partyName'];
	  $k++;
	}
	
	$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'];  
	}
	
	$finalKg     = $aKg         - $jKg;
	$finalAmount = $atotalAmt - $jtotalAmt;
	
	include("./bottom.php");
	$smarty->assign("finalKg",$finalKg);
	$smarty->assign("finalAmount",$finalAmount);
	$smarty->assign("partyId",$partyId);
	$smarty->assign("clientArray",$clientArray);
	$smarty->display("delete1.tpl");
}
?>