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;
|
||
}
|
||
}
|