renshen 3a97bf942c feat: 订单利润接入聚胜算页面费用结果 (#987)
订单利润的包材费、快递费、账单费用直接读取聚胜算页面接口结果;新增抓取脚本与导入命令。
2026-11-07 17:34:38 +08:00

455 lines
25 KiB
PHP

@php
$rows = $orderProfitRows ?? null;
$filters = $orderProfitFilters ?? [];
$status = $orderProfitStatus ?? [];
$hasFilters = (bool) ($orderProfitHasFilters ?? false);
$dateField = $filters['date_field'] ?? 'pay_time';
$dateFieldLabels = [
'pay_time' => '支付时间',
'send_time' => '发货时间',
'order_time' => '订单时间',
];
$columns = [
'shop_name' => '店铺名称',
'online_order_no' => '线上单号',
'internal_order_no' => '内部单号',
'order_time' => '订单时间',
'product_quantity' => '数量',
'product_amount' => '商品金额',
'cost_amount' => '成本',
'actual_shipped_quantity' => '实发数量',
'actual_shipped_amount' => '实发金额',
'actual_shipped_cost' => '实发成本',
'merchant_discount' => '商家优惠',
'freight_income' => '运费收入',
'payment_amount' => '付款金额',
'platform_discount' => '其中:平台优惠',
'refund_amount' => '退款金额',
'refund_type' => '退货类型',
'platform_subsidy' => '退平台补贴',
'pre_platform_subsidy' => '发货前-退平台补贴',
'pre_application_quantity' => '发货前-申请数量',
'pre_application_amount' => '发货前-申请金额',
'pre_application_cost' => '发货前-申请成本',
'pre_return_quantity' => '发货前-退回数量',
'pre_return_amount' => '发货前-退回金额',
'pre_return_cost' => '发货前-退回成本',
'pre_refund_amount' => '发货前-退款金额',
'pre_refund_cost' => '发货前-退款成本',
'post_platform_subsidy' => '发货后-退平台补贴',
'post_application_quantity' => '发货后-申请数量',
'post_application_amount' => '发货后-申请金额',
'post_application_cost' => '发货后-申请成本',
'post_return_quantity' => '发货后-退回数量',
'post_return_amount' => '发货后-退回金额',
'post_return_cost' => '发货后-退回成本',
'post_refund_amount' => '发货后-退款金额',
'post_refund_cost' => '发货后-退款成本',
'defective_quantity' => '次品数量',
'matched_package_fee' => '包材费(自动匹配)',
'matched_freight_fee' => '快递费(自动匹配)',
'matched_bill_fee' => '账单费用(自动匹配)',
'classification_order' => '分类单',
'source_row_count' => '来源行数',
];
$numberFields = array_flip([
'product_quantity', 'product_amount', 'cost_amount',
'actual_shipped_quantity', 'actual_shipped_amount', 'actual_shipped_cost',
'merchant_discount', 'freight_income', 'payment_amount', 'platform_discount',
'refund_amount', 'platform_subsidy',
'pre_platform_subsidy', 'pre_application_quantity', 'pre_application_amount', 'pre_application_cost',
'pre_return_quantity', 'pre_return_amount', 'pre_return_cost', 'pre_refund_amount', 'pre_refund_cost',
'post_platform_subsidy', 'post_application_quantity', 'post_application_amount', 'post_application_cost',
'post_return_quantity', 'post_return_amount', 'post_return_cost', 'post_refund_amount', 'post_refund_cost',
'defective_quantity', 'matched_package_fee', 'matched_freight_fee',
'matched_bill_fee', 'source_row_count',
]);
$fixedColumns = [
'shop_name' => ['left' => '0rem', 'width' => '13rem'],
'online_order_no' => ['left' => '13rem', 'width' => '9rem'],
'internal_order_no' => ['left' => '22rem', 'width' => '6rem'],
'detail' => ['left' => '28rem', 'width' => '4.75rem'],
];
$fixedColumnStyle = static fn (string $field): string => sprintf(
'left: %1$s; width: %2$s; min-width: %2$s; max-width: %2$s;',
$fixedColumns[$field]['left'],
$fixedColumns[$field]['width'],
);
@endphp
<div class="upstream-data-page upstream-data-page--spaced">
@include('business.pages.partials.upstream-data-styles')
<style>
.order-profit-table { min-width: 290rem; }
.order-profit-table [data-order-profit-fixed-key="detail"] {
box-shadow: 2px 0 0 var(--vip-border, #d8d2c7);
}
.order-profit-note {
display: flex;
align-items: flex-start;
gap: .55rem;
padding: .75rem 1.1rem;
border-bottom: 1px solid var(--vip-border, #d8d2c7);
background: #fffdf8;
color: var(--vip-content-muted, #595959);
font-size: .78rem;
line-height: 1.5;
}
.order-profit-note .material-icons-outlined { color: var(--vip-content-accent-dark, #856632); font-size: 1rem; }
.order-profit-detail-button {
height: 1.8rem;
padding: 0 .55rem;
border: 1px solid var(--vip-border, #d8d2c7);
border-radius: .4rem;
background: #fff;
color: var(--vip-content-accent-dark, #856632);
cursor: pointer;
}
.order-profit-detail-button:hover { border-color: var(--vip-content-accent-dark, #856632); background: var(--vip-content-accent-soft, #f7f0de); }
.order-profit-detail-row > td { padding: 0 !important; background: #fbfaf6 !important; }
.order-profit-detail-row[hidden] { display: none; }
.order-profit-detail-wrap { padding: .8rem 1rem 1rem 3rem; }
.order-profit-detail-title { margin-bottom: .55rem; color: var(--vip-content-accent-dark, #856632); font-size: .8rem; font-weight: 700; }
.order-profit-detail-table { width: 100%; min-width: 92rem; border-collapse: collapse; background: #fff; }
.order-profit-detail-table th,
.order-profit-detail-table td { padding: .42rem .55rem; border: 1px solid var(--vip-border, #d8d2c7); font-size: .72rem; white-space: nowrap; }
.order-profit-detail-table th { background: var(--vip-content-accent-soft, #f7f0de); text-align: left; }
.order-profit-detail-table .is-number { text-align: right; font-variant-numeric: tabular-nums; }
.order-profit-sync { padding: .6rem 1.1rem; border-bottom: 1px solid var(--vip-border, #d8d2c7); color: var(--vip-content-muted, #595959); font-size: .76rem; }
</style>
<form method="GET" action="{{ request()->url() }}" class="upstream-data-toolbar" aria-label="订单利润筛选">
<div class="upstream-data-field">
<label for="order-profit-date-field">时间类型</label>
<select id="order-profit-date-field" name="date_field">
@foreach ($dateFieldLabels as $value => $label)
<option value="{{ $value }}" @selected($dateField === $value)>{{ $label }}</option>
@endforeach
</select>
</div>
<div class="upstream-data-field">
<label for="order-profit-from">开始日期</label>
<input id="order-profit-from" name="from" type="date" value="{{ $filters['from'] ?? '' }}">
</div>
<div class="upstream-data-field">
<label for="order-profit-to">结束日期</label>
<input id="order-profit-to" name="to" type="date" value="{{ $filters['to'] ?? '' }}">
</div>
<div class="upstream-data-field">
<label for="order-profit-order">订单号</label>
<textarea id="order-profit-order" name="order_no" rows="2"
placeholder="线上、内部或子订单号;可批量:每行一个">{{ $filters['order_no'] ?? '' }}</textarea>
</div>
@include('business.pages.partials.order-data-shop-picker', [
'shopPickerOptions' => $orderProfitShopOptions ?? [],
'shopPickerSelected' => $filters['shop_names'] ?? [],
'shopPickerToggleId' => 'order-profit-shop',
])
<div class="upstream-data-field">
<label for="order-profit-sku">商品编码</label>
<textarea id="order-profit-sku" name="sku_id" rows="2"
placeholder="商品编码;可批量:每行一个">{{ $filters['sku_id'] ?? '' }}</textarea>
</div>
<div class="upstream-data-actions">
<button type="submit" class="upstream-data-button upstream-data-button--primary">
<span class="material-icons-outlined" aria-hidden="true">search</span>
<span>查询</span>
</button>
<a href="{{ route('business.pages.show', ['section' => 'order-profit', 'reset' => 1]) }}" class="upstream-data-button">
<span class="material-icons-outlined" aria-hidden="true">refresh</span>
<span>重置</span>
</a>
<button type="button" id="order-profit-export-button"
class="upstream-data-button"
data-export-table="order-profit-table"
data-export-filename="订单利润_第{{ $rows?->currentPage() ?? 1 }}页.csv"
@if (!$rows || $rows->isEmpty()) disabled="disabled" aria-disabled="true" @endif>
<span class="material-icons-outlined" aria-hidden="true">download</span>
<span>导出当前页</span>
</button>
</div>
</form>
<div class="order-profit-note">
<span class="material-icons-outlined" aria-hidden="true">info</span>
<span>商品成本、退款、退货类型和退平台补贴来自聚水潭订单利润共享镜像;包材费、快递费、账单费用直接读取聚胜算订单利润页面结果,未同步到该订单时显示“—”。</span>
</div>
@if ($orderProfitError ?? null)
<div class="upstream-data-alert" role="alert">
<span class="material-icons-outlined" aria-hidden="true">error_outline</span>
<span>{{ $orderProfitError }}</span>
</div>
@endif
<div class="upstream-data-table-panel">
@if (!empty($status))
<div class="order-profit-sync">
数据状态:{{ ($status['ready'] ?? false) ? '共享镜像已就绪' : '共享镜像同步中' }}
@if (!empty($status['last_succeeded_at'])) · 最近同步 {{ $status['last_succeeded_at'] }} @endif
@if (!empty($status['last_fees_synced_at'])) · 胜算费用同步 {{ $status['last_fees_synced_at'] }} @endif
</div>
@endif
<div class="upstream-data-table-scroll">
<table id="order-profit-table" class="upstream-data-table order-profit-table"
aria-label="订单利润" data-finance-table-name="订单利润"
data-order-profit-autofit-fixed>
<thead>
<tr>
@foreach ($columns as $field => $header)
<th data-field="{{ $field }}"
@if (isset($fixedColumns[$field]))
data-finance-fixed="true"
data-order-profit-fixed-key="{{ $field }}"
style="{{ $fixedColumnStyle($field) }}"
@endif
@class([
'is-number' => isset($numberFields[$field]),
'is-column-hidden' => $field === 'source_row_count',
])>{{ $header }}</th>
@if ($field === 'internal_order_no')
<th data-field="detail" data-finance-fixed="true"
data-order-profit-fixed-key="detail"
style="{{ $fixedColumnStyle('detail') }}">明细</th>
@endif
@endforeach
</tr>
</thead>
<tbody>
@forelse ($rows ?? [] as $row)
@php
$detailId = 'order-profit-detail-' . $loop->index;
$items = collect(data_get($row, 'items', []));
@endphp
<tr>
@foreach ($columns as $field => $header)
@php $value = data_get($row, $field); @endphp
<td data-export-value="{{ $value ?? '' }}"
data-export-number="{{ isset($numberFields[$field]) ? 'true' : 'false' }}"
data-export-field="{{ $field }}"
@if (isset($fixedColumns[$field]))
data-finance-fixed="true"
data-order-profit-fixed-key="{{ $field }}"
style="{{ $fixedColumnStyle($field) }}"
@endif
@class([
'is-number' => isset($numberFields[$field]),
'is-column-hidden' => $field === 'source_row_count',
])>
@if ($value === null || $value === '')
-
@elseif (isset($numberFields[$field]))
{{ number_format((float) $value, 2) }}
@else
{{ $value }}
@endif
</td>
@if ($field === 'internal_order_no')
<td data-export-field="detail" data-finance-fixed="true"
data-order-profit-fixed-key="detail"
style="{{ $fixedColumnStyle('detail') }}">
<button type="button" class="order-profit-detail-button"
data-order-profit-detail="{{ $detailId }}"
aria-expanded="false">查看</button>
</td>
@endif
@endforeach
</tr>
<tr id="{{ $detailId }}" class="order-profit-detail-row" hidden>
<td colspan="{{ count($columns) + 1 }}">
<div class="order-profit-detail-wrap">
<div class="order-profit-detail-title">
{{ data_get($row, 'shop_name') }} · {{ data_get($row, 'online_order_no') }} · 来源明细 {{ $items->count() }} 行
</div>
<div class="upstream-data-table-scroll">
<table class="order-profit-detail-table">
<thead>
<tr>
<th>售后单号</th><th>商品编码</th><th>款式编码</th>
<th>店铺商品编码</th><th>商品名称</th><th>颜色规格</th>
<th class="is-number">商品数量</th><th class="is-number">商品金额</th>
<th class="is-number">商品成本</th><th>退款类型</th>
<th class="is-number">发货后退款金额</th><th class="is-number">发货后退款成本</th>
<th class="is-number">退平台补贴</th><th>组合装</th><th>发货仓库</th>
<th>快递单号</th><th>快递公司</th><th>订单标签</th>
</tr>
</thead>
<tbody>
@forelse ($items as $item)
<tr>
<td>{{ data_get($item, 'after_sales_no') ?: '-' }}</td>
<td>{{ data_get($item, 'product_code') ?: '-' }}</td>
<td>{{ data_get($item, 'style_code') ?: '-' }}</td>
<td>{{ data_get($item, 'shop_product_code') ?: '-' }}</td>
<td>{{ data_get($item, 'product_name') ?: '-' }}</td>
<td>{{ data_get($item, 'color_spec') ?: '-' }}</td>
<td class="is-number">{{ number_format((float) data_get($item, 'product_quantity', 0), 2) }}</td>
<td class="is-number">{{ number_format((float) data_get($item, 'product_amount', 0), 2) }}</td>
<td class="is-number">{{ number_format((float) data_get($item, 'product_cost', 0), 2) }}</td>
<td>{{ data_get($item, 'refund_type') ?: '-' }}</td>
<td class="is-number">{{ number_format((float) data_get($item, 'post_refund_amount', 0), 2) }}</td>
<td class="is-number">{{ number_format((float) data_get($item, 'post_refund_cost', 0), 2) }}</td>
<td class="is-number">{{ number_format((float) data_get($item, 'post_platform_subsidy', 0), 2) }}</td>
<td>{{ data_get($item, 'combine_type') ?: '-' }}</td>
<td>{{ data_get($item, 'shipping_warehouse') ?: '-' }}</td>
<td>{{ data_get($item, 'express_no') ?: '-' }}</td>
<td>{{ data_get($item, 'express_company') ?: '-' }}</td>
<td>{{ data_get($item, 'order_tags') ?: '-' }}</td>
</tr>
@empty
<tr><td colspan="18">暂无来源明细</td></tr>
@endforelse
</tbody>
</table>
</div>
</div>
</td>
</tr>
@empty
<tr class="is-empty">
<td colspan="{{ count($columns) + 1 }}">
{{ $hasFilters ? '暂无订单利润数据' : '请输入订单号、店铺或日期后查询' }}
</td>
</tr>
@endforelse
</tbody>
</table>
</div>
<div class="upstream-data-pagination">
<span>
当前页 {{ $rows?->count() ?? 0 }} 条
@if (!empty($status['ready'])) · 共享镜像 @endif
</span>
<div class="upstream-data-pagination__controls">
<a href="{{ $rows?->previousPageUrl() ?? '#' }}"
@class(['upstream-data-button', 'is-disabled' => !$rows || $rows->onFirstPage()])
aria-label="上一页"><span class="material-icons-outlined" aria-hidden="true">chevron_left</span></a>
<span>第 {{ $rows?->currentPage() ?? 1 }} 页</span>
<a href="{{ $rows?->nextPageUrl() ?? '#' }}"
@class(['upstream-data-button', 'is-disabled' => !$rows?->hasMorePages()])
aria-label="下一页"><span class="material-icons-outlined" aria-hidden="true">chevron_right</span></a>
</div>
</div>
</div>
</div>
<script>
(() => {
const fitOrderProfitFixedColumns = () => {
const table = document.querySelector('table[data-order-profit-autofit-fixed]');
if (!table) return;
const cells = Array.from(table.querySelectorAll(
'thead th[data-order-profit-fixed-key], '
+ 'tbody > tr:not(.order-profit-detail-row):not(.is-empty) > td[data-order-profit-fixed-key]',
));
const widths = new Map();
const rootFontSize = parseFloat(getComputedStyle(document.documentElement).fontSize) || 16;
const minimumWidths = {
shop_name: rootFontSize * 8,
online_order_no: rootFontSize * 8,
internal_order_no: rootFontSize * 4.5,
detail: rootFontSize * 4.75,
};
cells.forEach((cell) => {
const key = cell.dataset.orderProfitFixedKey;
if (!key) return;
const style = getComputedStyle(cell);
const horizontalSpacing = [
style.paddingLeft,
style.paddingRight,
style.borderLeftWidth,
style.borderRightWidth,
].reduce((total, value) => total + (parseFloat(value) || 0), 0);
const detailButton = cell.querySelector('.order-profit-detail-button');
let contentWidth = 0;
if (detailButton) {
contentWidth = detailButton.getBoundingClientRect().width;
} else {
const range = document.createRange();
range.selectNodeContents(cell);
contentWidth = range.getBoundingClientRect().width;
}
const measuredWidth = Math.ceil(contentWidth + horizontalSpacing + 2);
widths.set(key, Math.max(widths.get(key) || 0, measuredWidth, minimumWidths[key] || 0));
});
let left = 0;
const orderedKeys = Array.from(table.tHead?.rows?.[0]?.cells || [])
.map((cell) => cell.dataset.orderProfitFixedKey)
.filter(Boolean);
orderedKeys.forEach((key) => {
const width = widths.get(key);
if (!width) return;
cells
.filter((cell) => cell.dataset.orderProfitFixedKey === key)
.forEach((cell) => {
cell.style.width = `${width}px`;
cell.style.minWidth = `${width}px`;
cell.style.maxWidth = `${width}px`;
cell.style.left = `${left}px`;
});
left += width;
});
};
const scheduleFixedColumnFit = () => {
window.requestAnimationFrame(() => window.requestAnimationFrame(fitOrderProfitFixedColumns));
};
scheduleFixedColumnFit();
document.fonts?.ready.then(scheduleFixedColumnFit).catch(() => {});
window.addEventListener('resize', scheduleFixedColumnFit, { passive: true });
document.querySelectorAll('[data-order-profit-detail]').forEach((button) => {
if (button.dataset.bound === 'true') return;
button.dataset.bound = 'true';
button.addEventListener('click', () => {
const row = document.getElementById(button.dataset.orderProfitDetail || '');
if (!row) return;
const open = row.hidden;
row.hidden = !open;
button.setAttribute('aria-expanded', open ? 'true' : 'false');
button.textContent = open ? '收起' : '查看';
});
});
const exportButton = document.getElementById('order-profit-export-button');
if (!exportButton || exportButton.dataset.bound === 'true') return;
exportButton.dataset.bound = 'true';
exportButton.addEventListener('click', () => {
const table = document.getElementById(exportButton.dataset.exportTable || '');
const bodyRows = table ? Array.from(table.querySelectorAll('tbody > tr:not(.order-profit-detail-row):not(.is-empty)')) : [];
if (!table || bodyRows.length === 0) return;
const protect = (value) => /^[=+\-@\t\r]/.test(value) ? `'${value}` : value;
const csvCell = (value, isNumber = false) => {
const raw = String(value ?? '').replace(/\r\n?/g, '\n');
const normalized = isNumber ? raw : protect(raw);
return `"${normalized.replace(/"/g, '""')}"`;
};
const headers = Array.from(table.querySelectorAll('thead th')).map((cell) => csvCell(cell.textContent.trim()));
const rows = bodyRows.map((row) => Array.from(row.querySelectorAll(':scope > td'))
.map((cell) => cell.matches('[data-export-value]')
? csvCell(cell.dataset.exportValue || '', cell.dataset.exportNumber === 'true')
: csvCell(cell.textContent.trim())));
const csv = [headers, ...rows].map((row) => row.join(',')).join('\r\n');
const url = URL.createObjectURL(new Blob(['\uFEFF', csv], { type: 'text/csv;charset=utf-8' }));
const link = document.createElement('a');
link.href = url;
link.download = exportButton.dataset.exportFilename || '订单利润.csv';
document.body.appendChild(link);
link.click();
window.setTimeout(() => {
link.remove();
URL.revokeObjectURL(url);
}, 1000);
});
})();
</script>