Segmenter.php
152 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class for adding segmenting support without cluttering the data stores. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\API\Reports\Segmenter as ReportsSegmenter; |
| 11 | |
| 12 | /** |
| 13 | * Date & time interval and numeric range handling class for Reporting API. |
| 14 | */ |
| 15 | class Segmenter extends ReportsSegmenter { |
| 16 | |
| 17 | /** |
| 18 | * Returns column => query mapping to be used for order-related order-level segmenting query (e.g. tax_rate_id). |
| 19 | * |
| 20 | * @param string $lookup_table Name of SQL table containing the order-level segmenting info. |
| 21 | * |
| 22 | * @return array Column => SELECT query mapping. |
| 23 | */ |
| 24 | protected function get_segment_selections_order_level( $lookup_table ) { |
| 25 | $columns_mapping = array( |
| 26 | 'tax_codes' => "COUNT(DISTINCT $lookup_table.tax_rate_id) as tax_codes", |
| 27 | 'total_tax' => "SUM($lookup_table.total_tax) AS total_tax", |
| 28 | 'order_tax' => "SUM($lookup_table.order_tax) as order_tax", |
| 29 | 'shipping_tax' => "SUM($lookup_table.shipping_tax) as shipping_tax", |
| 30 | 'orders_count' => "COUNT(DISTINCT $lookup_table.order_id) as orders_count", |
| 31 | ); |
| 32 | |
| 33 | return $columns_mapping; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Calculate segments for totals query where the segmenting property is bound to order (e.g. coupon or customer type). |
| 38 | * |
| 39 | * @param string $segmenting_select SELECT part of segmenting SQL query. |
| 40 | * @param string $segmenting_from FROM part of segmenting SQL query. |
| 41 | * @param string $segmenting_where WHERE part of segmenting SQL query. |
| 42 | * @param string $segmenting_groupby GROUP BY part of segmenting SQL query. |
| 43 | * @param string $table_name Name of SQL table which is the stats table for orders. |
| 44 | * @param array $totals_query Array of SQL clauses for intervals query. |
| 45 | * |
| 46 | * @return array |
| 47 | */ |
| 48 | protected function get_order_related_totals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $totals_query ) { |
| 49 | global $wpdb; |
| 50 | |
| 51 | $totals_segments = $wpdb->get_results( |
| 52 | "SELECT |
| 53 | $segmenting_groupby |
| 54 | $segmenting_select |
| 55 | FROM |
| 56 | $table_name |
| 57 | $segmenting_from |
| 58 | {$totals_query['from_clause']} |
| 59 | WHERE |
| 60 | 1=1 |
| 61 | {$totals_query['where_time_clause']} |
| 62 | {$totals_query['where_clause']} |
| 63 | $segmenting_where |
| 64 | GROUP BY |
| 65 | $segmenting_groupby", |
| 66 | ARRAY_A |
| 67 | ); // WPCS: cache ok, DB call ok, unprepared SQL ok. |
| 68 | |
| 69 | // Reformat result. |
| 70 | $totals_segments = $this->reformat_totals_segments( $totals_segments, $segmenting_groupby ); |
| 71 | return $totals_segments; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Calculate segments for intervals query where the segmenting property is bound to order (e.g. coupon or customer type). |
| 76 | * |
| 77 | * @param string $segmenting_select SELECT part of segmenting SQL query. |
| 78 | * @param string $segmenting_from FROM part of segmenting SQL query. |
| 79 | * @param string $segmenting_where WHERE part of segmenting SQL query. |
| 80 | * @param string $segmenting_groupby GROUP BY part of segmenting SQL query. |
| 81 | * @param string $table_name Name of SQL table which is the stats table for orders. |
| 82 | * @param array $intervals_query Array of SQL clauses for intervals query. |
| 83 | * |
| 84 | * @return array |
| 85 | */ |
| 86 | protected function get_order_related_intervals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $intervals_query ) { |
| 87 | global $wpdb; |
| 88 | $segmenting_limit = ''; |
| 89 | $limit_parts = explode( ',', $intervals_query['limit'] ); |
| 90 | if ( 2 === count( $limit_parts ) ) { |
| 91 | $orig_rowcount = intval( $limit_parts[1] ); |
| 92 | $segmenting_limit = $limit_parts[0] . ',' . $orig_rowcount * count( $this->get_all_segments() ); |
| 93 | } |
| 94 | |
| 95 | $intervals_segments = $wpdb->get_results( |
| 96 | "SELECT |
| 97 | MAX($table_name.date_created) AS datetime_anchor, |
| 98 | {$intervals_query['select_clause']} AS time_interval, |
| 99 | $segmenting_groupby |
| 100 | $segmenting_select |
| 101 | FROM |
| 102 | $table_name |
| 103 | $segmenting_from |
| 104 | {$intervals_query['from_clause']} |
| 105 | WHERE |
| 106 | 1=1 |
| 107 | {$intervals_query['where_time_clause']} |
| 108 | {$intervals_query['where_clause']} |
| 109 | $segmenting_where |
| 110 | GROUP BY |
| 111 | time_interval, $segmenting_groupby |
| 112 | $segmenting_limit", |
| 113 | ARRAY_A |
| 114 | ); // WPCS: cache ok, DB call ok, unprepared SQL ok. |
| 115 | |
| 116 | // Reformat result. |
| 117 | $intervals_segments = $this->reformat_intervals_segments( $intervals_segments, $segmenting_groupby ); |
| 118 | return $intervals_segments; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Return array of segments formatted for REST response. |
| 123 | * |
| 124 | * @param string $type Type of segments to return--'totals' or 'intervals'. |
| 125 | * @param array $query_params SQL query parameter array. |
| 126 | * @param string $table_name Name of main SQL table for the data store (used as basis for JOINS). |
| 127 | * |
| 128 | * @return array |
| 129 | * @throws \Automattic\WooCommerce\Admin\API\Reports\ParameterException In case of segmenting by variations, when no parent product is specified. |
| 130 | */ |
| 131 | protected function get_segments( $type, $query_params, $table_name ) { |
| 132 | if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) { |
| 133 | return array(); |
| 134 | } |
| 135 | |
| 136 | $segmenting_where = ''; |
| 137 | $segmenting_from = ''; |
| 138 | $segments = array(); |
| 139 | |
| 140 | if ( 'tax_rate_id' === $this->query_args['segmentby'] ) { |
| 141 | $tax_rate_level_columns = $this->get_segment_selections_order_level( $table_name ); |
| 142 | $segmenting_select = $this->prepare_selections( $tax_rate_level_columns ); |
| 143 | $this->report_columns = $tax_rate_level_columns; |
| 144 | $segmenting_groupby = $table_name . '.tax_rate_id'; |
| 145 | |
| 146 | $segments = $this->get_order_related_segments( $type, $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params ); |
| 147 | } |
| 148 | |
| 149 | return $segments; |
| 150 | } |
| 151 | } |
| 152 |