sigd/app/Models/ActivityFormDetail.php

48 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Support\Facades\Schema;
class ActivityFormDetail extends SigdModel
{
protected $table = 'form_detail';
protected $fillable = [
'id', 'form_id', 'activity_year', 'activity_code', 'activity_value', 'activity_value_str',
'activity_unit_code', 'row_status', 'created_by', 'updated_by',
];
public function activity()
{
return $this->belongsTo(ReferenceActivity::class, 'activity_code', 'code');
}
public function form()
{
return $this->belongsTo(ActivityForm::class, 'form_id', 'id');
}
public function unit()
{
return $this->belongsTo(Unit::class, 'activity_unit_code', 'code');
}
public function scopeGetValue($query, $inventoryYear, $activityYear, $sector, $code, $activityCode)
{
$form = ActivityForm::where('inventory_year', $inventoryYear)
->where('sector', $sector)->where('form_code', $code)->whereNull('agency_id')
->rowActive()->orderBy('inventory_year', 'desc')
->first();
if ($form) {
$result = $query->where('form_id', $form->id)->where('activity_year', $activityYear)
->where('activity_code', $activityCode)->rowActive()
->first();
return $result ? $result->activity_value : 0;
}
return 0;
}
}