Cambios al api revisado en UI

This commit is contained in:
2021-07-29 22:05:38 -04:00
parent e338e39e75
commit 4374392af1
5 changed files with 59 additions and 2 deletions

View File

@ -12,6 +12,15 @@ class Entradas {
public function __invoke(Request $request, Response $response, Factory $factory): Response {
$entradas = $factory->find(Entrada::class)->array();
if ($entradas !== null) {
usort($entradas, function($a, $b) {
$d = $a['fecha'] - $b['fecha'];
if ($d === 0) {
return strcmp($a['cuenta']['nombre'], $b['cuenta']['nombre']);
}
return $d;
});
}
$output = [
'entradas' => $entradas
];

View File

@ -69,6 +69,13 @@ class Fuentes {
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();
});

View File

@ -10,6 +10,22 @@ server {
}
location ~ \.php$ {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,
X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
add_header 'Content-Type' 'application/json';
add_header 'Content-Length' 0;
return 204;
}
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,
X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass api:9000;

View File

@ -8,12 +8,14 @@ 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', 'cuenta_id', 'value'];
protected static $fields = ['fecha', 'fuente_id', 'glosa', 'detalle', 'cuenta_id', 'valor'];
protected $fuente;
public function fuente() {
@ -25,7 +27,7 @@ class Entrada extends Model {
protected $cuenta;
public function cuenta() {
if ($this->cuenta === null) {
$this->cuenta = $this->childOf(Cuente::class, [Model::SELF_KEY => 'cuenta_id']);
$this->cuenta = $this->childOf(Cuenta::class, [Model::SELF_KEY => 'cuenta_id']);
}
return $this->cuenta;
}
@ -35,4 +37,13 @@ class Entrada extends Model {
}
$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

@ -26,6 +26,18 @@ class Fuente extends Model {
}
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() {
@ -39,6 +51,8 @@ class Fuente extends Model {
$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;
}
}