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

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

505 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 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly
11 }
12
13 class People_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/people';
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_all_people' ],
40 'args' => [],
41 'permission_callback' => function ( $request ) {
42 return current_user_can( 'erp_ac_view_expense' );
43 },
44 ],
45 ]
46 );
47
48 register_rest_route(
49 $this->namespace,
50 '/' . $this->rest_base . '/(?P<id>[\d]+)',
51 [
52 [
53 'methods' => WP_REST_Server::READABLE,
54 'callback' => [ $this, 'get_people' ],
55 'args' => [],
56 'permission_callback' => function ( $request ) {
57 return current_user_can( 'erp_ac_view_expense' );
58 },
59 ],
60 ]
61 );
62
63 register_rest_route(
64 $this->namespace,
65 '/' . $this->rest_base . '/(?P<id>[\d]+)' . '/address',
66 [
67 [
68 'methods' => WP_REST_Server::READABLE,
69 'callback' => [ $this, 'get_people_address' ],
70 'args' => [],
71 'permission_callback' => function ( $request ) {
72 return current_user_can( 'erp_ac_view_expense' );
73 },
74 ],
75 ]
76 );
77
78 register_rest_route(
79 $this->namespace,
80 '/' . $this->rest_base . '/(?P<id>[\d]+)' . '/opening-balance',
81 [
82 [
83 'methods' => WP_REST_Server::READABLE,
84 'callback' => [ $this, 'get_opening_balance' ],
85 'args' => [],
86 'permission_callback' => function ( $request ) {
87 return current_user_can( 'erp_ac_view_expense' );
88 },
89 ],
90 ]
91 );
92
93 register_rest_route(
94 $this->namespace,
95 '/' . $this->rest_base . '/check-email',
96 [
97 [
98 'methods' => WP_REST_Server::READABLE,
99 'callback' => [ $this, 'check_people_email' ],
100 'args' => [],
101 'permission_callback' => function ( $request ) {
102 return current_user_can( 'erp_ac_view_expense' );
103 },
104 ],
105 ]
106 );
107 }
108
109 /**
110 * Get all people
111 *
112 * @return array
113 */
114 public function get_all_people( $request ) {
115 $args = [
116 'number' => ! empty( $request['per_page'] ) ? $request['per_page'] : 20,
117 'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ),
118 'type' => ! empty( $request['type'] ) ? $request['type'] : [ 'customer', 'employee', 'vendor' ],
119 's' => ! empty( $request['search'] ) ? $request['search'] : '',
120 ];
121
122 $items = erp_get_peoples( $args );
123 $total_items = erp_get_peoples(
124 [
125 'type' => $args['type'],
126 'count' => true,
127 ]
128 );
129 $total_items = is_array( $total_items ) ? count( $total_items ) : $total_items;
130
131 $formatted_items = [];
132 $additional_fields = [];
133
134 $additional_fields['namespace'] = $this->namespace;
135 $additional_fields['rest_base'] = $this->rest_base;
136
137 foreach ( $items as $item ) {
138 if ( isset( $request['include'] ) ) {
139 $include_params = explode( ',', str_replace( ' ', '', $request['include'] ) );
140
141 if ( in_array( 'owner', $include_params, true ) ) {
142 $customer_owner_id = ( $item->user_id ) ? get_user_meta( $item->user_id, 'contact_owner', true ) : erp_people_get_meta( $item->id, 'contact_owner', true );
143
144 $item->owner = $this->get_user( $customer_owner_id );
145 $additional_fields = [ 'owner' => $item->owner ];
146 }
147 }
148
149 $data = $this->prepare_item_for_response( $item, $request, $additional_fields );
150 $formatted_items[] = $this->prepare_response_for_collection( $data );
151 }
152
153 $response = rest_ensure_response( $formatted_items );
154 $response = $this->format_collection_response( $response, $request, $total_items );
155
156 $response->set_status( 200 );
157
158 return $response;
159 }
160
161 /**
162 * Return formatted data of a people
163 *
164 * @param $id
165 *
166 * @return string
167 */
168 public function get_people( $request ) {
169 $id = (int) $request['id'];
170
171 if ( empty( $id ) ) {
172 return new WP_Error( 'rest_people_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
173 }
174
175 $people = erp_get_people( $id );
176
177 $people->{'state'} = erp_get_state_name( $people->country, $people->state );
178 $people->{'country'} = erp_get_country_name( $people->country );
179
180 return $people;
181 }
182
183 /**
184 * Return formatted address of a people
185 *
186 * @param $id
187 *
188 * @return string
189 */
190 public function get_people_address( $request ) {
191 global $wpdb;
192
193 $id = (int) $request['id'];
194
195 if ( empty( $id ) ) {
196 return new WP_Error( 'rest_people_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
197 }
198
199 $row = $wpdb->get_row( $wpdb->prepare( "SELECT street_1, street_2, city, state, postal_code, country FROM {$wpdb->prefix}erp_peoples WHERE id = %d", $id ), ARRAY_A );
200
201 return new WP_REST_Response( erp_acct_format_people_address( $row ), 200 );
202 }
203
204 /**
205 * Get opening balance of a people in a date range
206 *
207 * @param WP_REST_Request $request
208 *
209 * @return WP_Error|WP_REST_Response
210 */
211 public function get_opening_balance( $request ) {
212 $id = (int) $request['id'];
213 $args['people_id'] = $id;
214
215 $transactions = erp_acct_get_people_opening_balance( $args );
216
217 return new WP_REST_Response( $transactions, 200 );
218 }
219
220 /**
221 * Check people email existance
222 *
223 * @param WP_REST_Request $request
224 *
225 * @return WP_Error|WP_REST_Response
226 */
227 public function check_people_email( $request ) {
228 $res = erp_acct_check_people_exists( $request['email'] );
229
230 $response = rest_ensure_response( $res );
231 $response->set_status( 200 );
232
233 return $response;
234 }
235
236 /**
237 * Prepare a single item for create or update
238 *
239 * @param WP_REST_Request $request request object
240 *
241 * @return array $prepared_item
242 */
243 protected function prepare_item_for_database( $request ) {
244 $prepared_item = [];
245 // required arguments.
246 if ( isset( $request['first_name'] ) ) {
247 $prepared_item['first_name'] = $request['first_name'];
248 }
249
250 if ( isset( $request['last_name'] ) ) {
251 $prepared_item['last_name'] = $request['last_name'];
252 }
253
254 if ( isset( $request['email'] ) ) {
255 $prepared_item['email'] = $request['email'];
256 }
257
258 // optional arguments.
259 if ( isset( $request['id'] ) ) {
260 $prepared_item['id'] = absint( $request['id'] );
261 }
262
263 if ( isset( $request['phone'] ) ) {
264 $prepared_item['phone'] = $request['phone'];
265 }
266
267 if ( isset( $request['website'] ) ) {
268 $prepared_item['website'] = $request['website'];
269 }
270
271 if ( isset( $request['other'] ) ) {
272 $prepared_item['other'] = $request['other'];
273 }
274
275 if ( isset( $request['notes'] ) ) {
276 $prepared_item['notes'] = $request['notes'];
277 }
278
279 if ( isset( $request['street_1'] ) ) {
280 $prepared_item['street_1'] = $request['street_1'];
281 }
282
283 if ( isset( $request['street_2'] ) ) {
284 $prepared_item['street_2'] = $request['street_2'];
285 }
286
287 if ( isset( $request['city'] ) ) {
288 $prepared_item['city'] = $request['city'];
289 }
290
291 if ( isset( $request['state'] ) ) {
292 $prepared_item['state'] = $request['state']['id'];
293 }
294
295 if ( isset( $request['postal_code'] ) ) {
296 $prepared_item['postal_code'] = $request['postal_code'];
297 }
298
299 if ( isset( $request['country'] ) ) {
300 $prepared_item['country'] = $request['country']['id'];
301 }
302
303 if ( isset( $request['company'] ) ) {
304 $prepared_item['company'] = $request['company'];
305 }
306
307 if ( isset( $request['mobile'] ) ) {
308 $prepared_item['mobile'] = $request['mobile'];
309 }
310
311 if ( $request['fax'] ) {
312 $prepared_item['fax'] = $request['fax'];
313 }
314
315 $prepared_item['type'] = 'customer';
316
317 return $prepared_item;
318 }
319
320 /**
321 * Prepare a single user output for response
322 *
323 * @param array|object $item
324 * @param WP_REST_Request $request request object
325 * @param array $additional_fields (optional)
326 *
327 * @return WP_REST_Response $response response data
328 */
329 public function prepare_item_for_response( $item, $request, $additional_fields = [] ) {
330 $item = (object) $item;
331
332 $data = [
333 'id' => (int) $item->id,
334 'first_name' => $item->first_name,
335 'last_name' => $item->last_name,
336 'name' => $item->first_name . ' ' . $item->last_name,
337 'email' => $item->email,
338 'phone' => $item->phone,
339 'mobile' => $item->mobile,
340 'fax' => $item->fax,
341 'website' => $item->website,
342 'notes' => $item->notes,
343 'other' => $item->other,
344 'company' => $item->company,
345 'billing' => [
346 'first_name' => $item->first_name,
347 'last_name' => $item->last_name,
348 'street_1' => $item->street_1,
349 'street_2' => $item->street_2,
350 'city' => $item->city,
351 'state' => $item->state,
352 'postal_code' => $item->postal_code,
353 'country' => $item->country,
354 'email' => $item->email,
355 'phone' => $item->phone,
356 ],
357 ];
358
359 $data = array_merge( $data, $additional_fields );
360
361 // Wrap the data in a response object
362 $response = rest_ensure_response( $data );
363
364 $response = $this->add_links( $response, $item, $additional_fields );
365
366 return $response;
367 }
368
369 /**
370 * Get the User's schema, conforming to JSON Schema
371 *
372 * @return array
373 */
374 public function get_item_schema() {
375 $schema = [
376 '$schema' => 'http://json-schema.org/draft-04/schema#',
377 'title' => 'customer',
378 'type' => 'object',
379 'properties' => [
380 'id' => [
381 'description' => __( 'Unique identifier for the resource.' ),
382 'type' => 'integer',
383 'context' => [ 'embed', 'view', 'edit' ],
384 'readonly' => true,
385 ],
386 'first_name' => [
387 'description' => __( 'First name for the resource.' ),
388 'type' => 'string',
389 'context' => [ 'edit' ],
390 'arg_options' => [
391 'sanitize_callback' => 'sanitize_text_field',
392 ],
393 'required' => true,
394 ],
395 'last_name' => [
396 'description' => __( 'Last name for the resource.' ),
397 'type' => 'string',
398 'context' => [ 'edit' ],
399 'arg_options' => [
400 'sanitize_callback' => 'sanitize_text_field',
401 ],
402 'required' => true,
403 ],
404 'email' => [
405 'description' => __( 'The email address for the resource.' ),
406 'type' => 'string',
407 'format' => 'email',
408 'context' => [ 'edit' ],
409 'required' => true,
410 ],
411 'phone' => [
412 'description' => __( 'Phone for the resource.' ),
413 'type' => 'string',
414 'context' => [ 'edit' ],
415 'arg_options' => [
416 'sanitize_callback' => 'sanitize_text_field',
417 ],
418 ],
419 'other' => [
420 'description' => __( 'Other for the resource.' ),
421 'type' => 'string',
422 'context' => [ 'edit' ],
423 'arg_options' => [
424 'sanitize_callback' => 'sanitize_text_field',
425 ],
426 ],
427 'website' => [
428 'description' => __( 'Website of the resource.' ),
429 'type' => 'string',
430 'format' => 'uri',
431 'context' => [ 'embed', 'view', 'edit' ],
432 ],
433 'notes' => [
434 'description' => __( 'Notes of the resource.' ),
435 'type' => 'string',
436 'context' => [ 'embed', 'view', 'edit' ],
437 'arg_options' => [
438 'sanitize_callback' => 'sanitize_text_field',
439 ],
440 ],
441 'billing' => [
442 'description' => __( 'List of billing address data.', 'erp' ),
443 'type' => 'object',
444 'context' => [ 'view', 'edit' ],
445 'properties' => [
446 'first_name' => [
447 'description' => __( 'First name.', 'erp' ),
448 'type' => 'string',
449 'context' => [ 'view', 'edit' ],
450 ],
451 'last_name' => [
452 'description' => __( 'Last name.', 'erp' ),
453 'type' => 'string',
454 'context' => [ 'view', 'edit' ],
455 ],
456 'street_1' => [
457 'description' => __( 'Address line 1', 'erp' ),
458 'type' => 'string',
459 'context' => [ 'view', 'edit' ],
460 ],
461 'street_2' => [
462 'description' => __( 'Address line 2', 'erp' ),
463 'type' => 'string',
464 'context' => [ 'view', 'edit' ],
465 ],
466 'city' => [
467 'description' => __( 'City name.', 'erp' ),
468 'type' => 'string',
469 'context' => [ 'view', 'edit' ],
470 ],
471 'state' => [
472 'description' => __( 'ISO code or name of the state, province or district.', 'erp' ),
473 'type' => 'string',
474 'context' => [ 'view', 'edit' ],
475 ],
476 'postal_code' => [
477 'description' => __( 'Postal code.', 'erp' ),
478 'type' => 'string',
479 'context' => [ 'view', 'edit' ],
480 ],
481 'country' => [
482 'description' => __( 'ISO code of the country.', 'erp' ),
483 'type' => 'string',
484 'context' => [ 'view', 'edit' ],
485 ],
486 'email' => [
487 'description' => __( 'The email address for the resource.' ),
488 'type' => 'string',
489 'format' => 'email',
490 'context' => [ 'edit' ],
491 ],
492 'phone' => [
493 'description' => __( 'Phone for the resource.' ),
494 'type' => 'string',
495 'context' => [ 'edit' ],
496 ],
497 ],
498 ],
499 ],
500 ];
501
502 return $schema;
503 }
504 }
505