<?php

App::uses('PagSeguroAppController', 'PagSeguro.Controller');

/**
 * CreditHistories Controller
 *
 * @property Credit $Credit
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class PagSeguroController extends PagSeguroAppController {

/**
 * beforeFilter
 *
 * @return void
 */
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('retorno');
    }
    
/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator', 'Session');

/**
 * 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->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['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']);
		
		if ($this->Credit->save()) 
			return true;
		return false;
		
	}
	
/**
 * Trata o retorno do pagseguro
 */
	public function retorno() {
	    if ($this->request->is('post')) {
    	        debug($this->request->data);
    	      $VendedorEmail	=	$this->request->data['VendedorEmail'];
              $TransacaoID	=	$this->request->data['TransacaoID'];
              $Referencia	=	$this->request->data['Referencia'];
              $Extras	=	$this->request->data['Extras'];
              $TipoFrete	=	$this->request->data['TipoFrete'];
              $ValorFrete	=	$this->request->data['ValorFrete'];
              $Anotacao	=	$this->request->data['Anotacao'];
              $DataTransacao	=	$this->request->data['DataTransacao'];
              $TipoPagamento	=	$this->request->data['TipoPagamento'];
              $StatusTransacao	=	$this->request->data['StatusTransacao'];
              $CliNome	=	$this->request->data['CliNome'];
              $CliEmail	=	$this->request->data['CliEmail'];
              $CliEndereco	=	$this->request->data['CliEndereco'];
              $CliNumero	=	$this->request->data['CliNumero'];
              $CliComplemento	=	$this->request->data['CliComplemento'];
              $CliBairro	=	$this->request->data['CliBairro'];
              $CliCidade	=	$this->request->data['CliCidade'];
              $CliEstado	=	$this->request->data['CliEstado'];
              $CliCEP	=	$this->request->data['CliCEP'];
              $CliTelefone	=	$this->request->data['CliTelefone'];
              $ProdID_x	=	$this->request->data['ProdID_1'];
              $ProdDescricao_x	=	$this->request->data['ProdDescricao_1'];
              $ProdValor_x	=	$this->request->data['ProdValor_1'];
              $ProdQuantidade_x	=	$this->request->data['ProdQuantidade_1'];
              $ProdFrete_x	=	$this->request->data['ProdFrete_1'];
              $NumItens	=	$this->request->data['NumItens'];
              
              $this->set('VendedorEmail',
                            'TransacaoID',
                            'Referencia',
                            'Extras',
                            'TipoFrete',
                            'ValorFrete',
                            'Anotacao',
                            'DataTransacao',
                            'TipoPagamento',
                            'StatusTransacao',
                            'CliNome',
                            'CliEmail',
                            'CliEndereco',
                            'CliNumero',
                            'CliComplemento',
                            'CliBairro',
                            'CliCidade',
                            'CliEstado',
                            'CliCEP',
                            'CliTelefone',
                            'ProdID_1',
                            'ProdDescricao_1',
                            'ProdValor_1',
                            'ProdQuantidade_1',
                            'ProdFrete_1',
                            'NumItens');
	    }
	}
}
