From caf0dfe4ab99449811b19b51b38caa1a59a3699b Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Thu, 13 Nov 2025 19:15:20 -0300 Subject: [PATCH] Edit reservation --- app/common/Ideal/Service/API.php | 4 +- .../Controller/API/Ventas/Reservations.php | 2 +- app/src/Repository/Venta/Reservation.php | 6 +- app/src/Service/Venta/Credito.php | 18 ++- app/src/Service/Venta/Reservation.php | 151 +++++++++++++++++- 5 files changed, 170 insertions(+), 11 deletions(-) diff --git a/app/common/Ideal/Service/API.php b/app/common/Ideal/Service/API.php index 70da67a..73ed235 100644 --- a/app/common/Ideal/Service/API.php +++ b/app/common/Ideal/Service/API.php @@ -35,11 +35,11 @@ abstract class API extends Ideal\Service /** * @param Define\Model $model - * @param array $new_data + * @param array $newData * @return Define\Model * @throws ServiceAction\Update */ - abstract public function edit(Define\Model $model, array $new_data): Define\Model; + abstract public function edit(Define\Model $model, array $newData): Define\Model; /** * @param int $id diff --git a/app/src/Controller/API/Ventas/Reservations.php b/app/src/Controller/API/Ventas/Reservations.php index 6d92b97..c215a5c 100644 --- a/app/src/Controller/API/Ventas/Reservations.php +++ b/app/src/Controller/API/Ventas/Reservations.php @@ -108,7 +108,7 @@ class Reservations $output = [ 'input' => $input, 'reservation_id' => $reservation_id, - 'reservations' => null, + 'reservation' => null, 'success' => false, ]; diff --git a/app/src/Repository/Venta/Reservation.php b/app/src/Repository/Venta/Reservation.php index 0a19e11..209dd90 100644 --- a/app/src/Repository/Venta/Reservation.php +++ b/app/src/Repository/Venta/Reservation.php @@ -87,6 +87,7 @@ 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; } @@ -515,6 +516,7 @@ class Reservation extends Common\Ideal\Repository $reservation->units[$idx]['value'] = $unit['value']; } } + protected function editBroker(Model\Venta\Reservation &$reservation, array $new_data): void { if (!array_key_exists('broker_rut', $new_data) or $new_data['broker_rut'] === $reservation->broker->rut) { @@ -530,8 +532,8 @@ class Reservation extends Common\Ideal\Repository 'type' => Model\Venta\Reservation\Detail\Type::Broker->value, 'broker_rut' => $new_data['broker_rut'] ]); - $reservation->broker = $this->brokerService->fetchById($new_data['broker_rut']); - } catch (PDOException) {} + $reservation->broker = $this->brokerService->get($new_data['broker_rut']); + } catch (PDOException|Read) {} } protected function editPromotions(Model\Venta\Reservation &$reservation, array $new_data): void { diff --git a/app/src/Service/Venta/Credito.php b/app/src/Service/Venta/Credito.php index fab9e8e..2d966eb 100644 --- a/app/src/Service/Venta/Credito.php +++ b/app/src/Service/Venta/Credito.php @@ -4,6 +4,7 @@ namespace Incoviba\Service\Venta; use DateMalformedStringException; use Exception; use DateTimeImmutable; +use Incoviba\Exception\ServiceAction\Update; use PDOException; use Psr\Log\LoggerInterface; use Incoviba\Common\Ideal; @@ -85,13 +86,20 @@ class Credito extends Ideal\Service } /** - * @throws Exception + * @param Model\Venta\Credito $credito + * @param array $data + * @return Model\Venta\Credito + * @throws Update */ public function edit(Model\Venta\Credito $credito, array $data): Model\Venta\Credito { $uf = $this->moneyService->getUF($credito->pago->fecha); if (array_key_exists('fecha', $data)) { - $fecha = new DateTimeImmutable($data['fecha']); + try { + $fecha = new DateTimeImmutable($data['fecha']); + } catch (DateMalformedStringException $exception) { + throw new Update(__CLASS__, $exception); + } $data['fecha'] = $fecha->format('Y-m-d'); $uf = $this->moneyService->getUF($fecha); $data['uf'] = $uf; @@ -112,6 +120,10 @@ class Credito extends Ideal\Service } $credito->pago = $this->pagoService->edit($credito->pago, $filteredDataPago); - return $this->creditoRepository->edit($credito, $filteredData); + try { + return $this->creditoRepository->edit($credito, $filteredData); + } catch (EmptyResult $exception) { + throw new Update(__CLASS__, $exception); + } } } diff --git a/app/src/Service/Venta/Reservation.php b/app/src/Service/Venta/Reservation.php index 22285f5..0d23e5b 100644 --- a/app/src/Service/Venta/Reservation.php +++ b/app/src/Service/Venta/Reservation.php @@ -22,7 +22,9 @@ class Reservation extends Ideal\Service\API protected Repository\Venta\Reservation\State $stateRepository, protected Service\Persona $personaService, protected Service\Proyecto\Broker $brokerService, - protected Promotion $promotionService, protected Unidad $unitService) + protected Promotion $promotionService, protected Unidad $unitService, + protected Pie $pieService, protected Credito $creditService, + protected Subsidio $subsidyService) { parent::__construct($logger); } @@ -131,13 +133,16 @@ class Reservation extends Ideal\Service\API return $this->createNewReservation($data, $date); } } - public function edit(Define\Model $model, array $new_data): Model\Venta\Reservation + public function edit(Define\Model $model, array $newData): Model\Venta\Reservation { try { - return $this->process($this->reservationRepository->edit($model, $new_data)); + $reservation = $this->reservationRepository->edit($model, $newData); } 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); + $this->editPayment($reservation, $paymentData); + return $this->process($reservation); } public function delete(int $id): Model\Venta\Reservation { @@ -290,6 +295,49 @@ class Reservation extends Ideal\Service\API } } + /** + * @param Model\Venta\Reservation $reservation + * @param array $data + * @return void + * @throws ServiceAction\Update + */ + protected function editPayment(Model\Venta\Reservation $reservation, array $data): void + { + $fields = [ + 'pie', + 'advance', + 'cuotas', + 'investments', + 'credito', + 'credit', + 'subsidio', + 'subsidy', + 'ahorro', + 'savings', + ]; + $filteredData = array_intersect_key($data, array_flip($fields)); + $map = [ + 'pie' => 'advance', + 'cuotas' => 'investments', + 'credito' => 'credit', + 'subsidio' => 'subsidy', + 'ahorro' => 'savings' + ]; + $mappedData = []; + foreach ($filteredData as $key => $value) { + $mappedData[$map[$key]] = $value; + } + if (array_key_exists('advance', $mappedData)) { + $this->editAdvance($reservation, $mappedData); + } + if (array_key_exists('credit', $mappedData)) { + $this->editCredit($reservation, $mappedData); + } + /*if (array_key_exists('subsidy', $mappedData)) { + $this->editSubsidy($reservation, $mappedData); + }*/ + } + /** * @param Model\Venta\Reservation $reservation * @param array $data @@ -316,6 +364,40 @@ class Reservation extends Ideal\Service\API } } + /** + * @param Model\Venta\Reservation $reservation + * @param array $data + * @return void + * @throws ServiceAction\Update + */ + protected function editAdvance(Model\Venta\Reservation $reservation, array $data): void + { + $fields = [ + 'advance', + 'investments' + ]; + $filteredData = array_intersect_key($data, array_flip($fields)); + $inputData = [ + 'fecha' => $reservation->date->format('Y-m-d'), + 'valor' => $filteredData['advance'], + 'cuotas' => $filteredData['investments'] + ]; + if (isset($reservation->payment) and isset($reservation->payment->pie)) { + $this->pieService->edit($reservation->payment->pie, $inputData); + return; + } + try { + $pie = $this->pieService->add($inputData); + } catch (ServiceAction\Create $exception) { + throw new ServiceAction\Update(__CLASS__, $exception); + } + try { + $this->reservationRepository->saveDetail($reservation, Model\Venta\Reservation\Detail\Type::Advance->value, $pie->id); + } catch (PDOException $exception) { + throw new ServiceAction\Update(__CLASS__, $exception); + } + } + /** * @param Model\Venta\Reservation $reservation * @param array $data @@ -337,6 +419,35 @@ class Reservation extends Ideal\Service\API } } + /** + * @param Model\Venta\Reservation $reservation + * @param array $data + * @return void + * @throws ServiceAction\Update + */ + protected function editCredit(Model\Venta\Reservation $reservation, array $data): void + { + $creditValue = $data['credit']; + $inputData = [ + 'fecha' => $reservation->date->format('Y-m-d'), + 'valor' => $creditValue + ]; + if (isset($reservation->payment) and isset($reservation->payment->credito)) { + $this->creditService->edit($reservation->payment->credito, $inputData); + return; + } + try { + $credit = $this->creditService->add($inputData); + } catch (ServiceAction\Create $exception) { + throw new ServiceAction\Update(__CLASS__, $exception); + } + try { + $this->reservationRepository->saveDetail($reservation, Model\Venta\Reservation\Detail\Type::Credit->value, $credit->id); + } catch (PDOException $exception) { + throw new ServiceAction\Update(__CLASS__, $exception); + } + } + /** * @param Model\Venta\Reservation $reservation * @param array $data @@ -363,6 +474,40 @@ class Reservation extends Ideal\Service\API } } + /** + * @param Model\Venta\Reservation $reservation + * @param array $data + * @return void + * @throws ServiceAction\Update + */ + /*protected function editSubsidy(Model\Venta\Reservation $reservation, array $data): void + { + $fields = [ + 'subsidy', + 'savings' + ]; + $filteredData = array_intersect_key($data, array_flip($fields)); + $inputData = [ + 'fecha' => $reservation->date->format('Y-m-d'), + 'subsidio' => $filteredData['subsidy'], + 'ahorro' => $filteredData['savings'] + ]; + if (isset($reservation->payment) and isset($reservation->payment->subsidy)) { + $this->subsidyService->edit($reservation->payment->subsidy, $inputData); + return; + } + try { + $subsidy = $this->subsidyService->add($inputData); + } catch (ServiceAction\Create $exception) { + throw new ServiceAction\Update(__CLASS__, $exception); + } + try { + $this->reservationRepository->saveDetail($reservation, Model\Venta\Reservation\Detail\Type::Subsidy->value, $subsidy->id); + } catch (PDOException $exception) { + throw new ServiceAction\Update(__CLASS__, $exception); + } + }*/ + /** * @param array $data * @param DateTimeImmutable $date