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 / 25
updateLeaveQuarterly
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
156
0.00% covered (danger)
0.00%
0 / 25
 __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
132
0.00% covered (danger)
0.00%
0 / 23
<?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 updateLeaveQuarterly extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'updateLeave:quarterly';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'update leave if Leave Allocation Type is quarterly';
    /**
     * Create a new command instance.
     *
     * @return void
     */
    // update leave if Leave Allocation Type is quarterly
    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(!isset($leaveObject))  //to skip the loop if the leaveobject is null
                 {
                    continue;
                 } 
                 //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 quarterly (3 Monthly)
                 if( isset($leaveObject) && $leaveObject->leave_allocation_type == 'quarterly' ) {
                     $leave->leave_balance = $leave->leave_balance + ($leaveObject->n_days / 4);
                     $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;
         }
    }
}