@@ -769,11 +769,12 @@ public function addOrdersCount()
769769 */
770770 public function addRevenueToSelect ($ convertCurrency = false )
771771 {
772- $ expr = $ this ->getTotalsExpression (
772+ $ expr = $ this ->getTotalsExpressionWithDiscountRefunded (
773773 !$ convertCurrency ,
774774 $ this ->getConnection ()->getIfNullSql ('main_table.base_subtotal_refunded ' , 0 ),
775775 $ this ->getConnection ()->getIfNullSql ('main_table.base_subtotal_canceled ' , 0 ),
776- $ this ->getConnection ()->getIfNullSql ('main_table.base_discount_canceled ' , 0 )
776+ $ this ->getConnection ()->getIfNullSql ('ABS(main_table.base_discount_refunded) ' , 0 ),
777+ $ this ->getConnection ()->getIfNullSql ('ABS(main_table.base_discount_canceled) ' , 0 )
777778 );
778779 $ this ->getSelect ()->columns (['revenue ' => $ expr ]);
779780
@@ -791,11 +792,12 @@ public function addSumAvgTotals($storeId = 0)
791792 /**
792793 * calculate average and total amount
793794 */
794- $ expr = $ this ->getTotalsExpression (
795+ $ expr = $ this ->getTotalsExpressionWithDiscountRefunded (
795796 $ storeId ,
796797 $ this ->getConnection ()->getIfNullSql ('main_table.base_subtotal_refunded ' , 0 ),
797798 $ this ->getConnection ()->getIfNullSql ('main_table.base_subtotal_canceled ' , 0 ),
798- $ this ->getConnection ()->getIfNullSql ('main_table.base_discount_canceled ' , 0 )
799+ $ this ->getConnection ()->getIfNullSql ('ABS(main_table.base_discount_refunded) ' , 0 ),
800+ $ this ->getConnection ()->getIfNullSql ('ABS(main_table.base_discount_canceled) ' , 0 )
799801 );
800802
801803 $ this ->getSelect ()->columns (
@@ -808,13 +810,15 @@ public function addSumAvgTotals($storeId = 0)
808810 }
809811
810812 /**
811- * Get SQL expression for totals
813+ * Get SQL expression for totals.
812814 *
813815 * @param int $storeId
814816 * @param string $baseSubtotalRefunded
815817 * @param string $baseSubtotalCanceled
816818 * @param string $baseDiscountCanceled
817819 * @return string
820+ * @deprecated
821+ * @see getTotalsExpressionWithDiscountRefunded
818822 */
819823 protected function getTotalsExpression (
820824 $ storeId ,
@@ -825,10 +829,40 @@ protected function getTotalsExpression(
825829 $ template = ($ storeId != 0 )
826830 ? '(main_table.base_subtotal - %2$s - %1$s - ABS(main_table.base_discount_amount) - %3$s) '
827831 : '((main_table.base_subtotal - %1$s - %2$s - ABS(main_table.base_discount_amount) + %3$s) '
828- . ' * main_table.base_to_global_rate) ' ;
832+ . ' * main_table.base_to_global_rate) ' ;
829833 return sprintf ($ template , $ baseSubtotalRefunded , $ baseSubtotalCanceled , $ baseDiscountCanceled );
830834 }
831835
836+ /**
837+ * Get SQL expression for totals with discount refunded.
838+ *
839+ * @param int $storeId
840+ * @param string $baseSubtotalRefunded
841+ * @param string $baseSubtotalCanceled
842+ * @param string $baseDiscountRefunded
843+ * @param string $baseDiscountCanceled
844+ * @return string
845+ */
846+ private function getTotalsExpressionWithDiscountRefunded (
847+ $ storeId ,
848+ $ baseSubtotalRefunded ,
849+ $ baseSubtotalCanceled ,
850+ $ baseDiscountRefunded ,
851+ $ baseDiscountCanceled
852+ ) {
853+ $ template = ($ storeId != 0 )
854+ ? '(main_table.base_subtotal - %2$s - %1$s - (ABS(main_table.base_discount_amount) - %3$s - %4$s)) '
855+ : '((main_table.base_subtotal - %1$s - %2$s - (ABS(main_table.base_discount_amount) - %3$s - %4$s)) '
856+ . ' * main_table.base_to_global_rate) ' ;
857+ return sprintf (
858+ $ template ,
859+ $ baseSubtotalRefunded ,
860+ $ baseSubtotalCanceled ,
861+ $ baseDiscountRefunded ,
862+ $ baseDiscountCanceled
863+ );
864+ }
865+
832866 /**
833867 * Sort order by total amount
834868 *
0 commit comments