Split tax across items for discounts

This commit is contained in:
Mike Jolley 2017-08-11 17:53:49 +01:00
parent d61a97cb57
commit b826d02f9b
3 changed files with 54 additions and 27 deletions

View File

@ -1214,25 +1214,6 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
return array_unique( $found_tax_classes );
}
/**
* Get count of all tax classes for items in the order.
*
* @since 3.2.0
* @param array $item_types Type of items to count.
* @return array
*/
public function get_tax_class_counts_for_items( $item_types = array( 'line_item' ) ) {
$tax_classes = array_fill_keys( $this->get_items_tax_classes(), 0 );
foreach ( $this->get_items( $item_types ) as $item ) {
if ( 'taxable' === $item->get_tax_status() ) {
$tax_classes[ $item->get_tax_class() ] += $item->get_quantity();
}
}
return $tax_classes;
}
/**
* Get tax location for this order.
*

View File

@ -28,6 +28,37 @@ class WC_Order_Item_Discount extends WC_Order_Item {
),
);
/**
* Get item costs grouped by tax class.
*
* @since 3.2.0
* @param WC_Order $order Order object.
* @return array
*/
protected function get_tax_class_costs( $order ) {
$tax_classes = array_fill_keys( $order->get_items_tax_classes(), 0 );
$tax_classes['non-taxable'] = 0;
foreach ( $order->get_items( array( 'line_item', 'fee' ) ) as $item ) {
if ( 'taxable' === $item->get_tax_status() ) {
$tax_classes[ $item->get_tax_class() ] += $item->get_total();
} else {
$tax_classes['non-taxable'] += $item->get_total();
}
}
foreach ( $order->get_items( array( 'shipping' ) ) as $item ) {
if ( 'taxable' === $item->get_tax_status() ) {
$class = 'inherit' === $item->get_tax_class() ? current( $order->get_items_tax_classes() ): $item->get_tax_class();
$tax_classes[ $class ] += $item->get_total();
} else {
$tax_classes['non-taxable'] += $item->get_total();
}
}
return $tax_classes;
}
/**
* Calculate item taxes.
*
@ -41,15 +72,20 @@ class WC_Order_Item_Discount extends WC_Order_Item {
}
if ( wc_tax_enabled() && ( $order = $this->get_order() ) ) {
// Apportion taxes to order items, shipping, and fees.
$order = $this->get_order();
$tax_class_counts = $order->get_tax_class_counts_for_items( array( 'line_item', 'fee', 'shipping' ) );
$item_count = $order->get_item_count( array( 'line_item', 'fee', 'shipping' ) );
$discount_taxes = array();
$order = $this->get_order();
$tax_class_costs = $this->get_tax_class_costs( $order );
$total_costs = array_sum( $tax_class_costs );
$discount_taxes = array();
foreach ( $tax_class_counts as $tax_class => $tax_class_count ) {
$proportion = $tax_class_count / $item_count;
$cart_discount_proportion = $this->get_total() * $proportion;
$discount_taxes = wc_array_merge_recursive_numeric( $discount_taxes, WC_Tax::calc_tax( $cart_discount_proportion, WC_Tax::get_rates( $tax_class ) ) );
if ( $total_costs ) {
foreach ( $tax_class_costs as $tax_class => $tax_class_cost ) {
if ( 'non-taxable' === $tax_class ) {
continue;
}
$proportion = $tax_class_cost / $total_costs;
$cart_discount_proportion = $this->get_total() * $proportion;
$discount_taxes = wc_array_merge_recursive_numeric( $discount_taxes, WC_Tax::calc_tax( $cart_discount_proportion, WC_Tax::get_rates( $tax_class ) ) );
}
}
$this->set_taxes( array( 'total' => $discount_taxes ) );

View File

@ -221,6 +221,16 @@ class WC_Order_Item_Shipping extends WC_Order_Item {
return $this->get_prop( 'taxes', $context );
}
/**
* Get tax class.
*
* @param string $context
* @return string
*/
public function get_tax_class( $context = 'view' ) {
return get_option( 'woocommerce_shipping_tax_class' );
}
/*
|--------------------------------------------------------------------------
| Array Access Methods