Files
oficial/app/src/Controller/API/Ventas/Escrituras.php
2024-02-14 13:54:01 -03:00

83 lines
3.1 KiB
PHP

<?php
namespace Incoviba\Controller\API\Ventas;
use DateTimeImmutable;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Controller\API\withJson;
use Incoviba\Repository;
use Incoviba\Service;
class Escrituras
{
use withJson;
public function add(ServerRequestInterface $request, ResponseInterface $response,
Repository\Venta $ventaRepository, Service\Venta $ventaService,
Repository\Venta\Escritura $escrituraRepository, Service\Venta\Pago $pagoService,
Service\UF $ufService, int $venta_id): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
'venta_id' => $venta_id,
'input' => $body,
'status' => false
];
try {
$venta = $ventaService->getById($venta_id);
if (isset($venta->formaPago()->escritura)) {
throw new EmptyResult('');
}
$fecha = new DateTimeImmutable($body['fecha']);
$uf = $ufService->get($fecha);
$valor = $body['valor'];
if (str_contains($valor, ',')) {
$valor = str_replace(['.', ','], ['', '.'], $valor);
}
$valor = ((float) $valor) * (($body['uf']) ? $uf : 1);
$data = [
'fecha' => $fecha->format('Y-m-d'),
'valor' => $valor,
'banco' => $body['banco'],
'tipo' => $body['tipo'],
'uf' => $uf
];
$pago = $pagoService->add($data);
$data = [
'valor' => $valor,
'fecha' => $fecha->format('Y-m-d'),
'uf' => $uf,
'pago' => $pago->id
];
$escritura = $escrituraRepository->create($data);
$escrituraRepository->save($escritura);
$ventaRepository->edit($venta, ['escritura' => $escritura->id]);
$output['status'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta $ventaService, Repository\Venta\EstadoVenta $estadoVentaRepository,
int $venta_id): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
'venta_id' => $venta_id,
'input' => $body,
'edited' => false
];
try {
$venta = $ventaService->getById($venta_id);
$estado = $venta->currentEstado();
if (!in_array($estado->tipoEstadoVenta->descripcion, ['escriturando', 'firmado por inmobiliaria'])) {
throw new EmptyResult('');
}
$body['fecha'] = (new DateTimeImmutable($body['fecha']))->format('Y-m-d');
$estadoVentaRepository->edit($estado, $body);
$output['edited'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
}