FIX: edit payments

This commit is contained in:
Juan Pablo Vial
2025-11-25 13:22:52 -03:00
parent c9056451eb
commit 5f348e954e
6 changed files with 50 additions and 13 deletions

View File

@ -573,7 +573,7 @@
form.action = '{{ $urls->base }}/ventas/add' form.action = '{{ $urls->base }}/ventas/add'
// Add CSRF token // 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') const csrfInput = document.createElement('input')
csrfInput.type = 'hidden' csrfInput.type = 'hidden'
csrfInput.name = '_token' csrfInput.name = '_token'

View File

@ -285,9 +285,9 @@
'buyer_marital_status': this.reservation.buyer.datos?.estadoCivil, 'buyer_marital_status': this.reservation.buyer.datos?.estadoCivil,
'buyer_profession': this.reservation.buyer.datos?.ocupacion, 'buyer_profession': this.reservation.buyer.datos?.ocupacion,
'broker_rut': this.reservation.broker?.rut?.toString(), 'broker_rut': this.reservation.broker?.rut?.toString(),
'payment_pie': this.reservation.payment?.pie, 'payment_pie': this.reservation.payment?.pie?.valor?.toString(),
'payment_cuotas': this.reservation.payment?.cuotas?.toString(), 'payment_cuotas': this.reservation.payment?.pie?.cuotas?.toString(),
'payment_credit': this.reservation.payment?.credit, 'payment_credit': this.reservation.payment?.credito?.valor?.toString(),
'promotions[]': this.reservation.promotions?.map(p => p.id.toString()), 'promotions[]': this.reservation.promotions?.map(p => p.id.toString()),
'units[]': this.reservation.units?.map(u => u.unit.id.toString()), 'units[]': this.reservation.units?.map(u => u.unit.id.toString()),
'units_value[]': this.reservation.units?.map(u => u.value.toString()), 'units_value[]': this.reservation.units?.map(u => u.value.toString()),

View File

@ -120,12 +120,12 @@
birthdate.getDate().toString().padStart(2, '0')].join('-')) birthdate.getDate().toString().padStart(2, '0')].join('-'))
body.set(`${this.prefix}_broker_rut`, Rut.clean(this.components.$broker.dropdown('get value'))) 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_pie`)
body.delete(`${this.prefix}_payment_cuotas`) body.delete(`${this.prefix}_payment_cuotas`)
} }
body.delete(`${this.prefix}_payment_has_pie`) 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_credit`)
} }
body.delete(`${this.prefix}_payment_has_credit`) body.delete(`${this.prefix}_payment_has_credit`)

View File

@ -87,7 +87,6 @@ class Reservation extends Common\Ideal\Repository
$this->editUnits($model, $new_data); $this->editUnits($model, $new_data);
$this->editPromotions($model, $new_data); $this->editPromotions($model, $new_data);
$this->editBroker($model, $new_data); $this->editBroker($model, $new_data);
$this->editPayment($model, $new_data);
return $model; return $model;
} }
@ -463,6 +462,9 @@ class Reservation extends Common\Ideal\Repository
protected function editUnits(Model\Venta\Reservation &$reservation, array $new_data): void protected function editUnits(Model\Venta\Reservation &$reservation, array $new_data): void
{ {
if (!array_key_exists('units', $new_data)) {
return;
}
$querySelect = $this->connection->getQueryBuilder() $querySelect = $this->connection->getQueryBuilder()
->select() ->select()
->from('reservation_details') ->from('reservation_details')
@ -479,6 +481,7 @@ class Reservation extends Common\Ideal\Repository
->columns(['reservation_id', 'type', 'reference_id', 'value']) ->columns(['reservation_id', 'type', 'reference_id', 'value'])
->values([':reservation_id', ':type', ':reference_id', ':value']); ->values([':reservation_id', ':type', ':reference_id', ':value']);
$statementInsert = $this->connection->prepare($queryInsert); $statementInsert = $this->connection->prepare($queryInsert);
foreach ($new_data['units'] as $unit) { foreach ($new_data['units'] as $unit) {
$idx = $reservation->findUnit($unit['unit_id']); $idx = $reservation->findUnit($unit['unit_id']);
if ($idx === null) { if ($idx === null) {
@ -537,6 +540,9 @@ class Reservation extends Common\Ideal\Repository
} }
protected function editPromotions(Model\Venta\Reservation &$reservation, array $new_data): void protected function editPromotions(Model\Venta\Reservation &$reservation, array $new_data): void
{ {
if (!array_key_exists('promotions', $new_data)) {
return;
}
$querySelect = $this->connection->getQueryBuilder() $querySelect = $this->connection->getQueryBuilder()
->select() ->select()
->from('reservation_details') ->from('reservation_details')
@ -547,7 +553,7 @@ class Reservation extends Common\Ideal\Repository
->set('value = :value') ->set('value = :value')
->where('reservation_id = :id AND type = "Promotion" AND reference_id = :promotion_id'); ->where('reservation_id = :id AND type = "Promotion" AND reference_id = :promotion_id');
$statementUpdate = $this->connection->prepare($queryUpdate); $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')); $idx = array_search($promotion_id, array_column($reservation->promotions, 'id'));
if ($idx === false) { if ($idx === false) {
$reservation->promotions []= $this->promotionRepository->fetchById($promotion_id); $reservation->promotions []= $this->promotionRepository->fetchById($promotion_id);

View File

@ -56,7 +56,11 @@ class Credito extends Ideal\Service
} catch (DateMalformedStringException) { } catch (DateMalformedStringException) {
$fecha = new DateTimeImmutable(); $fecha = new DateTimeImmutable();
} }
if (array_key_exists('uf', $data)) {
$uf = $this->valorService->clean($data['uf']) ?? $this->moneyService->getUF($fecha); $uf = $this->valorService->clean($data['uf']) ?? $this->moneyService->getUF($fecha);
} else {
$uf = $this->moneyService->getUF($fecha);
}
try { try {
$tipoPago = $this->tipoPagoRepository->fetchByDescripcion('carta de resguardo'); $tipoPago = $this->tipoPagoRepository->fetchByDescripcion('carta de resguardo');
} catch (EmptyResult $exception) { } catch (EmptyResult $exception) {

View File

@ -135,12 +135,17 @@ class Reservation extends Ideal\Service\API
} }
public function edit(Define\Model $model, array $newData): Model\Venta\Reservation 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 { try {
$reservation = $this->reservationRepository->edit($model, $newData); $reservation = $this->reservationRepository->edit($model, $editData);
} catch (PDOException | Implement\Exception\EmptyResult $exception) { } catch (PDOException | Implement\Exception\EmptyResult $exception) {
throw new ServiceAction\Update(__CLASS__, $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); $this->editPayment($reservation, $paymentData);
return $this->process($reservation); return $this->process($reservation);
} }
@ -315,7 +320,13 @@ class Reservation extends Ideal\Service\API
'ahorro', 'ahorro',
'savings', '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 = [ $map = [
'pie' => 'advance', 'pie' => 'advance',
'cuotas' => 'investments', 'cuotas' => 'investments',
@ -325,13 +336,21 @@ class Reservation extends Ideal\Service\API
]; ];
$mappedData = []; $mappedData = [];
foreach ($filteredData as $key => $value) { foreach ($filteredData as $key => $value) {
if (array_key_exists($key, $map)) {
$mappedData[$map[$key]] = $value; $mappedData[$map[$key]] = $value;
continue;
}
$mappedData[$key] = $value;
} }
if (array_key_exists('advance', $mappedData)) { if (array_key_exists('advance', $mappedData)) {
$this->editAdvance($reservation, $mappedData); $this->editAdvance($reservation, $mappedData);
} else {
$this->removeAdvance($reservation);
} }
if (array_key_exists('credit', $mappedData)) { if (array_key_exists('credit', $mappedData)) {
$this->editCredit($reservation, $mappedData); $this->editCredit($reservation, $mappedData);
} else {
$this->removeCredit($reservation);
} }
/*if (array_key_exists('subsidy', $mappedData)) { /*if (array_key_exists('subsidy', $mappedData)) {
$this->editSubsidy($reservation, $mappedData); $this->editSubsidy($reservation, $mappedData);
@ -397,6 +416,10 @@ class Reservation extends Ideal\Service\API
throw new ServiceAction\Update(__CLASS__, $exception); throw new ServiceAction\Update(__CLASS__, $exception);
} }
} }
protected function removeAdvance(Model\Venta\Reservation $reservation): void
{
}
/** /**
* @param Model\Venta\Reservation $reservation * @param Model\Venta\Reservation $reservation
@ -447,6 +470,10 @@ class Reservation extends Ideal\Service\API
throw new ServiceAction\Update(__CLASS__, $exception); throw new ServiceAction\Update(__CLASS__, $exception);
} }
} }
protected function removeCredit(Model\Venta\Reservation $reservation): void
{
}
/** /**
* @param Model\Venta\Reservation $reservation * @param Model\Venta\Reservation $reservation