159 lines
5.8 KiB
PHP
159 lines
5.8 KiB
PHP
<?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\Common\Service\TiposCambios as Service;
|
|
use Contabilidad\Cuenta;
|
|
|
|
class Cuentas {
|
|
use Json;
|
|
|
|
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
|
$cuentas = $factory->find(Cuenta::class)->many();
|
|
if ($cuentas) {
|
|
array_walk($cuentas, function (&$item) {
|
|
$arr = $item->toArray();
|
|
$arr['categoria'] = $item->categoria()->toArray();
|
|
$item = $arr;
|
|
});
|
|
usort($cuentas, function($a, $b) {
|
|
$t = strcmp($a['tipo']['descripcion'], $b['tipo']['descripcion']);
|
|
if ($t != 0) {
|
|
return $t;
|
|
}
|
|
$c = strcmp($a['categoria']['nombre'], $b['categoria']['nombre']);
|
|
if ($c != 0) {
|
|
return $c;
|
|
}
|
|
return strcmp($a['nombre'], $b['nombre']);
|
|
});
|
|
}
|
|
$output = [
|
|
'cuentas' => $cuentas
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function show(Request $request, Response $response, Factory $factory, $cuenta_id): Response {
|
|
$cuenta = $factory->find(Cuenta::class)->one($cuenta_id);
|
|
$output = [
|
|
'input' => $cuenta_id,
|
|
'cuenta' => $cuenta?->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) {
|
|
$cuenta = Cuenta::add($factory, $in);
|
|
$results []= ['cuenta' => $cuenta?->toArray(), 'agregado' => $cuenta?->save()];
|
|
}
|
|
} else {
|
|
$cuenta = Cuenta::add($factory, $input);
|
|
$results []= ['cuenta' => $cuenta?->toArray(), 'agregado' => $cuenta?->save()];
|
|
}
|
|
$output = [
|
|
'input' => $input,
|
|
'cuentas' => $results
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function edit(Request $request, Response $response, Factory $factory, $cuenta_id): Response {
|
|
$cuenta = $factory->find(Cuenta::class)->one($cuenta_id);
|
|
$output = [
|
|
'input' => $cuenta_id,
|
|
'old' => $cuenta->toArray()
|
|
];
|
|
$input = json_decode($request->getBody());
|
|
$cuenta->edit($input);
|
|
$output['cuenta'] = $cuenta->toArray();
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function delete(Request $request, Response $response, Factory $factory, $cuenta_id): Response {
|
|
$cuenta = $factory->find(Cuenta::class)->one($cuenta_id);
|
|
$output = [
|
|
'input' => $cuenta_id,
|
|
'cuenta' => $cuenta->toArray(),
|
|
'eliminado' => $cuenta->delete()
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function categoria(Request $request, Response $response, Factory $factory, $cuenta_id): Response {
|
|
$cuenta = $factory->find(Cuenta::class)->one($cuenta_id);
|
|
$output = [
|
|
'input' => $cuenta_id,
|
|
'cuenta' => $cuenta?->toArray(),
|
|
'categoria' => $cuenta?->categoria()->toArray()
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function entradas(Request $request, Response $response, Factory $factory, $cuenta_id): Response {
|
|
$cuenta = $factory->find(Cuenta::class)->one($cuenta_id);
|
|
$entradas = null;
|
|
if ($cuenta !== null) {
|
|
$entradas = $cuenta->entradas();
|
|
if ($entradas !== null) {
|
|
array_walk($entradas, function(&$item) {
|
|
$item = $item->toArray();
|
|
});
|
|
}
|
|
}
|
|
$output = [
|
|
'input' => $cuenta_id,
|
|
'cuenta' => $cuenta?->toArray(),
|
|
'entradas' => $entradas
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
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;
|
|
if ($cuenta !== null) {
|
|
$transacciones = $cuenta->transacciones($limit, $start);
|
|
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 = $arr;
|
|
}
|
|
}
|
|
}
|
|
$output = [
|
|
'input' => $cuenta_id,
|
|
'cuenta' => $cuenta?->toArray(),
|
|
'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);
|
|
}
|
|
}
|