| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace Tests\Unit\Services\Export;
- use App\Models\File;
- use App\Models\Order;
- use App\Models\Product;
- use App\Models\ProductSKU;
- use App\Models\User;
- use App\Services\ExportMafRegistryService;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\Storage;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use Tests\TestCase;
- class ExportMafRegistryServiceTest extends TestCase
- {
- use RefreshDatabase;
- protected $seed = true;
- public function test_handle_exports_matching_mafs_and_sets_upd_number(): void
- {
- if (!file_exists('./templates/MafRegistry.xlsx')) {
- $this->markTestSkipped('Excel template MafRegistry.xlsx not found');
- }
- Storage::fake('public');
- $user = User::factory()->create();
- $year = (int) date('Y');
- $order = Order::factory()->create([
- 'year' => $year,
- 'name' => 'Площадка 1',
- ]);
- $product = Product::factory()->create([
- 'year' => $year,
- 'article' => 'ART-001',
- 'nomenclature_number' => 'NOM-001',
- 'statement_name' => 'Наименование в ведомости',
- 'total_price' => 1234.56,
- ]);
- $targetSku = ProductSKU::factory()
- ->forOrder($order)
- ->forProduct($product)
- ->create([
- 'year' => $year,
- 'statement_number' => 'STAT-001',
- 'statement_date' => '2026-05-19',
- 'upd_number' => null,
- ]);
- $blankStatementSku = ProductSKU::factory()->create([
- 'year' => $year,
- 'statement_number' => ' ',
- 'upd_number' => null,
- ]);
- $alreadyExportedSku = ProductSKU::factory()->create([
- 'year' => $year,
- 'statement_number' => 'STAT-002',
- 'upd_number' => 'OLD-UPD',
- ]);
- $link = (new ExportMafRegistryService())->handle($user->id, 'UPD-001', $year);
- $this->assertStringContainsString('/storage/export/maf-registry/', $link);
- $this->assertSame('UPD-001', $targetSku->fresh()->upd_number);
- $this->assertNull($blankStatementSku->fresh()->upd_number);
- $this->assertSame('OLD-UPD', $alreadyExportedSku->fresh()->upd_number);
- $file = File::query()->where('user_id', $user->id)->latest()->firstOrFail();
- $this->assertTrue($file->is_generated);
- Storage::disk('public')->assertExists($file->path);
- $spreadsheet = IOFactory::load(Storage::disk('public')->path($file->path));
- $sheet = $spreadsheet->getActiveSheet();
- $this->assertSame('НАШ ДВОР', $sheet->getTitle());
- $this->assertSame('Реестр оборудования и ведомостей "технической приемки" к УПД UPD-001', $sheet->getCell('C2')->getValue());
- $this->assertSame('НАШ ДВОР', $sheet->getCell('A4')->getValue());
- $this->assertSame('UPD-001', $sheet->getCell('C4')->getValue());
- $this->assertSame('NOM-001', $sheet->getCell('D4')->getValue());
- $this->assertSame('Наименование в ведомости', $sheet->getCell('E4')->getValue());
- $this->assertSame('ART-001', $sheet->getCell('F4')->getValue());
- $this->assertSame('STAT-001', $sheet->getCell('G4')->getValue());
- $this->assertSame('Площадка 1', $sheet->getCell('I4')->getValue());
- $this->assertSame(1, $sheet->getCell('M4')->getValue());
- $this->assertSame(0, $sheet->getCell('N4')->getValue());
- $this->assertSame(1234.56, $sheet->getCell('O4')->getValue());
- $this->assertSame('С бетоном', $sheet->getCell('P4')->getValue());
- $this->assertSame('=SUM(M4:M4)', $sheet->getCell('M1')->getValue());
- $this->assertSame(0, $sheet->getCell('N1')->getValue());
- $this->assertSame('=SUM(O4:O4)', $sheet->getCell('O1')->getValue());
- }
- }
|