AeliaWCEU_VAT_AssistantTax_Settings_Integration

Implements an integration with WooCommerce Tax Settings to allow additional information to be recorded an maintained.

Defined (1)

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

/src/lib/classes/integration/admin/tax_settings_integration.php  
  1. class Tax_Settings_Integration extends \Aelia\WC\Base_Class { 
  2. /** 
  3. * Returns the tax class corresponding to the tax section currently open. 
  4. * @return string 
  5. */ 
  6. protected static function get_current_tax_class() { 
  7. $current_section = get_value('section', $_REQUEST, ''); 
  8. $tax_classes = array_filter(array_map('trim', explode("\n", get_option('woocommerce_tax_classes')))); 
  9. $current_class = ''; 
  10. foreach($tax_classes as $class) { 
  11. if(sanitize_title($class) == $current_section) { 
  12. $current_class = $class; 
  13. return $current_class; 
  14.  
  15. /** 
  16. * Returns the data of the various tax rates displayed on a Tax Settings page. 
  17. * @return array 
  18. */ 
  19. public static function get_tax_rates_data() { 
  20. global $wpdb; 
  21.  
  22. $page = absint(get_value('p', $_GET, 1)); 
  23. $limit = 100; 
  24. $current_class = self::get_current_tax_class(); 
  25.  
  26. $SQL = " 
  27. SELECT 
  28. TR.tax_rate_id 
  29. , TR.tax_payable_to_country 
  30. FROM 
  31. {$wpdb->prefix}woocommerce_tax_rates TR 
  32. WHERE 
  33. (tax_rate_class = %s) 
  34. LIMIT 
  35. %d, %d; 
  36. "; 
  37.  
  38. $query = $wpdb->prepare( 
  39. $SQL,  
  40. sanitize_title($current_class),  
  41. ($page - 1) * $limit,  
  42. $limit 
  43. ); 
  44.  
  45. $tax_rates = $wpdb->get_results($query, OBJECT_K); 
  46. return $tax_rates; 
  47.  
  48. /** 
  49. * Adds localisation data for the JavaScript that extend the Tax Settings pages. 
  50. * @param array $admin_scripts_params The array of parameters to extend. 
  51. * @return array The array of parameters with additional data. 
  52. */ 
  53. public static function localize_admin_scripts(array $admin_scripts_params) { 
  54. $euva = WC_Aelia_EU_VAT_Assistant::instance(); 
  55. $text_domain = WC_Aelia_EU_VAT_Assistant::$text_domain; 
  56.  
  57. $admin_scripts_params['tax_settings'] = array( 
  58. 'eu_vat_rates' => $euva->get_eu_vat_rates(),  
  59. 'eu_vat_rate_types' => $euva->get_eu_vat_rate_types(),  
  60. 'tax_rates_data' => self::get_tax_rates_data(),  
  61. 'user_interface' => array( 
  62. 'update_eu_vat_rates_button_label' => __('Update EU VAT Rates', $text_domain),  
  63. 'eu_vat_rates_using_text' => __('using', $text_domain),  
  64. 'vat_label' => __('VAT', $text_domain),  
  65. 'vat_updated_message' => __('VAT rates have been updated. Please review them, to ' . 
  66. 'ensure that they are correct, then click on "Save ' . 
  67. 'changes" to save the new rates.', $text_domain),  
  68. 'invalid_vat_rates' => __('An error occurred retrieving the VAT rates, therefore ' . 
  69. 'this function cannot be used at the moment. Please try ' . 
  70. 'again by reloading the page later.', $text_domain),  
  71. 'tax_payable_to_country' => array( 
  72. 'header_label' => __('Tax payable to Country', $text_domain),  
  73. 'header_tooltip' => __('Specify to which country this tax should be paid. Leave empty ' . 
  74. 'to use the country to which the tax is applied (i.e. ' . 
  75. 'the one in the Country Code column).', $text_domain),  
  76. 'field_placeholder' => __('Auto', $text_domain),  
  77. ),  
  78. ); 
  79. return $admin_scripts_params; 
  80.  
  81. /** 
  82. * Sets the hooks required by the class. 
  83. */ 
  84. public static function set_hooks() { 
  85. add_action('woocommerce_settings_save_tax', array(__CLASS__, 'woocommerce_settings_save_tax'), 1); 
  86. add_action('woocommerce_tax_rate_added', array(__CLASS__, 'woocommerce_tax_rate_added'), 10, 2); 
  87. add_action('woocommerce_tax_rate_updated', array(__CLASS__, 'woocommerce_tax_rate_updated'), 10, 2); 
  88.  
  89. /** 
  90. * Fired when the tax settings are saved. 
  91. * Intercepts the new tax rates added by the Admin and associates them with the 
  92. * extra data added by the EU VAT plugin. 
  93. */ 
  94. public static function woocommerce_settings_save_tax() { 
  95. if(empty($_POST['tax_rate_country']) || empty($_POST['tax_rate_country']['new'])) { 
  96. return; 
  97.  
  98. /** Every new row has a unique ID. However, the "after tax rate insert" event 
  99. * doesn't pass such ID, so we use a trick: we append the row ID to the country 
  100. * code, so that we can extract it later. 
  101. */ 
  102. foreach($_POST['tax_rate_country']['new'] as $row_id => $country) { 
  103. $_POST['tax_rate_country']['new'][$row_id] = $country . '_' . $row_id; 
  104.  
  105. // Debug 
  106. //var_dump($_POST['tax_rate_country']['new']);die(); 
  107.  
  108. protected static function get_tax_payable_to_country($default = '') { 
  109. $tax_payable_to_country = $default; 
  110. if(isset($_POST['tax_payable_to_country'])) { 
  111. $tax_payable_to_country = $_POST['tax_payable_to_country']['new'][$row_id]; 
  112. return $tax_payable_to_country; 
  113.  
  114. /** 
  115. * Adds extra information to a newly added tax rate. 
  116. * @param int tax_rate_id The ID of the new tax rate. 
  117. * @param array A list with the details of the tax rate 
  118. */ 
  119. public static function woocommerce_tax_rate_added($tax_rate_id, $tax_rate_info) { 
  120. global $wpdb; 
  121. $country_parts = explode('_', $tax_rate_info['tax_rate_country']); 
  122. $country = array_shift($country_parts); 
  123. $row_id = array_shift($country_parts); 
  124.  
  125. if(!is_numeric($row_id)) { 
  126. return; 
  127.  
  128. // Debug 
  129. //var_dump($country, $row_id, $_POST['tax_payable_to_country']['new'][$row_id]);die(); 
  130.  
  131. // Retrieve the country to which the tax will be payable 
  132. $tax_payable_to_country = self::get_tax_payable_to_country(); 
  133.  
  134. // Update the row 
  135. $tax_info = array( 
  136. 'tax_rate_country' => $country,  
  137. 'tax_payable_to_country' => $tax_payable_to_country,  
  138. ); 
  139.  
  140. $result = $wpdb->update( 
  141. $wpdb->prefix . 'woocommerce_tax_rates',  
  142. $tax_info,  
  143. array( 
  144. 'tax_rate_id' => $tax_rate_id,  
  145. ); 
  146.  
  147. /** 
  148. * Adds extra information to a tax rate that has been updated. 
  149. * @param int tax_rate_id The ID of the new tax rate. 
  150. * @param array A list with the details of the tax rate 
  151. */ 
  152. public static function woocommerce_tax_rate_updated($tax_rate_id, $tax_rate_info) { 
  153. global $wpdb; 
  154. $wpdb->show_errors(); 
  155.  
  156. // Retrieve the country to which the tax will be payable 
  157. $tax_payable_to_country = self::get_tax_payable_to_country(); 
  158.  
  159. // Debug 
  160. //var_dump($tax_rate_id, $tax_rate_info, $_POST['tax_payable_to_country'][$tax_rate_id]);die(); 
  161.  
  162. // Update the row 
  163. $tax_info = array( 
  164. 'tax_payable_to_country' => $tax_payable_to_country,  
  165. ); 
  166.  
  167. $result = $wpdb->update( 
  168. $wpdb->prefix . 'woocommerce_tax_rates',  
  169. $tax_info,  
  170. array( 
  171. 'tax_rate_id' => $tax_rate_id,  
  172. );