AeliaWCEU_VAT_AssistantReportsWC22EU_VAT_By_Country_Report

Renders the report containing the EU VAT for each country in a specific period.

Defined (1)

The class is defined in the following location(s).

/src/lib/classes/reporting/reports/WC22/eu_vat_by_country_report.php  
  1. class EU_VAT_By_Country_Report extends \Aelia\WC\EU_VAT_Assistant\Reports\Base_EU_VAT_By_Country_Report { 
  2.  
  3. /** 
  4. * Merges the VAT refunds with the tax data. 
  5. * @param array tax_data The tax data produced by EU_VAT_By_Country_Report::get_tax_data(). 
  6. * @return array The tax data, including the refunds. 
  7. * @see \Aelia\WC\EU_VAT_Assistant\Base_EU_VAT_By_Country_Report::get_tax_data() 
  8. */ 
  9. protected function get_tax_refunds_data($tax_data) { 
  10. global $wpdb; 
  11. $px = $wpdb->prefix; 
  12.  
  13. if(get_arr_value(Definitions::ARG_REFUNDS_PERIOD, $_REQUEST) === Definitions::REFUNDS_IN_PERIOD) { 
  14. $refunds_date_range_where = " 
  15. -- Gather refunds granted in the selected period. This option is useful 
  16. -- to prepare domestic VAT returns. 
  17. (REFUNDS.post_date >= '" . date('Y-m-d', $this->start_date) . "') AND 
  18. (REFUNDS.post_date < '" . date('Y-m-d', strtotime('+1 DAY', $this->end_date)) . "') 
  19. "; 
  20. else { 
  21. $refunds_date_range_where = " 
  22. -- Gather refunds applied to orders placed within the quarter even if 
  23. -- those refunds were granted in a later quarter. 
  24. -- 
  25. -- As per official MOSS instructions: 
  26. -- 7.10 How are credit notes dealt with? 
  27. -- If you raise a credit note in a subsequent calendar quarter you must 
  28. -- amend the original MOSS VAT return. The exchange rate applicable will 
  29. -- be the European Central Bank rate applicable on the last day of the 
  30. -- calendar quarter in which the supply to which the credit note relates 
  31. -- was made. 
  32. -- 
  33. -- Original document: http://www.revenue.ie/en/tax/vat/leaflets/mini-one-stop-shop.html 
  34. (ORDERS.post_date >= '" . date('Y-m-d', $this->start_date) . "') AND 
  35. (ORDERS.post_date < '" . date('Y-m-d', strtotime('+1 DAY', $this->end_date)) . "') 
  36. "; 
  37.  
  38. $SQL = sprintf(" 
  39. SELECT 
  40. REFUNDS.ID 
  41. , REFUNDS.post_date 
  42. , OM.meta_value AS order_vat_data 
  43. -- , meta__eu_vat_data.meta_value 
  44. -- Refund items 
  45. , RI.order_item_id AS refund_item_id 
  46. , RI.order_item_name AS refund_item_name 
  47. , RI.order_item_type AS refund_item_type 
  48. -- Item/shipping tax refund data 
  49. , RIM2.meta_key AS tax_refund_data_type 
  50. , RIM2.meta_value AS tax_refund_data 
  51. -- Item/shipping price refund data 
  52. , RIM3.meta_key AS price_refund_data_type 
  53. , RIM3.meta_value AS price_refund_data 
  54. FROM 
  55. {$px}posts AS REFUNDS 
  56. JOIN 
  57. {$px}posts AS ORDERS ON 
  58. (ORDERS.ID = REFUNDS.post_parent) 
  59. JOIN 
  60. -- Order Meta 
  61. {$px}postmeta AS OM ON 
  62. (OM.post_id = ORDERS.ID) AND 
  63. (OM.meta_key = '_eu_vat_data') 
  64. JOIN 
  65. -- Refund items 
  66. {$px}woocommerce_order_items RI ON 
  67. (RI.order_id = REFUNDS.ID) AND 
  68. (RI.order_item_type IN ('line_item', 'shipping')) 
  69. JOIN 
  70. -- Refund items meta - Find refund items 
  71. {$px}woocommerce_order_itemmeta RIM1 ON 
  72. (RIM1.order_item_id = RI.order_item_id) AND 
  73. (RIM1.meta_key = '_refunded_item_id') AND 
  74. (RIM1.meta_value > 0) 
  75. JOIN 
  76. -- Refund items meta - Find item/shipping tax refund data 
  77. {$px}woocommerce_order_itemmeta RIM2 ON 
  78. (RIM2.order_item_id = RI.order_item_id) AND 
  79. (RIM2.meta_key IN ('_line_tax_data', 'taxes')) 
  80. LEFT JOIN 
  81. -- Refund items meta - Find item/shipping price refund data 
  82. {$px}woocommerce_order_itemmeta RIM3 ON 
  83. (RIM3.order_item_id = RI.order_item_id) AND 
  84. (RIM3.meta_key IN ('cost', '_line_total')) 
  85. WHERE 
  86. (REFUNDS.post_type IN ('shop_order_refund')) AND 
  87. -- The statuses to include always refer to the original orders. Refunds 
  88. -- are always in status 'wc-completed' 
  89. (ORDERS.post_status IN ('%s')) AND 
  90. " . $refunds_date_range_where,  
  91. implode("', '", $this->order_statuses_to_include(true))); 
  92. // Debug 
  93. //var_dump($SQL); 
  94.  
  95. $dataset = $wpdb->get_results($SQL); 
  96.  
  97. // Debug 
  98. //var_dump("REFUNDS RESULT", $dataset); 
  99.  
  100. // Initialise totals 
  101. foreach($tax_data as $tax_id => $tax_details) { 
  102. $tax_data[$tax_id]->refunded_items_tax_amount = 0; 
  103. $tax_data[$tax_id]->refunded_shipping_tax_amount = 0; 
  104. $tax_data[$tax_id]->refunded_items_total = 0; 
  105. $tax_data[$tax_id]->refunded_shipping_total = 0; 
  106.  
  107. // Debug 
  108. //var_dump($dataset); 
  109.  
  110. foreach($dataset as $data) { 
  111. $order_vat_data = maybe_unserialize($data->order_vat_data); 
  112. $vat_currency_exchange_rate = $order_vat_data['vat_currency_exchange_rate']; 
  113.  
  114. $refund_data = maybe_unserialize($data->tax_refund_data); 
  115.  
  116. switch($data->refund_item_type) { 
  117. case 'shipping': 
  118. $tax_refund_type = 'refunded_shipping_tax_amount'; 
  119. $item_total_to_update = 'refunded_shipping_total'; 
  120. break; 
  121. default: 
  122. $tax_refund_data = $refund_data['total']; 
  123. $tax_refund_type = 'refunded_items_tax_amount'; 
  124. $item_total_to_update = 'refunded_items_total'; 
  125. break; 
  126.  
  127. // Update the totals for each tax ID 
  128. foreach($tax_refund_data as $tax_id => $tax_amount) { 
  129. if(isset($tax_data[$tax_id])) { 
  130. $vat_rate = $tax_data[$tax_id]->tax_rate_data->tax_rate; 
  131. $tax_data[$tax_id]->$tax_refund_type += wc_round_tax_total($tax_amount * $vat_currency_exchange_rate); 
  132. $tax_data[$tax_id]->$item_total_to_update += ($data->price_refund_data * $vat_currency_exchange_rate); 
  133. return $tax_data;