Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions app/Console/Commands/excel/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,41 @@

class Generic extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'excel:generic';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generic Excel Importer';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*/
public function handle(): void
{
Log::info('Loading Generic Excel');

// Load rows to determine the actual count (only the first sheet)
$collection = Excel::toCollection(new GenericEventsImport($this->output), 'example.xlsx', 'excel');
$rows = $collection->first();
$rowCount = $rows->count();

$this->info("Rows to import: {$rowCount}");

// Re-run with row count and output for progress tracking
$importer = new GenericEventsImport($this->output, $rowCount);

Excel::import(
$importer,
new GenericEventsImport(),
'example.xlsx',
'excel'
);

$this->info("✅ Import completed successfully.");
}
}
}
32 changes: 0 additions & 32 deletions app/Imports/GenericEventsImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,9 @@
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;

class GenericEventsImport extends DefaultValueBinder implements ToModel, WithCustomValueBinder, WithHeadingRow
{
protected $output;
protected $progressBar;
protected $total;

public function __construct(?OutputInterface $output = null, int $total = 0)
{
$this->output = $output;
$this->total = $total;

if ($this->output && $total > 0) {
$this->progressBar = new ProgressBar($this->output, $total);
$this->progressBar->start();
}
}

public function __destruct()
{
if ($this->progressBar) {
$this->progressBar->finish();
$this->output->writeln("\nImport complete.");
}
}

public function parseDate($value)
{
// Handle Excel dates or normal string dates
Expand All @@ -59,13 +34,6 @@ public function parseDate($value)
public function model(array $row): ?Model
{
Log::info('Importing row:', $row);
if ($this->progressBar) {
$this->progressBar->advance();
}

if (!empty($row['activity_title']) && $this->output) {
$this->output->writeln(" Imported: " . $row['activity_title']);
}

// Validate required fields
if (
Expand Down