Show one month at a time, and change month with calendar and buttons

This commit is contained in:
2022-03-18 17:50:21 -03:00
parent e5cf3cfa07
commit 2b3b475d91
8 changed files with 309 additions and 29 deletions

View File

@ -1,8 +1,10 @@
<?php
namespace Contabilidad\Common\Controller;
use Contabilidad\Transaccion;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Carbon\Carbon;
use ProVM\Common\Define\Controller\Json;
use ProVM\Common\Factory\Model as Factory;
use Contabilidad\Common\Service\TiposCambios as Service;
@ -109,6 +111,24 @@ class Cuentas {
];
return $this->withJson($response, $output);
}
protected function transaccionToArray(Service $service, Cuenta $cuenta, Transaccion $transaccion): array {
$arr = $transaccion->toArray();
if ($cuenta->moneda()->codigo === 'CLP') {
if ($transaccion->debito()->moneda()->codigo !== 'CLP' or $transaccion->credito()->moneda()->codigo !== 'CLP') {
if ($transaccion->debito()->moneda()->codigo !== 'CLP') {
$c = $transaccion->debito();
} else {
$c = $transaccion->credito();
}
$service->get($transaccion->fecha(), $c->moneda()->id);
$arr['valor'] = $c->moneda()->cambiar($transaccion->fecha(), $transaccion->valor);
$arr['valorFormateado'] = $cuenta->moneda()->format($arr['valor']);
}
}
$arr['debito']['categoria'] = $transaccion->debito()->categoria()->toArray();
$arr['credito']['categoria'] = $transaccion->credito()->categoria()->toArray();
return $arr;
}
public function transacciones(Request $request, Response $response, Factory $factory, Service $service, $cuenta_id, $limit = null, $start = 0): Response {
$cuenta = $factory->find(Cuenta::class)->one($cuenta_id);
$transacciones = null;
@ -116,7 +136,7 @@ class Cuentas {
$transacciones = $cuenta->transacciones($limit, $start);
if (count($transacciones) > 0) {
foreach ($transacciones as &$transaccion) {
$arr = $transaccion->toArray();
/*$arr = $transaccion->toArray();
if ($cuenta->moneda()->codigo === 'CLP') {
if ($transaccion->debito()->moneda()->codigo !== 'CLP' or $transaccion->credito()->moneda()->codigo !== 'CLP') {
if ($transaccion->debito()->moneda()->codigo !== 'CLP') {
@ -130,8 +150,8 @@ class Cuentas {
}
}
$arr['debito']['categoria'] = $transaccion->debito()->categoria()->toArray();
$arr['credito']['categoria'] = $transaccion->credito()->categoria()->toArray();
$transaccion = $arr;
$arr['credito']['categoria'] = $transaccion->credito()->categoria()->toArray();*/
$transaccion = $this->transaccionToArray($service, $cuenta, $transaccion);
}
}
}
@ -142,6 +162,55 @@ class Cuentas {
];
return $this->withJson($response, $output);
}
public function transaccionesMonth(Request $request, Response $response, Factory $factory, Service $service, $cuenta_id, $month): Response {
$cuenta = $factory->find(Cuenta::class)->one($cuenta_id);
$month = Carbon::parse($month);
$transacciones = null;
if ($cuenta !== null) {
$transacciones = $cuenta->transaccionesMonth($month);
if (count($transacciones) > 0) {
foreach ($transacciones as &$transaccion) {
/*$arr = $transaccion->toArray();
if ($cuenta->moneda()->codigo === 'CLP') {
if ($transaccion->debito()->moneda()->codigo !== 'CLP' or $transaccion->credito()->moneda()->codigo !== 'CLP') {
if ($transaccion->debito()->moneda()->codigo !== 'CLP') {
$c = $transaccion->debito();
} else {
$c = $transaccion->credito();
}
$service->get($transaccion->fecha(), $c->moneda()->id);
$arr['valor'] = $c->moneda()->cambiar($transaccion->fecha(), $transaccion->valor);
$arr['valorFormateado'] = $cuenta->moneda()->format($arr['valor']);
}
}
$arr['debito']['categoria'] = $transaccion->debito()->categoria()->toArray();
$arr['credito']['categoria'] = $transaccion->credito()->categoria()->toArray();*/
$transaccion = $this->transaccionToArray($service, $cuenta, $transaccion);
}
}
}
$output = [
'input' => compact('cuenta_id', 'month'),
'cuenta' => $cuenta?->toArray(),
'transacciones' => $transacciones
];
return $this->withJson($response, $output);
}
public function transaccionesAcumulation(Request $request, Response $response, Factory $factory, $cuenta_id, $date): Response {
$cuenta = $factory->find(Cuenta::class)->one($cuenta_id);
$f = Carbon::parse($date);
$acum = 0;
if ($cuenta !== null) {
$acum = $cuenta->acumulacion($f);
}
$output = [
'input' => compact('cuenta_id', 'date'),
'cuenta' => $cuenta?->toArray(),
'format' => $cuenta->moneda()->toArray(),
'acumulation' => $acum
];
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;

View File

@ -9,6 +9,8 @@ $app->group('/cuenta/{cuenta_id}', function($app) {
$app->get('/entradas', [Cuentas::class, 'entradas']);
$app->group('/transacciones', function($app) {
$app->get('/amount', [Cuentas::class, 'transaccionesAmount']);
$app->get('/month/{month}', [Cuentas::class, 'transaccionesMonth']);
$app->get('/acum/{date}', [Cuentas::class, 'transaccionesAcumulation']);
$app->get('[/{limit:[0-9]+}[/{start:[0-9]+}]]', [Cuentas::class, 'transacciones']);
});
$app->get('/categoria', [Cuentas::class, 'categoria']);

View File

@ -1,7 +1,9 @@
<?php
namespace Contabilidad;
use DateTimeInterface;
use Carbon\Carbon;
use PhpParser\Node\Expr\AssignOp\Mod;
use ProVM\Common\Alias\Model;
use Contabilidad\Common\Service\TiposCambios as Service;
@ -54,7 +56,6 @@ class Cuenta extends Model {
}
protected $transacciones;
public function transacciones($limit = null, $start = 0) {
if ($this->transacciones === null) {
$transacciones = Model::factory(Transaccion::class)
->select('transacciones.*')
->join('cuentas', 'cuentas.id = transacciones.debito_id OR cuentas.id = transacciones.credito_id')
@ -64,15 +65,52 @@ class Cuenta extends Model {
$transacciones = $transacciones->limit($limit)
->offset($start);
}
$this->transacciones = $transacciones->findMany();
foreach ($this->transacciones as &$transaccion) {
$transacciones = $transacciones->findMany();
foreach ($transacciones as &$transaccion) {
$transaccion->setFactory($this->factory);
if ($transaccion->desde_id === $this->id) {
if ($transaccion->debito_id === $this->id) {
$transaccion->valor = - $transaccion->valor;
}
}
}
return $this->transacciones;
return $transacciones;
}
public function transaccionesMonth(Carbon $month) {
$start = $month->copy()->startOfMonth();
$end = $month->copy()->endOfMonth();
$transacciones = Model::factory(Transaccion::class)
->select('transacciones.*')
->join('cuentas', 'cuentas.id = transacciones.debito_id OR cuentas.id = transacciones.credito_id')
->whereEqual('cuentas.id', $this->id)
->whereRaw("transacciones.fecha BETWEEN '{$start->format('Y-m-d')}' AND '{$end->format('Y-m-d')}'")
->orderByAsc('transacciones.fecha');
$transacciones = $transacciones->findMany();
foreach ($transacciones as &$transaccion) {
$transaccion->setFactory($this->factory);
if ($transaccion->desde_id === $this->id) {
$transaccion->valor = - $transaccion->valor;
}
}
return $transacciones;
}
public function acumulacion(Carbon $date) {
$abonos = Model::factory(Transaccion::class)
->whereEqual('credito_id', $this->id)
->whereLt('fecha', $date->format('Y-m-d'))
->groupBy('credito_id')
->sum('valor');
$cargos = Model::factory(Transaccion::class)
->whereEqual('debito_id', $this->id)
->whereLt('fecha', $date->format('Y-m-d'))
->groupBy('debito_id')
->sum('valor');
if (in_array($this->tipo()->descripcion, ['activo', 'banco', 'perdida'])) {
return $abonos - $cargos;
}
return $cargos - $abonos;
}
protected $saldo;
public function saldo(Service $service = null, $in_clp = false) {

View File

@ -16,11 +16,11 @@ class Moneda extends Model {
protected static $fields = ['denominacion', 'codigo'];
public function format($valor) {
return implode('', [
return trim(implode('', [
$this->prefijo,
number_format($valor, $this->decimales, ',', '.'),
$this->sufijo
]);
]));
}
public function cambio(\DateTime $fecha) {
$cambio = $this->factory->find(TipoCambio::class)
@ -43,4 +43,14 @@ class Moneda extends Model {
}
return $cambio->transform($valor);
}
public function toArray(): array {
$arr = parent::toArray();
$arr['format'] = [
'prefijo' => $this->prefijo,
'sufijo' => $this->sufijo,
'decimales' => $this->decimales
];
return $arr;
}
}