<?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($aKg > 0) 
		{
		  $insertAavk = "INSERT INTO client(partyId,cDate,cliTransType,particular,kg,cstatus,is_opening)
						VALUES(".$partyId.",'".$newdate."','A','New Opening',".$aKg.",'A','1')";
		  $insertAavkRes = mysql_query($insertAavk);
		  if(!$insertAavkRes)
		  {
		    echo "Client Opening Insert Fail"; 
		  }
		  else
		  {
			$insertJavak = "INSERT INTO client(partyId,cDate,cliTransType,particular,kg,cstatus,is_opening)
						VALUES(".$partyId.",'".$newdate."','J','New Opening',".$javakKg.",'A','1')";
		    $insertJavakRes = mysql_query($insertJavak);
			if($insertJavakRes)
			{
		       $insertRsAavk = "INSERT INTO client(partyId,cDate,cliTransType,particular,totalAmt,cstatus,is_opening)
						VALUES(".$partyId.",'".$newdate."','A','New Rs Opening',".$aTotalAmt.",'A','1')";
		       $insertRsAavkRes = mysql_query($insertRsAavk);
		       if(!$insertRsAavkRes)
		       {
		         echo "Rs Insert Opening Insert Fail"; 
		       }
			   else
		       {
				$insertRsJavak = "INSERT INTO client(partyId,cDate,cliTransType,particular,totalAmt,cstatus,is_opening)
								VALUES(".$partyId.",'".$newdate."','J','New Rs Opening',".$javaktotalAmt.",'A','1')";
				$insertRsJavakRes = mysql_query($insertRsJavak); 
				if(!$insertRsJavakRes)
				{
				  echo "Rs 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");
				   }	
				}
			
			 }
	        }
			 
		  }
		}
	}
		 
	include("./bottom.php");
	$smarty->assign("partyId",$partyId);
	$smarty->assign("finalAmount",$finalAmount);
	$smarty->assign("finalKg",$finalKg);
	$smarty->assign("clientArray",$clientArray);
	$smarty->display("clientList.tpl");
}
?>