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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Admin / API / Reports / Revenue / Stats / Controller.php
woocommerce / src / Admin / API / Reports / Revenue / Stats Last commit date
Controller.php 3 months ago
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