<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Mira_service_anm_model
 *
 * @author zmq163
 */
class Mira_service_anm_model extends CI_Model {

    //put your code here
    
    var $mdb;

    public function __construct() {
        parent::__construct();
        $this->mdb = $this->load->database('mdb', TRUE);
    }

    public function updateAnmAnswerModel($_anm_alert1 = NULL) {
        if ($_anm_alert1 != NULL) {
            $this->mdb->trans_begin();
            for ($i = 0; $i < count($_anm_alert1); $i++) {
                $this->mdb->where(array('pregnant_id' => $_anm_alert1[$i]['pregnant_id'], 'week_id' => $_anm_alert1[$i]['week_id']));
                $this->mdb->update('master_asha_to_anm_alert', array('anm_status' => 1, 'anm_action_date' => date('Y-m-d')));
                $this->mdb->select('*');
                $this->mdb->from('master_asha_to_anm_alert');
                $this->mdb->where(array('pregnant_id' => $_anm_alert1[$i]['pregnant_id'], 'week_id' => $_anm_alert1[$i]['week_id']));
                $data = $this->getData($this->mdb->get());
                if (isset($data) and $data[0]['alert'] == 1) {
                    
                    for ($j = 0; $j < count($_anm_alert1[$i]['qus_id_ans']); $j++) {
                        $this->mdb->where(array('question_id' => $_anm_alert1[$i]['qus_id_ans'][$j]['question_id'], 'alert_id' => $data[0]['alert_id']));
                        $this->mdb->update('master_asha_anm_ans', array('anm_answer' => $_anm_alert1[$i]['qus_id_ans'][$j]['anm_answer']));
                    }
                }
            }
            if ($this->mdb->trans_status() === FALSE) {
                $this->mdb->trans_rollback();
                return FALSE;
            } else {
                $this->mdb->trans_commit();
                return TRUE;
            }
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function anmGetWomenHistoryModel($_region_code = NULL) {
        $temp_data_anc_pregnant = NULL;
        $data_return = NULL;
        $temp_pregnant_data = $this->getPregnantDetail($_region_code);
        for ($i = 0; $i < count($temp_pregnant_data); $i++) {
            $temp_data_anc_pregnant = $this->pregnantAncData($temp_pregnant_data[$i]['pregnant_id'], $_region_code);
            $temp_data_anc_pregnant['house_number'] = $temp_pregnant_data[$i]['house_number'];
            $temp_data_anc_pregnant['women_name'] = $temp_pregnant_data[$i]['name'];
            $temp_data_anc_pregnant['women_id'] = $temp_pregnant_data[$i]['pregnant_id'];

            $temp_week_data = $this->getAshaQusAnsByPregnantId($temp_pregnant_data[$i]['pregnant_id']);
            $temp_data_anc_pregnant['week_data'] = $this->getWeekQusAns($temp_week_data);
            $temp_data_anc_pregnant['lmc_date'] = date('d/m/Y', strtotime($temp_pregnant_data[$i]['pregnant_date']));
            $temp_data_anc_pregnant['register_date'] = date('d/m/Y', strtotime($temp_pregnant_data[$i]['registration_date']));
            $temp_data_anc_pregnant['server_date'] = date('d/m/Y');
            $data_return[] = $temp_data_anc_pregnant;
        }
        return $data_return;
    }

    /**
     * 
     * 
     * 
     * 
     */
   /*  
     /// Old code
    public function anmGetAllDataByAshaIdModel($_region_code = NULL) {
        if ($_region_code != NULL) {
            $data_return = NULL;
            $temp_data_anc_pregnant = NULL;
            $temp_pregnant_data = $this->getPregnantDetail($_region_code);
            for ($i = 0; $i < count($temp_pregnant_data); $i++) {
                $temp_week_data = $this->getAshaQusAnsByPregnantId($temp_pregnant_data[$i]['pregnant_id']);
                $temp_data_anc_pregnant['week_data'] = $this->getWeekQusAns($temp_week_data);
                $temp_data_anc_pregnant['house_number'] = $temp_pregnant_data[$i]['house_number'];
                $temp_data_anc_pregnant['women_name'] = $temp_pregnant_data[$i]['name'];
                $temp_data_anc_pregnant['lmc_date'] = date('d/m/Y', strtotime($temp_pregnant_data[$i]['pregnant_date']));
                $temp_data_anc_pregnant['women_id'] = $temp_pregnant_data[$i]['pregnant_id'];
                $temp_data_anc_pregnant['server_date'] = date('d/m/Y');
                $data_return[] = $temp_data_anc_pregnant;
            }
            return $data_return;
        } else {
            return FALSE;
        }
    }
   */
   
    public function anmGetAllDataByAshaIdModel($_region_code = NULL) {
       
     

    $data_return = [];  // Initialize as an empty array
    $temp_pregnant_data = $this->getPregnantDetail($_region_code);

    // Check if there are any pregnant data entries
    if (empty($temp_pregnant_data)) {
        return $data_return;  // Return an empty array if no data
   }

    foreach ($temp_pregnant_data as $pregnant) {
       $pregnant_id = $pregnant['pregnant_id'];
        $temp_week_data = $this->getAshaQusAnsByPregnantId($pregnant_id);

        // Create a new entry for each pregnant person
        $entry = [
            'week_data' => $this->getWeekQusAns($temp_week_data),
            'house_number' => $pregnant['house_number'],
           'women_name' => $pregnant['name'],
            'lmc_date' => date('d/m/Y', strtotime($pregnant['pregnant_date'])),
            'women_id' => $pregnant_id,
           'server_date' => date('d/m/Y')
        ];

       $data_return[] = $entry;
   }

    return $data_return;
    }
   
    /*  
    // Old code
    public function getAshaQusAnsByPregnantId($pregnant_id) {
        $asha_ans_alert0 = NULL;
        $asha_ans_alert1 = NULL;
        $this->mdb->select('*');
        $this->mdb->from('master_asha_to_anm_alert');
        $this->mdb->where('pregnant_id', $pregnant_id);
        $data = $this->getData($this->mdb->get());
        if ($data) {
            for ($i = 0; $i < count($data); $i++) {
                if ($data[$i]['alert'] == 0) {
                    $asha_ans_alert0[] = $data[$i];
                } else {
                    $asha_ans_alert1[] = array(
                        'pregnant_id' => $pregnant_id,
                        'week_id' => $data[$i]['week_id'],
                        'qus_id_ans' => $this->getArrayAlert1($data[$i]['alert_id'])
                    );
                }
            }
            return array('alert0' => $asha_ans_alert0, 'alert1' => $asha_ans_alert1);
        }
    }
    */
    
    public function getAshaQusAnsByPregnantId($pregnant_id) {
    // Initialize variables as arrays
    $asha_ans_alert0 = [];
    $asha_ans_alert1 = [];
    
    // Prepare and execute the query
    $this->mdb->select('*');
    $this->mdb->from('master_asha_to_anm_alert');
    $this->mdb->where('pregnant_id', $pregnant_id);
    $query = $this->mdb->get();
    $data = $this->getData($query);
    
    // Check if $data is an array before proceeding
    if (is_array($data)) {
        // Iterate through $data
        for ($i = 0; $i < count($data); $i++) {
            if (isset($data[$i]['alert'])) {
                if ($data[$i]['alert'] == 0) {
                    $asha_ans_alert0[] = $data[$i];
                } else {
                    $asha_ans_alert1[] = array(
                        'pregnant_id' => $pregnant_id,
                        'week_id' => $data[$i]['week_id'],
                        'qus_id_ans' => $this->getArrayAlert1($data[$i]['alert_id'])
                    );
                }
            }
        }
    }

    return array('alert0' => $asha_ans_alert0, 'alert1' => $asha_ans_alert1);
}
    
    public function getArrayAlert1($_alert_id = NULL) {
        $this->mdb->select('*');
        $this->mdb->from('master_asha_anm_ans');
        $this->mdb->where('alert_id', $_alert_id);
        $data = $this->getData($this->mdb->get());
        return $data;
    }

    /*
    //old code
       private function getWeekQusAns($asha_data) {
        for ($i = 0; $i < 40; $i++) {
            $ans_string = NULL;
            $alert0_flag = TRUE;
            if ($alert0_flag) {
                for ($j = 0; $j < count($asha_data['alert0']); $j++) {
                    if ($i ==($asha_data['alert0'][$j]['week_id']-1)) {
                        for ($l = 0; $l < 5; $l++) {
                            if ($l == 0) {
                                $ans_string = $ans_string . "1";
                            } elseif ($l == 4) {
                                $ans_string = $ans_string . "/" . "1" . "/";
                            } else {
                                $ans_string = $ans_string . "/" . "1";
                            }
                        }
                        $ans_array[] = $ans_string;
                        $alert0_flag = FALSE;
                        break;
                    }
                }
            }
            if ($alert0_flag) {
                for ($j = 0; $j < count($asha_data['alert1']); $j++) {
                    if ($i == ($asha_data['alert1'][$j]['week_id']-1)) {
                        for ($l = 0; $l < 5; $l++) {
                            $flag1 = TRUE;
                            for ($m = 0; $m < count($asha_data['alert1'][$j]['qus_id_ans']); $m++) {
                                if ($l == ($asha_data['alert1'][$j]['qus_id_ans'][$m]['question_id']-1)) {
                                    
                                    
                                    if ($l == 0) {
                                        if($asha_data['alert1'][$j]['qus_id_ans'][$m]['anm_answer']){
                                            $ans_string = $ans_string . $asha_data['alert1'][$j]['qus_id_ans'][$m]['anm_answer'];
                                        }  else {
                                            $ans_string = $ans_string . $asha_data['alert1'][$j]['qus_id_ans'][$m]['asha_answer'];
                                        }
                                        
                                    } elseif ($l == 4) {
                                        if($asha_data['alert1'][$j]['qus_id_ans'][$m]['anm_answer']){
                                           $ans_string = $ans_string . "/" . $asha_data['alert1'][$j]['qus_id_ans'][$m]['anm_answer'] . "/";
                                        }  else {
                                           $ans_string = $ans_string . "/" . $asha_data['alert1'][$j]['qus_id_ans'][$m]['asha_answer'] . "/";
                                        }
                                        
                                    } else {
                                        if($asha_data['alert1'][$j]['qus_id_ans'][$m]['anm_answer']){
                                           $ans_string = $ans_string . "/" . $asha_data['alert1'][$j]['qus_id_ans'][$m]['anm_answer'];
                                        }  else {
                                            $ans_string = $ans_string . "/" . $asha_data['alert1'][$j]['qus_id_ans'][$m]['asha_answer'];
                                        }
                                        
                                    }
                                    $flag1 = FALSE;
                                }
                            }
                            if ($flag1) {
                                if ($l == 0) {
                                    $ans_string = $ans_string . "1";
                                } elseif ($l == 4) {
                                    $ans_string = $ans_string . "/" . "1" . "/";
                                } else {
                                    $ans_string = $ans_string . "/" . "1";
                                }
                            }
                        }
                        $ans_array[] = $ans_string;
                        $alert0_flag = FALSE;
                        break;
                    }
                }
            }
            if ($alert0_flag) {
                for ($l = 0; $l < 5; $l++) {
                    if ($l == 0) {
                        $ans_string = $ans_string . "0";
                    } elseif ($l == 4) {
                        $ans_string = $ans_string . "/" . "0" . "/";
                    } else {
                        $ans_string = $ans_string . "/" . "0";
                    }
                }
                $ans_array[] = $ans_string;
            }
        }
        $ans_string = NULL;
        for ($i = 0; $i < count($ans_array); $i++) {

            if ($i == 0) {
                $ans_string = $i . ">" . $ans_array[$i] . ">";
            } elseif ($i == 39) {
                $ans_string = $ans_string . "*" . $i . ">" . $ans_array[$i] . ">*";
            } else {
                $ans_string = $ans_string . "*" . $i . ">" . $ans_array[$i] . ">";
            }
        }
        return $ans_string;
    }
    
    */

   private function getWeekQusAns($asha_data) {
    $ans_array = [];
    
    // Iterate over 40 weeks
    for ($i = 0; $i < 40; $i++) {
        $ans_string = '';
        $alert0_flag = true;

        // Handle alert0 data
        if ($alert0_flag) {
            $alert0 = isset($asha_data['alert0']) && is_array($asha_data['alert0']) ? $asha_data['alert0'] : [];
            $alert0_count = count($alert0);
            for ($j = 0; $j < $alert0_count; $j++) {
                if ($i == ($alert0[$j]['week_id'] - 1)) {
                    for ($l = 0; $l < 5; $l++) {
                      //  $ans_string .= ($l == 0 ? "1" : "/1") . ($l == 4 ? "/" : '');
                        if ($l == 0) {
                                $ans_string = $ans_string . "1";
                            } elseif ($l == 4) {
                                $ans_string = $ans_string . "/" . "1" . "/";
                            } else {
                                $ans_string = $ans_string . "/" . "1";
                            }
                    }
                    $ans_array[] = $ans_string;
                    $alert0_flag = false;
                    break;
                }
            }
        }

        // Handle alert1 data
        if ($alert0_flag) {
            $alert1 = isset($asha_data['alert1']) && is_array($asha_data['alert1']) ? $asha_data['alert1'] : [];
            $alert1_count = count($alert1);
            for ($j = 0; $j < $alert1_count; $j++) {
                if ($i == ($alert1[$j]['week_id'] - 1)) {
                    $qus_id_ans = isset($alert1[$j]['qus_id_ans']) && is_array($alert1[$j]['qus_id_ans']) ? $alert1[$j]['qus_id_ans'] : [];
                    $qus_id_ans_count = count($qus_id_ans);
                    for ($l = 0; $l < 5; $l++) {
                        $flag1 = true;
                        for ($m = 0; $m < $qus_id_ans_count; $m++) {
                           /// 
                            
                             if ($l == ($qus_id_ans[$m]['question_id']-1)) {
                                    
                                    
                                    if ($l == 0) {
                                        if($qus_id_ans[$m]['anm_answer']){
                                            $ans_string = $ans_string . $qus_id_ans[$m]['anm_answer'];
                                        }  else {
                                            $ans_string = $ans_string . $qus_id_ans[$m]['asha_answer'];
                                        }
                                        
                                    } elseif ($l == 4) {
                                        if($qus_id_ans[$m]['anm_answer']){
                                           $ans_string = $ans_string . "/" . $qus_id_ans[$m]['anm_answer'] . "/";
                                        }  else {
                                           $ans_string = $ans_string . "/" . $qus_id_ans[$m]['asha_answer'] . "/";
                                        }
                                        
                                    } else {
                                        if($qus_id_ans[$m]['anm_answer']){
                                           $ans_string = $ans_string . "/" . $qus_id_ans[$m]['anm_answer'];
                                        }  else {
                                            $ans_string = $ans_string . "/" . $qus_id_ans[$m]['asha_answer'];
                                        }
                                        
                                    }
                                    $flag1 = FALSE;
                                }
                            
                            
                            //end of foor lop
                        }
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        if ($flag1) {
                             if ($l == 0) {
                                    $ans_string = $ans_string . "1";
                                } elseif ($l == 4) {
                                    $ans_string = $ans_string . "/" . "1" . "/";
                                } else {
                                    $ans_string = $ans_string . "/" . "1";
                                }
                        }
                    }
                    $ans_array[] = $ans_string;
                    $alert0_flag = false;
                    break;
                }
            }
        }

        // Handle default case
        if ($alert0_flag) {
                for ($l = 0; $l < 5; $l++) {
                    if ($l == 0) {
                        $ans_string = $ans_string . "0";
                    } elseif ($l == 4) {
                        $ans_string = $ans_string . "/" . "0" . "/";
                    } else {
                        $ans_string = $ans_string . "/" . "0";
                    }
                }
                $ans_array[] = $ans_string;
            }
    }

    // Format final answer string
    $ans_string = '';
    $ans_array_count = count($ans_array);
    for ($i = 0; $i < $ans_array_count; $i++) {
          if ($i == 0) {
                $ans_string = $i . ">" . $ans_array[$i] . ">";
            } elseif ($i == 39) {
                $ans_string = $ans_string . "*" . $i . ">" . $ans_array[$i] . ">*";
            } else {
                $ans_string = $ans_string . "*" . $i . ">" . $ans_array[$i] . ">";
            }
    }

    return $ans_string;
}

    
    /**
     * 
     * 
     * 
     * 
     */
    public function anmGetPregnantImmunzationByAshaIdModel($_region_code = NULL) {
        if ($_region_code != NULL) {
            $return_data = NULL;
            $temp_pregnant_data = $this->getPregnantDetail($_region_code);
            if ($temp_pregnant_data) {
                for ($i = 0; $i < count($temp_pregnant_data); $i++) {
                    $temp_data_anc_pregnant=NULL;
                    $temp_data_anc_pregnant = $this->pregnantAncData($temp_pregnant_data[$i]['pregnant_id'], $_region_code);
                    $temp_data_anc_pregnant['house_number'] = $temp_pregnant_data[$i]['house_number'];
                    $temp_data_anc_pregnant['women_name'] = $temp_pregnant_data[$i]['name'];
                    $temp_data_anc_pregnant['lmc_date'] = date('d/m/Y', strtotime($temp_pregnant_data[$i]['pregnant_date']));
                    $temp_data_anc_pregnant['women_id'] = $temp_pregnant_data[$i]['pregnant_id'];
                    $temp_data_anc_pregnant['server_date'] = date('d/m/Y');
                    $return_data[] = $temp_data_anc_pregnant;
                }
                
            }
            return $return_data;
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    private function getPregnantDetail($_region_code = NULL) {
        $this->mdb->select('mira_pregnant_women.pregnant_id,mira_pregnant_women.women_id,mira_pregnant_women.pregnant_date,mira_pregnant_women.create_date as registration_date,mira_pregnant_women.mobile_date,mira_pregnant_women.actual_register_date,mira_pregnant_women.pregnant_status,mira_pregnant_women.delivery_status,mira_pregnant_women.closing_date,mira_pregnant_women.delivery_location,mira_pregnant_women.delivered_baby_status,mira_pregnant_women.delivered_women_status,mira_pregnant_women.tran_pid,mira_pregnant_women.region_code,mira_pregnant_women.delete_status,mira_women.name,mira_house.house_number');
        $this->mdb->from('mira_pregnant_women');
        $this->mdb->join('mira_women', 'mira_women.women_id=mira_pregnant_women.women_id', 'INNER');
        $this->mdb->join('mira_house', 'mira_house.auto_id=mira_women.house_id', 'INNER');
        $this->mdb->where(array('mira_house.region_id' => $_region_code,'mira_pregnant_women.pregnant_status'=>1,'mira_house.delete_status'=>0,'mira_women.delete_status'=>0,'mira_pregnant_women.delete_status'=>0));
        return $this->getData($this->mdb->get());
    }

    private function pregnantAncData($_pregnant_id = NULL, $_region_code = NULL) {
        $return_data = NULL;
        $this->mdb->select('mapping_application_anc.*');
        $this->mdb->from('mapping_application_anc');
        $this->mdb->join('master_area_create_by_super', 'master_area_create_by_super.app_id=mapping_application_anc.app_id', 'INNER');
        $this->mdb->where(array('master_area_create_by_super.region_code' => $_region_code));
        $data_anc_master = $this->getData($this->mdb->get());

        $this->mdb->select('anm_anc_critical.*');
        $this->mdb->from('anm_anc_critical');
        $this->mdb->where(array('anm_anc_critical.pregnant_id' => $_pregnant_id));
        $pregnant_anc_data = $this->getData($this->mdb->get());

        $this->mdb->select('mira_anc_status0.*');
        $this->mdb->from('mira_anc_status0');
        $this->mdb->where(array('mira_anc_status0.pregnant_id' => $_pregnant_id));
        $return_data['critical'] = NULL;
        if ($this->getData($this->mdb->get())) {
            $return_data['anc0_status'] = 1;
        } else {
            $return_data['anc0_status'] = 0;
        }
        if (isset($pregnant_anc_data) and $pregnant_anc_data) {

            for ($i = 0; $i < count($data_anc_master); $i++) {

                for ($j = 0; $j < count($pregnant_anc_data); $j++) {
                    $flag = TRUE;
                    if ($data_anc_master[$i]['anc_code'] == $pregnant_anc_data[$j]['anc_code']) {
                        $return_data['anc' . ($i + 1) . '_status'] = $pregnant_anc_data[$j]['anc_status'];
                        $return_data['anc' . ($i + 1) . '_code'] = $pregnant_anc_data[$j]['anc_code'];
                        if ($i == 0) {
                            $return_data['critical'] = $return_data['critical'] . $pregnant_anc_data[$j]['critical_status'];
                        } else {
                            $return_data['critical'] = $return_data['critical'] .':'.$pregnant_anc_data[$j]['critical_status'];
                        }
                        $flag = FALSE;
                        break;
                    }
                }
                if ($flag) {
                    $return_data['anc' . ($i + 1) . '_status'] = 0;
                    $return_data['anc' . ($i + 1) . '_code'] = $data_anc_master[$i]['anc_code'];
                    if ($i == 0) {
                        $return_data['critical'] = $return_data['critical'] . '0';
                    } else {
                        $return_data['critical'] = $return_data['critical'] . ':0';
                    }
                }
            }
        } else {

            for ($i = 0; $i < count($data_anc_master); $i++) {
                $return_data['anc' . ($i + 1) . '_status'] = 0;
                $return_data['anc' . ($i + 1) . '_code'] = $data_anc_master[$i]['anc_code'];
            }
            $return_data['critical'] = "0:0:0:0";
        }
        return $return_data;
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function insertANCStatusAndANMQusAns($_anc_data = NULL, $_anm_qus_ans = NULL) {
        if ($_anc_data != NULL and $_anm_qus_ans != NULL) {
            $this->mdb->trans_begin();
            $this->mdb->insert_batch('anm_anc_qus_ans', $_anm_qus_ans);
            if ($_anc_data['anc_code']) {
                $this->mdb->insert('anm_anc_critical', $_anc_data);
                
                $new_value=0;
                 $current_value = $this->mdb->get_where('women_tracking', array('pregnant_id' =>  $_anc_data['pregnant_id']));
                   if ($current_value->num_rows() > 0) {
                    $value= $current_value->row()->total_anc  ;
                 // Increment the value
                    $new_value = $value + 1;
                   }
                
                if($_anc_data['anc_code']=='1')
                {
                   $this->mdb->where('pregnant_id', $_anc_data['pregnant_id']);
                   $this->mdb->update('women_tracking', array('anc1' => 1,'anc1_date'=>$_anc_data['anc_date'],'anc1_critical'=>$_anc_data['critical_status'],'total_anc'=>$new_value));  
                }
               else if($_anc_data['anc_code']=='2')
                {
                   $this->mdb->where('pregnant_id', $_anc_data['pregnant_id']);
                   $this->mdb->update('women_tracking', array('anc2' => 1,'anc2_date'=>$_anc_data['anc_date'],'anc2_critical'=>$_anc_data['critical_status'],'total_anc'=>$new_value));  
                }
               else if($_anc_data['anc_code']=='3')
                {
                  $this->mdb->where('pregnant_id', $_anc_data['pregnant_id']);
                   $this->mdb->update('women_tracking', array('anc3' => 1,'anc3_date'=>$_anc_data['anc_date'],'anc3_critical'=>$_anc_data['critical_status'],'total_anc'=>$new_value));   
                }
                
                else if($_anc_data['anc_code']=='4')
                {
                   $this->mdb->where('pregnant_id', $_anc_data['pregnant_id']);
                   $this->mdb->update('women_tracking', array('anc4' => 1,'anc4_date'=>$_anc_data['anc_date'],'anc4_critical'=>$_anc_data['critical_status'],'total_anc'=>$new_value));  
                }
                
            } else {
                $this->mdb->insert('mira_anc_status0', $_anc_data);
                $this->mdb->where('pregnant_id', $_anc_data['pregnant_id']);
                $this->mdb->update('women_tracking', array('anc0' => 1,'anc0_date'=>$_anc_data['anc_date']));
                
            }
            if ($this->mdb->trans_status() === FALSE) {
                $this->mdb->trans_rollback();
                return FALSE;
            } else {
                $this->mdb->trans_commit();
                return TRUE;
            }
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function anmUserAuthonticationModel($_user_id = NULL, $_password = NULL) {
        if ($_user_id != NULL and $_password != NULL) {
            $this->mdb->select('master_login_user.*,master_user_details.user_name,application_create.app_total_level');
            $this->mdb->from('master_login_user');
            $this->mdb->join('application_create', 'application_create.app_id=master_login_user.app_id', 'INNER');
            $this->mdb->join('master_user_details', 'master_login_user.user_login_id=master_user_details.user_login_id', 'INNER');
            $this->mdb->where(array('master_login_user.user_login_id' => $_user_id, 'master_login_user.user_password' => $_password, 'user_activation_status' => 1));
            $user = $this->getData($this->mdb->get());
            if (isset($user) and $user and ( $user[0]['user_level'] + 1) == $user[0]['app_total_level']) {
                $return_data['user'] = $user;
                $return_data['ashaList'] = $this->getAshaListByAnm($user[0]['create_region_id'], $user[0]['app_id']);
            } else {
                $return_data['user'] = FALSE;
            }
            return $return_data;
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    private function getAshaListByAnm($_region_code = NULL, $_app_id = NULL) {
        if ($_region_code != NULL and $_app_id != NULL) {
            $this->mdb->select('master_user_details.user_name as asha_name ,master_area_create_by_super.region_code as asha_id');
            $this->mdb->from('master_login_user');
            $this->mdb->join('master_user_details', 'master_login_user.user_login_id=master_user_details.user_login_id', 'INNER');
            $this->mdb->join('master_area_create_by_super', 'master_area_create_by_super.region_code=master_login_user.create_region_id', 'INNER');
            $this->mdb->where(array('master_login_user.user_activation_status' => 1, 'master_login_user.duplicate'=>0,'master_area_create_by_super.region_code_super' => $_region_code, 'master_area_create_by_super.app_id' => $_app_id));
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getData($_query = NULL) {
        if ($_query != NULL and $_query->num_rows() > 0) {
            return $_query->result_array();
        } else {
            return FALSE;
        }
    }

}
