feat(domain): add income calculation with commission support

This commit is contained in:
zetaloop
2026-02-23 11:04:48 +08:00
parent 77d23d0c9d
commit 2222dccbb7
3 changed files with 90 additions and 5 deletions
+33
View File
@@ -0,0 +1,33 @@
import type { Shop } from "@/lib/types"
type ShopCommission = Pick<Shop, "commissionType" | "commissionValue">
export interface IncomeCalculation {
income: number
commissionLabel: string
}
function roundCurrency(amount: number) {
return Number(amount.toFixed(2))
}
export function calculateOrderIncome(totalPrice: number, shop?: ShopCommission): IncomeCalculation {
if (!shop) {
return {
income: roundCurrency(totalPrice),
commissionLabel: "独立接单无抽成",
}
}
if (shop.commissionType === "percentage") {
return {
income: roundCurrency(totalPrice * (1 - shop.commissionValue / 100)),
commissionLabel: `扣除${shop.commissionValue}%抽成`,
}
}
return {
income: roundCurrency(Math.max(0, totalPrice - shop.commissionValue)),
commissionLabel: `扣除¥${shop.commissionValue}固定抽成`,
}
}