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 / modules / hrm / includes / api / class-employees-controller.php

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

1,698 lines 60.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\HRM\API;
4
5 use WeDevs\ERP\API\REST_Controller;
6 use WeDevs\ERP\HRM\Employee;
7 use WeDevs\ERP\HRM\Models\Department;
8 use WeDevs\ERP\HRM\Models\Designation;
9 use WP_Error;
10 use WP_REST_Response;
11 use WP_REST_Server;
12
13
14 class Employees_Controller extends REST_Controller {
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 = 'hrm/employees';
28
29 /**
30 * Register the routes for the objects of the controller.
31 */
32 public function register_routes() {
33 register_rest_route( $this->namespace, '/' . $this->rest_base, [
34 [
35 'methods' => WP_REST_Server::READABLE,
36 'callback' => [ $this, 'get_employees' ],
37 'args' => $this->get_collection_params(),
38 'permission_callback' => function ( $request ) {
39 return current_user_can( 'erp_view_list' );
40 },
41 ],
42 [
43 'methods' => WP_REST_Server::CREATABLE,
44 'callback' => [ $this, 'create_employee' ],
45 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
46 'permission_callback' => function ( $request ) {
47 return current_user_can( 'erp_create_employee' );
48 },
49 ],
50 'schema' => [ $this, 'get_public_item_schema' ],
51 ] );
52
53 register_rest_route( $this->namespace, '/' . $this->rest_base . '/bulk', [
54 [
55 'methods' => WP_REST_Server::CREATABLE,
56 'callback' => [ $this, 'create_employees' ],
57 'permission_callback' => function ( $request ) {
58 return current_user_can( 'erp_create_employee' );
59 },
60 ],
61 'schema' => [ $this, 'get_public_item_schema' ],
62 ] );
63
64 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)', [
65 [
66 'methods' => WP_REST_Server::READABLE,
67 'callback' => [ $this, 'get_employee' ],
68 'args' => [
69 'context' => $this->get_context_param( [ 'default' => 'view' ] ),
70 ],
71 'permission_callback' => function ( $request ) {
72 return current_user_can( 'erp_list_employee' );
73 },
74 ],
75 [
76 'methods' => WP_REST_Server::EDITABLE,
77 'callback' => [ $this, 'update_employee' ],
78 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
79 'permission_callback' => function ( $request ) {
80 return current_user_can( 'erp_edit_employee', $request['user_id'] );
81 },
82 ],
83 [
84 'methods' => WP_REST_Server::DELETABLE,
85 'callback' => [ $this, 'delete_employee' ],
86 'permission_callback' => function ( $request ) {
87 return current_user_can( 'erp_delete_employee' );
88 },
89 ],
90 'schema' => [ $this, 'get_public_item_schema' ],
91 ] );
92
93 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/experiences', [
94 [
95 'methods' => WP_REST_Server::READABLE,
96 'callback' => [ $this, 'get_experiences' ],
97 'permission_callback' => function ( $request ) {
98 return current_user_can( 'erp_view_experience', $request['user_id'] );
99 },
100 ],
101 [
102 'methods' => WP_REST_Server::CREATABLE,
103 'callback' => [ $this, 'create_experience' ],
104 'permission_callback' => function ( $request ) {
105 return current_user_can( 'erp_create_experience', $request['user_id'] );
106 },
107 ],
108 ] );
109
110
111 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/experiences' . '/(?P<id>[\d]+)', [
112 [
113 'methods' => WP_REST_Server::READABLE,
114 'callback' => [ $this, 'get_experience' ],
115 'permission_callback' => function ( $request ) {
116 return current_user_can( 'erp_view_experience', $request['user_id'] );
117 },
118 ],
119 [
120 'methods' => WP_REST_Server::EDITABLE,
121 'callback' => [ $this, 'update_experience' ],
122 'permission_callback' => function ( $request ) {
123 return current_user_can( 'erp_edit_experience', $request['user_id'] );
124 },
125 ],
126 [
127 'methods' => WP_REST_Server::DELETABLE,
128 'callback' => [ $this, 'delete_experience' ],
129 'permission_callback' => function ( $request ) {
130 return current_user_can( 'erp_delete_experience', $request['user_id'] );
131 },
132 ],
133 ] );
134
135 //education
136 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/educations', [
137 [
138 'methods' => WP_REST_Server::READABLE,
139 'callback' => [ $this, 'get_educations' ],
140 'permission_callback' => function ( $request ) {
141 return current_user_can( 'erp_view_education', $request['user_id'] );
142 },
143 ],
144 [
145 'methods' => WP_REST_Server::CREATABLE,
146 'callback' => [ $this, 'create_education' ],
147 'permission_callback' => function ( $request ) {
148 return current_user_can( 'erp_create_education', $request['user_id'] );
149 },
150 ],
151 ] );
152
153 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/educations' . '/(?P<id>[\d]+)', [
154 [
155 'methods' => WP_REST_Server::EDITABLE,
156 'callback' => [ $this, 'update_education' ],
157 'permission_callback' => function ( $request ) {
158 return current_user_can( 'erp_edit_education', $request['user_id'] );
159 },
160 ],
161 [
162 'methods' => WP_REST_Server::DELETABLE,
163 'callback' => [ $this, 'delete_education' ],
164 'permission_callback' => function ( $request ) {
165 return current_user_can( 'erp_delete_education', $request['user_id'] );
166 },
167 ],
168 ] );
169
170 //dependents
171 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/dependents', [
172 [
173 'methods' => WP_REST_Server::READABLE,
174 'callback' => [ $this, 'get_dependents' ],
175 'permission_callback' => function ( $request ) {
176 return current_user_can( 'erp_view_dependent', $request['user_id'] );
177 },
178 ],
179 [
180 'methods' => WP_REST_Server::CREATABLE,
181 'callback' => [ $this, 'create_dependent' ],
182 'permission_callback' => function ( $request ) {
183 return current_user_can( 'erp_create_dependent', $request['user_id'] );
184 },
185 ],
186 ] );
187
188 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/dependents' . '/(?P<id>[\d]+)', [
189 [
190 'methods' => WP_REST_Server::EDITABLE,
191 'callback' => [ $this, 'update_dependent' ],
192 'permission_callback' => function ( $request ) {
193 return current_user_can( 'erp_edit_dependent', $request['user_id'] );
194 },
195 ],
196 [
197 'methods' => WP_REST_Server::DELETABLE,
198 'callback' => [ $this, 'delete_dependent' ],
199 'permission_callback' => function ( $request ) {
200 return current_user_can( 'erp_delete_dependent', $request['user_id'] );
201 },
202 ],
203 ] );
204
205 //job histories
206 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/job_histories', [
207 [
208 'methods' => WP_REST_Server::READABLE,
209 'callback' => [ $this, 'get_histories' ],
210 'permission_callback' => function ( $request ) {
211 return current_user_can( 'erp_view_jobinfo', $request['user_id'] );
212 },
213 ],
214 [
215 'methods' => WP_REST_Server::CREATABLE,
216 'callback' => [ $this, 'create_history' ],
217 'permission_callback' => function ( $request ) {
218 return current_user_can( 'erp_manage_jobinfo' );
219 },
220 ],
221 ] );
222
223 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/job_histories' . '/(?P<id>[\d]+)', [
224 [
225 'methods' => WP_REST_Server::DELETABLE,
226 'callback' => [ $this, 'delete_history' ],
227 'permission_callback' => function ( $request ) {
228 return current_user_can( 'erp_manage_jobinfo' );
229 },
230 ],
231 ] );
232
233 //performances
234
235 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/performances', [
236 [
237 'methods' => WP_REST_Server::READABLE,
238 'callback' => [ $this, 'get_performances' ],
239 'permission_callback' => function ( $request ) {
240 return current_user_can( 'erp_view_jobinfo', $request['user_id'] );
241 },
242 ],
243 [
244 'methods' => WP_REST_Server::CREATABLE,
245 'callback' => [ $this, 'create_performance' ],
246 'permission_callback' => function ( $request ) {
247 return current_user_can( 'erp_manage_jobinfo' );
248 },
249 ],
250 ] );
251
252 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/performances' . '/(?P<id>[\d]+)', [
253 [
254 'methods' => WP_REST_Server::DELETABLE,
255 'callback' => [ $this, 'delete_performance' ],
256 'permission_callback' => function ( $request ) {
257 return current_user_can( 'erp_manage_jobinfo' );
258 },
259 ],
260 ] );
261
262 //events
263 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/events', [
264 [
265 'methods' => WP_REST_Server::READABLE,
266 'callback' => [ $this, 'get_events' ],
267 'permission_callback' => function ( $request ) {
268 return current_user_can( 'erp_edit_employee', $request['user_id'] );
269 },
270 ]
271 ] );
272
273 //terminate
274 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/terminate', [
275 [
276 'methods' => WP_REST_Server::EDITABLE,
277 'callback' => [ $this, 'create_terminate' ],
278 'permission_callback' => function ( $request ) {
279 return current_user_can( 'erp_can_terminate' );
280 },
281 ],
282 ] );
283
284 //announcements
285 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/announcements', [
286 [
287 'methods' => WP_REST_Server::READABLE,
288 'callback' => [ $this, 'get_announcements' ],
289 'permission_callback' => function ( $request ) {
290 return current_user_can( 'erp_view_announcement', $request['user_id'] );
291 },
292 ]
293 ] );
294
295 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/announcements', [
296 [
297 'methods' => WP_REST_Server::READABLE,
298 'callback' => [ $this, 'get_announcements' ],
299 'permission_callback' => function ( $request ) {
300 return current_user_can( 'erp_view_announcement', $request['user_id'] );
301 },
302 ],
303 [
304 'methods' => WP_REST_Server::EDITABLE,
305 'callback' => [ $this, 'update_status' ],
306 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
307 'permission_callback' => function ( $request ) {
308 return current_user_can( 'erp_view_announcement', $request['user_id'] );
309 },
310 ],
311 ] );
312
313 //policies
314 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/policies', [
315 [
316 'methods' => WP_REST_Server::READABLE,
317 'callback' => [ $this, 'get_policies' ],
318 'permission_callback' => function ( $request ) {
319 return current_user_can( 'erp_edit_employee', $request['user_id'] );
320 },
321 ],
322 ] );
323
324 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/leaves', [
325 [
326 'methods' => WP_REST_Server::READABLE,
327 'callback' => [ $this, 'get_leaves' ],
328 'permission_callback' => function ( $request ) {
329 return current_user_can( 'erp_edit_employee', $request['user_id'] );
330 },
331 ],
332 [
333 'methods' => WP_REST_Server::CREATABLE,
334 'callback' => [ $this, 'create_leave' ],
335 'permission_callback' => function ( $request ) {
336 return current_user_can( 'erp_edit_employee', $request['user_id'] );
337 },
338 ],
339 ] );
340
341 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/notes', [
342 [
343 'methods' => WP_REST_Server::READABLE,
344 'callback' => [ $this, 'get_notes' ],
345 'permission_callback' => function ( $request ) {
346 return current_user_can( 'erp_manage_review' );
347 },
348 ],
349 [
350 'methods' => WP_REST_Server::CREATABLE,
351 'callback' => [ $this, 'create_note' ],
352 'permission_callback' => function ( $request ) {
353 return current_user_can( 'erp_manage_review' );
354 },
355 ],
356 ] );
357 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/notes' . '/(?P<note_id>[\d]+)', [
358 [
359 'methods' => WP_REST_Server::DELETABLE,
360 'callback' => [ $this, 'delete_note' ],
361 'permission_callback' => function ( $request ) {
362 return current_user_can( 'erp_edit_employee' );
363 },
364 ],
365 ] );
366
367 //roles
368 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/roles', [
369 [
370 'methods' => WP_REST_Server::READABLE,
371 'callback' => [ $this, 'get_roles' ],
372 'permission_callback' => function ( $request ) {
373 return current_user_can( erp_hr_get_manager_role() );
374 },
375 ],
376 [
377 'methods' => WP_REST_Server::EDITABLE,
378 'callback' => [ $this, 'update_role' ],
379 'permission_callback' => function ( $request ) {
380 return current_user_can( erp_hr_get_manager_role() );
381 },
382 ],
383 ] );
384
385 // Upload Photo
386 register_rest_route( $this->namespace, '/' . $this->rest_base . '/upload', [
387 [
388 'methods' => WP_REST_Server::CREATABLE,
389 'callback' => [ $this, 'upload_photo' ],
390 'permission_callback' => function( $request ) {
391 return current_user_can( 'erp_create_employee' );
392 }
393 ],
394 [
395 'methods' => WP_REST_Server::EDITABLE,
396 'callback' => [ $this, 'update_photo' ],
397 'permission_callback' => function() {
398 return current_user_can( 'erp_create_employee' );
399 }
400 ]
401 ] );
402
403 }
404
405 /**
406 * Upload employee photo
407 *
408 * @param \WP_REST_Request $request
409 * @return array
410 */
411 public function upload_photo( \WP_REST_Request $request ) {
412 $file = isset( $_FILES['image'] ) ? $_FILES['image'] : array();
413
414 if ( ! $file ) {
415 return;
416 }
417
418 require_once( ABSPATH . 'wp-admin/includes/image.php' );
419 require_once( ABSPATH . 'wp-admin/includes/file.php' );
420 require_once( ABSPATH . 'wp-admin/includes/media.php' );
421
422 $attachment_id = media_handle_upload( 'image', 0 );
423
424 $response = array(
425 'photo_id' => $attachment_id
426 );
427 return $response ;
428 }
429
430 /**
431 * Update Photo
432 *
433 * @param \WP_REST_Request $request
434 * @return bool
435 */
436 public function update_photo( \WP_REST_Request $request ) {
437 $photo_id = isset( $request['photo_id'] ) ? $request['photo_id'] : 0;
438 $user_id = isset( $request['user_id'] ) ? $request['user_id'] : 0;
439
440 update_user_meta( $user_id, 'photo_id', $photo_id );
441 }
442
443 /**
444 * Get a collection of employees
445 *
446 * @param \WP_REST_Request $request
447 *
448 * @return mixed|object|\WP_REST_Response
449 */
450 public function get_employees( \WP_REST_Request $request ) {
451 $args = [
452 'number' => $request['per_page'],
453 'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ),
454 'status' => ( $request['status'] ) ? $request['status'] : 'active',
455 'department' => ( $request['department'] ) ? $request['department'] : '-1',
456 'designation' => ( $request['designation'] ) ? $request['designation'] : '-1',
457 'location' => ( $request['location'] ) ? $request['location'] : '-1',
458 's' => ( $request['s'] ) ? $request['s'] : '',
459 ];
460
461
462 $items = erp_hr_get_employees( $args );
463
464 $args['count'] = true;
465 $total_items = erp_hr_get_employees( $args );
466
467 $formatted_items = [];
468 foreach ( $items as $item ) {
469 $additional_fields = [];
470 $data = $this->prepare_item_for_response( $item, $request, $additional_fields );
471 $formatted_items[] = $this->prepare_response_for_collection( $data );
472 }
473 $response = rest_ensure_response( $formatted_items );
474 $response = $this->format_collection_response( $response, $request, (int) $total_items );
475
476 return $response;
477 }
478
479 /**
480 * Get a specific employee
481 *
482 * @param \WP_REST_Request $request
483 *
484 * @return WP_Error|WP_REST_Response
485 */
486 public function get_employee( \WP_REST_Request $request ) {
487 $user_id = (int) $request['user_id'];
488 $item = new Employee( $user_id );
489
490 if ( ! $item->is_employee() ) {
491 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 404 ] );
492 }
493
494 $item = $this->prepare_item_for_response( $item, $request );
495 $response = rest_ensure_response( $item );
496
497 return $response;
498 }
499
500 /**
501 * Create employees
502 *
503 * @param $request
504 *
505 * @return $this|int|\WP_Error|\WP_REST_Response
506 */
507 public function create_employees( \WP_REST_Request $request ) {
508 $employees = json_decode( $request->get_body(), true );
509
510 foreach ( $employees as $employee ) {
511 $item_data = $this->prepare_item_for_database( $employee );
512 $item = new Employee( null );
513 $created = $item->create_employee( $item_data );
514 if ( is_wp_error( $created ) ) {
515 return $created;
516 }
517 }
518
519 return new WP_REST_Response( true, 201 );
520 }
521
522 /**
523 * Create an employee
524 *
525 * @param \WP_REST_Request $request
526 *
527 * @return WP_Error|\WP_REST_Request
528 */
529 public function create_employee( $request ) {
530 $item_data = $this->prepare_item_for_database( $request );
531
532 $employee = new Employee( null );
533 $created = $employee->create_employee( $item_data );
534 if ( is_wp_error( $created ) ) {
535 return $created;
536 }
537 $request->set_param( 'context', 'edit' );
538 $item = new Employee( $created->user_id );
539 $response = $this->prepare_item_for_response( $item, $request );
540 $response = rest_ensure_response( $response );
541 $response->set_status( 201 );
542 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $employee->get_user_id() ) ) );
543
544 return $response;
545 }
546
547 /**
548 * Update an employee
549 *
550 * @param \WP_REST_Request $request
551 *
552 * @return $this|mixed|object|\WP_Error|\WP_REST_Response
553 */
554 public function update_employee( \WP_REST_Request $request ) {
555 $id = (int) $request['user_id'];
556
557 $employee = new Employee( $id );
558 if ( ! $employee ) {
559 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
560 }
561
562 $data = $this->prepare_item_for_database( $request );
563 $updated = $employee->update_employee( $data );
564
565 if ( is_wp_error( $updated ) ) {
566 return $updated;
567 }
568
569 $request->set_param( 'context', 'edit' );
570 $updated_user = new Employee( $updated->user_id );
571 $response = $this->prepare_item_for_response( $updated_user, $request );
572
573 $response = rest_ensure_response( $response );
574 $response->set_status( 201 );
575 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
576
577 return $response;
578 }
579
580 /**
581 * Delete an employee
582 *
583 * @param $request
584 *
585 * @return \WP_REST_Response
586 */
587 public function delete_employee( \WP_REST_Request $request ) {
588 $id = (int) $request['user_id'];
589
590 erp_employee_delete( $id );
591 $response = rest_ensure_response( true );
592
593 return new WP_REST_Response( $response, 204 );
594 }
595
596
597 /**
598 * Get a collection of employee's experiences
599 *
600 * @param \WP_REST_Request $request
601 *
602 * @return mixed|object|\WP_Error|\WP_REST_Response
603 */
604 public function get_experiences( \WP_REST_Request $request ) {
605 $employee_id = (int) $request['user_id'];
606 $employee = new Employee( $employee_id );
607
608 if ( ! $employee->is_employee() ) {
609 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
610 }
611
612 $items = $employee->get_experiences();
613
614 $total_items = $employee->get_erp_user()->experiences()->count();
615 $response = rest_ensure_response( $items );
616 $response = $this->format_collection_response( $response, $request, $total_items );
617
618 return $response;
619 }
620
621 /**
622 * Create an experience
623 *
624 * @param \WP_REST_Request $request
625 *
626 * @return WP_Error|\WP_REST_Request
627 */
628 public function create_experience( \WP_REST_Request $request ) {
629 $employee_id = (int) $request['user_id'];
630 $employee = new Employee( $employee_id );
631
632 if ( ! $employee->is_employee() ) {
633 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
634 }
635 $experience = $employee->add_experience( $request->get_params() );
636
637 if ( is_wp_error( $experience ) ) {
638 return $experience;
639 }
640
641 $request->set_param( 'context', 'edit' );
642 $response = rest_ensure_response( $experience );
643 $response->set_status( 201 );
644 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $experience['id'] ) ) );
645
646 return $response;
647 }
648
649 /**
650 * Update an experience
651 *
652 * @param \WP_REST_Request $request
653 *
654 * @return WP_Error|\WP_REST_Request
655 */
656 public function update_experience( \WP_REST_Request $request ) {
657 $employee_id = (int) $request['user_id'];
658 $exp_id = (int) $request['id'];
659 $employee = new Employee( $employee_id );
660
661 if ( ! $employee->is_employee() ) {
662 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
663 }
664 $args = $request->get_params();
665 $args['id'] = $exp_id;
666
667 $experience = $employee->add_experience( $args );
668
669 if ( is_wp_error( $experience ) ) {
670 return $experience;
671 }
672
673 $request->set_param( 'context', 'edit' );
674 $response = rest_ensure_response( $experience );
675 $response->set_status( 201 );
676 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $experience['id'] ) ) );
677
678 return $response;
679 }
680
681 /**
682 * Delete an experience
683 *
684 * @param $request
685 *
686 * @return \WP_Error|\WP_REST_Response
687 */
688 public function delete_experience( $request ) {
689 $employee_id = (int) $request['user_id'];
690 $employee = new Employee( $employee_id );
691
692 if ( ! $employee->is_employee() ) {
693 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
694 }
695 $result = $employee->delete_experience( $request['id'] );
696 $response = rest_ensure_response( $result );
697
698 return $response;
699 }
700
701 /**
702 * Get a collection of employee's educations
703 *
704 * @param $request
705 *
706 * @return mixed|object|\WP_Error|\WP_REST_Response
707 */
708 public function get_educations( $request ) {
709 $employee_id = (int) $request['user_id'];
710 $employee = new Employee( $employee_id );
711 if ( ! $employee->is_employee() ) {
712 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 400 ] );
713 }
714 $items = $employee->get_educations();
715 $total = $employee->get_erp_user()->educations()->count();
716 $response = rest_ensure_response( $items );
717 $response = $this->format_collection_response( $response, $request, $total );
718
719 return $response;
720 }
721
722 /**
723 * Create an education
724 *
725 * @param \WP_REST_Request $request
726 *
727 * @return WP_Error|\WP_REST_Request
728 */
729 public function create_education( $request ) {
730 $employee_id = (int) $request['user_id'];
731 $employee = new Employee( $employee_id );
732 if ( ! $employee->is_employee() ) {
733 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 400 ] );
734 }
735 $education = $employee->add_education( $request->get_params() );
736
737 if ( is_wp_error( $education ) ) {
738 return $education;
739 }
740
741 $request->set_param( 'context', 'edit' );
742
743 $response = rest_ensure_response( $education );
744 $response->set_status( 201 );
745 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $education['id'] ) ) );
746
747 return $response;
748 }
749
750 /**
751 * Update an education
752 *
753 * @param \WP_REST_Request $request
754 *
755 * @return WP_Error|\WP_REST_Request
756 */
757 public function update_education( $request ) {
758 $employee_id = (int) $request['user_id'];
759 $edu_id = (int) $request['id'];
760 $employee = new Employee( $employee_id );
761 if ( ! $employee->is_employee() ) {
762 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 400 ] );
763 }
764
765 $args['id'] = $edu_id;
766 $education = $employee->add_education( $request->get_params() );
767
768 if ( is_wp_error( $education ) ) {
769 return $education;
770 }
771
772 $request->set_param( 'context', 'edit' );
773
774 $response = rest_ensure_response( $education );
775 $response->set_status( 201 );
776 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $education['id'] ) ) );
777
778 return $response;
779 }
780
781 /**
782 * Delete an education
783 *
784 * @param \WP_REST_Request $request
785 *
786 * @return WP_Error|\WP_REST_Request
787 */
788 public function delete_education( $request ) {
789 $employee_id = (int) $request['user_id'];
790 $employee = new Employee( $employee_id );
791
792 if ( ! $employee->is_employee() ) {
793 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
794 }
795 $result = $employee->delete_education( $request['id'] );
796 $response = rest_ensure_response( $result );
797
798 return $response;
799 }
800
801 /**
802 * Get a collection of employee's dependents
803 *
804 * @param \WP_REST_Request $request
805 *
806 * @return WP_Error|WP_REST_Response|mixed
807 */
808 public function get_dependents( $request ) {
809 $employee_id = (int) $request['user_id'];
810 $employee = new Employee( $employee_id );
811 if ( ! $employee->is_employee() ) {
812 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 400 ] );
813 }
814 $items = $employee->get_dependents();
815 $total = $employee->get_erp_user()->dependents()->count();
816 $response = rest_ensure_response( $items );
817 $response = $this->format_collection_response( $response, $request, $total );
818
819 return $response;
820 }
821
822 /**
823 * Create a dependent
824 *
825 * @param \WP_REST_Request $request
826 *
827 * @return WP_Error|\WP_REST_Request
828 */
829 public function create_dependent( $request ) {
830 $employee_id = (int) $request['user_id'];
831 $employee = new Employee( $employee_id );
832 if ( ! $employee->is_employee() ) {
833 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 400 ] );
834 }
835 $dependent = $employee->add_dependent( $request->get_params() );
836
837 if ( is_wp_error( $dependent ) ) {
838 return $dependent;
839 }
840
841 $request->set_param( 'context', 'edit' );
842
843 $response = rest_ensure_response( $dependent );
844 $response->set_status( 201 );
845 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $dependent['id'] ) ) );
846
847 return $response;
848 }
849
850 /**
851 * Update a dependent
852 *
853 * @param \WP_REST_Request $request
854 *
855 * @return WP_Error|\WP_REST_Request
856 */
857 public function update_dependent( $request ) {
858 $employee_id = (int) $request['user_id'];
859 $depen_id = (int) $request['id'];
860 $employee = new Employee( $employee_id );
861 if ( ! $employee->is_employee() ) {
862 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 400 ] );
863 }
864
865 $args['id'] = $depen_id;
866 $dependent = $employee->add_dependent( $request->get_params() );
867
868 if ( is_wp_error( $dependent ) ) {
869 return $dependent;
870 }
871
872 $request->set_param( 'context', 'edit' );
873
874 $response = rest_ensure_response( $dependent );
875 $response->set_status( 201 );
876 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $dependent['id'] ) ) );
877
878 return $response;
879 }
880
881 /**
882 * Delete a dependent
883 *
884 * @param \WP_REST_Request $request
885 *
886 * @return WP_Error|\WP_REST_Request
887 */
888 public function delete_dependent( $request ) {
889 $employee_id = (int) $request['user_id'];
890 $employee = new Employee( $employee_id );
891
892 if ( ! $employee->is_employee() ) {
893 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
894 }
895 $result = $employee->delete_dependent( $request['id'] );
896 $response = rest_ensure_response( $result );
897
898 return $response;
899 }
900
901 /**
902 * Get employee histories
903 *
904 * @since 1.3.0
905 *
906 * @param $request
907 *
908 * @return mixed|object|WP_Error|WP_REST_Response
909 */
910 public function get_histories( $request ) {
911 $employee_id = (int) $request['user_id'];
912 $employee = new Employee( $employee_id );
913 if ( ! $employee->is_employee() ) {
914 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 400 ] );
915 }
916 $module = ! empty( $request['module'] ) ? sanitize_key( $request['module'] ) : 'all';
917
918 $histories = $employee->get_job_histories( $module );
919
920 for ( $i = 0; $i < count( $histories['job'] ); $i ++ ) {
921 $reports_to = new Employee( $histories['job'][ $i ]['reporting_to'] );
922
923 if ( $employee->is_employee() ) {
924 $histories['job'][ $i ]['reporting_to_full_name'] = $reports_to->display_name;
925 }
926 }
927
928 $total = $employee->get_erp_user()->histories()->count();
929 $response = rest_ensure_response( $histories );
930 $response = $this->format_collection_response( $response, $request, $total );
931
932 return $response;
933 }
934
935 /**
936 * Create employee history
937 *
938 * @since 1.3.0
939 *
940 * @param $request
941 *
942 * @return mixed|WP_Error|WP_REST_Response
943 */
944 public function create_history( \WP_REST_Request $request ) {
945 $employee_id = (int) $request['user_id'];
946 $module = ! empty( $request['module'] ) ? sanitize_key( $request['module'] ) : 0;
947 $employee = new Employee( $employee_id );
948 if ( ! $employee ) {
949 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), array( 'status' => 404 ) );
950 }
951 if ( empty( $module ) || ( ! in_array( $module, [ 'employment', 'compensation', 'job' ] ) ) ) {
952 return new WP_Error( 'rest_no_module_type', __( 'Invalid/No module type' ), array( 'status' => 404 ) );
953 }
954
955 $history = new WP_Error();
956
957 if ( $request['module'] == 'employment' ) {
958 $history = $employee->update_employment_status( $request->get_params() );
959 }
960 if ( $request['module'] == 'compensation' ) {
961 $history = $employee->update_compensation( $request->get_params() );
962 }
963 if ( $request['module'] == 'job' ) {
964 $history = $employee->update_job_info( $request->get_params() );
965 }
966 if ( is_wp_error( $history ) ) {
967 return $history;
968 }
969 $response = rest_ensure_response( $history );
970
971 return $response;
972 }
973
974 /**
975 * Delete a history
976 *
977 * @since 1.3.0
978 *
979 * @param $request
980 *
981 * @return \WP_Error|\WP_REST_Response
982 * @throws \Exception
983 */
984 public function delete_history( $request ) {
985 $employee_id = (int) $request['user_id'];
986 $employee = new Employee( $employee_id );
987
988 if ( ! $employee->is_employee() ) {
989 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
990 }
991 $result = $employee->delete_job_history( $request['id'] );
992 $response = rest_ensure_response( $result );
993
994 return $response;
995 }
996
997 /**
998 * Get all performance of a single employee
999 *
1000 * @since 1.3.0
1001 *
1002 * @param $request
1003 *
1004 * @return mixed|object|WP_Error|WP_REST_Response
1005 */
1006 public function get_performances( \WP_REST_Request $request ) {
1007 $employee_id = (int) $request['user_id'];
1008 $employee = new Employee( $employee_id );
1009 if ( ! $employee->is_employee() ) {
1010 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 400 ] );
1011 }
1012 $items = $employee->get_performances( $request->get_params() );
1013
1014 foreach ( $items as $item ) {
1015 foreach ( $item as $performance ) {
1016 $user_id = 0;
1017
1018 if ( $performance['reporting_to'] ) {
1019 $user_id = $performance['reporting_to'];
1020 } elseif ( $performance['reviewer'] ) {
1021 $user_id = $performance['reviewer'];
1022 } elseif ( $performance['supervisor'] ) {
1023 $user_id = $performance['supervisor'];
1024 }
1025
1026 $associate_employee = new Employee( $user_id );
1027 if ( $associate_employee->is_employee() ) {
1028 $performance->reporting_to_full_name = $associate_employee->display_name;
1029 $performance->supervisor_full_name = $associate_employee->display_name;
1030 $performance->reviewer_full_name = $associate_employee->display_name;
1031 }
1032
1033 }
1034 }
1035
1036 $total = $employee->get_erp_user()->performances()->count();
1037 $response = rest_ensure_response( $items );
1038 $response = $this->format_collection_response( $response, $request, $total );
1039
1040 return $response;
1041 }
1042
1043 /**
1044 * Create a performance
1045 *
1046 * @param \WP_REST_Request $request
1047 *
1048 * @return WP_Error|\WP_REST_Request
1049 */
1050 public function create_performance( $request ) {
1051 $employee_id = (int) trim( $request['user_id'] );
1052 $employee = new Employee( $employee_id );
1053 if ( ! $employee ) {
1054 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), array( 'status' => 404 ) );
1055 }
1056
1057 $performance = $employee->add_performance( $request->get_params() );
1058
1059 if ( is_wp_error( $performance ) ) {
1060 return $performance;
1061 }
1062 $request->set_param( 'context', 'edit' );
1063 $response = rest_ensure_response( $performance );
1064 $response->set_status( 201 );
1065 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $performance['id'] ) ) );
1066
1067 return $response;
1068 }
1069
1070 /**
1071 * Delete performance
1072 *
1073 * @since 1.3.0
1074 *
1075 * @param $request
1076 *
1077 * @return mixed|\WP_Error|\WP_REST_Response
1078 */
1079 public function delete_performance( $request ) {
1080 $employee_id = (int) $request['user_id'];
1081 $employee = new Employee( $employee_id );
1082
1083 if ( ! $employee->is_employee() ) {
1084 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
1085 }
1086 $result = $employee->delete_performance( $request['id'] );
1087 $response = rest_ensure_response( $result );
1088
1089 return $response;
1090 }
1091
1092 /**
1093 * Get all the events of a single user
1094 *
1095 * @since 1.3.0
1096 *
1097 * @param $request
1098 *
1099 * @return mixed|object|WP_Error|WP_REST_Response
1100 */
1101 public function get_events( $request ) {
1102 $user_id = (int) $request['user_id'];
1103 $start = ! empty( $request['start'] ) ? $request['start'] : date( 'Y-01-01' );
1104 $end = ! empty( $request['end'] ) ? $request['end'] : date( 'Y-12-31' );
1105
1106 $employee = new Employee( $user_id );
1107 if ( ! $employee ) {
1108 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), array( 'status' => 404 ) );
1109 }
1110 $event_data = $employee->get_calender_events(['start' => $start, 'end' => $end]);
1111
1112 $response = rest_ensure_response( $event_data );
1113 $response = $this->format_collection_response( $response, $request, count( $event_data ) );
1114
1115 return $response;
1116 }
1117
1118 /**
1119 * Get Available leaves policies of a single employee
1120 *
1121 * @since 1.3.0
1122 *
1123 * @param $request
1124 *
1125 * @return mixed|object|\WP_Error|\WP_REST_Response
1126 */
1127 public function get_policies( $request ) {
1128 $user_id = (int) $request['user_id'];
1129 $employee = new Employee( $user_id );
1130
1131 if ( ! $employee ) {
1132 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), array( 'status' => 404 ) );
1133 }
1134 $policies = $employee->get_leave_summary();
1135 $response = rest_ensure_response( $policies );
1136 $response = $this->format_collection_response( $response, $request, count( $policies ) );
1137
1138 return $response;
1139 }
1140
1141 /**
1142 * Get all leaves of a single employee
1143 *
1144 * @since 1.3.0
1145 *
1146 * @param \WP_REST_Request $request
1147 *
1148 * @return array|WP_Error|object
1149 */
1150 public function get_leaves( \WP_REST_Request $request ) {
1151 $user_id = (int) $request['user_id'];
1152 $employee = new Employee( $user_id );
1153 if ( ! $employee ) {
1154 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), array( 'status' => 404 ) );
1155 }
1156 $leaves = $employee->get_leave_requests();
1157 $response = rest_ensure_response( $leaves );
1158 $response = $this->format_collection_response( $response, $request, count( $leaves ) );
1159
1160 return $response;
1161 }
1162
1163 /**
1164 * Create leave request
1165 *
1166 * @since 1.3.0
1167 *
1168 * @param \WP_REST_Request $request
1169 *
1170 * @return array|WP_Error|object
1171 */
1172 public function create_leave( $request ) {
1173 $id = (int) $request['user_id'];
1174 $employee = new Employee( $id );
1175
1176 if ( ! $employee ) {
1177 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), array( 'status' => 404 ) );
1178 }
1179
1180 if ( empty( $request['policy_id'] ) ) {
1181 return new WP_Error( 'rest_invalid_policy_id', __( 'Invalid Policy id.' ), array( 'status' => 404 ) );
1182 }
1183
1184 if ( empty( $request['start_date'] ) ) {
1185 return new WP_Error( 'rest_invalid_start_date', __( 'Invalid Leave Start Date.' ), array( 'status' => 404 ) );
1186 }
1187
1188 if ( empty( $request['end_date'] ) ) {
1189 return new WP_Error( 'rest_invalid_end_date', __( 'Invalid Leave End Date.' ), array( 'status' => 404 ) );
1190 }
1191
1192 $request_id = erp_hr_leave_insert_request(
1193 array(
1194 'user_id' => $request['user_id'],
1195 'leave_policy' => $request['policy_id'],
1196 'start_date' => $request['start_date'],
1197 'end_date' => $request['end_date'],
1198 'reason' => $request['reason'],
1199 'status' => 0
1200 )
1201 );
1202
1203 $response = rest_ensure_response( $request_id );
1204 $response->set_status( 201 );
1205 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $request_id ) ) );
1206
1207 return $response;
1208 }
1209
1210
1211 /**
1212 * Get all notes of a single employee
1213 *
1214 * @since 1.3.0
1215 *
1216 * @param $request
1217 *
1218 * @return mixed|object|\WP_Error|\WP_REST_Response
1219 */
1220 public function get_notes( $request ) {
1221 $args = array(
1222 'total' => isset( $request['perpage'] ) ? $request['perpage'] : 20,
1223 'offset' => isset( $request['offset'] ) ? $request['offset'] : 0,
1224 );
1225
1226 $id = (int) $request['id'];
1227 $employee = new Employee( $id );
1228 if ( ! $employee ) {
1229 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), array( 'status' => 404 ) );
1230 }
1231
1232 $notes = $employee->get_notes( $args['total'], $args['offset'] );
1233
1234 foreach ( $notes as $note ) {
1235 $user = get_user_by( 'id', $note->comment_by );
1236 $note->comment_by_display_name = $user->display_name;
1237 $note->comment_by_avatar_url = get_avatar_url( $user->comment_by );
1238 }
1239
1240 $total = $employee->get_erp_user()->notes()->count();
1241 $response = rest_ensure_response( $notes );
1242 $response = $this->format_collection_response( $response, $request, $total );
1243
1244 return $response;
1245 }
1246
1247 /**
1248 * Create a note for employee
1249 *
1250 * @since 1.3.0
1251 *
1252 * @param $request
1253 *
1254 * @return array|mixed|WP_Error|WP_REST_Response
1255 */
1256 public function create_note( $request ) {
1257 $id = (int) $request['user_id'];
1258 $employee = new Employee( $id );
1259 if ( ! $employee ) {
1260 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), array( 'status' => 404 ) );
1261 }
1262 $note = $employee->add_note( $request['note'], null, true );
1263
1264 $request->set_param( 'context', 'edit' );
1265 $response = rest_ensure_response( $note );
1266 $response->set_status( 201 );
1267 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $note['id'] ) ) );
1268
1269 return $response;
1270 }
1271
1272 /**
1273 * Delete a note
1274 *
1275 * @since 1.3.0
1276 *
1277 * @param $request
1278 *
1279 * @return WP_REST_Response|WP_Error
1280 */
1281 public function delete_note( $request ) {
1282 $employee_id = (int) $request['user_id'];
1283 $note_id = (int) $request['id'];
1284 $employee = new Employee( $employee_id );
1285
1286 if ( ! $employee->is_employee() ) {
1287 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
1288 }
1289 $result = $employee->delete_note( $note_id );
1290 $response = rest_ensure_response( $result );
1291
1292 return $response;
1293 }
1294
1295 /**
1296 * Get roles of an employee
1297 *
1298 * @since 1.3.0
1299 *
1300 * @param $request
1301 *
1302 * @return mixed|WP_Error|WP_REST_Response
1303 */
1304 public function get_roles( $request ) {
1305 $employee_id = (int) trim( $request['user_id'] );
1306 $employee = new Employee( $employee_id );
1307 if ( ! $employee ) {
1308 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), array( 'status' => 404 ) );
1309 }
1310 $response = rest_ensure_response( $employee->get_roles() );
1311
1312 return $response;
1313 }
1314
1315 /**
1316 * Update employee roles
1317 * accepts associative array eg. ['erp_hr_manager' => true, 'erp_crm_manager' => false ]
1318 *
1319 * @since 1.3.0
1320 *
1321 * @param $request
1322 *
1323 * @return array|mixed|WP_Error|WP_REST_Response
1324 */
1325 public function update_role( $request ) {
1326 $hr_manager_role = erp_hr_get_manager_role();
1327 if ( ! current_user_can( $hr_manager_role ) ) {
1328 return new WP_Error( 'rest_invalid_user_permission', __( 'User do not have permission for the action.' ), array( 'status' => 404 ) );
1329 }
1330 $employee_id = (int) trim( $request['user_id'] );
1331 $employee = new Employee( $employee_id );
1332 if ( ! $employee ) {
1333 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), array( 'status' => 404 ) );
1334 }
1335 if ( ! is_array( $request['roles'] ) || empty( $request['roles'] ) ) {
1336 return new WP_Error( 'rest_performance_invalid_permission_type', __( 'Invalid role format' ), array( 'status' => 400 ) );
1337 }
1338
1339 $roles = $employee->update_role( $request['roles'] )->get_roles();
1340 $request->set_param( 'context', 'edit' );
1341 $response = rest_ensure_response( $roles );
1342 $response->set_status( 201 );
1343 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $employee_id ) ) );
1344
1345 return $response;
1346 }
1347
1348 /**
1349 * Get announcement of an employee
1350 *
1351 * @since 1.3.0
1352 *
1353 * @param $request
1354 *
1355 * @return mixed|object|\WP_Error|\WP_REST_Response
1356 */
1357 public function get_announcements( $request ) {
1358 $user_id = (int) $request['user_id'];
1359 $employee = new Employee( $user_id );
1360 if ( ! $employee ) {
1361 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), array( 'status' => 404 ) );
1362 }
1363 $announcements = $employee->get_announcements();
1364
1365 foreach ($announcements as $announcement) {
1366 $user = get_user_by('id', $announcement['post_author']);
1367 $announcement['post_author'] = $user->data->display_name;
1368 }
1369
1370 $response = rest_ensure_response( $announcements );
1371 $response = $this->format_collection_response( $response, $request, count( $announcements ) );
1372
1373 return $response;
1374 }
1375
1376 /**
1377 * Terminate the employee
1378 *
1379 * @since 1.3.0
1380 *
1381 * @param $request
1382 *
1383 * @return mixed|\WP_Error|\WP_REST_Response
1384 */
1385 public function create_terminate( $request ) {
1386 $user_id = (int) $request['user_id'];
1387 $employee = new Employee( $user_id );
1388 if ( ! $employee ) {
1389 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), array( 'status' => 404 ) );
1390 }
1391
1392 $employee_id = isset( $request['user_id'] ) ? intval( $request['user_id'] ) : 0;
1393 $terminate_date = ( empty( $request['terminate_date'] ) ) ? current_time( 'mysql' ) : $request['terminate_date'];
1394 $termination_type = isset( $request['termination_type'] ) ? $request['termination_type'] : '';
1395 $termination_reason = isset( $request['termination_reason'] ) ? $request['termination_reason'] : '';
1396 $eligible_for_rehire = isset( $request['eligible_for_rehire'] ) ? $request['eligible_for_rehire'] : '';
1397
1398 $fields = [
1399 'employee_id' => $employee_id,
1400 'terminate_date' => $terminate_date,
1401 'termination_type' => $termination_type,
1402 'termination_reason' => $termination_reason,
1403 'eligible_for_rehire' => $eligible_for_rehire
1404 ];
1405 $result = $employee->terminate( $fields );
1406
1407 if ( is_wp_error( $result ) ) {
1408 return new WP_Error( 'rest_insufficient_data', $result->get_error_messages(), array( 'status' => 401 ) );
1409 }
1410
1411 $request->set_param( 'context', 'edit' );
1412 $response = rest_ensure_response( true );
1413 $response->set_status( 201 );
1414 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $user_id ) ) );
1415
1416 return $response;
1417
1418 }
1419
1420 /**
1421 * Prepare a single user output for response
1422 *
1423 * @param \WeDevs\ERP\HRM\Employee $item
1424 * @param \WP_REST_Request|null $request
1425 * @param array $additional_fields
1426 *
1427 * @return mixed|object|\WP_REST_Response
1428 */
1429 public function prepare_item_for_response( Employee $item, \WP_REST_Request $request = null, $additional_fields = [] ) {
1430 $default = [
1431 'user_id' => '',
1432 'employee_id' => '',
1433 'first_name' => '',
1434 'middle_name' => '',
1435 'last_name' => '',
1436 'full_name' => '',
1437 'location' => '',
1438 'date_of_birth' => '',
1439 'pay_rate' => '',
1440 'pay_type' => '',
1441 'hiring_source' => '',
1442 'hiring_date' => '',
1443 'type' => '',
1444 'status' => '',
1445 'other_email' => '',
1446 'phone' => '',
1447 'work_phone' => '',
1448 'mobile' => '',
1449 'address' => '',
1450 'gender' => '',
1451 'marital_status' => '',
1452 'nationality' => '',
1453 'driving_license' => '',
1454 'hobbies' => '',
1455 'user_url' => '',
1456 'description' => '',
1457 'street_1' => '',
1458 'street_2' => '',
1459 'city' => '',
1460 'country' => '',
1461 'state' => '',
1462 'postal_code' => '',
1463 ];
1464
1465 $data = wp_parse_args( $item->get_data( array(), true ), $default );
1466
1467 if ( isset( $request['include'] ) ) {
1468 $include_params = explode( ',', str_replace( ' ', '', $request['include'] ) );
1469
1470 if ( in_array( 'department', $include_params ) && ! empty( $item->get_department() ) ) {
1471 $data['department'] = Department::find( $item->get_department() );
1472 }
1473
1474 if ( in_array( 'designation', $include_params ) && ! empty( $item->get_designation() ) ) {
1475 $data['designation'] = Designation::find( $item->get_designation() );
1476 }
1477
1478 if ( in_array( 'reporting_to', $include_params ) && $item->get_reporting_to() ) {
1479 $reporting_to = new Employee( $item->get_reporting_to() );
1480 if ( $reporting_to->is_employee() ) {
1481 $data['reporting_to'] = $this->prepare_item_for_response( $reporting_to );
1482 }
1483 }
1484
1485 if ( in_array( 'avatar', $include_params ) ) {
1486 $data['avatar_url'] = $item->get_avatar_url( 80 );
1487 }
1488
1489 if ( in_array( 'roles', $include_params ) ) {
1490 $data['roles'] = $item->get_roles();
1491 }
1492 }
1493
1494 $data = array_merge( $data, $additional_fields );
1495
1496 // Wrap the data in a response object
1497 $response = rest_ensure_response( $data );
1498
1499 $response = $this->add_links( $response, $item );
1500
1501 return $response;
1502 }
1503
1504 /**
1505 * Prepare a single item for create or update
1506 *
1507 * @param \WP_REST_Request $request Request object.
1508 *
1509 * @return array $prepared_item
1510 */
1511 protected function prepare_item_for_database( $request ) {
1512 $prepared_item = [];
1513
1514 // required arguments.
1515 if ( isset( $request['first_name'] ) ) {
1516 $prepared_item['personal']['first_name'] = $request['first_name'];
1517 }
1518
1519 if ( isset( $request['last_name'] ) ) {
1520 $prepared_item['personal']['last_name'] = $request['last_name'];
1521 }
1522
1523 if ( isset( $request['employee_id'] ) ) {
1524 $prepared_item['work']['employee_id'] = $request['employee_id'];
1525 }
1526
1527 if ( isset( $request['email'] ) ) {
1528 $prepared_item['user_email'] = $request['email'];
1529 }
1530
1531 // optional arguments.
1532 if ( isset( $request['user_id'] ) ) {
1533 $prepared_item['user_id'] = absint( $request['user_id'] );
1534 }
1535
1536 if ( isset( $request['middle_name'] ) ) {
1537 $prepared_item['personal']['middle_name'] = $request['middle_name'];
1538 }
1539
1540 if ( isset( $request['designation'] ) ) {
1541 $prepared_item['work']['designation'] = $request['designation'];
1542 }
1543
1544 if ( isset( $request['department'] ) ) {
1545 $prepared_item['work']['department'] = $request['department'];
1546 }
1547
1548 if ( isset( $request['reporting_to'] ) ) {
1549 $prepared_item['work']['reporting_to'] = $request['reporting_to'];
1550 }
1551
1552 if ( isset( $request['location'] ) ) {
1553 $prepared_item['work']['location'] = $request['location'];
1554 }
1555
1556 if ( isset( $request['hiring_source'] ) ) {
1557 $prepared_item['work']['hiring_source'] = $request['hiring_source'];
1558 }
1559
1560 if ( isset( $request['hiring_date'] ) ) {
1561 $prepared_item['work']['hiring_date'] = $request['hiring_date'];
1562 }
1563
1564 if ( isset( $request['date_of_birth'] ) ) {
1565 $prepared_item['work']['date_of_birth'] = $request['date_of_birth'];
1566 }
1567
1568 if ( isset( $request['pay_rate'] ) ) {
1569 $prepared_item['work']['pay_rate'] = $request['pay_rate'];
1570 }
1571
1572 if ( isset( $request['pay_type'] ) ) {
1573 $prepared_item['work']['pay_type'] = $request['pay_type'];
1574 }
1575
1576 if ( isset( $request['type'] ) ) {
1577 $prepared_item['work']['type'] = $request['type'];
1578 }
1579
1580 if ( isset( $request['status'] ) ) {
1581 $prepared_item['work']['status'] = $request['status'];
1582 }
1583
1584 if ( isset( $request['other_email'] ) ) {
1585 $prepared_item['personal']['other_email'] = $request['other_email'];
1586 }
1587
1588 if ( isset( $request['phone'] ) ) {
1589 $prepared_item['personal']['phone'] = $request['phone'];
1590 }
1591
1592 if ( isset( $request['work_phone'] ) ) {
1593 $prepared_item['personal']['work_phone'] = $request['work_phone'];
1594 }
1595
1596 if ( isset( $request['mobile'] ) ) {
1597 $prepared_item['personal']['mobile'] = $request['mobile'];
1598 }
1599
1600 if ( isset( $request['address'] ) ) {
1601 $prepared_item['personal']['address'] = $request['address'];
1602 }
1603
1604 if ( isset( $request['gender'] ) ) {
1605 $prepared_item['personal']['gender'] = $request['gender'];
1606 }
1607
1608 if ( isset( $request['marital_status'] ) ) {
1609 $prepared_item['personal']['marital_status'] = $request['marital_status'];
1610 }
1611
1612 if ( isset( $request['nationality'] ) ) {
1613 $prepared_item['personal']['nationality'] = $request['nationality'];
1614 }
1615
1616 if ( isset( $request['driving_license'] ) ) {
1617 $prepared_item['personal']['driving_license'] = $request['driving_license'];
1618 }
1619
1620 if ( isset( $request['hobbies'] ) ) {
1621 $prepared_item['personal']['hobbies'] = $request['hobbies'];
1622 }
1623
1624 if ( isset( $request['user_url'] ) ) {
1625 $prepared_item['personal']['user_url'] = $request['user_url'];
1626 }
1627
1628 if ( isset( $request['description'] ) ) {
1629 $prepared_item['personal']['description'] = $request['description'];
1630 }
1631
1632 if ( isset( $request['street_1'] ) ) {
1633 $prepared_item['personal']['street_1'] = $request['street_1'];
1634 }
1635
1636 if ( isset( $request['street_2'] ) ) {
1637 $prepared_item['personal']['street_2'] = $request['street_2'];
1638 }
1639
1640 if ( isset( $request['city'] ) ) {
1641 $prepared_item['personal']['city'] = $request['city'];
1642 }
1643
1644 if ( isset( $request['country'] ) ) {
1645 $prepared_item['personal']['country'] = $request['country'];
1646 }
1647
1648 if ( isset( $request['state'] ) ) {
1649 $prepared_item['personal']['state'] = $request['state'];
1650 }
1651
1652 if ( isset( $request['postal_code'] ) ) {
1653 $prepared_item['personal']['postal_code'] = $request['postal_code'];
1654 }
1655 if ( isset( $request['photo_id'] ) ) {
1656 $prepared_item['personal']['photo_id'] = $request['photo_id'];
1657 }
1658
1659 return $prepared_item;
1660 }
1661
1662
1663 /**
1664 * Get the query params for collections.
1665 *
1666 * @return array
1667 */
1668 public function get_collection_params() {
1669 return [
1670 'context' => $this->get_context_param(),
1671 'page' => [
1672 'description' => __( 'Current page of the collection.' ),
1673 'type' => 'integer',
1674 'default' => 1,
1675 'sanitize_callback' => 'absint',
1676 'validate_callback' => 'rest_validate_request_arg',
1677 'minimum' => 1,
1678 ],
1679 'per_page' => [
1680 'description' => __( 'Maximum number of items to be returned in result set.' ),
1681 'type' => 'integer',
1682 'default' => 20,
1683 'minimum' => 1,
1684 'maximum' => 100,
1685 'sanitize_callback' => 'absint',
1686 'validate_callback' => 'rest_validate_request_arg',
1687 ],
1688 'search' => [
1689 'description' => __( 'Limit results to those matching a string.' ),
1690 'type' => 'string',
1691 'sanitize_callback' => 'sanitize_text_field',
1692 'validate_callback' => 'rest_validate_request_arg',
1693 ],
1694 ];
1695 }
1696
1697 }
1698