205 lines
7.9 KiB
PHP
205 lines
7.9 KiB
PHP
<?php
|
||
|
||
namespace Modules\EcommerceFinance\View;
|
||
|
||
use Illuminate\Pagination\LengthAwarePaginator;
|
||
use Illuminate\Support\Carbon;
|
||
use Modules\EcommerceFinance\Repositories\PromotionExpenseMirrorRepository;
|
||
use Throwable;
|
||
|
||
/** 财务经营报表 › 推广费用。 */
|
||
class PromotionExpensePageData
|
||
{
|
||
public const RANGE_OPTIONS = [
|
||
'6' => '近 6 个月',
|
||
'12' => '近 12 个月',
|
||
'ytd' => '本年至今',
|
||
];
|
||
|
||
public function __construct(private readonly PromotionExpenseMirrorRepository $repository)
|
||
{
|
||
}
|
||
|
||
public function page(): array
|
||
{
|
||
$range = (string) request()->query('range', '12');
|
||
if (! array_key_exists($range, self::RANGE_OPTIONS)) {
|
||
$range = '12';
|
||
}
|
||
[$fromMonth, $toMonth] = $this->monthRange($range);
|
||
$platform = trim((string) request()->query('platform', ''));
|
||
$store = trim((string) request()->query('store_name', ''));
|
||
|
||
try {
|
||
$status = $this->repository->syncStatus();
|
||
$platforms = $this->repository->platforms();
|
||
$months = $this->repository->monthOptions($fromMonth, $toMonth);
|
||
$requestedMonth = trim((string) request()->query('month', ''));
|
||
$month = preg_match('/^\d{4}-\d{2}$/', $requestedMonth) === 1
|
||
? $requestedMonth
|
||
: ($months[0] ?? $toMonth);
|
||
$periods = $this->filledPeriods(
|
||
$this->repository->periods($fromMonth, $toMonth, $platform, $store),
|
||
$fromMonth,
|
||
$toMonth,
|
||
);
|
||
$summary = $this->summary($periods, $month);
|
||
$details = $this->repository->details(
|
||
$month,
|
||
$platform,
|
||
$store,
|
||
max(1, (int) request()->query('page', 1)),
|
||
50,
|
||
);
|
||
$rows = new LengthAwarePaginator(
|
||
$details['rows'],
|
||
$details['total'],
|
||
$details['per_page'],
|
||
$details['current_page'],
|
||
[
|
||
'path' => request()->url(),
|
||
'query' => request()->query(),
|
||
],
|
||
);
|
||
$stores = $this->repository->stores($month, $platform);
|
||
|
||
return [
|
||
'view' => 'business.pages.financial-operations.promotion-expenses',
|
||
'promotionExpenseRange' => $range,
|
||
'promotionExpenseRangeOptions' => self::RANGE_OPTIONS,
|
||
'promotionExpenseFromMonth' => $fromMonth,
|
||
'promotionExpenseToMonth' => $toMonth,
|
||
'promotionExpenseMonth' => $month,
|
||
'promotionExpenseMonths' => $months,
|
||
'promotionExpensePlatform' => $platform,
|
||
'promotionExpensePlatforms' => $platforms,
|
||
'promotionExpenseStore' => $store,
|
||
'promotionExpenseStores' => $stores,
|
||
'promotionExpensePeriods' => $periods,
|
||
'promotionExpenseSummary' => $summary,
|
||
'promotionExpenseTotals' => $details['totals'],
|
||
'promotionExpenseRows' => $rows,
|
||
'promotionExpenseSync' => $status,
|
||
'promotionExpenseError' => $status['code'] === 'unavailable'
|
||
? '推广费用共享镜像尚未初始化,请完成数据库升级并等待自动同步。'
|
||
: null,
|
||
];
|
||
} catch (Throwable $exception) {
|
||
report($exception);
|
||
$emptyRows = new LengthAwarePaginator([], 0, 50, 1, [
|
||
'path' => request()->url(),
|
||
'query' => request()->query(),
|
||
]);
|
||
|
||
return [
|
||
'view' => 'business.pages.financial-operations.promotion-expenses',
|
||
'promotionExpenseRange' => $range,
|
||
'promotionExpenseRangeOptions' => self::RANGE_OPTIONS,
|
||
'promotionExpenseFromMonth' => $fromMonth,
|
||
'promotionExpenseToMonth' => $toMonth,
|
||
'promotionExpenseMonth' => $toMonth,
|
||
'promotionExpenseMonths' => [],
|
||
'promotionExpensePlatform' => $platform,
|
||
'promotionExpensePlatforms' => [],
|
||
'promotionExpenseStore' => $store,
|
||
'promotionExpenseStores' => [],
|
||
'promotionExpensePeriods' => [],
|
||
'promotionExpenseSummary' => $this->emptySummary(),
|
||
'promotionExpenseTotals' => [],
|
||
'promotionExpenseRows' => $emptyRows,
|
||
'promotionExpenseSync' => [],
|
||
'promotionExpenseError' => '推广费用数据暂时无法读取,请稍后重试。',
|
||
];
|
||
}
|
||
}
|
||
|
||
public function exportRows(string $month, string $platform = '', string $store = ''): array
|
||
{
|
||
return $this->repository->exportDetails($month, $platform, $store);
|
||
}
|
||
|
||
/** @return array{0:string,1:string} */
|
||
private function monthRange(string $range): array
|
||
{
|
||
$current = Carbon::now('Asia/Shanghai')->startOfMonth();
|
||
$from = match ($range) {
|
||
'6' => $current->copy()->subMonthsNoOverflow(5),
|
||
'ytd' => $current->copy()->startOfYear(),
|
||
default => $current->copy()->subMonthsNoOverflow(11),
|
||
};
|
||
|
||
return [$from->format('Y-m'), $current->format('Y-m')];
|
||
}
|
||
|
||
/**
|
||
* @param list<array{period:string,source_rows:int,push_fee_total:float,marketing_fee_total:float,total_fee:float}> $rows
|
||
* @return list<array{period:string,source_rows:int,push_fee_total:float,marketing_fee_total:float,total_fee:float}>
|
||
*/
|
||
private function filledPeriods(array $rows, string $fromMonth, string $toMonth): array
|
||
{
|
||
$byMonth = array_column($rows, null, 'period');
|
||
$periods = [];
|
||
$cursor = Carbon::createFromFormat('Y-m-d', $fromMonth . '-01', 'Asia/Shanghai')->startOfMonth();
|
||
$end = Carbon::createFromFormat('Y-m-d', $toMonth . '-01', 'Asia/Shanghai')->startOfMonth();
|
||
while ($cursor->lte($end)) {
|
||
$period = $cursor->format('Y-m');
|
||
$periods[] = $byMonth[$period] ?? [
|
||
'period' => $period,
|
||
'source_rows' => 0,
|
||
'push_fee_total' => 0.0,
|
||
'marketing_fee_total' => 0.0,
|
||
'total_fee' => 0.0,
|
||
];
|
||
$cursor->addMonthNoOverflow();
|
||
}
|
||
|
||
return $periods;
|
||
}
|
||
|
||
/** @param list<array<string,mixed>> $periods */
|
||
private function summary(array $periods, string $month): array
|
||
{
|
||
$currentIndex = null;
|
||
foreach ($periods as $index => $period) {
|
||
if ($period['period'] === $month) {
|
||
$currentIndex = $index;
|
||
break;
|
||
}
|
||
}
|
||
$current = $currentIndex === null ? $this->emptyPeriod($month) : $periods[$currentIndex];
|
||
$previousRows = $currentIndex === null ? [] : array_slice($periods, 0, $currentIndex);
|
||
$previous = $previousRows === [] ? null : end($previousRows);
|
||
$previousTotal = (float) ($previous['total_fee'] ?? 0);
|
||
|
||
return array_replace($current, [
|
||
'previous_period' => $previous['period'] ?? null,
|
||
'previous_total_fee' => round($previousTotal, 2),
|
||
'total_change_rate' => $previousTotal !== 0.0
|
||
? round((($current['total_fee'] - $previousTotal) / abs($previousTotal)) * 100, 1)
|
||
: null,
|
||
]);
|
||
}
|
||
|
||
/** @return array<string,mixed> */
|
||
private function emptyPeriod(string $month): array
|
||
{
|
||
return [
|
||
'period' => $month,
|
||
'source_rows' => 0,
|
||
'push_fee_total' => 0.0,
|
||
'marketing_fee_total' => 0.0,
|
||
'total_fee' => 0.0,
|
||
];
|
||
}
|
||
|
||
/** @return array<string,mixed> */
|
||
private function emptySummary(): array
|
||
{
|
||
return array_replace($this->emptyPeriod(''), [
|
||
'previous_period' => null,
|
||
'previous_total_fee' => 0.0,
|
||
'total_change_rate' => null,
|
||
]);
|
||
}
|
||
}
|