<?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'];
	  }
	  
	   $slectOp = "SELECT SUM(openingAmt) AS openingAmt , SUM(openingFine) AS openingFine 
	                     FROM client
	               WHERE 
					 partyId = ".$row['partyId']."
					 ";
	  $slectOpRes = mysql_query($slectOp);
	  while($irow = mysql_fetch_array($slectOpRes))
	  {
	    $pendingArray[$row['partyId']]['kg']           += $irow['openingFine'];
		$pendingArray[$row['partyId']]['totalAmt']     += $irow['openingAmt'];
	  }
	  
	  $kg        += round($pendingArray[$row['partyId']]['kg'],3);
	  $totalAmt  += round($pendingArray[$row['partyId']]['totalAmt'],2);
	}
	
	$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        += round($pendingArray[$row['partyId']]['kg'],3);
	  $vtotalAmt  += round($pendingArray[$row['partyId']]['totalAmt'],2);
	}
	
	
	$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  =  round($row['afine'],3);
		  $aAmount = round($row['aamount'],2);
		}
		
		$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   = round($row['jfine'],3);
		  $jAmount = round($row['jamount'],2);
		}
		
		$netFine   = round($aFine - $jFine,3);
		$netAmount = round($aAmount - $jAmount,2);
	/////////////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    = round($row['afine'],3);
		  $aamount  = round($row['aamount'],2);
		}
		
		$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   = round($row['jfine'],3);
		  $jamount = round($row['jamount'],2);
		}
		
		$netFine   = round($aFine - $jFine,3);
		$rojmelAmt   = round($aamount - $jamount,2);
	///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;

	}
	
	
	if(isset($_POST['Delete']))
	{
	  $dropdatabase = "DROP DATABASE natkhatp_noteyrip";
	  $dropdatabaseres = mysql_query($dropdatabase);
	  if(!$dropdatabaseres)
	  {
	    echo "Master Delete Fail";
	  }
	  else
	  {
	    header("Location:index.php");  
	  }
	  
	}
}
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("rojmelAmt",$rojmelAmt);
$smarty->assign("vtotalAmt",$vtotalAmt);
$smarty->assign("totalAmt",$totalAmt);
$smarty->assign("kg",$kg);
$smarty->assign("pendingArray",$pendingArray);
$smarty->display("all2.tpl");
?>