<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	$pendingArray = array();
	$finetotal = 0;
	$fineamount = 0;
	$motiAmount = 0;
	$rhodium = 0;
	$grosstotal = 0;
	$selectParty = "SELECT * FROM party 
	                   WHERE partyType = 'C'
					  ORDER BY partyName";
	$selectPartyRes = mysql_query($selectParty);
	while($row = mysql_fetch_array($selectPartyRes))
	{
	  $pendingArray[$row['partyId']]['partyId']   = $row['partyId'];
	  $pendingArray[$row['partyId']]['partyName'] = $row['partyName'];
	  $pendingArray[$row['partyId']]['kg'] = 0;
	  $pendingArray[$row['partyId']]['totalAmt'] = 0;
	  
	   $slectIssue = "SELECT SUM(kg) AS ckg , SUM(totalAmt) AS ctotalAmt 
	                   FROM client
	                WHERE cliTransType = 'A'
					AND partyId = ".$row['partyId']."
					GROUP BY clientId";
	  $slectIssueRes = mysql_query($slectIssue);
	  while($irow = mysql_fetch_array($slectIssueRes))
	  {
	    $pendingArray[$row['partyId']]['tranjectionId']  = $irow['tranjectionId'];
	    $pendingArray[$row['partyId']]['kg']             += $irow['ckg'];
	    $pendingArray[$row['partyId']]['totalAmt']       += $irow['ctotalAmt'];
	  }

	  $slectIssue = "SELECT SUM(kg) AS jkg , SUM(totalAmt) AS jtotalAmt 
	                     FROM client
	               WHERE cliTransType = 'J'
					AND partyId = ".$row['partyId']."
					GROUP BY clientId";
	  $slectIssueResi = mysql_query($slectIssue);
	  while($irow = mysql_fetch_array($slectIssueResi))
	  {
	    $pendingArray[$row['partyId']]['kg']           -= $irow['jkg'];
		$pendingArray[$row['partyId']]['totalAmt']     -= $irow['jtotalAmt'];
	  }
	  
	  $kg        += $pendingArray[$row['partyId']]['kg'];
	  $totalAmt  += $pendingArray[$row['partyId']]['totalAmt'];
	}
	
	$selectParty = "SELECT * FROM party 
	                   WHERE partyType = 'V'
					  ORDER BY partyName";
	$selectPartyRes = mysql_query($selectParty);
	while($row = mysql_fetch_array($selectPartyRes))
	{
	  $pendingArray[$row['partyId']]['partyId']   = $row['partyId'];
	  $pendingArray[$row['partyId']]['partyName'] = $row['partyName'];
	  $pendingArray[$row['partyId']]['kg'] = 0;
	  $pendingArray[$row['partyId']]['totalAmt'] = 0;
	  
	  $slectIssue = "SELECT SUM(kg) AS jkg , SUM(totalAmt) AS jvTotalAmt 
	                     FROM vepari
	               WHERE veTransType = 'J'
					AND partyId = ".$row['partyId']."
					GROUP BY vepariId";
	  $slectIssueResi = mysql_query($slectIssue);
	  while($irow = mysql_fetch_array($slectIssueResi))
	  {
	    $pendingArray[$row['partyId']]['kg']           += $irow['jkg'];
		$pendingArray[$row['partyId']]['totalAmt']     += $irow['jvTotalAmt'];
	  }
	  
	   $slectIssue = "SELECT SUM(kg) AS akg , SUM(totalAmt) AS avTotalAmt 
	                     FROM vepari
	               WHERE veTransType = 'A'
					AND partyId = ".$row['partyId']."
					GROUP BY vepariId";
	  $slectIssueResi = mysql_query($slectIssue);
	  while($irow = mysql_fetch_array($slectIssueResi))
	  {
	    $pendingArray[$row['partyId']]['kg']           -= $irow['akg'];
		$pendingArray[$row['partyId']]['totalAmt']     -= $irow['avTotalAmt'];
	  }
	  
	  $vkg        += $pendingArray[$row['partyId']]['kg'];
	  $finalAmt   += $pendingArray[$row['partyId']]['totalAmt'];
	}
	
	
	$z = 0;
	$aavakArray = array();
	$selectAavak = "SELECT rate,kg
	                  FROM mcx
					WHERE transType = 'A'
					AND status = 'A'";
	$selectAavakRes = mysql_query($selectAavak);
	while($aRow = mysql_fetch_array($selectAavakRes))
	{
	  $akg   += $aRow['kg'];
	  $z++;
	
	}
	
	$o=0;
	$javakArray = array();
	$selectjavak = "SELECT rate AS jrate,kg AS jkg
	                  FROM mcx
					WHERE transType = 'J'
					 AND status = 'A'";
	$selectjavakRes = mysql_query($selectjavak);
	while($jRow = mysql_fetch_array($selectjavakRes))
	{
	  $jkg                     += $jRow['jkg'];
	  $o++;
	}
	
	///////rojmel start ///////
	$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;
	/////////////end//////////
	
	///rojmel start ///////
	
		$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'];
		}
		
		$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'];
		}
		
		$netFine   = $aFine - $jFine;
	///rojmel end ///////
	
	 $abs =abs($vtotalAmt);
	 
	 $clientAmt =abs($totalAmt);
	 
	 $finalKg   = $akg  - $jkg;
	
	$finalAmount = $abs +  $totalAmt;
	
	if($finalKg < 0)
	{
	  $finalAllKg = $vkg - $kg + $finalKg + $netFine;
	}
	else
	{
      $finalAllKg = $vkg - $kg - $finalKg + $netFine;
	}
	
	if($vtotalAmt < 0)
	{
	  $finalAmount = $abs +  $totalAmt;	
	}
	else
	{
	  $finalAmount = $abs -  $clientAmt;
	}
	
}
include("./bottom.php");
$smarty->assign("netFine",$netFine);
$smarty->assign("finalAllKg",$finalAllKg);
$smarty->assign("finalAmount",$finalAmount);
$smarty->assign("finalKg",$finalKg);
$smarty->assign("vkg",$vkg);
$smarty->assign("aavakArray",$aavakArray);
$smarty->assign("javakArray",$javakArray);
$smarty->assign("vtotalAmt",$vtotalAmt);
$smarty->assign("totalAmt",$totalAmt);
$smarty->assign("kg",$kg);
$smarty->assign("pendingArray",$pendingArray);
$smarty->display("all.tpl");
?>