FIX: Importar cartolas correctamente.

This commit is contained in:
Juan Pablo Vial
2024-07-18 21:25:10 -04:00
parent a471a1083e
commit b2f1bd5ba0
4 changed files with 101 additions and 47 deletions

View File

@ -30,6 +30,7 @@
<table class="ui celled table" id="movimientos" style="display: none;">
<thead>
<tr>
<th>Sigla</th>
<th>Fecha</th>
<th>Glosa</th>
<th class="right aligned">Cargo</th>
@ -65,21 +66,25 @@
get banco() {
return $(`#${this.ids.banco}${this.props.index}`).dropdown('get value')
}
get mes() {
return $(`#${this.ids.mes}${this.props.index}`).calendar('get date')
}
get file() {
return $(`#archivo${this.props.index}`)[0].files[0]
}
get data() {
return {
sociedad_rut: this.sociedad,
banco_id: this.banco,
cuenta_id: this.banco,
mes: [this.mes.getFullYear(), this.mes.getMonth() + 1, 1].join('-'),
file: this.file,
index: this.props.index
}
}
draw({sociedades, bancos}) {
draw({sociedades}) {
const output = [
`<div class="fields" data-idx="${this.props.index}">`,
'<div class="six wide field">',
'<div class="five wide field">',
'<label>Sociedad</label>',
`<div class="ui search selection dropdown" id="${this.ids.sociedad}${this.props.index}">`,
`<input type="hidden" name="sociedad_rut${this.props.index}" />`,
@ -94,30 +99,30 @@
'</div>',
'</div>',
'</div>',
'<div class="two wide field">',
'<div class="four wide field">',
'<label>Banco</label>',
`<div class="ui search selection dropdown" id="${this.ids.banco}${this.props.index}">`,
`<input type="hidden" name="banco_id${this.props.index}" />`,
'<i class="dropdown icon"></i>',
'<div class="default text">Banco</div>',
'<div class="menu">',
...bancos.map(banco => {
return [
`<div class="item" data-value="${banco.id}">${banco.nombre}</div>`
].join("\n")
}),
'<div class="menu"></div>',
'</div>',
'</div>',
'<div class="field">',
'<label>Mes</label>',
`<div class="ui calendar" id="${this.ids.mes}${this.props.index}">`,
'<div class="ui input left icon">',
'<i class="calendar icon"></i>',
`<input type="text" name="mes${this.props.index}" placeholder="Mes" />`,
'</div>',
'</div>',
'</div>',
'<div class="six wide field">',
'<div class="field">',
'<label>Archivo</label>',
'<div class="ui file action input">',
`<input type="file" name="archivo${this.props.index}" id="archivo${this.props.index}" />`,
'<div class="ui icon button" id="archivo_button">',
'<i class="file icon"></i>',
'Seleccionar',
'</div>',
'</div>',
`<input type="file" class="ui invisible file input" name="archivo${this.props.index}" id="archivo${this.props.index}" />`,
`<label for="archivo${this.props.index}" class="ui icon button" id="archivo_button">`,
'<i class="file icon"></i>',
'</label>',
'</div>',
]
if (this.props.index > 1) {
@ -134,8 +139,31 @@
return output.join("\n")
}
activate() {
$(`#${this.ids.sociedad}${this.props.index}`).dropdown()
$(`#${this.ids.sociedad}${this.props.index}`).dropdown({
onChange: (value, text, $choice) => {
cartolas.fetch().bancos(value).then(response => {
if (!response) {
return
}
return response.json().then(data => {
const dropdown = $(`#${this.ids.banco}${this.props.index}`)
dropdown.dropdown('clear')
dropdown.dropdown('change values', data.cuentas.map(cuenta => {
const desc = [cuenta.banco.nombre, cuenta.cuenta].join(' - ')
return {
name: desc,
value: cuenta.id,
text: desc
}
}))
})
})
}
})
$(`#${this.ids.banco}${this.props.index}`).dropdown()
const cdo = structuredClone(calendar_date_options)
cdo.type = 'month'
$(`#${this.ids.mes}${this.props.index}`).calendar(cdo)
if (this.props.index > 1) {
$(`.${this.ids.buttons.remove}[data-idx="${this.props.index}"]`).click(clickEvent => {
const index = $(clickEvent.currentTarget).data('idx')
@ -151,8 +179,9 @@
}
class Movimiento {
props
constructor({fecha, glosa, cargo, abono, saldo, categoria, detalle, centro_costo, rut, nombres, identificador}) {
constructor({sociedad, fecha, glosa, cargo, abono, saldo, categoria, detalle, centro_costo, rut, nombres, identificador}) {
this.props = {
sociedad,
fecha,
glosa,
cargo,
@ -170,6 +199,7 @@
const fecha = new Date(this.props.fecha)
return [
'<tr>',
`<td>${this.props.sociedad.sigla}</td>`,
`<td>${formatters.date.format(fecha)}</td>`,
`<td>${this.props.glosa}</td>`,
`<td class="right aligned">${formatters.number.format(this.props.cargo ?? 0)}</td>`,
@ -229,6 +259,14 @@
}
}
},
fetch() {
return {
bancos: sociedad_rut => {
const url = `{{$urls->api}}/inmobiliaria/${sociedad_rut}/cuentas`
return fetchAPI(url)
}
}
},
import() {
return {
cartolas: () => {
@ -237,7 +275,8 @@
const body = new FormData()
this.data.cartolas.forEach(cartola => {
Object.entries(cartola.data).forEach(([key, value]) => {
body.append(key, value)
const name = `${key}[${cartola.props.index - 1}]`
body.append(name, value)
})
})
@ -290,6 +329,7 @@
cartolas: {
sociedad: 'sociedad',
banco: 'banco',
mes: 'mes',
buttons: {
remove: 'remove'
}