<?php
class Statue_books_sale_report_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        $this->inv_category = TBL_INV_CATEGORY;
        $this->inv_item = TBL_INV_ITEM;
        $this->templeshop = TBL_BILL_TEMPLESHOP_SALES;
        $this->templeshop_item = TBL_BILL_TEMPLESHOP_ITEM;
        $this->services = TBL_SERVICE;
    }
    
    public function list_category(){
        $this->db->select('id,code as category_code,name as category_name');
        $this->db->where('is_statue_cat',1);
        $this->db->where('status',1);
        return $this->db->get($this->inv_category)->result();
    }
    
    public function list_cat_item($category_id){
        $this->db->select('id,code as item_code,item_name');
        if($category_id != '') {
        $this->db->where('category_id',$category_id);
        }
        $this->db->where('is_statue_item',1);
        $this->db->where('status',1);
        return $this->db->get($this->inv_item)->result();
    }
    
    public function get_data($start_date='',$end_date='',$category='',$item=''){
        $this->db->select('cat.code as category_code, cat.name as category_name, inv.code as item_code, inv.item_name, tmp.created_on, tmp.receipt_no, serv.name as service_name, serv.prefix,, tp.*',FALSE);
        $this->db->join($this->templeshop . ' as tmp','tmp.id = tp.temple_shop_id');
        $this->db->join($this->inv_item . ' as inv','inv.id = tp.inventory_item_id and inv.is_statue_item=1 and inv.status=1');
        $this->db->join($this->inv_category . ' as cat','cat.id = inv.category_id and cat.is_statue_cat=1 and cat.status=1');
        $this->db->join($this->services . ' AS serv', 'serv.id = tmp.service_id', 'left' );
        
        if(!empty($start_date) && !empty($end_date)) {
            $this->db->where('tmp.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('tmp.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('tmp.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('tmp.created_on <=',date('Y-m-d 23:59:59'));
        }

        if(!empty($category)) {
            $this->db->where('inv.category_id',$category);
        }
        
        if(!empty($item)) {
            $this->db->where('tp.inventory_item_id',$item);
        }
        
        $this->db->where('tp.status !=',2);
        return $this->db->get($this->templeshop_item . ' as tp')->result();
    }
}
?>