Laravel-如何计算当年的可用休假天数

时间:2020-04-09 07:58:18

标签: laravel

我已经在我的Laravel-5.8应用程序中使用以下方法计算了授权假天数:

public function findLeaveCount(Request $request)
{
    $userCompany = Auth::user()->company_id;
    $userEmployee = Auth::user()->employee_id;
    $userId = Auth::user()->id;
    $employeeemptypeid              = DB::table('hr_employees')->select('employment_type_id')->where('id', $userId)->first()->employment_type_id;
    $leavetypeid                    = DB::table('hr_leave_types')->select('id')->where('company_id', $userCompany)->where('id',$request->id)->first()->id;
    $authorizedleavedays            = 0;
    $authorizedleavedays            = DB::table('hr_leave_type_details')->select('no_of_days')->where('leave_type_id', $leavetypeid)->where('employment_type_id',$employeeemptypeid)->first();

   $availableleavedays = 0;
    return response()->json([
        'authorizedleavedays' => $authorizedleavedays->no_of_days,
        'availableleavedays' => $availableleavedays->available_leave_days,
    ]);        
}

现在在上面的同一控制器函数(findLeaveCount)中,我想计算当年的$ availableleavedays。

我有这个模型课:

class HrLeaveRequest extends Model
{
  protected $table = 'hr_leave_requests';
  protected $fillable = [
              'id',
              'employee_id',
              'leave_type_id',
              'leave_day',
              'commencement_date',
              'resumption_date',
              'leave_status',
          ];

  public function employee()
  {
    return $this->belongsTo('App\Models\Hr\HrEmployee','employee_id');
  } 

  public function leavetype()
  {
    return $this->belongsTo('App\Models\Hr\HrLeaveType','leave_type_id');
  }
}

$ availableleavedays是:

当年休假天的总和,其中:

employee_id = $ userEmployee

leave_type_id = $ leavetypeid

leave_status = 4

然后从$ authorizedleavedays中减去结果

也就是说,

$ availableleavedays =结果高于-$ authorizedleavedays

如何实现此目标并将其添加到以下代码中:

公共函数findLeaveCount(Request $ request)

谢谢

0 个答案:

没有答案