feat(domain): add income calculation with commission support
This commit is contained in:
+11
-5
@@ -1,6 +1,8 @@
|
||||
import { create } from "zustand"
|
||||
import { calculateOrderIncome } from "@/lib/domain/income"
|
||||
import { generateId } from "@/lib/id"
|
||||
import { mockTransactions, walletBalance } from "@/lib/mock"
|
||||
import { useShopStore } from "@/store/shops"
|
||||
import type { WalletTransaction } from "@/lib/types"
|
||||
|
||||
interface WalletState {
|
||||
@@ -10,7 +12,7 @@ interface WalletState {
|
||||
withdraw: (amount: number) => void
|
||||
deductBalance: (orderId: string, amount: number) => boolean
|
||||
refundPayment: (orderId: string, amount: number) => boolean
|
||||
addIncome: (orderId: string, amount: number) => void
|
||||
addIncome: (orderId: string, totalPrice: number, shopId?: string) => void
|
||||
addTransaction: (transaction: WalletTransaction) => void
|
||||
}
|
||||
|
||||
@@ -109,8 +111,8 @@ export const useWalletStore = create<WalletState>((set, get) => ({
|
||||
|
||||
return true
|
||||
},
|
||||
addIncome: (orderId, amount) => {
|
||||
if (!Number.isFinite(amount) || amount <= 0) return
|
||||
addIncome: (orderId, totalPrice, shopId) => {
|
||||
if (!Number.isFinite(totalPrice) || totalPrice <= 0) return
|
||||
|
||||
const state = get()
|
||||
const exists = state.transactions.some(
|
||||
@@ -118,15 +120,19 @@ export const useWalletStore = create<WalletState>((set, get) => ({
|
||||
)
|
||||
if (exists) return
|
||||
|
||||
const shop = shopId
|
||||
? useShopStore.getState().shops.find((item) => item.id === shopId)
|
||||
: undefined
|
||||
const { income, commissionLabel } = calculateOrderIncome(totalPrice, shop)
|
||||
|
||||
const now = new Date().toISOString()
|
||||
const income = Number((amount * 0.85).toFixed(2))
|
||||
set((prev) => ({
|
||||
transactions: [
|
||||
{
|
||||
id: generateId("tx"),
|
||||
type: "income",
|
||||
amount: income,
|
||||
description: `订单 ${orderId} 收入(扣除15%抽成)`,
|
||||
description: `订单 ${orderId} 收入(${commissionLabel})`,
|
||||
createdAt: now,
|
||||
},
|
||||
...prev.transactions,
|
||||
|
||||
Reference in New Issue
Block a user