sigd/app/Models/ActivityFormDetail.php

53 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
class ActivityFormDetail extends SigdModel
{
use HasUuids;
protected $table = 'form_detail';
public $incrementing = false;
protected $keyType = 'string';
protected $guarded = [];
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;
}
}