Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 57 |
| CompOffAutomatic | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
306 | |
0.00% |
0 / 57 |
| __construct | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| handle | |
0.00% |
0 / 1 |
272 | |
0.00% |
0 / 55 |
|||
| <?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\Leave\CompOffLeave; | |
| class CompOffAutomatic extends Command | |
| { | |
| /** | |
| * The name and signature of the console command. | |
| * | |
| * @var string | |
| */ | |
| protected $signature = 'compOff:automatic'; | |
| /** | |
| * The console command description. | |
| * | |
| * @var string | |
| */ | |
| protected $description = 'CompOff 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'); | |
| //get value for comp off | |
| $comp_off = Setting::where('name','comp_off')->first(); | |
| //get value for comp off approval | |
| $approval = Setting::where('name','comp_off_approval')->first(); | |
| //get value for include weekend or not | |
| $include_weekend = Setting::where('name','include_weekend')->first(); | |
| $subday_date = Carbon::now(config('settings.time_zone'))->subday()->toDateString(); | |
| //converted dates to UTC | |
| $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 approval is automatic | |
| if($comp_off->value == 'enable' && $approval->value == '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') { | |
| //check weekend for sub day | |
| $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 comp off is empty | |
| if(empty($compOffLeave)){ | |
| //check if user on holiday or weekend | |
| if(!empty($holiday) || (!empty($userWeeklyOffSubday) && $weekend == 'yes')){ | |
| //get attendance detail | |
| $attendance = AttendanceDetail::where('user_id', $user->id) | |
| ->whereBetween('check_in', [$startDateForSubday, $endDateForSubday]) | |
| ->first(); | |
| //get comp off leave | |
| $checkCompOffLeave = Leave::where('name','Comp off Leave - Paid')->where('system_generated',1)->first(); | |
| $todayDate = Carbon::now(config('settings.time_zone')); | |
| //check if user as attendance on holiday or weekend | |
| if(!empty($attendance)){ | |
| //create comp off leave | |
| CompOffLeave::create([ | |
| 'date' => $subDate, | |
| 'is_process' => 'true', | |
| 'is_future' => 'false', | |
| 'user_id' => $user->id, | |
| 'leave_id' => $checkCompOffLeave->id, | |
| 'status' => 'Approve', | |
| 'approved_date' => $todayDate | |
| ]); | |
| $leaveEntitlementFindQuery = array( | |
| 'user_id' => $user->id, | |
| 'leave_id' => $checkCompOffLeave->id, | |
| 'year' => Carbon::now(config('settings.time_zone'))->year | |
| ); | |
| $leaveEntitlement = LeaveEntitlement::where($leaveEntitlementFindQuery)->first(); | |
| // check if entry is in leave entitlements | |
| if(isset($leaveEntitlement)) { | |
| //update leave balance | |
| $leaveEntitlement->leave_balance = 1 + $leaveEntitlement->leave_balance; | |
| $leaveEntitlement->save(); | |
| } else { | |
| //create leave entitlement | |
| $leaveEntitlementData = array( | |
| 'user_id' => $user->id, | |
| 'leave_id' => $checkCompOffLeave->id, | |
| 'leave_balance' => 1, | |
| 'year' => Carbon::now(config('settings.time_zone'))->year, | |
| 'entitled_leaves' => $checkCompOffLeave->n_days | |
| ); | |
| // return $leaveEntitlementData; | |
| LeaveEntitlement::create($leaveEntitlementData); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |