<?php 
$form_name = "Reimbursement Master";
$table_name = "custom_reimbursement";
$primary_key = "reimbrsmnt_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":
		if($_REQUEST['id'] == "" || in_array("1", $_REQUEST['issue_val'], true))
		{
			$query = "SELECT * FROM custom_app_settings WHERE app_survey_id = '".$_REQUEST['survey_id']."'";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			$FIN_YR = $rec[0]['app_fin_yr'];
			
			$query = "SELECT * FROM custom_vchr_ref WHERE ref_financial_year = '".$FIN_YR."' AND ref_for = 'RIMB' ORDER BY id DESC";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			
			$ref_code = split("/", $rec[0]['ref_code']);
			$id = substr($rec[0]['ref_code'], $pos)+1;
			$code = 'RV/'.$FIN_YR."/";
			$vno = '';
			$id = $ref_code[2]+1;

			if(count($rec) == 0)
				$pos = 4;
			else
				$pos = 5-(strlen($id));
			
			for($i=0; $i<$pos; $i++)
				$zero .= '0';
			$ref_id = $zero.$id;
			$vno = $zero.$id;
			
			if($len > 5)
			{
				$len_pos = $len - 5;
				$vno = substr($vno, $len_pos);
				$vchr_no = $code.$vno;
			}
			else
				$vchr_no = $code.$vno;
			
			/*$pos = strripos($rec[0]['ref_code'], '0');
			$id = substr($rec[0]['ref_code'], $pos)+1;
			$code = 'RV/'.$FIN_YR."/";
			$vno = '';

			if(count($rec) == 0)
				$pos = 3;
			else
			{
				$ref_code = substr($rec[0]['ref_code'],1,strpos($rec[0]['ref_code'],"/")-1);
				$exp = split("/", $rec[0]['ref_code']);
				$ref_code = $exp[2];
				$pos = strripos($ref_code, '0');
			}

			for($i=0; $i<=$pos; $i++)
				$vno .= '0';
			$vno .= $id;
			$len = strlen($vno);
			
			if($len > 5)
			{
				$len_pos = $len - 5;
				$vno = substr($vno, $len_pos);
				$vchr_no = $code.$vno;
			}
			else
				$vchr_no = $code.$vno;*/
		}
		else
			$vchr_no = $_REQUEST['vchr_no'];
        
		$reimburs_id = $_REQUEST['reimburs_id'];
		$reimburs_amt = $_REQUEST['reimburs_amt'];
		$reimburs_tot_amt = array_sum($_REQUEST['reimburs_amt']);
		$last_id = $_REQUEST['partcpnt_id'];
		$issue_val = $_REQUEST['issue_val'];
		
		$qry = "SELECT * FROM custom_participant_registration WHERE reg_participant_id = '".$_REQUEST['partcpnt_id']."'";
		$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
		// $_REQUEST['partcpnt_id'] = $rec[0]['reg_id'];
		$entry = true;
        
		for($i=0; $i<count($reimburs_id); $i++)
		{
			$qry = "SELECT * FROM ".$table_name." WHERE reimbrsmnt_particpnt_id = '".$_REQUEST['partcpnt_id']."' AND reimbrsmnt_survy_id = '".$_REQUEST['survey_id']."' AND reimbrsmnt_head_id = '".$reimburs_id[$i]."'";
			$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			
			if(($issue_val[$i] == "1" || $issue_val[$i] == "") && $reimburs_amt[$i] != "")
			{
				$issue_val_sts = "Y";
				$ins_vchr_no = $vchr_no;
				$ins_vchr_dt = "now()";
			}
			else
			{
				$issue_val_sts = "N";
				$ins_vchr_no = "";
				$ins_vchr_dt = "''";
			}
			
			if(count($rec) == 0)
			{
				$_query = sprintf("insert into ".$table_name." values(0, '%s', %s, '%s', '%s', '%s', '%s', '%s', 'Y', now(), '%s', 0, 0)",
					mysql_escape_string($ins_vchr_no),
					$ins_vchr_dt,
					mysql_escape_string($_REQUEST['partcpnt_id']),
					mysql_escape_string($_REQUEST['survey_id']),
					mysql_escape_string($reimburs_id[$i]),
					mysql_escape_string($reimburs_amt[$i]),
					mysql_escape_string($issue_val_sts),
					mysql_escape_string(USR)
				);
				$rec = $common->sql->query($_query);
				$last_id = mysql_insert_id();
				
				if($issue_val_sts == "Y")
					$_SESSION[$primary_key] = $last_id;
				
				if($entry)
				{
					/*$vchr_no = substr($_REQUEST['vchr_no'],1,strpos($_REQUEST['vchr_no'],"/")-1);
					$exp = split("/", $_REQUEST['vchr_no']);
					$vchr_no = $exp[2];*/
					
					$common->UserLog($form_name, $table_name, $last_id, 'Insert');
					
					$_query = sprintf("insert into custom_vchr_ref values(0, '%s', 'RIMB', '%s')",
							mysql_escape_string($FIN_YR),
							mysql_escape_string($vchr_no)
					);
					if($rec)
					{
						$common->sql->query($_query);
						$entry = false;
					}
				}
			}
			else
			{
				$_query = "UPDATE ".$table_name."	SET		reimbrsmnt_amt      	= '".$reimburs_amt[$i]."',
															reimbrsmnt_vchr_dt  	= now(),
															reimbrsmnt_vchr_no  	= '".$ins_vchr_no."',
															reimbrsmnt_pay_status  	= '".$issue_val_sts."',
															update_time         	= now(), 
															update_user         	= '".USR."'
													WHERE 	".$primary_key."    	= '".$rec[0][$primary_key]."'";
				
				if($issue_val_sts == "Y")
					$_SESSION[$primary_key] = $rec[0][$primary_key];
				
				if($rec[0]['reimbrsmnt_pay_status'] == "N" && $rec[0]['reimbrsmnt_amt'] != "0.00" && $issue_val_sts == "Y")
				{
					$rec = $common->sql->exeQuery($_query);
					$common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Update');
					
					if($entry)
					{
						/*$vchr_no = substr($_REQUEST['vchr_no'],1,strpos($_REQUEST['vchr_no'],"/")-1);
						$exp = split("/", $_REQUEST['vchr_no']);
						$vchr_no = $exp[2];*/
						
						$_query = sprintf("insert into custom_vchr_ref values(0, '%s', 'RIMB', '%s')",
								mysql_escape_string($FIN_YR),
								mysql_escape_string($vchr_no)
						);
						if($rec)
						{
							$common->sql->query($_query);
							$entry = false;
						}
					}
				}
			}
			//$_SESSION['msg'] = 'Reimbursement has been saved Successfully';
		}
		
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		header("location:".$link);
		exit;
	break;
	
	case "edit":
	case "edit_print":
		$query = "Select * FROM ".$table_name." 
					INNER JOIN custom_participant_registration 
						ON (custom_participant_registration.reg_participant_id = ".$table_name.".reimbrsmnt_particpnt_id)
				  WHERE reimbrsmnt_particpnt_id = '".$_REQUEST['id']."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
		
		$id = $_REQUEST['id'];
		$rec_id = $_REQUEST['rec_id'];
		$vchr_no = $rec[0]['reimbrsmnt_vchr_no'];
		$partcpnt_id = $rec[0]['reg_participant_id'];
		$survey_id = $rec[0]['reimbrsmnt_survy_id'];
		$vchr_dt = $rec[0]['reimbrsmnt_vchr_dt'];
		include($mode.".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_particpnt_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;
	
	case 'print':
		$query = "SELECT * FROM custom_participant_registration WHERE reg_participant_id = '".$_REQUEST['id']."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$reg = $common->sql->record;
		$partid = $reg[0]['reg_id'];
		$partid = $_REQUEST['id'];
		$rec_id = isset($_REQUEST['rec_id']) ? $_REQUEST['rec_id'] : "";
		$frm_actn = isset($_REQUEST['frm_actn']) ? $_REQUEST['frm_actn'] : "";
		
		if($frm_actn == "add")
		{
			$rec_id = $_SESSION[$primary_key];
			unset($_SESSION[$primary_key]);
		}
		
		$qry = "SELECT ".$table_name.".".$primary_key.", ".$table_name.".reimbrsmnt_head_id, reimbrsmnt_head_name, reimbrsmnt_amt, sur_title, sur_id, 
                            reimbrsmnt_vchr_no, reimbrsmnt_vchr_dt
                                FROM ".$table_name." 
					INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = reimbrsmnt_survy_id)
					LEFT JOIN custom_reimbursement_heads_master 
                                            ON (custom_reimbursement_heads_master.reimbrsmnt_head_id = ".$table_name.".reimbrsmnt_head_id)
				WHERE reimbrsmnt_particpnt_id = '".$partid."' AND reimbrsmnt_pay_status = 'Y' AND reimbrsmnt_survy_id = '".$_REQUEST['sur_id']."'";
		$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
		
		if($rec_id == "")
		{
			$vchr_no = $rec[0]['reimbrsmnt_vchr_no'];
			$vchr_dt = $rec[0]['reimbrsmnt_vchr_dt'];
		}
		else
		{
			for($i=0; $i<count($rec); $i++)
			{
				if($rec_id == $rec[$i][$primary_key])
				{
					$vchr_no = $rec[$i]['reimbrsmnt_vchr_no'];
					$vchr_dt = $rec[$i]['reimbrsmnt_vchr_dt'];
				}
			}
		}
		include("print.php");
	break;
	
	default:
		echo '<div class="content_bg">';
			include("add.php");
		echo '</div>';
	break;
}
?>