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('推送全部差异(3 条)') ->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', '已推送 3 条差异到 晨丰 钉钉群(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, 'B-1') && str_contains($text, 'C-1') && str_contains($text, 'D-1') && str_contains($text, '采购单号:编号-B-1') && str_contains($text, '**数量/金额不一致**') && str_contains($text, ',我方数量 ') && str_contains($text, ',厂家数量 ') && ! str_contains($text, '日期:2026-09-30') && ! str_contains($text, '`A-1`') && ! str_contains($text, '鸣飞家具'); }); $config = app(SupplierDingTalkNotifier::class)->configuration('晨丰'); $this->assertNotSame('', $config['last_pushed_at']); $this->assertSame(3, $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_are_pushed_as_supplier_differences(): 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) ->assertSessionHas('success', '已推送 1 条差异到 未定价厂家 钉钉群(1 条消息)。'); Http::assertSentCount(1); Http::assertSent(function ($request): bool { $text = (string) ($request['markdown']['text'] ?? ''); return str_contains($text, '未定价 1') && str_contains($text, '**未定价**') && str_contains($text, '条码:SZ014916N1109V01,我方未定价,厂家单价 186,厂家数量 20') && ! 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, '条码:UNPRICED-1N1097V01,我方未定价,厂家单价 12,厂家数量 3') && ! str_contains($text, '`C-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' => '编号-B-1', 'date' => '2026-09-30', 'barcode' => 'B-1', 'difference_type' => '数量/金额不一致', ]) ->assertStatus(302) ->assertSessionHas('success', '已推送 1 条差异到 晨丰 钉钉群(1 条消息)。'); Http::assertSent(function ($request): bool { $text = (string) ($request['markdown']['text'] ?? ''); return str_contains($text, 'B-1') && ! str_contains($text, '`C-1`') && ! str_contains($text, '`D-1`'); }); } 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', '已推送 2 条差异到 晨丰 钉钉群(1 条消息)。'); Http::assertSent(function ($request): bool { $text = (string) ($request['markdown']['text'] ?? ''); return str_contains($text, 'B-1') && str_contains($text, 'D-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_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', ]); $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, '', $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, '', $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('