50 lines
2.1 KiB
PHP
50 lines
2.1 KiB
PHP
<?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);
|
|
}
|
|
}
|