Version 3.0
New technologies
This commit is contained in:
@ -1,34 +0,0 @@
|
||||
<?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;
|
||||
|
||||
class Base {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response): Response {
|
||||
return $this->withJson($response, []);
|
||||
}
|
||||
public function generate_key(Request $request, Response $response): Response {
|
||||
$server_addr = explode('.', $request->getServerParams()['SERVER_ADDR']);
|
||||
$remote_addr = explode('.', $request->getServerParams()['REMOTE_ADDR']);
|
||||
for ($i = 0; $i < 3; $i ++) {
|
||||
if ($server_addr[$i] != $remote_addr[$i]) {
|
||||
throw new \InvalidArgumentException('Invalid connection address.');
|
||||
}
|
||||
}
|
||||
$salt = mt_rand();
|
||||
$signature = hash_hmac('sha256', $salt, 'contabilidad', true);
|
||||
$key = urlencode(base64_encode($signature));
|
||||
return $this->withJson($response, ['key' => $key]);
|
||||
}
|
||||
public function info(Request $request, Response $response): Response {
|
||||
ob_start();
|
||||
phpinfo();
|
||||
$data = ob_get_clean();
|
||||
$response->getBody()->write($data);
|
||||
return $response;
|
||||
}
|
||||
}
|
@ -1,112 +0,0 @@
|
||||
<?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\Categoria;
|
||||
|
||||
class Categorias {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response, Factory $factory, Service $service): Response {
|
||||
$categorias = $factory->find(Categoria::class)->many();
|
||||
if ($categorias !== null) {
|
||||
array_walk($categorias, function(&$item) use ($service) {
|
||||
$arr = $item->toArray();
|
||||
if ($item->cuentas()) {
|
||||
$arr['cuentas'] = array_map(function($item) {
|
||||
return $item->toArray();
|
||||
}, $item->cuentas());
|
||||
}
|
||||
$maps = ['activo', 'pasivo', 'ganancia', 'perdida'];
|
||||
foreach ($maps as $m) {
|
||||
$p = $m . 's';
|
||||
$t = ucfirst($m);
|
||||
$cuentas = $item->getCuentasOf($t);
|
||||
if ($cuentas === false or $cuentas === null) {
|
||||
$arr[$p] = 0;
|
||||
continue;
|
||||
}
|
||||
$arr[$p] = array_reduce($cuentas, function($sum, $item) use($service) {
|
||||
return $sum + $item->saldo($service, true);
|
||||
});
|
||||
}
|
||||
$item = $arr;
|
||||
});
|
||||
usort($categorias, function($a, $b) {
|
||||
return strcmp($a['nombre'], $b['nombre']);
|
||||
});
|
||||
}
|
||||
$output = [
|
||||
'categorias' => $categorias
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function show(Request $request, Response $response, Factory $factory, $categoria_id): Response {
|
||||
$categoria = $factory->find(Categoria::class)->one($categoria_id);
|
||||
$output = [
|
||||
'input' => $categoria_id,
|
||||
'categoria' => $categoria?->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) {
|
||||
$categoria = Categoria::add($factory, $in);
|
||||
$results []= ['categoria' => $categoria?->toArray(), 'agregado' => $categoria?->save()];
|
||||
}
|
||||
} else {
|
||||
$categoria = Categoria::add($factory, $input);
|
||||
$results []= ['categoria' => $categoria?->toArray(), 'agregado' => $categoria?->save()];
|
||||
}
|
||||
$output = [
|
||||
'input' => $input,
|
||||
'categorias' => $results
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function edit(Request $request, Response $response, Factory $factory, $categoria_id): Response {
|
||||
$categoria = $factory->find(Categoria::class)->one($categoria_id);
|
||||
$output = [
|
||||
'input' => $categoria_id,
|
||||
'old' => $categoria->toArray()
|
||||
];
|
||||
$input = json_decode($request->getBody());
|
||||
$categoria->edit($input);
|
||||
$output['categoria'] = $categoria->toArray();
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function delete(Request $request, Response $response, Factory $factory, $categoria_id): Response {
|
||||
$categoria = $factory->find(Categoria::class)->one($categoria_id);
|
||||
$output = [
|
||||
'input' => $categoria_id,
|
||||
'categoria' => $categoria->toArray(),
|
||||
'eliminado' => $categoria->delete()
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function cuentas(Request $request, Response $response, Factory $factory, Service $service, $categoria_id): Response {
|
||||
$categoria = $factory->find(Categoria::class)->one($categoria_id);
|
||||
$cuentas = null;
|
||||
if ($categoria !== null) {
|
||||
$cuentas = $categoria->cuentas();
|
||||
if ($cuentas !== null) {
|
||||
array_walk($cuentas, function(&$item) use ($service) {
|
||||
$item = $item->toArray($service);
|
||||
});
|
||||
}
|
||||
}
|
||||
$output = [
|
||||
'input' => $categoria_id,
|
||||
'categoria' => $categoria?->toArray(),
|
||||
'cuentas' => $cuentas
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
@ -1,66 +0,0 @@
|
||||
<?php
|
||||
namespace Contabilidad\Common\Controller;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use ProVM\Common\Factory\Model as Factory;
|
||||
use ProVM\Common\Define\Controller\Json;
|
||||
use Contabilidad\Common\Service\Consolidar as Service;
|
||||
use Contabilidad\Cuenta;
|
||||
use Contabilidad\Consolidado;
|
||||
|
||||
class Consolidados {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response, Factory $factory, $cuenta_id): Response {
|
||||
$consolidados = $factory->find(Consolidados::class)->where([['cuenta_id' => $cuenta_id]])->many();
|
||||
$output = [
|
||||
'consolidados' => array_map(function($item) {
|
||||
return $item->toArray();
|
||||
}, $consolidados)
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function add(Request $request, Response $response, Factory $factory): Response {
|
||||
$input = json_decode($request->getBody()->getContents());
|
||||
$output = [
|
||||
'input' => $input,
|
||||
'consolidados' => []
|
||||
];
|
||||
if (!is_array($input)) {
|
||||
$input = [$input];
|
||||
}
|
||||
foreach ($input as $data) {
|
||||
$consolidado = Consolidado::add($factory, $data);
|
||||
$status = $consolidado->save();
|
||||
$output['consolidados'] []= [
|
||||
'consolidado' => $consolidado->toArray(),
|
||||
'added' => $status
|
||||
];
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function cli(Request $request, Response $response, Service $service): Response {
|
||||
try {
|
||||
if (!$service->isConsolidado()) {
|
||||
$service->consolidar();
|
||||
}
|
||||
} catch (\Error | \Exception $e) {
|
||||
error_log($e);
|
||||
throw $e;
|
||||
}
|
||||
return $this->withJson($response, []);
|
||||
}
|
||||
public function update(Request $request, Response $response, Factory $factory, Service $service, $mes, $cuenta_id): Response {
|
||||
try {
|
||||
$cuenta = $factory->find(Cuenta::class)->one($cuenta_id);
|
||||
$mes = Carbon::parse($mes);
|
||||
$service->consolidarCuenta($cuenta, $mes);
|
||||
} catch (\Error | \Exception $e) {
|
||||
error_log($e);
|
||||
throw $e;
|
||||
}
|
||||
return $this->withJson($response, []);
|
||||
}
|
||||
}
|
@ -1,197 +0,0 @@
|
||||
<?php
|
||||
namespace Contabilidad\Common\Controller;
|
||||
|
||||
use Contabilidad\Transaccion;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Carbon\Carbon;
|
||||
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);
|
||||
}
|
||||
protected function transaccionToArray(Service $service, Cuenta $cuenta, Transaccion $transaccion): array {
|
||||
$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'] = $transaccion->valor;
|
||||
$arr['valorFormateado'] = $cuenta->moneda()->format($arr['valor']);
|
||||
}
|
||||
}
|
||||
$arr['debito']['categoria'] = $transaccion->debito()->categoria()->toArray();
|
||||
$arr['credito']['categoria'] = $transaccion->credito()->categoria()->toArray();
|
||||
return $arr;
|
||||
}
|
||||
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) {
|
||||
$transaccion = $this->transaccionToArray($service, $cuenta, $transaccion);
|
||||
}
|
||||
}
|
||||
}
|
||||
$output = [
|
||||
'input' => $cuenta_id,
|
||||
'cuenta' => $cuenta?->toArray(),
|
||||
'transacciones' => $transacciones
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function transaccionesMonth(Request $request, Response $response, Factory $factory, Service $service, $cuenta_id, $month): Response {
|
||||
$cuenta = $factory->find(Cuenta::class)->one($cuenta_id);
|
||||
$month = Carbon::parse($month);
|
||||
$transacciones = null;
|
||||
if ($cuenta !== null) {
|
||||
$transacciones = $cuenta->transaccionesMonth($month);
|
||||
if (count($transacciones) > 0) {
|
||||
foreach ($transacciones as &$transaccion) {
|
||||
$transaccion = $this->transaccionToArray($service, $cuenta, $transaccion);
|
||||
}
|
||||
}
|
||||
}
|
||||
$output = [
|
||||
'input' => compact('cuenta_id', 'month'),
|
||||
'cuenta' => $cuenta?->toArray(),
|
||||
'transacciones' => $transacciones
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function transaccionesAcumulation(Request $request, Response $response, Factory $factory, $cuenta_id, $date): Response {
|
||||
$cuenta = $factory->find(Cuenta::class)->one($cuenta_id);
|
||||
$f = Carbon::parse($date);
|
||||
$acum = 0;
|
||||
if ($cuenta !== null) {
|
||||
$acum = $cuenta->acumulacion($f);
|
||||
}
|
||||
$output = [
|
||||
'input' => compact('cuenta_id', 'date'),
|
||||
'cuenta' => $cuenta?->toArray(),
|
||||
'format' => $cuenta->moneda()->toArray(),
|
||||
'acumulation' => $acum
|
||||
];
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
<?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 Contabilidad\Common\Service\FileHandler as Handler;
|
||||
use ProVM\Common\Factory\Model as Factory;
|
||||
use Contabilidad\Cuenta;
|
||||
|
||||
class Files {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response, Handler $handler): Response {
|
||||
$files = $handler->listFiles();
|
||||
usort($files, function($a, $b) {
|
||||
$f = strcmp($a->folder, $b->folder);
|
||||
if ($f == 0) {
|
||||
return strcmp($a->filename, $b->filename);
|
||||
}
|
||||
return $f;
|
||||
});
|
||||
return $this->withJson($response, compact('files'));
|
||||
}
|
||||
public function upload(Request $request, Response $response, Handler $handler, Factory $factory): Response {
|
||||
$post = $request->getParsedBody();
|
||||
$cuenta = $factory->find(Cuenta::class)->one($post['cuenta']);
|
||||
$file = $request->getUploadedFiles()['archivo'];
|
||||
$new_name = implode(' - ', [$cuenta->nombre, $cuenta->categoria()->nombre, $post['fecha']]);
|
||||
$output = [
|
||||
'input' => [
|
||||
'name' => $file->getClientFilename(),
|
||||
'type' => $file->getClientMediaType(),
|
||||
'size' => $file->getSize(),
|
||||
'error' => $file->getError()
|
||||
],
|
||||
'new_name' => $new_name,
|
||||
'uploaded' => $handler->uploadFile($file, $new_name)
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function get(Request $request, Response $response, Handler $handler, $folder, $filename): Response {
|
||||
$file = $handler->getFile($folder, $filename);
|
||||
return $response
|
||||
->withHeader('Content-Type', $handler->getType($folder))
|
||||
->withHeader('Content-Disposition', 'attachment; filename=' . $filename)
|
||||
->withAddedHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
|
||||
->withHeader('Cache-Control', 'post-check=0, pre-check=0')
|
||||
->withHeader('Pragma', 'no-cache')
|
||||
->withBody($file);
|
||||
}
|
||||
public function edit(Request $request, Response $response, Handler $handler, Factory $factory, $folder, $filename): Response {
|
||||
$post = json_decode($request->getBody());
|
||||
$cuenta = $factory->find(Cuenta::class)->one($post->cuenta);
|
||||
$new_name = implode(' - ', [$cuenta->nombre, $cuenta->categoria()->nombre, $post->fecha]);
|
||||
$output = [
|
||||
'input' => [
|
||||
'folder' => $folder,
|
||||
'filename' => $filename,
|
||||
'post' => $post
|
||||
],
|
||||
'edited' => $handler->editFilename($folder, $filename, $new_name)
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function delete(Request $request, Response $response, Handler $handler, $folder, $filename): Response {
|
||||
$output = [
|
||||
'input' => [
|
||||
'folder' => $folder,
|
||||
'filename' => $filename
|
||||
],
|
||||
'deleted' => $handler->deleteFile($folder, $filename)
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
<?php
|
||||
namespace Contabilidad\Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Container\ContainerInterface as Container;
|
||||
use ProVM\Common\Define\Controller\Json;
|
||||
use ProVM\Common\Factory\Model as Factory;
|
||||
use Contabilidad\Common\Service\DocumentHandler as Handler;
|
||||
use Contabilidad\Cuenta;
|
||||
|
||||
class Import {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response, Factory $factory, Container $container): Response {
|
||||
$post =$request->getParsedBody();
|
||||
$cuenta = $factory->find(Cuenta::class)->one($post['cuenta']);
|
||||
$file = $request->getUploadedFiles()['archivo'];
|
||||
$valid_media = [
|
||||
'text/csv' => 'csvs',
|
||||
'application/pdf' => 'pdfs',
|
||||
'application/vnd.ms-excel' => 'xlss',
|
||||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlss',
|
||||
'application/json' => 'jsons'
|
||||
];
|
||||
if ($file->getError() === 0 and in_array($file->getClientMediaType(), array_keys($valid_media))) {
|
||||
$filenfo = new \SplFileInfo($file->getClientFilename());
|
||||
$new_name = implode('.', [implode(' - ', [$cuenta->nombre, $cuenta->categoria()->nombre, $post['fecha']]), $filenfo->getExtension()]);
|
||||
$to = implode(DIRECTORY_SEPARATOR, [$container->get('folders')->uploads, $valid_media[$file->getClientMediaType()], $new_name]);
|
||||
$file->moveTo($to);
|
||||
$status = file_exists($to);
|
||||
}
|
||||
$output = [
|
||||
'input' => [
|
||||
'name' => $file->getClientFilename(),
|
||||
'type' => $file->getClientMediaType(),
|
||||
'size' => $file->getSize(),
|
||||
'error' => $file->getError()
|
||||
],
|
||||
'new_name' => $new_name,
|
||||
'uploaded' => $status
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function uploads(Request $request, Response $response, Handler $handler): Response {
|
||||
$output = $handler->handle();
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
<?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\Moneda;
|
||||
|
||||
class Monedas {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
||||
$monedas = $factory->find(Moneda::class)->array();
|
||||
if ($monedas) {
|
||||
usort($monedas, function($a, $b) {
|
||||
return strcmp($a['denominacion'], $b['denominacion']);
|
||||
});
|
||||
}
|
||||
$output = [
|
||||
'monedas' => $monedas
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function show(Request $request, Response $response, Factory $factory, $moneda_id): Response {
|
||||
$moneda = $factory->find(Moneda::class)->one($moneda_id);
|
||||
$output = [
|
||||
'input' => $moneda_id,
|
||||
'moneda' => $moneda?->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) {
|
||||
$moneda = Moneda::add($factory, $in);
|
||||
$results []= ['moneda' => $moneda?->toArray(), 'agregado' => $moneda?->save()];
|
||||
}
|
||||
} else {
|
||||
$moneda = Moneda::add($factory, $input);
|
||||
$results []= ['moneda' => $moneda?->toArray(), 'agregado' => $moneda?->save()];
|
||||
}
|
||||
$output = [
|
||||
'input' => $input,
|
||||
'monedas' => $results
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function edit(Request $request, Response $response, Factory $factory, $moneda_id): Response {
|
||||
$moneda = $factory->find(Moneda::class)->one($moneda_id);
|
||||
$output = [
|
||||
'input' => $moneda_id,
|
||||
'old' => $moneda->toArray()
|
||||
];
|
||||
$input = json_decode($request->getBody());
|
||||
$moneda->edit($input);
|
||||
$output['moneda'] = $moneda->toArray();
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function delete(Request $request, Response $response, Factory $factory, $moneda_id): Response {
|
||||
$moneda = $factory->find(Moneda::class)->one($moneda_id);
|
||||
$output = [
|
||||
'input' => $moneda_id,
|
||||
'moneda' => $moneda->toArray(),
|
||||
'eliminado' => $moneda->delete()
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
<?php
|
||||
namespace Contabilidad\Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use ProVM\Common\Factory\Model as Factory;
|
||||
use ProVM\Common\Define\Controller\Json;
|
||||
use Contabilidad\Queue;
|
||||
|
||||
class Queues {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
||||
$queues = $factory->find(Queue::class)->many();
|
||||
$output = [
|
||||
'queues' => array_map(function($item) {return $item->toArray();}, $queues)
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function pending(Request $request, Response $response, Factory $factory): Response {
|
||||
$pending = $factory->find(Queue::class)->where([['processed', 0]])->many();
|
||||
$output = [
|
||||
'pending' => array_map(function($item) {return $item->toArray();}, $pending)
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function processed(Request $request, Response $response, Factory $factory): Response {
|
||||
$input = json_decode($request->getBody()->getContents());
|
||||
$output = [
|
||||
'input' => $input,
|
||||
'queues' => []
|
||||
];
|
||||
if (!is_array($input->processed)) {
|
||||
$input->processed = [$input->processed];
|
||||
}
|
||||
foreach ($input->processed as $id) {
|
||||
$queue = $factory->find(Queue::class)->one($id);
|
||||
$queue->setProcessed(true);
|
||||
$status = $queue->save();
|
||||
$output['queues'] []= [
|
||||
'queue' => $queue->toArray(),
|
||||
'processed' => $status
|
||||
];
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
<?php
|
||||
namespace Contabilidad\Common\Controller;
|
||||
|
||||
use Carbon\Carbon;
|
||||
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\TipoCambio;
|
||||
|
||||
class TiposCambios {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
||||
$tipos = $factory->find(TipoCambio::class)->array();
|
||||
if ($tipos) {
|
||||
usort($tipos, function($a, $b) {
|
||||
return strcmp($a['fecha'], $b['fecha']);
|
||||
});
|
||||
}
|
||||
$output = [
|
||||
'tipos' => $tipos
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function show(Request $request, Response $response, Factory $factory, $tipo_id): Response {
|
||||
$tipo = $factory->find(TipoCambio::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 = TipoCambio::add($factory, $in);
|
||||
$results []= ['tipo' => $tipo?->toArray(), 'agregado' => $tipo?->save()];
|
||||
}
|
||||
} else {
|
||||
$tipo = TipoCambio::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(TipoCambio::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(TipoCambio::class)->one($tipo_id);
|
||||
$output = [
|
||||
'input' => $tipo_id,
|
||||
'tipo' => $tipo->toArray(),
|
||||
'eliminado' => $tipo->delete()
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function obtain(Request $request, Response $response, Factory $factory, Service $service): Response {
|
||||
$post = $request->getParsedBody();
|
||||
$valor = $service->get($post['fecha'], $post['moneda_id']);
|
||||
if ($valor === null) {
|
||||
return $this->withJson($response, ['input' => $post, 'tipo' => null, 'error' => 'No se encontró valor']);
|
||||
}
|
||||
$data = [
|
||||
'fecha' => $post['fecha'],
|
||||
'desde_id' => $post['moneda_id'],
|
||||
'hasta_id' => 1,
|
||||
'valor' => $valor
|
||||
];
|
||||
$tipo = TipoCambio::add($factory, $data);
|
||||
if ($tipo !== false and $tipo->is_new()) {
|
||||
$tipo->save();
|
||||
}
|
||||
$output = [
|
||||
'input' => $post,
|
||||
'tipo' => $tipo?->toArray()
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
@ -1,132 +0,0 @@
|
||||
<?php
|
||||
namespace Contabilidad\Common\Controller;
|
||||
|
||||
use Contabilidad\TipoCuenta;
|
||||
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\TipoCategoria;
|
||||
|
||||
class TiposCategorias {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response, Factory $factory, Service $service): Response {
|
||||
$tipos = $factory->find(TipoCategoria::class)->many();
|
||||
if ($tipos !== null) {
|
||||
array_walk($tipos, function(&$item) use ($service) {
|
||||
$arr = $item->toArray();
|
||||
$arr['categorias'] = $item->categorias();
|
||||
if ($arr['categorias'] !== null) {
|
||||
$arr['categorias'] = array_map(function($item) {
|
||||
return $item->toArray();
|
||||
}, $item->categorias());
|
||||
}
|
||||
$arr['saldo'] = abs($item->saldo($service));
|
||||
$arr['totales'] = $item->getTotales($service);
|
||||
$item = $arr;
|
||||
});
|
||||
usort($tipos, function($a, $b) {
|
||||
return strcmp($a['descripcion'], $b['descripcion']);
|
||||
});
|
||||
}
|
||||
$output = [
|
||||
'tipos' => $tipos
|
||||
];
|
||||
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 = TipoCategoria::add($factory, $in);
|
||||
$results []= ['tipo' => $tipo?->toArray(), 'agregado' => $tipo?->save()];
|
||||
}
|
||||
} else {
|
||||
$tipo = TipoCategoria::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(TipoCategoria::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(TipoCategoria::class)->one($tipo_id);
|
||||
$output = [
|
||||
'input' => $tipo_id,
|
||||
'tipo' => $tipo->toArray(),
|
||||
'eliminado' => $tipo->delete()
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function categorias(Request $request, Response $response, Factory $factory, Service $service, $tipo_id): Response {
|
||||
$tipo = $factory->find(TipoCategoria::class)->one($tipo_id);
|
||||
$categorias = null;
|
||||
if ($tipo != null) {
|
||||
$categorias = $tipo->categorias();
|
||||
if ($categorias !== null) {
|
||||
array_walk($categorias, function(&$item) use ($service) {
|
||||
$arr = $item->toArray($service);
|
||||
$arr['totales'] = $item->getTotales($service);
|
||||
$item = $arr;
|
||||
});
|
||||
}
|
||||
}
|
||||
$output = [
|
||||
'input' => $tipo_id,
|
||||
'tipo' => $tipo?->toArray(),
|
||||
'categorias' => $categorias
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function balance(Request $request, Response $response, Factory $factory, Service $service): Response {
|
||||
$tipos = $factory->find(TipoCategoria::class)->many();
|
||||
$balance = array_reduce($tipos, function($sum, $item) use ($service) {
|
||||
$totales = $item->getTotales($service);
|
||||
if (!is_array($sum)) {
|
||||
$sum = [];
|
||||
}
|
||||
foreach ($totales as $p => $total) {
|
||||
if (!isset($sum[$p])) {
|
||||
$sum[$p] = 0;
|
||||
}
|
||||
$sum[$p] += $total;
|
||||
}
|
||||
return $sum;
|
||||
});
|
||||
/*$balance = array_reduce($tipos, function($sum, $item) use ($service) {
|
||||
$maps = ['activo', 'pasivo', 'ganancia', 'perdida'];
|
||||
foreach ($maps as $m) {
|
||||
$p = $m . 's';
|
||||
$t = ucfirst($m);
|
||||
if (!isset($sum[$p])) {
|
||||
$sum[$p] = 0;
|
||||
}
|
||||
$cuentas = $item->getCuentasOf($t);
|
||||
if ($cuentas === false or $cuentas === null) {
|
||||
continue;
|
||||
}
|
||||
$sum[$p] += array_reduce($cuentas, function($sum, $item) use($service) {
|
||||
return $sum + $item->saldo($service, true);
|
||||
});
|
||||
}
|
||||
return $sum;
|
||||
});*/
|
||||
return $this->withJson($response, $balance);
|
||||
}
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
<?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\Queuer as Service;
|
||||
use Contabilidad\Transaccion;
|
||||
|
||||
class Transacciones {
|
||||
use Json;
|
||||
|
||||
protected function parseTransacciones(?array $transacciones): ?array {
|
||||
if ($transacciones !== null) {
|
||||
usort($transacciones, function($a, $b) {
|
||||
$d = $a['fecha'] - $b['fecha'];
|
||||
if ($d === 0) {
|
||||
return strcmp($a['cuenta']['nombre'], $b['cuenta']['nombre']);
|
||||
}
|
||||
return $d;
|
||||
});
|
||||
}
|
||||
return $transacciones;
|
||||
}
|
||||
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
||||
$transacciones = $factory->find(Transaccion::class)->array();
|
||||
$output = [
|
||||
'transacciones' => $this->parseTransacciones($transacciones)
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function show(Request $request, Response $response, Factory $factory, $transaccion_id): Response {
|
||||
$transaccion = $factory->find(Transaccion::class)->one($transaccion_id);
|
||||
$output = [
|
||||
'input' => $transaccion_id,
|
||||
'transaccion' => $transaccion?->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) {
|
||||
$transaccion = Transaccion::add($factory, $in);
|
||||
$results []= ['transaccion' => $transaccion?->toArray(), 'agregado' => $transaccion?->save()];
|
||||
}
|
||||
} else {
|
||||
$transaccion = Transaccion::add($factory, $input);
|
||||
$results []= ['transaccion' => $transaccion?->toArray(), 'agregado' => $transaccion?->save()];
|
||||
}
|
||||
$output = [
|
||||
'input' => $input,
|
||||
'transacciones' => $results
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function edit(Request $request, Response $response, Factory $factory, Service $queuer, $transaccion_id): Response {
|
||||
$transaccion = $factory->find(Transaccion::class)->one($transaccion_id);
|
||||
$output = [
|
||||
'input' => $transaccion_id,
|
||||
'old' => $transaccion->toArray()
|
||||
];
|
||||
$old_cuentas = ['credito' => $transaccion->credito_id, 'debito_id' => $transaccion->debito_id];
|
||||
$input = json_decode($request->getBody());
|
||||
$transaccion->edit($input);
|
||||
$new_cuentas = ['credito' => $transaccion->credito_id, 'debito_id' => $transaccion->debito_id];
|
||||
$cuentas = [];
|
||||
foreach ($new_cuentas as $tipo => $id) {
|
||||
if ($old_cuentas[$tipo] != $id) {
|
||||
$cuentas []= $old_cuentas[$tipo];
|
||||
$cuentas []= $id;
|
||||
}
|
||||
}
|
||||
$this->updateConsolidar($queuer, $transaccion->fecha(), $cuentas);
|
||||
|
||||
$output['transaccion'] = $transaccion->toArray();
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
protected function updateConsolidar(Service $queuer, \DateTimeInterface $mes, $cuentas) {
|
||||
foreach ($cuentas as $cuenta_id) {
|
||||
$queuer->queue('update_consolidar', ['mes' => $mes->format('Y-m-1'), 'cuenta' => $cuenta_id]);
|
||||
}
|
||||
}
|
||||
public function delete(Request $request, Response $response, Factory $factory, $transaccion_id): Response {
|
||||
$transaccion = $factory->find(Transaccion::class)->one($transaccion_id);
|
||||
$output = [
|
||||
'input' => $transaccion_id,
|
||||
'transaccion' => $transaccion->toArray(),
|
||||
'eliminado' => $transaccion->delete()
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
<?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 Contabilidad\Common\Service\WorkerHandler as Handler;
|
||||
|
||||
class Workers {
|
||||
use Json;
|
||||
|
||||
public function register(Request $request, Response $response, Handler $handler): Response {
|
||||
$post = $request->getBody();
|
||||
$result = $handler->register($post);
|
||||
$output = [
|
||||
'input' => $post,
|
||||
'result' => $result
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user