<?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
{
	$today     = date('Y-m-d', strtotime(' +1 day'));
	$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($_REQUEST['btn']))
	{
	
	/////////////////////////////////Aavak : Satart : ///////////////////////
	$selectClientAavk = "SELECT SUM(fine) AS aKg,SUM(amount) AS aTotalAmt
	                   FROM rojmel
					  WHERE rojTransType = 'A'
				AND rojmelDate between '".$to."' 
				AND '".$from."'";
	$selectClientAavkRes = mysql_query($selectClientAavk) or die('Error:'.mysql_error());
	if($cavakRow = mysql_fetch_array($selectClientAavkRes))
	{
      $aKg          = $cavakRow['aKg'];
	  $aTotalAmt    = $cavakRow['aTotalAmt'];  
	}
	
	
	$insertAavak = "INSERT INTO rojmel(particular,rojmelDate,rojTransType,fine,amount)
						VALUES('Aavak Opening','0000-00-00','A','".$aKg."','".$aTotalAmt."')";
	$insertAavakRes = mysql_query($insertAavak);
	if($insertAavakRes)
	{
	   $deleteAavak = "DELETE FROM rojmel
					WHERE rojmelDate between '".$to."' 
				AND '".$from."'
				AND rojTransType = 'A'";
	   $deleteAavakRes = mysql_query($deleteAavak);
	   if(!$deleteAavakRes)
	   {
	      echo "Aavak Delete FAil"; 
	   }
	}
	else
	{
	  echo "Aavak INSERT FAIL";	
	}
	/////////////////////////////////Aavak : End : ///////////////////////
	
	
	/////////////////////////////////javak : Satart : ///////////////////////
    
	 $selectClientJavak = "SELECT SUM(fine) AS javakKg,SUM(amount) AS javaktotalAmt
	                   FROM rojmel
					  WHERE rojTransType = 'J'
				AND rojmelDate between '".$to."' 
				AND '".$from."'";
	$selectClientJavakRes = mysql_query($selectClientJavak);
	if($cjavakRow = mysql_fetch_array($selectClientJavakRes))
	{
      $javakKg          = $cjavakRow['javakKg'];
	  $javaktotalAmt    = $cjavakRow['javaktotalAmt'];  
	}
	
	$insertJavak = "INSERT INTO rojmel(particular,rojmelDate,rojTransType,fine,amount)
						VALUES('Javak Opening','0000-00-00','J','".$javakKg."','".$javaktotalAmt."')";
	$insertRes = mysql_query($insertJavak);
	if($insertRes)
	{
	   $delete = "DELETE FROM rojmel
					WHERE rojmelDate between '".$to."' 
				AND '".$from."'
				AND rojTransType = 'J'";
	   $deleteRes = mysql_query($delete);
	   if(!$deleteRes)
	   {
	      echo "Javak Delete FAil"; 
	   }
	}
	else
	{
	  echo "Javak INSERT FAIL";	
	}
	/////////////////////////////////Javak : End : ///////////////////////
  }
	include("./bottom.php");
	$smarty->assign("partyId",$partyId);
	$smarty->assign("finalAmount",$finalAmount);
	$smarty->assign("finalKg",$finalKg);
	$smarty->assign("clientArray",$clientArray);
	$smarty->display("roDelete.tpl");
}
?>