| 1 |
/** |
| 2 |
* Tax Calculation Hook |
| 3 |
* |
| 4 |
* Provides tax calculation functionality for booking forms |
| 5 |
* |
| 6 |
* @package Yatra.Hooks |
| 7 |
* @since 3.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
import { useState, useEffect, useMemo } from "react"; |
| 11 |
import { taxService } from "../services/TaxService"; |
| 12 |
|
| 13 |
interface BookingTaxCalculation { |
| 14 |
subtotal: number; |
| 15 |
tax_amount: number; |
| 16 |
total_amount: number; |
| 17 |
tax_rate: number; |
| 18 |
tax_inclusive: boolean; |
| 19 |
taxes: Array<{ |
| 20 |
name: string; |
| 21 |
rate: number; |
| 22 |
amount: number; |
| 23 |
formatted_amount: string; |
| 24 |
formatted_rate: string; |
| 25 |
}>; |
| 26 |
tax_breakdown: string; |
| 27 |
} |
| 28 |
|
| 29 |
interface UseTaxCalculationOptions { |
| 30 |
subtotal: number; |
| 31 |
country?: string; |
| 32 |
enabled?: boolean; |
| 33 |
} |
| 34 |
|
| 35 |
interface UseTaxCalculationReturn { |
| 36 |
taxCalculation: BookingTaxCalculation | null; |
| 37 |
isLoading: boolean; |
| 38 |
error: string | null; |
| 39 |
isTaxEnabled: boolean; |
| 40 |
recalculateTax: () => void; |
| 41 |
} |
| 42 |
|
| 43 |
export const useTaxCalculation = ({ |
| 44 |
subtotal, |
| 45 |
country, |
| 46 |
enabled = true, |
| 47 |
}: UseTaxCalculationOptions): UseTaxCalculationReturn => { |
| 48 |
const [isLoading, setIsLoading] = useState(true); |
| 49 |
const [error, setError] = useState<string | null>(null); |
| 50 |
const [taxSettingsLoaded, setTaxSettingsLoaded] = useState(false); |
| 51 |
|
| 52 |
// Load tax settings |
| 53 |
useEffect(() => { |
| 54 |
const loadSettings = async () => { |
| 55 |
try { |
| 56 |
setIsLoading(true); |
| 57 |
await taxService.loadTaxSettings(); |
| 58 |
setTaxSettingsLoaded(true); |
| 59 |
|
| 60 |
// Validate tax configuration |
| 61 |
const validation = taxService.validateTaxConfiguration(); |
| 62 |
if (!validation.valid) { |
| 63 |
setError("Tax configuration error: " + validation.errors.join(", ")); |
| 64 |
} |
| 65 |
} catch (err) { |
| 66 |
setError("Failed to load tax settings"); |
| 67 |
} finally { |
| 68 |
setIsLoading(false); |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
loadSettings(); |
| 73 |
}, []); |
| 74 |
|
| 75 |
// Calculate tax when dependencies change |
| 76 |
const taxCalculation = useMemo(() => { |
| 77 |
if (!enabled || !taxSettingsLoaded || isLoading || error) { |
| 78 |
return null; |
| 79 |
} |
| 80 |
|
| 81 |
if (!taxService.isTaxEnabled()) { |
| 82 |
return null; |
| 83 |
} |
| 84 |
|
| 85 |
try { |
| 86 |
return taxService.calculateBookingTax(subtotal, country); |
| 87 |
} catch (err) { |
| 88 |
setError("Failed to calculate tax"); |
| 89 |
return null; |
| 90 |
} |
| 91 |
}, [subtotal, country, enabled, taxSettingsLoaded, isLoading, error]); |
| 92 |
|
| 93 |
const recalculateTax = () => { |
| 94 |
// Force recalculation by triggering a settings reload |
| 95 |
setTaxSettingsLoaded(false); |
| 96 |
taxService.loadTaxSettings().then(() => { |
| 97 |
setTaxSettingsLoaded(true); |
| 98 |
setError(null); |
| 99 |
}); |
| 100 |
}; |
| 101 |
|
| 102 |
return { |
| 103 |
taxCalculation, |
| 104 |
isLoading, |
| 105 |
error, |
| 106 |
isTaxEnabled: taxService.isTaxEnabled(), |
| 107 |
recalculateTax, |
| 108 |
}; |
| 109 |
}; |
| 110 |
|