diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php b/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php index 7f71084007f..6aaf9b92e56 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php +++ b/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php @@ -46,6 +46,10 @@ class WC_Admin_Api_Init { // Common date time code. require_once dirname( __FILE__ ) . '/class-wc-admin-reports-interval.php'; + // Segmentation. + require_once dirname( __FILE__ ) . '/class-wc-admin-reports-segmenting.php'; + require_once dirname( __FILE__ ) . '/class-wc-admin-reports-orders-stats-segmenting.php'; + // Query classes for reports. require_once dirname( __FILE__ ) . '/class-wc-admin-reports-revenue-query.php'; require_once dirname( __FILE__ ) . '/class-wc-admin-reports-orders-query.php'; diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-orders-stats-segmenting.php b/plugins/woocommerce-admin/includes/class-wc-admin-reports-orders-stats-segmenting.php new file mode 100644 index 00000000000..26ad9a3400f --- /dev/null +++ b/plugins/woocommerce-admin/includes/class-wc-admin-reports-orders-stats-segmenting.php @@ -0,0 +1,442 @@ + "SUM($products_table.product_qty) as num_items_sold", + 'gross_revenue' => "SUM($products_table.product_gross_revenue) AS gross_revenue", + 'coupons' => "SUM($products_table.coupon_amount) AS coupons", + 'refunds' => "SUM($products_table.refund_amount) AS refunds", + 'taxes' => "SUM($products_table.tax_amount) AS taxes", + 'shipping' => "SUM($products_table.shipping_amount) AS shipping", + // TODO: product_net_revenue should already have refunds subtracted, so it should not be here. Pls check. + 'net_revenue' => "SUM($products_table.product_net_revenue) AS net_revenue", + ); + + return $this->prepare_selections( $columns_mapping ); + } + + /** + * Returns SELECT clause statements to be used for order-related product-level segmenting query (e.g. avg items per order when segmented by category). + * + * @param string $unique_orders_table Name of SQL table containing the order-level segmenting info. + * + * @return string SELECT clause statements. + */ + protected function get_segment_selections_order_level( $unique_orders_table ) { + $columns_mapping = array( + 'orders_count' => "COUNT($unique_orders_table.order_id) AS orders_count", + 'avg_items_per_order' => "AVG($unique_orders_table.num_items_sold) AS avg_items_per_order", + 'avg_order_value' => "(SUM($unique_orders_table.net_total) - SUM($unique_orders_table.refund_total))/COUNT($unique_orders_table.order_id) AS avg_order_value", + 'num_returning_customers' => "SUM($unique_orders_table.returning_customer) AS num_returning_customers", + 'num_new_customers' => "COUNT($unique_orders_table.returning_customer) - SUM($unique_orders_table.returning_customer) AS num_new_customers", + ); + + return $this->prepare_selections( $columns_mapping ); + } + + /** + * Returns SELECT clause statements to be used for order-level segmenting query (e.g. avg items per order or net revenue when segmented by coupons). + * + * @param string $order_stats_table Name of SQL table containing the order-level info. + * @param array $overrides Array of overrides for default column calculations. + * + * @return string + */ + protected function segment_selections_orders( $order_stats_table, $overrides = array() ) { + $columns_mapping = array( + 'num_items_sold' => "SUM($order_stats_table.num_items_sold) as num_items_sold", + 'gross_revenue' => "SUM($order_stats_table.gross_total) AS gross_revenue", + 'coupons' => "SUM($order_stats_table.coupon_total) AS coupons", + 'refunds' => "SUM($order_stats_table.refund_total) AS refunds", + 'taxes' => "SUM($order_stats_table.tax_total) AS taxes", + 'shipping' => "SUM($order_stats_table.shipping_total) AS shipping", + 'net_revenue' => "SUM($order_stats_table.net_total) - SUM($order_stats_table.refund_total) AS net_revenue", + 'orders_count' => "COUNT($order_stats_table.order_id) AS orders_count", + 'avg_items_per_order' => "AVG($order_stats_table.num_items_sold) AS avg_items_per_order", + 'avg_order_value' => "(SUM($order_stats_table.net_total) - SUM($order_stats_table.refund_total))/COUNT($order_stats_table.order_id) AS avg_order_value", + 'num_returning_customers' => "SUM($order_stats_table.returning_customer) AS num_returning_customers", + 'num_new_customers' => "COUNT($order_stats_table.returning_customer) - SUM($order_stats_table.returning_customer) AS num_new_customers", + ); + + if ( $overrides ) { + $columns_mapping = array_merge( $columns_mapping, $overrides ); + } + + return $this->prepare_selections( $columns_mapping ); + } + + /** + * Calculate segments for totals where the segmenting property is bound to product (e.g. category, product_id, variation_id). + * + * @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'. + * @param string $segmenting_from FROM part of segmenting SQL query. + * @param string $segmenting_where WHERE part of segmenting SQL query. + * @param string $segmenting_groupby GROUP BY part of segmenting SQL query. + * @param string $segmenting_dimension_name Name of the segmenting dimension. + * @param string $table_name Name of SQL table which is the stats table for orders. + * @param array $totals_query Array of SQL clauses for totals query. + * @param string $unique_orders_table Name of temporary SQL table that holds unique orders. + * + * @return array + */ + protected function get_product_related_totals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $totals_query, $unique_orders_table ) { + global $wpdb; + + $product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup'; + + // Can't get all the numbers from one query, so split it into one query for product-level numbers and one for order-level numbers (which first need to have orders uniqued). + // Product-level numbers. + $segments_products = $wpdb->get_results( + "SELECT + $segmenting_groupby AS $segmenting_dimension_name + {$segmenting_selections['product_level']} + FROM + $table_name + $segmenting_from + {$totals_query['from_clause']} + WHERE + 1=1 + {$totals_query['where_time_clause']} + {$totals_query['where_clause']} + $segmenting_where + GROUP BY + $segmenting_groupby", + ARRAY_A + ); // WPCS: cache ok, DB call ok, unprepared SQL ok. + + // Order level numbers. + // As there can be 2 same product ids (or variation ids) per one order, the orders first need to be uniqued before calculating averages, customer counts, etc. + $segments_orders = $wpdb->get_results( + "SELECT + $unique_orders_table.$segmenting_dimension_name AS $segmenting_dimension_name + {$segmenting_selections['order_level']} + FROM + ( + SELECT + $table_name.order_id, + $segmenting_groupby AS $segmenting_dimension_name, + MAX( num_items_sold ) AS num_items_sold, + MAX( net_total ) as net_total, + MAX( refund_total ) as refund_total, + MAX( returning_customer ) AS returning_customer + FROM + $table_name + $segmenting_from + {$totals_query['from_clause']} + WHERE + 1=1 + {$totals_query['where_time_clause']} + {$totals_query['where_clause']} + $segmenting_where + GROUP BY + $product_segmenting_table.order_id, $segmenting_groupby + ) AS $unique_orders_table + GROUP BY + $unique_orders_table.$segmenting_dimension_name", + ARRAY_A + ); // WPCS: cache ok, DB call ok, unprepared SQL ok. + + $totals_segments = $this->merge_segment_totals_results( $segmenting_dimension_name, $segments_products, $segments_orders ); + return $totals_segments; + } + + /** + * Calculate segments for intervals where the segmenting property is bound to product (e.g. category, product_id, variation_id). + * + * @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'. + * @param string $segmenting_from FROM part of segmenting SQL query. + * @param string $segmenting_where WHERE part of segmenting SQL query. + * @param string $segmenting_groupby GROUP BY part of segmenting SQL query. + * @param string $segmenting_dimension_name Name of the segmenting dimension. + * @param string $table_name Name of SQL table which is the stats table for orders. + * @param array $intervals_query Array of SQL clauses for intervals query. + * @param string $unique_orders_table Name of temporary SQL table that holds unique orders. + * + * @return array + */ + protected function get_product_related_intervals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $intervals_query, $unique_orders_table ) { + global $wpdb; + + $product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup'; + + // LIMIT offset, rowcount needs to be updated to LIMIT offset, rowcount * max number of segments. + $limit_parts = explode( ',', $intervals_query['limit'] ); + $orig_rowcount = intval( $limit_parts[1] ); + $segmenting_limit = $limit_parts[0] . ',' . $orig_rowcount * count( $this->get_all_segments() ); + + // Can't get all the numbers from one query, so split it into one query for product-level numbers and one for order-level numbers (which first need to have orders uniqued). + // Product-level numbers. + $segments_products = $wpdb->get_results( + "SELECT + {$intervals_query['select_clause']} AS time_interval, + $segmenting_groupby AS $segmenting_dimension_name + {$segmenting_selections['product_level']} + FROM + $table_name + $segmenting_from + {$intervals_query['from_clause']} + WHERE + 1=1 + {$intervals_query['where_time_clause']} + {$intervals_query['where_clause']} + $segmenting_where + GROUP BY + time_interval, $segmenting_groupby + $segmenting_limit", + ARRAY_A + ); // WPCS: cache ok, DB call ok, unprepared SQL ok. + + // Order level numbers. + // As there can be 2 same product ids (or variation ids) per one order, the orders first need to be uniqued before calculating averages, customer counts, etc. + $segments_orders = $wpdb->get_results( + "SELECT + $unique_orders_table.time_interval AS time_interval, + $unique_orders_table.$segmenting_dimension_name AS $segmenting_dimension_name + {$segmenting_selections['order_level']} + FROM + ( + SELECT + MAX( $table_name.date_created ) AS datetime_anchor, + {$intervals_query['select_clause']} AS time_interval, + $table_name.order_id, + $segmenting_groupby AS $segmenting_dimension_name, + MAX( num_items_sold ) AS num_items_sold, + MAX( net_total ) as net_total, + MAX( refund_total ) as refund_total, + MAX( returning_customer ) AS returning_customer + FROM + $table_name + $segmenting_from + {$intervals_query['from_clause']} + WHERE + 1=1 + {$intervals_query['where_time_clause']} + {$intervals_query['where_clause']} + $segmenting_where + GROUP BY + time_interval, $product_segmenting_table.order_id, $segmenting_groupby + ) AS $unique_orders_table + GROUP BY + time_interval, $unique_orders_table.$segmenting_dimension_name + $segmenting_limit", + ARRAY_A + ); // WPCS: cache ok, DB call ok, unprepared SQL ok. + + $intervals_segments = $this->merge_segment_intervals_results( $segmenting_dimension_name, $segments_products, $segments_orders ); + return $intervals_segments; + } + + /** + * Calculate segments for totals query where the segmenting property is bound to order (e.g. coupon or customer type). + * + * @param string $segmenting_select SELECT part of segmenting SQL query. + * @param string $segmenting_from FROM part of segmenting SQL query. + * @param string $segmenting_where WHERE part of segmenting SQL query. + * @param string $segmenting_groupby GROUP BY part of segmenting SQL query. + * @param string $table_name Name of SQL table which is the stats table for orders. + * @param array $totals_query Array of SQL clauses for intervals query. + * + * @return array + */ + protected function get_order_related_totals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $totals_query ) { + global $wpdb; + + $totals_segments = $wpdb->get_results( + "SELECT + $segmenting_groupby + $segmenting_select + FROM + $table_name + $segmenting_from + {$totals_query['from_clause']} + WHERE + 1=1 + {$totals_query['where_time_clause']} + {$totals_query['where_clause']} + $segmenting_where + GROUP BY + $segmenting_groupby", + ARRAY_A + ); // WPCS: cache ok, DB call ok, unprepared SQL ok. + + // Reformat result. + $totals_segments = $this->reformat_totals_segments( $totals_segments, $segmenting_groupby ); + return $totals_segments; + } + + /** + * Calculate segments for intervals query where the segmenting property is bound to order (e.g. coupon or customer type). + * + * @param string $segmenting_select SELECT part of segmenting SQL query. + * @param string $segmenting_from FROM part of segmenting SQL query. + * @param string $segmenting_where WHERE part of segmenting SQL query. + * @param string $segmenting_groupby GROUP BY part of segmenting SQL query. + * @param string $table_name Name of SQL table which is the stats table for orders. + * @param array $intervals_query Array of SQL clauses for intervals query. + * + * @return array + */ + protected function get_order_related_intervals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $intervals_query ) { + global $wpdb; + $limit_parts = explode( ',', $intervals_query['limit'] ); + $orig_rowcount = intval( $limit_parts[1] ); + $segmenting_limit = $limit_parts[0] . ',' . $orig_rowcount * count( $this->get_all_segments() ); + + $intervals_segments = $wpdb->get_results( + "SELECT + MAX($table_name.date_created) AS datetime_anchor, + {$intervals_query['select_clause']} AS time_interval, + $segmenting_groupby + $segmenting_select + FROM + $table_name + $segmenting_from + {$intervals_query['from_clause']} + WHERE + 1=1 + {$intervals_query['where_time_clause']} + {$intervals_query['where_clause']} + $segmenting_where + GROUP BY + time_interval, $segmenting_groupby + $segmenting_limit", + ARRAY_A + ); // WPCS: cache ok, DB call ok, unprepared SQL ok. + + // Reformat result. + $intervals_segments = $this->reformat_intervals_segments( $intervals_segments, $segmenting_groupby ); + return $intervals_segments; + } + + /** + * Return array of segments formatted for REST response. + * + * @param string $type Type of segments to return--'totals' or 'intervals'. + * @param array $query_params SQL query parameter array. + * @param string $table_name Name of main SQL table for the data store (used as basis for JOINS). + * + * @return array + * @throws WC_REST_Exception In case of segmenting by variations, when no parent product is specified. + */ + protected function get_segments( $type, $query_params, $table_name ) { + global $wpdb; + if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) { + return array(); + } + + $product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup'; + $unique_orders_table = 'uniq_orders'; + $segmenting_where = ''; + + // Product, variation, and category are bound to product, so here product segmenting table is required, + // while coupon and customer are bound to order, so we don't need the extra JOIN for those. + // This also means that segment selections need to be calculated differently. + if ( 'product' === $this->query_args['segmentby'] ) { + // TODO: how to handle shipping taxes when grouped by product? + $segmenting_selections = array( + 'product_level' => $this->get_segment_selections_product_level( $product_segmenting_table ), + 'order_level' => $this->get_segment_selections_order_level( $unique_orders_table ), + ); + $segmenting_from = "INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)"; + $segmenting_groupby = $product_segmenting_table . '.product_id'; + $segmenting_dimension_name = 'product_id'; + + $segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table ); + } elseif ( 'variation' === $this->query_args['segmentby'] ) { + if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) { + throw new WC_REST_Exception( 'woocommerce_rest_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'wc-admin' ), 400 ); + } + + $segmenting_selections = array( + 'product_level' => $this->get_segment_selections_product_level( $product_segmenting_table ), + 'order_level' => $this->get_segment_selections_order_level( $unique_orders_table ), + ); + $segmenting_from = "INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)"; + $segmenting_where = "AND $product_segmenting_table.product_id = {$this->query_args['product_includes'][0]}"; + $segmenting_groupby = $product_segmenting_table . '.variation_id'; + $segmenting_dimension_name = 'variation_id'; + + $segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table ); + } elseif ( 'category' === $this->query_args['segmentby'] ) { + $segmenting_selections = array( + 'product_level' => $this->get_segment_selections_product_level( $product_segmenting_table ), + 'order_level' => $this->get_segment_selections_order_level( $unique_orders_table ), + ); + $segmenting_from = " + INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id) + LEFT JOIN {$wpdb->prefix}term_relationships ON {$product_segmenting_table}.product_id = {$wpdb->prefix}term_relationships.object_id + RIGHT JOIN {$wpdb->prefix}term_taxonomy ON {$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}term_taxonomy.term_taxonomy_id + "; + $segmenting_where = " AND taxonomy = 'product_cat'"; + $segmenting_groupby = 'wp_term_taxonomy.term_taxonomy_id'; + $segmenting_dimension_name = 'category_id'; + + $segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table ); + } elseif ( 'coupon' === $this->query_args['segmentby'] ) { + // As there can be 2 or more coupons applied per one order, coupon amount needs to be split. + $coupon_override = array( + 'coupons' => 'SUM(coupon_lookup.discount_amount) AS coupons', + ); + $segmenting_selections = $this->segment_selections_orders( $table_name, $coupon_override ); + $segmenting_from = " + INNER JOIN {$wpdb->prefix}wc_order_coupon_lookup AS coupon_lookup ON ($table_name.order_id = coupon_lookup.order_id) + "; + $segmenting_groupby = 'coupon_lookup.coupon_id'; + + $segments = $this->get_order_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params ); + } elseif ( 'customer_type' === $this->query_args['segmentby'] ) { + $segmenting_selections = $this->segment_selections_orders( $table_name ); + $segmenting_from = ''; + $segmenting_groupby = "$table_name.returning_customer"; + + $segments = $this->get_order_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params ); + } + + return $segments; + } + + /** + * Returns an array of segments for totals part of REST response. + * + * @param array $query_params Totals SQL query parameters. + * @param string $table_name Name of the SQL table that is the main order stats table. + * + * @return array + */ + public function get_totals_segments( $query_params, $table_name ) { + $segments = $this->get_segments( 'totals', $query_params, $table_name ); + return $this->fill_in_missing_segments( $segments ); + } + + /** + * Adds an array of segments to data->intervals object. + * + * @param stdClass $data Data object representing the REST response. + * @param array $intervals_query Intervals SQL query parameters. + * @param string $table_name Name of the SQL table that is the main order stats table. + */ + public function add_intervals_segments( &$data, $intervals_query, $table_name ) { + $intervals_segments = $this->get_segments( 'intervals', $intervals_query, $table_name ); + $this->assign_segments_to_intervals( $data->intervals, $intervals_segments ); + $this->fill_in_missing_interval_segments( $data ); + } +} diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-segmenting.php b/plugins/woocommerce-admin/includes/class-wc-admin-reports-segmenting.php new file mode 100644 index 00000000000..96424eeb16e --- /dev/null +++ b/plugins/woocommerce-admin/includes/class-wc-admin-reports-segmenting.php @@ -0,0 +1,481 @@ +query_args = $query_args; + $this->report_columns = $report_columns; + } + + /** + * Filters definitions for SELECT clauses based on query_args and joins them into one string usable in SELECT clause. + * + * @param array $columns_mapping Column name -> SQL statememt mapping. + * + * @return string to be used in SELECT clause statements. + */ + protected function prepare_selections( $columns_mapping ) { + if ( isset( $this->query_args['fields'] ) && is_array( $this->query_args['fields'] ) ) { + $keep = array(); + foreach ( $this->query_args['fields'] as $field ) { + if ( isset( $columns_mapping[ $field ] ) ) { + $keep[ $field ] = $columns_mapping[ $field ]; + } + } + $selections = implode( ', ', $keep ); + } else { + $selections = implode( ', ', $columns_mapping ); + } + + if ( $selections ) { + $selections = ',' . $selections; + } + + return $selections; + } + + /** + * Update row-level db result for segments in 'totals' section to the format used for output. + * + * @param array $segments_db_result Results from the SQL db query for segmenting. + * @param string $segment_dimension Name of column used for grouping the result. + * + * @return array Reformatted array. + */ + protected function reformat_totals_segments( $segments_db_result, $segment_dimension ) { + $segment_result = array(); + + if ( strpos( $segment_dimension, '.' ) ) { + $segment_dimension = substr( strstr( $segment_dimension, '.' ), 1 ); + } + + foreach ( $segments_db_result as $segment_data ) { + $segment_id = $segment_data[ $segment_dimension ]; + unset( $segment_data[ $segment_dimension ] ); + $segment_datum = array( + 'segment_id' => $segment_id, + 'subtotals' => $segment_data, + ); + $segment_result[ $segment_id ] = $segment_datum; + } + + return $segment_result; + } + + /** + * Merges segmented results for totals response part. + * + * E.g. $r1 = array( + * 0 => array( + * 'product_id' => 3, + * 'net_amount' => 15, + * ), + * ); + * $r2 = array( + * 0 => array( + * 'product_id' => 3, + * 'avg_order_value' => 25, + * ), + * ); + * + * $merged = array( + * 3 => array( + * 'segment_id' => 3, + * 'subtotals' => array( + * 'net_amount' => 15, + * 'avg_order_value' => 25, + * ) + * ), + * ); + * + * @param string $segment_dimension Name of the segment dimension=key in the result arrays used to match records from result sets. + * @param array $result1 Array 1 of segmented figures. + * @param array $result2 Array 2 of segmented figures. + * + * @return array + */ + protected function merge_segment_totals_results( $segment_dimension, $result1, $result2 ) { + $result_segments = array(); + + foreach ( $result1 as $segment_data ) { + $segment_id = $segment_data[ $segment_dimension ]; + unset( $segment_data[ $segment_dimension ] ); + $result_segments[ $segment_id ] = array( + 'segment_id' => $segment_id, + 'subtotals' => $segment_data, + ); + } + + foreach ( $result2 as $segment_data ) { + $segment_id = $segment_data[ $segment_dimension ]; + unset( $segment_data[ $segment_dimension ] ); + if ( ! isset( $result_segments[ $segment_id ] ) ) { + $result_segments[ $segment_id ] = array( + 'segment_id' => $segment_id, + 'subtotals' => array(), + ); + } + $result_segments[ $segment_id ]['subtotals'] = array_merge( $result_segments[ $segment_id ]['subtotals'], $segment_data ); + } + return $result_segments; + } + /** + * Merges segmented results for intervals response part. + * + * E.g. $r1 = array( + * 0 => array( + * 'product_id' => 3, + * 'time_interval' => '2018-12' + * 'net_amount' => 15, + * ), + * ); + * $r2 = array( + * 0 => array( + * 'product_id' => 3, + * 'time_interval' => '2018-12' + * 'avg_order_value' => 25, + * ), + * ); + * + * $merged = array( + * '2018-12' => array( + * 'segments' => array( + * 3 => array( + * 'segment_id' => 3, + * 'subtotals' => array( + * 'net_amount' => 15, + * 'avg_order_value' => 25, + * ), + * ), + * ), + * ), + * ); + * + * @param string $segment_dimension Name of the segment dimension=key in the result arrays used to match records from result sets. + * @param array $result1 Array 1 of segmented figures. + * @param array $result2 Array 2 of segmented figures. + * + * @return array + */ + protected function merge_segment_intervals_results( $segment_dimension, $result1, $result2 ) { + $result_segments = array(); + + foreach ( $result1 as $segment_data ) { + $time_interval = $segment_data['time_interval']; + if ( ! isset( $result_segments[ $time_interval ] ) ) { + $result_segments[ $time_interval ] = array(); + $result_segments[ $time_interval ]['segments'] = array(); + } + unset( $segment_data['time_interval'] ); + unset( $segment_data['datetime_anchor'] ); + $segment_id = $segment_data[ $segment_dimension ]; + unset( $segment_data[ $segment_dimension ] ); + $segment_datum = array( + 'segment_id' => $segment_id, + 'subtotals' => $segment_data, + ); + $result_segments[ $time_interval ]['segments'][ $segment_id ] = $segment_datum; + } + + foreach ( $result2 as $segment_data ) { + $time_interval = $segment_data['time_interval']; + if ( ! isset( $result_segments[ $time_interval ] ) ) { + $result_segments[ $time_interval ] = array(); + $result_segments[ $time_interval ]['segments'] = array(); + } + unset( $segment_data['time_interval'] ); + unset( $segment_data['datetime_anchor'] ); + $segment_id = $segment_data[ $segment_dimension ]; + unset( $segment_data[ $segment_dimension ] ); + + if ( ! isset( $result_segments[ $time_interval ]['segments'][ $segment_id ] ) ) { + $result_segments[ $time_interval ]['segments'][ $segment_id ] = array( + 'segment_id' => $segment_id, + 'subtotals' => array(), + ); + } + $result_segments[ $time_interval ]['segments'][ $segment_id ]['subtotals'] = array_merge( $result_segments[ $time_interval ]['segments'][ $segment_id ]['subtotals'], $segment_data ); + } + return $result_segments; + } + + /** + * Update row-level db result for segments in 'intervals' section to the format used for output. + * + * @param array $segments_db_result Results from the SQL db query for segmenting. + * @param string $segment_dimension Name of column used for grouping the result. + * + * @return array Reformatted array. + */ + protected function reformat_intervals_segments( $segments_db_result, $segment_dimension ) { + $aggregated_segment_result = array(); + + if ( strpos( $segment_dimension, '.' ) ) { + $segment_dimension = substr( strstr( $segment_dimension, '.' ), 1 ); + } + + foreach ( $segments_db_result as $segment_data ) { + $time_interval = $segment_data['time_interval']; + if ( ! isset( $aggregated_segment_result[ $time_interval ] ) ) { + $aggregated_segment_result[ $time_interval ] = array(); + $aggregated_segment_result[ $time_interval ]['segments'] = array(); + } + unset( $segment_data['time_interval'] ); + unset( $segment_data['datetime_anchor'] ); + $segment_id = $segment_data[ $segment_dimension ]; + unset( $segment_data[ $segment_dimension ] ); + $segment_datum = array( + 'segment_id' => $segment_id, + 'subtotals' => $segment_data, + ); + $aggregated_segment_result[ $time_interval ]['segments'][ $segment_id ] = $segment_datum; + } + + return $aggregated_segment_result; + } + + /** + * Fetches all segment ids from db and stores it for later use. + * + * @return array + */ + protected function set_all_segments() { + global $wpdb; + + if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) { + $this->all_segment_ids = array(); + } + + if ( 'product' === $this->query_args['segmentby'] ) { + $segments = wc_get_products( + array( + 'return' => 'ids', + 'limit' => -1, + ) + ); + } elseif ( 'variation' === $this->query_args['segmentby'] ) { + // TODO: assuming that this will only be used for one product, check assumption. + if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) { + return array(); + } + + $segments = wc_get_products( + array( + 'return' => 'ids', + 'limit' => - 1, + 'type' => 'variation', + 'parent' => $this->query_args['product_includes'][0], + ) + ); + } elseif ( 'category' === $this->query_args['segmentby'] ) { + $categories = get_categories( + array( + 'taxonomy' => 'product_cat', + ) + ); + $segments = wp_list_pluck( $categories, 'cat_ID' ); + } elseif ( 'coupon' === $this->query_args['segmentby'] ) { + // TODO: switch to a non-direct-SQL way to get all coupons? + $coupon_ids = $wpdb->get_results( "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type='shop_coupon' AND post_status='publish'", ARRAY_A ); // WPCS: cache ok, DB call ok, unprepared SQL ok. + $segments = wp_list_pluck( $coupon_ids, 'ID' ); + } elseif ( 'customer_type' === $this->query_args['segmentby'] ) { + // 0 -- new customer + // 1 -- returning customer + $segments = array( 0, 1 ); + } else { + // Catch all default. + $segments = array(); + } + + $this->all_segment_ids = $segments; + } + + /** + * Return all segment ids for given segmentby query parameter. + * + * @return array + */ + protected function get_all_segments() { + if ( ! is_array( $this->all_segment_ids ) ) { + $this->set_all_segments(); + } + + return $this->all_segment_ids; + } + + /** + * Compares two report data objects by pre-defined object property and ASC/DESC ordering. + * + * @param stdClass $a Object a. + * @param stdClass $b Object b. + * @return string + */ + private function segment_cmp( $a, $b ) { + if ( $a['segment_id'] === $b['segment_id'] ) { + return 0; + } elseif ( $a['segment_id'] > $b['segment_id'] ) { + return 1; + } elseif ( $a['segment_id'] < $b['segment_id'] ) { + return - 1; + } + } + + /** + * Adds zeroes for segments not present in the data selection. + * + * @param array $segments Array of segments from the database for given data points. + * + * @return array + */ + protected function fill_in_missing_segments( $segments ) { + + $segment_subtotals = array(); + if ( isset( $this->query_args['fields'] ) && is_array( $this->query_args['fields'] ) ) { + foreach ( $this->query_args['fields'] as $field ) { + if ( isset( $this->report_columns[ $field ] ) ) { + $segment_subtotals[ $field ] = 0; + } + } + } else { + foreach ( $this->report_columns as $field => $sql_clause ) { + $segment_subtotals[ $field ] = 0; + } + } + if ( ! is_array( $segments ) ) { + $segments = array(); + } + $all_segment_ids = $this->get_all_segments(); + foreach ( $all_segment_ids as $segment_id ) { + if ( ! isset( $segments[ $segment_id ] ) ) { + $segments[ $segment_id ] = array( + 'segment_id' => $segment_id, + 'subtotals' => $segment_subtotals, + ); + } + } + + // Using array_values to remove custom keys, so that it gets later converted to JSON as an array. + $segments_no_keys = array_values( $segments ); + usort( $segments_no_keys, array( $this, 'segment_cmp' ) ); + return $segments_no_keys; + } + + /** + * Adds missing segments to intervals, modifies $data. + * + * @param stdClass $data Response data. + */ + protected function fill_in_missing_interval_segments( &$data ) { + foreach ( $data->intervals as $order_id => $interval_data ) { + $data->intervals[ $order_id ]['segments'] = $this->fill_in_missing_segments( $data->intervals[ $order_id ]['segments'] ); + } + } + + /** + * Calculate segments for segmenting property bound to product (e.g. category, product_id, variation_id). + * + * @param string $type Type of segments to return--'totals' or 'intervals'. + * @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'. + * @param string $segmenting_from FROM part of segmenting SQL query. + * @param string $segmenting_where WHERE part of segmenting SQL query. + * @param string $segmenting_groupby GROUP BY part of segmenting SQL query. + * @param string $segmenting_dimension_name Name of the segmenting dimension. + * @param string $table_name Name of SQL table which is the stats table for orders. + * @param array $query_params Array of SQL clauses for intervals/totals query. + * @param string $unique_orders_table Name of temporary SQL table that holds unique orders. + * + * @return array + */ + protected function get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table ) { + if ( 'totals' === $type ) { + return $this->get_product_related_totals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table ); + } elseif ( 'intervals' === $type ) { + return $this->get_product_related_intervals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table ); + } + } + + /** + * Calculate segments for segmenting property bound to order (e.g. coupon or customer type). + * + * @param string $type Type of segments to return--'totals' or 'intervals'. + * @param string $segmenting_select SELECT part of segmenting SQL query. + * @param string $segmenting_from FROM part of segmenting SQL query. + * @param string $segmenting_where WHERE part of segmenting SQL query. + * @param string $segmenting_groupby GROUP BY part of segmenting SQL query. + * @param string $table_name Name of SQL table which is the stats table for orders. + * @param array $query_params Array of SQL clauses for intervals/totals query. + * + * @return array + */ + protected function get_order_related_segments( $type, $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params ) { + if ( 'totals' === $type ) { + return $this->get_order_related_totals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params ); + } elseif ( 'intervals' === $type ) { + return $this->get_order_related_intervals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params ); + } + } + + /** + * Assign segments to time intervals by updating original $intervals array. + * + * @param array $intervals Result array from intervals SQL query. + * @param array $intervals_segments Result array from interval segments SQL query. + */ + protected function assign_segments_to_intervals( &$intervals, $intervals_segments ) { + $old_keys = array_keys( $intervals ); + foreach ( $intervals as $interval ) { + $intervals[ $interval['time_interval'] ] = $interval; + $intervals[ $interval['time_interval'] ]['segments'] = array(); + } + foreach ( $old_keys as $key ) { + unset( $intervals[ $key ] ); + } + + foreach ( $intervals_segments as $time_interval => $segment ) { + if ( ! isset( $intervals[ $time_interval ] ) ) { + $intervals[ $time_interval ]['segments'] = array(); + } + $intervals[ $time_interval ]['segments'] = $segment['segments']; + } + + // To remove time interval keys (so that REST response is formatted correctly). + $intervals = array_values( $intervals ); + } +} 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 32e5ef62561..054c72468a9 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 @@ -196,824 +196,6 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto } } - /** - * Returns SELECT clause statements to be used for product-related product-level segmenting query (e.g. products sold, revenue from product X when segmenting by category). - * - * @param array $query_args Query arguments supplied by the user. - * @param string $products_table Name of SQL table containing the product-level segmenting info. - * - * @return string SELECT clause statements. - */ - protected function get_segment_selections_product_level( $query_args, $products_table ) { - $columns_mapping = array( - 'num_items_sold' => "SUM($products_table.product_qty) as num_items_sold", - 'gross_revenue' => "SUM($products_table.product_gross_revenue) AS gross_revenue", - 'coupons' => "SUM($products_table.coupon_amount) AS coupons", - 'refunds' => "SUM($products_table.refund_amount) AS refunds", - 'taxes' => "SUM($products_table.tax_amount) AS taxes", - 'shipping' => "SUM($products_table.shipping_amount) AS shipping", - // TODO: product_net_revenue should already have refunds subtracted, so it should not be here. Pls check. - 'net_revenue' => "SUM($products_table.product_net_revenue) AS net_revenue", - ); - - return $this->prepare_selections( $query_args, $columns_mapping ); - } - - /** - * Returns SELECT clause statements to be used for order-related product-level segmenting query (e.g. avg items per order when segmented by category). - * - * @param array $query_args Query arguments supplied by the user. - * @param string $unique_orders_table Name of SQL table containing the order-level segmenting info. - * - * @return string SELECT clause statements. - */ - protected function get_segment_selections_order_level( $query_args, $unique_orders_table ) { - $columns_mapping = array( - 'orders_count' => "COUNT($unique_orders_table.order_id) AS orders_count", - 'avg_items_per_order' => "AVG($unique_orders_table.num_items_sold) AS avg_items_per_order", - 'avg_order_value' => "(SUM($unique_orders_table.net_total) - SUM($unique_orders_table.refund_total))/COUNT($unique_orders_table.order_id) AS avg_order_value", - 'num_returning_customers' => "SUM($unique_orders_table.returning_customer) AS num_returning_customers", - 'num_new_customers' => "COUNT($unique_orders_table.returning_customer) - SUM($unique_orders_table.returning_customer) AS num_new_customers", - ); - - return $this->prepare_selections( $query_args, $columns_mapping ); - } - - /** - * Filters definitions for SELECT clauses based on query_args and joins them into one string usable in SELECT clause. - * - * @param array $query_args Query arguments supplied by the user. - * @param array $columns_mapping Column name -> SQL statememt mapping. - * - * @return string to be used in SELECT clause statements. - */ - protected function prepare_selections( $query_args, $columns_mapping ) { - if ( isset( $query_args['fields'] ) && is_array( $query_args['fields'] ) ) { - $keep = array(); - foreach ( $query_args['fields'] as $field ) { - if ( isset( $columns_mapping[ $field ] ) ) { - $keep[ $field ] = $columns_mapping[ $field ]; - } - } - $selections = implode( ', ', $keep ); - } else { - $selections = implode( ', ', $columns_mapping ); - } - - if ( $selections ) { - $selections = ',' . $selections; - } - - return $selections; - } - - /** - * Returns SELECT clause statements to be used for order-level segmenting query (e.g. avg items per order or net revenue when segmented by coupons). - * - * @param array $query_args Query arguments supplied by the user. - * @param string $table_name Name of SQL table containing the order-level info. - * @param array $overrides Array of overrides for default column calculations. - * - * @return string - */ - protected function segment_selections_orders( $query_args, $table_name, $overrides = array() ) { - $columns_mapping = array( - 'num_items_sold' => "SUM($table_name.num_items_sold) as num_items_sold", - 'gross_revenue' => "SUM($table_name.gross_total) AS gross_revenue", - 'coupons' => "SUM($table_name.coupon_total) AS coupons", - 'refunds' => "SUM($table_name.refund_total) AS refunds", - 'taxes' => "SUM($table_name.tax_total) AS taxes", - 'shipping' => "SUM($table_name.shipping_total) AS shipping", - 'net_revenue' => "SUM($table_name.net_total) - SUM($table_name.refund_total) AS net_revenue", - 'orders_count' => "COUNT($table_name.order_id) AS orders_count", - 'avg_items_per_order' => "AVG($table_name.num_items_sold) AS avg_items_per_order", - 'avg_order_value' => "(SUM($table_name.net_total) - SUM($table_name.refund_total))/COUNT($table_name.order_id) AS avg_order_value", - 'num_returning_customers' => "SUM($table_name.returning_customer) AS num_returning_customers", - 'num_new_customers' => "COUNT($table_name.returning_customer) - SUM($table_name.returning_customer) AS num_new_customers", - ); - - if ( $overrides ) { - $columns_mapping = array_merge( $columns_mapping, $overrides ); - } - - return $this->prepare_selections( $query_args, $columns_mapping ); - } - - /** - * Update row-level db result for segments in 'totals' section to the format used for output. - * - * @param array $segments_db_result Results from the SQL db query for segmenting. - * @param string $segment_dimension Name of column used for grouping the result. - * - * @return array Reformatted array. - */ - protected function reformat_totals_segments( $segments_db_result, $segment_dimension ) { - $segment_result = array(); - - if ( strpos( $segment_dimension, '.' ) ) { - $segment_dimension = substr( strstr( $segment_dimension, '.' ), 1 ); - } - - foreach ( $segments_db_result as $segment_data ) { - $segment_id = $segment_data[ $segment_dimension ]; - unset( $segment_data[ $segment_dimension ] ); - $segment_datum = array( - 'segment_id' => $segment_id, - 'subtotals' => $this->cast_numbers( $segment_data ), - ); - $segment_result[ $segment_id ] = $segment_datum; - } - - return $segment_result; - } - - /** - * Merges segmented results for totals response part. - * - * E.g. $r1 = array( - * 0 => array( - * 'product_id' => 3, - * 'net_amount' => 15, - * ), - * ); - * $r2 = array( - * 0 => array( - * 'product_id' => 3, - * 'avg_order_value' => 25, - * ), - * ); - * - * $merged = array( - * 3 => array( - * 'segment_id' => 3, - * 'subtotals' => array( - * 'net_amount' => 15, - * 'avg_order_value' => 25, - * ) - * ), - * ); - * - * @param string $segment_dimension Name of the segment dimension=key in the result arrays used to match records from result sets. - * @param array $result1 Array 1 of segmented figures. - * @param array $result2 Array 2 of segmented figures. - * - * @return array - */ - protected function merge_segment_totals_results( $segment_dimension, $result1, $result2 ) { - $result_segments = array(); - - foreach ( $result1 as $segment_data ) { - $segment_id = $segment_data[ $segment_dimension ]; - unset( $segment_data[ $segment_dimension ] ); - $result_segments[ $segment_id ] = array( - 'segment_id' => $segment_id, - 'subtotals' => $segment_data, - ); - } - - foreach ( $result2 as $segment_data ) { - $segment_id = $segment_data[ $segment_dimension ]; - unset( $segment_data[ $segment_dimension ] ); - if ( ! isset( $result_segments[ $segment_id ] ) ) { - $result_segments[ $segment_id ] = array( - 'segment_id' => $segment_id, - 'subtotals' => array(), - ); - } - $result_segments[ $segment_id ]['subtotals'] = array_merge( $result_segments[ $segment_id ]['subtotals'], $segment_data ); - } - return $result_segments; - } - /** - * Merges segmented results for intervals response part. - * - * E.g. $r1 = array( - * 0 => array( - * 'product_id' => 3, - * 'time_interval' => '2018-12' - * 'net_amount' => 15, - * ), - * ); - * $r2 = array( - * 0 => array( - * 'product_id' => 3, - * 'time_interval' => '2018-12' - * 'avg_order_value' => 25, - * ), - * ); - * - * $merged = array( - * '2018-12' => array( - * 'segments' => array( - * 3 => array( - * 'segment_id' => 3, - * 'subtotals' => array( - * 'net_amount' => 15, - * 'avg_order_value' => 25, - * ), - * ), - * ), - * ), - * ); - * - * @param string $segment_dimension Name of the segment dimension=key in the result arrays used to match records from result sets. - * @param array $result1 Array 1 of segmented figures. - * @param array $result2 Array 2 of segmented figures. - * - * @return array - */ - protected function merge_segment_intervals_results( $segment_dimension, $result1, $result2 ) { - $result_segments = array(); - - foreach ( $result1 as $segment_data ) { - $time_interval = $segment_data['time_interval']; - if ( ! isset( $result_segments[ $time_interval ] ) ) { - $result_segments[ $time_interval ] = array(); - $result_segments[ $time_interval ]['segments'] = array(); - } - unset( $segment_data['time_interval'] ); - unset( $segment_data['datetime_anchor'] ); - $segment_id = $segment_data[ $segment_dimension ]; - unset( $segment_data[ $segment_dimension ] ); - $segment_datum = array( - 'segment_id' => $segment_id, - 'subtotals' => $this->cast_numbers( $segment_data ), - ); - $result_segments[ $time_interval ]['segments'][ $segment_id ] = $segment_datum; - } - - foreach ( $result2 as $segment_data ) { - $time_interval = $segment_data['time_interval']; - if ( ! isset( $result_segments[ $time_interval ] ) ) { - $result_segments[ $time_interval ] = array(); - $result_segments[ $time_interval ]['segments'] = array(); - } - unset( $segment_data['time_interval'] ); - unset( $segment_data['datetime_anchor'] ); - $segment_id = $segment_data[ $segment_dimension ]; - unset( $segment_data[ $segment_dimension ] ); - - if ( ! isset( $result_segments[ $time_interval ]['segments'][ $segment_id ] ) ) { - $result_segments[ $time_interval ]['segments'][ $segment_id ] = array( - 'segment_id' => $segment_id, - 'subtotals' => array(), - ); - } - $result_segments[ $time_interval ]['segments'][ $segment_id ]['subtotals'] = array_merge( $result_segments[ $time_interval ]['segments'][ $segment_id ]['subtotals'], $segment_data ); - } - return $result_segments; - } - - /** - * Update row-level db result for segments in 'intervals' section to the format used for output. - * - * @param array $segments_db_result Results from the SQL db query for segmenting. - * @param string $segment_dimension Name of column used for grouping the result. - * - * @return array Reformatted array. - */ - protected function reformat_intervals_segments( $segments_db_result, $segment_dimension ) { - $aggregated_segment_result = array(); - - if ( strpos( $segment_dimension, '.' ) ) { - $segment_dimension = substr( strstr( $segment_dimension, '.' ), 1 ); - } - - foreach ( $segments_db_result as $segment_data ) { - $time_interval = $segment_data['time_interval']; - if ( ! isset( $aggregated_segment_result[ $time_interval ] ) ) { - $aggregated_segment_result[ $time_interval ] = array(); - $aggregated_segment_result[ $time_interval ]['segments'] = array(); - } - unset( $segment_data['time_interval'] ); - unset( $segment_data['datetime_anchor'] ); - $segment_id = $segment_data[ $segment_dimension ]; - unset( $segment_data[ $segment_dimension ] ); - $segment_datum = array( - 'segment_id' => $segment_id, - 'subtotals' => $this->cast_numbers( $segment_data ), - ); - $aggregated_segment_result[ $time_interval ]['segments'][ $segment_id ] = $segment_datum; - } - - return $aggregated_segment_result; - } - - /** - * Return all segments for given segmentby parameter. - * - * @param array $query_args Query args provided by the user. - * - * @return array - */ - protected function get_all_segments( $query_args ) { - global $wpdb; - if ( ! isset( $query_args['segmentby'] ) || '' === $query_args['segmentby'] ) { - return array(); - } - - if ( 'product' === $query_args['segmentby'] ) { - $segments = wc_get_products( - array( - 'return' => 'ids', - 'limit' => -1, - ) - ); - } elseif ( 'variation' === $query_args['segmentby'] ) { - // TODO: assuming that this will only be used for one product, check assumption. - if ( ! isset( $query_args['product_includes'] ) || count( $query_args['product_includes'] ) !== 1 ) { - return array(); - } - - $segments = wc_get_products( - array( - 'return' => 'ids', - 'limit' => - 1, - 'type' => 'variation', - 'parent' => $query_args['product_includes'][0], - ) - ); - } elseif ( 'category' === $query_args['segmentby'] ) { - $categories = get_categories( - array( - 'taxonomy' => 'product_cat', - ) - ); - $segments = wp_list_pluck( $categories, 'cat_ID' ); - } elseif ( 'coupon' === $query_args['segmentby'] ) { - $coupon_ids = $wpdb->get_results( "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type='shop_coupon' AND post_status='publish'", ARRAY_A ); // WPCS: cache ok, DB call ok, unprepared SQL ok. - $segments = wp_list_pluck( $coupon_ids, 'ID' ); - } elseif ( 'customer_type' === $query_args['segmentby'] ) { - // 0 -- new customer - // 1 -- returning customer - $segments = array( 0, 1 ); - } - - return $segments; - } - - /** - * Adds zeroes for segments not present in the data selection. - * - * @param array $query_args Query arguments provided by the user. - * @param array $segments Array of segments from the database for given data points. - * @param array $all_segment_ids Array of all possible segment ids. - * - * @return array - */ - protected function fill_in_missing_segments( $query_args, $segments, $all_segment_ids ) { - - $segment_subtotals = array(); - if ( isset( $query_args['fields'] ) && is_array( $query_args['fields'] ) ) { - foreach ( $query_args['fields'] as $field ) { - if ( isset( $this->report_columns[ $field ] ) ) { - $segment_subtotals[ $field ] = 0; - } - } - } else { - foreach ( $this->report_columns as $field => $sql_clause ) { - $segment_subtotals[ $field ] = 0; - } - } - if ( ! is_array( $segments ) ) { - $segments = array(); - } - foreach ( $all_segment_ids as $segment_id ) { - if ( ! isset( $segments[ $segment_id ] ) ) { - $segments[ $segment_id ] = array( - 'segment_id' => $segment_id, - 'subtotals' => $segment_subtotals, - ); - } - } - - // Using array_values to remove custom keys, so that it gets later converted to JSON as an array. - $segments_no_keys = array_values( $segments ); - $this->sort_array( $segments_no_keys, 'segment_id', 'asc' ); - return $segments_no_keys; - } - - /** - * Adds missing segments to intervals, modifies $data. - * - * @param array $query_args Query arguments provided by the user. - * @param stdClass $data Response data. - * @param array $all_segment_ids Array of all possible segment ids. - */ - protected function fill_in_missing_interval_segments( $query_args, &$data, $all_segment_ids ) { - foreach ( $data->intervals as $order_id => $interval_data ) { - $data->intervals[ $order_id ]['segments'] = $this->fill_in_missing_segments( $query_args, $data->intervals[ $order_id ]['segments'], $all_segment_ids ); - } - } - - /** - * Calculate segments for segmenting property bound to product (e.g. category, product_id, variation_id). - * - * @param string $type Type of segments to return--'totals' or 'intervals'. - * @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'. - * @param string $segmenting_from FROM part of segmenting SQL query. - * @param string $segmenting_where WHERE part of segmenting SQL query. - * @param string $segmenting_groupby GROUP BY part of segmenting SQL query. - * @param string $segmenting_dimension_name Name of the segmenting dimension. - * @param string $table_name Name of SQL table which is the stats table for orders. - * @param array $query_params Array of SQL clauses for intervals/totals query. - * @param string $unique_orders_table Name of temporary SQL table that holds unique orders. - * @param array $all_segment_ids Array of all possible segment ids. - * - * @return array - */ - protected function get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table, $all_segment_ids ) { - if ( 'totals' === $type ) { - return $this->get_product_related_totals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table ); - } elseif ( 'intervals' === $type ) { - return $this->get_product_related_intervals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table, $all_segment_ids ); - } - } - - /** - * Calculate segments for segmenting property bound to order (e.g. coupon or customer type). - * - * @param string $type Type of segments to return--'totals' or 'intervals'. - * @param string $segmenting_select SELECT part of segmenting SQL query. - * @param string $segmenting_from FROM part of segmenting SQL query. - * @param string $segmenting_where WHERE part of segmenting SQL query. - * @param string $segmenting_groupby GROUP BY part of segmenting SQL query. - * @param string $table_name Name of SQL table which is the stats table for orders. - * @param array $query_params Array of SQL clauses for intervals/totals query. - * @param array $all_segment_ids Array of all possible segment ids. - * - * @return array - */ - protected function get_order_related_segments( $type, $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params, $all_segment_ids ) { - if ( 'totals' === $type ) { - return $this->get_order_related_totals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params ); - } elseif ( 'intervals' === $type ) { - return $this->get_order_related_intervals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params, $all_segment_ids ); - } - } - - /** - * Calculate segments for totals where the segmenting property is bound to product (e.g. category, product_id, variation_id). - * - * @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'. - * @param string $segmenting_from FROM part of segmenting SQL query. - * @param string $segmenting_where WHERE part of segmenting SQL query. - * @param string $segmenting_groupby GROUP BY part of segmenting SQL query. - * @param string $segmenting_dimension_name Name of the segmenting dimension. - * @param string $table_name Name of SQL table which is the stats table for orders. - * @param array $totals_query Array of SQL clauses for totals query. - * @param string $unique_orders_table Name of temporary SQL table that holds unique orders. - * - * @return array - */ - protected function get_product_related_totals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $totals_query, $unique_orders_table ) { - global $wpdb; - - $product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup'; - - // Can't get all the numbers from one query, so split it into one query for product-level numbers and one for order-level numbers (which first need to have orders uniqued). - // Product-level numbers. - $segments_products = $wpdb->get_results( - "SELECT - $segmenting_groupby AS $segmenting_dimension_name - {$segmenting_selections['product_level']} - FROM - $table_name - $segmenting_from - {$totals_query['from_clause']} - WHERE - 1=1 - {$totals_query['where_time_clause']} - {$totals_query['where_clause']} - $segmenting_where - GROUP BY - $segmenting_groupby", - ARRAY_A - ); // WPCS: cache ok, DB call ok, unprepared SQL ok. - - // Order level numbers. - // As there can be 2 same product ids (or variation ids) per one order, the orders first need to be uniqued before calculating averages, customer counts, etc. - $segments_orders = $wpdb->get_results( - "SELECT - $unique_orders_table.$segmenting_dimension_name AS $segmenting_dimension_name - {$segmenting_selections['order_level']} - FROM - ( - SELECT - $table_name.order_id, - $segmenting_groupby AS $segmenting_dimension_name, - MAX( num_items_sold ) AS num_items_sold, - MAX( net_total ) as net_total, - MAX( refund_total ) as refund_total, - MAX( returning_customer ) AS returning_customer - FROM - $table_name - $segmenting_from - {$totals_query['from_clause']} - WHERE - 1=1 - {$totals_query['where_time_clause']} - {$totals_query['where_clause']} - $segmenting_where - GROUP BY - $product_segmenting_table.order_id, $segmenting_groupby - ) AS $unique_orders_table - GROUP BY - $unique_orders_table.$segmenting_dimension_name", - ARRAY_A - ); // WPCS: cache ok, DB call ok, unprepared SQL ok. - - $totals_segments = $this->merge_segment_totals_results( $segmenting_dimension_name, $segments_products, $segments_orders ); - return $totals_segments; - } - - /** - * Calculate segments for intervals where the segmenting property is bound to product (e.g. category, product_id, variation_id). - * - * @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'. - * @param string $segmenting_from FROM part of segmenting SQL query. - * @param string $segmenting_where WHERE part of segmenting SQL query. - * @param string $segmenting_groupby GROUP BY part of segmenting SQL query. - * @param string $segmenting_dimension_name Name of the segmenting dimension. - * @param string $table_name Name of SQL table which is the stats table for orders. - * @param array $intervals_query Array of SQL clauses for intervals query. - * @param string $unique_orders_table Name of temporary SQL table that holds unique orders. - * @param array $all_segment_ids Array of all possible segment ids. - * - * @return array - */ - protected function get_product_related_intervals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $intervals_query, $unique_orders_table, $all_segment_ids ) { - global $wpdb; - - $product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup'; - - // LIMIT offset, rowcount needs to be updated to LIMIT offset, rowcount * max number of segments. - $limit_parts = explode( ',', $intervals_query['limit'] ); - $orig_rowcount = intval( $limit_parts[1] ); - $segmenting_limit = $limit_parts[0] . ',' . $orig_rowcount * count( $all_segment_ids ); - - // Can't get all the numbers from one query, so split it into one query for product-level numbers and one for order-level numbers (which first need to have orders uniqued). - // Product-level numbers. - $segments_products = $wpdb->get_results( - "SELECT - - {$intervals_query['select_clause']} AS time_interval, - $segmenting_groupby AS $segmenting_dimension_name - {$segmenting_selections['product_level']} - FROM - $table_name - $segmenting_from - {$intervals_query['from_clause']} - WHERE - 1=1 - {$intervals_query['where_time_clause']} - {$intervals_query['where_clause']} - $segmenting_where - GROUP BY - time_interval, $segmenting_groupby - $segmenting_limit", - ARRAY_A - ); // WPCS: cache ok, DB call ok, unprepared SQL ok. - - // Order level numbers. - // As there can be 2 same product ids (or variation ids) per one order, the orders first need to be uniqued before calculating averages, customer counts, etc. - $segments_orders = $wpdb->get_results( - "SELECT - $unique_orders_table.time_interval AS time_interval, - $unique_orders_table.$segmenting_dimension_name AS $segmenting_dimension_name - {$segmenting_selections['order_level']} - FROM - ( - SELECT - MAX( $table_name.date_created ) AS datetime_anchor, - {$intervals_query['select_clause']} AS time_interval, - $table_name.order_id, - $segmenting_groupby AS $segmenting_dimension_name, - MAX( num_items_sold ) AS num_items_sold, - MAX( net_total ) as net_total, - MAX( refund_total ) as refund_total, - MAX( returning_customer ) AS returning_customer - FROM - $table_name - $segmenting_from - {$intervals_query['from_clause']} - WHERE - 1=1 - {$intervals_query['where_time_clause']} - {$intervals_query['where_clause']} - $segmenting_where - GROUP BY - time_interval, $product_segmenting_table.order_id, $segmenting_groupby - ) AS $unique_orders_table - GROUP BY - time_interval, $unique_orders_table.$segmenting_dimension_name - $segmenting_limit", - ARRAY_A - ); // WPCS: cache ok, DB call ok, unprepared SQL ok. - - $intervals_segments = $this->merge_segment_intervals_results( $segmenting_dimension_name, $segments_products, $segments_orders ); - return $intervals_segments; - } - - /** - * Calculate segments for totals query where the segmenting property is bound to order (e.g. coupon or customer type). - * - * @param string $segmenting_select SELECT part of segmenting SQL query. - * @param string $segmenting_from FROM part of segmenting SQL query. - * @param string $segmenting_where WHERE part of segmenting SQL query. - * @param string $segmenting_groupby GROUP BY part of segmenting SQL query. - * @param string $table_name Name of SQL table which is the stats table for orders. - * @param array $totals_query Array of SQL clauses for intervals query. - * - * @return array - */ - protected function get_order_related_totals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $totals_query ) { - global $wpdb; - - $totals_segments = $wpdb->get_results( - "SELECT - $segmenting_groupby - $segmenting_select - FROM - $table_name - $segmenting_from - {$totals_query['from_clause']} - WHERE - 1=1 - {$totals_query['where_time_clause']} - {$totals_query['where_clause']} - $segmenting_where - GROUP BY - $segmenting_groupby", - ARRAY_A - ); // WPCS: cache ok, DB call ok, unprepared SQL ok. - - // Reformat result. - $totals_segments = $this->reformat_totals_segments( $totals_segments, $segmenting_groupby ); - return $totals_segments; - } - - /** - * Calculate segments for intervals query where the segmenting property is bound to order (e.g. coupon or customer type). - * - * @param string $segmenting_select SELECT part of segmenting SQL query. - * @param string $segmenting_from FROM part of segmenting SQL query. - * @param string $segmenting_where WHERE part of segmenting SQL query. - * @param string $segmenting_groupby GROUP BY part of segmenting SQL query. - * @param string $table_name Name of SQL table which is the stats table for orders. - * @param array $intervals_query Array of SQL clauses for intervals query. - * @param array $all_segment_ids Ids of all possible segments. - * - * @return array - */ - protected function get_order_related_intervals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $intervals_query, $all_segment_ids ) { - global $wpdb; - $limit_parts = explode( ',', $intervals_query['limit'] ); - $orig_rowcount = intval( $limit_parts[1] ); - $segmenting_limit = $limit_parts[0] . ',' . $orig_rowcount * count( $all_segment_ids ); - - $intervals_segments = $wpdb->get_results( - "SELECT - MAX($table_name.date_created) AS datetime_anchor, - {$intervals_query['select_clause']} AS time_interval, - $segmenting_groupby - $segmenting_select - FROM - $table_name - $segmenting_from - {$intervals_query['from_clause']} - WHERE - 1=1 - {$intervals_query['where_time_clause']} - {$intervals_query['where_clause']} - $segmenting_where - GROUP BY - time_interval, $segmenting_groupby - $segmenting_limit", - ARRAY_A - ); // WPCS: cache ok, DB call ok, unprepared SQL ok. - - // Reformat result. - $intervals_segments = $this->reformat_intervals_segments( $intervals_segments, $segmenting_groupby ); - return $intervals_segments; - } - - /** - * Return array of segments formatted for REST response. - * - * @param string $type Type of segments to return--'totals' or 'intervals'. - * @param array $query_args Parameters provided by the user. - * @param array $query_params SQL query parameter array. - * @param string $table_name Name of main SQL table for the data store (used as basis for JOINS). - * @param array $all_segments Array of all segment ids. - * - * @return array - * @throws WC_REST_Exception In case of segmenting by variations, when no parent product is specified. - */ - protected function get_segments( $type, $query_args, $query_params, $table_name, $all_segments ) { - global $wpdb; - if ( ! isset( $query_args['segmentby'] ) || '' === $query_args['segmentby'] ) { - return array(); - } - - $product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup'; - $unique_orders_table = 'uniq_orders'; - $segmenting_where = ''; - - // Product, variation, and category are bound to product, so here product segmenting table is required, - // while coupon and customer are bound to order, so we don't need the extra JOIN for those. - // This also means that segment selections need to be calculated differently. - if ( 'product' === $query_args['segmentby'] ) { - // TODO: how to handle shipping taxes when grouped by product? - $segmenting_selections = array( - 'product_level' => $this->get_segment_selections_product_level( $query_args, $product_segmenting_table ), - 'order_level' => $this->get_segment_selections_order_level( $query_args, $unique_orders_table ), - ); - $segmenting_from = "INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)"; - $segmenting_groupby = $product_segmenting_table . '.product_id'; - $segmenting_dimension_name = 'product_id'; - - $segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table, $all_segments ); - } elseif ( 'variation' === $query_args['segmentby'] ) { - if ( ! isset( $query_args['product_includes'] ) || count( $query_args['product_includes'] ) !== 1 ) { - throw new WC_REST_Exception( 'woocommerce_rest_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'wc-admin' ), 400 ); - } - - $segmenting_selections = array( - 'product_level' => $this->get_segment_selections_product_level( $query_args, $product_segmenting_table ), - 'order_level' => $this->get_segment_selections_order_level( $query_args, $unique_orders_table ), - ); - $segmenting_from = "INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)"; - $segmenting_where = "AND $product_segmenting_table.product_id = {$query_args['product_includes'][0]}"; - $segmenting_groupby = $product_segmenting_table . '.variation_id'; - $segmenting_dimension_name = 'variation_id'; - - $segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table, $all_segments ); - } elseif ( 'category' === $query_args['segmentby'] ) { - $segmenting_selections = array( - 'product_level' => $this->get_segment_selections_product_level( $query_args, $product_segmenting_table ), - 'order_level' => $this->get_segment_selections_order_level( $query_args, $unique_orders_table ), - ); - $segmenting_from = " - INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id) - LEFT JOIN {$wpdb->prefix}term_relationships ON {$product_segmenting_table}.product_id = {$wpdb->prefix}term_relationships.object_id - RIGHT JOIN {$wpdb->prefix}term_taxonomy ON {$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}term_taxonomy.term_taxonomy_id - "; - $segmenting_where = " AND taxonomy = 'product_cat'"; - $segmenting_groupby = 'wp_term_taxonomy.term_taxonomy_id'; - $segmenting_dimension_name = 'category_id'; - - $segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table, $all_segments ); - } elseif ( 'coupon' === $query_args['segmentby'] ) { - // As there can be 2 or more coupons applied per one order, coupon amount needs to be split. - $coupon_override = array( - 'coupons' => 'SUM(coupon_lookup.discount_amount) AS coupons', - ); - $segmenting_selections = $this->segment_selections_orders( $query_args, $table_name, $coupon_override ); - $segmenting_from = " - INNER JOIN {$wpdb->prefix}wc_order_coupon_lookup AS coupon_lookup ON ($table_name.order_id = coupon_lookup.order_id) - "; - $segmenting_groupby = 'coupon_lookup.coupon_id'; - - $segments = $this->get_order_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params, $all_segments ); - } elseif ( 'customer_type' === $query_args['segmentby'] ) { - $segmenting_selections = $this->segment_selections_orders( $query_args, $table_name ); - $segmenting_from = ''; - $segmenting_groupby = "$table_name.returning_customer"; - - $segments = $this->get_order_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params, $all_segments ); - } - - return $segments; - } - - /** - * Assign segments to time intervals by updating original $intervals array. - * - * @param array $intervals Result array from intervals SQL query. - * @param array $intervals_segments Result array from interval segments SQL query. - */ - protected function assign_segments_to_intervals( &$intervals, $intervals_segments ) { - $old_keys = array_keys( $intervals ); - foreach ( $intervals as $interval ) { - $intervals[ $interval['time_interval'] ] = $interval; - $intervals[ $interval['time_interval'] ]['segments'] = array(); - } - foreach ( $old_keys as $key ) { - unset( $intervals[ $key ] ); - } - - foreach ( $intervals_segments as $time_interval => $segment ) { - if ( ! isset( $intervals[ $time_interval ] ) ) { - $intervals[ $time_interval ]['segments'] = array(); - } - $intervals[ $time_interval ]['segments'] = $segment['segments']; - } - - // To remove time interval keys (so that REST response is formatted correctly). - $intervals = array_values( $intervals ); - } - /** * Returns the report data based on parameters supplied by the user. * @@ -1089,9 +271,9 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto $unique_products = $this->get_unique_product_count( $totals_query['from_clause'], $totals_query['where_time_clause'], $totals_query['where_clause'] ); $totals[0]['products'] = $unique_products; - $all_segments = $this->get_all_segments( $query_args ); - $segments = $this->get_segments( 'totals', $query_args, $totals_query, $table_name, $all_segments ); - $totals[0]['segments'] = $this->fill_in_missing_segments( $query_args, $segments, $all_segments ); + $segmenting = new WC_Admin_Reports_Orders_Stats_Segmenting( $query_args, $this->report_columns ); + $totals[0]['segments'] = $segmenting->get_totals_segments( $totals_query, $table_name ); + $totals = (object) $this->cast_numbers( $totals[0] ); $db_intervals = $wpdb->get_col( @@ -1146,11 +328,6 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto return new WP_Error( 'woocommerce_reports_revenue_result_failed', __( 'Sorry, fetching revenue data failed.', 'wc-admin' ) ); } - $intervals_segments = $this->get_segments( 'intervals', $query_args, $intervals_query, $table_name, $all_segments ); - - // Pigeon hole segments. - $this->assign_segments_to_intervals( $intervals, $intervals_segments ); - $data = (object) array( 'totals' => $totals, 'intervals' => $intervals, @@ -1163,10 +340,10 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto $this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data ); $this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] ); $this->remove_extra_records( $data, $query_args['page'], $intervals_query['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'] ); - $this->fill_in_missing_interval_segments( $query_args, $data, $all_segments ); } else { $this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $data->intervals ); } + $segmenting->add_intervals_segments( $data, $intervals_query, $table_name ); $this->create_interval_subtotals( $data->intervals ); wp_cache_set( $cache_key, $data, $this->cache_group );