|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use App\Models\RequestMaterial;
|
|
|
|
use App\Models\Activity;
|
|
|
|
|
|
|
|
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','created_at', 'created_by', 'updated_at', 'updated_by'
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
'id' => 'integer',
|
|
|
|
'budget' => 'float',
|
|
|
|
'qty_planning' => 'float',
|
|
|
|
];
|
|
|
|
|
|
|
|
public static function boot() {
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::created(function($data) {
|
|
|
|
$activity = Activity::find($data->activity_id);
|
|
|
|
$material = RequestMaterial::where("id", $data->material_id)->first();
|
|
|
|
$activity->rencana_biaya += floatval($material->price) * floatval($data->qty_planning);
|
|
|
|
$activity->save();
|
|
|
|
});
|
|
|
|
|
|
|
|
static::deleted(function($data) {
|
|
|
|
$activity = Activity::where('id', $data->activity_id)->first();
|
|
|
|
$activity->rencana_biaya -= floatval($data->budget) * floatval($data->qty_planning);
|
|
|
|
$activity->save();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|