From 074bf105a1042473381a30d70f6e296cb04c2794 Mon Sep 17 00:00:00 2001 From: wahyuun Date: Wed, 6 Mar 2024 16:52:00 +0700 Subject: [PATCH] update type data and boot function --- app/Models/UserToActivity.php | 57 ++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/app/Models/UserToActivity.php b/app/Models/UserToActivity.php index 1b795b1..c460df2 100644 --- a/app/Models/UserToActivity.php +++ b/app/Models/UserToActivity.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; use App\Models\Activity; +use Illuminate\Support\Facades\DB; class UserToActivity extends Model { @@ -29,35 +30,37 @@ class UserToActivity extends Model public static function boot() { parent::boot(); - 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(); + DB::transaction(function(){ + static::created(function($data) { + $activity = Activity::find($data->activity_id); + $getWorkingHours = UserToProyek::where([['user_id', $data->user_id],['proyek_id', $activity->proyek_id]])->first(); + $salary = 0; + if($getWorkingHours->uom_standart_rate == "Hour") { + $salary = (doubleval($getWorkingHours->standart_rate) * 8) * (int)$activity->duration * ($getWorkingHours->max_used / 100); + } else { + $salary = (doubleval($getWorkingHours->standart_rate) * (int)$activity->duration) * ($getWorkingHours->max_used / 100); + } + $activity->rencana_biaya += doubleval($salary); + $activity->save(); + }); - $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); - - $activity->rencana_biaya += $salary; - $activity->save(); - }); - - static::deleted(function($data) { - $activity = Activity::find($data->activity_id); - - $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); - } - - $activity->rencana_biaya -= $salary; - if ($activity->rencana_biaya < 0) { - $activity->rencana_biaya = 0; - } - $activity->save(); - }); + static::deleted(function($data) { + $activity = Activity::find($data->activity_id); + $getWorkingHours = UserToProyek::where([['user_id', $data->user_id],['proyek_id', $activity->proyek_id]])->first(); + $salary = 0; + if($getWorkingHours->uom_standart_rate == "Hour"){ + $salary = (doubleval($getWorkingHours->standart_rate) * 8) * (int)$activity->duration * ($getWorkingHours->max_used / 100); + } else { + $salary = (doubleval($getWorkingHours->standart_rate) * (int)$activity->duration) * ($getWorkingHours->max_used / 100); + } + $activity->rencana_biaya -= doubleval($salary); + if ($activity->rencana_biaya < 0) { + $activity->rencana_biaya = 0; + } + $activity->save(); + }); + }, 5); } }