<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Inventory_item extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('inv_item_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();
        $data['results'] = $this->inv_item_model->list_all();
        if (!empty($this->session->flashdata('success'))) 
            $data['success'] = $this->session->flashdata('success');
        else 
            $data['error'] = $this->session->flashdata('error');
        
        $actions = $this->actions();
        $data['add_btn']    = 'New Item';
        $data['view_link']  = $actions['view'] . '/';
        $data['add_link']   = $actions['add'];
        $data['edit_link']  = $actions['edit'] . '/';
        $data['delete_link']= $actions['delete'] . '/';
        
        $service_menu_id = get_inventory_controller_id($actions['controller']);
        $data['service_menu_id'] = array('service_menu_id' => $service_menu_id, 'menu_type' => '3');
        $this->render($data, 'inv_item/list');
    }
    
    private function get_item_url_key($name, $id = '') {
        $url_key = get_url_key($name);
        if ($this->inv_item_model->item_url_key_exists($url_key, $id)) {
            $this->uri_key_index ++;
            return $this->get_item_url_key($name . '_' . $this->uri_key_index, $id);
        }
        
        return $url_key;
    }
    
    public function add() {
        $data = array();
        $actions = $this->actions();
        $data['units'] = $this->inv_item_model->unit_list();
        $data['categories'] = $this->inv_item_model->category_list();
        $data['form_action'] = $actions['insert'];
        $data['code'] = generate_rec_no( TBL_INV_ITEM, array('is_statue_item' => 0) );  
        
        $this->render($data, 'inv_item/add');
    }
    
    public function insert() {
        $insert = array(
            'code' => $this->input->post('code'),
            'category_id' => $this->input->post('category_id'),
            'item_name' => $this->input->post('item_name'),
            'item_slug'  => get_var_name($this->input->post('item_name')),
            'uom' => $this->input->post('unit_id'),
            'quantity' => $this->input->post('quantity'),
            'selling_price' => $this->input->post('price'),
            'description' => $this->input->post('desc'),
            'url_key' => $this->get_item_url_key($this->input->post('code')),
            #'status' => $this->input->post('status'),
            'created_by' => $this->user_data['user_id'],
            'created_on' => date('Y-m-d H:i:s')
        );
        
        $insert_id = $this->inv_item_model->insert($insert);  
        if ($insert_id > 0)
            $this->session->set_flashdata('success', 'Item added successfully');    
        
        else
            $this->session->set_flashdata('error', 'Item not added!');
        redirect('inventory_item', 'refresh');
    }
    
    public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['units'] = $this->inv_item_model->unit_list();
        $data['categories'] = $this->inv_item_model->category_list();
        $data['inv_item'] = $this->inv_item_model->get_by_id($id);
        $data['used_item'] = $this->inv_item_model->get_used_item_status($id);
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $id;
        $this->render($data, 'inv_item/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $inv_item = $this->inv_item_model->get_by_id($id);
        
        $update = array(
            'code' => $this->input->post('code'),
            'category_id' => $this->input->post('category_id'),
            'uom' => $this->input->post('unit_id'),
            'item_name' => $this->input->post('item_name'),
            'item_slug'  => get_var_name($this->input->post('item_name')),
            'quantity' => $this->input->post('quantity'),
            'selling_price' => $this->input->post('price'),
            'description' => $this->input->post('desc'),
            'url_key' => $this->get_item_url_key($this->input->post('code')),
            #'status' => $this->input->post('status'),
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );        
        
        if ( $this->inv_item_model->update($update, $id) > 0)
            $this->session->set_flashdata('success', 'Item updated successfully');  
       
        else
            $this->session->set_flashdata('error', 'Item not updated!');    
        
        redirect('inventory_item','refresh');
    }
    
    public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['inv_item'] = $this->inv_item_model->get_by_id($id);
        $this->render($data, 'inv_item/view');
    }
    
    public function delete($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $update = array ( 
            'status' => 2,
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        if ($this->inv_item_model->update($update,$id) > 0)
            $this->session->set_flashdata('success', 'Item deleted successfully');
            
        else 
            $this->session->set_flashdata('error', 'Item not deleted!');
        
        redirect( 'inventory_item', 'refresh');
    }
    
    public function item_name_exists() {
        $data =  $this->inv_item_model->item_name_exists($_POST['category_id'],$_POST['id'], $_POST['item_name']);
        echo json_encode($data);
        exit(0);
    }
}
?>