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

	$purchId       = isset($_REQUEST['purchId']) && $_REQUEST['purchId'] != '' ? $_REQUEST['purchId'] : 0;
	$partyId       = isset($_REQUEST['partyId']) && $_REQUEST['partyId'] != '' ? $_REQUEST['partyId'] : 0;
	$particular    = isset($_REQUEST['particular']) && $_REQUEST['particular'] != '' ? $_REQUEST['particular'] : "";
	$fine          = isset($_REQUEST['fine']) && $_REQUEST['fine'] != '' ? $_REQUEST['fine'] : 0;
	$purchType 	   = isset($_REQUEST['purchType']) && $_REQUEST['purchType'] != '' ? $_REQUEST['purchType'] : 0;
	$amount        = isset($_REQUEST['amount']) && $_REQUEST['amount'] != '' ? $_REQUEST['amount'] : 0;
	$purchDate     = $_REQUEST['rdtYear']."-".$_REQUEST['rdtMonth']."-".$_REQUEST['rdtDay'];  

    if(isset($_POST['submit']) && $purchId == 0)
	{
	  $insertRojmel = "INSERT INTO parchuran (partyId,purchDate,particular,fine,purchType,amount)
	                    VALUES (".$partyId.",'".$purchDate."','".$particular."','".$fine."','".$purchType."',".$amount.")";
	  $insertRojmelRes = mysql_query($insertRojmel);
	  if(!$insertRojmelRes)
	  {
		echo "Parchuran Insert Fail";
	  }
	  else
	  {
		header("Location:purList.php");
	  }
	}
	   
	
	if($purchId > 0)
	{
	  $select = "SELECT * FROM parchuran
		             WHERE purchId = ".$purchId;
	  $res = mysql_query($select);
	  while($row = mysql_fetch_array($res))
	  {
	    $partyId      = $row['partyId'];
	    $purchDate    = $row['purchDate'];
	    $purchType    = $row['purchType'];
	    $particular   = $row['particular'];
	    $fine         = $row['fine'];
	    $amount       = $row['amount'];
	  }
	}
	
	if(isset($_POST['submit']) && $purchId > 0)
	{
		$updatePur = "UPDATE parchuran
						SET partyId = ".$_REQUEST['partyId'].",
							purchType = '".$_REQUEST['purchType']."',
							particular = '".$_REQUEST['particular']."',
							purchDate = '".$purchDate."',
							fine = '".$_REQUEST['fine']."',
							amount = '".$_REQUEST['amount']."'
						 WHERE purchId = ".$_REQUEST['purchId'];
		$updatePurRes = mysql_query($updatePur);
		if(!$updatePurRes)
		{
			echo "Parchuran Update Fail";
		}
		else
		{
			header("Location:purList.php");
		}
						
	}
	 
	
	$k=0;
	$clientArray = array();
	$selectClient = "SELECT partyId,partyName
	                   FROM party
					  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] = 'J';
	$typeArrayOutput[0] = 'Javak';
	$typeArrayValue[1] = 'A';
	$typeArrayOutput[1] = 'Aavak';
	
	include("./bottom.php");
	$smarty->assign("acc_type",$acc_type);
	$smarty->assign("partyId",$partyId);
	$smarty->assign("purchDate",$purchDate);
	$smarty->assign("purchType",$purchType);
	$smarty->assign("particular",$particular);
	$smarty->assign("fine",$fine);
	$smarty->assign("amount",$amount);
	$smarty->assign("purchId",$purchId);
	$smarty->assign("typeArrayValue",$typeArrayValue);
	$smarty->assign("typeArrayOutput",$typeArrayOutput);
	$smarty->assign("clientArray",$clientArray);
	$smarty->display("parchuran.tpl");
}
?>