diff --git a/app/Console/Commands/ImportFramework.php b/app/Console/Commands/ImportFramework.php new file mode 100644 index 0000000..5e13692 --- /dev/null +++ b/app/Console/Commands/ImportFramework.php @@ -0,0 +1,85 @@ +argument('filename'); + + if (! file_exists($fileName)) { + Log::error('ImportFramework - file does not exists'); + $this->components->error('File does not exists'); + } else { + Log::debug('ImportFramework - Import ' . $fileName); + + // XLSX + $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx(); + $reader->setReadDataOnly(true); + $spreadsheet = $reader->load($fileName); + + $sheet = $spreadsheet->getSheet($spreadsheet->getFirstSheetIndex()); + $data = $sheet->toArray(); + + // create error collection + $errors = Collect(); + + // create controller + $importController = new MeasureImportController(); + + // Import file + if ($importController->canImportFromFile($data, $this->option('clean'), $errors)) { + // Clear database + if ($this->option('clean')) { + $importController->clean(); + $errors->push('Database cleared'); + } + + // Improt data + $importController->importFromFile($data, $errors); + + foreach ($errors as $error) { + Log::info('ImportFramework - ' . $error); + $this->components->info($error); + } + } else { + foreach ($errors as $error) { + Log::error('ImportFramework - ' . $error); + $this->components->error($error); + } + } + } + + Log::debug('ImportFramework - DONE.'); + } +}