source: frontend/src/pages/billing/BillingList.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 8.9 KB
Line 
1import React, { useState, useEffect } from 'react';
2import { Link, useSearchParams } from 'react-router-dom';
3import { billingService } from '../../services/billingService';
4import { patientService } from '../../services/patientService';
5import Loading from '../../components/Loading';
6import ErrorAlert from '../../components/ErrorAlert';
7import SuccessAlert from '../../components/SuccessAlert';
8
9function BillingList() {
10 const [billings, setBillings] = useState([]);
11 const [loading, setLoading] = useState(true);
12 const [error, setError] = useState(null);
13 const [success, setSuccess] = useState(null);
14 const [searchParams] = useSearchParams();
15 const patientId = searchParams.get('patientId');
16 const user = JSON.parse(localStorage.getItem('user') || '{}');
17
18 // Filters
19 const [statusFilter, setStatusFilter] = useState('');
20 const [patientFilter, setPatientFilter] = useState('');
21 const [patients, setPatients] = useState([]);
22 const [pageSize] = useState(50); // Limit to 50 records per page
23
24 useEffect(() => {
25 const fetchBillings = async () => {
26 try {
27 setLoading(true);
28 let response;
29 if (patientId) {
30 response = await billingService.getBillingHistoryForPatient(patientId);
31 } else if (user.role === 'PATIENT') {
32 response = await billingService.getBillingHistoryForPatient(user.patientId);
33 } else {
34 response = await billingService.getAllBillings();
35 }
36 setBillings(response.data || []);
37 } catch (err) {
38 setError('Failed to fetch billing records');
39 console.error(err);
40 } finally {
41 setLoading(false);
42 }
43 };
44
45 fetchBillings();
46 }, [patientId, user.role, user.patientId]);
47
48 // Load patients for filter dropdown (for billing admin)
49 useEffect(() => {
50 if (user.role === 'BILLING_ADMIN') {
51 const loadPatients = async () => {
52 try {
53 const response = await patientService.getAllPatients();
54 setPatients(response.data || []);
55 } catch (err) {
56 console.error('Failed to load patients:', err);
57 }
58 };
59 loadPatients();
60 }
61 }, [user.role]);
62
63 // Filter billings based on selected filters
64 const filteredBillings = billings.filter(billing => {
65 if (statusFilter && billing.paymentStatus !== statusFilter) {
66 return false;
67 }
68 if (patientFilter && billing.patientName !== patientFilter) {
69 return false;
70 }
71 return true;
72 }).slice(0, pageSize); // Limit to first 50 records for performance
73
74 const pendingBillings = billings.filter(b => b.paymentStatus === 'PENDING');
75
76 const handleUpdateStatus = async (billId, newStatus) => {
77 try {
78 const updateData = {
79 paymentStatus: newStatus
80 };
81 if (newStatus === 'PAID') {
82 updateData.paymentDate = new Date().toISOString().split('T')[0];
83 }
84
85 console.log('Updating billing status:', { billId, updateData });
86 await billingService.updatePaymentStatus(billId, updateData);
87 setSuccess(`Billing status updated to ${newStatus}`);
88
89 // Refresh the list
90 const response = user.role === 'BILLING_ADMIN'
91 ? await billingService.getAllBillings()
92 : await billingService.getBillingHistoryForPatient(patientId || user.patientId);
93 setBillings(response.data || []);
94 } catch (err) {
95 setError(`Failed to update billing status: ${err.response?.data?.error || err.message}`);
96 console.error('Update status error:', err);
97 }
98 };
99
100 // Prevent unauthorized access to billing
101 if (user.role === 'DOCTOR' || user.role === 'LAB_TECHNICIAN') {
102 return (
103 <div style={{ padding: '20px', textAlign: 'center' }}>
104 <h1 className="text-2xl font-bold" style={{ color: '#7c3aed', marginBottom: '10px' }}>Access Denied</h1>
105 <p style={{ color: 'var(--color-neutral-600)' }}>You do not have permission to access billing records.</p>
106 </div>
107 );
108 }
109
110 if (loading) return <Loading />;
111
112 return (
113 <div>
114 <h1 className="text-3xl font-bold mb-6" style={{ color: '#7c3aed' }}>{patientId ? 'Patient Billing History' : 'Billing Records'}</h1>
115
116 {error && <ErrorAlert message={error} onClose={() => setError(null)} />}
117 {success && <SuccessAlert message={success} onClose={() => setSuccess(null)} />}
118
119 {/* Filters for Billing Admin */}
120 {user.role === 'BILLING_ADMIN' && (
121 <div className="bg-white rounded-lg shadow p-6 mb-6">
122 <h2 className="text-lg font-bold mb-4">Filters</h2>
123 <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
124 <div>
125 <label className="block text-sm font-semibold mb-2">Status</label>
126 <select
127 value={statusFilter}
128 onChange={(e) => setStatusFilter(e.target.value)}
129 className="w-full px-4 py-2 border rounded-lg"
130 >
131 <option value="">All Statuses</option>
132 <option value="PENDING">Pending</option>
133 <option value="PAID">Paid</option>
134 <option value="PROCESSING">Processing</option>
135 </select>
136 </div>
137 <div>
138 <label className="block text-sm font-semibold mb-2">Patient</label>
139 <select
140 value={patientFilter}
141 onChange={(e) => setPatientFilter(e.target.value)}
142 className="w-full px-4 py-2 border rounded-lg"
143 >
144 <option value="">All Patients</option>
145 {patients.map(p => (
146 <option key={p.patientId} value={`${p.firstName} ${p.lastName}`}>
147 {p.firstName} {p.lastName}
148 </option>
149 ))}
150 </select>
151 </div>
152 </div>
153 </div>
154 )}
155
156 {/* Pending Billing Records Section (for Billing Admin) */}
157 {user.role === 'BILLING_ADMIN' && pendingBillings.length > 0 && (
158 <div className="bg-yellow-50 rounded-lg shadow p-6 mb-6 border-l-4 border-yellow-500">
159 <h2 className="text-lg font-bold mb-4">Pending Billing Records ({pendingBillings.length})</h2>
160 <div className="space-y-3">
161 {pendingBillings.map(billing => (
162 <div key={billing.billId} className="bg-white rounded p-4 flex justify-between items-center">
163 <div>
164 <p className="font-semibold">{billing.patientName}</p>
165 <p className="text-sm text-gray-600">${billing.totalCost}</p>
166 </div>
167 <button
168 onClick={() => handleUpdateStatus(billing.billId, 'PAID')}
169 className="px-4 py-2 bg-green-500 text-white rounded hover:bg-green-600 text-sm"
170 >
171 Mark Paid
172 </button>
173 </div>
174 ))}
175 </div>
176 </div>
177 )}
178
179 <div className="bg-white rounded-lg shadow overflow-hidden">
180 <table className="w-full">
181 <thead className="bg-gray-100">
182 <tr>
183 <th className="px-6 py-3 text-left text-sm font-semibold">Patient</th>
184 <th className="px-6 py-3 text-left text-sm font-semibold">Total Cost</th>
185 <th className="px-6 py-3 text-left text-sm font-semibold">Payment Status</th>
186 <th className="px-6 py-3 text-left text-sm font-semibold">Payment Date</th>
187 <th className="px-6 py-3 text-left text-sm font-semibold">Actions</th>
188 </tr>
189 </thead>
190 <tbody>
191 {filteredBillings.map(billing => (
192 <tr key={billing.billId} className="border-t hover:bg-gray-50">
193 <td className="px-6 py-3">{billing.patientName}</td>
194 <td className="px-6 py-3">${billing.totalCost}</td>
195 <td className="px-6 py-3">
196 <span className={`px-3 py-1 rounded text-sm font-semibold ${
197 billing.paymentStatus === 'PENDING' ? 'bg-yellow-100 text-yellow-800' :
198 billing.paymentStatus === 'PAID' ? 'bg-green-100 text-green-800' :
199 'bg-red-100 text-red-800'
200 }`}>
201 {billing.paymentStatus}
202 </span>
203 </td>
204 <td className="px-6 py-3">{billing.paymentDate || 'Not paid'}</td>
205 <td className="px-6 py-3">
206 <Link to={`/billing/${billing.billId}`} style={{ color: '#7c3aed', textDecoration: 'none', fontWeight: '400' }} onMouseEnter={(e) => e.currentTarget.style.textDecoration = 'underline'} onMouseLeave={(e) => e.currentTarget.style.textDecoration = 'none'}>
207 View
208 </Link>
209 </td>
210 </tr>
211 ))}
212 </tbody>
213 </table>
214
215 {filteredBillings.length === 0 && (
216 <div className="p-6 text-center text-gray-500">
217 {billings.length === 0 ? 'No billing records found' : 'No records match the selected filters'}
218 </div>
219 )}
220 </div>
221 </div>
222 );
223}
224
225export default BillingList;
Note: See TracBrowser for help on using the repository browser.