155 lines
4.9 KiB
PHP
155 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\Bpa;
|
|
|
|
use App\Enums\Bpa\ExportacaoStatus;
|
|
use App\Events\Bpa\ExportacaoFinalizadaEvent;
|
|
use App\Models\Bpa\Exportacao;
|
|
use App\Services\Bpa\ExportacaoService;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Throwable;
|
|
|
|
class ExportacaoJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
const BASE_PATH = 'faturamento' . DIRECTORY_SEPARATOR . 'bpa' . DIRECTORY_SEPARATOR;
|
|
|
|
public $timeout = 0;
|
|
public $tries = 1;
|
|
|
|
protected $cliente;
|
|
protected $exportacaoId;
|
|
|
|
public function __construct($cliente, $exportacaoId)
|
|
{
|
|
$this->cliente = $cliente;
|
|
$this->exportacaoId = $exportacaoId;
|
|
}
|
|
|
|
public function handle()
|
|
{
|
|
set_time_limit(0);
|
|
|
|
$currentConnection = config('database.default');
|
|
|
|
try {
|
|
$this->setConnection();
|
|
|
|
$exportacao = Exportacao::find($this->exportacaoId);
|
|
|
|
if (is_null($exportacao)) {
|
|
Log::channel('bpa-exportacao')->error('Registro de exportação não encontrado', [
|
|
'cliente' => $this->cliente,
|
|
'origem' => 'ExportacaoJob',
|
|
'exportacao_id' => $this->exportacaoId,
|
|
]);
|
|
|
|
return;
|
|
}
|
|
|
|
$exportacao->status_id = ExportacaoStatus::EM_PROCESSAMENTO;
|
|
$exportacao->data_hora_inicio = Carbon::now();
|
|
$exportacao->save();
|
|
|
|
DB::transaction(function () use ($exportacao) {
|
|
$path = self::BASE_PATH . $this->cliente . DIRECTORY_SEPARATOR;
|
|
|
|
$params = [
|
|
'exportacao_id' => $exportacao->id,
|
|
'competencia_id' => $exportacao->competencia_id,
|
|
'unidades' => $exportacao->unidades->pluck('id'),
|
|
'financiamentos' => $exportacao->financiamentos->pluck('id'),
|
|
];
|
|
|
|
$file = ExportacaoService::generate($this->cliente, $params);
|
|
|
|
$exportacao->status_id = ExportacaoStatus::PROCESSADO;
|
|
$exportacao->quantidade_consistencia = $file['consistencias'];
|
|
$exportacao->quantidade_inconsistencia = $file['inconsistencias'];
|
|
|
|
if (Storage::exists($path . $file['nome'])) {
|
|
$exportacao->arquivo = $file['nome'];
|
|
chmod(Storage::path($path . $file['nome']), 0775);
|
|
}
|
|
|
|
$exportacao->data_hora_fim = Carbon::now();
|
|
$exportacao->save();
|
|
|
|
ExportacaoFinalizadaEvent::dispatch($exportacao->createdLotacao->usuario_id, $exportacao->id, $exportacao->arquivo);
|
|
});
|
|
} finally {
|
|
$this->clearConnection();
|
|
|
|
Config::set('database.default', $currentConnection);
|
|
}
|
|
}
|
|
|
|
public function failed(Throwable $exception)
|
|
{
|
|
$currentConnection = config('database.default');
|
|
|
|
try {
|
|
$this->setConnection();
|
|
|
|
$exportacao = Exportacao::find($this->exportacaoId);
|
|
|
|
if ($exportacao) {
|
|
$exportacao->status_id = ExportacaoStatus::ERRO_NO_PROCESSAMENTO;
|
|
$exportacao->data_hora_fim = Carbon::now();
|
|
$exportacao->save();
|
|
|
|
$path = self::BASE_PATH . $this->cliente . DIRECTORY_SEPARATOR;
|
|
$fileName = $exportacao->arquivo;
|
|
|
|
if (!is_null($fileName) && Storage::exists($path . $fileName)) {
|
|
Storage::delete($path . $fileName);
|
|
}
|
|
}
|
|
|
|
Log::channel('atencao-basica-exportacao')->error('Erro ao executar a exportação', [
|
|
'cliente' => $this->cliente,
|
|
'origem' => 'ExportacaoJob',
|
|
'erro' => $exception->getMessage(),
|
|
]);
|
|
} finally {
|
|
$this->clearConnection();
|
|
|
|
Config::set('database.default', $currentConnection);
|
|
}
|
|
}
|
|
|
|
private function setConnection()
|
|
{
|
|
Config::set("database.connections.job-{$this->cliente}", [
|
|
'driver' => 'mysql',
|
|
'host' => env('QUEUE_DB_HOST'),
|
|
'port' => env('QUEUE_DB_PORT'),
|
|
'database' => "conectasus_{$this->cliente}",
|
|
'username' => env('QUEUE_DB_USERNAME'),
|
|
'password' => env('QUEUE_DB_PASSWORD'),
|
|
'charset' => 'utf8mb4',
|
|
'collation' => 'utf8mb4_unicode_ci',
|
|
]);
|
|
|
|
Config::set('database.default', "job-{$this->cliente}");
|
|
|
|
$this->clearConnection();
|
|
}
|
|
|
|
private function clearConnection()
|
|
{
|
|
DB::disconnect("job-{$this->cliente}");
|
|
DB::purge("job-{$this->cliente}");
|
|
}
|
|
}
|