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

    //put your code here
    var $mdb;
    var $vdb;

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

    /**
     * 
     * 
     * 
     * 
     */
    public function assignVaccinePlannerFormSubmitModel($plannerId = NULL,$appId = NULL) {
        if ($appId != NULL and $plannerId != NULL) {
            $this->mdb->trans_begin();
            $this->mdb->where('app_id', $appId);
            $this->mdb->update('application_create', array('plannerId' => $plannerId));
            if ($this->mdb->trans_status() === FALSE) {
                $this->mdb->trans_rollback();
                return FALSE;
            } else {
                $this->mdb->trans_commit();
                return TRUE;
            }
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function assignVaccinePlannerFormModel() {

        $this->vdb = $this->load->database('vdb', TRUE); //This database use for get all vaccine planner.

        $this->mdb->select('application_create.app_id as appId,application_create.app_name as appName');
        $this->mdb->from('application_create');
        $this->mdb->where(array('application_create.plannerId' => NULL));

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

    /**
     * 
     * 
     * 
     * 
     */
    public function getVaccinePlannerModel() {


        $this->vdb = $this->load->database('vdb', TRUE); //This database use for get all vaccine planner.

        $this->vdb->select('masterPlanner.plannerId as plannerId,masterPlanner.plannername as plannerName');
        $this->vdb->from('masterPlanner');
        $this->vdb->where(array('masterPlanner.isActive' => 2));

        return $this->getData($this->vdb->get());
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getCurrentUserRightAssignedModel($_app_id = NULL, $_region_code = NULL, $_user_level = NULL) {
        if ($_app_id != NULL and $_region_code != NULL) {
            if ($_user_level == 1) {
                $this->mdb->select('mapping_app_rights.*,sadmin_app_rights.right_name');
                $this->mdb->from('mapping_app_rights');
                $this->mdb->join('sadmin_app_rights', 'sadmin_app_rights.right_id=mapping_app_rights.right_id', 'INNER');
                $this->mdb->where(array('mapping_app_rights.app_id' => $_app_id, 'mapp_right_status' => 1));
            } else {
                $this->mdb->select('mapping_area_rights_to_apps_right.*,sadmin_app_rights.right_name');
                $this->mdb->from('mapping_area_rights_to_apps_right');
                $this->mdb->join('sadmin_app_rights', 'sadmin_app_rights.right_id=mapping_area_rights_to_apps_right.right_id', 'INNER');
                $this->mdb->where(array('app_id' => $_app_id, 'region_code' => $_region_code, 'mapping_status' => 1));
            }
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function getRegionLevelModel($_app_id = NULL, $_level_id = NULL, $_user_level = NULL) {
        if ($_app_id != null and $_level_id != NULL and $_user_level != NULL) {
            $this->mdb->select('*');
            $this->mdb->from('application_hierarchy');
            $this->mdb->where(array('application_hierarchy.app_id' => $_app_id, 'application_hierarchy.hierar_level_id <' => $_level_id, 'application_hierarchy.hierar_level_id >' => $_user_level));
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function updateMiraApplicationRightModel($_app_id = NULL, $_right_array = NULL) {
        if ($_app_id != NULL) {
            $this->mdb->trans_begin();
            if ($_right_array != NULL) {
                $this->mdb->where('app_id', $_app_id);
                $this->mdb->where_not_in('right_id', $_right_array);
                $this->mdb->update('mapping_app_rights', array('mapp_right_status' => 0));
                $this->mdb->where('app_id', $_app_id);
                $this->mdb->where_in('right_id', $_right_array);
                $this->mdb->update('mapping_app_rights', array('mapp_right_status' => 1));
                $this->mdb->select('right_id');
                $this->mdb->from('mapping_app_rights');
                $this->mdb->where(array('app_id' => $_app_id));
                $right_id = NULL;
                foreach ($this->getData($this->mdb->get()) as $value) {
                    $right_id[] = $value['right_id'];
                }
                $insert_batch_array = NULL;
                for ($i = 0; $i < count($_right_array); $i++) {
                    if (!in_array($_right_array[$i], $right_id)) {
                        $insert_batch_array[] = array('app_id' => $_app_id, 'right_id' => $_right_array[$i], 'mapp_right_status' => 1);
                    }
                }
                if ($insert_batch_array != NULL) {
                    $this->mdb->insert_batch('mapping_app_rights', $insert_batch_array);
                }
            } else {
                $this->mdb->where('app_id', $_app_id);
                $this->mdb->update('mapping_app_rights', array('mapp_right_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 getActivatedApplicationRightModel() {
        $this->mdb->select('*');
        $this->mdb->from('mapping_application_admin');
        $this->mdb->join('application_create', 'mapping_application_admin.app_id=application_create.app_id', 'INNER');
        return $this->getData($this->mdb->get());
    }

    /**
     * @param Integer $app_id This is is current application Id.
     * @return Right This function return application details.
     * 
     * 
     * 
     */
    public function getAppDetailModel($_app_id = NULL) {
        if ($_app_id != NULL) {
            $this->mdb->select('*');
            $this->mdb->from('mapping_application_admin');
            $this->mdb->join('application_create', 'mapping_application_admin.app_id=application_create.app_id', 'INNER');
            $this->mdb->where('mapping_application_admin.app_id', $_app_id);
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getApplicationHirecharchyLevel($_app_id = NULL, $_user_level = NULL) {
        if ($_app_id != NULL and $_user_level != NULL) {
            $this->mdb->select('*');
            $this->mdb->from('application_hierarchy');
            $this->mdb->where(array('application_hierarchy.app_id' => $_app_id, 'application_hierarchy.hierar_level_id >' => $_user_level));
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getAppRightModel($_array_conditio = NULL) {
        if ($_array_conditio != NULL) {
            $this->mdb->select('*');
            $this->mdb->from('mapping_app_rights');
            $this->mdb->where($_array_conditio);
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function mappedAppActivateModel($_app_id = NULL) {
        if ($_app_id != NULL) {
            $this->mdb->where('app_id', $_app_id);
            $this->mdb->update('mapping_application_admin', array('activate_status' => 1, 'activate_date' => date('Y-m-d'), 'expire_date' => date('Y-m-d')));
            $this->mdb->where(array('app_id' => $_app_id, 'create_region_id' => $_app_id));
            $this->mdb->update('master_login_user', array('user_activation_status' => 1));
            return ($this->mdb->affected_rows() > 0) ? TRUE : FALSE;
        } else {
            return FALSE;
        }
    }

    public function mappedAppDeactivateModel($_app_id = NULL) {
        if ($_app_id != NULL) {
            $this->mdb->where('app_id', $_app_id);
            $this->mdb->update('mapping_application_admin', array('activate_status' => 0, 'activate_date' => NULL, 'expire_date' => NULL));
            $this->mdb->where(array('app_id' => $_app_id, 'create_region_id' => $_app_id));
            $this->mdb->update('master_login_user', array('user_activation_status' => 0));
            return ($this->mdb->affected_rows() > 0) ? TRUE : FALSE;
        } else {
            return FALSE;
        }
    }

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

    /**
     * 
     * 
     * 
     * 
     */
    public function getMappedAppAdminModel() {
        $this->mdb->select('mapping_application_admin.app_id,mapping_application_admin.mapping_date,mapping_application_admin.expire_date,mapping_application_admin.activate_date,mapping_application_admin.activate_status,admin_user_details.admin_name,application_create.app_name');
        $this->mdb->from('mapping_application_admin');
        $this->mdb->join('admin_user_details', 'admin_user_details.admin_id=mapping_application_admin.admin_id', 'INNER');
        $this->mdb->join('application_create', 'mapping_application_admin.app_id=application_create.app_id', 'INNER');
        $this->mdb->where(array('application_create.app_mapping_status' => 1, 'admin_user_details.admin_mapping_status' => 1));
        return $this->getData($this->mdb->get());
    }

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

    /**
     * 
     * 
     * 
     */
    public function applicationDeleteModel($_app_id = NULL) {
        if ($_app_id != NULL) {
            $this->mdb->where('app_id', $_app_id);
            $this->mdb->delete('application_create');
            return ($this->mdb->affected_rows() != 1) ? FALSE : TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getApplicationByCondition($_condition_array = NULL) {
        if ($_condition_array != NULL) {
            $this->mdb->select('*');
            $this->mdb->from('application_create');
            $this->mdb->where($_condition_array);
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function updateMiraApplication($_update_data = NULL, $_update_condition = NULL) {
        if ($_update_data != NULL and $_update_condition != NULL) {
            $this->mdb->where($_update_condition);
            $this->mdb->update('application_create', $_update_data);
            $this->mdb->select('*');
            $this->mdb->from('application_create');
            $this->mdb->where($_update_condition);
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function createApplicationModel($_application_data = NULL, $_application_hierarchy = NULL) {

        if ($_application_data != NULL and $_application_hierarchy != NULL) {
            $this->db->trans_start();

            $this->mdb->insert('application_create', $_application_data);
            $app_id = $this->mdb->insert_id();

            for ($i = 0; $i < count($_application_hierarchy); $i++) {
                $_application_hierarchy[$i]['app_id'] = $app_id;
            }
            $this->mdb->select('*');
            $this->mdb->from('master_vaccine_child');
            $data_vaccine = NULL;
            foreach ($this->getData($this->mdb->get()) as $value) {
                $data_vaccine[] = array('app_id' => $app_id, 'vaccine_id' => $value['vaccine_id']);
            }

            $this->mdb->select('*');
            $this->mdb->from('master_anc');
            foreach ($this->getData($this->mdb->get()) as $value) {
                $anc_data[] = array('app_id' => $app_id, 'anc_code' => $value['anc_id']);
            }


            $this->mappedANCToApplication($anc_data);
            $this->mappedChildVaccineToApplication($data_vaccine);
            $this->createApplicationHierarchy($_application_hierarchy);




            if ($this->db->trans_status() === FALSE) {
                $this->db->trans_rollback();
                return FALSE;
            } else {
                $this->db->trans_commit();
                return TRUE;
            }
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function createApplicationHierarchy($_application_hierarchy = NULL) {
        if ($_application_hierarchy != NULL) {
            $this->mdb->insert_batch('application_hierarchy', $_application_hierarchy);
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     */
    public function mappedChildVaccineToApplication($_vaccine_data = NULL) {
        if ($_vaccine_data != NULL) {
            $this->mdb->insert_batch('mapping_child_vaccine_to_application', $_vaccine_data);
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     */
    public function mappedANCToApplication($_anc_data = NULL) {
        if ($_anc_data != NULL) {
            $this->mdb->insert_batch('mapping_application_anc', $_anc_data);
        } else {
            return FALSE;
        }
    }

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

}
