PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.15
Yatra – Travel Booking & Tour Operator Software v3.0.15
3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 All 83 releases
yatra / resources / js / hooks / useTaxCalculation.ts

useTaxCalculation.ts in Yatra – Travel Booking & Tour Operator Software 3.0.15, at resources/js/hooks/useTaxCalculation.ts

110 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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