66 lines
1.6 KiB
PHP
66 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
|
|
class Turno extends Model
|
|
{
|
|
use HasUlids;
|
|
|
|
protected $table = 'turnos';
|
|
|
|
protected $fillable = ['fecha', 'numero_caja', 'numero_turno', 'fondo'];
|
|
|
|
protected $casts = [
|
|
'fecha' => 'date',
|
|
];
|
|
|
|
public function calculosFondo(): HasMany
|
|
{
|
|
return $this->hasMany(CalculoFondo::class, 'turno_id');
|
|
}
|
|
|
|
public function rendido(): Attribute
|
|
{
|
|
return Attribute::get(function () {
|
|
$documentos = $this->documentos()->sum('valor');
|
|
$egresos = $this->egresos()->sum('valor');
|
|
$efectivo = $this->efectivo()->first()?->total ?? 0;
|
|
|
|
return $documentos + $efectivo + $egresos;
|
|
});
|
|
}
|
|
|
|
public function documentos(): HasMany
|
|
{
|
|
return $this->hasMany(Documento::class, 'turno_id');
|
|
}
|
|
|
|
public function egresos(): HasMany
|
|
{
|
|
return $this->hasMany(Egreso::class, 'turno_id');
|
|
}
|
|
|
|
public function efectivo(): HasOne
|
|
{
|
|
return $this->hasOne(Efectivo::class, 'turno_id');
|
|
}
|
|
|
|
public function arqueo(): Attribute
|
|
{
|
|
return Attribute::get(function () {
|
|
return $this->rendido - $this->ingresos()->sum('total');
|
|
});
|
|
}
|
|
|
|
public function ingresos(): HasMany
|
|
{
|
|
return $this->hasMany(Ingreso::class, 'turno_id');
|
|
}
|
|
}
|