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 / 34
updateCarryForwardFor2021
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
132
0.00% covered (danger)
0.00%
0 / 34
 __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
110
0.00% covered (danger)
0.00%
0 / 32
<?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 App\Models\Auth\User;
use Carbon\Carbon;
class updateCarryForwardFor2021 extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'updateLeave:carryForward2021';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Leave Balance Carry Forward';
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        try {
            $subYear = Carbon::now(config('settings.time_zone'))->subYear()->format('Y');
            
            $users = User::all();
            
            
            foreach($users as $user)
             {
                //get current year leave entitlements
               
                $leaveEntitlements = LeaveEntitlement::where('year',$subYear)->where('user_id',$user->id)->get();
                
                if(count($leaveEntitlements) > 0){
                    
                $sumOfAllLeaveBalance = 0;
                foreach($leaveEntitlements as $leave)
                {
                    
                    $sumOfAllLeaveBalance += $leave->leave_balance;
                }
                
               
                
                $carry_days = 7;
              
                $leave_balance = $sumOfAllLeaveBalance > $carry_days ? $carry_days : $sumOfAllLeaveBalance;
                 
                
                $defineLeaves = Leave::all();
                foreach($defineLeaves as $defineLeave){
                    if($defineLeave->id == 1){
                        $balance = $leave_balance * (2/3);
                    }else{
                        $balance = $leave_balance * (1/3);
                    }
                    
                                       
                 $createLeaveEntitlement = new LeaveEntitlement();
                 $createLeaveEntitlement->user_id = $user->id;
                 $createLeaveEntitlement->leave_id = $defineLeave->id;
                 $createLeaveEntitlement->entitled_leaves = $defineLeave->n_days;
                 $createLeaveEntitlement->leave_balance = round($balance,2);
                 $createLeaveEntitlement->year = Carbon::now(config('settings.time_zone'))->format('Y');
                 $createLeaveEntitlement->save();
                }
                 
                }
             }
             $notificationList = NotificationList::firstOrFail();
             foreach($notificationList->users as $notification_user)
             {
                 $notification_user->notify(new UpdateLeavesNotification('Success'));
             }
             return;
         }
       catch (Throwable $e)
         {
             $notificationList = NotificationList::firstOrFail();
             foreach($notificationList->users as $notification_user)
             {
                 $notification_user->notify(new UpdateLeavesNotification('Failed'));
             }
             Log::info('Error updating leave :  '.$e->getMessage());
             return;
         }
    }
    
}