<?php

class ControllerPaymentEredeRetorno extends Controller
{

	private $_urlRedirect;

	public function index()
	{
		$this->language->load('payment/erede');
		$this->load->model('payment/erede');
		$this->load->model('checkout/order');
		$this->load->model('catalog/information');
		$this->data['link_orders'] = $this->url->link('account/order');
        $this->data['link_contact'] = $this->url->link('information/contact');
        $this->data['button_orders'] = $this->language->get('button_orders');
        $this->data['text_contact'] = $this->language->get('text_contact');
        $this->data['text_support'] = $this->language->get('text_support');
        $this->data['text_total'] = $this->language->get('text_total');
        $this->data['text_reference'] = $this->language->get('text_reference');
        $this->data['text_error'] = $this->language->get('text_error');

		if ($_POST)
		{
			
			
 			if ($this->session->data['order_id'])
 			{
 				$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
 			}
			

			if ((int)$_POST['pedido'] > 0)
	        {
	          $this->model_payment_erede->salvaPedidoRede($_POST);

	          $orderId = (int)$_POST['pedido'];
	          $statusOrder = $_POST['status'];
	          $urlBoleto = $_POST['url_boleto'];
	          $formaPagamento = $_POST['forma_pagamento'];

	          $order_info = $this->model_checkout_order->getOrder($orderId);
	        }

	        //print_r($order_info);
	        /*else
	        {
	            $redeOrder = $this->getRedeOrderById((int)$orderId);
	            $statusOrder = $redeOrder['erede_status'];
	            $urlBoleto = $redeOrder['url_boleto'];
	            $formaPagamento = $redeOrder['forma_pagamento'];
	        }*/

	        //$statusOrder = 1;
	        
	        if ((int)$statusOrder == 1)
	        {    
	            if ($formaPagamento != "BOLETO")
	            {
	            	$this->model_checkout_order->update($orderId, 2);
	            }
	            
	            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/erede_retorno.tpl')) {
		            $this->template = $this->config->get('config_template') . '/template/payment/erede_retorno.tpl';
		        } else {
		            $this->template = 'default/template/payment/erede_retorno.tpl';
		        }

	            $this->model_payment_erede->saveLog($orderId, 'Processando', 'Transação Aprovada na Análise de Risco');
	        }
	        elseif ((int)$statusOrder == 1126)
	        {
	        	$this->model_checkout_order->update($orderId, 7);
	            
	            $this->model_payment_erede->saveLog($orderId, 'Fraude', 'Transação Rejeitada na Análise de Risco');
	            $this->template = 'default/template/payment/erede_error.tpl';
	        }
	        elseif ((int)$statusOrder == -1) //cancelamento pelo cliente
	        {
	        	$this->model_checkout_order->update($orderId, 7);
	            
	            $this->model_payment_erede->saveLog($orderId, 'Cancelado', 'Transação cancelada pelo cliente');
	            $this->template = 'default/template/payment/erede_error.tpl';
	        }
	        else
	        {
	            if ((int)$orderId > 0)
	            {
	            	$this->model_checkout_order->update($orderId, 7);
	            }
	            
	            $this->model_payment_erede->saveLog($orderId, 'Cancelado', 'Erro na transação');
	            $this->template = 'default/template/payment/erede_error.tpl';
	        }

	        $this->data['total_to_pay'] = 'R$ '.number_format($order_info['total'],2,'.','');
	        $this->data['status'] = (int)$statusOrder;
	        $this->data['urlBoleto'] = $urlBoleto;
	        $this->data['order_id'] = (int)$orderId;
	        $this->data['text_success'] = $this->language->get('text_success');
        	$this->data['imprimir_boleto'] = $this->language->get('imprimir_boleto');
	    }
	    else
	    {
	    	$this->data['text_error'] = $this->language->get('text_error');
	    	$this->template = 'default/template/payment/erede_error.tpl';
	    }

	   	$breadcrumbs = array(); 

      	$breadcrumbs[] = array(
        	'href'      => $this->url->link('common/home'),
        	'text'      => $this->language->get('text_home'),
        	'separator' => false
      	); 

		
      	$breadcrumbs[] = array(
      		'href'      => 'javascript:void(0)',
        	'text'      => $this->language->get('text_confirmation'),
        	'separator' => $this->language->get('text_separator')
      	);	

      	$this->data['breadcrumbs'] = $breadcrumbs;

	   $this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}

	private function _redirect()
	{

		$this->_urlRedirect = $this->request->post['url_post'];

		if (!empty($this->_urlRedirect )) {
			header('Location: ' . $this->_urlRedirect);
			$this->cart->clear();
		}
	}
}
?>