Simplificacion a contabilidad clasica

This commit is contained in:
2021-07-30 16:38:09 -04:00
parent 510b5aa46d
commit 69b823750a
18 changed files with 194 additions and 468 deletions

View File

@ -1,71 +0,0 @@
<?php
namespace Contabilidad\Common\Controller;
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\Banco;
class Bancos {
use Json;
public function __invoke(Request $request, Response $response, Factory $factory): Response {
$bancos = $factory->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);
}
}

View File

@ -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);
}
}

View File

@ -1,91 +0,0 @@
<?php
namespace Contabilidad\Common\Controller;
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\Fuente;
class Fuentes {
use Json;
public function __invoke(Request $request, Response $response, Factory $factory): Response {
$fuentes = $factory->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);
}
}

View File

@ -1,89 +0,0 @@
<?php
namespace Contabilidad\Common\Controller;
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\TipoFuente;
class TiposFuentes {
use Json;
public function __invoke(Request $request, Response $response, Factory $factory): Response {
$tipos_fuentes = $factory->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);
}
}

View File

@ -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);
}

View File

@ -1,12 +0,0 @@
<?php
use Contabilidad\Common\Controller\Bancos;
$app->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']);
});

View File

@ -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']);

View File

@ -1,12 +0,0 @@
<?php
use Contabilidad\Common\Controller\Entradas;
$app->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']);
});

View File

@ -1,13 +0,0 @@
<?php
use Contabilidad\Common\Controller\Fuentes;
$app->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']);
});

View File

@ -1,13 +0,0 @@
<?php
use Contabilidad\Common\Controller\TiposFuentes;
$app->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']);
});

View File

@ -0,0 +1,12 @@
<?php
use Contabilidad\Common\Controller\Transacciones;
$app->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']);
});

View File

@ -1,13 +0,0 @@
<?php
namespace Contabilidad;
use ProVM\Common\Alias\Model;
/**
* @property int $id
* @property string $nombre
*/
class Banco extends Model {
public static $_table = 'bancos';
protected static $fields = ['nombre'];
}

View File

@ -18,4 +18,24 @@ class Categoria extends Model {
}
return $this->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;
}
}

View File

@ -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;
}
}

View File

@ -1,49 +0,0 @@
<?php
namespace Contabilidad;
use Carbon\Carbon;
use ProVM\Common\Alias\Model;
/**
* @property int $id
* @property \DateTime $fecha
* @property Fuente $fuente_id
* @property string $glosa
* @property string $detalle
* @property Cuenta $cuenta_id
* @property double $valor
*/
class Entrada extends Model {
public static $_table = 'entradas';
protected static $fields = ['fecha', 'fuente_id', 'glosa', 'detalle', 'cuenta_id', 'valor'];
protected $fuente;
public function fuente() {
if ($this->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;
}
}

View File

@ -1,58 +0,0 @@
<?php
namespace Contabilidad;
use ProVM\Common\Alias\Model;
/**
* @property int $id
* @property TipoFuente $tipo_id
* @property Banco $banco_id
*/
class Fuente extends Model {
public static $_table = 'fuentes';
protected static $fields = ['tipo_id', 'banco_id'];
protected $tipo;
public function tipo() {
if ($this->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;
}
}

View File

@ -1,21 +0,0 @@
<?php
namespace Contabilidad;
use ProVM\Common\Alias\Model;
/**
* @property int $id
* @property string $descripcion
*/
class TipoFuente extends Model {
public static $_table = 'tipos_fuente';
protected static $fields = ['descripcion'];
protected $fuentes;
public function fuentes() {
if ($this->fuentes === null) {
$this->fuentes = $this->parentOf(Fuente::class, [Model::CHILD_KEY => 'tipo_id']);
}
return $this->fuentes;
}
}

49
api/src/Transaccion.php Normal file
View File

@ -0,0 +1,49 @@
<?php
namespace Contabilidad;
use Carbon\Carbon;
use ProVM\Common\Alias\Model;
/**
* @property int $id
* @property Cuenta $desde_id
* @property Cuenta $hasta_id
* @property \DateTime $fecha
* @property string $glosa
* @property string $detalle
* @property double $valor
*/
class Transaccion extends Model {
public static $_table = 'transacciones';
protected static $fields = ['desde_id', 'hasta_id', 'fecha', 'glosa', 'detalle', 'valor'];
protected $desde;
public function desde() {
if ($this->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;
}
}