This commit is contained in:
root
2026-01-03 06:06:54 +00:00
parent d138dae2ca
commit eeaa9a66bb
16 changed files with 728 additions and 348 deletions

View File

@@ -192,23 +192,35 @@ export async function POST(request: NextRequest) {
)
}
// Get points_to_chf setting
// Get points_to_eur setting (fallback to points_to_chf for backward compatibility)
const [settingsRows] = await connection.execute(
'SELECT setting_value FROM referral_settings WHERE setting_key = ?',
['points_to_chf']
'SELECT setting_key, setting_value FROM referral_settings WHERE setting_key IN (?, ?)',
['points_to_eur', 'points_to_chf']
)
const settings = settingsRows as any[]
const pointsToChf = parseFloat(settings[0]?.setting_value || '100')
// Calculate discount in CHF, then convert to user's currency
const discountChf = pointsToUse / pointsToChf
let pointsToEur = parseFloat(settings.find(s => s.setting_key === 'points_to_eur')?.setting_value || '0')
// Convert discount based on user's currency
// If points_to_eur not found, use points_to_chf and convert
if (pointsToEur === 0) {
const pointsToChf = parseFloat(settings.find(s => s.setting_key === 'points_to_chf')?.setting_value || '100')
// Convert CHF-based points to EUR-based (1 CHF ≈ 1.0309 EUR)
pointsToEur = pointsToChf / 1.030927835
}
if (pointsToEur === 0) {
pointsToEur = 100 // Default fallback
}
// Calculate discount in EUR first (universal base currency)
const discountEur = pointsToUse / pointsToEur
// Convert discount to user's currency
if (currency === 'CHF') {
pointsDiscount = discountChf
// Convert EUR to CHF (1 EUR = 0.97 CHF)
pointsDiscount = discountEur * 0.97
} else {
// Convert CHF to EUR (1 CHF ≈ 1.03 EUR)
pointsDiscount = discountChf * 1.03
// Already in EUR
pointsDiscount = discountEur
}
// Don't allow discount to exceed the product price (before shipping)