153 lines
5.4 KiB
PHP
153 lines
5.4 KiB
PHP
<?php
|
||
|
||
namespace App\Business;
|
||
|
||
use Illuminate\Database\DatabaseManager;
|
||
|
||
/**
|
||
* 财务经营分析表 › 快递运费按月、按店铺汇总。
|
||
*
|
||
* 每一行快递运费由「结算运费 + 其他附加费汇总 + 退件费」组成:
|
||
* 结算运费 = 提供成本运费 + 运费差;三份快递应付快照都按交易单号匹配。
|
||
* 同一交易单号的多条快照金额全部相加;基础运费多行也全部相加,
|
||
* 但同一月份、同一交易单号的附加费用只挂一次,避免一条订单拆成多行后重复计入。
|
||
*/
|
||
class CourierFreightFinancialSummary
|
||
{
|
||
public function __construct(
|
||
private readonly DatabaseManager $databases,
|
||
private readonly CourierPayableSummaryMap $summaries,
|
||
) {}
|
||
|
||
/**
|
||
* 返回仅包含有快递运费来源的月份。调用方据此区分「本月无数据」和「金额为 0」。
|
||
*
|
||
* @param list<string> $reportMonths YYYY-MM
|
||
* @return array<string,array<string,float>> 月份 => [店铺 => 快递运费]
|
||
*/
|
||
public function monthlyStoreTotals(array $reportMonths, int $companyId): array
|
||
{
|
||
$wantedMonths = array_fill_keys(array_values(array_unique(array_filter(
|
||
$reportMonths,
|
||
static fn (string $month): bool => preg_match('/^\d{4}-\d{2}$/', $month) === 1,
|
||
))), true);
|
||
if ($wantedMonths === []) {
|
||
return [];
|
||
}
|
||
|
||
$differences = $this->summaries->mapByMonth(
|
||
$companyId,
|
||
CourierPayableSummaryMap::PAGE_FREIGHT_DIFFERENCE,
|
||
);
|
||
$surcharges = $this->summaries->mapByMonth(
|
||
$companyId,
|
||
CourierPayableSummaryMap::PAGE_OTHER_SURCHARGE,
|
||
);
|
||
$returnFees = $this->summaries->mapByMonth(
|
||
$companyId,
|
||
CourierPayableSummaryMap::PAGE_RETURN_FEE,
|
||
);
|
||
|
||
$totals = [];
|
||
$feeApplied = [];
|
||
$rows = $this->databases->connection()->table('courier_freight_rows')
|
||
->where('company_id', $companyId)
|
||
->whereNull('deleted_at')
|
||
->select(['source', 'shop_name', 'transaction_no'])
|
||
->selectRaw('SUM(COALESCE(freight_amount, 0)) AS freight_amount')
|
||
->selectRaw('MAX(created_at) AS imported_at')
|
||
->groupBy('source', 'shop_name', 'transaction_no')
|
||
->orderBy('source')
|
||
->orderBy('shop_name')
|
||
->orderBy('transaction_no')
|
||
->cursor();
|
||
|
||
foreach ($rows as $row) {
|
||
$month = $this->sourceMonth(
|
||
(string) $row->source,
|
||
(string) ($row->imported_at ?? ''),
|
||
array_keys($wantedMonths),
|
||
);
|
||
if ($month === null || ! isset($wantedMonths[$month])) {
|
||
continue;
|
||
}
|
||
|
||
// 即使店铺为空也保留月份,表示该月已有快递成本来源。
|
||
$totals[$month] ??= [];
|
||
$storeName = trim((string) $row->shop_name);
|
||
if ($storeName === '') {
|
||
continue;
|
||
}
|
||
|
||
$transactionNo = trim((string) $row->transaction_no);
|
||
$amount = (float) $row->freight_amount;
|
||
$feeKey = $month . "\0" . $transactionNo;
|
||
if ($transactionNo !== '' && ! isset($feeApplied[$feeKey])) {
|
||
$amount += $this->numeric($differences[$month][$transactionNo] ?? null);
|
||
$amount += $this->numeric($surcharges[$month][$transactionNo] ?? null);
|
||
$amount += $this->numeric($returnFees[$month][$transactionNo] ?? null);
|
||
$feeApplied[$feeKey] = true;
|
||
}
|
||
|
||
$totals[$month][$storeName] = round(
|
||
($totals[$month][$storeName] ?? 0.0) + $amount,
|
||
2,
|
||
);
|
||
}
|
||
|
||
return $totals;
|
||
}
|
||
|
||
/**
|
||
* 来源通常为「提供成本9月汇总」。优先取来源里的年份;没有年份时,
|
||
* 用导入时间年份补齐。若来源是「5-7月」范围,取最后月份作为该汇总的账期。
|
||
*
|
||
* @param list<string> $reportMonths
|
||
*/
|
||
private function sourceMonth(string $source, string $importedAt, array $reportMonths): ?string
|
||
{
|
||
$source = trim($source);
|
||
if ($source === '') {
|
||
return null;
|
||
}
|
||
|
||
if (preg_match(
|
||
'/(?<!\d)((?:19|20)\d{2})\D{0,4}(1[0-2]|0?[1-9])(?!\d)/u',
|
||
$source,
|
||
$matches,
|
||
) === 1) {
|
||
return $matches[1] . '-' . str_pad((string) (int) $matches[2], 2, '0', STR_PAD_LEFT);
|
||
}
|
||
|
||
if (preg_match(
|
||
'/(?:1[0-2]|0?[1-9])\s*[-~至]\s*(1[0-2]|0?[1-9])\s*月/u',
|
||
$source,
|
||
$range,
|
||
) === 1) {
|
||
$month = (int) $range[1];
|
||
} elseif (preg_match('/(1[0-2]|0?[1-9])\s*月/u', $source, $single) === 1) {
|
||
$month = (int) $single[1];
|
||
} else {
|
||
return null;
|
||
}
|
||
|
||
$year = preg_match('/^((?:19|20)\d{2})-/u', $importedAt, $date) === 1 ? $date[1] : null;
|
||
if ($year === null) {
|
||
$year = $reportMonths !== [] ? substr($reportMonths[0], 0, 4) : null;
|
||
}
|
||
|
||
return $year === null
|
||
? null
|
||
: $year . '-' . str_pad((string) $month, 2, '0', STR_PAD_LEFT);
|
||
}
|
||
|
||
private function numeric(mixed $value): float
|
||
{
|
||
if ($value === null || $value === '') {
|
||
return 0.0;
|
||
}
|
||
|
||
return is_numeric($value) ? (float) $value : 0.0;
|
||
}
|
||
}
|