<?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 * FROM vepari
						 WHERE vepari.partyId = ".$partyId."
						 AND vstatus = 'A'	
						 AND  vDate BETWEEN '".$toDate."'
						 AND '".$fromDate."'						 
						 AND saudaId > 0					 
						ORDER BY vepariId ASC";
		$selectClientRes = mysql_query($selectClient);
		while($clientRow = mysql_fetch_array($selectClientRes))
		{ 
		  $clientDetail[$k]['vepariId']     = $clientRow['vepariId'];
		  $clientDetail[$k]['vRate']        = $clientRow['vRate'];
		  $clientDetail[$k]['particular']   = $clientRow['particular'];
		  $clientDetail[$k]['veTransType']  = $clientRow['veTransType'];
		  $clientDetail[$k]['vDate']        = date('d-m-Y',strtotime($clientRow['vDate']));
		  $clientDetail[$k]['rate']    	    = $clientRow['rate'];
		  $clientDetail[$k]['kg']      		= $clientRow['kg'];
		  $clientDetail[$k]['totalAmt']     = $clientRow['totalAmt'];
		  $clientDetail[$k]['sell_type']    = $clientRow['sell_type'];
		  $k++;
		}
		
		$kk = 0;
		$clientDet = array();
		$selectClient = "SELECT *,acc_type FROM vepari
						LEFT JOIN rojmel ON rojmel.rojmelId = vepari.rojmelId
						 WHERE vepari.partyId = ".$partyId."
						 AND  vstatus = 'A'
						 AND  vDate BETWEEN '".$toDate."'
						 AND '".$fromDate."'	
						 AND vepari.rojmelId > 0
						 ORDER BY vepariId ASC";
		$selectClientRes = mysql_query($selectClient);
		while($clientRow = mysql_fetch_array($selectClientRes))
		{
		  $clientDet[$kk]['vepariId']     = $clientRow['vepariId'];
		  $clientDet[$kk]['particular']   = $clientRow['particular'];
		  $clientDet[$kk]['veTransType']  = $clientRow['veTransType'];
		  $clientDet[$kk]['vDate']        = date('d-m-Y',strtotime($clientRow['vDate']));
		  $clientDet[$kk]['rate']         = $clientRow['rate'];
		  $clientDet[$kk]['kg']           = $clientRow['kg'];
		  $clientDet[$kk]['rojmelId']     = $clientRow['rojmelId'];
		  $clientDet[$kk]['sell_type']    = $clientRow['sell_type'];
		  $clientDet[$kk]['totalAmt']     = $clientRow['totalAmt'];
		  $clientDet[$kk]['is_opening']   = $clientRow['is_opening'];
		  $kk++;
		}

		$selectClient = "SELECT * FROM vepari
						 WHERE partyId = ".$partyId."
					  AND  vstatus = 'A'
					  AND is_opening = 1
					  AND rojmelId = 0
					  AND  vDate BETWEEN '".$toDate."'
					  AND '".$fromDate."'	
					 ORDER BY vepariId ASC";
		$selectClientRes = mysql_query($selectClient);
		while($clientRow = mysql_fetch_array($selectClientRes))
		{
		  $clientDet[$kk]['vepariId']     = '';
		  $clientDet[$kk]['particular']   = $clientRow['particular'];
		  $clientDet[$kk]['veTransType']  = $clientRow['veTransType'];
		  $clientDet[$kk]['cDate']        = '';
		  $clientDet[$kk]['vDate']        = date('d-m-Y',strtotime($clientRow['vDate']));
		  $clientDet[$kk]['rate']         = '';
		  $clientDet[$kk]['is_opening']   = $clientRow['is_opening'];
		  $clientDet[$kk]['kg']           = $clientRow['kg'];
		  $clientDet[$kk]['rojmelId']     = '';
		  $clientDet[$kk]['sell_type']    = '';
		  $clientDet[$kk]['totalAmt']     = $clientRow['totalAmt'];
		  $kk++;
		}


		uasort($clientDet, function($a, $b) {
                $value1 = strtotime($a['vDate']);
                $value2 = strtotime($b['vDate']);
                return $value1 - $value2;
            });
            $clientDet = array_values($clientDet);
		
		$selectClientAavk = "SELECT SUM(kg) AS aKg,SUM(totalAmt) AS aTotalAmt
							  FROM vepari
						   WHERE veTransType = 'A'
						AND partyId = ".$partyId;
		$selectClientAavkRes = mysql_query($selectClientAavk);
		if($cavakRow = mysql_fetch_assoc($selectClientAavkRes))
		{
		  $aKg          = $cavakRow['aKg'];
		  $aTotalAmt    = $cavakRow['aTotalAmt'];  
		}
    
		$selectClientJavak = "SELECT SUM(kg) AS javakKg,SUM(totalAmt) AS javaktotalAmt
						   FROM vepari
						  WHERE veTransType = 'J'
					AND partyId = ".$partyId;
		$selectClientJavakRes = mysql_query($selectClientJavak);
		if($cjavakRow = mysql_fetch_assoc($selectClientJavakRes))
		{
		  $javakKg          = $cjavakRow['javakKg'];
		  $javaktotalAmt    = $cjavakRow['javaktotalAmt'];  
		}
	
 
		if($javakKg > 0)
		{
		  $finalKg     = $javakKg  - $aKg;
		}
		else
		{
		$finalKg     = $aKg - $javakKg;
		}
		
		$finalAmount = $javaktotalAmt - $aTotalAmt;
	}
		
		$z=0;
		$clientArray = array();
		$selectClient = "SELECT partyId,partyName
						   FROM party
						WHERE partyType = 'V'";
		$selectClientRes = mysql_query($selectClient);
		while($clientRow = mysql_fetch_array($selectClientRes))
		{
		  $clientArray['partyId'][$z]   = $clientRow['partyId'];
		  $clientArray['partyName'][$z] = $clientRow['partyName'];
		  $z++;
		}
	
	include("./bottom.php");
	$smarty->assign("fromDate",$fromDate);
	$smarty->assign("toDate",$toDate);
	$smarty->assign("clientDet",$clientDet);
	$smarty->assign("crArray",$crArray);
	$smarty->assign("rojmelArray",$rojmelArray);
	$smarty->assign("finalAmount",$finalAmount);
	$smarty->assign("finalKg",$finalKg);
	$smarty->assign("partyId",$partyId);
	$smarty->assign("clientDetail",$clientDetail);
	$smarty->assign("clientArray",$clientArray);
	$smarty->display("vepariList.tpl");
}
?>