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 / 32
CreateDailyUserWeeklyOff
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
272
0.00% covered (danger)
0.00%
0 / 32
 __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
240
0.00% covered (danger)
0.00%
0 / 30
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Auth\UserWeeklyOff;
use App\Models\Auth\User;
use Carbon\Carbon;
use App\Models\Admin\Team;
class CreateDailyUserWeeklyOff extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'create:UserWeeklyOff';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'create record if user is on weekly off today';
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $users = User::all();
        
        $currentDateWithTimezone = Carbon::now(config('settings.time_zone'));
        
        foreach ($users as $key => $user) {
            if (!empty($user->userdetail) && count($user->userdetail->shifts) > 0) {
                //user on weekend
                $weekend = $user->userdetail->shifts[0]->weekend;
                
                $day = [];
                if ($weekend['monday'] == 1) {
                    $day[] = 'Monday';
                }
                if ($weekend['tuesday'] == 1) {
                    $day[] = 'Tuesday';
                }
                if ($weekend['wednesday'] == 1) {
                    $day[] = 'Wednesday';
                }
                if ($weekend['thursday'] == 1) {
                    $day[] = 'Thursday';
                }
                if ($weekend['friday'] == 1) {
                    $day[] = 'Friday';
                }
                if ($weekend['saturday'] == 1) {
                    $day[] = 'Saturday';
                }
                if ($weekend['sunday'] == 1) {
                    $day[] = 'Sunday';
                }
                
               
                //primary team id
                $teamId = $user->userdetail->primary_team_id;
                if($teamId != 0 || $teamId != NULL){
                $team =  Team::find($teamId);
                
                //if user's team has no schedule and today is weekend
                if ($team->has_schedule != 1 && in_array($currentDateWithTimezone->format('l'), $day)) {
                    
                    $userWeeklyOff = new UserWeeklyOff();
                    $userWeeklyOff->user_id = $user->id;
                    $userWeeklyOff->date = $currentDateWithTimezone->toDateString();
                    $userWeeklyOff->weekly_off = 1;
                    $userWeeklyOff->save();
                }
                }
            }
        }
    }
}