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-customers.php

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

768 lines 26.6 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 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly
11 }
12
13 class Customers_Controller extends \WeDevs\ERP\API\REST_Controller {
14
15 /**
16 * Endpoint namespace.
17 *
18 * @var string
19 */
20 protected $namespace = 'erp/v1';
21
22 /**
23 * Route base.
24 *
25 * @var string
26 */
27 protected $rest_base = 'accounting/v1/customers';
28
29 /**
30 * Register the routes for the objects of the controller.
31 */
32 public function register_routes() {
33 register_rest_route(
34 $this->namespace,
35 '/' . $this->rest_base,
36 [
37 [
38 'methods' => WP_REST_Server::READABLE,
39 'callback' => [ $this, 'get_customers' ],
40 'args' => $this->get_collection_params(),
41 'permission_callback' => function ( $request ) {
42 return current_user_can( 'erp_ac_view_customer' );
43 },
44 ],
45 [
46 'methods' => WP_REST_Server::CREATABLE,
47 'callback' => [ $this, 'create_customer' ],
48 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
49 'permission_callback' => function ( $request ) {
50 return current_user_can( 'erp_ac_create_customer' );
51 },
52 ],
53 'schema' => [ $this, 'get_item_schema' ],
54 ]
55 );
56
57 register_rest_route(
58 $this->namespace,
59 '/' . $this->rest_base . '/(?P<id>[\d]+)',
60 [
61 [
62 'methods' => WP_REST_Server::READABLE,
63 'callback' => [ $this, 'get_customer' ],
64 'args' => [
65 'context' => $this->get_context_param( [ 'default' => 'view' ] ),
66 ],
67 'permission_callback' => function ( $request ) {
68 return current_user_can( 'erp_ac_view_customer' );
69 },
70 ],
71 [
72 'methods' => WP_REST_Server::EDITABLE,
73 'callback' => [ $this, 'update_customer' ],
74 'args' => $this->get_collection_params(),
75 'permission_callback' => function ( $request ) {
76 return current_user_can( 'erp_ac_edit_customer' );
77 },
78 ],
79 [
80 'methods' => WP_REST_Server::DELETABLE,
81 'callback' => [ $this, 'delete_customer' ],
82 'permission_callback' => function ( $request ) {
83 return current_user_can( 'erp_ac_delete_customer' );
84 },
85 ],
86 'schema' => [ $this, 'get_item_schema' ],
87 ]
88 );
89
90 register_rest_route(
91 $this->namespace,
92 '/' . $this->rest_base . '/delete/(?P<ids>[\d,?]+)',
93 [
94 [
95 'methods' => WP_REST_Server::DELETABLE,
96 'callback' => [ $this, 'bulk_delete_customers' ],
97 'args' => [
98 'ids' => [ 'required' => true ],
99 ],
100 'permission_callback' => function ( $request ) {
101 return current_user_can( 'erp_ac_delete_customer' );
102 },
103 ],
104 'schema' => [ $this, 'get_item_schema' ],
105 ]
106 );
107
108 register_rest_route(
109 $this->namespace,
110 '/' . $this->rest_base . '/(?P<id>[\d]+)' . '/transactions',
111 [
112 [
113 'methods' => WP_REST_Server::READABLE,
114 'callback' => [ $this, 'get_transactions' ],
115 'args' => $this->get_collection_params(),
116 'permission_callback' => function ( $request ) {
117 return current_user_can( 'erp_ac_view_customer' );
118 },
119 ],
120 ]
121 );
122 register_rest_route(
123 $this->namespace,
124 '/' . $this->rest_base . '/(?P<id>[\d]+)' . '/transactions/filter',
125 [
126 [
127 'methods' => WP_REST_Server::READABLE,
128 'callback' => [ $this, 'filter_transactions' ],
129 'args' => $this->get_collection_params(),
130 'permission_callback' => function ( $request ) {
131 return current_user_can( 'erp_ac_view_customer' );
132 },
133 ],
134 ]
135 );
136
137 register_rest_route(
138 $this->namespace,
139 '/' . $this->rest_base . '/country',
140 [
141 [
142 'methods' => WP_REST_Server::READABLE,
143 'callback' => [ $this, 'get_countries' ],
144 'args' => $this->get_collection_params(),
145 'permission_callback' => function ( $request ) {
146 return current_user_can( 'erp_ac_view_customer' );
147 },
148 ],
149 ]
150 );
151 }
152
153 /**
154 * Get a collection of customers
155 *
156 * @param WP_REST_Request $request
157 *
158 * @return WP_Error|WP_REST_Response
159 */
160 public function get_customers( $request ) {
161 $args = [
162 'number' => $request['per_page'],
163 'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ),
164 'type' => 'customer',
165 's' => ! empty( $request['search'] ) ? $request['search'] : '',
166 ];
167
168 $items = erp_acct_get_accounting_people( $args );
169 $total_items = erp_acct_get_accounting_people(
170 [ 'type' => 'customer', 's' => $args['s'], 'count' => true ]
171 );
172 $total_items = is_array( $total_items ) ? count( $total_items ) : $total_items;
173
174 $formatted_items = [];
175 $additional_fields = [];
176
177 $additional_fields['namespace'] = $this->namespace;
178 $additional_fields['rest_base'] = $this->rest_base;
179
180 foreach ( $items as $item ) {
181 $photo_id = erp_people_get_meta( $item->id, 'photo_id', true );
182
183 $item->{'photo_id'} = $photo_id;
184 $item->{'photo'} = wp_get_attachment_thumb_url( $photo_id );
185
186 if ( isset( $request['include'] ) ) {
187 $include_params = explode( ',', str_replace( ' ', '', $request['include'] ) );
188
189 if ( in_array( 'owner', $include_params, true ) ) {
190 $customer_owner_id = ( $item->user_id ) ? get_user_meta( $item->user_id, 'contact_owner', true ) : erp_people_get_meta( $item->id, 'contact_owner', true );
191
192 $item->owner = $this->get_user( $customer_owner_id );
193 $additional_fields = [ 'owner' => $item->owner ];
194 }
195 }
196
197 $data = $this->prepare_item_for_response( $item, $request, $additional_fields );
198 $formatted_items[] = $this->prepare_response_for_collection( $data );
199 }
200
201 $response = rest_ensure_response( $formatted_items );
202 $response = $this->format_collection_response( $response, $request, $total_items );
203
204 $response->set_status( 200 );
205
206 return $response;
207 }
208
209 /**
210 * Get a specific customer
211 *
212 * @param WP_REST_Request $request
213 *
214 * @return WP_Error|WP_REST_Response
215 */
216 public function get_customer( $request ) {
217 $id = (int) $request['id'];
218 $item = erp_get_people( $id );
219 $item = (array) $item;
220
221 if ( empty( $id ) || empty( $item['id'] ) ) {
222 return new WP_Error( 'rest_customer_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
223 }
224
225 $photo_id = erp_people_get_meta( $id, 'photo_id', true );
226
227 $item['photo_id'] = $photo_id;
228 $item['photo'] = wp_get_attachment_thumb_url( $photo_id );
229
230 $additional_fields = [];
231
232 if ( isset( $request['include'] ) ) {
233 $include_params = explode( ',', str_replace( ' ', '', $request['include'] ) );
234
235 if ( in_array( 'owner', $include_params, true ) ) {
236 $customer_owner_id = ( $item->user_id ) ? get_user_meta( $item->user_id, 'contact_owner', true ) : erp_people_get_meta( $item->id, 'contact_owner', true );
237
238 $item->owner = $this->get_user( $customer_owner_id );
239 $additional_fields = [ 'owner' => $item->owner ];
240 }
241 }
242
243 $additional_fields['namespace'] = $this->namespace;
244 $additional_fields['rest_base'] = $this->rest_base;
245 $item = $this->prepare_item_for_response( $item, $request, $additional_fields );
246 $response = rest_ensure_response( $item );
247
248 $response->set_status( 200 );
249
250 return $response;
251 }
252
253 /**
254 * Create a customer
255 *
256 * @param WP_REST_Request $request
257 *
258 * @return WP_Error|WP_REST_Request
259 */
260 public function create_customer( $request ) {
261 if ( erp_acct_check_people_exists( $request['email'] ) ) {
262 return new WP_Error( 'rest_customer_invalid_id', __( 'Email already exists!' ), [ 'status' => 400 ] );
263 }
264
265 $item = $this->prepare_item_for_database( $request );
266
267 $id = erp_insert_people( $item );
268
269 $customer = (array) erp_get_people( $id );
270 $customer['id'] = $id;
271
272 $this->add_log( $customer, 'add' );
273
274 $additional_fields['namespace'] = $this->namespace;
275 $additional_fields['rest_base'] = $this->rest_base;
276
277 $response = $this->prepare_item_for_response( $customer, $request, $additional_fields );
278 $response = rest_ensure_response( $response );
279 $response->set_status( 201 );
280
281 return $response;
282 }
283
284 /**
285 * Update a customer
286 *
287 * @param WP_REST_Request $request
288 *
289 * @return WP_Error|WP_REST_Request
290 */
291 public function update_customer( $request ) {
292 $id = (int) $request['id'];
293
294 $item = erp_get_people( $id );
295
296 if ( ! $item ) {
297 return new WP_Error( 'rest_customer_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
298 }
299
300 $item = $this->prepare_item_for_database( $request );
301
302 $id = erp_insert_people( $item );
303
304 $customer = (array) erp_get_people( $id );
305 $customer['id'] = $id;
306
307 $this->add_log( $customer, 'update' );
308
309 $additional_fields['namespace'] = $this->namespace;
310 $additional_fields['rest_base'] = $this->rest_base;
311
312 $response = $this->prepare_item_for_response( $customer, $request, $additional_fields );
313 $response = rest_ensure_response( $response );
314 $response->set_status( 200 );
315
316 return $response;
317 }
318
319 /**
320 * Delete a customer
321 *
322 * @param WP_REST_Request $request
323 *
324 * @return WP_Error|WP_REST_Request
325 */
326 public function delete_customer( $request ) {
327 $id = (int) $request['id'];
328
329 $exist = erp_acct_check_associated_tranasaction( $id );
330
331 if ( $exist ) {
332 $error = new WP_Error( 'rest_customer_has_trans', __( 'Can not remove! Customer has transactions.' ) );
333
334 wp_send_json_error( $error );
335 }
336
337 $data = [
338 'id' => $id,
339 'hard' => true,
340 'type' => 'customer',
341 ];
342
343 erp_delete_people( $data );
344
345 return new WP_REST_Response( true, 204 );
346 }
347
348 /**
349 * Bulk Delete customers
350 *
351 * @param WP_REST_Request $request
352 *
353 * @return WP_Error|WP_REST_Request
354 */
355 public function bulk_delete_customers( $request ) {
356 $ids = (string) $request['ids'];
357
358 $data = [
359 'id' => explode( ',', $ids ),
360 'hard' => true,
361 'type' => 'customer',
362 ];
363
364 foreach ( $data['id'] as $id ) {
365 $exist = erp_acct_check_associated_tranasaction( $id );
366
367 if ( $exist ) {
368 $error = new WP_Error( 'rest_customer_has_trans', __( 'Can not remove! Customer has transactions.' ) );
369
370 wp_send_json_error( $error );
371 }
372 }
373
374 erp_delete_people( $data );
375
376 return new WP_REST_Response( true, 204 );
377 }
378
379 /**
380 * Get a collection of transactions
381 *
382 * @param WP_REST_Request $request
383 *
384 * @return WP_Error|WP_REST_Response
385 */
386 public function get_transactions( $request ) {
387 $id = (int) $request['id'];
388
389 $args['people_id'] = $id;
390
391 $transactions = erp_acct_get_people_transactions( $args );
392
393 return new WP_REST_Response( $transactions, 200 );
394 }
395
396 /**
397 * Get countries
398 *
399 * @return object
400 */
401 public function get_countries( $request ) {
402 $country = \WeDevs\ERP\Countries::instance();
403 $c = $country->get_countries();
404 $state = $country->get_states();
405 $response = [
406 'country' => $c,
407 'state' => $state,
408 ];
409 $response = rest_ensure_response( $response );
410
411 return $response;
412 }
413
414 /**
415 * Get transaction by date
416 *
417 * @param object $request
418 *
419 * @return array
420 */
421 public function filter_transactions( $request ) {
422 $id = $request['id'];
423 $start_date = $request['start_date'];
424 $end_date = $request['end_date'];
425 $args = [
426 'people_id' => $id,
427 'start_date' => $start_date,
428 'end_date' => $end_date,
429 ];
430 $transactions = erp_acct_get_people_transactions( $args );
431 $response = rest_ensure_response( $transactions );
432
433 return $response;
434 }
435
436 /**
437 * Log when customer insert or update
438 *
439 * @param $data
440 * @param $action
441 */
442 public function add_log( $data, $action ) {
443 erp_log()->add(
444 [
445 'component' => 'Accounting',
446 'sub_component' => __( 'Customer', 'erp' ),
447 'old_value' => '',
448 'new_value' => '',
449 'message' => $data['first_name'] . ' ' . $data['last_name'] . __( ' customer has been created', 'erp' ),
450 'changetype' => $action,
451 'created_by' => get_current_user_id(),
452 ]
453 );
454 }
455
456 /**
457 * Prepare a single item for create or update
458 *
459 * @param WP_REST_Request $request request object
460 *
461 * @return array $prepared_item
462 */
463 protected function prepare_item_for_database( $request ) {
464 $prepared_item = [];
465 // required arguments.
466 if ( isset( $request['first_name'] ) ) {
467 $prepared_item['first_name'] = $request['first_name'];
468 }
469
470 if ( isset( $request['last_name'] ) ) {
471 $prepared_item['last_name'] = $request['last_name'];
472 }
473
474 if ( isset( $request['email'] ) ) {
475 $prepared_item['email'] = $request['email'];
476 }
477
478 // optional arguments.
479 if ( isset( $request['id'] ) ) {
480 $prepared_item['id'] = absint( $request['id'] );
481 }
482
483 if ( isset( $request['photo_id'] ) ) {
484 $prepared_item['photo_id'] = $request['photo_id'];
485 }
486
487 if ( isset( $request['phone'] ) ) {
488 $prepared_item['phone'] = $request['phone'];
489 }
490
491 if ( isset( $request['website'] ) ) {
492 $prepared_item['website'] = $request['website'];
493 }
494
495 if ( isset( $request['other'] ) ) {
496 $prepared_item['other'] = $request['other'];
497 }
498
499 if ( isset( $request['notes'] ) ) {
500 $prepared_item['notes'] = $request['notes'];
501 }
502
503 if ( isset( $request['street_1'] ) ) {
504 $prepared_item['street_1'] = $request['street_1'];
505 }
506
507 if ( isset( $request['street_2'] ) ) {
508 $prepared_item['street_2'] = $request['street_2'];
509 }
510
511 if ( isset( $request['city'] ) ) {
512 $prepared_item['city'] = $request['city'];
513 }
514
515 if ( ! empty( $request['state'] ) ) {
516 $prepared_item['state'] = $request['state']['id'];
517 }
518
519 if ( isset( $request['postal_code'] ) ) {
520 $prepared_item['postal_code'] = $request['postal_code'];
521 }
522
523 if ( ! empty( $request['country'] ) ) {
524 $prepared_item['country'] = $request['country']['id'];
525 }
526
527 if ( isset( $request['company'] ) ) {
528 $prepared_item['company'] = $request['company'];
529 }
530
531 if ( isset( $request['mobile'] ) ) {
532 $prepared_item['mobile'] = $request['mobile'];
533 }
534
535 if ( $request['fax'] ) {
536 $prepared_item['fax'] = $request['fax'];
537 }
538
539 $prepared_item['raw_data'] = json_decode( $request->get_body(), true );
540 $prepared_item['type'] = 'customer';
541
542 return $prepared_item;
543 }
544
545 /**
546 * Prepare a single user output for response
547 *
548 * @param array|object $item
549 * @param WP_REST_Request $request request object
550 * @param array $additional_fields (optional)
551 *
552 * @return WP_REST_Response $response response data
553 */
554 public function prepare_item_for_response( $item, $request, $additional_fields = [] ) {
555 $item = (object) $item;
556
557 $data = [
558 'id' => (int) $item->id,
559 'first_name' => $item->first_name,
560 'last_name' => $item->last_name,
561 'email' => $item->email,
562 'phone' => $item->phone,
563 'mobile' => $item->mobile,
564 'fax' => $item->fax,
565 'website' => $item->website,
566 'notes' => $item->notes,
567 'other' => $item->other,
568 'company' => $item->company,
569 'photo_id' => !empty( $item->photo_id ) ? $item->photo_id : null,
570 'photo' => !empty( $item->photo ) ? $item->photo : null,
571 'billing' => [
572 'first_name' => $item->first_name,
573 'last_name' => $item->last_name,
574 'street_1' => $item->street_1,
575 'street_2' => $item->street_2,
576 'city' => $item->city,
577 'state' => $item->state,
578 'postal_code' => $item->postal_code,
579 'country' => $item->country,
580 'email' => $item->email,
581 'phone' => $item->phone,
582 ],
583 ];
584
585 $data = array_merge( $data, $additional_fields );
586
587 // Wrap the data in a response object
588 $response = rest_ensure_response( $data );
589
590 $response = $this->add_links( $response, $item, $additional_fields );
591
592 return $response;
593 }
594
595 /**
596 * Get the User's schema, conforming to JSON Schema
597 *
598 * @return array
599 */
600 public function get_item_schema() {
601 $schema = [
602 '$schema' => 'http://json-schema.org/draft-04/schema#',
603 'title' => 'customer',
604 'type' => 'object',
605 'properties' => [
606 'id' => [
607 'description' => __( 'Unique identifier for the resource.' ),
608 'type' => 'integer',
609 'context' => [ 'embed', 'view', 'edit' ],
610 'readonly' => true,
611 ],
612 'first_name' => [
613 'description' => __( 'First name for the resource.' ),
614 'type' => 'string',
615 'context' => [ 'view', 'edit' ],
616 'arg_options' => [
617 'sanitize_callback' => 'sanitize_text_field',
618 ],
619 'required' => true,
620 ],
621 'last_name' => [
622 'description' => __( 'Last name for the resource.' ),
623 'type' => 'string',
624 'context' => [ 'view', 'edit' ],
625 'arg_options' => [
626 'sanitize_callback' => 'sanitize_text_field',
627 ],
628 'required' => true,
629 ],
630 'email' => [
631 'description' => __( 'The email address for the resource.' ),
632 'type' => 'string',
633 'format' => 'email',
634 'context' => [ 'view', 'edit' ],
635 'required' => true,
636 ],
637 'mobile' => [
638 'description' => __( 'Mobile number for the resource.' ),
639 'type' => 'string',
640 'context' => [ 'view', 'edit' ],
641 'arg_options' => [
642 'sanitize_callback' => 'sanitize_text_field',
643 ],
644 ],
645 'company' => [
646 'description' => __( 'Company name for the resource.' ),
647 'type' => 'string',
648 'context' => [ 'view', 'edit' ],
649 'arg_options' => [
650 'sanitize_callback' => 'sanitize_text_field',
651 ],
652 ],
653 'phone' => [
654 'description' => __( 'Phone number for the resource.' ),
655 'type' => 'string',
656 'context' => [ 'view', 'edit' ],
657 'arg_options' => [
658 'sanitize_callback' => 'sanitize_text_field',
659 ],
660 ],
661 'website' => [
662 'description' => __( 'Website link of the resource.' ),
663 'type' => 'string',
664 'format' => 'uri',
665 'context' => [ 'embed', 'view', 'edit' ],
666 ],
667 'notes' => [
668 'description' => __( 'Notes of the resource.' ),
669 'type' => 'string',
670 'context' => [ 'view', 'edit' ],
671 'arg_options' => [
672 'sanitize_callback' => 'sanitize_text_field',
673 ],
674 ],
675 'fax' => [
676 'description' => __( 'Fax of the resource.' ),
677 'type' => 'string',
678 'context' => [ 'view', 'edit' ],
679 'arg_options' => [
680 'sanitize_callback' => 'sanitize_text_field',
681 ],
682 ],
683 'street_1' => [
684 'description' => __( 'Stree 1 for the resource.' ),
685 'type' => 'string',
686 'context' => [ 'view', 'edit' ],
687 'arg_options' => [
688 'sanitize_callback' => 'sanitize_text_field',
689 ],
690 ],
691 'street_2' => [
692 'description' => __( 'Stree 2 for the resource.' ),
693 'type' => 'string',
694 'context' => [ 'view', 'edit' ],
695 'arg_options' => [
696 'sanitize_callback' => 'sanitize_text_field',
697 ],
698 ],
699 'city' => [
700 'description' => __( 'City for the resource.' ),
701 'type' => 'string',
702 'context' => [ 'view', 'edit' ],
703 'arg_options' => [
704 'sanitize_callback' => 'sanitize_text_field',
705 ],
706 ],
707 'postal_code' => [
708 'description' => __( 'Zip code for the resource.' ),
709 'type' => 'string',
710 'context' => [ 'view', 'edit' ],
711 'arg_options' => [
712 'sanitize_callback' => 'sanitize_text_field',
713 ],
714 ],
715 'photo_id' => [
716 'description' => __( 'Photo ID for the resource.' ),
717 'type' => 'integer',
718 'context' => [ 'view', 'edit' ],
719 ],
720 'photo' => [
721 'description' => __( 'Photo for the resource.' ),
722 'type' => 'string',
723 'context' => [ 'view', 'edit' ],
724 'arg_options' => [
725 'sanitize_callback' => 'sanitize_text_field',
726 ],
727 ],
728 'country' => [
729 'description' => __( 'List of countries data.', 'erp' ),
730 'type' => [ 'array', 'object' ],
731 'context' => [ 'view', 'edit' ],
732 'properties' => [
733 'id' => [
734 'description' => __( 'Unique identifier for the resource.', 'erp' ),
735 'type' => 'integer',
736 'context' => [ 'view', 'edit' ],
737 ],
738 'name' => [
739 'description' => __( 'Country name for the resource.', 'erp' ),
740 'type' => 'string',
741 'context' => [ 'view', 'edit' ],
742 ],
743 ],
744 ],
745 'state' => [
746 'description' => __( 'State for the resource.', 'erp' ),
747 'type' => [ 'array', 'object' ],
748 'context' => [ 'view', 'edit' ],
749 'properties' => [
750 'id' => [
751 'description' => __( 'Unique identifier for the resource.', 'erp' ),
752 'type' => 'string',
753 'context' => [ 'view', 'edit' ],
754 ],
755 'name' => [
756 'description' => __( 'State name for the resource.', 'erp' ),
757 'type' => 'string',
758 'context' => [ 'view', 'edit' ],
759 ],
760 ],
761 ],
762 ],
763 ];
764
765 return $schema;
766 }
767 }
768