'. $shift->shift->shift_name.'
'. $shift->shift->office_start_time .' - '.$shift->shift->office_end_time .'
';
$shiftColorCode[Carbon::parse($shift->date)->day] = $shift->color;
}
$emplolyeeName = view('components.employee', [
'user' => $employee
]);
$final[$employee->id . '#' . $employee->name][] = $emplolyeeName;
$joiningDate = Carbon::createFromFormat('Y-m-d', $employee->employeeDetail->joining_date->toDateString(), company()->timezone)->startOfDay();
if ($joiningDate->greaterThan($this->weekEndDate)) {
foreach ($this->weekPeriod->toArray() as $date) {
$final[$employee->id . '#' . $employee->name][$date->toDateString()] = '-';
}
} elseif ($joiningDate->greaterThan($this->weekStartDate) && $joiningDate->lessThan($this->weekEndDate)) {
foreach ($this->weekPeriod->toArray() as $date) {
if ($date->lessThan($joiningDate)) {
$final[$employee->id . '#' . $employee->name][$date->toDateString()] = '-';
}
}
}
foreach ($employee->leaves as $leave) {
if ($leave->duration != 'half day') {
$final[$employee->id . '#' . $employee->name][$leave->leave_date->toDateString()] = 'Leave';
$shiftColorCode[$leave->leave_date->day] = '';
$leaveType[$leave->leave_date->toDateString()] = $leave->type->type_name;
}
}
foreach ($this->holidays as $holiday) {
if ($final[$employee->id . '#' . $employee->name][$holiday->date->toDateString()] == 'Absent' || $final[$employee->id . '#' . $employee->name][$holiday->date->toDateString()] == 'EMPTY') {
$final[$employee->id . '#' . $employee->name][$holiday->date->toDateString()] = 'Holiday';
$holidayOccasions[$holiday->date->toDateString()] = $holiday->occassion;
$shiftColorCode[$holiday->date->toDateString()] = '';
}
}
}
$this->employeeAttendence = $final;
$this->holidayOccasions = $holidayOccasions;
$this->leaveType = $leaveType;
$this->shiftColorCode = $shiftColorCode;
$this->weekMap = [
0 => __('app.su'),
1 => __('app.mo'),
2 => __('app.tu'),
3 => __('app.we'),
4 => __('app.th'),
5 => __('app.fr'),
6 => __('app.sa'),
];
$view = view('shift-rosters.ajax.week_summary_data', $this->data)->render();
return Reply::dataOnly(['status' => 'success', 'data' => $view]);
}
public function mark(Request $request, $userid, $day, $month, $year)
{
$manageEmployeeShifts = user()->permission('manage_employee_shifts');
abort_403(!(in_array($manageEmployeeShifts, ['all'])));
$this->date = Carbon::createFromFormat('d-m-Y', $day . '-' . $month . '-' . $year)->format('Y-m-d');
$this->employee = User::findOrFail($userid);
$this->shiftSchedule = EmployeeShiftSchedule::with('pendingRequestChange')->where('user_id', $userid)->where('date', $this->date)->first();
$this->employeeShifts = EmployeeShift::all();
return view('shift-rosters.ajax.edit', $this->data);
}
public function store(Request $request)
{
EmployeeShiftSchedule::firstOrCreate([
'user_id' => $request->user_id,
'date' => $request->shift_date,
'employee_shift_id' => $request->employee_shift_id
]);
return Reply::success(__('messages.employeeShiftAdded'));
}
public function update(Request $request, $id)
{
$shift = EmployeeShiftSchedule::findOrFail($id);
$shift->employee_shift_id = $request->employee_shift_id;
$shift->save();
return Reply::success(__('messages.employeeShiftAdded'));
}
public function destroy($id)
{
EmployeeShiftSchedule::destroy($id);
return Reply::success(__('messages.deleteSuccess'));
}
public function exportAllShift($year, $month, $id, $department, $startDate, $viewType)
{
if($viewType == 'week'){
$now = Carbon::parse($startDate, company()->timezone);
$startDate = $now->copy()->startOfWeek(attendance_setting()->week_start_from);
$endDate = $startDate->copy()->addDays(6);
}
else{
$startDate = Carbon::createFromFormat('d-m-Y', '01-' . $month . '-' . $year)->startOfMonth()->startOfDay();
$endDate = $startDate->copy()->endOfMonth()->endOfDay();
}
$date = $endDate->lessThan(now()) ? $endDate : now();
return Excel::download(new ShiftScheduleExport($year, $month, $id, $department, $startDate, $endDate, $viewType), 'Attendance_From_'.$startDate->format('d-m-Y').'_To_'.$date->format('d-m-Y').'.xlsx');
}
public function employeeShiftCalendar(Request $request)
{
if (request('start') && request('end')) {
$model = EmployeeShiftSchedule::with('shift')->where('user_id', $request->employeeId);
$events = $model->get();
$eventData = array();
foreach ($events as $key => $event) {
$startTime = Carbon::parse($event->date->toDateString().' '.$event->shift->office_start_time);
$endTime = Carbon::parse($event->date->toDateString().' '.$event->shift->office_end_time);
if ($startTime->gt($endTime)) {
$endTime->addDay();
}
$eventData[] = [
'id' => $event->id,
'userId' => $event->user_id,
'day' => $event->date->day,
'month' => $event->date->month,
'year' => $event->date->year,
'title' => ucfirst($event->shift->shift_name),
'start' => $startTime,
'end' => $endTime,
'color' => $event->shift->color
];
}
return $eventData;
}
}
public function create()
{
$this->employees = User::allEmployees(null, true, 'all');
$this->departments = Team::all();
$this->employeeShifts = EmployeeShift::all();
$this->pageTitle = __('modules.attendance.bulkShiftAssign');
$this->year = now()->format('Y');
$this->month = now()->format('m');
if (request()->ajax()) {
$html = view('shift-rosters.ajax.create', $this->data)->render();
return Reply::dataOnly(['status' => 'success', 'html' => $html, 'title' => $this->pageTitle]);
}
$this->view = 'shift-rosters.ajax.create';
return view('attendances.create', $this->data);
}
public function bulkShift(StoreBulkShift $request)
{
$employees = $request->user_id;
$employeeData = User::with('employeeDetail')->whereIn('id', $employees)->get();
$date = Carbon::createFromFormat('d-m-Y', '01-' . $request->month . '-' . $request->year)->format('Y-m-d');
if ($request->assign_shift_by == 'month') {
$startDate = Carbon::createFromFormat('d-m-Y', '01-' . $request->month . '-' . $request->year)->startOfMonth();
$endDate = $startDate->copy()->endOfMonth();
$period = CarbonPeriod::create($startDate, $endDate);
$holidays = Holiday::getHolidayByDates($startDate->format('Y-m-d'), $endDate->format('Y-m-d'))->pluck('holiday_date')->toArray();
} else {
$dates = explode(',', $request->multi_date);
$period = [];
$holidays = [];
foreach($dates as $dateData)
{
array_push($period, Carbon::parse($dateData));
$isHoliday = Holiday::checkHolidayByDate(Carbon::parse($dateData)->format('Y-m-d'));
if (!is_null($isHoliday)) {
$holidays[] = $isHoliday->date->format('Y-m-d');
}
}
}
$insertData = [];
$dateRange = [];
foreach ($period as $date) {
$dateRange[] = $date->format('Y-m-d');
}
EmployeeShiftSchedule::whereIn('user_id', $employees)
->whereIn('date', $dateRange)
->delete();
foreach ($employees as $key => $userId) {
$userData = $employeeData->filter(function ($value) use($userId) {
return $value->id == $userId;
})->first();
foreach ($period as $date) {
if ($date->greaterThanOrEqualTo($userData->employeeDetail->joining_date) && !in_array($date->format('Y-m-d'), $holidays)) {
$insertData[] = [
'user_id' => $userId,
'date' => $date->format('Y-m-d'),
'employee_shift_id' => $request->shift,
'added_by' => user()->id,
'last_updated_by' => user()->id
];
}
}
}
EmployeeShiftSchedule::insert($insertData);
if ($request->send_email && count($insertData) > 0) {
foreach ($employees as $key => $userId) {
$userData = $employeeData->filter(function ($value) use($userId) {
return $value->id == $userId;
})->first();
if (smtp_setting()->mail_connection == 'sync') {
Mail::to($userData->email)->send(new BulkShiftEmail($dateRange, $userId));
} else {
Mail::to($userData->email)->queue(new BulkShiftEmail($dateRange, $userId));
}
}
}
$redirectUrl = urldecode($request->redirect_url);
if ($redirectUrl == '') {
$redirectUrl = route('shifts.index');
}
return Reply::redirect($redirectUrl, __('messages.employeeShiftAdded'));
}
}