diff --git a/CLI.Dockerfile b/CLI.Dockerfile new file mode 100644 index 0000000..82fe06c --- /dev/null +++ b/CLI.Dockerfile @@ -0,0 +1,14 @@ +FROM php:8.1-fpm + +RUN apt-get update && apt-get install -y --no-install-recommends cron && rm -r /var/lib/apt/lists/* + +RUN pecl install xdebug-3.1.3 \ + && docker-php-ext-enable xdebug + +COPY ./php-errors.ini /usr/local/etc/php/conf.d/docker-php-errors.ini + +WORKDIR /code + +COPY ./cli/crontab /var/spool/cron/crontabs/root + +CMD ["cron", "-f"] diff --git a/cli/common/Alias/Application.php b/cli/common/Alias/Application.php new file mode 100644 index 0000000..6565ed1 --- /dev/null +++ b/cli/common/Alias/Application.php @@ -0,0 +1,20 @@ +container->has('APP_NAME')) { + $name = $this->container->get('APP_NAME'); + } + parent::__construct($name, $version); + } + public function getContainer(): ContainerInterface + { + return $this->container; + } +} diff --git a/cli/composer.json b/cli/composer.json new file mode 100644 index 0000000..b656c63 --- /dev/null +++ b/cli/composer.json @@ -0,0 +1,29 @@ +{ + "name": "incoviba/cli", + "type": "project", + "require": { + "symfony/console": "^6.3", + "php-di/php-di": "^7.0", + "guzzlehttp/guzzle": "^7.8", + "monolog/monolog": "^3.5" + }, + "require-dev": { + "phpunit/phpunit": "^10.4", + "kint-php/kint": "^5.1" + }, + "authors": [ + { + "name": "Aldarien", + "email": "aldarien85@gmail.com" + } + ], + "autoload": { + "psr-4": { + "Incoviba\\Common\\": "common/", + "Incoviba\\": "src/" + } + }, + "config": { + "sort-packages": true + } +} diff --git a/cli/crontab b/cli/crontab new file mode 100644 index 0000000..e41ff5a --- /dev/null +++ b/cli/crontab @@ -0,0 +1,8 @@ +0 2 * * * php /code/bin/index.php ventas:cuotas:hoy +0 2 * * * php /code/bin/index.php ventas:cuotas:pendientes +0 2 * * * php /code/bin/index.php ventas:cuotas:vencer +0 2 * * * php /code/bin/index.php ventas:cierres:vigentes +0 2 * * * php /code/bin/index.php proyectos:activos +0 2 * * * php /code/bin/index.php comunas +0 2 * * * php /code/bin/index.php money:uf +0 2 1 * * php /code/bin/index.php money:ipc diff --git a/cli/resources/commands/comunas.php b/cli/resources/commands/comunas.php new file mode 100644 index 0000000..e1f1fa8 --- /dev/null +++ b/cli/resources/commands/comunas.php @@ -0,0 +1,2 @@ +add($app->getContainer()->get(Incoviba\Command\Comunas::class)); diff --git a/cli/resources/commands/money.php b/cli/resources/commands/money.php new file mode 100644 index 0000000..2e087bc --- /dev/null +++ b/cli/resources/commands/money.php @@ -0,0 +1,3 @@ +add($app->getContainer()->get(Incoviba\Command\Money\UF::class)); +$app->add($app->getContainer()->get(Incoviba\Command\Money\IPC::class)); diff --git a/cli/resources/commands/proyectos.php b/cli/resources/commands/proyectos.php new file mode 100644 index 0000000..c9cb911 --- /dev/null +++ b/cli/resources/commands/proyectos.php @@ -0,0 +1,9 @@ +isDir()) { + continue; + } + include_once $file->getRealPath(); +} diff --git a/cli/resources/commands/proyectos/activos.php b/cli/resources/commands/proyectos/activos.php new file mode 100644 index 0000000..836ba5b --- /dev/null +++ b/cli/resources/commands/proyectos/activos.php @@ -0,0 +1,2 @@ +add($app->getContainer()->get(Incoviba\Command\Proyectos\Activos::class)); diff --git a/cli/resources/commands/ventas.php b/cli/resources/commands/ventas.php new file mode 100644 index 0000000..dd4cf66 --- /dev/null +++ b/cli/resources/commands/ventas.php @@ -0,0 +1,9 @@ +isDir()) { + continue; + } + include_once $file->getRealPath(); +} diff --git a/cli/resources/commands/ventas/cierres.php b/cli/resources/commands/ventas/cierres.php new file mode 100644 index 0000000..1ab6cff --- /dev/null +++ b/cli/resources/commands/ventas/cierres.php @@ -0,0 +1,2 @@ +add($app->getContainer()->get(Incoviba\Command\Ventas\Cierres\Vigentes::class)); diff --git a/cli/resources/commands/ventas/cuotas.php b/cli/resources/commands/ventas/cuotas.php new file mode 100644 index 0000000..8dd8fd5 --- /dev/null +++ b/cli/resources/commands/ventas/cuotas.php @@ -0,0 +1,4 @@ +add($app->getContainer()->get(Incoviba\Command\Ventas\Cuotas\Hoy::class)); +$app->add($app->getContainer()->get(Incoviba\Command\Ventas\Cuotas\Pendientes::class)); +$app->add($app->getContainer()->get(Incoviba\Command\Ventas\Cuotas\PorVencer::class)); diff --git a/cli/setup/app.php b/cli/setup/app.php new file mode 100644 index 0000000..2310f7c --- /dev/null +++ b/cli/setup/app.php @@ -0,0 +1,42 @@ +isDir()) { + continue; + } + $builder->addDefinitions($file->getRealPath()); + } + } + $app = new Application($builder->build()); + + $folder = implode(DIRECTORY_SEPARATOR, [__DIR__, 'middlewares']); + if (file_exists($folder)) { + $files = new FilesystemIterator($folder); + foreach ($files as $file) { + if ($file->isDir()) { + continue; + } + include_once $file->getRealPath(); + } + } + + return $app; +} +return buildApp(); diff --git a/cli/setup/composer.php b/cli/setup/composer.php new file mode 100644 index 0000000..b451f96 --- /dev/null +++ b/cli/setup/composer.php @@ -0,0 +1,6 @@ +getContainer()->get('folders')->commands); + foreach ($files as $file) { + if ($file->isDir()) { + continue; + } + include_once $file->getRealPath(); + } +} +loadCommands($app); diff --git a/cli/setup/settings/env.php b/cli/setup/settings/env.php new file mode 100644 index 0000000..580b775 --- /dev/null +++ b/cli/setup/settings/env.php @@ -0,0 +1,2 @@ + function() { + $arr = ['base' => dirname(__FILE__, 3)]; + $arr['resources'] = implode(DIRECTORY_SEPARATOR, [ + $arr['base'], + 'resources' + ]); + $arr['commands'] = implode(DIRECTORY_SEPARATOR, [ + $arr['resources'], + 'commands' + ]); + return (object) $arr; + } +]; diff --git a/cli/setup/setups/client.php b/cli/setup/setups/client.php new file mode 100644 index 0000000..12292f8 --- /dev/null +++ b/cli/setup/setups/client.php @@ -0,0 +1,15 @@ + function(ContainerInterface $container) { + return new GuzzleHttp\Client([ + 'base_uri' => $container->get('API_URL'), + 'headers' => [ + 'Authorization' => [ + 'Bearer ' . md5($container->get('API_KEY')) + ] + ] + ]); + } +]; diff --git a/cli/setup/setups/logs.php b/cli/setup/setups/logs.php new file mode 100644 index 0000000..0ccd933 --- /dev/null +++ b/cli/setup/setups/logs.php @@ -0,0 +1,32 @@ + function(ContainerInterface $container) { + return new Monolog\Logger('incoviba', [ + new Monolog\Handler\FilterHandler( + (new Monolog\Handler\RotatingFileHandler('/logs/debug.log')) + ->setFormatter(new Monolog\Formatter\LineFormatter(null, null, false, false, true)), + Monolog\Level::Debug, + Monolog\Level::Notice + ), + new Monolog\Handler\FilterHandler( + (new Monolog\Handler\RotatingFileHandler('/logs/error.log')) + ->setFormatter(new Monolog\Formatter\LineFormatter(null, null, false, false, true)), + Monolog\Level::Warning, + Monolog\Level::Error + ), + new Monolog\Handler\FilterHandler( + (new Monolog\Handler\RotatingFileHandler('/logs/critical.log')) + ->setFormatter(new Monolog\Formatter\LineFormatter(null, null, false, false, true)), + Monolog\Level::Critical + ) + ], [ + $container->get(Monolog\Processor\PsrLogMessageProcessor::class), + $container->get(Monolog\Processor\WebProcessor::class), + $container->get(Monolog\Processor\IntrospectionProcessor::class), + $container->get(Monolog\Processor\MemoryUsageProcessor::class), + $container->get(Monolog\Processor\MemoryPeakUsageProcessor::class) + ]); + } +]; diff --git a/cli/src/Command/Comunas.php b/cli/src/Command/Comunas.php new file mode 100644 index 0000000..235ff11 --- /dev/null +++ b/cli/src/Command/Comunas.php @@ -0,0 +1,25 @@ +writeln("GET {$uri}"); + $response = $this->client->get($uri); + $output->writeln("Response Code: {$response->getStatusCode()}"); + return Console\Command\Command::SUCCESS; + } +} diff --git a/cli/src/Command/Money/IPC.php b/cli/src/Command/Money/IPC.php new file mode 100644 index 0000000..06d29c4 --- /dev/null +++ b/cli/src/Command/Money/IPC.php @@ -0,0 +1,35 @@ +sub(new DateInterval('P1M')); + $uri = '/api/money'; + $data = [ + 'provider' => 'ipc', + 'fecha' => $lastMonth->format('Y-m-1') + ]; + $output->writeln("POST {$uri}"); + $response = $this->client->post($uri, [ + 'body' => http_build_query($data), + 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'] + ]); + $output->writeln("Response Code: {$response->getStatusCode()}"); + return Console\Command\Command::SUCCESS; + } +} diff --git a/cli/src/Command/Money/UF.php b/cli/src/Command/Money/UF.php new file mode 100644 index 0000000..109c96f --- /dev/null +++ b/cli/src/Command/Money/UF.php @@ -0,0 +1,34 @@ + 'uf', + 'fecha' => $now->format('Y-m-d') + ]; + $output->writeln("POST {$uri}"); + $response = $this->client->post($uri, [ + 'body' => http_build_query($data), + 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'] + ]); + $output->writeln("Response Code: {$response->getStatusCode()}"); + return Console\Command\Command::SUCCESS; + } +} diff --git a/cli/src/Command/Proyectos/Activos.php b/cli/src/Command/Proyectos/Activos.php new file mode 100644 index 0000000..80113c1 --- /dev/null +++ b/cli/src/Command/Proyectos/Activos.php @@ -0,0 +1,25 @@ +writeln("GET {$uri}"); + $response = $this->client->get($uri); + $output->writeln("Response Code: {$response->getStatusCode()}"); + return Console\Command\Command::SUCCESS; + } +} diff --git a/cli/src/Command/Ventas/Cierres/Vigentes.php b/cli/src/Command/Ventas/Cierres/Vigentes.php new file mode 100644 index 0000000..ca65e36 --- /dev/null +++ b/cli/src/Command/Ventas/Cierres/Vigentes.php @@ -0,0 +1,25 @@ +writeln("GET {$uri}"); + $response = $this->client->get($uri); + $output->writeln("Response Code: {$response->getStatusCode()}"); + return Console\Command\Command::SUCCESS; + } +} diff --git a/cli/src/Command/Ventas/Cuotas/Hoy.php b/cli/src/Command/Ventas/Cuotas/Hoy.php new file mode 100644 index 0000000..de6ccf6 --- /dev/null +++ b/cli/src/Command/Ventas/Cuotas/Hoy.php @@ -0,0 +1,25 @@ +writeln("GET {$uri}"); + $response = $this->client->get($uri); + $output->writeln("Response Code: {$response->getStatusCode()}"); + return Console\Command\Command::SUCCESS; + } +} diff --git a/cli/src/Command/Ventas/Cuotas/Pendientes.php b/cli/src/Command/Ventas/Cuotas/Pendientes.php new file mode 100644 index 0000000..ddc8a62 --- /dev/null +++ b/cli/src/Command/Ventas/Cuotas/Pendientes.php @@ -0,0 +1,25 @@ +writeln("GET {$uri}"); + $response = $this->client->get($uri); + $output->writeln("Response Code: {$response->getStatusCode()}"); + return Console\Command\Command::SUCCESS; + } +} diff --git a/cli/src/Command/Ventas/Cuotas/PorVencer.php b/cli/src/Command/Ventas/Cuotas/PorVencer.php new file mode 100644 index 0000000..2938945 --- /dev/null +++ b/cli/src/Command/Ventas/Cuotas/PorVencer.php @@ -0,0 +1,25 @@ +writeln("GET {$uri}"); + $response = $this->client->get($uri); + $output->writeln("Response Code: {$response->getStatusCode()}"); + return Console\Command\Command::SUCCESS; + } +} diff --git a/docker-compose.yml b/docker-compose.yml index 3e628b8..e73d660 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -26,6 +26,7 @@ services: env_file: - ${APP_PATH:-.}/.env - ${APP_PATH:-.}/.db.env + - ./.key.env #- ${APP_PATH:-.}/.remote.env volumes: - ${APP_PATH:-.}/:/code @@ -83,6 +84,20 @@ services: - "./logs:/logs" ports: - "8084:80" + cli: + profiles: + - cli + build: + context: . + dockerfile: CLI.Dockerfile + container_name: incoviba_cli + <<: *restart + env_file: + - ${CLI_PATH:-.}/.env + - ./.key.env + volumes: + - ${CLI_PATH:-.}:/code + - ./logs/cli:/logs volumes: dbdata: {}