Jay-ar Cristobal
Jay-ar Cristobal

Reputation: 39

Accrued Leave Credits

does someone has an idea on how to create a leave credits that will Accrued every month base on the day of Employee hired. like for example when the employee hired on February 24 then on March 24 the leave credits will start to accrued of 1.5 and then on April 24 another 1.5 credits will add.

this is my leave controller by the way

//leave Employee
public function leavesEmployee()
{
    $user = Auth::User()->rec_id;
    $credits = 0;
    $leaves = DB::table('leaves_admin')
                ->where('rec_id',Auth::user()->rec_id)
                ->get();
    return view('Leave.leaveEmployee', compact('leaves','user','credits'));
}

employee or user migration

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('rec_id');
        $table->string('email')->unique();
        $table->string('join_date')->unique();
        $table->string('status')->nullable();
        $table->string('role_name')->nullable();
        $table->string('avatar')->nullable();
        $table->timestamp('email_verified_at')->nullable();
        $table->boolean('needs_password')->default(false);
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

the join_date is the hire date.

Upvotes: 0

Views: 132

Answers (1)

JS TECH
JS TECH

Reputation: 1583

First, find the differences in months between the date of hired and the current date. Then apply your logic using difference months

For example

$hiredDate = Carbon::parse(Auth::user()->join_date);
$currentDate = Carbon::now();

if (!$hiredDate->isSameYear($currentDate)) {
    $hiredDate = $currentDate->copy()->startOfYear();
}

$diffInMonths = $currentDate->diffInMonths($hiredDate);

$credits = $diffInMonths * 1.5;

if($credits > 15 ) {
    $credits = 15;
}

Update

Credits will reset and return to 0 after the end of the current year. Also, when the credits reach 15 it will stop accumulating

Upvotes: 1

Related Questions