67 lines
2.3 KiB
PHP
67 lines
2.3 KiB
PHP
<?php
|
|
namespace Contabilidad\Common\Controller;
|
|
|
|
use Carbon\Carbon;
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use ProVM\Common\Factory\Model as Factory;
|
|
use ProVM\Common\Define\Controller\Json;
|
|
use Contabilidad\Common\Service\Consolidar as Service;
|
|
use Contabilidad\Cuenta;
|
|
use Contabilidad\Consolidado;
|
|
|
|
class Consolidados {
|
|
use Json;
|
|
|
|
public function __invoke(Request $request, Response $response, Factory $factory, $cuenta_id): Response {
|
|
$consolidados = $factory->find(Consolidados::class)->where([['cuenta_id' => $cuenta_id]])->many();
|
|
$output = [
|
|
'consolidados' => array_map(function($item) {
|
|
return $item->toArray();
|
|
}, $consolidados)
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function add(Request $request, Response $response, Factory $factory): Response {
|
|
$input = json_decode($request->getBody()->getContents());
|
|
$output = [
|
|
'input' => $input,
|
|
'consolidados' => []
|
|
];
|
|
if (!is_array($input)) {
|
|
$input = [$input];
|
|
}
|
|
foreach ($input as $data) {
|
|
$consolidado = Consolidado::add($factory, $data);
|
|
$status = $consolidado->save();
|
|
$output['consolidados'] []= [
|
|
'consolidado' => $consolidado->toArray(),
|
|
'added' => $status
|
|
];
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function cli(Request $request, Response $response, Service $service): Response {
|
|
try {
|
|
if (!$service->isConsolidado()) {
|
|
$service->consolidar();
|
|
}
|
|
} catch (\Error | \Exception $e) {
|
|
error_log($e);
|
|
throw $e;
|
|
}
|
|
return $this->withJson($response, []);
|
|
}
|
|
public function update(Request $request, Response $response, Factory $factory, Service $service, $mes, $cuenta_id): Response {
|
|
try {
|
|
$cuenta = $factory->find(Cuenta::class)->one($cuenta_id);
|
|
$mes = Carbon::parse($mes);
|
|
$service->consolidarCuenta($cuenta, $mes);
|
|
} catch (\Error | \Exception $e) {
|
|
error_log($e);
|
|
throw $e;
|
|
}
|
|
return $this->withJson($response, []);
|
|
}
|
|
}
|