97 lines
1.9 KiB
PHP
Executable File
97 lines
1.9 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
#[Fillable(['user_id', 'name', 'year', 'status'])]
|
|
class Month extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'months';
|
|
|
|
/**
|
|
* Los atributos que son asignables en masa.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'user_id',
|
|
'name',
|
|
'year',
|
|
'status',
|
|
];
|
|
|
|
/**
|
|
* Los atributos que deben ser convertidos.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'year' => 'integer',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Relación con usuario
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
/**
|
|
* Relación con ventas diarias
|
|
*/
|
|
public function dailySales(): HasMany
|
|
{
|
|
return $this->hasMany(DailySale::class);
|
|
}
|
|
|
|
/**
|
|
* Relación con gastos
|
|
*/
|
|
public function expenses(): HasMany
|
|
{
|
|
return $this->hasMany(Expense::class);
|
|
}
|
|
|
|
/**
|
|
* Obtener el nombre del mes con formato
|
|
*/
|
|
public function getDisplayNameAttribute(): string
|
|
{
|
|
return $this->name . ' ' . $this->year;
|
|
}
|
|
|
|
/**
|
|
* Calcular ventas totales del mes
|
|
*/
|
|
public function getTotalUserSalesAttribute(): float
|
|
{
|
|
return $this->dailySales()->sum('user_sales');
|
|
}
|
|
|
|
/**
|
|
* Calcular ventas del sistema
|
|
*/
|
|
public function getTotalSystemSalesAttribute(): float
|
|
{
|
|
return $this->dailySales()->sum('system_sales');
|
|
}
|
|
|
|
/**
|
|
* Calcular gastos totales del mes
|
|
*/
|
|
public function getTotalExpensesAttribute(): float
|
|
{
|
|
return $this->expenses()->sum('amount');
|
|
}
|
|
} |