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 {
price: (id, value) => {
const idx = this.unidades.findIndex(unidad => unidad.pu_id === id)
if (idx === -1) {
return
}
const old_value = this.unidades[idx].precio
if (old_value === parseFloat(value)) {
return

View File

@ -2,12 +2,18 @@
@section('page_content')
<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">
<thead>
<tr>
<th>Tipo</th>
<th>Unidad</th>
<th>Valor</th>
<th class="right aligned">
<button class="ui small green circular icon button" id="add_unidad">
<i class="plus icon"></i>
@ -15,13 +21,19 @@
</th>
</tr>
</thead>
<tbody>
<tbody id="unidades">
@foreach($propiedad->unidades as $unidad)
<tr>
<td>{{ucwords($unidad->proyectoTipoUnidad->tipoUnidad->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">
<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>
</button>
</td>
@ -54,51 +66,132 @@
@push('page_scripts')
<script type="text/javascript">
const unidades = {
@foreach($tiposUnidades as $tipoUnidad)
{{$tipoUnidad->id}}: [
@foreach($unidades as $unidad)
@if ($unidad->proyectoTipoUnidad->tipoUnidad->id === $tipoUnidad->id)
{
value: {{$unidad->id}},
text: '{{$unidad->descripcion}}',
name: '{{$unidad->descripcion}}'
},
@endif
const propiedad = {
unidades: [
@foreach($propiedad->unidades as $unidad)
{
id: {{$unidad->id}},
tipo: '{{ucwords($unidad->proyectoTipoUnidad->tipoUnidad->descripcion)}}',
descripcion: '{{$unidad->descripcion}}',
pid: {{$unidad->pu_id}},
valor: {{($unidad->valor > 0) ? $unidad->valor : $unidad->precio($venta->fecha)}}
},
@endforeach
],
@endforeach
}
function changeTipoUnidad(tipo_unidad_id) {
$('#unidad').dropdown('change values', unidades[tipo_unidad_id].sort((a, b) => a.text.padStart(4, '0').localeCompare(b.text.padStart(4, '0'))))
}
function addUnidad() {
$('#add_modal').modal('show')
}
function removeUnidad(unidad_id) {
console.debug(unidad_id)
tipos: {
@foreach($tiposUnidades as $tipoUnidad)
{{$tipoUnidad->id}}: [
@foreach($unidades as $unidad)
@if ($unidad->proyectoTipoUnidad->tipoUnidad->id === $tipoUnidad->id)
{
value: {{$unidad->id}},
text: '{{$unidad->descripcion}}',
name: '{{$unidad->descripcion}}'
},
@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(() => {
$('#add_unidad').click(event => {
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
})
propiedad.setup()
})
</script>
@endpush

View File

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