<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	$to        = $_REQUEST['toDtYear']."-".$_REQUEST['toDtMonth']."-".$_REQUEST['toDtDay'];  
	$from      = $_REQUEST['fromDtYear']."-".$_REQUEST['fromDtMonth']."-".$_REQUEST['fromDtDay'];
    $date = date('Y-m-d');
	$todayfrom = date('Y-m-d');
	$todayto = date('Y-m-d');
    $partyId = isset($_REQUEST['partyId']) ? $_REQUEST['partyId'] : 0;
    $a = 0;
    $rojmelArray = array();
	if(isset($_REQUEST['submit']))
	{
		$selectRojmel = "SELECT *,partyName FROM rojmel
						 LEFT  JOIN party ON party.partyId = rojmel.partyId
						  WHERE rojTransType = 'A'
						  AND rojmelDate BETWEEN '".$to."' AND '".$from."'";
						 
		$selectRojmelRes = mysql_query($selectRojmel);
		while($rojmelRow = mysql_fetch_array($selectRojmelRes))
		{
		  $rojmelArray[$a]['rojmelId']     = $rojmelRow['rojmelId'];
		  $rojmelArray[$a]['rojmelDate']   = date('d-m-Y',strtotime($rojmelRow['rojmelDate']));
		  $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'];
		  $a++;
		}
	
		$k = 0;
		$javakArray = array();
		$selectJavak = "SELECT *,partyName FROM rojmel
						LEFT JOIN party ON party.partyId = rojmel.partyId
						  WHERE rojTransType = 'J'
						   AND rojmelDate BETWEEN '".$to."' AND '".$from."'";
		$selectJavakRes = mysql_query($selectJavak);
		while($javakRow = mysql_fetch_array($selectJavakRes))
		{
		  $javakArray[$k]['rojmelId']     = $javakRow['rojmelId'];
		  $javakArray[$k]['rojmelDate']   = date('d-m-Y',strtotime($javakRow['rojmelDate']));
		  $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'];
		  $k++;
		}	
	}
	else
	{
	    $selectRojmel = "SELECT *,partyName FROM rojmel
						 LEFT  JOIN party ON party.partyId = rojmel.partyId
						  WHERE rojTransType = 'A'
						  AND rojmelDate = '".$date."'";
						 
		$selectRojmelRes = mysql_query($selectRojmel);
		while($rojmelRow = mysql_fetch_array($selectRojmelRes))
		{
		  $rojmelArray[$a]['rojmelId']     = $rojmelRow['rojmelId'];
		  $rojmelArray[$a]['rojmelDate']   = date('d-m-Y',strtotime($rojmelRow['rojmelDate']));
		  $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'];
		  $a++;
		}
	
		$k = 0;
		$javakArray = array();
		$selectJavak = "SELECT *,partyName FROM rojmel
						LEFT JOIN party ON party.partyId = rojmel.partyId
						  WHERE rojTransType = 'J'
						   AND rojmelDate = '".$date."'";
		$selectJavakRes = mysql_query($selectJavak);
		while($javakRow = mysql_fetch_array($selectJavakRes))
		{
		  $javakArray[$k]['rojmelId']     = $javakRow['rojmelId'];
		  $javakArray[$k]['rojmelDate']   = date('d-m-Y',strtotime($javakRow['rojmelDate']));
		  $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'];
		  $k++;
		}	
	}
		$selectRojmel = "SELECT SUM(fine) AS afine,SUM(amount) AS aamount FROM rojmel
							WHERE rojTransType = 'A'";
		$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 rojmel
							WHERE rojTransType = 'J'
							 ";
		$selectRojmelRes = mysql_query($selectRojmel);
		if($row = mysql_fetch_array($selectRojmelRes))
		{
		  $jFine   = $row['jfine'];
		  $jAmount = $row['jamount'];
		}
	
		$netFine   = $aFine - $jFine;
		$netAmount = $aAmount - $jAmount;
	
	
	
		$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("javakArray",$javakArray);
		$smarty->assign("partyId",$partyId);
		$smarty->assign("netFine",$netFine);
		$smarty->assign("netAmount",$netAmount);
		$smarty->assign("rojmelArray",$rojmelArray);
		$smarty->assign("clientArray",$clientArray);
		$smarty->display("rojmelList.tpl");
}
?>