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

class Inventory_item_master extends REF_Controller {

    public function __construct() {

        parent::__construct();

        $this->load->library('upload');

        $this->load->model('inventory_item_master_model', '', TRUE);

        $this->uri_key_index = 0;

    }

    

    public function index() {

        $data = array();

        $data['results'] = $this->inventory_item_master_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 Inventory 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' => '4');

        $this->render($data, 'inventory_item/list');

    }

    

    private function get_item_url_key($name, $id = '') {

        $url_key = get_url_key($name);

        if ($this->inventory_item_master_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->inventory_item_master_model->unit_list();

        $data['categories'] 	= $this->inventory_item_master_model->category_list();

        $data['form_action'] 	= $actions['insert'];
		
		$prefix_wt_sub 			= CODE_ITEM;
        //$data['code'] 			= generate_rec_no( TBL_INVENTORY_ITEM, array('is_statue_item' => 0) );  
		$data['code'] 			= $this->item_auto_generation_code(TBL_INVENTORY_ITEM, $prefix_wt_sub, '', 4, '');


        $this->render($data, 'inventory_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('item_name')),

            #'status' => $this->input->post('status'),

            'created_by' => $this->user_data['user_id'],

            'created_on' => date('Y-m-d H:i:s')

        );

        

        $insert_id = $this->inventory_item_master_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_master', 'refresh');

    }

    

    public function edit($id = '') {

        if (empty($id)) {

            show_400_error();

        }

        

        $data 					= array();

        $data['units'] 			= $this->inventory_item_master_model->unit_list();

        $data['categories'] 	= $this->inventory_item_master_model->category_list();

        $data['inv_item'] 		= $this->inventory_item_master_model->get_by_id($id);

        $data['used_item'] 		= $this->inventory_item_master_model->get_used_item_status($id);

        $actions 				= $this->actions();

        $data['form_action'] 	= $actions['update'] . '/' . $id;

        $this->render($data, 'inventory_item/edit');

    }

    

    public function update($id = '') {

        if (empty($id)) {

            show_400_error();

        }

        

        $inv_item = $this->inventory_item_master_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('item_name')),

            #'status' => $this->input->post('status'),

            'updated_by' => $this->user_data['user_id'],

            'updated_on' => date('Y-m-d H:i:s')

        );        

        

        if ( $this->inventory_item_master_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_master','refresh');

    }

    

    public function view($id = '') {

        if (empty($id)) {

            show_400_error();

        }

        

        $data = array();

        $data['inv_item'] = $this->inventory_item_master_model->get_by_id($id);

        $this->render($data, 'inventory_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->inventory_item_master_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_master', 'refresh');

    }

    

    public function item_name_exists() {

        $data =  $this->inventory_item_master_model->item_name_exists($_POST['category_id'],$_POST['id'], $_POST['item_name']);

        echo json_encode($data);

        exit(0);

    }
	
	public function print_barcode($id)
    {
        $data = array();
        $actions = $this->actions();
        $data['browser'] = browser_info();
        $data['list_link'] = $actions['index'];
        if (!empty($from_list))
            $data['redirect_link'] = $actions['index'];
        else
            $data['redirect_link'] = $actions['add'];
        $data['item'] = $this->inventory_item_master_model->get_by_id($id);
        $data['success'] = $this->data['success'];
        $data['error'] = $this->data['error'];
        $data['invoice_link'] = base_url() . 'inventory_item_master/barcode/' . $id;
        $this->render($data, 'inventory_item/print');
    }

    public function barcode($id, $nos = NULL) 
    {
        $data = array();
        $user_data = get_user_data();
        $data['item'] = $this->inventory_item_master_model->get_by_id($id);
        //$data['company'] = $this->company_model->get_company();
        $data['nos'] = $nos;
        $filename = 'Barcode_' . $data['item']->code;
        $filename = strtoupper($filename);
        // Load Views
        $header = $this->load->view('inventory_item/pdf_header', $data, TRUE); //'<div></div>';
        $content = '';
        $footer = '';
        $this->load->helper(array('My_Pdf'));
        invoice_pdf($header, $content, $footer, $filename);
        exit(0);
    }

    public function thermal($id, $nos = NULL)
    {
        $data = array();
        $data['item'] = $this->inventory_item_master_model->get_by_id($id);
        $data['units'] = $this->inventory_item_master_model->units();
        //$data['company'] = $this->company_model->get_company();
        $data['nos'] = $id.'--'.$nos;
		//echo "<pre>"; print_r($data); //exit;
        $filename = 'Barcode_' . $data['item']->code;
        echo $this->load->view('inventory_item/thermal', $data, TRUE);
		
    }

}

?>