UI
This commit is contained in:
@ -1,3 +1,85 @@
|
||||
class Transaccion {
|
||||
constructor({id, debito_id, credito_id, fecha, glosa, detalle, valor, debito, credito, fechaFormateada, valorFormateado}) {
|
||||
this.id = id
|
||||
this.debito_id = debito_id
|
||||
this.credito_id = credito_id
|
||||
this.fecha = {
|
||||
fecha,
|
||||
formateada: fechaFormateada
|
||||
}
|
||||
this.glosa = glosa
|
||||
this.detalle = detalle
|
||||
this.valor = {
|
||||
valor,
|
||||
formateado: valorFormateado
|
||||
}
|
||||
this.debito = debito
|
||||
this.credito = credito
|
||||
this.modal = null
|
||||
}
|
||||
setCuenta(cuenta) {
|
||||
this.cuenta = cuenta
|
||||
}
|
||||
setModal(modal) {
|
||||
this.modal = modal
|
||||
}
|
||||
isDebito() {
|
||||
return this.debito.id === this.cuenta.id;
|
||||
}
|
||||
isIncrement() {
|
||||
const debits = ['Activo', 'Perdida']
|
||||
if (debits.indexOf(this.cuenta.tipo.descripcion)) {
|
||||
return this.isDebito()
|
||||
}
|
||||
return !this.isDebito()
|
||||
}
|
||||
draw({saldo, format}) {
|
||||
const fuente = (this.isDebito()) ? this.credito : this.debito
|
||||
return $('<tr></tr>').append(
|
||||
$('<td></td>').html(this.fecha.formateada)
|
||||
).append(
|
||||
$('<td></td>').append(
|
||||
$('<a></a>').attr('href', _urls.base + 'cuenta/' + fuente.id).html(fuente.nombre + ' (' + fuente.categoria.nombre + ')')
|
||||
)
|
||||
).append(
|
||||
$('<td></td>').html(this.glosa + '<br />' + this.detalle)
|
||||
).append(
|
||||
$('<td></td>').attr('class', 'right aligned').html((this.isIncrement()) ? '' : format.format(this.valor.valor))
|
||||
).append(
|
||||
$('<td></td>').attr('class', 'right aligned').html((this.isIncrement()) ? format.format(this.valor.valor) : '')
|
||||
).append(
|
||||
$('<td></td>').attr('class', 'right aligned').html(format.format(saldo))
|
||||
).append(
|
||||
$('<td></td>').attr('class', 'right aligned')/*.append(
|
||||
$('<button></button>').attr('class', 'ui tiny circular icon button').append(
|
||||
$('<i></i>').attr('class', 'edit icon')
|
||||
).click((e) => {
|
||||
e.preventDefault()
|
||||
this.edit()
|
||||
return false
|
||||
})
|
||||
)*/.append(
|
||||
$('<button></button>').attr('class', 'ui tiny circular red icon button').append(
|
||||
$('<i></i>').attr('class', 'remove icon')
|
||||
).click((e) => {
|
||||
e.preventDefault()
|
||||
this.remove()
|
||||
return false
|
||||
})
|
||||
)
|
||||
)
|
||||
}
|
||||
edit() {
|
||||
const form = this.modal.find('form')
|
||||
form.find("[name='fecha']")
|
||||
}
|
||||
remove() {
|
||||
sendDelete(_urls.api + '/transaccion/' + this.id + '/delete').then(() => {
|
||||
transacciones.get().transacciones()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const transacciones = {
|
||||
id: '#transacciones',
|
||||
cuenta_id: 0,
|
||||
@ -9,49 +91,50 @@ const transacciones = {
|
||||
return {
|
||||
transacciones: () => {
|
||||
let promises = []
|
||||
$.ajax({
|
||||
url: _urls.api + '/cuenta/' + this.cuenta_id + '/transacciones/amount',
|
||||
method: 'GET',
|
||||
dataType: 'json'
|
||||
}).then((data) => {
|
||||
const amount = data.transacciones
|
||||
const step = 100
|
||||
for (let i = 0; i < amount; i += step) {
|
||||
promises.push($.ajax({
|
||||
url: _urls.api + '/cuenta/' + this.cuenta_id + '/transacciones/' + step + '/' + i,
|
||||
method: 'GET',
|
||||
dataType: 'json'
|
||||
}))
|
||||
sendGet(_urls.api + '/cuenta/' + this.cuenta_id + '/transacciones/amount').then((data) => {
|
||||
if (data.cuenta === null) {
|
||||
return
|
||||
}
|
||||
Promise.all(promises).then((data_arr) => {
|
||||
if (data_arr[0].cuenta === null) {
|
||||
return
|
||||
}
|
||||
this.cuenta = data_arr[0].cuenta
|
||||
this.saldo = this.cuenta.saldo
|
||||
$('#cuenta').html(this.cuenta.nombre + ' (' + this.cuenta.categoria.nombre + ')')
|
||||
this.transacciones = []
|
||||
data_arr.forEach(data => {
|
||||
if (data.transacciones === null || data.transacciones.length == 0) {
|
||||
return
|
||||
}
|
||||
this.transacciones.push(...data.transacciones)
|
||||
this.cuenta = data.cuenta
|
||||
this.saldo = this.cuenta.saldo
|
||||
$('#cuenta').html(this.cuenta.nombre + ' (' + this.cuenta.categoria.nombre + ')').append(
|
||||
$('<i></i>').attr('class', 'square full icon').css('color', '#' + this.cuenta.tipo.color)
|
||||
)
|
||||
const amount = data.transacciones
|
||||
const step = 50
|
||||
for (let i = 0; i < amount; i += step) {
|
||||
promises.push(
|
||||
sendGet(_urls.api + '/cuenta/' + this.cuenta_id + '/transacciones/' + step + '/' + i)
|
||||
)
|
||||
}
|
||||
if (promises.length > 0) {
|
||||
Promise.all(promises).then((data_arr) => {
|
||||
this.transacciones = []
|
||||
data_arr.forEach(data => {
|
||||
if (data.transacciones === null || data.transacciones.length === 0) {
|
||||
return
|
||||
}
|
||||
$.each(data.transacciones, (i, el) => {
|
||||
const tr = new Transaccion(el)
|
||||
tr.setCuenta(this.cuenta)
|
||||
tr.setModal(this.modal)
|
||||
this.transacciones.push(tr)
|
||||
})
|
||||
})
|
||||
this.transacciones.sort((a, b) => {
|
||||
return (new Date(b.fecha)) - (new Date(a.fecha))
|
||||
})
|
||||
}).then(() => {
|
||||
this.draw()
|
||||
})
|
||||
this.transacciones.sort((a, b) => {
|
||||
return (new Date(b.fecha)) - (new Date(a.fecha))
|
||||
})
|
||||
}).then(() => {
|
||||
} else {
|
||||
this.draw()
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
cuentas: () => {
|
||||
return $.ajax({
|
||||
url: _urls.api + '/cuentas',
|
||||
method: 'GET',
|
||||
dataType: 'json'
|
||||
}).then((data) => {
|
||||
if (data.cuentas === null || data.cuentas.length == 0) {
|
||||
return sendGet(_urls.api + '/cuentas').then((data) => {
|
||||
if (data.cuentas === null || data.cuentas.length === 0) {
|
||||
return
|
||||
}
|
||||
this.cuentas = data.cuentas
|
||||
@ -67,29 +150,12 @@ const transacciones = {
|
||||
}
|
||||
},
|
||||
draw: function() {
|
||||
const format = Intl.NumberFormat('es-CL', {style: 'currency', currency: 'CLP'})
|
||||
const format = Intl.NumberFormat('es-CL', {style: 'currency', currency: this.cuenta.moneda.codigo})
|
||||
const parent = $(this.id)
|
||||
parent.html('')
|
||||
$.each(this.transacciones, (i, el) => {
|
||||
const fuente = (el.valor < 0) ? el.hasta : el.desde
|
||||
parent.append(
|
||||
$('<tr></tr>').append(
|
||||
$('<td></td>').html(el.fechaFormateada)
|
||||
).append(
|
||||
$('<td></td>').append(
|
||||
$('<a></a>').attr('href', _urls.base + 'cuenta/' + fuente.id).html(fuente.nombre + ' (' + fuente.categoria.nombre + ')')
|
||||
)
|
||||
).append(
|
||||
$('<td></td>').html(el.glosa + '<br />' + el.detalle)
|
||||
).append(
|
||||
$('<td></td>').attr('class', 'right aligned').html((el.valor < 0) ? '' : el.valorFormateado.replace('-', ''))
|
||||
).append(
|
||||
$('<td></td>').attr('class', 'right aligned').html((el.valor < 0) ? el.valorFormateado.replace('-', '') : '')
|
||||
).append(
|
||||
$('<td></td>').attr('class', 'right aligned').html(format.format(this.saldo))
|
||||
)
|
||||
)
|
||||
this.saldo -= parseInt(el.valor)
|
||||
parent.append(el.draw({saldo: this.saldo, format: format}))
|
||||
this.saldo = this.saldo + parseInt(el.valor.valor) * ((el.isIncrement()) ? 1 : -1)
|
||||
})
|
||||
},
|
||||
add: function() {
|
||||
@ -99,22 +165,26 @@ const transacciones = {
|
||||
this.modal.modal('show')
|
||||
},
|
||||
exec: () => {
|
||||
const fecha = $("[name='fecha']").val()
|
||||
const data1 = JSON.stringify({
|
||||
desde_id: $("[name='moneda']").val(),
|
||||
hasta_id: 1,
|
||||
fecha: fecha,
|
||||
valor: $("[name='cambio']").val()
|
||||
})
|
||||
sendPut(_urls.api + '/tipos/cambios/add', data1)
|
||||
|
||||
const valor = $("[name='valor']").val()
|
||||
const cuenta = $("[name='cuenta']").val()
|
||||
const data = JSON.stringify({
|
||||
desde_id: (valor < 0) ? this.cuenta_id : cuenta,
|
||||
hasta_id: (valor < 0) ? cuenta : this.cuenta_id,
|
||||
fecha: $("[name='fecha']").val(),
|
||||
debito_id: (valor < 0) ? this.cuenta_id : cuenta,
|
||||
credito_id: (valor < 0) ? cuenta : this.cuenta_id,
|
||||
fecha: fecha,
|
||||
glosa: $("[name='glosa']").val(),
|
||||
detalle: $("[name='detalle']").val(),
|
||||
valor: (valor < 0) ? -valor : valor
|
||||
})
|
||||
return $.ajax({
|
||||
url: _urls.api + '/transacciones/add',
|
||||
method: 'POST',
|
||||
data: data,
|
||||
dataType: 'json'
|
||||
}).then(() => {
|
||||
return sendPost(_urls.api + '/transacciones/add', data).then(() => {
|
||||
this.modal.modal('hide')
|
||||
this.get().transacciones()
|
||||
})
|
||||
|
Reference in New Issue
Block a user