diff --git a/src/dealer-claim/components/request-creation/ClaimManagementWizard.tsx b/src/dealer-claim/components/request-creation/ClaimManagementWizard.tsx index 9bd3c5e..f215fec 100644 --- a/src/dealer-claim/components/request-creation/ClaimManagementWizard.tsx +++ b/src/dealer-claim/components/request-creation/ClaimManagementWizard.tsx @@ -1,5 +1,4 @@ -import { useState, useEffect, useRef } from 'react'; -import { useNavigate } from 'react-router-dom'; +import { useState, useRef } from 'react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; @@ -24,6 +23,7 @@ import { Info, FileText, Users, + XCircle, } from 'lucide-react'; import { format } from 'date-fns'; import { toast } from 'sonner'; @@ -72,11 +72,7 @@ const STEP_NAMES = [ export function ClaimManagementWizard({ onBack, onSubmit }: ClaimManagementWizardProps) { const { user } = useAuth(); - const navigate = useNavigate(); const [currentStep, setCurrentStep] = useState(1); - const [dealers, setDealers] = useState([]); - const [loadingDealers, setLoadingDealers] = useState(true); - const [searchTerm, setSearchTerm] = useState(''); const [verifyingDealer, setVerifyingDealer] = useState(false); const [dealerSearchResults, setDealerSearchResults] = useState([]); const [dealerSearchLoading, setDealerSearchLoading] = useState(false); @@ -114,23 +110,6 @@ export function ClaimManagementWizard({ onBack, onSubmit }: ClaimManagementWizar const totalSteps = STEP_NAMES.length; - // Fetch dealers from API on component mount - useEffect(() => { - const fetchDealers = async () => { - setLoadingDealers(true); - try { - const fetchedDealers = await fetchDealersFromAPI(undefined, 10); // Limit to 10 records - setDealers(fetchedDealers); - } catch (error) { - toast.error('Failed to load dealer list.'); - console.error('Error fetching dealers:', error); - } finally { - setLoadingDealers(false); - } - }; - fetchDealers(); - }, []); - // Handle dealer search input with debouncing const handleDealerSearchInputChange = (value: string) => { setDealerSearchInput(value); @@ -489,14 +468,11 @@ export function ClaimManagementWizard({ onBack, onSubmit }: ClaimManagementWizar )} -
+
{dealer.isLoggedIn ? ( - - - Logged In - + ) : ( - Not Logged In + )}
@@ -508,6 +484,18 @@ export function ClaimManagementWizard({ onBack, onSubmit }: ClaimManagementWizar )} +
+ Status: +
+ + Logged in +
+ +
+ + Not logged in +
+
{formData.dealerCode && (

@@ -528,10 +516,10 @@ export function ClaimManagementWizard({ onBack, onSubmit }: ClaimManagementWizar @@ -585,10 +573,10 @@ export function ClaimManagementWizard({ onBack, onSubmit }: ClaimManagementWizar @@ -610,11 +598,11 @@ export function ClaimManagementWizard({ onBack, onSubmit }: ClaimManagementWizar diff --git a/src/dealer-claim/components/request-detail/IOTab.tsx b/src/dealer-claim/components/request-detail/IOTab.tsx index 2fc07a6..7ce71c0 100644 --- a/src/dealer-claim/components/request-detail/IOTab.tsx +++ b/src/dealer-claim/components/request-detail/IOTab.tsx @@ -80,11 +80,36 @@ export function IOTab({ request, apiRequest, onRefresh }: IOTabProps) { // Only set blocked details if amount is blocked if (existingBlockedAmount > 0) { + const blockedAmt = Number(existingBlockedAmount) || 0; + const backendRemaining = Number(existingRemainingBalance) || 0; + + // Calculate expected remaining balance for validation/debugging + const expectedRemaining = availableBeforeBlock - blockedAmt; + + // Log for debugging backend calculation + console.log('[IOTab] Loading existing IO block:', { + availableBeforeBlock, + blockedAmount: blockedAmt, + expectedRemaining, + backendRemaining, + }); + + // Warn if remaining balance calculation seems incorrect (for backend debugging) + if (Math.abs(backendRemaining - expectedRemaining) > 0.01) { + console.warn('[IOTab] ⚠️ Remaining balance calculation issue detected!', { + availableBalance: availableBeforeBlock, + blockedAmount: blockedAmt, + expectedRemaining, + backendRemaining, + difference: backendRemaining - expectedRemaining, + }); + } + setBlockedDetails({ ioNumber: existingIONumber, - blockedAmount: Number(existingBlockedAmount) || 0, + blockedAmount: blockedAmt, availableBalance: availableBeforeBlock, // Available amount before block - remainingBalance: Number(existingRemainingBalance) || Number(existingAvailableBalance), + remainingBalance: backendRemaining, // Use backend calculated value blockedDate: internalOrder.organizedAt || internalOrder.organized_at || new Date().toISOString(), blockedBy: blockedByName, sapDocumentNumber: sapDocNumber, @@ -252,16 +277,20 @@ export function IOTab({ request, apiRequest, onRefresh }: IOTabProps) { if (updatedInternalOrder) { const savedBlockedAmount = Number(updatedInternalOrder.ioBlockedAmount || updatedInternalOrder.io_blocked_amount || blockAmount); - const savedRemainingBalance = Number(updatedInternalOrder.ioRemainingBalance || updatedInternalOrder.io_remaining_balance || (fetchedAmount - blockAmount)); + const savedRemainingBalance = Number(updatedInternalOrder.ioRemainingBalance || updatedInternalOrder.io_remaining_balance || 0); + + // Calculate expected remaining balance for validation/debugging + const expectedRemainingBalance = fetchedAmount - savedBlockedAmount; // Log what was saved vs what we sent console.log('[IOTab] Blocking result:', { sentAmount: blockAmount, savedBlockedAmount, - sentRemaining: fetchedAmount - blockAmount, - savedRemainingBalance, availableBalance: fetchedAmount, + expectedRemaining: expectedRemainingBalance, + backendRemaining: savedRemainingBalance, difference: savedBlockedAmount - blockAmount, + remainingDifference: fetchedAmount - savedRemainingBalance, }); // Warn if the saved amount differs from what we sent @@ -269,6 +298,17 @@ export function IOTab({ request, apiRequest, onRefresh }: IOTabProps) { console.warn('[IOTab] ⚠️ Amount mismatch! Sent:', blockAmount, 'Saved:', savedBlockedAmount); } + // Warn if remaining balance calculation seems incorrect (for backend debugging) + if (Math.abs(savedRemainingBalance - expectedRemainingBalance) > 0.01) { + console.warn('[IOTab] ⚠️ Remaining balance calculation issue detected!', { + availableBalance: fetchedAmount, + blockedAmount: savedBlockedAmount, + expectedRemaining: expectedRemainingBalance, + backendRemaining: savedRemainingBalance, + difference: savedRemainingBalance - expectedRemainingBalance, + }); + } + const currentUser = user as any; // When blocking, always use the current user who is performing the block action // The organizer association may be from initial IO organization, but we want who blocked the amount @@ -351,11 +391,8 @@ export function IOTab({ request, apiRequest, onRefresh }: IOTabProps) { {/* IO Remark Input */}

-