Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 38 |
| ProcessAttendance | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
72 | |
0.00% |
0 / 38 |
| __construct | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| handle | |
0.00% |
0 / 1 |
56 | |
0.00% |
0 / 36 |
|||
| <?php | |
| namespace App\Console\Commands; | |
| use Illuminate\Console\Command; | |
| use Illuminate\Support\Facades\Auth; | |
| use App\Models\Auth\UserShift; | |
| use App\Helpers\AttendanceHelper; | |
| use App\Models\Auth\User; | |
| use Carbon\Carbon; | |
| use App\Models\Dashboard\AttendanceDetail; | |
| use Gate; | |
| use DB; | |
| use Illuminate\Support\Facades\Log; | |
| class ProcessAttendance extends Command | |
| { | |
| /** | |
| * The name and signature of the console command. | |
| * | |
| * @var string | |
| */ | |
| protected $signature = 'attendance:process'; | |
| /** | |
| * The console command description. | |
| * | |
| * @var string | |
| */ | |
| protected $description = 'Process attendance for each user daily when no checkin/checkout is selected'; | |
| /** | |
| * Create a new command instance. | |
| * | |
| * @return void | |
| */ | |
| public function __construct() | |
| { | |
| parent::__construct(); | |
| } | |
| /** | |
| * Execute the console command. | |
| * | |
| * @return int | |
| */ | |
| public function handle() | |
| { | |
| $users = User::all(); | |
| // $users = DB::table('users')->where('id', '!=', 1)->get(); | |
| foreach ($users as $user) { | |
| $user_id = $user->id; | |
| $val = AttendanceHelper::getAttendanceMarkValue(); | |
| $valTeam = AttendanceHelper::getAttendanceTeam($user_id,0); | |
| $valUser = AttendanceHelper::getAttendanceUser($user_id,0); | |
| $startDate = Carbon::parse(Carbon::now(config('settings.time_zone'))->toDateString() . '00:00:00', config('settings.time_zone'))->setTimezone(config('app.time_zone')); | |
| $endDate = Carbon::parse(Carbon::now(config('settings.time_zone'))->addDay()->toDateString() . '00:00:00', config('settings.time_zone'))->setTimezone(config('app.time_zone')); | |
| // Log::info('User object:', ['user' => $user]); | |
| // //primary team id | |
| // $teamId = $user->primary_team_id; | |
| // $team = Team::find($teamId); | |
| // //current time | |
| // $today_date_time = Carbon::now(config('settings.time_zone')); | |
| // $time_now = $today_date_time->toTimeString(); | |
| // $currentDate = Carbon::now(config('settings.time_zone'))->toDateString(); | |
| // $subdayWithTimezone = Carbon::now(config('settings.time_zone'))->subday()->toDateString(); | |
| // // if user is on scheduled team | |
| // if ($team->has_schedule == 1) { | |
| // //get sub day user shift first | |
| // $subday_date = Carbon::now(config('settings.time_zone'))->subday()->toDateString(); | |
| // $startDateForSubday = Carbon::parse($subday_date . '00:00:00', config('settings.time_zone'))->setTimezone(config('app.time_zone')); | |
| // $endDateForSubday = Carbon::parse(Carbon::parse($subday_date)->addDay()->toDateString() . '00:00:00', config('settings.time_zone'))->setTimezone(config('app.time_zone')); | |
| // //get user shift for sub day | |
| // $userShiftForSubday = UserShift::where('user_id', $user->id) | |
| // ->whereBetween('created_at', [$startDateForSubday, $endDateForSubday]) | |
| // ->first(); | |
| // //if user shift for sub day is not empty | |
| // if (!empty($userShiftForSubday)) { | |
| // $shiftStartTime = $userShiftForSubday->shift_start; | |
| // $shiftEndTime = $userShiftForSubday->shift_end; | |
| // //get shift start time for settings timezone | |
| // $shiftStartTimeForSubday = Carbon::createFromFormat('H:i:s', $shiftStartTime, Config('app.timezone')); | |
| // $shiftStartTimeForSubday->setTimezone(config('settings.time_zone')); | |
| // $shiftStartTimeForSubday = $shiftStartTimeForSubday->toTimeString(); | |
| // //get shift end time for settings timezone | |
| // $shiftEndTimeForSubday = Carbon::createFromFormat('H:i:s', $shiftEndTime, Config('app.timezone')); | |
| // $shiftEndTimeForSubday->setTimezone(config('settings.time_zone')); | |
| // $shiftEndTimeForSubday = $shiftEndTimeForSubday->toTimeString(); | |
| // //check user is on two day shift | |
| // if ($shiftStartTimeForSubday > $shiftEndTimeForSubday) { | |
| // //offset start time for two day shift | |
| // $offsetStartTime = Carbon::parse($shiftStartTimeForSubday); | |
| // $offsetStartTime = $offsetStartTime->subHours(4)->toTimeString(); | |
| // //offset end time for two day shift | |
| // $offsetEndTime = Carbon::parse($shiftEndTimeForSubday); | |
| // $offsetEndTime = $offsetEndTime->addHours(4)->toTimeString(); | |
| // $time = Carbon::now(config('settings.time_zone')); | |
| // $day = $time->day + 1; | |
| // //set time to shift start time | |
| // $setShiftStartTime = Carbon::createFromFormat('Y-m-d H:i:s', $time->year . '-' . $time->month . '-' . $time->day . ' ' . $offsetStartTime, config('settings.time_zone')); | |
| // //set time to next day 12am | |
| // $set12am = Carbon::createFromFormat('Y-m-d H:i:s', $time->year . '-' . $time->month . '-' . $day . ' 00:00:00', config('settings.time_zone')); | |
| // //checking current time for sub day shift | |
| // if (!$time->between($setShiftStartTime, $set12am, true) && $time_now < $offsetEndTime) { | |
| // } else { | |
| // //if user is single day shift check user shift for today | |
| // $userShift = UserShift::where('user_id', $user->id) | |
| // ->whereBetween('created_at', [$startDate, $endDate]) | |
| // ->first(); | |
| // if (!empty($userShift)) { | |
| // $shiftStartTime = $userShift->shift_start; | |
| // $shiftEndTime = $userShift->shift_end; | |
| // //set the time for settings timezone | |
| // $shiftStartTimeForToday = Carbon::createFromFormat('H:i:s', $shiftStartTime, Config('app.timezone')); | |
| // $shiftStartTimeForToday->setTimezone(config('settings.time_zone')); | |
| // //set time | |
| // $shiftStartTimeForToday = $shiftStartTimeForToday->toTimeString(); | |
| // //get shift time with date | |
| // $setShiftStartTime = Carbon::createFromFormat('H:i:s', $shiftStartTime, Config('app.timezone')) | |
| // ->setTimezone(config('settings.time_zone')) | |
| // ->format('Y-m-d H:i:s'); | |
| // //set offset time with date | |
| // $offsetStartTime = Carbon::createFromFormat('H:i:s', $shiftStartTime, Config('app.timezone')) | |
| // ->setTimezone(config('settings.time_zone')) | |
| // ->subHours(4) | |
| // ->format('Y-m-d H:i:s'); | |
| // //time for check | |
| // $time = Carbon::now(config('settings.time_zone')); | |
| // //check offset start time and shift start time | |
| // if (($time->between($offsetStartTime, $setShiftStartTime)) || | |
| // ($time_now == $shiftStartTimeForToday) || ($time_now > $shiftStartTimeForToday) | |
| // ) { //if time now is near to shift start time | |
| // } else { | |
| // // Session::flash('warning', 'Shift not assigned for you in schedule. Please request attendance.'); | |
| // // redirect()->route('request-attendance.index')->send(); | |
| // return response()->json( | |
| // [ | |
| // 'status' => 'warning', | |
| // 'message' => 'Shift not assigned for you in schedule. Please request attendance.', | |
| // 'url' => '/request-attendance' | |
| // ], | |
| // 200 | |
| // ); | |
| // } | |
| // } else { | |
| // // Session::flash('warning', 'Shift not assigned for you in schedule. Please request attendance.'); | |
| // // redirect()->route('request-attendance.index')->send(); | |
| // return response()->json( | |
| // [ | |
| // 'status' => 'warning', | |
| // 'message' => 'Shift not assigned for you in schedule. Please request attendance.', | |
| // 'url' => '/request-attendance' | |
| // ], | |
| // 200 | |
| // ); | |
| // } | |
| // } | |
| // } else { | |
| // //if user is single day shift check user shift for today | |
| // $userShift = UserShift::where('user_id', $user->id) | |
| // ->whereBetween('created_at', [$startDate, $endDate]) | |
| // ->first(); | |
| // if (!empty($userShift)) { | |
| // $shiftStartTime = $userShift->shift_start; | |
| // $shiftEndTime = $userShift->shift_end; | |
| // $shiftStartTimeForToday = Carbon::createFromFormat('H:i:s', $shiftStartTime, Config('app.timezone')); | |
| // $shiftStartTimeForToday->setTimezone(config('settings.time_zone')); | |
| // //set time | |
| // $shiftStartTimeForToday = $shiftStartTimeForToday->toTimeString(); | |
| // //get shift time with date | |
| // $setShiftStartTime = Carbon::createFromFormat('H:i:s', $shiftStartTime, Config('app.timezone')) | |
| // ->setTimezone(config('settings.time_zone')) | |
| // ->format('Y-m-d H:i:s'); | |
| // //set offset time with date | |
| // $offsetStartTime = Carbon::createFromFormat('H:i:s', $shiftStartTime, Config('app.timezone')) | |
| // ->setTimezone(config('settings.time_zone')) | |
| // ->subHours(4) | |
| // ->format('Y-m-d H:i:s'); | |
| // //time for check | |
| // $time = Carbon::now(config('settings.time_zone')); | |
| // //check offset start time and shift start time | |
| // if (($time->between($offsetStartTime, $setShiftStartTime)) || | |
| // ($time_now == $shiftStartTimeForToday) || ($time_now > $shiftStartTimeForToday) | |
| // ) { //if time now is near to shift start time | |
| // } else { | |
| // // Session::flash('warning', 'Shift not assigned for you in schedule. Please request attendance.'); | |
| // // redirect()->route('request-attendance.index')->send(); | |
| // return response()->json( | |
| // [ | |
| // 'status' => 'warning', | |
| // 'message' => 'Shift not assigned for you in schedule. Please request attendance.', | |
| // 'url' => '/request-attendance' | |
| // ], | |
| // 200 | |
| // ); | |
| // } | |
| // } else { | |
| // // Session::flash('warning', 'Shift not assigned for you in schedule. Please request attendance.'); | |
| // // redirect()->route('request-attendance.index')->send(); | |
| // return response()->json( | |
| // [ | |
| // 'status' => 'warning', | |
| // 'message' => 'Shift not assigned for you in schedule. Please request attendance.', | |
| // 'url' => '/request-attendance' | |
| // ], | |
| // 200 | |
| // ); | |
| // } | |
| // } | |
| // } else { | |
| // //if empty check user shift for today | |
| // $userShift = UserShift::where('user_id', $user->id) | |
| // ->whereBetween('created_at', [$startDate, $endDate]) | |
| // ->first(); | |
| // if (!empty($userShift)) { | |
| // $shiftStartTime = $userShift->shift_start; | |
| // $shiftEndTime = $userShift->shift_end; | |
| // $shiftStartTimeForToday = Carbon::createFromFormat('H:i:s', $shiftStartTime, Config('app.timezone')); | |
| // $shiftStartTimeForToday->setTimezone(config('settings.time_zone')); | |
| // //set time | |
| // $shiftStartTimeForToday = $shiftStartTimeForToday->toTimeString(); | |
| // //get shift time with date | |
| // $setShiftStartTime = Carbon::createFromFormat('H:i:s', $shiftStartTime, Config('app.timezone')) | |
| // ->setTimezone(config('settings.time_zone')) | |
| // ->format('Y-m-d H:i:s'); | |
| // //set offset time with date | |
| // $offsetStartTime = Carbon::createFromFormat('H:i:s', $shiftStartTime, Config('app.timezone')) | |
| // ->setTimezone(config('settings.time_zone')) | |
| // ->subHours(4) | |
| // ->format('Y-m-d H:i:s'); | |
| // //time for check | |
| // $time = Carbon::now(config('settings.time_zone')); | |
| // //check offset start time and shift start time | |
| // if (($time->between($offsetStartTime, $setShiftStartTime)) || | |
| // ($time_now == $shiftStartTimeForToday) || ($time_now > $shiftStartTimeForToday) | |
| // ) { //if time now is near to shift start time | |
| // } else { | |
| // // Session::flash('warning', 'Shift not assigned for you in schedule. Please request attendance.'); | |
| // // redirect()->route('request-attendance.index')->send(); | |
| // return response()->json( | |
| // [ | |
| // 'status' => 'warning', | |
| // 'message' => 'Shift not assigned for you in schedule. Please request attendance.', | |
| // 'url' => '/request-attendance' | |
| // ], | |
| // 200 | |
| // ); | |
| // } | |
| // } else { | |
| // // Session::flash('warning', 'Shift not assigned for you in schedule. Please request attendance.'); | |
| // // redirect()->route('request-attendance.index')->send(); | |
| // return response()->json( | |
| // [ | |
| // 'status' => 'warning', | |
| // 'message' => 'Shift not assigned for you in schedule. Please request attendance.', | |
| // 'url' => '/request-attendance' | |
| // ], | |
| // 200 | |
| // ); | |
| // } | |
| // } | |
| // } else { | |
| // if ($shiftDetails->count() == 1) //if there is only one applicable shift for a user | |
| // { | |
| // foreach ($shiftDetails as $shift) { | |
| // $shiftStartTime = $shift->start_time; | |
| // $shiftEndTime = $shift->end_time; | |
| // } | |
| // } | |
| // } | |
| $userShift = UserShift::where('user_id', $user_id) | |
| ->whereBetween('created_at', [$startDate, $endDate]) | |
| ->first(); | |
| if (!empty($userShift)) { | |
| $shiftStartTime = $userShift->shift_start; | |
| $shiftEndTime = $userShift->shift_end; | |
| } else { | |
| $shiftStartTime = null; | |
| $shiftEndTime = null; | |
| } | |
| $attendanceValue = null; | |
| if ($valUser) { | |
| $attendanceValue = $valUser; | |
| } elseif ($valTeam) { | |
| $attendanceValue = $valTeam; | |
| } elseif ($val) { | |
| $attendanceValue = $val; | |
| } else { | |
| $attendanceValue = null; | |
| } | |
| if ($attendanceValue == 1) { | |
| $attendance = new AttendanceDetail; | |
| $attendance->mode = 'No Checkin'; | |
| $attendance->attendance_setting = $attendanceValue; | |
| $attendance->user_id = $user_id; | |
| $attendance->check_in = null; | |
| $attendance->check_out = null; | |
| $attendance->shift_start = $shiftStartTime; | |
| $attendance->shift_end = $shiftEndTime; | |
| $attendance->save(); | |
| } | |
| } | |
| $this->info('Attendance processed successfully.'); | |
| } | |
| } | |