PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.3.13
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.3.13
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 / includes / api / class-customers-controller.php

class-customers-controller.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.3.13, at includes/api/class-customers-controller.php

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