diff --git a/api/common/Controller/Cuentas.php b/api/common/Controller/Cuentas.php index 8129e24..1bd9bd2 100644 --- a/api/common/Controller/Cuentas.php +++ b/api/common/Controller/Cuentas.php @@ -90,25 +90,11 @@ class Cuentas { ]; return $this->withJson($response, $output); } - public function transacciones(Request $request, Response $response, Factory $factory, $cuenta_id): Response { + public function transacciones(Request $request, Response $response, Factory $factory, $cuenta_id, $limit = null, $start = 0): Response { $cuenta = $factory->find(Cuenta::class)->one($cuenta_id); - $cargos = null; - $abonos = null; $transacciones = null; if ($cuenta !== null) { - $cargos = $cuenta->cargos(); - if ($cargos !== null) { - array_walk($cargos, function(&$item) { - $item = $item->toArray(); - }); - } - $abonos = $cuenta->abonos(); - if ($abonos !== null) { - array_walk($abonos, function(&$item) { - $item = $item->toArray(); - }); - } - $transacciones = $cuenta->transacciones(); + $transacciones = $cuenta->transacciones($limit, $start); if (count($transacciones)) { array_walk($transacciones, function(&$item) { $item = $item->toArray(); @@ -118,8 +104,19 @@ class Cuentas { $output = [ 'input' => $cuenta_id, 'cuenta' => $cuenta?->toArray(), - 'cargos' => $cargos, - 'abonos' => $abonos, + 'transacciones' => $transacciones + ]; + return $this->withJson($response, $output); + } + public function transaccionesAmount(Request $request, Response $response, Factory $factory, $cuenta_id): Response { + $cuenta = $factory->find(Cuenta::class)->one($cuenta_id); + $transacciones = 0; + if ($cuenta !== null) { + $transacciones = count($cuenta->transacciones()); + } + $output = [ + 'input' => $cuenta_id, + 'cuenta' => $cuenta?->toArray(), 'transacciones' => $transacciones ]; return $this->withJson($response, $output); diff --git a/api/composer.json b/api/composer.json index 8d7b004..c065ec0 100644 --- a/api/composer.json +++ b/api/composer.json @@ -9,7 +9,7 @@ "nyholm/psr7-server": "^1.0", "zeuxisoo/slim-whoops": "^0.7.3", "provm/controller": "^1.0", - "provm/models": "1.0.0-rc2", + "provm/models": "^1.0.0-rc3", "nesbot/carbon": "^2.50" }, "require-dev": { diff --git a/api/resources/routes/cuentas.php b/api/resources/routes/cuentas.php index 6e93ebb..06ea592 100644 --- a/api/resources/routes/cuentas.php +++ b/api/resources/routes/cuentas.php @@ -7,7 +7,10 @@ $app->group('/cuentas', function($app) { }); $app->group('/cuenta/{cuenta_id}', function($app) { $app->get('/entradas', [Cuentas::class, 'entradas']); - $app->get('/transacciones', [Cuentas::class, 'transacciones']); + $app->group('/transacciones', function($app) { + $app->get('/amount', [Cuentas::class, 'transaccionesAmount']); + $app->get('[/{limit:[0-9]+}[/{start:[0-9]+}]]', [Cuentas::class, 'transacciones']); + }); $app->put('/edit', [Cuentas::class, 'edit']); $app->delete('/delete', [Cuentas::class, 'delete']); $app->get('[/]', [Cuentas::class, 'show']); diff --git a/api/src/Cuenta.php b/api/src/Cuenta.php index 66d809d..f49de81 100644 --- a/api/src/Cuenta.php +++ b/api/src/Cuenta.php @@ -43,25 +43,23 @@ class Cuenta extends Model { return $this->abonos; } protected $transacciones; - public function transacciones() { + public function transacciones($limit = null, $start = 0) { if ($this->transacciones === null) { - $this->transacciones = []; - if ($this->abonos() !== null) { - $this->transacciones = array_merge($this->transacciones, $this->abonos()); + $transacciones = Model::factory(Transaccion::class) + ->join('cuentas', 'cuentas.id = transacciones.desde_id OR cuentas.id = transacciones.hasta_id') + ->whereEqual('cuentas.id', $this->id) + ->orderByAsc('transacciones.fecha'); + if ($limit !== null) { + $transacciones = $transacciones->limit($limit) + ->offset($start); } - if ($this->cargos() !== null) { - $this->transacciones = array_merge($this->transacciones, array_map(function($item) { - $item->valor = - $item->valor; - return $item; - }, $this->cargos())); - } - usort($this->transacciones, function($a, $b) { - $d = $a->fecha()->diffInDays($b->fecha(), false); - if ($d === 0) { - return $a->valor - $b->valor; + $this->transacciones = $transacciones->findMany(); + foreach ($this->transacciones as &$transaccion) { + $transaccion->setFactory($this->factory); + if ($transaccion->desde_id === $this->id) { + $transaccion->valor = - $transaccion->valor; } - return $d; - }); + } } return $this->transacciones; } diff --git a/ui/public/assets/scripts/cuentas.show.js b/ui/public/assets/scripts/cuentas.show.js index bbea966..9035489 100644 --- a/ui/public/assets/scripts/cuentas.show.js +++ b/ui/public/assets/scripts/cuentas.show.js @@ -8,23 +8,41 @@ const transacciones = { get: function() { return { transacciones: () => { - return $.ajax({ - url: _urls.api + '/cuenta/' + this.cuenta_id + '/transacciones', + let promises = [] + $.ajax({ + url: _urls.api + '/cuenta/' + this.cuenta_id + '/transacciones/amount', method: 'GET', dataType: 'json' }).then((data) => { - if (data.cuenta === null) { - return + const amount = data.transacciones + const step = 100 + for (let i = 0; i < amount; i += step) { + promises.push($.ajax({ + url: _urls.api + '/cuenta/' + this.cuenta_id + '/transacciones/' + step + '/' + i, + method: 'GET', + dataType: 'json' + })) } - 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() + Promise.all(promises).then((data_arr) => { + if (data_arr[0].cuenta === null) { + return + } + this.cuenta = data_arr[0].cuenta + this.saldo = this.cuenta.saldo + $('#cuenta').html(this.cuenta.nombre + ' (' + this.cuenta.categoria.nombre + ')') + this.transacciones = [] + data_arr.forEach(data => { + if (data.transacciones === null || data.transacciones.length == 0) { + return + } + this.transacciones.push(...data.transacciones) + }) + this.transacciones.sort((a, b) => { + return (new Date(b.fecha)) - (new Date(a.fecha)) + }) + }).then(() => { + this.draw() + }) }) }, cuentas: () => { @@ -53,7 +71,7 @@ const transacciones = { const parent = $(this.id) parent.html('') $.each(this.transacciones, (i, el) => { - const fuente = (el.valor < 0) ? el.desde : el.hasta + const fuente = (el.valor < 0) ? el.hasta : el.desde parent.append( $('