jeudi 30 juillet 2020

Leave System Balance on a HR application Laravel

I'll just head on to the question directly, I'm currently trying to build an HR(Human Resources) Application on Laravel and at the Leave System where a user can take a specific type of Leave(Vacation and such), I would like to create a Balance system where the user selects a date from and date to and that number in between them gets deducted from the main balance that a user has and that would be the number 20 which I added to my user table on a field called leave_balance(the number is set by default as 20 when a user is created).

Now except deducting the number I would also like to add a function somehow that adds a specific number to the leave_balance every day ex 0.05 I am suspecting I can do this with carbon but I have not the slightest idea how since im relatively new to Laravel

So this is the Leave Table(it is not connected by a foreign key with the user's table):

  1. id
  2. user_id
  3. from
  4. to
  5. type
  6. description
  7. status
  8. message

The user_id is only there to store the id of the authenticated user who creates a leave it is not related by a foreign key

This is what I was thinking of adding in the LeaveController:

// checks for table if employee leave is at 0
$employee = DB::table('leaves')->where('id', $id)->where('leave', 0)->get()->all();
$employeeObj = DB::table('leaves')->where('id', $id)->get()->all();

// init for needed condition
$leaveBool = false;
if(sizeof($employee) < 1) {
  $leaveBool = true;
}

// leave is not at 0
if(!$leaveBool) {
  DB::table('leaves')->update([
    'id' => $id,
    'leave'       => $employeeObj['leaves'] - 1,
  ]);
}

I would like to fix this code so that I can add this in the Leave Controller(also if it's possible to explain to me where exactly I should put such a code in the controller) by fix I mean to not just removed -1 but the number between "from" and "to"

I'm not sure if these are helpful but I will post them too just to make it easier to see,

This is the Leave Model:

class Leave extends Model
{
    use Notifiable;

    protected $guarded=[];
    
    public function user(){
        return $this->belongsTo(User::class,'user_id','id');   
     }
}

This is the view of the Leave

<div class="card-body">
                    <form method="POST" action="">
                        @csrf

                        <div class="form-group">
                            <label>From Date</label>
                            <div class="col-md-6">
                                <input class="datepicker" type="text" class="form-control @error('from') is-invalid @enderror" name="from" required="">

                                @error('from')
                                    <span class="invalid-feedback" role="alert">
                                        <strong></strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group">
                            <label>To Date</label>
                            <div class="col-md-6">
                                <input class="datepicker1" type="text" class="form-control @error('to') is-invalid @enderror" name="to" required="">

I used datepicker for the date, this would be a visualization of what I'm trying to achieve enter image description here

Also by any means, if you have a better code or idea on how I can do this do share it your way it may probably be heaps better



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire