From 5f348e954eadd1e345d5d94ab70be529f392d65b Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Tue, 25 Nov 2025 13:22:52 -0300 Subject: [PATCH] FIX: edit payments --- .../views/ventas/reservations.blade.php | 2 +- .../modal/common/scripts/modal.blade.php | 6 ++-- .../ventas/reservations/modal/edit.blade.php | 4 +-- app/src/Repository/Venta/Reservation.php | 10 ++++-- app/src/Service/Venta/Credito.php | 6 +++- app/src/Service/Venta/Reservation.php | 35 ++++++++++++++++--- 6 files changed, 50 insertions(+), 13 deletions(-) diff --git a/app/resources/views/ventas/reservations.blade.php b/app/resources/views/ventas/reservations.blade.php index 0dc45a8..dfe2591 100644 --- a/app/resources/views/ventas/reservations.blade.php +++ b/app/resources/views/ventas/reservations.blade.php @@ -573,7 +573,7 @@ form.action = '{{ $urls->base }}/ventas/add' // Add CSRF token - const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content') + const csrfToken = Math.random().toString(36).substring(2) const csrfInput = document.createElement('input') csrfInput.type = 'hidden' csrfInput.name = '_token' diff --git a/app/resources/views/ventas/reservations/modal/common/scripts/modal.blade.php b/app/resources/views/ventas/reservations/modal/common/scripts/modal.blade.php index 7e1657e..fd1b0fd 100644 --- a/app/resources/views/ventas/reservations/modal/common/scripts/modal.blade.php +++ b/app/resources/views/ventas/reservations/modal/common/scripts/modal.blade.php @@ -285,9 +285,9 @@ 'buyer_marital_status': this.reservation.buyer.datos?.estadoCivil, 'buyer_profession': this.reservation.buyer.datos?.ocupacion, 'broker_rut': this.reservation.broker?.rut?.toString(), - 'payment_pie': this.reservation.payment?.pie, - 'payment_cuotas': this.reservation.payment?.cuotas?.toString(), - 'payment_credit': this.reservation.payment?.credit, + 'payment_pie': this.reservation.payment?.pie?.valor?.toString(), + 'payment_cuotas': this.reservation.payment?.pie?.cuotas?.toString(), + 'payment_credit': this.reservation.payment?.credito?.valor?.toString(), 'promotions[]': this.reservation.promotions?.map(p => p.id.toString()), 'units[]': this.reservation.units?.map(u => u.unit.id.toString()), 'units_value[]': this.reservation.units?.map(u => u.value.toString()), diff --git a/app/resources/views/ventas/reservations/modal/edit.blade.php b/app/resources/views/ventas/reservations/modal/edit.blade.php index 7415984..5e366f5 100644 --- a/app/resources/views/ventas/reservations/modal/edit.blade.php +++ b/app/resources/views/ventas/reservations/modal/edit.blade.php @@ -120,12 +120,12 @@ birthdate.getDate().toString().padStart(2, '0')].join('-')) body.set(`${this.prefix}_broker_rut`, Rut.clean(this.components.$broker.dropdown('get value'))) - if (this.components.payments.components.pie.$checkbox.checkbox('is unchecked')) { + if (!this.components.payments.components.pie.$checkbox.is(':checked')) { body.delete(`${this.prefix}_payment_pie`) body.delete(`${this.prefix}_payment_cuotas`) } body.delete(`${this.prefix}_payment_has_pie`) - if (this.components.payments.components.credit.$checkbox.checkbox('is unchecked')) { + if (!this.components.payments.components.credit.$checkbox.is(':checked')) { body.delete(`${this.prefix}_payment_credit`) } body.delete(`${this.prefix}_payment_has_credit`) diff --git a/app/src/Repository/Venta/Reservation.php b/app/src/Repository/Venta/Reservation.php index 209dd90..7cf1816 100644 --- a/app/src/Repository/Venta/Reservation.php +++ b/app/src/Repository/Venta/Reservation.php @@ -87,7 +87,6 @@ class Reservation extends Common\Ideal\Repository $this->editUnits($model, $new_data); $this->editPromotions($model, $new_data); $this->editBroker($model, $new_data); - $this->editPayment($model, $new_data); return $model; } @@ -463,6 +462,9 @@ class Reservation extends Common\Ideal\Repository protected function editUnits(Model\Venta\Reservation &$reservation, array $new_data): void { + if (!array_key_exists('units', $new_data)) { + return; + } $querySelect = $this->connection->getQueryBuilder() ->select() ->from('reservation_details') @@ -479,6 +481,7 @@ class Reservation extends Common\Ideal\Repository ->columns(['reservation_id', 'type', 'reference_id', 'value']) ->values([':reservation_id', ':type', ':reference_id', ':value']); $statementInsert = $this->connection->prepare($queryInsert); + foreach ($new_data['units'] as $unit) { $idx = $reservation->findUnit($unit['unit_id']); if ($idx === null) { @@ -537,6 +540,9 @@ class Reservation extends Common\Ideal\Repository } protected function editPromotions(Model\Venta\Reservation &$reservation, array $new_data): void { + if (!array_key_exists('promotions', $new_data)) { + return; + } $querySelect = $this->connection->getQueryBuilder() ->select() ->from('reservation_details') @@ -547,7 +553,7 @@ class Reservation extends Common\Ideal\Repository ->set('value = :value') ->where('reservation_id = :id AND type = "Promotion" AND reference_id = :promotion_id'); $statementUpdate = $this->connection->prepare($queryUpdate); - foreach ($new_data as $promotion_id => $value) { + foreach ($new_data['promotions'] as $promotion_id => $value) { $idx = array_search($promotion_id, array_column($reservation->promotions, 'id')); if ($idx === false) { $reservation->promotions []= $this->promotionRepository->fetchById($promotion_id); diff --git a/app/src/Service/Venta/Credito.php b/app/src/Service/Venta/Credito.php index 2d966eb..efb128e 100644 --- a/app/src/Service/Venta/Credito.php +++ b/app/src/Service/Venta/Credito.php @@ -56,7 +56,11 @@ class Credito extends Ideal\Service } catch (DateMalformedStringException) { $fecha = new DateTimeImmutable(); } - $uf = $this->valorService->clean($data['uf']) ?? $this->moneyService->getUF($fecha); + if (array_key_exists('uf', $data)) { + $uf = $this->valorService->clean($data['uf']) ?? $this->moneyService->getUF($fecha); + } else { + $uf = $this->moneyService->getUF($fecha); + } try { $tipoPago = $this->tipoPagoRepository->fetchByDescripcion('carta de resguardo'); } catch (EmptyResult $exception) { diff --git a/app/src/Service/Venta/Reservation.php b/app/src/Service/Venta/Reservation.php index 0d23e5b..fc6779c 100644 --- a/app/src/Service/Venta/Reservation.php +++ b/app/src/Service/Venta/Reservation.php @@ -135,12 +135,17 @@ class Reservation extends Ideal\Service\API } public function edit(Define\Model $model, array $newData): Model\Venta\Reservation { + $editData = []; + foreach ($newData as $key => $value) { + $newKey = str_replace('edit_', '', $key); + $editData[$newKey] = $value; + } try { - $reservation = $this->reservationRepository->edit($model, $newData); + $reservation = $this->reservationRepository->edit($model, $editData); } catch (PDOException | Implement\Exception\EmptyResult $exception) { throw new ServiceAction\Update(__CLASS__, $exception); } - $paymentData = array_filter($newData, fn($key) => str_starts_with($key, 'payment_'), ARRAY_FILTER_USE_KEY); + $paymentData = array_filter($editData, fn($key) => str_starts_with($key, 'payment_'), ARRAY_FILTER_USE_KEY); $this->editPayment($reservation, $paymentData); return $this->process($reservation); } @@ -315,7 +320,13 @@ class Reservation extends Ideal\Service\API 'ahorro', 'savings', ]; - $filteredData = array_intersect_key($data, array_flip($fields)); + $filteredData = []; + foreach ($data as $key => $value) { + $newKey = str_replace('payment_', '', $key); + if (in_array($newKey, $fields)) { + $filteredData[$newKey] = $value; + } + } $map = [ 'pie' => 'advance', 'cuotas' => 'investments', @@ -325,13 +336,21 @@ class Reservation extends Ideal\Service\API ]; $mappedData = []; foreach ($filteredData as $key => $value) { - $mappedData[$map[$key]] = $value; + if (array_key_exists($key, $map)) { + $mappedData[$map[$key]] = $value; + continue; + } + $mappedData[$key] = $value; } if (array_key_exists('advance', $mappedData)) { $this->editAdvance($reservation, $mappedData); + } else { + $this->removeAdvance($reservation); } if (array_key_exists('credit', $mappedData)) { $this->editCredit($reservation, $mappedData); + } else { + $this->removeCredit($reservation); } /*if (array_key_exists('subsidy', $mappedData)) { $this->editSubsidy($reservation, $mappedData); @@ -397,6 +416,10 @@ class Reservation extends Ideal\Service\API throw new ServiceAction\Update(__CLASS__, $exception); } } + protected function removeAdvance(Model\Venta\Reservation $reservation): void + { + + } /** * @param Model\Venta\Reservation $reservation @@ -447,6 +470,10 @@ class Reservation extends Ideal\Service\API throw new ServiceAction\Update(__CLASS__, $exception); } } + protected function removeCredit(Model\Venta\Reservation $reservation): void + { + + } /** * @param Model\Venta\Reservation $reservation