<?php
class Kattalai_archanai_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',8);
        $this->db->where('status',1);
        return $this->db->get($this->services)->result();
    }
    
    public function get_daily_data($start_date='',$end_date='',$type='',$date_type=''){
        $this->db->select('s.name as service_name, s.prefix, k.bk_rec_no, k.bk_name_1, star.name as star, k.bk_name_2, k.bk_name_3, k.bk_tamil_name_1, k.bk_tamil_name_2, k.bk_tamil_name_3, star2.name as star2, star3.name as star3, ki.bki_from_date, ki.bki_to_date, ki.bki_amount, ki.bki_day_type, ki.bki_dates, ki.bki_god_ids, ki.bki_no_days, ki.bki_week_day, "Daily" as day_type, ki.created_on, k.bk_desc',FALSE);
        $this->db->join($this->services . ' as s','s.id = k.service_id');
        $this->db->join($this->star . ' as star','star.id = k.bk_star_1','left');
        $this->db->join($this->star . ' as star2','star2.id = k.bk_star_2','left');
        $this->db->join($this->star . ' as star3','star3.id = k.bk_star_3','left');
        $this->db->join('thiru_bill_kattalai_items as ki','ki.bk_id = k.id AND ki.status = 1 AND ki.bki_day_type = 1');
        
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
            } else {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00'));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59'));
            }
        } else if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            } elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))');
            }
        }
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1);
        return $this->db->get('thiru_bill_kattalai as k')->result();
    }
	
	public function get_daily_prasadam_data($start_date='',$end_date='',$type='',$date_type=''){
        $this->db->select('s.name as service_name, s.prefix, k.bk_rec_no, k.bk_name_1, star.name as star, k.bk_name_2, k.bk_tamil_name_1, k.bk_tamil_name_2, k.bk_tamil_name_3, star2.name as star2, star3.name as star3, k.bk_begin_date as bki_from_date, k.bk_end_date as bki_to_date, kp.sub_total as bki_amount, kp.bkp_day_type as bki_day_type, kp.dates as bki_dates, kp.deity_id as bki_god_ids, kp.bkp_no_days as bki_no_days, kp.bkp_week_day as bki_week_day, "Daily" as day_type, k.created_on, k.bk_desc',FALSE);
        $this->db->join($this->services . ' as s','s.id = k.service_id');
        $this->db->join($this->star . ' as star','star.id = k.bk_star_1','left');
        $this->db->join($this->star . ' as star2','star2.id = k.bk_star_2','left');
        $this->db->join($this->star . ' as star3','star3.id = k.bk_star_3','left');
        $this->db->join('thiru_bill_kattalai_prasadam as kp','kp.kattalai_id = k.id AND kp.status = 1 AND kp.bkp_day_type = 1');
        
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d',strtotime($start_date)));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d',strtotime($end_date)));
            } else {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d'));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d'));
            }
        } else if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            } elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d') . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d') . '"))');
            }
        }
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1);
        return $this->db->get('thiru_bill_kattalai as k')->result();
    }
    
    public function get_weekly_data($start_date='',$end_date='',$type='',$date_type=''){
        $this->db->select('s.name as service_name, s.prefix, k.bk_rec_no, k.bk_name_1, star.name as star, k.bk_name_2, k.bk_tamil_name_1, k.bk_tamil_name_2, k.bk_tamil_name_3, star2.name as star2, star3.name as star3, ki.bki_from_date, ki.bki_to_date, ki.bki_amount, ki.bki_day_type, ki.bki_dates, ki.bki_god_ids, ki.bki_no_days, ki.bki_week_day, "Weekly" as day_type, ki.created_on, k.bk_desc',FALSE);
        $this->db->join($this->services . ' as s','s.id = k.service_id');
        $this->db->join($this->star . ' as star','star.id = k.bk_star_1','left');
        $this->db->join($this->star . ' as star2','star2.id = k.bk_star_2','left');
        $this->db->join($this->star . ' as star3','star3.id = k.bk_star_3','left');
        $this->db->join('thiru_bill_kattalai_items as ki','ki.bk_id = k.id AND ki.status = 1 AND ki.bki_day_type = 2');
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
            } else {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00'));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59'));
            }
        } else if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            } elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))');
            } 
        }
        
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1);
        return $this->db->get('thiru_bill_kattalai as k')->result();
    }
	
	public function get_weekly_prasadam_data($start_date='',$end_date='',$type='',$date_type){
        $this->db->select('s.name as service_name, s.prefix, k.bk_rec_no, k.bk_name_1, star.name as star, k.bk_name_2, k.bk_tamil_name_1, k.bk_tamil_name_2, k.bk_tamil_name_3, star2.name as star2, star3.name as star3, k.bk_begin_date as bki_from_date, k.bk_end_date as bki_to_date, kp.sub_total as bki_amount, kp.bkp_day_type as bki_day_type, kp.dates as bki_dates, kp.deity_id as bki_god_ids, kp.bkp_no_days as bki_no_days, kp.bkp_week_day as bki_week_day, "Weekly" as day_type, k.created_on, k.bk_desc',FALSE);
        $this->db->join($this->services . ' as s','s.id = k.service_id');
        $this->db->join($this->star . ' as star','star.id = k.bk_star_1','left');
        $this->db->join($this->star . ' as star2','star2.id = k.bk_star_2','left');
        $this->db->join($this->star . ' as star3','star3.id = k.bk_star_3','left');
        $this->db->join('thiru_bill_kattalai_prasadam as kp','kp.kattalai_id = k.id AND kp.status = 1 AND kp.bkp_day_type = 2');
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d',strtotime($start_date)));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d',strtotime($end_date)));
            } else {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d'));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d'));
            }
        } else if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            } elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d') . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d') . '"))');
            }
        }
        
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1);
        return $this->db->get('thiru_bill_kattalai as k')->result();
    }
    
    public function get_multiple_data($start_date='',$end_date='',$type='',$date_type=''){
        $this->db->select('s.name as service_name, s.prefix, k.bk_rec_no, k.bk_name_1, star.name as star, k.bk_name_2,  k.bk_tamil_name_1, k.bk_tamil_name_2, k.bk_tamil_name_3, star2.name as star2, star3.name as star3, ki.bki_from_date, ki.bki_to_date, ki.bki_amount, ki.bki_day_type, ki.bki_dates, ki.bki_god_ids, ki.bki_no_days, ki.bki_week_day, "Multiple Date" as day_type, ki.created_on,k.bk_desc',FALSE);
        $this->db->join($this->services . ' as s','s.id = k.service_id');
        $this->db->join($this->star . ' as star','star.id = k.bk_star_1','left');
        $this->db->join($this->star . ' as star2','star2.id = k.bk_star_2','left');
        $this->db->join($this->star . ' as star3','star3.id = k.bk_star_3','left');
        $this->db->join('thiru_bill_kattalai_items as ki','ki.bk_id = k.id AND ki.status = 1 AND ki.bki_day_type = 3');
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
            } else {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00'));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59'));
            }
        } else if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            }  elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                 $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))');
            } 
        }
       /* if(!empty($start_date) && !empty($end_date)) {
            $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59'));
        }*/
        
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1);
        return $this->db->get('thiru_bill_kattalai as k')->result();
    }
	
	public function get_multiple_prasadam_data($start_date='',$end_date='',$type='',$date_type=''){
        $this->db->select('s.name as service_name, s.prefix, k.bk_rec_no, k.bk_name_1, star.name as star, k.bk_name_2,  k.bk_tamil_name_1, k.bk_tamil_name_2, k.bk_tamil_name_3, star2.name as star2, star3.name as star3, k.bk_begin_date as bki_from_date, k.bk_end_date as bki_to_date, kp.sub_total as bki_amount, kp.bkp_day_type as bki_day_type, kp.dates as bki_dates, kp.deity_id as bki_god_ids, kp.bkp_no_days as bki_no_days, kp.bkp_week_day as bki_week_day, "Multiple Date" as day_type, k.created_on,k.bk_desc',FALSE);
        $this->db->join($this->services . ' as s','s.id = k.service_id');
        $this->db->join($this->star . ' as star','star.id = k.bk_star_1','left');
        $this->db->join($this->star . ' as star2','star2.id = k.bk_star_2','left');
        $this->db->join($this->star . ' as star3','star3.id = k.bk_star_3','left');
        $this->db->join('thiru_bill_kattalai_prasadam as kp','kp.kattalai_id = k.id AND kp.status = 1 AND kp.bkp_day_type = 3');
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d',strtotime($start_date)));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d',strtotime($end_date)));
            } else {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d'));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d'));
            }
        } else if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            } elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d') . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d') . '"))');
            }
        }
       /* if(!empty($start_date) && !empty($end_date)) {
            $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59'));
        }*/
        
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1);
        return $this->db->get('thiru_bill_kattalai as k')->result();
    }
    
    public function get_deity_name($ids) {
        $this->db->select('GROUP_CONCAT(name SEPARATOR ",") as deity',FALSE);
        $this->db->where_in('id',$ids);
        return $this->db->get($this->deity)->row();
    }
    
    public function getGodName($ids) {
        $e = explode(',',$ids);
        $god = array();
        foreach($e as $k => $v) {
            $god[] = $v;
        }
        $this->db->select('GROUP_CONCAT(name SEPARATOR ",") as deity',FALSE);
        $this->db->where_in('id',$god);
        return $this->db->get($this->deity)->row();
    } 
    
    public function get_gods_by_kattalai_items() {
        $this->db->select('d.name ,d.id', false);
		$this->db->join($this->deity . ' as d', 'FIND_IN_SET(d.id , ki.bki_god_ids)');
        $this->db->group_by('d.id');
        $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))');
		return $this->db->get('thiru_bill_kattalai_items as ki')->result();
    }
    
    public function get_today_record_by_god() {
        //$select = ', (SELECT GROUP_CONCAT(d.name SEPARATOR ",") as deity FROM ' . $this->deity . ' as d WHERE d.id IN ki.bki_god_ids ) as god';
        $this->db->select('ki.bki_god_ids, k.bk_name_1, k.bk_name_2, k.bk_tamil_name_2, k.bk_tamil_name_1, s.name as star, s.tamil_name as star_tamil,s2.name as star2, s2.tamil_name as star_tamil_2, s3.tamil_name as star_tamil_3, s3.name as star3',FALSE);
        $this->db->join('thiru_bill_kattalai as k','k.id = ki.bk_id AND k.bk_book_status = 1');
        $this->db->join($this->star . ' as s','s.id = k.bk_star_1','left');
        $this->db->join($this->star . ' as s2','s2.id = k.bk_star_2','left');
        $this->db->join($this->star . ' as s3','s3.id = k.bk_star_3','left');
        #$this->db->where('ki.status !=',2);
        $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))');        
        return $this->db->get('thiru_bill_kattalai_items as ki')->result();
    } 
    
    public function get_items_by_god($id) {
        $this->db->select('k.*,ki.bki_god_ids');
        $this->db->join('thiru_bill_kattalai as k','k.id = ki.bk_id');
        $this->db->where_in('ki.bki_god_ids',$id);
        #$this->db->where('ki.status !=',2);
        return $this->db->get('thiru_bill_kattalai_items as ki')->result();
    }
    
    public function get_details_gods_by_kattalai_items_daily($start_date='',$end_date='',$type='',$date_type='') {
        $this->db->select('d.name , d.tamil_name, d.id, ki.bki_day_type, ki.bki_dates, ki.bki_week_day, ki.created_on', false);
		$this->db->join($this->deity . ' as d', 'FIND_IN_SET(d.id , ki.bki_god_ids)');
        $this->db->join('thiru_bill_kattalai as k', 'k.id = ki.bk_id','left');
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
            } else {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00'));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59'));
            }
        } else if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            } elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))');
            }
        }
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1);
        $this->db->where('ki.status',1);
        $this->db->where('ki.bki_day_type',1);
        $this->db->group_by('d.id');
		return $this->db->get('thiru_bill_kattalai_items as ki')->result();
    }
	
	public function get_details_gods_by_kattalai_prasadam_daily($start_date='',$end_date='',$type='',$date_type='') {
        $this->db->select('d.name , d.tamil_name, d.id, kp.bkp_day_type as bki_day_type, kp.dates as bki_dates, kp.bkp_week_day as bki_week_day, kp.created_on', false);
		$this->db->join($this->deity . ' as d', 'FIND_IN_SET(d.id , kp.deity_id)');
        $this->db->join('thiru_bill_kattalai as k', 'k.id = kp.kattalai_id','left');
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d',strtotime($start_date)));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d',strtotime($end_date)));
            } else {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d'));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d'));
            }
        } else if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            } elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d') . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d') . '"))');
            }
        }
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1);
        $this->db->where('kp.status',1);
        $this->db->where('kp.bkp_day_type',1);
        $this->db->group_by('d.id');
		return $this->db->get('thiru_bill_kattalai_prasadam as kp')->result();
    }
    
    
    public function get_details_gods_by_kattalai_items_weekly($start_date='',$end_date='',$type='',$date_type='') {
        $this->db->select('d.name , d.tamil_name, d.id, ki.bki_day_type, ki.bki_dates, ki.bki_week_day, ki.created_on', false);
		$this->db->join($this->deity . ' as d', 'FIND_IN_SET(d.id , ki.bki_god_ids)');
        $this->db->join('thiru_bill_kattalai as k', 'k.id = ki.bk_id','left');
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
            } else {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00'));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59'));
            }
        } else if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            } elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))');
            } 
        }
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1);
        $this->db->where('ki.status',1);
        $this->db->where('ki.bki_day_type',2);
        $this->db->group_by('d.id');
		return $this->db->get('thiru_bill_kattalai_items as ki')->result();
    }
	
	public function get_details_gods_by_kattalai_prasadam_weekly($start_date='',$end_date='',$type='',$date_type='') {
        $this->db->select('d.name , d.tamil_name, d.id, kp.bkp_day_type as bki_day_type, kp.dates as bki_dates, kp.bkp_week_day as bki_week_day, kp.created_on', false);
		$this->db->join($this->deity . ' as d', 'FIND_IN_SET(d.id , kp.deity_id)');
        $this->db->join('thiru_bill_kattalai as k', 'k.id = kp.kattalai_id','left');
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d',strtotime($start_date)));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d',strtotime($end_date)));
            } else {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d'));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d'));
            }
        } else if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            } elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d') . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d') . '"))');
            }
        }
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1);
        $this->db->where('kp.status',1);
        $this->db->where('kp.bkp_day_type',2);
        $this->db->group_by('d.id');
		return $this->db->get('thiru_bill_kattalai_prasadam as kp')->result();
    }
    
    public function get_details_gods_by_kattalai_items_multiple($start_date='',$end_date='',$type='',$date_type='') {
        $this->db->select('d.name ,d.tamil_name, d.id, ki.bki_day_type, ki.bki_dates, ki.bki_week_day, ki.created_on', false);
		$this->db->join($this->deity . ' as d', 'FIND_IN_SET(d.id , ki.bki_god_ids)');
        $this->db->join('thiru_bill_kattalai as k', 'k.id = ki.bk_id','left');
        if($date_type == 2) {
           if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
            } else {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00'));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59'));
            }
        } else if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            }  elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                 $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))');
            }
        }
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1);
        $this->db->where('ki.status',1);
        $this->db->where('ki.bki_day_type',3);
        $this->db->group_by('d.id');
		return $this->db->get('thiru_bill_kattalai_items as ki')->result();
    }
	
	public function get_details_gods_by_kattalai_prasadam_multiple($start_date='',$end_date='',$type='',$date_type='') {
        $this->db->select('d.name , d.tamil_name, d.id, kp.bkp_day_type as bki_day_type, kp.dates as bki_dates, kp.bkp_week_day as bki_week_day, kp.created_on', false);
		$this->db->join($this->deity . ' as d', 'FIND_IN_SET(d.id , kp.deity_id)');
        $this->db->join('thiru_bill_kattalai as k', 'k.id = kp.kattalai_id','left');
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d',strtotime($start_date)));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d',strtotime($end_date)));
            } else {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d'));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d'));
            }
        } else if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            } elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d') . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d') . '"))');
            }
        }
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1);
        $this->db->where('kp.status',1);
        $this->db->where('kp.bkp_day_type',3);
        $this->db->group_by('d.id');
		return $this->db->get('thiru_bill_kattalai_prasadam as kp')->result();
    }
    
    public function get_details_data_by_god_daily($start_date='',$end_date='',$type='',$date_type='') {
        $this->db->select('k.*, ki.*, s.name as star, s.tamil_name as star_tamil,s2.name as star2, s2.tamil_name as star_tamil_2, s3.tamil_name as star_tamil_3, s3.name as star3, ser.prefix, ki.created_on',FALSE);
        $this->db->join('thiru_bill_kattalai as k','k.id = ki.bk_id');
        $this->db->join($this->star . ' as s','s.id = k.bk_star_1','left');
        $this->db->join($this->star . ' as s2','s2.id = k.bk_star_2','left');
        $this->db->join($this->star . ' as s3','s3.id = k.bk_star_3','left');
        $this->db->join($this->services . ' as ser','ser.id = k.service_id','left');
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
            } else {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00'));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59'));
            }
        } else if($date_type == 1){
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            } elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))');
            }
        }
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1); 
        $this->db->where('ki.status',1);
        $this->db->where('ki.bki_day_type',1);        
        return $this->db->get('thiru_bill_kattalai_items as ki')->result();
    }
	
	public function get_details_data_by_god_prasadam_daily($start_date='',$end_date='',$type='',$date_type='') {
        $this->db->select('k.*, k.bk_begin_date as bki_from_date, k.bk_end_date as bki_to_date, kp.sub_total as bki_amount, kp.bkp_day_type as bki_day_type, kp.dates as bki_dates, kp.deity_id as bki_god_ids, kp.bkp_no_days as bki_no_days, kp.bkp_week_day as bki_week_day, k.created_on, s.name as star, s.tamil_name as star_tamil,s2.name as star2, s2.tamil_name as star_tamil_2, s3.tamil_name as star_tamil_3, s3.name as star3, ser.prefix',FALSE);
        $this->db->join('thiru_bill_kattalai as k','k.id = kp.kattalai_id');
        $this->db->join($this->star . ' as s','s.id = k.bk_star_1','left');
        $this->db->join($this->star . ' as s2','s2.id = k.bk_star_2','left');
        $this->db->join($this->star . ' as s3','s3.id = k.bk_star_3','left');
        $this->db->join($this->services . ' as ser','ser.id = k.service_id','left');
         if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d',strtotime($start_date)));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d',strtotime($end_date)));
            } else {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d'));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d'));
            }
        } else if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            } elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d') . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d') . '"))');
            }
        }
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1); 
        $this->db->where('kp.status',1);
        $this->db->where('kp.bkp_day_type',1);        
        return $this->db->get('thiru_bill_kattalai_prasadam as kp')->result();
    }
    
    public function get_details_data_by_god_weekly($start_date='',$end_date='',$type='',$date_type='') {
        $this->db->select('k.*, ki.*, s.name as star, s.tamil_name as star_tamil,s2.name as star2, s2.tamil_name as star_tamil_2, s3.tamil_name as star_tamil_3, s3.name as star3, ser.prefix, ki.created_on',FALSE);
        $this->db->join('thiru_bill_kattalai as k','k.id = ki.bk_id');
        $this->db->join($this->star . ' as s','s.id = k.bk_star_1','left');
        $this->db->join($this->star . ' as s2','s2.id = k.bk_star_2','left');
        $this->db->join($this->star . ' as s3','s3.id = k.bk_star_3','left');
        $this->db->join($this->services . ' as ser','ser.id = k.service_id','left');
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
            } else {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00'));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59'));
            }
        } elseif($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            } elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))');
            }
        }
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1); 
        $this->db->where('ki.status',1);
        $this->db->where('ki.bki_day_type',2);        
        return $this->db->get('thiru_bill_kattalai_items as ki')->result();
    }
	
	public function get_details_data_by_god_prasadam_weekly($start_date='',$end_date='',$type='',$date_type='') {
        $this->db->select('k.*, k.bk_begin_date as bki_from_date, k.bk_end_date as bki_to_date, kp.sub_total as bki_amount, kp.bkp_day_type as bki_day_type, kp.dates as bki_dates, kp.deity_id as bki_god_ids, kp.bkp_no_days as bki_no_days, kp.bkp_week_day as bki_week_day, k.created_on, s.name as star, s.tamil_name as star_tamil,s2.name as star2, s2.tamil_name as star_tamil_2, s3.tamil_name as star_tamil_3, s3.name as star3, ser.prefix',FALSE);
        $this->db->join('thiru_bill_kattalai as k','k.id = kp.kattalai_id');
        $this->db->join($this->star . ' as s','s.id = k.bk_star_1','left');
        $this->db->join($this->star . ' as s2','s2.id = k.bk_star_2','left');
        $this->db->join($this->star . ' as s3','s3.id = k.bk_star_3','left');
        $this->db->join($this->services . ' as ser','ser.id = k.service_id','left');
         if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d',strtotime($start_date)));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d',strtotime($end_date)));
            } else {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d'));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d'));
            }
        } else if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            } elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d') . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d') . '"))');
            }
        }
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1); 
        $this->db->where('kp.status',1);
        $this->db->where('kp.bkp_day_type',2);        
        return $this->db->get('thiru_bill_kattalai_prasadam as kp')->result();
    }
   
    public function get_details_data_by_god_multiple($start_date='',$end_date='',$type='',$date_type='') {
        $this->db->select('k.*, ki.*, s.name as star, s.tamil_name as star_tamil,s2.name as star2, s2.tamil_name as star_tamil_2, s3.tamil_name as star_tamil_3, s3.name as star3, ser.prefix, ki.created_on',FALSE);
        $this->db->join('thiru_bill_kattalai as k','k.id = ki.bk_id');
        $this->db->join($this->star . ' as s','s.id = k.bk_star_1','left');
        $this->db->join($this->star . ' as s2','s2.id = k.bk_star_2','left');
        $this->db->join($this->star . ' as s3','s3.id = k.bk_star_3','left');
        $this->db->join($this->services . ' as ser','ser.id = k.service_id','left');
        if($date_type == 2) { 
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
            } else {
                $this->db->where('ki.created_on >=',date('Y-m-d 00:00:00'));
                $this->db->where('ki.created_on <=',date('Y-m-d 23:59:59'));
            }
        } elseif($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            }  elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                 $this->db->where('((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))');
            }
        }
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1); 
        $this->db->where('ki.status',1);
        $this->db->where('ki.bki_day_type',3);        
        return $this->db->get('thiru_bill_kattalai_items as ki')->result();
    }

    public function get_details_data_by_god_prasadam_multiple($start_date='',$end_date='',$type='',$date_type='') {
        $this->db->select('k.*, k.bk_begin_date as bki_from_date, k.bk_end_date as bki_to_date, kp.sub_total as bki_amount, kp.bkp_day_type as bki_day_type, kp.dates as bki_dates, kp.deity_id as bki_god_ids, kp.bkp_no_days as bki_no_days, kp.bkp_week_day as bki_week_day, k.created_on, s.name as star, s.tamil_name as star_tamil,s2.name as star2, s2.tamil_name as star_tamil_2, s3.tamil_name as star_tamil_3, s3.name as star3, ser.prefix',FALSE);
        $this->db->join('thiru_bill_kattalai as k','k.id = kp.kattalai_id');
        $this->db->join($this->star . ' as s','s.id = k.bk_star_1','left');
        $this->db->join($this->star . ' as s2','s2.id = k.bk_star_2','left');
        $this->db->join($this->star . ' as s3','s3.id = k.bk_star_3','left');
        $this->db->join($this->services . ' as ser','ser.id = k.service_id','left');
         if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d',strtotime($start_date)));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d',strtotime($end_date)));
            } else {
                $this->db->where('DATE(k.created_on) >=',date('Y-m-d'));
                $this->db->where('DATE(k.created_on) <=',date('Y-m-d'));
            }
        } else if($date_type == 1) {
            if(!empty($start_date) && !empty($end_date)) {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d',strtotime($end_date)) . '"))');
            } elseif(!empty($start_date) && empty($end_date)) { 
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d',strtotime($start_date)) . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d',strtotime($start_date)) . '"))');
            } else {
                $this->db->where('((DATE(k.bk_begin_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_begin_date) <= "' . date('Y-m-d') . '")) and ((DATE(k.bk_end_date) >= "' . date('Y-m-d') . '") or (DATE(k.bk_end_date) <= "' . date('Y-m-d') . '"))');
            }
        }
        if(!empty($type)) {
            $this->db->where('k.service_id',$type);
        }
        #$this->db->where('k.status',1);
        $this->db->where('k.bk_book_status',1); 
        $this->db->where('kp.status',1);
        $this->db->where('kp.bkp_day_type',3);        
        return $this->db->get('thiru_bill_kattalai_prasadam as kp')->result();
    }
    
    
}
?>