Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 50
DaliyAttendance
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
182
0.00% covered (danger)
0.00%
0 / 50
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 handle
0.00% covered (danger)
0.00%
0 / 1
156
0.00% covered (danger)
0.00%
0 / 48
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Auth\User;
use App\Models\Admin\Shift;
use App\Models\Admin\Team;
use App\Models\Schedule\Schedule;
use App\Models\Schedule\ScheduleDetail;
use App\Models\Auth\UserWeeklyOff;
use App\Models\Auth\UserShift;
use App\Models\Admin\TeamShiftDetail;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class DaliyAttendance extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
     protected $signature = 'create:UserDailyOnOff';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create Daily Attendance';
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $users = User::all();
        $todayDate = Carbon::now(config('settings.time_zone'));
        
        $day =  strtolower(Carbon::now(config('settings.time_zone'))->format('D'));
        //$todayDate = $todayDate->format('Y-m-d');
        foreach ($users as  $user) {
            if (!empty($user->userdetail)) {
                //Log::info('Daily Attendance : ' .$user->userdetail);
                
                
                //primary team id
                $teamId = $user->userdetail->primary_team_id;
                if($teamId != 0 || $teamId != NULL){
                $team = Team::where('id', $teamId)->first();
                if ($team->has_schedule == 1){
                //$schedules = Schedule::where('team_id', $teamId)->get();
                $schedule = Schedule::where('from_date', '<=', $todayDate->format('Y-m-d'))
                ->where('to_date', '>=', $todayDate->format('Y-m-d'))->where('status', '0')
                ->where('team_id', $teamId)
                ->first();
                  
                $teamSchedule = $schedule;
                if(!isset($teamSchedule)) {
                  $teamSchedule = Schedule::where('is_default', 1)
                  ->where('status', '0')
                  ->where('team_id', $teamId)
                  ->first();
                }
                if(isset($teamSchedule)) {
                    $teamShifts = TeamShiftDetail::where('team_id', $teamSchedule->team_id)->with('shift')->whereHas('shift')->get();
                    $isWorkingDay = false;
                    $workingShift;
                    foreach($teamShifts as $teamShift){
                        $workingOnQuery = array(
                            'user_id' => $user->id, 
                            'schedule_id' => $teamSchedule->id,
                            'is_active'=> 1,
                            'shift_id' => $teamShift->shift_id,
                        );
                        $scheduleDetail = ScheduleDetail::where($workingOnQuery)->first();
                        
                        if( isset($scheduleDetail) && $scheduleDetail[$day] == 1 ) {
                          $isWorkingDay = true;
                          $workingShift = Shift::where('id', $scheduleDetail->shift_id)->first();
                          // $workingShift = teamShift
                        }
                    }
                    if( $isWorkingDay == true ) {
                      $userShift = new UserShift();
                      $userShift->user_id = $user->id;
                      $userShift->date = $todayDate->toDateString();
                      $userShift->shift_id = $workingShift->id;
                      $userShift->shift_start = $workingShift->start_time;
                      $userShift->shift_end = $workingShift->end_time;
                      $userShift->shift_name = $workingShift->shift;
                      $userShift->team_id = $teamSchedule->team_id;
                      $userShift->team_name = $team->team;
                      $userShift->save();  
                              
                    }
                    else {
                      UserWeeklyOff::updateOrCreate([
                          "user_id" => $user->id,
                          'date' => $todayDate->toDateString(),
                          'has_schedule' => 1,
                          'weekly_off' => 1
                      ]);
                    }
                }
                }
            }
            }
        }
    }
}