Release inicial 1.0.0
This commit is contained in:
123
Console/Commands/GenerateCsvFromXlsx.php
Normal file
123
Console/Commands/GenerateCsvFromXlsx.php
Normal file
@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace Koneko\SatCatalogs\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Koneko\SatCatalogs\Services\CsvGeneratorService;
|
||||
|
||||
class GenerateCsvFromXlsx extends Command
|
||||
{
|
||||
protected $signature = 'sat:generate-csv {xlsxFile} {--all} {sheetName?} {csvFile?} {--skipRows=7}';
|
||||
|
||||
protected $description = 'Genera archivos CSV optimizados a partir de un archivo XLSX y actualiza el archivo de versión.';
|
||||
|
||||
private $csvRecordsCount = [];
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$xlsxFile = database_path("data/{$this->argument('xlsxFile')}");
|
||||
|
||||
if (!file_exists($xlsxFile)) {
|
||||
$this->error("⚠️ El archivo XLSX no existe: {$xlsxFile}");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->option('all')) {
|
||||
$this->generateAllCsv($xlsxFile);
|
||||
$this->updateVersionFile($xlsxFile);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$sheetName = $this->argument('sheetName');
|
||||
$csvFile = $this->argument('csvFile');
|
||||
$skipRows = (int) $this->option('skipRows');
|
||||
|
||||
if (!$sheetName || !$csvFile) {
|
||||
$this->error("⚠️ Debes proporcionar sheetName y csvFile o usar --all para procesar todos.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->generateCsv($xlsxFile, $sheetName, $csvFile, $skipRows);
|
||||
}
|
||||
|
||||
private function generateAllCsv(string $xlsxFile)
|
||||
{
|
||||
$catalogs = [
|
||||
'c_FormaPago' => 6,
|
||||
'c_Moneda' => 5,
|
||||
'c_CodigoPostal' => [['c_CodigoPostal_Parte_1', 'c_CodigoPostal_Parte_2'], 7],
|
||||
'c_RegimenFiscal' => 6,
|
||||
'c_Pais' => 5,
|
||||
'c_UsoCFDI' => 6,
|
||||
'c_ClaveProdServ' => 5,
|
||||
'c_ClaveUnidad' => 6,
|
||||
'c_Aduana' => 5,
|
||||
'C_Colonia' => [['C_Colonia_1', 'C_Colonia_2', 'C_Colonia_3'], 5],
|
||||
'c_Estado' => 5,
|
||||
'C_Localidad' => 5,
|
||||
'C_Municipio' => 5,
|
||||
];
|
||||
|
||||
foreach ($catalogs as $csvFile => $sheetData) {
|
||||
if (is_array($sheetData) && is_array($sheetData[0])) {
|
||||
// Se trata de una combinación de hojas
|
||||
[$sheets, $skipRows] = $sheetData;
|
||||
|
||||
$this->generateCsv($xlsxFile, $sheets, strtolower($csvFile), $skipRows);
|
||||
|
||||
} else {
|
||||
// Proceso normal para una sola hoja
|
||||
$this->generateCsv($xlsxFile, $csvFile, strtolower($csvFile), $sheetData);
|
||||
}
|
||||
}
|
||||
|
||||
echo "\n";
|
||||
}
|
||||
|
||||
private function generateCsv(string $xlsxFile, string|array $sheetName, string $csvFile, int $skipRows)
|
||||
{
|
||||
echo "\n📌 Procesando hojas: " . (is_array($sheetName) ? implode(", ", $sheetName) : $sheetName) . "\n";
|
||||
|
||||
$csvPath = CsvGeneratorService::convertToCsv($xlsxFile, $sheetName, $csvFile, $skipRows);
|
||||
|
||||
if (isset($csvPath['error'])) {
|
||||
$this->error("❌ Error en {$csvFile}: {$csvPath['error']}");
|
||||
|
||||
} else {
|
||||
$rowCount = CsvGeneratorService::countCsvRows($csvPath);
|
||||
|
||||
$this->csvRecordsCount[$csvFile] = $rowCount;
|
||||
$this->info("✅ CSV generado: {$csvPath} ({$rowCount} filas)");
|
||||
}
|
||||
|
||||
// Liberar memoria forzadamente
|
||||
gc_collect_cycles();
|
||||
}
|
||||
|
||||
private function updateVersionFile(string $xlsxFile)
|
||||
{
|
||||
$versionFile = database_path('seeders/sat_cache/version.txt');
|
||||
$versionContent = "📄 Versión de los Catálogos SAT\n";
|
||||
$versionContent .= "-----------------------------------\n";
|
||||
$versionContent .= "📌 Archivo XLSX: " . basename($xlsxFile) . "\n";
|
||||
$versionContent .= "📅 Fecha de generación: " . now()->format('Y-m-d H:i:s') . "\n";
|
||||
$versionContent .= "-----------------------------------\n";
|
||||
$versionContent .= "📂 Archivos CSV generados:\n";
|
||||
|
||||
foreach ($this->csvRecordsCount as $csvFile => $rowCount) {
|
||||
$versionContent .= " - {$csvFile}.csv ({$rowCount} filas)\n";
|
||||
}
|
||||
|
||||
$versionContent .= "-----------------------------------\n";
|
||||
$versionContent .= "🔗 Información del paquete:\n";
|
||||
$versionContent .= " - Packagist: https://packagist.org/packages/koneko/laravel-sat-catalogs\n";
|
||||
$versionContent .= " - Git Repo: https://git.koneko.mx/koneko/laravel-sat-catalogs\n";
|
||||
|
||||
file_put_contents($versionFile, $versionContent);
|
||||
|
||||
$this->info("✅ Archivo de versión actualizado: {$versionFile}");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user