<?php

class ModelPaymentErede extends Model {
  	public function getMethod($address, $total) {
		$this->load->language('payment/erede');
		$status = true;
 
		$currencies = array(
			'EUR',
			'USD',
			'ILS',
			'BRL'
		);
 
		if (!in_array(strtoupper($this->currency->getCode()), $currencies)) {
			$status = false;
		}
 
		$method_data = array();
 
		if ($status) {
      		$method_data = array(
        		'code'       => 'erede',
        		'title'      => $this->language->get('erede_logo'),
				'sort_order' => $this->config->get('erede_sort_order')
      		);
    	}
 
    	return $method_data;
  	}

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

    public function getRiscoProduto($id_product)
	{
		$query = $this->db->query("SELECT risco_produto FROM `" . DB_PREFIX . "product` WHERE product_id = '" . (int)$id_product . "'");

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

	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();
	}

	public function salvaPedidoRede( Array $dados ) 
	{
		if (!isset($dados['card_scheme'])){ $dados['card_scheme'] = ''; }

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "rede_orders WHERE order_id = '" . (int)$dados['pedido'] . "'");
		if (!$query->rows)
		{
			$sql = "INSERT INTO ".DB_PREFIX."rede_orders values (
      			'',
      			'".(int)$dados['pedido']."',
      			'".$dados['token']."',
      			'".$dados['gateway_reference']."',
      			'".$dados['auth_host_reference']."',
      			'".$dados['auth_code']."',
      			'".$dados['reason']."',
      			'".$dados['status']."',
      			'".$dados['url_boleto']."',
      			'".$dados['extended_response_message']."',
      			'".$dados['extended_status']."',
      			'".$dados['forma_pagamento']."',
      			'".$dados['parcelas']."',
      			'".$dados['card_scheme']."'
      		)";

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

    public function getProductCategories($product_id) {
		$product_category_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
		
		foreach ($query->rows as $result) {
			$product_category_data[] = $result['category_id'];
		}

		return $product_category_data;
	}
}
?>