212 lines
10 KiB
PHP
212 lines
10 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\View;
|
|
|
|
use InvalidArgumentException;
|
|
use Modules\EcommerceFinance\Repositories\PlantingReimbursementRepository as Repository;
|
|
use Modules\EcommerceFinance\Services\PlantingReimbursementAuditor as Auditor;
|
|
use Modules\EcommerceFinance\Services\PlantingReimbursementReferences as References;
|
|
use Modules\EcommerceFinance\Services\PlantingReimbursementWorkbench as Workbench;
|
|
|
|
/** 种菜数据汇总 → 种菜报销工作台:待报销种菜工单逐单校验,出打款明细、钉钉流程明细和要取消的工单。 */
|
|
class PlantingReimbursementWorkbenchPageData
|
|
{
|
|
public const TITLE = '种菜报销工作台';
|
|
|
|
/** 聚工单镜像超过这么久没同步成功就提示数据不是最新的。 */
|
|
public const STALE_MINUTES = 20;
|
|
|
|
public const DECISION_FILTERS = ['all' => '全部', Auditor::DECISION_PASS => '可报销', Auditor::DECISION_CANCEL => '取消工单', Auditor::DECISION_RECHECK => '待复查'];
|
|
|
|
public const COLUMNS = [
|
|
'decision_label' => '处理结果', 'issue_text' => '原因', 'work_order_no' => '工单编号', 'status' => '工单状态',
|
|
'paid_at' => '付款时间', 'initiator' => '发起人', 'shop_name' => '店铺', 'profit_center' => '所属利润中心',
|
|
'online_order_no' => '线上订单号', 'task_payment' => '任务付款', 'task_freight' => '快递费', 'task_commission' => '佣金',
|
|
'jingxi_price_diff' => '京喜差价', 'task_deduction' => '扣款', 'task_empty_package_fee' => '空包费',
|
|
'reimbursement_amount' => '最终报销金额', 'actual_paid_amount' => '买家实付', 'payee_name' => '收款人',
|
|
'payee_account' => '账号', 'payee_bank' => '开户行', 'erp_order_label' => 'ERP订单标签', 'task_remark' => '任务备注',
|
|
'quota_ider' => 'ID负责人', 'quota_level' => '职级', 'quota_rule' => '额度规则', 'quota_amount' => '种菜报销金额(额度口径)',
|
|
'quota_day_total' => '日累计', 'quota_day_limit' => '日限额', 'quota_month_total' => '月累计', 'quota_month_limit' => '月限额',
|
|
'quota_message' => '额度说明',
|
|
];
|
|
|
|
public const MONEY_COLUMNS = [
|
|
'task_payment', 'task_freight', 'task_commission', 'jingxi_price_diff', 'task_deduction',
|
|
'task_empty_package_fee', 'reimbursement_amount', 'actual_paid_amount',
|
|
'quota_amount', 'quota_day_total', 'quota_day_limit', 'quota_month_total', 'quota_month_limit',
|
|
];
|
|
|
|
public function __construct(
|
|
private readonly Workbench $workbench,
|
|
private readonly Repository $repository,
|
|
private readonly References $references,
|
|
private readonly \Modules\EcommerceFinance\Services\PlantingReimbursementRobot $robot,
|
|
private readonly \Modules\EcommerceFinance\Services\PlantingQuotaRules $quotaRules,
|
|
private readonly \Modules\EcommerceFinance\Services\PlantingCommissionPeople $commissionPeople,
|
|
private readonly \Modules\EcommerceFinance\Services\PlantingDingTalkSubmission $dingTalk,
|
|
private readonly \Modules\EcommerceFinance\Services\PlantingReimbursementBatches $batches,
|
|
) {}
|
|
|
|
/** @return array{date:string,paid_from:string,scope:string,decision:string} */
|
|
public function filters(): array
|
|
{
|
|
$date = trim((string) request('date', '')) ?: Workbench::defaultDate();
|
|
try {
|
|
$paidFrom = trim((string) request('paid_from', '')) ?: Workbench::defaultPaidFrom($date);
|
|
Workbench::paidWindow($date, $paidFrom);
|
|
} catch (InvalidArgumentException $exception) {
|
|
abort(422, $exception->getMessage());
|
|
}
|
|
$scope = (string) request('scope', '') ?: Workbench::DEFAULT_SCOPE;
|
|
abort_unless(isset(Workbench::SCOPES[$scope]), 422, '取单范围无效。');
|
|
$decision = (string) request('decision', '') ?: 'all';
|
|
abort_unless(isset(self::DECISION_FILTERS[$decision]), 422, '处理结果筛选无效。');
|
|
|
|
return ['date' => $date, 'paid_from' => $paidFrom, 'scope' => $scope, 'decision' => $decision];
|
|
}
|
|
|
|
/** @param array{date:string,paid_from:string,scope:string} $filters */
|
|
/** 已确认的批次直接读确认时固定下来的结果;没确认的按聚工单当前状态现取现算。 */
|
|
public function build(array $filters): array
|
|
{
|
|
return $this->batches->get((int) company_id(), $filters['date']) ?? $this->fresh($filters);
|
|
}
|
|
|
|
/** 不看已确认批次,按聚工单当前状态重新生成。 */
|
|
public function fresh(array $filters): array
|
|
{
|
|
return $this->workbench->build((int) company_id(), $filters['date'], $filters['paid_from'], Workbench::scopeStatuses($filters['scope']));
|
|
}
|
|
|
|
public function page(): array
|
|
{
|
|
$filters = $this->filters();
|
|
$sync = $this->repository->syncStatus();
|
|
$result = null;
|
|
$error = null;
|
|
|
|
$confirmed = $this->batches->get((int) company_id(), $filters['date']);
|
|
if ($confirmed !== null) {
|
|
$result = $confirmed;
|
|
} elseif (! in_array($sync['code'], ['ready', 'failed'], true)) {
|
|
$error = '聚工单镜像未就绪,暂时无法生成报销批次。';
|
|
} else {
|
|
try {
|
|
$result = $this->build($filters);
|
|
} catch (\Throwable $exception) {
|
|
report($exception);
|
|
$error = '种菜报销工作台数据暂时无法读取,请稍后重试。';
|
|
}
|
|
}
|
|
|
|
// 保留完整批次供页面局部筛选;切换结果不重新取单,也不改变汇总或导出范围。
|
|
$rows = $result['rows'] ?? [];
|
|
// 先看要处理的:取消、待复查在前。
|
|
$order = [Auditor::DECISION_CANCEL => 0, Auditor::DECISION_RECHECK => 1, Auditor::DECISION_PASS => 2];
|
|
usort($rows, static fn (array $a, array $b): int => [$order[$a['decision']], $a['initiator'], $a['paid_at']] <=> [$order[$b['decision']], $b['initiator'], $b['paid_at']]);
|
|
|
|
// 镜像停更超过 20 分钟或最近一次同步失败:数据不是最新的,提醒先别报销。
|
|
$syncStale = $sync['code'] !== 'ready' || $sync['last_succeeded_at'] === null
|
|
|| now()->diffInMinutes(\Illuminate\Support\Carbon::parse($sync['last_succeeded_at']), true) > self::STALE_MINUTES;
|
|
|
|
return [
|
|
'view' => 'business.pages.billing-transactions.planting-reimbursement-workbench',
|
|
'workbenchSyncStale' => $syncStale,
|
|
'workbenchRobot' => $this->robotSummary(),
|
|
'workbenchNoticeLog' => $this->robot->noticeLog((int) company_id(), $filters['date']),
|
|
'workbenchRosterSource' => $result['roster_source'] ?? null,
|
|
'workbenchShopSource' => $result['shop_source'] ?? null,
|
|
'workbenchQuotaError' => $result['quota_error'] ?? null,
|
|
'workbenchQuotaRules' => $this->quotaRulesPage(),
|
|
'workbenchCommission' => $this->commissionPage(),
|
|
'workbenchGate' => $result ? \Modules\EcommerceFinance\Services\PlantingReimbursementGate::check($result) : null,
|
|
'workbenchDingRecord' => $result ? $this->dingTalk->record((int) company_id(), (string) $result['summary']['bill_date']) : null,
|
|
'workbenchDingPreview' => $result && request('ding_preview') === '1' ? $this->dingPreview($result) : null,
|
|
'workbenchBatch' => $result['batch'] ?? null,
|
|
'workbenchOperations' => $confirmed !== null ? $this->operations($confirmed) : null,
|
|
'workbenchFilters' => $filters,
|
|
'workbenchWindow' => Workbench::paidWindow($filters['date'], $filters['paid_from']),
|
|
'workbenchSync' => $sync,
|
|
'workbenchError' => $error,
|
|
'workbenchSummary' => $result['summary'] ?? null,
|
|
'workbenchRows' => $rows,
|
|
'workbenchUploads' => $result['uploads'] ?? $this->safeUploads(),
|
|
];
|
|
}
|
|
|
|
/** @return array{rules:list<array<string,mixed>>,source:string,updated_at:?string,error:?string} */
|
|
private function quotaRulesPage(): array
|
|
{
|
|
try {
|
|
$document = $this->quotaRules->document((int) company_id());
|
|
|
|
return ['rules' => $document['rules'], 'source' => $document['source'], 'updated_at' => $document['updated_at'], 'error' => null];
|
|
} catch (\Throwable $exception) {
|
|
report($exception);
|
|
|
|
return ['rules' => [], 'source' => 'error', 'updated_at' => null, 'error' => '额度规则读取失败'];
|
|
}
|
|
}
|
|
|
|
/** @return array<string,mixed>|null 聚工单操作清单与进度(按同步回来的工单状态核对) */
|
|
private function operations(array $batch): ?array
|
|
{
|
|
try {
|
|
$statuses = $this->repository->currentStatuses(array_column($batch['rows'], 'work_order_no'));
|
|
} catch (\Throwable $exception) {
|
|
report($exception);
|
|
$statuses = [];
|
|
}
|
|
|
|
return \Modules\EcommerceFinance\Services\PlantingReimbursementBatches::operations($batch, $statuses);
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
private function commissionPage(): array
|
|
{
|
|
try {
|
|
return $this->commissionPeople->document((int) company_id()) + ['error' => null];
|
|
} catch (\Throwable $exception) {
|
|
report($exception);
|
|
|
|
return ['people' => [], 'source' => 'error', 'updated_at' => null, 'error' => '报销佣金人员名单读取失败'];
|
|
}
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
private function dingPreview(array $result): array
|
|
{
|
|
try {
|
|
return $this->dingTalk->preview($result) + ['error' => null];
|
|
} catch (\Throwable $exception) {
|
|
report($exception);
|
|
|
|
return ['error' => $exception instanceof \RuntimeException ? $exception->getMessage() : '读取钉钉表单失败,请稍后重试。'];
|
|
}
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
private function safeUploads(): array
|
|
{
|
|
try {
|
|
return $this->references->uploads((int) company_id());
|
|
} catch (\Throwable $exception) {
|
|
report($exception);
|
|
|
|
return array_fill_keys(array_keys(References::KINDS), null);
|
|
}
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
private function robotSummary(): array
|
|
{
|
|
try {
|
|
return $this->robot->summary();
|
|
} catch (\Throwable $exception) {
|
|
report($exception);
|
|
|
|
return ['configured' => false, 'webhook' => '', 'has_secret' => false, 'keyword' => '', 'reporters' => '', 'digest_webhook' => '', 'has_digest_secret' => false];
|
|
}
|
|
}
|
|
}
|