Move to add date

This commit is contained in:
Juan Pablo Vial
2025-11-25 11:45:52 -03:00
parent 07a1607554
commit c9056451eb
3 changed files with 215 additions and 57 deletions

View File

@ -33,12 +33,12 @@
<label for="proyecto">Proyecto</label>
<div class="four wide field">
<div class="ui fluid search selection dropdown" id="proyecto">
<input type="hidden" name="proyecto" />
<input type="hidden" name="proyecto" value="{{ $proyecto_id ?? '' }}" />
<i class="dropdown icon"></i>
<div class="default text">Proyecto</div>
<div class="menu">
@foreach($proyectos as $proyecto)
<div class="item" data-value="{{$proyecto->id}}">{{$proyecto->descripcion}}</div>
<div class="item {{ (isset($proyecto_id) && $proyecto_id == $proyecto->id) ? 'active selected' : '' }}" data-value="{{$proyecto->id}}">{{$proyecto->descripcion}}</div>
@endforeach
</div>
</div>
@ -68,7 +68,7 @@
<label for="valor">Valor</label>
<div class="inline field">
<div class="ui right labeled input">
<input type="text" name="valor" id="valor" required />
<input type="text" name="valor" id="valor" value="{{ $valor ?? '' }}" required />
<div class="ui label">UF</div>
</div>
</div>
@ -807,60 +807,105 @@
console.error(errors)
}
$(document).ready(() => {
const cdo = structuredClone(calendar_date_options)
cdo['maxDate'] = new Date()
$('#fecha_venta_calendar').calendar(cdo)
new Propietario({id: '#propietario', id_tipo: 'persona_propietario', id_cantidad: 'cantidad_propietario'})
new Proyecto({unidades_id: '#unidades', proyecto_id: '#proyecto'})
const payments = [
'pie',
'subsidio',
'credito',
'bono_pie'
]
payments.forEach(payment => {
new Payment({
id: '#' + payment,
checkbox_id: 'has_' + payment
})
})
$('#add_form').submit(event => {
event.preventDefault()
const button = $(event.currentTarget).find(".ui.button[type='submit']")
button.prop('disabled', true)
button.css('cursor', 'wait')
const body = new FormData(event.currentTarget)
const unidades = event.currentTarget.querySelectorAll("[name^='unidad']")
const emptyUnidades = []
unidades.forEach(unidad => {
if (unidad.value.trim() === '') {
emptyUnidades.push(unidad)
body.delete(unidad.name)
}
})
if (unidades.length === emptyUnidades.length) {
alert("No hay unidades seleccionadas")
return;
const venta = {
ids: {
form: ''
},
components: {
date: null,
project: null,
buyer: null,
payments: [],
$form: null
},
setup({dateId, buyerId, buyerTypeId, buyerNId, unitsId, projectId, fromReservation = false}) {
this.ids = {
dateId,
buyerId,
buyerTypeId,
buyerNId,
unitsId,
projectId
}
const uri = '{{$urls->api}}/ventas/add'
return fetchAPI(uri, {method: 'post', body}).then(response => {
if (!response) {
return false
}
return response.json().then(data => {
if (data.status) {
window.location = '{{$urls->base}}/venta/' + data.venta_id
return true
this.components.$date = $(dateId)
const cdo = structuredClone(calendar_date_options)
cdo['maxDate'] = new Date()
this.components.$date.calendar(cdo)
this.components.project = new Proyecto({unidades_id: unitsId, proyecto_id: projectId})
this.components.buyer = new Propietario({id: buyerId, id_tipo: buyerTypeId, id_cantidad: buyerNId})
const payments = [
'pie',
'subsidio',
'credito',
'bono_pie'
]
payments.forEach(payment => {
this.components.payments.push(new Payment({
id: '#' + payment,
checkbox_id: 'has_' + payment
}))
})
this.components.$form = $(this.ids.form)
this.components.$form.submit(event => {
event.preventDefault()
const button = $(event.currentTarget).find(".ui.button[type='submit']")
button.prop('disabled', true)
button.css('cursor', 'wait')
const body = new FormData(event.currentTarget)
const unidades = event.currentTarget.querySelectorAll("[name^='unidad']")
const emptyUnidades = []
unidades.forEach(unidad => {
if (unidad.value.trim() === '') {
emptyUnidades.push(unidad)
body.delete(unidad.name)
}
button.prop('disabled', false)
button.css('cursor', 'pointer')
showErrors(data.errors)
return false
})
if (unidades.length === emptyUnidades.length) {
alert("No hay unidades seleccionadas")
return;
}
const uri = '{{$urls->api}}/ventas/add'
return APIClient.fetch(uri, {method: 'post', body}).then(response => {
if (!response) {
return false
}
return response.json().then(data => {
if (data.status) {
window.location = '{{$urls->base}}/venta/' + data.venta_id
return true
}
button.prop('disabled', false)
button.css('cursor', 'pointer')
showErrors(data.errors)
return false
})
})
})
if (fromReservation) {
const date = new Date('{{ $date?->format('Y-m-d') }}')
date.setDate(date.getDate() + 1)
this.components.$date.calendar('set date', date)
$(this.ids.projectId).dropdown('set selection', {{ $proyecto_id }})
}
}
}
$(document).ready(() => {
const fromReservation = '{{ $fromReservation ?? false }}'
venta.setup({
dateId: '#fecha_venta_calendar',
buyerId: '#propietario',
buyerTypeId: 'persona_propietario',
buyerNId: 'cantidad_propietario',
unitsId: '#unidades',
projectId: '#proyecto',
fromReservation: fromReservation === 'true'
})
})
</script>

View File

@ -562,6 +562,60 @@
reservations.components.modals.edit.show({type: 'active', reservation_id: id})
return false
},
promise: event => {
event.preventDefault()
const reservationId = event.currentTarget.dataset.id
const reservation = this.find(reservationId)
// Create a form to submit the data via POST
const form = document.createElement('form')
form.method = 'POST'
form.action = '{{ $urls->base }}/ventas/add'
// Add CSRF token
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content')
const csrfInput = document.createElement('input')
csrfInput.type = 'hidden'
csrfInput.name = '_token'
csrfInput.value = csrfToken
form.appendChild(csrfInput)
// Add reservation data
const addInput = (name, value) => {
if (value) {
const input = document.createElement('input')
input.type = 'hidden'
input.name = name
input.value = value
form.appendChild(input)
}
}
addInput('from_reservation', 'true')
addInput('reservation_id', reservationId)
if (reservation.cliente) {
addInput('cliente_nombre', reservation.cliente.nombre)
addInput('cliente_rut', reservation.cliente.rut)
addInput('cliente_email', reservation.cliente.email)
addInput('cliente_telefono', reservation.cliente.telefono)
}
if (reservation.propiedad) {
addInput('proyecto_id', reservation.propiedad.proyecto_id)
// Add other property-related parameters as needed
}
if (reservation.oferta) {
addInput('valor', reservation.oferta.valor)
// Add other offer-related parameters as needed
}
// Submit the form
document.body.appendChild(form)
form.submit()
return false
},
remove: event => {
event.preventDefault()
const reservation_id = event.currentTarget.dataset.id