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 / 41
updateLeaveProrata
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
240
0.00% covered (danger)
0.00%
0 / 41
 __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
210
0.00% covered (danger)
0.00%
0 / 39
<?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;
use App\Helpers\LeaveLogHelper;
class updateLeaveProrata extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'updateLeave:prorata';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'update leave if Leave Allocation Type is Prorata';
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $users = User::all();
        $leaveLogHelper = new LeaveLogHelper;
        $addedLeaveMap = array();
        $leaveIds = array();
        try {
            $leaves = Leave::all();
            foreach($leaves as $leaveType){
                array_push($leaveIds,$leaveType->id);
            }
            $currentYear = Carbon::now(config('settings.time_zone'))->format('Y');
            $leaveEntitlements = LeaveEntitlement::where('year',$currentYear)->get();
            foreach($leaveEntitlements as $leave)
             {
                    $leaveObject = Leave::find($leave->leave_id);
                    $userId = $leave->user_id;
                    $mapIndex = $userId.$leave->leave_id;
                    $addedLeaveMap[$mapIndex] = 0;
                    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 Prorata (Monthly)
                    if( isset($leaveObject) && $leaveObject->leave_allocation_type == 'pro_rata' ) {
                        $leave->leave_balance = $leave->leave_balance + ($leaveObject->n_days / 12);
                        $addedLeaveMap[$mapIndex] += ($leaveObject->n_days/12);
                        $leave->save();
                    }
                    }
             }
            foreach ($users as $user) {
                if($user->can('Administration Module')){
                        $user->notify(new UpdateLeavesNotification('Success'));
               }
               //Leave Log entry
               $totalLeavesAdded = 0;
               foreach($leaveIds as $leaveId){
                $mapIndex = $user->id.$leaveId;
                if(isset($addedLeaveMap[$mapIndex])){
                    $totalLeavesAdded += $addedLeaveMap[$mapIndex];
                }
               }
               $leaveLogHelper->updateLeaveLog($totalLeavesAdded,"ADDED",$user->id,null,"Monthly Leave add-on",null);
            }
            
             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;
         }
    }
}