<?php

class ControllerPaymentFuturaKomerci extends Controller {

	private $error = array();

	public function index() {

		$this->load->type('tipocomum');
		$this->load->helper('ftr_geral');

		$this->language->load('payment/FuturaKomerci');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('setting/setting');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting('FuturaKomerci', $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'));
		}

		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_all_zones'] = $this->language->get('text_all_zones');

		$this->data['entry_order_status'] = $this->language->get('entry_order_status');
		$this->data['entry_total'] = $this->language->get('entry_total');
		$this->data['entry_geo_zone'] = $this->language->get('entry_geo_zone');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_display'] = $this->language->get('entry_display');

		$this->data['button_save'] = $this->language->get('button_save');
    $this->data['button_cancel'] = $this->language->get('button_cancel');

    $this->data['text_module_info'] = $this->language->get('text_module_info');
    $this->data['text_module_sub_info'] = $this->language->get('text_module_sub_info');


 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

  		$this->data['breadcrumbs'] = array();

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

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_payment'),
			'href'      => $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'),
      		'separator' => ' :: '
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('payment/FuturaKomerci', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL'),
      		'separator' => ' :: '
   		);

		$this->data['action'] = $this->url->link('payment/FuturaKomerci', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL');
    $this->data['cancel'] = $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL');

		$this->data['displaymoduleoption'] = Type_DisplayModuleOption::getRemoveArray([Type_DisplayModuleOption::Tp_Aplicativo, Type_DisplayModuleOption::Tp_Todos]);

		//dados modulo
		$this->pegar('FuturaKomerci_serial');
		$this->pegar('FuturaKomerci_nome');
		$this->pegar('FuturaKomerci_order_status_id');
		$this->pegar('FuturaKomerci_aprovado');
		$this->pegar('FuturaKomerci_cancelado');
		$this->pegar('FuturaKomerci_afiliacao');
		$this->pegar('FuturaKomerci_minimo');
		$this->pegar('FuturaKomerci_desconto_credito');
		$this->pegar('FuturaKomerci_api_display');

		//visa
		$this->pegar('FuturaKomerci_visa_ativo');
		$this->pegar('FuturaKomerci_visa_div');
		$this->pegar('FuturaKomerci_visa_sem');
		$this->pegar('FuturaKomerci_visa_juros');
		//mastercard
		$this->pegar('FuturaKomerci_mastercard_ativo');
		$this->pegar('FuturaKomerci_mastercard_div');
		$this->pegar('FuturaKomerci_mastercard_sem');
		$this->pegar('FuturaKomerci_mastercard_juros');
		//diners
		$this->pegar('FuturaKomerci_diners_ativo');
		$this->pegar('FuturaKomerci_diners_div');
		$this->pegar('FuturaKomerci_diners_sem');
		$this->pegar('FuturaKomerci_diners_juros');
		//hiper
		$this->pegar('FuturaKomerci_hiper_ativo');
		$this->pegar('FuturaKomerci_hiper_div');
		$this->pegar('FuturaKomerci_hiper_sem');
		$this->pegar('FuturaKomerci_hiper_juros');
		//hipercard
		$this->pegar('FuturaKomerci_hipercard_ativo');
		$this->pegar('FuturaKomerci_hipercard_div');
		$this->pegar('FuturaKomerci_hipercard_sem');
		$this->pegar('FuturaKomerci_hipercard_juros');

		//tipo de parcelamento
		$this->pegar('FuturaKomerci_tipo_parcelamento');
		//total minimo
		$this->pegar('FuturaKomerci_total');
		$this->pegar('FuturaKomerci_debug');
		$this->pegar('FuturaKomerci_modo_teste');
		$this->pegar('FuturaKomerci_cliente_teste');

		$this->load->model('localisation/order_status');
		$this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
		$this->pegar('FuturaKomerci_geo_zone_id');

		$this->load->model('localisation/geo_zone');
		$this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();
		$this->pegar('FuturaKomerci_status');

		$this->pegar('FuturaKomerci_sort_order');

		$this->template = 'payment/FuturaKomerci.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

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

	public function pegar($campo){
		if (isset($this->request->post[$campo])) {
			$this->data[$campo] = $this->request->post[$campo];
		} else {
			$this->data[$campo] = $this->config->get($campo);
		}
	}

	protected function validate() {
		if (!$this->user->hasPermission('modify', 'payment/FuturaKomerci')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	public function getStatus($status)
		{

			switch($status)
			{
				case "0": $status = "Criada (Aguardando Pagamento)";
						break;
				case "1": $status = "Em andamento";
						break;
				case "2": $status = "Autenticada";
						break;
				case "3": $status = "Nao autenticada";
						break;
				case "4": $status = "Autorizada";
						break;
				case "5": $status = "Nao autorizada";
						break;
				case "6": $status = "Capturada (Aprovada)";
						break;
				case "8": $status = "Nao capturada";
						break;
				case "9": $status = "Cancelada";
						break;
				case "10": $status = "Em autenticacao";
						break;
				default: $status = "n/a";
						break;
			}

			return $status;
		}

		public function getLr($lrcod)
		{

			switch($lrcod)
			{
				case "00":
				$lrcod = "00 - Transacao autorizada";
				break;
				case "01":
				case "04":
				case "07":
				case "41":
				case "62":
				case "63":
				$lrcod = $lrcod." - Transacao referida/com restricao pelo emissor (Contate o emissor de seu cartao)";
				break;
				case "05":
				$lrcod = "05 - Transacao nao autorizada";
				break;
				case "12":
				case "82":
				$lrcod = $lrcod." - Transacao invalida";
				break;
				case "13":
				$lrcod = "13 - Valor invalido";
				break;
				case "14":
				$lrcod = "14 - Cartao invalido";
				break;
				case "15":
				$lrcod = "15 - Emissor invalido";
				break;
				case "51":
				$lrcod = "51 - Saldo insuficiente";
				break;
				case "54":
				$lrcod = "54 - Cartao vencido";
				break;
				case "57":
				case "58":
				$lrcod = $lrcod." - Transacao nao permitida";
				break;
				case "78":
				$lrcod = "78 - Cartao nao desbloqueado (Contate o emissor de seu cartao)";
				break;
				case "91":
				$lrcod = "91 - Banco indisponivel";
				break;
				case "AC":
				$lrcod = "AC - Cartao de debito tentando seu usado como credito.";
				break;
				default:
				$lrcod = $lrcod;
			}

			return $lrcod;
		}

	public function getStatusCielo($status)
	{
			switch($status)
			{
				case "0":
				$status = "Criada (Aguardando Pagamento)";
				$status_id = $this->config->get('FuturaKomerci_order_status_id');
				break;
				case "1":
				$status = "Em andamento";
				$status_id = $this->config->get('FuturaKomerci_pendente');
				break;
				case "2":
				$status = "Autenticada";
				$status_id = $this->config->get('FuturaKomerci_pendente');
				break;
				case "3":
				$status = "Nao autenticada";
				$status_id = $this->config->get('FuturaKomerci_pendente');
				break;
				case "4":
				$status = "Autorizada";
				$status_id = $this->config->get('FuturaKomerci_autorizado');
				break;
				case "5":
				$status = "Nao autorizada";
				$status_id = $this->config->get('FuturaKomerci_cancelado');
				break;
				case "6":
				$status = "Capturada (Aprovada)";
				$status_id = $this->config->get('FuturaKomerci_aprovado');
				break;
				case "8":
				$status = "Nao capturada";
				$status_id = $this->config->get('FuturaKomerci_pendente');
				break;
				case "9":
				$status = "Cancelada";
				$status_id = $this->config->get('FuturaKomerci_cancelado');
				break;
				case "10":
				$status = "Em autenticacao";
				$status_id = $this->config->get('FuturaKomerci_pendente');
				break;
				default:
				$status = "n/a";
				$status_id = $this->config->get('FuturaKomerci_order_status_id');
				break;
			}

			return array('nome'=>$status,'id'=>$status_id);
	}
}
