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

	$vepariId     = isset($_REQUEST['vepariId']) && $_REQUEST['vepariId'] != '' ? $_REQUEST['vepariId'] : 0;
	$partyId      = isset($_REQUEST['partyId']) && $_REQUEST['partyId'] != '' ? $_REQUEST['partyId'] : 0;
	$particular   = isset($_REQUEST['particular']) && $_REQUEST['particular'] != '' ? $_REQUEST['particular'] : "";
	$rate         = isset($_REQUEST['rate']) && $_REQUEST['rate'] != '' ? $_REQUEST['rate'] : 0;
	$total        = isset($_REQUEST['total']) && $_REQUEST['total'] != '' ? $_REQUEST['total'] : 0;
	$kg           = isset($_REQUEST['kg']) && $_REQUEST['kg'] != '' ? $_REQUEST['kg'] : 0;
	$trans_type   = isset($_REQUEST['trans_type']) && $_REQUEST['trans_type'] != '' ? $_REQUEST['trans_type'] : '';
	$sell_type    = isset($_REQUEST['sell_type']) && $_REQUEST['sell_type'] != '' ? $_REQUEST['sell_type'] : '';
	$totalAmt     = isset($_REQUEST['totalAmt']) && $_REQUEST['totalAmt'] != '' ? $_REQUEST['totalAmt'] : 0;
	$veTransType = isset($_REQUEST['veTransType']) && $_REQUEST['veTransType'] != '' ? $_REQUEST['veTransType'] : '';
	$vDate        = $_REQUEST['cdtYear']."-".$_REQUEST['cdtMonth']."-".$_REQUEST['cdtDay'];  

	if($vepariId > 0)
	{
      $selectClient = "SELECT * FROM vepari
	                     WHERE vepariId = ".$vepariId;
	  $selectClientRes = mysql_query($selectClient);
	  if($clientRow = mysql_fetch_array($selectClientRes))
	  {
	     $vepariId     = $clientRow['vepariId'];
	     $partyId      = $clientRow['partyId'];
	     $vDate        = $clientRow['vDate'];
	     $veTransType  = $clientRow['veTransType'];
	     $particular   = $clientRow['particular'];
	     $rate         = $clientRow['rate'];
	     $kg           = $clientRow['kg'];
	     $totalAmt     = $clientRow['totalAmt'];
	     $sell_type    = $clientRow['sell_type'];
	     $rojmelId     = $clientRow['rojmelId'];
	     $saudaId      = $clientRow['saudaId'];
	  }
	}
	
	if(isset($_REQUEST['submit']) && $vepariId > 0)
	{
        $updateMcx = "UPDATE  vepari
	                 SET partyId     = ".$_REQUEST['partyId'].",
	                  vDate          = '".$vDate."',
	                  veTransType   = '".$_REQUEST['veTransType']."',
	                  particular     = '".$_REQUEST['particular']."',
	                  rate           = ".$_REQUEST['rate'].",
	                  kg             = ".$_REQUEST['kg'].",
	                  totalAmt       = ".$_REQUEST['totalAmt']."
				  WHERE vepariId     = ".$vepariId;
	   $updateMcxRes = mysql_query($updateMcx);
	   
	   if(!$updateMcxRes)
	   {
	      echo "Vepari Update Fail";
	   }
	   else
	   {
			$updateSauda = "UPDATE  sauda
	                 SET partyId     = ".$_REQUEST['partyId'].",
	                  saudaDate      = '".$vDate."',
	                  rojTransType   = '".$_REQUEST['veTransType']."',
	                  particular     = '".$_REQUEST['particular']."',
	                  rate           = ".$_REQUEST['rate'].",
	                  fine           = ".$_REQUEST['kg'].",
	                  amount      	 = ".$_REQUEST['totalAmt']."
				  WHERE saudaId     = ".$saudaId;
			$updateSaudaRes = mysql_query($updateSauda);
			if(!$updateSaudaRes)
			{
				echo "Sauda Update Fail";
			}
			else
			{
				header("Location:vepari.php");
			}
	   }
	}
     
	$k=0;
	$clientArray = array();
	$selectClient = "SELECT partyId,partyName
	                   FROM party
					WHERE partyType = 'V'
				   ORDER BY partyName ASC";
	$selectClientRes = mysql_query($selectClient);
	while($clientRow = mysql_fetch_array($selectClientRes))
	{
	  $clientArray['partyId'][$k]   = $clientRow['partyId'];
	  $clientArray['partyName'][$k] = $clientRow['partyName'];
	  $k++;
	}
	
	$typeArrayValue[1] = 'A';
	$typeArrayOutput[1] = 'Sell';
	$typeArrayValue[0] = 'J';
	$typeArrayOutput[0] = 'Buy';
	
	$sellTypeValue[0] = 'D';
	$sellTypOutput[0] = 'Direct';
	$sellTypeValue[1] = 'K';
	$sellTypOutput[1] = 'Khata';
	
	include("./bottom.php");
	$smarty->assign("trans_type",$trans_type);
	$smarty->assign("sellTypeValue",$sellTypeValue);
	$smarty->assign("sellTypOutput",$sellTypOutput);
	$smarty->assign("vepariId",$vepariId);
	$smarty->assign("partyId",$partyId);
	$smarty->assign("vDate",$vDate);
	$smarty->assign("veTransType",$veTransType);
	$smarty->assign("particular",$particular);
	$smarty->assign("rate",$rate);
	$smarty->assign("diff",$diff);
	$smarty->assign("diffAmt",$diffAmt);
	$smarty->assign("total",$total);
	$smarty->assign("kg",$kg);
	$smarty->assign("totalAmt",$totalAmt);
	$smarty->assign("typeArrayOutput",$typeArrayOutput);
	$smarty->assign("typeArrayValue",$typeArrayValue);
	$smarty->assign("clientArray",$clientArray);
	$smarty->display("vepari.tpl");
}
?>