businessdad

Aelia Currency Switcher - SQL query to compare order data for sales reports and analytics totals

Mar 15th, 2021 (edited)
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.60 KB | None | 0 0
  1. /**
  2.  * Extracts and compares the order data used by WooCommerce and the Aelia Currency Switcher to generate
  3.  * the sales reports on the traditional reports and the analytics.
  4.  *
  5.  * DISCLAIMER
  6.  * THE USE OF THIS CODE IS AT YOUR OWN RISK. You remain fully liable for compliance with tax laws.
  7.  * This code is offered free of charge and there is no warranty for it, to the extent permitted by applicable law.
  8.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  9.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  10.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  11.  * is with you. Should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  12.  *
  13.  * The code is provided as an example and it's not covered by our support service. We won't be able to offer free support
  14.  * in relation to it. Should you need a consultation, or assistance to customise this code, you can contact us to avail
  15.  * of our paid consultation services: https://aelia.co/hire_us
  16.  */
  17.  
  18. SELECT
  19.     ORDERS_META1.post_id AS order_id
  20.     ,ORDERS_META1.meta_value AS order_total
  21.     ,ORDERS_META2.meta_value AS order_total_base_currency
  22.     ,ORDERS_META3.meta_value AS exchange_rate
  23.     ,(ORDERS_META2.meta_value / ORDERS_META1.meta_value) AS exchange_rate_calculated
  24.     ,(ORDERS_META1.meta_value * ORDERS_META3.meta_value) AS order_total_base_currency_calculated
  25.     -- Calculates the difference between the exchange rate stored in the database and the one calculated from
  26.     -- the source data
  27.     ,(ORDERS_META2.meta_value / ORDERS_META1.meta_value) - ORDERS_META3.meta_value as exchange_rate_difference
  28.     -- Calculates the difference between the order totals in base currency stored in the database and the one
  29.     -- calculated using the exchange rate
  30.     ,(ORDERS_META1.meta_value * ORDERS_META3.meta_value) - ORDERS_META2.meta_value as order_total_calculated_difference
  31. FROM
  32.     wp_posts ORDERS
  33. LEFT JOIN
  34.     wp_postmeta ORDERS_META1 ON
  35.         (ORDERS_META1.meta_key = '_order_total') AND
  36.         (ORDERS_META1.post_id = ORDERS.ID)
  37. LEFT JOIN
  38.     wp_postmeta ORDERS_META2 ON
  39.         (ORDERS_META2.post_id = ORDERS_META1.post_id) AND
  40.         (ORDERS_META2.meta_key = '_order_total_base_currency')
  41. LEFT JOIN
  42.     wp_postmeta ORDERS_META3 ON
  43.         (ORDERS_META3.post_id = ORDERS_META1.post_id) AND
  44.         (ORDERS_META3.meta_key = '_base_currency_exchange_rate')
  45. WHERE
  46.   ORDERS.post_type = 'shop_order' AND
  47.   ORDERS.post_status IN ('wc-completed', 'wc-processing')
  48. ORDER BY
  49.   ORDERS.ID
Add Comment
Please, Sign In to add comment