This commit is contained in:
2026-04-28 16:12:14 +08:00
parent 69c66046b2
commit bb031d6a9f
5 changed files with 118 additions and 34 deletions

View File

@@ -7,6 +7,16 @@ export function safeTruncate(num, decimals = 2) {
return (scaled / factor).toFixed(decimals)
}
function toTruncatedCents(num) {
if (Number.isNaN(num) || !Number.isFinite(num))
return 0
return Math.trunc((num + Number.EPSILON) * 100)
}
function centsToFixed(cents) {
return (cents / 100).toFixed(2)
}
function calculatePlatformOverpricingCost(price, config) {
if (price <= config.p_pricing_standard)
return 0
@@ -37,10 +47,12 @@ export function calculatePromotionPricing(priceInput, config) {
const platformOverpricingCost = calculatePlatformOverpricingCost(price, config)
const superiorOverpricingCost = calculateSuperiorOverpricingCost(price, config)
const totalCost = baseCost + platformOverpricingCost + superiorOverpricingCost
const revenue = price - totalCost
const priceCents = toTruncatedCents(price)
const costCents = toTruncatedCents(totalCost)
const revenueCents = priceCents - costCents
return {
costPrice: safeTruncate(totalCost),
promotionRevenue: safeTruncate(revenue),
costPrice: centsToFixed(costCents),
promotionRevenue: centsToFixed(revenueCents),
}
}