feat(顾家过账): 同步修正异常物料成本和金额 #1269
@ -100,9 +100,23 @@ class KukaPostingBasePageData
|
||||
$companyId,
|
||||
$decodedRows->pluck('item_number')->filter()->unique()->values()->all(),
|
||||
);
|
||||
$repairedMaterialNumbers = $decodedRows
|
||||
->filter(static fn (array $row): bool => trim((string) (
|
||||
$row['values']['物料信息修正'] ?? ''
|
||||
)) === '是')
|
||||
->map(static fn (array $row): string => trim((string) (
|
||||
$row['values']['物料编号'] ?? ''
|
||||
)))
|
||||
->filter();
|
||||
$quotations = $this->repository->effectiveQuotations(
|
||||
$companyId,
|
||||
collect($maps)->pluck('material_number')->filter()->unique()->values()->all(),
|
||||
collect($maps)
|
||||
->pluck('material_number')
|
||||
->filter()
|
||||
->merge($repairedMaterialNumbers)
|
||||
->unique()
|
||||
->values()
|
||||
->all(),
|
||||
);
|
||||
$rows->setCollection($decodedRows->map(function (array $row) use ($maps, $brandItems, $itemBrandItems, $quotations): object {
|
||||
$values = $row['values'];
|
||||
@ -115,7 +129,10 @@ class KukaPostingBasePageData
|
||||
if (in_array($storedMaterialDescription, ['', '—', '-'], true)) {
|
||||
$storedMaterialDescription = '';
|
||||
}
|
||||
$materialNumber = trim((string) ($maps[$row['item_number']]->material_number ?? ''));
|
||||
$mappedMaterialNumber = trim((string) ($maps[$row['item_number']]->material_number ?? ''));
|
||||
$materialNumber = $mappedMaterialNumber !== ''
|
||||
? $mappedMaterialNumber
|
||||
: ($usesRepairedMaterial ? $storedMaterialNumber : '');
|
||||
$quotation = $materialNumber === '' ? null : ($quotations[$materialNumber] ?? null);
|
||||
$brandItemNumber = trim((string) (
|
||||
$brandItems[$row['style_code']]->brand_item_number ?? ''
|
||||
@ -145,7 +162,7 @@ class KukaPostingBasePageData
|
||||
$values['发货金额'] = $cost !== null && $quantity !== null
|
||||
? $this->multiplyDecimal($cost, $quantity)
|
||||
: '';
|
||||
} elseif ($materialNumber === '' && $usesRepairedMaterial) {
|
||||
} elseif ($mappedMaterialNumber === '' && $usesRepairedMaterial) {
|
||||
$values['物料编号'] = $storedMaterialNumber;
|
||||
$values['型号'] = $storedMaterialDescription;
|
||||
}
|
||||
|
||||
@ -29,6 +29,10 @@ class KukaPostingMaterialRepairImporter
|
||||
|
||||
private const MATERIAL_DESCRIPTION_DATABASE_FIELD = '型号';
|
||||
|
||||
private const COST_FIELD = '成本';
|
||||
|
||||
private const SHIPPING_AMOUNT_FIELD = '发货金额';
|
||||
|
||||
private const REPAIR_MARKER_FIELD = '物料信息修正';
|
||||
|
||||
private const MISSING_VALUES = ['', '—', '-'];
|
||||
@ -103,9 +107,21 @@ class KukaPostingMaterialRepairImporter
|
||||
$materials,
|
||||
$allowedStores,
|
||||
);
|
||||
$quotations = $this->loadQuotations(
|
||||
$connection,
|
||||
$companyId,
|
||||
array_values(array_unique(array_column($records, 'material_number'))),
|
||||
);
|
||||
|
||||
if ($dryRun) {
|
||||
$collected = $this->collectTargets($connection, $records, $period, $companyId, false);
|
||||
$collected = $this->collectTargets(
|
||||
$connection,
|
||||
$records,
|
||||
$quotations,
|
||||
$period,
|
||||
$companyId,
|
||||
false,
|
||||
);
|
||||
$result = $collected['result'];
|
||||
} else {
|
||||
$result = [];
|
||||
@ -113,12 +129,14 @@ class KukaPostingMaterialRepairImporter
|
||||
&$result,
|
||||
$connection,
|
||||
$records,
|
||||
$quotations,
|
||||
$period,
|
||||
$companyId,
|
||||
): void {
|
||||
$targets = $this->collectTargets(
|
||||
$connection,
|
||||
$records,
|
||||
$quotations,
|
||||
$period,
|
||||
$companyId,
|
||||
true,
|
||||
@ -254,8 +272,65 @@ class KukaPostingMaterialRepairImporter
|
||||
return $records;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $materialNumbers
|
||||
* @return array<string, array{price: string, description: string}>
|
||||
*/
|
||||
private function loadQuotations(
|
||||
ConnectionInterface $connection,
|
||||
int $companyId,
|
||||
array $materialNumbers,
|
||||
): array {
|
||||
if (! Schema::connection($this->connectionName())->hasTable('kuka_posting_quotation_rows')) {
|
||||
throw new RuntimeException('顾家报价单表尚未建立,无法计算成本和发货金额。');
|
||||
}
|
||||
|
||||
$quotations = [];
|
||||
$rows = $connection->table('kuka_posting_quotation_rows')
|
||||
->where('company_id', $companyId)
|
||||
->where('record_status', '生效')
|
||||
->whereIn('material_number', $materialNumbers)
|
||||
->orderBy('material_number')
|
||||
->get(['material_number', 'material_desc', 'price_including_tax']);
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$materialNumber = trim((string) $row->material_number);
|
||||
if (isset($quotations[$materialNumber])) {
|
||||
throw new RuntimeException(sprintf(
|
||||
'物料编号 %s 存在多条生效报价,无法确定唯一成本。',
|
||||
$materialNumber,
|
||||
));
|
||||
}
|
||||
$price = $this->normalizeDecimal($row->price_including_tax);
|
||||
if ($price === null) {
|
||||
throw new RuntimeException(sprintf(
|
||||
'物料编号 %s 的生效报价含税价为空或格式无效。',
|
||||
$materialNumber,
|
||||
));
|
||||
}
|
||||
$price = $this->formatDecimal($price);
|
||||
|
||||
$quotations[$materialNumber] = [
|
||||
'price' => $price,
|
||||
'description' => trim((string) ($row->material_desc ?? '')),
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($materialNumbers as $materialNumber) {
|
||||
if (! isset($quotations[$materialNumber])) {
|
||||
throw new RuntimeException(sprintf(
|
||||
'物料编号 %s 没有生效报价,无法计算成本和发货金额。',
|
||||
$materialNumber,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
return $quotations;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array{excel_row_number: int, key: string, material_number: string, description: string}> $records
|
||||
* @param array<string, array{price: string, description: string}> $quotations
|
||||
* @return array{
|
||||
* result: array{
|
||||
* rows: int,
|
||||
@ -271,6 +346,7 @@ class KukaPostingMaterialRepairImporter
|
||||
private function collectTargets(
|
||||
ConnectionInterface $connection,
|
||||
array $records,
|
||||
array $quotations,
|
||||
string $period,
|
||||
int $companyId,
|
||||
bool $lockRows,
|
||||
@ -316,6 +392,8 @@ class KukaPostingMaterialRepairImporter
|
||||
$currentDescription = trim((string) (
|
||||
$values[self::MATERIAL_DESCRIPTION_DATABASE_FIELD] ?? ''
|
||||
));
|
||||
$currentCost = trim((string) ($values[self::COST_FIELD] ?? ''));
|
||||
$currentAmount = trim((string) ($values[self::SHIPPING_AMOUNT_FIELD] ?? ''));
|
||||
$isMarked = trim((string) ($values[self::REPAIR_MARKER_FIELD] ?? '')) === '是';
|
||||
$expected = $expectedMaterials[$key];
|
||||
$currentIsBlank = $this->isMissingMaterialValue($currentMaterial)
|
||||
@ -329,9 +407,22 @@ class KukaPostingMaterialRepairImporter
|
||||
));
|
||||
}
|
||||
|
||||
$quotation = $quotations[$expected['material_number']];
|
||||
$quantity = $this->normalizeDecimal($values['实发数量'] ?? null);
|
||||
if ($quantity === null) {
|
||||
throw new RuntimeException(sprintf(
|
||||
'共享镜像 source_row_number=%d 的实发数量无效,无法计算发货金额。',
|
||||
(int) $row->source_row_number,
|
||||
));
|
||||
}
|
||||
$expectedCost = $quotation['price'];
|
||||
$expectedAmount = $this->multiplyDecimal($expectedCost, $quantity);
|
||||
$financialMatches = $this->decimalEquals($currentCost, $expectedCost)
|
||||
&& $this->decimalEquals($currentAmount, $expectedAmount);
|
||||
|
||||
$store = trim((string) ($values['店铺'] ?? ''));
|
||||
$stores[$store] = ($stores[$store] ?? 0) + 1;
|
||||
if ($currentMatches && $isMarked) {
|
||||
if ($currentMatches && $isMarked && $financialMatches) {
|
||||
$unchanged++;
|
||||
} else {
|
||||
$updated++;
|
||||
@ -341,7 +432,9 @@ class KukaPostingMaterialRepairImporter
|
||||
'values' => $values,
|
||||
'material_number' => $expected['material_number'],
|
||||
'description' => $expected['description'],
|
||||
'changed' => ! $currentMatches || ! $isMarked,
|
||||
'cost' => $expectedCost,
|
||||
'shipping_amount' => $expectedAmount,
|
||||
'changed' => ! $currentMatches || ! $isMarked || ! $financialMatches,
|
||||
];
|
||||
}
|
||||
|
||||
@ -398,6 +491,8 @@ class KukaPostingMaterialRepairImporter
|
||||
$values = $target['values'];
|
||||
$values[self::MATERIAL_NUMBER_FIELD] = $target['material_number'];
|
||||
$values[self::MATERIAL_DESCRIPTION_DATABASE_FIELD] = $target['description'];
|
||||
$values[self::COST_FIELD] = $target['cost'];
|
||||
$values[self::SHIPPING_AMOUNT_FIELD] = $target['shipping_amount'];
|
||||
$values[self::REPAIR_MARKER_FIELD] = '是';
|
||||
$encoded = json_encode(
|
||||
$values,
|
||||
@ -463,6 +558,43 @@ class KukaPostingMaterialRepairImporter
|
||||
return in_array(trim($value), self::MISSING_VALUES, true);
|
||||
}
|
||||
|
||||
private function normalizeDecimal(mixed $value): ?string
|
||||
{
|
||||
$normalized = str_replace(',', '', trim((string) $value));
|
||||
|
||||
return $normalized !== '' && is_numeric($normalized) ? $normalized : null;
|
||||
}
|
||||
|
||||
private function multiplyDecimal(string $left, string $right): string
|
||||
{
|
||||
if (function_exists('bcmul')) {
|
||||
return bcmul($left, $right, 4);
|
||||
}
|
||||
|
||||
return number_format((float) $left * (float) $right, 4, '.', '');
|
||||
}
|
||||
|
||||
private function formatDecimal(string $value): string
|
||||
{
|
||||
if (function_exists('bcadd')) {
|
||||
return bcadd($value, '0', 4);
|
||||
}
|
||||
|
||||
return number_format((float) $value, 4, '.', '');
|
||||
}
|
||||
|
||||
private function decimalEquals(string $left, string $right): bool
|
||||
{
|
||||
if ($left === '' || $right === '') {
|
||||
return false;
|
||||
}
|
||||
if (function_exists('bccomp')) {
|
||||
return bccomp($left, $right, 4) === 0;
|
||||
}
|
||||
|
||||
return abs((float) $left - (float) $right) < 0.00005;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
|
||||
@ -259,6 +259,7 @@ class KukaPostingBasePageTest extends FeatureTestCase
|
||||
$sourceValues['物料编号'] = '91.MANUAL.00';
|
||||
$sourceValues['型号'] = 'DS-MANUAL~异常数据修正物料';
|
||||
$sourceValues['物料信息修正'] = '是';
|
||||
$sourceValues['实发数量'] = '2';
|
||||
$sourceValues['成本'] = '';
|
||||
$sourceValues['发货金额'] = '';
|
||||
$encoded = json_encode(
|
||||
@ -272,6 +273,19 @@ class KukaPostingBasePageTest extends FeatureTestCase
|
||||
'row_hash' => hash('sha256', $encoded),
|
||||
]);
|
||||
|
||||
$connection->table('kuka_posting_quotation_rows')->insert([
|
||||
'company_id' => $this->company->id,
|
||||
'material_number' => '91.MANUAL.00',
|
||||
'material_desc' => 'DS-MANUAL~异常数据修正物料',
|
||||
'record_status' => '生效',
|
||||
'price_including_tax' => '125.5000',
|
||||
'price_excluding_tax' => '111.0600',
|
||||
'row_hash' => str_repeat('d', 64),
|
||||
'source_values' => '{}',
|
||||
'source_row_number' => 6,
|
||||
'imported_at' => now(),
|
||||
]);
|
||||
|
||||
$this->loginAs()
|
||||
->get(route('business.pages.show', [
|
||||
'section' => 'posting-data',
|
||||
@ -281,6 +295,8 @@ class KukaPostingBasePageTest extends FeatureTestCase
|
||||
->assertOk()
|
||||
->assertSeeText('91.MANUAL.00')
|
||||
->assertSeeText('DS-MANUAL~异常数据修正物料')
|
||||
->assertSeeText('125.50')
|
||||
->assertSeeText('251.00')
|
||||
->assertDontSeeText('199.00');
|
||||
|
||||
@unlink($file);
|
||||
|
||||
@ -51,6 +51,15 @@ class KukaPostingMaterialRepairImporterTest extends TestCase
|
||||
'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
|
||||
@ -69,11 +78,11 @@ class KukaPostingMaterialRepairImporterTest extends TestCase
|
||||
$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', '补单(订单四组)'),
|
||||
$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~物料二'),
|
||||
$this->row('O-2', 'P-2', 'W-2', '补单(订单四组)', '91.M2', 'DS-M2~物料二', '2'),
|
||||
]);
|
||||
|
||||
$result = $this->importer()->import(
|
||||
@ -109,6 +118,8 @@ class KukaPostingMaterialRepairImporterTest extends TestCase
|
||||
$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']);
|
||||
}
|
||||
@ -146,11 +157,11 @@ class KukaPostingMaterialRepairImporterTest extends TestCase
|
||||
$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', '补单(订单四组)'),
|
||||
$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~物料二'),
|
||||
$this->row('O-2', 'P-2', 'W-2', '补单(订单四组)', '91.M2', 'DS-M2~物料二', '2'),
|
||||
]);
|
||||
|
||||
$result = $this->importer()->import(
|
||||
@ -214,8 +225,27 @@ class KukaPostingMaterialRepairImporterTest extends TestCase
|
||||
{
|
||||
$this->insertBaseRow(2, $this->payload('O-1', 'P-1', 'W-1', '是'));
|
||||
$this->insertBaseRow(3, $this->payload('O-1', 'P-1', 'W-1', '是'));
|
||||
$this->insertBaseRow(4, $this->payload('O-2', 'P-2', 'W-2', '补单(订单四组)'));
|
||||
$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,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -304,6 +334,7 @@ class KukaPostingMaterialRepairImporterTest extends TestCase
|
||||
string $postingLabel,
|
||||
string $materialNumber = '—',
|
||||
string $description = '—',
|
||||
string $quantity = '1',
|
||||
): array {
|
||||
return [
|
||||
'发货日期' => '46326',
|
||||
@ -313,7 +344,7 @@ class KukaPostingMaterialRepairImporterTest extends TestCase
|
||||
'发货仓' => $warehouse,
|
||||
'店铺' => '天猫-HX旗舰店101',
|
||||
'款式编码' => 'STYLE-1',
|
||||
'实发数量' => '1',
|
||||
'实发数量' => $quantity,
|
||||
'过账标签' => $postingLabel,
|
||||
'物料编号' => $materialNumber,
|
||||
'物料描述' => $description,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user