<?php
include("include/config.inc.php");
include("include/conn.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	$newdate   = date('Y-m-d', strtotime(' +1 day'));
	$todayDate = date('Y-m-d');
	$partyId   = isset($_REQUEST['partyId']) && $_REQUEST['partyId'] != '' ? $_REQUEST['partyId'] : 0;	 

    if($partyId > 0)
	{
	
		$selectClientAavk = "SELECT SUM(kg) AS aKg,SUM(totalAmt) AS aTotalAmt
							  FROM client
						   WHERE cliTransType = 'A'
						AND partyId = ".$partyId;
		$selectClientAavkRes = mysql_query($selectClientAavk);
		if($cavakRow = mysql_fetch_assoc($selectClientAavkRes))
		{
		  $aKg          = $cavakRow['aKg'];
		  $aTotalAmt    = $cavakRow['aTotalAmt'];  
		}
    
		$selectClientJavak = "SELECT SUM(kg) AS javakKg,SUM(totalAmt) AS javaktotalAmt
						   FROM client
						  WHERE cliTransType = 'J'
					AND partyId = ".$partyId;
		$selectClientJavakRes = mysql_query($selectClientJavak);
		if($cjavakRow = mysql_fetch_assoc($selectClientJavakRes))
		{
		  $javakKg          = $cjavakRow['javakKg'];
		  $javaktotalAmt    = $cjavakRow['javaktotalAmt'];  
		}
	     
		
     	$finalKg = $aKg - $javakKg;
		
		///////////////////fine : Start/////////////////////////
		if($finalKg < 0) 
		{
		echo  $insert = "INSERT INTO client(partyId,cDate,cliTransType,particular,kg,cstatus)
						VALUES(".$partyId.",'".$newdate."','A','New Opening',".$finalKg.",'A')";
		  $insertRes = mysql_query($insert);
		  if(!$insertRes)
		  {
		    echo "Client Opening Insert Fail"; 
		  }
		  else
		  {
			//header("Location:clientList.php");
		  }
		}
		else
		{
		  $insert = "INSERT INTO client(partyId,cDate,cliTransType,particular,kg,cstatus)
						VALUES(".$partyId.",'".$newdate."','J','New Opening',".$finalKg.",'A')";
		  $insertRes = mysql_query($insert);
		  if(!$insertRes)
		  {
		    echo "Client Opening Insert Fail"; 
		  }
		  else
		  {
			// header("Location:clientList.php");
		  }	
		}
		////////////////////fine : End/////////////////////////
	    
		///////////////Amt : Start/////////////////////////
		$finalAmount = $aTotalAmt - $javaktotalAmt;
		 
		
		if($finalAmount > 0)
		{
		  $insert = "INSERT INTO client(partyId,cDate,cliTransType,particular,totalAmt,cstatus)
						VALUES(".$partyId.",'".$newdate."','J','New Opening',".$finalAmount.",'A')";
		  $insertRes = mysql_query($insert);
		  if(!$insertRes)
		  {
		    echo "Vepari Opening Insert Fail"; 
		  }
		  else
		  {
			 $deletevepari = "DELETE FROM client	
								WHERE partyId = ".$partyId."
								AND cDate <= '".$todayDate."'";
			 $deletevepariRes = mysql_query($deletevepari);
			 if(!$deletevepariRes)
			 {
			   echo "Client Delete Fail";
			 }
			 else
			 {
			    //header("Location:clientList.php");
			 }
		  }	
		}
		else
		{
		  $insert = "INSERT INTO client(partyId,cDate,cliTransType,particular,totalAmt,cstatus)
						VALUES(".$partyId.",'".$newdate."','A','New Opening',".$finalAmount.",'A')";
		  $insertRes = mysql_query($insert);
		  if(!$insertRes)
		  {
		    echo "Client Opening Insert Fail"; 
		  }
		  else
		  {
			$deletevepari = "DELETE FROM client	
						  WHERE partyId = ".$partyId."
						AND  cDate <= '".$todayDate."'";
			 $deletevepariRes = mysql_query($deletevepari);
			 if(!$deletevepariRes)
			 {
			   echo "Client Delete Fail";
			 }
			 else
			 {
			   //  header("Location:clientList.php");
			 }
		  }	
		}
		///////////////Amt : End/////////////////////////

		
		
	 
 		  
    }
	include("./bottom.php");
	$smarty->assign("partyId",$partyId);
	$smarty->assign("finalAmount",$finalAmount);
	$smarty->assign("finalKg",$finalKg);
	$smarty->assign("clientArray",$clientArray);
	$smarty->display("clientList.tpl");
}
?>