<?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;


	///////////////////////////////// branch 0 : Start ////////////////////////////////////////
	
    $a = 0;
    $rojmelArray = array();

	$selectRojmel = "SELECT *,partyName FROM sauda
					 LEFT  JOIN party ON party.partyId = sauda.partyId
					  WHERE rojTransType = 'J'
					  AND mcxcheckStatus = 0
					  AND branch = 0
					  ORDER BY partyName,saudaDate ASC";
	$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]['mcxcheckStatus']    = $rojmelRow['mcxcheckStatus'];
	  $rojmelArray[$a]['branch']    		= $rojmelRow['branch'];
	  $a++;
	}
	
	$k = 0;
	$javakArray = array();
	$selectJavak = "SELECT *,partyName FROM sauda
					LEFT JOIN party ON party.partyId = sauda.partyId
					 WHERE rojTransType = 'A'
					  AND mcxcheckStatus = 0
					  AND branch = 0
					  ORDER BY partyName,saudaDate ASC";
	$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]['cash_type']   	= $javakRow['cash_type'];
	  $javakArray[$k]['mcxcheckStatus'] = $javakRow['mcxcheckStatus'];
	  $javakArray[$k]['branch']   		= $javakRow['branch'];
	  $k++;
	}
	///////////////////////////////// branch 0 : End ////////////////////////////////////////	 
	
		///////////////////////////////// branch 1 : Start ////////////////////////////////////////
	
    $aaa = 0;
    $rojmelArray1 = array();

	$selectRojmel1 = "SELECT *,partyName FROM sauda
					 LEFT  JOIN party ON party.partyId = sauda.partyId
					  WHERE rojTransType = 'J'
					  AND mcxcheckStatus = 0
					  AND branch = 1
					   ORDER BY partyName,saudaDate ASC";
	$selectRojmelRes1 = mysql_query($selectRojmel1);
	while($rojmelRow1 = mysql_fetch_array($selectRojmelRes1))
	{
	  $rojmelArray1[$aaa]['saudaId']     		= $rojmelRow1['saudaId'];
	  $rojmelArray1[$aaa]['saudaDate']   		= date('d-m-Y',strtotime($rojmelRow1['saudaDate']));
	  $rojmelArray1[$aaa]['rojTransType'] 		= $rojmelRow1['rojTransType'];
	  $rojmelArray1[$aaa]['partyName']    		= $rojmelRow1['partyName'];
	  $rojmelArray1[$aaa]['particular']   		= $rojmelRow1['particular'];
	  $rojmelArray1[$aaa]['fine']         		= $rojmelRow1['fine'];
	  $rojmelArray1[$aaa]['amount']       		= $rojmelRow1['amount'];
	  $rojmelArray1[$aaa]['acc_type']     		= $rojmelRow1['acc_type'];
	  $rojmelArray1[$aaa]['rate']   	  		= $rojmelRow1['rate'];
	  $rojmelArray1[$aaa]['cash_type']    		= $rojmelRow1['cash_type'];
	  $rojmelArray1[$aaa]['mcxcheckStatus']    	= $rojmelRow1['mcxcheckStatus'];
	  $rojmelArray1[$aaa]['branch']    			= $rojmelRow1['branch'];
	  $aaa++;
	}
	
	$kk = 0;
	$javakArray1 = array();
	$selectJavak1 = "SELECT *,partyName FROM sauda
					LEFT JOIN party ON party.partyId = sauda.partyId
					 WHERE rojTransType = 'A'
					  AND mcxcheckStatus = 0
					  AND branch = 1
					   ORDER BY partyName,saudaDate ASC";
	$selectJavakRes1 = mysql_query($selectJavak1);
	while($javakRow1 = mysql_fetch_array($selectJavakRes1))
	{
	  $javakArray1[$kk]['saudaId']     		= $javakRow1['saudaId'];
	  $javakArray1[$kk]['saudaDate']   		= date('d-m-Y',strtotime($javakRow1['saudaDate']));
	  $javakArray1[$kk]['rojTransType'] 	= $javakRow1['rojTransType'];
	  $javakArray1[$kk]['partyName']    	= $javakRow1['partyName'];
	  $javakArray1[$kk]['particular']   	= $javakRow1['particular'];
	  $javakArray1[$kk]['fine']         	= $javakRow1['fine'];
	  $javakArray1[$kk]['amount']       	= $javakRow1['amount'];
	  $javakArray1[$kk]['acc_type']     	= $javakRow1['acc_type'];
	  $javakArray1[$kk]['rate']   	  		= $javakRow1['rate'];
	  $javakArray1[$kk]['cash_type']   		= $javakRow1['cash_type'];
	  $javakArray1[$kk]['mcxcheckStatus'] 	= $javakRow1['mcxcheckStatus'];
	  $javakArray1[$kk]['branch']   		= $javakRow1['branch'];
	  $kk++;
	}
	///////////////////////////////// branch 1 : End ////////////////////////////////////////	 
		 
		 
		//////////////////////////////////////Branch 0 : Start ////////////////////////
		$selectRojmel = "SELECT SUM(fine) AS afine,SUM(amount) AS aamount 
							FROM sauda
							WHERE rojTransType = 'A'
							AND branch = 0
							AND mcxcheckStatus = 0";
		$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 mcxcheckStatus = 0";
		$selectRojmelRes = mysql_query($selectRojmel);
		if($row = mysql_fetch_array($selectRojmelRes))
		{
		  $jFine   = $row['jfine'];
		  $jAmount = $row['jamount'];
		}
		
		$rtgsfine = $jFine - $aFine ;
		$rtgsAmt = $aAmount - $jAmount;
		
		///////////////////////////////////////Branch 0 : End //////////////////////// 
		
		
		//////////////////////////////////////Branch 1 : Start ////////////////////////
		$selectRojmelCash = "SELECT SUM(fine) AS cAavakFine,SUM(amount) AS cAavkamount FROM sauda
							WHERE rojTransType = 'A'
							AND branch = 1
							AND mcxcheckStatus = 0";
		$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 mcxcheckStatus = 0";
		$selectRojmelJavakRes = mysql_query($selectRojmelJavak);
		if($row = mysql_fetch_array($selectRojmelJavakRes))
		{
		  $cJavakFine   = $row['cJavakFine'];
		  $cJavkamount  = $row['cJavkamount'];
		}
		
		//////////////////////////////////////Branch 1 : 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("rojmelArray1",$rojmelArray1);
	$smarty->assign("javakArray1",$javakArray1);
	$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->display("pendingsaudaList.tpl");
}
?>