<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	$partyId	 = isset($_REQUEST['partyId']) ? $_REQUEST['partyId'] : 0;
	$toDate       = $_REQUEST['toYear'].'-'.$_REQUEST['toMonth'].'-'.$_REQUEST['toDay'];
	$fromDate     = $_REQUEST['fromYear'].'-'.$_REQUEST['fromMonth'].'-'.$_REQUEST['fromDay'];
		
	if($partyId > 0)
	{
		$k = 0;
		$clientDetail = array();
		$selectClient = "SELECT *,partyName FROM mcx
						   LEFT JOIN party ON party.partyId = mcx.mcxPartyId
						   WHERE status = 'A'
						   AND transType = 'A'
						   AND mcxPartyId = ".$partyId."
						   AND transDate BETWEEN '".$toDate."'
							   AND '".$fromDate."'
						  ORDER BY mcxId";
		$selectClientRes = mysql_query($selectClient);
		while($clientRow = mysql_fetch_array($selectClientRes))
		{
		  $clientDetail[$k]['mcxId']        = $clientRow['mcxId'];
		  $clientDetail[$k]['partyName']    = $clientRow['partyName'];
		  $clientDetail[$k]['rate']         = $clientRow['rate'];
		  $clientDetail[$k]['transDate']    = date('d-m-Y',strtotime($clientRow['transDate']));
		  $clientDetail[$k]['transType']    = $clientRow['transType'];
		  $clientDetail[$k]['total']        = $clientRow['total'];
		  $clientDetail[$k]['kg']           = $clientRow['kg'];
		  $clientDetail[$k]['totalAmt']     = $clientRow['totalAmt'];
		  $clientDetail[$k]['a']            = $clientRow['rate'] * $clientRow['kg'];
		  $totalAmtNew					    += $clientRow['rate'] * $clientRow['kg'];
		  $k++;
		}
		
		
		$kk = 0;
		$javakDetail = array();
		$selectJavak = "SELECT *,partyName FROM mcx
						   LEFT JOIN party ON party.partyId = mcx.mcxPartyId
						   WHERE status = 'A'
						   AND transType = 'J'
						   AND transDate BETWEEN '".$toDate."'
						   AND '".$fromDate."'
						   AND mcxPartyId = ".$partyId."
						  ORDER BY mcxId";
		$selectJavakRes = mysql_query($selectJavak);
		while($javakRow = mysql_fetch_array($selectJavakRes))
		{
		  $javakDetail[$kk]['mcxId']        = $javakRow['mcxId'];
		  $javakDetail[$kk]['partyName']    = $javakRow['partyName'];
		  $javakDetail[$kk]['rate']         = $javakRow['rate'];
		  $javakDetail[$kk]['transDate']    = date('d-m-Y',strtotime($javakRow['transDate']));
		  $javakDetail[$kk]['transType']    = $javakRow['transType'];
		  $javakDetail[$kk]['total']        = $javakRow['total'];
		  $javakDetail[$kk]['kg']           = $javakRow['kg'];
		  $javakDetail[$kk]['totalAmt']     = $javakRow['totalAmt'];
		  $javakDetail[$kk]['a']            = $javakRow['rate'] * $javakRow['kg'];
		  $totalAmtNew					    += $javakRow['rate'] * $javakRow['kg'];
		  $kk++;
		}
	
		$z = 0;
		$aavakArray = array();
		$selectAavak = "SELECT rate,kg, totalAmt AS aAmt
						  FROM mcx
						WHERE transType = 'A'
						AND mcxPartyId = ".$partyId."
						AND transDate BETWEEN '".$toDate."'
						AND '".$fromDate."'
						AND status = 'A'";
		$selectAavakRes = mysql_query($selectAavak);
		while($aRow = mysql_fetch_array($selectAavakRes))
		{
		  $aavakArray['rate'][$z] = $aRow['rate'];
		  $aavakArray['kg'][$z]   = $aRow['kg'];
		  $aavakArray['x'][$z]    = $aRow['rate'] * $aRow['kg'];
		  $aa                     += $aRow['rate'];
		  $aRas                   +=  $aRow['rate'] * $aRow['kg'];
		  $akg                    += $aRow['kg'];
		  $aAmt                   += $aRow['aAmt'];
		  $z++;
		}
		
		$o=0;
		$javakArray = array();
		$selectjavak = "SELECT rate AS jrate,kg AS jkg, totalAmt AS jAmt
						  FROM mcx
						WHERE transType = 'J'
						AND mcxPartyId = ".$partyId."
						AND transDate BETWEEN '".$toDate."'
						AND '".$fromDate."'
						AND status = 'A'";
		$selectjavakRes = mysql_query($selectjavak);
		while($jRow = mysql_fetch_array($selectjavakRes))
		{
		  $javakArray['jrate'][$o] = $jRow['jrate'];
		  $javakArray['jkg'][$o]   = $jRow['jkg'];  
		  $aavakArray['w'][$z]     = $jRow['jrate'] * $jRow['jkg'];
		  $jRas                    += $jRow['jrate'] * $jRow['jkg'];
		  $jkg                     += $jRow['jkg'];
		  $jAmt                    += $jRow['jAmt'];
		  $o++;
		}
	}

   
    $finalAmt   = $aAmt - $jAmt;
	 
	 
	//////////////////////////Broke:start/////////////////////////// 
	$finalKg   = $akg - $jkg;
	if($akg > $jkg)
	{
		$mcxKg = $akg;
	}
	else
	{
		$mcxKg = $jkg;
	}
	//////////////////////////Broke:End/////////////////////////// 
	$k=0;
	$clientArray = array();
	$selectClient = "SELECT partyId,partyName
	                   FROM party
					WHERE partyType = 'M'
				   ORDER BY partyName ASC";
	$selectClientRes = mysql_query($selectClient);
	while($clientRow = mysql_fetch_array($selectClientRes))
	{
	  $clientArray['partyId'][$k]   = $clientRow['partyId'];
	  $clientArray['partyName'][$k] = $clientRow['partyName'];
	  $k++;
	}
	
	include("./bottom.php");
	$smarty->assign("javakDetail",$javakDetail);
	$smarty->assign("finalAmt",$finalAmt);
	$smarty->assign("aa",$aa);
	$smarty->assign("finalRasAmt",$finalRasAmt);
	$smarty->assign("finalRas",$finalRas);
	$smarty->assign("mcxtotal",$mcxtotal);
	$smarty->assign("totalAmtNew",$totalAmtNew);
	$smarty->assign("totalAmt",$totalAmt);
	$smarty->assign("jtotalAmt",$jtotalAmt);
	$smarty->assign("atotalAmt",$atotalAmt);
	$smarty->assign("partyId",$partyId);
	$smarty->assign("finalRate",$finalRate);
	$smarty->assign("finalKg",$finalKg);
	$smarty->assign("clientDetail",$clientDetail);
	$smarty->assign("mcxKg",$mcxKg);
	$smarty->assign("clientArray",$clientArray);
	$smarty->display("mcx.tpl");
}
?>