<?php 
include 'session.php';

extract($_REQUEST);
$act=$action;


switch($act)
{
	case 'insert':
	
	if(!empty($f_end_date) && !empty($f_start_date)) {
		$strChk = "select count(finance_id) from m_financial_year where f_start_date = '".$f_start_date."' and f_end_date = '".$f_end_date."' and disp_status != '2' ";		
 		$reslt = $db->get_a_line($strChk);
		if($reslt[0] == 0) {			

			$str="insert into m_financial_year(f_start_date, f_end_date, IsActive, disp_status, createddate)values('".$f_start_date."', '".getRealescape($f_end_date)."', '0', '1',  NOW() )";
			
			$rslt = $db->insert($str);
			
			$log = $db->insert_log("insert","m_financial_year","","Financial Year Added Newly","financial",$str);
					
			
			echo json_encode(array("rslt"=>"1")); //success
		}
		else {
			 echo json_encode(array("rslt"=>"3")); //same exists
		}
	}
	else {
		echo json_encode(array("rslt"=>"4"));  //no values
	}
	
	break;
	
	
	case 'update':	 	
	//$edit_id		
	if(!empty($f_end_date) && !empty($f_start_date)) {
		$strChk = "select count(financial_id) from m_financial_year where f_start_date = '".$f_start_date."' and f_end_date = '".$f_end_date."' and financial_id != '".$edit_id."' and disp_status != '2' ";			
 		$reslt = $db->get_a_line($strChk);
		if($reslt[0] == 0) {
			$str = "update m_financial_year set f_start_date= '".$f_start_date."', f_end_date= '".getRealescape($f_end_date)."'  where financial_id = '".$edit_id."'  ";
							
				
			$db->insert($str);
			$db->insert_log("update","m_financial_year",$edit_id,"Financial Year Details Update","financial",$str);

			echo json_encode(array("rslt"=>"2"));
		}
		else {
			echo json_encode(array("rslt"=>"3")); //same exists
		}
	}
	else {
		echo json_encode(array("rslt"=>"4"));  //no values
	}
		
	break;
	
	case 'del':
	  $edit_id = base64_decode($Id);
	  
	  $chk_ref = check_reference($db, $edit_id);
	  
	  if($chk_ref == 1){
		   $str="update m_financial_year set disp_status = '2'  where financial_id = '".$edit_id."'";
		  $db->insert($str); 	 
		  
		  $db->insert_log("delete","m_financial_year",$edit_id,"Financial Year deleted","financial",$str);
		  echo json_encode(array("rslt"=>"5")); //deletion
	  }
	  else{
		 echo json_encode(array("rslt"=>"-1", "err_msg"=>"Reference exists with other data. Cannot Perform the action.")); //deletion  
	  }	 		
	break;
	
	case 'changestatus':
	  $edit_id = base64_decode($Id);
	  $status = $actval;
	  
	  $chk_ref = check_reference($db, $edit_id);
	  
	  if($chk_ref == 1){
			$str="update m_financial_year set IsActive = '0'";
			$str1="update m_financial_year set IsActive = '".$status."'  where financial_id = '".$edit_id."'";
			$db->insert($str); 
			$db->insert($str1); 			
		  
			$db->insert_log("changestatus","m_financial_year",$edit_id,"Financial Year status changed","financial",$str);
			echo json_encode(array("rslt"=>"6")); //status update
	  }
	  else{
			echo json_encode(array("rslt"=>"-1", "err_msg"=>"Reference exists with other data. Cannot Perform the action.")); //deletion  
	  }	 	
	  	 
		
	break;
	
	case 'changedispstatus':
	  $edit_id = base64_decode($Id);
	  $status = $actval;
	  
	  $chk_ref = check_reference($db, $edit_id);
	  
	  if($chk_ref == 1){
			$str="update m_financial_year set disp_status = '".$status."'  where financial_id = '".$edit_id."'";
			$db->insert($str); 	 
		  
			$db->insert_log("changestatus","m_financial_year",$edit_id,"Financial Year status changed","financial",$str);
			echo json_encode(array("rslt"=>"6")); //status update
	  }
	  else{
			echo json_encode(array("rslt"=>"-1", "err_msg"=>"Reference exists with other data. Cannot Perform the action.")); //deletion  
	  }	 	
	  	 
		
	break;
}


function check_reference($db, $financial_id){
	return 1;
}


?>