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.

53 lines
1.3 KiB

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Activity;
use App\Models\AssignMaterial;
use Carbon\Carbon;
class ReportActivityMaterial extends Model
{
protected $table = 'report_activity_material';
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
protected $fillable = [
'activity_id', 'user_id', 'qty', 'lat', 'lon','assign_material_id',
'report_date', 'description', 'created_at', 'created_by', 'updated_at', 'updated_by'
];
public function getReportDateAttribute($value)
{
return Carbon::createFromTimestamp(strtotime($value))
->timezone(env('APP_TIMEZONE'))
->toDateTimeString();
}
public static function boot() {
parent::boot();
static::created(function($data) {
$activity = Activity::find($data->activity_id);
$assignedMaterial = AssignMaterial::find($data->assign_material_id);
$activity->biaya_actual += floatval($assignedMaterial->budget) * floatval($data->qty);
$activity->save();
});
static::deleted(function($data) {
$activity = Activity::find($data->activity_id);
$assignedMaterial = AssignMaterial::find($data->assign_material_id);
$activity->biaya_actual -= floatval($assignedMaterial->budget) * floatval($data->qty);
$activity->save();
});
}
}