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 / 48
monthlyCarryForwardNone
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
600
0.00% covered (danger)
0.00%
0 / 48
 __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
552
0.00% covered (danger)
0.00%
0 / 46
<?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 DateTime;
use DateInterval;
class monthlyCarryForwardNone extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'updateLeave:monthlyCarryForwardNone';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Leave Balance monthly Carry Forward None';
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return int
     */
    // Leave Balance monthly Carry Forward None 
    public function handle()
    {
        $leaves = Leave::all();
        $users = User::all();
            try {
                $Year = Carbon::now(config('settings.time_zone'))->format('Y');
                // dd($year);
                foreach($leaves as $leaveOB){
                    foreach($users as $user){
                //get current year leave entitlements
                $leave = LeaveEntitlement::where('year','=',$Year)
                                                    ->where('user_id','=',$user->id)
                                                    ->where('leave_id','=',$leaveOB->id)
                                                    ->latest()->first();
                if($leave!=null){
                    $leaveObject = Leave::find($leave->leave_id);
                    if($leaveObject !== null){
                       if($leaveObject->n_days == 0 && $leaveObject->leave_allocation_type != 'no_application'){
                           $leave->leave_balance = 0;
                       }else{
                           if (isset($leaveObject) && $leaveObject->to_carry == 3) {
                               if ($leaveObject->leave_allocation_type == 'pro_rata') {
                                   $today = new DateTime();
                                   $oneMonthFromNow = ($leave->updated_at)->add(new DateInterval('P1M'));
                                   $oneDayAfterOneMonth = ($leave->updated_at)->add(new DateInterval('P1M'))->add(new DateInterval('P1D'));
                                   
                                   if ($leave->updated_at <= $oneMonthFromNow && $today >= $oneMonthFromNow) {
                                       $leave->leave_balance = 0;
                                   }
                               } else if ($leaveObject->leave_allocation_type == 'quarterly') {
                                   $today = Carbon::now();
                                   $threeMonthFromNow = ($leave->updated_at)->add(new DateInterval('P3M'));
                                   $oneDayAfterThreeMonth = ($leave->updated_at)->add(new DateInterval('P3M'))->add(new DateInterval('P1D'));
                    
                                   if ($leave->updated_at <= $threeMonthFromNow && $today >= $oneMonthFromNow) {
                                       $leave->leave_balance = 0;
                                   }
                               } else if ($leaveObject->leave_allocation_type == 'semiyearly') {
                                   $today = new DateTime();
                                   $sixMonthFromNow = ($leave->updated_at)->add(new DateInterval('P6M'));
                                   $oneDayAfterSixMonth = ($leave->updated_at)->add(new DateInterval('P6M'))->add(new DateInterval('P1D'));
                                   
                                   if ($leave->updated_at <= $sixMonthFromNow && $today >= $oneMonthFromNow) {
                                       $leave->leave_balance = 0;
                                   }
                               }
                            }
                           }
                           $leave->entitled_leaves = $leaveObject->n_days;
                           $leave->year = Carbon::now(config('settings.time_zone'))->format('Y');
                           $leave->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;
             }
    }
}