You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.7 KiB

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Activity;
class UserToActivity extends Model
{
1 year ago
protected $table = 'assign_hr_to_activity', $guarded = ['id'];
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
1 year ago
public static function boot()
{
parent::boot();
1 year ago
static::created(function ($data) {
$activity = Activity::find($data->activity_id);
$getWorkingHours = UserToProyek::where('user_id', $data->user_id)->where('proyek_id', $activity->proyek_id)->first();
1 year ago
$salary = ($getWorkingHours->standart_rate * $activity->duration) * ($getWorkingHours->max_used / 100);
1 year ago
if ($getWorkingHours->uom_standart_rate == "Hour")
$salary = ($getWorkingHours->standart_rate * 8) * $activity->duration * ($getWorkingHours->max_used / 100);
1 year ago
$activity->rencana_biaya += $salary;
$activity->save();
});
1 year ago
static::deleted(function ($data) {
$activity = Activity::find($data->activity_id);
1 year ago
$getWorkingHours = UserToProyek::where('user_id', $data->user_id)->where('proyek_id', $activity->proyek_id)->first();
$salary = ($getWorkingHours->standart_rate * $activity->duration) * ($getWorkingHours->max_used / 100);
if ($getWorkingHours->uom_standart_rate == "Hour") {
$salary = ($getWorkingHours->standart_rate * 8) * $activity->duration * ($getWorkingHours->max_used / 100);
}
1 year ago
$activity->rencana_biaya -= $salary;
if ($activity->rencana_biaya < 0) {
$activity->rencana_biaya = 0;
}
$activity->save();
});
}
}