Files
contabilidad/api/common/Controller/Cuentas.php
2022-08-08 22:36:04 -04:00

126 lines
4.0 KiB
PHP

<?php
namespace Common\Controller;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use PDOException;
use function Safe\{json_decode,error_log};
use ProVM\Alias\Controller\Json;
use Contabilidad\Repository\Cuenta;
class Cuentas
{
use Json;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository): ResponseInterface
{
$cuentas = [];
try {
$cuentas = $repository->fetchAll();
} catch (PDOException $e) {
error_log($e);
}
return $this->withJson($response, compact('cuentas'));
}
public function get(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository, $cuenta_id): ResponseInterface
{
$cuenta = null;
try {
$cuenta = $repository->fetchById($cuenta_id);
} catch (PDOException $e) {
error_log($e);
}
return $this->withJson($response, compact('cuenta'));
}
public function add(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository): ResponseInterface
{
$body = $request->getBody();
$contents = $body->getContents();
$json = json_decode($contents);
if (!is_array($json)) {
$json = [$json];
}
$output = [
'input' => $json,
'cuentas' => []
];
foreach ($json as $data) {
$cuenta = $repository->create((array) $data);
$status = true;
$exists = true;
if ($cuenta->isNew()) {
$exists = false;
try {
$repository->save($cuenta);
} catch (PDOException $e) {
error_log($e);
$status = false;
}
}
$output['cuentas'] []= [
'cuenta' => $cuenta,
'exists' => $exists,
'added' => $status
];
}
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository): ResponseInterface
{
$body = $request->getBody();
$contents = $body->getContents();
$json = json_decode($contents);
if (!is_array($json)) {
$json = [$json];
}
$output = [
'input' => $json,
'cuentas' => []
];
foreach ($json as $data) {
$cuenta = $repository->fetchById($data->id);
$old = clone $cuenta;
try {
$cuenta->edit((array) $data);
$status = $cuenta->isDirty();
if ($status) {
$repository->save($cuenta);
}
} catch (PDOException $e) {
error_log($e);
$status = false;
}
$output['cuentas'] []= [
'antes' => $old,
'cuenta' => $cuenta,
'edited' => $status
];
}
return $this->withJson($response, $output);
}
public function editOne(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository, $cuenta_id): ResponseInterface
{
$cuenta = $repository->fetchById($cuenta_id);
$body = $request->getBody();
$contents = $body->getContents();
$json = json_decode($contents, JSON_OBJECT_AS_ARRAY);
$output = [
'input' => $json,
'old' => clone $cuenta
];
try {
$cuenta->edit((array) $json);
$status = $cuenta->isDirty();
if ($status) {
$repository->save($cuenta);
}
} catch (PDOException $e) {
error_log($e);
$status = false;
}
$output['cuenta'] = $cuenta;
$output['edited'] = $status;
return $this->withJson($response, $output);
}
}