Skip to content
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

Merged
merged 6 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions assets/src/js/dokan-admin-analytics.js

This file was deleted.

77 changes: 64 additions & 13 deletions includes/Analytics/Reports/Orders/Stats/QueryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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' ] );
Expand All @@ -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;
}
Comment on lines +58 to 61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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:

 /**
  * Modifies the admin report columns to include Dokan-specific data.
  *
  * @param array  $column        The existing report columns.
  * @param string $context       The context of the report.
- * @param string $wc_table_name The WooCommerce table name being queried.
+ * @param string $table_name    The WooCommerce table name being queried.
  *
  * @return array Modified report columns.
  */
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function modify_admin_report_columns( array $column, string $context, string $table_name ): array {
if ( $context !== $this->context ) {
return $column;
}
/**
* Modifies the admin report columns to include Dokan-specific data.
*
* @param array $column The existing report columns.
* @param string $context The context of the report.
* @param string $table_name The WooCommerce table name being queried.
*
* @return array Modified report columns.
*/
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure consistent usage of $types and $order_types variables

There is potential confusion between the variables $order_types and $types. In line 71, you use $order_types within the SQL string, but elsewhere $types is used for similar purposes. For consistency and to avoid errors, consider standardizing the variable names or verifying that each variable is correctly used.

Option 1: Update the SQL expression to use $types if that is the intended variable.

- $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 $order_types is intended, ensure its value matches the expected format and usage throughout the code.

Committable suggestion was skipped due to low confidence.


/**
* 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;
}
Expand All @@ -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 );
}
}
8 changes: 0 additions & 8 deletions includes/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ public function enqueue_admin_scripts( $hook ) {
wp_localize_script( 'dokan-admin-product', 'dokan_admin_product', $this->admin_product_localize_scripts() );
}

// Load admin scripts for analytics.
wp_enqueue_script( 'dokan-admin-analytics' );

do_action( 'dokan_enqueue_admin_scripts', $hook );
}

Expand Down Expand Up @@ -557,11 +554,6 @@ public function get_scripts() {
'deps' => [ 'jquery' ],
'version' => filemtime( $asset_path . 'js/dokan-frontend.js' ),
],
'dokan-admin-analytics' => [
'src' => $asset_url . '/js/dokan-admin-analytics.js',
'deps' => [ 'wc-admin-app', 'wp-hooks' ],
'version' => filemtime( $asset_path . 'js/dokan-admin-analytics.js' ),
],
];

return $scripts;
Expand Down
1 change: 0 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const entryPoint = {
],
'helper': './assets/src/js/helper.js',
'dokan-frontend': './assets/src/js/dokan-frontend.js',
'dokan-admin-analytics': './assets/src/js/dokan-admin-analytics.js',

'style': '/assets/src/less/style.less',
'rtl': '/assets/src/less/rtl.less',
Expand Down
Loading