<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Expense_type extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('expenses_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();
        $data['results'] = $this->expenses_model->list_all_types();
        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']    = 'Create New Expense Type';
        $data['view_link']  = $actions['view'] . '/';
        $data['add_link']   = $actions['add'];
        $data['edit_link']  = $actions['edit'] . '/';
        $data['delete_link']= $actions['delete'] . '/';
        
        $service_menu_id = get_master_controller_id($actions['controller']);
        $data['service_menu_id'] = array('service_menu_id' => $service_menu_id, 'menu_type' => '1');
        $this->render($data, 'expense_type/list');
    }
    
    public function add() {
        $data = array();
        $actions = $this->actions();
        $data['form_action'] = $actions['insert'];
        $this->render($data, 'expense_type/add');
    }
    
    public function insert() {
        $insert = array(
            'type_name' => $this->input->post('type'),
            'slug'  => get_var_name($this->input->post('type')),
            'status' => $this->input->post('status'),
            'created_by' => $this->user_data['user_id'],
            'created_on' => date('Y-m-d H:i:s')
        );
        
        $insert_id = $this->expenses_model->insert($insert);  
        if ($insert_id > 0) {
            $this->session->set_flashdata('success', 'Expense Type added successfully');    
        }
        else{
            $this->session->set_flashdata('error', 'Expense Type not added!');   
        }
        
        redirect('expense_type', 'refresh');
    }
    
     public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['type'] = $this->expenses_model->get_by_id($id);
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $id;
        $this->render($data, 'expense_type/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }

        $update = array(
            'type_name' => $this->input->post('type'),
            'slug'  => get_var_name($this->input->post('type')),
            'status' => $this->input->post('status'),
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        if ($this->expenses_model->update($update, $id) > 0)
            $this->session->set_flashdata('success', 'Expense Type updated successfully');  
        
        else
            $this->session->set_flashdata('error', 'Expense Type not updated!');    
        
        redirect('expense_type','refresh');
    }
    
   public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['type'] = $this->expenses_model->get_by_id($id);
        $this->render($data, 'expense_type/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->expenses_model->update($update,$id) > 0) {
            $this->session->set_flashdata('success', 'Expense Type deleted successfully');
        }
        else 
            $this->session->set_flashdata('error', 'Expense Type not deleted!');
        
        redirect( 'expense_type', 'refresh');
    }
    
    /*public function category_name_exists() {
        $data =  $this->category_model->category_name_exists($_POST['category_name'], $_POST['category_id']);
        echo json_encode($data);
        exit(0);
    } */
}
?>