<?php
class Prasadam_sale_report_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        $this->services = TBL_SERVICE;
        $this->prasadam = TBL_BP;
        $this->prasadam_item = TBL_BPI;
        $this->star = TBL_STAR;
        $this->prasadam_list = TBL_PRASADAM;
        $this->unit = TBL_UNIT;
        $this->god = TBL_GOD_NAME;
    }
    
    public function service_types() {
        $this->db->where('menu_name',15);
        $this->db->where('status',1);
        return $this->db->get($this->services)->result();
    }
    
    public function god_list() {
        $this->db->where('status',1);
        return $this->db->get($this->god)->result();
    }
    
    public function unit_list() {
        $this->db->where('status',1);
        return $this->db->get($this->unit)->result();
    }
    
    public function get_data($start_date='',$end_date='',$type='',$god=''){
        $this->db->select('p.bp_fn_date, SUM(pi.bpi_qty) as qty, SUM(pi.bpi_sub_total) as total, pl.name as prasadam, s.name as service_name, u.unit_name, g.name as god_name',FALSE);
        $this->db->join($this->prasadam . ' as p','p.id = pi.bp_id');
        $this->db->join($this->prasadam_list . ' as pl','pl.id = pi.prasadam_id');
        $this->db->join($this->services . ' as s','s.id = p.service_id');
        $this->db->join($this->unit . ' as u','u.id = pl.unit','left');
        $this->db->join($this->god . ' as g','g.id = pi.god_id','left');
        if(!empty($start_date) && !empty($end_date)) {
            $this->db->where('p.bp_fn_date >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('p.bp_fn_date <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('p.bp_fn_date >=',date('Y-m-d 00:00:00'));
            $this->db->where('p.bp_fn_date <=',date('Y-m-d 23:59:59'));
        }
        if(!empty($type)) {
            $this->db->where('p.service_id',$type);
        }
        
        if(!empty($god)) {
            $this->db->where('pi.god_id',$god);
        }
        #$this->db->where('p.status',1);
        $this->db->where('pi.status',1);
        $this->db->group_by('pi.prasadam_id');
        return $this->db->get($this->prasadam_item . ' as pi')->result();
    }
}
?>