86 lines
1.6 KiB
PHP
86 lines
1.6 KiB
PHP
<?php namespace App\Models;
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
class CashClose extends Model
|
|
{
|
|
protected $fillable = [
|
|
'balance',
|
|
'income',
|
|
'exit',
|
|
'expected_balance',
|
|
'difference',
|
|
'close_date',
|
|
'notes',
|
|
'status',
|
|
'user_id',
|
|
];
|
|
|
|
protected $casts = [
|
|
'balance' => 'decimal:2',
|
|
'income' => 'decimal:2',
|
|
'exit' => 'decimal:2',
|
|
'expected_balance' => 'decimal:2',
|
|
'difference' => 'decimal:2',
|
|
'close_date' => 'date',
|
|
];
|
|
|
|
/**
|
|
* Relación con el usuario que realizó el corte
|
|
*/
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
/**
|
|
* Relación con las ventas asociadas a este corte
|
|
*/
|
|
public function sales()
|
|
{
|
|
return $this->hasMany(Sale::class);
|
|
}
|
|
|
|
/**
|
|
* Scope para cortes abiertos
|
|
*/
|
|
public function scopeOpen($query)
|
|
{
|
|
return $query->where('status', 'open');
|
|
}
|
|
|
|
/**
|
|
* Scope para cortes cerrados
|
|
*/
|
|
public function scopeClosed($query)
|
|
{
|
|
return $query->where('status', 'closed');
|
|
}
|
|
|
|
/**
|
|
* Scope para cortes de una fecha específica
|
|
*/
|
|
public function scopeByDate($query, $date)
|
|
{
|
|
return $query->whereDate('close_date', $date);
|
|
}
|
|
|
|
/**
|
|
* Calcula automáticamente el balance
|
|
*/
|
|
public function calculateBalance()
|
|
{
|
|
$this->balance = $this->income - $this->exit;
|
|
|
|
if ($this->expected_balance) {
|
|
$this->difference = $this->balance - $this->expected_balance;
|
|
}
|
|
|
|
return $this->balance;
|
|
}
|
|
}
|