2021-07-27 22:29:56 -04:00
|
|
|
<?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;
|
2021-07-30 16:38:09 -04:00
|
|
|
use Contabilidad\Transaccion;
|
2021-07-27 22:29:56 -04:00
|
|
|
|
2021-07-30 16:38:09 -04:00
|
|
|
class Transacciones {
|
2021-07-27 22:29:56 -04:00
|
|
|
use Json;
|
|
|
|
|
|
|
|
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
2021-07-30 16:38:09 -04:00
|
|
|
$transacciones = $factory->find(Transaccion::class)->array();
|
|
|
|
if ($transacciones !== null) {
|
|
|
|
usort($transacciones, function($a, $b) {
|
2021-07-29 22:05:38 -04:00
|
|
|
$d = $a['fecha'] - $b['fecha'];
|
|
|
|
if ($d === 0) {
|
|
|
|
return strcmp($a['cuenta']['nombre'], $b['cuenta']['nombre']);
|
|
|
|
}
|
|
|
|
return $d;
|
|
|
|
});
|
|
|
|
}
|
2021-07-27 22:29:56 -04:00
|
|
|
$output = [
|
2021-07-30 16:38:09 -04:00
|
|
|
'transacciones' => $transacciones
|
2021-07-27 22:29:56 -04:00
|
|
|
];
|
|
|
|
return $this->withJson($response, $output);
|
|
|
|
}
|
2021-07-30 16:38:09 -04:00
|
|
|
public function show(Request $request, Response $response, Factory $factory, $transaccion_id): Response {
|
|
|
|
$transaccion = $factory->find(Transaccion::class)->one($transaccion_id);
|
2021-07-27 22:29:56 -04:00
|
|
|
$output = [
|
2021-07-30 16:38:09 -04:00
|
|
|
'input' => $transaccion_id,
|
|
|
|
'transaccion' => $transaccion?->toArray()
|
2021-07-27 22:29:56 -04:00
|
|
|
];
|
|
|
|
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) {
|
2021-07-30 16:38:09 -04:00
|
|
|
$transaccion = Transaccion::add($factory, $in);
|
|
|
|
$results []= ['transaccion' => $transaccion?->toArray(), 'agregado' => $transaccion?->save()];
|
2021-07-27 22:29:56 -04:00
|
|
|
}
|
|
|
|
} else {
|
2021-07-30 16:38:09 -04:00
|
|
|
$transaccion = Transaccion::add($factory, $input);
|
|
|
|
$results []= ['transaccion' => $transaccion?->toArray(), 'agregado' => $transaccion?->save()];
|
2021-07-27 22:29:56 -04:00
|
|
|
}
|
|
|
|
$output = [
|
|
|
|
'input' => $input,
|
2021-07-30 16:38:09 -04:00
|
|
|
'transacciones' => $results
|
2021-07-27 22:29:56 -04:00
|
|
|
];
|
|
|
|
return $this->withJson($response, $output);
|
|
|
|
}
|
2021-07-30 16:38:09 -04:00
|
|
|
public function edit(Request $request, Response $response, Factory $factory, $transaccion_id): Response {
|
|
|
|
$transaccion = $factory->find(Transaccion::class)->one($transaccion_id);
|
2021-07-27 22:29:56 -04:00
|
|
|
$output = [
|
2021-07-30 16:38:09 -04:00
|
|
|
'input' => $transaccion_id,
|
|
|
|
'old' => $transaccion->toArray()
|
2021-07-27 22:29:56 -04:00
|
|
|
];
|
|
|
|
$input = json_decode($request->getBody());
|
2021-07-30 16:38:09 -04:00
|
|
|
$transaccion->edit($input);
|
|
|
|
$output['transaccion'] = $transaccion->toArray();
|
2021-07-27 22:29:56 -04:00
|
|
|
return $this->withJson($response, $output);
|
|
|
|
}
|
2021-07-30 16:38:09 -04:00
|
|
|
public function delete(Request $request, Response $response, Factory $factory, $transaccion_id): Response {
|
|
|
|
$transaccion = $factory->find(Transaccion::class)->one($transaccion_id);
|
2021-07-27 22:29:56 -04:00
|
|
|
$output = [
|
2021-07-30 16:38:09 -04:00
|
|
|
'input' => $transaccion_id,
|
|
|
|
'transaccion' => $transaccion->toArray(),
|
|
|
|
'eliminado' => $transaccion->delete()
|
2021-07-27 22:29:56 -04:00
|
|
|
];
|
|
|
|
return $this->withJson($response, $output);
|
|
|
|
}
|
|
|
|
}
|