PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.6.7
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.6.7
1.17.9 1.17.8 1.17.7 1.17.6 1.17.5 1.17.4 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.11 1.3.12 1.3.13 1.3.14 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 143 releases
erp / modules / accounting / api / class-rest-api-products.php

class-rest-api-products.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.6.7, at modules/accounting/api/class-rest-api-products.php

495 lines 17.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WeDevs\ERP\Accounting\API;
4
5 use WP_Error;
6 use WP_REST_Response;
7 use WP_REST_Server;
8
9 class Inventory_Products_Controller extends \WeDevs\ERP\API\REST_Controller {
10
11 /**
12 * Endpoint namespace.
13 *
14 * @var string
15 */
16 protected $namespace = 'erp/v1';
17
18 /**
19 * Route base.
20 *
21 * @var string
22 */
23 protected $rest_base = 'accounting/v1/products';
24
25 /**
26 * Register the routes for the objects of the controller.
27 */
28 public function register_routes() {
29 register_rest_route(
30 $this->namespace,
31 '/' . $this->rest_base,
32 [
33 [
34 'methods' => WP_REST_Server::READABLE,
35 'callback' => [ $this, 'get_inventory_products' ],
36 'args' => $this->get_collection_params(),
37 'permission_callback' => function ( $request ) {
38 return current_user_can( 'erp_ac_manager' );
39 },
40 ],
41 [
42 'methods' => WP_REST_Server::CREATABLE,
43 'callback' => [ $this, 'create_inventory_product' ],
44 'args' => $this->get_collection_params(),
45 'permission_callback' => function ( $request ) {
46 return current_user_can( 'erp_ac_manager' );
47 },
48 ],
49 'schema' => [ $this, 'get_public_item_schema' ],
50 ]
51 );
52
53 register_rest_route(
54 $this->namespace,
55 '/' . $this->rest_base . '/(?P<id>[\d]+)',
56 [
57 [
58 'methods' => WP_REST_Server::READABLE,
59 'callback' => [ $this, 'get_inventory_product' ],
60 'args' => [
61 'context' => $this->get_context_param( [ 'default' => 'view' ] ),
62 ],
63 'permission_callback' => function ( $request ) {
64 return current_user_can( 'erp_ac_manager' );
65 },
66 ],
67 [
68 'methods' => WP_REST_Server::EDITABLE,
69 'callback' => [ $this, 'update_inventory_product' ],
70 'args' => $this->get_collection_params(),
71 'permission_callback' => function ( $request ) {
72 return current_user_can( 'erp_ac_manager' );
73 },
74 ],
75 [
76 'methods' => WP_REST_Server::DELETABLE,
77 'callback' => [ $this, 'delete_inventory_product' ],
78 'permission_callback' => function ( $request ) {
79 return current_user_can( 'erp_ac_manager' );
80 },
81 ],
82 'schema' => [ $this, 'get_public_item_schema' ],
83 ]
84 );
85
86 register_rest_route(
87 $this->namespace,
88 '/' . $this->rest_base . '/delete/(?P<ids>[\d,?]+)',
89 [
90 [
91 'methods' => WP_REST_Server::DELETABLE,
92 'callback' => [ $this, 'bulk_delete' ],
93 'args' => [
94 'ids' => [ 'required' => true ],
95 ],
96 'permission_callback' => function ( $request ) {
97 return current_user_can( 'erp_ac_manager' );
98 },
99 ],
100 'schema' => [ $this, 'get_public_item_schema' ],
101 ]
102 );
103
104 register_rest_route(
105 $this->namespace,
106 '/' . $this->rest_base . '/types',
107 [
108 [
109 'methods' => WP_REST_Server::READABLE,
110 'callback' => [ $this, 'get_product_types' ],
111 'args' => [
112 'context' => $this->get_context_param( [ 'default' => 'view' ] ),
113 ],
114 'permission_callback' => function ( $request ) {
115 return current_user_can( 'erp_ac_manager' );
116 },
117 ],
118 ]
119 );
120 }
121
122 /**
123 * Get a collection of inventory_products
124 *
125 * @param WP_REST_Request $request
126 *
127 * @return WP_Error|WP_REST_Response
128 */
129 public function get_inventory_products( $request ) {
130 $args = [
131 'number' => ! empty( $request['number'] ) ? (int) $request['number'] : 20,
132 'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ),
133 ];
134
135 $formatted_items = [];
136 $additional_fields = [];
137
138 $additional_fields['namespace'] = $this->namespace;
139 $additional_fields['rest_base'] = $this->rest_base;
140
141 $product_data = erp_acct_get_all_products( $args );
142 $total_items = erp_acct_get_all_products(
143 [
144 'count' => true,
145 'number' => -1,
146 ]
147 );
148
149 foreach ( $product_data as $item ) {
150 $data = $this->prepare_item_for_response( $item, $request, $additional_fields );
151 $formatted_items[] = $this->prepare_response_for_collection( $data );
152 }
153
154 $response = rest_ensure_response( $formatted_items );
155 $response = $this->format_collection_response( $response, $request, $total_items );
156 $response->set_status( 200 );
157
158 return $response;
159 }
160
161 /**
162 * Get a specific inventory product
163 *
164 * @param WP_REST_Request $request
165 *
166 * @return WP_Error|WP_REST_Response
167 */
168 public function get_inventory_product( $request ) {
169 $id = (int) $request['id'];
170 $item = erp_acct_get_product( $id );
171
172 if ( empty( $id ) ) {
173 return new WP_Error( 'rest_inventory_product_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
174 }
175
176 $additional_fields['namespace'] = $this->namespace;
177 $additional_fields['rest_base'] = $this->rest_base;
178 $item = $this->prepare_item_for_response( $item, $request, $additional_fields );
179 $response = rest_ensure_response( $item );
180
181 $response->set_status( 200 );
182
183 return $response;
184 }
185
186 /**
187 * Create an inventory product
188 *
189 * @param WP_REST_Request $request
190 *
191 * @return WP_Error|WP_REST_Request
192 */
193 public function create_inventory_product( $request ) {
194 $item = $this->prepare_item_for_database( $request );
195
196 $id = erp_acct_insert_product( $item );
197 $item['id'] = $id;
198
199 $additional_fields['namespace'] = $this->namespace;
200 $additional_fields['rest_base'] = $this->rest_base;
201
202 $response = $this->prepare_item_for_response( $item, $request, $additional_fields );
203 $response = rest_ensure_response( $response );
204 $response->set_status( 201 );
205
206 return $response;
207 }
208
209 /**
210 * Update an inventory product
211 *
212 * @param WP_REST_Request $request
213 *
214 * @return WP_Error|WP_REST_Request
215 */
216 public function update_inventory_product( $request ) {
217 $id = (int) $request['id'];
218
219 if ( empty( $id ) ) {
220 return new WP_Error( 'rest_payment_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
221 }
222
223 $item = $this->prepare_item_for_database( $request );
224
225 $id = erp_acct_update_product( $item, $id );
226 $item['id'] = $id;
227
228 $additional_fields['namespace'] = $this->namespace;
229 $additional_fields['rest_base'] = $this->rest_base;
230
231 $response = $this->prepare_item_for_response( $item, $request, $additional_fields );
232 $response = rest_ensure_response( $response );
233 $response->set_status( 200 );
234
235 return $response;
236 }
237
238 /**
239 * Delete an inventory product
240 *
241 * @param WP_REST_Request $request
242 *
243 * @return WP_Error|WP_REST_Request
244 */
245 public function delete_inventory_product( $request ) {
246 $id = (int) $request['id'];
247
248 erp_acct_delete_product( $id );
249
250 return new WP_REST_Response( true, 204 );
251 }
252
253 /**
254 * Prepare a single item for create or update
255 *
256 * @param WP_REST_Request $request request object
257 *
258 * @return array $prepared_item
259 */
260 protected function prepare_item_for_database( $request ) {
261 $prepared_item = [];
262 // required arguments.
263 if ( isset( $request['name'] ) ) {
264 $prepared_item['name'] = $request['name'];
265 }
266
267 if ( isset( $request['product_type_id'] ) ) {
268 $prepared_item['product_type_id'] = $request['product_type_id']['id'];
269 }
270
271 if ( isset( $request['category_id'] ) ) {
272 $prepared_item['category_id'] = $request['category_id']['id'];
273 }
274
275 if ( isset( $request['tax_cat_id'] ) ) {
276 $prepared_item['tax_cat_id'] = $request['tax_cat_id']['id'];
277 }
278
279 if ( isset( $request['vendor'] ) ) {
280 $prepared_item['vendor'] = $request['vendor']['id'];
281 }
282
283 if ( isset( $request['cost_price'] ) ) {
284 $prepared_item['cost_price'] = $request['cost_price'];
285 }
286
287 if ( isset( $request['sale_price'] ) ) {
288 $prepared_item['sale_price'] = $request['sale_price'];
289 }
290
291 return $prepared_item;
292 }
293
294 /**
295 * Prepare a single user output for response
296 *
297 * @param array|object $item
298 * @param WP_REST_Request $request request object
299 * @param array $additional_fields (optional)
300 *
301 * @return WP_REST_Response $response response data
302 */
303 public function prepare_item_for_response( $item, $request, $additional_fields = [] ) {
304 $item = (object) $item;
305
306 $data = [
307 'id' => $item->id,
308 'name' => $item->name,
309 'product_type_id' => $item->product_type_id,
310 'product_type_name' => $item->product_type_name,
311 'category_id' => $item->category_id,
312 'tax_cat_id' => $item->tax_cat_id,
313 'vendor' => $item->vendor,
314 'cost_price' => $item->cost_price,
315 'sale_price' => $item->sale_price,
316 'vendor_name' => $item->vendor_name,
317 'cat_name' => $item->cat_name,
318 'tax_cat_name' => erp_acct_get_tax_category_by_id( $item->tax_cat_id ),
319 ];
320
321 $data = array_merge( $data, $additional_fields );
322
323 // Wrap the data in a response object
324 $response = rest_ensure_response( $data );
325
326 $response = $this->add_links( $response, $item, $additional_fields );
327
328 return $response;
329 }
330
331 /**
332 * Get the User's schema, conforming to JSON Schema
333 *
334 * @return array
335 */
336 public function get_item_schema() {
337 $schema = [
338 '$schema' => 'http://json-schema.org/draft-04/schema#',
339 'title' => 'erp_inv_product',
340 'type' => 'object',
341 'properties' => [
342 'id' => [
343 'description' => __( 'Unique identifier for the resource.' ),
344 'type' => 'integer',
345 'context' => [ 'embed', 'view', 'edit' ],
346 'readonly' => true,
347 ],
348 'name' => [
349 'description' => __( 'Name for the resource.' ),
350 'type' => 'string',
351 'context' => [ 'edit' ],
352 'arg_options' => [
353 'sanitize_callback' => 'sanitize_text_field',
354 ],
355 'required' => true,
356 ],
357 'product_type_id' => [
358 'description' => __( 'State for the resource.', 'erp' ),
359 'type' => 'object',
360 'context' => [ 'view', 'edit' ],
361 'properties' => [
362 'id' => [
363 'description' => __( 'Unique identifier for the resource.', 'erp' ),
364 'type' => 'integer',
365 'context' => [ 'view', 'edit' ],
366 'required' => true,
367 ],
368 'name' => [
369 'description' => __( 'Type name for the resource.', 'erp' ),
370 'type' => 'string',
371 'context' => [ 'view', 'edit' ],
372 'arg_options' => [
373 'sanitize_callback' => 'sanitize_text_field',
374 ],
375 ],
376 ],
377 ],
378 'category_id' => [
379 'description' => __( 'Category id for the resource.', 'erp' ),
380 'type' => 'object',
381 'context' => [ 'view', 'edit' ],
382 'properties' => [
383 'id' => [
384 'description' => __( 'Unique identifier for the resource.', 'erp' ),
385 'type' => 'integer',
386 'context' => [ 'view', 'edit' ],
387 ],
388 'name' => [
389 'description' => __( 'Type name for the resource.', 'erp' ),
390 'type' => 'string',
391 'context' => [ 'view', 'edit' ],
392 ],
393 'parent' => [
394 'description' => __( 'Parent category for the resource.', 'erp' ),
395 'type' => 'integer',
396 'context' => [ 'view', 'edit' ],
397 ],
398 ],
399 ],
400 'tax_cat_id' => [
401 'description' => __( 'Tax category id for the resource.', 'erp' ),
402 'type' => 'object',
403 'context' => [ 'view', 'edit' ],
404 'properties' => [
405 'id' => [
406 'description' => __( 'Unique identifier for the resource.', 'erp' ),
407 'type' => 'integer',
408 'context' => [ 'view', 'edit' ],
409 ],
410 'name' => [
411 'description' => __( 'Tax category name for the resource.', 'erp' ),
412 'type' => 'string',
413 'context' => [ 'view', 'edit' ],
414 ],
415 'description' => [
416 'description' => __( 'Description for the resource.', 'erp' ),
417 'type' => 'integer',
418 'context' => [ 'view', 'edit' ],
419 ],
420 ],
421 ],
422 'vendor' => [
423 'description' => __( 'Vendor for the resource.', 'erp' ),
424 'type' => 'object',
425 'context' => [ 'view', 'edit' ],
426 'properties' => [
427 'id' => [
428 'description' => __( 'Unique identifier for the resource.', 'erp' ),
429 'type' => 'integer',
430 'context' => [ 'view', 'edit' ],
431 'required' => false,
432 ],
433 'name' => [
434 'description' => __( 'Name for the resource.', 'erp' ),
435 'type' => 'string',
436 'context' => [ 'view', 'edit' ],
437 'arg_options' => [
438 'sanitize_callback' => 'sanitize_text_field',
439 ],
440 ],
441 ],
442 ],
443 'cost_price' => [
444 'description' => __( 'Cost price for the resource.' ),
445 'type' => 'number',
446 'context' => [ 'embed', 'view', 'edit' ],
447 'readonly' => true,
448 ],
449 'sale_price' => [
450 'description' => __( 'Sale price for the resource.' ),
451 'type' => 'number',
452 'context' => [ 'embed', 'view', 'edit' ],
453 'readonly' => true,
454 ],
455 ],
456 ];
457
458 return $schema;
459 }
460
461 /**
462 * Get product type
463 *
464 * @return object
465 */
466 public function get_product_types() {
467 $types = erp_acct_get_product_types();
468 $response = rest_ensure_response( $types );
469
470 return $response;
471 }
472
473 /**
474 * Bulk delete action
475 *
476 * @param object $request
477 *
478 * @return object
479 */
480 public function bulk_delete( $request ) {
481 $ids = $request['ids'];
482 $ids = explode( ',', $ids );
483
484 if ( ! $ids ) {
485 return;
486 }
487
488 foreach ( $ids as $id ) {
489 erp_acct_delete_product( $id );
490 }
491
492 return new WP_REST_Response( true, 204 );
493 }
494 }
495