<?php  
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$clinicObj =  new  clinic();


if(isset($_REQUEST['act']) && $_REQUEST['act'] == 'getprocedurecode' && isset($_REQUEST['procedure_code']) && $_REQUEST['procedure_code'] != '')
{
    $procedure_code = explode(",", $_REQUEST['procedure_code']);
    if(count($procedure_code) > 1)
    {
        $procedurearg =  "'" . implode ( "', '", $procedure_code ) . "'";
        
        $procedure_code_data .= " procedure_code_category IN (".$procedurearg.")";

    }else{
        $procedure_code_data = " procedure_code_category IN ('".$_REQUEST['procedure_code']."') ";
    }
    $procedure_codeObj = new procedure_code();
    $where = "procedure_code_clinic_id = '".$_REQUEST['clinic_id']."' and procedure_code_sikkaId != '' and ".$procedure_code_data;
    //echo $where;exit;
    //$groupBy = array(" procedure_code_category_id desc ");
    $procedurecodeArg    = array('where' => $where);
    $sikkaDetails = $procedure_codeObj->getAllRecords($procedurecodeArg);
    ?>
    <!-- <option value="">Select Code</option> -->
    <?php
    foreach ($sikkaDetails as $key => $sikkayvalue) 
    { ?>
        <option value="<?php echo $sikkayvalue->procedure_code_sikkaId; ?>" ><?php echo $sikkayvalue->procedure_code_sikkaId; ?></option> 
                             
    <?php }
    //echo json_encode($sikkaDetails);
    exit;
}

if (isset($_REQUEST['act']) && $_REQUEST['act']=='load' && $_REQUEST['clinicId'] != ''){
        
    $cliniId = $_POST['clinicId'];
    $clinic_overdueval_6 = $_POST['clinic_overdueval_6'];
    $clinic_initiation_days_6 = $_POST['clinic_initiation_days_6'];
    $clinic_overdue_status_6 = $_POST['clinic_overdue_status_6'];
    $clinic_procedure_category_6 = $_POST['clinic_procedure_category_6'];
    $clinic_procedure_code_6 = $_POST['clinic_procedure_code_6'];

    $clinic_overdueval_12 = $_POST['clinic_overdueval_12'];
    $clinic_initiation_days_12 = $_POST['clinic_initiation_days_12'];
    $clinic_overdue_status_12 = $_POST['clinic_overdue_status_12'];
    $clinic_procedure_category_12 = $_POST['clinic_procedure_category_12'];
    $clinic_procedure_code_12 = $_POST['clinic_procedure_code_12'];

    $clinic_overdueval_18 = $_POST['clinic_overdueval_18'];
    $clinic_initiation_days_18 = $_POST['clinic_initiation_days_18'];
    $clinic_overdue_status_18 = $_POST['clinic_overdue_status_18'];
    $clinic_procedure_category_18 = $_POST['clinic_procedure_category_18'];
    $clinic_procedure_code_18 = $_POST['clinic_procedure_code_18'];

    $clinic_overdueval_24 = $_POST['clinic_overdueval_24'];
    $clinic_initiation_days_24 = $_POST['clinic_initiation_days_24'];
    $clinic_overdue_status_24 = $_POST['clinic_overdue_status_24'];
    $clinic_procedure_category_24 = $_POST['clinic_procedure_category_24'];
    $clinic_procedure_code_24 = $_POST['clinic_procedure_code_24'];

    $clinicOverdue_details6 = array();
    $clinicOverdue_details12 = array();
    $clinicOverdue_details18 = array();
    $clinicOverdue_details24 = array();
    $data = array();
    
    if(isset($clinic_initiation_days_6) && $clinic_initiation_days_6 != '')
    {
        $clinicOverdue_details6['initiation_days'] = $clinic_initiation_days_6;
    }
    if(!empty($clinic_overdue_status_6))
    {
    
        foreach ($clinic_overdue_status_6 as $ps6key => $pc6status) 
        {        
            $status_6 .= $pc6status.",";
            
        }
        $clinicOverdue_details6['status'] = rtrim($status_6,",");
    }
    if(!empty($clinic_procedure_category_6))
    {
    
        foreach ($clinic_procedure_category_6 as $pc6key => $pc6category) 
        {        
            $procedure_category_6 .= $pc6category.",";
            
        }
        $clinicOverdue_details6['procedure_category'] = rtrim($procedure_category_6,",");
    }else{
        $clinicOverdue_details6['procedure_category'] = '';
    }
    if(!empty($clinic_procedure_code_6))
    {
    
        foreach ($clinic_procedure_code_6 as $pcd6key => $pc6code) 
        {        
            $procedure_code_6 .= $pc6code.",";
            
        }
        $clinicOverdue_details6['procedure_code'] = rtrim($procedure_code_6,",");
    }else{
        $clinicOverdue_details6['procedure_code'] = '';
    }

    if(isset($clinic_initiation_days_12) && $clinic_initiation_days_12 != '')
    {
        $clinicOverdue_details12['initiation_days'] = $clinic_initiation_days_12;
    }
    if(!empty($clinic_overdue_status_12))
    {
    
        foreach ($clinic_overdue_status_12 as $ps12key => $pc12status) 
        {        
            $status_12 .= $pc12status.",";
            
        }
        $clinicOverdue_details12['status'] = rtrim($status_12,",");
    }
    if(!empty($clinic_procedure_category_12))
    {
    
        foreach ($clinic_procedure_category_12 as $pc12key => $pc12category) 
        {        
            $procedure_category_12 .= $pc12category.",";
            
        }
        $clinicOverdue_details12['procedure_category'] = rtrim($procedure_category_12,",");
    }else{
        $clinicOverdue_details12['procedure_category'] = '';
    }
    if(!empty($clinic_procedure_code_12))
    {
    
        foreach ($clinic_procedure_code_12 as $pcd12key => $pc12code) 
        {        
            $procedure_code_12 .= $pc12code.",";
            
        }
        $clinicOverdue_details12['procedure_code'] = rtrim($procedure_code_12,",");
    }else{
        $clinicOverdue_details12['procedure_code'] = '';
    }

    if(isset($clinic_initiation_days_18) && $clinic_initiation_days_18 != '')
    {
        $clinicOverdue_details18['initiation_days'] = $clinic_initiation_days_18;
    }
    if(!empty($clinic_overdue_status_18))
    {
    
        foreach ($clinic_overdue_status_18 as $ps18key => $pc18status) 
        {        
            $status_18 .= $pc18status.",";
            
        }
        $clinicOverdue_details18['status'] = rtrim($status_18,",");
    }
    if(!empty($clinic_procedure_category_18))
    {
    
        foreach ($clinic_procedure_category_18 as $pc18key => $pc18category) 
        {        
            $procedure_category_18 .= $pc18category.",";
            
        }
        $clinicOverdue_details18['procedure_category'] = rtrim($procedure_category_18,",");
    }else{
        $clinicOverdue_details18['procedure_category'] ='';
    }
    if(!empty($clinic_procedure_code_18))
    {
    
        foreach ($clinic_procedure_code_18 as $pcd18key => $pc18code) 
        {        
            $procedure_code_18 .= $pc18code.",";
            
        }
        $clinicOverdue_details18['procedure_code'] = rtrim($procedure_code_18,",");
    }else{
        $clinicOverdue_details18['procedure_code'] = '';
    }

    if(isset($clinic_initiation_days_24) && $clinic_initiation_days_24 != '')
    {
        $clinicOverdue_details24['initiation_days'] = $clinic_initiation_days_24;
    }
    if(!empty($clinic_overdue_status_24))
    {
    
        foreach ($clinic_overdue_status_24 as $ps24key => $pc24status) 
        {        
            $status_24 .= $pc24status.",";
            
        }
        $clinicOverdue_details24['status'] = rtrim($status_24,",");
    }
    if(!empty($clinic_procedure_category_24))
    {
    
        foreach ($clinic_procedure_category_24 as $pc24key => $pc24category) 
        {        
            $procedure_category_24 .= $pc24category.",";
            
        }
        $clinicOverdue_details24['procedure_category'] = rtrim($procedure_category_24,",");
    }else{
        $clinicOverdue_details24['procedure_category'] = '';
    }
    if(!empty($clinic_procedure_code_24))
    {
    
        foreach ($clinic_procedure_code_24 as $pcd24key => $pc24code) 
        {        
            $procedure_code_24 .= $pc24code.",";
            
        }
        $clinicOverdue_details24['procedure_code'] = rtrim($procedure_code_24,",");
    }else{
        $clinicOverdue_details24['procedure_code'] = '';
    }

    $data[$clinic_overdueval_6] = $clinicOverdue_details6;
    $data[$clinic_overdueval_12] = $clinicOverdue_details12;
    $data[$clinic_overdueval_18] = $clinicOverdue_details18;
    $data[$clinic_overdueval_24] = $clinicOverdue_details24;
    $all_overdue = json_encode($data);
    $data=array(
            'clinic_overdue'=>($all_overdue),
        );
    if (isset($cliniId)){    
        $id=$cliniId;
        //print_r($data);exit;
        $rs=$clinicObj->updateRecords($data,$id);
        if ($rs){

            header('Location:clinic_overdue.php?act=overdue&clinic_id='.$id);
            exit;
        }
    }
}

if (isset($_REQUEST['act']) && $_REQUEST['act']=='overdue' && $_REQUEST['clinic_id'] != ''){
    

    $fields = array('clinic_id','clinic_overdue','clinic_new_patient_status');
    $where = array("clinic_id="=>$_REQUEST['clinic_id']);
    $clinicArg    = array('fields'=>$fields,'where' => $where);
    $clinicObj->resultAs = "array";
    $clinicDetails = $clinicObj->fromFields($clinicArg);
    if($clinicDetails)
    {
        if(!empty($clinicDetails['clinic_overdue']))
        {
            $overduedata = json_decode($clinicDetails['clinic_overdue'],1);
            if(!empty($overduedata['0-6']))
            {
                $initiation_days_6 = $overduedata['0-6']['initiation_days'];
                $status_6 = explode(",",$overduedata['0-6']['status']);
                $procedure_category_6 = explode(",",$overduedata['0-6']['procedure_category']);
                $procedure_code_6 = explode(",",$overduedata['0-6']['procedure_code']);

                if(!empty($procedure_category_6) && count($procedure_category_6) > 0)
                {
                    $procedurearg =  "'" . implode ( "', '", $procedure_category_6 ) . "'";
                    
                    $procedure_code_data6 .= " procedure_code_category IN (".$procedurearg.")";
                    $procedure_codeObj = new procedure_code();
                    $where = "procedure_code_clinic_id = '".$_REQUEST['clinic_id']."' and procedure_code_sikkaId != '' and ".$procedure_code_data6;
                    $procedurecodeArg    = array('where' => $where);
                    $codeDetails_6 = $procedure_codeObj->getAllRecords($procedurecodeArg);

                }

            }

            if(!empty($overduedata['7-12']))
            {
                $initiation_days_12 = $overduedata['7-12']['initiation_days'];
                $status_12 = explode(",",$overduedata['7-12']['status']);
                $procedure_category_12 = explode(",",$overduedata['7-12']['procedure_category']);
                $procedure_code_12 = explode(",",$overduedata['7-12']['procedure_code']);

                if(!empty($procedure_category_12) && count($procedure_category_12) > 0)
                {
                    $procedurearg =  "'" . implode ( "', '", $procedure_category_12 ) . "'";
                    
                    $procedure_code_data12 .= " procedure_code_category IN (".$procedurearg.")";
                    $procedure_codeObj = new procedure_code();
                    $where = "procedure_code_clinic_id = '".$_REQUEST['clinic_id']."' and procedure_code_sikkaId != '' and ".$procedure_code_data12;
                    $procedurecodeArg    = array('where' => $where);
                    $codeDetails_12 = $procedure_codeObj->getAllRecords($procedurecodeArg);

                }
            }

            if(!empty($overduedata['13-18']))
            {
                $initiation_days_18 = $overduedata['13-18']['initiation_days'];
                $status_18 = explode(",",$overduedata['13-18']['status']);
                $procedure_category_18 = explode(",",$overduedata['13-18']['procedure_category']);
                $procedure_code_18 = explode(",",$overduedata['13-18']['procedure_code']);

                if(!empty($procedure_category_18) && count($procedure_category_18) > 0)
                {
                    $procedurearg =  "'" . implode ( "', '", $procedure_category_18 ) . "'";
                    
                    $procedure_code_data18 .= " procedure_code_category IN (".$procedurearg.")";
                    $procedure_codeObj = new procedure_code();
                    $where = "procedure_code_clinic_id = '".$_REQUEST['clinic_id']."' and procedure_code_sikkaId != '' and ".$procedure_code_data18;
                    $procedurecodeArg    = array('where' => $where);
                    $codeDetails_18 = $procedure_codeObj->getAllRecords($procedurecodeArg);

                }
            }

            if(!empty($overduedata['19-24']))
            {
                $initiation_days_24 = $overduedata['19-24']['initiation_days'];
                $status_24 = explode(",",$overduedata['19-24']['status']);
                $procedure_category_24 = explode(",",$overduedata['19-24']['procedure_category']);
                $procedure_code_24 = explode(",",$overduedata['19-24']['procedure_code']);

                if(!empty($procedure_category_24) && count($procedure_category_24) > 0)
                {
                    $procedurearg =  "'" . implode ( "', '", $procedure_category_24 ) . "'";
                    
                    $procedure_code_data24 .= " procedure_code_category IN (".$procedurearg.")";
                    $procedure_codeObj = new procedure_code();
                    $where = "procedure_code_clinic_id = '".$_REQUEST['clinic_id']."' and procedure_code_sikkaId != '' and ".$procedure_code_data24;
                    $procedurecodeArg    = array('where' => $where);
                    $codeDetails_24 = $procedure_codeObj->getAllRecords($procedurecodeArg);

                }
            }
        }

        $clinic_patient_status = array('');
        if(!empty($clinicDetails['clinic_new_patient_status']))
        {

            $clinic_patient_status = explode(",",$clinicDetails['clinic_new_patient_status']);
        }
    }
   // exit;
}

if (isset($_REQUEST['act']) && $_REQUEST['act']=='patient_status' && $_REQUEST['clinicId'] != ''){
        
    $cliniId = $_POST['clinicId'];
    $clinic_patient_status = $_POST['clinic_patient_status'];
    
    $patient_status =  implode ( ",", $clinic_patient_status );

    $data=array(
            'clinic_new_patient_status'=>($patient_status),
        );
    if (isset($cliniId)){    
        $id=$cliniId;
        //print_r($data);exit;
        $rs=$clinicObj->updateRecords($data,$id);
        if ($rs){

            header('Location:clinic_overdue.php?act=overdue&clinic_id='.$id);
            exit;
        }
    }
}

$procedure_codeObj = new procedure_code();
$where = "procedure_code_clinic_id = '".$_REQUEST['clinic_id']."' and procedure_code_category != ''";
$groupBy = array("procedure_code_category");
$procedurecodeArg    = array('where' => $where,'groupBy'=>$groupBy);
$categoryDetails = $procedure_codeObj->getAllRecords($procedurecodeArg);



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