<?php 
include 'session.php';

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


switch($act)
{
	case 'insert':
	
	if(!empty($txt_bank_name)) {
		$strChk = "select count(bank_id) from m_banks where bank_name = '".$txt_bank_name."' and company_id='".$sel_company."' and IsActive != '2' ";		
 		$reslt = $db->get_a_line($strChk);
		if($reslt[0] == 0) {			

			$str="insert into m_banks(company_id, bank_name, account_no, branch_name, ifsc_code, bank_cus_id, bank_debit_card_no, authorized_name, IsActive, UserId, createddate)values('".$sel_company."', '".getRealescape($txt_bank_name)."', '".getRealescape($txt_acc_no)."', '".getRealescape($txt_branch)."', '".getRealescape($txt_ifsc_code)."', '".getRealescape($txt_bank_cus_id)."', '".getRealescape($txt_debit_card_no)."', '".getRealescape($txt_auth_name)."', '1', '".$_SESSION["UserId"]."',  NOW() )";
			
			$rslt = $db->insert($str);
			
			$log = $db->insert_log("insert","m_banks","","Bank Added Newly","bank",$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($txt_bank_name)) {
		$strChk = "select count(bank_id) from m_banks where bank_name = '".$txt_bank_name."' and company_id='".$sel_company."' and IsActive != '2' and bank_id != '".$edit_id."'  ";			
 		$reslt = $db->get_a_line($strChk);
		if($reslt[0] == 0) {
			$str = "update m_banks set company_id= '".$sel_company."', bank_name= '".getRealescape($txt_bank_name)."', account_no= '".getRealescape($txt_acc_no)."', branch_name= '".getRealescape($txt_branch)."', ifsc_code= '".getRealescape($txt_ifsc_code)."', bank_cus_id= '".getRealescape($txt_bank_cus_id)."', bank_debit_card_no= '".getRealescape($txt_debit_card_no)."', authorized_name= '".getRealescape($txt_auth_name)."',  UserId='".$_SESSION["UserId"]."'  where bank_id = '".$edit_id."'  ";
							
				
			$db->insert($str);
			$db->insert_log("update","m_banks",$edit_id,"Bank Details Update","bank",$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_banks set IsActive = '2', UserId='".$_SESSION["UserId"]."'  where bank_id = '".$edit_id."'";
		  $db->insert($str); 	 
		  
		  $db->insert_log("delete","m_banks",$edit_id,"Bank deleted","Bank",$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_banks set IsActive = '".$status."', UserId='".$_SESSION["UserId"]."'  where bank_id = '".$edit_id."'";
			$db->insert($str); 	 
		  
			$db->insert_log("changestatus","m_banks",$edit_id,"Bank status changed","Bank",$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, $bank_id){
	return 1;
}


?>