Controller.php
282 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Reports taxes controller |
| 4 | * |
| 5 | * Handles requests to the /reports/taxes endpoint. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\API\Reports\Taxes; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface; |
| 13 | use Automattic\WooCommerce\Admin\API\Reports\ExportableTraits; |
| 14 | use Automattic\WooCommerce\Admin\API\Reports\GenericController; |
| 15 | use Automattic\WooCommerce\Admin\API\Reports\GenericQuery; |
| 16 | use WP_REST_Request; |
| 17 | use WP_REST_Response; |
| 18 | |
| 19 | /** |
| 20 | * REST API Reports taxes controller class. |
| 21 | * |
| 22 | * @internal |
| 23 | * @extends GenericController |
| 24 | */ |
| 25 | class Controller extends GenericController 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/taxes'; |
| 37 | |
| 38 | /** |
| 39 | * Get data from `'taxes'` GenericQuery. |
| 40 | * |
| 41 | * @override GenericController::get_datastore_data() |
| 42 | * |
| 43 | * @param array $query_args Query arguments. |
| 44 | * @return mixed Results from the data store. |
| 45 | */ |
| 46 | protected function get_datastore_data( $query_args = array() ) { |
| 47 | $query = new GenericQuery( $query_args, 'taxes' ); |
| 48 | return $query->get_data(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Maps query arguments from the REST request. |
| 53 | * |
| 54 | * @param array $request Request array. |
| 55 | * @return array |
| 56 | */ |
| 57 | protected function prepare_reports_query( $request ) { |
| 58 | $args = array(); |
| 59 | $args['before'] = $request['before']; |
| 60 | $args['after'] = $request['after']; |
| 61 | $args['page'] = $request['page']; |
| 62 | $args['per_page'] = $request['per_page']; |
| 63 | $args['orderby'] = $request['orderby']; |
| 64 | $args['order'] = $request['order']; |
| 65 | $args['taxes'] = $request['taxes']; |
| 66 | $args['force_cache_refresh'] = $request['force_cache_refresh']; |
| 67 | |
| 68 | return $args; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Prepare a report data item for serialization. |
| 73 | * |
| 74 | * @param mixed $report Report data item as returned from Data Store. |
| 75 | * @param WP_REST_Request $request Request object. |
| 76 | * @return WP_REST_Response |
| 77 | */ |
| 78 | public function prepare_item_for_response( $report, $request ) { |
| 79 | $response = parent::prepare_item_for_response( $report, $request ); |
| 80 | |
| 81 | // Map to `object` for backwards compatibility. |
| 82 | $report = (object) $report; |
| 83 | $response->add_links( $this->prepare_links( $report ) ); |
| 84 | |
| 85 | /** |
| 86 | * Filter a report returned from the API. |
| 87 | * |
| 88 | * Allows modification of the report data right before it is returned. |
| 89 | * |
| 90 | * @param WP_REST_Response $response The response object. |
| 91 | * @param object $report The original report object. |
| 92 | * @param WP_REST_Request $request Request used to generate the response. |
| 93 | */ |
| 94 | return apply_filters( 'woocommerce_rest_prepare_report_taxes', $response, $report, $request ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Prepare links for the request. |
| 99 | * |
| 100 | * @param WC_Reports_Query $object Object data. |
| 101 | * @return array |
| 102 | */ |
| 103 | protected function prepare_links( $object ) { |
| 104 | $links = array( |
| 105 | 'tax' => array( |
| 106 | 'href' => rest_url( sprintf( '/%s/taxes/%d', $this->namespace, $object->tax_rate_id ) ), |
| 107 | ), |
| 108 | ); |
| 109 | |
| 110 | return $links; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get the Report's schema, conforming to JSON Schema. |
| 115 | * |
| 116 | * @return array |
| 117 | */ |
| 118 | public function get_item_schema() { |
| 119 | $schema = array( |
| 120 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 121 | 'title' => 'report_taxes', |
| 122 | 'type' => 'object', |
| 123 | 'properties' => array( |
| 124 | 'tax_rate_id' => array( |
| 125 | 'description' => __( 'Tax rate ID.', 'woocommerce' ), |
| 126 | 'type' => 'integer', |
| 127 | 'context' => array( 'view', 'edit' ), |
| 128 | 'readonly' => true, |
| 129 | ), |
| 130 | 'name' => array( |
| 131 | 'description' => __( 'Tax rate name.', 'woocommerce' ), |
| 132 | 'type' => 'string', |
| 133 | 'context' => array( 'view', 'edit' ), |
| 134 | 'readonly' => true, |
| 135 | ), |
| 136 | 'tax_rate' => array( |
| 137 | 'description' => __( 'Tax rate.', 'woocommerce' ), |
| 138 | 'type' => 'number', |
| 139 | 'context' => array( 'view', 'edit' ), |
| 140 | 'readonly' => true, |
| 141 | ), |
| 142 | 'country' => array( |
| 143 | 'description' => __( 'Country / Region.', 'woocommerce' ), |
| 144 | 'type' => 'string', |
| 145 | 'context' => array( 'view', 'edit' ), |
| 146 | 'readonly' => true, |
| 147 | ), |
| 148 | 'state' => array( |
| 149 | 'description' => __( 'State.', 'woocommerce' ), |
| 150 | 'type' => 'string', |
| 151 | 'context' => array( 'view', 'edit' ), |
| 152 | 'readonly' => true, |
| 153 | ), |
| 154 | 'priority' => array( |
| 155 | 'description' => __( 'Priority.', 'woocommerce' ), |
| 156 | 'type' => 'integer', |
| 157 | 'context' => array( 'view', 'edit' ), |
| 158 | 'readonly' => true, |
| 159 | ), |
| 160 | 'total_tax' => array( |
| 161 | 'description' => __( 'Total tax.', 'woocommerce' ), |
| 162 | 'type' => 'number', |
| 163 | 'context' => array( 'view', 'edit' ), |
| 164 | 'readonly' => true, |
| 165 | ), |
| 166 | 'order_tax' => array( |
| 167 | 'description' => __( 'Order tax.', 'woocommerce' ), |
| 168 | 'type' => 'number', |
| 169 | 'context' => array( 'view', 'edit' ), |
| 170 | 'readonly' => true, |
| 171 | ), |
| 172 | 'shipping_tax' => array( |
| 173 | 'description' => __( 'Shipping tax.', 'woocommerce' ), |
| 174 | 'type' => 'number', |
| 175 | 'context' => array( 'view', 'edit' ), |
| 176 | 'readonly' => true, |
| 177 | ), |
| 178 | 'orders_count' => array( |
| 179 | 'description' => __( 'Number of orders.', 'woocommerce' ), |
| 180 | 'type' => 'integer', |
| 181 | 'context' => array( 'view', 'edit' ), |
| 182 | 'readonly' => true, |
| 183 | ), |
| 184 | ), |
| 185 | ); |
| 186 | |
| 187 | return $this->add_additional_fields_schema( $schema ); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Get the query params for collections. |
| 192 | * |
| 193 | * @return array |
| 194 | */ |
| 195 | public function get_collection_params() { |
| 196 | $params = parent::get_collection_params(); |
| 197 | $params['orderby']['default'] = 'tax_rate_id'; |
| 198 | $params['orderby']['enum'] = $this->apply_custom_orderby_filters( |
| 199 | array( |
| 200 | 'name', |
| 201 | 'tax_rate_id', |
| 202 | 'tax_code', |
| 203 | 'rate', |
| 204 | 'order_tax', |
| 205 | 'total_tax', |
| 206 | 'shipping_tax', |
| 207 | 'orders_count', |
| 208 | ) |
| 209 | ); |
| 210 | $params['taxes'] = array( |
| 211 | 'description' => __( 'Limit result set to items assigned one or more tax rates.', 'woocommerce' ), |
| 212 | 'type' => 'array', |
| 213 | 'sanitize_callback' => 'wp_parse_id_list', |
| 214 | 'validate_callback' => 'rest_validate_request_arg', |
| 215 | 'items' => array( |
| 216 | 'type' => 'string', |
| 217 | ), |
| 218 | ); |
| 219 | |
| 220 | return $params; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Get the column names for export. |
| 225 | * |
| 226 | * @return array Key value pair of Column ID => Label. |
| 227 | */ |
| 228 | public function get_export_columns() { |
| 229 | $export_columns = array( |
| 230 | 'tax_code' => __( 'Tax code', 'woocommerce' ), |
| 231 | 'rate' => __( 'Rate', 'woocommerce' ), |
| 232 | 'total_tax' => __( 'Total tax', 'woocommerce' ), |
| 233 | 'order_tax' => __( 'Order tax', 'woocommerce' ), |
| 234 | 'shipping_tax' => __( 'Shipping tax', 'woocommerce' ), |
| 235 | 'orders_count' => __( 'Orders', 'woocommerce' ), |
| 236 | ); |
| 237 | |
| 238 | /** |
| 239 | * Filter to add or remove column names from the taxes report for export. |
| 240 | * |
| 241 | * @since 10.7.0 |
| 242 | * @param array $export_columns Key value pair of column ID and label. |
| 243 | */ |
| 244 | return apply_filters( 'woocommerce_report_taxes_export_columns', $export_columns ); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Get the column values for export. |
| 249 | * |
| 250 | * @param array $item Single report item/row. |
| 251 | * @return array Key value pair of Column ID => Row Value. |
| 252 | */ |
| 253 | public function prepare_item_for_export( $item ) { |
| 254 | $export_item = array( |
| 255 | 'tax_code' => \WC_Tax::get_rate_code( |
| 256 | (object) array( |
| 257 | 'tax_rate_id' => $item['tax_rate_id'], |
| 258 | 'tax_rate_country' => $item['country'], |
| 259 | 'tax_rate_state' => $item['state'], |
| 260 | 'tax_rate_name' => $item['name'], |
| 261 | 'tax_rate_priority' => $item['priority'], |
| 262 | ) |
| 263 | ), |
| 264 | 'rate' => $item['tax_rate'], |
| 265 | 'total_tax' => self::csv_number_format( $item['total_tax'] ), |
| 266 | 'order_tax' => self::csv_number_format( $item['order_tax'] ), |
| 267 | 'shipping_tax' => self::csv_number_format( $item['shipping_tax'] ), |
| 268 | 'orders_count' => $item['orders_count'], |
| 269 | ); |
| 270 | |
| 271 | /** |
| 272 | * Filter to prepare extra columns in the export item for the taxes |
| 273 | * report. |
| 274 | * |
| 275 | * @since 10.7.0 |
| 276 | * @param array $export_item Key value pair of column ID and row value. |
| 277 | * @param array $item The original report item. |
| 278 | */ |
| 279 | return apply_filters( 'woocommerce_report_taxes_prepare_export_item', $export_item, $item ); |
| 280 | } |
| 281 | } |
| 282 |