<?php
class ModelPaymentErede extends Model {

    public function install() {

        $this->db->query("CREATE TABLE IF NOT EXISTS `".DB_PREFIX."rede_orders` (
        `id` INT(10) NOT NULL AUTO_INCREMENT,
        `order_id` INT( 10 ) NOT NULL,
        `erede_token` VARCHAR(100) NOT NULL DEFAULT '0',
        `gateway_reference` VARCHAR(100) NULL,
        `auth_host_reference` VARCHAR(100) NULL,
        `auth_code` VARCHAR(100) NULL,
        `reason` VARCHAR(100) NULL,
        `erede_status` VARCHAR(100) NULL,
        `url_boleto` VARCHAR(255) NULL,
        `extended_response_message` VARCHAR(200) NULL,
        `extended_status` VARCHAR(200) NULL,
        `forma_pagamento` VARCHAR(100) NULL,
        `parcelas` VARCHAR(100) NULL,
        `card_scheme` VARCHAR(100) NULL,
        PRIMARY KEY (`id`),
        UNIQUE (`order_id`)
      ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;");

        $this->db->query("CREATE TABLE IF NOT EXISTS `".DB_PREFIX."rede_orders_log` (
            `id_rede_orders_log` int(10) NOT NULL auto_increment,
            `order_id` varchar(255) NOT NULL,
            `processed_at` timestamp NOT NULL default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            `status` varchar(255) NULL,
            `message` text NULL,
            PRIMARY KEY  (`id_rede_orders_log`)
          ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;");

        $sql = 'SHOW COLUMNS FROM '. DB_PREFIX .'product LIKE "risco_produto"';
        $result = $this->db->query($sql);
        if ($result->num_rows <= 0)
        {
            $this->db->query('ALTER TABLE ' . DB_PREFIX . 'product 
                                    ADD `risco_produto` VARCHAR(255) NOT NULL default "low" ');   
        }

       //$this->load->model('setting/setting');
       //$this->model_setting_setting->editSetting('erede', array('erede_status'=>1));
    }

    public function uninstall() {

        //$this->db->query('ALTER TABLE ' . DB_PREFIX . 'product DROP COLUMN `risco_produto`');
        $this->model_setting_setting->editSetting('erede', array('erede_status'=>0));
    }

    public function saveLog($orderId, $status, $message)
    {
        $sql = "INSERT INTO ".DB_PREFIX."rede_orders_log values ('','".(int)$orderId."','".$status."','".$message."','')";
        $this->db->query($sql);
    }

    public function getPedidoRede($idOrder)
    {
        $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "rede_orders` WHERE order_id = '" . (int)$idOrder . "' LIMIT 0,1");

        if ($query->num_rows > 0) 
        {
            $result = $query->rows;
            return $result[0];
        }
        else
            return array();
    }

}
?>
