osb/app/Models/Cost.php

66 lines
1.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Cost\{Broadband,Generic,Phone};
class Cost extends Model
{
use HasFactory;
protected $dates = [
'billed_at',
];
protected $with = [
'broadbands',
'generics',
'phones',
];
/* RELATIONS */
public function broadbands()
{
return $this->hasMany(Broadband::class)
->where('active',TRUE);
}
public function generics()
{
return $this->hasMany(Generic::class)
->where('active',TRUE);
}
public function phones()
{
return $this->hasMany(Phone::class)
->where('active',TRUE);
}
/* ATTRIBUTES */
public function getTotalBroadbandAttribute(): float
{
return $this->broadbands->sum('base')+$this->broadbands->sum('excess');
}
public function getTotalGenericAttribute(): float
{
return $this->generics->sum('base')+$this->generics->sum('excess');
}
public function getTotalPhoneAttribute(): float
{
return $this->phones->sum('base')+$this->phones->sum('excess');
}
public function getTotalAttribute(): float
{
return $this->getTotalBroadbandAttribute()+$this->getTotalGenericAttribute()+$this->getTotalPhoneAttribute();
}
}