<?php 
$form_name = "Incentive Master";
$table_name = "custom_incentive";
$primary_key = "incentive_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'], 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 = 'INCND' 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 = 'IV/'.$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');
			if($pos == (strlen($rec[0]['ref_code'])-1))
			{
				$ref_code = $rec[0]['ref_code'];
				$ref_code = substr($ref_code, 0, -1);
				$pos = strripos($ref_code, '0');
				$id = substr($rec[0]['ref_code'], $pos)+1;
			}
			
			$id = substr($rec[0]['ref_code'], $pos)+1;
			$code = 'IV/'.$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;

			$pos = strripos($rec[0]['ref_code'], '0');
			$id = substr($rec[0]['ref_code'], $pos)+1;
			$code = 'IV/'.$FIN_YR."/";

			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++)
				$code .= '0';
			$code .= $id;
			$vchr_no = $code;*/
		}
		else
			$vchr_no = $_REQUEST['vchr_no'];
        
		$reimburs_id = $_REQUEST['reimburs_id'];
		$reimburs_amt = $_REQUEST['reimburs_amt'];
		$status = $_REQUEST['status'];
		$issue_val = $_REQUEST['issue_val'];
		$paid_val = $_REQUEST['paid_val'];
		
		$last_id = $_REQUEST['partcpnt_id'];
		
		$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 incentive_particpnt_id = '".$_REQUEST['partcpnt_id']."' AND incentive_survy_id = '".$_REQUEST['survey_id']."' AND incentive_cupn_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] != "")
			{
				$ins_vchr_no = $vchr_no;
			}
			else
			{
				$ins_vchr_no = "";
			}
			
			if(count($rec) == 0)
			{
				$_query = sprintf("insert into ".$table_name." values(0, '%s', now(), '%s', '%s', '%s', '%s', '%s', '%s', '%s', 'Y', now(), '%s', 0, 0)",
					mysql_escape_string($ins_vchr_no),
					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($status[$i]),
					mysql_escape_string($issue_val[$i]),
					mysql_escape_string($paid_val[$i]),
					mysql_escape_string(USR)
				);
				$rec = $common->sql->query($_query);
				$last_id = mysql_insert_id();
				
				if($entry)
				{
					$_SESSION[$primary_key] = $last_id;
					$common->UserLog($form_name, $table_name, $last_id, 'Insert');
					$_query = sprintf("insert into custom_vchr_ref values(0, '%s', 'INCND', '%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   incentive_amt       = '".$reimburs_amt[$i]."',
														incentive_vchr_no   = '".$vchr_no."',
														incentive_vchr_dt   = now(),
														incentive_sts       = '".$status[$i]."',
														incentive_issue     = '".$issue_val[$i]."',
														incentive_paid      = '".$paid_val[$i]."',
														update_time         = now(), 
														update_user         = '".USR."'
												  WHERE ".$primary_key."    = '".$rec[0][$primary_key]."'";
				
				if($rec[0]['incentive_issue'] == 0 && $issue_val[$i] == 1)
				{
					$_SESSION[$primary_key] = $rec[0][$primary_key];
					$rec = $common->sql->exeQuery($_query);
					
					if($entry && $rec)
					{
						$common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Update');
						$_query = sprintf("insert into custom_vchr_ref values(0, '%s', 'INCND', '%s')",
							mysql_escape_string($FIN_YR),
							mysql_escape_string($vchr_no)
						);
						$common->sql->query($_query);
						$entry = false;
					}
				}
			}
			// $_SESSION['msg'] = 'Lab Test 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.".incentive_particpnt_id)
                          WHERE incentive_particpnt_id = '".$_REQUEST['id']."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
                
		$id = $_REQUEST['id'];
		$incentive_id = $rec[0]['incentive_id'];
		$incentive_id = $_REQUEST['incentive_id'];
		$vchr_no = $rec[0]['incentive_vchr_no'];
		$vchr_dt = $rec[0]['incentive_vchr_dt'];
		$partcpnt_id = $rec[0]['reg_participant_id'];
		$survey_id = $rec[0]['incentive_survy_id'];
		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 incentive_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'];
		
		$qry = "SELECT * FROM ".$table_name." 
					INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = incentive_survy_id)
					INNER JOIN custom_coupon_master ON (custom_coupon_master.coupn_participant_id = incentive_cupn_id)
				WHERE incentive_particpnt_id = '".$partid."' AND incentive_survy_id = '".$_REQUEST['sur_id']."' AND incentive_issue = '1'";
		$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
		$incentive_id = $_REQUEST['incentive_id'];
		
		if(isset($_SESSION[$primary_key]))
		{
			$incentive_id = $_SESSION[$primary_key];
			unset($_SESSION[$primary_key]);
		}
		else
			$incentive_id = $_REQUEST['incentive_id'];
		
		$query = "SELECT * FROM ".$table_name." WHERE ".$primary_key." = '".$incentive_id."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$inc_rec = $common->sql->record;
		$incentive_id = $inc_rec[0]['incentive_vchr_no'];
		
		$vchr_no = $rec[0]['incentive_vchr_no'];
		$vchr_dt = $rec[0]['incentive_vchr_dt'];
		
		if($incentive_id == "")
		{
			$vchr_no = $rec[0]['incentive_vchr_no'];
			$vchr_dt = $rec[0]['incentive_vchr_dt'];
			$coupn_participant_id = $rec[0]['coupn_participant_id'];
		}
		else
		{
			for($i=0; $i<count($rec); $i++)
			{
				if($incentive_id == $rec[$i]["incentive_vchr_no"])
				{
					$vchr_no = $rec[$i]['incentive_vchr_no'];
					$vchr_dt = $rec[$i]['incentive_vchr_dt'];
					$coupn_participant_id = $rec[$i]['coupn_participant_id'];
				}
			}
		}
		include("print.php");
	break;
	
	default:
		echo '<div class="content_bg">';
			include("add.php");
		echo '</div>';
	break;
}
?>