From 7b63cbc7576a252da8248beb3c0b409c54a0658f Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 24 Feb 2025 14:02:12 -0300 Subject: [PATCH] FIX: Fechas revisadas. --- .../views/ventas/escriturar.blade.php | 19 +++++-- app/src/Service/Venta.php | 54 ++++++++++++------- app/src/Service/Venta/Pago.php | 24 +++++---- 3 files changed, 63 insertions(+), 34 deletions(-) diff --git a/app/resources/views/ventas/escriturar.blade.php b/app/resources/views/ventas/escriturar.blade.php index 23445e2..feb6786 100644 --- a/app/resources/views/ventas/escriturar.blade.php +++ b/app/resources/views/ventas/escriturar.blade.php @@ -251,12 +251,21 @@ event.preventDefault() const url = '{{$urls->api}}/venta/{{$venta->id}}/escriturar' const body = new FormData(event.currentTarget) - body.set('fecha', $('#fecha').calendar('get date').toISOString()) - if (body.get('fecha_pago') !== '') { - body.set('fecha_pago', $('#fecha_pago').calendar('get date').toISOString()) + const fecha = $('#fecha').calendar('get date') + body.set('fecha', [fecha.getFullYear(), fecha.getMonth()+1, fecha.getDate()].join('-')) + const $fechaPago = $('#fecha_pago') + if ($fechaPago.length > 0) { + const fechaPago = $fechaPago.calendar('get date') + if (fechaPago !== null) { + body.set('fecha_pago', [fechaPago.getFullYear(), fechaPago.getMonth()+1, fechaPago.getDate()].join('-')) + } } - if (body.get('fecha_reajuste') !== '') { - body.set('fecha_reajuste', $('#fecha_reajuste').calendar('get date').toISOString()) + const $fechaReajuste = $('#fecha_reajuste') + if ($fechaReajuste.length > 0) { + const fechaReajuste = $fechaReajuste.calendar('get date') + if (fechaReajuste !== null) { + body.set('fecha_reajuste', [fechaReajuste.getFullYear(), fechaReajuste.getMonth()+1, fechaReajuste.getDate()].join('-')) + } } fetchAPI(url, {method: 'post', body}).then(response => { if (response.ok) { diff --git a/app/src/Service/Venta.php b/app/src/Service/Venta.php index 596ebfb..70a761d 100644 --- a/app/src/Service/Venta.php +++ b/app/src/Service/Venta.php @@ -285,12 +285,13 @@ 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']); + try { + $fecha = new DateTimeImmutable($data['fecha']); + } catch (\DateMalformedStringException) { + $fecha = new DateTimeImmutable(); + } $estadoData = [ 'venta' => $venta->id, 'estado' => $tipoEstadoVenta->id, @@ -343,12 +344,13 @@ class Venta extends Service return true; } - /** - * @throws Exception - */ protected function reajustarEscritura(Model\Venta $venta, array $data): void { - $fecha = new DateTimeImmutable($data['fecha_reajuste']); + try { + $fecha = new DateTimeImmutable($data['fecha_reajuste']); + } catch (\DateMalformedStringException) { + $fecha = new DateTimeImmutable(); + } $reajusteData = [ 'valor' => $this->valorService->clean($data['valor_reajuste']), 'fecha' => $fecha->format('Y-m-d') @@ -357,12 +359,13 @@ class Venta extends Service $this->pieService->reajustar($pie, $reajusteData); } - /** - * @throws Exception - */ protected function abonoEscritura(Model\Venta $venta, array $data): void { - $fecha = new DateTimeImmutable($data['fecha_pago']); + try { + $fecha = new DateTimeImmutable($data['fecha_pago']); + } catch (\DateMalformedStringException) { + $fecha = new DateTimeImmutable(); + } $uf = $this->moneyService->getUF($fecha); $valor = $data['valor_pago_ufs'] !== '' ? $this->valorService->clean($data['valor_pago_ufs']) * $uf : $this->valorService->clean($data['valor_pago_pesos']); $pagoData = [ @@ -382,12 +385,13 @@ class Venta extends Service $this->ventaRepository->edit($venta, ['escritura' => $escritura->id]); } - /** - * @throws Exception - */ protected function subsidioEscritura(Model\Venta $venta, array $data): void { - $fecha = new DateTimeImmutable($data['fecha']); + try { + $fecha = new DateTimeImmutable($data['fecha']); + } catch (\DateMalformedStringException) { + $fecha = new DateTimeImmutable(); + } $uf = $this->moneyService->getUF($fecha); $subsidioData = [ 'fecha_venta' => $fecha->format('Y-m-d'), @@ -399,15 +403,19 @@ class Venta extends Service $this->ventaRepository->edit($venta, ['subsidio' => $subsidio->id]); } - /** - * @throws Exception - */ protected function editCredito(Model\Venta $venta, array $data): void { - $fecha = new DateTimeImmutable($data['fecha']); + try { + $fecha = new DateTimeImmutable($data['fecha']); + } catch (\DateMalformedStringException) { + $fecha = new DateTimeImmutable(); + } $uf = $this->moneyService->getUF($fecha); $valor = $this->valorService->clean($data['valor_credito']) * $uf; if ($venta->formaPago()->credito === null) { + if ($data['valor_credito'] === 0) { + return; + } $pagoData = [ 'valor' => $valor, 'fecha' => $fecha->format('Y-m-d'), @@ -424,6 +432,12 @@ class Venta extends Service $this->ventaRepository->edit($venta, ['credito' => $credito->id]); return; } + if ($data['valor_credito'] === 0) { + $this->pagoRepository->remove($venta->formaPago()->credito->pago); + $this->creditoRepository->remove($venta->formaPago()->credito); + $this->ventaRepository->edit($venta, ['credito' => null]); + return; + } $this->pagoRepository->edit($venta->formaPago()->credito->pago, [ 'valor' => $valor, 'banco' => $data['banco_credito'], diff --git a/app/src/Service/Venta/Pago.php b/app/src/Service/Venta/Pago.php index 329541a..c211af5 100644 --- a/app/src/Service/Venta/Pago.php +++ b/app/src/Service/Venta/Pago.php @@ -117,13 +117,14 @@ class Pago return $this->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'])); + try { + $data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha'])); + } catch (\DateMalformedStringException) { + $data['uf'] = 0; + } } $filtered_data = $this->pagoRepository->filterData($data); $filtered_data['valor'] = round($this->valorService->clean($filtered_data['valor'])); @@ -141,16 +142,21 @@ class Pago 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'); + try { + $data['fecha'] = (new DateTimeImmutable($data['fecha']))->format('Y-m-d'); + } catch (\DateMalformedStringException) { + $data['fecha'] = (new DateTimeImmutable())->format('Y-m-d'); + } } if (array_key_exists('uf', $data)) { - $data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha'])); + try { + $data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha'])); + } catch (\DateMalformedStringException) { + $data['uf'] = 0; + } } $filteredData = $this->pagoRepository->filterData($data); if (array_key_exists('valor', $filteredData)) {