91 lines
5.2 KiB
PHP
91 lines
5.2 KiB
PHP
<?php
|
||
|
||
namespace Tests\Unit\EcommerceFinance;
|
||
|
||
use Carbon\Carbon;
|
||
use Modules\EcommerceFinance\Repositories\PlantingReimbursementIncomeRepository;
|
||
use Modules\EcommerceFinance\Services\PlantingReimbursementIncomeCheck as Check;
|
||
use Tests\TestCase;
|
||
|
||
class PlantingReimbursementIncomeCheckTest extends TestCase
|
||
{
|
||
protected function tearDown(): void
|
||
{
|
||
Carbon::setTestNow();
|
||
parent::tearDown();
|
||
}
|
||
private function row(array $patch = []): array
|
||
{
|
||
return array_replace(['platform' => '头条放心购', 'shop_name' => '抖音-测试', 'online_order_no' => " ' 1234567890123456789",
|
||
'paid_at' => '2026-11-12 12:00:00', 'task_payment' => '668.00', 'platform_order_status' => 'TRADE_FINISHED'], $patch);
|
||
}
|
||
|
||
private function check(array $rows, array $records, ?array $expectedOrders = null): array
|
||
{
|
||
Carbon::setTestNow('2026-11-20 12:00:00');
|
||
$repo = \Mockery::mock(PlantingReimbursementIncomeRepository::class);
|
||
$repo->shouldReceive('status')->andReturn(['jd_through' => '2026-11-11', 'douyin_failed' => true, 'douyin_last_sync' => '2026-11-07']);
|
||
$repo->shouldReceive('records')->once()->withArgs(function ($orders, $from, $to) use ($expectedOrders) {
|
||
$this->assertSame('2026-10-25 00:00:00', $from);
|
||
$this->assertSame('2026-11-21 00:00:00', $to);
|
||
if ($expectedOrders !== null) $this->assertSame($expectedOrders, $orders);
|
||
return true;
|
||
})->andReturnUsing(static function () use ($records) { yield from $records; });
|
||
return (new Check($repo))->rows($rows);
|
||
}
|
||
|
||
private function bill(array $patch = []): array
|
||
{
|
||
return array_replace(['source' => 'douyin', 'record_code' => 'B1', 'online_order_no' => '1234567890123456789', 'occurred_at' => '2026-11-19 10:00:00',
|
||
'bill_item' => '订单实付', 'ledger_remark' => '订单收入', 'income' => '630.0000', 'expense' => '2.0000'], $patch);
|
||
}
|
||
|
||
public function test_exact_order_deduplication_net_income_and_subsidy_does_not_offset_recovery(): void
|
||
{
|
||
$bill = $this->bill();
|
||
$result = $this->check([$this->row()], [$bill, $bill, $this->bill(['record_code' => 'PROMO', 'ledger_remark' => '优惠收入', 'bill_item' => '平台补贴', 'income' => '40.00', 'expense' => '0.00']),
|
||
$this->bill(['record_code' => 'OLD', 'occurred_at' => '2026-10-24 23:59:59']), $this->bill(['record_code' => 'PREFIX', 'online_order_no' => '1234567890123456789-A'])], ['douyin' => ['1234567890123456789']])[0];
|
||
$this->assertSame('628.00', $result['income_total']);
|
||
$this->assertSame(1, $result['income_count']);
|
||
$this->assertSame('40.00', $result['income_difference']);
|
||
$this->assertSame('多报销', $result['income_result']);
|
||
$this->assertStringContainsString('40.00元,仍算多报销', $result['income_note']);
|
||
}
|
||
|
||
public function test_cent_boundaries_zero_income_record_and_negative_net(): void
|
||
{
|
||
$rows = $bills = [];
|
||
foreach (['100.01', '99.99', '100.02', '99.98', '0.00', '-2.00'] as $i => $payment) {
|
||
$rows[] = $this->row(['online_order_no' => (string) $i, 'task_payment' => $payment]);
|
||
$bills[] = $this->bill(['record_code' => (string) $i, 'online_order_no' => (string) $i, 'income' => $i < 4 ? '100.00' : '0.00', 'expense' => $i === 5 ? '2.00' : '0.00']);
|
||
}
|
||
$result = $this->check($rows, $bills);
|
||
$this->assertSame(['一致', '一致', '多报销', '少报销', '一致', '一致'], array_column($result, 'income_result'));
|
||
}
|
||
|
||
public function test_missing_income_distinguishes_jd_coverage_finished_orders_and_other_bill_evidence(): void
|
||
{
|
||
$rows = [
|
||
$this->row(['platform' => '京东商城']), $this->row(),
|
||
$this->row(['platform' => '拼多多']), $this->row(['platform_order_status' => 'WAIT_BUYER_CONFIRM_GOODS']),
|
||
$this->row(['platform' => '阿里巴巴']),
|
||
];
|
||
$result = $this->check($rows, [$this->bill(['source' => 'pinduoduo', 'ledger_remark' => '退款', 'bill_item' => '退款支出', 'income' => '0.00', 'expense' => '5.00'])]);
|
||
$this->assertSame(['未入账', '需核实', '需核实', '未入账', '需核实'], array_column($result, 'income_result'));
|
||
$this->assertSame('京东账单尚未导入', $result[0]['income_note']);
|
||
$this->assertStringContainsString('可能未同步;抖音原始账单同步异常', $result[1]['income_note']);
|
||
$this->assertStringContainsString('退款支出 -5.00元(B1)', $result[2]['income_note']);
|
||
$this->assertSame('买家未确认收货', $result[3]['income_note']);
|
||
$this->assertSame('0.00', $result[3]['income_total']);
|
||
$this->assertSame('668.00', $result[3]['income_difference']);
|
||
}
|
||
|
||
public function test_each_work_order_uses_its_own_payment_month_window(): void
|
||
{
|
||
$result = $this->check([$this->row(['paid_at' => '2026-12-01']), $this->row()], [$this->bill(['occurred_at' => '2026-11-24 23:59:59'])]);
|
||
$this->assertSame(0, $result[0]['income_count']);
|
||
$this->assertSame(1, $result[1]['income_count']);
|
||
$this->assertSame('2025-12-25', Check::billFrom($this->row(['paid_at' => '2026-01-15'])));
|
||
}
|
||
}
|