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.10 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 All 144 releases
erp / modules / hrm / includes / functions-leave.php

functions-leave.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.3.13, at modules/hrm/includes/functions-leave.php

1,877 lines 57.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Get holiday between two date
5 *
6 * @since 0.1
7 *
8 * @param date $start_date
9 * @param date $end_date
10 *
11 * @return array
12 */
13 function erp_hr_leave_get_holiday_between_date_range( $start_date, $end_date ) {
14 $holiday = new \WeDevs\ERP\HRM\Models\Leave_Holiday();
15
16 $holiday = $holiday->where( function ( $condition ) use ( $start_date ) {
17 $condition->where( 'start', '<=', $start_date );
18 $condition->where( 'end', '>=', $start_date );
19 } );
20
21 $holiday = $holiday->orWhere( function ( $condition ) use ( $end_date ) {
22 $condition->where( 'start', '<=', $end_date );
23 $condition->where( 'end', '>=', $end_date );
24 } );
25
26
27 $holiday = $holiday->orWhere( function ( $condition ) use ( $start_date, $end_date ) {
28 $condition->where( 'start', '>=', $start_date );
29 $condition->where( 'start', '<=', $end_date );
30 } );
31
32 $holiday = $holiday->orWhere( function ( $condition ) use ( $start_date, $end_date ) {
33 $condition->where( 'end', '>=', $start_date );
34 $condition->where( 'end', '<=', $end_date );
35 } );
36
37 $results = $holiday->get()->toArray();
38
39 $holiday_extrat = [];
40 $given_date_extrat = erp_extract_dates( $start_date, $end_date );
41
42 foreach ( $results as $result ) {
43 $date_extrat = erp_extract_dates( $result['start'], $result['end'] );
44 $holiday_extrat = array_merge( $holiday_extrat, $date_extrat );
45 }
46
47 $extract = array_intersect( $given_date_extrat, $holiday_extrat );
48
49 return $extract;
50 }
51
52 /**
53 * Checking is user take leave within date rang in before
54 *
55 * @since 0.1
56 *
57 * @param string $start_date
58 * @param string $end_date
59 * @param int $user_id
60 *
61 * @return boolean
62 */
63 function erp_hrm_is_leave_recored_exist_between_date( $start_date, $end_date, $user_id ) {
64
65 $start_date = date( 'Y-m-d', strtotime( $start_date ) );
66 $end_date = date( 'Y-m-d', strtotime( $end_date ) );
67
68 $holiday = new \WeDevs\ERP\HRM\Models\Leave_request();
69
70 $holiday->where( 'user_id', '=', $user_id );
71
72 $holiday = $holiday->where( function ( $condition ) use ( $start_date, $user_id ) {
73 $condition->where( 'start_date', '<=', $start_date );
74 $condition->where( 'end_date', '>=', $start_date );
75 $condition->whereIn( 'status', [ 1, 2 ] );
76 $condition->where( 'user_id', '=', $user_id );
77 } );
78
79 $holiday = $holiday->orWhere( function ( $condition ) use ( $end_date, $user_id ) {
80 $condition->where( 'start_date', '<=', $end_date );
81 $condition->where( 'end_date', '>=', $end_date );
82 $condition->whereIn( 'status', [ 1, 2 ] );
83 $condition->where( 'user_id', '=', $user_id );
84 } );
85
86 $holiday = $holiday->orWhere( function ( $condition ) use ( $start_date, $end_date, $user_id ) {
87 $condition->where( 'start_date', '>=', $start_date );
88 $condition->where( 'start_date', '<=', $end_date );
89 $condition->whereIn( 'status', [ 1, 2 ] );
90 $condition->where( 'user_id', '=', $user_id );
91 } );
92
93 $holiday = $holiday->orWhere( function ( $condition ) use ( $start_date, $end_date, $user_id ) {
94 $condition->where( 'end_date', '>=', $start_date );
95 $condition->where( 'end_date', '<=', $end_date );
96 $condition->whereIn( 'status', [ 1, 2 ] );
97 $condition->where( 'user_id', '=', $user_id );
98 } );
99
100 $results = $holiday->get()->toArray();
101
102 $holiday_extrat = [];
103 $given_date_extrat = erp_extract_dates( $start_date, $end_date );
104
105 foreach ( $results as $result ) {
106 $date_extrat = erp_extract_dates( $result['start_date'], $result['end_date'] );
107 $holiday_extrat = array_merge( $holiday_extrat, $date_extrat );
108 }
109
110 $extract = array_intersect( $given_date_extrat, $holiday_extrat );
111
112 return $extract;
113 }
114
115 /**
116 * Check leave duration exist or not the plicy days
117 *
118 * @since 0.1
119 *
120 * @param string $start_date
121 * @param string $end_date
122 * @param int $policy_id
123 * @param int $user_id
124 *
125 * @return boolean
126 */
127 function erp_hrm_is_valid_leave_duration( $start_date, $end_date, $policy_id, $user_id ) {
128
129 if ( ! $user_id || ! $policy_id ) {
130 return true;
131 }
132
133 $financial_start_date = date( 'Y-m-d', strtotime( erp_financial_start_date() ) );
134 $financial_end_date = date( 'Y-m-d', strtotime( erp_financial_end_date() ) );
135
136 $user_request = new \WeDevs\ERP\HRM\Models\Leave_request();
137 $policy = new \WeDevs\ERP\HRM\Models\Leave_Policies();
138
139 $user_request = $user_request->where( function ( $condition ) use ( $financial_start_date, $financial_end_date, $user_id, $policy_id ) {
140 //$start_date = date( 'Y-m-d', strtotime( $start_date ) );
141 // $end_date = date( 'Y-m-d', strtotime( $end_date ) );
142
143 $condition->where( 'start_date', '>=', $financial_start_date );
144 $condition->where( 'end_date', '<=', $financial_end_date );
145 $condition->where( 'user_id', '=', $user_id );
146 $condition->where( 'policy_id', '=', $policy_id );
147 $condition->where( 'status', '!=', 3 );
148 } );
149
150 $user_enti_count = $user_request->sum( 'days' );
151 $policy_count = $policy->find( $policy_id )->toArray();
152 $working_day = erp_hr_get_work_days_without_off_day( $start_date, $end_date );//erp_hr_get_work_days_between_dates( $start_date, $end_date );erp_hr_get_work_days_without_holiday
153 $apply_days = $working_day['total'] + $user_enti_count;
154
155 if ( $apply_days > $policy_count['value'] ) {
156 return false;
157 }
158
159 return true;
160 }
161
162 /**
163 * Leave request time checking the apply date duration with the financial date duration
164 *
165 * @since 0.1
166 *
167 * @param string $start_date
168 * @param string $end_date
169 *
170 * @return boolean
171 */
172 function erp_hrm_is_valid_leave_date_range_within_financial_date_range( $start_date, $end_date ) {
173
174 $financial_start_date = date( 'Y-m-d', strtotime( erp_financial_start_date() ) );
175 $financial_end_date = date( 'Y-m-d', strtotime( erp_financial_end_date() ) );
176 $apply_start_date = date( 'Y-m-d', strtotime( $start_date ) );
177 $apply_end_date = date( 'Y-m-d', strtotime( $end_date ) );
178
179 if ( $financial_start_date > $apply_start_date || $apply_start_date > $financial_end_date ) {
180 return false;
181 }
182
183 if ( $financial_start_date > $apply_end_date || $apply_end_date > $financial_end_date ) {
184 return false;
185 }
186
187 return true;
188 }
189
190 /**
191 * Insert a new leave policy
192 *
193 * @since 0.1
194 *
195 * @param array $args
196 *
197 * @return int $policy_id
198 */
199 function erp_hr_leave_insert_policy( $args = array() ) {
200 $defaults = array(
201 'id' => null,
202 'name' => '',
203 'value' => 0,
204 'color' => '',
205 'description' => '',
206 );
207
208 $args = wp_parse_args( $args, $defaults );
209
210 // some validation
211 if ( empty( $args['name'] ) ) {
212 return new WP_Error( 'no-name', __( 'No name provided.', 'erp' ) );
213 }
214
215 $exist = erp_hr_leave_get_policy_by_name( $args['name'] );
216 if ( $exist && $args['id'] !== $exist->id ) {
217 return new WP_Error( 'policy_name_exists', __( 'Policy name already exists, please use a different one.', 'erp' ) );
218 }
219
220 if ( ! intval( $args['value'] ) ) {
221 return new WP_Error( 'no-value', __( 'No duration provided.', 'erp' ) );
222 }
223
224 $args['name'] = sanitize_text_field( $args['name'] );
225
226 $policy_id = (int) $args['id'];
227 unset( $args['id'] );
228
229 $leave_policies = new \WeDevs\ERP\HRM\Models\Leave_Policies();
230
231 if ( ! $policy_id ) {
232 // insert a new
233 $leave_policy = $leave_policies->create( $args );
234
235 if ( ! empty( $leave_policy ) ) {
236 do_action( 'erp_hr_leave_policy_new', $leave_policy, $args );
237 }
238
239 } else {
240 do_action( 'erp_hr_leave_before_policy_updated', $policy_id, $args );
241
242 if ( $leave_policies->find( $policy_id )->update( $args ) ) {
243 do_action( 'erp_hr_leave_after_policy_updated', $policy_id, $args );
244
245 return $policy_id;
246 }
247 }
248 }
249
250 /**
251 * Apply policy in existing employee
252 *
253 * @since 0.1
254 * @since 1.2.0 Using `erp_hr_apply_policy_to_employee` for both Immediate and
255 * Scheduled policy when `instant_apply` is true
256 *
257 * @param object $policy Leave_Policies model
258 * @param array $args
259 *
260 * @return void
261 */
262 function erp_hr_apply_policy_existing_employee( $policy, $args ) {
263 if ( ! erp_validate_boolean( $args['instant_apply'] ) ) {
264 return;
265 }
266
267 erp_bulk_policy_assign($policy);
268 // erp_hr_apply_policy_to_employee( $policy );
269 }
270
271 /**
272 * Apply a leave policy to all or filtered employees
273 *
274 * @since 1.2.0
275 *
276 * @param object $policy Leave_Policies eloquent object
277 * @param array $employee_ids Employee ids
278 *
279 * @return void
280 */
281 function erp_hr_apply_policy_to_employee( $policy, $employee_ids = [] ) {
282 if ( is_int( $policy ) ) {
283 $policy = $policy = \WeDevs\ERP\HRM\Models\Leave_Policies::find( $policy );
284 }
285
286 $db = \WeDevs\ORM\Eloquent\Facades\DB::instance();
287 $prefix = $db->db->prefix;
288
289 $employees = $db->table( 'erp_hr_employees as employee' )
290 ->select( 'employee.user_id' )
291 ->leftJoin( "{$prefix}usermeta as gender", function ( $join ) {
292 $join->on( 'employee.user_id', '=', 'gender.user_id' )->where( 'gender.meta_key', '=', 'gender' );
293 } )
294 ->leftJoin( "{$prefix}usermeta as marital_status", function ( $join ) {
295 $join->on( 'employee.user_id', '=', 'marital_status.user_id' )->where( 'marital_status.meta_key', '=', 'marital_status' );
296 } )
297 ->where( 'status', '=', 'active' );
298
299 if ( ! empty( $employee_ids ) && is_array( $employee_ids ) ) {
300 $employees->whereIn( 'employee.user_id', $employee_ids );
301 }
302
303 if ( $policy->department > 0 ) {
304 $employees->where( 'department', $policy->department );
305 }
306
307 if ( $policy->designation > 0 ) {
308 $employees->where( 'designation', $policy->designation );
309 }
310
311 if ( $policy->location > 0 ) {
312 $employees->where( 'location', $policy->location );
313 }
314
315 if ( $policy->gender != - 1 ) {
316 $employees->where( 'gender.meta_value', $policy->gender );
317 }
318
319 if ( $policy->marital != - 1 ) {
320 $employees->where( 'marital_status.meta_value', $policy->marital );
321 }
322
323 if ( $policy->activate == 2 && ! empty( $policy->execute_day ) ) {
324 $current_date = date( 'Y-m-d', current_time( 'timestamp' ) );
325
326 $employees->where(
327 $db->raw( "DATEDIFF( '$current_date', `employee`.`hiring_date` )" ), '>=', $policy->execute_day
328 );
329 }
330
331 $employees = $employees->get();
332
333 if ( ! empty( $employees ) ) {
334 foreach ( $employees as $employee ) {
335 erp_hr_apply_leave_policy( $employee->user_id, $policy );
336 }
337 }
338 }
339
340 /**
341 * Assign entitlement
342 *
343 * @since 0.1
344 * @since 1.2.0 Calculate from_date and to_date based on policy
345 * effective_date and financial start/end dates
346 *
347 * @param int $user_id
348 * @param object $leave_policy
349 *
350 * @return void
351 */
352 function erp_hr_apply_leave_policy( $user_id, $policy ) {
353 $financial_year = erp_get_financial_year_dates();
354
355 $from_date = ! empty( $policy->effective_date ) ? $policy->effective_date : $financial_year['start'];
356 $from_date_timestamp = strtotime( $from_date );
357 $financial_year_start_timestamp = strtotime( $financial_year['start'] );
358
359 if ( $from_date_timestamp < $financial_year_start_timestamp ) {
360 $from_date = date( 'Y-m-d 00:00:00', $financial_year_start_timestamp );
361 } else {
362 $from_date = date( 'Y-m-d 00:00:00', $from_date_timestamp );
363 }
364
365 $to_date = $financial_year['end'];
366 $financial_year_end_timestamp = strtotime( $financial_year['end'] );
367
368 if ( $from_date_timestamp > $financial_year_end_timestamp ) {
369 $financial_year_end_timestamp += YEAR_IN_SECONDS;
370 $to_date = date( 'Y-m-d 23:59:59', $financial_year_end_timestamp );
371 }
372
373 $policy = [
374 'user_id' => $user_id,
375 'policy_id' => $policy->id,
376 'days' => $policy->value,
377 'from_date' => $from_date,
378 'to_date' => $to_date,
379 'comments' => $policy->description
380 ];
381
382 erp_hr_leave_insert_entitlement( $policy );
383 }
384
385 /**
386 * Insert a new policy entitlement for an employee
387 *
388 * @since 0.1
389 * @since 1.2.0 Use `erp_get_financial_year_dates` for financial start and end dates
390 *
391 * @param array $args
392 *
393 * @return int|object New entitlement id or WP_Error object
394 */
395 function erp_hr_leave_insert_entitlement( $args = [] ) {
396 global $wpdb;
397
398 $financial_year = erp_get_financial_year_dates();
399
400 $defaults = array(
401 'id' => null,
402 'user_id' => 0,
403 'policy_id' => 0,
404 'days' => 0,
405 'from_date' => $financial_year['start'],
406 'to_date' => $financial_year['end'],
407 'comments' => '',
408 'status' => 1,
409 'created_by' => get_current_user_id(),
410 'created_on' => current_time( 'mysql' )
411 );
412
413 $fields = wp_parse_args( $args, $defaults );
414
415 if ( ! intval( $fields['user_id'] ) ) {
416 return new WP_Error( 'no-user', __( 'No employee provided.', 'erp' ) );
417 }
418
419 if ( ! intval( $fields['policy_id'] ) ) {
420 return new WP_Error( 'no-policy', __( 'No policy provided.', 'erp' ) );
421 }
422
423 if ( empty( $fields['from_date'] ) || empty( $fields['to_date'] ) ) {
424 return new WP_Error( 'no-date', __( 'No date provided.', 'erp' ) );
425 }
426
427 $entitlement = new \WeDevs\ERP\HRM\Models\Leave_Entitlement();
428 $user_id = intval( $fields['user_id'] );
429 $policy_id = intval( $fields['policy_id'] );
430
431 $entitlement = $entitlement->where( function ( $condition ) use ( $user_id, $policy_id, $fields, $financial_year ) {
432 $financial_start_date = $fields['from_date'] ? $fields['from_date'] : $financial_year['start'];
433 $financial_end_date = $fields['to_date'] ? $fields['to_date'] : $financial_year['end'];
434 $condition->where( 'from_date', '>=', $financial_start_date );
435 $condition->where( 'to_date', '<=', $financial_end_date );
436 $condition->where( 'user_id', '=', $user_id );
437 $condition->where( 'policy_id', '=', $policy_id );
438 } );
439
440 $existing_entitlement = $entitlement->get();
441
442 if ( $existing_entitlement->count() ) {
443 return $existing_entitlement->first()->id;
444 }
445
446 $wpdb->insert( $wpdb->prefix . 'erp_hr_leave_entitlements', $fields );
447
448 do_action( 'erp_hr_leave_insert_new_entitlement', $wpdb->insert_id, $fields );
449
450 return $wpdb->insert_id;
451 }
452
453 /**
454 * Apply `Immediately` type policies on new employee
455 *
456 * @since 1.2.0
457 *
458 * @param int $user_id Employee user_id provided by `erp_hr_employee_new` hook
459 *
460 * @return void
461 */
462 function erp_hr_apply_policy_on_new_employee( $user_id ) {
463 $policies = \WeDevs\ERP\HRM\Models\Leave_Policies::where( 'activate', 1 )->get();
464
465 $policies->each( function ( $policy ) use ( $user_id ) {
466 erp_hr_apply_policy_to_employee( $policy, [ $user_id ] );
467 } );
468 }
469
470 /**
471 * Apply `Scheduled` type policies on new employee
472 *
473 * @since 1.2.0
474 *
475 * @return void
476 */
477 function erp_hr_apply_scheduled_policies() {
478 $policies = \WeDevs\ERP\HRM\Models\Leave_Policies::where( 'activate', 2 )->get();
479
480 $policies->each( function ( $policy ) {
481 erp_hr_apply_policy_to_employee( $policy );
482 } );
483 }
484
485 /**
486 * Insert a leave holiday
487 *
488 * @since 0.1
489 *
490 * @param array $args
491 *
492 * @return integer [$holiday_id]
493 */
494 function erp_hr_leave_insert_holiday( $args = array() ) {
495
496 $defaults = array(
497 'id' => null,
498 'title' => '',
499 'start' => current_time( 'mysql' ),
500 'end' => '',
501 'description' => '',
502 );
503
504 $args = wp_parse_args( $args, $defaults );
505
506 // some validation
507 if ( empty( $args['title'] ) ) {
508 return new WP_Error( 'no-name', __( 'No title provided.', 'erp' ) );
509 }
510
511 if ( empty( $args['start'] ) ) {
512 return new WP_Error( 'no-value', __( 'No start date provided.', 'erp' ) );
513 }
514
515 if ( empty( $args['end'] ) ) {
516 return new WP_Error( 'no-value', __( 'No end date provided.', 'erp' ) );
517 }
518
519 $args['title'] = sanitize_text_field( $args['title'] );
520
521 $holiday_id = (int) $args['id'];
522 unset( $args['id'] );
523
524 $holiday = new \WeDevs\ERP\HRM\Models\Leave_Holiday();
525
526 if ( ! $holiday_id ) {
527 // insert a new
528 $leave_policy = $holiday->create( $args );
529
530 if ( $leave_policy ) {
531 do_action( 'erp_hr_new_holiday', $leave_policy->insert_id, $args );
532
533 return $leave_policy->id;
534 }
535
536 } else {
537 do_action( 'erp_hr_before_update_holiday', $holiday_id, $args );
538
539 if ( $holiday->find( $holiday_id )->update( $args ) ) {
540 do_action( 'erp_hr_after_update_holiday', $holiday_id, $args );
541
542 return $holiday_id;
543 }
544 }
545 }
546
547 /**
548 * Get all leave policies with different condition
549 *
550 * @since 0.1
551 *
552 * @param array $args
553 *
554 * @return array
555 */
556 function erp_hr_leave_get_policies( $args = array() ) {
557
558 $defaults = array(
559 'number' => 20,
560 'offset' => 0,
561 'orderby' => 'name',
562 'order' => 'ASC',
563 );
564
565 $args = wp_parse_args( $args, $defaults );
566
567
568 $cache_key = 'erp-leave-pol';
569 $policies = wp_cache_get( $cache_key, 'erp' );
570
571 if ( false === $policies ) {
572
573 $policies = erp_array_to_object(
574 \WeDevs\ERP\HRM\Models\Leave_Policies::select( array(
575 'id',
576 'name',
577 'value',
578 'color',
579 'department',
580 'designation',
581 'gender',
582 'marital',
583 'activate',
584 'execute_day',
585 'effective_date',
586 'location',
587 'description'
588 ) )
589 ->skip( $args['offset'] )
590 ->take( $args['number'] )
591 ->orderBy( $args['orderby'], $args['order'] )
592 ->get()
593 ->toArray()
594 );
595
596 wp_cache_set( $cache_key, $policies, 'erp' );
597 }
598
599 return $policies;
600 }
601
602 /**
603 * Fetch a leave policy by match policy name
604 *
605 * @since 1.3.2
606 *
607 * @param string $name
608 *
609 * @return \stdClass
610 */
611 function erp_hr_leave_get_policy_by_name( $name ) {
612 return \WeDevs\ERP\HRM\Models\Leave_Policies::where( 'name', $name )->first();
613 }
614
615 /**
616 * Fetch a leave policy by policy id
617 *
618 * @since 0.1
619 * @since 1.2.0 Return Eloquent Leave_Policies model
620 *
621 * @param integer $policy_id
622 *
623 * @return \stdClass
624 */
625 function erp_hr_leave_get_policy( $policy_id ) {
626 return \WeDevs\ERP\HRM\Models\Leave_Policies::find( $policy_id );
627 }
628
629 /**
630 * Count total leave policies
631 *
632 * @since 0.1
633 *
634 * @return integer
635 */
636 function erp_hr_count_leave_policies() {
637 return \WeDevs\ERP\HRM\Models\Leave_Policies::count();
638 }
639
640 /**
641 * Fetch all holidays by company
642 *
643 * @since 0.1
644 *
645 * @param array $args
646 *
647 * @return array
648 */
649 function erp_hr_get_holidays( $args = [] ) {
650
651 $defaults = array(
652 'number' => 20,
653 'offset' => 0,
654 'orderby' => 'created_at',
655 'order' => 'DESC',
656 );
657
658 $args = wp_parse_args( $args, $defaults );
659
660 $holiday = new \WeDevs\ERP\HRM\Models\Leave_Holiday();
661
662 $holiday_results = $holiday->select( array( 'id', 'title', 'start', 'end', 'description' ) );
663
664 $holiday_results = erp_hr_holiday_filter_param( $holiday_results, $args );
665
666 //if not search then execute nex code
667 if ( isset( $args['id'] ) && ! empty( $args['id'] ) ) {
668 $id = intval( $args['id'] );
669 $holiday_results = $holiday_results->where( 'id', '=', "$id" );
670 }
671
672 $cache_key = 'erp-get-holidays-' . md5( serialize( $args ) );
673 $holidays = wp_cache_get( $cache_key, 'erp' );
674
675 if ( false === $holidays ) {
676
677 if ( $args['number'] == '-1' ) {
678 $holidays = erp_array_to_object( $holiday_results->get()->toArray() );
679 } else {
680 $holidays = erp_array_to_object(
681 $holiday_results->skip( $args['offset'] )
682 ->take( $args['number'] )
683 ->orderBy( $args['orderby'], $args['order'] )
684 ->get()
685 ->toArray()
686 );
687 }
688
689 wp_cache_set( $cache_key, $holidays, 'erp' );
690 }
691
692 return $holidays;
693 }
694
695 /**
696 * Count total holidays
697 *
698 * @since 0.1
699 *
700 * @return \stdClass
701 */
702 function erp_hr_count_holidays( $args ) {
703
704 $holiday = new \WeDevs\ERP\HRM\Models\Leave_Holiday();
705 $holiday = erp_hr_holiday_filter_param( $holiday, $args );
706
707 return $holiday->count();
708 }
709
710 /**
711 * Filter parameter for holidays
712 *
713 * @since 0.1
714 *
715 * @param object $holiday
716 * @param array $args
717 *
718 * @return object
719 */
720 function erp_hr_holiday_filter_param( $holiday, $args ) {
721
722 $args_s = isset( $args['s'] ) ? $args['s'] : '';
723
724 if ( $args_s && ! empty( $args['s'] ) ) {
725 $holiday = $holiday->where( 'title', 'LIKE', "%$args_s%" );
726 }
727
728 if ( isset( $args['from'] ) && ! empty( $args['from'] ) ) {
729 $holiday = $holiday->where( 'start', '>=', $args['from'] );
730 }
731
732 if ( isset( $args['to'] ) && ! empty( $args['to'] ) ) {
733 $holiday = $holiday->where( 'end', '<=', $args['to'] );
734 }
735
736 if ( isset( $args['s'] ) && ! empty( $args['s'] ) ) {
737 $holiday = $holiday->orWhere( 'description', 'LIKE', "%$args_s%" );
738 }
739
740 return $holiday;
741 }
742
743 /**
744 * Remove holidays
745 *
746 * @since 0.1
747 *
748 * @return \stdClass
749 */
750 function erp_hr_delete_holidays( $holidays_id ) {
751
752 if ( is_array( $holidays_id ) ) {
753
754 foreach ( $holidays_id as $key => $holiday_id ) {
755 do_action( 'erp_hr_leave_holiday_delete', $holiday_id );
756 }
757
758 \WeDevs\ERP\HRM\Models\Leave_Holiday::destroy( $holidays_id );
759
760 } else {
761 do_action( 'erp_hr_leave_holiday_delete', $holidays_id );
762
763 return \WeDevs\ERP\HRM\Models\Leave_Holiday::find( $holidays_id )->delete();
764 }
765 }
766
767 /**
768 * Get policies as formatted for dropdown
769 *
770 * @since 0.1
771 *
772 * @return array
773 */
774 function erp_hr_leave_get_policies_dropdown_raw() {
775 $policies = erp_hr_leave_get_policies();
776 $dropdown = array();
777
778 foreach ( $policies as $policy ) {
779 $dropdown[ $policy->id ] = stripslashes( $policy->name );
780 }
781
782 return $dropdown;
783 }
784
785 /**
786 * Delete a policy
787 *
788 * @since 0.1
789 *
790 * @param int|array $policy_ids
791 *
792 * @return void
793 */
794 function erp_hr_leave_policy_delete( $policy_ids ) {
795 if ( ! is_array( $policy_ids ) ) {
796 $policy_ids = [ $policy_ids ];
797 }
798
799 $policies = \WeDevs\ERP\HRM\Models\Leave_Policies::find( $policy_ids );
800
801 $policies->each( function ( $policy ) {
802 $has_request = $policy->leave_requests()->count();
803
804 if ( ! $has_request ) {
805 $policy->entitlements()->delete();
806 $policy->delete();
807
808 do_action( 'erp_hr_leave_policy_delete', $policy );
809 }
810 } );
811 }
812
813
814 /**
815 * Get assign policies according to employee entitlement
816 *
817 * @since 0.1
818 *
819 * @param integer $employee_id
820 *
821 * @return boolean|array
822 */
823 function erp_hr_get_assign_policy_from_entitlement( $employee_id ) {
824
825 global $wpdb;
826
827 $data = [];
828 $dropdown = [];
829 $policy = new \WeDevs\ERP\HRM\Models\Leave_Policies();
830 $en = new \WeDevs\ERP\HRM\Models\Leave_Entitlement();
831 $policy_tb = $wpdb->prefix . 'erp_hr_leave_policies';
832 $en_tb = $wpdb->prefix . 'erp_hr_leave_entitlements';
833
834 $financial_start_date = erp_financial_start_date();
835 $financial_end_date = erp_financial_end_date();
836
837 $policies = \WeDevs\ERP\HRM\Models\Leave_Policies::select( $policy_tb . '.name', $policy_tb . '.id' )
838 ->leftjoin( $en_tb, $en_tb . '.policy_id', '=', $policy_tb . '.id' )
839 ->where( $en_tb . '.user_id', $employee_id )
840 ->where( 'from_date', '>=', $financial_start_date )
841 ->where( 'to_date', '<=', $financial_end_date )
842 ->distinct()
843 ->get()
844 ->toArray();
845
846 if ( ! empty( $policies ) ) {
847 foreach ( $policies as $policy ) {
848 $dropdown[ $policy['id'] ] = stripslashes( $policy['name'] );
849 }
850
851 return $dropdown;
852 }
853
854 return false;
855 }
856
857 /**
858 * Add a new leave request
859 *
860 * @since 0.1
861 *
862 * @param array $args
863 *
864 * @return integet request_id
865 */
866 function erp_hr_leave_insert_request( $args = array() ) {
867 global $wpdb;
868
869 $defaults = array(
870 'user_id' => 0,
871 'leave_policy' => 0,
872 'start_date' => current_time( 'mysql' ),
873 'end_date' => current_time( 'mysql' ),
874 'reason' => '',
875 'status' => 0
876 );
877
878 $args = wp_parse_args( $args, $defaults );
879 extract( $args );
880
881 if ( ! intval( $user_id ) ) {
882 return new WP_Error( 'no-employee', __( 'No employee ID provided.', 'erp' ) );
883 }
884
885 if ( ! intval( $leave_policy ) ) {
886 return new WP_Error( 'no-policy', __( 'No leave policy provided.', 'erp' ) );
887 }
888
889 $period = erp_hr_get_work_days_between_dates( $start_date, $end_date );
890
891 if ( is_wp_error( $period ) ) {
892 return $period;
893 }
894
895 // prepare the periods
896 $leaves = array();
897 if ( $period['days'] ) {
898 foreach ( $period['days'] as $date ) {
899 if ( ! $date['count'] ) {
900 continue;
901 }
902
903 $leaves[] = array(
904 'date' => $date['date'],
905 'length_hours' => '08:00:00',
906 'length_days' => '1.00',
907 'start_time' => '00:00:00',
908 'end_time' => '00:00:00',
909 'duration_type' => 1
910 );
911 }
912 }
913
914 if ( $leaves ) {
915
916 $request = apply_filters( 'erp_hr_leave_new_args', [
917 'user_id' => $user_id,
918 'policy_id' => $leave_policy,
919 'days' => count( $leaves ),
920 'start_date' => $start_date,
921 'end_date' => $end_date,
922 'reason' => wp_kses_post( $reason ),
923 'status' => 2, // default is pending
924 'created_by' => get_current_user_id(),
925 'created_on' => current_time( 'mysql' ),
926 ] );
927
928 if ( $wpdb->insert( $wpdb->prefix . 'erp_hr_leave_requests', $request ) ) {
929 $request_id = $wpdb->insert_id;
930
931 foreach ( $leaves as $leave ) {
932 $leave['request_id'] = $request_id;
933
934 $wpdb->insert( $wpdb->prefix . 'erp_hr_leaves', $leave );
935 }
936
937 do_action( 'erp_hr_leave_new', $request_id, $request, $leaves );
938
939 return $request_id;
940 }
941 }
942
943 return false;
944 }
945
946 /**
947 * Fetch a single request
948 *
949 * @param int $request_id
950 *
951 * @return object
952 */
953 function erp_hr_get_leave_request( $request_id ) {
954 global $wpdb;
955
956 $sql = "SELECT req.id, req.user_id, u.display_name, req.policy_id, pol.name as policy_name, req.status, req.reason, req.comments, req.created_on, req.days, req.start_date, req.end_date
957 FROM {$wpdb->prefix}erp_hr_leave_requests AS req
958 LEFT JOIN {$wpdb->prefix}erp_hr_leave_policies AS pol ON pol.id = req.policy_id
959 LEFT JOIN $wpdb->users AS u ON req.user_id = u.ID
960 WHERE req.id = %d";
961
962 $row = $wpdb->get_row( $wpdb->prepare( $sql, $request_id ) );
963
964 return $row;
965 }
966
967 /**
968 * Fetch the leave requests
969 *
970 * @since 0.1
971 *
972 * @param array $args
973 *
974 * @return array
975 */
976 function erp_hr_get_leave_requests( $args = array() ) {
977 global $wpdb;
978
979 $defaults = array(
980 'user_id' => 0,
981 'policy_id' => 0,
982 'status' => 1,
983 'year' => date( 'Y' ),
984 'number' => 20,
985 'offset' => 0,
986 'orderby' => 'created_on',
987 'order' => 'DESC',
988 'start_date' => '',
989 'end_date' => ''
990 );
991
992 $args = wp_parse_args( $args, $defaults );
993 $where = '';
994
995 // Check if the row want to search
996 if ( ! empty( $args['s'] ) ) {
997 $where .= " WHERE u.display_name LIKE '%{$args['s']}%'";
998 }
999
1000 if ( 'all' != $args['status'] && $args['status'] != '' ) {
1001
1002 if ( empty( $where ) ) {
1003 $where .= " WHERE";
1004 } else {
1005 $where .= " AND";
1006 }
1007
1008 if ( is_array( $args['status'] ) ) {
1009 $where .= " `status` IN(" . implode( ",", array_map( 'intval', $args['status'] ) ) . ") ";
1010 } else {
1011 $where .= " `status` = " . intval( $args['status'] ) . " ";
1012 }
1013 }
1014
1015 if ( $args['user_id'] != '0' ) {
1016
1017 if ( empty( $where ) ) {
1018 $where .= " WHERE req.user_id = " . intval( $args['user_id'] );
1019 } else {
1020 $where .= " AND req.user_id = " . intval( $args['user_id'] );
1021 }
1022 }
1023
1024 if ( $args['policy_id'] ) {
1025
1026 if ( empty( $where ) ) {
1027 $where .= " WHERE req.policy_id = " . intval( $args['policy_id'] );
1028 } else {
1029 $where .= " AND req.policy_id = " . intval( $args['policy_id'] );
1030 }
1031 }
1032
1033 if ( ! empty( $args['year'] ) ) {
1034 $from_date = $args['year'] . '-01-01';
1035 $to_date = $args['year'] . '-12-31';
1036
1037 if ( empty( $where ) ) {
1038 $where .= " WHERE req.start_date >= date('$from_date') AND req.start_date <= date('$to_date')";
1039 } else {
1040 $where .= " AND req.start_date >= date('$from_date') AND req.start_date <= date('$to_date')";
1041 }
1042 }
1043
1044 if ( $args['start_date'] && $args['end_date'] ) {
1045 $from_date = $args['start_date'];
1046 $to_date = $args['end_date'];
1047
1048 if ( empty( $where ) ) {
1049 $where .= " WHERE req.start_date >= date('$from_date') AND req.start_date <= date('$to_date')";
1050 } else {
1051 $where .= " AND req.start_date >= date('$from_date') AND req.start_date <= date('$to_date')";
1052 }
1053 }
1054
1055 $cache_key = 'erp_hr_leave_requests_' . md5( serialize( $args ) );
1056 $requests = wp_cache_get( $cache_key, 'erp' );
1057 $limit = $args['number'] == '-1' ? '' : 'LIMIT %d, %d';
1058
1059 $sql = "SELECT req.id, req.user_id, u.display_name, req.policy_id, pol.name as policy_name, req.status, req.reason, req.comments, req.created_on, req.days, req.start_date, req.end_date
1060 FROM {$wpdb->prefix}erp_hr_leave_requests AS req
1061 LEFT JOIN {$wpdb->prefix}erp_hr_leave_policies AS pol ON pol.id = req.policy_id
1062 LEFT JOIN $wpdb->users AS u ON req.user_id = u.ID
1063 $where
1064 ORDER BY {$args['orderby']} {$args['order']}
1065 $limit";
1066
1067 if ( $requests === false ) {
1068 if ( $args['number'] == '-1' ) {
1069 $requests = $wpdb->get_results( $sql );
1070 } else {
1071 $requests = $wpdb->get_results( $wpdb->prepare( $sql, absint( $args['offset'] ), absint( $args['number'] ) ) );
1072 }
1073 wp_cache_set( $cache_key, $requests, 'erp', HOUR_IN_SECONDS );
1074 }
1075
1076 return $requests;
1077 }
1078
1079 /**
1080 * Get leave requests count
1081 *
1082 * @since 0.1
1083 *
1084 * @return array
1085 */
1086 function erp_hr_leave_get_requests_count() {
1087 global $wpdb;
1088
1089 $statuses = erp_hr_leave_request_get_statuses();
1090 $counts = array();
1091
1092 foreach ( $statuses as $status => $label ) {
1093 $counts[ $status ] = array( 'count' => 0, 'label' => $label );
1094 }
1095
1096 $cache_key = 'erp-hr-leave-request-counts';
1097 $results = wp_cache_get( $cache_key, 'erp' );
1098
1099 if ( false === $results ) {
1100 $sql = "SELECT status, COUNT(id) as num FROM {$wpdb->prefix}erp_hr_leave_requests WHERE status != 0 GROUP BY status;";
1101 $results = $wpdb->get_results( $sql );
1102
1103 wp_cache_set( $cache_key, $results, 'erp' );
1104 }
1105
1106 foreach ( $results as $row ) {
1107 if ( array_key_exists( $row->status, $counts ) ) {
1108 $counts[ $row->status ]['count'] = (int) $row->num;
1109 }
1110
1111 $counts['all']['count'] += (int) $row->num;
1112 }
1113
1114 return $counts;
1115 }
1116
1117 /**
1118 * Update leave request status
1119 *
1120 * Statuses and their ids
1121 * delete - 3
1122 * reject - 3
1123 * approve - 1
1124 * pending - 2
1125 *
1126 * @since 0.1
1127 *
1128 * @param integer $request_id
1129 * @param string $status
1130 *
1131 * @return object Eloquent Leave_request model
1132 */
1133 function erp_hr_leave_request_update_status( $request_id, $status ) {
1134 $request = \WeDevs\ERP\HRM\Models\Leave_request::find( $request_id );
1135 if ( empty( $request ) ) {
1136 return new WP_Error( 'no-request-found', __( 'Invalid leave request', 'erp' ) );
1137 }
1138 $status = absint( $status );
1139 $existing_request = erp_hr_get_leave_requests( array(
1140 'user_id' => $request->user_id,
1141 'status' => 1,
1142 'start_date' => $request->start_date,
1143 'end_date' => $request->end_date,
1144 ) );
1145
1146 if ( count( $existing_request ) ) {
1147 return new WP_Error( 'existing-request-found', __( 'Leave request already approved', 'erp' ) );
1148 }
1149
1150 $request->status = $status;
1151 $request->updated_by = get_current_user_id();
1152 $request->save();
1153
1154 // notification email
1155 if ( 1 === $status ) {
1156
1157 $approved_email = wperp()->emailer->get_email( 'Approved_Leave_Request' );
1158
1159 if ( is_a( $approved_email, '\WeDevs\ERP\Email' ) ) {
1160 $approved_email->trigger( $request_id );
1161 }
1162
1163 } else if ( 3 === $status ) {
1164
1165 $rejected_email = wperp()->emailer->get_email( 'Rejected_Leave_Request' );
1166
1167 if ( is_a( $rejected_email, '\WeDevs\ERP\Email' ) ) {
1168 $rejected_email->trigger( $request_id );
1169 }
1170 }
1171
1172 $status = ( $status == 1 ) ? 'approved' : 'pending';
1173
1174 do_action( "erp_hr_leave_request_{$status}", $request_id, $request );
1175
1176 return $request;
1177 }
1178
1179 /**
1180 * Get leave requests status
1181 *
1182 * @since 0.1
1183 *
1184 * @param int|boolean $status
1185 *
1186 * @return array|string
1187 */
1188 function erp_hr_leave_request_get_statuses( $status = false ) {
1189 $statuses = array(
1190 'all' => __( 'All', 'erp' ),
1191 '1' => __( 'Approved', 'erp' ),
1192 '2' => __( 'Pending', 'erp' ),
1193 '3' => __( 'Rejected', 'erp' )
1194 );
1195
1196 if ( false !== $status && array_key_exists( $status, $statuses ) ) {
1197 return $statuses[ $status ];
1198 }
1199
1200 return $statuses;
1201 }
1202
1203 /**
1204 * Entitlement checking
1205 *
1206 * Check if an employee has already entitled to a policy in
1207 * a certain calendar year
1208 *
1209 * @since 0.1
1210 *
1211 * @param integer $employee_id
1212 * @param integer $policy_id
1213 * @param integer $year
1214 *
1215 * @return bool
1216 */
1217 function erp_hr_leave_has_employee_entitlement( $employee_id, $policy_id, $year ) {
1218 global $wpdb;
1219
1220 $from_date = $year . '-01-01';
1221 $to_date = $year . '-12-31';
1222
1223 $query = "SELECT id FROM {$wpdb->prefix}erp_hr_leave_entitlements
1224 WHERE user_id = %d AND policy_id = %d AND from_date = %s AND to_date = %s";
1225 $result = $wpdb->get_var( $wpdb->prepare( $query, $employee_id, $policy_id, $from_date, $to_date ) );
1226
1227 return $result;
1228 }
1229
1230 /**
1231 * Get all the leave entitlements of a calendar year
1232 *
1233 * @since 0.1
1234 * @since 1.2.0 Depricate `year` arg and using `from_date` and `to_date` instead
1235 *
1236 * @param integer $year
1237 *
1238 * @return array
1239 */
1240 function erp_hr_leave_get_entitlements( $args = array() ) {
1241 global $wpdb;
1242
1243 $financial_year_dates = erp_get_financial_year_dates();
1244
1245 $defaults = array(
1246 'employee_id' => 0,
1247 'policy_id' => 0,
1248 'from_date' => $financial_year_dates['start'],
1249 'to_date' => $financial_year_dates['end'],
1250 'number' => 20,
1251 'offset' => 0,
1252 'orderby' => 'en.user_id, en.created_on',
1253 'order' => 'DESC',
1254 'debug' => false
1255 );
1256
1257 $args = wp_parse_args( $args, $defaults );
1258 $where = 'WHERE 1 = 1';
1259
1260 /**
1261 * @deprecated 1.2.0 Use $args['from_date'] and $args['to_date'] instead
1262 */
1263 if ( ! empty( $args['year'] ) ) {
1264 $from_date = date( $args['year'] . '-m-d H:i:s', strtotime( $financial_year_dates['start'] ) );
1265 $to_date = date( $args['year'] . '-m-d H:i:s', strtotime( $financial_year_dates['end'] ) );
1266 $where .= " AND en.from_date >= date('$from_date') AND en.to_date <= date('$to_date')";
1267 }
1268
1269 if ( ! empty( $args['from_date'] ) ) {
1270 $where .= ' AND en.from_date >= "' . $args['from_date'] . '"';
1271 }
1272
1273 if ( ! empty( $args['to_date'] ) ) {
1274 $where .= ' AND en.to_date <= "' . $args['to_date'] . '"';
1275 }
1276
1277 if ( $args['employee_id'] ) {
1278 $where .= " AND en.user_id = " . intval( $args['employee_id'] );
1279 }
1280
1281 if ( ! empty( $args['search'] ) ) {
1282 $where .= $wpdb->prepare(" AND u.display_name LIKE '%%%s%%' ", $args['search'] );
1283 }
1284
1285 if ( $args['policy_id'] ) {
1286 $where .= " AND en.policy_id = " . intval( $args['policy_id'] );
1287 }
1288
1289 $query = "SELECT en.*, u.display_name as employee_name, pol.name as policy_name
1290 FROM `{$wpdb->prefix}erp_hr_leave_entitlements` AS en
1291 LEFT JOIN {$wpdb->prefix}erp_hr_leave_policies AS pol ON pol.id = en.policy_id
1292 LEFT JOIN {$wpdb->users} AS u ON en.user_id = u.ID
1293 $where
1294 ORDER BY {$args['orderby']} {$args['order']}
1295 LIMIT %d,%d;";
1296
1297 $sql = $wpdb->prepare( $query, absint( $args['offset'] ), absint( $args['number'] ) );
1298 $results = $wpdb->get_results( $sql );
1299
1300 return $results;
1301 }
1302
1303 /**
1304 * Count leave entitlement
1305 *
1306 * @since 0.1
1307 *
1308 * @param array $args
1309 *
1310 * @return integer
1311 */
1312 function erp_hr_leave_count_entitlements( $args = array() ) {
1313 $financial_year_dates = erp_get_financial_year_dates();
1314
1315 $defaults = [
1316 'from_date' => $financial_year_dates['start'],
1317 'to_date' => $financial_year_dates['end']
1318 ];
1319
1320 $args = wp_parse_args( $args, $defaults );
1321
1322 return \WeDevs\ERP\HRM\Models\Leave_Entitlement::where( 'from_date', '>=', $args['from_date'] )
1323 ->where( 'to_date', '<=', $args['to_date'] )
1324 ->count();
1325 }
1326
1327 /**
1328 * Delete entitlement with leave request
1329 *
1330 * @since 0.1
1331 *
1332 * @param integer $id
1333 * @param integer $user_id
1334 * @param integer $policy_id
1335 *
1336 * @return void
1337 */
1338 function erp_hr_delete_entitlement( $id, $user_id, $policy_id ) {
1339 // $leave_recored = \WeDevs\ERP\HRM\Models\Leave_request::where( 'user_id', '=', $user_id )
1340 // ->where( 'policy_id', '=', $policy_id )->get()->toArray();
1341 // $leave_recored = wp_list_pluck( $leave_recored, 'status' );
1342 //
1343 // if ( in_array( '1', $leave_recored ) ) {
1344 // return;
1345 // }
1346 //
1347 // if ( \WeDevs\ERP\HRM\Models\Leave_Entitlement::find( $id )->delete() ) {
1348 // return \WeDevs\ERP\HRM\Models\Leave_request::where( 'user_id', '=', $user_id )
1349 // ->where( 'policy_id', '=', $policy_id )
1350 // ->delete();
1351 // }
1352
1353 return \WeDevs\ERP\HRM\Models\Leave_Entitlement::find( $id )->delete();
1354 }
1355
1356 /**
1357 * Erp get leave balance
1358 *
1359 * @since 0.1
1360 * @since 1.1.18 Add start_date in where clause
1361 * @since 1.2.1 Fix main query statement
1362 *
1363 * @param integer $user_id
1364 *
1365 * @return float|boolean
1366 */
1367 function erp_hr_leave_get_balance( $user_id ) {
1368 global $wpdb;
1369
1370 $query = "select policy_id, days";
1371 $query .= " from {$wpdb->prefix}erp_hr_leave_entitlements";
1372 $query .= " where user_id = %d";
1373
1374 $results = $wpdb->get_results( $wpdb->prepare( $query, $user_id ) );
1375
1376 $balance = [];
1377
1378 if ( ! empty( $results ) ) {
1379 foreach ( $results as $result ) {
1380 $balance[ $result->policy_id ] = array(
1381 'policy_id' => $result->policy_id,
1382 'scheduled' => 0,
1383 'entitlement' => $result->days,
1384 'total' => 0,
1385 'available' => $result->days
1386 );
1387 }
1388 }
1389
1390 $financial_start_date = erp_financial_start_date();
1391 $financial_end_date = erp_financial_end_date();
1392
1393 $query = "SELECT req.id, req.days, req.policy_id, req.start_date, en.days as entitlement";
1394 $query .= " FROM {$wpdb->prefix}erp_hr_leave_requests AS req";
1395 $query .= " LEFT JOIN {$wpdb->prefix}erp_hr_leave_entitlements as en on (req.user_id = en.user_id and req.policy_id = en.policy_id and en.from_date >= '$financial_start_date' )";
1396 $query .= " WHERE req.status = 1 and req.user_id = %d AND ( req.start_date >= '$financial_start_date' AND req.end_date <= '$financial_end_date' )";
1397
1398 $sql = $wpdb->prepare( $query, $user_id );
1399 $results = $wpdb->get_results( $sql );
1400
1401 $temp = [];
1402 $current_time = current_time( 'timestamp' );
1403
1404 if ( $results ) {
1405 // group by policy
1406 foreach ( $results as $request ) {
1407 $temp[ $request->policy_id ][] = $request;
1408 }
1409
1410 // calculate each policy
1411 foreach ( $temp as $policy_id => $requests ) {
1412 $balance[ $policy_id ] = array(
1413 'policy_id' => $policy_id,
1414 'scheduled' => 0,
1415 'entitlement' => 0,
1416 'total' => 0,
1417 'available' => 0
1418 );
1419
1420 foreach ( $requests as $request ) {
1421 $balance[ $policy_id ]['entitlement'] = (int) $request->entitlement;
1422 $balance[ $policy_id ]['total'] += $request->days;
1423
1424 if ( $current_time < strtotime( $request->start_date ) ) {
1425 $balance[ $policy_id ]['scheduled'] += $request->days;
1426 }
1427 }
1428 }
1429
1430 // calculate available
1431 foreach ( $balance as &$policy ) {
1432 $available = $policy['entitlement'] - $policy['total'];
1433 $policy['available'] = $available < 0 ? 0 : $available;
1434 }
1435 }
1436
1437 return $balance;
1438 }
1439
1440 /**
1441 * Get cuurent month approve leave request list
1442 *
1443 * @since 0.1
1444 * @since 1.2.0 Ignore terminated employees
1445 * @since 1.2.2 Exclude past requests
1446 * Sort results by start_date
1447 *
1448 * @return array
1449 */
1450 function erp_hr_get_current_month_leave_list() {
1451 $db = new \WeDevs\ORM\Eloquent\Database();
1452 $prefix = $db->db->prefix;
1453
1454 return $db->table( 'erp_hr_leave_requests as req' )
1455 ->select( 'req.user_id', 'req.start_date', 'req.end_date', 'req.status', 'req.id' )
1456 ->leftJoin( "{$prefix}erp_hr_employees as em", 'req.user_id', '=', 'em.user_id' )
1457 ->where( 'em.status', '=', 'active' )
1458 ->whereDate( 'req.start_date', '>=', date( 'Y-m-d 00:00:00', current_time( 'timestamp' ) ) )
1459 ->whereDate( 'req.start_date', '<=', date( 'Y-m-d 23:59:59', strtotime( 'last day of this month' ) ) )
1460 ->where( 'req.status', 1 )
1461 ->orderBy( 'req.start_date', 'asc' )
1462 ->get();
1463 }
1464
1465 /**
1466 * Get next month leave request approved list
1467 *
1468 * @since 0.1
1469 * @since 1.2.0 Ignore terminated employees
1470 * @since 1.2.2 Sort results by start_date
1471 *
1472 * @return array
1473 */
1474 function erp_hr_get_next_month_leave_list() {
1475 $db = new \WeDevs\ORM\Eloquent\Database();
1476 $prefix = $db->db->prefix;
1477
1478 return $db->table( 'erp_hr_leave_requests as req' )
1479 ->select( 'req.user_id', 'req.start_date', 'req.end_date' )
1480 ->leftJoin( "{$prefix}erp_hr_employees as em", 'req.user_id', '=', 'em.user_id' )
1481 ->where( 'em.status', '!=', 'terminated' )
1482 ->where( 'req.start_date', '>=', date( 'Y-m-d 00:00:00', strtotime( 'first day of next month' ) ) )
1483 ->where( 'req.start_date', '<=', date( 'Y-m-d 23:59:59', strtotime( 'last day of next month' ) ) )
1484 ->where( 'req.status', 1 )
1485 ->orderBy( 'req.start_date', 'asc' )
1486 ->get();
1487 }
1488
1489
1490 /**
1491 * Leave period dropdown at entitlement create time
1492 *
1493 * @since 0.1
1494 *
1495 * @return void
1496 */
1497 function erp_hr_leave_period() {
1498
1499 $next_sart_date = date( 'Y-m-01 H:i:s', strtotime( '+1 year', strtotime( erp_financial_start_date() ) ) );
1500 $next_end_date = date( 'Y-m-t H:i:s', strtotime( '+1 year', strtotime( erp_financial_end_date() ) ) );
1501
1502 $date = [
1503 erp_financial_start_date() => erp_format_date( erp_financial_start_date() ) . ' - ' . erp_format_date( erp_financial_end_date() ),
1504 $next_sart_date => erp_format_date( $next_sart_date ) . ' - ' . erp_format_date( $next_end_date )
1505 ];
1506
1507 return $date;
1508 }
1509
1510 /**
1511 * Apply entitlement yearly
1512 *
1513 * @since 0.1
1514 *
1515 * @return void
1516 */
1517 function erp_hr_apply_entitlement_yearly() {
1518
1519 $financial_start_date = erp_financial_start_date();
1520 $financial_end_date = erp_financial_end_date();
1521
1522 $before_financial_start_date = date( 'Y-m-01 H:i:s', strtotime( '-1 year', strtotime( $financial_start_date ) ) );
1523 $before_financial_end_date = date( 'Y-m-t H:i:s', strtotime( '+11 month', strtotime( $before_financial_start_date ) ) );
1524
1525 $entitlement = new \WeDevs\ERP\HRM\Models\Leave_Entitlement();
1526
1527 $entitlement = $entitlement->where( function ( $condition ) use ( $before_financial_start_date, $before_financial_end_date ) {
1528 $condition->where( 'from_date', '>=', $before_financial_start_date );
1529 $condition->where( 'to_date', '<=', $before_financial_end_date );
1530 } );
1531
1532 $entitlements = $entitlement->get()->toArray();
1533
1534 foreach ( $entitlements as $key => $entitlement ) {
1535
1536 $policy = array(
1537 'user_id' => $entitlement['user_id'],
1538 'policy_id' => $entitlement['policy_id'],
1539 'days' => $entitlement['days'],
1540 'from_date' => erp_financial_start_date(),
1541 'to_date' => erp_financial_end_date(),
1542 'comments' => $entitlement['comments']
1543 );
1544
1545 erp_hr_leave_insert_entitlement( $policy );
1546 }
1547 }
1548
1549 /**
1550 * Get calendar leave events
1551 *
1552 * @param array|boolean $get filter args
1553 * @param int|boolean $user_id Get leaves for given user only
1554 * @param boolean $approved_only Get leaves which are approved
1555 *
1556 * @since 0.1
1557 *
1558 * @return array
1559 */
1560 function erp_hr_get_calendar_leave_events( $get = false, $user_id = false, $approved_only = false ) {
1561
1562 global $wpdb;
1563
1564 $employee_tb = $wpdb->prefix . 'erp_hr_employees';
1565 $users_tb = $wpdb->users;
1566 $request_tb = $wpdb->prefix . 'erp_hr_leave_requests';
1567 $policy_tb = $wpdb->prefix . 'erp_hr_leave_policies';
1568
1569 $employee = new \WeDevs\ERP\HRM\Models\Employee();
1570 $leave_request = new \WeDevs\ERP\HRM\Models\Leave_request();
1571
1572 $department = isset( $get['department'] ) && ! empty( $get['department'] ) && $get['department'] != '-1' ? intval( $get['department'] ) : false;
1573 $designation = isset( $get['designation'] ) && ! empty( $get['designation'] ) && $get['designation'] != '-1' ? intval( $get['designation'] ) : false;
1574
1575 $leave_request = $leave_request->where( $request_tb . '.status', '!=', 3 );
1576
1577 if ( ! $get ) {
1578 $request = $leave_request->leftJoin( $users_tb, $request_tb . '.user_id', '=', $users_tb . '.ID' )
1579 ->leftJoin( $policy_tb, $request_tb . '.policy_id', '=', $policy_tb . '.id' )
1580 ->select( $users_tb . '.display_name', $request_tb . '.*', $policy_tb . '.color' );
1581
1582 if ( $user_id ) {
1583 $request = $request->where( $request_tb . '.user_id', $user_id );
1584 }
1585
1586 if ( $approved_only ) {
1587 $request = $request->where( $request_tb . '.status', 1 );
1588 }
1589
1590 return erp_array_to_object( $request->get()->toArray() );
1591 }
1592
1593 if ( $department && $designation ) {
1594 $leave_requests = $leave_request->leftJoin( $employee_tb, $request_tb . '.user_id', '=', $employee_tb . '.user_id' )
1595 ->leftJoin( $users_tb, $request_tb . '.user_id', '=', $users_tb . '.ID' )
1596 ->leftJoin( $policy_tb, $request_tb . '.policy_id', '=', $policy_tb . '.id' )
1597 ->select( $users_tb . '.display_name', $request_tb . '.*', $policy_tb . '.color' )
1598 ->where( $employee_tb . '.designation', '=', $designation )
1599 ->where( $employee_tb . '.department', '=', $department );
1600
1601 if ( $approved_only ) {
1602 $leave_requests = $leave_requests->where( $request_tb . '.status', 1 );
1603 }
1604
1605 $leave_requests = erp_array_to_object( $leave_requests->get()->toArray() );
1606
1607 } else if ( $designation ) {
1608 $leave_requests = $leave_request->leftJoin( $employee_tb, $request_tb . '.user_id', '=', $employee_tb . '.user_id' )
1609 ->leftJoin( $users_tb, $request_tb . '.user_id', '=', $users_tb . '.ID' )
1610 ->leftJoin( $policy_tb, $request_tb . '.policy_id', '=', $policy_tb . '.id' )
1611 ->select( $users_tb . '.display_name', $request_tb . '.*', $policy_tb . '.color' )
1612 ->where( $employee_tb . '.designation', '=', $designation );
1613
1614 if ( $approved_only ) {
1615 $leave_requests = $leave_requests->where( $request_tb . '.status', 1 );
1616 }
1617
1618 $leave_requests = erp_array_to_object( $leave_requests->get()->toArray() );
1619
1620 } else if ( $department ) {
1621 $leave_requests = $leave_request->leftJoin( $employee_tb, $request_tb . '.user_id', '=', $employee_tb . '.user_id' )
1622 ->leftJoin( $users_tb, $request_tb . '.user_id', '=', $users_tb . '.ID' )
1623 ->leftJoin( $policy_tb, $request_tb . '.policy_id', '=', $policy_tb . '.id' )
1624 ->select( $users_tb . '.display_name', $request_tb . '.*', $policy_tb . '.color' )
1625 ->where( $employee_tb . '.department', '=', $department );
1626
1627 if ( $approved_only ) {
1628 $leave_requests = $leave_requests->where( $request_tb . '.status', 1 );
1629 }
1630
1631 $leave_requests = erp_array_to_object( $leave_requests->get()->toArray() );
1632 }
1633
1634 return $leave_requests;
1635 }
1636
1637 /**
1638 * Get year ranges based on available financial years
1639 *
1640 * @since 1.2.0
1641 *
1642 * @return array
1643 */
1644 function get_entitlement_financial_years() {
1645 $db = \WeDevs\ORM\Eloquent\Facades\DB::instance();
1646 $prefix = $db->db->prefix;
1647
1648 $min_max_dates = $db->table( 'erp_hr_leave_entitlements' )
1649 ->select( $db->raw( 'min( `from_date` ) as min' ), $db->raw( 'max( `to_date` ) max' ) )
1650 ->first();
1651
1652 $start_year = $end_year = current_time( 'Y' );
1653
1654 if ( ! empty( $min_max_dates->min ) ) {
1655 $min_date_fy_year = get_financial_year_from_date( $min_max_dates->min );
1656
1657 $start_year = $min_date_fy_year['start'];
1658 $end_year = date( 'Y', strtotime( $min_max_dates->max ) );
1659
1660 } else {
1661 return [];
1662 }
1663
1664 $start_month = erp_get_option( 'gen_financial_month', 'erp_settings_general', 1 );
1665
1666 $years = [];
1667
1668 for ( $i = $start_year; $i <= $end_year; $i ++ ) {
1669 if ( 1 === absint( $start_month ) ) {
1670 $years[] = $i;
1671
1672 } else if ( ! ( ( $i + 1 ) > $end_year ) ) {
1673 $years[] = $i . '-' . ( $i + 1 );
1674
1675 } else if ( $start_year === $end_year ) {
1676 $years[] = ( $i - 1 ) . '-' . $i;
1677 }
1678 }
1679
1680 return $years;
1681 }
1682
1683 /**
1684 * Generate leave reports
1685 *
1686 * @since 1.3.2
1687 *
1688 * @param array $employees
1689 * @param null $start_date
1690 * @param null $end_date
1691 *
1692 * @return array
1693 *
1694 */
1695 function erp_get_leave_report( array $employees, $start_date = null, $end_date = null ) {
1696 $year_dates = erp_get_financial_year_dates( date( 'Y-m-d' ) );
1697
1698 if ( ! $start_date ) {
1699 $start_date = $year_dates['start'];
1700 }
1701 if ( ! $end_date ) {
1702 $end_date = $year_dates['end'];
1703 }
1704 $employees_report = \WeDevs\ERP\HRM\Models\Employee::whereIn( 'user_id', $employees );
1705 $employees_report = $employees_report->select( 'user_id' )
1706 ->with( [
1707 'leave_requests' => function ( $q ) use ( $start_date, $end_date ) {
1708 $q->where( 'status', '=', '1' )
1709 ->whereDate( 'start_date', '>=', $start_date )
1710 ->whereDate( 'end_date', '<=', $end_date );
1711 },
1712 'entitlements' => function ( $q ) use ( $start_date, $end_date ) {
1713 $q->whereDate( 'from_date', '>=', $start_date )
1714 ->whereDate( 'to_date', '<=', $end_date )
1715 ->JoinWithPolicy();
1716 }
1717 ] )
1718 ->get();
1719
1720 $reports = [];
1721 foreach ( $employees_report as $employee_report ) {
1722 $entitlements = [];
1723 foreach ( $employee_report->entitlements as $entitlement ) {
1724 $report = [
1725 'entitlement_id' => $entitlement->id,
1726 'days' => $entitlement->days,
1727 'from_date' => $entitlement->from_date,
1728 'to_date' => $entitlement->to_date,
1729 'policy' => $entitlement->name,
1730 'policy_id' => $entitlement->policy_id,
1731 'color' => $entitlement->color,
1732 'spent' => 0,
1733 ];
1734
1735 $entitlements[ $entitlement->policy_id ] = $report;
1736 }
1737
1738 foreach ( $employee_report->leave_requests as $leave_report ) {
1739 if ( isset( $entitlements[ $leave_report->policy_id ] ) ) {
1740 $entitlements[ $leave_report->policy_id ]['spent'] += $leave_report->days;
1741 }
1742
1743 $reports[ $employee_report->user_id ] = $entitlements;
1744 }
1745
1746 }
1747
1748 return $reports;
1749 }
1750
1751 /**
1752 * Assign policy bulk
1753 *
1754 * @since 1.3.2
1755 *
1756 * @param $policy
1757 * @param array $employee_ids
1758 *
1759 */
1760 function erp_bulk_policy_assign( $policy, $employee_ids = [] ) {
1761 if ( is_int( $policy ) ) {
1762 $policy = \WeDevs\ERP\HRM\Models\Leave_Policies::find( $policy );
1763 }
1764
1765 $db = \WeDevs\ORM\Eloquent\Facades\DB::instance();
1766 $prefix = $db->db->prefix;
1767 global $wpdb;
1768
1769 $employees = $db->table( 'erp_hr_employees as employee' )
1770 ->select( 'employee.user_id' )
1771 ->leftJoin( "{$prefix}usermeta as gender", function ( $join ) {
1772 $join->on( 'employee.user_id', '=', 'gender.user_id' )->where( 'gender.meta_key', '=', 'gender' );
1773 } )
1774 ->leftJoin( "{$prefix}usermeta as marital_status", function ( $join ) {
1775 $join->on( 'employee.user_id', '=', 'marital_status.user_id' )->where( 'marital_status.meta_key', '=', 'marital_status' );
1776 } )
1777 ->where( 'status', '=', 'active' );
1778 if ( ! empty( $employee_ids ) && is_array( $employee_ids ) ) {
1779 $employees->whereIn( 'employee.user_id', $employee_ids );
1780 }
1781
1782 if ( $policy->department > 0 ) {
1783 $employees->where( 'department', $policy->department );
1784 }
1785
1786 if ( $policy->designation > 0 ) {
1787 $employees->where( 'designation', $policy->designation );
1788 }
1789
1790 if ( $policy->location > 0 ) {
1791 $employees->where( 'location', $policy->location );
1792 }
1793
1794 if ( $policy->gender != - 1 ) {
1795 $employees->where( 'gender.meta_value', $policy->gender );
1796 }
1797
1798 if ( $policy->marital != - 1 ) {
1799 $employees->where( 'marital_status.meta_value', $policy->marital );
1800 }
1801
1802 if ( $policy->activate == 2 && ! empty( $policy->execute_day ) ) {
1803 $current_date = date( 'Y-m-d', current_time( 'timestamp' ) );
1804
1805 $employees->where(
1806 $db->raw( "DATEDIFF( '$current_date', `employee`.`hiring_date` )" ), '>=', $policy->execute_day
1807 );
1808 }
1809
1810 $employees = $employees->get();
1811
1812 if ( empty( $employees ) ) {
1813 return;
1814 }
1815
1816 $financial_year = erp_get_financial_year_dates();
1817
1818 $from_date = ! empty( $policy->effective_date ) ? $policy->effective_date : $financial_year['start'];
1819 $from_date_timestamp = strtotime( $from_date );
1820 $financial_year_start_timestamp = strtotime( $financial_year['start'] );
1821
1822 if ( $from_date_timestamp < $financial_year_start_timestamp ) {
1823 $from_date = date( 'Y-m-d 00:00:00', $financial_year_start_timestamp );
1824 } else {
1825 $from_date = date( 'Y-m-d 00:00:00', $from_date_timestamp );
1826 }
1827
1828 $to_date = $financial_year['end'];
1829 $financial_year_end_timestamp = strtotime( $financial_year['end'] );
1830
1831 if ( $from_date_timestamp > $financial_year_end_timestamp ) {
1832 $financial_year_end_timestamp += YEAR_IN_SECONDS;
1833 $to_date = date( 'Y-m-d 23:59:59', $financial_year_end_timestamp );
1834 }
1835
1836
1837 $fields = [
1838 'user_id' => '',
1839 'policy_id' => $policy->id,
1840 'days' => $policy->value,
1841 'from_date' => $from_date,
1842 'to_date' => $to_date,
1843 'comments' => $policy->description,
1844 'id' => null,
1845 'status' => 1,
1846 'created_by' => get_current_user_id(),
1847 'created_on' => current_time( 'mysql' )
1848 ];
1849
1850 foreach ( $employees as $employee ) {
1851 $user_id = $employee->user_id;
1852 $fields['user_id'] = $user_id;
1853 $entitlement = new \WeDevs\ERP\HRM\Models\Leave_Entitlement();
1854 $entitlement = $entitlement->where( function ( $condition ) use ( $fields, $financial_year ) {
1855 $financial_start_date = $fields['from_date'] ? $fields['from_date'] : $financial_year['start'];
1856 $financial_end_date = $fields['to_date'] ? $fields['to_date'] : $financial_year['end'];
1857 $condition->where( 'from_date', '>=', $financial_start_date );
1858 $condition->where( 'to_date', '<=', $financial_end_date );
1859 $condition->where( 'user_id', '=', $fields['user_id'] );
1860 $condition->where( 'policy_id', '=', $fields['policy_id'] );
1861 } );
1862
1863 $existing_entitlement = $entitlement->first();
1864
1865 if ( $existing_entitlement ) {
1866 continue;
1867 }
1868
1869 $result = $wpdb->insert( $wpdb->prefix . 'erp_hr_leave_entitlements', $fields );
1870 if ( $result ) {
1871 do_action( 'erp_hr_leave_insert_new_entitlement', $wpdb->insert_id, $fields );
1872 }
1873
1874 }
1875
1876 }
1877