<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	$partyListArray = array();
	$type       	= isset($_REQUEST['type']) && $_REQUEST['type'] != '' ? $_REQUEST['type'] : '';
	$i = 0;
    
	if($type == 'C')
	{
		$selectItem = "SELECT party.partyId,partyName,address,mobile,otherInfo,partyType
						 FROM  party
						WHERE partyType = 'C'
						ORDER BY partyName";
		$selectItemRes = mysql_query($selectItem);
		while($itemRow = mysql_fetch_array($selectItemRes))
		{
		  
			$partyListArray[$i]['partyId']       = $itemRow['partyId'];
			$partyListArray[$i]['partyName']     = $itemRow['partyName'];
			$partyListArray[$i]['partyType']     = $itemRow['partyType'];
			$partyListArray[$i]['address']       = $itemRow['address'];
			$partyListArray[$i]['mobile']        = $itemRow['mobile'];
			$partyListArray[$i]['otherInfo']     = $itemRow['otherInfo'];
			
			$selectAavak = "SELECT SUM(totalAmt) AS avakAmt,SUM(kg) AS avakkg
							 FROM client
							WHERE partyId =".$itemRow['partyId']."
							AND cliTransType = 'A'";
			$selectAavakRes = mysql_query($selectAavak);
			while($aavakRow = mysql_fetch_array($selectAavakRes))
			{
			  $partyListArray[$i]['avakAmt']         =  $aavakRow['avakAmt'];
			  $partyListArray[$i]['avakkg']          = $aavakRow['avakkg'];
			}
			
			$selectJavak = "SELECT SUM(totalAmt) AS javakAmt,SUM(kg) AS javakkg
							 FROM client
							WHERE partyId =".$itemRow['partyId']."
							AND cliTransType = 'J'";
			$selectJavakRes = mysql_query($selectJavak);
			while($javakRow = mysql_fetch_array($selectJavakRes))
			{
			  $partyListArray[$i]['javakAmt']   = $javakRow['javakAmt'];
			  $partyListArray[$i]['javakkg']    = $javakRow['javakkg'];
			}
			
			$partyListArray[$i]['totAmt']        = $partyListArray[$i]['avakAmt'] - $partyListArray[$i]['javakAmt'] ;
			$partyListArray[$i]['totoKg']        = $partyListArray[$i]['avakkg'] - $partyListArray[$i]['javakkg'];
			$partyListArray[$i]['otherInfo']     = $itemRow['otherInfo'];
			$i++;                                      
		}
	}
	
	
	
	if($type == 'V')
	{
		$selectItem = "SELECT party.partyId,partyName,address,mobile,otherInfo,partyType
						 FROM  party
						WHERE partyType = 'V'
						ORDER BY partyName";
		$selectItemRes = mysql_query($selectItem);
		while($itemRow = mysql_fetch_array($selectItemRes))
		{
		  
			$partyListArray[$i]['partyId']       = $itemRow['partyId'];
			$partyListArray[$i]['partyName']     = $itemRow['partyName'];
			$partyListArray[$i]['partyType']     = $itemRow['partyType'];
			$partyListArray[$i]['address']       = $itemRow['address'];
			$partyListArray[$i]['mobile']        = $itemRow['mobile'];
			$partyListArray[$i]['otherInfo']     = $itemRow['otherInfo'];
			
			$selectAavak = "SELECT SUM(totalAmt) AS avakAmt,SUM(kg) AS avakkg
							 FROM  vepari
							WHERE partyId =".$itemRow['partyId']."
							AND veTransType = 'A'";
			$selectAavakRes = mysql_query($selectAavak);
			while($aavakRow = mysql_fetch_array($selectAavakRes))
			{
			  $partyListArray[$i]['avakAmt']         =  $aavakRow['avakAmt'];
			  $partyListArray[$i]['avakkg']          = $aavakRow['avakkg'];
			}
			
			$selectJavak = "SELECT SUM(totalAmt) AS javakAmt,SUM(kg) AS javakkg
							 FROM  vepari
							WHERE partyId =".$itemRow['partyId']."
							AND veTransType = 'J'";
			$selectJavakRes = mysql_query($selectJavak);
			while($javakRow = mysql_fetch_array($selectJavakRes))
			{
			  $partyListArray[$i]['javakAmt']   = $javakRow['javakAmt'];
			  $partyListArray[$i]['javakkg']    = $javakRow['javakkg'];
			}
			
			$partyListArray[$i]['totAmt']        = $partyListArray[$i]['avakAmt'] - $partyListArray[$i]['javakAmt'] ;
			$partyListArray[$i]['totoKg']        = $partyListArray[$i]['javakkg'] - $partyListArray[$i]['avakkg'];
			$partyListArray[$i]['otherInfo']     = $itemRow['otherInfo'];
			$i++;                                      
		}
	}
	
	$typeArrayValue[0] = 'C';
	$typeArrayOutput[0] = 'Client';
	$typeArrayValue[1] = 'V';
	$typeArrayOutput[1] = 'Vepari';
}
include("./bottom.php");
$smarty->assign("type",$type);
$smarty->assign("typeArrayValue",$typeArrayValue);
$smarty->assign("typeArrayOutput",$typeArrayOutput);
$smarty->assign("partyTypeValuePost",$partyTypeValuePost);
$smarty->assign("partyOutput",$partyOutput);
$smarty->assign("partyValue",$partyValue);
$smarty->assign("partyListArray",$partyListArray);
$smarty->display("partyList.tpl");
?>