上传入口不设截止时间;系统仅在每月 1-14 日 10:00 提醒未上传厂家,15 日财务出上月终版。 Co-authored-by: 苏木 <594326199@qq.com> Co-committed-by: 苏木 <594326199@qq.com>
263 lines
12 KiB
PHP
263 lines
12 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Business;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Modules\EcommerceFinance\Services\SupplierDingTalkNotifier;
|
|
use Modules\EcommerceFinance\Services\SupplierPayableOverrideStore;
|
|
use Modules\EcommerceFinance\Services\SupplierStatementReminder;
|
|
use Modules\EcommerceFinance\Services\SupplierStatementStore;
|
|
use Tests\Feature\FeatureTestCase;
|
|
|
|
class SupplierStatementReminderTest extends FeatureTestCase
|
|
{
|
|
private const WEBHOOK = 'https://oapi.dingtalk.com/robot/send?access_token=statement-reminder-test';
|
|
|
|
private const DATASET = 'final:supplier:payment-reconciliation';
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->company->makeCurrent();
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
Carbon::setTestNow();
|
|
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_daily_reminder_uses_imported_month_suppliers_and_stops_after_upload(): void
|
|
{
|
|
Carbon::setTestNow(Carbon::parse('2026-11-01 10:00:00', 'Asia/Shanghai'));
|
|
$this->seedSourceRow('2026-10', '晨丰', 'oct:chenfeng');
|
|
$this->seedSourceRow('2026-10', '鸣飞家具', 'oct:mingfei');
|
|
$this->seedSourceRow('2026-09', '历史供应商', 'sep:history');
|
|
$notifier = app(SupplierDingTalkNotifier::class);
|
|
$notifier->save('晨丰', self::WEBHOOK, '', false);
|
|
$notifier->save('鸣飞家具', self::WEBHOOK . '-mingfei', '', false);
|
|
$notifier->save('历史供应商', self::WEBHOOK . '-history', '', false);
|
|
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
|
|
|
|
$this->artisan('supplier-statement:send-reminders', ['--date' => '2026-11-01'])
|
|
->assertSuccessful();
|
|
$this->artisan('supplier-statement:send-reminders', ['--date' => '2026-11-01'])
|
|
->assertSuccessful();
|
|
|
|
Http::assertSentCount(2);
|
|
$texts = Http::recorded()
|
|
->map(static fn (array $pair): string => (string) ($pair[0]['markdown']['text'] ?? ''))
|
|
->implode("\n");
|
|
$this->assertStringContainsString('供应商月度对账单催办 · 晨丰', $texts);
|
|
$this->assertStringContainsString('供应商月度对账单催办 · 鸣飞家具', $texts);
|
|
$this->assertStringNotContainsString('历史供应商', $texts);
|
|
$this->assertStringContainsString('对账月份:**2026年10月**', $texts);
|
|
$this->assertStringContainsString('终版日期:**11月15日**', $texts);
|
|
$this->assertStringContainsString('距离财务出上月终版还有 **14 天**', $texts);
|
|
$this->assertStringContainsString('上传入口不设截止时间', $texts);
|
|
$this->assertStringContainsString('/1/workspace/supplier-portal', $texts);
|
|
|
|
$state = json_decode((string) setting('supplier_reconciliation.statement_reminders'), true);
|
|
$this->assertArrayHasKey('晨丰', $state['2026-10']['2026-11-01'] ?? []);
|
|
$this->assertArrayHasKey('鸣飞家具', $state['2026-10']['2026-11-01'] ?? []);
|
|
|
|
Carbon::setTestNow(Carbon::parse('2026-11-02 10:00:00', 'Asia/Shanghai'));
|
|
app(SupplierStatementStore::class)->save(
|
|
'晨丰',
|
|
[],
|
|
'晨丰-202610.csv',
|
|
(int) $this->company->id,
|
|
null,
|
|
);
|
|
$this->artisan('supplier-statement:send-reminders', ['--date' => '2026-11-02'])
|
|
->assertSuccessful();
|
|
|
|
Http::assertSentCount(3);
|
|
$lastRequest = Http::recorded()->last()[0];
|
|
$lastText = (string) ($lastRequest['markdown']['text'] ?? '');
|
|
$this->assertStringContainsString('鸣飞家具', $lastText);
|
|
$this->assertStringNotContainsString('晨丰', $lastText);
|
|
}
|
|
|
|
public function test_reminder_runs_every_day_from_1_through_14_and_not_on_15(): void
|
|
{
|
|
$this->seedSourceRow('2026-10', '晨丰', 'oct:daily');
|
|
app(SupplierDingTalkNotifier::class)->save('晨丰', self::WEBHOOK, '', false);
|
|
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
|
|
|
|
for ($day = 1; $day <= 15; $day++) {
|
|
$date = sprintf('2026-11-%02d', $day);
|
|
Carbon::setTestNow(Carbon::parse($date . ' 10:00:00', 'Asia/Shanghai'));
|
|
$this->artisan('supplier-statement:send-reminders', ['--date' => $date])
|
|
->assertSuccessful();
|
|
}
|
|
|
|
Http::assertSentCount(14);
|
|
$texts = Http::recorded()
|
|
->map(static fn (array $pair): string => (string) ($pair[0]['markdown']['text'] ?? ''))
|
|
->implode("\n");
|
|
$this->assertStringContainsString('距离财务出上月终版还有 **14 天**', $texts);
|
|
$this->assertStringContainsString('今天是终版前的最后一次系统提醒', $texts);
|
|
}
|
|
|
|
public function test_dynamic_source_tracks_added_and_removed_suppliers(): void
|
|
{
|
|
Carbon::setTestNow(Carbon::parse('2026-11-01 10:00:00', 'Asia/Shanghai'));
|
|
$this->seedSourceRow('2026-10', '晨丰', 'oct:a');
|
|
$this->seedSourceRow('2026-10', '鸣飞家具', 'oct:b');
|
|
$notifier = app(SupplierDingTalkNotifier::class);
|
|
$notifier->save('晨丰', self::WEBHOOK . '-a', '', false);
|
|
$notifier->save('鸣飞家具', self::WEBHOOK . '-b', '', false);
|
|
$notifier->save('新增供应商', self::WEBHOOK . '-c', '', false);
|
|
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
|
|
|
|
$this->artisan('supplier-statement:send-reminders', ['--date' => '2026-11-01'])
|
|
->assertSuccessful();
|
|
Http::assertSentCount(2);
|
|
|
|
Carbon::setTestNow(Carbon::parse('2026-11-02 10:00:00', 'Asia/Shanghai'));
|
|
app(SupplierStatementStore::class)->save('晨丰', [], '晨丰-202610.csv', (int) $this->company->id, null);
|
|
app(SupplierPayableOverrideStore::class)->deleteBatch(
|
|
self::DATASET,
|
|
['oct:b'],
|
|
(int) $this->company->id,
|
|
);
|
|
$this->seedSourceRow('2026-10', '新增供应商', 'oct:c');
|
|
|
|
$this->artisan('supplier-statement:send-reminders', ['--date' => '2026-11-02'])
|
|
->assertSuccessful();
|
|
Http::assertSentCount(3);
|
|
$lastText = (string) (Http::recorded()->last()[0]['markdown']['text'] ?? '');
|
|
$this->assertStringContainsString('新增供应商', $lastText);
|
|
$this->assertStringNotContainsString('鸣飞家具', $lastText);
|
|
}
|
|
|
|
public function test_dry_run_reports_source_uploaded_pending_and_missing_robot_suppliers(): void
|
|
{
|
|
Carbon::setTestNow(Carbon::parse('2026-11-03 10:00:00', 'Asia/Shanghai'));
|
|
$this->seedSourceRow('2026-10', '晨丰', 'oct:uploaded');
|
|
$this->seedSourceRow('2026-10', '鸣飞家具', 'oct:pending');
|
|
$this->seedSourceRow('2026-10', '未配置厂家', 'oct:no-robot');
|
|
$notifier = app(SupplierDingTalkNotifier::class);
|
|
$notifier->save('晨丰', self::WEBHOOK, '', false);
|
|
$notifier->save('鸣飞家具', self::WEBHOOK . '-pending', '', false);
|
|
app(SupplierStatementStore::class)->save('晨丰', [], '晨丰-202610.csv', (int) $this->company->id, null);
|
|
Http::fake();
|
|
|
|
$result = app(SupplierStatementReminder::class)->send(
|
|
Carbon::parse('2026-11-03 10:00:00', 'Asia/Shanghai'),
|
|
(int) $this->company->id,
|
|
true,
|
|
);
|
|
|
|
Http::assertNothingSent();
|
|
$this->assertSame(3, $result['source_suppliers']);
|
|
$this->assertSame(2, $result['configured']);
|
|
$this->assertSame(['鸣飞家具'], $result['previewed']);
|
|
$this->assertSame(['晨丰'], $result['uploaded']);
|
|
$this->assertSame(['未配置厂家'], $result['missing_robot']);
|
|
$this->assertNull(setting('supplier_reconciliation.statement_reminders'));
|
|
}
|
|
|
|
public function test_failed_delivery_is_not_recorded_and_can_be_retried(): void
|
|
{
|
|
Carbon::setTestNow(Carbon::parse('2026-11-03 10:00:00', 'Asia/Shanghai'));
|
|
$this->seedSourceRow('2026-10', '晨丰', 'oct:retry');
|
|
app(SupplierDingTalkNotifier::class)->save('晨丰', self::WEBHOOK, '', false);
|
|
Http::fakeSequence()
|
|
->push(['errcode' => 500, 'errmsg' => 'temporary failure'], 500)
|
|
->push(['errcode' => 0, 'errmsg' => 'ok']);
|
|
|
|
$this->artisan('supplier-statement:send-reminders', ['--date' => '2026-11-03'])
|
|
->assertFailed();
|
|
$this->assertNull(setting('supplier_reconciliation.statement_reminders'));
|
|
|
|
$this->artisan('supplier-statement:send-reminders', ['--date' => '2026-11-03'])
|
|
->assertSuccessful();
|
|
Http::assertSentCount(2);
|
|
$state = json_decode((string) setting('supplier_reconciliation.statement_reminders'), true);
|
|
$this->assertArrayHasKey('晨丰', $state['2026-10']['2026-11-03'] ?? []);
|
|
}
|
|
|
|
public function test_no_imported_source_suppliers_means_no_reminder(): void
|
|
{
|
|
Carbon::setTestNow(Carbon::parse('2026-11-03 10:00:00', 'Asia/Shanghai'));
|
|
$this->seedSourceRow('2026-09', '历史供应商', 'sep:only');
|
|
app(SupplierDingTalkNotifier::class)->save('历史供应商', self::WEBHOOK, '', false);
|
|
Http::fake();
|
|
|
|
$this->artisan('supplier-statement:send-reminders', ['--date' => '2026-11-03'])
|
|
->expectsOutputToContain('上月对账入库数据尚未导入或没有入库供应商')
|
|
->assertSuccessful();
|
|
|
|
Http::assertNothingSent();
|
|
$this->assertNull(setting('supplier_reconciliation.statement_reminders'));
|
|
}
|
|
|
|
public function test_dry_run_handles_122_imported_suppliers(): void
|
|
{
|
|
Carbon::setTestNow(Carbon::parse('2026-11-03 10:00:00', 'Asia/Shanghai'));
|
|
$suppliers = array_map(static fn (int $index): string => '测试供应商' . str_pad(
|
|
(string) $index,
|
|
3,
|
|
'0',
|
|
STR_PAD_LEFT,
|
|
), range(1, 122));
|
|
foreach ($suppliers as $index => $supplier) {
|
|
$this->seedSourceRow('2026-10', $supplier, 'oct:scale:' . $index);
|
|
}
|
|
|
|
$configs = [];
|
|
foreach ($suppliers as $supplier) {
|
|
$configs[$supplier] = [
|
|
'webhook' => Crypt::encryptString(self::WEBHOOK),
|
|
'secret' => '',
|
|
'at_all' => false,
|
|
'card_conversation_id' => '',
|
|
'card_robot_code' => '',
|
|
'last_pushed_at' => '',
|
|
'last_push_count' => 0,
|
|
];
|
|
}
|
|
setting()->set(
|
|
'supplier_reconciliation.dingtalk_robots',
|
|
json_encode($configs, JSON_UNESCAPED_UNICODE),
|
|
);
|
|
setting()->save();
|
|
Http::fake();
|
|
|
|
$result = app(SupplierStatementReminder::class)->send(
|
|
Carbon::parse('2026-11-03 10:00:00', 'Asia/Shanghai'),
|
|
(int) $this->company->id,
|
|
true,
|
|
);
|
|
|
|
Http::assertNothingSent();
|
|
$this->assertSame(122, $result['source_suppliers']);
|
|
$this->assertCount(122, $result['previewed']);
|
|
$this->assertSame([], $result['failed']);
|
|
$this->assertNull(setting('supplier_reconciliation.statement_reminders'));
|
|
}
|
|
|
|
private function seedSourceRow(string $month, string $supplier, string $key): void
|
|
{
|
|
app(SupplierPayableOverrideStore::class)->save(
|
|
self::DATASET,
|
|
$key,
|
|
[
|
|
'summary_month' => $month,
|
|
'date' => $month . '-01',
|
|
'supplier' => $supplier,
|
|
'qty' => '1',
|
|
'amount' => '10',
|
|
],
|
|
(int) $this->company->id,
|
|
null,
|
|
);
|
|
}
|
|
}
|