> 开工时标题使用 `WIP: <任务名称>` 并先填写“任务占用与边界”。Codex 完成独立合并前自审后补齐其余内容并移除 `WIP:`。 ## 任务占用与边界 - 任务名称: - 实际开发者: - 任务来源/团队分工: - 目标结果: - 预计修改路径: - 将写入的公共契约(路由、Controller 方法、Repository、表/视图、`source_key`、`scope`、权限名、菜单键或共享组件): - 仅复用、明确不修改的公共契约: - 数据源、表/视图和字段: - 上游 `source_key` / `scope`: - 权限名、菜单键或共享组件: - 明确不修改的范围: ## 开工碰撞检查 - [ ] Codex 已执行 `scripts/codex-task-preflight.ps1` - [ ] 已检查最新 `main` 和所有远程任务分支 - [ ] 已搜索主线既有页面、接口、Repository 和数据查询 - [ ] 已把“将写入”和“仅复用、不修改”的公共契约分开;仅复用项不作为单独阻断条件 - [ ] 未发现实际写入重叠;或已在下方写明相关分支、具体重叠项和协调结论 相关分支、重叠项与协调结论: ## 实际改动 - 完成内容: - Codex 实际修改的文件: - 新增或变更的公共契约: - 与原计划的差异: ## 验证证据 - 正常流程: - 空数据/错误输入/无权限场景: - 受影响旧功能: - 自动测试及结果: - 实际页面、接口或只读业务查询结果: ## 风险检查 - [ ] 不涉及金额、权限、审批、数据库迁移、生产配置或数据修复 - [ ] 如涉及上述高风险内容,已在下方写明影响、回滚和所需授权 - [ ] 未提交 `.env`、密码、Token、私钥、真实财务数据、生产日志或构建产物 - [ ] 前端改动符合 `DESIGN.md`;无前端改动则写“不适用” 高风险影响、回滚和授权状态: ## 合并前复查 - [ ] 已重新拉取远程引用并用实际修改范围再次运行碰撞检查 - [ ] PR 不再是 `WIP:`,所有测试结果已经补齐 - [ ] Codex 已重新读取原始需求和验收标准,并逐文件审核 `origin/main...任务分支` 的完整 diff - [ ] Codex 自审确认无越界修改、重复实现、敏感内容、未处理冲突和未验证结果 - [ ] 合并顺序不会覆盖其他未合并任务 - [ ] 已授权当前任务的 Codex 执行 Squash merge、验证 `main` 并清理任务分支 - [ ] 如涉及生产迁移、数据修复、删除、覆盖配置或部署,已另行取得对应高风险授权 Co-authored-by: qiuxiang <qiuxiang@local> Reviewed-on: #1703
193 lines
7.5 KiB
PHP
193 lines
7.5 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use Illuminate\Console\Command;
|
||
use Illuminate\Support\Carbon;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Modules\EcommerceFinance\Repositories\JdDailyBillRepository;
|
||
use Modules\EcommerceFinance\Repositories\StoreReceivingAlipayBillMirrorRepository;
|
||
use Modules\EcommerceFinance\Repositories\StoreReceivingDewuAlipayBillMirrorRepository;
|
||
use Modules\EcommerceFinance\Repositories\StoreReceivingDewuBillMirrorRepository;
|
||
use Modules\EcommerceFinance\Repositories\StoreReceivingDouyinBillMirrorRepository;
|
||
use Modules\EcommerceFinance\Repositories\StoreReceivingJdWalletBillRepository;
|
||
use Modules\EcommerceFinance\Repositories\StoreReceivingPinduoduoBillMirrorRepository;
|
||
use Modules\EcommerceFinance\Repositories\TaobaoWechatSettlementBillRepository;
|
||
use Throwable;
|
||
|
||
class SummarizeBillRange extends Command
|
||
{
|
||
private const ALLOWED_TABS = [
|
||
'taobao-alipay',
|
||
'taobao-wechat',
|
||
'pinduoduo',
|
||
'jd-wallet',
|
||
'jd-transactions',
|
||
'jingxi',
|
||
'douyin',
|
||
'dewu',
|
||
'dewu-alipay',
|
||
'xiaohongshu',
|
||
];
|
||
|
||
protected $signature = 'bill-sync:summarize-range
|
||
{--tabs= : 逗号分隔的账单页签}
|
||
{--from= : 开始日期 YYYY-MM-DD}
|
||
{--to= : 截止日期 YYYY-MM-DD}
|
||
{--company-id=1 : 公司 ID}
|
||
{--json : 输出 JSON}';
|
||
|
||
protected $description = '按指定日期范围和页签汇总财务系统账单笔数、收入与支出';
|
||
|
||
public function handle(): int
|
||
{
|
||
$tabs = array_values(array_unique(array_filter(array_map(
|
||
static fn (string $tab): string => trim($tab),
|
||
explode(',', (string) $this->option('tabs')),
|
||
))));
|
||
$unknownTabs = array_values(array_diff($tabs, self::ALLOWED_TABS));
|
||
if ($tabs === [] || $unknownTabs !== []) {
|
||
$this->error('账单页签无效:' . ($unknownTabs ? implode(',', $unknownTabs) : '不能为空'));
|
||
|
||
return self::FAILURE;
|
||
}
|
||
|
||
$from = $this->date((string) $this->option('from'));
|
||
$to = $this->date((string) $this->option('to'));
|
||
if ($from === null || $to === null || $from->gt($to)) {
|
||
$this->error('日期范围无效,应为 from <= to,格式 YYYY-MM-DD。');
|
||
|
||
return self::FAILURE;
|
||
}
|
||
|
||
$companyId = max(1, (int) $this->option('company-id'));
|
||
$fromText = $from->toDateString();
|
||
$toText = $to->toDateString();
|
||
$platforms = [];
|
||
$rowCount = 0;
|
||
$incomeCents = 0;
|
||
$expenseCents = 0;
|
||
|
||
foreach ($tabs as $tab) {
|
||
try {
|
||
$summary = $this->summary($tab, $fromText, $toText, $companyId);
|
||
} catch (Throwable $exception) {
|
||
report($exception);
|
||
$this->error("账单页签 {$tab} 汇总失败。");
|
||
|
||
return self::FAILURE;
|
||
}
|
||
|
||
$platforms[$tab] = $summary;
|
||
$rowCount += $summary['row_count'];
|
||
$incomeCents += $summary['income_cents'];
|
||
$expenseCents += $summary['expense_cents'];
|
||
}
|
||
|
||
$result = [
|
||
'from' => $fromText,
|
||
'to' => $toText,
|
||
'company_id' => $companyId,
|
||
'row_count' => $rowCount,
|
||
'income_cents' => $incomeCents,
|
||
'expense_cents' => $expenseCents,
|
||
'platforms' => $platforms,
|
||
];
|
||
|
||
if ((bool) $this->option('json')) {
|
||
$this->line(json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR));
|
||
} else {
|
||
$this->table(
|
||
['页签', '笔数', '收入', '支出'],
|
||
array_map(
|
||
static fn (string $tab, array $summary): array => [
|
||
$tab,
|
||
number_format($summary['row_count']),
|
||
number_format($summary['income_cents'] / 100, 2),
|
||
number_format($summary['expense_cents'] / 100, 2),
|
||
],
|
||
array_keys($platforms),
|
||
array_values($platforms),
|
||
),
|
||
);
|
||
$this->line(sprintf(
|
||
'合计:%d 笔,收入 %.2f,支出 %.2f',
|
||
$rowCount,
|
||
$incomeCents / 100,
|
||
$expenseCents / 100,
|
||
));
|
||
}
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
/** @return array{row_count:int,income_cents:int,expense_cents:int} */
|
||
private function summary(string $tab, string $from, string $to, int $companyId): array
|
||
{
|
||
if ($tab === 'xiaohongshu') {
|
||
$summary = DB::table('ec_account_bills')
|
||
->where('company_id', $companyId)
|
||
->where('bill_type', 'xiaohongshu')
|
||
->where('occurred_at', '>=', $from . ' 00:00:00')
|
||
->where('occurred_at', '<', Carbon::parse($to)->addDay()->toDateString() . ' 00:00:00')
|
||
->selectRaw(
|
||
'COUNT(*) AS row_count, COALESCE(SUM(income), 0) AS income, COALESCE(SUM(expense), 0) AS expense'
|
||
)
|
||
->first();
|
||
|
||
return $this->normalize($summary->row_count, $summary->income, $summary->expense);
|
||
}
|
||
|
||
$filters = ['start' => $from, 'end' => $to];
|
||
$result = match ($tab) {
|
||
'taobao-alipay' => app(StoreReceivingAlipayBillMirrorRepository::class)
|
||
->paginate($filters, 'occurred_at', 'desc', 1, 1),
|
||
'taobao-wechat' => app(TaobaoWechatSettlementBillRepository::class)
|
||
->paginateAccountBills($filters, 'occurred_at', 'desc', 1, 1),
|
||
'pinduoduo' => app(StoreReceivingPinduoduoBillMirrorRepository::class)
|
||
->paginate($filters, 'occurred_at', 'desc', 1, 1),
|
||
'jd-wallet' => app(StoreReceivingJdWalletBillRepository::class)
|
||
->paginate($filters, 'occurred_at', 'desc', 1, 1),
|
||
'jd-transactions' => app(JdDailyBillRepository::class)
|
||
->forPlatform('jd')
|
||
->paginate($filters, 'occurred_at', 'desc', 1, 1),
|
||
'jingxi' => app(JdDailyBillRepository::class)
|
||
->forPlatform('jingxi')
|
||
->paginate($filters, 'occurred_at', 'desc', 1, 1),
|
||
'douyin' => app(StoreReceivingDouyinBillMirrorRepository::class)
|
||
->paginate($filters, 'occurred_at', 'desc', 1, 1),
|
||
'dewu' => app(StoreReceivingDewuBillMirrorRepository::class)
|
||
->paginate($filters, 'occurred_at', 'desc', 1, 1),
|
||
'dewu-alipay' => app(StoreReceivingDewuAlipayBillMirrorRepository::class)
|
||
->paginate($filters, 'occurred_at', 'desc', 1, 1),
|
||
};
|
||
|
||
return $this->normalize(
|
||
$result['total'] ?? ($result['summary']['count'] ?? 0),
|
||
$result['summary']['income'] ?? 0,
|
||
$result['summary']['expense'] ?? 0,
|
||
);
|
||
}
|
||
|
||
/** @return array{row_count:int,income_cents:int,expense_cents:int} */
|
||
private function normalize(mixed $rows, mixed $income, mixed $expense): array
|
||
{
|
||
return [
|
||
'row_count' => (int) $rows,
|
||
'income_cents' => (int) round((float) $income * 100),
|
||
'expense_cents' => (int) round((float) $expense * 100),
|
||
];
|
||
}
|
||
|
||
private function date(string $value): ?Carbon
|
||
{
|
||
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value) !== 1) {
|
||
return null;
|
||
}
|
||
|
||
$date = Carbon::createFromFormat('!Y-m-d', $value, 'Asia/Shanghai');
|
||
|
||
return $date !== false && $date->format('Y-m-d') === $value ? $date : null;
|
||
}
|
||
}
|