PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.12.6
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.12.6
1.17.9 1.17.8 1.17.7 1.17.6 1.17.5 1.17.4 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.11 1.3.12 1.3.13 1.3.14 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 143 releases
erp / includes / AddonTask.php

AddonTask.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.12.6, at includes/AddonTask.php

147 lines 5.9 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;
4
5 use DateInterval;
6 use DatePeriod;
7 use DateTime;
8
9 class AddonTask {
10
11 /*
12 * Initialize
13 */
14 public function __construct() {
15 $this->action_hook();
16 }
17
18 public function action_hook() {
19 add_action( 'erp_hr_new_holiday', [ $this, 'erp_hr_new_holiday_hook_callback' ], 10, 2 );
20 add_action( 'erp_hr_after_update_holiday', [ $this, 'erp_hr_after_update_holiday_hook_callback' ], 10, 2 );
21 add_action( 'erp_hr_leave_holiday_delete', [ $this, 'erp_hr_leave_holiday_delete_hook_callback' ], 10, 1 );
22 add_action( 'erp_hr_leave_request_approved', [ $this, 'erp_hr_leave_request_approved_hook_callback' ], 10, 2 );
23 add_action( 'erp_hr_leave_request_pending', [ $this, 'erp_hr_leave_request_pending_hook_callback' ], 10, 2 );
24 add_action( 'erp_hr_leave_request_reject', [ $this, 'erp_hr_leave_request_reject_hook_callback' ], 10, 2 );
25 }
26
27 public function erp_hr_new_holiday_hook_callback( $id, $args ) {
28 if ( ! empty( $id ) && ! empty( $args ) ) {
29 $this->holiday_create( $id, $args );
30 }
31 }
32
33 public function erp_hr_after_update_holiday_hook_callback( $id, $args ) {
34 if ( ! empty( $id ) && ! empty( $args ) ) {
35 $this->holiday_create( $id, $args );
36 }
37 }
38
39 public function erp_hr_leave_holiday_delete_hook_callback( $id ) {
40 $result = $this->make_query( 'select', '', [ 'sql' => function ( $wpdb ) use ( $id ) {
41 return "SELECT * FROM {$wpdb->prefix}erp_holidays_indv WHERE holiday_id = {$id}";
42 } ] );
43
44 $this->make_query( 'delete', 'erp_holidays_indv', [ 'where' => [ 'holiday_id' => $id ] ] );
45
46 do_action( 'after_calling_erp_hr_holiday_delete_hook_callback', $id, $result );
47 }
48
49 public function erp_hr_leave_request_approved_hook_callback( $id, $request ) {
50 if ( ! empty( $id ) && ! empty( $request ) ) {
51 $period = new DatePeriod(
52 new DateTime( erp_current_datetime()->setTimestamp( $request->start_date )->setTime( 0, 0, 0 )->format( 'Y-m-d H:i:s' ) ),
53 new DateInterval( 'P1D' ),
54 new DateTime( erp_current_datetime()->setTimestamp( $request->end_date )->setTime( 11, 59, 59 )->format( 'Y-m-d H:i:s' ) )
55 );
56
57 foreach ( $period as $value ) {
58 $leaveday_value = $value->format( 'Y-m-d' );
59 $data = [
60 'user_id' => $request->user_id,
61 'request_id' => $id,
62 'title' => $request->reason,
63 'date' => $leaveday_value,
64 ];
65
66 $result = $this->make_query( 'insert', 'erp_user_leaves', [ 'data' => $data ] );
67
68 do_action( 'after_calling_erp_hr_leave_request_approved_hook_callback', $result, $data, $id, $request );
69 }
70 }
71 }
72
73 public function erp_hr_leave_request_pending_hook_callback( $id, $request ) {
74 $results = $this->make_query( 'select', '', [ 'sql' => function ( $wpdb ) use ( $id, $request ) {
75 return "SELECT * FROM {$wpdb->prefix}erp_user_leaves WHERE user_id = {$request->user_id} AND request_id = {$id}";
76 } ] );
77
78 $this->make_query( 'delete', 'erp_user_leaves', [ 'where' => [ 'user_id' => $request->user_id, 'request_id' => $id ] ] );
79
80 do_action( 'after_calling_erp_hr_leave_request_pending_hook_callback', $results, $id, $request );
81 }
82
83 public function erp_hr_leave_request_reject_hook_callback( $id, $request ) {
84 $results = $this->make_query( 'select', '', [ 'sql' => function ( $wpdb ) use ( $id, $request ) {
85 return "SELECT * FROM {$wpdb->prefix}erp_user_leaves WHERE user_id = {$request->user_id} AND request_id = {$id}";
86 } ] );
87
88 $this->make_query( 'delete', 'erp_user_leaves', [ 'where' => [ 'user_id' => $request->user_id, 'request_id' => $id ] ] );
89
90 do_action( 'after_calling_erp_hr_leave_request_pending_hook_callback', $results, $id, $request );
91 }
92
93 public function make_query( $type, $table, $input_data ) {
94 global $wpdb;
95
96 $table = $wpdb->prefix . $table;
97
98 if ( $type == 'insert' ) {
99 return $wpdb->insert( $table, $input_data['data'] );
100 }
101
102 if ( $type == 'update' ) {
103 return $wpdb->update( $table, $input_data['data'], $input_data['where'] );
104 }
105
106 if ( $type == 'delete' ) {
107 return $wpdb->delete( $table, $input_data['where'] );
108 }
109
110 if ( $type == 'select' || $type == 'raw' ) {
111 return $wpdb->get_results( $input_data['sql']( $wpdb ) );
112 }
113 }
114
115 public function holiday_create( $id, $args ) {
116 if ( ! empty( $id ) && ! empty( $args ) ) {
117 $results_prev = $this->make_query( 'select', '', [ 'sql' => function ( $wpdb ) use ( $id ) {
118 return "SELECT * FROM {$wpdb->prefix}erp_holidays_indv WHERE holiday_id = {$id}";
119 } ] );
120
121 $this->make_query( 'delete', 'erp_holidays_indv', [ 'where' => [ 'holiday_id' => $id ] ] );
122 $period = new DatePeriod(
123 new DateTime( $args['start'] ),
124 new DateInterval( 'P1D' ),
125 new DateTime( $args['end'] )
126 );
127
128 foreach ( $period as $key => $value ) {
129 $holiday_value = $value->format( 'Y-m-d' );
130 $data = [
131 'holiday_id' => $id,
132 'title' => $args['title'],
133 'date' => $holiday_value,
134 ];
135
136 $this->make_query( 'insert', 'erp_holidays_indv', [ 'data' => $data ] );
137 }
138
139 $results_now = $this->make_query( 'select', '', [ 'sql' => function ( $wpdb ) use ( $id ) {
140 return "SELECT * FROM {$wpdb->prefix}erp_holidays_indv WHERE holiday_id = {$id}";
141 } ] );
142
143 do_action( 'after_calling_erp_hr_holiday_create_hook_callback', $results_prev, $results_now );
144 }
145 }
146 }
147