FIX: Empty results threw errors
This commit is contained in:
@ -13,35 +13,35 @@ class Categorias {
|
|||||||
|
|
||||||
public function __invoke(Request $request, Response $response, Factory $factory, Service $service): Response {
|
public function __invoke(Request $request, Response $response, Factory $factory, Service $service): Response {
|
||||||
$categorias = $factory->find(Categoria::class)->many();
|
$categorias = $factory->find(Categoria::class)->many();
|
||||||
array_walk($categorias, function(&$item) use ($service) {
|
if ($categorias !== null) {
|
||||||
$arr = $item->toArray();
|
array_walk($categorias, function(&$item) use ($service) {
|
||||||
if ($item->cuentas()) {
|
$arr = $item->toArray();
|
||||||
$arr['cuentas'] = array_map(function($item) {
|
if ($item->cuentas()) {
|
||||||
return $item->toArray();
|
$arr['cuentas'] = array_map(function($item) {
|
||||||
}, $item->cuentas());
|
return $item->toArray();
|
||||||
}
|
}, $item->cuentas());
|
||||||
$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) {
|
$maps = ['activo', 'pasivo', 'ganancia', 'perdida'];
|
||||||
return $sum + $item->saldo($service, true);
|
foreach ($maps as $m) {
|
||||||
});
|
$p = $m . 's';
|
||||||
}
|
$t = ucfirst($m);
|
||||||
$item = $arr;
|
$cuentas = $item->getCuentasOf($t);
|
||||||
});
|
if ($cuentas === false or $cuentas === null) {
|
||||||
if ($categorias) {
|
$arr[$p] = 0;
|
||||||
usort($categorias, function($a, $b) {
|
continue;
|
||||||
return strcmp($a['nombre'], $b['nombre']);
|
}
|
||||||
});
|
$arr[$p] = array_reduce($cuentas, function($sum, $item) use($service) {
|
||||||
|
return $sum + $item->saldo($service, true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
$item = $arr;
|
||||||
|
});
|
||||||
|
usort($categorias, function($a, $b) {
|
||||||
|
return strcmp($a['nombre'], $b['nombre']);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
$output = [
|
$output = [
|
||||||
'categorias' => $categorias
|
'categorias' => $categorias
|
||||||
];
|
];
|
||||||
return $this->withJson($response, $output);
|
return $this->withJson($response, $output);
|
||||||
}
|
}
|
||||||
|
@ -13,31 +13,34 @@ class TiposCategorias {
|
|||||||
|
|
||||||
public function __invoke(Request $request, Response $response, Factory $factory, Service $service): Response {
|
public function __invoke(Request $request, Response $response, Factory $factory, Service $service): Response {
|
||||||
$tipos = $factory->find(TipoCategoria::class)->many();
|
$tipos = $factory->find(TipoCategoria::class)->many();
|
||||||
array_walk($tipos, function(&$item) use ($service) {
|
if ($tipos !== null) {
|
||||||
$arr = $item->toArray();
|
array_walk($tipos, function(&$item) use ($service) {
|
||||||
$arr['categorias'] = array_map(function($item) {
|
$arr = $item->toArray();
|
||||||
return $item->toArray();
|
$arr['categorias'] = $item->categorias();
|
||||||
}, $item->categorias());
|
if ($arr['categorias'] !== null) {
|
||||||
$arr['saldo'] = abs($item->saldo($service));
|
$arr['categorias'] = array_map(function($item) {
|
||||||
$maps = ['activo', 'pasivo', 'ganancia', 'perdida'];
|
return $item->toArray();
|
||||||
foreach ($maps as $m) {
|
}, $item->categorias());
|
||||||
$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) {
|
$arr['saldo'] = abs($item->saldo($service));
|
||||||
return $sum + $item->saldo($service, true);
|
$maps = ['activo', 'pasivo', 'ganancia', 'perdida'];
|
||||||
});
|
foreach ($maps as $m) {
|
||||||
}
|
$p = $m . 's';
|
||||||
$item = $arr;
|
$t = ucfirst($m);
|
||||||
});
|
$cuentas = $item->getCuentasOf($t);
|
||||||
if ($tipos) {
|
if ($cuentas === false or $cuentas === null) {
|
||||||
usort($tipos, function($a, $b) {
|
$arr[$p] = 0;
|
||||||
return strcmp($a['descripcion'], $b['descripcion']);
|
continue;
|
||||||
});
|
}
|
||||||
|
$arr[$p] = array_reduce($cuentas, function($sum, $item) use($service) {
|
||||||
|
return $sum + $item->saldo($service, true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
$item = $arr;
|
||||||
|
});
|
||||||
|
usort($tipos, function($a, $b) {
|
||||||
|
return strcmp($a['descripcion'], $b['descripcion']);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
$output = [
|
$output = [
|
||||||
'tipos' => $tipos
|
'tipos' => $tipos
|
||||||
|
@ -10,16 +10,16 @@ use Contabilidad\Common\Service\TiposCambios as Service;
|
|||||||
* @property int $activo
|
* @property int $activo
|
||||||
*/
|
*/
|
||||||
class TipoCategoria extends Model {
|
class TipoCategoria extends Model {
|
||||||
public static $_table = 'tipos_categoria';
|
public static $_table = 'tipos_categoria';
|
||||||
protected static $fields = ['descripcion', 'activo'];
|
protected static $fields = ['descripcion', 'activo'];
|
||||||
|
|
||||||
protected $categorias;
|
protected $categorias;
|
||||||
public function categorias() {
|
public function categorias() {
|
||||||
if ($this->categorias === null) {
|
if ($this->categorias === null) {
|
||||||
$this->categorias = $this->parentOf(Categoria::class, [Model::CHILD_KEY => 'tipo_id']);
|
$this->categorias = $this->parentOf(Categoria::class, [Model::CHILD_KEY => 'tipo_id']);
|
||||||
|
}
|
||||||
|
return $this->categorias;
|
||||||
}
|
}
|
||||||
return $this->categorias;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getCuentasOf($tipo) {
|
public function getCuentasOf($tipo) {
|
||||||
return $this->factory->find(Cuenta::class)
|
return $this->factory->find(Cuenta::class)
|
||||||
@ -37,7 +37,7 @@ class TipoCategoria extends Model {
|
|||||||
protected $saldo;
|
protected $saldo;
|
||||||
public function saldo(Service $service = null) {
|
public function saldo(Service $service = null) {
|
||||||
if ($this->saldo === null) {
|
if ($this->saldo === null) {
|
||||||
$this->saldo = array_reduce($this->categorias(), function($sum, $item) use ($service) {
|
$this->saldo = array_reduce($this->categorias() ?? [], function($sum, $item) use ($service) {
|
||||||
return $sum + $item->saldo($service);
|
return $sum + $item->saldo($service);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user