FIX: comentario en redirect y cleanup
This commit is contained in:
@ -86,22 +86,11 @@ Editar Propietario
|
||||
}
|
||||
}
|
||||
|
||||
digito() {
|
||||
const cleanRut = this.props.rut.replace(/\D/g, ''); // Removes non-digit characters more efficiently
|
||||
let sum = 0;
|
||||
const factors = [2, 3, 4, 5, 6, 7, 2, 3, 4, 5];
|
||||
for (let i = 0; i < cleanRut.length; i++) {
|
||||
const digit = parseInt(cleanRut[cleanRut.length - 1 - i], 10);
|
||||
sum += digit * factors[i % factors.length];
|
||||
}
|
||||
const dv = 11 - (sum % 11);
|
||||
return dv === 10 ? 'K' : dv === 11 ? '0' : dv.toString();
|
||||
}
|
||||
update() {
|
||||
return {
|
||||
rut: rut => {
|
||||
this.props.rut = rut
|
||||
this.update().digito(this.digito())
|
||||
this.update().digito(Rut.digito(this.props.rut))
|
||||
},
|
||||
digito: digito => {
|
||||
this.props.digito = digito
|
||||
@ -169,12 +158,22 @@ Editar Propietario
|
||||
return
|
||||
}
|
||||
if (key === 'direccion') {
|
||||
let changed = false
|
||||
Object.entries(value).forEach(([k, v]) => {
|
||||
const val = this.props[key][k]
|
||||
if (collator.compare(val, v) !== 0) {
|
||||
changed = true
|
||||
data[k] = val
|
||||
}
|
||||
})
|
||||
if (changed) {
|
||||
Object.entries(value).forEach(([k, v]) => {
|
||||
if (k in data) {
|
||||
return
|
||||
}
|
||||
data[k] = this.props[key][k]
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
const val = this.props[key]
|
||||
@ -248,30 +247,145 @@ Editar Propietario
|
||||
}
|
||||
}
|
||||
}
|
||||
class Form {
|
||||
static submit(event) {
|
||||
event.preventDefault()
|
||||
propietario.props.data.edit()
|
||||
return false
|
||||
}
|
||||
}
|
||||
class Watcher {
|
||||
static rut() {
|
||||
const rut = $("[name='rut']")
|
||||
rut.on('input', EventHandler.rut().update)
|
||||
rut.on('blur', EventHandler.rut().change)
|
||||
}
|
||||
static nombres() {
|
||||
$("[name='nombres']").change(event => {
|
||||
propietario.props.data.update().nombres($(event.currentTarget).val())
|
||||
})
|
||||
}
|
||||
static apellidos() {
|
||||
const watched = [
|
||||
'apellido_paterno',
|
||||
'apellido_materno'
|
||||
]
|
||||
watched.forEach(name => {
|
||||
$("[name='" + name + "']").change(event => {
|
||||
const value = $(event.currentTarget).val()
|
||||
const key = name.split('_')[1]
|
||||
propietario.props.data.update().apellidos()[key](value)
|
||||
})
|
||||
})
|
||||
}
|
||||
static direccion() {
|
||||
const watched = [
|
||||
'calle',
|
||||
'numero',
|
||||
'extra'
|
||||
]
|
||||
watched.forEach(name => {
|
||||
$("[name='" + name + "']").change(EventHandler.direccion)
|
||||
})
|
||||
}
|
||||
static region() {
|
||||
$('#region').change(EventHandler.region)
|
||||
}
|
||||
static comuna() {
|
||||
$('#comunas').change(event => {
|
||||
const comuna_id = $(event.currentTarget).val()
|
||||
propietario.props.data.update().direccion().comuna(comuna_id)
|
||||
})
|
||||
}
|
||||
|
||||
static run() {
|
||||
Watcher.rut()
|
||||
Watcher.nombres()
|
||||
Watcher.apellidos()
|
||||
Watcher.direccion()
|
||||
Watcher.region()
|
||||
Watcher.comuna()
|
||||
}
|
||||
}
|
||||
class EventHandler {
|
||||
static region(event) {
|
||||
const region_id = $(event.currentTarget).val()
|
||||
propietario.props.regiones.forEach(region => {
|
||||
region.props.selected = false
|
||||
})
|
||||
const region = propietario.props.regiones.find(region => region.props.id === region_id)
|
||||
region.props.selected = true
|
||||
region.get().comunas().then(() => {
|
||||
propietario.draw().comunas()
|
||||
})
|
||||
}
|
||||
static direccion(event) {
|
||||
const names = [
|
||||
'calle',
|
||||
'numero',
|
||||
'extra'
|
||||
]
|
||||
const originals = [
|
||||
'{{trim($propietario->datos->direccion->calle)}}',
|
||||
'{{trim($propietario->datos->direccion->numero)}}',
|
||||
'{{trim($propietario->datos->direccion->extra)}}'
|
||||
]
|
||||
const values = []
|
||||
names.forEach(name => {
|
||||
const val = $("[name='" + name + "']").val()
|
||||
propietario.props.data.update().direccion()[name](val)
|
||||
values.push(val)
|
||||
})
|
||||
const collator = new Intl.Collator('es')
|
||||
if (collator.compare(originals.join(' '), values.join(' ')) !== 0) {
|
||||
propietario.find().comuna(values.join(' ').trim())
|
||||
}
|
||||
}
|
||||
static rut() {
|
||||
return {
|
||||
change: event => {
|
||||
const rut = $("[name='rut']").val().replace(/\D/g, '')
|
||||
if (rut.length < 7) {
|
||||
return
|
||||
}
|
||||
propietario.find().propietario(rut)
|
||||
},
|
||||
update: event => {
|
||||
const input = $("[name='rut']")
|
||||
const rut = input.val().replace(/\D/g, '')
|
||||
propietario.props.data.update().rut(rut)
|
||||
input.val(Rut.format(rut))
|
||||
$('#digito').html(propietario.props.data.props.digito)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
class Rut {
|
||||
static format(rut) {
|
||||
return Intl.NumberFormat('es-CL', {maximumFractionDigits: 0}).format(rut)
|
||||
}
|
||||
static digito(rut) {
|
||||
const cleanRut = rut.replace(/\D/g, ''); // Removes non-digit characters more efficiently
|
||||
let sum = 0;
|
||||
const factors = [2, 3, 4, 5, 6, 7, 2, 3, 4, 5];
|
||||
for (let i = 0; i < cleanRut.length; i++) {
|
||||
const digit = parseInt(cleanRut[cleanRut.length - 1 - i], 10);
|
||||
sum += digit * factors[i % factors.length];
|
||||
}
|
||||
const dv = 11 - (sum % 11);
|
||||
return dv === 10 ? 'K' : dv === 11 ? '0' : dv.toString();
|
||||
}
|
||||
}
|
||||
const propietario = {
|
||||
props: {
|
||||
ids: {},
|
||||
data: {},
|
||||
regiones: []
|
||||
},
|
||||
rut() {
|
||||
return {
|
||||
format: rut => {
|
||||
return Intl.NumberFormat('es-CL', {maximumFractionDigits: 0}).format(rut)
|
||||
},
|
||||
update: () => {
|
||||
const input = $("[name='rut']")
|
||||
const rut = input.val().replace(/\D/g, '')
|
||||
this.props.data.update().rut(rut)
|
||||
input.val(this.rut().format(rut))
|
||||
$('#digito').html(this.props.data.props.digito)
|
||||
}
|
||||
}
|
||||
regiones: [],
|
||||
},
|
||||
update() {
|
||||
return {
|
||||
propietario: () => {
|
||||
$("[name='rut']").val(this.rut().format(this.props.data.props.rut))
|
||||
$("[name='rut']").val(Rut.format(this.props.data.props.rut))
|
||||
$('#digito').html(this.props.data.props.digito)
|
||||
$("[name='nombres']").val(this.props.data.props.nombres)
|
||||
$("[name='apellido_paterno']").val(this.props.data.props.apellidos.paterno)
|
||||
@ -282,13 +396,6 @@ Editar Propietario
|
||||
$('#region').val(this.props.data.props.direccion.region)
|
||||
this.update().region()
|
||||
},
|
||||
rut: () => {
|
||||
const input = $("[name='rut']")
|
||||
const rut = input.val().replace(/\D/g, '')
|
||||
this.props.data.update().rut(rut)
|
||||
input.val(this.rut().format(rut))
|
||||
$('#digito').html(this.props.data.props.digito)
|
||||
},
|
||||
region: () => {
|
||||
this.props.regiones.forEach(region => {
|
||||
region.props.selected = false
|
||||
@ -306,46 +413,6 @@ Editar Propietario
|
||||
}
|
||||
}
|
||||
},
|
||||
change() {
|
||||
return {
|
||||
rut: () => {
|
||||
const rut = $("[name='rut']").val().replace(/\D/g, '')
|
||||
this.find().propietario(rut)
|
||||
},
|
||||
direccion: () => {
|
||||
const names = [
|
||||
'calle',
|
||||
'numero',
|
||||
'extra'
|
||||
]
|
||||
const originals = [
|
||||
'{{trim($propietario->datos->direccion->calle)}}',
|
||||
'{{trim($propietario->datos->direccion->numero)}}',
|
||||
'{{trim($propietario->datos->direccion->extra)}}'
|
||||
]
|
||||
const values = []
|
||||
names.forEach(name => {
|
||||
const val = $("[name='" + name + "']").val()
|
||||
values.push(val)
|
||||
})
|
||||
const collator = new Intl.Collator('es')
|
||||
if (collator.compare(originals.join(' '), values.join(' ')) !== 0) {
|
||||
this.find.comuna(values.join(' ').trim())
|
||||
}
|
||||
},
|
||||
region: event => {
|
||||
const region_id = $(event.currentTarget).val()
|
||||
propietario.props.regiones.forEach(region => {
|
||||
region.props.selected = false
|
||||
})
|
||||
const region = propietario.props.regiones.find(region => region.props.id === region_id)
|
||||
region.props.selected = true
|
||||
region.get().comunas().then(() => {
|
||||
propietario.draw().comunas()
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
find() {
|
||||
return {
|
||||
propietario: rut => {
|
||||
@ -360,8 +427,6 @@ Editar Propietario
|
||||
this.draw().reset()
|
||||
return
|
||||
}
|
||||
/*this.props.data.update().rut(data.propietario.rut)
|
||||
this.props.data.update().digito(data.propietario.dv)*/
|
||||
this.props.data.update().nombres(data.propietario.nombres)
|
||||
this.props.data.update().apellidos().paterno(data.propietario.apellidos.paterno)
|
||||
this.props.data.update().apellidos().materno(data.propietario.apellidos.materno)
|
||||
@ -427,53 +492,15 @@ Editar Propietario
|
||||
}
|
||||
}
|
||||
},
|
||||
form() {
|
||||
return {
|
||||
submit: event => {
|
||||
event.preventDefault()
|
||||
propietario.props.data.edit()
|
||||
return false
|
||||
}
|
||||
}
|
||||
},
|
||||
watch() {
|
||||
return {
|
||||
rut: () => {
|
||||
const rut = $("[name='rut']")
|
||||
rut.on('input', event => {
|
||||
propietario.rut().update()
|
||||
})
|
||||
rut.on('blur', event => {
|
||||
if (rut.val().length < 9) {
|
||||
return
|
||||
}
|
||||
propietario.change().rut()
|
||||
})
|
||||
},
|
||||
direccion: () => {
|
||||
const watched = [
|
||||
'calle',
|
||||
'numero',
|
||||
'extra'
|
||||
]
|
||||
watched.forEach(name => {
|
||||
$("[name='" + name + "']").change(event => {
|
||||
propietario.change().direccion()
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
redirect() {
|
||||
console.debug('Redirecting')
|
||||
//window.location = '{{$urls->base}}/venta/{{$venta_id}}'
|
||||
window.location = '{{$urls->base}}/venta/{{$venta_id}}'
|
||||
},
|
||||
setup({ids, propietario}) {
|
||||
this.props.ids = ids
|
||||
this.props.data = propietario
|
||||
|
||||
$(this.props.ids.region).dropdown()
|
||||
$(this.props.ids.region).change(this.change().region)
|
||||
$(this.props.ids.region).change(EventHandler.region)
|
||||
|
||||
$(this.props.ids.comuna).hide()
|
||||
$(this.props.ids.region).find('option').each((index, element) => {
|
||||
@ -484,9 +511,8 @@ Editar Propietario
|
||||
})
|
||||
this.update().region()
|
||||
|
||||
$(this.props.ids.forms.edit).submit(this.form().submit)
|
||||
this.watch().direccion()
|
||||
this.watch().rut()
|
||||
$(this.props.ids.forms.edit).submit(Form.submit)
|
||||
Watcher.run()
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user