<?php
App::uses('AppController', 'Controller');
class BankController extends AppController {

/**
 * Controller name
 *
 * @var string
 */
	public function index(){ 
		//$this->gradPermission(1);
		$conditions = array();
		$order=$this->paginate['order'] = array('Bank.name' => 'asc');

		$querystring = $this->request->query;
		$this->set(compact('querystring'));
		if(isset($this->request->query['export'])){   
			if(!empty($this->request->query['offset'])){
				$offset = (int) $this->request->query['offset'];
				if($offset > 10000){
					$offset = 0;
				}
			}else{
				$offset = 0;
			}
				
				
			$banks = $this->Bank->find('all',array('conditions' =>$conditions,'order'=>$order ,'limit'=>10000, 'offset'=>$offset));
			$this->set(compact('banks','offset'));
			$this->set('filename', 'Banks');
			$this->render('export_bank','export_xls');
		} else {
		    $banks = $this->paginate('Bank', $conditions);
		}
		$this->set('banks', $banks);
		
	}
	
	public function add(){
		//$this->gradPermission(1);
		if ($this->request->data){
			$is_exists = $this->Bank->find('count', array('conditions'=>array('LOWER(name)'=>strtolower($this->request->data['Bank']['name']))));
			if ($is_exists > 0){
				$this->Session->setFlash(__('"%s" already exists', $this->request->data['Bank']['name']));
			}else{
				$this->Bank->save($this->request->data);
				$this->Session->setFlash(__('Bank was added successfully'));
				$this->redirect('/Bank');
			}
		}
		//$this->loadModel('State');
	}
	
	public function edit($id=0){
		$this->loadModel('Audit');
		//$set_var=$this->gradPermission(1);
		$userData = $this->Bank->read(null, $id);
		if ($this->request->data){
			$is_exists = $this->Bank->find('count', array('conditions'=>array('LOWER(name)'=>strtolower($this->request->data['Bank']['name']), 'id !=' => $id)));
			if ($is_exists > 0){
				$this->Session->setFlash(__('"%s" already exists', $this->request->data['Bank']['name']));
			}else{
				$this->Bank->save($this->request->data);
				$this->Audit->saveDeletedData($userData['Bank']['id'], 'Bank', $this->Auth->user('id') , serialize($userData),'Bank Has been updated', STATUS_EDIT);
				$this->Session->setFlash(__('Bank was updated successfully'));
				$this->redirect('/bank');
			}
		}
		
		if (!empty($id)){
			$this->Bank->id = $id;
			$this->request->data = $this->Bank->read();
		}
		//if($set_var){
		$this->render('add');
		//}
	}
	
	
	public function updatebank($id){
		if(!id){
			$this->redirect('/bank');
		}
		$this->loadModel('Bank');
		if ($this->request->data){
			$is_exists = $this->Bank->find('count', array('conditions'=>array('LOWER(name)'=>strtolower($this->request->data['BAnk']['name']), 'id !=' => $id)));
			if ($is_exists > 0){
				$this->Session->setFlash(__('"%s" already exists', $this->request->data['Bank']['name']));
			}else{
				$this->State->save($this->request->data);
				$this->Session->setFlash(__('State was updated successfully'));
				$this->redirect('/bank');
			}
		}
		$this->State->id = $id;
		$this->request->data = $this->Bank->read();
	}
	
	
	public function deletebank($id){
		if(!$id){
			$this->redirect('/bank');
		}
		if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
		$this->loadModel('Bank');
		$this->Bank->id = $id;
		$bank = $this->Bank->read();

		$data = array();
		$data['serialize'] = serialize($bank);
		$data['model_id'] = $bank['Bank']['id'];
		$this->request->data = $this->Bank->delete();
		$this->loadModel('Audit');
		$this->Audit->saveDeletedData($data['model_id'], 'Bank', $this->Auth->user('id') , $data['serialize'],'Bank Has been Deleted',STATUS_DELETE);
		$this->Session->setFlash(__('Bank was deleted successfully'));
		$this->redirect('/Bank');
	}
	public function deleteAllBanks(){
		if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
		
		if(!empty($this->request->data['deleteAllValue'])){
			$this->Bank->deleteAll(array('Bank.id'=>explode(",", $this->request->data['deleteAllValue'])) );
			$this->Session->setFlash(__('All selected Banks was deleted successfully') );
			$this->redirect(array('action'=>'index'));
		}
		exit;
	}
}