-
@@ -254,9 +254,15 @@
$('#escriturar_form').submit(event => {
event.preventDefault()
const url = '{{$urls->api}}/venta/{{$venta->id}}/escriturar'
- const data = new FormData(event.currentTarget)
- data.set('fecha', $('#fecha').calendar('get date').toISOString())
- fetchAPI(url, {method: 'post', body: data}).then(response => {
+ 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())
+ }
+ if (body.get('fecha_reajuste') !== '') {
+ body.set('fecha_reajuste', $('#fecha_reajuste').calendar('get date').toISOString())
+ }
+ fetchAPI(url, {method: 'post', body}).then(response => {
if (response.ok) {
return response.json()
}
@@ -267,6 +273,8 @@
})
return false
})
+ $('#fecha_pago').calendar(calendar_date_options)
+ $('#fecha_reajuste').calendar(calendar_date_options)
})
@endpush
diff --git a/app/src/Service/Venta.php b/app/src/Service/Venta.php
index 7265668..be79d78 100644
--- a/app/src/Service/Venta.php
+++ b/app/src/Service/Venta.php
@@ -5,6 +5,7 @@ use DateTimeImmutable;
use Incoviba\Common\Implement;
use Incoviba\Repository;
use Incoviba\Model;
+use PhpParser\Node\Expr\AssignOp\Mod;
class Venta
{
@@ -14,6 +15,8 @@ class Venta
protected Repository\Venta\TipoEstadoVenta $tipoEstadoVentaRepository,
protected Repository\Venta\Credito $creditoRepository,
protected Repository\Venta\Escritura $escrituraRepository,
+ protected Repository\Venta\Pago $pagoRepository,
+ protected Repository\Venta\EstadoPago $estadoPagoRepository,
protected Venta\Propietario $propietarioService,
protected Venta\Propiedad $propiedadService,
protected Venta\Pie $pieService,
@@ -312,8 +315,8 @@ class Venta
if ($this->validarData($data, ['fecha_pago'], ['valor_pago_pesos', 'valor_pago_ufs'])) {
$this->abonoEscritura($venta, $data);
}
- if ($this->validarData($data, ['banco_credito'])) {
- $this->creditoRepository->edit($venta->formaPago()->credito, ['banco' => $data['banco_credito']]);
+ if ($this->validarData($data, ['banco_credito'], ['valor_credito'])) {
+ $this->editCredito($venta, $data);
}
if ($this->validarData($data, ['valor_subsidio', 'valor_ahorro', 'fecha'])) {
$this->subsidioEscritura($venta, $data);
@@ -357,11 +360,18 @@ class Venta
$fecha = new DateTimeImmutable($data['fecha_pago']);
$uf = $this->moneyService->getUF($fecha);
$valor = $data['valor_pago_ufs'] !== '' ? $data['valor_pago_ufs'] * $uf : $data['valor_pago_pesos'];
- $escrituraData = [
+ $pagoData = [
'valor' => $valor,
'fecha' => $fecha->format('Y-m-d'),
'uf' => $uf
];
+ $pago = $this->pagoService->add($pagoData);
+ $escrituraData = [
+ 'valor' => $valor,
+ 'fecha' => $fecha->format('Y-m-d'),
+ 'uf' => $uf,
+ 'pago' => $pago->id
+ ];
$escritura = $this->escrituraRepository->create($escrituraData);
$escritura = $this->escrituraRepository->save($escritura);
$this->ventaRepository->edit($venta, ['escritura' => $escritura->id]);
@@ -379,6 +389,42 @@ class Venta
$subsidio = $this->addSubsidio($subsidioData);
$this->ventaRepository->edit($venta, ['subsidio' => $subsidio->id]);
}
+ protected function editCredito(Model\Venta $venta, array $data): void
+ {
+ $fecha = new DateTimeImmutable($data['fecha']);
+ $uf = $this->moneyService->getUF($fecha);
+ $valor = $data['valor_credito'] * $uf;
+ if ($venta->formaPago()->credito === null) {
+ $pagoData = [
+ 'valor' => $valor,
+ 'fecha' => $fecha->format('Y-m-d'),
+ 'uf' => $uf
+ ];
+ $pago = $this->pagoService->add($pagoData);
+ $creditoData = [
+ 'banco' => $data['banco_credito'],
+ 'valor' => $valor,
+ 'pago' => $pago->id
+ ];
+ $credito = $this->creditoRepository->create($creditoData);
+ $credito = $this->creditoRepository->save($credito);
+ $this->ventaRepository->edit($venta, ['credito' => $credito->id]);
+ return;
+ }
+ $this->pagoRepository->edit($venta->formaPago()->credito->pago, [
+ 'valor' => $valor,
+ 'banco' => $data['banco_credito'],
+ 'uf' => $uf
+ ]);
+ $this->estadoPagoRepository->edit($venta->formaPago()->credito->pago->currentEstado, [
+ 'fecha' => $fecha->format('Y-m-d')
+ ]);
+ $this->creditoRepository->edit($venta->formaPago()->credito, [
+ 'valor' => $valor,
+ 'fecha' => $fecha->format('Y-m-d'),
+ 'uf' => $uf
+ ]);
+ }
public function desistir(Model\Venta $venta, array $data): bool
{