<?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 Immuization_women_child_model
 *
 * @author zmq163
 */
class Immuization_women_child_model extends CI_Model {

    //put your code here
    var $mdb;

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

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function updateMissedVaccineModel($vaccination_date = NULL, $child_id_vaccine_id = NULL) {
        if ($vaccination_date != NULL and $child_id_vaccine_id != NULL) {
            $this->mdb->insert('master_child_immunization', array('vaccine_id' => $child_id_vaccine_id[1], 'child_id' => $child_id_vaccine_id[0], 'vaccine_date' => $vaccination_date));

            return ($this->mdb->affected_rows() > 0) ? TRUE : FALSE;
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getVaccineStartEndDateModel($child_id_vaccine_id = NULL) {
        if ($child_id_vaccine_id != NULL) {
            $this->mdb->select('dob');
            $this->mdb->from('mira_child');
            $this->mdb->where('child_id', $child_id_vaccine_id[0]);
            $child_dob = $this->getData($this->mdb->get());
            $this->mdb->select('*');
            $this->mdb->from('master_vaccine_child');
            $this->mdb->where('vaccine_id', $child_id_vaccine_id[1]);
            $vaccine_detail = $this->getData($this->mdb->get());

            $minDay = '+ ' . ($vaccine_detail[0]['from']) . 'day';


            $maxDay = '+ ' . ($vaccine_detail[0]['to']) . 'day';

            return array('vaccine_code' => $vaccine_detail[0]['vaccine_code'], 'startDate' => date('d-m-Y', strtotime($child_dob[0]['dob'] . $minDay)), 'endDate' => date('d-m-Y', strtotime($child_dob[0]['dob'] . $maxDay)));
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getPregnantWomenAncStatusDeliver($_pregnant_id = NULL, $_current_week = NULL) {
        $data_array = array('anc1' => 0, 'anc2' => 0, 'anc3' => 0, 'anc4' => 0);



        $this->mdb->select('*');
        $this->mdb->from('anm_anc_critical');
        $this->mdb->where('pregnant_id', $_pregnant_id);
        $data = $this->getData($this->mdb->get());

        if ($data) {
            foreach ($data as $key => $value['anc_code']) {
                switch ($value) {
                    case 1:
                        $data_array['anc1'] = 1;
                        break;
                    case 2:
                        $data_array['anc2'] = 1;
                        break;
                    case 3:
                        $data_array['anc3'] = 1;
                        break;
                    case 4:
                        $data_array['anc4'] = 1;
                        break;
                }
            }
        }
        return $data_array;
    }

    /**
     * 
     * 
     * 
     */
    public function getPregnantWomenAncStatus($_pregnant_id = NULL, $_current_week = NULL) {

        $data_array = array('anc1' => 3, 'anc2' => 3, 'anc3' => 3, 'anc4' => 3);
        if ($_current_week > 11) {
            $data_array['anc1'] = 0;
        }
        if ($_current_week > 25) {
            $data_array['anc2'] = 0;
        }
        if (($_current_week > 34)) {
            $data_array['anc3'] = 0;
        }


        if ($_current_week < 13) {
            $data_array['anc1'] = 2;
        } elseif (($_current_week < 27 and $_current_week > 13)) {
            $data_array['anc2'] = 2;
        } elseif (($_current_week < 35 and $_current_week > 27)) {
            $data_array['anc3'] = 2;
        } elseif (($_current_week > 35)) {
            $data_array['anc4'] = 2;
        }
        $this->mdb->select('*');
        $this->mdb->from('anm_anc_critical');
        $this->mdb->where('pregnant_id', $_pregnant_id);
        $data = $this->getData($this->mdb->get());

        if ($data) {
            foreach ($data as $key => $value) {
                switch ($value['anc_code']) {
                    case 1:
                        $data_array['anc1'] = 1;
                        break;
                    case 2:
                        $data_array['anc2'] = 1;
                        break;
                    case 3:
                        $data_array['anc3'] = 1;
                        break;
                    case 4:
                        $data_array['anc4'] = 1;
                        break;
                }
            }
        }
        return $data_array;
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function updatePregnancyWeekByAnmModel($_pregnant_id = NULL, $_week_id = NULL, $_refer_id = NULL) {
        if ($_pregnant_id != NULL and $_week_id != NULL and $_refer_id != NULL) {

            $this->mdb->trans_begin();

            $this->mdb->where(array('pregnant_id' => $_pregnant_id, 'week_id' => $_week_id));
            $this->mdb->update('master_asha_to_anm_alert', array('anm_status' => 1));

            $this->mdb->select('*');
            $this->mdb->from('master_asha_to_anm_alert');
            $this->mdb->where(array('pregnant_id' => $_pregnant_id, 'week_id' => $_week_id));
            $alert_data = $this->getData($this->mdb->get());
            if ($alert_data[0]['alert']) {
                $this->mdb->where(array('alert_id' => $alert_data[0]['alert_id'], 'asha_answer' => 2));
                $this->mdb->update('master_asha_anm_ans', array('anm_answer' => $_refer_id));
                $this->mdb->where(array('alert_id' => $alert_data[0]['alert_id'], 'asha_answer' => 1));
                $this->mdb->update('master_asha_anm_ans', array('anm_answer' => 1));
            }
            if ($this->mdb->trans_status() === FALSE) {
                $this->mdb->trans_rollback();
                return FALSE;
            } else {
                $this->mdb->trans_commit();
                return TRUE;
            }
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function updateDeliveryStatusModel($_pregnant_id = NULL, $_delivery_status_data = NULL) {
        if ($_pregnant_id != NULL and $_delivery_status_data != NULL) {
            $this->mdb->trans_begin();
            $this->mdb->where('pregnant_id', $_pregnant_id);
            $this->mdb->update('mira_pregnant_women', $_delivery_status_data);
            $this->mdb->select('*');
            $this->mdb->from('mira_pregnant_women');
            $this->mdb->where('pregnant_id', $_pregnant_id);
            $pregnant_data = $this->getData($this->mdb->get());
            $this->mdb->where('women_id', $pregnant_data[0]['women_id']);
            $this->mdb->update('mira_women', array('pregnant_status' => 0));
            if ($this->mdb->trans_status() === FALSE) {
                $this->mdb->trans_rollback();
                return FALSE;
            } else {
                $this->mdb->trans_commit();
                return TRUE;
            }
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function getChildVaccineListModel($_app_id = NULL) {
        if ($_app_id != NULL) {
            $this->mdb->select('mapping_child_vaccine_to_application.vaccine_id,master_vaccine_child.vaccine_name,master_vaccine_child.vaccine_code');
            $this->mdb->from('mapping_child_vaccine_to_application');
            $this->mdb->join('master_vaccine_child', 'mapping_child_vaccine_to_application.vaccine_id=master_vaccine_child.vaccine_id', 'INNER');
            $this->mdb->where(array('mapping_child_vaccine_to_application.app_id' => $_app_id, 'mapping_child_vaccine_to_application.running_status' => 1));
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function getChilVaccineDoneModel($_master_vaccine_list = NULL, $_child_data = NULL) {
        if ($_master_vaccine_list != NULL and $_child_data != NULL) {
            $this->mdb->select('master_child_immunization.*');
            $this->mdb->from('master_child_immunization');
            $this->mdb->where(array('child_id' => $_child_data['child_id']));
            $child_vaccine_list = $this->getData($this->mdb->get());
            $child_vaccine_list_due = $this->getDueVaccineListByDay(floor((time() - strtotime($_child_data['dob'])) / (60 * 60 * 24)));
            $child_vaccine_list_missed = $this->getMissedVaccineListByDay(floor((time() - strtotime($_child_data['dob'])) / (60 * 60 * 24)));
            for ($i = 0; $i < count($_master_vaccine_list); $i++) {
                for ($j = 0; $j < count($child_vaccine_list); $j++) {
                    if ($_master_vaccine_list[$i]['vaccine_id'] == $child_vaccine_list[$j]['vaccine_id']) {
                        $_master_vaccine_list[$i]['status'] = 1; // Dopne vaccine
                        break;
                    }
                }
                if (!isset($_master_vaccine_list[$i]['status']) and $child_vaccine_list_due) {
                    for ($k = 0; $k < count($child_vaccine_list_due); $k++) {
                        if ($_master_vaccine_list[$i]['vaccine_id'] == $child_vaccine_list_due[$k]['vaccine_id']) {
                            $_master_vaccine_list[$i]['status'] = 2; // Due vaccine
                            break;
                        }
                    }
                }
                if (!isset($_master_vaccine_list[$i]['status']) and $child_vaccine_list_missed) {
                    for ($l = 0; $l < count($child_vaccine_list_missed); $l++) {
                        if ($_master_vaccine_list[$i]['vaccine_id'] == $child_vaccine_list_missed[$l]['vaccine_id']) {
                            $_master_vaccine_list[$i]['status'] = 3; // Dose missed
                            break;
                        }
                    }
                }
                if (!isset($_master_vaccine_list[$i]['status'])) {
                    $_master_vaccine_list[$i]['status'] = 0; // Coming Vaccine
                }
            }
            return $_master_vaccine_list;
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function getChildListModel($_region_id = NULL, $_current_page = NULL, $_per_page_row = NULL) {
        if ($_region_id != NULL) {
            $this->mdb->select('mira_house.house_number,mira_women.name,mira_child.*');
            $this->mdb->from('mira_child');
            $this->mdb->join('mira_women', 'mira_women.women_id=mira_child.women_id', 'INNER');
            $this->mdb->join('mira_house', 'mira_women.house_id=mira_house.auto_id', 'INNER');
            $this->mdb->where(array('mira_house.region_id' => $_region_id,'mira_child.delete_status'=>0));
            if ($_current_page != NULL and $_per_page_row != NULL) {
                $this->mdb->order_by('mira_child.child_id', 'asc');
                $this->mdb->limit($_per_page_row, ($_current_page - 1) * $_per_page_row);
            }
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function getChildListDueVaccineModel($_region_id = NULL, $_current_page = NULL, $_per_page_row = NULL) {
        if ($_region_id != NULL) {
            $this->mdb->select('mira_house.house_number,mira_women.name,mira_child.*');
            $this->mdb->from('mira_child');
            $this->mdb->join('mira_women', 'mira_women.women_id=mira_child.women_id', 'INNER');
            $this->mdb->join('mira_house', 'mira_women.house_id=mira_house.auto_id', 'INNER');
            $this->mdb->where(array('mira_house.region_id' => $_region_id));
             if ($_current_page != NULL and $_per_page_row != NULL) {
                $this->mdb->order_by('mira_child.child_id', 'asc');
                $this->mdb->limit($_per_page_row, ($_current_page - 1) * $_per_page_row);
            }
            $child_data = $this->getData($this->mdb->get());
            if ($child_data) {
                for ($i = 0; $i < count($child_data); $i++) {
                    $child_data[$i]['due_vaccine'] = $this->getVaccineFromMappingTable(floor((time() - strtotime($child_data[$i]['dob'])) / (60 * 60 * 24)), $child_data[$i]['child_id']);
                }
                return $child_data;
            } else {
                return $child_data;
            }
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function updateVaccineChildUpdate($_vaccine_data = NULL) {
        if ($_vaccine_data != NULL) {
            $this->mdb->insert('master_child_immunization', $_vaccine_data);
            return ($this->mdb->affected_rows() != 1) ? FALSE : TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getDueVaccineListByDay($_day = NULL) {
        if (isset($_day)) {
            $this->mdb->select('*');
            $this->mdb->from('master_vaccine_child');
            $this->mdb->where('from <=', $_day);
            $this->mdb->where('to >=', $_day);
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getMissedVaccineListByDay($_day = NULL) {
        if (isset($_day)) {
            $this->mdb->select('*');
            $this->mdb->from('master_vaccine_child');
            $this->mdb->where('to <', $_day);
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getVaccineFromMappingTable($_day = NULL, $_child_id = NULL) {

        if ($_child_id != NULL) {
            $this->mdb->select('*');
            $this->mdb->from('master_vaccine_child');
            $this->mdb->where('from <=', $_day);
            $this->mdb->where('to >=', $_day);
            $data_master = $this->getData($this->mdb->get());
            $counter = count($data_master);
            $data_master_return = NULL;
            if ($data_master) {
                for ($i = 0; $i < $counter; $i++) {
                    if ($this->getChildVaccineModel($data_master[$i], $_child_id)) {
                        unset($data_master[$i]);
                    }
                }


                return array_values($data_master);
            }
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     */
    private function getChildVaccineModel($_data_master = NULL, $_child_id = NULL) {

        if ($_data_master['dependent_vaccine_id']) {
            $this->mdb->select('*');
            $this->mdb->from('master_child_immunization');
            $this->mdb->where(array('vaccine_id' => $_data_master['dependent_vaccine_id'], 'child_id' => $_child_id));
            $data = $this->getData($this->mdb->get());
            if ($data) {
                if (floor((time() - strtotime($data[0]['vaccine_date'])) / (60 * 60 * 24)) > $_data_master['gap_between_depentdent_vaccine']) {
                    $this->mdb->select('*');
                    $this->mdb->from('master_child_immunization');
                    $this->mdb->where(array('vaccine_id' => $_data_master['vaccine_id'], 'child_id' => $_child_id));
                    if ($this->getData($this->mdb->get())) {
                        return TRUE;
                    } else {
                        return FALSE;
                    }
                } else {
                    return TRUE;
                }
            } else {
                return TRUE;
            }
        } else {
            $this->mdb->select('*');
            $this->mdb->from('master_child_immunization');
            $this->mdb->where(array('vaccine_id' => $_data_master['vaccine_id'], 'child_id' => $_child_id));
            if ($this->getData($this->mdb->get())) {
                return TRUE;
            } else {
                return FALSE;
            }
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function getCurrentPregnantListWithCurrentWeekByAnmModel($_region_code = NULL, $_pregnant_status = NULL, $_app_id = NULL, $_page_number = '', $_per_page_row = '') {
        if ($_region_code != NULL and $_pregnant_status != NULL and $_app_id != NULL) {
            $region_code_array = $this->getRegionCodeBySuperRegion($_region_code, $_app_id);
            if ($region_code_array) {
                $women_list_data = $this->getPregnantIdListByAnmLevel($region_code_array, $_pregnant_status, $_page_number, $_per_page_row);
                if ($women_list_data) {
                    for ($i = 0; $i < count($women_list_data); $i++) {
                        $women_list_data[$i]['current_week'] = $this->getWeek(floor((time() - strtotime($women_list_data[$i]['pregnant_date'])) / (60 * 60 * 24)));
                        $women_list_data[$i]['week_qus_ans_list'] = $this->getWeekAnswerByWeekIdPregnantId($women_list_data[$i]['current_week'], $women_list_data[$i]['pregnant_id']);
                    }
                    return $women_list_data;
                } else {
                    return FALSE;
                }
            } else {
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }

    private function getWeekAnswerByWeekIdPregnantId($_week_id = NULL, $_pregnant_id = NULL) {
        if ($_week_id != NULL and $_pregnant_id != NULL) {
            $qus_ans_list_and_attempt = NULL;
            $qus_ans_list = NULL;
            $attempt = NULL;
            $question_list = $this->getPregnamtImmunizationQuestion();
            $this->mdb->select('*');
            $this->mdb->from('master_asha_to_anm_alert');
            $this->mdb->where(array('pregnant_id' => $_pregnant_id, 'week_id' => $_week_id));
            $data = $this->getData($this->mdb->get());
            if ($data) {
                if ($data[0]['alert']) {
                    $asha_anm_answer = $this->getAshaAnmAnswer($data[0]['alert_id']);
                    foreach ($question_list as $value) {
                        foreach ($asha_anm_answer as $value1) {
                            if ($value['qus_id'] === $value1['question_id']) {
                                if ($value1['anm_answer']) {
                                    $attempt = 2;
                                    $qus_ans_list[] = array('question_id' => $value['qus_id'], 'answer' => $value1['anm_answer']);
                                } else {
                                    $attempt = 1;
                                    $qus_ans_list[] = array('question_id' => $value['qus_id'], 'answer' => $value1['asha_answer']);
                                }
                                break;
                            }
                        }
                    }
                    $qus_ans_list_and_attempt = array('attempt' => $attempt, 'qus_ans_list' => $qus_ans_list);
                } elseif ($data[0]['anm_status']) {
                    foreach ($question_list as $value) {
                        $qus_ans_list[] = array('question_id' => $value['qus_id'], 'answer' => 1);
                    }
                    $qus_ans_list_and_attempt = array('attempt' => 2, 'qus_ans_list' => $qus_ans_list);
                } else {
                    foreach ($question_list as $value) {
                        $qus_ans_list[] = array('question_id' => $value['qus_id'], 'answer' => 1);
                    }
                    $qus_ans_list_and_attempt = array('attempt' => 1, 'qus_ans_list' => $qus_ans_list);
                }
            } else {
                foreach ($question_list as $value) {
                    $qus_ans_list[] = array('question_id' => $value['qus_id'], 'answer' => '-');
                }
                $qus_ans_list_and_attempt = array('attempt' => 0, 'qus_ans_list' => $qus_ans_list);
            }
            return $qus_ans_list_and_attempt;
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     */
    private function getAshaAnmAnswer($_alert_id = NULL) {
        if ($_alert_id != NULL) {
            $this->mdb->select('*');
            $this->mdb->from('master_asha_anm_ans');
            $this->mdb->where('alert_id', $_alert_id);
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getWeek($_number_of_days = NULL) {
        if ($_number_of_days != NULL) {
            $num_week = (int) ($_number_of_days / 7);
            if ($_number_of_days % 7) {
                $num_week++;
            }
            return $num_week;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getRegionCodeBySuperRegion($_super_region_code = NULL, $_app_id = NULL) {
        if ($_super_region_code != NULL) {
            $this->mdb->select('master_area_create_by_super.region_code');
            $this->mdb->from('master_area_create_by_super');
            $this->mdb->where(array('region_code_super' => $_super_region_code, 'app_id' => $_app_id));
            $data = $this->getData($this->mdb->get());
            if ($data) {
                $region_array = NULL;
                foreach ($data as $key => $value) {
                    $region_array[] = $value['region_code'];
                }
                return $region_array;
            } else {
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     */
    private function getPregnantIdListByAnmLevel($_region_code_array = NULL, $_pregnant_status = NULL, $_page_number = NULL, $_per_page_row = NULL) {
        if ($_region_code_array != NULL and $_pregnant_status != NULL) {
            $this->mdb->select('mira_women.name,mira_house.house_number,mira_pregnant_women.*');
            $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_in('mira_house.region_id', $_region_code_array);
            $this->mdb->where('mira_pregnant_women.pregnant_status', $_pregnant_status);
            if ($_page_number != NULL and $_per_page_row != NULL) {
                $this->mdb->order_by('mira_pregnant_women.pregnant_id', 'asc');
                $this->mdb->limit($_per_page_row, ($_page_number - 1) * $_per_page_row);
            }

            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getPregnantWomenImmunizatio($_region_code = NULL, $_current_page = NULL, $_per_page_row = NULL) {
        return $this->getPregnantIdListByRegionCode($_region_code, $_current_page, $_per_page_row);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getPregnantWomenImmunizatioCurrent($_region_code = NULL, $_current_page = NULL, $_per_page_row = NULL) {
        return $this->getPregnantIdListByRegionCodeCurrent($_region_code, $_current_page, $_per_page_row);
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getAnswerAshaANMCurrent($_pregnant_id = NULL, $_current_week = NULL) {
        if ($_pregnant_id != NULL) {
            $this->mdb->select('master_asha_to_anm_alert.*');
            $this->mdb->from('master_asha_to_anm_alert');
            $this->mdb->where(array('master_asha_to_anm_alert.pregnant_id' => $_pregnant_id, 'master_asha_to_anm_alert.week_id' => $_current_week));
            $asha_master = $this->getData($this->mdb->get());
            if ($asha_master) {
                for ($i = 0; $i < count($asha_master); $i++) {
                    if (!$asha_master[$i]['alert']) {
                        $temp = NULL;
                        for ($j = 0; $j < 5; $j++) {
                            $temp[] = array('question_id' => ($j + 1), 'asha_answer' => 1);
                        }
                        $asha_master[$i]['ans'] = $temp;
                    } else {
                        $this->mdb->select('*');
                        $this->mdb->from('master_asha_anm_ans');
                        $this->mdb->where('alert_id', $asha_master[$i]['alert_id']);
                        $asha_master[$i]['ans'] = $this->getData($this->mdb->get());
                    }
                }
            }
            //$num_week=  $this->getWeek(floor((strtotime($asha_master[0]['registration_date']) - strtotime($asha_master[0]['pregnant_date'])) / (60 * 60 * 24)));
            //print_r(count($asha_master));
            return $asha_master;
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getAnswerAshaANM($_pregnant_id = NULL) {
        if ($_pregnant_id != NULL) {
            $this->mdb->select('master_asha_to_anm_alert.*,mira_pregnant_women.registration_date,mira_pregnant_women.pregnant_date');
            $this->mdb->from('master_asha_to_anm_alert');
            $this->mdb->join('mira_pregnant_women', 'master_asha_to_anm_alert.pregnant_id=mira_pregnant_women.pregnant_id', 'INNER');
            $this->mdb->where('master_asha_to_anm_alert.pregnant_id', $_pregnant_id);
            $asha_master = $this->getData($this->mdb->get());
            if ($asha_master) {
                for ($i = 0; $i < count($asha_master); $i++) {

                    if (!$asha_master[$i]['alert']) {
                        $temp = NULL;
                        for ($j = 0; $j < 5; $j++) {
                            $temp[] = array('question_id' => ($j + 1), 'asha_answer' => 1);
                        }
                        $asha_master[$i]['ans'] = $temp;
                    } else {
                        $this->mdb->select('*');
                        $this->mdb->from('master_asha_anm_ans');
                        $this->mdb->where('alert_id', $asha_master[$i]['alert_id']);
                        $asha_master[$i]['ans'] = $this->getData($this->mdb->get());
                    }
                }
            }
            //$num_week=  $this->getWeek(floor((strtotime($asha_master[0]['registration_date']) - strtotime($asha_master[0]['pregnant_date'])) / (60 * 60 * 24)));
            //print_r(count($asha_master));


            return $asha_master;
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     */
    private function getPregnantIdListByRegionCodeCurrent($_region_code = NULL, $_current_page = NULL, $_per_page_row = NULL) {
        if ($_region_code != NULL) {
            $this->mdb->select('mira_women.name,mira_house.house_number,mira_pregnant_women.*');
            $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('mira_house.region_id', $_region_code);
            $this->mdb->where('mira_pregnant_women.pregnant_status', 1);
            $this->mdb->where('mira_house.delete_status', 0);
            $this->mdb->where('mira_women.delete_status', 0);
            if ($_current_page != NULL and $_per_page_row != NULL) {
                $this->mdb->order_by('mira_pregnant_women.pregnant_id', 'asc');
                $this->mdb->limit($_per_page_row, ($_current_page - 1) * $_per_page_row);
            }
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     */
    private function getPregnantIdListByRegionCode($_region_code = NULL, $_current_page = NULL, $_per_page_row = NULL) {
        if ($_region_code != NULL) {
            $this->mdb->select('mira_women.name,mira_house.house_number,mira_pregnant_women.*');
            $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_women.delete_status'=>0));
            
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function pregnantImmunizationSubmitModel($_date_alert = NULL, $_question_answer = NULL) {
        if ($_date_alert != NULL) {
            $this->mdb->trans_begin();
            $this->mdb->insert('master_asha_to_anm_alert', $_date_alert);

            if ($_question_answer != NULL and $_date_alert['alert']) {
                for ($i = 0; $i < count($_question_answer); $i++) {
                    $_question_answer[$i]['alert_id'] = $this->mdb->insert_id();
                }
                $this->mdb->insert_batch('master_asha_anm_ans', $_question_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 getCurrentWeekPregnancyModel($_pregnant_id = NULL) {
        if ($_pregnant_id != NULL) {
            $this->mdb->select('*');
            $this->mdb->from('mira_pregnant_women');
            $this->mdb->where('pregnant_id', $_pregnant_id);
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getCurrentWeekStatus($_pregnant_id = NULL, $_week_id = NULL) {
        if ($_pregnant_id != NULL and $_week_id != NULL) {
            $this->mdb->select('*');
            $this->mdb->from('master_asha_to_anm_alert');
            $this->mdb->where(array('pregnant_id' => $_pregnant_id, 'week_id' => $_week_id));
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function getPregnamtImmunizationQuestion() {
        $this->mdb->select('*');
        $this->mdb->from('immunization_pregnant_question');
        return $this->getData($this->mdb->get());
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getPregnantWomenByAshaRegionIdModel($_region_code = NULL) {
        if ($_region_code != NULL) {
            $this->mdb->select('mira_women.name,mira_pregnant_women.pregnant_id');
            $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_pregnant_women.pregnant_status' => 1, 'mira_house.region_id' => $_region_code));
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

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

}
