diff --git a/app/common/Ideal/Repository.php b/app/common/Ideal/Repository.php index 18a4237..527c6ae 100644 --- a/app/common/Ideal/Repository.php +++ b/app/common/Ideal/Repository.php @@ -161,4 +161,9 @@ abstract class Repository implements Define\Repository } return $results; } + + public function filterData(array $data): array + { + return $data; + } } diff --git a/app/common/Implement/Exception/EmptyResult.php b/app/common/Implement/Exception/EmptyResult.php index 2a8e3ef..421ba0b 100644 --- a/app/common/Implement/Exception/EmptyResult.php +++ b/app/common/Implement/Exception/EmptyResult.php @@ -6,7 +6,7 @@ use Throwable; class EmptyResult extends Exception { - public function __construct(string $query, ?Throwable $previous = null) + public function __construct(public string $query, ?Throwable $previous = null) { $message = "Empty results for {$query}"; $code = 700; diff --git a/app/src/Controller/API/Ventas/Escrituras.php b/app/src/Controller/API/Ventas/Escrituras.php index ad7f1cc..c1d8cb1 100644 --- a/app/src/Controller/API/Ventas/Escrituras.php +++ b/app/src/Controller/API/Ventas/Escrituras.php @@ -14,51 +14,23 @@ 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 + Service\Venta\Escritura $escrituraService, int $venta_id): ResponseInterface { $body = $request->getParsedBody(); $output = [ 'venta_id' => $venta_id, 'input' => $body, + 'escritura' => null, '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['escritura'] = $escrituraService->add($venta_id, $body); $output['status'] = true; } catch (EmptyResult) {} return $this->withJson($response, $output); } public function edit(ServerRequestInterface $request, ResponseInterface $response, - Service\Venta $ventaService, Repository\Venta\EstadoVenta $estadoVentaRepository, + Service\Venta\Escritura $escrituraService, int $venta_id): ResponseInterface { $body = $request->getParsedBody(); @@ -68,13 +40,7 @@ class Escrituras '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); + $escrituraService->edit($venta_id, $body); $output['edited'] = true; } catch (EmptyResult) {} return $this->withJson($response, $output); diff --git a/app/src/Controller/API/Ventas/Facturacion.php b/app/src/Controller/API/Ventas/Facturacion.php index e88e7ae..272a388 100644 --- a/app/src/Controller/API/Ventas/Facturacion.php +++ b/app/src/Controller/API/Ventas/Facturacion.php @@ -34,8 +34,7 @@ class Facturacion extends Ideal\Service return $this->withJson($response, $output); } public function ventas(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService, - Service\Venta $ventaService, Service\Proyecto\Terreno $terrenoService, Service\UF $ufService, - Service\IPC $ipcService): ResponseInterface + Service\Venta $ventaService): ResponseInterface { $input = $request->getParsedBody(); $output = [ diff --git a/app/src/Controller/API/Ventas/Pagos.php b/app/src/Controller/API/Ventas/Pagos.php index 9f7fa4e..bdf8622 100644 --- a/app/src/Controller/API/Ventas/Pagos.php +++ b/app/src/Controller/API/Ventas/Pagos.php @@ -1,7 +1,9 @@ withJson($response, $output); } + + /** + * @param ServerRequestInterface $request + * @param ResponseInterface $response + * @param Repository\Venta\Pago $pagoRepository + * @param int $pago_id + * @return ResponseInterface + * @throws Exception + */ public function edit(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Pago $pagoRepository, int $pago_id): ResponseInterface { $body = $request->getParsedBody(); @@ -44,6 +55,16 @@ class Pagos } catch (EmptyResult) {} return $this->withJson($response, $output); } + + /** + * @param ServerRequestInterface $request + * @param ResponseInterface $response + * @param Service\Venta\Pago $pagoService + * @param Service\Format $formatService + * @param int $pago_id + * @return ResponseInterface + * @throws Exception + */ public function depositar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService, Service\Format $formatService, int $pago_id): ResponseInterface { @@ -63,6 +84,16 @@ class Pagos } catch (EmptyResult) {} return $this->withJson($response, $output); } + + /** + * @param ServerRequestInterface $request + * @param ResponseInterface $response + * @param Service\Venta\Pago $pagoService + * @param Service\Format $formatService + * @param int $pago_id + * @return ResponseInterface + * @throws Exception + */ public function abonar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService, Service\Format $formatService, int $pago_id): ResponseInterface { @@ -83,6 +114,15 @@ class Pagos } catch (EmptyResult) {} return $this->withJson($response, $output); } + + /** + * @param ServerRequestInterface $request + * @param ResponseInterface $response + * @param Service\Venta\Pago $pagoService + * @param int $pago_id + * @return ResponseInterface + * @throws Exception + */ public function devolver(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService, int $pago_id): ResponseInterface { $body = $request->getParsedBody(); @@ -102,34 +142,49 @@ class Pagos public function para_pendientes(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface { $pagos = $pagoService->getPendientes(); - $pagos_pendientes = []; + $pagos_pendientes = array_map(function(Pago $pago) { + return [ + 'id' => $pago->id + ]; + }, $pagos); + /*$pagos_pendientes = []; foreach ($pagos as $pago) { $pagos_pendientes []= [ 'id' => $pago->id ]; - } + }*/ return $this->withJson($response, ['pagos' => $pagos_pendientes, 'total' => count($pagos_pendientes)]); } public function para_abonar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface { $pagos = $pagoService->getDepositados(); - $pagos_depositados = []; + $pagos_depositados = array_map(function(Pago $pago) { + return [ + 'id' => $pago->id + ]; + }, $pagos); + /*$pagos_depositados = []; foreach ($pagos as $pago) { $pagos_depositados []= [ 'id' => $pago->id ]; - } + }*/ return $this->withJson($response, ['pagos' => $pagos_depositados, 'total' => count($pagos_depositados)]); } public function rebotes(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface { $pagos = $pagoService->getRebotes(); - $rebotes = []; + $rebotes = array_map(function(Pago $pago) { + return [ + 'id' => $pago->id + ]; + }, $pagos); + /*$rebotes = []; foreach ($pagos as $pago) { $rebotes []= [ 'id' => $pago->id ]; - } + }*/ return $this->withJson($response, ['pagos' => $rebotes, 'total' => count($rebotes)]); } diff --git a/app/src/Controller/API/Ventas/Unidades.php b/app/src/Controller/API/Ventas/Unidades.php index aa0ced4..d45a67a 100644 --- a/app/src/Controller/API/Ventas/Unidades.php +++ b/app/src/Controller/API/Ventas/Unidades.php @@ -34,7 +34,10 @@ class Unidades } return $this->withJson($response, $output); } - public function disponibles(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Unidad $unidadRepository, Repository\Proyecto\TipoUnidad $tipoUnidadRepository, Service\Redis $redisService): ResponseInterface + public function disponibles(ServerRequestInterface $request, ResponseInterface $response, + Repository\Venta\Unidad $unidadRepository, + Repository\Proyecto\TipoUnidad $tipoUnidadRepository, + Service\Redis $redisService): ResponseInterface { $body = $request->getBody(); $json = json_decode($body->getContents()); @@ -83,7 +86,10 @@ class Unidades } return $this->withJson($response, $output); } - public function prorrateo(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Unidad $unidadRepository, int $unidad_id): ResponseInterface + public function prorrateo(ServerRequestInterface $request, ResponseInterface $response, + Repository\Venta\Unidad $unidadRepository, + Service\Venta\Unidad $unidadService, + int $unidad_id): ResponseInterface { $body = $request->getParsedBody(); $output = [ @@ -92,8 +98,7 @@ class Unidades 'edited' => false ]; try { - $unidad = $unidadRepository->fetchById($unidad_id); - $unidadRepository->editProrrateo($unidad, $body); + $unidadService->editProrrateo($unidad_id, $body); $output['edited'] = true; } catch (EmptyResult) {} return $this->withJson($response, $output); diff --git a/app/src/Controller/Ventas/Facturacion.php b/app/src/Controller/Ventas/Facturacion.php index 18203ef..0b5a689 100644 --- a/app/src/Controller/Ventas/Facturacion.php +++ b/app/src/Controller/Ventas/Facturacion.php @@ -1,6 +1,7 @@ render($response, 'ventas.facturacion', compact('proyectos')); } + + /** + * @throws Exception + */ public function show(ServerRequestInterface $request, ResponseInterface $response, View $view, - Service\Venta $ventaService, Service\Proyecto\Terreno $terrenoService, - Service\IPC $ipcService, Service\UF $ufService, - int $venta_id): ResponseInterface + Service\Venta $ventaService, Service\Proyecto\Terreno $terrenoService, + Service\IPC $ipcService, Service\UF $ufService, + int $venta_id): ResponseInterface { $venta = $ventaService->getById($venta_id); $uf = $ufService->get($venta->currentEstado()->fecha); diff --git a/app/src/Model/Venta/Unidad/Prorrateo.php b/app/src/Model/Venta/Unidad/Prorrateo.php new file mode 100644 index 0000000..51b71e2 --- /dev/null +++ b/app/src/Model/Venta/Unidad/Prorrateo.php @@ -0,0 +1,19 @@ + $this->unidad->id, + 'prorrateo' => $this->prorrateo, + ]; + } +} diff --git a/app/src/Repository/Venta/Cuota.php b/app/src/Repository/Venta/Cuota.php index ab23319..a64f430 100644 --- a/app/src/Repository/Venta/Cuota.php +++ b/app/src/Repository/Venta/Cuota.php @@ -203,4 +203,8 @@ GROUP BY a.`id`"; ->where('pago = ?'); return $this->fetchOne($query, [$pago_id]); } + public function filterData(array $data): array + { + return array_intersect_key($data, array_fill_keys(['pie', 'fecha', 'valor', 'estado', 'banco', 'fecha_pago', 'abonado', 'fecha_abonado', 'uf', 'pago', 'numero'], 0)); + } } diff --git a/app/src/Repository/Venta/Pago.php b/app/src/Repository/Venta/Pago.php index 0f78cc9..886cb86 100644 --- a/app/src/Repository/Venta/Pago.php +++ b/app/src/Repository/Venta/Pago.php @@ -117,4 +117,8 @@ WHERE venta_id = ?"; ->where('venta.id = ?'); return $this->fetchOne($query, [$venta_id]); } + public function filterData(array $data): array + { + return array_intersect_key($data, array_fill_keys(['valor', 'banco', 'tipo', 'identificador', 'fecha', 'uf', 'pagador', 'asociado'], 0)); + } } diff --git a/app/src/Repository/Venta/Pie.php b/app/src/Repository/Venta/Pie.php index 7faf91b..b6b61f0 100644 --- a/app/src/Repository/Venta/Pie.php +++ b/app/src/Repository/Venta/Pie.php @@ -81,4 +81,8 @@ class Pie extends Ideal\Repository ->where('venta.id = ?'); return $this->fetchOne($query, [$venta_id]); } + public function filterData(array $data): array + { + return array_intersect_key($data, array_fill_keys(['fecha', 'valor', 'uf', 'cuotas', 'asociado', 'reajuste'], 0)); + } } diff --git a/app/src/Repository/Venta/Unidad.php b/app/src/Repository/Venta/Unidad.php index 644fece..19a013c 100644 --- a/app/src/Repository/Venta/Unidad.php +++ b/app/src/Repository/Venta/Unidad.php @@ -39,40 +39,6 @@ class Unidad extends Ideal\Repository { return $this->update($model, ['subtipo', 'piso', 'descripcion', 'orientacion', 'pt'], $new_data); } - public function editProrrateo(Model\Venta\Unidad $model, array $new_data): Model\Venta\Unidad - { - try { - $query = $this->connection->getQueryBuilder() - ->select('prorrateo') - ->from('unidad_prorrateo') - ->where('unidad_id = ?'); - $result = $this->connection->execute($query, [$model->id])->fetch(PDO::FETCH_ASSOC); - if ($result === false) { - throw new Implement\Exception\EmptyResult($query); - } - if ($new_data['prorrateo'] !== $result['prorrateo']) { - $query = $this->connection->getQueryBuilder() - ->update('unidad_prorrateo') - ->set('prorrateo = ?') - ->where('unidad_id = ?'); - $this->connection->execute($query, [$new_data['prorrateo'], $model->id]); - $model->prorrateo = $new_data['prorrateo']; - } - } catch (PDOException | Implement\Exception\EmptyResult) { - $query = $this->connection->getQueryBuilder() - ->insert() - ->into('unidad_prorrateo') - ->columns(['unidad_id', 'prorrateo']) - ->values(['?', '?']); - try { - $this->connection->execute($query, [$model->id, $new_data['prorrateo']]); - $model->prorrateo = $new_data['prorrateo']; - } catch (PDOException) { - throw new Implement\Exception\EmptyResult($query); - } - } - return $model; - } public function fetchById(int $id): Model\Venta\Unidad { diff --git a/app/src/Repository/Venta/Unidad/Prorrateo.php b/app/src/Repository/Venta/Unidad/Prorrateo.php new file mode 100644 index 0000000..c5f5f76 --- /dev/null +++ b/app/src/Repository/Venta/Unidad/Prorrateo.php @@ -0,0 +1,54 @@ +setTable('unidad_prorrateo'); + } + + public function create(?array $data = null): Model\Venta\Unidad\Prorrateo + { + $map = (new Implement\Repository\MapperParser(['prorrateo'])) + ->register('unidad_id', (new Implement\Repository\Mapper()) + ->setProperty('unidad') + ->setFunction(function($data) { + return $this->unidadRepository->fetchById($data['unidad_id']); + })); + return $this->parseData(new Model\Venta\Unidad\Prorrateo(), $data, $map); + } + public function save(Define\Model $model): Model\Venta\Unidad\Prorrateo + { + $model->id = $this->saveNew( + ['unidad_id', 'prorrateo'], + [$model->unidad->id, $model->prorrateo] + ); + return $model; + } + public function edit(Define\Model $model, array $new_data): Model\Venta\Unidad\Prorrateo + { + return $this->update($model, ['unidad_id', 'prorrateo'], $new_data); + } + + public function fetchByUnidad(int $unidad_id): Model\Venta\Unidad\Prorrateo + { + $query = $this->connection->getQueryBuilder() + ->select() + ->from($this->getTable()) + ->where('unidad_id = ?'); + return $this->fetchOne($query, [$unidad_id]); + } + + public function filterData(array $data): array + { + return array_intersect_key($data, array_flip(['prorrateo'])); + } +} diff --git a/app/src/Service/Informe/Excel.php b/app/src/Service/Informe/Excel.php index f11e8a3..d187a92 100644 --- a/app/src/Service/Informe/Excel.php +++ b/app/src/Service/Informe/Excel.php @@ -2,6 +2,7 @@ namespace Incoviba\Service\Informe; use PhpOffice\PhpSpreadsheet; +use PhpOffice\PhpSpreadsheet\Exception; use Incoviba\Common\Define; class Excel implements Define\Informe @@ -40,6 +41,10 @@ class Excel implements Define\Informe return $this; } + /** + * @throws Exception + * @throws PhpSpreadsheet\Writer\Exception + */ public function build(): void { $spreadsheet = new PhpSpreadsheet\Spreadsheet(); diff --git a/app/src/Service/Money/Ine.php b/app/src/Service/Money/Ine.php index 79093fb..07edfe0 100644 --- a/app/src/Service/Money/Ine.php +++ b/app/src/Service/Money/Ine.php @@ -1,6 +1,7 @@ format('Y-m-1')); @@ -21,6 +26,9 @@ class Ine implements Provider return $this->getVar($start, $end); } + /** + * @throws EmptyResponse + */ public function getVar(DateTimeInterface $start, DateTimeInterface $end): float { $base = 1000000000; @@ -33,9 +41,10 @@ class Ine implements Provider ]; $request_uri = implode('?', [ $this->uri, - implode('&', array_map(function($val, $key) { + http_build_query($request_query), + /*implode('&', array_map(function($val, $key) { return "{$key}={$val}"; - }, $request_query, array_keys($request_query))) + }, $request_query, array_keys($request_query)))*/ ]); try { $response = $this->client->get($request_uri); @@ -44,6 +53,9 @@ class Ine implements Provider } $body = $response->getBody(); $json = json_decode($body->getContents()); + if (empty($json)) { + throw new EmptyResponse($request_uri); + } return ((int) str_replace('.', '', $json[0]->valorajustado) - $base) / $base; } } diff --git a/app/src/Service/Money/MiIndicador.php b/app/src/Service/Money/MiIndicador.php index 21dba16..a97cfd6 100644 --- a/app/src/Service/Money/MiIndicador.php +++ b/app/src/Service/Money/MiIndicador.php @@ -15,6 +15,9 @@ class MiIndicador implements Provider public function __construct(protected ClientInterface $client) {} + /** + * @throws EmptyResponse + */ public function get(string $money_symbol, DateTimeInterface $dateTime): float { $request_uri = "{$money_symbol}/{$dateTime->format('d-m-Y')}"; @@ -31,7 +34,7 @@ class MiIndicador implements Provider $body = $response->getBody(); $json = json_decode($body->getContents()); - if ($json->codigo !== $money_symbol or count($json->serie) === 0) { + if (empty($json) or $json->codigo !== $money_symbol or count($json->serie) === 0) { throw new EmptyResponse($request_uri); } return $json->serie[0]->valor; diff --git a/app/src/Service/Proyecto/Terreno.php b/app/src/Service/Proyecto/Terreno.php index 6afa8c5..ae1f7b1 100644 --- a/app/src/Service/Proyecto/Terreno.php +++ b/app/src/Service/Proyecto/Terreno.php @@ -1,8 +1,12 @@ nuboxService->getCuenta($proyecto->inmobiliaria()->rut, 'Terrenos'); @@ -57,6 +72,9 @@ class Terreno extends Ideal\Service return $proyecto->terreno; } + /** + * @throws Exception + */ protected function getValorReajustado(Model\Proyecto $proyecto): ?Model\Proyecto\Terreno { $novPrevTerreno = new DateTimeImmutable($proyecto->terreno->fecha->format('m') < 12 ? $proyecto->terreno->fecha->sub(new DateInterval('P1Y'))->format('Y-11-1') : $proyecto->terreno->fecha->format('Y-11-1')); diff --git a/app/src/Service/Valor.php b/app/src/Service/Valor.php new file mode 100644 index 0000000..cebfbd6 --- /dev/null +++ b/app/src/Service/Valor.php @@ -0,0 +1,13 @@ + $propietario->rut, 'propiedad' => $propiedad->id, 'fecha' => $fecha->format('Y-m-d'), - 'valor_uf' => $this->cleanValue($data['valor']), + 'valor_uf' => $this->valorService->clean($data['valor']), 'fecha_ingreso' => (new DateTimeImmutable())->format('Y-m-d'), 'uf' => $data['uf'] ]; @@ -279,6 +284,10 @@ class Venta extends Service { return $this->formaPagoService->add($data); } + + /** + * @throws Exception + */ protected function addEstado(Model\Venta $venta, Model\Venta\TipoEstadoVenta $tipoEstadoVenta, array $data): void { $fecha = new DateTimeImmutable($data['fecha']); @@ -290,13 +299,6 @@ class Venta extends Service $estado = $this->estadoVentaRepository->create($estadoData); $this->estadoVentaRepository->save($estado); } - protected function cleanValue($value): float - { - if ((float) $value == $value) { - return (float) $value; - } - return (float) str_replace(['.', ','], ['', '.'], $value); - } public function escriturar(Model\Venta $venta, array $data): bool { @@ -340,21 +342,29 @@ class Venta extends Service } return true; } + + /** + * @throws Exception + */ protected function reajustarEscritura(Model\Venta $venta, array $data): void { $fecha = new DateTimeImmutable($data['fecha_reajuste']); $reajusteData = [ - 'valor' => $data['valor_reajuste'], + 'valor' => $this->valorService->clean($data['valor_reajuste']), 'fecha' => $fecha->format('Y-m-d') ]; $pie = $venta->formaPago()->pie; $this->pieService->reajustar($pie, $reajusteData); } + + /** + * @throws Exception + */ protected function abonoEscritura(Model\Venta $venta, array $data): void { $fecha = new DateTimeImmutable($data['fecha_pago']); $uf = $this->moneyService->getUF($fecha); - $valor = $data['valor_pago_ufs'] !== '' ? $data['valor_pago_ufs'] * $uf : $data['valor_pago_pesos']; + $valor = $data['valor_pago_ufs'] !== '' ? $this->valorService->clean($data['valor_pago_ufs']) * $uf : $this->valorService->clean($data['valor_pago_pesos']); $pagoData = [ 'valor' => $valor, 'fecha' => $fecha->format('Y-m-d'), @@ -371,24 +381,32 @@ class Venta extends Service $escritura = $this->escrituraRepository->save($escritura); $this->ventaRepository->edit($venta, ['escritura' => $escritura->id]); } + + /** + * @throws Exception + */ protected function subsidioEscritura(Model\Venta $venta, array $data): void { $fecha = new DateTimeImmutable($data['fecha']); $uf = $this->moneyService->getUF($fecha); $subsidioData = [ 'fecha_venta' => $fecha->format('Y-m-d'), - 'ahorro' => $data['valor_ahorro'], - 'subsidio' => $data['valor_subsidio'], + 'ahorro' => $this->valorService->clean($data['valor_ahorro']), + 'subsidio' => $this->valorService->clean($data['valor_subsidio']), 'uf' => $uf ]; $subsidio = $this->addSubsidio($subsidioData); $this->ventaRepository->edit($venta, ['subsidio' => $subsidio->id]); } + + /** + * @throws Exception + */ protected function editCredito(Model\Venta $venta, array $data): void { $fecha = new DateTimeImmutable($data['fecha']); $uf = $this->moneyService->getUF($fecha); - $valor = $data['valor_credito'] * $uf; + $valor = $this->valorService->clean($data['valor_credito']) * $uf; if ($venta->formaPago()->credito === null) { $pagoData = [ 'valor' => $valor, @@ -425,7 +443,7 @@ class Venta extends Service { try { if ($this->validarData($data, ['fecha', 'devolucion'])) { - $pago = $this->pagoService->add(['fecha' => $data['fecha'], 'valor' => str_replace(['.', ','], ['', '.'], $data['devolucion'])]); + $pago = $this->pagoService->add(['fecha' => $data['fecha'], 'valor' => $this->valorService->clean($data['devolucion'])]); $this->ventaRepository->edit($venta, ['resciliacion' => $pago->id]); } $tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('desistida'); diff --git a/app/src/Service/Venta/BonoPie.php b/app/src/Service/Venta/BonoPie.php index 2702558..04f2b79 100644 --- a/app/src/Service/Venta/BonoPie.php +++ b/app/src/Service/Venta/BonoPie.php @@ -10,7 +10,9 @@ class BonoPie public function add(array $data): Model\Venta\BonoPie { - return new Model\Venta\BonoPie(); + $filteredData = $this->bonoPieRepository->filterData($data); + $bono = $this->bonoPieRepository->create($filteredData); + return $this->bonoPieRepository->save($bono); } public function getByVenta(int $venta_id): Model\Venta\BonoPie { diff --git a/app/src/Service/Venta/Credito.php b/app/src/Service/Venta/Credito.php index cc11a9b..9ca45a3 100644 --- a/app/src/Service/Venta/Credito.php +++ b/app/src/Service/Venta/Credito.php @@ -1,23 +1,25 @@ creditoRepository->fetchByVenta($venta_id); } + /** + * @throws Exception + */ public function add(array $data): Model\Venta\Credito { $fecha = new DateTimeImmutable($data['fecha']); - $uf = $data['uf'] ?? $this->moneyService->getUF($fecha); + $uf = $this->valorService->clean($data['uf']) ?? $this->moneyService->getUF($fecha); $tipoPago = $this->tipoPagoRepository->fetchByDescripcion('carta de resguardo'); - $pago = $this->addPago(['fecha' => $fecha->format('Y-m-d'), 'valor' => $data['valor'] * $uf, 'uf' => $uf, 'tipo' => $tipoPago->id]); + $valor = $this->valorService->clean($data['valor']); + $pago = $this->pagoService->add([ + 'fecha' => $fecha->format('Y-m-d'), + 'valor' => $valor * $uf, + 'uf' => $uf, + 'tipo' => $tipoPago->id + ]); $credito = $this->creditoRepository->create([ - 'valor' => $data['valor'], + 'valor' => $valor, 'fecha' => $fecha->format('Y-m-d'), 'pago' => $pago->id ]); return $this->creditoRepository->save($credito); } + + /** + * @throws Exception + */ public function edit(Model\Venta\Credito $credito, array $data): Model\Venta\Credito { $uf = $this->moneyService->getUF($credito->pago->fecha); @@ -50,7 +65,8 @@ class Credito extends Service $data['uf'] = $uf; } if (array_key_exists('valor', $data)) { - $data['valor'] = round(((float) $data['valor']) * $uf); + $data['valor'] = $this->valorService->clean($data['valor']); + $valorPago = round($data['valor'] * $uf); } $filteredData = array_intersect_key($data, array_fill_keys([ 'fecha', @@ -58,21 +74,12 @@ class Credito extends Service 'valor', 'banco' ], 0)); - $credito->pago = $this->pagoRepository->edit($credito->pago, $filteredData); + $filteredDataPago = $filteredData; + if (isset($valorPago)) { + $filteredDataPago['valor'] = $valorPago; + } + $credito->pago = $this->pagoService->edit($credito->pago, $filteredDataPago); + return $this->creditoRepository->edit($credito, $filteredData); } - protected function addPago(array $data): Model\Venta\Pago - { - $pago = $this->pagoRepository->create($data); - $pago = $this->pagoRepository->save($pago); - $tipoEstado = $this->tipoEstadoPagoRepository->fetchByDescripcion('no pagado'); - $data = [ - 'pago' => $pago->id, - 'fecha' => $pago->fecha->format('Y-m-d'), - 'estado' => $tipoEstado->id - ]; - $estado = $this->estadoPagoRepository->create($data); - $this->estadoPagoRepository->save($estado); - return $pago; - } } diff --git a/app/src/Service/Venta/Cuota.php b/app/src/Service/Venta/Cuota.php index 9735701..771e559 100644 --- a/app/src/Service/Venta/Cuota.php +++ b/app/src/Service/Venta/Cuota.php @@ -98,19 +98,10 @@ class Cuota $filtered_data = array_intersect_key($data, $fields); $pago_data = array_merge($filtered_data, ['tipo' => $tipoPago->id]); $pago = $this->pagoService->add($pago_data); + $data['pago'] = $pago->id; $data['estado'] = $pago->currentEstado->tipoEstadoPago->id; - $fields = array_fill_keys([ - 'pie', - 'fecha', - 'valor', - 'estado', - 'banco', - 'uf', - 'pago', - 'numero' - ], 0); - $filtered_data = array_intersect_key($data, $fields); + $filtered_data = $this->cuotaRepository->filterData($data); $mapped_data = $filtered_data; $mapped_data['valor_$'] = $mapped_data['valor']; unset($mapped_data['valor']); diff --git a/app/src/Service/Venta/Escritura.php b/app/src/Service/Venta/Escritura.php new file mode 100644 index 0000000..f7fdb8d --- /dev/null +++ b/app/src/Service/Venta/Escritura.php @@ -0,0 +1,70 @@ +ventaService->getById($venta_id); + if (isset($venta->formaPago()->escritura)) { + throw new EmptyResult(''); + } + $fecha = new DateTimeImmutable($data['fecha']); + $uf = $this->ufService->get($fecha); + $valor = $this->valorService->clean($data['valor']) * (($data['uf']) ? $uf : 1); + $pago = $this->pagoService->add([ + 'fecha' => $fecha->format('Y-m-d'), + 'valor' => $valor, + 'banco' => $data['banco'], + 'tipo' => $data['tipo'], + 'uf' => $uf + ]); + $escritura = $this->escrituraRepository->create([ + 'valor' => $valor, + 'fecha' => $fecha->format('Y-m-d'), + 'uf' => $uf, + 'pago' => $pago->id + ]); + $escritura = $this->escrituraRepository->save($escritura); + $this->ventaRepository->edit($venta, ['escritura' => $escritura->id]); + return $escritura; + } + + /** + * @throws EmptyResult + * @throws Exception + */ + public function edit(int $venta_id, array $data): Model\Venta\EstadoVenta + { + $venta = $this->ventaService->getById($venta_id); + $estado = $venta->currentEstado(); + if (!in_array($estado->tipoEstadoVenta->descripcion, ['escriturando', 'firmado por inmobiliaria'])) { + throw new EmptyResult(''); + } + $data['fecha'] = (new DateTimeImmutable($data['fecha']))->format('Y-m-d'); + return $this->estadoVentaRepository->edit($estado, $data); + } +} diff --git a/app/src/Service/Venta/FormaPago.php b/app/src/Service/Venta/FormaPago.php index f939db8..440ac96 100644 --- a/app/src/Service/Venta/FormaPago.php +++ b/app/src/Service/Venta/FormaPago.php @@ -1,6 +1,7 @@ cleanValue($mapped_data['valor']); + $mapped_data['valor'] = $this->valorService->clean($mapped_data['valor']); return $this->pieService->add($mapped_data); } protected function addSubsidio(array $data): Model\Venta\Subsidio @@ -101,6 +103,8 @@ class FormaPago extends Ideal\Service 'subsidio', 'uf' ], $filtered_data); + $mapped_data['ahorro'] = $this->valorService->clean($mapped_data['ahorro']); + $mapped_data['subsidio'] = $this->valorService->clean($mapped_data['subsidio']); return $this->subsidioService->add($mapped_data); } protected function addCredito(array $data): Model\Venta\Credito @@ -116,6 +120,7 @@ class FormaPago extends Ideal\Service 'valor', 'uf' ], $filtered_data); + $mapped_data['valor'] = $this->valorService->clean($mapped_data['valor']); return $this->creditoService->add($mapped_data); } protected function addBonoPie(array $data): Model\Venta\BonoPie @@ -129,14 +134,7 @@ class FormaPago extends Ideal\Service 'fecha', 'valor' ], $filtered_data); + $mapped_data['valor'] = $this->valorService->clean($mapped_data['valor']); return $this->bonoPieService->add($mapped_data); } - - protected function cleanValue($value): float - { - if ((float) $value == $value) { - return (float) $value; - } - return (float) str_replace(['.', ','], ['', '.'], $value); - } } diff --git a/app/src/Service/Venta/Pago.php b/app/src/Service/Venta/Pago.php index 93bd986..bc6d9f5 100644 --- a/app/src/Service/Venta/Pago.php +++ b/app/src/Service/Venta/Pago.php @@ -1,11 +1,11 @@ process($this->pagoRepository->fetchDevolucionByVenta($venta_id)); } + /** + * @throws \Exception + */ public function add(array $data): Model\Venta\Pago { if (!isset($data['uf'])) { $data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha'])); } - $fields = array_fill_keys([ - 'valor', - 'banco', - 'tipo', - 'identificador', - 'fecha', - 'uf', - 'pagador', - 'asociado' - ], 0); - $filtered_data = array_intersect_key($data, $fields); + $filtered_data = $this->pagoRepository->filterData($data); + $filtered_data['valor'] = round($this->valorService->clean($filtered_data['valor'])); $pago = $this->pagoRepository->create($filtered_data); $pago = $this->pagoRepository->save($pago); + $tipoEstado = $this->tipoEstadoPagoRepository->fetchByDescripcion('no pagado'); $estado = $this->estadoPagoRepository->create([ 'pago' => $pago->id, @@ -143,6 +139,24 @@ class Pago $pago->currentEstado = $estado; return $pago; } + + /** + * @throws Exception + */ + public function edit(Model\Venta\Pago $pago, array $data): Model\Venta\Pago + { + if (array_key_exists('fecha', $data)) { + $data['fecha'] = (new DateTimeImmutable($data['fecha']))->format('Y-m-d'); + } + if (array_key_exists('uf', $data)) { + $data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha'])); + } + $filteredData = $this->pagoRepository->filterData($data); + $filteredData['valor'] = round($this->valorService->clean($filteredData['valor'])); + $pago = $this->pagoRepository->edit($pago, $filteredData); + $pago->currentEstado = $this->estadoPagoRepository->fetchCurrentByPago($pago->id); + return $pago; + } public function delete(Model\Venta\Pago $pago): bool { try { diff --git a/app/src/Service/Venta/Pie.php b/app/src/Service/Venta/Pie.php index 872c637..92e13bc 100644 --- a/app/src/Service/Venta/Pie.php +++ b/app/src/Service/Venta/Pie.php @@ -25,7 +25,8 @@ class Pie public function add(array $data): Model\Venta\Pie { - $pie = $this->pieRepository->create($data); + $filteredData = $this->pieRepository->filterData($data); + $pie = $this->pieRepository->create($filteredData); return $this->pieRepository->save($pie); } public function addCuota(array $data): Model\Venta\Cuota @@ -34,7 +35,8 @@ class Pie } public function edit(Model\Venta\Pie $pie, array $data): Model\Venta\Pie { - return $this->pieRepository->edit($pie, $data); + $filteredData = $this->pieRepository->filterData($data); + return $this->pieRepository->edit($pie, $filteredData); } public function reajustar(Model\Venta\Pie $pie, array $data): Model\Venta\Pie { diff --git a/app/src/Service/Venta/Subsidio.php b/app/src/Service/Venta/Subsidio.php index bcce88a..d7cdfb9 100644 --- a/app/src/Service/Venta/Subsidio.php +++ b/app/src/Service/Venta/Subsidio.php @@ -1,6 +1,7 @@ subsidioRepository->fetchByVenta($venta_id); } + /** + * @throws Exception + */ public function add(array $data): Model\Venta\Subsidio { $fecha = new DateTimeImmutable($data['fecha']); $uf = $data['uf'] ?? $this->moneyService->getUF($fecha); $tipoPago = $this->tipoPagoRepository->fetchByDescripcion('vale vista'); - $ahorro = $this->addPago(['fecha' => $fecha->format('Y-m-d'), 'valor' => $data['ahorro'] * $uf, 'uf' => $uf, 'tipo' => $tipoPago->id]); - $subsidio = $this->addPago(['fecha' => $fecha->format('Y-m-d'), 'valor' => $data['subsidio'] * $uf, 'uf' => $uf, 'tipo' => $tipoPago->id]); - $subsidio = $this->subsidioRepository->create(['pago' => $ahorro->id, 'subsidio' => $subsidio->id]); + $ahorro = $this->pagoService->add(['fecha' => $fecha->format('Y-m-d'), 'valor' => $this->valorService->clean($data['ahorro']) * $uf, 'uf' => $uf, 'tipo' => $tipoPago->id]); + $subsidioPago = $this->pagoService->add(['fecha' => $fecha->format('Y-m-d'), 'valor' => $this->valorService->clean($data['subsidio']) * $uf, 'uf' => $uf, 'tipo' => $tipoPago->id]); + $subsidio = $this->subsidioRepository->create(['pago' => $ahorro->id, 'subsidio' => $subsidioPago->id]); return $this->subsidioRepository->save($subsidio); } - protected function addPago(array $data): Model\Venta\Pago - { - $pago = $this->pagoRepository->create($data); - $pago = $this->pagoRepository->save($pago); - $tipoEstado = $this->tipoEstadoPagoRepository->fetchByDescripcion('no pagado'); - $data = [ - 'pago' => $pago->id, - 'fecha' => $pago->fecha->format('Y-m-d'), - 'estado' => $tipoEstado->id - ]; - $estado = $this->estadoPagoRepository->create($data); - $this->estadoPagoRepository->save($estado); - return $pago; - } } diff --git a/app/src/Service/Venta/Unidad.php b/app/src/Service/Venta/Unidad.php index 1f5276f..01be029 100644 --- a/app/src/Service/Venta/Unidad.php +++ b/app/src/Service/Venta/Unidad.php @@ -10,6 +10,7 @@ class Unidad { public function __construct( protected Repository\Venta\Unidad $unidadRepository, + protected Repository\Venta\Unidad\Prorrateo $unidadProrrateoRepository, protected Precio $precioService ) {} @@ -38,6 +39,22 @@ class Unidad return $this->unidadRepository->fetchByIdForSearch($unidad_id); } + public function editProrrateo(int $unidad_id, array $new_data): Model\Venta\Unidad + { + $model = $this->unidadRepository->fetchById($unidad_id); + $filteredData = $this->unidadProrrateoRepository->filterData($new_data); + try { + $prorrateo = $this->unidadProrrateoRepository->fetchByUnidad($model->id); + $prorrateo = $this->unidadProrrateoRepository->edit($prorrateo, $filteredData); + } catch (PDOException | EmptyResult) { + $filteredData['unidad_id'] = $model->id; + $prorrateo = $this->unidadProrrateoRepository->create($filteredData); + $this->unidadProrrateoRepository->save($prorrateo); + } + $model->prorrateo = $prorrateo->prorrateo; + return $model; + } + protected function process($unidad): Model\Venta\Unidad { try {