<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
    $date = date('Y-m-d');
	$todayfrom = date('Y-m-d');
	$todayto = date('Y-m-d');
    $partyId = isset($_REQUEST['partyId']) ? $_REQUEST['partyId'] : 0;
	$to        = $_REQUEST['toDtYear']."-".$_REQUEST['toDtMonth']."-".$_REQUEST['toDtDay'];  
	$from      = $_REQUEST['fromDtYear']."-".$_REQUEST['fromDtMonth']."-".$_REQUEST['fromDtDay']; 
    $a = 0;
    $rojmelArray = array();
	
	if(isset($_REQUEST['submit']))
	{
		 ///////////////////////////////// branch 0 : start ////////////////////////////////////////
		$selectRojmel = "SELECT *,partyName FROM sauda
						 LEFT  JOIN party ON party.partyId = sauda.partyId
						  WHERE rojTransType = 'J'
						  AND saudaDate between '".$to."' AND '".$from."'
						   AND branch = 0";
		$selectRojmelRes = mysql_query($selectRojmel);
		while($rojmelRow = mysql_fetch_array($selectRojmelRes))
		{
		  $rojmelArray[$a]['saudaId']        = $rojmelRow['saudaId'];
		  $rojmelArray[$a]['saudaDate']      = date('d-m-Y',strtotime($rojmelRow['saudaDate']));
		  $rojmelArray[$a]['rojTransType']   = $rojmelRow['rojTransType'];
		  $rojmelArray[$a]['partyName']      = $rojmelRow['partyName'];
		  $rojmelArray[$a]['particular']     = $rojmelRow['particular'];
		  $rojmelArray[$a]['fine']           = $rojmelRow['fine'];
		  $rojmelArray[$a]['amount']         = $rojmelRow['amount'];
		  $rojmelArray[$a]['acc_type']       = $rojmelRow['acc_type'];
		  $rojmelArray[$a]['rate']   	     = $rojmelRow['rate'];
		  $rojmelArray[$a]['cash_type']      = $rojmelRow['cash_type'];
		  $rojmelArray[$a]['branch']    	 = $rojmelRow['branch'];
		  $rojmelArray[$a]['mcxcheckStatus'] = $rojmelRow['mcxcheckStatus'];
		  $a++;
		}
		
		$k = 0;
		$javakArray = array();
		$selectJavak = "SELECT *,partyName FROM sauda
						LEFT JOIN party ON party.partyId = sauda.partyId
						  WHERE rojTransType = 'A'
						   AND saudaDate between '".$to."' AND '".$from."'
						    AND branch = 0";
		$selectJavakRes = mysql_query($selectJavak);
		while($javakRow = mysql_fetch_array($selectJavakRes))
		{
		  $javakArray[$k]['saudaId']         = $javakRow['saudaId'];
		  $javakArray[$k]['saudaDate']       = date('d-m-Y',strtotime($javakRow['saudaDate']));
		  $javakArray[$k]['rojTransType']    = $javakRow['rojTransType'];
		  $javakArray[$k]['partyName']       = $javakRow['partyName'];
		  $javakArray[$k]['particular']      = $javakRow['particular'];
		  $javakArray[$k]['fine']            = $javakRow['fine'];
		  $javakArray[$k]['amount']          = $javakRow['amount'];
		  $javakArray[$k]['acc_type']        = $javakRow['acc_type'];
		  $javakArray[$k]['cash_type']       = $javakRow['cash_type'];
		  $javakArray[$k]['rate']            = $javakRow['rate'];
		  $javakArray[$k]['mcxcheckStatus']  = $javakRow['mcxcheckStatus'];
		  $javakArray[$k]['branch'] 		 = $javakRow['branch'];
		  $k++;
		}	
		
		///////////////////////////////// branch 0 : End ////////////////////////////////////////
		
		
		///////////////////////////////// branch 1 : start ////////////////////////////////////////
		$a=0;
		$rojmelArray1 = array();
		$selectRojmel = "SELECT *,partyName FROM sauda
						 LEFT  JOIN party ON party.partyId = sauda.partyId
						  WHERE rojTransType = 'J'
						  AND saudaDate between '".$to."' AND '".$from."'
						   AND branch = 1";
		$selectRojmelRes = mysql_query($selectRojmel);
		while($rojmelRow = mysql_fetch_array($selectRojmelRes))
		{
		  $rojmelArray1[$a]['saudaId']        = $rojmelRow['saudaId'];
		  $rojmelArray1[$a]['saudaDate']      = date('d-m-Y',strtotime($rojmelRow['saudaDate']));
		  $rojmelArray1[$a]['rojTransType']   = $rojmelRow['rojTransType'];
		  $rojmelArray1[$a]['partyName']      = $rojmelRow['partyName'];
		  $rojmelArray1[$a]['particular']     = $rojmelRow['particular'];
		  $rojmelArray1[$a]['fine']           = $rojmelRow['fine'];
		  $rojmelArray1[$a]['amount']         = $rojmelRow['amount'];
		  $rojmelArray1[$a]['acc_type']       = $rojmelRow['acc_type'];
		  $rojmelArray1[$a]['rate']   	     = $rojmelRow['rate'];
		  $rojmelArray1[$a]['cash_type']      = $rojmelRow['cash_type'];
		  $rojmelArray1[$a]['branch']    	 = $rojmelRow['branch'];
		  $rojmelArray1[$a]['mcxcheckStatus'] = $rojmelRow['mcxcheckStatus'];
		  $a++;
		}
		
		$k = 0;
		$javakArray1 = array();
		$selectJavak = "SELECT *,partyName FROM sauda
						LEFT JOIN party ON party.partyId = sauda.partyId
						  WHERE rojTransType = 'A'
						   AND saudaDate between '".$to."' AND '".$from."'
						    AND branch = 1";
		$selectJavakRes = mysql_query($selectJavak);
		while($javakRow = mysql_fetch_array($selectJavakRes))
		{
		  $javakArray1[$k]['saudaId']         = $javakRow['saudaId'];
		  $javakArray1[$k]['saudaDate']       = date('d-m-Y',strtotime($javakRow['saudaDate']));
		  $javakArray1[$k]['rojTransType']    = $javakRow['rojTransType'];
		  $javakArray1[$k]['partyName']       = $javakRow['partyName'];
		  $javakArray1[$k]['particular']      = $javakRow['particular'];
		  $javakArray1[$k]['fine']            = $javakRow['fine'];
		  $javakArray1[$k]['amount']          = $javakRow['amount'];
		  $javakArray1[$k]['acc_type']        = $javakRow['acc_type'];
		  $javakArray1[$k]['cash_type']       = $javakRow['cash_type'];
		  $javakArray1[$k]['rate']            = $javakRow['rate'];
		  $javakArray1[$k]['mcxcheckStatus']  = $javakRow['mcxcheckStatus'];
		  $javakArray1[$k]['branch'] 		  = $javakRow['branch'];
		  $k++;
		}	
		
		///////////////////////////////// branch 1 : End ////////////////////////////////////////
		
		///////////////////////////////////////Rtgs : Start ////////////////////////
		$selectRojmel = "SELECT SUM(fine) AS afine,SUM(amount) AS aamount FROM sauda
							WHERE rojTransType = 'A'
							AND branch = 0
							AND  saudaDate between '".$to."' AND '".$from."'";
		$selectRojmelRes = mysql_query($selectRojmel);
		if($row = mysql_fetch_array($selectRojmelRes))
		{
		  $aFine  =  $row['afine'];
		  $aAmount = $row['aamount'];
		}
		
		$selectRojmel = "SELECT SUM(fine) AS jfine,SUM(amount) AS jamount FROM sauda
							WHERE rojTransType = 'J'
							AND branch = 0
							AND  saudaDate between '".$to."' AND '".$from."'";
		$selectRojmelRes = mysql_query($selectRojmel);
		if($row = mysql_fetch_array($selectRojmelRes))
		{
		  $jFine   = $row['jfine'];
		  $jAmount = $row['jamount'];
		}
		
		$rtgsfine = $jFine - $aFine;
		$rtgsAmt = $aAmount - $jAmount;
		
		///////////////////////////////////////Rtgs : End ////////////////////////
		
		
		
		//////////////////////////////////////Cash : Start ////////////////////////
		$selectRojmelCash = "SELECT SUM(fine) AS cAavakFine,SUM(amount) AS cAavkamount FROM sauda
							WHERE rojTransType = 'A'
							AND branch = 1
						AND  saudaDate between '".$to."' AND '".$from."'";
		$selectRojmelCashRes = mysql_query($selectRojmelCash);
		if($rowCash = mysql_fetch_array($selectRojmelCashRes))
		{
		  $cAavakFine  =  $rowCash['cAavakFine'];
		  $cAavkamount = $rowCash['cAavkamount'];
		}
		
		$selectRojmelJavak = "SELECT SUM(fine) AS cJavakFine,SUM(amount) AS cJavkamount FROM sauda
							WHERE rojTransType = 'J'
							AND branch = 1
						AND  saudaDate between '".$to."' AND '".$from."'";
		$selectRojmelJavakRes = mysql_query($selectRojmelJavak);
		if($row = mysql_fetch_array($selectRojmelJavakRes))
		{
		  $cJavakFine   = $row['cJavakFine'];
		  $cJavkamount  = $row['cJavkamount'];
		}
		
		//////////////////////////////////////Cash : End ////////////////////////
		
		$Cashfine =  $cJavakFine - $cAavakFine;
		$CashAmt  = $cAavkamount - $cJavkamount;
		
		$netFine   = $Cashfine + $rtgsfine;
		$netAmount = $rtgsAmt +  $CashAmt;
		
		
 
		
		
	}
	else
	{
		///////////////////////////////// branch 0 : start ////////////////////////////////////////
	    $selectRojmel = "SELECT *,partyName FROM sauda
						 LEFT  JOIN party ON party.partyId = sauda.partyId
						  WHERE rojTransType = 'J'
						  AND saudaDate = '".$date."'
						   AND branch = 0";
		$selectRojmelRes = mysql_query($selectRojmel);
		while($rojmelRow = mysql_fetch_array($selectRojmelRes))
		{
		  $rojmelArray[$a]['saudaId']     		= $rojmelRow['saudaId'];
		  $rojmelArray[$a]['saudaDate']   		= date('d-m-Y',strtotime($rojmelRow['saudaDate']));
		  $rojmelArray[$a]['rojTransType'] 		= $rojmelRow['rojTransType'];
		  $rojmelArray[$a]['partyName']    		= $rojmelRow['partyName'];
		  $rojmelArray[$a]['particular']   		= $rojmelRow['particular'];
		  $rojmelArray[$a]['fine']         		= $rojmelRow['fine'];
		  $rojmelArray[$a]['amount']       		= $rojmelRow['amount'];
		  $rojmelArray[$a]['acc_type']     		= $rojmelRow['acc_type'];
		  $rojmelArray[$a]['rate']   	  		= $rojmelRow['rate'];
		  $rojmelArray[$a]['branch']   	  		= $rojmelRow['branch'];
		  $rojmelArray[$a]['cash_type']    		= $rojmelRow['cash_type'];
		  $rojmelArray[$a]['mcxcheckStatus']    = $rojmelRow['mcxcheckStatus'];
		  $a++;
		}
		
		$k = 0;
		$javakArray = array();
		$selectJavak = "SELECT *,partyName FROM sauda
						LEFT JOIN party ON party.partyId = sauda.partyId
						  WHERE rojTransType = 'A'
						   AND saudaDate = '".$date."'
						    AND branch = 0";
		$selectJavakRes = mysql_query($selectJavak);
		while($javakRow = mysql_fetch_array($selectJavakRes))
		{
		  $javakArray[$k]['saudaId']     	= $javakRow['saudaId'];
		  $javakArray[$k]['saudaDate']   	= date('d-m-Y',strtotime($javakRow['saudaDate']));
		  $javakArray[$k]['rojTransType'] 	= $javakRow['rojTransType'];
		  $javakArray[$k]['partyName']    	= $javakRow['partyName'];
		  $javakArray[$k]['particular']   	= $javakRow['particular'];
		  $javakArray[$k]['fine']         	= $javakRow['fine'];
		  $javakArray[$k]['amount']       	= $javakRow['amount'];
		  $javakArray[$k]['acc_type']     	= $javakRow['acc_type'];
		  $javakArray[$k]['rate']   	  	= $javakRow['rate'];
		  $javakArray[$k]['branch']   	  	= $javakRow['branch'];
		  $javakArray[$k]['cash_type']   	= $javakRow['cash_type'];
		  $javakArray[$k]['mcxcheckStatus'] = $javakRow['mcxcheckStatus'];
		  $k++;
		}	
		
		///////////////////////////////// branch 0 : end ////////////////////////////////////////
		
		///////////////////////////////// branch 1 : start ////////////////////////////////////////
		$rojmelArray1 = array();
		$a = 0;
	    $selectRojmel = "SELECT *,partyName FROM sauda
						 LEFT  JOIN party ON party.partyId = sauda.partyId
						  WHERE rojTransType = 'J'
						  AND saudaDate = '".$date."'
						   AND branch = 1";
		$selectRojmelRes = mysql_query($selectRojmel);
		while($rojmelRow = mysql_fetch_array($selectRojmelRes))
		{
		  $rojmelArray1[$a]['saudaId']     		= $rojmelRow['saudaId'];
		  $rojmelArray1[$a]['saudaDate']   		= date('d-m-Y',strtotime($rojmelRow['saudaDate']));
		  $rojmelArray1[$a]['rojTransType'] 		= $rojmelRow['rojTransType'];
		  $rojmelArray1[$a]['partyName']    		= $rojmelRow['partyName'];
		  $rojmelArray1[$a]['particular']   		= $rojmelRow['particular'];
		  $rojmelArray1[$a]['fine']         		= $rojmelRow['fine'];
		  $rojmelArray1[$a]['amount']       		= $rojmelRow['amount'];
		  $rojmelArray1[$a]['acc_type']     		= $rojmelRow['acc_type'];
		  $rojmelArray1[$a]['rate']   	  		= $rojmelRow['rate'];
		  $rojmelArray1[$a]['branch']   	  		= $rojmelRow['branch'];
		  $rojmelArray1[$a]['cash_type']    		= $rojmelRow['cash_type'];
		  $rojmelArray1[$a]['mcxcheckStatus']    = $rojmelRow['mcxcheckStatus'];
		  $a++;
		}
		
		$k = 0;
		$javakArray1 = array();
		$selectJavak = "SELECT *,partyName FROM sauda
						LEFT JOIN party ON party.partyId = sauda.partyId
						  WHERE rojTransType = 'A'
						   AND saudaDate = '".$date."'
						    AND branch = 1";
		$selectJavakRes = mysql_query($selectJavak);
		while($javakRow = mysql_fetch_array($selectJavakRes))
		{
		  $javakArray1[$k]['saudaId']     	= $javakRow['saudaId'];
		  $javakArray1[$k]['saudaDate']   	= date('d-m-Y',strtotime($javakRow['saudaDate']));
		  $javakArray1[$k]['rojTransType'] 	= $javakRow['rojTransType'];
		  $javakArray1[$k]['partyName']    	= $javakRow['partyName'];
		  $javakArray1[$k]['particular']   	= $javakRow['particular'];
		  $javakArray1[$k]['fine']         	= $javakRow['fine'];
		  $javakArray1[$k]['amount']       	= $javakRow['amount'];
		  $javakArray1[$k]['acc_type']     	= $javakRow['acc_type'];
		  $javakArray1[$k]['rate']   	  	= $javakRow['rate'];
		  $javakArray1[$k]['branch']   	  	= $javakRow['branch'];
		  $javakArray1[$k]['cash_type']   	= $javakRow['cash_type'];
		  $javakArray1[$k]['mcxcheckStatus'] = $javakRow['mcxcheckStatus'];
		  $k++;
		}	
		
		///////////////////////////////// branch 1 : end ////////////////////////////////////////
		
	    ///////////////////////////////////////Rtgs : Start ////////////////////////
		$selectRojmel = "SELECT SUM(fine) AS afine,SUM(amount) AS aamount FROM sauda
							WHERE rojTransType = 'A'
							AND branch = 0
							AND saudaDate = '".$todayfrom."'";
		$selectRojmelRes = mysql_query($selectRojmel);
		if($row = mysql_fetch_array($selectRojmelRes))
		{
		  $aFine  =  $row['afine'];
		  $aAmount = $row['aamount'];
		}
		
		$selectRojmel = "SELECT SUM(fine) AS jfine,SUM(amount) AS jamount FROM sauda
							WHERE rojTransType = 'J'
							AND branch = 0
							AND saudaDate = '".$todayfrom."'";
		$selectRojmelRes = mysql_query($selectRojmel);
		if($row = mysql_fetch_array($selectRojmelRes))
		{
		  $jFine   = $row['jfine'];
		  $jAmount = $row['jamount'];
		}
		
		$rtgsfine = $jFine - $aFine;
		$rtgsAmt = $aAmount - $jAmount;
		
		///////////////////////////////////////Rtgs : End ////////////////////////
		
		
		
		//////////////////////////////////////Cash : Start ////////////////////////
		$selectRojmelCash = "SELECT SUM(fine) AS cAavakFine,SUM(amount) AS cAavkamount FROM sauda
							WHERE rojTransType = 'A'
							AND branch = 1
						AND saudaDate = '".$todayfrom."'";
		$selectRojmelCashRes = mysql_query($selectRojmelCash);
		if($rowCash = mysql_fetch_array($selectRojmelCashRes))
		{
		  $cAavakFine  =  $rowCash['cAavakFine'];
		  $cAavkamount = $rowCash['cAavkamount'];
		}
		
		$selectRojmelJavak = "SELECT SUM(fine) AS cJavakFine,SUM(amount) AS cJavkamount FROM sauda
							WHERE rojTransType = 'J'
							AND branch = 1
							AND saudaDate = '".$todayfrom."'";
		$selectRojmelJavakRes = mysql_query($selectRojmelJavak);
		if($row = mysql_fetch_array($selectRojmelJavakRes))
		{
		  $cJavakFine   = $row['cJavakFine'];
		  $cJavkamount  = $row['cJavkamount'];
		}
		
		//////////////////////////////////////Cash : End ////////////////////////
		
		$Cashfine =  $cJavakFine - $cAavakFine;
		$CashAmt  = $cAavkamount - $cJavkamount;
		
		$netFine   = $Cashfine + $rtgsfine;
		$netAmount = $rtgsAmt +  $CashAmt;
	}
	
	
	$z=0;
	$clientArray = array();
	$selectClient = "SELECT partyId,partyName
	                   FROM party";
	$selectClientRes = mysql_query($selectClient);
	while($rojmelRow = mysql_fetch_array($selectClientRes))
	{
	  $clientArray['partyId'][$k]   = $rojmelRow['partyId'];
	  $clientArray['partyName'][$k] = $rojmelRow['partyName'];
	  $z++;
	}
	
	
	
	include("./bottom.php");
	$smarty->assign("to",$to);
	$smarty->assign("from",$from);
	$smarty->assign("todayAmt",$todayAmt);
	$smarty->assign("todayFine",$todayFine);
	$smarty->assign("TodayCashAmt",$TodayCashAmt);
	$smarty->assign("TodayCashfine",$TodayCashfine);
	$smarty->assign("trtgsfine",$trtgsfine);
	$smarty->assign("trtgsAmt",$trtgsAmt);
	$smarty->assign("CashAmt",$CashAmt);
	$smarty->assign("Cashfine",$Cashfine);
	$smarty->assign("rtgsfine",$rtgsfine);
	$smarty->assign("rtgsAmt",$rtgsAmt);
	$smarty->assign("aAmount",$aAmount);
	$smarty->assign("aFine",$aFine);
	$smarty->assign("jFine",$jFine);
	$smarty->assign("jAmount",$jAmount);
	$smarty->assign("javakArray",$javakArray);
	$smarty->assign("partyId",$partyId);
	$smarty->assign("netFine",$netFine);
	$smarty->assign("netAmount",$netAmount);
	$smarty->assign("rojmelArray",$rojmelArray);
	$smarty->assign("clientArray",$clientArray);
	$smarty->assign("javakArray1",$javakArray1);
	$smarty->assign("rojmelArray1",$rojmelArray1);
	$smarty->display("saudaList.tpl");
}
?>