Controller.php
327 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Reports revenue stats controller |
| 4 | * |
| 5 | * Handles requests to the /reports/revenue/stats endpoint. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\API\Reports\Revenue\Stats; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\WooCommerce\Admin\API\Reports\GenericStatsController; |
| 13 | use Automattic\WooCommerce\Admin\API\Reports\Revenue\Query as RevenueQuery; |
| 14 | use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface; |
| 15 | use Automattic\WooCommerce\Admin\API\Reports\ExportableTraits; |
| 16 | use WP_REST_Request; |
| 17 | use WP_REST_Response; |
| 18 | |
| 19 | /** |
| 20 | * REST API Reports revenue stats controller class. |
| 21 | * |
| 22 | * @internal |
| 23 | * @extends GenericStatsController |
| 24 | */ |
| 25 | class Controller extends GenericStatsController implements ExportableInterface { |
| 26 | /** |
| 27 | * Exportable traits. |
| 28 | */ |
| 29 | use ExportableTraits; |
| 30 | |
| 31 | /** |
| 32 | * Route base. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | protected $rest_base = 'reports/revenue/stats'; |
| 37 | |
| 38 | /** |
| 39 | * Maps query arguments from the REST request. |
| 40 | * |
| 41 | * @param array $request Request array. |
| 42 | * @return array |
| 43 | */ |
| 44 | protected function prepare_reports_query( $request ) { |
| 45 | $args = array(); |
| 46 | $args['before'] = $request['before']; |
| 47 | $args['after'] = $request['after']; |
| 48 | $args['interval'] = $request['interval']; |
| 49 | $args['page'] = $request['page']; |
| 50 | $args['per_page'] = $request['per_page']; |
| 51 | $args['orderby'] = $request['orderby']; |
| 52 | $args['order'] = $request['order']; |
| 53 | $args['segmentby'] = $request['segmentby']; |
| 54 | $args['fields'] = $request['fields']; |
| 55 | $args['force_cache_refresh'] = $request['force_cache_refresh']; |
| 56 | $args['date_type'] = $request['date_type']; |
| 57 | |
| 58 | return $args; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get data from RevenueQuery. |
| 63 | * |
| 64 | * @override GenericController::get_datastore_data() |
| 65 | * |
| 66 | * @param array $query_args Query arguments. |
| 67 | * @return mixed Results from the data store. |
| 68 | */ |
| 69 | protected function get_datastore_data( $query_args = array() ) { |
| 70 | $query = new RevenueQuery( $query_args ); |
| 71 | return $query->get_data(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get report items for export. |
| 76 | * |
| 77 | * Returns only the interval data. |
| 78 | * |
| 79 | * @param WP_REST_Request $request Request data. |
| 80 | * @return WP_REST_Response |
| 81 | */ |
| 82 | public function get_export_items( $request ) { |
| 83 | $response = $this->get_items( $request ); |
| 84 | $data = $response->get_data(); |
| 85 | $intervals = $data['intervals']; |
| 86 | |
| 87 | $response->set_data( $intervals ); |
| 88 | |
| 89 | return $response; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Prepare a report data item for serialization. |
| 94 | * |
| 95 | * @param array $report Report data item as returned from Data Store. |
| 96 | * @param WP_REST_Request $request Request object. |
| 97 | * @return WP_REST_Response |
| 98 | */ |
| 99 | public function prepare_item_for_response( $report, $request ) { |
| 100 | $response = parent::prepare_item_for_response( $report, $request ); |
| 101 | |
| 102 | /** |
| 103 | * Filter a report returned from the API. |
| 104 | * |
| 105 | * Allows modification of the report data right before it is returned. |
| 106 | * |
| 107 | * @param WP_REST_Response $response The response object. |
| 108 | * @param object $report The original report object. |
| 109 | * @param WP_REST_Request $request Request used to generate the response. |
| 110 | */ |
| 111 | return apply_filters( 'woocommerce_rest_prepare_report_revenue_stats', $response, $report, $request ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get the Report's item properties schema. |
| 116 | * Will be used by `get_item_schema` as `totals` and `subtotals`. |
| 117 | * |
| 118 | * @return array |
| 119 | */ |
| 120 | protected function get_item_properties_schema() { |
| 121 | return array( |
| 122 | 'total_sales' => array( |
| 123 | 'description' => __( 'Total sales.', 'woocommerce' ), |
| 124 | 'type' => 'number', |
| 125 | 'context' => array( 'view', 'edit' ), |
| 126 | 'readonly' => true, |
| 127 | 'indicator' => true, |
| 128 | 'format' => 'currency', |
| 129 | ), |
| 130 | 'net_revenue' => array( |
| 131 | 'description' => __( 'Net sales.', 'woocommerce' ), |
| 132 | 'type' => 'number', |
| 133 | 'context' => array( 'view', 'edit' ), |
| 134 | 'readonly' => true, |
| 135 | 'indicator' => true, |
| 136 | 'format' => 'currency', |
| 137 | ), |
| 138 | 'coupons' => array( |
| 139 | 'description' => __( 'Amount discounted by coupons.', 'woocommerce' ), |
| 140 | 'type' => 'number', |
| 141 | 'context' => array( 'view', 'edit' ), |
| 142 | 'readonly' => true, |
| 143 | ), |
| 144 | 'coupons_count' => array( |
| 145 | 'description' => __( 'Unique coupons count.', 'woocommerce' ), |
| 146 | 'type' => 'number', |
| 147 | 'context' => array( 'view', 'edit' ), |
| 148 | 'readonly' => true, |
| 149 | 'format' => 'currency', |
| 150 | ), |
| 151 | 'shipping' => array( |
| 152 | 'title' => __( 'Shipping', 'woocommerce' ), |
| 153 | 'description' => __( 'Total of shipping.', 'woocommerce' ), |
| 154 | 'type' => 'number', |
| 155 | 'context' => array( 'view', 'edit' ), |
| 156 | 'readonly' => true, |
| 157 | 'indicator' => true, |
| 158 | 'format' => 'currency', |
| 159 | ), |
| 160 | 'taxes' => array( |
| 161 | 'description' => __( 'Total of taxes.', 'woocommerce' ), |
| 162 | 'type' => 'number', |
| 163 | 'context' => array( 'view', 'edit' ), |
| 164 | 'readonly' => true, |
| 165 | 'format' => 'currency', |
| 166 | ), |
| 167 | 'refunds' => array( |
| 168 | 'title' => __( 'Returns', 'woocommerce' ), |
| 169 | 'description' => __( 'Total of returns.', 'woocommerce' ), |
| 170 | 'type' => 'number', |
| 171 | 'context' => array( 'view', 'edit' ), |
| 172 | 'readonly' => true, |
| 173 | 'indicator' => true, |
| 174 | 'format' => 'currency', |
| 175 | ), |
| 176 | 'orders_count' => array( |
| 177 | 'description' => __( 'Number of orders.', 'woocommerce' ), |
| 178 | 'type' => 'integer', |
| 179 | 'context' => array( 'view', 'edit' ), |
| 180 | 'readonly' => true, |
| 181 | ), |
| 182 | 'num_items_sold' => array( |
| 183 | 'description' => __( 'Items sold.', 'woocommerce' ), |
| 184 | 'type' => 'integer', |
| 185 | 'context' => array( 'view', 'edit' ), |
| 186 | 'readonly' => true, |
| 187 | ), |
| 188 | 'gross_sales' => array( |
| 189 | 'description' => __( 'Gross sales.', 'woocommerce' ), |
| 190 | 'type' => 'number', |
| 191 | 'context' => array( 'view', 'edit' ), |
| 192 | 'readonly' => true, |
| 193 | 'indicator' => true, |
| 194 | 'format' => 'currency', |
| 195 | ), |
| 196 | ); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Get the Report's schema, conforming to JSON Schema. |
| 201 | * |
| 202 | * @return array |
| 203 | */ |
| 204 | public function get_item_schema() { |
| 205 | $schema = parent::get_item_schema(); |
| 206 | $schema['title'] = 'report_revenue_stats'; |
| 207 | |
| 208 | // Products is not shown in intervals, only in totals. |
| 209 | $schema['properties']['totals']['properties']['products'] = array( |
| 210 | 'description' => __( 'Products sold.', 'woocommerce' ), |
| 211 | 'type' => 'integer', |
| 212 | 'context' => array( 'view', 'edit' ), |
| 213 | 'readonly' => true, |
| 214 | ); |
| 215 | |
| 216 | return $this->add_additional_fields_schema( $schema ); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Get the query params for collections. |
| 221 | * |
| 222 | * @return array |
| 223 | */ |
| 224 | public function get_collection_params() { |
| 225 | $params = parent::get_collection_params(); |
| 226 | $params['orderby']['enum'] = $this->apply_custom_orderby_filters( |
| 227 | array( |
| 228 | 'date', |
| 229 | 'total_sales', |
| 230 | 'coupons', |
| 231 | 'refunds', |
| 232 | 'shipping', |
| 233 | 'taxes', |
| 234 | 'net_revenue', |
| 235 | 'orders_count', |
| 236 | 'items_sold', |
| 237 | 'gross_sales', |
| 238 | ) |
| 239 | ); |
| 240 | $params['segmentby'] = array( |
| 241 | 'description' => __( 'Segment the response by additional constraint.', 'woocommerce' ), |
| 242 | 'type' => 'string', |
| 243 | 'enum' => array( |
| 244 | 'product', |
| 245 | 'category', |
| 246 | 'variation', |
| 247 | 'coupon', |
| 248 | 'customer_type', // new vs returning. |
| 249 | ), |
| 250 | 'validate_callback' => 'rest_validate_request_arg', |
| 251 | ); |
| 252 | $params['date_type'] = array( |
| 253 | 'description' => __( 'Override the "woocommerce_date_type" option that is used for the database date field considered for revenue reports.', 'woocommerce' ), |
| 254 | 'type' => 'string', |
| 255 | 'enum' => array( |
| 256 | 'date_paid', |
| 257 | 'date_created', |
| 258 | 'date_completed', |
| 259 | ), |
| 260 | 'validate_callback' => 'rest_validate_request_arg', |
| 261 | ); |
| 262 | unset( $params['fields'] ); |
| 263 | |
| 264 | return $params; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Get the column names for export. |
| 269 | * |
| 270 | * @return array Key value pair of Column ID => Label. |
| 271 | */ |
| 272 | public function get_export_columns() { |
| 273 | $export_columns = array( |
| 274 | 'date' => __( 'Date', 'woocommerce' ), |
| 275 | 'orders_count' => __( 'Orders', 'woocommerce' ), |
| 276 | 'gross_sales' => __( 'Gross sales', 'woocommerce' ), |
| 277 | 'refunds' => __( 'Returns', 'woocommerce' ), |
| 278 | 'coupons' => __( 'Coupons', 'woocommerce' ), |
| 279 | 'net_revenue' => __( 'Net sales', 'woocommerce' ), |
| 280 | 'taxes' => __( 'Taxes', 'woocommerce' ), |
| 281 | 'shipping' => __( 'Shipping', 'woocommerce' ), |
| 282 | 'total_sales' => __( 'Total sales', 'woocommerce' ), |
| 283 | ); |
| 284 | |
| 285 | /** |
| 286 | * Filter to add or remove column names from the revenue stats report for |
| 287 | * export. |
| 288 | * |
| 289 | * @since 10.7.0 |
| 290 | * @param array $export_columns Key value pair of column ID and label. |
| 291 | */ |
| 292 | return apply_filters( 'woocommerce_report_revenue_stats_export_columns', $export_columns ); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Get the column values for export. |
| 297 | * |
| 298 | * @param array $item Single report item/row. |
| 299 | * @return array Key value pair of Column ID => Row Value. |
| 300 | */ |
| 301 | public function prepare_item_for_export( $item ) { |
| 302 | $subtotals = (array) $item['subtotals']; |
| 303 | |
| 304 | $export_item = array( |
| 305 | 'date' => $item['date_start'], |
| 306 | 'orders_count' => $subtotals['orders_count'], |
| 307 | 'gross_sales' => self::csv_number_format( $subtotals['gross_sales'] ), |
| 308 | 'refunds' => self::csv_number_format( $subtotals['refunds'] ), |
| 309 | 'coupons' => self::csv_number_format( $subtotals['coupons'] ), |
| 310 | 'net_revenue' => self::csv_number_format( $subtotals['net_revenue'] ), |
| 311 | 'taxes' => self::csv_number_format( $subtotals['taxes'] ), |
| 312 | 'shipping' => self::csv_number_format( $subtotals['shipping'] ), |
| 313 | 'total_sales' => self::csv_number_format( $subtotals['total_sales'] ), |
| 314 | ); |
| 315 | |
| 316 | /** |
| 317 | * Filter to prepare extra columns in the export item for the revenue |
| 318 | * stats report. |
| 319 | * |
| 320 | * @since 10.7.0 |
| 321 | * @param array $export_item Key value pair of column ID and row value. |
| 322 | * @param array $item The original report item. |
| 323 | */ |
| 324 | return apply_filters( 'woocommerce_report_revenue_stats_prepare_export_item', $export_item, $item ); |
| 325 | } |
| 326 | } |
| 327 |