<?php 
$form_name = "Reimbursement Mapping";
$table_name = "custom_reimbursement_mapping";
$primary_key = "reimbrsmnt_mapping_id";

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
	
include_once("includes/config.php");
include_once("includes/common_functions.php");

$common = new General_Functions();
$css_class = "x-gray";
// $common->SetCssClass($css_class);

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
$act_type = isset($_REQUEST['act_type']) ? $_REQUEST['act_type'] : "";
$cancel_link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
$action = $_SERVER['SCRIPT_NAME']."?page=".$_REQUEST['page']."&mode=submit";

switch($mode)
{
	case "submit":
		$remimbst_mstr_id = $_REQUEST['remimbst_mstr_id'];
		$reimbrsmnt_type = $_REQUEST['reimbrsmnt_type'];
		$reimbrsmnt_type_val = $_REQUEST['reimbrsmnt_type_val'];
		$reimbrsmnt_amt = $_REQUEST['reimbrsmnt_amt'];
		
		if($_REQUEST['id'] != "")
		{
			$qry = "DELETE FROM ".$table_name." WHERE reimbrsmnt_survey_id = '".$_REQUEST['sur_mstr']."'";
			$rec = $common->sql->exeQuery($qry);
		}
		
		for($i=0; $i<count($remimbst_mstr_id); $i++)
		{
			$qry = "SELECT * FROM ".$table_name." WHERE reimbrsmnt_survey_id = '".$_REQUEST['sur_mstr']."' AND reimbrsmnt_head_mstr_id = '".$remimbst_mstr_id[$i]."' AND reimbrsmnt_type = '".$reimbrsmnt_type[$i]."'";
			$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			
			if(count($rec) == 0)
			{
				$_query = sprintf("insert into ".$table_name." values(0, '%s', '%s', '%s', '%s', 'Y', now(), '%s', 0, 0)",
					mysql_escape_string($_REQUEST['sur_mstr']),
					mysql_escape_string($remimbst_mstr_id[$i]),
					mysql_escape_string($reimbrsmnt_type_val[$i]),
					mysql_escape_string($reimbrsmnt_amt[$i]),
					mysql_escape_string(USR)
				);
				$exe_rec = $common->sql->query($_query);
				$last_id = mysql_insert_id();
				// $_SESSION['msg'] = 'Reimbursement Mapping has been saved Successfully';
			}
			else
			{
				$_SESSION['msg'] = 'Selected Reimbursement Heads are Already Exsists for this Survey';
			}
		}
		
		if($exe_rec)
		{
			if($_REQUEST['id'] != "")
			{
				$_SESSION['msg'] = 'Reimbursement Mapping has been updated Successfully';
				$common->UserLog($form_name, $table_name, $_REQUEST['sur_mstr'], 'Update');
			}
			else
			{
				$_SESSION['msg'] = 'Reimbursement Mapping has been saved Successfully';
				$common->UserLog($form_name, $table_name, $_REQUEST['sur_mstr'], 'Insert');
			}
		}
		
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		header("location:".$link);
		exit;
	break;
	
	case "edit":
		$query = "Select * FROM ".$table_name." WHERE reimbrsmnt_survey_id = '".$_REQUEST['id']."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
                
                $id = $_REQUEST['id'];
                $sur_mstr = $rec[0]['reimbrsmnt_survey_id'];
		include("edit.php");
	break;
        
        case "delete":
                if(isset($_REQUEST[$prefix.'id']))
                        $id = $_REQUEST[$prefix.'id'];
                else
                        $id = implode("', '", $_REQUEST[$primary_key]);

                $query = "DELETE FROM ".$table_name." WHERE reimbrsmnt_survey_id IN ('".$id."')";

                $rec = $common->sql->exeQuery($query);

                if($rec)
                {
                        $_SESSION['msg'] = 'Selected record has been deleted successfully';
                        if(isset($_REQUEST['id']))
                                $common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Delete');
                        else
                                for($i=0; $i<count($_REQUEST[$primary_key]); $i++)
                                        $common->UserLog($form_name, $table_name, $_REQUEST[$primary_key][$i], 'Delete');
                }

                $query_string = str_replace("&mode=delete","",$_SERVER['QUERY_STRING']);
                $link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
                header("location:".$link);
        break;
	
	default:
		echo '<div class="content_bg">';
			include("add.php");
		echo '</div>';
	break;
}
?>