feat: 淘系支付宝账单新增期初余额与月度余额 #881

Merged
qiuxiang merged 1 commits from fix/codex-taobao-alipay-opening-balance into main 2026-11-05 11:05:06 +08:00
6 changed files with 317 additions and 8 deletions

View File

@ -0,0 +1,54 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
private function connection(): string
{
return (string) config('upstream_mirrors.connection', 'business_mirror');
}
public function up(): void
{
if (app()->environment('testing')
|| (string) config('system_database.mode', 'bootstrap') === 'bootstrap') {
return;
}
$schema = Schema::connection($this->connection());
if (! $schema->hasTable('taobao_alipay_bill_rows')
|| collect($schema->getIndexes('taobao_alipay_bill_rows'))
->contains('name', 'taobao_alipay_account_time_row_idx')) {
return;
}
$schema->table('taobao_alipay_bill_rows', function (Blueprint $table): void {
$table->index(
['alipay_account', 'occurred_at', 'source_row_number', 'balance_amount'],
'taobao_alipay_account_time_row_idx',
);
});
}
public function down(): void
{
if (app()->environment('testing')
|| (string) config('system_database.mode', 'bootstrap') === 'bootstrap') {
return;
}
$schema = Schema::connection($this->connection());
if (! $schema->hasTable('taobao_alipay_bill_rows')
|| ! collect($schema->getIndexes('taobao_alipay_bill_rows'))
->contains('name', 'taobao_alipay_account_time_row_idx')) {
return;
}
$schema->table('taobao_alipay_bill_rows', function (Blueprint $table): void {
$table->dropIndex('taobao_alipay_account_time_row_idx');
});
}
};

View File

@ -91,7 +91,14 @@ class AlipayBills extends Controller
'current_page' => 1,
'last_page' => 1,
'per_page' => min(max((int) $request->get('limit', 10), 1), 100),
'summary' => ['income' => 0, 'expense' => 0, 'balance' => 0, 'count' => 0],
'summary' => [
'income' => 0,
'expense' => 0,
'opening_balance' => 0,
'opening_balance_date' => null,
'balance' => 0,
'count' => 0,
],
], 503);
}
}

View File

@ -6,6 +6,7 @@ use DateTimeImmutable;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Schema;
use Modules\EcommerceFinance\Services\LedgerRemarkFilter;
use RuntimeException;
@ -15,6 +16,8 @@ class TaobaoAlipaySettlementBillRepository
private const IMPORT_TABLE = 'taobao_alipay_bill_imports';
private const ROW_TABLE = 'taobao_alipay_bill_rows';
private const OPENING_BALANCE_CACHE_SECONDS = 300;
private readonly ConnectionInterface $connection;
public function __construct(DatabaseManager $databases)
@ -61,6 +64,7 @@ class TaobaoAlipaySettlementBillRepository
$total = (int) ($summary->row_count ?? 0);
$income = (float) ($summary->income_total ?? 0);
$expense = (float) ($summary->expense_total ?? 0);
$opening = $this->openingBalance($filters);
$sortColumn = $this->sortColumn($sort);
$direction = $direction === 'asc' ? 'asc' : 'desc';
$query->orderBy($sortColumn, $direction);
@ -78,7 +82,9 @@ class TaobaoAlipaySettlementBillRepository
'summary' => [
'income' => $income,
'expense' => $expense,
'balance' => round($income - $expense, 2),
'opening_balance' => $opening['balance'],
'opening_balance_date' => $opening['date'],
'balance' => round($opening['balance'] + $income - $expense, 2),
'count' => $total,
],
'sync' => [
@ -167,6 +173,63 @@ class TaobaoAlipaySettlementBillRepository
][$sort] ?? 'occurred_at';
}
/**
* 当前账期开始前,每个支付宝账号最后一笔余额之和。
*
* 同一秒可能有多笔流水,按源表行号继续取最后一笔,避免同一时间点重复计入。
*
* @return array{balance:float,date:?string}
*/
private function openingBalance(array $filters): array
{
$start = $this->date($filters['start'] ?? null)
?? $this->date($filters['end'] ?? null);
if ($start === null) {
return ['balance' => 0.0, 'date' => null];
}
$monthStart = $start->modify('first day of this month');
$cutoff = $monthStart->format('Y-m-d') . ' 00:00:00';
$store = trim((string) ($filters['store_name'] ?? ''));
$importVersion = (string) ($this->connection->table(self::IMPORT_TABLE)
->where('status', 'succeeded')
->max('completed_at') ?? '');
$cacheKey = 'taobao-alipay-opening-balance:v1:' . hash('sha256', implode('|', [
$cutoff,
$store,
$importVersion,
]));
$balance = Cache::remember(
$cacheKey,
self::OPENING_BALANCE_CACHE_SECONDS,
function () use ($cutoff, $store): float {
$ranked = $this->connection->table(self::ROW_TABLE)
->where('occurred_at', '<', $cutoff)
->whereNotNull('alipay_account')
->where('alipay_account', '<>', '')
->when($store !== '', fn (Builder $query) => $query->where('store_name', $store))
->select(['alipay_account', 'occurred_at', 'source_row_number', 'balance_amount'])
->selectRaw(
'ROW_NUMBER() OVER (' .
'PARTITION BY alipay_account ' .
'ORDER BY occurred_at DESC, source_row_number DESC' .
') AS row_rank',
);
return round((float) $this->connection->query()
->fromSub($ranked, 'latest_balances')
->where('row_rank', 1)
->sum('balance_amount'), 2);
},
);
return [
'balance' => (float) $balance,
'date' => $monthStart->modify('-1 day')->format('Y-m-d'),
];
}
/** @return array<string,mixed> */
private function mapRow(object $row): array
{

View File

@ -1,6 +1,6 @@
<template>
<div class="table-workspace">
<div class="tw-stats">
<div class="tw-stats tw-stats--taobao-alipay">
<div v-for="stat in stats" :key="stat.label" class="tw-stat">
<div class="tw-label">{{ stat.label }}</div>
<div class="tw-value" :class="stat.className">{{ stat.value }}</div>
@ -163,11 +163,19 @@ const SORTABLE = ['occurred_at', 'income', 'expense', 'balance', 'record_code'];
export default {
name: 'TaobaoAlipayBills', components: { TableColumnSettings, flatPicker, [Select.name]: Select, [Option.name]: Option },
props: { apiBase: { type: String, required: true }, exportBase: { type: String, required: true }, progressBase: { type: String, default: '' }, userKey: { type: [String, Number], required: true } },
data() { return { loading: false, error: '', rows: [], configuredColumns: [], summary: { income: 0, expense: 0, balance: 0, count: 0 }, total: 0, currentPage: 1, lastPage: 1, perPage: 20, filters: { store_name: '', ledger_remark: '', source_channel: '', bill_item: '', business_type: '', ...currentMonthRange(), keyword: '' }, filterOptions: { shops: [], bill_items: [], ledger_remarks: [] }, filterOptionsLoading: false, filterOptionsError: false, sort: { key: 'occurred_at', dir: 'desc' }, exportMode: 'filtered-visible', exporting: false, exportModalOpen: false, exportTasks: [], exportHint: '导出在后台生成,成功后点「下载」;期间可以继续查看其它页面。', exportTimer: null }; },
data() { return { loading: false, error: '', rows: [], configuredColumns: [], summary: { income: 0, expense: 0, opening_balance: 0, opening_balance_date: null, balance: 0, count: 0 }, total: 0, currentPage: 1, lastPage: 1, perPage: 20, filters: { store_name: '', ledger_remark: '', source_channel: '', bill_item: '', business_type: '', ...currentMonthRange(), keyword: '' }, filterOptions: { shops: [], bill_items: [], ledger_remarks: [] }, filterOptionsLoading: false, filterOptionsError: false, sort: { key: 'occurred_at', dir: 'desc' }, exportMode: 'filtered-visible', exporting: false, exportModalOpen: false, exportTasks: [], exportHint: '导出在后台生成,成功后点「下载」;期间可以继续查看其它页面。', exportTimer: null }; },
computed: {
defaultColumns() { return COLUMNS; }, visibleColumns() { return this.configuredColumns.filter((column) => column.visible); },
visibleGroups() { return this.visibleColumns.reduce((groups, column) => { const last = groups[groups.length - 1]; if (last && last.label === column.group) last.span += 1; else groups.push({ label: column.group, span: 1 }); return groups; }, []); },
stats() { return [{ label: '收入合计', value: this.moneyWan(this.summary.income), note: `${this.summary.count} 笔交易`, className: 'is-income' }, { label: '支出合计', value: this.moneyWan(this.summary.expense), note: '按当前筛选', className: 'is-expense' }, { label: '收支差额', value: this.moneyWan(this.summary.balance), note: '收入合计 - 支出合计', className: '' }, { label: '交易笔数', value: this.summary.count, note: '当前筛选结果', className: '' }]; },
stats() {
return [
{ label: '期初余额', value: this.moneyWan(this.summary.opening_balance), note: this.summary.opening_balance_date ? `截至 ${this.summary.opening_balance_date} 最后一笔` : '上月最后一笔', className: '' },
{ label: '收入合计', value: this.moneyWan(this.summary.income), note: `${this.summary.count} 笔交易`, className: 'is-income' },
{ label: '支出合计', value: this.moneyWan(this.summary.expense), note: '按当前筛选', className: 'is-expense' },
{ label: '余额', value: this.moneyWan(this.summary.balance), note: '期初余额 + 收入合计 - 支出合计', className: '' },
{ label: '交易笔数', value: this.summary.count, note: '当前筛选结果', className: '' },
];
},
rangeStart() { return this.total === 0 ? 0 : (this.currentPage - 1) * this.perPage + 1; }, rangeEnd() { return Math.min(this.currentPage * this.perPage, this.total); },
companyPrefix() { const matched = (this.apiBase || '').match(/^\/\d+/); return matched ? matched[0] : ''; },
tasksBaseUrl() { return this.companyPrefix + '/ecommerce-finance/account-bills/export-tasks'; },
@ -179,7 +187,7 @@ export default {
mounted() { this.fetchFilterOptions(); this.fetch(); },
methods: {
async fetchFilterOptions() { this.filterOptionsLoading = true; this.filterOptionsError = false; try { const response = await window.axios.get(`${this.apiBase}/filter-options`); const body = response.data || {}; this.filterOptions = { shops: Array.isArray(body.shops) ? body.shops : [], bill_items: Array.isArray(body.bill_items) ? body.bill_items : [], ledger_remarks: Array.isArray(body.ledger_remarks) ? body.ledger_remarks : [] }; } catch (error) { this.filterOptionsError = true; } finally { this.filterOptionsLoading = false; } },
async fetch() { this.loading = true; this.error = ''; try { const response = await window.axios.get(this.apiBase, { params: { ...this.filters, sort: this.sort.key, dir: this.sort.dir, limit: this.perPage, page: this.currentPage } }); const body = response.data || {}; this.rows = body.data || []; this.total = body.total || 0; this.currentPage = body.current_page || 1; this.lastPage = body.last_page || 1; this.perPage = body.per_page || this.perPage; this.summary = body.summary || { income: 0, expense: 0, balance: 0, count: 0 }; } catch (error) { this.rows = []; this.total = 0; this.summary = { income: 0, expense: 0, balance: 0, count: 0 }; this.error = (error.response && error.response.data && error.response.data.message) || '淘系支付宝账单暂时无法读取,请稍后重试。'; } finally { this.loading = false; } },
async fetch() { this.loading = true; this.error = ''; try { const response = await window.axios.get(this.apiBase, { params: { ...this.filters, sort: this.sort.key, dir: this.sort.dir, limit: this.perPage, page: this.currentPage } }); const body = response.data || {}; this.rows = body.data || []; this.total = body.total || 0; this.currentPage = body.current_page || 1; this.lastPage = body.last_page || 1; this.perPage = body.per_page || this.perPage; this.summary = body.summary || { income: 0, expense: 0, opening_balance: 0, opening_balance_date: null, balance: 0, count: 0 }; } catch (error) { this.rows = []; this.total = 0; this.summary = { income: 0, expense: 0, opening_balance: 0, opening_balance_date: null, balance: 0, count: 0 }; this.error = (error.response && error.response.data && error.response.data.message) || '淘系支付宝账单暂时无法读取,请稍后重试。'; } finally { this.loading = false; } },
applyFilters() { if (this.invalidDateRange) return; this.currentPage = 1; this.fetch(); }, resetFilters() { this.filters = { store_name: '', ledger_remark: '', source_channel: '', bill_item: '', business_type: '', ...currentMonthRange(), keyword: '' }; this.applyFilters(); },
openDatePicker(refName) { const picker = this.$refs[refName]; if (picker && picker.fp) picker.fp.open(); },
// 选日期不自动查询:日期是一对(开始/结束),中途触发会先按半截条件查全表,很慢。改完点「查询」再生效。
@ -272,7 +280,7 @@ export default {
<style scoped>
.table-workspace { padding: 1rem; color: #171717; font: 400 0.8125rem/1.5 "JetBrains Mono", "Microsoft YaHei", "PingFang SC", monospace; container-type: inline-size; }
.tw-stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .375rem; margin-bottom: .375rem; }.tw-stat { padding: .375rem .625rem; border: 1px solid #d8d2c7; border-radius: .5rem; background: #fff; }.tw-label,.tw-note,.tw-count { color: #595959; font-size: .625rem; }.tw-value { margin-top: .125rem; font-size: 1rem; font-weight: 700; font-variant-numeric: tabular-nums; }.tw-note { margin-top: .125rem; }.is-income { color: #53793e; }.is-expense { color: #b80000; }
.tw-stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .375rem; margin-bottom: .375rem; }.tw-stats--taobao-alipay { grid-template-columns: repeat(5, minmax(0, 1fr)); }.tw-stat { padding: .375rem .625rem; border: 1px solid #d8d2c7; border-radius: .5rem; background: #fff; }.tw-label,.tw-note,.tw-count { color: #595959; font-size: .625rem; }.tw-value { margin-top: .125rem; font-size: 1rem; font-weight: 700; font-variant-numeric: tabular-nums; }.tw-note { margin-top: .125rem; }.is-income { color: #53793e; }.is-expense { color: #b80000; }
.tw-filter { display: flex; flex-wrap: wrap; gap: .375rem; margin-bottom: .375rem; padding: .375rem .625rem; border: 1px solid #d8d2c7; border-radius: .5rem; background: #fff; }.tw-filter>label { display: flex; flex-direction: column; gap: 0; color: #595959; font-size: .6875rem; font-weight: 500; }.tw-filter input,.tw-filter select,.tw-tools select { height: 1.875rem; min-width: 10rem; padding: 0 .625rem; border: 1px solid #d8d2c7; border-radius: .5rem; background: #fff; color: #171717; font: 400 .8125rem/1 inherit; }.tw-filter input:focus,.tw-filter select:focus,.tw-tools select:focus { border-color: #856632; box-shadow: 0 0 0 3px rgba(133,102,50,.2); outline: none; }.tw-filter .tw-keyword { flex: 1; min-width: 14rem; }.tw-keyword input { width: 100%; }.tw-date span,.tw-actions,.tw-tools,.tw-page-actions { display: flex; align-items: center; gap: .5rem; }.tw-date b { font-weight: 400; }.tw-date input { min-width: 8.5rem; }.tw-actions { align-self: flex-end; margin-left: auto; }
.tw-button,.tw-page-actions button { display: inline-flex; align-items: center; justify-content: center; gap: .375rem; height: 1.875rem; padding: 0 .75rem; border: 1px solid #d8d2c7; border-radius: .5rem; background: #fff; color: #171717; font: 500 .8125rem/1 inherit; cursor: pointer; }.tw-button:hover { border-color: #856632; color: #856632; }.tw-primary { border-color: #856632; background: #856632; color: #fff; }.tw-primary:hover { background: #6a5129; color: #fff; }.tw-button .material-icons-outlined { font-size: 1.125rem; }
/* 导出进度弹窗:和财务规则 › 科目映射表的「操作记录」同一个观感 */
@ -301,7 +309,7 @@ export default {
.tw-panel { overflow: visible; border: 1px solid #d8d2c7; border-radius: .5rem; background: #fff; }.tw-alert { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin: .75rem; padding: .75rem; border: 1px solid #935f07; border-radius: .5rem; background: #f7f0de; color: #935f07; }.tw-alert button { border: 0; background: transparent; color: #856632; font: inherit; font-weight: 600; cursor: pointer; }.tw-toolbar { display: flex; align-items: center; justify-content: space-between; gap: .75rem; min-height: 3.5rem; padding: .625rem .875rem; border-bottom: 1px solid #d8d2c7; }.tw-toolbar strong { margin-right: .625rem; font-size: .875rem; }.tw-tools { flex-wrap: wrap; justify-content: flex-end; }.tw-tools>select { min-width: 13.75rem; }
.tw-scroll { position: relative; max-height: min(62vh, 42rem); overflow: auto; overscroll-behavior: contain; scrollbar-gutter: stable; }.tw-table { width: max-content; min-width: 100%; border-collapse: separate; border-spacing: 0; font-size: .78125rem; }.tw-table th,.tw-table td { min-width: 7.5rem; padding: .5rem .625rem; border-right: 1px solid #eeeae2; border-bottom: 1px solid #eeeae2; white-space: nowrap; }.tw-table thead th { position: sticky; z-index: 4; background: #f7f0de; color: #171717; font-weight: 600; }.tw-groups th { top: 0; height: 2rem; text-align: center; color: #856632; font-size: .75rem; }.tw-columns th { top: 2rem; height: 2.25rem; text-align: left; background: #fbfaf7; box-shadow: inset 0 -1px #d8d2c7; }.tw-columns th.is-sortable { cursor: pointer; }.tw-columns th.is-sortable:hover,.tw-sort { color: #856632; }.tw-table tbody tr:hover { background: #f7f0de; }.tw-table .is-number { text-align: right; font-variant-numeric: tabular-nums; }.tw-table .is-id { color: #595959; }.tw-tag { display: inline-block; padding: .0625rem .375rem; border-radius: .25rem; background: #f7f0de; color: #856632; }.tw-empty { padding: 3rem 1rem!important; text-align: center!important; color: #595959; }
.tw-pager { display: flex; align-items: center; justify-content: space-between; gap: .75rem; min-height: 3.5rem; padding: .625rem .875rem; border-top: 1px solid #d8d2c7; color: #595959; }.tw-page-actions select { height: 1.875rem; border: 1px solid #d8d2c7; border-radius: .375rem; }.tw-page-actions button { width: 1.875rem; height: 1.875rem; padding: 0; }.tw-page-actions button:disabled { cursor: not-allowed; opacity: .45; }
@media(max-width:900px){.table-workspace{padding:.75rem}.tw-stats{grid-template-columns:repeat(2,minmax(0,1fr))}.tw-filter>label,.tw-filter .tw-keyword{width:100%}.tw-filter input,.tw-filter select{width:100%}.tw-date input{min-width:0}.tw-actions{margin-left:0}.tw-toolbar,.tw-pager{align-items:flex-start;flex-direction:column}.tw-toolbar .tw-tools{flex:0 0 auto;height:auto;justify-content:flex-start}.tw-scroll{max-height:58vh;width:100%}}@media(max-width:520px){.tw-stats{grid-template-columns:1fr}.tw-date span{align-items:stretch;flex-direction:column}.tw-tools>select{min-width:0;max-width:100%}.tw-page-actions{flex-wrap:wrap}}
@media(max-width:900px){.table-workspace{padding:.75rem}.tw-stats,.tw-stats--taobao-alipay{grid-template-columns:repeat(2,minmax(0,1fr))}.tw-filter>label,.tw-filter .tw-keyword{width:100%}.tw-filter input,.tw-filter select{width:100%}.tw-date input{min-width:0}.tw-actions{margin-left:0}.tw-toolbar,.tw-pager{align-items:flex-start;flex-direction:column}.tw-toolbar .tw-tools{flex:0 0 auto;height:auto;justify-content:flex-start}.tw-scroll{max-height:58vh;width:100%}}@media(max-width:520px){.tw-stats,.tw-stats--taobao-alipay{grid-template-columns:1fr}.tw-date span{align-items:stretch;flex-direction:column}.tw-tools>select{min-width:0;max-width:100%}.tw-page-actions{flex-wrap:wrap}}
.tw-toolbar { flex-wrap: wrap; }
.tw-tools { flex: 1 1 24rem; min-width: 0; }
/* 筛选栏与淘系微信账户(AccountBills)同一套栅格:宽屏多列、窄屏两列。 */

View File

@ -0,0 +1,169 @@
<?php
namespace Tests\Unit\EcommerceFinance;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\EcommerceFinance\Repositories\TaobaoAlipaySettlementBillRepository;
use PHPUnit\Framework\TestCase;
class TaobaoAlipaySettlementBillRepositoryTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$app = require dirname(__DIR__, 3) . '/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
config([
'database.connections.business_mirror' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
'upstream_mirrors.connection' => 'business_mirror',
]);
DB::purge('business_mirror');
Cache::flush();
$this->createTables();
}
public function test_month_balance_uses_previous_account_balances_plus_month_flow(): void
{
$rows = DB::connection('business_mirror')->table('taobao_alipay_bill_rows');
$rows->insert([
$this->row('A-SEP-1', '天猫-A店', '20880001', '2026-09-30 23:58:00', '0', '0', '100', 10),
$this->row('A-SEP-2', '天猫-A店', '20880001', '2026-09-30 23:59:59', '0', '0', '150', 20),
// 同一时间的两笔按源表行号取后一笔。
$this->row('A-SEP-3', '天猫-A店', '20880001', '2026-09-30 23:59:59', '0', '0', '180', 30),
$this->row('B-SEP', '淘宝-B店', '20880002', '2026-09-29 18:00:00', '0', '0', '50', 40),
$this->row('A-OCT', '天猫-A店', '20880001', '2026-10-15 10:00:00', '40', '10', '210', 50),
$this->row('B-OCT', '淘宝-B店', '20880002', '2026-10-20 10:00:00', '20', '5', '65', 60),
]);
$result = app(TaobaoAlipaySettlementBillRepository::class)->paginate(
['start' => '2026-10-01', 'end' => '2026-10-31'],
'occurred_at',
'desc',
1,
20,
);
$this->assertSame(2, $result['summary']['count']);
$this->assertSame(60.0, $result['summary']['income']);
$this->assertSame(15.0, $result['summary']['expense']);
$this->assertSame(230.0, $result['summary']['opening_balance']);
$this->assertSame('2026-09-30', $result['summary']['opening_balance_date']);
$this->assertSame(275.0, $result['summary']['balance']);
}
public function test_month_balance_respects_store_filter(): void
{
$rows = DB::connection('business_mirror')->table('taobao_alipay_bill_rows');
$rows->insert([
$this->row('A-SEP', '天猫-A店', '20880001', '2026-09-30 23:59:59', '0', '0', '150', 10),
$this->row('B-SEP', '淘宝-B店', '20880002', '2026-09-30 23:59:59', '0', '0', '50', 20),
$this->row('A-OCT', '天猫-A店', '20880001', '2026-10-15 10:00:00', '40', '10', '180', 30),
$this->row('B-OCT', '淘宝-B店', '20880002', '2026-10-20 10:00:00', '20', '5', '65', 40),
]);
$result = app(TaobaoAlipaySettlementBillRepository::class)->paginate(
['store_name' => '天猫-A店', 'start' => '2026-10-01', 'end' => '2026-10-31'],
'occurred_at',
'desc',
1,
20,
);
$this->assertSame(1, $result['summary']['count']);
$this->assertSame(40.0, $result['summary']['income']);
$this->assertSame(10.0, $result['summary']['expense']);
$this->assertSame(150.0, $result['summary']['opening_balance']);
$this->assertSame(180.0, $result['summary']['balance']);
}
private function createTables(): void
{
$schema = Schema::connection('business_mirror');
$schema->create('taobao_alipay_bill_imports', function (Blueprint $table): void {
$table->uuid('generation');
$table->string('status');
$table->timestamp('completed_at')->nullable();
});
$schema->create('taobao_alipay_bill_rows', function (Blueprint $table): void {
$table->string('record_code')->primary();
$table->string('store_name')->nullable();
$table->string('custom_remark')->nullable();
$table->string('bill_item')->nullable();
$table->string('alipay_account')->nullable();
$table->string('account_order_no')->nullable();
$table->string('business_serial_no')->nullable();
$table->string('merchant_order_no')->nullable();
$table->string('business_base_order_no')->nullable();
$table->string('goods_name')->nullable();
$table->string('counterparty_account')->nullable();
$table->dateTime('occurred_at');
$table->decimal('income_amount', 20, 6)->default(0);
$table->decimal('expense_amount', 20, 6)->default(0);
$table->decimal('balance_amount', 20, 6)->default(0);
$table->string('business_type')->nullable();
$table->string('remark')->nullable();
$table->string('online_order_no')->nullable();
$table->string('online_real_store')->nullable();
$table->string('business_description')->nullable();
$table->string('business_bill_source')->nullable();
$table->string('business_order_no')->nullable();
$table->dateTime('source_created_at')->nullable();
$table->dateTime('source_updated_at')->nullable();
$table->string('source_modified_by')->nullable();
$table->unsignedBigInteger('source_row_number');
});
DB::connection('business_mirror')->table('taobao_alipay_bill_imports')->insert([
'generation' => 'test-generation',
'status' => 'succeeded',
'completed_at' => '2026-11-01 09:00:00',
]);
}
private function row(
string $recordCode,
string $storeName,
string $account,
string $occurredAt,
string $income,
string $expense,
string $balance,
int $sourceRowNumber,
): array {
return [
'record_code' => $recordCode,
'store_name' => $storeName,
'custom_remark' => null,
'bill_item' => '交易收款',
'alipay_account' => $account,
'account_order_no' => null,
'business_serial_no' => null,
'merchant_order_no' => null,
'business_base_order_no' => null,
'goods_name' => null,
'counterparty_account' => null,
'occurred_at' => $occurredAt,
'income_amount' => $income,
'expense_amount' => $expense,
'balance_amount' => $balance,
'business_type' => '交易付款',
'remark' => null,
'online_order_no' => null,
'online_real_store' => null,
'business_description' => null,
'business_bill_source' => '支付宝',
'business_order_no' => null,
'source_created_at' => null,
'source_updated_at' => null,
'source_modified_by' => null,
'source_row_number' => $sourceRowNumber,
];
}
}

View File

@ -22,6 +22,14 @@ class TaobaoAlipayBillsTableTest extends TestCase
$this->assertStringContainsString('@click="exportRows"', $source);
$this->assertStringContainsString('商品名称', $source);
$this->assertStringContainsString('余额', $source);
$this->assertStringContainsString('tw-stats--taobao-alipay', $source);
$this->assertStringContainsString("label: '期初余额'", $source);
$this->assertStringContainsString("label: '余额'", $source);
$this->assertStringContainsString("note: '期初余额 + 收入合计 - 支出合计'", $source);
$this->assertLessThan(
strpos($source, "label: '收入合计'"),
strpos($source, "label: '期初余额'"),
);
$this->assertStringNotContainsString('<th>财务备注</th>', $source);
$this->assertStringNotContainsString('<th>订单号</th>', $source);
$this->assertStringNotContainsString('<th class="tb-num">金额</th>', $source);