整表物料源逐行修正有效物料、成本和发货金额,#N/A 行保持原样,页面优先使用修正物料。 验证:导入器 5 个测试、53 条断言;页面 9 个测试、112 条断言;生产审计 8,837 行有效、5,112 行 #N/A。
471 lines
17 KiB
PHP
471 lines
17 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Business;
|
|
|
|
use App\Business\KukaPostingBase\KukaPostingMaterialRepairImporter;
|
|
use App\Business\XlsxSheetReader;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use RuntimeException;
|
|
use Tests\TestCase;
|
|
use ZipArchive;
|
|
|
|
class KukaPostingMaterialRepairImporterTest extends TestCase
|
|
{
|
|
/**
|
|
* @var list<string>
|
|
*/
|
|
private array $temporaryFiles = [];
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
config([
|
|
'database.connections.business_mirror' => [
|
|
'driver' => 'sqlite',
|
|
'database' => ':memory:',
|
|
'prefix' => '',
|
|
],
|
|
'upstream_mirrors.connection' => 'business_mirror',
|
|
]);
|
|
DB::purge('business_mirror');
|
|
|
|
Schema::connection('business_mirror')->dropIfExists('kuka_posting_base_rows');
|
|
Schema::connection('business_mirror')->create('kuka_posting_base_rows', function (Blueprint $table): void {
|
|
$table->bigIncrements('id');
|
|
$table->unsignedBigInteger('company_id');
|
|
$table->char('period', 7);
|
|
$table->string('store_name', 191);
|
|
$table->string('internal_order_no', 191)->nullable();
|
|
$table->string('online_order_no', 191)->nullable();
|
|
$table->string('product_code', 191)->nullable();
|
|
$table->char('row_hash', 64);
|
|
$table->longText('source_values');
|
|
$table->unsignedInteger('source_row_number');
|
|
$table->timestamp('imported_at');
|
|
|
|
$table->unique(
|
|
['company_id', 'period', 'source_row_number'],
|
|
'kuka_posting_material_test_row_unique',
|
|
);
|
|
});
|
|
Schema::connection('business_mirror')->dropIfExists('kuka_posting_quotation_rows');
|
|
Schema::connection('business_mirror')->create('kuka_posting_quotation_rows', function (Blueprint $table): void {
|
|
$table->bigIncrements('id');
|
|
$table->unsignedBigInteger('company_id');
|
|
$table->string('material_number', 191);
|
|
$table->string('material_desc', 500)->nullable();
|
|
$table->string('record_status', 64)->nullable();
|
|
$table->decimal('price_including_tax', 18, 4)->nullable();
|
|
});
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
foreach ($this->temporaryFiles as $file) {
|
|
@unlink($file);
|
|
}
|
|
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_updates_only_abnormal_material_fields_and_preserves_everything_else(): void
|
|
{
|
|
$this->seedBaseRows();
|
|
$before = $this->storedRows();
|
|
$abnormal = $this->workbook([
|
|
$this->row('O-1', 'P-1', 'W-1', '是'),
|
|
$this->row('O-1', 'P-1', 'W-1', '是'),
|
|
$this->row('O-2', 'P-2', 'W-2', '补单(订单四组)', quantity: '2'),
|
|
]);
|
|
$mapping = $this->workbook([
|
|
$this->row('O-1', 'P-1', 'W-1', '是', '91.M1', 'DS-M1~物料一'),
|
|
$this->row('O-2', 'P-2', 'W-2', '补单(订单四组)', '91.M2', 'DS-M2~物料二', '2'),
|
|
]);
|
|
|
|
$result = $this->importer()->import(
|
|
$abnormal,
|
|
$mapping,
|
|
'2026-10',
|
|
'Sheet1',
|
|
1,
|
|
);
|
|
|
|
$this->assertSame(3, $result['rows']);
|
|
$this->assertSame(2, $result['identities']);
|
|
$this->assertSame(2, $result['material_pairs']);
|
|
$this->assertSame(3, $result['updated']);
|
|
$this->assertSame(0, $result['unchanged']);
|
|
|
|
$after = $this->storedRows();
|
|
$expected = [
|
|
2 => ['91.M1', 'DS-M1~物料一'],
|
|
3 => ['91.M1', 'DS-M1~物料一'],
|
|
4 => ['91.M2', 'DS-M2~物料二'],
|
|
];
|
|
foreach ($expected as $sourceRowNumber => [$materialNumber, $description]) {
|
|
$this->assertSame(
|
|
$before[$sourceRowNumber]['id'],
|
|
$after[$sourceRowNumber]['id'],
|
|
);
|
|
$this->assertSame(
|
|
$before[$sourceRowNumber]['imported_at'],
|
|
$after[$sourceRowNumber]['imported_at'],
|
|
);
|
|
|
|
$payload = $before[$sourceRowNumber]['payload'];
|
|
$payload['物料编号'] = $materialNumber;
|
|
$payload['型号'] = $description;
|
|
$payload['成本'] = $sourceRowNumber === 4 ? '300.0000' : '125.5000';
|
|
$payload['发货金额'] = $sourceRowNumber === 4 ? '600.0000' : '125.5000';
|
|
$payload['物料信息修正'] = '是';
|
|
$this->assertSame($payload, $after[$sourceRowNumber]['payload']);
|
|
}
|
|
|
|
$this->assertSame('', $after[5]['payload']['物料编号']);
|
|
$this->assertSame('', $after[5]['payload']['型号']);
|
|
$this->assertSame($before[5], $after[5]);
|
|
|
|
foreach ($after as $row) {
|
|
$this->assertSame(
|
|
hash('sha256', (string) json_encode(
|
|
$row['payload'],
|
|
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR,
|
|
)),
|
|
$row['row_hash'],
|
|
);
|
|
}
|
|
|
|
$second = $this->importer()->import(
|
|
$abnormal,
|
|
$mapping,
|
|
'2026-10',
|
|
'Sheet1',
|
|
1,
|
|
);
|
|
$this->assertSame(0, $second['updated']);
|
|
$this->assertSame(3, $second['unchanged']);
|
|
$this->assertSame($after, $this->storedRows());
|
|
}
|
|
|
|
public function test_dry_run_reports_changes_without_writing_them(): void
|
|
{
|
|
$this->seedBaseRows();
|
|
$before = $this->storedRows();
|
|
$abnormal = $this->workbook([
|
|
$this->row('O-1', 'P-1', 'W-1', '是'),
|
|
$this->row('O-1', 'P-1', 'W-1', '是'),
|
|
$this->row('O-2', 'P-2', 'W-2', '补单(订单四组)', quantity: '2'),
|
|
]);
|
|
$mapping = $this->workbook([
|
|
$this->row('O-1', 'P-1', 'W-1', '是', '91.M1', 'DS-M1~物料一'),
|
|
$this->row('O-2', 'P-2', 'W-2', '补单(订单四组)', '91.M2', 'DS-M2~物料二', '2'),
|
|
]);
|
|
|
|
$result = $this->importer()->import(
|
|
$abnormal,
|
|
$mapping,
|
|
'2026-10',
|
|
'Sheet1',
|
|
1,
|
|
true,
|
|
);
|
|
|
|
$this->assertTrue($result['dry_run']);
|
|
$this->assertSame(3, $result['updated']);
|
|
$this->assertSame($before, $this->storedRows());
|
|
}
|
|
|
|
public function test_source_import_updates_valid_rows_in_order_and_skips_na_rows(): void
|
|
{
|
|
$this->seedBaseRows();
|
|
$before = $this->storedRows();
|
|
$source = $this->workbook([
|
|
$this->row('O-1', 'P-1', 'W-1', '是', '91.M1', 'DS-M1~物料一'),
|
|
$this->row('O-1', 'P-1', 'W-1', '是', '#N/A', '#N/A'),
|
|
$this->row('O-2', 'P-2', 'W-2', '补单(订单四组)', '91.M2', 'DS-M2~物料二', '2'),
|
|
$this->row('O-1', 'P-1', 'W-OTHER', '是', '91.M1', 'DS-M1~物料一'),
|
|
]);
|
|
|
|
$result = $this->importer()->importSource(
|
|
$source,
|
|
'2026-10',
|
|
'Sheet1',
|
|
1,
|
|
);
|
|
|
|
$this->assertSame(4, $result['rows']);
|
|
$this->assertSame(3, $result['valid_rows']);
|
|
$this->assertSame(1, $result['skipped_unmapped_rows']);
|
|
$this->assertSame(2, $result['materials']);
|
|
$this->assertSame(3, $result['updated']);
|
|
$this->assertSame(0, $result['unchanged']);
|
|
|
|
$after = $this->storedRows();
|
|
$this->assertSame($before[3], $after[3]);
|
|
foreach ([2, 4, 5] as $sourceRowNumber) {
|
|
$payload = $before[$sourceRowNumber]['payload'];
|
|
$payload['物料编号'] = $sourceRowNumber === 4 ? '91.M2' : '91.M1';
|
|
$payload['型号'] = $sourceRowNumber === 4 ? 'DS-M2~物料二' : 'DS-M1~物料一';
|
|
$payload['成本'] = $sourceRowNumber === 4 ? '300.0000' : '125.5000';
|
|
$payload['发货金额'] = $sourceRowNumber === 4 ? '600.0000' : '125.5000';
|
|
$payload['物料信息修正'] = '是';
|
|
$this->assertSame($payload, $after[$sourceRowNumber]['payload']);
|
|
}
|
|
|
|
$second = $this->importer()->importSource($source, '2026-10', 'Sheet1');
|
|
$this->assertSame(0, $second['updated']);
|
|
$this->assertSame(3, $second['unchanged']);
|
|
$this->assertSame($after, $this->storedRows());
|
|
}
|
|
|
|
public function test_conflicting_material_mapping_rejects_the_file_before_writing(): void
|
|
{
|
|
$this->seedBaseRows();
|
|
$abnormal = $this->workbook([
|
|
$this->row('O-1', 'P-1', 'W-1', '是'),
|
|
]);
|
|
$mapping = $this->workbook([
|
|
$this->row('O-1', 'P-1', 'W-1', '是', '91.M1', 'DS-M1~物料一'),
|
|
$this->row('O-1', 'P-1', 'W-1', '是', '91.M9', 'DS-M9~冲突物料'),
|
|
]);
|
|
|
|
$this->expectException(RuntimeException::class);
|
|
$this->expectExceptionMessage('多个物料编号/物料描述组合');
|
|
|
|
$this->importer()->import($abnormal, $mapping, '2026-10', 'Sheet1');
|
|
}
|
|
|
|
public function test_production_row_count_must_match_the_abnormal_list_before_writing(): void
|
|
{
|
|
$this->seedBaseRows();
|
|
$before = $this->storedRows();
|
|
$abnormal = $this->workbook([
|
|
$this->row('O-1', 'P-1', 'W-1', '是'),
|
|
]);
|
|
$mapping = $this->workbook([
|
|
$this->row('O-1', 'P-1', 'W-1', '是', '91.M1', 'DS-M1~物料一'),
|
|
]);
|
|
|
|
try {
|
|
$this->importer()->import($abnormal, $mapping, '2026-10', 'Sheet1');
|
|
$this->fail('生产行数多于异常清单时不应写入。');
|
|
} catch (RuntimeException $exception) {
|
|
$this->assertStringContainsString('多条与异常清单不匹配', $exception->getMessage());
|
|
}
|
|
|
|
$this->assertSame($before, $this->storedRows());
|
|
}
|
|
|
|
private function importer(): KukaPostingMaterialRepairImporter
|
|
{
|
|
return new KukaPostingMaterialRepairImporter(app('db'), new XlsxSheetReader());
|
|
}
|
|
|
|
private function seedBaseRows(): void
|
|
{
|
|
$this->insertBaseRow(2, $this->payload('O-1', 'P-1', 'W-1', '是'));
|
|
$this->insertBaseRow(3, $this->payload('O-1', 'P-1', 'W-1', '是'));
|
|
$payload = $this->payload('O-2', 'P-2', 'W-2', '补单(订单四组)');
|
|
$payload['实发数量'] = '2';
|
|
$this->insertBaseRow(4, $payload);
|
|
$this->insertBaseRow(5, $this->payload('O-1', 'P-1', 'W-OTHER', '是'));
|
|
$this->insertQuotation('91.M1', 'DS-M1~物料一', '125.5000');
|
|
$this->insertQuotation('91.M2', 'DS-M2~物料二', '300.0000');
|
|
$this->insertQuotation('91.M9', 'DS-M9~冲突物料', '999.0000');
|
|
}
|
|
|
|
private function insertQuotation(
|
|
string $materialNumber,
|
|
string $description,
|
|
string $price,
|
|
): void {
|
|
DB::connection('business_mirror')->table('kuka_posting_quotation_rows')->insert([
|
|
'company_id' => 1,
|
|
'material_number' => $materialNumber,
|
|
'material_desc' => $description,
|
|
'record_status' => '生效',
|
|
'price_including_tax' => $price,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
private function payload(
|
|
string $onlineOrderNo,
|
|
string $productCode,
|
|
string $warehouse,
|
|
string $postingLabel,
|
|
): array {
|
|
return [
|
|
'发货日期' => '2026-10-31 16:12:55',
|
|
'线上订单号' => $onlineOrderNo,
|
|
'订单状态' => '已发货',
|
|
'发货仓' => $warehouse,
|
|
'店铺' => '天猫-HX旗舰店101',
|
|
'商品编码' => $productCode,
|
|
'款式编码' => 'STYLE-1',
|
|
'货号' => 'ITEM-1',
|
|
'物料编号' => '',
|
|
'型号' => '',
|
|
'成本' => '',
|
|
'产品材质' => '',
|
|
'实发数量' => '1',
|
|
'发货金额' => '',
|
|
'过账标签' => $postingLabel,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param array<string, string> $payload
|
|
*/
|
|
private function insertBaseRow(int $sourceRowNumber, array $payload): void
|
|
{
|
|
$encoded = json_encode(
|
|
$payload,
|
|
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR,
|
|
);
|
|
DB::connection('business_mirror')->table('kuka_posting_base_rows')->insert([
|
|
'company_id' => 1,
|
|
'period' => '2026-10',
|
|
'store_name' => $payload['店铺'],
|
|
'internal_order_no' => 'INTERNAL-'.$sourceRowNumber,
|
|
'online_order_no' => $payload['线上订单号'],
|
|
'product_code' => $payload['商品编码'],
|
|
'row_hash' => hash('sha256', $encoded),
|
|
'source_values' => $encoded,
|
|
'source_row_number' => $sourceRowNumber,
|
|
'imported_at' => now(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array{id: int, row_hash: string, imported_at: string, payload: array<string, string>}>
|
|
*/
|
|
private function storedRows(): array
|
|
{
|
|
return DB::connection('business_mirror')
|
|
->table('kuka_posting_base_rows')
|
|
->orderBy('source_row_number')
|
|
->get()
|
|
->mapWithKeys(static fn (object $row): array => [
|
|
(int) $row->source_row_number => [
|
|
'id' => (int) $row->id,
|
|
'row_hash' => (string) $row->row_hash,
|
|
'imported_at' => (string) $row->imported_at,
|
|
'payload' => json_decode(
|
|
(string) $row->source_values,
|
|
true,
|
|
512,
|
|
JSON_THROW_ON_ERROR,
|
|
),
|
|
],
|
|
])
|
|
->all();
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
private function row(
|
|
string $onlineOrderNo,
|
|
string $productCode,
|
|
string $warehouse,
|
|
string $postingLabel,
|
|
string $materialNumber = '—',
|
|
string $description = '—',
|
|
string $quantity = '1',
|
|
): array {
|
|
return [
|
|
'发货日期' => '46326.6756365741',
|
|
'线上订单号' => $onlineOrderNo,
|
|
'商品编码' => $productCode,
|
|
'订单状态' => '已发货',
|
|
'发货仓' => $warehouse,
|
|
'店铺' => '天猫-HX旗舰店101',
|
|
'款式编码' => 'STYLE-1',
|
|
'实发数量' => $quantity,
|
|
'过账标签' => $postingLabel,
|
|
'物料编号' => $materialNumber,
|
|
'物料描述' => $description,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param list<array<string, string>> $rows
|
|
*/
|
|
private function workbook(array $rows): string
|
|
{
|
|
$headers = [
|
|
'发货日期',
|
|
'线上订单号',
|
|
'商品编码',
|
|
'订单状态',
|
|
'发货仓',
|
|
'店铺',
|
|
'款式编码',
|
|
'实发数量',
|
|
'过账标签',
|
|
'物料编号',
|
|
'物料描述',
|
|
];
|
|
$path = tempnam(sys_get_temp_dir(), 'kuka-material-repair-').'.xlsx';
|
|
$this->temporaryFiles[] = $path;
|
|
|
|
$zip = new ZipArchive();
|
|
$this->assertTrue($zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true);
|
|
$zip->addFromString(
|
|
'xl/worksheets/sheet1.xml',
|
|
$this->worksheetXml(array_merge([$headers], $rows), $headers),
|
|
);
|
|
$zip->close();
|
|
|
|
return $path;
|
|
}
|
|
|
|
/**
|
|
* @param list<array<int, string>|array<string, string>> $rows
|
|
* @param list<string> $headers
|
|
*/
|
|
private function worksheetXml(array $rows, array $headers): string
|
|
{
|
|
$xmlRows = [];
|
|
foreach ($rows as $rowIndex => $row) {
|
|
$values = array_is_list($row)
|
|
? $row
|
|
: array_map(static fn (string $header): string => $row[$header] ?? '', $headers);
|
|
$cells = [];
|
|
foreach ($values as $columnIndex => $value) {
|
|
$reference = $this->columnName($columnIndex + 1).($rowIndex + 1);
|
|
if ($rowIndex > 0 && $headers[$columnIndex] === '发货日期') {
|
|
$cells[] = '<c r="'.$reference.'"><v>'.$value.'</v></c>';
|
|
} else {
|
|
$cells[] = '<c r="'.$reference.'" t="inlineStr"><is><t>'
|
|
.htmlspecialchars((string) $value, ENT_XML1 | ENT_QUOTES, 'UTF-8')
|
|
.'</t></is></c>';
|
|
}
|
|
}
|
|
$xmlRows[] = '<row r="'.($rowIndex + 1).'">'.implode('', $cells).'</row>';
|
|
}
|
|
|
|
return '<?xml version="1.0" encoding="UTF-8"?>'
|
|
.'<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">'
|
|
.'<sheetData>'.implode('', $xmlRows).'</sheetData></worksheet>';
|
|
}
|
|
|
|
private function columnName(int $index): string
|
|
{
|
|
$name = '';
|
|
while ($index > 0) {
|
|
$index--;
|
|
$name = chr(65 + ($index % 26)).$name;
|
|
$index = intdiv($index, 26);
|
|
}
|
|
|
|
return $name;
|
|
}
|
|
}
|