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

Sales reporting fixes and improvements #2859

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .changelogs/fix_date-range-widget-1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
significance: patch
type: fixed
links:
- "#2860"
- "#2861"
entry: Net Sales reporting includes partially refunded transactions, and
transactions from orders regardless of status.
6 changes: 6 additions & 0 deletions .changelogs/fix_date-range-widget-2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
significance: patch
type: fixed
links:
- "#2860"
entry: On-hold, pending cancellation, cancelled and expired orders now included
in "# of Sales" widget.
3 changes: 3 additions & 0 deletions .changelogs/fix_date-range-widget-3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
significance: patch
type: added
entry: New "# of transactions" sales reporting widget.
6 changes: 6 additions & 0 deletions .changelogs/fix_date-range-widget.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
significance: minor
type: fixed
links:
- "#2858"
entry: Fixes sales reporting for transactions or orders that happened between
23:23:59 and midnight.
24 changes: 10 additions & 14 deletions includes/abstracts/abstract.llms.analytics.widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,18 @@ protected function get_posted_dates() {

$dates = llms_filter_input_sanitize_string( INPUT_POST, 'dates', array( FILTER_REQUIRE_ARRAY ) );
return $dates ? $dates : '';

}

protected function get_posted_courses() {

$courses = llms_filter_input( INPUT_POST, 'courses', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY );
return $courses ? $courses : array();

}

protected function get_posted_memberships() {

$memberships = llms_filter_input( INPUT_POST, 'memberships', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY );
return $memberships ? $memberships : array();

}

protected function get_posted_posts() {
Expand All @@ -140,7 +137,6 @@ protected function get_posted_posts() {
protected function get_posted_students() {
$students = llms_filter_input( INPUT_POST, 'students', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY );
return $students ? $students : array();

}

protected function get_prepared_query() {
Expand Down Expand Up @@ -169,20 +165,26 @@ protected function format_date( $date, $type ) {
break;

case 'end':
$date .= ' 23:23:59';
/**
* Return 00:00:00 on the next day after this date, using PHP datetime functions to avoid issues with daylight savings time or leap years.
*
* 23:59:59 is not a safe way to capture the end of day in newer versions of MySQL, if the transaction happened at (say) 23:59:59.999.
*/
$end_date = new DateTime( $date );
$end_date->modify( '+1 day' );

$date = $end_date->format( 'Y-m-d' ) . ' 00:00:00';

break;

}

return $date;

}

protected function is_error() {

return ( $this->success ) ? false : true;

}

protected function set_order_data_query( $args = array() ) {
Expand Down Expand Up @@ -235,7 +237,7 @@ protected function set_order_data_query( $args = array() ) {
$order_dates = '';
if ( $date_range ) {
$dates = $this->get_posted_dates();
$order_dates = "AND orders.{$date_field} BETWEEN CAST( %s AS DATETIME ) AND CAST( %s AS DATETIME )";
$order_dates = "AND orders.{$date_field} >= %s AND orders.{$date_field} < %s";
$this->query_vars[] = $this->format_date( $dates['start'], 'start' );
$this->query_vars[] = $this->format_date( $dates['end'], 'end' );
}
Expand Down Expand Up @@ -291,7 +293,6 @@ protected function set_order_data_query( $args = array() ) {
{$wheres_clause}
{$order_clause}
;";

}

/**
Expand Down Expand Up @@ -333,7 +334,6 @@ protected function query() {
$this->message = $wpdb->last_error;

}

}

/**
Expand Down Expand Up @@ -384,7 +384,6 @@ public function can_be_processed() {
$widget_name,
$this
);

}

/**
Expand All @@ -409,8 +408,5 @@ public function output() {
header( 'Content-Type: application/json' );
echo wp_json_encode( $this );
wp_die();

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public function __construct() {

add_action( 'llms_reporting_after_nav', array( $this, 'output_filters' ), 10, 1 );
add_action( 'llms_reporting_content_sales', array( $this, 'output' ) );

}

public static function get_filter_data() {
Expand All @@ -49,7 +48,6 @@ public static function get_filter_data() {
$data['date_end'] = $data['dates']['end'];

return $data;

}

/**
Expand All @@ -68,7 +66,7 @@ public function get_widget_data() {
'title' => __( '# of Sales', 'lifterlms' ),
'cols' => '1-4',
'content' => __( 'loading...', 'lifterlms' ),
'info' => __( 'Number of new active or completed orders placed within this period', 'lifterlms' ),
'info' => __( 'Number of new non-refunded orders placed within this period', 'lifterlms' ),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Possibly sanitize translation string

),
'sold' => array(
'title' => __( 'Net Sales', 'lifterlms' ),
Expand All @@ -90,24 +88,24 @@ public function get_widget_data() {
),
),
array(
// 'revenue' => array(
// 'title' => __( 'Grosse Revenue', 'lifterlms' ),
// 'cols' => '1-4',
// 'content' => __( 'loading...', 'lifterlms' ),
// 'info' => __( 'Total of all transactions minus all refunds processed during this period', 'lifterlms' ),
// ),
'coupons' => array(
'coupons' => array(
'title' => __( '# of Coupons Used', 'lifterlms' ),
'cols' => '1-4',
'content' => __( 'loading...', 'lifterlms' ),
'info' => __( 'Number of orders completed using coupons during this period', 'lifterlms' ),
),
'discounts' => array(
'discounts' => array(
'title' => __( 'Amount of Coupons', 'lifterlms' ),
'cols' => '1-4',
'content' => __( 'loading...', 'lifterlms' ),
'info' => __( 'Total amount of coupons used during this period', 'lifterlms' ),
),
'transactions' => array(
'title' => __( '# of Transactions', 'lifterlms' ),
'cols' => '1-4',
'content' => __( 'loading...', 'lifterlms' ),
'info' => __( 'Number of transactions within this period', 'lifterlms' ),
),
),
)
);
Expand All @@ -129,7 +127,6 @@ public function output() {
'widget_data' => $this->get_widget_data(),
)
);

}

/**
Expand All @@ -146,8 +143,6 @@ public function output_filters( $tab ) {
llms_get_template( 'admin/reporting/nav-filters.php', self::get_filter_data() );

}

}

}
return new LLMS_Admin_Reporting_Tab_Sales();
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public function set_query() {
upm.meta_key = '_is_complete'
AND p.post_type = 'course'
AND upm.meta_value = 'yes'
AND upm.updated_date BETWEEN CAST( %s AS DATETIME ) AND CAST( %s AS DATETIME )
AND upm.updated_date >= %s
AND upm.updated_date < %s
{$student_ids}
{$lesson_ids}
;";
Expand All @@ -69,7 +70,6 @@ public function set_query() {
$this->format_date( $dates['start'], 'start' ),
$this->format_date( $dates['end'], 'end' ),
);

}

protected function format_response() {
Expand All @@ -79,7 +79,5 @@ protected function format_response() {
return count( $this->get_results() );

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public function set_query() {
WHERE
meta_key = '_status'
AND ( meta_value = 'Enrolled' OR meta_value = 'enrolled' )
AND updated_date BETWEEN CAST( %s AS DATETIME ) AND CAST( %s AS DATETIME )
AND updated_date >= %s
AND updated_date < %s
{$student_ids}
{$product_ids}
;";
Expand All @@ -68,7 +69,6 @@ public function set_query() {
$this->format_date( $dates['start'], 'start' ),
$this->format_date( $dates['end'], 'end' ),
);

}

protected function format_response() {
Expand All @@ -78,7 +78,5 @@ protected function format_response() {
return count( $this->get_results() );

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ private function get_lesson_ids( $products ) {
}

return $lessons;

}

public function set_query() {
Expand Down Expand Up @@ -90,7 +89,8 @@ public function set_query() {
upm.meta_key = '_is_complete'
AND p.post_type = 'lesson'
AND upm.meta_value = 'yes'
AND upm.updated_date BETWEEN CAST( %s AS DATETIME ) AND CAST( %s AS DATETIME )
AND upm.updated_date >= %s
AND upm.updated_date < %s
{$student_ids}
{$lesson_ids}
;";
Expand All @@ -99,7 +99,6 @@ public function set_query() {
$this->format_date( $dates['start'], 'start' ),
$this->format_date( $dates['end'], 'end' ),
);

}

protected function format_response() {
Expand All @@ -109,7 +108,5 @@ protected function format_response() {
return count( $this->get_results() );

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ public function set_query() {
WHERE
( txns.post_status = 'llms-txn-succeeded' OR txns.post_status = 'llms-txn-refunded' )
AND txns.post_type = 'llms_transaction'
AND txns.post_date BETWEEN CAST( %s AS DATETIME ) AND CAST( %s AS DATETIME )
AND txns.post_date >= %s
AND txns.post_date < %s
AND refund.meta_key = '_llms_refund_amount'
{$txn_meta_where}
ORDER BY txns.post_modified ASC
;";

}

/**
Expand All @@ -117,7 +117,5 @@ protected function format_response() {
return llms_price_raw( floatval( is_array( $results ) ? array_sum( wp_list_pluck( $results, 'amount' ) ) : $results ) );

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public function set_query() {
$this->query = "SELECT user_registered AS date
FROM {$wpdb->users}
WHERE
user_registered BETWEEN CAST( %s AS DATETIME ) AND CAST( %s AS DATETIME )
user_registered >= %s
AND user_registered < %s
{$student_ids}
;";

$this->query_vars = array(
$this->format_date( $dates['start'], 'start' ),
$this->format_date( $dates['end'], 'end' ),
);

}

protected function format_response() {
Expand All @@ -66,7 +66,5 @@ protected function format_response() {
return count( $this->get_results() );

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ public function set_query() {
WHERE
( txns.post_status = 'llms-txn-succeeded' OR txns.post_status = 'llms-txn-refunded' )
AND txns.post_type = 'llms_transaction'
AND txns.post_date BETWEEN CAST( %s AS DATETIME ) AND CAST( %s AS DATETIME )
AND txns.post_date >= %s
AND txns.post_date < %s
{$txn_meta_where}
;";

}

protected function format_response() {
Expand All @@ -104,7 +104,5 @@ protected function format_response() {
return llms_price_raw( floatval( $this->get_results() ) );

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ public function set_query() {
'statuses' => array(
'llms-active',
'llms-completed',
'llms-on-hold',
'llms-pending-cancel',
'llms-cancelled',
'llms-expired',
),
)
);

}

protected function format_response() {
Expand All @@ -59,7 +62,5 @@ protected function format_response() {
return count( $this->get_results() );

}

}

}
Loading
Loading