<?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;
	$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;
	$totalAmt     = isset($_REQUEST['totalAmt']) && $_REQUEST['totalAmt'] != '' ? $_REQUEST['totalAmt'] : 0;
	$sell_type    = isset($_REQUEST['sell_type']) && $_REQUEST['sell_type'] != '' ? $_REQUEST['sell_type'] : 0;
	$cliTransType = isset($_REQUEST['cliTransType']) && $_REQUEST['cliTransType'] != '' ? $_REQUEST['cliTransType'] : '';
	$cDate        = $_REQUEST['cdtYear']."-".$_REQUEST['cdtMonth']."-".$_REQUEST['cdtDay'];  
	

	if($clientId > 0)
	{
      $selectClient = "SELECT * FROM client
	                     WHERE clientId = ".$clientId;
	  $selectClientRes = mysql_query($selectClient);
	  if($clientRow = mysql_fetch_array($selectClientRes))
	  {
	     $clientId     = $clientRow['clientId'];
	     $partyId      = $clientRow['partyId'];
	     $cDate        = $clientRow['cDate'];
	     $cliTransType = $clientRow['cliTransType'];
	     $particular   = $clientRow['particular'];
	     $rate         = $clientRow['rate'];
	     $kg           = $clientRow['kg'];
	     $totalAmt     = $clientRow['totalAmt'];
	     $sell_type    = $clientRow['sell_type'];
	     $saudaId      = $clientRow['saudaId'];
	  }
	}
	
	if(isset($_REQUEST['submit']) && $clientId > 0)
	{
		$newRate      = '-'.$rate;
		$updateMcx = "UPDATE client
	                 SET partyId     = ".$_REQUEST['partyId'].",
	                  cDate          = '".$cDate."',
	                  cliTransType   = '".$_REQUEST['cliTransType']."',
	                  particular     = '".$_REQUEST['particular']."',
	                  rate           = ".$newRate.",
	                  kg             = ".$_REQUEST['kg'].",
	                  totalAmt       = ".$_REQUEST['totalAmt']."
				  WHERE clientId     = ".$clientId;
	   $updateMcxRes = mysql_query($updateMcx);
	   if($updateMcxRes)
	   {
		 	$updateSauda = "UPDATE sauda
							SET partyId     = ".$_REQUEST['partyId'].",
							saudaDate          = '".$cDate."',
							rojTransType   = '".$_REQUEST['cliTransType']."',
							particular     = '".$_REQUEST['particular']."',
							rate           = ".$newRate.",
							fine           = ".$_REQUEST['kg'].",
							amount         = ".$_REQUEST['totalAmt']."
						WHERE saudaId      = ".$saudaId;
			$updateSaudaRes = mysql_query($updateSauda);
			if(!$updateSaudaRes)
			{
				echo "Sauda Update Fail";
			}
			else
			{
				header("Location:client.php");
			}
		}
	}
     
	$k=0;
	$clientArray = array();
	$selectClient = "SELECT partyId,partyName
	                   FROM party
					WHERE partyType = 'C'
				   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[0] = 'A';
	$typeArrayOutput[0] = 'Sell';
	$typeArrayValue[1] = 'J';
	$typeArrayOutput[1] = 'Buy';
	
	$sellTypeValue[0] = 'D';
	$sellTypOutput[0] = 'Direct';
	$sellTypeValue[1] = 'K';
	$sellTypOutput[1] = 'Khata';
	
	
		
	$cashValue[0] = 'R';
	$cashOutput[0] = 'Rtgs';
	$cashValue[1] = 'C';
	$cashOutput[1] = 'Cash';
	
	include("./bottom.php");
	$smarty->assign("cashOutput",$cashOutput);
	$smarty->assign("cashValue",$cashValue);
	$smarty->assign("sell_type",$sell_type);
	$smarty->assign("sellTypOutput",$sellTypOutput);
	$smarty->assign("sellTypeValue",$sellTypeValue);
	$smarty->assign("clientId",$clientId);
	$smarty->assign("partyId",$partyId);
	$smarty->assign("cDate",$cDate);
	$smarty->assign("cliTransType",$cliTransType);
	$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("client.tpl");
}
?>