113 lines
4.0 KiB
PHP
113 lines
4.0 KiB
PHP
<?php
|
|
namespace Contabilidad\Common\Controller;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use ProVM\Common\Define\Controller\Json;
|
|
use ProVM\Common\Factory\Model as Factory;
|
|
use Contabilidad\Common\Service\TiposCambios as Service;
|
|
use Contabilidad\Categoria;
|
|
|
|
class Categorias {
|
|
use Json;
|
|
|
|
public function __invoke(Request $request, Response $response, Factory $factory, Service $service): Response {
|
|
$categorias = $factory->find(Categoria::class)->many();
|
|
if ($categorias !== null) {
|
|
array_walk($categorias, function(&$item) use ($service) {
|
|
$arr = $item->toArray();
|
|
if ($item->cuentas()) {
|
|
$arr['cuentas'] = array_map(function($item) {
|
|
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) {
|
|
return $sum + $item->saldo($service, true);
|
|
});
|
|
}
|
|
$item = $arr;
|
|
});
|
|
usort($categorias, function($a, $b) {
|
|
return strcmp($a['nombre'], $b['nombre']);
|
|
});
|
|
}
|
|
$output = [
|
|
'categorias' => $categorias
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function show(Request $request, Response $response, Factory $factory, $categoria_id): Response {
|
|
$categoria = $factory->find(Categoria::class)->one($categoria_id);
|
|
$output = [
|
|
'input' => $categoria_id,
|
|
'categoria' => $categoria?->toArray()
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function add(Request $request, Response $response, Factory $factory): Response {
|
|
$input = json_decode($request->getBody());
|
|
$results = [];
|
|
if (is_array($input)) {
|
|
foreach ($input as $in) {
|
|
$categoria = Categoria::add($factory, $in);
|
|
$results []= ['categoria' => $categoria?->toArray(), 'agregado' => $categoria?->save()];
|
|
}
|
|
} else {
|
|
$categoria = Categoria::add($factory, $input);
|
|
$results []= ['categoria' => $categoria?->toArray(), 'agregado' => $categoria?->save()];
|
|
}
|
|
$output = [
|
|
'input' => $input,
|
|
'categorias' => $results
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function edit(Request $request, Response $response, Factory $factory, $categoria_id): Response {
|
|
$categoria = $factory->find(Categoria::class)->one($categoria_id);
|
|
$output = [
|
|
'input' => $categoria_id,
|
|
'old' => $categoria->toArray()
|
|
];
|
|
$input = json_decode($request->getBody());
|
|
$categoria->edit($input);
|
|
$output['categoria'] = $categoria->toArray();
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function delete(Request $request, Response $response, Factory $factory, $categoria_id): Response {
|
|
$categoria = $factory->find(Categoria::class)->one($categoria_id);
|
|
$output = [
|
|
'input' => $categoria_id,
|
|
'categoria' => $categoria->toArray(),
|
|
'eliminado' => $categoria->delete()
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function cuentas(Request $request, Response $response, Factory $factory, Service $service, $categoria_id): Response {
|
|
$categoria = $factory->find(Categoria::class)->one($categoria_id);
|
|
$cuentas = null;
|
|
if ($categoria !== null) {
|
|
$cuentas = $categoria->cuentas();
|
|
if ($cuentas !== null) {
|
|
array_walk($cuentas, function(&$item) use ($service) {
|
|
$item = $item->toArray($service);
|
|
});
|
|
}
|
|
}
|
|
$output = [
|
|
'input' => $categoria_id,
|
|
'categoria' => $categoria?->toArray(),
|
|
'cuentas' => $cuentas
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
}
|