|
|
@@ -0,0 +1,98 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Tests\Unit\Services\Import;
|
|
|
+
|
|
|
+use App\Models\PricingCode;
|
|
|
+use App\Models\SparePart;
|
|
|
+use App\Services\Import\ImportSparePartsService;
|
|
|
+use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
+use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
+use Tests\TestCase;
|
|
|
+
|
|
|
+class ImportSparePartsServiceTest extends TestCase
|
|
|
+{
|
|
|
+ use RefreshDatabase;
|
|
|
+
|
|
|
+ protected $seed = true;
|
|
|
+
|
|
|
+ private string $tempFilePath;
|
|
|
+
|
|
|
+ protected function tearDown(): void
|
|
|
+ {
|
|
|
+ if (isset($this->tempFilePath) && file_exists($this->tempFilePath)) {
|
|
|
+ unlink($this->tempFilePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ parent::tearDown();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_import_stores_prices_in_kopecks_from_rubles(): void
|
|
|
+ {
|
|
|
+ $this->createTestFile([
|
|
|
+ ['SP-IMPORT-001', 'Для теста', 'Заметка', 1500, 1999.99, 0, 'ТСН-1', 'PC-001', 3],
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $service = new ImportSparePartsService($this->tempFilePath, 1);
|
|
|
+ $result = $service->handle();
|
|
|
+
|
|
|
+ $this->assertTrue($result['success']);
|
|
|
+
|
|
|
+ $sparePart = SparePart::where('article', 'SP-IMPORT-001')->firstOrFail();
|
|
|
+
|
|
|
+ $this->assertSame(150000, $sparePart->getRawOriginal('purchase_price'));
|
|
|
+ $this->assertSame(199999, $sparePart->getRawOriginal('customer_price'));
|
|
|
+ $this->assertSame(0, $sparePart->getRawOriginal('expertise_price'));
|
|
|
+ $this->assertEquals(1500.0, $sparePart->purchase_price);
|
|
|
+ $this->assertEquals(1999.99, $sparePart->customer_price);
|
|
|
+ $this->assertEquals(0.0, $sparePart->expertise_price);
|
|
|
+ $this->assertDatabaseHas('pricing_codes', [
|
|
|
+ 'type' => PricingCode::TYPE_PRICING_CODE,
|
|
|
+ 'code' => 'PC-001',
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function createTestFile(array $rows): void
|
|
|
+ {
|
|
|
+ $spreadsheet = new Spreadsheet();
|
|
|
+ $sheet = $spreadsheet->getActiveSheet();
|
|
|
+
|
|
|
+ $sheet->setCellValue('A1', 'ID');
|
|
|
+ $sheet->setCellValue('B1', 'Артикул');
|
|
|
+ $sheet->setCellValue('C1', 'Где используется');
|
|
|
+ $sheet->setCellValue('D1', 'Кол-во без док');
|
|
|
+ $sheet->setCellValue('E1', 'Кол-во с док');
|
|
|
+ $sheet->setCellValue('F1', 'Кол-во общее');
|
|
|
+ $sheet->setCellValue('G1', 'Примечание');
|
|
|
+ $sheet->setCellValue('H1', 'Цена закупки');
|
|
|
+ $sheet->setCellValue('I1', 'Цена для заказчика');
|
|
|
+ $sheet->setCellValue('J1', 'Цена экспертизы');
|
|
|
+ $sheet->setCellValue('K1', '№ по ТСН');
|
|
|
+ $sheet->setCellValue('L1', 'Шифры расценки');
|
|
|
+ $sheet->setCellValue('M1', 'Мин. остаток');
|
|
|
+
|
|
|
+ $rowNum = 2;
|
|
|
+ foreach ($rows as $row) {
|
|
|
+ $sheet->setCellValue('B' . $rowNum, $row[0] ?? '');
|
|
|
+ $sheet->setCellValue('C' . $rowNum, $row[1] ?? '');
|
|
|
+ $sheet->setCellValue('G' . $rowNum, $row[2] ?? '');
|
|
|
+ $sheet->setCellValue('H' . $rowNum, $row[3] ?? '');
|
|
|
+ $sheet->setCellValue('I' . $rowNum, $row[4] ?? '');
|
|
|
+ $sheet->setCellValue('J' . $rowNum, $row[5] ?? '');
|
|
|
+ $sheet->setCellValue('K' . $rowNum, $row[6] ?? '');
|
|
|
+ $sheet->setCellValue('L' . $rowNum, $row[7] ?? '');
|
|
|
+ $sheet->setCellValue('M' . $rowNum, $row[8] ?? 0);
|
|
|
+ $rowNum++;
|
|
|
+ }
|
|
|
+
|
|
|
+ $pricingCodesSheet = $spreadsheet->createSheet(1);
|
|
|
+ $pricingCodesSheet->setCellValue('A1', 'ID');
|
|
|
+ $pricingCodesSheet->setCellValue('B1', 'Тип');
|
|
|
+ $pricingCodesSheet->setCellValue('C1', 'Код');
|
|
|
+ $pricingCodesSheet->setCellValue('D1', 'Расшифровка');
|
|
|
+
|
|
|
+ $this->tempFilePath = sys_get_temp_dir() . '/test_spare_parts_' . uniqid() . '.xlsx';
|
|
|
+ $writer = new Xlsx($spreadsheet);
|
|
|
+ $writer->save($this->tempFilePath);
|
|
|
+ }
|
|
|
+}
|