<?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']))
	{
		$selectRojmel = "SELECT *,partyName FROM parchuran
						 LEFT  JOIN party ON party.partyId = parchuran.partyId
						  WHERE purchType = 'A'
						  AND purchDate between '".$to."' AND '".$from."'";
		$selectRojmelRes = mysql_query($selectRojmel);
		while($rojmelRow = mysql_fetch_array($selectRojmelRes))
		{
		  $rojmelArray[$a]['purchId']      = $rojmelRow['purchId'];
		  $rojmelArray[$a]['purchDate']    = date('d-m-Y',strtotime($rojmelRow['purchDate']));
		  $rojmelArray[$a]['purchType']    = $rojmelRow['purchType'];
		  $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 parchuran
						LEFT JOIN party ON party.partyId = parchuran.partyId
						  WHERE purchType = 'J'
						   AND purchDate between '".$to."' AND '".$from."'";
		$selectJavakRes = mysql_query($selectJavak);
		while($javakRow = mysql_fetch_array($selectJavakRes))
		{
		  $javakArray[$k]['purchId']     = $javakRow['purchId'];
		  $javakArray[$k]['purchDate']   = date('d-m-Y',strtotime($javakRow['purchDate']));
		  $javakArray[$k]['purchType'] = $javakRow['purchType'];
		  $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 parchuran
							WHERE purchType = 'A'
							AND  purchDate 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 parchuran
							WHERE purchType = 'J'
							AND  purchDate between '".$to."' AND '".$from."'";
		$selectRojmelRes = mysql_query($selectRojmel);
		if($row = mysql_fetch_array($selectRojmelRes))
		{
		  $jFine   = $row['jfine'];
		  $jAmount = $row['jamount'];
		}
		
		$netFine   = $aFine - $jFine;
		$netAmount = $aAmount - $jAmount;
	}
	else
	{
	    $selectRojmel = "SELECT *,partyName FROM parchuran
						 LEFT  JOIN party ON party.partyId = parchuran.partyId
						  WHERE purchType = 'A'
						  AND purchDate = '".$date."'";
		$selectRojmelRes = mysql_query($selectRojmel);
		while($rojmelRow = mysql_fetch_array($selectRojmelRes))
		{
		  $rojmelArray[$a]['purchId']     = $rojmelRow['purchId'];
		  $rojmelArray[$a]['purchDate']    = date('d-m-Y',strtotime($rojmelRow['purchDate']));
		  $rojmelArray[$a]['purchType']    = $rojmelRow['purchType'];
		  $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 parchuran
						LEFT JOIN party ON party.partyId = parchuran.partyId
						  WHERE purchType = 'J'
						   AND purchDate = '".$date."'";
		$selectJavakRes = mysql_query($selectJavak);
		while($javakRow = mysql_fetch_array($selectJavakRes))
		{
		  $javakArray[$k]['purchId']     = $javakRow['purchId'];
		  $javakArray[$k]['purchDate']    = date('d-m-Y',strtotime($javakRow['purchDate']));
		  $javakArray[$k]['purchType']    = $javakRow['purchType'];
		  $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 parchuran
							WHERE purchType = '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 parchuran
							WHERE purchType = '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("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("purList.tpl");
}
?>