| 1 |
<?php |
| 2 |
|
| 3 |
namespace WeDevs\ERP\HRM\API; |
| 4 |
|
| 5 |
use Carbon\Carbon; |
| 6 |
use WP_REST_Server; |
| 7 |
use WP_REST_Response; |
| 8 |
use WP_Error; |
| 9 |
use WeDevs\ERP\HRM\Employee; |
| 10 |
use WeDevs\ERP\API\REST_Controller; |
| 11 |
class Birthdays_Controller extends REST_Controller { |
| 12 |
/** |
| 13 |
* Endpoint namespace. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
protected $namespace = 'erp/v1'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Route base. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $rest_base = 'hrm/birthdays'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Register the routes for the objects of the controller. |
| 28 |
*/ |
| 29 |
public function register_routes() { |
| 30 |
register_rest_route( $this->namespace, '/' . $this->rest_base, [ |
| 31 |
[ |
| 32 |
'methods' => WP_REST_Server::READABLE, |
| 33 |
'callback' => [ $this, 'get_birthdays' ], |
| 34 |
'args' => $this->get_collection_params(), |
| 35 |
'permission_callback' => function ( $request ) { |
| 36 |
return current_user_can( 'erp_view_list' ); |
| 37 |
}, |
| 38 |
], |
| 39 |
'schema' => [ $this, 'get_public_item_schema' ], |
| 40 |
] ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get a collection of employee birthdays |
| 45 |
* |
| 46 |
* @param WP_REST_Request $request |
| 47 |
* |
| 48 |
* @return WP_Error|WP_REST_Response |
| 49 |
*/ |
| 50 |
public function get_birthdays( $request ) { |
| 51 |
$args = [ |
| 52 |
'upcoming' => empty($request['upcoming']) ? false : true, |
| 53 |
'number' => $request['per_page'] ? $request['per_page'] : 20, |
| 54 |
'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ), |
| 55 |
]; |
| 56 |
|
| 57 |
$from_date = \Carbon\Carbon::today()->format('m d'); |
| 58 |
if ( empty( $args['upcoming'] ) ) { |
| 59 |
$from_date = '01 01'; |
| 60 |
} |
| 61 |
|
| 62 |
$db = new \WeDevs\ORM\Eloquent\Database(); |
| 63 |
$employees = erp_array_to_object ( \WeDevs\ERP\HRM\Models\Employee::select('*') |
| 64 |
->where( $db->raw("DATE_FORMAT( `date_of_birth`, '%m %d' )" ), '>=', $from_date ) |
| 65 |
->orderByRaw('MONTH(date_of_birth)') |
| 66 |
->orderByRaw('MONTH(date_of_birth)') |
| 67 |
->orderByRaw('DAYOFMONTH(date_of_birth)') |
| 68 |
->where( 'termination_date', '0000-00-00' ) |
| 69 |
->where( $db->raw( "DATE_FORMAT( `date_of_birth`, '%m %d' )" ), '<=', \Carbon\Carbon::now()->addWeek()->format( 'm d' ) ) |
| 70 |
->limit( $args['number'] ) |
| 71 |
->offset( $args['offset'] ) |
| 72 |
->get() |
| 73 |
->toArray() ); |
| 74 |
|
| 75 |
$total_items = count( $employees ); |
| 76 |
$formated_items = []; |
| 77 |
foreach ( $employees as $employee ) { |
| 78 |
$item = []; |
| 79 |
$item['id'] = $employee->id; |
| 80 |
$item['user_id'] = $employee->user_id; |
| 81 |
$item['employee_id'] = $employee->employee_id; |
| 82 |
$item['date_of_birth'] = $employee->date_of_birth; |
| 83 |
$item['birthday'] = date('dS, M', strtotime($employee->date_of_birth)); |
| 84 |
$item['designation'] = $employee->designation; |
| 85 |
$item['department'] = $employee->department; |
| 86 |
|
| 87 |
$employee_user = new \WeDevs\ERP\HRM\Employee( intval( $employee->user_id ) ); |
| 88 |
$item['name'] = $employee_user->get_full_name(); |
| 89 |
$item['avatar'] = $employee_user->get_avatar( 80 ); |
| 90 |
$item['job_title'] = $employee_user->get_job_title(); |
| 91 |
$item['department_title'] = $employee_user->get_department_title(); |
| 92 |
$item['url'] = $employee_user->get_details_url(); |
| 93 |
|
| 94 |
$formated_items[] = $item; |
| 95 |
} |
| 96 |
|
| 97 |
$response = rest_ensure_response( $formated_items ); |
| 98 |
$response = $this->format_collection_response( $response, $request, $total_items ); |
| 99 |
|
| 100 |
return $response; |
| 101 |
} |
| 102 |
} |
| 103 |
|