72 lines
2.3 KiB
PHP
72 lines
2.3 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\TipoCuenta;
|
|
|
|
class TiposCuentas {
|
|
use Json;
|
|
|
|
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
|
$tipos = $factory->find(TipoCuenta::class)->array();
|
|
if ($tipos) {
|
|
usort($tipos, function($a, $b) {
|
|
return strcmp($a['descripcion'], $b['descripcion']);
|
|
});
|
|
}
|
|
$output = [
|
|
'tipos' => $tipos
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function show(Request $request, Response $response, Factory $factory, $tipo_id): Response {
|
|
$tipo = $factory->find(TipoCuenta::class)->one($tipo_id);
|
|
$output = [
|
|
'input' => $tipo_id,
|
|
'tipo' => $tipo?->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) {
|
|
$tipo = TipoCuenta::add($factory, $in);
|
|
$results []= ['tipo' => $tipo?->toArray(), 'agregado' => $tipo?->save()];
|
|
}
|
|
} else {
|
|
$tipo = TipoCuenta::add($factory, $input);
|
|
$results []= ['tipo' => $tipo?->toArray(), 'agregado' => $tipo?->save()];
|
|
}
|
|
$output = [
|
|
'input' => $input,
|
|
'tipos' => $results
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function edit(Request $request, Response $response, Factory $factory, $tipo_id): Response {
|
|
$tipo = $factory->find(TipoCuenta::class)->one($tipo_id);
|
|
$output = [
|
|
'input' => $tipo_id,
|
|
'old' => $tipo->toArray()
|
|
];
|
|
$input = json_decode($request->getBody());
|
|
$tipo->edit($input);
|
|
$output['tipo'] = $tipo->toArray();
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function delete(Request $request, Response $response, Factory $factory, $tipo_id): Response {
|
|
$tipo = $factory->find(TipoCuenta::class)->one($tipo_id);
|
|
$output = [
|
|
'input' => $tipo_id,
|
|
'tipo' => $tipo->toArray(),
|
|
'eliminado' => $tipo->delete()
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
}
|