Reduce net order total by refunded amount less tax and shipping refunds (https://github.com/woocommerce/woocommerce-admin/pull/1600)

* Reduce net order total by refunded amount less tax and shipping refunds

* Fix net_totals in tests with refunds
This commit is contained in:
Joshua T Flowers 2019-02-20 09:39:42 +08:00 committed by GitHub
parent 4f64358c8e
commit 44604bcd02
2 changed files with 26 additions and 9 deletions

View File

@ -401,7 +401,7 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
'refund_total' => $order->get_total_refunded(), 'refund_total' => $order->get_total_refunded(),
'tax_total' => $order->get_total_tax(), 'tax_total' => $order->get_total_tax(),
'shipping_total' => $order->get_shipping_total(), 'shipping_total' => $order->get_shipping_total(),
'net_total' => (float) $order->get_total() - (float) $order->get_total_tax() - (float) $order->get_shipping_total(), 'net_total' => self::get_net_total( $order ),
'returning_customer' => self::is_returning_customer( $order ), 'returning_customer' => self::is_returning_customer( $order ),
'status' => self::normalize_order_status( $order->get_status() ), 'status' => self::normalize_order_status( $order->get_status() ),
); );
@ -507,6 +507,23 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
return $num_items; return $num_items;
} }
/**
* Get the net amount from an order without shipping, tax, or refunds.
*
* @param array $order WC_Order object.
* @return float
*/
protected static function get_net_total( $order ) {
$net_total = $order->get_total() - $order->get_total_tax() - $order->get_shipping_total();
$refunds = $order->get_refunds();
foreach ( $refunds as $refund ) {
$net_total += $refund->get_total() - $refund->get_total_tax() - $refund->get_shipping_total();
}
return $net_total > 0 ? (float) $net_total : 0;
}
/** /**
* Check to see if an order's customer has made previous orders or not * Check to see if an order's customer has made previous orders or not
* *

View File

@ -58,13 +58,13 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
'orders_count' => 1, 'orders_count' => 1,
'num_items_sold' => 4, 'num_items_sold' => 4,
'avg_items_per_order' => 4, 'avg_items_per_order' => 4,
'avg_order_value' => 68, 'avg_order_value' => 56,
'gross_revenue' => 97, 'gross_revenue' => 97,
'coupons' => 20, 'coupons' => 20,
'refunds' => 12, 'refunds' => 12,
'taxes' => 7, 'taxes' => 7,
'shipping' => 10, 'shipping' => 10,
'net_revenue' => 68, 'net_revenue' => 56,
'num_returning_customers' => 0, 'num_returning_customers' => 0,
'num_new_customers' => 1, 'num_new_customers' => 1,
'products' => 1, 'products' => 1,
@ -79,7 +79,7 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
'date_end_gmt' => $end_time, 'date_end_gmt' => $end_time,
'subtotals' => array( 'subtotals' => array(
'gross_revenue' => 97, 'gross_revenue' => 97,
'net_revenue' => 68, 'net_revenue' => 56,
'coupons' => 20, 'coupons' => 20,
'shipping' => 10, 'shipping' => 10,
'taxes' => 7, 'taxes' => 7,
@ -87,7 +87,7 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
'orders_count' => 1, 'orders_count' => 1,
'num_items_sold' => 4, 'num_items_sold' => 4,
'avg_items_per_order' => 4, 'avg_items_per_order' => 4,
'avg_order_value' => 68, 'avg_order_value' => 56,
'num_returning_customers' => 0, 'num_returning_customers' => 0,
'num_new_customers' => 1, 'num_new_customers' => 1,
'segments' => array(), 'segments' => array(),
@ -106,8 +106,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
$query = new WC_Admin_Reports_Orders_Stats_Query( $args ); $query = new WC_Admin_Reports_Orders_Stats_Query( $args );
$expected_stats = array( $expected_stats = array(
'totals' => array( 'totals' => array(
'net_revenue' => 68, 'net_revenue' => 56,
'avg_order_value' => 68, 'avg_order_value' => 56,
'orders_count' => 1, 'orders_count' => 1,
'avg_items_per_order' => 4, 'avg_items_per_order' => 4,
'num_items_sold' => 4, 'num_items_sold' => 4,
@ -125,8 +125,8 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
'date_end' => $end_time, 'date_end' => $end_time,
'date_end_gmt' => $end_time, 'date_end_gmt' => $end_time,
'subtotals' => array( 'subtotals' => array(
'net_revenue' => 68, 'net_revenue' => 56,
'avg_order_value' => 68, 'avg_order_value' => 56,
'orders_count' => 1, 'orders_count' => 1,
'avg_items_per_order' => 4, 'avg_items_per_order' => 4,
'num_items_sold' => 4, 'num_items_sold' => 4,