42 lines
975 B
PHP
42 lines
975 B
PHP
<?php
|
|
namespace Contabilidad;
|
|
|
|
use ProVM\Common\Alias\Model;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property string $nombre
|
|
*/
|
|
class Categoria extends Model {
|
|
public static $_table = 'categorias';
|
|
protected static $fields = ['nombre'];
|
|
|
|
protected $cuentas;
|
|
public function cuentas() {
|
|
if ($this->cuentas === null) {
|
|
$this->cuentas = $this->parentOf(Cuenta::class, [Model::CHILD_KEY => 'categoria_id']);
|
|
}
|
|
return $this->cuentas;
|
|
}
|
|
|
|
protected $saldo;
|
|
public function saldo() {
|
|
if ($this->saldo === null) {
|
|
$this->saldo = 0;
|
|
if ($this->cuentas() !== null) {
|
|
$this->saldo = array_reduce($this->cuentas(), function($sum, $item) {
|
|
return $sum + $item->saldo();
|
|
});
|
|
}
|
|
}
|
|
return $this->saldo;
|
|
}
|
|
|
|
public function toArray(): array {
|
|
$arr = parent::toArray();
|
|
$arr['saldo'] = $this->saldo();
|
|
$arr['saldoFormateado'] = '$' . number_format($this->saldo(), 0, ',', '.');
|
|
return $arr;
|
|
}
|
|
}
|