Simplificacion a contabilidad clasica
This commit is contained in:
@ -28,9 +28,61 @@ class Cuenta extends Model {
|
||||
return $this->entradas;
|
||||
}
|
||||
|
||||
protected $cargos;
|
||||
public function cargos() {
|
||||
if ($this->cargos === null) {
|
||||
$this->cargos = $this->parentOf(Transaccion::class, [Model::CHILD_KEY => 'hasta_id']);
|
||||
}
|
||||
return $this->cargos;
|
||||
}
|
||||
protected $abonos;
|
||||
public function abonos() {
|
||||
if ($this->abonos === null) {
|
||||
$this->abonos = $this->parentOf(Transaccion::class, [Model::CHILD_KEY => 'desde_id']);
|
||||
}
|
||||
return $this->abonos;
|
||||
}
|
||||
protected $transacciones;
|
||||
public function transacciones() {
|
||||
if ($this->transacciones === null) {
|
||||
$this->transacciones = [];
|
||||
if ($this->abonos() !== null) {
|
||||
$this->transacciones = array_merge($this->transacciones, $this->abonos());
|
||||
}
|
||||
if ($this->cargos() !== null) {
|
||||
$this->transacciones = array_merge($this->transacciones, array_map(function($item) {
|
||||
$item->valor = - $item->valor;
|
||||
return $item;
|
||||
}, $this->cargos()));
|
||||
}
|
||||
usort($this->transacciones, function($a, $b) {
|
||||
$d = $a->fecha()->diffInDays($b->fecha(), false);
|
||||
if ($d === 0) {
|
||||
return $a->valor - $b->valor;
|
||||
}
|
||||
return $d;
|
||||
});
|
||||
}
|
||||
return $this->transacciones;
|
||||
}
|
||||
protected $saldo;
|
||||
public function saldo() {
|
||||
if ($this->saldo === null) {
|
||||
$this->saldo = 0;
|
||||
if (count($this->transacciones()) > 0) {
|
||||
$this->saldo = array_reduce($this->transacciones(), function($sum, $item) {
|
||||
return $sum + $item->valor;
|
||||
});
|
||||
}
|
||||
}
|
||||
return $this->saldo;
|
||||
}
|
||||
|
||||
public function toArray(): array {
|
||||
$arr = parent::toArray();
|
||||
$arr['categoria'] = $this->categoria()->toArray();
|
||||
$arr['saldo'] = $this->saldo();
|
||||
$arr['saldoFormateado'] = '$' . number_format($this->saldo(), 0, ',', '.');
|
||||
return $arr;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user