diff --git a/app/resources/views/ventas/escrituras/show.blade.php b/app/resources/views/ventas/escrituras/show.blade.php
index fd50174..a6da40f 100644
--- a/app/resources/views/ventas/escrituras/show.blade.php
+++ b/app/resources/views/ventas/escrituras/show.blade.php
@@ -37,7 +37,7 @@
@@ -62,9 +62,9 @@
function editEscritura() {
const url = '{{$urls->api}}/ventas/escritura/{{$venta->id}}/edit'
const data = new FormData()
- data.set('venta', {{$venta->id}})
const fecha = $('#fecha').calendar('get date')
data.set('fecha', fecha.toISOString())
+ data.set('valor', $('#valor').val())
data.set('estado', $('#estado').dropdown('get value'))
return fetchAPI(url, {method: 'post', body: data}).then(response => {
if (response.ok) {
@@ -78,10 +78,11 @@
})
}
$(document).ready(() => {
- calendar_date_options.initialDate = new Date({{$venta->currentEstado()->fecha->format('Y, m-1, j')}})
+ calendar_date_options.initialDate = new Date({{$venta->formaPago()->escritura->pago->currentEstado->fecha->format('Y, m-1, j')}})
$('#fecha').calendar(calendar_date_options)
- $('#estado').dropdown()
- $('#estado').dropdown('set selected', '{{$venta->currentEstado()->id}}')
+ const $estado = $('#estado')
+ $estado.dropdown()
+ $estado.dropdown('set selected', '{{$venta->formaPago()->escritura->pago->currentEstado->tipoEstadoPago->id}}')
$('#edit_form').submit(event => {
event.preventDefault()
editEscritura()
diff --git a/app/src/Controller/API/Ventas/Escrituras.php b/app/src/Controller/API/Ventas/Escrituras.php
index c1d8cb1..200c472 100644
--- a/app/src/Controller/API/Ventas/Escrituras.php
+++ b/app/src/Controller/API/Ventas/Escrituras.php
@@ -37,11 +37,11 @@ class Escrituras
$output = [
'venta_id' => $venta_id,
'input' => $body,
- 'edited' => false
+ 'success' => false
];
try {
$escrituraService->edit($venta_id, $body);
- $output['edited'] = true;
+ $output['success'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
diff --git a/app/src/Service/Venta/Escritura.php b/app/src/Service/Venta/Escritura.php
index f7fdb8d..9b6824f 100644
--- a/app/src/Service/Venta/Escritura.php
+++ b/app/src/Service/Venta/Escritura.php
@@ -55,16 +55,27 @@ class Escritura extends Ideal\Service
/**
* @throws EmptyResult
- * @throws Exception
*/
- public function edit(int $venta_id, array $data): Model\Venta\EstadoVenta
+ public function edit(int $venta_id, array $data): Model\Venta\Escritura
{
$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);
+ try {
+ $data['fecha'] = (new DateTimeImmutable($data['fecha']))->format('Y-m-d');
+ } catch (\DateMalformedStringException) {
+ unset($data['fecha']);
+ }
+
+ $escritura = $venta->formaPago()->escritura;
+ $pagoData = array_intersect_key($data, array_flip(['valor', 'fecha']));
+ $pago = $escritura->pago;
+ $this->escrituraRepository->edit($escritura, $pagoData);
+ $this->pagoService->edit($pago, $pagoData);
+ $this->pagoService->updateEstado($pago, $data);
+
+ return $this->escrituraRepository->fetchById($escritura->id);
}
}
diff --git a/app/src/Service/Venta/Pago.php b/app/src/Service/Venta/Pago.php
index c211af5..3273f58 100644
--- a/app/src/Service/Venta/Pago.php
+++ b/app/src/Service/Venta/Pago.php
@@ -175,6 +175,18 @@ class Pago
return false;
}
}
+ public function updateEstado(Model\Venta\Pago $pago, array $data): Model\Venta\Pago
+ {
+ if ($pago->currentEstado->tipoEstadoPago->id === $data['estado']) {
+ return $pago;
+ }
+
+ $data['pago'] = $pago->id;
+ $estado = $this->estadoPagoRepository->create($data);
+ $this->estadoPagoRepository->save($estado);
+
+ return $this->process($this->pagoRepository->fetchById($pago->id));
+ }
protected function process($pago): Model\Venta\Pago
{