149 lines
5.6 KiB
PHP
149 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Mail\ExportCodesUniques;
|
|
use App\Mail\JobFailed;
|
|
use App\Models\Codes;
|
|
use App\Models\Settings;
|
|
use Exception;
|
|
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\Mail;
|
|
|
|
class ProcessExportCodesUniques implements ShouldQueue
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithQueue;
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
private $nbCodeFic, $nbCodeTot, $nbCodeLigne;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct($nbCodeFic, $nbCodeTot, $nbCodeLigne)
|
|
{
|
|
$this->nbCodeFic = $nbCodeFic;
|
|
$this->nbCodeTot = $nbCodeTot;
|
|
$this->nbCodeLigne = $nbCodeLigne;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
$nbCodeFic = $this->nbCodeFic; // Nombre de code par fichier
|
|
$nbCodeTot = $this->nbCodeTot; // Nombre total de code à exporter
|
|
$nbCodeLigne = $this->nbCodeLigne; // Nombre de code par ligne
|
|
|
|
$totalExportedCodes = 0;
|
|
$currentFilename = 1;
|
|
$currentExportedCodes = 0;
|
|
$currentLineCodes = ($nbCodeLigne === 1 ? 1 : 0);
|
|
|
|
$this->process($nbCodeFic, $nbCodeTot, $nbCodeLigne, $totalExportedCodes, $currentFilename, $currentExportedCodes, $currentLineCodes);
|
|
|
|
// Compression des fichiers
|
|
$zip = new \ZipArchive();
|
|
if ($zip->open(storage_path('export_codes_uniques/') . "CodesUniques_" . date('dmY') . ".zip", \ZipArchive::CREATE) === true) {
|
|
if ($handle = opendir(storage_path('export_codes_uniques/tmp'))) {
|
|
while (false !== ($file_to_zip = readdir($handle))) {
|
|
if (pathinfo($file_to_zip, PATHINFO_EXTENSION) != 'csv') {
|
|
continue;
|
|
}
|
|
$zip->addFile(storage_path('export_codes_uniques/tmp/') . $file_to_zip, "$file_to_zip");
|
|
}
|
|
closedir($handle);
|
|
}
|
|
$zip->close();
|
|
}
|
|
|
|
// On vide le dossier temporaire des fichiers csv
|
|
if ($handle = opendir(storage_path('export_codes_uniques/tmp'))) {
|
|
while (false !== ($file_to_zip = readdir($handle))) {
|
|
if (pathinfo($file_to_zip, PATHINFO_EXTENSION) != 'csv') {
|
|
continue;
|
|
}
|
|
unlink(storage_path('export_codes_uniques/tmp/') . $file_to_zip);
|
|
}
|
|
closedir($handle);
|
|
}
|
|
|
|
// Envoi de mail à MAIL_ADMIN avec le nom du ZIP "CodesUniques_". date('dmY') .".zip"
|
|
Mail::to(config('mail.MAIL_ADMIN'))->send(new ExportCodesUniques(Settings::find(1)->name, $nbCodeFic, $nbCodeTot, $nbCodeLigne));
|
|
}
|
|
|
|
private function process($nbCodeFic, $nbCodeTot, $nbCodeLigne, $totalExportedCodes, $currentFilename, $currentExportedCodes, $currentLineCodes, $lastExportedCodeId = null)
|
|
{
|
|
$paginate = config('codes.paginate');
|
|
|
|
if ($nbCodeTot < $paginate) {
|
|
$nbCodeToRetrieve = $nbCodeTot;
|
|
} else {
|
|
if ($totalExportedCodes + $paginate > $nbCodeTot) {
|
|
$nbCodeToRetrieve = $nbCodeTot - $totalExportedCodes;
|
|
} else {
|
|
$nbCodeToRetrieve = $paginate;
|
|
}
|
|
}
|
|
|
|
if ($lastExportedCodeId === null) {
|
|
$codes = Codes::where('code_exported', 0)->take($nbCodeToRetrieve)->get();
|
|
} else {
|
|
$codes = Codes::where([
|
|
['code_exported', '=', 0],
|
|
['id', '>', $lastExportedCodeId]
|
|
])->take($nbCodeToRetrieve)->get();
|
|
}
|
|
|
|
$countCodes = count($codes);
|
|
|
|
$exportedCodesId = [];
|
|
|
|
foreach ($codes as $k => $code) {
|
|
if ($currentExportedCodes < $nbCodeTot) {
|
|
if ($currentLineCodes === ($nbCodeLigne === 1 ? 1 : $nbCodeLigne - 1)) {
|
|
file_put_contents(storage_path('export_codes_uniques/tmp/') . $currentFilename . ".csv", $code->code_unique . PHP_EOL, FILE_APPEND);
|
|
$totalExportedCodes++;
|
|
$currentExportedCodes++;
|
|
$currentLineCodes = ($nbCodeLigne === 1 ? 1 : 0);
|
|
} else {
|
|
file_put_contents(storage_path('export_codes_uniques/tmp/') . $currentFilename . ".csv", $code->code_unique . ";", FILE_APPEND);
|
|
$totalExportedCodes++;
|
|
$currentExportedCodes++;
|
|
$currentLineCodes++;
|
|
}
|
|
|
|
array_push($exportedCodesId, $code->id);
|
|
$lastExportedCodeId = $code->id;
|
|
|
|
if ($currentExportedCodes === $nbCodeFic) {
|
|
$currentFilename++;
|
|
$currentExportedCodes = 0;
|
|
}
|
|
if ((($k + 1) === $countCodes) && $totalExportedCodes < $nbCodeTot) {
|
|
Codes::whereIn('id', $exportedCodesId)->update(['code_exported' => 1]);
|
|
return $this->process($nbCodeFic, $nbCodeTot, $nbCodeLigne, $totalExportedCodes, $currentFilename, $currentExportedCodes, $currentLineCodes, $lastExportedCodeId);
|
|
}
|
|
if (($k + 1) === $countCodes) {
|
|
Codes::whereIn('id', $exportedCodesId)->update(['code_exported' => 1]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function failed(Exception $exception)
|
|
{
|
|
Mail::to(config('mail.MAIL_ADMIN'))->send(new JobFailed("ProcessExportCodeUnique", ['message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString()]));
|
|
}
|
|
}
|