diff --git a/ui/common/Controller/Bancos.php b/ui/common/Controller/Bancos.php deleted file mode 100644 index d3c8e8d..0000000 --- a/ui/common/Controller/Bancos.php +++ /dev/null @@ -1,12 +0,0 @@ -render($response, 'bancos.add'); - } -} diff --git a/ui/common/Controller/Cuentas.php b/ui/common/Controller/Cuentas.php index b7b24b8..332096d 100644 --- a/ui/common/Controller/Cuentas.php +++ b/ui/common/Controller/Cuentas.php @@ -9,6 +9,9 @@ class Cuentas { public function __invoke(Request $request, Response $response, View $view): Response { return $view->render($response, 'cuentas.list'); } + public function show(Request $request, Response $response, View $view, $cuenta_id): Response { + return $view->render($response, 'cuentas.show', compact('cuenta_id')); + } public function add(Request $request, Response $response, View $view): Response { return $view->render($response, 'cuentas.add'); } diff --git a/ui/common/Controller/Fuentes.php b/ui/common/Controller/Fuentes.php deleted file mode 100644 index 8f7ebfe..0000000 --- a/ui/common/Controller/Fuentes.php +++ /dev/null @@ -1,12 +0,0 @@ -render($response, 'fuentes.show', compact('fuente_id')); - } -} diff --git a/ui/common/Controller/TiposFuentes.php b/ui/common/Controller/TiposFuentes.php deleted file mode 100644 index ed17d65..0000000 --- a/ui/common/Controller/TiposFuentes.php +++ /dev/null @@ -1,12 +0,0 @@ -render($response, 'tipos_fuentes.add'); - } -} diff --git a/ui/public/assets/scripts/cuentas.show.js b/ui/public/assets/scripts/cuentas.show.js new file mode 100644 index 0000000..bbea966 --- /dev/null +++ b/ui/public/assets/scripts/cuentas.show.js @@ -0,0 +1,143 @@ +const transacciones = { + id: '#transacciones', + cuenta_id: 0, + cuenta: null, + transacciones: [], + cuentas: [], + saldo: 0, + get: function() { + return { + transacciones: () => { + return $.ajax({ + url: _urls.api + '/cuenta/' + this.cuenta_id + '/transacciones', + method: 'GET', + dataType: 'json' + }).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 + ')') + if (data.transacciones === null || data.transacciones.length == 0) { + return + } + this.transacciones = data.transacciones + }).then(() => { + this.draw() + }) + }, + cuentas: () => { + return $.ajax({ + url: _urls.api + '/cuentas', + method: 'GET', + dataType: 'json' + }).then((data) => { + if (data.cuentas === null || data.cuentas.length == 0) { + return + } + this.cuentas = data.cuentas + }).then(() => { + const select = this.modal.find("[name='cuenta']") + $.each(this.cuentas, (i, el) => { + select.append( + $('').attr('value', el.id).html(el.nombre + ' (' + el.categoria.nombre + ')') + ) + }) + }) + } + } + }, + draw: function() { + const format = Intl.NumberFormat('es-CL', {style: 'currency', currency: 'CLP'}) + const parent = $(this.id) + parent.html('') + $.each(this.transacciones, (i, el) => { + const fuente = (el.valor < 0) ? el.desde : el.hasta + parent.append( + $('').append( + $('').html(el.fechaFormateada) + ).append( + $('').append( + $('').attr('href', _urls.base + 'cuenta/' + fuente.id).html(fuente.nombre + ' (' + fuente.categoria.nombre + ')') + ) + ).append( + $('').html(el.glosa + '
' + el.detalle) + ).append( + $('').attr('class', 'right aligned').html((el.valor < 0) ? el.valorFormateado.replace('-', '') : '') + ).append( + $('').attr('class', 'right aligned').html((el.valor < 0) ? '' : el.valorFormateado) + ).append( + $('').attr('class', 'right aligned').html(format.format(this.saldo)) + ) + ) + this.saldo -= parseInt(el.valor) + }) + }, + add: function() { + return { + show: () => { + this.modal.find('form').trigger('reset') + this.modal.modal('show') + }, + exec: () => { + const valor = $("[name='valor']").val() + const cuenta = $("[name='cuenta']").val() + const data = JSON.stringify({ + desde_id: (valor < 0) ? this.cuenta_id : cuenta, + hasta_id: (valor < 0) ? cuenta : this.cuenta_id, + fecha: $("[name='fecha']").val(), + glosa: $("[name='glosa']").val(), + detalle: $("[name='detalle']").val(), + valor: (valor < 0) ? -valor : valor + }) + return $.ajax({ + url: _urls.api + '/transacciones/add', + method: 'POST', + data: data, + dataType: 'json' + }).then(() => { + 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() + } +} diff --git a/ui/public/assets/scripts/fuentes.show.js b/ui/public/assets/scripts/fuentes.show.js deleted file mode 100644 index 3593e44..0000000 --- a/ui/public/assets/scripts/fuentes.show.js +++ /dev/null @@ -1,121 +0,0 @@ -const entradas = { - id: '#entradas', - modal: null, - fuente: null, - entradas: [], - getEntradas: function() { - return $.ajax({ - url: _urls.api + '/fuente/' + this.fuente + '/entradas', - method: 'GET', - dataType: 'json' - }).then((data) => { - if (data.fuente === null) { - return - } - $('#fuente').html(data.fuente.tipo.descripcion + ' - ' + data.fuente.banco.nombre + ' (' + data.fuente.saldoFormateado + ')') - if (data.entradas == null || data.entradas.length == 0) { - return - } - this.entradas = data.entradas - }).then(() => { - this.draw() - }) - }, - draw: function() { - const table = $(this.id) - table.html('') - $.each(this.entradas, (i, el) => { - table.append( - $('').append( - $('').html(el.fechaFormateada) - ).append( - $('').html(el.cuenta.nombre + ' (' + el.cuenta.categoria.nombre + ')') - ).append( - $('').html(el.glosa) - ).append( - $('').html(el.detalle) - ).append( - $('').attr('class', 'right aligned').html(el.valorFormateado) - ) - ) - }) - }, - getCuentas: function() { - return $.ajax({ - url: _urls.api + '/cuentas', - method: 'GET', - dataType: 'json' - }).then((data) => { - if (data.cuentas === null || data.cuentas.length == 0) { - return - } - this.cuentas = data.cuentas - }).then(() => { - const select = $("select[name='cuenta']") - $.each(this.cuentas, (i, el) => { - select.append( - $('').attr('value', el.id).html(el.nombre + ' (' + el.categoria.nombre + ')') - ) - }) - }) - }, - add: function() { - this.modal.find('form').trigger('reset') - this.modal.modal('show') - }, - doAdd: function() { - const data = JSON.stringify({ - fecha: $("[name='fecha']").val(), - fuente_id: this.fuente, - glosa: $("[name='glosa']").val(), - detalle: $("[name='detalle']").val(), - cuenta_id: $("[name='cuenta']").val(), - valor: $("[name='valor']").val() - }) - return $.ajax({ - url: _urls.api + '/entradas/add', - method: 'POST', - data: data, - dataType: 'json' - }).then((data) => { - this.modal.modal('hide') - this.getEntradas() - }) - }, - setupModal: function() { - this.modal = $('.ui.modal') - this.modal.modal() - this.modal.find('.ui.calendar').calendar({ - type: 'date', - formatter: { - date: function(date, settings) { - if (!date) return '' - let day = date.getDate() - let month = ('00' + (date.getMonth() + 1)).slice(-2) - let year = date.getFullYear() - return year + '/' + month + '/' + day - } - }, - maxDate: new Date() - }) - this.modal.find('.close.icon').css('cursor', 'pointer').click(() => { - this.modal.modal('hide') - }) - this.modal.find('form').submit((e) => { - e.preventDefault() - this.doAdd() - return false - }) - }, - setup: function() { - this.setupModal() - this.getEntradas().then(() => { - this.getCuentas() - }) - $(this.id).parent().find('.ui.button').click((e) => { - e.preventDefault() - this.add() - return false - }) - } -} diff --git a/ui/public/assets/scripts/home.js b/ui/public/assets/scripts/home.js index 87f851b..c8de00e 100644 --- a/ui/public/assets/scripts/home.js +++ b/ui/public/assets/scripts/home.js @@ -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 = $('
').attr('class', 'ui table').append( - $('').append( - $('').append( - $('').html('Fuente') - ).append( - $('').html('Saldo') - ).append( - $('').attr('class', 'right aligned').append( - $('').attr('class', 'ui tiny green circular icon button').append( - $('').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 = $('
').attr('class', 'ui table').append( + $('').append( + $('').append( + $('').html('Cuenta') + ).append( + $('').attr('class', 'right aligned').html('Saldo') + ) ) ) - ) - ) - parent = $('').attr('id', this.id) - table.append(parent) - $('h1.header').after(table) + parent = $('').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( - $('').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( - $('').attr('value', el.id).html(el.nombre) - ) - }) - }) + } }, draw: function() { - const parent = this.getParent() - $.each(this.fuentes, (i, el) => { - const f = $('').append( - $('').append( - $('').attr('href', _urls.base + 'fuente/' + el.id).html(el.tipo.descripcion + ' - ' + el.banco.nombre) - ) - ).append( - $('').html(el.saldoFormateado) - ) - parent.append(f) - }) + return { + categorias: () => { + const parent = this.get().parent() + $.each(this.categorias, (i, el) => { + const button = $('').attr('class', 'ui mini compact icon button').append( + $('').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 = $('').attr('data-id', el.id).append( + $('').append( + $('
').append(button).append(el.nombre) + ) + ).append( + $('').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( + $('').attr('class', 'item').append( + $('').append( + $('').attr('href', _urls.base + 'cuenta/' + el.id).html(el.nombre) + ) + ).append( + $('').attr('class', 'right aligned').html(el.saldoFormateado) + ) + ) + }) + } + } + }, + setup: async function() { + this.get().categorias() } } diff --git a/ui/resources/routes/bancos.php b/ui/resources/routes/bancos.php deleted file mode 100644 index c0ea4e8..0000000 --- a/ui/resources/routes/bancos.php +++ /dev/null @@ -1,4 +0,0 @@ -get('/bancos/add', [Bancos::class, 'add']); diff --git a/ui/resources/routes/cuentas.php b/ui/resources/routes/cuentas.php index be40131..376e107 100644 --- a/ui/resources/routes/cuentas.php +++ b/ui/resources/routes/cuentas.php @@ -2,3 +2,4 @@ use Contabilidad\Common\Controller\Cuentas; $app->get('/cuentas', Cuentas::class); +$app->get('/cuenta/{cuenta_id}', [Cuentas::class, 'show']); diff --git a/ui/resources/routes/fuentes.php b/ui/resources/routes/fuentes.php deleted file mode 100644 index fa0ba4c..0000000 --- a/ui/resources/routes/fuentes.php +++ /dev/null @@ -1,4 +0,0 @@ -get('/fuente/{fuente_id}', [Fuentes::class, 'show']); diff --git a/ui/resources/routes/tipos_fuentes.php b/ui/resources/routes/tipos_fuentes.php deleted file mode 100644 index 5ce612e..0000000 --- a/ui/resources/routes/tipos_fuentes.php +++ /dev/null @@ -1,4 +0,0 @@ -get('/tipos_fuentes/add', [TiposFuentes::class, 'add']); diff --git a/ui/resources/views/bancos/base.blade.php b/ui/resources/views/bancos/base.blade.php deleted file mode 100644 index b9ea728..0000000 --- a/ui/resources/views/bancos/base.blade.php +++ /dev/null @@ -1,14 +0,0 @@ -@extends('layout.base') - -@section('page_content') -

- @hasSection('bancos_title') - Banco @yield('bancos_title') - @else - Bancos - @endif -

-
- @yield('bancos_content') -
-@endsection diff --git a/ui/resources/views/fuentes/show.blade.php b/ui/resources/views/cuentas/show.blade.php similarity index 77% rename from ui/resources/views/fuentes/show.blade.php rename to ui/resources/views/cuentas/show.blade.php index 25b6a32..541dec9 100644 --- a/ui/resources/views/fuentes/show.blade.php +++ b/ui/resources/views/cuentas/show.blade.php @@ -1,27 +1,30 @@ -@extends('fuentes.base') +@extends('cuentas.base') -@section('fuentes_title') - +@section('cuentas_title') + @endsection -@section('fuentes_content') - +@section('cuentas_content') +
- + + - +
Fecha - Cuenta - - Glosa + Categoría Detalle - Valor + Debe + + Haber + + Saldo
+