<?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 
					  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(vkg) AS jkg , SUM(finalAmt) 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(vkg) AS akg , SUM(finalAmt) 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'];
	  }
	  ///////////////////////////// mcx : start////////////////////////
	  
	 
	 $selectAavak = "SELECT SUM(kg) AS akg, SUM(totalAmt) AS aAmt
	                  FROM mcx
					WHERE transType = 'A'
					AND status = 'A'";
	$selectAavakRes = mysql_query($selectAavak);
	if($aRow = mysql_fetch_array($selectAavakRes))
	{
	  $akg          = $aRow['akg'];
	  $aAmt         = $aRow['aAmt'];
	   
	
	}

	 
	$selectjavak = "SELECT SUM(kg) AS jkg, SUM(totalAmt) AS jAmt
	                  FROM mcx
					WHERE transType = 'J'
					 AND status = 'A'";
	$selectjavakRes = mysql_query($selectjavak);
	if($jRow = mysql_fetch_array($selectjavakRes))
	{
	  $jkg                     = $jRow['jkg'];
	  $jAmt                    = $jRow['jAmt'];
	}
	
	 $finalKg   = $akg - $jkg;
     $finalAmt   = $aAmt - $jAmt;
	  ///////////////////////////// mcx : end////////////////////////
	  $kg        += $pendingArray[$row['partyId']]['kg'];
	  $totalAmt  += $pendingArray[$row['partyId']]['totalAmt'];
	}
	if ($finalKg < 0)
	{
	 $finalGrosskg =  $kg - $finalKg;
	}
	else
	{
		$finalGrosskg =  $kg + $finalKg;
	}
	
	if($finalAmt < 0)
	{
	 $finalgrossAmt =  $totalAmt + $finalAmt;
	}
	else
	{
		$finalgrossAmt =  $totalAmt - $finalAmt;
	}
}
include("./bottom.php");
$smarty->assign("finalgrossAmt",$finalgrossAmt);
$smarty->assign("finalGrosskg",$finalGrosskg);
$smarty->assign("finalKg",$finalKg);
$smarty->assign("finalAmt",$finalAmt);
$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("all1.tpl");
?>