API 1.0.0-rc
This commit is contained in:
93
api/common/Controller/Cuentas.php
Normal file
93
api/common/Controller/Cuentas.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user