diff --git a/api/common/Controller/Bancos.php b/api/common/Controller/Bancos.php deleted file mode 100644 index 968cf05..0000000 --- a/api/common/Controller/Bancos.php +++ /dev/null @@ -1,71 +0,0 @@ -find(Banco::class)->array(); - if ($bancos) { - usort($bancos, function($a, $b) { - return strcmp($a['nombre'], $b['nombre']); - }); - } - $output = [ - 'bancos' => $bancos - ]; - return $this->withJson($response, $output); - } - public function show(Request $request, Response $response, Factory $factory, $banco_id): Response { - $banco = $factory->find(Banco::class)->one($banco_id); - $output = [ - 'input' => $banco_id, - 'banco' => $banco?->toArray() - ]; - return $this->withJson($response, $output); - } - public function add(Request $request, Response $response, Factory $factory): Response { - $input = json_decode($request->getBody()); - $results = []; - if (is_array($input)) { - foreach ($input as $in) { - $banco = Banco::add($factory, $in); - $results []= ['banco' => $banco?->toArray(), 'agregado' => $banco?->save()]; - } - } else { - $banco = Banco::add($factory, $input); - $results []= ['banco' => $banco?->toArray(), 'agregado' => $banco?->save()]; - } - $output = [ - 'input' => $input, - 'bancos' => $results - ]; - return $this->withJson($response, $output); - } - public function edit(Request $request, Response $response, Factory $factory, $banco_id): Response { - $banco = $factory->find(Banco::class)->one($banco_id); - $output = [ - 'input' => $banco_id, - 'old' => $banco->toArray() - ]; - $input = json_decode($request->getBody()); - $banco->edit($input); - $output['banco'] = $banco->toArray(); - return $this->withJson($response, $output); - } - public function delete(Request $request, Response $response, Factory $factory, $banco_id): Response { - $banco = $factory->find(Banco::class)->one($banco_id); - $output = [ - 'input' => $banco_id, - 'banco' => $banco->toArray(), - 'eliminado' => $banco->delete() - ]; - return $this->withJson($response, $output); - } -} diff --git a/api/common/Controller/Cuentas.php b/api/common/Controller/Cuentas.php index 49634cf..8129e24 100644 --- a/api/common/Controller/Cuentas.php +++ b/api/common/Controller/Cuentas.php @@ -90,4 +90,38 @@ class Cuentas { ]; return $this->withJson($response, $output); } + public function transacciones(Request $request, Response $response, Factory $factory, $cuenta_id): 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(); + if (count($transacciones)) { + array_walk($transacciones, function(&$item) { + $item = $item->toArray(); + }); + } + } + $output = [ + 'input' => $cuenta_id, + 'cuenta' => $cuenta?->toArray(), + 'cargos' => $cargos, + 'abonos' => $abonos, + 'transacciones' => $transacciones + ]; + return $this->withJson($response, $output); + } } diff --git a/api/common/Controller/Fuentes.php b/api/common/Controller/Fuentes.php deleted file mode 100644 index ab2136c..0000000 --- a/api/common/Controller/Fuentes.php +++ /dev/null @@ -1,91 +0,0 @@ -find(Fuente::class)->array(); - $output = [ - 'fuentes' => $fuentes - ]; - return $this->withJson($response, $output); - } - public function show(Request $request, Response $response, Factory $factory, $fuente_id): Response { - $fuente = $factory->find(Fuente::class)->one($fuente_id); - $output = [ - 'input' => $fuente_id, - 'fuente' => $fuente?->toArray() - ]; - return $this->withJson($response, $output); - } - public function add(Request $request, Response $response, Factory $factory): Response { - $input = json_decode($request->getBody()); - $results = []; - if (is_array($input)) { - foreach ($input as $in) { - $fuente = Fuente::add($factory, $in); - $results []= ['fuente' => $fuente?->toArray(), 'agregado' => $fuente?->save()]; - } - } else { - $fuente = Fuente::add($factory, $input); - $results []= ['fuente' => $fuente?->toArray(), 'agregado' => $fuente?->save()]; - } - $output = [ - 'input' => $input, - 'fuentes' => $results - ]; - return $this->withJson($response, $output); - } - public function edit(Request $request, Response $response, Factory $factory, $fuente_id): Response { - $fuente = $factory->find(Fuente::class)->one($fuente_id); - $output = [ - 'input' => $fuente_id, - 'old' => $fuente->toArray() - ]; - $input = json_decode($request->getBody()); - $fuente->edit($input); - $output['fuente'] = $fuente->toArray(); - return $this->withJson($response, $output); - } - public function delete(Request $request, Response $response, Factory $factory, $fuente_id): Response { - $fuente = $factory->find(Fuente::class)->one($fuente_id); - $output = [ - 'input' => $fuente_id, - 'fuente' => $fuente->toArray(), - 'eliminado' => $fuente->delete() - ]; - return $this->withJson($response, $output); - } - public function entradas(Request $request, Response $response, Factory $factory, $fuente_id): Response { - $fuente = $factory->find(Fuente::class)->one($fuente_id); - $entradas = null; - if ($fuente !== null) { - $entradas = $fuente->entradas(); - if ($entradas !== null) { - usort($entradas, function($a, $b) { - $d = $a->fecha()->diffInDays($b->fecha(), false); - if ($d === 0) { - return strcmp($a->cuenta()->nombre, $b->cuenta()->nombre); - } - return $d; - }); - array_walk($entradas, function(&$item) { - $item = $item->toArray(); - }); - } - } - $output = [ - 'input' => $fuente_id, - 'fuente' => $fuente?->toArray(), - 'entradas' => $entradas - ]; - return $this->withJson($response, $output); - } -} diff --git a/api/common/Controller/TiposFuentes.php b/api/common/Controller/TiposFuentes.php deleted file mode 100644 index 030eb3f..0000000 --- a/api/common/Controller/TiposFuentes.php +++ /dev/null @@ -1,89 +0,0 @@ -find(TipoFuente::class)->array(); - if ($tipos_fuentes) { - usort($tipos_fuentes, function($a, $b) { - return strcmp($a['descripcion'], $b['descripcion']); - }); - } - $output = [ - 'tipos_fuentes' => $tipos_fuentes - ]; - return $this->withJson($response, $output); - } - public function show(Request $request, Response $response, Factory $factory, $tipo_fuente_id): Response { - $tipo_fuente = $factory->find(TipoFuente::class)->one($tipo_fuente_id); - $output = [ - 'input' => $tipo_fuente_id, - 'tipo_fuente' => $tipo_fuente?->toArray() - ]; - return $this->withJson($response, $output); - } - public function add(Request $request, Response $response, Factory $factory): Response { - $input = json_decode($request->getBody()); - $results = []; - if (is_array($input)) { - foreach ($input as $in) { - $tipo_fuente = TipoFuente::add($factory, $in); - $results []= ['tipo_fuente' => $tipo_fuente?->toArray(), 'agregado' => $tipo_fuente?->save()]; - } - } else { - $tipo_fuente = TipoFuente::add($factory, $input); - $results []= ['tipo_fuente' => $tipo_fuente?->toArray(), 'agregado' => $tipo_fuente?->save()]; - } - $output = [ - 'input' => $input, - 'tipo_fuentes' => $results - ]; - return $this->withJson($response, $output); - } - public function edit(Request $request, Response $response, Factory $factory, $tipo_fuente_id): Response { - $tipo_fuente = $factory->find(TipoFuente::class)->one($tipo_fuente_id); - $output = [ - 'input' => $tipo_fuente_id, - 'old' => $tipo_fuente->toArray() - ]; - $input = json_decode($request->getBody()); - $tipo_fuente->edit($input); - $output['tipo_fuente'] = $tipo_fuente->toArray(); - return $this->withJson($response, $output); - } - public function delete(Request $request, Response $response, Factory $factory, $tipo_fuente_id): Response { - $tipo_fuente = $factory->find(TipoFuente::class)->one($tipo_fuente_id); - $output = [ - 'input' => $tipo_fuente_id, - 'tipo_fuente' => $tipo_fuente->toArray(), - 'eliminado' => $tipo_fuente->delete() - ]; - return $this->withJson($response, $output); - } - public function fuentes(Request $request, Response $response, Factory $factory, $tipo_fuente_id): Response { - $tipo_fuente = $factory->find(TipoFuente::class)->one($tipo_fuente_id); - $fuentes = null; - if ($tipo_fuente !== null) { - $fuentes = $tipo_fuente->fuentes(); - if ($fuentes !== null) { - array_walk($fuentes, function(&$item) { - $item = $item->toArray(); - }); - } - } - $output = [ - 'input' => $tipo_fuente_id, - 'tipo_fuente' => $tipo_fuente?->toArray(), - 'fuentes' => $fuentes - ]; - return $this->withJson($response, $output); - } -} diff --git a/api/common/Controller/Entradas.php b/api/common/Controller/Transacciones.php similarity index 51% rename from api/common/Controller/Entradas.php rename to api/common/Controller/Transacciones.php index 2d7c177..5dc6eec 100644 --- a/api/common/Controller/Entradas.php +++ b/api/common/Controller/Transacciones.php @@ -5,15 +5,15 @@ use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; use ProVM\Common\Define\Controller\Json; use ProVM\Common\Factory\Model as Factory; -use Contabilidad\Entrada; +use Contabilidad\Transaccion; -class Entradas { +class Transacciones { use Json; public function __invoke(Request $request, Response $response, Factory $factory): Response { - $entradas = $factory->find(Entrada::class)->array(); - if ($entradas !== null) { - usort($entradas, function($a, $b) { + $transacciones = $factory->find(Transaccion::class)->array(); + if ($transacciones !== null) { + usort($transacciones, function($a, $b) { $d = $a['fecha'] - $b['fecha']; if ($d === 0) { return strcmp($a['cuenta']['nombre'], $b['cuenta']['nombre']); @@ -22,15 +22,15 @@ class Entradas { }); } $output = [ - 'entradas' => $entradas + 'transacciones' => $transacciones ]; return $this->withJson($response, $output); } - public function show(Request $request, Response $response, Factory $factory, $entrada_id): Response { - $entrada = $factory->find(Entrada::class)->one($entrada_id); + public function show(Request $request, Response $response, Factory $factory, $transaccion_id): Response { + $transaccion = $factory->find(Transaccion::class)->one($transaccion_id); $output = [ - 'input' => $entrada_id, - 'entrada' => $entrada?->toArray() + 'input' => $transaccion_id, + 'transaccion' => $transaccion?->toArray() ]; return $this->withJson($response, $output); } @@ -39,36 +39,36 @@ class Entradas { $results = []; if (is_array($input)) { foreach ($input as $in) { - $entrada = Entrada::add($factory, $in); - $results []= ['entrada' => $entrada?->toArray(), 'agregado' => $entrada?->save()]; + $transaccion = Transaccion::add($factory, $in); + $results []= ['transaccion' => $transaccion?->toArray(), 'agregado' => $transaccion?->save()]; } } else { - $entrada = Entrada::add($factory, $input); - $results []= ['entrada' => $entrada?->toArray(), 'agregado' => $entrada?->save()]; + $transaccion = Transaccion::add($factory, $input); + $results []= ['transaccion' => $transaccion?->toArray(), 'agregado' => $transaccion?->save()]; } $output = [ 'input' => $input, - 'entradas' => $results + 'transacciones' => $results ]; return $this->withJson($response, $output); } - public function edit(Request $request, Response $response, Factory $factory, $entrada_id): Response { - $entrada = $factory->find(Entrada::class)->one($entrada_id); + public function edit(Request $request, Response $response, Factory $factory, $transaccion_id): Response { + $transaccion = $factory->find(Transaccion::class)->one($transaccion_id); $output = [ - 'input' => $entrada_id, - 'old' => $entrada->toArray() + 'input' => $transaccion_id, + 'old' => $transaccion->toArray() ]; $input = json_decode($request->getBody()); - $entrada->edit($input); - $output['entrada'] = $entrada->toArray(); + $transaccion->edit($input); + $output['transaccion'] = $transaccion->toArray(); return $this->withJson($response, $output); } - public function delete(Request $request, Response $response, Factory $factory, $entrada_id): Response { - $entrada = $factory->find(Entrada::class)->one($entrada_id); + public function delete(Request $request, Response $response, Factory $factory, $transaccion_id): Response { + $transaccion = $factory->find(Transaccion::class)->one($transaccion_id); $output = [ - 'input' => $entrada_id, - 'entrada' => $entrada->toArray(), - 'eliminado' => $entrada->delete() + 'input' => $transaccion_id, + 'transaccion' => $transaccion->toArray(), + 'eliminado' => $transaccion->delete() ]; return $this->withJson($response, $output); } diff --git a/api/resources/routes/bancos.php b/api/resources/routes/bancos.php deleted file mode 100644 index ec73cc6..0000000 --- a/api/resources/routes/bancos.php +++ /dev/null @@ -1,12 +0,0 @@ -group('/bancos', function($app) { - $app->post('/add[/]', [Bancos::class, 'add']); - $app->get('[/]', Bancos::class); -}); -$app->group('/banco/{banco_id}', function($app) { - $app->put('/edit', [Bancos::class, 'edit']); - $app->delete('/delete', [Bancos::class, 'delete']); - $app->get('[/]', [Bancos::class, 'show']); -}); diff --git a/api/resources/routes/cuentas.php b/api/resources/routes/cuentas.php index b68375e..6e93ebb 100644 --- a/api/resources/routes/cuentas.php +++ b/api/resources/routes/cuentas.php @@ -7,6 +7,7 @@ $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->put('/edit', [Cuentas::class, 'edit']); $app->delete('/delete', [Cuentas::class, 'delete']); $app->get('[/]', [Cuentas::class, 'show']); diff --git a/api/resources/routes/entradas.php b/api/resources/routes/entradas.php deleted file mode 100644 index aa41b03..0000000 --- a/api/resources/routes/entradas.php +++ /dev/null @@ -1,12 +0,0 @@ -group('/entradas', function($app) { - $app->post('/add[/]', [Entradas::class, 'add']); - $app->get('[/]', Entradas::class); -}); -$app->group('/entrada/{entrada_id}', function($app) { - $app->put('/edit', [Entradas::class, 'edit']); - $app->delete('/delete', [Entradas::class, 'delete']); - $app->get('[/]', [Entradas::class, 'show']); -}); diff --git a/api/resources/routes/fuentes.php b/api/resources/routes/fuentes.php deleted file mode 100644 index 20c2a94..0000000 --- a/api/resources/routes/fuentes.php +++ /dev/null @@ -1,13 +0,0 @@ -group('/fuentes', function($app) { - $app->post('/add[/]', [Fuentes::class, 'add']); - $app->get('[/]', Fuentes::class); -}); -$app->group('/fuente/{fuente_id}', function($app) { - $app->get('/entradas', [Fuentes::class, 'entradas']); - $app->put('/edit', [Fuentes::class, 'edit']); - $app->delete('/delete', [Fuentes::class, 'delete']); - $app->get('[/]', [Fuentes::class, 'show']); -}); diff --git a/api/resources/routes/tipos_fuentes.php b/api/resources/routes/tipos_fuentes.php deleted file mode 100644 index 5af20ae..0000000 --- a/api/resources/routes/tipos_fuentes.php +++ /dev/null @@ -1,13 +0,0 @@ -group('/tipos_fuentes', function($app) { - $app->post('/add[/]', [TiposFuentes::class, 'add']); - $app->get('[/]', TiposFuentes::class); -}); -$app->group('/tipo_fuente/{tipo_fuente_id}', function($app) { - $app->get('/fuentes', [TiposFuentes::class, 'fuentes']); - $app->put('/edit', [TiposFuentes::class, 'edit']); - $app->delete('/delete', [TiposFuentes::class, 'delete']); - $app->get('[/]', [TiposFuentes::class, 'show']); -}); diff --git a/api/resources/routes/transacciones.php b/api/resources/routes/transacciones.php new file mode 100644 index 0000000..53b73c6 --- /dev/null +++ b/api/resources/routes/transacciones.php @@ -0,0 +1,12 @@ +group('/transacciones', function($app) { + $app->post('/add[/]', [Transacciones::class, 'add']); + $app->get('[/]', Transacciones::class); +}); +$app->group('/transaccion/{transaccion_id}', function($app) { + $app->put('/edit', [Transacciones::class, 'edit']); + $app->delete('/delete', [Transacciones::class, 'delete']); + $app->get('[/]', [Transacciones::class, 'show']); +}); diff --git a/api/src/Banco.php b/api/src/Banco.php deleted file mode 100644 index 971ce42..0000000 --- a/api/src/Banco.php +++ /dev/null @@ -1,13 +0,0 @@ -cuentas; } + + protected $saldo; + public function saldo() { + if ($this->saldo === null) { + $this->saldo = 0; + if ($this->cuentas() !== null) { + $this->saldo = array_reduce($this->cuentas(), function($sum, $item) { + return $sum + $item->saldo(); + }); + } + } + return $this->saldo; + } + + public function toArray(): array { + $arr = parent::toArray(); + $arr['saldo'] = $this->saldo(); + $arr['saldoFormateado'] = '$' . number_format($this->saldo(), 0, ',', '.'); + return $arr; + } } diff --git a/api/src/Cuenta.php b/api/src/Cuenta.php index bf14878..66d809d 100644 --- a/api/src/Cuenta.php +++ b/api/src/Cuenta.php @@ -28,9 +28,61 @@ class Cuenta extends Model { return $this->entradas; } + protected $cargos; + public function cargos() { + if ($this->cargos === null) { + $this->cargos = $this->parentOf(Transaccion::class, [Model::CHILD_KEY => 'hasta_id']); + } + return $this->cargos; + } + protected $abonos; + public function abonos() { + if ($this->abonos === null) { + $this->abonos = $this->parentOf(Transaccion::class, [Model::CHILD_KEY => 'desde_id']); + } + return $this->abonos; + } + protected $transacciones; + public function transacciones() { + if ($this->transacciones === null) { + $this->transacciones = []; + if ($this->abonos() !== null) { + $this->transacciones = array_merge($this->transacciones, $this->abonos()); + } + 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; + } + return $d; + }); + } + return $this->transacciones; + } + protected $saldo; + public function saldo() { + if ($this->saldo === null) { + $this->saldo = 0; + if (count($this->transacciones()) > 0) { + $this->saldo = array_reduce($this->transacciones(), function($sum, $item) { + return $sum + $item->valor; + }); + } + } + return $this->saldo; + } + public function toArray(): array { $arr = parent::toArray(); $arr['categoria'] = $this->categoria()->toArray(); + $arr['saldo'] = $this->saldo(); + $arr['saldoFormateado'] = '$' . number_format($this->saldo(), 0, ',', '.'); return $arr; } } diff --git a/api/src/Entrada.php b/api/src/Entrada.php deleted file mode 100644 index 48b3196..0000000 --- a/api/src/Entrada.php +++ /dev/null @@ -1,49 +0,0 @@ -fuente === null) { - $this->fuente = $this->childOf(Fuente::class, [Model::SELF_KEY => 'fuente_id']); - } - return $this->fuente; - } - protected $cuenta; - public function cuenta() { - if ($this->cuenta === null) { - $this->cuenta = $this->childOf(Cuenta::class, [Model::SELF_KEY => 'cuenta_id']); - } - return $this->cuenta; - } - public function fecha(\DateTime $fecha = null) { - if ($fecha === null) { - return Carbon::parse($this->fecha); - } - $this->fecha = $fecha->format('Y-m-d'); - } - - public function toArray(): array { - $arr = parent::toArray(); - $arr['fuente'] = $this->fuente()->toArray(); - $arr['cuenta'] = $this->cuenta()->toArray(); - $arr['fechaFormateada'] = $this->fecha()->format('d-m-Y'); - $arr['valorFormateado'] = '$' . number_format($this->valor, 0, ',', '.'); - return $arr; - } -} diff --git a/api/src/Fuente.php b/api/src/Fuente.php deleted file mode 100644 index 17869bf..0000000 --- a/api/src/Fuente.php +++ /dev/null @@ -1,58 +0,0 @@ -tipo === null) { - $this->tipo = $this->childOf(TipoFuente::class, [Model::SELF_KEY => 'tipo_id']); - } - return $this->tipo; - } - protected $banco; - public function banco() { - if ($this->banco === null) { - $this->banco = $this->childOf(Banco::class, [Model::SELF_KEY => 'banco_id']); - } - return $this->banco; - } - protected $saldo; - public function saldo() { - if ($this->saldo === null) { - $this->saldo = 0; - if ($this->entradas() !== null) { - $this->saldo = array_reduce($this->entradas(), function($sum, $item) { - return $sum + $item->valor; - }); - } - } - return $this->saldo; - } - - protected $entradas; - public function entradas() { - if ($this->entradas === null) { - $this->entradas = $this->parentOf(Entrada::class, [Model::CHILD_KEY => 'fuente_id']); - } - return $this->entradas; - } - - public function toArray(): array { - $arr = parent::toArray(); - $arr['tipo'] = $this->tipo()->toArray(); - $arr['banco'] = $this->banco()->toArray(); - $arr['saldo'] = $this->saldo(); - $arr['saldoFormateado'] = '$' . number_format($this->saldo(), 0, ',', '.'); - return $arr; - } -} diff --git a/api/src/TipoFuente.php b/api/src/TipoFuente.php deleted file mode 100644 index 8397cf8..0000000 --- a/api/src/TipoFuente.php +++ /dev/null @@ -1,21 +0,0 @@ -fuentes === null) { - $this->fuentes = $this->parentOf(Fuente::class, [Model::CHILD_KEY => 'tipo_id']); - } - return $this->fuentes; - } -} diff --git a/api/src/Transaccion.php b/api/src/Transaccion.php new file mode 100644 index 0000000..162005b --- /dev/null +++ b/api/src/Transaccion.php @@ -0,0 +1,49 @@ +desde === null) { + $this->desde = $this->childOf(Cuenta::class, [Model::SELF_KEY => 'desde_id']); + } + return $this->desde; + } + protected $hasta; + public function hasta() { + if ($this->hasta === null) { + $this->hasta = $this->childOf(Cuenta::class, [Model::SELF_KEY => 'hasta_id']); + } + return $this->hasta; + } + public function fecha(\DateTime $fecha = null) { + if ($fecha === null) { + return Carbon::parse($this->fecha); + } + $this->fecha = $fecha->format('Y-m-d'); + } + + public function toArray(): array { + $arr = parent::toArray(); + $arr['desde'] = $this->desde()->toArray(); + $arr['hasta'] = $this->hasta()->toArray(); + $arr['fechaFormateada'] = $this->fecha()->format('d-m-Y'); + $arr['valorFormateado'] = '$' . number_format($this->valor, 0, ',', '.'); + return $arr; + } +}