<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Inventory_stock_issue extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('inventory_stock_issue_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
       
        $data = array();
        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 Stock Issue';
        $data['view_link']  = $actions['view'] . '/';
        $data['edit_link']  = $actions['edit'] . '/';
        $data['add_link']   = $actions['add'];
        $data['delete_link']= $actions['delete'] . '/';
        $data['results'] 	= $this->inventory_stock_issue_model->list_all();
        
        $service_menu_id 			= get_inventory_controller_id($actions['controller']);
        $data['service_menu_id'] 	= array('service_menu_id' => $service_menu_id, 'menu_type' => '6');
        
        //echo "<pre>"; print_r( $data['results'] ); die;
        
        $this->render($data, 'inventory_stock_issue/list');
    }
    
    private function get_general_url_key($name, $id = '') {
        $url_key = get_url_key($name);
        if ($this->inventory_stock_issue_model->general_url_key_exists($url_key, $id)) {
            $this->uri_key_index ++;
            return $this->get_general_url_key($name . '_' . $this->uri_key_index, $id);
        }
        
        return $url_key;
    }
    
    public function add() {
        $this->manipulate();
    }
    
    function edit( $inv_item_id ){
        $this->manipulate( $inv_item_id, array('page' => 'edit') );
    }
    
    function view( $inv_item_id ) {
        $this->manipulate( $inv_item_id, array('page' => 'view') );
        
    }
    
    function manipulate($inv_id, $args){
        $actions = $this->actions();
        
        $data = array();
        $post = $this->input->post();
        $page = empty( $args['page'] ) ? 'add' : $args['page'];
        
        if( !empty( $post['submit'] ) ) :
            $db_id = $this->inventory_stock_issue_model->manipulate($inv_id);
            if( $db_id ) :
                $this->session->set_flashdata('success', 'Inventory – Stock Issue '. ( $page == 'add' ? 'added' : 'updated' ) .' successfully');
            else :
                $this->session->set_flashdata('error', 'Inventory – Stock Issue details are failed to '. ( $page == 'add' ? 'add' : 'update' ) .'!');
            endif;
            
            redirect('inventory_stock_issue', 'refresh');
        endif;
        
        if( !empty($inv_id) ) :
            $data['result'] = $this->inventory_stock_issue_model->get_gen_inv($inv_id); //_get( TBL_INV_LOG, array('inv_item_id' => $inv_item_id), array('is_single' => TRUE ));
        else :
            $data['categories'] = $this->inventory_stock_issue_model->category_list();
        endif;
        
        $this->render($data, 'inventory_stock_issue/'. $page);
    }
    
    
    public function delete($inv_item_id = '') {
        if (empty($inv_item_id)) {
            show_400_error();
        }
        
        $this->inventory_stock_issue_model->delete($inv_item_id);
        $this->session->set_flashdata('success', 'General Inventory details deleted successfully');
        
        redirect( 'inventory_stock_issue', 'refresh');
    }
    
    public function get_items_by_cat_id(){
        $data['items'] =  $this->inventory_stock_issue_model->get_items_by_cat_id($_POST['category_id']);
        echo json_encode($data);
        exit(0);
    }
    
    public function get_unit_by_item_id() {
		//echo '<pre>'; print_r( $_POST ); die;
        $data['item'] =  $this->inventory_stock_issue_model->get_unit_by_item_id($_POST['category_id'], $_POST['item_id']);
        //echo '<pre>'; print_r( $data ); die;
        
        echo json_encode($data);
        exit(0);
    }
    
    function ajax(){        
        echo json_encode( $this->inventory_stock_issue_model->ajax() );
    }
}
?>