fix: 支持快递差异与未定价批量推送

Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
This commit is contained in:
苏木 2026-11-12 09:34:59 +08:00 committed by sumu
parent 474866b5f8
commit 52ff4999d1
6 changed files with 211 additions and 40 deletions

View File

@ -245,10 +245,7 @@ class SupplierReconciliation extends Controller
}
$differences = array_values(array_filter(
$detail['rows'],
static fn (array $row): bool => ! in_array($row['type'] ?? '', [
SupplierStatementMatcher::TYPE_MATCH,
SupplierStatementMatcher::TYPE_UNPRICED,
], true),
static fn (array $row): bool => ($row['type'] ?? '') !== SupplierStatementMatcher::TYPE_MATCH,
));
if ($request->input('scope') === 'row') {

View File

@ -356,11 +356,18 @@ class SupplierDingTalkNotifier
/**
* @param list<array<string,mixed>> $rows
* @return array{count:int,extra:int,mismatch:int,missing:int,difference_amount:float}
* @return array{count:int,extra:int,mismatch:int,missing:int,unpriced:int,difference_amount:float}
*/
public function summarize(array $rows): array
{
$summary = ['count' => count($rows), 'extra' => 0, 'mismatch' => 0, 'missing' => 0, 'difference_amount' => 0.0];
$summary = [
'count' => count($rows),
'extra' => 0,
'mismatch' => 0,
'missing' => 0,
'unpriced' => 0,
'difference_amount' => 0.0,
];
foreach ($rows as $row) {
$type = (string) ($row['type'] ?? '');
$mine = (float) ($row['mineAmount'] ?? 0);
@ -375,6 +382,8 @@ class SupplierDingTalkNotifier
} elseif ($type === SupplierStatementMatcher::TYPE_SUPPLIER_MISSING) {
$summary['missing']++;
$summary['difference_amount'] += $mine;
} elseif ($type === SupplierStatementMatcher::TYPE_UNPRICED) {
$summary['unpriced']++;
}
}
$summary['difference_amount'] = round($summary['difference_amount'], 2);
@ -467,7 +476,8 @@ class SupplierDingTalkNotifier
'- 对账月份:' . ($month !== '' ? $month : '全部月份'),
'- 对账人:' . ($reconciler !== '' ? $reconciler : '全部'),
'- 差异共 **' . $summary['count'] . '** 条(厂家多 ' . $summary['extra']
. ' / 数量金额不一致 ' . $summary['mismatch'] . ' / 厂家少 ' . $summary['missing'] . ')',
. ' / 数量金额不一致 ' . $summary['mismatch'] . ' / 厂家少 ' . $summary['missing']
. ' / 未定价 ' . $summary['unpriced'] . ')',
'- 差异金额:**¥' . number_format($summary['difference_amount'], 2) . '**',
];
@ -489,7 +499,10 @@ class SupplierDingTalkNotifier
);
$prefix = count($differenceDetails) > 1 ? ' - ' : ' ';
foreach ($differenceDetails as $detail) {
$lines[] = $prefix . $this->differenceDetailLine($detail);
$lines[] = $prefix . $this->differenceDetailLine(
$detail,
(string) ($row['type'] ?? ''),
);
}
continue;
@ -532,23 +545,46 @@ class SupplierDingTalkNotifier
}
/** @param array<string,mixed> $detail */
private function differenceDetailLine(array $detail): string
private function differenceDetailLine(array $detail, string $type): string
{
$purchaseNo = trim((string) ($detail['purchaseNo'] ?? ''));
$expressNo = trim((string) ($detail['expressNo'] ?? ''));
$barcode = trim((string) ($detail['barcode'] ?? ''));
$mineQty = (float) ($detail['mineQty'] ?? 0);
$mineUnitPrice = (float) ($detail['mineUnitPrice'] ?? 0);
$mineGuessUnitPrice = (float) ($detail['mineGuessUnitPrice'] ?? 0);
$theirsQty = (float) ($detail['theirsQty'] ?? 0);
$theirsUnitPrice = (float) ($detail['theirsUnitPrice'] ?? 0);
$amountDifference = (float) ($detail['amountDifference'] ?? 0);
$referenceLabel = trim((string) ($detail['referenceLabel'] ?? ''));
$referenceNo = trim((string) ($detail['referenceNo'] ?? ''));
if ($referenceLabel === '' || $referenceNo === '') {
$useExpress = (int) ($detail['matchedByLayer'] ?? 0) === 4
? $expressNo !== ''
: ($purchaseNo === '' && $expressNo !== '');
$referenceLabel = $useExpress ? '快递单号' : '采购单号';
$referenceNo = $useExpress ? $expressNo : $purchaseNo;
}
$amountText = abs($amountDifference) < 0.005
? '厂家多算 0.00'
: '厂家' . ($amountDifference > 0 ? '多算 ' : '少算 ') . number_format(abs($amountDifference), 2);
return '采购单号:' . ($purchaseNo !== '' ? $purchaseNo : '—')
$base = $referenceLabel . ':' . ($referenceNo !== '' ? $referenceNo : '—')
. ',条码:' . ($barcode !== '' ? $barcode : '—')
. ',我方数量 ' . $this->number($mineQty)
. ',单价 ' . $this->number($mineUnitPrice)
. ',';
if ($type === SupplierStatementMatcher::TYPE_UNPRICED) {
return $base
. ($mineGuessUnitPrice > 0
? '预估单价 ' . $this->number($mineGuessUnitPrice)
: '我方未定价')
. ',厂家数量 ' . $this->number($theirsQty)
. ',单价 ' . $this->number($theirsUnitPrice)
. ',待定价后结算';
}
return $base
. '单价 ' . $this->number($mineUnitPrice)
. ',厂家数量 ' . $this->number($theirsQty)
. ',单价 ' . $this->number($theirsUnitPrice)
. ',' . $amountText;

View File

@ -662,7 +662,7 @@ class SupplierReconciliationPageData
}
$type = (string) ($row['type'] ?? '');
if (in_array($type, [SupplierStatementMatcher::TYPE_MATCH, SupplierStatementMatcher::TYPE_UNPRICED], true)) {
if ($type === SupplierStatementMatcher::TYPE_MATCH) {
$row['differenceDetails'] = [];
continue;
}
@ -678,8 +678,8 @@ class SupplierReconciliationPageData
usort(
$differenceDetails,
static fn (array $left, array $right): int => strnatcmp(
(string) ($left['purchaseNo'] ?? ''),
(string) ($right['purchaseNo'] ?? ''),
(string) ($left['referenceNo'] ?? ''),
(string) ($right['referenceNo'] ?? ''),
),
);
$row['differenceDetails'] = $differenceDetails;
@ -691,18 +691,30 @@ class SupplierReconciliationPageData
/**
* @param array<string,mixed> $row
* @return array{purchaseNo:string,barcode:string,mineQty:float,mineUnitPrice:float,theirsQty:float,theirsUnitPrice:float,amountDifference:float}
* @return array{purchaseNo:string,expressNo:string,referenceLabel:string,referenceNo:string,matchedByLayer:int|null,barcode:string,mineQty:float,mineUnitPrice:float,mineGuessUnitPrice:float,theirsQty:float,theirsUnitPrice:float,amountDifference:float,type:string}
*/
private function differenceDetailPayload(array $row): array
{
$purchaseNo = trim((string) ($row['purchaseNo'] ?? ''));
$expressNo = trim((string) ($row['expressNo'] ?? ''));
$matchedByLayer = isset($row['matchedByLayer']) ? (int) $row['matchedByLayer'] : null;
$useExpress = ($matchedByLayer === 4 && $expressNo !== '')
|| ($purchaseNo === '' && $expressNo !== '');
return [
'purchaseNo' => trim((string) ($row['purchaseNo'] ?? '')),
'purchaseNo' => $purchaseNo,
'expressNo' => $expressNo,
'referenceLabel' => $useExpress ? '快递单号' : '采购单号',
'referenceNo' => $useExpress ? $expressNo : $purchaseNo,
'matchedByLayer' => $matchedByLayer,
'barcode' => trim((string) ($row['barcode'] ?? '')),
'mineQty' => (float) ($row['mineQty'] ?? 0),
'mineUnitPrice' => (float) ($row['mineUnitPrice'] ?? 0),
'mineGuessUnitPrice' => (float) ($row['mineGuessUnitPrice'] ?? 0),
'theirsQty' => (float) ($row['theirsQty'] ?? 0),
'theirsUnitPrice' => (float) ($row['theirsUnitPrice'] ?? 0),
'amountDifference' => (float) ($row['amountDifference'] ?? 0),
'type' => (string) ($row['type'] ?? ''),
];
}
@ -744,10 +756,7 @@ class SupplierReconciliationPageData
$differenceCount = 0;
foreach ($allRows as $row) {
if (! in_array((string) ($row['type'] ?? ''), [
SupplierStatementMatcher::TYPE_MATCH,
SupplierStatementMatcher::TYPE_UNPRICED,
], true)) {
if ((string) ($row['type'] ?? '') !== SupplierStatementMatcher::TYPE_MATCH) {
$differenceCount++;
}
foreach (['unpriced', 'supplier_over', 'supplier_under', 'mismatch', 'match'] as $filter) {
@ -763,10 +772,7 @@ class SupplierReconciliationPageData
));
$filteredDifferenceCount = count(array_filter(
$rows,
static fn (array $row): bool => ! in_array((string) ($row['type'] ?? ''), [
SupplierStatementMatcher::TYPE_MATCH,
SupplierStatementMatcher::TYPE_UNPRICED,
], true),
static fn (array $row): bool => (string) ($row['type'] ?? '') !== SupplierStatementMatcher::TYPE_MATCH,
));
$detail['all_total_rows'] = count($allRows);

View File

@ -661,7 +661,7 @@
@endphp
<tr class="{{ $tagClass }}">
<td class="sr-select-cell">
@if (! in_array($row['type'], ['一致', '未定价'], true))
@if ($row['type'] !== '一致')
<input class="js-difference-checkbox"
form="push-selected-form"
type="checkbox"
@ -713,9 +713,7 @@
<td class="is-num">{{ $row['theirsUnitPrice'] !== 0.0 ? number_format($row['theirsUnitPrice'], 2) : '—' }}</td>
<td class="is-num">{{ $row['theirsAmount'] !== 0.0 ? number_format($row['theirsAmount'], 2) : '—' }}</td>
<td class="sr-difference-details">
@if ($row['type'] === '未定价')
<span>未定价,待定价后结算</span>
@elseif ($row['type'] === '一致')
@if ($row['type'] === '一致')
<span>数量、金额一致</span>
@else
@foreach ($row['differenceDetails'] ?? [] as $difference)
@ -724,9 +722,15 @@
$detailAmountText = abs($detailAmountDifference) < 0.005
? '厂家多算 0.00'
: '厂家' . ($detailAmountDifference > 0 ? '多算 ' : '少算 ') . number_format(abs($detailAmountDifference), 2);
$detailGuessUnitPrice = (float) ($difference['mineGuessUnitPrice'] ?? 0);
$detailMineUnitPriceText = $row['type'] === '未定价'
? ($detailGuessUnitPrice > 0
? '预估单价 ' . $formatDifferenceNumber($detailGuessUnitPrice)
: '我方未定价')
: '单价 ' . $formatDifferenceNumber($difference['mineUnitPrice']);
@endphp
<div class="sr-difference-line">
采购单号:{{ $difference['purchaseNo'] !== '' ? $difference['purchaseNo'] : '—' }},条码:{{ $difference['barcode'] !== '' ? $difference['barcode'] : '—' }},我方数量 {{ $formatDifferenceNumber($difference['mineQty']) }},单价 {{ $formatDifferenceNumber($difference['mineUnitPrice']) }},厂家数量 {{ $formatDifferenceNumber($difference['theirsQty']) }},单价 {{ $formatDifferenceNumber($difference['theirsUnitPrice']) }},{{ $detailAmountText }}
{{ $difference['referenceLabel'] }}:{{ $difference['referenceNo'] !== '' ? $difference['referenceNo'] : '—' }},条码:{{ $difference['barcode'] !== '' ? $difference['barcode'] : '—' }},我方数量 {{ $formatDifferenceNumber($difference['mineQty']) }},{{ $detailMineUnitPriceText }},厂家数量 {{ $formatDifferenceNumber($difference['theirsQty']) }},单价 {{ $formatDifferenceNumber($difference['theirsUnitPrice']) }},{{ $row['type'] === '未定价' ? '待定价后结算' : $detailAmountText }}
</div>
@endforeach
@endif
@ -736,9 +740,7 @@
</td>
<td class="is-num">{{ $row['type'] === '未定价' ? '待定价' : number_format($row['settleAmount'], 2) }}</td>
<td>
@if ($row['type'] === '未定价')
<span class="sr-muted">待定价</span>
@elseif ($row['type'] !== '一致')
@if ($row['type'] !== '一致')
<form method="POST"
action="{{ route('business.supplier-reconciliation.push-differences') }}"
onsubmit="return confirm('确认将这条差异推送到该厂家钉钉群?')">

View File

@ -180,6 +180,55 @@ class SupplierReconciliationDingTalkTest extends FeatureTestCase
});
}
public function test_push_message_uses_express_number_for_express_match(): void
{
$companyId = (int) $this->company->id;
app(SupplierPayableOverrideStore::class)->save(
'final:supplier:payment-reconciliation',
'express:push',
[
'summary_month' => '2026-10',
'expressNo' => '@SF123456',
'purchaseNo' => 'MINE-PO',
'date' => '2026-10-01',
'supplier' => '晨丰',
'skuCode' => 'CZ000927',
'productCode' => 'CZ000927N1201V02',
'qty' => '7',
'unitPrice' => '86',
'amount' => '602',
],
$companyId,
null,
);
app(SupplierStatementStore::class)->save('晨丰', [
array_merge($this->statementRow('CZ000927N1201V01', '86.00', '8', '688.00'), [
'expressNo' => 'SF123456',
'purchaseNo' => null,
'createdAt' => '2026-10-09',
]),
], 'statement.csv', $companyId, null);
$this->saveRobot();
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
$this->loginAs()
->post(route('business.supplier-reconciliation.push-differences'), [
'supplier' => '晨丰',
'month' => '2026-10',
'scope' => 'all',
])
->assertStatus(302)
->assertSessionHas('success', '已推送 1 条差异到 晨丰 钉钉群(1 条消息)。');
Http::assertSent(function ($request): bool {
$text = (string) ($request['markdown']['text'] ?? '');
return str_contains($text, '快递单号:SF123456')
&& str_contains($text, '我方数量 7,单价 86,厂家数量 8,单价 86,厂家多算 86.00')
&& ! str_contains($text, '采购单号:MINE-PO');
});
}
public function test_push_reconciliation_summary_uses_settlement_quantity_and_amount_by_default(): void
{
$companyId = (int) $this->company->id;
@ -246,7 +295,7 @@ class SupplierReconciliationDingTalkTest extends FeatureTestCase
});
}
public function test_unpriced_rows_are_not_pushed_as_supplier_differences(): void
public function test_unpriced_rows_are_pushed_as_supplier_differences(): void
{
app(SupplierDingTalkNotifier::class)->save('未定价厂家', self::WEBHOOK, 'SEC-test-123', false);
app(SupplierPayableOverrideStore::class)->save(
@ -276,7 +325,7 @@ class SupplierReconciliationDingTalkTest extends FeatureTestCase
'supplier' => '未定价厂家',
]),
], 'unpriced.csv', (int) $this->company->id, null);
Http::fake();
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
$this->loginAs()
->post(route('business.supplier-reconciliation.push-differences'), [
@ -286,11 +335,74 @@ class SupplierReconciliationDingTalkTest extends FeatureTestCase
'scope' => 'all',
])
->assertStatus(302)
->assertSessionHasErrors([
'dingtalk_push' => '当前筛选没有可推送的差异。',
]);
->assertSessionHas('success', '已推送 1 条差异到 未定价厂家 钉钉群(1 条消息)。');
Http::assertNothingSent();
Http::assertSentCount(1);
Http::assertSent(function ($request): bool {
$text = (string) ($request['markdown']['text'] ?? '');
return str_contains($text, '未定价 1')
&& str_contains($text, '**未定价**')
&& str_contains($text, '采购单号:948180,条码:SZ014916N1109V01,我方数量 20,预估单价 195,厂家数量 20,单价 186,待定价后结算')
&& ! str_contains($text, '厂家少算');
});
}
public function test_selected_push_can_include_unpriced_and_regular_differences(): void
{
$companyId = (int) $this->company->id;
$this->seedReconciliation();
$statements = app(SupplierStatementStore::class);
$statementRows = $statements->rows('晨丰', $companyId);
$statementRows[] = array_merge($this->statementRow('UNPRICED-1N1097V01', '12', '3', '36'), [
'purchaseNo' => 'PO-UNPRICED',
'createdAt' => '2026-09-30',
]);
$statements->save('晨丰', $statementRows, 'statement.csv', $companyId, null);
app(SupplierPayableOverrideStore::class)->save(
'final:supplier:payment-reconciliation',
'mine:unpriced-selected',
[
'summary_month' => '2026-09',
'purchaseNo' => 'PO-UNPRICED',
'date' => '2026-09-30',
'supplier' => '晨丰',
'skuCode' => 'UNPRICED-1',
'productCode' => 'UNPRICED-1N1097V01',
'qty' => '3',
'unitPrice' => null,
'amount' => null,
'guessUnitPrice' => '12',
'guessAmount' => '36',
],
$companyId,
null,
);
$this->saveRobot();
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
$this->loginAs()
->post(route('business.supplier-reconciliation.push-differences'), [
'supplier' => '晨丰',
'month' => '2026-09',
'scope' => 'selected',
'selected_rows' => [
json_encode(['purchaseNo' => '编号-B-1', 'date' => '2026-09-30', 'barcode' => 'B-1', 'type' => '数量/金额不一致']),
json_encode(['purchaseNo' => 'PO-UNPRICED', 'date' => '2026-09-30', 'barcode' => 'UNPRICED-1N1097V01', 'type' => '未定价']),
],
])
->assertStatus(302)
->assertSessionHas('success', '已推送 2 条差异到 晨丰 钉钉群(1 条消息)。');
Http::assertSent(function ($request): bool {
$text = (string) ($request['markdown']['text'] ?? '');
return str_contains($text, '**数量/金额不一致**')
&& str_contains($text, '**未定价**')
&& str_contains($text, '采购单号:PO-UNPRICED')
&& str_contains($text, '预估单价 12,厂家数量 3,单价 12,待定价后结算')
&& ! str_contains($text, '`C-1`');
});
}
public function test_push_reconciliation_summary_rejects_unknown_basis_without_http_call(): void

View File

@ -104,6 +104,17 @@ class SupplierReconciliationTest extends FeatureTestCase
$this->assertSame('SF123456', $detail['detailedRows'][0]['expressNo']);
$this->assertSame(4, $detail['detailedRows'][0]['matchedByLayer']);
$this->assertStringContainsString('快递单号+商品编码匹配', $detail['detailedRows'][0]['note']);
$this->assertSame('快递单号', $detail['rows'][0]['differenceDetails'][0]['referenceLabel']);
$this->assertSame('SF123456', $detail['rows'][0]['differenceDetails'][0]['referenceNo']);
$this->loginAs()->get(route('business.pages.show', [
'section' => 'supplier-reconciliation',
'page' => 'finance-data',
'supplier' => '晨丰',
'month' => '2026-10',
]))
->assertOk()
->assertSeeText('快递单号:SF123456');
}
public function test_finance_supplier_list_shows_return_confirmation_progress(): void
@ -594,9 +605,11 @@ class SupplierReconciliationTest extends FeatureTestCase
$this->assertSame(0.0, $detail['summary']['supplierOverAmount']);
$this->assertSame(0.0, $detail['summary']['differenceAmount']);
$this->assertSame(1, $detail['comparison_counts']['unpriced']);
$this->assertSame(0, $detail['difference_count']);
$this->assertSame(1, $detail['difference_count']);
$this->assertSame(195.0, $unpriced['mineGuessUnitPrice']);
$this->assertSame(3900.0, $unpriced['mineGuessAmount']);
$this->assertSame('采购单号', $unpriced['differenceDetails'][0]['referenceLabel']);
$this->assertSame(195.0, $unpriced['differenceDetails'][0]['mineGuessUnitPrice']);
$filtered = app(SupplierReconciliationPageData::class)->financeDetailForFilters(
'木缘居',
@ -609,7 +622,7 @@ class SupplierReconciliationTest extends FeatureTestCase
);
$this->assertSame(1, $filtered['total_rows']);
$this->assertSame(1, $filtered['comparison_counts']['unpriced']);
$this->assertSame(0, $filtered['difference_count']);
$this->assertSame(1, $filtered['difference_count']);
$this->loginAs()->get(route('business.pages.show', [
'section' => 'supplier-reconciliation',
@ -623,6 +636,11 @@ class SupplierReconciliationTest extends FeatureTestCase
->assertSeeText('预估 195.00')
->assertSeeText('预估 3,900.00')
->assertSeeText('待定价')
->assertSeeText('推送全部差异(1 条)')
->assertSee('aria-label="选择差异 SZ014916N1109V01"', false)
->assertSeeText('推送本条')
->assertSeeText('采购单号:948180')
->assertSeeText('预估单价 195,厂家数量 20,单价 186,待定价后结算')
->assertSeeText('未计价金额不计入推送货款,待定价后结算');
}