Compare commits
2 Commits
f36399c1f4
...
71975ec6d9
Author | SHA1 | Date | |
---|---|---|---|
71975ec6d9 | |||
84a3f8e2e3 |
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Contabilidad\Common\Controller;
|
namespace Contabilidad\Common\Controller;
|
||||||
|
|
||||||
|
use Contabilidad\TipoCuenta;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use Psr\Http\Message\ResponseInterface as Response;
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
use ProVM\Common\Define\Controller\Json;
|
use ProVM\Common\Define\Controller\Json;
|
||||||
@ -23,19 +24,7 @@ class TiposCategorias {
|
|||||||
}, $item->categorias());
|
}, $item->categorias());
|
||||||
}
|
}
|
||||||
$arr['saldo'] = abs($item->saldo($service));
|
$arr['saldo'] = abs($item->saldo($service));
|
||||||
$maps = ['activo', 'pasivo', 'ganancia', 'perdida'];
|
$arr['totales'] = $item->getTotales($service);
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
$item = $arr;
|
$item = $arr;
|
||||||
});
|
});
|
||||||
usort($tipos, function($a, $b) {
|
usort($tipos, function($a, $b) {
|
||||||
@ -93,19 +82,7 @@ class TiposCategorias {
|
|||||||
if ($categorias !== null) {
|
if ($categorias !== null) {
|
||||||
array_walk($categorias, function(&$item) use ($service) {
|
array_walk($categorias, function(&$item) use ($service) {
|
||||||
$arr = $item->toArray($service);
|
$arr = $item->toArray($service);
|
||||||
$maps = ['activo', 'pasivo', 'ganancia', 'perdida'];
|
$arr['totales'] = $item->getTotales($service);
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
$item = $arr;
|
$item = $arr;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -120,6 +97,19 @@ class TiposCategorias {
|
|||||||
public function balance(Request $request, Response $response, Factory $factory, Service $service): Response {
|
public function balance(Request $request, Response $response, Factory $factory, Service $service): Response {
|
||||||
$tipos = $factory->find(TipoCategoria::class)->many();
|
$tipos = $factory->find(TipoCategoria::class)->many();
|
||||||
$balance = array_reduce($tipos, function($sum, $item) use ($service) {
|
$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'];
|
$maps = ['activo', 'pasivo', 'ganancia', 'perdida'];
|
||||||
foreach ($maps as $m) {
|
foreach ($maps as $m) {
|
||||||
$p = $m . 's';
|
$p = $m . 's';
|
||||||
@ -136,7 +126,7 @@ class TiposCategorias {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
return $sum;
|
return $sum;
|
||||||
});
|
});*/
|
||||||
return $this->withJson($response, $balance);
|
return $this->withJson($response, $balance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,33 +46,43 @@ class Categoria extends Model {
|
|||||||
])
|
])
|
||||||
->many();
|
->many();
|
||||||
}
|
}
|
||||||
protected $activos;
|
protected $cuentas_of;
|
||||||
public function activos() {
|
public function getCuentas() {
|
||||||
if ($this->activos === null) {
|
if ($this->cuentas_of === null) {
|
||||||
$this->activos = $this->getCuentasOf('Activo');
|
$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;
|
protected $totales;
|
||||||
public function pasivos() {
|
public function getTotales(Service $service) {
|
||||||
if ($this->pasivos === null) {
|
if ($this->totales === null) {
|
||||||
$this->activos = $this->getCuentasOf('Pasivo');
|
$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;
|
return $this->totales;
|
||||||
}
|
|
||||||
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;
|
protected $saldo;
|
||||||
|
@ -33,6 +33,26 @@ class TipoCategoria extends Model {
|
|||||||
['categorias.tipo_id', $this->id]
|
['categorias.tipo_id', $this->id]
|
||||||
])->many();
|
])->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;
|
protected $saldo;
|
||||||
public function saldo(Service $service = null) {
|
public function saldo(Service $service = null) {
|
||||||
|
@ -59,15 +59,12 @@ class Cuenta {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
class Categoria {
|
class Categoria {
|
||||||
constructor({id, nombre, tipo_id, tipo, activos, pasivos, ganancias, perdidas}) {
|
constructor({id, nombre, tipo_id, tipo, totales}) {
|
||||||
this.id = id
|
this.id = id
|
||||||
this.nombre = nombre
|
this.nombre = nombre
|
||||||
this.tipo_id = tipo_id
|
this.tipo_id = tipo_id
|
||||||
this.tipo = tipo
|
this.tipo = tipo
|
||||||
this.activos = activos
|
this.totales = totales
|
||||||
this.pasivos = pasivos
|
|
||||||
this.ganancias = ganancias
|
|
||||||
this.perdidas = perdidas
|
|
||||||
this.is_open = false
|
this.is_open = false
|
||||||
this.cuentas = []
|
this.cuentas = []
|
||||||
}
|
}
|
||||||
@ -75,7 +72,7 @@ class Categoria {
|
|||||||
this.tipos = tipos
|
this.tipos = tipos
|
||||||
}
|
}
|
||||||
draw({format}) {
|
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')
|
$('<i></i>').attr('class', down_icon + ' icon')
|
||||||
).click((e) => {
|
).click((e) => {
|
||||||
const plus = button.find('.' + down_icon.replace(' ', '.') + '.icon')
|
const plus = button.find('.' + down_icon.replace(' ', '.') + '.icon')
|
||||||
@ -98,7 +95,7 @@ class Categoria {
|
|||||||
)
|
)
|
||||||
$.each(this.tipos, (i, el) => {
|
$.each(this.tipos, (i, el) => {
|
||||||
tr.append(
|
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)
|
$("[data-id='" + this.tipo_id + "'][data-class='tipo_categoria']").after(tr)
|
||||||
@ -156,21 +153,18 @@ class Categoria {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
class TipoCategoria {
|
class TipoCategoria {
|
||||||
constructor({id, descripcion, activo, activos, pasivos, ganancias, perdidas}) {
|
constructor({id, descripcion, activo, totales}) {
|
||||||
this.id = id
|
this.id = id
|
||||||
this.descripcion = descripcion
|
this.descripcion = descripcion
|
||||||
this.activo = activo
|
this.activo = activo
|
||||||
this.activos = activos
|
this.totales = totales
|
||||||
this.pasivos = pasivos
|
|
||||||
this.ganancias = ganancias
|
|
||||||
this.perdidas = perdidas
|
|
||||||
this.categorias = []
|
this.categorias = []
|
||||||
}
|
}
|
||||||
setTipos(tipos) {
|
setTipos(tipos) {
|
||||||
this.tipos = tipos
|
this.tipos = tipos
|
||||||
}
|
}
|
||||||
draw({format}) {
|
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')
|
$('<i></i>').attr('class', down_icon + ' icon')
|
||||||
).click((e) => {
|
).click((e) => {
|
||||||
const plus = button.find('.' + down_icon.replace(' ', '.') + '.icon')
|
const plus = button.find('.' + down_icon.replace(' ', '.') + '.icon')
|
||||||
@ -188,7 +182,7 @@ class TipoCategoria {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
$.each(this.tipos, (i, el) => {
|
$.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
|
return tr
|
||||||
}
|
}
|
||||||
@ -295,7 +289,7 @@ const cuentas = {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
$.each(data.tipos, (i, el) => {
|
$.each(data.tipos, (i, el) => {
|
||||||
tipo = new TipoCategoria(el)
|
const tipo = new TipoCategoria(el)
|
||||||
tipo.setTipos(this.tipos)
|
tipo.setTipos(this.tipos)
|
||||||
this.tipos_categorias.push(tipo)
|
this.tipos_categorias.push(tipo)
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user