2026-01-28 16:46:31 -06:00

97 lines
2.0 KiB
PHP

<?php namespace App\Models;
/**
* @copyright (c) 2025 Notsoweb Software (https://notsoweb.com) - All Rights Reserved
*/
use Illuminate\Database\Eloquent\Model;
/**
* Descripción
*
* @author Moisés Cortés C. <moises.cortes@notsoweb.com>
*
* @version 1.0.0
*/
class Sale extends Model
{
protected $fillable = [
'user_id',
'client_id',
'cash_register_id',
'invoice_number',
'subtotal',
'tax',
'total',
'discount_percentage',
'discount_amount',
'client_tier_name',
'cash_received',
'change',
'payment_method',
'status',
];
protected $casts = [
'subtotal' => 'decimal:2',
'tax' => 'decimal:2',
'total' => 'decimal:2',
'discount_percentage' => 'decimal:2',
'discount_amount' => 'decimal:2',
'cash_received' => 'decimal:2',
'change' => 'decimal:2',
];
public function user()
{
return $this->belongsTo(User::class);
}
public function details()
{
return $this->hasMany(SaleDetail::class);
}
public function cashRegister()
{
return $this->belongsTo(CashRegister::class);
}
public function client()
{
return $this->belongsTo(Client::class);
}
/**
* Devoluciones asociadas a esta venta
*/
public function returns()
{
return $this->hasMany(Returns::class, 'sale_id');
}
/**
* Total devuelto de esta venta
*/
public function getTotalReturnedAttribute(): float
{
return (float) $this->returns()->sum('total');
}
/**
* Verificar si la venta tiene devoluciones
*/
public function hasReturns(): bool
{
return $this->returns()->exists();
}
/**
* Total neto (total - devoluciones)
*/
public function getNetTotalAttribute(): float
{
return (float) ($this->total - $this->getTotalReturnedAttribute());
}
}