<?php
include('accesscheck2.php'); 
require_once('Connections/courier.php');
require_once('functions/functions.php');

$action = $_POST['act'];
$id = $_POST['id'];

if($action=='guestvisitno'){
$client = 'Guest';	
}

$PROVIDERID=$_SESSION['provider_id'];
$user_id=$_SESSION['myMM_Userid'];
if(!$MEMBERSHIPID){
$MEMBERSHIPID=$_POST['id'];
}
if(!$patient_no){
$patient_no=$_POST['patient_no'];
}

$typeofvisit=$_POST['visit_type1'];
$insurance_claim_no=$_POST['insurance_claim_no'];
$attending_officer_id=$_POST['attending_officer'];
$service_cat = $_POST['service_category'];
$service_description = $_POST['service_description'];
$fvisit_type = $_POST['visit_type'];
$service= $_POST['service'];
$payment_mode = $_POST['payment_mode'];
$short_code=$_SESSION['provider_short_code'];
$modeofpayment=$_POST['mode_of_payment'];
$media_option=$_POST['media_option'];
$visit_date = $_POST['visit_date'];
$visit_time = date('H:i:s');
$visit_date_time = $visit_date.' '.$visit_time;

if($modeofpayment=='cash'){
$insurance_company='CASH';		
}

else if($modeofpayment=='staff'){
$insurance_company='STAFF';	
$insurance_no=$_POST['staff_no'];
}

else if($modeofpayment=='company'){
$insurance_company=$_POST['insurance_company_name'];	
$insurance_no=$_POST['company_staff_no'];
}
else if($modeofpayment=='insurance'){
$insurance_company=$_POST['insurance_com1'];		
$insurance_no = $_POST['insurance_no1'];
}



if($mem_id==''){
$mem_id=$_POST['mem_id'];
}

$REQUESTMODE='SOFTWARE';



$query_rsodest_claim = "select *, DATE_FORMAT(datetimeadded, '%e %b %Y %r') as dateprocessing from process_claim_no where  patient_no='$patient_no' and patient_status='In Use'";
// $query_rsodest_claim = "select *, DATE_FORMAT(datetimeadded, '%e %b %Y %r') as dateprocessing from process_claim_no where  patient_no='$patient_no' and provider_id='$PROVIDERID' and patient_status='In Use'";
//echo $query_rsodest_claim;

	$rsodest_claim = mysql_query($query_rsodest_claim, $courier) or die(mysql_error());
	$row_rsodest_claim = mysql_fetch_assoc($rsodest_claim);
	$totalRows_rsodest_claim = mysql_num_rows($rsodest_claim);	
if($action=='patientvisitno' || $action=="view_patient_visits"){
if($totalRows_rsodest_claim==0){
	
	$query_rsodest_officer = "SELECT * FROM users where id='$attending_officer_id'";
	//echo $query_rsodest_officer;	     
$rsodest_officer = mysql_query($query_rsodest_officer, $courier) or die(mysql_error());
$row_rsodest_officer=mysql_fetch_assoc($rsodest_officer);
$attending_officer=$row_rsodest_officer['title']." ".$row_rsodest_officer['firstnames']." ".$row_rsodest_officer['surname'];
$area_specialisation=$row_rsodest_officer['area_specialisation'];



$characters = array("A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z", "1","2","3","4","5","6","7","8","9");
		$PASSSALT=$characters[rand(0,32)].$characters[rand(0,32)];
		
		$characters = array("A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z", "1","2","3","4","5","6","7","8","9");
		$VALIDSALT=$characters[rand(0,32)].$characters[rand(0,32)].$characters[rand(0,32)].$characters[rand(0,32)];
		
                if($modeofpayment == 'cash'){
                    $consulting_room_status = 'awaiting';
                    $next_location = '';
                }else{
                    $consulting_room_status = 'pending';
                    $next_location = 'Consulting Room';
                }

        if($modeofpayment=='insurance'){
                $action_2 = 'processclaimno';
                include('api_integration.php');
                if($attending_officer_id_0 == '0'){
                    $attending_officer_id = '0';
                    $attending_officer = 'NO ATTENDING OFFICER';
                }
        }

        if(($CLAIMNUMBER_API != '' && $modeofpayment=='insurance') || $modeofpayment !='insurance' ){
         $query_rsodest="INSERT INTO `process_claim_no` (`id`, `provider_id`, `mode_of_payment`,`insurance_company`, `insurance_no`, `patient_id`, `patient_no`, `pass_salt`, `datetimeadded`, `date_added`, `request_mode`, `validation_salt`, `typeofvisit`, `pregnancy_visit_type`,`attending_officer_id`, `attending_officer`, `user_id`, `area_specialisation`,service_category,service_description,insurance_claim_no,consulting_room_status,session_status,media_option,branch_id) VALUES (NULL, '$PROVIDERID', '$modeofpayment', '$insurance_company', '$insurance_no', '$MEMBERSHIPID', '$patient_no', '$PASSSALT', '$visit_date_time', '$visit_date', '$REQUESTMODE', '$VALIDSALT', '$typeofvisit','$fvisit_type', '$attending_officer_id', '$attending_officer', '$user_id', '$area_specialisation','$service_cat','$service_description','$insurance_claim_no','$consulting_room_status','$next_location','$media_option','".$_SESSION['branch_id']."');";
		$rsodest = mysql_query($query_rsodest, $courier) or die(mysql_error());
        $CLAIMID=mysql_insert_id();
        $update_payment_mode = mysql_query("UPDATE patient_information SET type_of_client='$modeofpayment',insurance_company1='$insurance_company',insurance_no1='$insurance_no' WHERE patient_no='$patient_no'");

		$lenclaim=strlen($CLAIMID);
		$lenprovider=strlen($PROVIDERID);
		$claimidzero=str_repeat("0",(5-$lenclaim)).$CLAIMID;
		$CLAIMNUMBER=$_SESSION['branch_code'].$_SESSION['provider_short_code'].$claimidzero.$PASSSALT.date('m').date('y');
        //$CLAIMNUMBER1='DZ'.$claimidzero.$PASSSALT.date('y');

		$query_rsodest="update process_claim_no set process_claim_no='$CLAIMNUMBER',insurance_claim_no='$CLAIMNUMBER_API' where id='$CLAIMID'";
		$rsodest = mysql_query($query_rsodest, $courier) or die(mysql_error());

		
		
	$query_rsodest_claim = "select *, DATE_FORMAT(datetimeadded, '%e %b %Y %r') as dateprocessing  from process_claim_no where patient_no='$patient_no' and provider_id='$PROVIDERID' and id=$CLAIMID";
	$rsodest_claim = mysql_query($query_rsodest_claim, $courier) or die(mysql_error());
	$row_rsodest_claim = mysql_fetch_assoc($rsodest_claim);
	$totalRows_rsodest_claim = mysql_num_rows($rsodest_claim);
	
	$CLAIMNUMBER=$row_rsodest_claim['process_claim_no'];
$claimdate=$row_rsodest_claim['dateprocessing'];
$status=$row_rsodest_claim['patient_status'];
$validation_key=$row_rsodest_claim['validation_key'];
$validation_salt=$row_rsodest_claim['validation_salt'];
$typeofvisit=$row_rsodest_claim['typeofvisit'];
$attending_officer=$row_rsodest_claim['attending_officer'];

$query_claim_drug="SELECT * FROM patient_information where id='$MEMBERSHIPID'";
$rsclaim_drug= mysql_query($query_claim_drug) or die(mysql_error());
$row_rsclaim_drug = mysql_fetch_assoc($rsclaim_drug);
$totalRows_rsclaim_drug = mysql_num_rows($rsclaim_drug);
$mobile_number=$row_rsclaim_drug['tel'];
$member_no_txt=$row_rsclaim_drug['card_number'];	
$member_surname=$row_rsclaim_drug['surname'];

$update_status = mysql_query("update patient_information set patient_status='$status' where card_number = '$member_no_txt'");
    }
		
}else{
	
$CLAIMNUMBER=$row_rsodest_claim['process_claim_no'];
$process_claim_no=$CLAIMNUMBER;
$claimdate=$row_rsodest_claim['dateprocessing'];
$status=$row_rsodest_claim['patient_status'];
$validation_key=$row_rsodest_claim['validation_key'];
$validation_salt=$row_rsodest_claim['validation_salt'];
$typeofvisit=$row_rsodest_claim['typeofvisit'];
$attending_officer=$row_rsodest_claim['attending_officer'];
$area_specialisation=$row_rsodest_claim['area_specialisation'];
$STATUSMSG="Visit Number Processed Successfully";	
}

}


if($action=='guestvisitno' || $action == 'view_normal_patient_visits'){
if($totalRows_rsodest_claim==0){

$characters = array("A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z", "1","2","3","4","5","6","7","8","9");
		$PASSSALT=$characters[rand(0,32)].$characters[rand(0,32)];
		
		$characters = array("A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z", "1","2","3","4","5","6","7","8","9");
		$VALIDSALT=$characters[rand(0,32)].$characters[rand(0,32)].$characters[rand(0,32)].$characters[rand(0,32)];
		
		$query_rsodest="INSERT INTO `process_claim_no` (`id`, `provider_id`, `mode_of_payment`,`insurance_company`, `insurance_no`, `patient_id`, `patient_no`, `pass_salt`, `datetimeadded`, `date_added`, `request_mode`, `validation_salt`, `typeofvisit`, `pregnancy_visit_type`,`attending_officer_id`, `attending_officer`, `user_id`, `area_specialisation`,`service_category`,service_description,`guest_service`,`media_option`,`branch_id`) VALUES (NULL, '$PROVIDERID', '$payment_mode', '$insurance_company', '$insurance_no', '$MEMBERSHIPID', '$patient_no', '$PASSSALT', '$visit_date_time', '$visit_date', '$REQUESTMODE', '$VALIDSALT', '$client','$fvisit_type', '$attending_officer_id', '$attending_officer', '$user_id', '$area_specialisation','$service_cat','$service_description','$service','$media_option','".$_SESSION['branch_id']."');";
		$rsodest = mysql_query($query_rsodest, $courier) or die(mysql_error());
		$CLAIMID=mysql_insert_id();
		$lenclaim=strlen($CLAIMID);
		$lenprovider=strlen($PROVIDERID);
		$claimidzero=str_repeat("0",(3-$lenclaim)).$CLAIMID;
		$provideridzero=str_repeat("0",(3-$lenprovider)).$PROVIDERID;
		$CLAIMNUMBER="G".$_SESSION['provider_short_code'].$claimidzero.$PASSSALT.date('y');
		//$CLAIMNUMBER1='DZ'.$claimidzero.$PASSSALT.date('y');
		$query_rsodest="update process_claim_no set process_claim_no='$CLAIMNUMBER' where id='$CLAIMID'";
		$rsodest = mysql_query($query_rsodest, $courier) or die(mysql_error());
		
		
		
		$query_rsodest_claim = "select *, DATE_FORMAT(datetimeadded, '%e %b %Y %r') as dateprocessing  from process_claim_no where patient_no='$patient_no' and provider_id='$PROVIDERID' and id=$CLAIMID";
//echo $query_rsodest_claim;
	$rsodest_claim = mysql_query($query_rsodest_claim, $courier) or die(mysql_error());
	$row_rsodest_claim = mysql_fetch_assoc($rsodest_claim);
	$totalRows_rsodest_claim = mysql_num_rows($rsodest_claim);
	
$CLAIMNUMBER=$row_rsodest_claim['process_claim_no'];
$claimdate=$row_rsodest_claim['dateprocessing'];
$status=$row_rsodest_claim['patient_status'];
$validation_key=$row_rsodest_claim['validation_key'];
$validation_salt=$row_rsodest_claim['validation_salt'];
$typeofvisit=$row_rsodest_claim['typeofvisit'];
$attending_officer=$row_rsodest_claim['attending_officer'];

$query_claim_drug="SELECT * FROM patient_information where id='$MEMBERSHIPID'";
$rsclaim_drug= mysql_query($query_claim_drug) or die(mysql_error());
$row_rsclaim_drug = mysql_fetch_assoc($rsclaim_drug);
$totalRows_rsclaim_drug = mysql_num_rows($rsclaim_drug);
$mobile_number=$row_rsclaim_drug['tel'];
$member_no_txt=$row_rsclaim_drug['card_number'];	
$member_surname=$row_rsclaim_drug['surname'];
$update_status = mysql_query("update patient_information set patient_status='$status' where card_number = '$member_no_txt'");		
}else{
	
$CLAIMNUMBER=$row_rsodest_claim['process_claim_no'];
$process_claim_no=$CLAIMNUMBER;
$claimdate=$row_rsodest_claim['dateprocessing'];
$status=$row_rsodest_claim['patient_status'];
$validation_key=$row_rsodest_claim['validation_key'];
$validation_salt=$row_rsodest_claim['validation_salt'];
$typeofvisit=$row_rsodest_claim['typeofvisit'];
$attending_officer=$row_rsodest_claim['attending_officer'];
$area_specialisation=$row_rsodest_claim['area_specialisation'];
$STATUSMSG="Visit Number Processed Successfully";	
	
}

}
?>




<div class="form-body">
    <div class="alert alert-success">
        <strong>Success!</strong> Visit Number Generated ! 
    </div>
<table class="table table-bordered">
    <tbody>
        <?php if($action=='patientvisitno' || $action=="view_patient_visits"){?>
        <tr>
            <td> <div style="padding: 5px;"> Type of Visit </div> </td>
            <td class="bold"> <div style="padding: 5px;"> <?php echo $row_rsodest_claim['service_category']; ?> </div> </td>
        </tr>
        <tr>
            <td> <div style="padding: 5px;"> Attending Officer </div> </td>
            <td class="bold"> <div style="padding: 5px;"> <?php if($row_rsodest_claim['attending_officer_id'] == '0'){echo 'No Consultaion Needed';}else{ echo $attending_officer;} ?> </div> </td>
        </tr>
        <?php } ?>
        <tr>
            <td> <div style="padding: 5px;"> Visit Number </div> </td>
            <td class="bold"> <div style="padding: 5px;"> <?php echo $CLAIMNUMBER; ?> </div> </td>
        </tr>
        <tr>
            <td> <div style="padding: 5px;"> Date Generated </div> </td>
            <td class="bold"> <div style="padding: 5px;"> <?php echo $claimdate; ?> </div> </td>
        </tr>
        <tr>
            <td> <div style="padding: 5px;"> Visit Status </div> </td>
            <td class="bold"> <div style="padding: 5px;"> <?php echo $status; ?> </div> </td>
        </tr>
       <?php if($action=='guestvisitno'){ ?>
        <tr>
            <td> <div style="padding: 5px;"> Visit Status </div> </td>
            <td class="bold"> <div style="padding: 5px;"> Repeating Pulsate </div> </td>
        </tr>
        <?php } ?>
    </tbody>
</table>
</div>

<div class="form-actions right">
    
    <p style="font-size:18px">
    <?php if($action=='guestvisitno'){ 
		if($service=='PHARMACY'){ ?>
    <a href="#" onclick="getstep('forward_pharmacy',<?php echo $MEMBERSHIPID; ?>);" class="btn green"><span style="color:white; font-weight:bold">Forward To Pharmacy <span class="glyphicon glyphicon-cloud-download"></span></a> <?php }elseif($service=='LABORATORY'){?><a href="#" onclick="getstep('forward_lab',<?php echo $MEMBERSHIPID; ?>);" class="btn green"><span style="color:white; font-weight:bold">Forward To Laboratory <span class="glyphicon glyphicon-cloud-download"></span></a><?php } else {?><a href="#" onclick="getstep('view_guest_patient_visits',<?php echo $MEMBERSHIPID; ?>);" class="btn green"><span style="color:white; font-weight:bold">Add Charges Now <i class="icon-plus-sign"></i></span></a> <?php }
	
    }else { ?>
    <button type="submit" class="btn green" onclick="getProcess('add_administrative_charges', '#patient_page_wrapper', '<?php echo $id."@@@".$CLAIMNUMBER; ?>','patient_administraitve_charges');return false;"> <i class="fa fa-plus-circle"></i> Add Charges Now </button>
    <!--<a href="#" onclick="getstep('view_patient_visits',<?php echo $MEMBERSHIPID; ?>);" class="btn green"><span style="color:white; font-weight:bold"> Add Charges Now <i class="icon-plus-sign"></i></span></a>-->
        <?php } ?>
</p>
</div>
