Edit transacciones, refresh, sort cuentas
This commit is contained in:
@ -20,27 +20,19 @@ class TiposCambios {
|
|||||||
$this->base_url = $api_url;
|
$this->base_url = $api_url;
|
||||||
$this->key = $api_key;
|
$this->key = $api_key;
|
||||||
}
|
}
|
||||||
public function get(string $fecha, int $moneda_id) {
|
protected function getWeekday(\DateTimeInterface $fecha) {
|
||||||
$fecha = Carbon::parse($fecha);
|
if ($fecha->weekday() == 0) {
|
||||||
$moneda = $this->factory->find(Moneda::class)->one($moneda_id);
|
return $fecha->subWeek()->weekday(5);
|
||||||
if ($moneda->codigo == 'USD') {
|
|
||||||
if ($fecha->weekday() == 0) {
|
|
||||||
$fecha = $fecha->subWeek()->weekday(5);
|
|
||||||
}
|
|
||||||
if ($fecha->weekday() == 6) {
|
|
||||||
$fecha = $fecha->weekday(5);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$cambio = $moneda->cambio($fecha);
|
if ($fecha->weekday() == 6) {
|
||||||
if ($cambio) {
|
return $fecha->weekday(5);
|
||||||
if ($cambio->desde()->id != $moneda->id) {
|
|
||||||
return 1 / $cambio->valor;
|
|
||||||
}
|
|
||||||
return $cambio->valor;
|
|
||||||
}
|
}
|
||||||
|
return $fecha;
|
||||||
|
}
|
||||||
|
protected function getValor(\DateTimeInterface $fecha, string $moneda_codigo) {
|
||||||
$data = [
|
$data = [
|
||||||
'fecha' => $fecha->format('Y-m-d'),
|
'fecha' => $fecha->format('Y-m-d'),
|
||||||
'desde' => $moneda->codigo
|
'desde' => $moneda_codigo
|
||||||
];
|
];
|
||||||
$headers = [
|
$headers = [
|
||||||
'Authorization' => 'Bearer ' . $this->key
|
'Authorization' => 'Bearer ' . $this->key
|
||||||
@ -60,7 +52,23 @@ class TiposCambios {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$result = json_decode($response->getBody());
|
$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 = [
|
$data = [
|
||||||
'fecha' => $fecha->format('Y-m-d H:i:s'),
|
'fecha' => $fecha->format('Y-m-d H:i:s'),
|
||||||
'desde_id' => $moneda->id,
|
'desde_id' => $moneda->id,
|
||||||
|
@ -18,6 +18,11 @@ class Categoria extends Model {
|
|||||||
public function cuentas() {
|
public function cuentas() {
|
||||||
if ($this->cuentas === null) {
|
if ($this->cuentas === null) {
|
||||||
$this->cuentas = $this->parentOf(Cuenta::class, [Model::CHILD_KEY => 'categoria_id']);
|
$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;
|
return $this->cuentas;
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ class Transaccion {
|
|||||||
).append(
|
).append(
|
||||||
$('<td></td>').attr('class', 'right aligned').html(format.format(saldo))
|
$('<td></td>').attr('class', 'right aligned').html(format.format(saldo))
|
||||||
).append(
|
).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(
|
$('<button></button>').attr('class', 'ui tiny circular icon button').append(
|
||||||
$('<i></i>').attr('class', 'edit icon')
|
$('<i></i>').attr('class', 'edit icon')
|
||||||
).click((e) => {
|
).click((e) => {
|
||||||
@ -58,7 +58,7 @@ class Transaccion {
|
|||||||
this.edit()
|
this.edit()
|
||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
)*/.append(
|
).append(
|
||||||
$('<button></button>').attr('class', 'ui tiny circular red icon button').append(
|
$('<button></button>').attr('class', 'ui tiny circular red icon button').append(
|
||||||
$('<i></i>').attr('class', 'remove icon')
|
$('<i></i>').attr('class', 'remove icon')
|
||||||
).click((e) => {
|
).click((e) => {
|
||||||
@ -71,7 +71,15 @@ class Transaccion {
|
|||||||
}
|
}
|
||||||
edit() {
|
edit() {
|
||||||
const form = this.modal.find('form')
|
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() {
|
remove() {
|
||||||
sendDelete(_urls.api + '/transaccion/' + this.id + '/delete').then(() => {
|
sendDelete(_urls.api + '/transaccion/' + this.id + '/delete').then(() => {
|
||||||
@ -90,6 +98,7 @@ const transacciones = {
|
|||||||
get: function() {
|
get: function() {
|
||||||
return {
|
return {
|
||||||
transacciones: () => {
|
transacciones: () => {
|
||||||
|
this.draw().loading()
|
||||||
let promises = []
|
let promises = []
|
||||||
sendGet(_urls.api + '/cuenta/' + this.cuenta_id + '/transacciones/amount').then((data) => {
|
sendGet(_urls.api + '/cuenta/' + this.cuenta_id + '/transacciones/amount').then((data) => {
|
||||||
if (data.cuenta === null) {
|
if (data.cuenta === null) {
|
||||||
@ -128,10 +137,10 @@ const transacciones = {
|
|||||||
return (new Date(b.fecha)) - (new Date(a.fecha))
|
return (new Date(b.fecha)) - (new Date(a.fecha))
|
||||||
})
|
})
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
this.draw()
|
this.draw().table()
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
this.draw()
|
this.draw().table()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -161,13 +170,30 @@ const transacciones = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
draw: function() {
|
draw: function() {
|
||||||
const format = Intl.NumberFormat('es-CL', {style: 'currency', currency: this.cuenta.moneda.codigo})
|
return {
|
||||||
const parent = $(this.id)
|
loading: () => {
|
||||||
parent.html('')
|
const parent = $(this.id)
|
||||||
$.each(this.transacciones, (i, el) => {
|
parent.html('')
|
||||||
parent.append(el.draw({saldo: this.saldo, format: format}))
|
parent.append(
|
||||||
this.saldo = this.saldo + parseInt(el.valor.valor) * ((el.isIncrement()) ? 1 : -1)
|
$('<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() {
|
add: function() {
|
||||||
return {
|
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() {
|
build: function() {
|
||||||
return {
|
return {
|
||||||
modal: () => {
|
modal: () => {
|
||||||
@ -212,7 +259,12 @@ const transacciones = {
|
|||||||
})
|
})
|
||||||
this.modal.find('form').submit((e) => {
|
this.modal.find('form').submit((e) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
this.add().exec()
|
const add = $(e.currentTarget).find('.plus.icon')
|
||||||
|
if (add.length > 0) {
|
||||||
|
this.add().exec()
|
||||||
|
} else {
|
||||||
|
this.edit()
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
this.modal.find('.ui.calendar').calendar({
|
this.modal.find('.ui.calendar').calendar({
|
||||||
@ -234,7 +286,10 @@ const transacciones = {
|
|||||||
},
|
},
|
||||||
setup: function() {
|
setup: function() {
|
||||||
this.build().modal()
|
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.add().show()
|
||||||
})
|
})
|
||||||
this.get().transacciones()
|
this.get().transacciones()
|
||||||
|
@ -39,7 +39,20 @@ class Cuenta {
|
|||||||
}
|
}
|
||||||
tr.append(td)
|
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() {
|
remove() {
|
||||||
$("[data-id='" + this.id + "'][data-class='cuenta']").remove()
|
$("[data-id='" + this.id + "'][data-class='cuenta']").remove()
|
||||||
|
@ -27,23 +27,16 @@
|
|||||||
Saldo
|
Saldo
|
||||||
</th>
|
</th>
|
||||||
<th class="right aligned">
|
<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>
|
<i class="plus icon"></i>
|
||||||
</button>
|
</button>
|
||||||
</th>
|
</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody id="transacciones">
|
<tbody id="transacciones"></tbody>
|
||||||
<tr>
|
|
||||||
<td colspan="7">
|
|
||||||
<div class="ui active dimmer">
|
|
||||||
<div class="ui indeterminate elastic text loader">
|
|
||||||
Buscando los datos
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<div class="ui modal">
|
<div class="ui modal">
|
||||||
|
Reference in New Issue
Block a user