diff --git a/api/common/Controller/TiposCategorias.php b/api/common/Controller/TiposCategorias.php index 1d34274..f023410 100644 --- a/api/common/Controller/TiposCategorias.php +++ b/api/common/Controller/TiposCategorias.php @@ -1,6 +1,7 @@ categorias()); } $arr['saldo'] = abs($item->saldo($service)); - $maps = ['activo', 'pasivo', 'ganancia', 'perdida']; - foreach ($maps as $m) { - $p = $m . 's'; - $t = ucfirst($m); - $cuentas = $item->getCuentasOf($t); - if ($cuentas === false or $cuentas === null) { - $arr[$p] = 0; - continue; - } - $arr[$p] = array_reduce($cuentas, function($sum, $item) use($service) { - return $sum + $item->saldo($service, true); - }); - } + $arr['totales'] = $item->getTotales($service); $item = $arr; }); usort($tipos, function($a, $b) { @@ -93,19 +82,7 @@ class TiposCategorias { if ($categorias !== null) { array_walk($categorias, function(&$item) use ($service) { $arr = $item->toArray($service); - $maps = ['activo', 'pasivo', 'ganancia', 'perdida']; - foreach ($maps as $m) { - $p = $m . 's'; - $t = ucfirst($m); - $cuentas = $item->getCuentasOf($t); - if ($cuentas === false or $cuentas === null) { - $arr[$p] = 0; - continue; - } - $arr[$p] = array_reduce($cuentas, function($sum, $item) use($service) { - return $sum + $item->saldo($service, true); - }); - } + $arr['totales'] = $item->getTotales($service); $item = $arr; }); } @@ -120,6 +97,19 @@ class TiposCategorias { public function balance(Request $request, Response $response, Factory $factory, Service $service): Response { $tipos = $factory->find(TipoCategoria::class)->many(); $balance = array_reduce($tipos, function($sum, $item) use ($service) { + $totales = $item->getTotales($service); + if (!is_array($sum)) { + $sum = []; + } + foreach ($totales as $p => $total) { + if (!isset($sum[$p])) { + $sum[$p] = 0; + } + $sum[$p] += $total; + } + return $sum; + }); + /*$balance = array_reduce($tipos, function($sum, $item) use ($service) { $maps = ['activo', 'pasivo', 'ganancia', 'perdida']; foreach ($maps as $m) { $p = $m . 's'; @@ -136,7 +126,7 @@ class TiposCategorias { }); } return $sum; - }); + });*/ return $this->withJson($response, $balance); } } diff --git a/api/src/Categoria.php b/api/src/Categoria.php index 8680751..cb22e21 100644 --- a/api/src/Categoria.php +++ b/api/src/Categoria.php @@ -46,33 +46,43 @@ class Categoria extends Model { ]) ->many(); } - protected $activos; - public function activos() { - if ($this->activos === null) { - $this->activos = $this->getCuentasOf('Activo'); + protected $cuentas_of; + public function getCuentas() { + if ($this->cuentas_of === null) { + $tipos = $this->factory->find(TipoCuenta::class)->many(); + $cos = []; + foreach ($tipos as $tipo) { + $p = strtolower($tipo->descripcion) . 's'; + $cos[$p] = []; + $cuentas = $this->getCuentasOf($tipos->descripcion); + if ($cuentas === null) { + continue; + } + $cos[$p] = $cuentas; + } + $this->cuentas_of = $cos; } - return $this->activos(); + return $this->cuentas_of; } - protected $pasivos; - public function pasivos() { - if ($this->pasivos === null) { - $this->activos = $this->getCuentasOf('Pasivo'); + protected $totales; + public function getTotales(Service $service) { + if ($this->totales === null) { + $tipos = $this->factory->find(TipoCuenta::class)->many(); + $totals = []; + foreach ($tipos as $tipo) { + $p = strtolower($tipo->descripcion) . 's'; + $totals[$p] = 0; + $cuentas = $this->getCuentasOf($tipo->descripcion); + if ($cuentas === null) { + continue; + } + $totals[$p] = array_reduce($cuentas, function($sum, $item) use ($service) { + return $sum + $item->saldo($service, true); + }); + } + $this->totales = $totals; } - 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; + return $this->totales; } protected $saldo; diff --git a/api/src/TipoCategoria.php b/api/src/TipoCategoria.php index bd4b778..b6048d7 100644 --- a/api/src/TipoCategoria.php +++ b/api/src/TipoCategoria.php @@ -33,6 +33,26 @@ class TipoCategoria extends Model { ['categorias.tipo_id', $this->id] ])->many(); } + protected $totales; + public function getTotales(Service $service) { + if ($this->totales === null) { + $tipos = $this->factory->find(TipoCuenta::class)->many(); + $totals = []; + foreach ($tipos as $tipo) { + $p = strtolower($tipo->descripcion) . 's'; + $totals[$p] = 0; + $cuentas = $this->getCuentasOf($tipo->descripcion); + if ($cuentas === null) { + continue; + } + $totals[$p] = array_reduce($cuentas, function($sum, $item) use ($service) { + return $sum + $item->saldo($service, true); + }); + } + $this->totales = $totals; + } + return $this->totales; + } protected $saldo; public function saldo(Service $service = null) { diff --git a/ui/public/assets/scripts/home.js b/ui/public/assets/scripts/home.js index 7f88b39..6b0214d 100644 --- a/ui/public/assets/scripts/home.js +++ b/ui/public/assets/scripts/home.js @@ -59,15 +59,12 @@ class Cuenta { } } class Categoria { - constructor({id, nombre, tipo_id, tipo, activos, pasivos, ganancias, perdidas}) { + constructor({id, nombre, tipo_id, tipo, totales}) { this.id = id this.nombre = nombre this.tipo_id = tipo_id this.tipo = tipo - this.activos = activos - this.pasivos = pasivos - this.ganancias = ganancias - this.perdidas = perdidas + this.totales = totales this.is_open = false this.cuentas = [] } @@ -75,7 +72,7 @@ class Categoria { this.tipos = tipos } draw({format}) { - const button = $('').attr('class', 'ui mini compact icon button').append( + const button = $('').attr('class', 'ui mini compact circular icon button').append( $('').attr('class', down_icon + ' icon') ).click((e) => { const plus = button.find('.' + down_icon.replace(' ', '.') + '.icon') @@ -98,7 +95,7 @@ class Categoria { ) $.each(this.tipos, (i, el) => { tr.append( - $('