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

class BankReconciliation extends CI_Controller {
	
	public function __construct(){
		parent::__construct();
        $this->load->model('Useraccess');
        $query = $this->Useraccess->useraccess_id_module2('3', 'transaction');
        if (!$query) {
            echo '<script>alert("Not authorised..");
				window.location.replace("' . base_url() . 'Error");
				</script>';
        }
		$this->load->library('layouts');
		
		$this->load->model('master/masterCode_model');
		$this->load->model('Menu');
		$this->load->model('bank/Bank_model');
		$this->load->model('bank/Bank_reconciliation');
		$this->lang->load("bnkreconciliation",$this->session->userdata('language'));	
	}
	public function home(){
		header("HTTP/1.1 301 Moved Permanently"); 
		header("Location: ".base_url()."locationtransfer"); 
	}
	public function index($statDate = ''){
		//Gather bank information
		$data['bank'] = $this->Bank_model->bank_all();
		$data['statDate'] = $statDate?$statDate:date("Y-m-d",time());
		//Retrive location transfer informations
		//$data['datatbls'] = $this->Bank_reconciliation->getReconciliation(element('compID', $this->session->userdata('logged_in')));
		
		//Configuration for table header
		$data['tbHdr'] = array(
				'paymentDate' => 'paymentDate',
				'transNo' => 'transNo',
				'referenceNo' => 'referenceNo',
				'accountDescription'=>'accountDescription',
				'amountPaid'=>'amountPaid',
		);
		
		//right aligned columns
		//$data['alignRight'] = array("quantity","purchasePrice","value");
		
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js')
					->add_includes('js/validationengine/jquery.validationEngine.js');
		$this->layouts->view('bank/reconciliation', array('latest' => 'sidebar/latest'), $data);
	}
	
	//Gather bank information
	public function gatherinfo(){
		$statement = $this->Bank_reconciliation->getStatByIdDate($this->input->post('bankID'),$this->input->post('date'));
		echo json_encode($statement);
		exit();
	}
	
	//Set payment's reconcile status to 1
	public function setconcile(){
		$IDs = explode(',',$this->input->post('conID'));
		$flag = true;
		$flagCon = true;
		
		foreach($IDs as $id){
			if(!$this->Bank_reconciliation->setConcile($id)){
				$flag = false;
			}
		}
		$reconcile = array(
				'reconcileDate' 		=> date('Y-m-d',time()),
				'bankID'				=> $this->input->post('bankID'),
				'bankStatementDate' 	=> date('Y-m-d',strtotime($this->input->post('bankStatementDate'))),
				'totalDebit'			=> 0,
				'totalCredit'			=> $this->input->post('totalCredit'),
				'bankStatementBalance'	=> $this->input->post('bankStatementBalance')-$this->input->post('modReconBal'),
				'reconcileBalance'		=> $this->input->post('modReconBal'),
		);
		if(!$this->Bank_reconciliation->reConcile($reconcile)){
			$flagCon = false;
		}
		
		if($flag && $flagCon)
			echo "Success";
		else
			echo "Unsuccess";
		exit();
	}
}