Edit transacciones, refresh, sort cuentas

This commit is contained in:
2022-01-05 16:02:40 -03:00
5 changed files with 119 additions and 45 deletions

View File

@ -20,27 +20,19 @@ class TiposCambios {
$this->base_url = $api_url;
$this->key = $api_key;
}
public function get(string $fecha, int $moneda_id) {
$fecha = Carbon::parse($fecha);
$moneda = $this->factory->find(Moneda::class)->one($moneda_id);
if ($moneda->codigo == 'USD') {
if ($fecha->weekday() == 0) {
$fecha = $fecha->subWeek()->weekday(5);
}
if ($fecha->weekday() == 6) {
$fecha = $fecha->weekday(5);
}
protected function getWeekday(\DateTimeInterface $fecha) {
if ($fecha->weekday() == 0) {
return $fecha->subWeek()->weekday(5);
}
$cambio = $moneda->cambio($fecha);
if ($cambio) {
if ($cambio->desde()->id != $moneda->id) {
return 1 / $cambio->valor;
}
return $cambio->valor;
if ($fecha->weekday() == 6) {
return $fecha->weekday(5);
}
return $fecha;
}
protected function getValor(\DateTimeInterface $fecha, string $moneda_codigo) {
$data = [
'fecha' => $fecha->format('Y-m-d'),
'desde' => $moneda->codigo
'desde' => $moneda_codigo
];
$headers = [
'Authorization' => 'Bearer ' . $this->key
@ -60,7 +52,23 @@ class TiposCambios {
return null;
}
$result = json_decode($response->getBody());
$valor = $result->serie[0]->valor;
return $result->serie[0]->valor;
}
public function get(string $fecha, int $moneda_id) {
$fecha = Carbon::parse($fecha);
$moneda = $this->factory->find(Moneda::class)->one($moneda_id);
if ($moneda->codigo == 'USD') {
$fecha = $this->getWeekday($fecha);
}
// If a value exists in the database
$cambio = $moneda->cambio($fecha);
if ($cambio) {
if ($cambio->desde()->id != $moneda->id) {
return 1 / $cambio->valor;
}
return $cambio->valor;
}
$valor = $this->getValor($fecha, $moneda->codigo);
$data = [
'fecha' => $fecha->format('Y-m-d H:i:s'),
'desde_id' => $moneda->id,

View File

@ -18,6 +18,11 @@ class Categoria extends Model {
public function cuentas() {
if ($this->cuentas === null) {
$this->cuentas = $this->parentOf(Cuenta::class, [Model::CHILD_KEY => 'categoria_id']);
if ($this->cuentas !== null) {
usort($this->cuentas, function($a, $b) {
return strcmp($a->nombre, $b->nombre);
});
}
}
return $this->cuentas;
}

View File

@ -50,7 +50,7 @@ class Transaccion {
).append(
$('<td></td>').attr('class', 'right aligned').html(format.format(saldo))
).append(
$('<td></td>').attr('class', 'right aligned')/*.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) => {
@ -58,7 +58,7 @@ class Transaccion {
this.edit()
return false
})
)*/.append(
).append(
$('<button></button>').attr('class', 'ui tiny circular red icon button').append(
$('<i></i>').attr('class', 'remove icon')
).click((e) => {
@ -71,7 +71,15 @@ class Transaccion {
}
edit() {
const form = this.modal.find('form')
form.find("[name='fecha']")
form.trigger('reset')
form.find("[name='id']").val(this.id)
form.find(".ui.calendar").calendar('set date', new Date(this.fecha.fecha))
form.find("[name='cuenta']").dropdown('set selected', (this.isDebito()) ? this.credito_id : this.credito_id)
form.find("[name='glosa']").val(this.glosa)
form.find("[name='detalle']").val(this.detalle)
form.find("[name='valor']").val(((this.isDebito()) ? -1 : 1) * this.valor.valor)
modalToEdit(this.modal)
this.modal.modal('show')
}
remove() {
sendDelete(_urls.api + '/transaccion/' + this.id + '/delete').then(() => {
@ -90,6 +98,7 @@ const transacciones = {
get: function() {
return {
transacciones: () => {
this.draw().loading()
let promises = []
sendGet(_urls.api + '/cuenta/' + this.cuenta_id + '/transacciones/amount').then((data) => {
if (data.cuenta === null) {
@ -128,10 +137,10 @@ const transacciones = {
return (new Date(b.fecha)) - (new Date(a.fecha))
})
}).then(() => {
this.draw()
this.draw().table()
})
} else {
this.draw()
this.draw().table()
}
})
})
@ -161,13 +170,30 @@ const transacciones = {
}
},
draw: function() {
const format = Intl.NumberFormat('es-CL', {style: 'currency', currency: this.cuenta.moneda.codigo})
const parent = $(this.id)
parent.html('')
$.each(this.transacciones, (i, el) => {
parent.append(el.draw({saldo: this.saldo, format: format}))
this.saldo = this.saldo + parseInt(el.valor.valor) * ((el.isIncrement()) ? 1 : -1)
})
return {
loading: () => {
const parent = $(this.id)
parent.html('')
parent.append(
$('<tr></tr>').append(
$('<td></td>').attr('colspan', 7).append(
$('<div></div>').attr('class', 'ui active dimmer').append(
$('<div></div>').attr('class', 'ui indeterminate elastic text loader').html('Buscando los datos')
)
)
)
)
},
table: () => {
const format = Intl.NumberFormat('es-CL', {style: 'currency', currency: this.cuenta.moneda.codigo})
const parent = $(this.id)
parent.html('')
$.each(this.transacciones, (i, el) => {
parent.append(el.draw({saldo: this.saldo, format: format}))
this.saldo = this.saldo + parseInt(el.valor.valor) * ((el.isIncrement()) ? 1 : -1)
})
}
}
},
add: function() {
return {
@ -202,6 +228,27 @@ const transacciones = {
}
}
},
edit: function() {
const id = $("[name='id']").val()
const fecha = $("[name='fecha']").val()
const valor = $("[name='valor']").val()
const cuenta = $("[name='cuenta']").val()
const data = JSON.stringify({
debito_id: (valor < 0) ? this.cuenta_id : cuenta,
credito_id: (valor < 0) ? cuenta : this.cuenta_id,
fecha,
glosa,
detalle,
valor: (valor < 0) ? -valor : valor
})
return sendPut(_urls.api + '/transaccion/' + id + '/edit', data).then(() => {
this.modal.modal('hide')
this.get().transacciones()
})
},
refresh: function () {
this.get().transacciones()
},
build: function() {
return {
modal: () => {
@ -212,7 +259,12 @@ const transacciones = {
})
this.modal.find('form').submit((e) => {
e.preventDefault()
this.add().exec()
const add = $(e.currentTarget).find('.plus.icon')
if (add.length > 0) {
this.add().exec()
} else {
this.edit()
}
return false
})
this.modal.find('.ui.calendar').calendar({
@ -234,7 +286,10 @@ const transacciones = {
},
setup: function() {
this.build().modal()
$(this.id).parent().find('.ui.button').click(() => {
$(this.id).parent().find('#refresh').click(() => {
this.refresh()
})
$(this.id).parent().find('#add').click(() => {
this.add().show()
})
this.get().transacciones()

View File

@ -39,7 +39,20 @@ class Cuenta {
}
tr.append(td)
})
$("[data-id='" + this.categoria_id + "'][data-class='categoria']").after(tr)
const prev = this.prev()
prev.after(tr)
}
prev() {
let prev = $("[data-id='" + this.categoria_id + "'][data-class='categoria']")
let n = 0
while (prev.next().attr('data-class') === 'cuenta') {
prev = prev.next()
n ++;
if (n >= 100) {
return prev
}
}
return prev
}
remove() {
$("[data-id='" + this.id + "'][data-class='cuenta']").remove()

View File

@ -27,23 +27,16 @@
Saldo
</th>
<th class="right aligned">
<button class="ui tiny green circular icon button">
<button class="ui tiny blue circular icon button" id="refresh">
<i class="refresh icon"></i>
</button>
<button class="ui tiny green circular icon button" id="add">
<i class="plus icon"></i>
</button>
</th>
</tr>
</thead>
<tbody id="transacciones">
<tr>
<td colspan="7">
<div class="ui active dimmer">
<div class="ui indeterminate elastic text loader">
Buscando los datos
</div>
</div>
</td>
</tr>
</tbody>
<tbody id="transacciones"></tbody>
</table>
<div class="ui modal">