From 44604bcd02a263179e99d5c5d18bb4df9f6a2dd8 Mon Sep 17 00:00:00 2001 From: Joshua T Flowers Date: Wed, 20 Feb 2019 09:39:42 +0800 Subject: [PATCH] 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 --- ...-admin-reports-orders-stats-data-store.php | 19 ++++++++++++++++++- .../class-wc-tests-reports-orders-stats.php | 16 ++++++++-------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-orders-stats-data-store.php b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-orders-stats-data-store.php index 9918fd952bc..7b7a5ba4433 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-orders-stats-data-store.php +++ b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-orders-stats-data-store.php @@ -401,7 +401,7 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto 'refund_total' => $order->get_total_refunded(), 'tax_total' => $order->get_total_tax(), '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 ), '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; } + /** + * 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 * diff --git a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders-stats.php b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders-stats.php index 5ab3ab3bbc0..e976becb698 100644 --- a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders-stats.php +++ b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders-stats.php @@ -58,13 +58,13 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { 'orders_count' => 1, 'num_items_sold' => 4, 'avg_items_per_order' => 4, - 'avg_order_value' => 68, + 'avg_order_value' => 56, 'gross_revenue' => 97, 'coupons' => 20, 'refunds' => 12, 'taxes' => 7, 'shipping' => 10, - 'net_revenue' => 68, + 'net_revenue' => 56, 'num_returning_customers' => 0, 'num_new_customers' => 1, 'products' => 1, @@ -79,7 +79,7 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { 'date_end_gmt' => $end_time, 'subtotals' => array( 'gross_revenue' => 97, - 'net_revenue' => 68, + 'net_revenue' => 56, 'coupons' => 20, 'shipping' => 10, 'taxes' => 7, @@ -87,7 +87,7 @@ class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case { 'orders_count' => 1, 'num_items_sold' => 4, 'avg_items_per_order' => 4, - 'avg_order_value' => 68, + 'avg_order_value' => 56, 'num_returning_customers' => 0, 'num_new_customers' => 1, '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 ); $expected_stats = array( 'totals' => array( - 'net_revenue' => 68, - 'avg_order_value' => 68, + 'net_revenue' => 56, + 'avg_order_value' => 56, 'orders_count' => 1, 'avg_items_per_order' => 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_gmt' => $end_time, 'subtotals' => array( - 'net_revenue' => 68, - 'avg_order_value' => 68, + 'net_revenue' => 56, + 'avg_order_value' => 56, 'orders_count' => 1, 'avg_items_per_order' => 4, 'num_items_sold' => 4,