/**
  * Returns the Currency used in the Country to which a specific IP Address
  * belongs.
  *
  * @param string host A host name or IP Address.
  * @param string default_currency The Currency to use as a default in case the
  * Country currency could not be detected.
  * @return string|bool A currency code, or False if an error occurred.
  */
 public function get_currency_by_host($host, $default_currency)
 {
     $ip2location = WC_Aelia_IP2Location::factory();
     $country_code = $ip2location->get_country_code($host);
     if ($country_code === false) {
         Logger::log(sprintf(__('Could not retrieve Country Code for host "%s". Using ' . 'default currency: %s. Error messages (JSON): %s.', AELIA_CS_PLUGIN_TEXTDOMAIN), $host, $default_currency, json_encode($ip2location->get_errors())));
         return $default_currency;
     }
     $country_currency = $this->get_country_currency($country_code);
     if (WC_Aelia_CurrencySwitcher::settings()->is_currency_enabled($country_currency)) {
         return $country_currency;
     } else {
         return $default_currency;
     }
 }
 /**
  * Get a report class and render it. This method returns a report class
  * implemented by the Currency Switcher, overriding the standard one.
  *
  * @param string report_name The name of the report to render.
  */
 public function get_report($report_name)
 {
     $report_name = sanitize_title($report_name);
     // Load report override class
     $report_override_class = $this->report_override_class($report_name);
     $report_class = class_exists($report_override_class) ? $report_override_class : $this->report_original_class($report_name);
     Logger::log(sprintf(__('Attempting to render report "%s". Report class: "%s".', AELIA_CS_PLUGIN_TEXTDOMAIN), $report_name, $report_class), true);
     if (class_exists($report_class)) {
         $report = new $report_override_class();
         $report->output_report();
     } else {
         Logger::log(sprintf(__('Class "%s" not found, report rendering aborted.', AELIA_CS_PLUGIN_TEXTDOMAIN), $report_class), true);
     }
 }
 /**
  * Logs a message.
  *
  * @param string message The message to log.
  * @param bool debug Indicates if the message is for debugging. Debug messages
  * are not saved if the "debug mode" flag is turned off.
  */
 protected function log($message, $debug = true)
 {
     Logger::log($message, $debug);
 }
 /**
  * Calculate order totals and taxes in base currency for Orders that have been
  * generated before version 3.2.10.1402126. This method corrects the calculation
  * of order totals in base currency, which were incorrectly made taking into
  * account the exchange markup eventually specified in configuration.
  * Note: recalculation is made from 2014-01-01 onwards, as exchange rates have
  * changed significantly in the past months and it's not currently possible
  * to retrieve them at a specific point in time.
  *
  * @return bool
  */
 protected function update_to_3_2_10_1402126()
 {
     $base_currency = $this->settings->base_currency();
     // Retrieve the exchange rates for the orders whose data already got
     // partially converted
     $SQL = "\r\n\t\t\tSELECT\r\n\t\t\t\tposts.ID AS order_id\r\n\t\t\t\t,posts.post_date AS post_date\r\n\t\t\t\t,meta_order.meta_key\r\n\t\t\t\t,meta_order.meta_value\r\n\t\t\t\t-- ,meta_order_base_currency.meta_key AS meta_key_base_currency\r\n\t\t\t\t,meta_order_base_currency.meta_value AS meta_value_base_currency\r\n\t\t\t\t,meta_order_currency.meta_value AS currency\r\n\t\t\tFROM\r\n\t\t\t\t{$this->wpdb->posts} AS posts\r\n\t\t\tJOIN\r\n\t\t\t\t{$this->wpdb->postmeta} AS meta_order ON\r\n\t\t\t\t\t(meta_order.post_id = posts.ID) AND\r\n\t\t\t\t\t(meta_order.meta_key IN ('_order_total', '_order_discount', '_cart_discount', '_order_shipping', '_order_tax', '_order_shipping_tax'))\r\n\t\t\tLEFT JOIN\r\n\t\t\t\t{$this->wpdb->postmeta} AS meta_order_base_currency ON\r\n\t\t\t\t\t(meta_order_base_currency.post_id = posts.ID) AND\r\n\t\t\t\t\t(meta_order_base_currency.meta_key = CONCAT(meta_order.meta_key, '_base_currency')) AND\r\n\t\t\t\t\t(meta_order_base_currency.meta_value > 0)\r\n\t\t\tLEFT JOIN\r\n\t\t\t\t{$this->wpdb->postmeta} AS meta_order_currency ON\r\n\t\t\t\t\t(meta_order_currency.post_id = posts.ID) AND\r\n\t\t\t\t\t(meta_order_currency.meta_key = '_order_currency')\r\n\t\t\tLEFT JOIN\r\n\t\t\t\t{$this->wpdb->term_relationships} AS rel ON\r\n\t\t\t\t\t(rel.object_ID = posts.ID)\r\n\t\t\tLEFT JOIN\r\n\t\t\t\t{$this->wpdb->term_taxonomy} AS taxonomy ON\r\n\t\t\t\t\t(taxonomy.term_taxonomy_id = rel.term_taxonomy_id)\r\n\t\t\tLEFT JOIN\r\n\t\t\t\t{$this->wpdb->terms} AS term ON\r\n\t\t\t\t\t(term.term_id = taxonomy.term_id)\r\n\t\t\tWHERE\r\n\t\t\t\t(posts.post_type = 'shop_order') AND\r\n\t\t\t\t(meta_order.meta_key = '_order_total') AND\r\n\t\t\t\t(meta_order.meta_value IS NOT NULL) AND\r\n\t\t\t\t(meta_order_base_currency.meta_value IS NOT NULL) AND\r\n\t\t\t\t(post_date >= '2014-01-01 00:00:00')\r\n\t\t";
     $orders_to_update = $this->select($SQL);
     // Debug
     //var_dump($orders_to_update); die();
     foreach ($orders_to_update as $order) {
         // If order currency is empty, for whatever reason, no conversion can be
         // performed (it's not possible to assume that a specific currency was
         // used)
         if (empty($order->currency)) {
             Logger::log(sprintf(__('Order %s does not have a currency associated, therefore ' . 'it is not possible to determine its value in base currency (%s). ' . 'This may lead to imprecise results in the reports.', AELIA_CS_PLUGIN_TEXTDOMAIN), $order->order_id, $base_currency));
             continue;
         }
         // Try to retrieve the exchange rate used when the order was placed
         $value_in_base_currency = $this->convert($order->meta_value, $order->currency, $base_currency, $order);
         $value_in_base_currency = WC_Aelia_CurrencySwitcher::instance()->float_to_string($value_in_base_currency);
         try {
             update_post_meta($order->order_id, $order->meta_key . '_base_currency', $value_in_base_currency);
         } catch (Exception $e) {
             $this->add_message(E_USER_ERROR, sprintf(__('Exception occurred updating base currency values for order %s. ' . 'Error: %s.'), $order->order_id, $e->getMessage()));
             return false;
         }
     }
     return true;
 }