<?php  
// Include main file
include_once('../libs/main.php');
// Check Login
include_once('checkLogin.php');

// Logical section of page
$patientbillableObj =  new  patientbillable();


if (isset($_REQUEST['act']) && $_REQUEST['act']=='load'){
    
    $records = $_REQUEST['length'];
    $start=0;
    if(isset($_GET['start']) && $_GET['start']!='0'){
        $page = $_GET['start'];
    }
    $start=($page-1)*$limit;
    $limit = array('start'=>$_GET['start'],'record'=>$records);
    
    $where = " 1=1 ";

    if(isset($_REQUEST['unique_id']) && $_REQUEST['unique_id'] != '')
    {
        $where .= " AND appointment_unique_id = '".$_REQUEST['unique_id']."'";
    }
    if(isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id'] != '')
    {
        $where .= " AND  appointment_clinic_id = '".$_REQUEST['clinic_id']."' ";
    }
    if(isset($_REQUEST['dependant_id']) && $_REQUEST['dependant_id'] != '')
    {
        $where .= " AND  appointment_dependant_id = '".$_REQUEST['dependant_id']."' ";
    }
    if (isset($_REQUEST['start_date']) && $_REQUEST['start_date']!="" && isset($_REQUEST['end_date']) && $_REQUEST['end_date']!="" ){
        $where .= " AND  appointment_date BETWEEN '".$_REQUEST['start_date']."' AND '".$_REQUEST['end_date']."' ";
    }
     
    if (isset($_REQUEST['start_time']) && $_REQUEST['start_time']!="" && isset($_REQUEST['end_time']) && $_REQUEST['end_time']!="" )
    {
        
        $cstart_time = date("h:i A",strtotime($_REQUEST['start_time']));
        $cend_time = date("h:i A",strtotime($_REQUEST['end_time']));
        
        $where .= " AND  (STR_TO_DATE(appointment_time, '%l:%i %p' ) >= STR_TO_DATE('".$cstart_time."', '%l:%i %p' ) AND STR_TO_DATE(appointment_etime, '%l:%i %p' ) <= STR_TO_DATE( '".$cend_time."', '%l:%i %p' ) ) ";
    }
    if(isset($_REQUEST['appointment_status']) && $_REQUEST['appointment_status'] != '')
    {
        $where .= " AND  appointment_status = '".$_REQUEST['appointment_status']."' ";
    }
    if(isset($_REQUEST['patientbillable_month']) && $_REQUEST['patientbillable_month'] != '')
    {
        $where .= " AND  patientbillabl_month = '".$_REQUEST['patientbillable_month']."' ";
    }
    if(isset($_REQUEST['patientbillable_year']) && $_REQUEST['patientbillable_year'] != '')
    {
        $where .= " AND  patientbillabl_year = '".$_REQUEST['patientbillable_year']."' ";
    }
    if(isset($_REQUEST['patient_billable']) && $_REQUEST['patient_billable'] != '')
    {
        $where .= " AND patientbillabl_status = '".$_REQUEST['patient_billable']."'";
    }
    /*if(isset($_REQUEST['patientbillable_name']) && $_REQUEST['patientbillable_name'] != '')
    {
        $patientname = preg_replace('/\s\s+/', ' ', $_REQUEST['patientbillable_name']);
        $fullname = explode(" ", $patientname);
        if($fullname && count($fullname)>0)
        {
            $where ="";
            foreach ($fullname as $key => $f) 
            {
                $where .=" patient_fname LIKE '%".w::e($f)."%' OR patient_fname LIKE '%".w::e(ucfirst($f))."%'  OR patient_lname LIKE '%".w::e($f)."%' OR patient_lname LIKE '%".w::e(ucfirst($f))."%'";
                if( ($key+1) != count($fullname) )
                {
                        $where .=" OR ";
                }
            }
        }
        
    }*/

    if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] != '')
    {
        $firstname = trim($_REQUEST['firstname']);
        $lastname = trim($_REQUEST['lastname']);

        $where .= " AND ((patient_fname='".w::e($firstname)."' and patient_lname='".w::e($lastname)."') OR (patient_fname='".w::e(ucfirst($firstname))."' and   patient_lname='".w::e(ucfirst($lastname))."'))";
    }else if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] == ''){

        $firstname = trim($_REQUEST['firstname']);

        $where .= " AND ((patient_fname LIKE '%".w::e($firstname)."%') OR (patient_fname LIKE '%".w::e(ucfirst($firstname))."%' ))";
    }else if(isset($_REQUEST['lastname']) && $_REQUEST['lastname'] != '' && $_REQUEST['firstname'] == ''){

        $lastname = trim($_REQUEST['lastname']);

        $where .= " AND ((patient_lname LIKE '%".w::e($lastname)."%') OR ( patient_lname LIKE '%".w::e(ucfirst($lastname))."%'))";
    }

    if(isset($_REQUEST['patientbillable_email']) && $_REQUEST['patientbillable_email'] != '')
    {
        $where .=" AND ( patient_email_address = '".w::e($_REQUEST['patientbillable_email'])."' OR clinic_email = '".$_REQUEST['patientbillable_email']."' OR cdoctor_email = '".$_REQUEST['patientbillable_email']."') ";
    }

    if(isset($_REQUEST['patientbillable_phone']) && $_REQUEST['patientbillable_phone'] != '')
    {
        $where .=" AND ( patient_phone = '".w::e($_REQUEST['patientbillable_phone'])."' OR clinic_phone = '".$_REQUEST['patientbillable_phone']."' OR cdoctor_phone = '".$_REQUEST['patientbillable_phone']."') ";
    }

    if(isset($_REQUEST['patientbillabl_id']) && $_REQUEST['patientbillabl_id'] != '')
    {
        $where .= " AND patientbillabl_id = '".$_REQUEST['patientbillabl_id']."'";
    }
    if(isset($_REQUEST['patientappointment_id']) && $_REQUEST['patientappointment_id'] != '')
    {
        $where .= " AND appointment_id = '".$_REQUEST['patientappointment_id']."'";
    }


    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        
        $where = " patientbillabl_id LIKE '%".$_REQUEST['search']['value']."%' OR a.appointment_unique_id LIKE '%".$_REQUEST['search']['value']."%' OR a.appointment_id LIKE '%".$_REQUEST['search']['value']."%' OR  a.appointment_clinic_id LIKE '%".$_REQUEST['search']['value']."%' OR a.appointment_doctor_id LIKE '%".$_REQUEST['search']['value']."%' OR  a.appointment_patient_id LIKE '%".$_REQUEST['search']['value']."%' OR  a.appointment_dependant_id LIKE '%".$_REQUEST['search']['value']."%' OR  a.appointment_appointmenttype_id LIKE '%".$_REQUEST['search']['value']."%' OR dep.dependant_fname LIKE '%".w::e($_REQUEST['search']['value'])."%' OR dep.dependant_lname LIKE '%".w::e($_REQUEST['search']['value'])."%' OR c.clinic_name LIKE '%".$_REQUEST['search']['value']."%' OR p.patientbillabl_month LIKE '%".$_REQUEST['search']['value']."%' OR p.patientbillabl_year LIKE '%".$_REQUEST['search']['value']."%' OR p.patientbillabl_total LIKE '%".$_REQUEST['search']['value']."%' OR p.patientbillabl_subtotal LIKE '%".$_REQUEST['search']['value']."%' OR p.patientbillabl_status LIKE '%".$_REQUEST['search']['value']."%' OR a.appointment_status LIKE '%".$_REQUEST['search']['value']."%' OR at.appointmenttype_name LIKE '%".$_REQUEST['search']['value']."%'  OR pt.patient_id LIKE '%".$_REQUEST['search']['value']."%' OR pt.patient_fname LIKE '%".w::e($_REQUEST['search']['value'])."%' OR pt.patient_lname LIKE '%".w::e($_REQUEST['search']['value'])."%' OR pt.patient_email_address LIKE '%".w::e($_REQUEST['search']['value'])."%' OR pt.patient_phone LIKE '%".w::e($_REQUEST['search']['value'])."%' OR pt.patient_unique_id LIKE '%".$_REQUEST['search']['value']."%' OR a.appointment_date LIKE '%".$_REQUEST['search']['value']."%' OR a.appointment_time LIKE '%".$_REQUEST['search']['value']."%' OR dep.dependant_fname LIKE '%".w::e(ucfirst($_REQUEST['search']['value']))."%' OR dep.dependant_lname LIKE '%".w::e(ucfirst($_REQUEST['search']['value']))."%' OR dep.dependant_fname LIKE '%".w::e(ucwords($_REQUEST['search']['value']))."%' OR dep.dependant_lname LIKE '%".w::e(ucwords($_REQUEST['search']['value']))."%' OR pt.patient_fname LIKE '%".w::e(ucfirst($_REQUEST['search']['value']))."%' OR pt.patient_lname LIKE '%".w::e(ucfirst($_REQUEST['search']['value']))."%' OR pt.patient_fname LIKE '%".w::e(ucwords($_REQUEST['search']['value']))."%' OR pt.patient_lname LIKE '%".w::e(ucwords($_REQUEST['search']['value']))."%' OR pt.patient_email_address LIKE '%".w::e(ucfirst($_REQUEST['search']['value']))."%' OR pt.patient_email_address LIKE '%".w::e(ucwords($_REQUEST['search']['value']))."%' OR c.clinic_unique_id LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_phone LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_email LIKE '%".$_REQUEST['search']['value']."%' OR cd.cdoctor_fullname LIKE '%".$_REQUEST['search']['value']."%' OR cd.cdoctor_provider_id LIKE '%".$_REQUEST['search']['value']."%' OR cd.cdoctor_email LIKE '%".$_REQUEST['search']['value']."%' OR cd.cdoctor_phone LIKE '%".$_REQUEST['search']['value']."%' ";
    }    
     
      
    $orderFields = array('patientbillabl_id','clinic_id','patient_id');
    
    // Array of order by 
    $orderby=array();
    if (isset($_REQUEST['order']['0']['column']) && $_REQUEST['order']['0']['column']!=""){
        $orderby = array(" ".$orderFields[$_REQUEST['order']['0']['column']]." ".$_REQUEST['order']['0']['dir']);
    }else{
        $orderby = array(" patientbillabl_id desc ");
    }
    
    $fields  = array('a.*','c.*','dep.*','p.*','d.*','apt.*','pt.*','cd.cdoctor_fullname','cd.cdoctor_provider_id','cd.cdoctor_email','cd.cdoctor_phone');
    $tables = array('tbl_appointment a','tbl_clinic c','tbl_doctor d','tbl_dependant dep','tbl_appointmenttype apt','tbl_patient pt','tbl_cdoctor cd');
    $jfields = array('a.appointment_id=p.patientbillabl_appointment_id','a.appointment_clinic_id=c.clinic_id','a.appointment_doctor_id=d.doctor_id','a.appointment_dependant_id=dep.dependant_id','apt.appointmenttype_id=a.appointment_appointmenttype_id','a.appointment_patient_id=pt.patient_id','cd.cdoctor_doctor_id=a.appointment_doctor_id');
    $jtype = array("INNER","LEFT","LEFT","LEFT","LEFT","LEFT","LEFT");
    $data   = array('fields' => $fields, 'where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'orderBy'=>$orderby,'limit' => $limit);

    $patientbillableDetails = $patientbillableObj->getAllRecords($data);
    //print_r($billableDetails);exit;
    $recordsTotal   =   $patientbillableObj->getAllCount($data);
     
    if ($patientbillableDetails && count($patientbillableDetails)>0){
        foreach ($patientbillableDetails as $key => $patientbillable){

            if(!is_null($patientbillable->appointment_assignTo))
            {
                $patientObj = new patient();
                $where = 'patient_id IN ('.$patientbillable->appointment_assignTo.')';
                $data = array('where' => $where);
                $patientDetail = $patientObj->getAllRecords($data);
                //print_r($patientDetail);exit;
                $assign_detail = '';
                foreach ($patientDetail as $key => $patientdata) {
                    
                    $assign_detail .= "<b>Id: </b>".$patientdata->patient_id."<br/><b>External Id: </b>".$patientdata->patient_unique_id."<br/><b>First Name: </b>".w::d($patientdata->patient_fname)."<br/><b>Last Name: </b>".w::d($patientdata->patient_lname)."<br/><b>Email: </b>".w::d($patientdata->patient_email_address)."<br/><b>Phone : </b>".w::d($patientdata->patient_phone)."<br/><br/>";

                }
            }

            $response[] = array( 
                    "<b>ID: </b>".$patientbillable->patientbillabl_id."<br/><b>Appointment Id: </b>".$patientbillable->appointment_id."<br/><b>Appointment External Id: </b>".$patientbillable->appointment_unique_id."<br/><b>Clinic Id: </b>".$patientbillable->appointment_clinic_id."<br/><b>Provider Id: </b>".$patientbillable->appointment_doctor_id."<br><b>Sign In Id: </b>".$patientbillable->appointment_patient_id."<br/><b>Patient Id: </b>".$patientbillable->appointment_dependant_id."<br/><b>AppointmentType: </b>".$patientbillable->appointment_appointmenttype_id,

                    "<b>First Name: </b>".w::d($patientbillable->dependant_fname)."<br/><b>Last Name: </b>".w::d($patientbillable->dependant_lname)."<br/><b>Clinic Name: </b>".$patientbillable->clinic_name."<br/><b>Clinic External Id: </b>".$patientbillable->clinic_unique_id."<br/><b>Clinic Email: </b>".$patientbillable->clinic_email."<br/><b>Clinic Phone: </b>".$patientbillable->clinic_phone."<br/><b>Provider Name: </b>".$patientbillable->cdoctor_fullname."<br/><b>Provider External Id: </b>".$patientbillable->cdoctor_provider_id."<br/><b>Provider Email: </b>".$patientbillable->cdoctor_email."<br/><b>Provider Phone: </b>".$patientbillable->cdoctor_phone,

                    

                    "<b>Id: </b>".$patientbillable->patient_id."<br/><b> External Id: </b>".$patientbillable->patient_unique_id."<br/><b>First Name: </b>".w::d($patientbillable->patient_fname)."<br/><b>Last Name: </b>".w::d($patientbillable->patient_lname).'<br/><b>Email: </b>'.w::d($patientbillable->patient_email_address)."<br/><b>Phone: </b>".w::d($patientbillable->patient_phone),

                    $assign_detail,

                    

                    "<b>Month: </b>".$patientbillable->patientbillabl_month."<br/><b>Year: </b>".$patientbillable->patientbillabl_year."<br/><b>Total: </b>".$patientbillable->patientbillabl_total."<br/><b>SubTotal: </b>".$patientbillable->patientbillabl_subtotal."<br/><b>PatientBillable: </b>".$patientbillable->patientbillabl_status."<br/><b>Appointment: </b>".$patientbillable->appointment_status."<br/><b>Appointment Type: </b>".$patientbillable->appointmenttype_name."<br/><b>AppointmentDate: </b>".$patientbillable->appointment_date." ".$patientbillable->appointment_time,

                    "<b><a href='billableappointmentdetail.php?type=patientbillable&appoint_id=".$patientbillable->patientbillabl_appointment_id."'>View</a></b>",
                    

            );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

// Include Template 
include_once('../libs/routs.php');

?>