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

        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('cielo_api_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('cielo_api_total') > 0 && (float)$this->config->get('cielo_api_total') > $total) ||
            ((float)$this->config->get('cielo_api_parcela_minimo') > 0 && (float)$this->config->get('cielo_api_parcela_minimo') > $total)
        ) {
            $status = false;
        } elseif (!$this->config->get('cielo_api_geo_zone_id')) {
            $status = true;
        } elseif ($query->num_rows) {
            $status = true;
        } else {
            $status = false;
        }

        if ($this->config->get('cielo_api_config_parcelamento') == Type_ConfigParcelamento::PorParcela) {
            $status = $this->ftrgeralregistry->validateMinInstallment($total,$this->config->get('cielo_api_parcela_minimo'),$this->config->get('cielo_api_parcelamento_configs'));
        }

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

        $method_data = array();

        if ($status) {
            $quote_data[Type_PaymentOption::Tp_CreditCard] = array(
                'code'       => 'cielo_api_credito',
                'code_type'  => Type_PaymentOption::Tp_CreditCard,
                'title'      => ($this->config->get('cielo_api_descricao')) ? $this->config->get('cielo_api_descricao') : $this->language->get('text_title'),
                'sort_order' => $this->config->get('cielo_api_sort_order')
            );

            $method_data = array(
                'code'       => 'cielo_api_credito',
                'quote'      => $quote_data,
                'title'      => ($this->config->get('cielo_api_descricao')) ? $this->config->get('cielo_api_descricao') : $this->language->get('text_title'),
                'terms'      => '',
                'sort_order' => $this->config->get('cielo_api_sort_order')
            );
        }

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