feat: add update price modifier for existing customers based on last license date
Some checks failed
Staging Build / build (push) Has been cancelled
Some checks failed
Staging Build / build (push) Has been cancelled
This commit is contained in:
@@ -86,7 +86,8 @@ export function buildOrderSnapshot(
|
||||
products: Product[],
|
||||
categories: Category[],
|
||||
billingInterval?: 'one_time' | 'monthly',
|
||||
moduleQuantities?: Record<string, number>
|
||||
moduleQuantities?: Record<string, number>,
|
||||
lastLicenseDate?: string | null
|
||||
): OrderSnapshot {
|
||||
const items: OrderItem[] = []
|
||||
let subtotal = 0
|
||||
@@ -131,14 +132,43 @@ export function buildOrderSnapshot(
|
||||
})
|
||||
}
|
||||
|
||||
const taxAmount = Math.round(subtotal * (dominantTaxRate / 100) * 100) / 100
|
||||
const total = Math.round((subtotal + taxAmount) * 100) / 100
|
||||
|
||||
// Wenn billingInterval übergeben, dieses nutzen; sonst automatisch ermitteln
|
||||
const billingCycle = billingInterval || (items.every((i) => i.billing_interval === 'one_time')
|
||||
? 'one_time'
|
||||
: 'monthly')
|
||||
|
||||
let multiplier = 1
|
||||
if (lastLicenseDate && billingCycle === 'one_time') {
|
||||
const lastDate = new Date(lastLicenseDate)
|
||||
const today = new Date()
|
||||
const yearsDiff = today.getFullYear() - lastDate.getFullYear()
|
||||
const monthsDiff = today.getMonth() - lastDate.getMonth()
|
||||
const months = yearsDiff * 12 + monthsDiff
|
||||
|
||||
if (months <= 12) {
|
||||
multiplier = 0
|
||||
} else if (months <= 14) {
|
||||
multiplier = 0.15
|
||||
} else if (months <= 25) {
|
||||
multiplier = 0.30
|
||||
} else if (months <= 37) {
|
||||
multiplier = 0.45
|
||||
} else if (months <= 49) {
|
||||
multiplier = 0.60
|
||||
} else {
|
||||
multiplier = 0.90
|
||||
}
|
||||
|
||||
// Multiply item_total in items by multiplier
|
||||
items.forEach(item => {
|
||||
item.item_total = item.item_total * multiplier
|
||||
})
|
||||
subtotal = subtotal * multiplier
|
||||
}
|
||||
|
||||
const taxAmount = Math.round(subtotal * (dominantTaxRate / 100) * 100) / 100
|
||||
const total = Math.round((subtotal + taxAmount) * 100) / 100
|
||||
|
||||
return {
|
||||
schema_version: 1,
|
||||
billing_cycle: billingCycle,
|
||||
@@ -147,6 +177,8 @@ export function buildOrderSnapshot(
|
||||
tax_rate: dominantTaxRate,
|
||||
tax_amount: taxAmount,
|
||||
total,
|
||||
last_license_date: lastLicenseDate || null,
|
||||
price_multiplier: lastLicenseDate && billingCycle === 'one_time' ? multiplier : null,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user