<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class statue_book_model extends CI_Model {

    public function __construct() {

        parent::__construct();

        /*$this->statue = TBL_STATUE_BOOK;

        $this->sb_item = TBL_SB_ITEM;

        $this->category = TBL_INV_CATEGORY;

        $this->item = TBL_INV_ITEM;

        $this->vendor = TBL_VENDOR;*/
		
		$this->purchase 		= TBL_INVENTORY_PURCHASE;
        $this->purchase_items 	= TBL_INVENTORY_PURCHASE_ITEM;
        $this->vendor	 		= TBL_INVENTORY_VENDOR;
        $this->inv_category		= TBL_INVENTORY_CATEGORY;
        $this->item 			= TBL_INVENTORY_ITEM;
        $this->unit 			= TBL_UNIT;	
		$this->purchase_receive	= TBL_INVENTORY_PURCHASE_RECEIVED;	
		$this->purchase_rec		= 'thiru_inventory_purchase_received';
		$this->inv_stock_issue	= TBL_INVENTORY_STOCK_ISSUE;
        $this->templeshop_item 	= TBL_BILL_TEMPLESHOP_ITEM;

    }

    

    public function list_all() {

       $this->db->select('isi.*, c.name as cat_name, i.item_name, i.code, i.selling_price, SUM(isi.available_qty) as available_qty', FALSE);

       $this->db->join($this->inv_category . ' as c','c.id = isi.category_id AND (c.id = 4 OR c.id = 5)');

       $this->db->join($this->item . ' as i','i.id = isi.item_id');
	   
	   $this->db->where('isi.status =',1);
	   
	   $this->db->group_by('isi.item_id');
		
       $this->db->order_by('isi.id','desc');

       return $this->db->get($this->inv_stock_issue . ' as isi')->result();

    }

    

    public function category_list() {

        $this->db->where('status',1);

        $this->db->where('is_statue_cat',1);

        return $this->db->get($this->category)->result();

    }

    

    public function item_list() {

        $this->db->where('status',1);

        $this->db->where('is_statue_item',1);

        return $this->db->get($this->item)->result();

    }

    

    public function vendor_list() {

        $this->db->where('status',1);

        return $this->db->get($this->vendor)->result();

    }

    

    public function get_last_code($cat_id) {

        $this->db->where('status',1);

        $this->db->where('cat_id',$cat_id);

        $this->db->order_by('id','desc');

        return $this->db->get($this->statue);

    }

    

    public function get_cat_name($cat_id) {

        $this->db->where('id',$cat_id);

        return $this->db->get($this->category)->row();

    }

    

    public function insert($data) {

        $this->db->insert($this->statue,$data);

        return $this->db->insert_id();

    }

    

    public function get_by_id($id) {

       $this->db->select('s.*, c.name as cat_name, i.item_name, v.name as vendor', FALSE);

       $this->db->join($this->category . ' as c','c.id = s.cat_id AND is_statue_cat = 1');

       $this->db->join($this->item . ' as i','i.id = s.item_id AND is_statue_item = 1');

       $this->db->join($this->vendor . ' as v','v.id = s.vendor_id');

       $this->db->where('s.id',$id);

       return $this->db->get($this->statue . ' as s')->row();

    }

    

    public function update($data, $id){

        $this->db->where('id', $id);

        $this->db->update($this->statue, $data);

        return $this->db->affected_rows();

    }

    

    public function get_sb_item_rows($item_id) {

        $this->db->where('item_id',$item_id);

        return $this->db->get($this->sb_item);

    }

    

    public function update_sb_item($data,$id) {

        $this->db->where('id',$id);

        $this->db->update($this->sb_item,$data);

        return $this->db->affected_rows();

    }

    

    public function insert_sb_item($data) {

        $this->db->insert($this->sb_item,$data);

        return $this->db->insert_id();

    }

    

    public function check_valid_qty($item) {

        $this->db->select('SUM(total_quantity) as total_quantity');

        $this->db->where('item_id',$item);

        return $this->db->get($this->sb_item)->row();

    } 

    

    public function list_qty_by_item() {

       $this->db->select('si.*, c.name as cat_name, i.item_name', FALSE);

       $this->db->join($this->item . ' as i','i.id = si.item_id AND is_statue_item = 1');

       $this->db->join($this->category . ' as c','c.id = i.category_id AND is_statue_cat = 1');

       $this->db->order_by('id','desc');

       return $this->db->get($this->sb_item . ' as si')->result();

    }

    

    public function get_num_rows_temple_item($item_id) {

        $this->db->where('inventory_item_id',$item_id);

        $this->db->where('status !=',2);

        $getData = $this->db->get($this->templeshop_item);

        if($getData->num_rows() > 0)

            return 1;



        else

            return 0;            

    }

    

    public function get_item_by_cat_id($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();

    }

}

?>