<?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'] : 0;
	$partyId       = isset($_REQUEST['partyId']) && $_REQUEST['partyId'] != '' ? $_REQUEST['partyId'] : 0;
	$kg      	   = isset($_REQUEST['kg']) && $_REQUEST['kg'] != '' ? $_REQUEST['kg'] : 0;
	$fine          = isset($_REQUEST['fine']) && $_REQUEST['fine'] != '' ? $_REQUEST['fine'] : 0;
	$cliTransType  = isset($_REQUEST['cliTransType']) && $_REQUEST['cliTransType'] != '' ? $_REQUEST['cliTransType'] : 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 client (partyId,cliTransType,kg,cDate,totalAmt,particular,is_opening)
	                    VALUES (".$partyId.",'".$cliTransType."',".$kg.",'".$cDate."','".$totalAmt."','New Opening',1)";
		$insertmcxRes = mysql_query($insertmcx);
		if(!$insertmcxRes)
		{
			echo "Insert Fail";
		}
		else
		{
			header("Location:cliententry.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'];
	      $cliTransType = $row['cliTransType'];
	      $rate         = $row['rate'];
	      $kg           = $row['kg'];
	      $totalAmt     = $row['totalAmt'];
	   }
	}
	
	if($clientId > 0 && $_REQUEST['submit'])
	{
 	  $update = "UPDATE mcx 
	                   SET transDate = '".$transDate."',
	                   cliTransType = '".$_REQUEST['cliTransType']."',
	                   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 = 'C'
				   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("cliTransType",$cliTransType);
	$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("rojcliTransType",$rojcliTransType);
	$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("cliententry.tpl");
}
?>