76 lines
2.5 KiB
PHP
76 lines
2.5 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\Entrada;
|
|
|
|
class Entradas {
|
|
use Json;
|
|
|
|
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
|
$entradas = $factory->find(Entrada::class)->array();
|
|
if ($entradas !== null) {
|
|
usort($entradas, function($a, $b) {
|
|
$d = $a['fecha'] - $b['fecha'];
|
|
if ($d === 0) {
|
|
return strcmp($a['cuenta']['nombre'], $b['cuenta']['nombre']);
|
|
}
|
|
return $d;
|
|
});
|
|
}
|
|
$output = [
|
|
'entradas' => $entradas
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function show(Request $request, Response $response, Factory $factory, $entrada_id): Response {
|
|
$entrada = $factory->find(Entrada::class)->one($entrada_id);
|
|
$output = [
|
|
'input' => $entrada_id,
|
|
'entrada' => $entrada?->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) {
|
|
$entrada = Entrada::add($factory, $in);
|
|
$results []= ['entrada' => $entrada?->toArray(), 'agregado' => $entrada?->save()];
|
|
}
|
|
} else {
|
|
$entrada = Entrada::add($factory, $input);
|
|
$results []= ['entrada' => $entrada?->toArray(), 'agregado' => $entrada?->save()];
|
|
}
|
|
$output = [
|
|
'input' => $input,
|
|
'entradas' => $results
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function edit(Request $request, Response $response, Factory $factory, $entrada_id): Response {
|
|
$entrada = $factory->find(Entrada::class)->one($entrada_id);
|
|
$output = [
|
|
'input' => $entrada_id,
|
|
'old' => $entrada->toArray()
|
|
];
|
|
$input = json_decode($request->getBody());
|
|
$entrada->edit($input);
|
|
$output['entrada'] = $entrada->toArray();
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function delete(Request $request, Response $response, Factory $factory, $entrada_id): Response {
|
|
$entrada = $factory->find(Entrada::class)->one($entrada_id);
|
|
$output = [
|
|
'input' => $entrada_id,
|
|
'entrada' => $entrada->toArray(),
|
|
'eliminado' => $entrada->delete()
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
}
|