Simplificacion a contabilidad clasica
This commit is contained in:
@ -1,136 +1,127 @@
|
||||
const fuentes = {
|
||||
id: '#fuentes',
|
||||
fuentes: [],
|
||||
tipos: [],
|
||||
bancos: [],
|
||||
modal: null,
|
||||
getParent: function() {
|
||||
let parent = $(this.id)
|
||||
if (parent.length === 0) {
|
||||
const table = $('<table></table>').attr('class', 'ui table').append(
|
||||
$('<thead></thead>').append(
|
||||
$('<tr></tr>').append(
|
||||
$('<th></th>').html('Fuente')
|
||||
).append(
|
||||
$('<th></th>').html('Saldo')
|
||||
).append(
|
||||
$('<th></th>').attr('class', 'right aligned').append(
|
||||
$('<button></button>').attr('class', 'ui tiny green circular icon button').append(
|
||||
$('<i></i>').attr('class', 'plus icon')
|
||||
).click(() => {
|
||||
this.add()
|
||||
})
|
||||
const cuentas = {
|
||||
id: '#cuentas',
|
||||
categorias: [],
|
||||
get: function() {
|
||||
return {
|
||||
parent: () => {
|
||||
let parent = $(this.id)
|
||||
if (parent.length === 0) {
|
||||
const table = $('<table></table>').attr('class', 'ui table').append(
|
||||
$('<thead></thead>').append(
|
||||
$('<tr></tr>').append(
|
||||
$('<th></th>').html('Cuenta')
|
||||
).append(
|
||||
$('<th></th>').attr('class', 'right aligned').html('Saldo')
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
parent = $('<tbody></tbody>').attr('id', this.id)
|
||||
table.append(parent)
|
||||
$('h1.header').after(table)
|
||||
parent = $('<tbody></tbody>').attr('id', this.id)
|
||||
table.append(parent)
|
||||
$('h1.header').after(table)
|
||||
}
|
||||
return parent
|
||||
},
|
||||
categorias: () => {
|
||||
return $.ajax({
|
||||
url: _urls.api + '/categorias',
|
||||
method: 'GET',
|
||||
dataType: 'json'
|
||||
}).then((data) => {
|
||||
if (data.categorias === null || data.categorias.length == 0) {
|
||||
return
|
||||
}
|
||||
this.categorias = data.categorias
|
||||
}).then(() => {
|
||||
this.draw().categorias()
|
||||
})
|
||||
},
|
||||
cuentas: (categoria_id) => {
|
||||
return $.ajax({
|
||||
url: _urls.api + '/categoria/' + categoria_id + '/cuentas',
|
||||
method: 'GET',
|
||||
dataType: 'json'
|
||||
}).then((data) => {
|
||||
if (data.cuentas === null || data.cuentas.length == 0) {
|
||||
return
|
||||
}
|
||||
const idx = this.categorias.findIndex(el => {
|
||||
if (el.id == categoria_id) {
|
||||
return true
|
||||
}
|
||||
})
|
||||
this.categorias[idx].cuentas = data.cuentas
|
||||
}).then(() => {
|
||||
this.draw().cuentas(categoria_id)
|
||||
})
|
||||
}
|
||||
}
|
||||
return parent
|
||||
},
|
||||
setup: async function() {
|
||||
this.modal = $('.ui.modal')
|
||||
this.modal.modal()
|
||||
this.modal.find('.close.icon').css('cursor', 'pointer').click(() => {
|
||||
this.modal.modal('hide')
|
||||
})
|
||||
this.getFuentes().then(() => {
|
||||
this.getTipos().then(() => {
|
||||
this.getBancos()
|
||||
})
|
||||
})
|
||||
},
|
||||
add: function() {
|
||||
this.modal.find('form').trigger('reset')
|
||||
this.modal.find('form').submit((e) => {
|
||||
e.preventDefault()
|
||||
this.doAdd()
|
||||
return false
|
||||
})
|
||||
this.modal.modal('show')
|
||||
},
|
||||
doAdd: function() {
|
||||
const data = JSON.stringify({
|
||||
tipo_id: $("select[name='tipo']").val(),
|
||||
banco_id: $("select[name='banco']").val()
|
||||
})
|
||||
$.ajax({
|
||||
url: _urls.api + '/fuentes/add',
|
||||
method: 'POST',
|
||||
data: data,
|
||||
dataType: 'json'
|
||||
}).then((data) => {
|
||||
this.modal.modal('hide')
|
||||
this.getFuentes()
|
||||
})
|
||||
},
|
||||
getFuentes: function() {
|
||||
return $.ajax({
|
||||
url: _urls.api + '/fuentes',
|
||||
method: 'GET',
|
||||
dataType: 'json'
|
||||
}).then((data) => {
|
||||
if (data.fuentes === null || data.fuentes.length == 0) {
|
||||
return
|
||||
remove: function() {
|
||||
return {
|
||||
cuentas: (categoria_id) => {
|
||||
const idx = this.categorias.findIndex(el => {
|
||||
if (el.id == categoria_id) {
|
||||
return true
|
||||
}
|
||||
})
|
||||
const parent = $("[data-id='" + categoria_id + "']")
|
||||
for (let i = 0; i < this.categorias[idx].cuentas.length; i ++) {
|
||||
parent.next().remove()
|
||||
}
|
||||
}
|
||||
this.fuentes = data.fuentes
|
||||
}).then(() => {
|
||||
this.draw()
|
||||
})
|
||||
},
|
||||
getTipos: function() {
|
||||
return $.ajax({
|
||||
url: _urls.api + '/tipos_fuentes',
|
||||
method: 'GET',
|
||||
dataType: 'json'
|
||||
}).then((data) => {
|
||||
if (data.tipos_fuentes === null || data.tipos_fuentes.length == 0) {
|
||||
return
|
||||
}
|
||||
this.tipos = data.tipos_fuentes
|
||||
}).then(() => {
|
||||
const select = $("select[name='tipo']")
|
||||
select.html('')
|
||||
$.each(this.tipos, (i, el) => {
|
||||
select.append(
|
||||
$('<option></option>').attr('value', el.id).html(el.descripcion)
|
||||
)
|
||||
})
|
||||
select.dropdown()
|
||||
})
|
||||
},
|
||||
getBancos: function() {
|
||||
return $.ajax({
|
||||
url: _urls.api + '/bancos',
|
||||
method: 'GET',
|
||||
dataType: 'json'
|
||||
}).then((data) => {
|
||||
if (data.bancos === null || data.bancos.length == 0) {
|
||||
return
|
||||
}
|
||||
this.bancos = data.bancos
|
||||
}).then(() => {
|
||||
const select = $("select[name='banco']")
|
||||
$.each(this.bancos, (i, el) => {
|
||||
select.append(
|
||||
$('<option></option>').attr('value', el.id).html(el.nombre)
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
},
|
||||
draw: function() {
|
||||
const parent = this.getParent()
|
||||
$.each(this.fuentes, (i, el) => {
|
||||
const f = $('<tr></tr>').append(
|
||||
$('<td></td>').append(
|
||||
$('<a></a>').attr('href', _urls.base + 'fuente/' + el.id).html(el.tipo.descripcion + ' - ' + el.banco.nombre)
|
||||
)
|
||||
).append(
|
||||
$('<td></td>').html(el.saldoFormateado)
|
||||
)
|
||||
parent.append(f)
|
||||
})
|
||||
return {
|
||||
categorias: () => {
|
||||
const parent = this.get().parent()
|
||||
$.each(this.categorias, (i, el) => {
|
||||
const button = $('<button></button>').attr('class', 'ui mini compact icon button').append(
|
||||
$('<i></i>').attr('class', 'plus icon')
|
||||
).click((e) => {
|
||||
const plus = button.find('.plus')
|
||||
if (plus.length == 0) {
|
||||
console.debug(e.target)
|
||||
this.remove().cuentas(el.id)
|
||||
button.find('i.icon').removeClass('minus').addClass('plus')
|
||||
} else {
|
||||
console.debug(e.target)
|
||||
this.get().cuentas(el.id)
|
||||
button.find('i.icon').removeClass('plus').addClass('minus')
|
||||
}
|
||||
})
|
||||
const f = $('<tr></tr>').attr('data-id', el.id).append(
|
||||
$('<td></td>').append(
|
||||
$('<div></div>').append(button).append(el.nombre)
|
||||
)
|
||||
).append(
|
||||
$('<td></td>').attr('class', 'right aligned').html(el.saldoFormateado)
|
||||
)
|
||||
parent.append(f)
|
||||
})
|
||||
},
|
||||
cuentas: (categoria_id) => {
|
||||
const idx = this.categorias.findIndex(el => {
|
||||
if (el.id == categoria_id) {
|
||||
return true
|
||||
}
|
||||
})
|
||||
const parent = $("[data-id='" + categoria_id + "']")
|
||||
$.each(this.categorias[idx].cuentas, (i, el) => {
|
||||
parent.after(
|
||||
$('<tr></tr>').attr('class', 'item').append(
|
||||
$('<td></td>').append(
|
||||
$('<a></a>').attr('href', _urls.base + 'cuenta/' + el.id).html(el.nombre)
|
||||
)
|
||||
).append(
|
||||
$('<td></td>').attr('class', 'right aligned').html(el.saldoFormateado)
|
||||
)
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
setup: async function() {
|
||||
this.get().categorias()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user