<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class inv_gold_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->gold = TBL_INV_GOLD;
        $this->gold_type = TBL_INV_GOLD_TYPE;
    }
    
    public function list_all() {
        $this->db->select('g.*, gt.type as gt_type, CASE g.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str',FALSE);
        $this->db->join($this->gold_type . ' as gt','gt.id = g.type');
        $this->db->where('g.status !=', 2);
        $this->db->order_by('g.id', 'desc');
        return $this->db->get($this->gold . ' as g')->result();
    }
    
    public function gold_type_list() {
        $this->db->where('status', 1);
        return $this->db->get($this->gold_type)->result();
    }
    
    public function add_new_type($type,$user_id) {
        $data = array(
            'type' => $type,
            'created_on' => date('Y-m-d H:i:s'),
            'created_by' => $user_id
        );
        $this->db->insert($this->gold_type, $data);  
        return $this->db->insert_id();
    }
    
    public  function insert($data){  
        $this->db->insert($this->gold, $data);  
        return $this->db->insert_id();
    }   
    
    public function update($data, $id){
        $this->db->where('id', $id);
        $this->db->update($this->gold, $data);
        return $this->db->affected_rows();
    }
    
    public function get_by_id($id) {
        $this->db->select('g.*, gt.type as gt_type, CASE g.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str',FALSE);
        $this->db->join($this->gold_type . ' as gt','gt.id = g.type');
        $this->db->where('g.id',$id);
        return $this->db->get($this->gold . ' as g')->row();
    }
   
     public function item_name_exists($category_id = '', $id = '',$name = '') {
        $this->db->where('category_id', $category_id);
        if($id != '')
            $this->db->where('id !=', $id);
         
        if($category_id == 0)
            $this->db->where('item_id', $name);
        else
            $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 gold_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->gold)->num_rows();
    }
    
    public function check_type_exists($type){
        $this->db->where('type',$type);
        $this->db->where('status !=',2);
        $getData = $this->db->get($this->gold_type);
        if($getData->num_rows() > 0)
            return 1;
        else
            return 0;
    }
}
?>