|
21 | 21 | use Automattic\WooCommerce\Utilities\OrderUtil; |
22 | 22 | use WCPOS\WooCommercePOS\Services\Cache; |
23 | 23 | use WP_Error; |
| 24 | +use WC_Tax; |
24 | 25 |
|
25 | 26 | use const WCPOS\WooCommercePOS\PLUGIN_NAME; |
26 | 27 |
|
@@ -96,6 +97,7 @@ public function wcpos_dispatch_request( $dispatch_result, WP_REST_Request $reque |
96 | 97 | add_filter( 'woocommerce_order_get_items', array( $this, 'wcpos_order_get_items' ), 10, 3 ); |
97 | 98 | add_action( 'woocommerce_before_order_object_save', array( $this, 'wcpos_before_order_object_save' ), 10, 2 ); |
98 | 99 | add_filter( 'woocommerce_rest_shop_order_object_query', array( $this, 'wcpos_shop_order_query' ), 10, 2 ); |
| 100 | + add_action( 'woocommerce_order_item_fee_after_calculate_taxes', array( $this, 'wcpos_order_item_fee_after_calculate_taxes' ), 10, 2 ); |
99 | 101 |
|
100 | 102 | /** |
101 | 103 | * Check if the request is for all orders and if the 'posts_per_page' is set to -1. |
@@ -325,6 +327,41 @@ public function prepare_line_items( $posted, $action = 'create', $item = null ) |
325 | 327 | return $item; |
326 | 328 | } |
327 | 329 |
|
| 330 | + /** |
| 331 | + * The way WooCommerce handles negative fees is ... weird. |
| 332 | + * They by-pass the normal tax calculation, disregard the tax_status and tax_class, and apply the taxes to the fee line. |
| 333 | + * This is a problem because if people want to apply a negative fee to an order, and set tax_status to 'none', it will give |
| 334 | + * the wrong result. |
| 335 | + * |
| 336 | + * @param WC_Order_Item_Fee $fee_item The fee item. |
| 337 | + * @param array $calculate_tax_for The tax calculation data. |
| 338 | + */ |
| 339 | + public function wcpos_order_item_fee_after_calculate_taxes( $fee_item, $calculate_tax_for ) { |
| 340 | + if ( $fee_item->get_total() < 0 ) { |
| 341 | + // Respect the fee line's tax_class and tax_status. |
| 342 | + $tax_class = $fee_item->get_tax_class(); |
| 343 | + $tax_status = $fee_item->get_tax_status(); |
| 344 | + |
| 345 | + if ( $tax_status === 'taxable' ) { |
| 346 | + // Use the tax_class if set, otherwise default. |
| 347 | + $calculate_tax_for['tax_class'] = $tax_class ? : ''; |
| 348 | + |
| 349 | + // Find rates and calculate taxes for the fee. |
| 350 | + $tax_rates = WC_Tax::find_rates( $calculate_tax_for ); |
| 351 | + $discount_taxes = WC_Tax::calc_tax( $fee_item->get_total(), $tax_rates ); |
| 352 | + |
| 353 | + // Apply calculated taxes to the fee item. |
| 354 | + $fee_item->set_taxes( array( 'total' => $discount_taxes ) ); |
| 355 | + } else { |
| 356 | + // Set taxes to none if tax_status is 'none'. |
| 357 | + $fee_item->set_taxes( false ); |
| 358 | + } |
| 359 | + |
| 360 | + // Save the updated fee item. |
| 361 | + $fee_item->save(); |
| 362 | + } |
| 363 | + } |
| 364 | + |
328 | 365 | /** |
329 | 366 | * Gets the product ID from posted ID. |
330 | 367 | * |
@@ -391,15 +428,15 @@ public function get_collection_params() { |
391 | 428 |
|
392 | 429 | // Add 'pos_cashier' parameter |
393 | 430 | $params['pos_cashier'] = array( |
394 | | - 'description' => __('Filter orders by POS cashier.', 'woocommerce-pos'), |
| 431 | + 'description' => __( 'Filter orders by POS cashier.', 'woocommerce-pos' ), |
395 | 432 | 'type' => 'integer', |
396 | 433 | 'required' => false, |
397 | 434 | ); |
398 | 435 |
|
399 | 436 | // Add 'pos_store' parameter |
400 | 437 | // @NOTE - this is different to 'store_id' which is the store the request was made from. |
401 | 438 | $params['pos_store'] = array( |
402 | | - 'description' => __('Filter orders by POS store.', 'woocommerce-pos'), |
| 439 | + 'description' => __( 'Filter orders by POS store.', 'woocommerce-pos' ), |
403 | 440 | 'type' => 'integer', |
404 | 441 | 'required' => false, |
405 | 442 | ); |
|
0 commit comments