Editar propiedad en venta

This commit is contained in:
2023-11-29 20:44:13 -03:00
parent 39048e12b3
commit 62153dd1ef
4 changed files with 145 additions and 45 deletions

View File

@ -244,6 +244,9 @@
return { return {
price: (id, value) => { price: (id, value) => {
const idx = this.unidades.findIndex(unidad => unidad.pu_id === id) const idx = this.unidades.findIndex(unidad => unidad.pu_id === id)
if (idx === -1) {
return
}
const old_value = this.unidades[idx].precio const old_value = this.unidades[idx].precio
if (old_value === parseFloat(value)) { if (old_value === parseFloat(value)) {
return return

View File

@ -2,12 +2,18 @@
@section('page_content') @section('page_content')
<div class="ui container"> <div class="ui container">
<h2>Editar Propiedad - {{$proyecto->descripcion}} {{$propiedad->summary()}}</h2> <h2 class="ui header">
Editar Propiedad - {{$proyecto->descripcion}}
<a href="{{$urls->base}}/venta/{{$venta->id}}">
{{$propiedad->summary()}}
</a>
</h2>
<table class="ui table"> <table class="ui table">
<thead> <thead>
<tr> <tr>
<th>Tipo</th> <th>Tipo</th>
<th>Unidad</th> <th>Unidad</th>
<th>Valor</th>
<th class="right aligned"> <th class="right aligned">
<button class="ui small green circular icon button" id="add_unidad"> <button class="ui small green circular icon button" id="add_unidad">
<i class="plus icon"></i> <i class="plus icon"></i>
@ -15,13 +21,19 @@
</th> </th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody id="unidades">
@foreach($propiedad->unidades as $unidad) @foreach($propiedad->unidades as $unidad)
<tr> <tr>
<td>{{ucwords($unidad->proyectoTipoUnidad->tipoUnidad->descripcion)}}</td> <td>{{ucwords($unidad->proyectoTipoUnidad->tipoUnidad->descripcion)}}</td>
<td>{{$unidad->descripcion}}</td> <td>{{$unidad->descripcion}}</td>
<td>
<div class="ui right labeled input">
<input type="text" name="precio{{$unidad->pu_id}}" class="precio" data-pid="{{$unidad->pu_id}}" value="{{($unidad->valor > 0) ? $unidad->valor : $unidad->precio($venta->fecha)}}" />
<div class="ui basic label">UF</div>
</div>
</td>
<td class="right aligned"> <td class="right aligned">
<button class="ui small red circular icon button remove_unidad" data-id="{{$unidad->id}}"> <button class="ui small red circular icon button remove_unidad" data-pid="{{$unidad->pu_id}}">
<i class="remove icon"></i> <i class="remove icon"></i>
</button> </button>
</td> </td>
@ -54,51 +66,132 @@
@push('page_scripts') @push('page_scripts')
<script type="text/javascript"> <script type="text/javascript">
const unidades = { const propiedad = {
@foreach($tiposUnidades as $tipoUnidad) unidades: [
{{$tipoUnidad->id}}: [ @foreach($propiedad->unidades as $unidad)
@foreach($unidades as $unidad) {
@if ($unidad->proyectoTipoUnidad->tipoUnidad->id === $tipoUnidad->id) id: {{$unidad->id}},
{ tipo: '{{ucwords($unidad->proyectoTipoUnidad->tipoUnidad->descripcion)}}',
value: {{$unidad->id}}, descripcion: '{{$unidad->descripcion}}',
text: '{{$unidad->descripcion}}', pid: {{$unidad->pu_id}},
name: '{{$unidad->descripcion}}' valor: {{($unidad->valor > 0) ? $unidad->valor : $unidad->precio($venta->fecha)}}
}, },
@endif
@endforeach @endforeach
], ],
@endforeach tipos: {
} @foreach($tiposUnidades as $tipoUnidad)
function changeTipoUnidad(tipo_unidad_id) { {{$tipoUnidad->id}}: [
$('#unidad').dropdown('change values', unidades[tipo_unidad_id].sort((a, b) => a.text.padStart(4, '0').localeCompare(b.text.padStart(4, '0')))) @foreach($unidades as $unidad)
} @if ($unidad->proyectoTipoUnidad->tipoUnidad->id === $tipoUnidad->id)
function addUnidad() { {
$('#add_modal').modal('show') value: {{$unidad->id}},
} text: '{{$unidad->descripcion}}',
function removeUnidad(unidad_id) { name: '{{$unidad->descripcion}}'
console.debug(unidad_id) },
@endif
@endforeach
],
@endforeach
},
changeTipoUnidad: function(tipo_unidad_id) {
$('#unidad').dropdown('change values', this.tipos[tipo_unidad_id].sort((a, b) => a.text.padStart(4, '0').localeCompare(b.text.padStart(4, '0'))))
},
addUnidad: function() {
$('#add_modal').modal('show')
},
removeUnidad: function(unidad_id) {
console.debug(unidad_id)
},
updatePrecio: function(pid, valor) {
const idx = this.unidades.findIndex(unidad => unidad.pid === pid)
if (idx === -1) {
return
}
const old_value = this.unidades[idx].valor
if (old_value === parseFloat(value)) {
return
}
const url = '{{$urls->api}}/ventas/propiedades/unidad/' + id + '/edit'
const data = new FormData()
data.set('valor', value)
return fetchAPI(url, {method: 'post', body: data}).then(response => {
if (response.ok) {
return response.json()
}
}).then(json => {
if (!json.edited) {
return
}
const idx = this.unidades.findIndex(unidad => unidad.pid === json.propiedad_unidad_id)
this.unidades[idx].valor = parseFloat(json.input.valor)
this.draw()
})
},
draw: function() {
const tbody = $('#unidades')
tbody.html('')
this.unidades.forEach(unidad => {
const row = $('<tr></tr>').append(
$('<td></td>').html(unidad.tipo)
).append(
$('<td></td>').html(unidad.descripcion)
).append(
$('<td></td>').append(
$('<div></div>').addClass('ui right labeled input').append(
$('<input />').addClass('precio').attr('type', 'text').attr('data-pid', unidad.pid).attr('value', unidad.valor)
).append(
$('<div></div>').addClass('ui basic label').html('UF')
)
)
).append(
$('<td></td>').addClass('right aligned').append(
$('<button></button>').addClass('ui small red circular icon button remove_unidad').attr('data-pid', unidad.pid).append(
$('<i></i>').addClass('remove icon')
)
)
)
tbody.append(row)
})
$('.price').change(event => {
const pid = $(event.currentTarget).data('pid')
const val = $(event.currentTarget).val()
this.updatePrecio(pid, val)
})
$('.remove_unidad').click(event => {
const unidad_id = $(event.currentTarget).data('pid')
this.removeUnidad(unidad_id)
})
},
setup: function() {
$('#add_unidad').click(event => {
this.addUnidad()
})
$('.remove_unidad').click(event => {
const unidad_id = $(event.currentTarget).data('pid')
this.removeUnidad(unidad_id)
})
$('#add_modal').modal()
const tipo = $('#tipo')
tipo.dropdown()
tipo.change(event => {
this.changeTipoUnidad(tipo.val())
})
$('#unidad').dropdown()
this.changeTipoUnidad(tipo.val())
$('#add_form').submit(event => {
event.preventDefault()
tipo.model('hide')
return false
})
$('.precio').change(event => {
const pid = $(event.currentTarget).data('pid')
const val = $(event.currentTarget).val()
this.updatePrecio(pid, val)
})
}
} }
$(document).ready(() => { $(document).ready(() => {
$('#add_unidad').click(event => { propiedad.setup()
addUnidad()
})
$('.remove_unidad').click(event => {
const unidad_id = $(event.currentTarget).data('id')
removeUnidad(unidad_id)
})
$('#add_modal').modal()
const tipo = $('#tipo')
tipo.dropdown()
tipo.change(event => {
changeTipoUnidad(tipo.val())
})
$('#unidad').dropdown()
changeTipoUnidad(tipo.val())
$('#add_form').submit(event => {
event.preventDefault()
tipo.model('hide')
return false
})
}) })
</script> </script>
@endpush @endpush

View File

@ -16,6 +16,7 @@
<th class="center aligned">Piso</th> <th class="center aligned">Piso</th>
<th class="right aligned">Metros vendibles</th> <th class="right aligned">Metros vendibles</th>
<th class="right aligned">Precio</th> <th class="right aligned">Precio</th>
<th class="right aligned">Valor Venta</th>
<th class="right aligned">UF/</th> <th class="right aligned">UF/</th>
<th class="center aligned">Orientacion</th> <th class="center aligned">Orientacion</th>
</tr> </tr>
@ -41,6 +42,9 @@
<td class="right aligned"> <td class="right aligned">
{{$format->ufs($unidad->precio($venta->fecha)->valor)}} {{$format->ufs($unidad->precio($venta->fecha)->valor)}}
</td> </td>
<td class="right aligned">
{{$format->ufs($unidad->valor)}}
</td>
<td class="right aligned"> <td class="right aligned">
@if ($unidad->proyectoTipoUnidad->tipoUnidad->descripcion === 'departamento') @if ($unidad->proyectoTipoUnidad->tipoUnidad->descripcion === 'departamento')
{{$format->number($unidad->precio($venta->fecha)->valor / $unidad->proyectoTipoUnidad->vendible(), 2)}} UF/ {{$format->number($unidad->precio($venta->fecha)->valor / $unidad->proyectoTipoUnidad->vendible(), 2)}} UF/

View File

@ -69,7 +69,7 @@ class Ventas
$tiposUnidades []= $unidad->proyectoTipoUnidad->tipoUnidad; $tiposUnidades []= $unidad->proyectoTipoUnidad->tipoUnidad;
} }
} }
return $view->render($response, 'ventas.propiedades.edit', compact('propiedad', 'proyecto', 'tiposUnidades', 'unidades', 'venta_id')); return $view->render($response, 'ventas.propiedades.edit', compact('propiedad', 'proyecto', 'tiposUnidades', 'unidades', 'venta'));
} }
public function add(ServerRequestInterface $request, ResponseInterface $response, View $view, Repository\Region $regionRepository, Repository\Proyecto $proyectoRepository): ResponseInterface public function add(ServerRequestInterface $request, ResponseInterface $response, View $view, Repository\Region $regionRepository, Repository\Proyecto $proyectoRepository): ResponseInterface
{ {