2021-07-27 22:29:56 -04:00
|
|
|
<?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\Cuenta;
|
|
|
|
|
|
|
|
class Cuentas {
|
|
|
|
use Json;
|
|
|
|
|
|
|
|
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
|
|
|
$cuentas = $factory->find(Cuenta::class)->array();
|
|
|
|
if ($cuentas) {
|
|
|
|
usort($cuentas, function($a, $b) {
|
|
|
|
$c = strcmp($a['categoria']['nombre'], $b['categoria']['nombre']);
|
|
|
|
if ($c == 0) {
|
|
|
|
return strcmp($a['nombre'], $b['nombre']);
|
|
|
|
}
|
|
|
|
return $c;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
$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 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);
|
|
|
|
}
|
2021-07-30 16:38:09 -04:00
|
|
|
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);
|
|
|
|
}
|
2021-07-27 22:29:56 -04:00
|
|
|
}
|