<?php 
$form_name = "Lab Master";
$table_name = "custom_lab_samples";
$primary_key = "lab_smple_id";

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

//require_once(PEAR.'/tcpdf/config/lang/eng.php');
//require_once(PEAR.'/tcpdf/tcpdf.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":
		/*echo "<pre>";
		print_r($_REQUEST);
		exit;*/
		$lab_mstr = $_REQUEST['lab_mstr'];
		$result_id = $_REQUEST['result_id'];
		$location = $_REQUEST['location'];
		$spcmn_mstr_id = $_REQUEST['spcmn_id'];
		$spcmn_type_hdn = $_REQUEST['spcmn_type_hdn'];
		$spcmn_type = $_REQUEST['spcmn_type'];
		$accs_no = $_REQUEST['accs_no_txt'];
		$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'];
		
		/*$acs_chk_qry = "SELECT * FROM ".$table_name." WHERE lab_smple_acc_no = '".$accs_no."'";
		$common->sql->query($acs_chk_qry, SQL_ALL, SQL_ASSOC);
		$chk_rec = $common->sql->record;
		
		if(count($chk_rec) == 0)
		{*/
			$query = "SELECT * FROM custom_lab_ref WHERE lab_ref_loc = '".$location[0]."' ORDER BY lab_ref_id DESC";
			$common->sql->query($query, SQL_ALL, SQL_ASSOC);
			$ref_rec = $common->sql->record;
			
			$ref_code = $ref_rec[0]['lab_ref_code'];
			$id = ($ref_code+1);
			$code = $location[0].'-LA-';

			if(count($ref_rec) == 0)
				$pos = 4;
			else
				$pos = 5-(strlen($id));
				// $pos = strripos($ref_code, '0');
			
			for($i=0; $i<=$pos; $i++)
				$zero .= '0';
			$ref_id = $zero.$id;
			$vno = $zero.$id;
			/*$code .= $vno;
			$accs_no_rec = $code;*/
			
			$len = strlen($vno);
			
			if($len > 5)
			{
				$len_pos = $len - 5;
				$vno = substr($vno, $len_pos);
				$ref_id = $vno;
				$accs_no_rec = $code.$vno;
			}
			else
				$accs_no_rec = $code.$vno;
			
			$last_id = "";
			for($i=0; $i<count($lab_mstr); $i++)
			{
				$qry = "SELECT * FROM ".$table_name." WHERE lab_smple_part_id = '".$_REQUEST['partcpnt_id']."' AND lab_smple_sur_id = '".$_REQUEST['survey_id']."' AND lab_smple_tst_id = '".$lab_mstr[$i]."' AND lab_smple_spcmn_id = '".$spcmn_mstr_id[$i]."'";
				$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
				$rec = $common->sql->record;
				
				$is_chk = isset($spcmn_type[$lab_mstr[$i]][$spcmn_mstr_id[$i]]) ? 1 : 0;
				
				if($is_chk == 0)
				{
					if(isset($spcmn_type_hdn[$i]) && $spcmn_type_hdn[$i] == 1)
						$is_chk = $spcmn_type_hdn[$i];
				}
				
				if($is_chk == 1)
					$accs_no = $accs_no_rec;
				else
					$accs_no = '';
				
				/*$acs_chk_qry = "SELECT * FROM ".$table_name." WHERE lab_smple_acc_no = '".$accs_no."'".$chk_whr;
				$common->sql->query($acs_chk_qry, SQL_ALL, SQL_ASSOC);
				$chk_rec = $common->sql->record;*/
				
				if(count($rec) == 0)
				{
					$_query = sprintf("insert into ".$table_name." values(0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), 'Y', now(), '%s', 0, 0)",
						mysql_escape_string($_REQUEST['partcpnt_id']),
						mysql_escape_string($_REQUEST['survey_id']),
						mysql_escape_string($accs_no),
						mysql_escape_string($lab_mstr[$i]),
						mysql_escape_string($spcmn_mstr_id[$i]),
						mysql_escape_string($is_chk),
						mysql_escape_string($result_id[$i]),
						mysql_escape_string(USR)
					);
					$add_rec = $common->sql->query($_query);
					
					$add_rec = true;
					if($last_id == "")
						$last_id = mysql_insert_id();
				}
				else
				{
					//if($rec[0]['lab_smple_spcmn_clctd'] == "0")
					if($rec[0]['lab_smple_spcmn_clctd'] == 0) // changed by Vidhya - 20-10-2017
					{
						$_query = "UPDATE ".$table_name." SET lab_smple_acc_no = '".$accs_no."', lab_smple_spcmn_clctd = '".$is_chk."', lab_smple_result_id = '".$result_id[$i]."' WHERE lab_smple_id = '".$rec[0]['lab_smple_id']."'";
						$add_rec = $common->sql->exeQuery($_query);
					}
					//else if($rec[0]['lab_smple_spcmn_clctd'] == "1" && $rec[0]['lab_smple_result_id'] == "RES0")
					else if($rec[0]['lab_smple_spcmn_clctd'] == 1 && $rec[0]['lab_smple_result_id'] == 9) // changed by Vidhya - 20-10-2017
					{
						$_query = "UPDATE ".$table_name." SET lab_smple_result_id = '".$result_id[$i]."' WHERE lab_smple_id = '".$rec[0]['lab_smple_id']."'";
						$add_rec = $common->sql->exeQuery($_query);
					}
				}
				
				if($_REQUEST['updt_vchr'] == "true")
					$_SESSION['msg'] = 'Lab Test has been Saved Successfully with the Accession Number is '.$accs_no_rec;
				else
					$_SESSION['msg'] = 'Lab Test has been Updated Successfully';
			}
			
			if($add_rec)
			{
				if($_REQUEST['updt_vchr'] == "true")
				{
					if(count($ref_rec) == 0)
					{
						$_query = sprintf("insert into custom_lab_ref values(0, '%s', '%s')",
								mysql_escape_string($location[0]),
								mysql_escape_string($ref_id)
						);
						$common->sql->query($_query);
					}
					else
					{
						$_query = "UPDATE custom_lab_ref SET lab_ref_code = '".$ref_id."' WHERE lab_ref_loc = '".$location[0]."'";
						$common->sql->exeQuery($_query);
					}
				}
			}
			$common->UserLog($form_name, $table_name, $last_id, 'Insert');
		/*}
		else
		{
			$_SESSION['msg'] = 'Accession Number Already Exsists';
		}*/
		
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		header("location:".$link);
		exit;
	break;
	
	case "edit":
		$query = "Select * FROM ".$table_name." 
                            INNER JOIN custom_participant_registration 
                                ON (custom_participant_registration.reg_participant_id = ".$table_name.".lab_smple_part_id)
                          WHERE lab_smple_part_id = '".$_REQUEST['id']."' AND lab_smple_sur_id = '".$_REQUEST['surid']."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
		
		$id = $_REQUEST['id'];
		$partcpnt_id = $rec[0]['reg_participant_id'];
		$survey_id = $rec[0]['lab_smple_sur_id'];
		$accs_no = $rec[0]['lab_smple_acc_no'];
		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 lab_smple_part_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;
}
?>