<?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();		
		$bank_deposit_list = $this->Bank_model->get_bank_deposit_details();
        $data['bank_deposit_list'] = array();
        if (count($bank_deposit_list) > 0) {
            foreach ($bank_deposit_list as $key => $value) {
                $data['bank_deposit_list'][$value->account_id]['debit'] = $value->debit;
                $data['bank_deposit_list'][$value->account_id]['credit'] = $value->credit;
            }
        }		
        $this->load->view('bank_list',$data);		
    }*/
    
    
    
    public function index(){
        $data['bank_list'] = $this->Bank_model->get_bank_list();		
		$bank_deposit_list = $this->Bank_model->get_bank_deposit_details();
		$bank_expance_list = $this->Bank_model->get_bank_expance_details();
		
		//print_r($bank_expance_list);
		//print_r($bank_deposit_list);
		
        $data['bank_deposit_list'] = array();
        if (count($bank_deposit_list) > 0) {
            foreach ($bank_deposit_list as $key => $value) {
                $data['bank_deposit_list'][$value->account_id]['debit'] = $value->debit;
                $data['bank_deposit_list'][$value->account_id]['credit'] = $value->credit;
            }
        }		
		
		
		if (count($bank_expance_list) > 0) {
            foreach ($bank_expance_list as $key => $value) {
                $data['bank_deposit_list'][$value->account_id]['debit'] += $value->debit;
            }
        }	
        
       /* echo "<pre>";
        print_r($data);
        exit(); */
		
		
        $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;   
    }
	
	public function deposit(){
        $data['bank_id'] = !empty($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $where_bank = array('status' => 1);
        $data['accounts'] = $this->Invoice_model->get_from_table('bank_accounts','*',$where_bank);
        $this->load->view('new_bank_deposit',$data);
    }

    public function deposit_amount(){
        $deposit_arr['to_acc_id'] = $this->input->post('bank_id'); 
        $deposit_arr['from_acc_id'] = $this->input->post('from_acc_id'); 
        $deposit_arr['receive_via'] = $this->input->post('receive_via'); 
        $deposit_arr['reference'] = $this->input->post('reference'); 
        $deposit_arr['amount'] = $this->input->post('amount'); 
        $deposit_arr['notes'] = $this->input->post('notes'); 
        $t_date = $this->input->post('tran_date');
        $arr1 = explode("/",$t_date);
        $deposit_arr['tran_date'] = $arr1[2]."-".$arr1[1]."-".$arr1[0];
        $deposit_arr['date_added'] = $arr1[2]."-".$arr1[1]."-".$arr1[0];
        $this->Bank_model->insert_bank_deposit($deposit_arr);
        echo 1;
    }
	
}
?>