<?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;
	$weeklyDate   = date("Y-m-d", strtotime("-6 day"));  
	
	if($partyId > 0)
	{
		$k = 0;
		$toDate       = $_REQUEST['toYear'].'-'.$_REQUEST['toMonth'].'-'.$_REQUEST['toDay'];
		$fromDate     = $_REQUEST['fromYear'].'-'.$_REQUEST['fromMonth'].'-'.$_REQUEST['fromDay'];
		
		///////////////////////Pending Sauda : Start ////////////////////////
			
    $a = 0;
    $rojmelArray = array();

	$selectRojmel = "SELECT *,partyName FROM sauda
					 LEFT  JOIN party ON party.partyId = sauda.partyId
					  WHERE rojTransType = 'J'
					  AND mcxcheckStatus = 0
					  AND sauda.partyId = ".$partyId."
					  ORDER BY saudaDate ASC";
	$selectRojmelRes = mysql_query($selectRojmel);
	while($rojmelRow = mysql_fetch_array($selectRojmelRes))
	{
	  $rojmelArray[$a]['saudaId']     		= $rojmelRow['saudaId'];
	  $rojmelArray[$a]['saudaDate']   		= date('d-m-Y',strtotime($rojmelRow['saudaDate']));
	  $rojmelArray[$a]['rojTransType'] 		= $rojmelRow['rojTransType'];
	  $rojmelArray[$a]['partyName']    		= $rojmelRow['partyName'];
	  $rojmelArray[$a]['particular']   		= $rojmelRow['particular'];
	  $rojmelArray[$a]['fine']         		= $rojmelRow['fine'];
	  $rojmelArray[$a]['amount']       		= $rojmelRow['amount'];
	  $rojmelArray[$a]['acc_type']     		= $rojmelRow['acc_type'];
	  $rojmelArray[$a]['rate']   	  		= $rojmelRow['rate'];
	  $rojmelArray[$a]['cash_type']    		= $rojmelRow['cash_type'];
	  $rojmelArray[$a]['metal_type']    	= $rojmelRow['metal_type'];
	  $rojmelArray[$a]['mcxcheckStatus']    = $rojmelRow['mcxcheckStatus'];
	  $rojmelArray[$a]['branch']    		= $rojmelRow['branch'];
	  $a++;
	}
	
	$k = 0;
	$javakArray = array();
	$selectJavak = "SELECT *,partyName FROM sauda
					LEFT JOIN party ON party.partyId = sauda.partyId
					 WHERE rojTransType = 'A'
					  AND mcxcheckStatus = 0
					  AND sauda.partyId = ".$partyId."
					  ORDER BY saudaDate ASC";
	$selectJavakRes = mysql_query($selectJavak);
	while($javakRow = mysql_fetch_array($selectJavakRes))
	{
	  $javakArray[$k]['saudaId']     	= $javakRow['saudaId'];
	  $javakArray[$k]['saudaDate']   	= date('d-m-Y',strtotime($javakRow['saudaDate']));
	  $javakArray[$k]['rojTransType'] 	= $javakRow['rojTransType'];
	  $javakArray[$k]['partyName']    	= $javakRow['partyName'];
	  $javakArray[$k]['particular']   	= $javakRow['particular'];
	  $javakArray[$k]['fine']         	= $javakRow['fine'];
	  $javakArray[$k]['amount']       	= $javakRow['amount'];
	  $javakArray[$k]['acc_type']     	= $javakRow['acc_type'];
	  $javakArray[$k]['rate']   	  	= $javakRow['rate'];
	  $javakArray[$k]['metal_type']   	= $javakRow['metal_type'];
	  $javakArray[$k]['cash_type']   	= $javakRow['cash_type'];
	  $javakArray[$k]['mcxcheckStatus'] = $javakRow['mcxcheckStatus'];
	  $javakArray[$k]['branch']   		= $javakRow['branch'];
	  $k++;
	}
		///////////////////////Pending Sauda : End ////////////////////////
		//////////////////////////////////////Buy : Start //////////////////////////////////
		$kkk = 0;
		$clientDetail = array();
		$selectClient = "SELECT *,sauda.metal_type,sauda.saudaId FROM client
						LEFT JOIN sauda ON sauda.saudaId = client.saudaId
						 WHERE client.partyId = ".$partyId."
						 AND  cDate BETWEEN '".$toDate."'
						 AND '".$fromDate."'
						 AND  cstatus = 'A'
						  AND client.saudaId > 0
						 ORDER BY client.cDate ASC";
		$selectClientRes = mysql_query($selectClient);
		while($clientRow = mysql_fetch_array($selectClientRes))
		{
		  $clientDetail[$kkk]['clientId']     = $clientRow['clientId'];
		  $clientDetail[$kkk]['saudaId']      = $clientRow['saudaId'];
		  $clientDetail[$kkk]['particular']   = $clientRow['particular'];
		  $clientDetail[$kkk]['cliTransType'] = $clientRow['cliTransType'];
		  $clientDetail[$kkk]['cDate']        = date('d-m-Y',strtotime($clientRow['cDate']));
		  $clientDetail[$kkk]['rate']         = $clientRow['rate'];
		  $clientDetail[$kkk]['kg']           = $clientRow['kg'];
		  $clientDetail[$kkk]['rojmelId']     = $clientRow['rojmelId'];
		  $clientDetail[$kkk]['sell_type']    = $clientRow['sell_type'];
		  $clientDetail[$kkk]['acc_type']     = $clientRow['acc_type'];
		  $clientDetail[$kkk]['totalAmt']     = $clientRow['totalAmt'];
		  $clientDetail[$kkk]['metal_type']   = $clientRow['metal_type'];
		  $clientTotalAmt				   	  += $clientRow['totalAmt'];
		  $clientTotalKg					  += $clientRow['kg'];
		  $kkk++;
		} 
		
		//////////////////////////////////////Buy : End //////////////////////////////////
	
	
		//////////////////////////////////////Sell : Start //////////////////////////////////
			
			$kk = 0;
			$clientDet = array();
			$selectClient = "SELECT *,rojmel.acc_type,rojmel.rojTransType,client.particular,rojmel.metal_type
							FROM client
							LEFT JOIN rojmel ON rojmel.rojmelId = client.rojmelId
							LEFT JOIN sauda ON sauda.saudaId = client.saudaId
							 WHERE client.partyId = ".$partyId."
							 AND  cDate BETWEEN '".$toDate."'
							 AND '".$fromDate."'
							 AND  cstatus = 'A'
							 AND client.rojmelId > 0
							  ORDER BY client.cDate ASC";
			$selectClientRes = mysql_query($selectClient);
			while($clientRow = mysql_fetch_array($selectClientRes))
			{
			  $clientDet[$kk]['clientId']     = $clientRow['clientId'];
			  $clientDet[$kk]['particular']   = $clientRow['particular'];
			  $clientDet[$kk]['cliTransType'] = $clientRow['cliTransType'];
			  $clientDet[$kk]['cDate']        = date('d-m-Y',strtotime($clientRow['cDate']));
			  $clientDet[$kk]['rate']         = $clientRow['rate'];
			  $clientDet[$kk]['kg']           = $clientRow['kg'];
			  $clientDet[$kk]['rojmelId']     = $clientRow['rojmelId'];
			  $clientDet[$kk]['sell_type']    = $clientRow['sell_type'];
			  $clientDet[$kk]['acc_type']     = $clientRow['acc_type'];
			  $clientDet[$kk]['rojTransType'] = $clientRow['rojTransType'];
			  $clientDet[$kk]['is_opening']   = $clientRow['is_opening'];
			  $clientDet[$kk]['totalAmt']     = $clientRow['totalAmt'];
			  $clientDet[$kk]['metal_type']   = $clientRow['metal_type'];
			  $rojmelAmt				      += $clientRow['totalAmt'];
			  $rojmelKg				    	  += $clientRow['kg'];
			  $kk++;
			} 
			
			$selectClient = "SELECT * FROM client
						 WHERE partyId = ".$partyId."
						 AND  cstatus = 'A'
						  AND is_opening = 1
						  AND rojmelId = 0
						  AND  cDate BETWEEN '".$toDate."'
						  AND '".$fromDate."'
						 ORDER BY clientId ASC";
			$selectClientRes = mysql_query($selectClient);
			while($clientRow = mysql_fetch_array($selectClientRes))
			{
			  $clientDet[$kk]['clientId']     = '';
			  $clientDet[$kk]['particular']   = $clientRow['particular'];
			  $clientDet[$kk]['cliTransType'] = $clientRow['cliTransType'];
			  $clientDet[$kk]['is_opening']   = $clientRow['is_opening'];
			  $clientDet[$kk]['cDate']        = date('d-m-Y',strtotime($clientRow['cDate']));
			  $clientDet[$kk]['rate']         = '';
			  $clientDet[$kk]['kg']           = $clientRow['kg'];
			  $clientDet[$kk]['rojmelId']     = '';
			  $clientDet[$kk]['sell_type']    = '';
			  $clientDet[$kk]['acc_type']     = '';
			  $clientDet[$kk]['totalAmt']     = $clientRow['totalAmt'];
			  $isopeningAmt				      += $clientRow['totalAmt'];
			  $isopeningkg				      += $clientRow['kg'];
			  $kk++;
			} 
			
			uasort($clientDet, function($a, $b) {
                $value1 = strtotime($a['cDate']);
                $value2 = strtotime($b['cDate']);
                return $value1 - $value2;
            });
            $clientDet = array_values($clientDet);
		
		//////////////////////////////////////Sell : End //////////////////////////////////
		
		$grossAmt = $rojmelAmt + $isopeningAmt;
		$grossKg  = $rojmelKg + $isopeningkg;
	}
	
		$z=0;
		$clientArray = array();
		$selectClient = "SELECT partyId,partyName
						   FROM party
						WHERE partyType = 'C'
					   ORDER BY partyName";
		$selectClientRes = mysql_query($selectClient);
		while($clientRow = mysql_fetch_array($selectClientRes))
		{
		  $clientArray['partyId'][$z]   = $clientRow['partyId'];
		  $clientArray['partyName'][$z] = $clientRow['partyName'];
		  $z++;
		}

		$selectAavak = "SELECT SUM(totalAmt) AS avakAmt,SUM(kg) AS avakkg
		                 FROM client
						WHERE partyId = ".$partyId."
						AND cliTransType = 'A'";
		$selectAavakRes = mysql_query($selectAavak);
		while($aavakRow = mysql_fetch_array($selectAavakRes))
		{
		  $avakAmt         =  $aavakRow['avakAmt'];
		  $avakkg          = $aavakRow['avakkg'];
		}
		
		$selectJavak = "SELECT SUM(totalAmt) AS javakAmt,SUM(kg) AS javakkg
		                 FROM client
						WHERE partyId = ".$partyId."
						AND cliTransType = 'J'";
		$selectJavakRes = mysql_query($selectJavak);
		while($javakRow = mysql_fetch_array($selectJavakRes))
		{
		  $javakAmt   = $javakRow['javakAmt'];
		  $javakkg    = $javakRow['javakkg'];
		}
 
		$finalKg 	 = $avakkg - $javakkg;
		$finalAmount = $avakAmt - $javakAmt;
	 
	
	include("./bottom.php");
	$smarty->assign("rojmelArray",$rojmelArray);
	$smarty->assign("javakArray",$javakArray);
	$smarty->assign("weeklyDate",$weeklyDate);
	$smarty->assign("grossKg",$grossKg);
	$smarty->assign("grossKg",$grossKg);
	$smarty->assign("grossAmt",$grossAmt);
	$smarty->assign("clientTotalAmt",$clientTotalAmt);
	$smarty->assign("clientTotalKg",$clientTotalKg);
	$smarty->assign("clientDet",$clientDet);
	$smarty->assign("fromDate",$fromDate);
	$smarty->assign("toDate",$toDate);
	$smarty->assign("finalAmount",$finalAmount);
	$smarty->assign("finalKg",$finalKg);
	$smarty->assign("partyId",$partyId);
	$smarty->assign("clientDetail",$clientDetail);
	$smarty->assign("clientArray",$clientArray);
	$smarty->display("clientList.tpl");
}
?>