2021-12-06 22:10:57 -03:00
|
|
|
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()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-30 16:38:17 -04:00
|
|
|
const transacciones = {
|
|
|
|
id: '#transacciones',
|
|
|
|
cuenta_id: 0,
|
|
|
|
cuenta: null,
|
|
|
|
transacciones: [],
|
|
|
|
cuentas: [],
|
|
|
|
saldo: 0,
|
|
|
|
get: function() {
|
|
|
|
return {
|
|
|
|
transacciones: () => {
|
2021-08-01 22:53:02 -04:00
|
|
|
let promises = []
|
2021-12-06 22:10:57 -03:00
|
|
|
sendGet(_urls.api + '/cuenta/' + this.cuenta_id + '/transacciones/amount').then((data) => {
|
|
|
|
if (data.cuenta === null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
)
|
2021-08-01 22:53:02 -04:00
|
|
|
const amount = data.transacciones
|
2021-12-06 22:10:57 -03:00
|
|
|
const step = 50
|
2021-08-01 22:53:02 -04:00
|
|
|
for (let i = 0; i < amount; i += step) {
|
2021-12-06 22:10:57 -03:00
|
|
|
promises.push(
|
|
|
|
sendGet(_urls.api + '/cuenta/' + this.cuenta_id + '/transacciones/' + step + '/' + i)
|
|
|
|
)
|
2021-07-30 16:38:17 -04:00
|
|
|
}
|
2021-12-06 22:10:57 -03:00
|
|
|
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()
|
2021-08-01 22:53:02 -04:00
|
|
|
})
|
2021-12-06 22:10:57 -03:00
|
|
|
} else {
|
2021-08-01 22:53:02 -04:00
|
|
|
this.draw()
|
2021-12-06 22:10:57 -03:00
|
|
|
}
|
2021-07-30 16:38:17 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
cuentas: () => {
|
2021-12-06 22:10:57 -03:00
|
|
|
return sendGet(_urls.api + '/cuentas').then((data) => {
|
|
|
|
if (data.cuentas === null || data.cuentas.length === 0) {
|
2021-07-30 16:38:17 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
this.cuentas = data.cuentas
|
|
|
|
}).then(() => {
|
|
|
|
const select = this.modal.find("[name='cuenta']")
|
|
|
|
$.each(this.cuentas, (i, el) => {
|
|
|
|
select.append(
|
|
|
|
$('<option></option>').attr('value', el.id).html(el.nombre + ' (' + el.categoria.nombre + ')')
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
draw: function() {
|
2021-12-06 22:10:57 -03:00
|
|
|
const format = Intl.NumberFormat('es-CL', {style: 'currency', currency: this.cuenta.moneda.codigo})
|
2021-07-30 16:38:17 -04:00
|
|
|
const parent = $(this.id)
|
|
|
|
parent.html('')
|
|
|
|
$.each(this.transacciones, (i, el) => {
|
2021-12-06 22:10:57 -03:00
|
|
|
parent.append(el.draw({saldo: this.saldo, format: format}))
|
|
|
|
this.saldo = this.saldo + parseInt(el.valor.valor) * ((el.isIncrement()) ? 1 : -1)
|
2021-07-30 16:38:17 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
add: function() {
|
|
|
|
return {
|
|
|
|
show: () => {
|
|
|
|
this.modal.find('form').trigger('reset')
|
|
|
|
this.modal.modal('show')
|
|
|
|
},
|
|
|
|
exec: () => {
|
2021-12-06 22:10:57 -03:00
|
|
|
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)
|
|
|
|
|
2021-07-30 16:38:17 -04:00
|
|
|
const valor = $("[name='valor']").val()
|
|
|
|
const cuenta = $("[name='cuenta']").val()
|
|
|
|
const data = JSON.stringify({
|
2021-12-06 22:10:57 -03:00
|
|
|
debito_id: (valor < 0) ? this.cuenta_id : cuenta,
|
|
|
|
credito_id: (valor < 0) ? cuenta : this.cuenta_id,
|
|
|
|
fecha: fecha,
|
2021-07-30 16:38:17 -04:00
|
|
|
glosa: $("[name='glosa']").val(),
|
|
|
|
detalle: $("[name='detalle']").val(),
|
|
|
|
valor: (valor < 0) ? -valor : valor
|
|
|
|
})
|
2021-12-06 22:10:57 -03:00
|
|
|
return sendPost(_urls.api + '/transacciones/add', data).then(() => {
|
2021-07-30 16:38:17 -04:00
|
|
|
this.modal.modal('hide')
|
|
|
|
this.get().transacciones()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
build: function() {
|
|
|
|
return {
|
|
|
|
modal: () => {
|
|
|
|
this.modal = $('.ui.modal')
|
|
|
|
this.modal.modal()
|
|
|
|
this.modal.find('.close.icon').click(() => {
|
|
|
|
this.modal.modal('hide')
|
|
|
|
})
|
|
|
|
this.modal.find('form').submit((e) => {
|
|
|
|
e.preventDefault()
|
|
|
|
this.add().exec()
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
this.modal.find('.ui.calendar').calendar({
|
|
|
|
type: 'date',
|
|
|
|
formatter: {
|
|
|
|
date: function(date, settings) {
|
|
|
|
if (!date) return ''
|
|
|
|
let day = ('00' + date.getDate()).slice(-2)
|
|
|
|
let month = ('00' + (date.getMonth() + 1)).slice(-2)
|
|
|
|
let year = date.getFullYear()
|
|
|
|
return year + '/' + month + '/' + day
|
|
|
|
}
|
|
|
|
},
|
|
|
|
maxDate: new Date()
|
|
|
|
})
|
|
|
|
this.get().cuentas()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setup: function() {
|
|
|
|
this.build().modal()
|
|
|
|
$(this.id).parent().find('.ui.button').click(() => {
|
|
|
|
this.add().show()
|
|
|
|
})
|
|
|
|
this.get().transacciones()
|
|
|
|
}
|
|
|
|
}
|