Merge branch 'develop' into release

This commit is contained in:
2022-01-07 01:13:22 -03:00
4 changed files with 80 additions and 66 deletions

View File

@ -1,6 +1,7 @@
<?php
namespace Contabilidad\Common\Controller;
use Contabilidad\TipoCuenta;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use ProVM\Common\Define\Controller\Json;
@ -23,19 +24,7 @@ class TiposCategorias {
}, $item->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);
}
}

View File

@ -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;

View File

@ -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) {

View File

@ -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 = $('<button></button>').attr('class', 'ui mini compact icon button').append(
const button = $('<button></button>').attr('class', 'ui mini compact circular icon button').append(
$('<i></i>').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(
$('<td></td>').attr('class', 'right aligned').html(format.format(this[el.descripcion.toLowerCase() + 's']))
$('<td></td>').attr('class', 'right aligned').html(format.format(this.totales[el.descripcion.toLowerCase() + 's']))
)
})
$("[data-id='" + this.tipo_id + "'][data-class='tipo_categoria']").after(tr)
@ -156,21 +153,18 @@ class Categoria {
}
}
class TipoCategoria {
constructor({id, descripcion, activo, activos, pasivos, ganancias, perdidas}) {
constructor({id, descripcion, activo, totales}) {
this.id = id
this.descripcion = descripcion
this.activo = activo
this.activos = activos
this.pasivos = pasivos
this.ganancias = ganancias
this.perdidas = perdidas
this.totales = totales
this.categorias = []
}
setTipos(tipos) {
this.tipos = tipos
}
draw({format}) {
const button = $('<button></button>').attr('class', 'ui mini compact icon button').append(
const button = $('<button></button>').attr('class', 'ui mini compact circular icon button').append(
$('<i></i>').attr('class', down_icon + ' icon')
).click((e) => {
const plus = button.find('.' + down_icon.replace(' ', '.') + '.icon')
@ -188,7 +182,7 @@ class TipoCategoria {
)
)
$.each(this.tipos, (i, el) => {
tr.append($('<td></td>').attr('class', 'right aligned').html(format.format(this[el.descripcion.toLowerCase() + 's'])))
tr.append($('<td></td>').attr('class', 'right aligned').html(format.format(this.totales[el.descripcion.toLowerCase() + 's'])))
})
return tr
}
@ -295,7 +289,7 @@ const cuentas = {
return
}
$.each(data.tipos, (i, el) => {
tipo = new TipoCategoria(el)
const tipo = new TipoCategoria(el)
tipo.setTipos(this.tipos)
this.tipos_categorias.push(tipo)
})