<?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 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(kg) AS javakKg,SUM(totalAmt) 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'];  
		}
	     
		
     	$finalKg     = $javakKg  - $aKg;
		
		///////////////////fine : Start/////////////////////////
		if($finalKg > 0) 
		{
		  $insert = "INSERT INTO vepari(partyId,vDate,veTransType,particular,kg,vstatus)
						VALUES(".$partyId.",'".$newdate."','A','New Opening',".$finalKg.",'A')";
		  $insertRes = mysql_query($insert);
		  if(!$insertRes)
		  {
		    echo "Vepari Opening Insert Fail"; 
		  }
		  else
		  {
			header("Location:vepariList.php");
		  }
		}
		else
		{
		  $insert = "INSERT INTO vepari(partyId,vDate,veTransType,particular,kg,vstatus)
						VALUES(".$partyId.",'".$newdate."','J','New Opening',".$finalKg.",'A')";
		  $insertRes = mysql_query($insert);
		  if(!$insertRes)
		  {
		    echo "Vepari Opening Insert Fail"; 
		  }
		  else
		  {
			 header("Location:vepariList.php");
		  }	
		}
		////////////////////fine : End/////////////////////////
	    
		///////////////Amt : Start/////////////////////////
		$finalAmount = $javaktotalAmt - $aTotalAmt;
		 
		
		if($finalAmount > 0)
		{
		  $insert = "INSERT INTO vepari(partyId,vDate,veTransType,particular,totalAmt,vstatus)
						VALUES(".$partyId.",'".$newdate."','J','New Opening',".$finalAmount.",'A')";
		  $insertRes = mysql_query($insert);
		  if(!$insertRes)
		  {
		    echo "Vepari Opening Insert Fail"; 
		  }
		  else
		  {
			 $deletevepari = "DELETE FROM vepari	
								WHERE partyId = ".$partyId."
								AND vDate <= '".$todayDate."'";
			 $deletevepariRes = mysql_query($deletevepari);
			 if(!$deletevepariRes)
			 {
			   echo "Vepari Delete Fail";
			 }
			 else
			 {
			    header("Location:vepariList.php");
			 }
		  }	
		}
		else
		{
		  $insert = "INSERT INTO vepari(partyId,vDate,veTransType,particular,totalAmt,vstatus)
						VALUES(".$partyId.",'".$newdate."','A','New Opening',".$finalAmount.",'A')";
		  $insertRes = mysql_query($insert);
		  if(!$insertRes)
		  {
		    echo "Vepari Opening Insert Fail"; 
		  }
		  else
		  {
			$deletevepari = "DELETE FROM vepari	
						  WHERE partyId = ".$partyId."
						AND  vDate <= '".$todayDate."'";
			 $deletevepariRes = mysql_query($deletevepari);
			 if(!$deletevepariRes)
			 {
			   echo "Vepari Delete Fail";
			 }
			 else
			 {
			    header("Location:vepariList.php");
			 }
		  }	
		}
		///////////////Amt : End/////////////////////////

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