<?php
class Statue_books_purchase_report_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        $this->statue_book = TBL_STATUE_BOOK;
        $this->sb_item = TBL_SB_ITEM;
        $this->category = TBL_INV_CATEGORY;
        $this->item = TBL_INV_ITEM;
        $this->vendor = TBL_VENDOR;
    }
    
    public function list_categories(){
        $this->db->select('id,name as cat_name');
        $this->db->where('status != ',2);
        $this->db->where('is_statue_cat',1);
        return $this->db->get($this->category)->result();
    }
    
    public function get_item_by_cat_id($cat_id = ''){
        $this->db->select('id,item_name,code as item_code');
        if(!empty($cat_id)) {
            $this->db->where('category_id',$cat_id);
        }
        $this->db->where('status !=',2);
        $this->db->where('is_statue_item',1);
        return $this->db->get($this->item)->result();
    }
    
    public function get_data($start_date='',$end_date='',$cat_id='',$item_id=''){
        $this->db->select('sb.*, c.name as cat_name, i.item_name, v.name as vendor',FALSE);
        $this->db->join($this->category . ' as c','c.id = sb.cat_id AND c.is_statue_cat = 1','left');
        $this->db->join($this->item . ' as i','i.id = sb.item_id AND i.is_statue_item = 1','left');
        $this->db->join($this->vendor . ' as v','v.id = sb.vendor_id','left');
        if(!empty($start_date) && !empty($end_date)) {
            $this->db->where('sb.received_date >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('sb.received_date <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('sb.received_date >=',date('Y-m-d 00:00:00'));
            $this->db->where('sb.received_date <=',date('Y-m-d 23:59:59'));
        } 
        if(!empty($cat_id)) {
            $this->db->where('sb.cat_id',$cat_id);
        }
        if(!empty($item_id)) {
            $this->db->where('sb.item_id',$item_id);
        }
        $this->db->where('sb.status !=',2);
        return $this->db->get($this->statue_book . ' as sb')->result();
    }
}
?>


