<?php

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

class inventory_item_master_model extends CI_Model {

    public function __construct() {

        parent::__construct();

        $this->item 	= TBL_INVENTORY_ITEM;       

        $this->category = TBL_INVENTORY_CATEGORY;

		 $this->unit	= TBL_UNIT;

        $this->po_items = TBL_INVENTORY_PURCHASE_ITEM;

    }

    

    public function list_all() {

        $this->db->select('ini.*, cat.name, unit.unit_name, CASE ini.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str',FALSE);

        $this->db->join($this->category . ' as cat','cat.id = ini.category_id','left');

        $this->db->join($this->unit . ' as unit','unit.id = ini.uom','left');

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

        $this->db->where('ini.is_statue_item', 0);

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

        return $this->db->get($this->item . ' as ini')->result();

    }

    

    public function unit_list() {

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

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

    }

    

    public function category_list() {

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

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

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

    }

    

    public  function insert($data){  

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

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

    }   

    

    public function update($data, $id){

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

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

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

    }

    

    public function get_by_id($id) {

        $this->db->select('ini.*, cat.name, unit.unit_name, CASE ini.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str',FALSE);

        $this->db->join($this->category . ' as cat','cat.id = ini.category_id','left');

        $this->db->join($this->unit . ' as unit','unit.id = ini.uom','left');

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

        return $this->db->get($this->item . ' as ini')->row();

    }

   

     public function item_name_exists($category_id = '', $id = '',$name = '') {

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

        if($id != '')

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

       

        $this->db->where('item_name', $name);

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

        if($this->db->get($this->item)->num_rows())

            return 1;

        else 

            return 0;

    }

   

    public function item_url_key_exists($url_key, $id) {

        if (!empty($id)) {

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

        }

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

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

    }

    

    public function get_last_insert_id() {

        $this->db->select('id,code');

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

        $this->db->limit(1);

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

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

    }

    

    public function get_used_item_status($id) {

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

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

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

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

           return 1;

       else

           return 0;

    }
	
	public function units() {
        $this->db->select('id,unit_name, CASE status WHEN 1 THEN "Active" WHEN 2 THEN "Inactive" ELSE "" END as status_str ', FALSE);
        $this->db->where('status != 10');
        $this->db->order_by('id', 'desc');
        return $this->db->get($this->unit)->result();
    }

}

?>