Tweak financial calculations

This commit is contained in:
2025-09-20 17:40:34 -04:00
parent 512b351429
commit 5d46a2a7e5
2 changed files with 193 additions and 131 deletions

View File

@@ -450,8 +450,9 @@ router.get('/financials', async (req, res) => {
grossSales: calculateComparison(totals.grossSales, previousTotals.grossSales),
refunds: calculateComparison(totals.refunds, previousTotals.refunds),
taxCollected: calculateComparison(totals.taxCollected, previousTotals.taxCollected),
discounts: calculateComparison(totals.discounts, previousTotals.discounts),
cogs: calculateComparison(totals.cogs, previousTotals.cogs),
netRevenue: calculateComparison(totals.netRevenue, previousTotals.netRevenue),
income: calculateComparison(totals.income, previousTotals.income),
profit: calculateComparison(totals.profit, previousTotals.profit),
margin: calculateComparison(totals.margin, previousTotals.margin),
};
@@ -706,10 +707,13 @@ function buildFinancialTotalsQuery(whereClause) {
SELECT
COALESCE(SUM(sale_amount), 0) as grossSales,
COALESCE(SUM(refund_amount), 0) as refunds,
COALESCE(SUM(shipping_collected_amount + small_order_fee_amount + rush_fee_amount), 0) as shippingFees,
COALESCE(SUM(tax_collected_amount), 0) as taxCollected,
COALESCE(SUM(discount_total_amount), 0) as discounts,
COALESCE(SUM(cogs_amount), 0) as cogs
FROM report_sales_data
WHERE ${whereClause}
AND action IN (1, 2, 3)
`;
}
@@ -719,10 +723,13 @@ function buildFinancialTrendQuery(whereClause) {
DATE(date_change) as date,
SUM(sale_amount) as grossSales,
SUM(refund_amount) as refunds,
SUM(shipping_collected_amount + small_order_fee_amount + rush_fee_amount) as shippingFees,
SUM(tax_collected_amount) as taxCollected,
SUM(discount_total_amount) as discounts,
SUM(cogs_amount) as cogs
FROM report_sales_data
WHERE ${whereClause}
AND action IN (1, 2, 3)
GROUP BY DATE(date_change)
ORDER BY date ASC
`;
@@ -731,20 +738,23 @@ function buildFinancialTrendQuery(whereClause) {
function normalizeFinancialTotals(row = {}) {
const grossSales = parseFloat(row.grossSales || 0);
const refunds = parseFloat(row.refunds || 0);
const shippingFees = parseFloat(row.shippingFees || 0);
const taxCollected = parseFloat(row.taxCollected || 0);
const discounts = parseFloat(row.discounts || 0);
const cogs = parseFloat(row.cogs || 0);
const netSales = grossSales - refunds;
const netRevenue = netSales - taxCollected;
const profit = netRevenue - cogs;
const margin = netRevenue !== 0 ? (profit / netRevenue) * 100 : 0;
const productNet = grossSales - refunds - discounts;
const income = productNet + shippingFees;
const profit = income - cogs;
const margin = income !== 0 ? (profit / income) * 100 : 0;
return {
grossSales,
refunds,
shippingFees,
taxCollected,
discounts,
cogs,
netSales,
netRevenue,
income,
profit,
margin,
};
@@ -753,12 +763,14 @@ function normalizeFinancialTotals(row = {}) {
function normalizeFinancialTrendRow(row = {}) {
const grossSales = parseFloat(row.grossSales || 0);
const refunds = parseFloat(row.refunds || 0);
const shippingFees = parseFloat(row.shippingFees || 0);
const taxCollected = parseFloat(row.taxCollected || 0);
const discounts = parseFloat(row.discounts || 0);
const cogs = parseFloat(row.cogs || 0);
const netSales = grossSales - refunds;
const netRevenue = netSales - taxCollected;
const profit = netRevenue - cogs;
const margin = netRevenue !== 0 ? (profit / netRevenue) * 100 : 0;
const productNet = grossSales - refunds - discounts;
const income = productNet + shippingFees;
const profit = income - cogs;
const margin = income !== 0 ? (profit / income) * 100 : 0;
let timestamp = null;
if (row.date instanceof Date) {
@@ -771,10 +783,11 @@ function normalizeFinancialTrendRow(row = {}) {
date: row.date,
grossSales,
refunds,
shippingFees,
taxCollected,
discounts,
cogs,
netSales,
netRevenue,
income,
profit,
margin,
timestamp,