85 lines
2.8 KiB
PHP
85 lines
2.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\Fuente;
|
|
|
|
class Fuentes {
|
|
use Json;
|
|
|
|
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
|
$fuentes = $factory->find(Fuente::class)->array();
|
|
$output = [
|
|
'fuentes' => $fuentes
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function show(Request $request, Response $response, Factory $factory, $fuente_id): Response {
|
|
$fuente = $factory->find(Fuente::class)->one($fuente_id);
|
|
$output = [
|
|
'input' => $fuente_id,
|
|
'fuente' => $fuente?->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) {
|
|
$fuente = Fuente::add($factory, $in);
|
|
$results []= ['fuente' => $fuente?->toArray(), 'agregado' => $fuente?->save()];
|
|
}
|
|
} else {
|
|
$fuente = Fuente::add($factory, $input);
|
|
$results []= ['fuente' => $fuente?->toArray(), 'agregado' => $fuente?->save()];
|
|
}
|
|
$output = [
|
|
'input' => $input,
|
|
'fuentes' => $results
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function edit(Request $request, Response $response, Factory $factory, $fuente_id): Response {
|
|
$fuente = $factory->find(Fuente::class)->one($fuente_id);
|
|
$output = [
|
|
'input' => $fuente_id,
|
|
'old' => $fuente->toArray()
|
|
];
|
|
$input = json_decode($request->getBody());
|
|
$fuente->edit($input);
|
|
$output['fuente'] = $fuente->toArray();
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function delete(Request $request, Response $response, Factory $factory, $fuente_id): Response {
|
|
$fuente = $factory->find(Fuente::class)->one($fuente_id);
|
|
$output = [
|
|
'input' => $fuente_id,
|
|
'fuente' => $fuente->toArray(),
|
|
'eliminado' => $fuente->delete()
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function entradas(Request $request, Response $response, Factory $factory, $fuente_id): Response {
|
|
$fuente = $factory->find(Fuente::class)->one($fuente_id);
|
|
$entradas = null;
|
|
if ($fuente !== null) {
|
|
$entradas = $fuente->entradas();
|
|
if ($entradas !== null) {
|
|
array_walk($entradas, function(&$item) {
|
|
$item = $item->toArray();
|
|
});
|
|
}
|
|
}
|
|
$output = [
|
|
'input' => $fuente_id,
|
|
'fuente' => $fuente?->toArray(),
|
|
'entradas' => $entradas
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
}
|