<?php

/*
************************************************************************
Copyright [2013] [PagSeguro Internet Ltda.]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************
*/

/**
 * Model Payment PagSeguro.
 * Model responsible for the searches, changes and updates to the tables of PagSeguro.
 */
class ModelPaymentPagSeguro extends Model
{

    private $_languages = array();

    /**
     * Array with the language codes supported by PagSeguro
     * @var array
     */
    private static $array_languages = array(
        "English" => "en",
        "Portugues" => "pt-br"
    );

    /**
     * Array with the PagSeguro status, Portuguese and English
     * @var array
     */
    private static $array_order_status = array(
        0 => array('pt-br' => 'Pedido abandonado', 'en' => 'Abandoned order'),
        1 => array('pt-br' => 'Aguardando pagamento', 'en' => 'Awaiting payment'),
        2 => array('pt-br' => 'Processando', 'en' => 'Processing'),
        3 => array('pt-br' => 'Processado', 'en' => 'Processed'),
        4 => array('pt-br' => 'Completo', 'en' => 'Complete'),
        5 => array('pt-br' => 'Em disputa', 'en' => 'Dispute'),
        6 => array('pt-br' => 'Reembolsado', 'en' => 'Refunded'),
        7 => array('pt-br' => 'Cancelado', 'en' => 'Canceled'));

    private static $array_paymentMethod = array(
        1 => 'Cartão de Crédito',
        2 => 'Boleto',
        3 => 'Débito Online (TEF)',
        4 => 'Saldo PagSeguro',
        5 => 'Oi Paggo',
        7 => 'Depósito em Conta');

    /**
     *
     * @param type $address
     * @param type $total
     * @return string
     */
    public function getMethod($address, $total)
    {
        $status = true;
        $this->language->load('payment/pagseguro');

        if ($this->config->get('pagseguro_total') > 0 && $this->config->get('pagseguro_total') > $total) {
            $status = false;
        }

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

        $method_data = array();

        if ($status) {
            $method_data = array(
                'code' => 'pagseguro',
                'title' => ($this->config->get('pagseguro_title_name')) ? $this->config->get('pagseguro_title_name') : $this->language->get('heading_title'),
                'sort_order' => $this->config->get('pagseguro_sort_order')
            );
        }
        return $method_data;

    }

    /**
     * Method responsible for saving the status PagSeguro according to the languages ​​active in the system, by default saves english
     */
    public function savePagSeguroOrderStatus()
    {
        $this->_languages = $this->_getCodeLanguages();

        if (in_array(self::$array_languages['Portugues'], $this->_languages)) {
            $id_language_br = $this->_getIdLanguageByCode(self::$array_languages['Portugues']);
            $this->_saveOrderStatus(self::$array_languages['Portugues'], $id_language_br);
        }

        $id_language_en = $this->_getIdLanguageByCode(self::$array_languages['English']);
        $this->_saveOrderStatus(self::$array_languages['English'], $id_language_en);
    }

    /**
     * Retrieves the id and code of languages ​​active in the system
     * @return array
     */
    private function _getCodeLanguages()
    {
        $data = array();
        $query = $this->db->query(' SELECT language_id, code
                                           FROM ' . DB_PREFIX . 'language
                                           WHERE status = ' . (int)1);

        foreach ($query->rows as $result) {
            $data[$result['language_id']] = $result['code'];
        }

        return $data;
    }

    /**
     * Retrieves the id and code of languages ​​active in the system.
     * @param type $language_code
     * @return int
     */
    public function _getIdLanguageByCode($language_code = 'en')
    {
        $id_language = null;
        $query = $this->db->query(" SELECT language_id
                                           FROM " . DB_PREFIX . "language
                                           WHERE `code` = '" . $this->db->escape($language_code) . "'");

        foreach ($query->rows as $result) {
            $id_language = (int)$result['language_id'];
        }

        return $id_language;
    }

    /**
     * Retrieves the code of the language according to the id.
     * @param type $id_language
     * @return string
     */
    public function getCodeLanguageById($id_language)
    {
        $code_language = null;
        $query = $this->db->query(" SELECT code
                                           FROM " . DB_PREFIX . "language
                                           WHERE `language_id` = " . (int)$id_language);

        foreach ($query->rows as $result) {
            $code_language = (string)$result['code'];
        }

        return $code_language;
    }

    /**
     * Save Order Status
     * @param type $code_language
     * @param type $id_language
     */
    private function _saveOrderStatus($code_language, $id_language)
    {
        foreach (self::$array_order_status as $key => $value) {
            if ($this->getOrderStatusByName($value[$code_language], $id_language) == NULL)
                $this->db->query("INSERT INTO " . DB_PREFIX . "order_status SET `language_id` = '" . (int)$id_language . "', `name` = '" . $this->db->escape($value[$code_language]) . "'");
        }
    }

    /**
     * Retrieves the id of status in accordance with the name and id of the language.
     * @param type $name_status
     * @param type $id_language
     * @return int
     */
    public function getOrderStatusByName($name_status, $id_language)
    {

        $order_id = NULL;
        $query = $this->db->query("SELECT order_status_id
                                           FROM " . DB_PREFIX . "order_status
                                           WHERE `name` = '" . $this->db->escape($name_status) . "'
                                           AND   `language_id` = '" . (int)$id_language . "'");
        if (!empty($query->rows)) {
            foreach ($query->rows as $result) {
                $order_id = (int)$result['order_status_id'];
            }
        }

        return $order_id;
    }


    /**
     * Update table order
     * @param type $id_order
     * @param type $order_status_id
     */
    public function updateOrder($id_order, $order_status_id, $codigo_integracao)
    {
        $this->db->query("UPDATE `" . DB_PREFIX . "order` SET `order_status_id` = '" . (int)$order_status_id . "', `codigo_integracao_pagamento` = '" . $codigo_integracao . "' WHERE `order_id` = '" . (int)$id_order . "'");
    }

    public function updateValueOrder($id_order, $order_value)
    {
        $this->db->query("UPDATE `" . DB_PREFIX . "order` SET `total` = '" . $order_value . "' WHERE `order_id` = '" . (int)$id_order . "'");
        $this->db->query("UPDATE `" . DB_PREFIX . "order_total`
                       SET
                        `value` = '" . $order_value . "',
                        `text` = '" . $this->currency->format($order_value) . "'
                       WHERE
                        `order_id` = '" . (int)$id_order . "' AND
                        `code` = 'total' "
        );
    }

    public function InsertOrderDescontoPagseguro($id_order, $discount, $paymentMethod)
    {
        $this->db->query("INSERT INTO `" . DB_PREFIX . "order_total`
             SET
                           `order_id` = '" . $id_order . "',
                 `code`  = 'descontopagseguro',
                 `title` = '" . $paymentMethod . "',
                 `text`  = '" . $this->currency->format($discount * -1) . "',
                 `value` = '" . ($discount * -1) . "',
                 `sort_order` = '2'"
        );
    }

    /**
     * Save order history
     * @param type $order_id
     * @param type $id_order_status
     */
    public function saveOrderHistory($order_id, $id_order_status)
    {
        $this->load->dmodel('checkout/order');
        $this->dmodel_checkout_order->update($order_id, $id_order_status, 'STATUS ATUALIZADO', true);
    }

    /**
     * Return name order Status by id
     * @param type $id_order_status
     * @return string
     */
    private function _getNameOrderStatusById($id_order_status)
    {

        $name_status = NULL;
        $query = $this->db->query("SELECT `name`
                                           FROM " . DB_PREFIX . "order_status
                                           WHERE   `order_status_id` = '" . (int)$id_order_status . "'");

        if (!empty($query->rows)) {
            foreach ($query->rows as $result) {
                $name_status = $result['name'];
            }
        }
        return $name_status;
    }

    public function getInfoOrderPagseguro($order_id)
    {

        $sql = 'select '
            . 'count(oh.order_history_id) as total, '
            . '(oh.order_status_id) as order_status_id '
            . 'from oc_order_history oh '
            . 'inner join oc_order_status os on (oh.order_status_id = os.order_status_id) '
            . 'inner join oc_order o on (o.order_id = oh.order_id) '
            . 'where oh.order_id = ' . $order_id;

        $query = $this->db->query($sql);

        if (!empty($query->rows)) {
            return $query->row;
        } else {
            return false;
        }

    }

    /**
     * Remove accent
     * @param type $value
     * @return string
     */
    public function removeAccent($value)
    {
        return strtoupper(strtr($value, "áàãâéêíóôõúüçÁÀÃÂÉÊÍÓÔÕÚÜÇ", "aaaaeeiooouucAAAAEEIOOOUUC"));
    }

    /**
     * Return array of status
     * @return array
     */
    public function getOrderStatus()
    {
        return self::$array_order_status;
    }

    public function getPaymentMethod()
    {
        return self::$array_paymentMethod;
    }

}
