|
|
@@ -0,0 +1,146 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Tests\Unit\Services;
|
|
|
+
|
|
|
+use App\Helpers\DateHelper;
|
|
|
+use App\Models\Import;
|
|
|
+use App\Models\ProductSKU;
|
|
|
+use App\Services\ImportMafsService;
|
|
|
+use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
+use Illuminate\Support\Facades\Storage;
|
|
|
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
+use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
+use Tests\TestCase;
|
|
|
+
|
|
|
+class ImportMafsServiceTest extends TestCase
|
|
|
+{
|
|
|
+ use RefreshDatabase;
|
|
|
+
|
|
|
+ protected $seed = true;
|
|
|
+
|
|
|
+ public function test_handle_imports_editable_maf_columns_and_excel_dates(): void
|
|
|
+ {
|
|
|
+ Storage::fake('upload');
|
|
|
+
|
|
|
+ $year = 2026;
|
|
|
+ $sku = ProductSKU::factory()->create([
|
|
|
+ 'year' => $year,
|
|
|
+ 'rfid' => 'old-rfid',
|
|
|
+ 'factory_number' => 'old-factory',
|
|
|
+ 'manufacture_date' => null,
|
|
|
+ 'statement_number' => null,
|
|
|
+ 'statement_date' => null,
|
|
|
+ 'upd_number' => null,
|
|
|
+ 'comment' => null,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $filename = $this->createImportFile([
|
|
|
+ $sku->id,
|
|
|
+ $year,
|
|
|
+ 'ЦАО',
|
|
|
+ 'Тверской',
|
|
|
+ 'ул. Тестовая, 1',
|
|
|
+ 'МАФ-ORDER-1',
|
|
|
+ 'требуется',
|
|
|
+ 'rfid-new',
|
|
|
+ 'factory-new',
|
|
|
+ DateHelper::ISODateToExcelDate('2025-01-15'),
|
|
|
+ 'statement-new',
|
|
|
+ DateHelper::ISODateToExcelDate('2025-02-20'),
|
|
|
+ 'upd-new',
|
|
|
+ 'NOM-1',
|
|
|
+ 'ART-1',
|
|
|
+ 'Название',
|
|
|
+ 'Тип ТЗ',
|
|
|
+ 'Тип',
|
|
|
+ 'Производитель',
|
|
|
+ 'comment-new',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $import = Import::factory()->mafs()->create([
|
|
|
+ 'year' => $year,
|
|
|
+ 'filename' => $filename,
|
|
|
+ 'result' => '',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $this->assertTrue((new ImportMafsService($import, $year))->handle());
|
|
|
+
|
|
|
+ $sku->refresh();
|
|
|
+ $this->assertSame('rfid-new', $sku->rfid);
|
|
|
+ $this->assertSame('factory-new', $sku->factory_number);
|
|
|
+ $this->assertSame('2025-01-15', $sku->manufacture_date);
|
|
|
+ $this->assertSame('statement-new', $sku->statement_number);
|
|
|
+ $this->assertSame('2025-02-20', $sku->statement_date);
|
|
|
+ $this->assertSame('upd-new', $sku->upd_number);
|
|
|
+ $this->assertSame('comment-new', $sku->comment);
|
|
|
+ $this->assertSame('DONE', $import->fresh()->status);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_handle_imports_maf_string_dates(): void
|
|
|
+ {
|
|
|
+ Storage::fake('upload');
|
|
|
+
|
|
|
+ $year = 2026;
|
|
|
+ $sku = ProductSKU::factory()->create([
|
|
|
+ 'year' => $year,
|
|
|
+ 'manufacture_date' => null,
|
|
|
+ 'statement_date' => null,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $filename = $this->createImportFile([
|
|
|
+ $sku->id,
|
|
|
+ $year,
|
|
|
+ '',
|
|
|
+ '',
|
|
|
+ '',
|
|
|
+ '',
|
|
|
+ '',
|
|
|
+ '',
|
|
|
+ '',
|
|
|
+ '15.01.2025',
|
|
|
+ '',
|
|
|
+ '2025-02-20',
|
|
|
+ '',
|
|
|
+ '',
|
|
|
+ '',
|
|
|
+ '',
|
|
|
+ '',
|
|
|
+ '',
|
|
|
+ '',
|
|
|
+ '',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $import = Import::factory()->mafs()->create([
|
|
|
+ 'year' => $year,
|
|
|
+ 'filename' => $filename,
|
|
|
+ 'result' => '',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $this->assertTrue((new ImportMafsService($import, $year))->handle());
|
|
|
+
|
|
|
+ $sku->refresh();
|
|
|
+ $this->assertSame('2025-01-15', $sku->manufacture_date);
|
|
|
+ $this->assertSame('2025-02-20', $sku->statement_date);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function createImportFile(array $row): string
|
|
|
+ {
|
|
|
+ $spreadsheet = new Spreadsheet();
|
|
|
+ $sheet = $spreadsheet->getActiveSheet();
|
|
|
+
|
|
|
+ $headers = array_keys(ImportMafsService::HEADERS);
|
|
|
+ foreach ($headers as $index => $header) {
|
|
|
+ $sheet->setCellValue([$index + 1, 1], $header);
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($row as $index => $value) {
|
|
|
+ $sheet->setCellValue([$index + 1, 2], $value);
|
|
|
+ }
|
|
|
+
|
|
|
+ $filename = 'imports/mafs.xlsx';
|
|
|
+ Storage::disk('upload')->makeDirectory('imports');
|
|
|
+ (new Xlsx($spreadsheet))->save(Storage::disk('upload')->path($filename));
|
|
|
+
|
|
|
+ return $filename;
|
|
|
+ }
|
|
|
+}
|