Custom Backend OSPRO Surveyor Indonesia
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.

45 lines
1.4 KiB

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\{Activity,RequestMaterial,ReportActivityMaterial};
use Illuminate\Support\Facades\DB;
class AssignMaterial extends Model
{
protected $table = 'assign_material_to_activity';
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
protected $fillable = [
'proyek_id', 'activity_id', 'material_id', 'qty_planning',
'budget', 'plan_date', 'status_activity', 'type',
'created_at', 'created_by', 'updated_at', 'updated_by'
];
protected $casts = [
'id' => 'integer',
'budget' => 'string',
'qty_planning' => 'float'
];
public static function boot() {
parent::boot();
DB::transaction(function () {
static::created(function($data) {
$activity = Activity::find((int)$data->activity_id);
$material = RequestMaterial::find((int)$data->material_id);
$activity->rencana_biaya += (doubleval($material->price) * doubleval($data->qty_planning));
$activity->save();
});
static::deleted(function($data) {
ReportActivityMaterial::where('assign_material_id', $data->id)->delete();
$activity = Activity::find($data->activity_id);
$activity->rencana_biaya -= (doubleval($data->budget) * doubleval($data->qty_planning));
$activity->save();
});
}, 5);
}
}