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

	$mcxId         = isset($_REQUEST['mcxId']) && $_REQUEST['mcxId'] != '' ? $_REQUEST['mcxId'] : "";
	$kg            = isset($_REQUEST['kg']) && $_REQUEST['kg'] != '' ? $_REQUEST['kg'] : "";
	$rate          = isset($_REQUEST['rate']) && $_REQUEST['rate'] != '' ? $_REQUEST['rate'] : "";
	$fine          = isset($_REQUEST['fine']) && $_REQUEST['fine'] != '' ? $_REQUEST['fine'] : 0;
	$mcxPartyId       = isset($_REQUEST['mcxPartyId']) && $_REQUEST['mcxPartyId'] != '' ? $_REQUEST['mcxPartyId'] : 0;
	$transType     = isset($_REQUEST['transType']) && $_REQUEST['transType'] != '' ? $_REQUEST['transType'] : 0;
	$totalAmt      = isset($_REQUEST['totalAmt']) && $_REQUEST['totalAmt'] != '' ? $_REQUEST['totalAmt'] : 0;
	$transDate     = $_REQUEST['rdtYear']."-".$_REQUEST['rdtMonth']."-".$_REQUEST['rdtDay'];  

    if(isset($_POST['submit']) && $mcxId == 0)
	{
	  $insertmcx = "INSERT INTO mcx (mcxPartyId,transType,transDate,rate,kg,totalAmt)
	                    VALUES (".$mcxPartyId.",'".$transType."','".$transDate."','".$rate."',".$kg.",'".$totalAmt."')";
	  $insertmcxRes = mysql_query($insertmcx);
	  if(!$insertmcxRes)
	  {
	     echo "Insert Fail";
	  }
	  else
	  {
	    header("Location:mcxentry.php");
	  }
	}

	$typeArrayValue[0] = 'J';
	$typeArrayOutput[0] = 'Buy';
	$typeArrayValue[1] = 'A';
	$typeArrayOutput[1] = 'Sell';
	
	
	if($mcxId > 0)
	{
      $selectMcx = "SELECT * FROM mcx 
	                  WHERE mcxId = ".$mcxId;
	   $selectMcxRes = mysql_query($selectMcx);
	   if($row = mysql_fetch_array($selectMcxRes))
	   {
	      $mcxId     = $row['mcxId'];
	      $partyId   = $row['partyId'];
	      $transDate = $row['transDate'];
	      $transType = $row['transType'];
	      $rate      = $row['rate'];
	      $kg        = $row['kg'];
	      $totalAmt  = $row['totalAmt'];
	      $mcxPartyId  = $row['mcxPartyId'];
	   }
	}
	
	if($mcxId > 0 && $_REQUEST['submit'])
	{
 	  $update = "UPDATE mcx 
	                   SET transDate = '".$transDate."',
	                   transType = '".$_REQUEST['transType']."',
	                   mcxPartyId = '".$_REQUEST['mcxPartyId']."',
	                   rate = '".$_REQUEST['rate']."',
	                   kg = '".$_REQUEST['kg']."',
	                   totalAmt = '".$_REQUEST['totalAmt']."'
				 WHERE mcxId  = ".$mcxId;
	  $res = mysql_query($update);
	  if(!$res)
	  {
	    echo "Mcx Update Fail";
	  }
	  else
	  {
	    header("Location:mcxentry.php");
	  }
	}
	
	
	$k=0;
	$clientArray = array();
	$selectClient = "SELECT partyId,partyName
	                   FROM party
					WHERE partyType = 'M'
					  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("clientArray",$clientArray);
	$smarty->assign("mcxPartyId",$mcxPartyId);
	$smarty->assign("transDate",$transDate);
	$smarty->assign("transType",$transType);
	$smarty->assign("rate",$rate);
	$smarty->assign("kg",$kg);
	$smarty->assign("totalAmt",$totalAmt);
	$smarty->assign("mcxId",$mcxId);
	$smarty->assign("partyId",$partyId);
	$smarty->assign("rojmelDate",$rojmelDate);
	$smarty->assign("rojTransType",$rojTransType);
	$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("mcxentry.tpl");
}
?>