API
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
<?php
|
||||
namespace Contabilidad;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use ProVM\Common\Alias\Model;
|
||||
use Contabilidad\Common\Service\TiposCambios as Service;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
@ -10,7 +12,7 @@ use ProVM\Common\Alias\Model;
|
||||
*/
|
||||
class Categoria extends Model {
|
||||
public static $_table = 'categorias';
|
||||
protected static $fields = ['nombre'];
|
||||
protected static $fields = ['nombre', 'tipo_id'];
|
||||
|
||||
protected $cuentas;
|
||||
public function cuentas() {
|
||||
@ -27,24 +29,64 @@ class Categoria extends Model {
|
||||
return $this->tipo;
|
||||
}
|
||||
|
||||
public function getCuentasOf($tipo) {
|
||||
return $this->factory->find(Cuenta::class)
|
||||
->select([['cuentas', '*']])
|
||||
->join([
|
||||
['tipos_cuenta', 'tipos_cuenta.id', 'cuentas.tipo_id']
|
||||
])
|
||||
->where([
|
||||
['tipos_cuenta.descripcion', $tipo],
|
||||
['cuentas.categoria_id', $this->id]
|
||||
])
|
||||
->many();
|
||||
}
|
||||
protected $activos;
|
||||
public function activos() {
|
||||
if ($this->activos === null) {
|
||||
$this->activos = $this->getCuentasOf('Activo');
|
||||
}
|
||||
return $this->activos();
|
||||
}
|
||||
protected $pasivos;
|
||||
public function pasivos() {
|
||||
if ($this->pasivos === null) {
|
||||
$this->activos = $this->getCuentasOf('Pasivo');
|
||||
}
|
||||
return $this->pasivos;
|
||||
}
|
||||
protected $ganancias;
|
||||
public function ganancias() {
|
||||
if ($this->ganancias === null) {
|
||||
$this->ganancias = $this->getCuentasOf('Ganancia');
|
||||
}
|
||||
return $this->ganancias;
|
||||
}
|
||||
protected $perdidas;
|
||||
public function perdidas() {
|
||||
if ($this->perdidas === null) {
|
||||
$this->perdidas = $this->getCuentasOf('Perdida');
|
||||
}
|
||||
return $this->perdidas;
|
||||
}
|
||||
|
||||
protected $saldo;
|
||||
public function saldo() {
|
||||
public function saldo(Service $service = null) {
|
||||
if ($this->saldo === null) {
|
||||
$this->saldo = 0;
|
||||
if ($this->cuentas() !== null) {
|
||||
$this->saldo = array_reduce($this->cuentas(), function($sum, $item) {
|
||||
return $sum + $item->saldo();
|
||||
});
|
||||
$sum = 0;
|
||||
$debitos = ['Activo', 'Perdida'];
|
||||
foreach ($this->cuentas() as $cuenta) {
|
||||
if (array_search($cuenta->tipo()->descripcion, $debitos) !== false) {
|
||||
$sum -= $cuenta->saldo($service, true);
|
||||
continue;
|
||||
}
|
||||
$sum += $cuenta->saldo($service, true);
|
||||
}
|
||||
$this->saldo = $sum;
|
||||
}
|
||||
}
|
||||
return $this->saldo;
|
||||
}
|
||||
|
||||
public function toArray(): array {
|
||||
$arr = parent::toArray();
|
||||
$arr['tipo'] = $this->tipo()->toArray();
|
||||
$arr['saldo'] = $this->saldo();
|
||||
$arr['saldoFormateado'] = '$' . number_format($this->saldo(), 0, ',', '.');
|
||||
return $arr;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user