CaiWuXiTong/modules/EcommerceFinance/Services/StoreReceiptDefaultCompensationSnapshotBuilder.php
yueyang f7197b4976 财务分析新增开票/不开票费用,并补齐违约赔付预览 (#480)
## 任务占用与边界

- 任务名称:财务分析开票费用与业绩明细违约赔付预览
- 实际开发者:yueyang
- 任务来源/团队分工:负责人直接指定
- 目标结果:财务经营分析表按科目映射表拆分开票费用/不开票费用;业绩明细增加违约赔付(冲减收入)列并按指定顺序展示。
- 预计修改路径:业务 Pages Controller、财务分析视图、账单明细数据服务、业绩明细配置与相关测试。
- 将写入的公共契约:财务分析费用聚合、默认补偿预览命令、业绩明细列配置。
- 仅复用、明确不修改的公共契约:store_receipt_order_match_rows、ec_account_mapping_rules。
- 数据源、表/视图和字段:店铺收款账单镜像、store_receipt_order_match_rows、store_receipt_product_cost_rows、ec_account_mapping_rules。
- 明确不修改的范围:账单抓取与上游同步链路。

## 开工碰撞检查

- [x] Codex 已执行 scripts/codex-task-preflight.ps1
- [x] 已检查最新 main 和远程任务分支
- [x] 已搜索主线既有页面、接口和数据服务
- [x] 已将写入和复用契约分开
- [x] 预检列出的残留分支均无开放 PR;当前 2 个开放 PR 与本任务无关,确认无活跃写入冲突

## 实际改动

- 财务经营分析表增加“开票费用”“不开票费用”及合计;财务类型=费用,票据口径 0.06/0.09 归入开票,不开票归入不开票,金额按支出减收入净额。
- 费用聚合并入现有月度缓存,缓存键包含科目映射哈希,映射变化后自动失效。
- 业绩明细增加违约赔付(冲减收入)列,放在冲减金额后。
- CSV 导出列同步更新。

## 验证证据

- 正常流程:2026-07/08/09 实库数据计算成功;2026-09 开票费用 3,896,538.65,不开票费用 81,377.62。
- 实际页面:2026-09 财务经营分析表 438 行,两列与合计正常展示。
- 性能:单月费用聚合 7.0–13.6 秒,PHP 峰值约 65.5 MB;页面结果缓存 5 分钟。
- 自动测试:NavigationTest 目标 2 条通过(35 assertions),StoreReceiptMappingPreviewOverrideTest 2 条通过(5 assertions)。

## 风险检查

- [x] 不涉及数据库迁移或生产数据写入
- [x] 涉及金额口径,已用实库页面和固定总额核对
- [x] 未提交 .env、密码、Token、真实数据或构建产物
- [x] 前端符合 DESIGN.md 黑金表格样式

## 合并前复查

- [x] 已基于最新 main 53bd1dc4 变基
- [x] 完整 diff 自审,无冲突标记
- [x] 目标测试通过
- [x] 负责人已授权 Squash merge 和发布

Reviewed-on: #480
2026-10-23 19:05:35 +08:00

340 lines
14 KiB
PHP

<?php
namespace Modules\EcommerceFinance\Services;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use PhpOffice\PhpSpreadsheet\IOFactory;
use RuntimeException;
/**
* 本地预览用:直接从最新科目映射表和账单镜像生成违约赔付金额快照。
*
* 不写共享数据库,只输出 storage/app/local 下的 JSON。正式接入系统前用于核对
* 「种菜金额」后面的「违约赔付(冲减收入)」列。
*/
class StoreReceiptDefaultCompensationSnapshotBuilder
{
public const REMARK = '违约赔付(冲减收入)';
private const SOURCES = [
'taobao-alipay' => [
'label' => '淘系支付宝', 'table' => 'taobao_alipay_bill_rows',
'order' => 'online_order_no', 'time' => 'occurred_at', 'amount' => 'income_amount',
'real_store' => 'online_real_store',
],
'taobao-wechat' => [
'label' => '淘系微信', 'table' => 'taobao_wechat_bill_rows',
'order' => 'online_order_no', 'time' => 'occurred_at', 'amount' => 'income_amount',
],
'pinduoduo' => [
'label' => '拼多多', 'table' => 'pdd_settlement_bill_rows',
'order' => 'online_order_no', 'time' => 'occurred_at', 'amount' => 'income_amount',
],
'dewu' => [
'label' => '得物', 'table' => 'dewu_settlement_bill_rows',
'order' => 'online_order_no', 'time' => 'bill_date', 'amount' => 'income_amount',
'real_store' => 'online_real_store',
],
'jd-transactions' => [
'label' => '京东账单', 'table' => 'jd_daily_bill_rows',
'order' => 'order_no', 'time' => 'occurred_at', 'amount' => 'income',
'store_exclude' => '京喜', 'periods' => 'jd_daily_bill_periods',
],
'jingxi' => [
'label' => '京喜账单', 'table' => 'jd_daily_bill_rows',
'order' => 'order_no', 'time' => 'occurred_at', 'amount' => 'income',
'store_include' => '京喜', 'periods' => 'jd_daily_bill_periods',
],
'douyin' => [
'label' => '抖音', 'table' => 'douyin_authoritative_bills',
'order' => 'order_no', 'time' => 'occurred_at', 'amount' => 'income',
'periods' => 'douyin_authoritative_periods',
],
];
/** 页签内多平台按店铺名前缀拆分;单平台页签无需拆分。 */
private const TAB_PLATFORM_GROUPS = [
'taobao-alipay' => [
['platform' => '淘宝', 'prefixes' => ['淘宝'], 'include_default' => true],
['platform' => '天猫', 'prefixes' => ['天猫']],
['platform' => '阿里巴巴', 'prefixes' => ['阿里巴巴', '1688', '新佰广']],
],
'taobao-wechat' => [
['platform' => '淘宝', 'prefixes' => ['淘宝'], 'include_default' => true],
['platform' => '天猫', 'prefixes' => ['天猫']],
],
'pinduoduo' => [['platform' => '拼多多']],
'dewu' => [['platform' => '得物']],
'jd-transactions' => [['platform' => '京东']],
'jingxi' => [['platform' => '京喜']],
'douyin' => [['platform' => '抖音']],
];
private const STORE_PREFIXES = [
'唯品会JX', '唯品会MP', '阿里巴巴', '1688', '新佰广',
'拼多多', '天猫', '淘宝', '抖音', '得物', '京喜', '京东',
];
/**
* @return array{
* month:string,remark:string,generated_at:string,mapping_sha256:string,mapping_rows:int,
* source_stats:array<string,array{rows:int,amount:float,groups:int}>,
* amounts:array<string,float>,total:float
* }
*/
public function build(string $month, string $mappingPath): array
{
if (preg_match('/^\d{4}-\d{2}$/', $month) !== 1) {
throw new RuntimeException('月份格式必须是 YYYY-MM。');
}
if (! is_file($mappingPath)) {
throw new RuntimeException('科目映射表不存在:' . $mappingPath);
}
$start = CarbonImmutable::parse($month . '-01')->startOfMonth();
$end = $start->addMonth();
$reportMonth = $start->toDateString();
$itemsByPlatform = $this->readMappingItems($mappingPath);
$connection = (string) config('upstream_mirrors.connection', 'business_mirror');
$amounts = [];
$sourceStats = [];
foreach (self::SOURCES as $tab => $source) {
$groups = $this->groupsForSource($tab, $itemsByPlatform);
if ($groups === []) {
continue;
}
$table = (string) $source['table'];
$orderColumn = (string) $source['order'];
$timeColumn = (string) $source['time'];
$amountColumn = (string) $source['amount'];
$realStoreColumn = (string) ($source['real_store'] ?? '');
$expenseColumn = '';
if (Schema::connection($connection)->hasColumn($table, 'expense_amount')) {
$expenseColumn = 'expense_amount';
} elseif (Schema::connection($connection)->hasColumn($table, 'expense')) {
$expenseColumn = 'expense';
}
$select = array_values(array_unique(array_filter([
'store_name', 'bill_item', $orderColumn, $timeColumn, $amountColumn,
$expenseColumn, $realStoreColumn,
])));
$query = DB::connection($connection)->table($table)
->select($select)
->where($timeColumn, '>=', $start->toDateTimeString())
->where($timeColumn, '<', $end->toDateTimeString());
if (isset($source['store_include'])) {
$query->where('store_name', 'like', '%' . $source['store_include'] . '%');
}
if (isset($source['store_exclude'])) {
$query->where('store_name', 'not like', '%' . $source['store_exclude'] . '%');
}
$periodsTable = (string) ($source['periods'] ?? '');
if ($periodsTable !== '') {
$generations = DB::connection($connection)->table($periodsTable)
->where('period_start', '<', $end->toDateString())
->where('period_end', '>', $start->toDateString())
->pluck('active_generation')
->filter()
->unique()
->values()
->all();
if ($generations === []) {
continue;
}
$query->whereIn('generation', $generations);
}
$query->where(function ($outer) use ($groups): void {
foreach ($groups as $group) {
$outer->orWhere(function ($sub) use ($group): void {
if ($group['prefixes'] !== []) {
$sub->where(function ($scope) use ($group): void {
$scope->where(function ($own) use ($group): void {
foreach ($group['prefixes'] as $prefix) {
$own->orWhere('store_name', 'like', $prefix . '%');
}
});
if ($group['include_default']) {
$scope->orWhere(function ($other): void {
foreach (self::STORE_PREFIXES as $prefix) {
$other->where('store_name', 'not like', $prefix . '%');
}
});
}
});
}
$sub->whereIn('bill_item', $group['items']);
});
}
});
$rows = 0;
$amountTotal = 0.0;
foreach ($query->orderBy($timeColumn)->cursor() as $row) {
$rows++;
$store = $realStoreColumn !== ''
? trim((string) ($row->{$realStoreColumn} ?? ''))
: '';
if ($store === '') {
$store = trim((string) ($row->store_name ?? ''));
}
$orderNo = trim((string) ($row->{$orderColumn} ?? ''));
$amount = (float) ($row->{$amountColumn} ?? 0)
- ($expenseColumn !== '' ? (float) ($row->{$expenseColumn} ?? 0) : 0.0);
$key = $this->rowKey(
$reportMonth,
(string) $source['label'],
$store,
$orderNo,
(string) ($row->bill_item ?? ''),
);
$amounts[$key] = round(($amounts[$key] ?? 0.0) + $amount, 2);
$amountTotal += $amount;
}
$sourceStats[(string) $source['label']] = [
'rows' => $rows,
'amount' => round($amountTotal, 2),
'groups' => count($groups),
];
}
ksort($amounts);
return [
'month' => $month,
'remark' => self::REMARK,
'generated_at' => now()->toIso8601String(),
'mapping_sha256' => hash_file('sha256', $mappingPath),
'mapping_rows' => array_sum(array_map('count', $itemsByPlatform)),
'source_stats' => $sourceStats,
'amounts' => $amounts,
'total' => round(array_sum($amounts), 2),
];
}
/** @return array<string,list<string>> */
private function readMappingItems(string $mappingPath): array
{
$sheet = IOFactory::load($mappingPath)->getActiveSheet();
$rows = $sheet->toArray(null, true, true, false);
if ($rows === []) {
throw new RuntimeException('科目映射表没有数据。');
}
$headers = array_map(static fn ($value): string => trim((string) $value), array_shift($rows));
$platformIndex = array_search('平台', $headers, true);
$itemIndex = array_search('账单项目', $headers, true);
$remarkIndex = array_search('财务备注', $headers, true);
if ($platformIndex === false || $itemIndex === false || $remarkIndex === false) {
throw new RuntimeException('科目映射表缺少「平台」「账单项目」或「财务备注」列。');
}
$items = [];
foreach ($rows as $row) {
if (trim((string) ($row[$remarkIndex] ?? '')) !== self::REMARK) {
continue;
}
$platform = trim((string) ($row[$platformIndex] ?? ''));
$item = trim((string) ($row[$itemIndex] ?? ''));
if ($platform !== '' && $item !== '') {
$items[$platform][$item] = true;
}
}
$result = [];
foreach ($items as $platform => $values) {
$result[$platform] = array_keys($values);
sort($result[$platform], SORT_NATURAL | SORT_FLAG_CASE);
}
return $result;
}
/**
* @param array<string,list<string>> $itemsByPlatform
* @return list<array{prefixes:list<string>,include_default:bool,items:list<string>}>
*/
private function groupsForSource(string $tab, array $itemsByPlatform): array
{
$groups = [];
foreach (self::TAB_PLATFORM_GROUPS[$tab] ?? [] as $definition) {
$platform = (string) $definition['platform'];
$items = $itemsByPlatform[$platform] ?? [];
if ($items === []) {
continue;
}
if ($tab === 'taobao-wechat') {
$items = $this->withWxVariants($items);
}
$groups[] = [
'prefixes' => array_values((array) ($definition['prefixes'] ?? [])),
'include_default' => (bool) ($definition['include_default'] ?? false),
'items' => $items,
];
}
return $groups;
}
/** @param list<string> $items @return list<string> */
private function withWxVariants(array $items): array
{
$variants = [];
foreach ($items as $item) {
$variants[$item] = true;
if (stripos($item, 'wx') === 0) {
$without = ltrim(mb_substr($item, 2));
if ($without !== '') {
$variants[$without] = true;
}
} else {
$variants['wx' . $item] = true;
}
}
$list = array_keys($variants);
sort($list, SORT_NATURAL | SORT_FLAG_CASE);
return $list;
}
private function rowKey(string $month, string $platform, string $storeName, string $orderNo, string $billItem): string
{
$shopKey = $this->normalizeShop($storeName);
$identity = $this->orderIdentity($orderNo);
if ($identity === '') {
return md5($month . '|' . $platform . '|' . $shopKey . '|promo|' . $billItem);
}
return md5($month . '|' . $platform . '|' . $shopKey . '|' . $identity);
}
private function normalizeShop(string $value): string
{
$value = str_replace(["\u{3000}", '_'], [' ', '-'], trim($value));
$value = preg_replace('/\s+/u', '', $value) ?? '';
return strtoupper($value);
}
private function orderIdentity(string $value): string
{
$value = str_replace("\u{3000}", ' ', trim($value));
if ($value === '') {
return '';
}
$value = preg_replace('/[\x{FF0D}\x{2014}\x{2013}\x{2010}]/u', '-', $value) ?? $value;
$value = preg_replace('/\s+/u', '', $value) ?? '';
return strtoupper($value);
}
}