<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{

	$clientId      = isset($_REQUEST['clientId']) && $_REQUEST['clientId'] != '' ? $_REQUEST['clientId'] : "";
	$partyId       = isset($_REQUEST['partyId']) && $_REQUEST['partyId'] != '' ? $_REQUEST['partyId'] : "";
	$kg            = isset($_REQUEST['kg']) && $_REQUEST['kg'] != '' ? $_REQUEST['kg'] : 0;
	$fine          = isset($_REQUEST['fine']) && $_REQUEST['fine'] != '' ? $_REQUEST['fine'] : 0;
	$veTransType   = isset($_REQUEST['veTransType']) && $_REQUEST['veTransType'] != '' ? $_REQUEST['veTransType'] : 0;
	$totalAmt      = isset($_REQUEST['totalAmt']) && $_REQUEST['totalAmt'] != '' ? $_REQUEST['totalAmt'] : 0;
	$cDate         = $_REQUEST['rdtYear']."-".$_REQUEST['rdtMonth']."-".$_REQUEST['rdtDay'];  

    if(isset($_POST['submit']) && $clientId == 0)
	{
	  $insertmcx = "INSERT INTO vepari (partyId,veTransType,kg,vDate,totalAmt,is_opening,particular)
	                    VALUES (".$partyId.",'".$veTransType."',".$kg.",'".$cDate."','".$totalAmt."','1','New Opening')";
	  $insertmcxRes = mysql_query($insertmcx);
	  if(!$insertmcxRes)
	  {
	     echo "Insert Fail";
	  }
	  else
	  {
	    header("Location:veparientry.php");
	  }
	}

	$typeArrayValue[0] = 'J';
	$typeArrayOutput[0] = 'Javak';
	$typeArrayValue[1] = 'A';
	$typeArrayOutput[1] = 'Aavak';
	
	
	if($clientId > 0)
	{
      $selectMcx = "SELECT * FROM mcx 
	                  WHERE clientId = ".$clientId;
	   $selectMcxRes = mysql_query($selectMcx);
	   if($row = mysql_fetch_array($selectMcxRes))
	   {
	      $clientId     = $row['clientId'];
	      $partyId      = $row['partyId'];
	      $transDate    = $row['transDate'];
	      $veTransType  = $row['veTransType'];
	      $rate         = $row['rate'];
	      $kg           = $row['kg'];
	      $totalAmt     = $row['totalAmt'];
	   }
	}
	
	if($clientId > 0 && $_REQUEST['submit'])
	{
 	  $update = "UPDATE mcx 
	                   SET transDate = '".$transDate."',
	                   veTransType = '".$_REQUEST['veTransType']."',
	                   rate = '".$_REQUEST['rate']."',
	                   kg = '".$_REQUEST['kg']."',
	                   totalAmt = '".$_REQUEST['totalAmt']."'
				 WHERE clientId  = ".$clientId;
	  $res = mysql_query($update);
	  if(!$res)
	  {
	    echo "Mcx Update Fail";
	  }
	  else
	  {
	  //  header("Location:cliententry.php");
	  }
	}
	
	$k=0;
	$clientArray = array();
	$selectClient = "SELECT partyId,partyName
	                   FROM party
					WHERE partyType = 'V'
				   ORDER BY partyName";
	$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("transDate",$transDate);
	$smarty->assign("clientArray",$clientArray);
	$smarty->assign("veTransType",$veTransType);
	$smarty->assign("rate",$rate);
	$smarty->assign("kg",$kg);
	$smarty->assign("totalAmt",$totalAmt);
	$smarty->assign("clientId",$clientId);
	$smarty->assign("partyId",$partyId);
	$smarty->assign("rojmelDate",$rojmelDate);
	$smarty->assign("particular",$particular);
	$smarty->assign("fine",$fine);
	$smarty->assign("amount",$amount);
	$smarty->assign("rojmelId",$rojmelId);
	$smarty->assign("typeArrayValue",$typeArrayValue);
	$smarty->assign("typeArrayOutput",$typeArrayOutput);
	$smarty->display("veparientry.tpl");
}
?>