<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Supplier_vendor_management extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('supplier_vendor_management_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();
        $data['results'] = $this->supplier_vendor_management_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']    = 'Add New Supplier / Vendor';
        $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' => '1');
        $this->render($data, 'inventory_supplier/list');
    }
    
    public function add() {
        $data 					= array();
        $actions 				= $this->actions();
        $data['form_action'] 	= $actions['insert'];  
        
        $this->render($data, 'inventory_supplier/add');
    }
    
    public function insert() {
        $insert = array(
            'ven_company' => $this->input->post('companyName'),
            'ven_contact_name' => $this->input->post('contactName'),
            'ven_contact_no' => $this->input->post('contactNo'),
            'ven_fax_no'  => $this->input->post('faxNo'),
            'ven_email_id' => $this->input->post('emailId'),
            'ven_address' => $this->input->post('address'),
            'status' => $this->input->post('status'),
            'created_by' => $this->user_data['user_id'],
            'created_on' => date('Y-m-d H:i:s')
        );
        
        $insert_id = $this->supplier_vendor_management_model->insert($insert);  
        if ($insert_id > 0)
            $this->session->set_flashdata('success', 'Supplier/Vendor added successfully');    
        
        else
            $this->session->set_flashdata('error', 'Supplier/Vendor not added!');
        redirect('supplier_vendor_management', 'refresh');
    }
    
    public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data 					= array();
        $data['inv_vendor'] 	= $this->supplier_vendor_management_model->get_by_id($id);
        $actions 				= $this->actions();
        $data['form_action'] 	= $actions['update'] . '/' . $id;
        $this->render($data, 'inventory_supplier/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $inv_item = $this->supplier_vendor_management_model->get_by_id($id);
        
        $update = array(
            'ven_company' => $this->input->post('companyName'),
            'ven_contact_name' => $this->input->post('contactName'),
            'ven_contact_no' => $this->input->post('contactNo'),
            'ven_fax_no'  => $this->input->post('faxNo'),
            'ven_email_id' => $this->input->post('emailId'),
            'ven_address' => $this->input->post('address'),
            'status' => $this->input->post('status'),
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );        
        
        if ( $this->supplier_vendor_management_model->update($update, $id) > 0)
            $this->session->set_flashdata('success', 'Supplier/Vendor updated successfully');  
       
        else
            $this->session->set_flashdata('error', 'Supplier/Vendor not updated!');    
        
        redirect('supplier_vendor_management','refresh');
    }
    
    public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['inv_vendor'] = $this->supplier_vendor_management_model->get_by_id($id);
        $this->render($data, 'inventory_supplier/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->supplier_vendor_management_model->update($update,$id) > 0)
            $this->session->set_flashdata('success', 'Supplier/Vendor deleted successfully');
            
        else 
            $this->session->set_flashdata('error', 'Supplier/Vendor not deleted!');
        
        redirect( 'supplier_vendor_management', 'refresh');
    }
}
?>