fix(analytics): harden period-linked sales queries

This commit is contained in:
ogt
2026-07-22 17:59:10 +08:00
parent 9b677ae616
commit f5df7bb1eb
8 changed files with 1529 additions and 909 deletions

View File

@@ -159,8 +159,8 @@
};
window.exportMarketingExcel = function (campaign) {
const url = new URL('/api/export/excel/marketing', window.location.origin);
url.searchParams.set('campaign', campaign || 'all');
const url = new URL('/api/sales_analysis/export_marketing', window.location.origin);
url.searchParams.set('type', campaign || 'all');
new URLSearchParams(window.location.search).forEach((v, k) => {
if (!url.searchParams.has(k)) url.searchParams.set(k, v);
});
@@ -569,17 +569,21 @@
const discountCanvas = document.getElementById('mktDiscountChart');
const couponCanvas = document.getElementById('mktCouponChart');
if (data.marketingData) {
const marketingMetric = data.marketingData.metric || 'revenue';
const marketingFormatter = marketingMetric === 'qty' ? fmtNum : fmtMoney;
const marketingLabel = marketingMetric === 'qty' ? '活動銷量' :
(marketingMetric === 'profit' ? '活動毛利' : '活動業績');
if (data.marketingData.discount) {
horizontalBar(discountCanvas,
data.marketingData.discount.labels, data.marketingData.discount.values,
'折扣業績', fmtMoney);
marketingLabel, marketingFormatter);
} else {
renderChartEmpty(discountCanvas, '所選期間沒有折扣活動業績。');
}
if (data.marketingData.coupon) {
horizontalBar(couponCanvas,
data.marketingData.coupon.labels, data.marketingData.coupon.values,
'折價券業績', fmtMoney);
marketingLabel, marketingFormatter);
} else {
renderChartEmpty(couponCanvas, '所選期間沒有折價券活動業績。');
}
@@ -729,7 +733,7 @@
columns,
processing: true, serverSide: false,
pageLength: 25, lengthMenu: [10, 25, 50, 100],
order: [[$tbl.find('thead th').length - 1, 'desc']],
order: [],
language: typeof window.EwoooCDataTableLanguage === 'function'
? window.EwoooCDataTableLanguage()
: {},
@@ -761,6 +765,13 @@
}
};
document.addEventListener('click', (event) => {
const trigger = event.target.closest('[data-sales-filter-key]');
if (!trigger) return;
event.preventDefault();
window.setFilter(trigger.dataset.salesFilterKey, trigger.dataset.salesFilterValue);
});
// Show loading on form submit
document.addEventListener('submit', (e) => {
if (e.target.matches('form[action="/sales_analysis"]')) showLoading('正在查詢...');