Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 23 |
| updateLeaveAdvance | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
132 | |
0.00% |
0 / 23 |
| __construct | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| handle | |
0.00% |
0 / 1 |
110 | |
0.00% |
0 / 21 |
|||
| <?php | |
| namespace App\Console\Commands; | |
| use Illuminate\Console\Command; | |
| use App\Models\Leave\LeaveEntitlement; | |
| //use App\Models\NotificationModel\NotificationList; | |
| use App\Notifications\UpdateLeavesNotification; | |
| use Illuminate\Support\Facades\Log; | |
| use App\Exceptions\Handler; | |
| use App\Models\Admin\Leave; | |
| use Carbon\Carbon; | |
| use App\Models\Auth\User; | |
| class updateLeaveAdvance extends Command | |
| { | |
| /** | |
| * The name and signature of the console command. | |
| * | |
| * @var string | |
| */ | |
| protected $signature = 'updateLeave:advance'; | |
| /** | |
| * The console command description. | |
| * | |
| * @var string | |
| */ | |
| protected $description = 'update leave if Leave Allocation Type is Advance'; | |
| /** | |
| * Create a new command instance. | |
| * | |
| * @return void | |
| */ | |
| public function __construct() | |
| { | |
| parent::__construct(); | |
| } | |
| /** | |
| * Execute the console command. | |
| * | |
| * @return int | |
| */ | |
| public function handle() | |
| { | |
| $users = User::all(); | |
| try { | |
| $currentYear = Carbon::now(config('settings.time_zone'))->format('Y'); | |
| //get current year leave entitlements | |
| $leaveEntitlements = LeaveEntitlement::where('year',$currentYear)->get(); | |
| foreach($leaveEntitlements as $leave) | |
| { | |
| $leaveObject = Leave::find($leave->leave_id); | |
| //if total leaves in a year is 0 | |
| if($leaveObject->n_days == 0){ | |
| $leave->leave_balance = 0; | |
| $leave->save(); | |
| }else{ | |
| // if Leave Allocation Type is Advance | |
| if(isset($leaveObject) && $leaveObject->leave_allocation_type == 'advance') { | |
| $leave->leave_balance = $leave->leave_balance + $leaveObject->n_days; //n_days = Total Leaves in a year | |
| $leave->save(); | |
| } | |
| } | |
| } | |
| foreach ($users as $user) { | |
| if($user->can('Administration Module')){ | |
| $user->notify(new UpdateLeavesNotification('Success')); | |
| } | |
| } | |
| return; | |
| } | |
| catch (Throwable $e) | |
| { | |
| // $notificationList = NotificationList::firstOrFail(); | |
| // foreach($notificationList->users as $notification_user) | |
| // { | |
| // $notification_user->notify(new UpdateLeavesNotification('Failed')); | |
| // } | |
| foreach ($users as $user) { | |
| if($user->can('Administration Module')){ | |
| $user->notify(new UpdateLeavesNotification('Failed')); | |
| } | |
| } | |
| Log::info('Error updating leave : '.$e->getMessage()); | |
| return; | |
| } | |
| } | |
| } |