CaiWuXiTong/tests/Feature/Business/SupplierReconciliationDingTalkTest.php
renshen 59e0e46b8a fix(供应商对账): 限制钉钉差异消息正文大小 (#1763)
按实际请求体字节动态分批,并限制单条差异展开明细数量。
2026-12-05 17:27:20 +08:00

1666 lines
69 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Feature\Business;
use App\Models\Auth\Permission;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Routing\Exceptions\InvalidSignatureException;
use Modules\EcommerceFinance\Services\SupplierDingTalkNotifier;
use Modules\EcommerceFinance\Services\SupplierPayableOverrideStore;
use Modules\EcommerceFinance\Services\SupplierReturnConfirmation;
use Modules\EcommerceFinance\Services\SupplierReturnShareLink;
use Modules\EcommerceFinance\Services\SupplierStatementStore;
use Modules\EcommerceFinance\Services\SupplierSummaryAICard;
use Modules\EcommerceFinance\Services\SupplierSummaryConfirmation;
use Tests\Feature\FeatureTestCase;
class SupplierReconciliationDingTalkTest extends FeatureTestCase
{
private const WEBHOOK = 'https://oapi.dingtalk.com/robot/send?access_token=supplier-test-token';
protected function setUp(): void
{
parent::setUp();
$this->user->attachPermission(
Permission::where('name', 'read-admin-panel')->firstOrFail()
);
}
public function test_finance_detail_shows_push_buttons_and_robot_configuration(): void
{
$this->seedReconciliation();
$this->loginAs()->get($this->pageUrl())
->assertOk()
->assertSeeText('推送全部需厂家修改差异(1 条)')
->assertSeeText('推送选中(')
->assertSee('aria-label="选择全部需厂家修改差异"', false)
->assertSeeText('推送本条')
->assertSeeText('推送到对账群')
->assertSeeText('推送内容预览')
->assertSeeText('账期:2026年9月,件数3件,货款250.00元')
->assertSeeText('按结算口径(推荐)')
->assertSeeText('按我方原始数据')
->assertSeeText('按口径结算件数')
->assertSeeText('厂家少算或漏报取厂家数据;厂家多算不纳入结算。')
->assertSee('name="push_basis"', false)
->assertSeeText('厂家钉钉群机器人')
->assertSeeText('未配置,配置后才能推送差异')
->assertSeeText('互动卡片群会话 ID')
->assertSeeText('厂家确认状态')
->assertDontSee(self::WEBHOOK);
}
public function test_robot_configuration_is_saved_and_secret_is_not_rendered(): void
{
$this->seedReconciliation();
$this->loginAs()
->post(route('business.supplier-reconciliation.dingtalk-robot.save'), [
'supplier' => '晨丰',
'dingtalk_webhook' => self::WEBHOOK,
'dingtalk_secret' => 'SEC-test-123',
'dingtalk_at_all' => '1',
'dingtalk_card_conversation_id' => 'https://example.test/?openConversationId=cid-saved-group==',
])
->assertStatus(302)
->assertSessionHas('success', '晨丰 的钉钉群机器人已保存。');
$config = app(SupplierDingTalkNotifier::class)->configuration('晨丰');
$this->assertTrue($config['configured']);
$this->assertTrue($config['at_all']);
$this->assertTrue($config['card_group_configured']);
$this->assertStringContainsString('access_token=••••••', $config['webhook_hint']);
$this->assertStringContainsString('cid••••••', $config['card_group_hint']);
$this->assertStringNotContainsString('supplier-test-token', (string) setting('supplier_reconciliation.dingtalk_robots'));
$this->assertStringNotContainsString('SEC-test-123', (string) setting('supplier_reconciliation.dingtalk_robots'));
$this->loginAs()->get($this->pageUrl())
->assertOk()
->assertSeeText('已配置')
->assertDontSeeText('supplier-test-token')
->assertDontSeeText('SEC-test-123');
}
public function test_invalid_robot_webhook_is_rejected(): void
{
$this->withExceptionHandling();
$this->loginAs()
->post(route('business.supplier-reconciliation.dingtalk-robot.save'), [
'supplier' => '晨丰',
'dingtalk_webhook' => 'https://example.com/robot/send?access_token=unsafe',
])
->assertStatus(302)
->assertSessionHasErrors('dingtalk_webhook');
}
public function test_invalid_interactive_card_group_id_is_rejected(): void
{
$this->withExceptionHandling();
$this->loginAs()
->post(route('business.supplier-reconciliation.dingtalk-robot.save'), [
'supplier' => '晨丰',
'dingtalk_webhook' => self::WEBHOOK,
'dingtalk_card_conversation_id' => 'not-a-conversation-id',
])
->assertStatus(302)
->assertSessionHasErrors('dingtalk_card_conversation_id');
}
public function test_push_all_differences_sends_only_current_supplier_and_filtered_rows(): void
{
$this->seedReconciliation();
$this->saveRobot();
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
$this->loginAs()
->post(route('business.supplier-reconciliation.push-differences'), [
'supplier' => '晨丰',
'month' => '2026-09',
'reconciler' => '张三',
'scope' => 'all',
])
->assertStatus(302)
->assertSessionHas('success', '已推送 1 条差异到 晨丰 钉钉群(1 条消息)。');
Http::assertSentCount(1);
Http::assertSent(function ($request): bool {
$text = (string) ($request['markdown']['text'] ?? '');
return str_contains($request->url(), 'oapi.dingtalk.com/robot/send')
&& str_contains($request->url(), 'timestamp=')
&& str_contains($request->url(), 'sign=')
&& str_contains($text, '货款对账差异通知 · 晨丰')
&& str_contains($text, 'D-1')
&& str_contains($text, '采购单号:编号-D-1')
&& str_contains($text, '**厂家多**')
&& str_contains($text, ',我方数量 ')
&& str_contains($text, ',厂家数量 ')
&& ! str_contains($text, '日期:2026-09-30')
&& ! str_contains($text, '`A-1`')
&& ! str_contains($text, '`B-1`')
&& ! str_contains($text, '`C-1`')
&& ! str_contains($text, '鸣飞家具');
});
$config = app(SupplierDingTalkNotifier::class)->configuration('晨丰');
$this->assertNotSame('', $config['last_pushed_at']);
$this->assertSame(1, $config['last_push_count']);
}
public function test_push_message_shows_purchase_level_difference_details(): void
{
$companyId = (int) $this->company->id;
$store = app(SupplierPayableOverrideStore::class);
foreach ([
'line:982750' => ['purchaseNo' => '982750', 'qty' => '69', 'amount' => '3208.50'],
'line:929138' => ['purchaseNo' => '929138', 'qty' => '10', 'amount' => '465.00'],
] as $key => $row) {
$store->save('final:supplier:payment-reconciliation', $key, $row + [
'summary_month' => '2026-10',
'date' => '2026-10-30',
'supplier' => '晨丰',
'skuCode' => 'ZWJ003946N181V01',
'unitPrice' => '46.5',
], $companyId, null);
}
app(SupplierStatementStore::class)->save('晨丰', [
array_merge($this->statementRow('ZWJ003946N181V01', '46.5', '70', '3255.00'), [
'purchaseNo' => '982750',
'createdAt' => '2026-10-30',
]),
array_merge($this->statementRow('ZWJ003946N181V01', '46.5', '10', '465.00'), [
'purchaseNo' => '929138',
'createdAt' => '2026-10-30',
]),
], '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, '**数量/金额不一致**')
&& str_contains($text, '采购单号:982750,条码:ZWJ003946N181V01,我方数量 69,单价 46.5,金额 3208.5,厂家数量 70,单价 46.5,金额 3255,厂家多算 46.50')
&& ! str_contains($text, '929138、982750')
&& ! str_contains($text, '我方:数量')
&& ! str_contains($text, '四层明细匹配后');
});
}
public function test_push_message_omits_fully_offset_date_groups_for_the_same_purchase_and_barcode(): void
{
$companyId = (int) $this->company->id;
$store = app(SupplierPayableOverrideStore::class);
foreach ([
'line:929186:49' => ['purchaseNo' => '929186', 'date' => '2026-10-06', 'qty' => '49', 'amount' => '8207.50'],
'line:929186:31' => ['purchaseNo' => '929186', 'date' => '2026-10-07', 'qty' => '31', 'amount' => '5192.50'],
'line:950366:80' => ['purchaseNo' => '950366', 'date' => '2026-10-14', 'qty' => '80', 'amount' => '13400.00'],
] as $key => $row) {
$store->save('final:supplier:payment-reconciliation', $key, $row + [
'summary_month' => '2026-10',
'supplier' => '晨丰',
'skuCode' => 'SJ029602',
'productCode' => 'SJ029602N1275V01',
'unitPrice' => '167.5',
'reconciler' => '张三',
], $companyId, null);
}
app(SupplierStatementStore::class)->save('晨丰', [
array_merge($this->statementRow('SJ029602N1275V01', '167.5', '31', '5192.50'), [
'purchaseNo' => '929186',
'createdAt' => '2026-10-09',
]),
array_merge($this->statementRow('SJ029602N1275V01', '167.5', '49', '8207.50'), [
'purchaseNo' => '929186',
'createdAt' => '2026-10-07',
]),
array_merge($this->statementRow('SJ029602N1275V01', '167.5', '160', '26800.00'), [
'purchaseNo' => '950366',
'createdAt' => '2026-10-15',
]),
], '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',
'reconciler' => '张三',
'scope' => 'all',
])
->assertStatus(302)
->assertSessionHas('success', '已推送 1 条差异到 晨丰 钉钉群(1 条消息)。');
Http::assertSent(function ($request): bool {
$text = (string) ($request['markdown']['text'] ?? '');
return str_contains($text, '采购单号:950366,条码:SJ029602N1275V01')
&& str_contains($text, '我方数量 80')
&& str_contains($text, '厂家数量 160')
&& ! str_contains($text, '采购单号:929186')
&& ! str_contains($text, '厂家少算 3,015.00')
&& ! str_contains($text, '厂家多算 3,015.00');
});
}
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,金额 602,厂家数量 8,单价 86,金额 688,厂家多算 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;
$this->seedReconciliation();
$this->saveSummaryRobot();
app(SupplierPayableOverrideStore::class)->save(
'final:supplier:payment-reconciliation',
'mine:other-reconciler',
[
'summary_month' => '2026-09',
'purchaseNo' => 'OTHER-1',
'date' => '2026-09-30',
'supplier' => '晨丰',
'skuCode' => 'OTHER-1',
'qty' => '99',
'unitPrice' => '10',
'amount' => '990',
'reconciler' => '李四',
],
$companyId,
null,
);
$this->fakeSummaryCardHttp();
$this->loginAs()
->post(route('business.supplier-reconciliation.push-summary'), [
'supplier' => '晨丰',
'month' => '2026-09',
'reconciler' => '张三',
])
->assertStatus(302)
->assertSessionHasInput('push_basis', 'settlement')
->assertSessionHas('success', '已将 2026年9月 对账确认卡片推送到 晨丰 钉钉群,等待厂家确认。');
Http::assertSent(function ($request): bool {
if (! str_contains($request->url(), '/v1.0/card/ai/instances/createAndDeliver')) {
return false;
}
$messages = (array) ($request['a2uiMessages'] ?? []);
$components = (array) (json_decode((string) ($messages[2] ?? '[]'), true)['updateComponents']['components'] ?? []);
$summary = collect($components)->firstWhere('id', 'summary');
$button = collect($components)->firstWhere('id', 'confirm-button');
$event = (array) ($button['action']['event'] ?? []);
return ($request['callbackType'] ?? null) === 'HTTP'
&& ($request['imGroupOpenSpaceModel']['supportForward'] ?? null) === false
&& ($request['flowStatus'] ?? null) === 'CONFIRMING'
&& ($event['name'] ?? null) === 'confirm_supplier_summary'
&& ($event['context']['action'] ?? null) === 'confirm'
&& str_contains((string) ($summary['content'] ?? ''), '账期:**2026年9月**')
&& str_contains((string) ($summary['content'] ?? ''), '件数:**3** 件')
&& str_contains((string) ($summary['content'] ?? ''), '货款:**¥250.00**')
&& ! str_contains((string) ($summary['content'] ?? ''), '990');
});
$config = app(SupplierDingTalkNotifier::class)->configuration('晨丰');
$this->assertSame(1, $config['last_push_count']);
}
public function test_push_reconciliation_summary_can_use_our_original_quantity_and_amount(): void
{
$this->seedReconciliation();
$this->saveSummaryRobot();
$this->fakeSummaryCardHttp();
$this->loginAs()
->post(route('business.supplier-reconciliation.push-summary'), [
'supplier' => '晨丰',
'month' => '2026-09',
'reconciler' => '张三',
'push_basis' => 'mine',
])
->assertStatus(302)
->assertSessionHasInput('push_basis', 'mine')
->assertSessionHas('success', '已将 2026年9月 对账确认卡片推送到 晨丰 钉钉群,等待厂家确认。');
Http::assertSent(function ($request): bool {
if (! str_contains($request->url(), '/v1.0/card/ai/instances/createAndDeliver')) {
return false;
}
$messages = (array) ($request['a2uiMessages'] ?? []);
$components = (array) (json_decode((string) ($messages[2] ?? '[]'), true)['updateComponents']['components'] ?? []);
$summary = collect($components)->firstWhere('id', 'summary');
return ! str_contains((string) ($summary['content'] ?? ''), '口径')
&& str_contains((string) ($summary['content'] ?? ''), '件数:**5** 件')
&& str_contains((string) ($summary['content'] ?? ''), '货款:**¥330.00**');
});
}
public function test_push_reconciliation_summary_requires_an_interactive_card_group(): void
{
$this->seedReconciliation();
$this->saveRobot();
config(['dingtalk.summary_confirmation_mode' => 'card']);
Http::fake();
$this->loginAs()
->post(route('business.supplier-reconciliation.push-summary'), [
'supplier' => '晨丰',
'month' => '2026-09',
'reconciler' => '张三',
])
->assertStatus(302)
->assertSessionHasErrors([
'dingtalk_summary_push' => '该厂家尚未配置互动卡片群会话 ID,请展开“厂家钉钉群机器人”补齐后再推送。',
]);
Http::assertNothingSent();
}
public function test_manufacturer_card_confirmation_updates_finance_page_status_without_opening_a_page(): void
{
$companyId = (int) $this->company->id;
$this->seedReconciliation();
$this->saveSummaryRobot();
$this->fakeSummaryCardHttp();
config([
'app.platform_timezone' => 'Asia/Shanghai',
'app.timezone' => 'UTC',
]);
$this->travelTo(Carbon::parse('2026-11-15 08:08:09', 'UTC'));
$this->loginAs()
->post(route('business.supplier-reconciliation.push-summary'), [
'supplier' => '晨丰',
'month' => '2026-09',
'reconciler' => '张三',
])
->assertStatus(302);
$confirmation = app(SupplierSummaryConfirmation::class)->latest(
'晨丰',
'2026-09',
'settlement',
$companyId,
);
$this->assertSame(SupplierSummaryConfirmation::STATUS_PENDING, $confirmation['status'] ?? null);
$timestamp = (string) now()->timestamp;
$signature = app(SupplierSummaryAICard::class)->callbackSignature($timestamp);
$payload = [
'type' => 'actionCallback',
'outTrackId' => $confirmation['_action_key'],
'corpId' => 'ding-test-corp',
'userId' => 'manufacturer-user-1',
'content' => json_encode([
'cardPrivateData' => [
'actionIds' => ['confirm'],
'params' => ['action' => 'confirm'],
],
], JSON_UNESCAPED_UNICODE),
];
$this->postJson(
route('integrations.dingtalk.supplier-summary-card.callback'),
$payload,
[
'x-ddpaas-signature-timestamp' => $timestamp,
'x-ddpaas-signature' => $signature,
],
)->assertOk()
->assertJson([
'success' => true,
'confirmed' => true,
'newly_confirmed' => true,
]);
$confirmed = app(SupplierSummaryConfirmation::class)->latest(
'晨丰',
'2026-09',
'settlement',
$companyId,
);
$this->assertSame(SupplierSummaryConfirmation::STATUS_CONFIRMED, $confirmed['status'] ?? null);
$this->assertSame('manufacturer-user-1', $confirmed['confirmed_by_dingtalk_user_id'] ?? null);
$this->assertSame('2026-11-15 16:08:09', $confirmed['confirmed_at'] ?? null);
$this->loginAs()->getJson(route('business.supplier-reconciliation.summary-confirmation.status', [
'supplier' => '晨丰',
'month' => '2026-09',
'push_basis' => 'settlement',
]))->assertOk()->assertJson([
'status' => 'confirmed',
'label' => '厂家已确认',
'confirmed_at' => '2026-11-15 16:08:09',
]);
$this->loginAs()->get($this->pageUrl())
->assertOk()
->assertSeeText('厂家确认状态')
->assertSeeText('厂家已确认')
->assertSee('data-status-url=', false);
$updatesBeforeRetry = collect(Http::recorded())->filter(
static fn (array $pair): bool => str_contains($pair[0]->url(), '/v1.0/card/ai/instances/update'),
)->count();
$this->assertSame(1, $updatesBeforeRetry);
$this->postJson(
route('integrations.dingtalk.supplier-summary-card.callback'),
$payload,
[
'x-ddpaas-signature-timestamp' => $timestamp,
'x-ddpaas-signature' => $signature,
],
)->assertOk()->assertJson([
'success' => true,
'newly_confirmed' => false,
]);
$updatesAfterRetry = collect(Http::recorded())->filter(
static fn (array $pair): bool => str_contains($pair[0]->url(), '/v1.0/card/ai/instances/update'),
)->count();
$this->assertSame(2, $updatesAfterRetry);
}
public function test_supplier_summary_card_callback_rejects_an_invalid_signature(): void
{
$this->withExceptionHandling();
$companyId = (int) $this->company->id;
$confirmation = app(SupplierSummaryConfirmation::class)->create(
'supplier-summary-c' . $companyId . '-' . str_repeat('a', 32),
'晨丰',
'2026-09',
'settlement',
3,
250,
$companyId,
null,
);
$timestamp = (string) now()->timestamp;
$payload = [
'type' => 'actionCallback',
'outTrackId' => $confirmation['_action_key'],
'corpId' => 'ding-test-corp',
'userId' => 'manufacturer-user-1',
'content' => json_encode([
'cardPrivateData' => [
'actionIds' => ['confirm'],
'params' => ['action' => 'confirm'],
],
], JSON_UNESCAPED_UNICODE),
];
$this->postJson(
route('integrations.dingtalk.supplier-summary-card.callback'),
$payload,
[
'x-ddpaas-signature-timestamp' => $timestamp,
'x-ddpaas-signature' => 'invalid-signature',
],
)->assertForbidden();
$this->assertSame(
SupplierSummaryConfirmation::STATUS_SENDING,
app(SupplierSummaryConfirmation::class)->find($confirmation['_action_key'], $companyId)['status'] ?? null,
);
}
public function test_unpriced_rows_do_not_require_a_supplier_statement_change(): void
{
app(SupplierDingTalkNotifier::class)->save('未定价厂家', self::WEBHOOK, 'SEC-test-123', false);
app(SupplierPayableOverrideStore::class)->save(
'final:supplier:payment-reconciliation',
'mine:unpriced',
[
'summary_month' => '2026-10',
'purchaseNo' => '948180',
'date' => '2026-10-14',
'supplier' => '未定价厂家',
'skuCode' => 'SZ014916',
'productCode' => 'SZ014916N1109V01',
'qty' => '20',
'unitPrice' => null,
'amount' => null,
'guessUnitPrice' => '195',
'guessAmount' => '3900',
'reconciler' => '陈亲园',
],
(int) $this->company->id,
null,
);
app(SupplierStatementStore::class)->save('未定价厂家', [
array_merge($this->statementRow('SZ014916N1109V01', '186.00', '20', '3720.00'), [
'purchaseNo' => '948180',
'createdAt' => '2026-10-08',
'supplier' => '未定价厂家',
]),
], 'unpriced.csv', (int) $this->company->id, null);
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
$this->loginAs()
->post(route('business.supplier-reconciliation.push-differences'), [
'supplier' => '未定价厂家',
'month' => '2026-10',
'reconciler' => '陈亲园',
'scope' => 'all',
])
->assertStatus(302)
->assertSessionHasErrors([
'dingtalk_push' => '当前筛选没有可推送的差异。',
]);
Http::assertNothingSent();
}
public function test_selected_push_only_sends_rows_that_require_supplier_correction(): 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' => '编号-D-1', 'date' => '2026-09-30', 'barcode' => 'D-1', 'type' => '厂家多']),
json_encode(['purchaseNo' => 'PO-UNPRICED', 'date' => '2026-09-30', 'barcode' => 'UNPRICED-1N1097V01', 'type' => '未定价']),
],
])
->assertStatus(302)
->assertSessionHas('success', '已推送 1 条差异到 晨丰 钉钉群(1 条消息)。');
Http::assertSent(function ($request): bool {
$text = (string) ($request['markdown']['text'] ?? '');
return str_contains($text, '**厂家多**')
&& str_contains($text, 'D-1')
&& ! str_contains($text, '**未定价**')
&& ! str_contains($text, 'UNPRICED-1N1097V01')
&& ! str_contains($text, '`B-1`');
});
}
public function test_push_reconciliation_summary_rejects_unknown_basis_without_http_call(): void
{
$this->withExceptionHandling();
$this->seedReconciliation();
$this->saveRobot();
Http::fake();
$this->loginAs()
->post(route('business.supplier-reconciliation.push-summary'), [
'supplier' => '晨丰',
'month' => '2026-09',
'reconciler' => '张三',
'push_basis' => 'theirs',
])
->assertStatus(302)
->assertSessionHasErrors('push_basis');
Http::assertNothingSent();
}
public function test_push_reconciliation_summary_requires_robot_and_upload(): void
{
$this->seedReconciliation();
Http::fake();
$this->loginAs()
->post(route('business.supplier-reconciliation.push-summary'), [
'supplier' => '晨丰',
'month' => '2026-09',
'reconciler' => '张三',
])
->assertStatus(302)
->assertSessionHasErrors([
'dingtalk_summary_push' => '该厂家还没有配置钉钉群机器人 Webhook。',
]);
Http::assertNothingSent();
app(SupplierDingTalkNotifier::class)->save('无账单厂家', self::WEBHOOK, 'SEC-test-123', false);
app(SupplierPayableOverrideStore::class)->save(
'final:supplier:payment-reconciliation',
'mine:no-statement',
[
'summary_month' => '2026-09',
'purchaseNo' => 'NO-STATEMENT',
'date' => '2026-09-30',
'supplier' => '无账单厂家',
'skuCode' => 'NO-STATEMENT',
'qty' => '1',
'unitPrice' => '10',
'amount' => '10',
'reconciler' => '张三',
],
(int) $this->company->id,
null,
);
$this->loginAs()
->post(route('business.supplier-reconciliation.push-summary'), [
'supplier' => '无账单厂家',
'month' => '2026-09',
'reconciler' => '张三',
])
->assertStatus(302)
->assertSessionHasErrors([
'dingtalk_summary_push' => '厂家尚未上传对账单,暂不能推送对账汇总。',
]);
Http::assertNothingSent();
}
public function test_push_single_difference_only_sends_the_selected_row(): void
{
$this->seedReconciliation();
$this->saveRobot();
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
$this->loginAs()
->post(route('business.supplier-reconciliation.push-differences'), [
'supplier' => '晨丰',
'month' => '2026-09',
'reconciler' => '张三',
'scope' => 'row',
'purchase_no' => '编号-D-1',
'date' => '2026-09-30',
'barcode' => 'D-1',
'difference_type' => '厂家多',
])
->assertStatus(302)
->assertSessionHas('success', '已推送 1 条差异到 晨丰 钉钉群(1 条消息)。');
Http::assertSent(function ($request): bool {
$text = (string) ($request['markdown']['text'] ?? '');
return str_contains($text, 'D-1')
&& ! str_contains($text, '`B-1`')
&& ! str_contains($text, '`C-1`')
&& ! str_contains($text, '厂家少算');
});
}
public function test_push_selected_differences_only_sends_checked_rows(): void
{
$this->seedReconciliation();
$this->saveRobot();
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
$this->loginAs()
->post(route('business.supplier-reconciliation.push-differences'), [
'supplier' => '晨丰',
'month' => '2026-09',
'reconciler' => '张三',
'scope' => 'selected',
'selected_rows' => [
json_encode(['purchaseNo' => '编号-B-1', 'date' => '2026-09-30', 'barcode' => 'B-1', 'type' => '数量/金额不一致']),
json_encode(['purchaseNo' => '编号-D-1', 'date' => '2026-09-30', 'barcode' => 'D-1', 'type' => '厂家多']),
],
])
->assertStatus(302)
->assertSessionHas('success', '已推送 1 条差异到 晨丰 钉钉群(1 条消息)。');
Http::assertSent(function ($request): bool {
$text = (string) ($request['markdown']['text'] ?? '');
return str_contains($text, 'D-1')
&& ! str_contains($text, '`B-1`')
&& ! str_contains($text, '`C-1`');
});
}
public function test_push_without_robot_configuration_is_rejected_without_http_call(): void
{
$this->seedReconciliation();
Http::fake();
$this->loginAs()
->post(route('business.supplier-reconciliation.push-differences'), [
'supplier' => '晨丰',
'month' => '2026-09',
'reconciler' => '张三',
'scope' => 'all',
])
->assertStatus(302)
->assertSessionHasErrors('dingtalk_push');
Http::assertNothingSent();
}
public function test_large_difference_set_is_sent_in_multiple_robot_messages(): void
{
$this->saveRobot();
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
$rows = [];
for ($index = 1; $index <= 31; $index++) {
$rows[] = [
'barcode' => sprintf('BULK-%02d', $index),
'supplierBarcode' => '',
'purchaseNo' => sprintf('PO-BULK-%02d', $index),
'date' => '2026-09-30',
'unitPrice' => 10,
'mineUnitPrice' => 10,
'theirsUnitPrice' => 10,
'mineQty' => 2,
'theirsQty' => 1,
'mineAmount' => 20,
'theirsAmount' => 10,
'qtyDifference' => -1,
'amountDifference' => -10,
'type' => '数量/金额不一致',
'settleAmount' => 10,
'note' => '厂家数量少 1;按金额低的一方结算',
];
}
$result = app(SupplierDingTalkNotifier::class)->push('晨丰', $rows, [
'month' => '2026-09',
'reconciler' => '张三',
]);
$this->assertSame(31, $result['rows']);
$this->assertSame(2, $result['messages']);
Http::assertSentCount(2);
}
public function test_difference_messages_stay_below_dingtalk_body_limit_when_details_are_large(): void
{
$this->saveRobot();
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
$rows = [];
for ($index = 1; $index <= 38; $index++) {
$details = [];
for ($detail = 1; $detail <= 50; $detail++) {
$details[] = [
'purchaseNo' => sprintf('PO-%03d-%03d', $index, $detail),
'expressNo' => sprintf('SF-%03d-%03d', $index, $detail),
'barcode' => sprintf('BARCODE-%03d-%03d', $index, $detail),
'mineQty' => 2,
'mineUnitPrice' => 58.29,
'mineAmount' => 116.58,
'theirsQty' => 1,
'theirsUnitPrice' => 58.29,
'theirsAmount' => 58.29,
'amountDifference' => 58.29,
];
}
$rows[] = [
'barcode' => sprintf('BULK-LONG-%03d', $index),
'purchaseNo' => sprintf('PO-LONG-%03d', $index),
'date' => '2026-11-30',
'mineQty' => 100,
'theirsQty' => 50,
'mineAmount' => 5829,
'theirsAmount' => 2914.5,
'mineUnitPrice' => 58.29,
'theirsUnitPrice' => 58.29,
'type' => '数量/金额不一致',
'note' => '厂家数量少,请修改后重新上传',
'differenceDetails' => $details,
];
}
$result = app(SupplierDingTalkNotifier::class)->push('晨丰', $rows, [
'month' => '2026-11',
'reconciler' => '张三',
]);
$this->assertSame(38, $result['rows']);
$this->assertGreaterThan(1, $result['messages']);
$this->assertLessThanOrEqual(20, $result['messages']);
Http::assertSentCount($result['messages']);
$texts = [];
foreach (Http::recorded() as [$request]) {
$this->assertLessThanOrEqual(20000, strlen($request->body()));
$texts[] = (string) ($request['markdown']['text'] ?? '');
}
$combined = implode("\n", $texts);
$this->assertStringContainsString('差异共 **38** 条', $combined);
$this->assertStringContainsString('其余 47 项明细请登录厂家对账门户查看', $combined);
$this->assertStringContainsString('1. **数量/金额不一致**', $combined);
$this->assertStringContainsString('38. **数量/金额不一致**', $combined);
}
public function test_finance_page_shows_monthly_return_push_entry(): void
{
$this->seedReconciliation();
$this->loginAs()->get($this->pageUrl())
->assertOk()
->assertSeeText('一键推送整月退货')
->assertSeeText('推送该月全部退货,包含待确认、已确认和异议中记录,不按确认状态筛选。')
->assertSee('name="month"', false);
}
public function test_finance_supplier_list_is_paginated_fifteen_per_page_with_return_push_action(): void
{
$companyId = (int) $this->company->id;
$store = app(SupplierPayableOverrideStore::class);
for ($index = 1; $index <= 16; $index++) {
$supplier = sprintf('分页厂家%02d', $index);
$store->save('final:supplier:return-reconciliation', 'return:' . $index, [
'summary_month' => '2026-10',
'send_time' => '2026-10-' . str_pad((string) $index, 2, '0', STR_PAD_LEFT) . ' 10:00:00',
'supplier' => $supplier,
'barcode' => 'RETURN-PAGE-' . $index,
'sku_name' => '分页退货',
'qty' => '1',
'unit_price' => '10',
'amount' => '10',
], $companyId, null);
}
app(SupplierDingTalkNotifier::class)->save('分页厂家01', self::WEBHOOK, 'SEC-test-123', false);
$pageUrl = route('business.pages.show', [
'company_id' => $companyId,
'section' => 'supplier-reconciliation',
'page' => 'finance-data',
'month' => '2026-10',
]);
$firstPage = $this->loginAs()->get($pageUrl)->assertOk();
$firstPage->assertSeeText('共 16 家')
->assertSeeText('每页 15 家')
->assertSeeText('第 1 / 2 页')
->assertSee('id="select-all-return-push"', false)
->assertSee('class="js-batch-supplier-checkbox"', false)
->assertSeeText('一键推送退货')
->assertSee('name="month" value="2026-10"', false);
$firstHtml = (string) $firstPage->getContent();
$firstTableStart = strpos($firstHtml, 'data-finance-supplier-list');
$firstTableEnd = strpos($firstHtml, '</table>', $firstTableStart);
$this->assertNotFalse($firstTableStart);
$this->assertNotFalse($firstTableEnd);
$firstTable = substr($firstHtml, $firstTableStart, $firstTableEnd - $firstTableStart);
$this->assertStringContainsString('分页厂家01', $firstTable);
$this->assertStringContainsString('分页厂家15', $firstTable);
$this->assertStringNotContainsString('分页厂家16', $firstTable);
$secondPage = $this->loginAs()->get($pageUrl . '&supplier_page=2')->assertOk();
$secondPage->assertSeeText('第 2 / 2 页');
$secondHtml = (string) $secondPage->getContent();
$secondTableStart = strpos($secondHtml, 'data-finance-supplier-list');
$secondTableEnd = strpos($secondHtml, '</table>', $secondTableStart);
$this->assertNotFalse($secondTableStart);
$this->assertNotFalse($secondTableEnd);
$secondTable = substr($secondHtml, $secondTableStart, $secondTableEnd - $secondTableStart);
$this->assertStringContainsString('分页厂家16', $secondTable);
$this->assertStringNotContainsString('分页厂家01', $secondTable);
}
public function test_reconciler_options_include_finance_names_when_month_has_only_returns(): void
{
$companyId = (int) $this->company->id;
$store = app(SupplierPayableOverrideStore::class);
$store->save('final:supplier:payment-reconciliation', 'payment:september', [
'summary_month' => '2026-09',
'purchaseNo' => 'PO-SEPTEMBER',
'date' => '2026-09-30',
'supplier' => '晨丰',
'skuCode' => 'SKU-1',
'qty' => '1',
'unitPrice' => '10',
'amount' => '10',
'reconciler' => '李萌',
], $companyId, null);
$store->save('final:supplier:return-reconciliation', 'return:october', [
'summary_month' => '2026-10',
'send_time' => '2026-10-08 10:00:00',
'supplier' => '晨丰',
'barcode' => 'RETURN-OCTOBER',
'sku_name' => '十月退货',
'qty' => '1',
'unit_price' => '10',
'amount' => '10',
'reconciler' => '陈亲园',
], $companyId, null);
$store->save('final:supplier:return-reconciliation', 'return:october-foreign', [
'summary_month' => '2026-10',
'send_time' => '2026-10-09 10:00:00',
'supplier' => '鸣飞家具',
'barcode' => 'RETURN-OCTOBER-FOREIGN',
'sku_name' => '其他对账人退货',
'qty' => '1',
'unit_price' => '10',
'amount' => '10',
'reconciler' => '李萌',
], $companyId, null);
$this->loginAs()->get(route('business.pages.show', [
'company_id' => $companyId,
'section' => 'supplier-reconciliation',
'page' => 'finance-data',
'month' => '2026-10',
'reconciler' => '陈亲园',
]))
->assertOk()
->assertSee('<option value="李萌"', false)
->assertSee('<option value="陈亲园"', false)
->assertSeeText('共 1 家')
->assertSeeText('晨丰')
->assertDontSeeText('鸣飞家具');
}
public function test_batch_return_push_sends_each_selected_supplier_to_its_own_robot(): void
{
$companyId = (int) $this->company->id;
$store = app(SupplierPayableOverrideStore::class);
$notifier = app(SupplierDingTalkNotifier::class);
foreach (['晨丰', '鸣飞家具'] as $index => $supplier) {
$notifier->save($supplier, self::WEBHOOK, 'SEC-test-' . $index, false);
$store->save('final:supplier:return-reconciliation', 'return:' . $index, [
'summary_month' => '2026-10',
'send_time' => '2026-10-0' . ($index + 1) . ' 10:00:00',
'supplier' => $supplier,
'barcode' => 'RETURN-BATCH-' . $index,
'sku_name' => '批量推送退货',
'qty' => '1',
'unit_price' => '10',
'amount' => '10',
], $companyId, null);
}
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
$this->loginAs()
->post(route('business.supplier-reconciliation.push-returns-batch'), [
'selected_returns' => [
json_encode(['supplier' => '晨丰', 'month' => '2026-10']),
json_encode(['supplier' => '鸣飞家具', 'month' => '2026-10']),
],
])
->assertStatus(302)
->assertSessionHas('success', '已批量推送 2 家供应商,共 2 条退货(2 条钉钉消息)。');
Http::assertSentCount(2);
Http::assertSent(fn ($request): bool => str_contains(
(string) ($request['markdown']['text'] ?? ''),
'供应商退货对账通知 · 晨丰',
));
Http::assertSent(fn ($request): bool => str_contains(
(string) ($request['markdown']['text'] ?? ''),
'供应商退货对账通知 · 鸣飞家具',
));
}
public function test_batch_return_push_reports_suppliers_without_robots(): void
{
$companyId = (int) $this->company->id;
$store = app(SupplierPayableOverrideStore::class);
app(SupplierDingTalkNotifier::class)->save('晨丰', self::WEBHOOK, 'SEC-test-123', false);
foreach (['晨丰', '鸣飞家具'] as $index => $supplier) {
$store->save('final:supplier:return-reconciliation', 'return:partial:' . $index, [
'summary_month' => '2026-10',
'send_time' => '2026-10-0' . ($index + 1) . ' 10:00:00',
'supplier' => $supplier,
'barcode' => 'RETURN-PARTIAL-' . $index,
'sku_name' => '部分推送退货',
'qty' => '1',
'unit_price' => '10',
'amount' => '10',
], $companyId, null);
}
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
$this->loginAs()
->post(route('business.supplier-reconciliation.push-returns-batch'), [
'selected_returns' => [
json_encode(['supplier' => '晨丰', 'month' => '2026-10']),
json_encode(['supplier' => '鸣飞家具', 'month' => '2026-10']),
],
])
->assertStatus(302)
->assertSessionHas('success', function (string $message): bool {
return str_contains($message, '已批量推送 1 家供应商,共 1 条退货(1 条钉钉消息)')
&& str_contains($message, '未推送 1 家:鸣飞家具(该厂家还没有配置钉钉群机器人 Webhook。)');
});
Http::assertSentCount(1);
}
public function test_return_month_follows_the_selected_accounting_month(): void
{
$this->seedReconciliation();
$this->saveRobot();
$store = app(SupplierPayableOverrideStore::class);
$store->save(
'final:supplier:payment-reconciliation',
'mine:october',
[
'summary_month' => '2026-10',
'purchaseNo' => 'OCTOBER-1',
'date' => '2026-10-31',
'supplier' => '晨丰',
'skuCode' => 'OCTOBER-1',
'qty' => '1',
'unitPrice' => '10',
'amount' => '10',
],
(int) $this->company->id,
null,
);
$store->save(
'final:supplier:return-reconciliation',
'return:october-entry',
[
'summary_month' => '2026-10',
'send_time' => '2026-10-08 10:00:00',
'supplier' => '晨丰',
'barcode' => 'RETURN-OCTOBER-ENTRY',
'sku_name' => '十月退货',
'qty' => '1',
'unit_price' => '10',
'amount' => '10',
],
(int) $this->company->id,
null,
);
$this->loginAs()->get(route('business.pages.show', [
'section' => 'supplier-reconciliation',
'page' => 'finance-data',
'supplier' => '晨丰',
'month' => '2026-10',
]))
->assertOk()
->assertSee('<option value="2026-10"', false)
->assertSeeText('一键推送整月退货')
->assertSee('name="month" value="2026-10" required', false);
}
public function test_finance_return_table_uses_the_supplier_data_headers(): void
{
$this->seedReconciliation();
$html = $this->loginAs()->get($this->pageUrl())->assertOk()->getContent();
$tableStart = strpos($html, 'data-finance-return-table');
$this->assertNotFalse($tableStart, '财务退货表应正常渲染。');
$headStart = strpos($html, '<thead>', $tableStart);
$headEnd = strpos($html, '</thead>', $headStart);
$this->assertNotFalse($headStart);
$this->assertNotFalse($headEnd);
preg_match_all('/<th scope="col">(.*?)<\/th>/s', substr($html, $headStart, $headEnd - $headStart), $matches);
$headers = array_map(
static fn (string $value): string => html_entity_decode(strip_tags(trim($value))),
$matches[1],
);
$this->assertSame([
'发货时间',
'明细供应商',
'条形码',
'商品名称',
'发货量',
'单价',
'金额',
'供应商条码',
'备注',
'标题',
'wms配货单号',
'仓库名称',
'快递单号',
'类型',
'操作',
], $headers);
}
public function test_monthly_return_push_sends_all_statuses_and_only_the_selected_supplier_month(): void
{
$this->saveRobot();
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
$companyId = (int) $this->company->id;
$store = app(SupplierPayableOverrideStore::class);
foreach ([
'return:pending' => [
'send_time' => '2026-09-03 10:00:00', 'barcode' => 'RETURN-PENDING',
'sku_name' => '待确认退货', 'qty' => '1', 'unit_price' => '100', 'amount' => '100',
],
'return:confirmed' => [
'send_time' => '2026-09-12 10:00:00', 'barcode' => 'RETURN-CONFIRMED',
'sku_name' => '已确认退货', 'qty' => '2', 'unit_price' => '50', 'amount' => '100',
],
'return:disputed' => [
'send_time' => '2026-09-22 10:00:00', 'barcode' => 'RETURN-DISPUTED',
'sku_name' => '异议中退货', 'qty' => '3', 'unit_price' => '10', 'amount' => '30',
],
'return:october' => [
'send_time' => '2026-10-01 10:00:00', 'barcode' => 'RETURN-OCTOBER',
'sku_name' => '其他月份退货', 'qty' => '9', 'unit_price' => '10', 'amount' => '90',
],
'return:foreign' => [
'send_time' => '2026-09-15 10:00:00', 'supplier' => '鸣飞家具', 'barcode' => 'RETURN-FOREIGN',
'sku_name' => '其他厂家退货', 'qty' => '8', 'unit_price' => '10', 'amount' => '80',
],
] as $key => $row) {
$store->save('final:supplier:return-reconciliation', $key, $row + [
'summary_month' => '2026-09',
'supplier' => '晨丰',
'warehouse' => '一号仓',
'wms_pick_no' => 'WMS-' . strtoupper(substr($key, 7)),
'express_no' => 'SF-' . strtoupper(substr($key, 7)),
], $companyId, null);
}
$confirmations = app(SupplierReturnConfirmation::class);
$confirmations->mark('晨丰', 'return:confirmed', SupplierReturnConfirmation::STATUS_CONFIRMED, $companyId, null);
$confirmations->mark('晨丰', 'return:disputed', SupplierReturnConfirmation::STATUS_DISPUTED, $companyId, null);
$this->loginAs()
->post(route('business.supplier-reconciliation.push-returns'), [
'supplier' => '晨丰',
'month' => '2026-09',
])
->assertStatus(302)
->assertSessionHas('success', '已推送 3 条退货到 晨丰 钉钉群(2026-09,1 条消息)。');
Http::assertSentCount(1);
Http::assertSent(function ($request): bool {
$text = (string) ($request['markdown']['text'] ?? '');
return str_contains($request->url(), 'oapi.dingtalk.com/robot/send')
&& str_contains($text, '供应商退货对账通知 · 晨丰')
&& str_contains($text, '业务类型:货款对账 / 退货明细')
&& str_contains($text, '- 对账月份:**2026-09**')
&& str_contains($text, '待确认 1 / 已确认 1 / 异议中 1')
&& str_contains($text, '[查看退货明细表格](')
&& str_contains($text, '全部确认无异议')
&& str_contains($text, '无需厂家登录')
&& ! str_contains($text, 'RETURN-PENDING')
&& ! str_contains($text, 'RETURN-CONFIRMED')
&& ! str_contains($text, 'RETURN-DISPUTED')
&& ! str_contains($text, 'RETURN-OCTOBER')
&& ! str_contains($text, 'RETURN-FOREIGN');
});
}
public function test_return_share_page_displays_table_and_download_links_without_login(): void
{
$companyId = (int) $this->company->id;
$store = app(SupplierPayableOverrideStore::class);
foreach ([
'return:share-pending' => ['barcode' => 'SHARE-PENDING', 'qty' => '2', 'amount' => '20'],
'return:share-confirmed' => ['barcode' => 'SHARE-CONFIRMED', 'qty' => '3', 'amount' => '30'],
] as $key => $row) {
$store->save('final:supplier:return-reconciliation', $key, $row + [
'summary_month' => '2026-10',
'send_time' => '2026-10-08 10:00:00',
'supplier' => '晨丰',
'sku_name' => '共享明细商品',
'unit_price' => '10',
'warehouse' => '一号仓',
'wms_pick_no' => 'WMS-SHARE',
'express_no' => 'SF-SHARE',
], $companyId, null);
}
$url = app(SupplierReturnShareLink::class)->pageUrl($companyId, '晨丰', '2026-10');
$datasetQueries = [];
DB::connection(config('database.supplier_payable_maintenance_connection'))
->listen(function ($query) use (&$datasetQueries): void {
if (str_contains($query->sql, 'supplier_payable_overrides')
&& in_array('final:supplier:return-reconciliation', $query->bindings, true)) {
$datasetQueries[] = $query->sql;
}
});
$response = $this->get($url)
->assertOk()
->assertHeader('X-Robots-Tag', 'noindex, nofollow, noarchive')
->assertHeader('Referrer-Policy', 'no-referrer');
$this->assertNotEmpty($datasetQueries);
foreach ($datasetQueries as $sql) {
$this->assertMatchesRegularExpression(
'/supplier_name_key|json_extract\\([^)]*supplier/i',
$sql,
);
}
$response->assertSeeText('晨丰 · 2026-10 退货明细')
->assertSeeText('全部确认无异议(2 行)')
->assertSeeText('下载 Excel')
->assertSeeText('下载 CSV')
->assertSeeText('发货时间')
->assertSeeText('wms配货单号')
->assertSeeText('SHARE-PENDING')
->assertSeeText('SHARE-CONFIRMED')
->assertDontSeeText('本月退货行数')
->assertDontSeeText('供应商条码');
$response->assertSee('table-layout: fixed', false)
->assertSee('overflow-x: hidden', false)
->assertSee('<colgroup>', false)
->assertDontSee('min-width: 1580px', false);
$html = (string) $response->getContent();
$tableStart = strpos($html, '<table>');
$tableEnd = strpos($html, '</table>', $tableStart);
$this->assertNotFalse($tableStart);
$this->assertNotFalse($tableEnd);
$tableHtml = substr($html, $tableStart, $tableEnd - $tableStart);
$this->assertSame(14, substr_count($tableHtml, '<th scope="col">'));
$summaryStart = strpos($html, '<section class="share-summary">');
$summaryEnd = strpos($html, '</section>', $summaryStart);
$this->assertNotFalse($summaryStart);
$this->assertNotFalse($summaryEnd);
$summaryHtml = substr($html, $summaryStart, $summaryEnd - $summaryStart);
$this->assertGreaterThan(
strpos($summaryHtml, '确认状态'),
strpos($summaryHtml, '全部确认无异议'),
);
$this->assertMatchesRegularExpression(
'/href="([^"]*supplier-returns[^"]*\/excel[^"]*)"/',
$html,
);
preg_match('/href="([^"]*supplier-returns[^"]*\/excel[^"]*)"/', $html, $matches);
$excelUrl = html_entity_decode($matches[1], ENT_QUOTES | ENT_HTML5, 'UTF-8');
$this->get($excelUrl)
->assertOk()
->assertHeader('content-type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
preg_match('/href="([^"]*supplier-returns[^"]*\/csv[^"]*)"/', $html, $matches);
$this->assertNotEmpty($matches);
$csvUrl = html_entity_decode($matches[1], ENT_QUOTES | ENT_HTML5, 'UTF-8');
$this->get($csvUrl)
->assertOk()
->assertHeader('content-type', 'text/csv; charset=UTF-8');
}
public function test_manufacturer_confirmation_from_signed_link_is_saved_to_the_system(): void
{
$companyId = (int) $this->company->id;
$store = app(SupplierPayableOverrideStore::class);
foreach ([
'return:confirm-first' => 'CONFIRM-FIRST',
'return:confirm-disputed' => 'CONFIRM-DISPUTED',
] as $key => $barcode) {
$store->save('final:supplier:return-reconciliation', $key, [
'summary_month' => '2026-10',
'send_time' => '2026-10-08 10:00:00',
'supplier' => '晨丰',
'barcode' => $barcode,
'sku_name' => '厂家确认测试',
'qty' => '1',
'unit_price' => '10',
'amount' => '10',
], $companyId, null);
}
app(SupplierReturnConfirmation::class)->mark(
'晨丰',
'return:confirm-disputed',
SupplierReturnConfirmation::STATUS_DISPUTED,
$companyId,
null,
);
$this->saveRobot();
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
config([
'app.timezone' => 'UTC',
'app.platform_timezone' => 'Asia/Shanghai',
]);
$this->travelTo(Carbon::parse('2026-11-04 07:09:02', 'UTC'));
$pageUrl = app(SupplierReturnShareLink::class)->pageUrl($companyId, '晨丰', '2026-10');
$page = $this->get($pageUrl)->assertOk();
$page->assertSeeText('全部确认无异议(1 行)');
$html = (string) $page->getContent();
preg_match('/action="([^"]*supplier-returns[^"]*\/confirm-all[^"]*)"/', $html, $matches);
$this->assertNotEmpty($matches);
$confirmUrl = html_entity_decode($matches[1], ENT_QUOTES | ENT_HTML5, 'UTF-8');
$redirect = $this->post($confirmUrl)
->assertStatus(302)
->assertSessionHas(
'success',
'已全部确认:1 行退货扣减生效(1 行异议保持原状态,暂不扣减);确认回执已发送到该厂家钉钉群。',
);
Http::assertSentCount(1);
Http::assertSent(fn ($request): bool => str_contains(
(string) ($request['markdown']['text'] ?? ''),
'供应商退货确认回执 · 晨丰',
) && str_contains(
(string) ($request['markdown']['text'] ?? ''),
'系统状态:**已回传财务系统**',
) && str_contains(
(string) ($request['markdown']['text'] ?? ''),
'确认时间:2026-11-04 15:09:02',
));
$statuses = app(SupplierReturnConfirmation::class)->statuses('晨丰', $companyId);
$this->assertSame(SupplierReturnConfirmation::STATUS_CONFIRMED, $statuses['return:confirm-first'] ?? null);
$this->assertSame(SupplierReturnConfirmation::STATUS_DISPUTED, $statuses['return:confirm-disputed'] ?? null);
$confirmationRows = collect($store->listRows(
SupplierReturnConfirmation::PREFIX . '晨丰',
$companyId,
))->keyBy('_action_key');
$this->assertSame(
'manufacturer_link',
$confirmationRows['return:confirm-first']['marked_via'] ?? null,
);
$this->get($redirect->headers->get('Location'))
->assertOk()
->assertSeeText('已全部确认:1 行退货扣减生效')
->assertSeeText('待 0 · 已 1 · 异 1');
}
public function test_return_share_link_rejects_a_tampered_supplier_token(): void
{
$companyId = (int) $this->company->id;
app(SupplierPayableOverrideStore::class)->save(
'final:supplier:return-reconciliation',
'return:share-security',
[
'summary_month' => '2026-10',
'send_time' => '2026-10-08 10:00:00',
'supplier' => '晨丰',
'barcode' => 'SHARE-SECURITY',
'sku_name' => '签名校验商品',
'qty' => '1',
'unit_price' => '10',
'amount' => '10',
],
$companyId,
null,
);
$url = app(SupplierReturnShareLink::class)->pageUrl($companyId, '晨丰', '2026-10');
$tampered = preg_replace_callback(
'/supplier-returns\/([^?\/]+)/',
static fn (array $matches): string => 'supplier-returns/'
. ($matches[1][0] === 'A' ? 'B' : 'A') . substr($matches[1], 1),
$url,
1,
);
$this->assertNotSame($url, $tampered);
$this->expectException(InvalidSignatureException::class);
$this->get($tampered);
}
public function test_large_monthly_return_push_uses_one_summary_message_without_chat_details(): void
{
$this->saveRobot();
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
$rows = [];
for ($index = 1; $index <= 601; $index++) {
$rows[] = $this->returnNotificationRow($index);
}
$result = app(SupplierDingTalkNotifier::class)->pushReturns('晨丰', $rows, '2026-09', (int) $this->company->id);
$this->assertSame(601, $result['rows']);
$this->assertSame(1, $result['messages']);
Http::assertSentCount(1);
Http::assertSent(function ($request): bool {
$text = (string) ($request['markdown']['text'] ?? '');
return str_contains($text, '本月全部退货:**601** 行')
&& str_contains($text, '[查看退货明细表格](')
&& ! str_contains($text, 'RETURN-BULK-01')
&& ! str_contains($text, 'RETURN-BULK-601');
});
}
public function test_monthly_return_push_rejects_an_empty_month_without_http_call(): void
{
$this->saveRobot();
Http::fake();
$this->loginAs()
->post(route('business.supplier-reconciliation.push-returns'), [
'supplier' => '晨丰',
'month' => '2026-08',
])
->assertStatus(302)
->assertSessionHasErrors([
'dingtalk_return_push' => '该厂家在 2026-08 没有可推送的退货数据。',
]);
Http::assertNothingSent();
}
private function returnNotificationRow(int $index): array
{
return [
'send_time' => '2026-09-' . str_pad((string) (($index - 1) % 28 + 1), 2, '0', STR_PAD_LEFT) . ' 10:00:00',
'barcode' => sprintf('RETURN-BULK-%02d', $index),
'sku_name' => '批量退货',
'qty' => '1',
'unit_price' => '10',
'amount' => '10',
'_supplier_qty' => 1.0,
'_supplier_amount' => 10.0,
'_status' => '',
'_accounting_label' => '厂家扣减',
'_accounting_note' => '按厂家退货扣减',
];
}
private function seedReconciliation(): void
{
$companyId = (int) $this->company->id;
$store = app(SupplierPayableOverrideStore::class);
foreach ([
'mine:1' => ['skuCode' => 'A-1', 'qty' => '2', 'unitPrice' => '100', 'amount' => '200'],
'mine:2' => ['skuCode' => 'B-1', 'qty' => '2', 'unitPrice' => '50', 'amount' => '100'],
'mine:3' => ['skuCode' => 'C-1', 'qty' => '1', 'unitPrice' => '30', 'amount' => '30'],
] as $key => $row) {
$store->save('final:supplier:payment-reconciliation', $key, $row + [
'summary_month' => '2026-09',
'purchaseNo' => '编号-' . $row['skuCode'],
'date' => '2026-09-30',
'supplier' => '晨丰',
'reconciler' => '张三',
], $companyId, null);
}
app(SupplierStatementStore::class)->save('晨丰', [
$this->statementRow('A-1', '100', '2', '200'),
$this->statementRow('B-1', '50', '1', '50'),
$this->statementRow('D-1', '80', '1', '80'),
], 'statement.csv', $companyId, null);
}
private function saveRobot(): void
{
app(SupplierDingTalkNotifier::class)->save('晨丰', self::WEBHOOK, 'SEC-test-123', false);
}
private function saveSummaryRobot(): void
{
config([
'dingtalk.client_id' => 'card-test-client',
'dingtalk.client_secret' => 'card-test-secret',
'dingtalk.card.enabled' => true,
'dingtalk.summary_confirmation_mode' => 'card',
'dingtalk.card.callback_url' => 'https://caiwu.example.test/webhooks/dingtalk/card/supplier-confirmation',
'dingtalk.card.callback_route_key' => 'supplier-summary-test-route',
'dingtalk.card.robot_code' => null,
'dingtalk.card.robot_name' => null,
]);
app(SupplierDingTalkNotifier::class)->save(
'晨丰',
self::WEBHOOK,
'SEC-test-123',
false,
'cid-supplier-test-group==',
);
}
private function fakeSummaryCardHttp(): void
{
Http::fake(function ($request) {
$url = $request->url();
if (str_contains($url, '/gettoken')) {
return Http::response([
'errcode' => 0,
'errmsg' => 'ok',
'access_token' => 'card-test-access-token',
'expires_in' => 7200,
]);
}
if (str_contains($url, '/v1.0/card/callbacks/register')) {
return Http::response(['success' => true]);
}
if (str_contains($url, '/v1.0/robot/groups/robots/query')) {
return Http::response([
'chatbotInstanceVOList' => [[
'robotCode' => 'ding-card-robot',
'name' => '曹县天山机器人',
'openRobotType' => 2,
]],
]);
}
if (str_contains($url, '/v1.0/card/ai/instances/createAndDeliver')) {
return Http::response([
'success' => true,
'result' => [
'outTrackId' => $request['outTrackId'] ?? null,
'deliverResults' => [[
'spaceType' => 'IM_GROUP',
'spaceId' => 'cid-supplier-test-group==',
'success' => true,
]],
],
]);
}
if (str_contains($url, '/v1.0/card/ai/instances/update')) {
return Http::response([
'success' => true,
'result' => [
'outTrackId' => $request['outTrackId'] ?? null,
'outUpdateId' => $request['outUpdateId'] ?? null,
'flowStatus' => 'CONFIRMED',
],
]);
}
return Http::response(['success' => false, 'message' => 'unexpected request'], 500);
});
}
private function pageUrl(): string
{
return route('business.pages.show', [
'section' => 'supplier-reconciliation',
'page' => 'finance-data',
'supplier' => '晨丰',
'month' => '2026-09',
'reconciler' => '张三',
]);
}
private function statementRow(string $barcode, string $unitPrice, string $qty, string $amount): array
{
return [
'expressName' => null,
'expressNo' => null,
'purchaseNo' => '编号-' . $barcode,
'createdAt' => '2026-09-30',
'supplier' => '晨丰',
'barcode' => $barcode,
'skuName' => '规格甲',
'qty' => $qty,
'unitPrice' => $unitPrice,
'amount' => $amount,
];
}
}