59 lines
1.5 KiB
PHP
59 lines
1.5 KiB
PHP
<?php
|
|
namespace Contabilidad;
|
|
|
|
use ProVM\Common\Alias\Model;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property TipoFuente $tipo_id
|
|
* @property Banco $banco_id
|
|
*/
|
|
class Fuente extends Model {
|
|
public static $_table = 'fuentes';
|
|
protected static $fields = ['tipo_id', 'banco_id'];
|
|
|
|
protected $tipo;
|
|
public function tipo() {
|
|
if ($this->tipo === null) {
|
|
$this->tipo = $this->childOf(TipoFuente::class, [Model::SELF_KEY => 'tipo_id']);
|
|
}
|
|
return $this->tipo;
|
|
}
|
|
protected $banco;
|
|
public function banco() {
|
|
if ($this->banco === null) {
|
|
$this->banco = $this->childOf(Banco::class, [Model::SELF_KEY => 'banco_id']);
|
|
}
|
|
return $this->banco;
|
|
}
|
|
protected $saldo;
|
|
public function saldo() {
|
|
if ($this->saldo === null) {
|
|
$this->saldo = 0;
|
|
if ($this->entradas() !== null) {
|
|
$this->saldo = array_reduce($this->entradas(), function($sum, $item) {
|
|
return $sum + $item->valor;
|
|
});
|
|
}
|
|
}
|
|
return $this->saldo;
|
|
}
|
|
|
|
protected $entradas;
|
|
public function entradas() {
|
|
if ($this->entradas === null) {
|
|
$this->entradas = $this->parentOf(Entrada::class, [Model::CHILD_KEY => 'fuente_id']);
|
|
}
|
|
return $this->entradas;
|
|
}
|
|
|
|
public function toArray(): array {
|
|
$arr = parent::toArray();
|
|
$arr['tipo'] = $this->tipo()->toArray();
|
|
$arr['banco'] = $this->banco()->toArray();
|
|
$arr['saldo'] = $this->saldo();
|
|
$arr['saldoFormateado'] = '$' . number_format($this->saldo(), 0, ',', '.');
|
|
return $arr;
|
|
}
|
|
}
|