96 lines
3.6 KiB
PHP
96 lines
3.6 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\Queuer as Service;
|
|
use Contabilidad\Transaccion;
|
|
|
|
class Transacciones {
|
|
use Json;
|
|
|
|
protected function parseTransacciones(?array $transacciones): ?array {
|
|
if ($transacciones !== null) {
|
|
usort($transacciones, function($a, $b) {
|
|
$d = $a['fecha'] - $b['fecha'];
|
|
if ($d === 0) {
|
|
return strcmp($a['cuenta']['nombre'], $b['cuenta']['nombre']);
|
|
}
|
|
return $d;
|
|
});
|
|
}
|
|
return $transacciones;
|
|
}
|
|
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
|
$transacciones = $factory->find(Transaccion::class)->array();
|
|
$output = [
|
|
'transacciones' => $this->parseTransacciones($transacciones)
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function show(Request $request, Response $response, Factory $factory, $transaccion_id): Response {
|
|
$transaccion = $factory->find(Transaccion::class)->one($transaccion_id);
|
|
$output = [
|
|
'input' => $transaccion_id,
|
|
'transaccion' => $transaccion?->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) {
|
|
$transaccion = Transaccion::add($factory, $in);
|
|
$results []= ['transaccion' => $transaccion?->toArray(), 'agregado' => $transaccion?->save()];
|
|
}
|
|
} else {
|
|
$transaccion = Transaccion::add($factory, $input);
|
|
$results []= ['transaccion' => $transaccion?->toArray(), 'agregado' => $transaccion?->save()];
|
|
}
|
|
$output = [
|
|
'input' => $input,
|
|
'transacciones' => $results
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function edit(Request $request, Response $response, Factory $factory, Service $queuer, $transaccion_id): Response {
|
|
$transaccion = $factory->find(Transaccion::class)->one($transaccion_id);
|
|
$output = [
|
|
'input' => $transaccion_id,
|
|
'old' => $transaccion->toArray()
|
|
];
|
|
$old_cuentas = ['credito' => $transaccion->credito_id, 'debito_id' => $transaccion->debito_id];
|
|
$input = json_decode($request->getBody());
|
|
$transaccion->edit($input);
|
|
$new_cuentas = ['credito' => $transaccion->credito_id, 'debito_id' => $transaccion->debito_id];
|
|
$cuentas = [];
|
|
foreach ($new_cuentas as $tipo => $id) {
|
|
if ($old_cuentas[$tipo] != $id) {
|
|
$cuentas []= $old_cuentas[$tipo];
|
|
$cuentas []= $id;
|
|
}
|
|
}
|
|
$this->updateConsolidar($queuer, $transaccion->fecha(), $cuentas);
|
|
|
|
$output['transaccion'] = $transaccion->toArray();
|
|
return $this->withJson($response, $output);
|
|
}
|
|
protected function updateConsolidar(Service $queuer, \DateTimeInterface $mes, $cuentas) {
|
|
foreach ($cuentas as $cuenta_id) {
|
|
$queuer->queue('update_consolidar', ['mes' => $mes->format('Y-m-1'), 'cuenta' => $cuenta_id]);
|
|
}
|
|
}
|
|
public function delete(Request $request, Response $response, Factory $factory, $transaccion_id): Response {
|
|
$transaccion = $factory->find(Transaccion::class)->one($transaccion_id);
|
|
$output = [
|
|
'input' => $transaccion_id,
|
|
'transaccion' => $transaccion->toArray(),
|
|
'eliminado' => $transaccion->delete()
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
}
|