<?php
include("../db_con.php");

//$rules = array("Application List Actions","Reciept List","Document type list","Visatype List","Application Status List","Document Position List","Update Position status","Update Payment status","Create New Report","View Report List");
$rules = array("List action","Update payment status","Update application status","New application","Application list","Receipt list","Main visa category","Visa category","Document tracker list","Document position list","Update doc position","General report","Admin report","Document report","Embassy report","Scanning report");

function isauth_have($chkrule){
    
    if(strpos( $_SESSION['rules'], $chkrule) !== false ) {
        
        return true;
    }
    else header('location:adduser.php'); 
}

function ismenuauth($chkrule){
    
    if(strpos($_SESSION['rules'], $chkrule) !== false ) {
        return true;
        //header('location:adduser.php');
    }
    else return false;
}

function get_session_user_name($uid,$con){

	$rs = mysqli_query($con,"select name from adminusers where u_id='$uid'");
    $row=mysqli_fetch_array($rs);
    return $row["name"];
}

function get_session_user_fullname($con){

	$rs = mysqli_query($con,"select name from adminusers where u_name='$_SESSION[UNAME]'");
    $row=mysqli_fetch_array($rs);
    return $row["name"];
}

function get_session_user_id($con){

	$rs = mysqli_query($con,"select u_id from adminusers where u_name='$_SESSION[UNAME]'");
	//echo $con; die();
    $row=mysqli_fetch_array($rs);
    return $row["u_id"];
}

function ver_update_history($notes,$appid,$con){

	$dt = new DateTime('now', new DateTimezone('Asia/Dhaka'));
	$now=$dt->format('Y-m-d H:i:s');
	$adminuserid=get_session_user_id($con);
	$sql = "INSERT INTO `app_his_table` (`vh_id`, `vh_notes`, `vh_datetime`, `app_id`, `user_id`) VALUES (NULL, '$notes', '$now', '$appid', '$adminuserid');";
	@mysqli_query($con,$sql);

}

function get_app_status_name($sid,$con){

	$result = mysqli_query($con,"select vstatus_name from app_status_table where vstatus_id= '$sid'");
	$row1 = mysqli_fetch_array($result);
	return $row1["vstatus_name"];
}

function get_position_name($sid,$con){

	$result = mysqli_query($con,"select docp_name from doc_position_table where docp_id= '$sid'");
	$row1 = mysqli_fetch_array($result);
	return $row1["docp_name"];
}

function get_purpose_name($sid,$con){

	$result = mysqli_query($con,"select purpose_name from purpose_table where pur_id= '$sid'");
	$row1 = mysqli_fetch_array($result);
	return $row1["purpose_name"];
}
function get_deliverycenter_name($sid,$con){

	$result = mysqli_query($con,"select deliloc_name from deli_location_table where deliloc_id= '$sid'");
	$row1 = mysqli_fetch_array($result);
	return $row1["deliloc_name"];
}
function get_deliverymode_name($sid,$con){

	$result = mysqli_query($con,"select delitype_name, delitype_kword from deli_type_table where deli_typeid= '$sid'");
	$row1 = mysqli_fetch_array($result);
	return $row1["delitype_name"]." (".$row1["delitype_kword"].")";
}
function get_user_deliverycenter_id($sid,$con){

	$result = mysqli_query($con,"select delcenter from adminusers where u_id= '$sid'");
	$row1 = mysqli_fetch_array($result);
	return $row1["delcenter"];
}
function get_area_name($sid,$con){

	$result = mysqli_query($con,"select area_name from area_table where area_id= '$sid'");
	$row1 = mysqli_fetch_array($result);
	return $row1["area_name"];
}
// Create the Function to get the string
 function GetStringBetween ($string, $start, $finish) {
    $string = " ".$string;
    $position = strpos($string, $start);
    if ($position == 0) return "";
    $position += strlen($start);
    $length = strpos($string, $finish, $position) - $position;
    return substr($string, $position, $length);
}

function text_position($fulltext, $searchtxt, $increment){

	$html = $fulltext;
	$needle = $searchtxt;
	$lastPos = 0;
	$positions = array();

	while (($lastPos = strpos($html, $needle, $lastPos))!== false) {
	    $positions[] = $lastPos;
	    $lastPos = $lastPos + strlen($needle);
	}

	

	// Displays 3 and 10
	foreach ($positions as $value) {
	    $myval= substr($fulltext, $value+$increment);
	    $ids[] = strtok($myval, " ");
		//echo strtok($myval, " ")."<br>";
		//								    echo $value ."<br />";
	}
	return $ids;
}

function appoint_update_history($notes,$appid,$con){

	$dt = new DateTime('now', new DateTimezone('Asia/Dhaka'));
	$now=$dt->format('Y-m-d H:i:s');
	$adminuserid=get_session_user_id($con);
	$sql = "INSERT INTO `ver_his_table` (`vh_id`, `vh_notes`, `vh_datetime`, `app_id`, `user_id`) VALUES (NULL, '$notes', '$now', '$appid', '$adminuserid');";
	@mysqli_query($con,$sql);

}

function get_appoint_status_name($sid,$con){

	$result = mysqli_query($con,"select aptstatus_name from appoint_status_table where aptstatus_id= '$sid'");
	$row1 = mysqli_fetch_array($result);
	return $row1["aptstatus_name"];
}

$timelist = array("1000"=>"10:00", "1030"=>"10:30", "1100"=>"11:00", "1130"=>"11:30");

//////////////////////////

function get_delivery_location_kword($did,$con){

	$result = mysqli_query($con,"select deliloc_kword from deli_location_table where deliloc_id= '$did'");
	$row1 = mysqli_fetch_array($result);
	return $row1["deliloc_kword"];
}

function get_parent_id($did,$con){

	$result = mysqli_query($con,"select parent from applicant_table where app_id= '$did'");
	$row1 = mysqli_fetch_array($result);
	if($row1["parent"]==0) return $did;
	else return $row1["parent"];
}

function get_parent_paymode($did,$con){

	$parent_appid=get_parent_id($did,$con);
	$result = mysqli_query($con,"select paymode from applicant_table where app_id= '$parent_appid'");
	$row1 = mysqli_fetch_array($result);
	//if($row1["parent"]==0) return $did;
	return $row1["paymode"];
}

function get_visatype_name($did,$con){

	$result = mysqli_query($con,"select visatype_name from visatype_table where visatype_id= '$did'");
	$row1 = mysqli_fetch_array($result);
	return $row1["visatype_name"];
}

function get_mainvisatype_name($did,$con){

	$result = mysqli_query($con,"select mvisatype_name from main_visatype_table where mvisatype_id= '$did'");
	$row1 = mysqli_fetch_array($result);
	return $row1["mvisatype_name"];
}

function get_parent_mainvisatype_name($did,$con){

	$result = mysqli_query($con,"select mvisatype_name from main_visatype_table, visatype_table where mvisatype_id=visa_cat AND visatype_id='$did'");
	$row1 = mysqli_fetch_array($result);
	return $row1["mvisatype_name"];
}

function get_visa_fee($did,$con){

	$result = mysqli_query($con,"select visatype_fee from visatype_table where visatype_id= '$did'");
	$row1 = mysqli_fetch_array($result);
	return $row1["visatype_fee"];
}

function get_service_fee($con){

	$result = mysqli_query($con,"select value from setting_table where s_code= 'scharge'");
	$row1 = mysqli_fetch_array($result);
	return $row1["value"];
}

function get_bank_fee($con){

	$result = mysqli_query($con,"select value from setting_table where s_code= 'bcharge'");
	$row1 = mysqli_fetch_array($result);
	return $row1["value"];
}

function get_location_dates_list($con, $act){

	echo "<div class='row filterbox'>
				<form class='form-inline col-md-12' action='".$act."' method='post'>
				
				<div class='col-md-3'>
					<p><b>Select Delivery Location :</b></p> 
					<select name='location' class='form-control' required>
					  	<option value=''> - Select -</option>
					  	<option value='0'> All </option>";
						
		                    $urs = mysqli_query($con,"select deliloc_id, deliloc_name from deli_location_table order by deliloc_name ASC");
		            		while($urow=mysqli_fetch_array($urs)){ 
		                
		                 	  	echo "<option value=".$urow["deliloc_id"]."> ".$urow["deliloc_name"]." </option>";					  	
							} 
					
				echo "</select>						
					
				</div>
				
				<div class='col-md-8'>

					<p><b>Select Date Range:</b></p>											
						<div class='input-group input-large date-picker input-daterange' style='float: left;' data-date='10/11/2012' data-date-format='yyyy-mm-dd'>
	                        <input type='text' class='form-control' name='date1'>
	                        <span class='input-group-addon'> to </span>
	                        <input type='text' class='form-control' name='date2'>					                           
	                    </div>
	                    <div class='col-md-2'>
	                    	 <button class='btn green'> Search </button>
	                    </div>						

				</div>

				
				</form>
			</div>";								

}
function get_location_daterange($con, $act){

	echo "<div class='row filterbox'>
				<form class='form-inline col-md-12' action='".$act."' method='post'>
				
				<div class='col-md-3'>
					<p><b>Select Delivery Location :</b></p> 
					<select name='location' class='form-control' required>
					  	<option value=''> - Select -</option>
					  	<option value='0'> All </option>";
						
		                    $urs = mysqli_query($con,"select deliloc_id, deliloc_name from deli_location_table order by deliloc_name ASC");
		            		while($urow=mysqli_fetch_array($urs)){ 
		                
		                 	  	echo "<option value=".$urow["deliloc_id"]."> ".$urow["deliloc_name"]." </option>";					  	
							} 
					
		 echo "</select>						
					
				</div>
				
				<div class='col-md-8'>

					<p><b>Select Date Range:</b></p>
					<input type='text' class='form-control' id='reportrange' name='dates' style='float: left; width: 30%;'>
						
	                    <div class='col-md-2'>
	                    	 <button class='btn green'> Search </button>
	                    </div>						

				</div>

				
				</form>
			</div>";
		echo "<script type='text/javascript' src='moment.js'></script>
      <script type='text/javascript' src='daterangepicker.js'></script>";
}
?>