<?php
class ModelShippingJadLogTransport extends Model
{

    function getQuote($address, $pProduct = array())
    {
        $this->load->type('tipocomum');
        $this->load->language('shipping/jadlog_transport');
        $this->load->helper('ftr_metrics');


        if ((!$address['zone_id']) && ($address['layout_id'] == Type_SendPostcode::Tp_Product)) {
            $status = true;
        } else {
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('jadlog_transport_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')");

            if (!$this->config->get('jadlog_transport_geo_zone_id')) {
                $status = true;
            } elseif ($query->num_rows) {
                $status = true;
            } else {
                $status = false;
            }
        }

        $method_data = array();

        if ($status) {
            $peso      = 0;
            $sub_total = 0;
            if (sizeof($pProduct) > 0) {
                $cubic     = ftr_metrics::getCubicValues($pProduct);
                foreach ($pProduct as $prod) {
                    $peso       += $prod['weight'];
                    $sub_total  += $prod['price'] * $prod['quantity'];
                }
                $peso      = ($peso > $cubic['cubic_total']) ? $peso : $cubic['cubic_total'];
            } else {
                $products  = $this->cart->getProducts();
                $cubic     = ftr_metrics::getCubicValues($products);
                $peso      = $this->cart->getWeight();
                $peso      = ($peso > $cubic['cubic_total']) ? $peso : $cubic['cubic_total'];
                $sub_total = $this->cart->getSubTotal();
            }

            $dataSend =  array(
                "cepori"      => $this->config->get('jadlog_transport_cep'),
                "cepdes"      => str_replace('-', '', $address['postcode']),
                "frap"        => $this->config->get('jadlog_transport_frap'),
                "peso"        => $peso,
                "cnpj"        => $this->config->get('jadlog_transport_cnpj'),
                "conta"       => $this->config->get('jadlog_transport_conta'),
                "contrato"    => $this->config->get('jadlog_transport_contrato'),
                "modalidade"  => $this->config->get('jadlog_transport_mod'),
                "tpentrega"   => $this->config->get('jadlog_transport_tipo_entrega'),
                "tpseguro"    => $this->config->get('jadlog_transport_tipo_seguro'),
                "vldeclarado" => $sub_total,
                "vlcoleta"    => $this->config->get('jadlog_transport_vcoleta')
            );

            $jsonsend['frete'][] = $dataSend;
            $return_data = $this->jadlog->getQuote(json_encode($jsonsend));

            if (!isset($return_data['error']) && $return_data['valor'] > 0) {
                $return_data['prazo'] = $this->config->get('jadlog_transport_extra_days') ? $this->config->get('jadlog_transport_extra_days') + $return_data['prazo'] : $return_data['prazo'];
                $quote_data['jadlog_transport'] = array(
                    'code'         => 'jadlog_transport.jadlog_transport',
                    'title'        => sprintf($this->language->get('text_jadlog_prazo'), $return_data['prazo']),
                    'days'         => $return_data['prazo'],
                    'cost'         => round((float)$return_data['valor'], 2, PHP_ROUND_HALF_UP),
                    'text'         => $this->currency->format($return_data['valor'])
                );

                $method_data = array(
                    'code'       => 'jadlog_transport',
                    'title'      => $this->config->get('jadlog_transport_name'),
                    'quote'      => $quote_data,
                    'sort_order' => $this->config->get('jadlog_transport_sort_order'),
                    'error'      => false
                );

                return $method_data;
            }
        }
    }
}
