<?php
class Mega_function_report_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        $this->services = TBL_SERVICE;
        $this->mega_function = TBL_BILL_MEGA_FUNCTION;
        $this->deity = TBL_GOD_NAME;
        $this->star = TBL_STAR;
    }
    
    public function list_service_type(){
        $this->db->select('id,name as service_name');
        $this->db->where('menu_name',10);
        $this->db->where('status',1);
        return $this->db->get($this->services)->result();
    }
    
    public function get_data($start_date='',$end_date='',$type='',$date_type){
        $this->db->select('s.name as service_name, s.prefix, d.name as deity, CASE mf.period WHEN 1 THEN "Morning" ELSE "Evening" END as period_str, st.name as star, st2.name as star2, mf.*',FALSE);
        $this->db->join($this->services . ' as s','s.id = mf.service_id');
        $this->db->join($this->deity . ' as d','d.id = mf.deity_id');
        $this->db->join($this->star . ' as st','st.id = mf.star1','left');
         $this->db->join($this->star . ' as st2','st2.id = mf.star2','left');
        if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('mf.date >=',date('Y-m-d 00:00:00',strtotime($start_date)));
                $this->db->where('mf.date <=',date('Y-m-d 23:59:59',strtotime($end_date)));
            } else {
                $this->db->where('mf.date >=',date('Y-m-d 00:00:00'));
                $this->db->where('mf.date <=',date('Y-m-d 23:59:59'));
            } 
        } elseif($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('mf.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
                $this->db->where('mf.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
            } else {
                $this->db->where('mf.created_on >=',date('Y-m-d 00:00:00'));
                $this->db->where('mf.created_on <=',date('Y-m-d 23:59:59'));
            }
        }
        if(!empty($type)) {
            $this->db->where('mf.service_id',$type);
        }
        #$this->db->where('mf.status',1);
        $this->db->where('mf.book_status',1);
        return $this->db->get($this->mega_function . ' as mf')->result();
    }
}
?>