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 / 31
updateCarryForward
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
306
0.00% covered (danger)
0.00%
0 / 31
 __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
272
0.00% covered (danger)
0.00%
0 / 29
<?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 updateCarryForward extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'updateLeave:carryForward';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Leave Balance Carry Forward';
    /**
     * Create a new command instance.
     *
     * @return void
     */
    // Leave Balance Carry Forward
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $users = User::all();
        try {
            $subYear = Carbon::now(config('settings.time_zone'))->subYear()->format('Y');
            
            
            //get current year leave entitlements
            $leaveEntitlements = LeaveEntitlement::where('year',$subYear)->get();
            foreach($leaveEntitlements as $leave)
             {
                $createLeaveEntitlement = new LeaveEntitlement();
                 $leaveObject = Leave::find($leave->leave_id);
                 if($leaveObject->n_days == 0 && $leaveObject->leave_allocation_type != 'no_application'){
                    $createLeaveEntitlement->leave_balance = 0;
                 }else{
                 if( isset($leaveObject) && $leaveObject->to_carry == 0 ){
                     $createLeaveEntitlement->leave_balance = 0;
                 }else if( isset($leaveObject) && $leaveObject->to_carry == 1 ){
                     $createLeaveEntitlement->leave_balance = $leave->leave_balance;
                 }else if( isset($leaveObject) && $leaveObject->to_carry == 2 ){
                     $createLeaveEntitlement->leave_balance = $leave->leave_balance > $leaveObject->carry_days ? $leaveObject->carry_days : $leave->leave_balance;
                 }
                //  else if (isset($leaveObject) && $leaveObject->to_carry == 3) {
                //     if ($leaveObject->leave_allocation_type == 'pro_rata') {
                //         $today = new DateTime();
                //         $oneMonthFromNow = (new DateTime())->add(new DateInterval('P1M'));
                //         $oneDayAfterOneMonth = (new DateTime())->add(new DateInterval('P1M'))->add(new DateInterval('P1D'));
                //         if ($createLeaveEntitlement->created_at <= $oneMonthFromNow && $today >= $oneDayAfterOneMonth) {
                //             $createLeaveEntitlement->leave_balance = 0;
                //         }
                //     } else if ($leaveObject->leave_allocation_type == 'quarterly') {
                //         $today = new DateTime();
                //         $threeMonthFromNow = (new DateTime())->add(new DateInterval('P3M'));
                //         $oneDayAfterThreeMonth = (new DateTime())->add(new DateInterval('P3M'))->add(new DateInterval('P1D'));
                //         if ($createLeaveEntitlement->created_at <= $threeMonthFromNow && $today >= $oneDayAfterThreeMonth) {
                //             $createLeaveEntitlement->leave_balance = 0;
                //         }
                //     } else if ($leaveObject->leave_allocation_type == 'semiyearly') {
                //         $today = new DateTime();
                //         $sixMonthFromNow = (new DateTime())->add(new DateInterval('P6M'));
                //         $oneDayAfterSixMonth = (new DateTime())->add(new DateInterval('P6M'))->add(new DateInterval('P1D'));
                //         if ($createLeaveEntitlement->created_at <= $sixMonthFromNow && $today >= $oneDayAfterSixMonth) {
                //             $createLeaveEntitlement->leave_balance = 0;
                //         }
                //     }
                //  }
                 }
                 $createLeaveEntitlement->user_id = $leave->user_id;
                 $createLeaveEntitlement->leave_id = $leave->leave_id;
                 $createLeaveEntitlement->entitled_leaves = $leaveObject->n_days;
                 $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'));
            //  }
            
                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;
         }
    }
}