<?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;
	
	if($partyId > 0)
	{
		$k = 0;
		$clientDetail = array();
		$toDate       = $_REQUEST['toYear'].'-'.$_REQUEST['toMonth'].'-'.$_REQUEST['toDay'];
		$fromDate     = $_REQUEST['fromYear'].'-'.$_REQUEST['fromMonth'].'-'.$_REQUEST['fromDay'];
		//////////////////////////////////////Buy : Start //////////////////////////////////
		$selectClient = "SELECT * FROM client
						 WHERE partyId = ".$partyId."
						 AND  cDate BETWEEN '".$toDate."'
						 AND '".$fromDate."'
						 AND  cstatus = 'A'
						  AND saudaId > 0
						 ORDER BY clientId ASC";
		$selectClientRes = mysql_query($selectClient);
		while($clientRow = mysql_fetch_array($selectClientRes))
		{
		  $clientDetail[$k]['clientId']     = $clientRow['clientId'];
		  $clientDetail[$k]['particular']   = $clientRow['particular'];
		  $clientDetail[$k]['cliTransType'] = $clientRow['cliTransType'];
		  $clientDetail[$k]['cDate']        = date('d-m-Y',strtotime($clientRow['cDate']));
		  $clientDetail[$k]['rate']       	= $clientRow['rate'];
		  $clientDetail[$k]['kg']         	= $clientRow['kg'];
		  $clientDetail[$k]['rojmelId']     = $clientRow['rojmelId'];
		  $clientDetail[$k]['sell_type']    = $clientRow['sell_type'];
		  $clientDetail[$k]['acc_type']     = $clientRow['acc_type'];
		  $clientDetail[$k]['totalAmt']   	= $clientRow['totalAmt'];
		  $clientTotalAmt				   	+= $clientRow['totalAmt'];
		  $clientTotalKg					+= $clientRow['kg'];
		  $k++;
		} 
		
		//////////////////////////////////////Buy : End //////////////////////////////////
	
	
		//////////////////////////////////////Sell : Start //////////////////////////////////
			
			$kk = 0;
			$clientDet = array();
			$selectClient = "SELECT *,acc_type,rojTransType,client.particular FROM client
							LEFT JOIN rojmel ON rojmel.rojmelId = client.rojmelId
							 WHERE client.partyId = ".$partyId."
							 AND  cDate BETWEEN '".$toDate."'
							 AND '".$fromDate."'
							 AND  cstatus = 'A'
							 AND client.rojmelId > 0
							 ORDER BY clientId 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'];
			  $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("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");
}
?>