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 / 58
CompOffSemiAutomatic
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
462
0.00% covered (danger)
0.00%
0 / 58
 __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
420
0.00% covered (danger)
0.00%
0 / 56
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Auth\User;
use App\Helpers\HolidayHelper;
use App\Models\Admin\Setting;
use App\Models\Admin\Holiday;
use Carbon\Carbon;
use App\Models\Auth\UserWeeklyOff;
use App\Models\Dashboard\AttendanceDetail;
use App\Models\Leave\LeaveEntitlement;
use App\Models\Admin\Leave;
use App\Models\NotificationModel\Notification;
use App\Notifications\AppNotifications;
use App\Models\Leave\CompOffLeave;
class CompOffSemiAutomatic extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'compOff:semiAutomatic';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'CompOff Semi Automatic Approval';
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $subDate = Carbon::now(config('settings.time_zone'))->subday()->format('Y-m-d');
        $comp_off = Setting::where('name','comp_off')->first();
        $approval = Setting::where('name', 'comp_off_approval')->first();
        $include_weekend = Setting::where('name', 'include_weekend')->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'));
        if (!empty($comp_off) && !empty($approval)) {
            if ($comp_off->value == 'enable' && $approval->value == 'semi_automatic') {
                //get all users
                $users = User::all();
                if (!empty($users)) {
                    foreach ($users as $user) {
                        if (!empty($user->userdetail)) {
                            //get holidays
                            $holidayIds = HolidayHelper::getholidayList($user->id);
                            $holiday = Holiday::where('date', $subDate)->whereIn('id', $holidayIds)->first();
                            //user sub day weekly off
                            if (!empty($include_weekend) && $include_weekend->value == 'yes') {
                                $userWeeklyOffSubday = UserWeeklyOff::where('user_id', $user->id)
                                    ->whereBetween('created_at', [$startDateForSubday, $endDateForSubday])
                                    ->where('weekly_off', 1)->first();
                            $weekend = 'yes';        
                            } else {
                                $userWeeklyOffSubday = [];
                                $weekend = 'no';
                            }
                            $compOffLeave =  CompOffLeave::where('user_id',$user->id)
                            ->whereDate('date',$subDate)
                            ->first();
                            if(empty($compOffLeave)){
                            if (!empty($holiday) || (!empty($userWeeklyOffSubday) && $weekend == 'yes')) {
                                $attendance =  AttendanceDetail::where('user_id', $user->id)
                                    ->whereBetween('check_in', [$startDateForSubday, $endDateForSubday])
                                    ->first();
                                $checkCompOffLeave = Leave::where('name', 'Comp off Leave - Paid')->where('system_generated', 1)->first();
                                //primary reporting manager
                                $primary_reporting_manager_id = $user->userdetail->primary_reporting_manager_id;
                                //secondary reporting manager
                                $secondaryManagers = $user->userdetail->secondaryManager;
                                if (!empty($attendance)) {
                                    CompOffLeave::create([
                                        'date' => $subDate,
                                        'is_process' => 'false',
                                        'is_future' => 'false',
                                        'user_id' => $user->id,
                                        'leave_id' => $checkCompOffLeave->id,
                                        'status' => 'Pending'
                                    ]);
                                    if ($primary_reporting_manager_id != 0) {
                                        //primary reporting manager
                                        $primary_reporting_manager = User::find($primary_reporting_manager_id);
                                        //check if user is exists  
                                        if (!empty($primary_reporting_manager)) {
                                            $title = "Comp Off Leave Balance Approval";
                                            $arr = null;
                                            $arr = [$title, $user->name . ' ' . $user->lastName . "'s Comp Off Leave Balance approval.", null];
                                            
                                            $primary_reporting_manager->notify(new AppNotifications($arr));
                                        }
                                    }
                                    //secondary reporting managers
                                    if (count($secondaryManagers) > 0) {
                                        foreach ($secondaryManagers as $secondaryManager) {
                                            if ($primary_reporting_manager_id != $secondaryManager->id) {
                                                $noCheckinMessage =  $user->name . ' ' . $user->lastName . ' Comp off leave approval.';
                                                $arr = null;
                                                $arr = [$title, $noCheckinMessage, null];
                                                
                                                $secondaryManager->notify(new AppNotifications($arr));
                                            }
                                        }
                                    }
                                }
                            }
                            }
                        }
                    }
                }
            }
        }
    }
}