<?php

App::uses('CreditAppController', 'Credit.Controller');
App::uses('CreditType', 'Credit.Controller.Component');
App::uses('StatusCredit', 'Credit.Controller.Component');

/**
 * CreditHistories Controller
 *
 * @property Credit $Credit
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class CreditController extends CreditAppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator', 'Session', 'Cash.Cash');
	
/**
 * Helpers
 *
 * @var array
 */
	public $helpers = array('Api');	

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		
		$this->set('title_for_layout', 'Histórico de Saldos');
		
		/**
		 * Valida o usuario logado para mostrar os dados
		 * dele apenas, salvo quando for admin
		 */
		$conditions = array();
		if ($this->Auth->user('role_id') != '1') {
			$conditions['User.id'] = $this->Auth->user('id');
		}
		
		$this->Credit->recursive = 1;
		$this->Paginator->settings = array(
				'conditions' => $conditions,
				'order' => array(
						'Credit.id' => 'desc',
				),
		);		
		$this->set('credits', $this->Paginator->paginate());
	}

/**
 * admin_add
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_add() {
		$this->set('title_for_layout', 'Adição de Créditos');
		
		if ($this->request->is('post')) {
			$this->Credit->create();
			if ($this->Credit->save($this->request->data)) {			    				
				$this->Cash->add(
				        $this->request->data['Credit']['cash'], 
				        $this->request->data['Credit']['user_id'],
				        $this->request->data['Credit']['credit_type']);				
				
				$this->Session->setFlash(__('The credit has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The credit could not be saved. Please, try again.'));
			}
		}
		$users = $this->Credit->User->find('list');
		$this->set(compact('users'));
	}	
	
/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		if (!$this->Credit->exists($id)) {
			throw new NotFoundException(__('Invalid credit history'));
		}
		$options = array('conditions' => array('Credit.' . $this->Credit->primaryKey => $id));
		$this->set('Credit', $this->Credit->find('first', $options));
		$this->set('currentkey', $id);
	}
	
/**
 * Salva um Credito no banco de dados
 * @param real $value
 * @param unknown $user_id
 */	
	public function saveCredit($_data = array()) {
		
	    if (empty($_data['plano_price']) ||
	            empty($_data['plano_id']) ||
	            empty($_data['credit_type']) ||
	            empty($_data['user_id'])) {
	        return false;
	    }	    
	    
		$this->loadModel('Credit');
		$this->Credit->create();
		$this->Credit->set('cash', $_data['plano_price']);
		$this->Credit->set('user_id', $_data['user_id']);
		$this->Credit->set('plano_id', $_data['plano_id']);
		$this->Credit->set('credit_type', $_data['credit_type']);
		
		if ($this->Credit->save()) 
			return true;
		return false;
		
	}
}
