<?php

class ModelPaymentPaypal extends Model
{
  public function getMethod($address, $total)
  {

    if(!$this->config->get('paypal_status')){
      return false;
    }

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

    $this->load->language('payment/paypal');
    $this->load->helper('ftr_geral');

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

    $method_data = array();

    if ($status) {
        $quote_data = array();

      if($this->config->get('paypal_checkout_buttons')){
        $quote_data[Type_PaymentOption::Tp_ExternalCheckout] = array(
          'code' => 'paypal',
          'code_type' => Type_PaymentOption::Tp_ExternalCheckout,
          'title' => ($this->config->get('paypal_title_checkout')) ? $this->config->get('paypal_title_checkout') : $this->language->get('text_title') ,
          'sort_order' => $this->config->get('paypal_sort_order')
        );
      }
        $method_data = array(
            'code' => 'paypal',
            'quote' => $quote_data,
            'title' => $this->language->get('text_title'),
            'terms' => '',
            'sort_order' => $this->config->get('paypal_sort_order')
        );
    }




    return $method_data;
  }

  public function getOrderIdByIntegracao($codigo_integracao){
    $query = $this->db->query("SELECT order_id FROM oc_order where codigo_integracao_pagamento=$codigo_integracao")->rows;
    if ($query->num_rows == 1){
      return $query['order_id'];
    }else{
      return false;
    }
  }

  public function setCheckoutOrderHistory($order_id, $paypal_status, $client_notify = 0){

    $this->load->dmodel('checkout/order');
    $this->load->type('tipopaypal');

    switch ($paypal_status){
      case Type_StatusOrderPaypal::Tp_Created:
        $order_status_id = $this->config->get('paypal_order_created_status_id');
        break;
      case Type_StatusOrderPaypal::Tp_Saved:
        $order_status_id = $this->config->get('paypal_order_saved_status_id');
        break;
      case Type_StatusOrderPaypal::Tp_Approved:
        $order_status_id = $this->config->get('paypal_order_approved_status_id');
        break;
      case Type_StatusOrderPaypal::Tp_Voided:
        $order_status_id = $this->config->get('paypal_order_voided_status_id');
        break;
      case Type_StatusOrderPaypal::Tp_Completed:
        $order_status_id = $this->config->get('paypal_order_completed_status_id');
        break;
      default:
        $order_status_id = $this->config->get('paypal_order_status_waiting');
    }

    $datahistory = array(
      'order_status_id' => $order_status_id,
      'comment' => '',
      'client_notify' => $client_notify
    );

    $this->dmodel_checkout_order->addOrderHistory($order_id, $datahistory);

  }

}
