Cli
This commit is contained in:
50
app/Alias/Connection.php
Normal file
50
app/Alias/Connection.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
namespace App\Alias;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
class Connection
|
||||
{
|
||||
public function __construct(
|
||||
protected string $host,
|
||||
protected string $database,
|
||||
protected string $username,
|
||||
protected string $password,
|
||||
protected ?int $port = null,
|
||||
protected int $retries = 5
|
||||
) {}
|
||||
|
||||
protected PDO $connection;
|
||||
public function connect(): PDO
|
||||
{
|
||||
if (!isset($this->connection)) {
|
||||
$r = 0;
|
||||
$exception = null;
|
||||
while ($r < $this->retries) {
|
||||
try {
|
||||
$dsn = $this->getDsn();
|
||||
$this->connection = new PDO($dsn, $this->username, $this->password);
|
||||
return $this->connection;
|
||||
} catch (PDOException $e) {
|
||||
if ($exception !== null) {
|
||||
$e = new PDOException($e->getMessage(), $e->getCode(), $exception);
|
||||
}
|
||||
$exception = $e;
|
||||
usleep(500);
|
||||
}
|
||||
$r ++;
|
||||
}
|
||||
throw $exception;
|
||||
}
|
||||
return $this->connection;
|
||||
}
|
||||
protected function getDsn(): string
|
||||
{
|
||||
$dsn = "mysql:host={$this->host};dbname={$this->database}";
|
||||
if (isset($this->port)) {
|
||||
$dsn .= ";port={$this->port}";
|
||||
}
|
||||
return $dsn;
|
||||
}
|
||||
}
|
@ -1,58 +1,17 @@
|
||||
<?php
|
||||
namespace App\Alias;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
class RemoteConnection
|
||||
class RemoteConnection extends Connection
|
||||
{
|
||||
public function __construct(protected int $retries = 5)
|
||||
{
|
||||
$this->host = $_ENV['REMOTE_HOST'];
|
||||
$this->database = $_ENV['REMOTE_DATABASE'];
|
||||
$this->username = $_ENV['REMOTE_USER'];
|
||||
$this->password = $_ENV['REMOTE_PASSWORD'];
|
||||
if (isset($_ENV['REMOTE_PORT'])) {
|
||||
$this->port = $_ENV['REMOTE_PORT'];
|
||||
}
|
||||
}
|
||||
|
||||
protected string $host;
|
||||
protected string $database;
|
||||
protected int $port;
|
||||
protected string $username;
|
||||
protected string $password;
|
||||
|
||||
protected PDO $connection;
|
||||
public function connect(): PDO
|
||||
{
|
||||
if (!isset($this->connection)) {
|
||||
$r = 0;
|
||||
$exception = null;
|
||||
while ($r < $this->retries) {
|
||||
try {
|
||||
$dsn = $this->getDsn();
|
||||
$this->connection = new PDO($dsn, $this->username, $this->password);
|
||||
} catch (PDOException $e) {
|
||||
if ($exception !== null) {
|
||||
$e = new PDOException($e->getMessage(), $e->getCode(), $exception);
|
||||
}
|
||||
$exception = $e;
|
||||
usleep(500);
|
||||
}
|
||||
$r ++;
|
||||
}
|
||||
throw $exception;
|
||||
}
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
protected function getDsn(): string
|
||||
{
|
||||
$dsn = "mysql:host={$this->host};dbname={$this->database}";
|
||||
if (isset($this->port)) {
|
||||
$dsn .= ";port={$this->port}";
|
||||
}
|
||||
return $dsn;
|
||||
parent::__construct(
|
||||
$_ENV['REMOTE_HOST'],
|
||||
$_ENV['REMOTE_DATABASE'],
|
||||
$_ENV['REMOTE_USER'],
|
||||
$_ENV['REMOTE_PASSWORD'],
|
||||
$_ENV['REMOTE_PORT'] ?? null,
|
||||
$this->retries
|
||||
);
|
||||
}
|
||||
}
|
||||
|
86
app/Command/Money/Get.php
Normal file
86
app/Command/Money/Get.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
namespace App\Command\Money;
|
||||
|
||||
use DateTimeInterface;
|
||||
use DateTimeImmutable;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use App\Alias\Connection;
|
||||
use App\Service\Money;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'money:uf:get',
|
||||
hidden: false
|
||||
)]
|
||||
class Get extends Command
|
||||
{
|
||||
public function __construct(protected Money $service, protected Connection $connection, string $name = null)
|
||||
{
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$io->title('Get Money');
|
||||
|
||||
$dates = $this->getDates();
|
||||
foreach ($dates as $date_string => $ids) {
|
||||
$date = $this->parseDate($date_string);
|
||||
$response = $this->service->getUF($date);
|
||||
if ($response->total === 0) {
|
||||
continue;
|
||||
}
|
||||
foreach ($ids as $id) {
|
||||
$this->queueUpdate($id, $response->uf->value);
|
||||
}
|
||||
}
|
||||
$this->updateUF();
|
||||
}
|
||||
|
||||
protected function getDates(): array
|
||||
{
|
||||
$query = "SELECT id, fecha FROM pago WHERE uf IS NULL AND fecha BETWEEN 0 AND DATE_ADD(CURDATE(), INTERVAL 9 DAY) ORDER BY fecha";
|
||||
$statement = $this->connection->connect()->query($query);
|
||||
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
if (count($rows) === 0) {
|
||||
return [];
|
||||
}
|
||||
$dates = [];
|
||||
foreach ($rows as $row) {
|
||||
if (!isset($dates[$row['fecha']])) {
|
||||
$dates[$row['fecha']] = [];
|
||||
}
|
||||
$dates[$row['fecha']] []= (int) $row['id'];
|
||||
}
|
||||
return $dates;
|
||||
}
|
||||
protected function parseDate(string $date_string): DateTimeInterface
|
||||
{
|
||||
return new DateTimeImmutable($date_string);
|
||||
}
|
||||
protected array $rows;
|
||||
protected function queueUpdate(int $id, float $value): void
|
||||
{
|
||||
$this->rows []= [$value, $id];
|
||||
}
|
||||
protected function updateUF(): void
|
||||
{
|
||||
$query = "UPDATE pago SET uf = ? WHERE id = ?";
|
||||
$statement = $this->connection->connect()->prepare($query);
|
||||
foreach ($this->rows as $row) {
|
||||
$this->connection->connect()->beginTransaction();
|
||||
try {
|
||||
$statement->execute($row);
|
||||
$this->connection->connect()->commit();
|
||||
} catch (PDOException $e) {
|
||||
$this->connection->connect()->rollBack();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
51
app/Command/Money/Lookup.php
Normal file
51
app/Command/Money/Lookup.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
namespace App\Command\Money;
|
||||
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use App\Alias\Connection;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'money:lookup',
|
||||
hidden: false
|
||||
)]
|
||||
class Lookup extends Command
|
||||
{
|
||||
public function __construct(protected Connection $connection, string $name = null)
|
||||
{
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$io->title('Lookup Money');
|
||||
|
||||
while (true) {
|
||||
$io->info('Checking pending');
|
||||
if ($this->hasPendingMoney()) {
|
||||
$io->success('Running money get UF');
|
||||
$io->note($this->runGetUF());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function hasPendingMoney(): bool
|
||||
{
|
||||
$query = "SELECT 1 FROM pago WHERE uf IS NULL AND fecha BETWEEN 0 AND DATE_ADD(CURDATE(), INTERVAL 9 DAY)";
|
||||
$statement = $this->connection->connect()->query($query);
|
||||
return $statement->rowCount() > 0;
|
||||
}
|
||||
protected function runGetUF(): string
|
||||
{
|
||||
$command = "/code/bin/console money:uf:get";
|
||||
$result = shell_exec($command);
|
||||
if (!$result or $result === null) {
|
||||
throw new \Exception();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
@ -312,7 +312,7 @@ class Proyectos
|
||||
}
|
||||
return view('proyectos.reservas.base', compact('proyecto', 'pisos', 'max_unidades', 'totales'));
|
||||
}
|
||||
public function unidades()
|
||||
public static function unidades()
|
||||
{
|
||||
if (get('proyecto')) {
|
||||
$proyecto = model(Proyecto::class)->findOne(get('proyecto'));
|
||||
|
@ -40,6 +40,7 @@ class Money
|
||||
protected function checkNextMonthValueCalculationByDate(DateTimeInterface $date): bool
|
||||
{
|
||||
$next_m_9 = Carbon::today(config('app.timezone'))->copy()->endOfMonth()->addDays(9);
|
||||
$date = Carbon::parse($date);
|
||||
return $date->lessThan($next_m_9);
|
||||
}
|
||||
protected function isOk(ResponseInterface $response): bool
|
||||
|
Reference in New Issue
Block a user