162 lines
8.3 KiB
PHP
162 lines
8.3 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\View;
|
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Modules\EcommerceFinance\Repositories\PlantingReimbursementRepository as Repository;
|
|
use Modules\EcommerceFinance\Services\PlantingReimbursementIncomeCheck as IncomeCheck;
|
|
use Carbon\CarbonImmutable;
|
|
|
|
/** 种菜数据汇总 → 种菜报销(聚工单打款工单“种菜报销”,已完成,完成时间自 2026-09-02 起)。 */
|
|
class PlantingReimbursementPageData
|
|
{
|
|
use PlantingPageFilters;
|
|
|
|
public const TITLE = '种菜报销';
|
|
|
|
public const PER_PAGE = 50;
|
|
|
|
public const TEXT_FILTERS = [
|
|
'work_order_no' => '工单编号',
|
|
'online_order_no' => '线上订单号',
|
|
'shop_name' => '店铺名称',
|
|
'task_remark' => '任务备注',
|
|
'seller_remark' => '卖家备注',
|
|
'payee_name' => '信誉账号名称',
|
|
'initiator' => '发起人',
|
|
];
|
|
|
|
public const COLUMNS = [
|
|
'completed_at' => '完成时间', 'started_at' => '发起时间', 'work_order_no' => '工单编号', 'status' => '工单状态',
|
|
'platform' => '平台', 'shop_name' => '店铺名称', 'online_order_no' => '线上订单号', 'internal_order_no' => '内部单号',
|
|
'task_remark' => '任务备注', 'seller_remark' => '卖家备注',
|
|
'actual_paid_amount' => '实付金额', 'task_payment' => '任务付款金额', 'task_freight' => '任务快递费',
|
|
'task_commission' => '任务佣金', 'jingxi_price_diff' => '京喜差价', 'task_deduction' => '任务扣款费用',
|
|
'task_empty_package_fee' => '任务空包费', 'reimbursement_amount' => '最终报销金额',
|
|
'payee_name' => '信誉账号名称', 'payee_account' => '信誉账号', 'payee_bank' => '信誉账号开户行', 'payee_mobile' => '收款人手机号',
|
|
'initiator' => '发起人', 'executor' => '执行人', 'completer' => '完成人',
|
|
'product_code' => '商品编码', 'product_name' => '商品名称', 'product_quantity' => '商品数量', 'platform_product_id' => '平台商品ID',
|
|
'erp_order_status' => 'ERP订单状态', 'platform_order_status' => '平台订单状态', 'paid_at' => '付款时间',
|
|
'income_total' => '订单收入合计', 'income_count' => '收入记录数', 'income_sources' => '收入来源',
|
|
'income_latest_at' => '最近入账时间', 'income_difference' => '差异', 'income_result' => '核对结论', 'income_note' => '备注',
|
|
];
|
|
|
|
public const MONEY_COLUMNS = [...Repository::MONEY_FIELDS, 'income_total', 'income_difference'];
|
|
|
|
public function __construct(private readonly Repository $repository, private readonly IncomeCheck $incomeCheck) {}
|
|
|
|
/** @return array<string,string> */
|
|
public function filters(): array
|
|
{
|
|
$queryMonth = request()->query('month', '');
|
|
abort_unless(is_string($queryMonth), 422, '报销月份无效。');
|
|
$filters = $this->baseFilters();
|
|
if ($queryMonth === '') $filters['month'] = '';
|
|
$defaultFrom = $filters['month'] === '' ? IncomeCheck::DEFAULT_FROM : substr(Repository::window($filters['month'])['from'], 0, 10);
|
|
$from = request()->query('completed_from', $defaultFrom);
|
|
abort_unless(is_string($from) && preg_match('/^\d{4}-\d{2}-\d{2}$/', $from)
|
|
&& checkdate((int) substr($from, 5, 2), (int) substr($from, 8, 2), (int) substr($from, 0, 4))
|
|
&& $from >= substr(Repository::START_AT, 0, 10) && $from <= today()->format('Y-m-d'), 422, '完成起日无效。');
|
|
$result = request()->query('income_result', '');
|
|
abort_unless(is_string($result) && in_array($result, ['', ...IncomeCheck::RESULTS], true), 422, '核对结论无效。');
|
|
return $filters + ['completed_from' => $from, 'income_result' => $result];
|
|
}
|
|
|
|
/**
|
|
* @param array<string,string> $filters
|
|
* @param list<array<string,mixed>>|null $rows
|
|
* @return \Generator<int,array<string,mixed>>
|
|
*/
|
|
public function rows(array $filters, ?array $rows = null): \Generator
|
|
{
|
|
$rows ??= $this->incomeRows($filters);
|
|
foreach ($rows as $row) {
|
|
if ($this->matchesText($row, $filters) && ($filters['income_result'] === '' || $row['income_result'] === $filters['income_result'])) {
|
|
yield $row;
|
|
}
|
|
}
|
|
}
|
|
|
|
private function incomeRows(array $filters): array
|
|
{
|
|
$rows = [];
|
|
$firstMonth = Repository::defaultMonth(CarbonImmutable::parse($filters['completed_from']));
|
|
$months = $filters['month'] === '' ? array_reverse(Repository::months()) : [$filters['month']];
|
|
foreach ($months as $month) {
|
|
if ($month < $firstMonth) continue;
|
|
foreach ($this->repository->rows($month) as $row) {
|
|
if ($row['completed_at'] >= $filters['completed_from'] . ' 00:00:00'
|
|
&& $row['completed_at'] < today()->addDay()->format('Y-m-d') && $this->matchesText($row, $filters)) $rows[] = $row;
|
|
}
|
|
}
|
|
usort($rows, static fn ($a, $b) => [$b['completed_at'], $b['work_order_no']] <=> [$a['completed_at'], $a['work_order_no']]);
|
|
return $this->incomeCheck->rows($rows);
|
|
}
|
|
|
|
public function page(): array
|
|
{
|
|
$filters = $this->filters();
|
|
$page = max(1, (int) request('page', 1));
|
|
$sync = $this->repository->syncStatus();
|
|
$summary = ['count' => 0, 'task_payment' => 0, 'reimbursement_amount' => 0, 'task_commission' => 0, 'task_empty_package_fee' => 0];
|
|
$items = [];
|
|
$incomeSummary = array_fill_keys(IncomeCheck::RESULTS, ['count' => 0, 'difference' => 0]);
|
|
$initiators = [];
|
|
$incomeStatus = null;
|
|
$error = $this->syncError($sync);
|
|
|
|
if ($error === null) {
|
|
try {
|
|
$incomeStatus = $this->incomeCheck->status();
|
|
$all = $this->incomeRows($filters);
|
|
foreach ($all as $row) {
|
|
if (! $this->matchesText($row, $filters)) continue;
|
|
$result = $row['income_result'];
|
|
$incomeSummary[$result]['count']++;
|
|
$incomeSummary[$result]['difference'] += self::cents($row['income_difference']);
|
|
if ($result === '多报销') {
|
|
$name = trim($row['initiator']) ?: '未标注';
|
|
$initiators[$name] ??= ['count' => 0, 'difference' => 0];
|
|
$initiators[$name]['count']++;
|
|
$initiators[$name]['difference'] += self::cents($row['income_difference']);
|
|
}
|
|
}
|
|
uasort($initiators, static fn ($a, $b) => $b['difference'] <=> $a['difference']);
|
|
foreach ($this->rows($filters, $all) as $row) {
|
|
if ($summary['count'] >= ($page - 1) * self::PER_PAGE && count($items) < self::PER_PAGE) {
|
|
$items[] = $row;
|
|
}
|
|
$summary['count']++;
|
|
foreach (['task_payment', 'reimbursement_amount', 'task_commission', 'task_empty_package_fee'] as $key) {
|
|
$summary[$key] += self::cents($row[$key]);
|
|
}
|
|
}
|
|
} catch (\Throwable $exception) {
|
|
report($exception);
|
|
$error = '种菜报销数据暂时无法读取,请稍后重试。';
|
|
$summary = array_fill_keys(array_keys($summary), 0);
|
|
$items = [];
|
|
$incomeSummary = array_fill_keys(IncomeCheck::RESULTS, ['count' => 0, 'difference' => 0]);
|
|
$initiators = [];
|
|
}
|
|
}
|
|
|
|
return [
|
|
'view' => 'business.pages.billing-transactions.planting-reimbursements',
|
|
'plantingFilters' => $filters,
|
|
'plantingWindow' => $filters['month'] === '' ? null : Repository::window($filters['month']),
|
|
'plantingMonths' => Repository::months(),
|
|
'plantingSummary' => $summary,
|
|
'plantingIncomeSummary' => $incomeSummary,
|
|
'plantingIncomeInitiators' => $initiators,
|
|
'plantingIncomeStatus' => $incomeStatus,
|
|
'plantingSync' => $sync,
|
|
'plantingError' => $error,
|
|
'plantingRows' => new LengthAwarePaginator($items, $summary['count'], self::PER_PAGE, $page, [
|
|
'path' => request()->url(),
|
|
'query' => request()->except('page'),
|
|
]),
|
|
];
|
|
}
|
|
}
|