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 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 29
CompOffLapseUnutilizedLeaves
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
380
0.00% covered (danger)
0.00%
0 / 29
 __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
182
0.00% covered (danger)
0.00%
0 / 16
 updateLeaveBalance
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 11
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Carbon\Carbon;
use App\Models\Leave\LeaveEntitlement;
use App\Models\Admin\Leave;
use App\Models\Leave\CompOffLeave;
use App\Models\Admin\Setting;
use App\Models\Auth\User;
use App\Helpers\LeaveHelper;
class CompOffLapseUnutilizedLeaves extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'compOff:lapseUnutilizedLeaves';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'CompOff Lapse Unutilized Leaves';
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $compOff = Setting::where('name', 'comp_off')->first();
        //get value for lapse unutilized leaves
        $lapseUnutilizedLeaves = Setting::where('name', 'lapse_unutilized_leaves')->first();
        //get value for leaves lapse after
        $leavesLapseAfter = Setting::where('name', 'leaves_lapse_after')->first();
        $nowDate = Carbon::now(config('settings.time_zone'));
        if (!empty($compOff) && !empty($lapseUnutilizedLeaves) && !empty($leavesLapseAfter)) {
            //if lapse unutilized leaves is yes
            if ($compOff->value == 'enable' && $lapseUnutilizedLeaves->value == 'yes') {
                //get all comp off leaves
                $compOffLeave =  CompOffLeave::all();
                
                if (!empty($compOffLeave)) {
                    foreach ($compOffLeave as $coff) {
                        //if approved date is not null
                        //if lapse date is null
                        //if avail date is null
                        if ($coff->is_process == 'true' && $coff->approved_date != NULL && $coff->lapsed_date == NULL && $coff->avail_date == NULL) {
                            $approveddateWithTimezone = Carbon::parse($coff->approved_date, config('settings.time_zone'));
                            $difference = $approveddateWithTimezone->diffInDays($nowDate);
                            //if approved date is more than lapsed days, then update lapsed date
                            if ($difference > $leavesLapseAfter->value) {
                                $coff->update(['lapsed_date' => $nowDate->format('Y-m-d')]);
                                //remove 1 day from leave balance
                                $this->updateLeaveBalance($coff->leave_id ,1,$coff->user_id);
                            }
                        }
                    }
                }
            }
        }
    }
    public static function updateLeaveBalance($leaveId,$leaveDuration,$user_id)
    {
        $query = LeaveEntitlement::query();
            $query->where('user_id','=',$user_id);
            $query->where('year', '=',Carbon::now(config('settings.time_zone'))->year);
            $query->where('leave_id','=',$leaveId);
            $leaveEntitlementArr = $query->get();
            $leave = Leave::where('id', $leaveId)->first();
           
            foreach( $leaveEntitlementArr as $leaveEntitlement)
            {
                if(isset($leaveEntitlement->leave_balance) && ($leave->leave_allocation_type == 'no_application' || $leaveEntitlement->entitled_leaves != '0.00'))
                {
                    $leaveEntitlement->leave_balance = $leaveEntitlement->leave_balance - $leaveDuration;
                    $leaveEntitlement->save();
                }
            }
    }
}