-
Notifications
You must be signed in to change notification settings - Fork 206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix/revenue reports #2423
Fix/revenue reports #2423
Changes from all commits
4a42f29
066e31a
30ec378
893fb2c
b807d32
5c9530e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
namespace WeDevs\Dokan\Analytics\Reports\Orders\Stats; | ||
|
||
use WeDevs\Dokan\Analytics\Reports\Orders\QueryFilter as OrdersQueryFilter; | ||
use WeDevs\Dokan\Analytics\Reports\OrderType; | ||
|
||
/** | ||
* Class QueryFilter | ||
|
@@ -29,8 +30,11 @@ public function register_hooks(): void { | |
add_filter( 'woocommerce_analytics_clauses_join_orders_stats_total', [ $this, 'add_join_subquery' ] ); | ||
add_filter( 'woocommerce_analytics_clauses_join_orders_stats_interval', [ $this, 'add_join_subquery' ] ); | ||
|
||
add_filter( 'woocommerce_analytics_clauses_where_orders_stats_total', [ $this, 'add_where_subquery' ], 30 ); | ||
add_filter( 'woocommerce_analytics_clauses_where_orders_stats_interval', [ $this, 'add_where_subquery' ], 30 ); | ||
// add_filter( 'woocommerce_analytics_clauses_where_orders_stats_total', [ $this, 'add_where_subquery' ], 30 ); | ||
// add_filter( 'woocommerce_analytics_clauses_where_orders_stats_interval', [ $this, 'add_where_subquery' ], 30 ); | ||
|
||
add_filter( 'woocommerce_analytics_clauses_where_orders_stats_total', [ $this, 'add_where_subquery_for_vendor_filter' ], 30 ); | ||
add_filter( 'woocommerce_analytics_clauses_where_orders_stats_interval', [ $this, 'add_where_subquery_for_vendor_filter' ], 30 ); | ||
|
||
add_filter( 'woocommerce_analytics_clauses_select_orders_stats_total', [ $this, 'add_select_subquery_for_total' ] ); | ||
add_filter( 'woocommerce_analytics_clauses_select_orders_stats_interval', [ $this, 'add_select_subquery_for_total' ] ); | ||
|
@@ -51,21 +55,61 @@ public function register_hooks(): void { | |
* | ||
* @return array Modified report columns. | ||
*/ | ||
public function modify_admin_report_columns( array $column, string $context, string $wc_table_name ): array { | ||
public function modify_admin_report_columns( array $column, string $context, string $table_name ): array { | ||
if ( $context !== $this->context ) { | ||
return $column; | ||
} | ||
|
||
$table_name = $this->get_dokan_table(); | ||
$types = $this->get_order_types_for_sql_excluding_refunds(); | ||
|
||
$order_count = "SUM( CASE WHEN {$table_name}.order_type IN($types) THEN 1 ELSE 0 END )"; | ||
|
||
$column['orders_count'] = "{$order_count} as orders_count"; | ||
$column['avg_items_per_order'] = "SUM( {$wc_table_name}.num_items_sold ) / {$order_count} AS avg_items_per_order"; | ||
$column['avg_order_value'] = "SUM( {$wc_table_name}.net_total ) / {$order_count} AS avg_order_value"; | ||
$column['avg_admin_commission'] = "SUM( {$table_name}.admin_commission ) / {$order_count} AS avg_admin_commission"; | ||
$column['avg_vendor_earning'] = "SUM( {$table_name}.vendor_earning ) / {$order_count} AS avg_vendor_earning"; | ||
$dokan_table_name = $this->get_dokan_table(); | ||
$order_types = $this->get_order_types_for_sql_excluding_refunds(); | ||
$types = implode( ',', ( new OrderType() )->get_vendor_order_types() ); | ||
// $types = $this->get_order_types_for_sql_excluding_refunds(); | ||
|
||
$parent_order_types_str = implode( ',', ( new OrderType() )->get_admin_order_types_excluding_refunds() ); | ||
$refund_order_types_str = implode( ',', ( new OrderType() )->get_vendor_refund_types() ); | ||
|
||
$order_count = "SUM( CASE WHEN {$dokan_table_name}.order_type IN($order_types) THEN 1 ELSE 0 END )"; | ||
Comment on lines
+63
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure consistent usage of There is potential confusion between the variables Option 1: Update the SQL expression to use - $order_count = "SUM( CASE WHEN {$dokan_table_name}.order_type IN($order_types) THEN 1 ELSE 0 END )";
+ $order_count = "SUM( CASE WHEN {$dokan_table_name}.order_type IN($types) THEN 1 ELSE 0 END )"; Option 2: If
|
||
|
||
/** | ||
* Override WC column. | ||
* | ||
* We can apply the common where clause after Dokan Coupon Distribution. | ||
* File to restore: @see https://github.com/getdokan/dokan/blob/2cffa360a94b32033e7591fece5950068ab758f5/includes/Analytics/Reports/Orders/Stats/QueryFilter.php#L4 | ||
*/ | ||
$coupon = "SUM(CASE WHEN {$dokan_table_name}.order_type IN($parent_order_types_str) THEN discount_amount END)"; | ||
|
||
$net_total = "SUM(CASE WHEN {$dokan_table_name}.order_type IN($types) THEN {$table_name}.net_total END)"; | ||
|
||
$item_sold = "SUM( CASE WHEN {$dokan_table_name}.order_type IN($types) THEN {$table_name}.num_items_sold END)"; | ||
|
||
$refunds = "ABS( SUM( CASE WHEN {$table_name}.net_total < 0 AND {$dokan_table_name}.order_type IN($refund_order_types_str) THEN {$table_name}.net_total ELSE 0 END ) )"; | ||
|
||
$gross_sales = | ||
"( SUM( CASE WHEN {$dokan_table_name}.order_type IN($types) THEN {$table_name}.total_sales END )" . | ||
" + COALESCE( $coupon, 0 )" . // SUM() all nulls gives null. | ||
" - SUM(CASE WHEN {$dokan_table_name}.order_type IN($types) THEN {$table_name}.tax_total END)" . | ||
" - SUM(CASE WHEN {$dokan_table_name}.order_type IN($types) THEN {$table_name}.shipping_total END)" . | ||
" + {$refunds}" . | ||
' ) as gross_sales'; | ||
|
||
$column['num_items_sold'] = "$item_sold as num_items_sold"; | ||
$column['gross_sales'] = $gross_sales; | ||
$column['total_sales'] = "SUM( CASE WHEN {$dokan_table_name}.order_type IN($types) THEN {$table_name}.total_sales END) AS total_sales"; | ||
$column['coupons'] = "COALESCE( $coupon, 0 ) AS coupons"; // SUM() all nulls gives null; | ||
$column['coupons_count'] = 'COALESCE( coupons_count, 0 ) as coupons_count'; | ||
$column['refunds'] = "{$refunds} AS refunds"; | ||
$column['taxes'] = "SUM(CASE WHEN {$dokan_table_name}.order_type IN($types) THEN {$table_name}.tax_total END) AS taxes"; | ||
$column['shipping'] = "SUM(CASE WHEN {$dokan_table_name}.order_type IN($types) THEN {$table_name}.shipping_total END) AS shipping"; | ||
$column['net_revenue'] = " $net_total AS net_revenue"; | ||
$column['total_customers'] = "COUNT( DISTINCT( {$table_name}.customer_id ) ) as total_customers"; | ||
// End of override | ||
|
||
$column['orders_count'] = "{$order_count} as orders_count"; | ||
|
||
$column['avg_items_per_order'] = "{$item_sold} / {$order_count} AS avg_items_per_order"; | ||
$column['avg_order_value'] = "{$net_total} / {$order_count} AS avg_order_value"; | ||
$column['avg_admin_commission'] = "SUM( CASE WHEN {$dokan_table_name}.order_type IN($types) THEN {$dokan_table_name}.admin_commission END) / {$order_count} AS avg_admin_commission"; | ||
$column['avg_vendor_earning'] = "SUM( CASE WHEN {$dokan_table_name}.order_type IN($types) THEN {$dokan_table_name}.vendor_earning END) / {$order_count} AS avg_vendor_earning"; | ||
|
||
return $column; | ||
} | ||
|
@@ -87,4 +131,11 @@ public function add_select_subquery_for_total( $clauses ) { | |
|
||
return $clauses; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function add_where_subquery_for_vendor_filter( array $clauses ): array { | ||
return parent::add_where_subquery_for_vendor_filter( $clauses ); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the docblock parameter name to match the method signature
The parameter in the method docblock is still named
$wc_table_name
, but it has been renamed to$table_name
in the method signature. Update the docblock to reflect this change for consistency and clarity.Apply this diff to update the docblock:
📝 Committable suggestion