Browse Source

update type data and boot function

pull/1/head
wahyuun 10 months ago
parent
commit
485b9466af
  1. 23
      app/Models/AssignMaterial.php

23
app/Models/AssignMaterial.php

@ -3,10 +3,8 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use App\Models\RequestMaterial; use App\Models\{Activity,RequestMaterial,ReportActivityMaterial};
use App\Models\Activity; use Illuminate\Support\Facades\DB;
use App\Models\ReportActivityMaterial;
class AssignMaterial extends Model class AssignMaterial extends Model
{ {
protected $table = 'assign_material_to_activity'; protected $table = 'assign_material_to_activity';
@ -23,24 +21,25 @@ class AssignMaterial extends Model
protected $casts = [ protected $casts = [
'id' => 'integer', 'id' => 'integer',
'budget' => 'string', 'budget' => 'string',
'qty_planning' => 'float'
]; ];
public static function boot() { public static function boot() {
parent::boot(); parent::boot();
DB::transaction(function () {
static::created(function($data) { static::created(function($data) {
$activity = Activity::find($data->activity_id); $activity = Activity::find((int)$data->activity_id);
$material = RequestMaterial::where("id", $data->material_id)->first(); $material = RequestMaterial::find((int)$data->material_id);
$activity->rencana_biaya += floatval($material->price) * floatval($data->qty_planning); $activity->rencana_biaya += (doubleval($material->price) * doubleval($data->qty_planning));
$activity->save(); $activity->save();
}); });
static::deleted(function($data) { static::deleted(function($data) {
$reportActivities = ReportActivityMaterial::where('assign_material_id', $data->id)->delete(); ReportActivityMaterial::where('assign_material_id', $data->id)->delete();
$activity = Activity::where('id', $data->activity_id)->first(); $activity = Activity::find($data->activity_id);
$activity->rencana_biaya -= floatval($data->budget) * floatval($data->qty_planning); $activity->rencana_biaya -= (doubleval($data->budget) * doubleval($data->qty_planning));
$activity->save(); $activity->save();
}); });
}, 5);
} }
} }

Loading…
Cancel
Save