-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeLeaveController.php
More file actions
280 lines (241 loc) · 9.83 KB
/
EmployeeLeaveController.php
File metadata and controls
280 lines (241 loc) · 9.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
namespace App\Http\Controllers\Organization\hrm;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
USE App\Model\Organization\Leave as EMP_LEV;
USE App\Model\Organization\Category as cat;
USE App\Model\Organization\Employee as EMP;
USE App\Model\Organization\CategoryMeta as catMeta;
use Auth;
use Carbon\Carbon;
use Session;
class EmployeeLeaveController extends Controller
{
public function leave_listing(){
echoo "edit"
echo "made new branch";
echo "make change";
echo "new changeas";
$leave_count_by_cat =$leave_rule =$leavesData = $error =null;
if(in_array(1, role_id())){
$error = "You can not view leave.";
}else{
$catMetas = catMeta::whereIn('key',['include_designation','user_include','user_exclude'])->get();
dd($catMetas->groupBy('key')->toArray());
$user = user_info()->toArray();
$designation_id = get_current_user_meta('designation');
dump($user['id']);
$leave_rule = cat::with('leave_meta')->where(['type'=>'leave', 'status'=>1])->get();
dump($leave_rule->toArray());
$emp_id = get_current_user_meta('employee_id');
$leavesData = EMP_LEV::where(['employee_id'=>$emp_id])->get();
$leave_count_by_cat = $leavesData->where('status',1)->groupBy('leave_category_id');
// dump($data);
// // $collect = collect($leavesData->toArray());
// // dump($collect->where('status',1));
// $leave_count_by_cat = $data->groupBy('leave_category_id');
}
return view('organization.profile.leaves',['data'=>$leavesData, 'leave_rule'=>$leave_rule , 'leave_count_by_cat'=>$leave_count_by_cat,'error'=>$error]);
}
Public function store(Request $request, $id=null)
{
$user = user_info()->toArray();
echo $emp_id = get_current_user_meta('employee_id');
if($request->isMethod('post')){
// $request['reason_of_leave'] = $request['accleasec1f1'];
$current = Carbon::now();
$from = Carbon::parse($request->from);
$before = $from->diffInDays($current);
$to = Carbon::parse($request->to);
$request['from'] = $from->toDateString();
$request['to'] = $to->toDateString();
$data = EMP_LEV::where(function($query)use($request){
$query->whereBetween('from', [$request['from'], $request['to'] ])->orWhereBetween('to',[$request['from'], $request['to']]);
})->where('employee_id',$emp_id);
if($data->exists()){
$error['already_taken_leave_between_from_and_to_date'] = 'Already apply leave between from and to date Correct the dates.';
}
if($to->month < $from->month )
{
$error['from_greater_than_to'] = 'from date must be less than to date.';
Session::flash('error',$error);
return redirect()->route('account.leaves');
}
$request['total_days'] = $from->diffInDays($to) + 1;
$rules = catMeta::where('category_id', $request['leave_category_id']);
if($rules->exists())
{
$rule_check = json_decode($rules->get()->keyBy('key'),true);
if(!empty($rule_check['include_designation']['value']))
{
$include_designation = array_map('intval',json_decode($rule_check['include_designation']['value'],true));
if(!in_array($user['employee_rel']['designation'], $include_designation))
{
$error['include_designation'] = "Designation not Includes";
}
}
/*Designation Include Check*/
elseif(!empty($rule_check['exclude_designation']['value'])){
$exclude_designation = array_map('intval',json_decode($rule_check['exclude_designation']['value'],true));
if(in_array($user['employee_rel']['designation'], $exclude_designation))
{
$error['exclude_designation'] = "Exclude Designation";
}
}
//user Include Check
if(!empty($rule_check['user_include']['value']))
{
$include_designation = array_map('intval',json_decode($rule_check['user_include']['value'],true));
if(!in_array($user['id'], $include_designation))
{
$error['user_include'] = "User not Includes";
}
}
/*user exclude Check*/
elseif(!empty($rule_check['user_exclude']['value'])){
$user_exclude = array_map('intval',json_decode($rule_check['user_exclude']['value'],true));
if(in_array($user['id'], $exclude_designation))
{
$error['user_exclude'] = "Exclude User";
}
}
//Role Include Check
// if(!empty($rule_check['role_include']['value']))
// {
// $role_include = array_map('intval',json_decode($rule_check['role_include']['value'],true));
// $roleIdExistingVal = array_intersect($role_include,role_id());
// if(empty($roleIdExistingVal))
// {
// $error['role_include'] = "Role not Includes";
// }
// }
/*Role Include Check*/
// elseif(!empty($rule_check['roles_exclude']['value'])){
// $roles_exclude = array_map('intval',json_decode($rule_check['roles_exclude']['value'],true));
// $roleIdExcludeExistingVal = array_intersect($roles_exclude,role_id());
// if(empty($roleIdExcludeExistingVal))
// {
// $error['roles_exclude'] = "Exclude Role";
// }
// }
if($request['total_days'] > $rule_check['number_of_day']['value'])
{
$error['exceed_number_of_day'] = "You can only take leave ".$rule_check['number_of_day']['value'];
}
if($rule_check['valid_for']['value'] == "monthly")
{
$leaveFrm = EMP_LEV::where(['employee_id'=>$emp_id, 'leave_category_id'=>$request['leave_category_id']])->whereMonth('from',array($from->month))->get()->keyBy('id');
$leaveTo = EMP_LEV::where(['employee_id'=>$emp_id, 'leave_category_id'=>$request['leave_category_id']])->whereMonth('to',array($from->month))->get()->keyBy('id');
$leaveData = $leaveFrm->merge($leaveTo);//->toArray();
if($from->month != $to->month)
{
$leaveToFormReq = EMP_LEV::where(['employee_id'=>$emp_id, 'leave_category_id'=>$request['leave_category_id']])->whereMonth('from',array($to->month))->get()->keyBy('id');
$leaveToReq = EMP_LEV::where(['employee_id'=>$emp_id, 'leave_category_id'=>$request['leave_category_id']])->whereMonth('to',array($to->month))->get()->keyBy('id');
$data = $leaveToFormReq->merge($leaveToReq);
$leaveData = $data->merge($leaveData);
}
foreach($leaveData->toArray() as $key => $val){
$fromMo = Carbon::parse($val['from']);
$toMo = Carbon::parse($val['to']);
if($fromMo->month != $toMo->month){
if($from->month == $fromMo->month ){
$totalMoDay = $from->daysInMonth;
$total_days[$fromMo->month][] = $totalMoDay - $fromMo->day;
}
elseif($from->month == $toMo->month ){
$total_days[$toMo->month][] = $toMo->day;
}
}
else{
$total_days[$toMo->month][] = $val['total_days'];
}
}
// dump($from->month, $to->month);
if(!empty($total_days))
{
if($from->month == $to->month)
{
$takenLeave = collect($total_days[$from->month])->sum();
$sumAll = $request['total_days'] + $takenLeave;
if($sumAll >$rule_check['number_of_day']['value'])
{
$error['exceed_number_of_day'] = "You already taken leave ".$takenLeave."&& Now your applying leave for ".$request['total_days'].' day';
}
}
elseif($from->month != $to->month){
$fromTakenLeave =null;
if(!empty($total_days[$from->month])){
$fromTakenLeave = collect($total_days[$from->month])->sum();
}
if($from->day == $from->daysInMonth)
{
$totalFrm = $fromTakenLeave +1;
if($totalFrm > $rule_check['number_of_day']['value'])
{
$error['exceed_number_of_day'] = "you exceed leave limit in month ".$from->month;
}
}else{
$totalFrm = $from->daysInMonth - $from->day;
$totalSumFrom = $fromTakenLeave + $totalFrm;
if($totalSumFrom > $rule_check['number_of_day']['value'])
{
$error['exceed_number_of_day'] = "you exceed leave limit in month ".$from->month;
}
}
$toTakenLeave = collect($total_days[$to->month])->sum();
$totalTo = $to->day + $toTakenLeave;
if($totalTo > $rule_check['number_of_day']['value'])
{
$error['exceed_number_of_day'] = "you exceed leave limit in month ".$to->month;
}
}
}
// echo "to sum";
//dump( @$total_days);
// $leave_sumdays = $leave->sum('total_days');
// $total_sum = $leave_sumdays + $request['total_days'];
// if($total_sum >= $rule_check['number_of_day']['value'])
// {
// $error['exceed_number_of_day'] = "You already taken leave include current ".$total_sum;
// }
}
elseif($rule_check['valid_for']['value'] == "yearly")
{
$leave_sumdays = EMP_LEV::where(['employee_id'=>$emp_id, 'leave_category_id'=>$request['leave_category_id']])->whereYear('from',array($from->year))->sum('total_days');
$total_sum = $leave_sumdays + $request['total_days'];
if($total_sum >= $rule_check['number_of_day']['value'])
{
$error['exceed_number_of_day'] = "You already taken leave ".$leave_sumdays;
}
}
if($rule_check['apply_before']['value'] > $before)
{
$error['apply_before'] = "Apply leave After ".$rule_check['apply_before']['value'];
}
// dump(@$error);
//dd($rule_check);
}
if(empty($error)) {
$leave = new EMP_LEV();
$request['employee_id'] = $emp_id;
$leave->fill($request->all());
$leave->save();
save_activity('apply_leave');
Session::flash('sucessful', 'Successfully Apply Leave ');
}else{
Session::flash('error', $error);
//dd($error);
}
}
return redirect()->route('account.leaves');
}
}
// else if($request->isMethod('patch')){
// $leave_id = $request['leave_id'];
// unset( $request['leave_id'] , $request['_method'] , $request['_token']);
// EMP_LEV::where('id', $leave_id)->update($request->all());
// }
// elseif($request->isMethod('DELETE')){
// $data = EMP_LEV::find($request['delete_id']);
// $data->delete();
// }