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

class Bank extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
         if (! $this->session->userdata ( 'user_logged_in' ) || !company_url($this->session->userdata ('company_slag'))) {
            redirect ( 'login', 'refresh' );
        }
        $this->load->vars ( array (
                'global' => 'Available to all views',
                'header' => 'common/header',
                'left' => 'common/left',
                'footer' => 'common/footer' 
        ) );
        $this->load->model('Bank_model');
        $this->load->model('Invoice_model');
    }

    public function index(){
        $data['bank_list'] = $this->Bank_model->get_bank_list();
        $this->load->view('bank_list',$data);
    }

    public function newbank(){
        $data['bank_id'] = !empty($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        if ($data['bank_id'] > 0) {
            $data['bank_data'] = $this->Bank_model->get_bank_details($data['bank_id']);
        }
        $this->load->view('new_bank',$data);
    }

    public function save_bank(){
        $bank_id         = $this->input->post('bank_id');
        $account_name    = $this->input->post('account_name');
        $account_code    = $this->input->post('account_code');
        $account_number  = $this->input->post('account_number'); 
        $bank_name       = $this->input->post('bank_name');
        $opening_balance = $this->input->post('opening_balance');
        $routing_number  = $this->input->post('routing_number');
        $description     = $this->input->post('description');
        $is_primary      = $this->input->post('is_primary');
        $status          = $this->input->post('status');

        $bank_data = array(
                'account_name' => $account_name,  
                'account_code' => $account_code,  
                'account_number' => $account_number,
                'bank_name' => $bank_name,  
                'opening_balance' => $opening_balance,        
                'routing_number' => $routing_number,
                'description' => $description,   
                'is_primary' => $is_primary,    
                'status' => $status        
            );

        if (($bank_id == 0)) {
            $this->Bank_model->insert_bank($bank_data);
        } else {
            $this->Bank_model->update_bank($bank_data,$bank_id);
        }
        echo 1;   
    }
}
?>