<?php
class ModelPaymentRedeCredito extends Model
{
    public function getMethod($address, $total)
    {
        $this->load->language('payment/rede');

        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('rede_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')");

        if (((float)$this->config->get('rede_total') > 0 && (float)$this->config->get('rede_total') > $total)||
            ((float)$this->config->get('rede_parcela_minimo') > 0 && (float)$this->config->get('rede_parcela_minimo') > $total)) {
            $status = false;
        } elseif (!$this->config->get('rede_geo_zone_id')) {
            $status = true;
        } elseif ($query->num_rows) {
            $status = true;
        } else {
            $status = false;
        }

        if ($this->config->get('rede_config_parcelamento') == Type_ConfigParcelamento::PorParcela) {
            $status = $this->ftrgeralregistry->validateMinInstallment($total, $this->config->get('rede_parcela_minimo'),$this->config->get('rede_parcelamento_configs'));
        }

        if (ftr_geral::isIsset($this->config->get('rede_display')) && (!$this->ftrgeralregistry->exibeMuduloLayout($this->config->get('rede_display')))) {
            $status = false;
        }

        $method_data = array();

        if ($status) {

            $quote_data[Type_PaymentOption::Tp_CreditCard] = array(
                'code'       => 'rede_credito',
                'code_type'  => Type_PaymentOption::Tp_CreditCard,
                'title'      => ($this->config->get('rede_descricao')) ? $this->config->get('rede_descricao') : $this->language->get('text_title'),
                'sort_order' => $this->config->get('rede_credito_sort_order')
            );

            $method_data = array(
                'code'       => 'rede_credito',
                'code_type'  => Type_PaymentOption::Tp_CreditCard,
                'quote'      => $quote_data,
                'title'      => ($this->config->get('rede_descricao')) ? $this->config->get('rede_descricao') : $this->language->get('text_title'),
                'terms'      => '',
                'sort_order' => $this->config->get('rede_credito_sort_order')
            );
        }

        if (ftr_geral::isIsset($this->config->get('rede_client_teste')) && (strlen($this->config->get('rede_client_teste')) > 0)) {
            if ($this->customer->getId() == $this->config->get('rede_client_teste')) {
                return $method_data;
            }
        } else {
            return $method_data;
        }
    }
}
