<?php
class Festival_function_report_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        $this->services = TBL_SERVICE;
        $this->festival = TBL_BILL_FESTIVAL_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',5);
        $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, star.name as star, star2.name as star2, CASE f.period WHEN 1 THEN "Morning" ELSE "Evening" END as period_str, f.*',FALSE);
        $this->db->join($this->services . ' as s','s.id = f.service_id');
        $this->db->join($this->deity . ' as d','d.id = f.deity_id');
        $this->db->join($this->star . ' as star','star.id = f.star1','left');
        $this->db->join($this->star . ' as star2','star2.id = f.star2','left');
        if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('f.date >=',date('Y-m-d',strtotime($start_date)));
                $this->db->where('f.date <=',date('Y-m-d',strtotime($end_date)));
            } else {
                $this->db->where('f.date >=',date('Y-m-d'));
                $this->db->where('f.date <=',date('Y-m-d'));
            }
        } elseif($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('f.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
                $this->db->where('f.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
            } else {
                $this->db->where('f.created_on >=',date('Y-m-d 00:00:00'));
                $this->db->where('f.created_on <=',date('Y-m-d 23:59:59'));
            }

        }
        if(!empty($type)) {
            $this->db->where('f.service_id',$type);
        }
        #$this->db->where('f.status',1);
        $this->db->where('f.book_status',1);
        return $this->db->get($this->festival . ' as f')->result();
    }
}
?>