<?php
class Madappalli_report_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        $this->services = TBL_SERVICE;
        $this->madappalli = TBL_BM;
        $this->madappalli_item = TBL_BMI;
        $this->deity = TBL_GOD_NAME;
        $this->prasadam = TBL_PRASADAM;
        $this->madappalli_master = TBL_MADAPPALLI;
    }
    
    public function get_data($start_date='',$end_date='',$period='',$date_type){
        $this->db->select('s.name as service_name, s.prefix, d.name as deity, mada.prasadam_name as prasadam, mi.bmi_qty, mi.bmi_price, mi.bmi_sub_total, CASE m.bm_period WHEN 1 THEN "Morning" ELSE "Evening" END as period_str, m.*',FALSE);
        $this->db->join($this->services . ' as s','s.id = m.service_id','LEFT');
        $this->db->join($this->madappalli_item . ' as mi','mi.bm_id	 = m.id AND mi.status = 1','LEFT');
        $this->db->join($this->deity . ' as d','d.id = mi.god_id','LEFT');
        $this->db->join($this->madappalli_master . ' as mada','mada.id = mi.prasadam_id','LEFT');
        if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('m.bm_fn_date >=',date('Y-m-d 00:00:00',strtotime($start_date)));
                $this->db->where('m.bm_fn_date <=',date('Y-m-d 23:59:59',strtotime($end_date)));
            } elseif(!empty($start_date) && (empty($end_date)) ) {
                $this->db->where('m.bm_fn_date >=',date('Y-m-d 00:00:00',strtotime($start_date)));
                $this->db->where('m.bm_fn_date <=',date('Y-m-d 23:59:59',strtotime($start_date)));
            } else {
                $this->db->where('m.bm_fn_date >=',date('Y-m-d 00:00:00'));
                $this->db->where('m.bm_fn_date <=',date('Y-m-d 23:59:59'));
            } 
        } else if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('m.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
                $this->db->where('m.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
            } elseif(!empty($start_date) && (empty($end_date)) ) {
                $this->db->where('m.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
                $this->db->where('m.created_on <=',date('Y-m-d 23:59:59',strtotime($start_date)));
            } else {
                $this->db->where('m.created_on >=',date('Y-m-d 00:00:00'));
                $this->db->where('m.created_on <=',date('Y-m-d 23:59:59'));
            } 
        }
        if(!empty($period)) {
            $this->db->where('m.bm_period',$period);
        }
        
        #$this->db->where('m.status',1);
		#$this->db->group_by('mi.bm_id');
		#$this->db->group_by('m.bm_rec_no');
        #$this->db->where('m.bm_book_status',1);
        return $this->db->get($this->madappalli . ' as m')->result();
    }
}
?>