-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchanges.patch
More file actions
2273 lines (2206 loc) · 94.8 KB
/
changes.patch
File metadata and controls
2273 lines (2206 loc) · 94.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/src/App.jsx b/src/App.jsx
index 897e3bf..fbbd23a 100644
--- a/src/App.jsx
+++ b/src/App.jsx
@@ -89,7 +89,7 @@ function App() {
case 'reports':
return <Reports />
case 'administration':
- return <Administration />
+ return <Administration userRole={user?.role} />
default:
return <Dashboard />
}
diff --git a/src/components/AddStaffModal.jsx b/src/components/AddStaffModal.jsx
index 835d2fb..cb0973b 100644
--- a/src/components/AddStaffModal.jsx
+++ b/src/components/AddStaffModal.jsx
@@ -146,8 +146,10 @@ function AddStaffModal({ onClose, onSuccess }) {
<option value="Doctor">Doctor</option>
<option value="Nurse">Nurse</option>
<option value="Receptionist">Receptionist</option>
- <option value="Manager">Manager</option>
</select>
+ <p className="text-xs text-gray-500 mt-1">
+ Note: Branch managers can only be created by administrators
+ </p>
</div>
<div>
<label className="block text-[11px] uppercase tracking-wider mb-2 font-semibold">
diff --git a/src/components/Administration.jsx b/src/components/Administration.jsx
index bf42d5c..8ada1a0 100644
--- a/src/components/Administration.jsx
+++ b/src/components/Administration.jsx
@@ -2,21 +2,28 @@ import { useState, useEffect } from 'react'
import { api, API_ENDPOINTS } from '../config/api'
import AddBranchModal from './AddBranchModal'
import AddStaffModal from './AddStaffModal'
+import AddBranchManagerModal from './AddBranchManagerModal'
-function Administration() {
- const [activeTab, setActiveTab] = useState('branches-tab')
+function Administration({ userRole = 'manager' }) {
+ // Set initial tab based on role - admin sees branches first, managers see staff first
+ const initialTab = userRole === 'admin' ? 'branches-tab' : 'staff-tab'
+ const [activeTab, setActiveTab] = useState(initialTab)
const [branches, setBranches] = useState([])
const [staff, setStaff] = useState([])
+ const [managers, setManagers] = useState([])
const [loading, setLoading] = useState(true)
const [error, setError] = useState(null)
const [isAddBranchModalOpen, setIsAddBranchModalOpen] = useState(false)
const [isAddStaffModalOpen, setIsAddStaffModalOpen] = useState(false)
+ const [isAddManagerModalOpen, setIsAddManagerModalOpen] = useState(false)
useEffect(() => {
if (activeTab === 'branches-tab') {
fetchBranches()
} else if (activeTab === 'staff-tab') {
fetchStaff()
+ } else if (activeTab === 'managers-tab') {
+ fetchManagers()
}
}, [activeTab])
@@ -48,6 +55,20 @@ function Administration() {
}
}
+ const fetchManagers = async () => {
+ setLoading(true)
+ setError(null)
+ try {
+ const data = await api.get(`${API_ENDPOINTS.STAFF}?role=manager`)
+ setManagers(data)
+ } catch (err) {
+ setError(err.message)
+ console.error('Error fetching managers:', err)
+ } finally {
+ setLoading(false)
+ }
+ }
+
const getStatusBadge = (status) => {
return (
<span className="inline-block px-2 py-1 text-[10px] font-semibold uppercase tracking-wider border border-black bg-black text-white">
@@ -62,16 +83,30 @@ function Administration() {
{/* Admin Tabs */}
<div className="flex border-b border-black mb-6">
- <div
- className={`px-6 py-3 cursor-pointer text-xs font-semibold uppercase tracking-wider border-b-[3px] -mb-px transition-all ${
- activeTab === 'branches-tab'
- ? 'border-b-black'
- : 'border-b-transparent hover:bg-gray-bg'
- }`}
- onClick={() => setActiveTab('branches-tab')}
- >
- Branches
- </div>
+ {userRole === 'admin' && (
+ <div
+ className={`px-6 py-3 cursor-pointer text-xs font-semibold uppercase tracking-wider border-b-[3px] -mb-px transition-all ${
+ activeTab === 'branches-tab'
+ ? 'border-b-black'
+ : 'border-b-transparent hover:bg-gray-bg'
+ }`}
+ onClick={() => setActiveTab('branches-tab')}
+ >
+ Branches
+ </div>
+ )}
+ {userRole === 'admin' && (
+ <div
+ className={`px-6 py-3 cursor-pointer text-xs font-semibold uppercase tracking-wider border-b-[3px] -mb-px transition-all ${
+ activeTab === 'managers-tab'
+ ? 'border-b-black'
+ : 'border-b-transparent hover:bg-gray-bg'
+ }`}
+ onClick={() => setActiveTab('managers-tab')}
+ >
+ Branch Managers
+ </div>
+ )}
<div
className={`px-6 py-3 cursor-pointer text-xs font-semibold uppercase tracking-wider border-b-[3px] -mb-px transition-all ${
activeTab === 'staff-tab'
@@ -246,6 +281,84 @@ function Administration() {
</div>
)}
+ {/* Branch Managers Tab (Admin Only) */}
+ {activeTab === 'managers-tab' && userRole === 'admin' && (
+ <div className="border border-black">
+ <div className="px-4 py-4 border-b border-black flex justify-between items-center bg-gray-bg">
+ <h3 className="text-sm font-semibold uppercase tracking-wider">Branch Manager Management</h3>
+ <button
+ onClick={() => setIsAddManagerModalOpen(true)}
+ className="px-4 py-2 border-2 border-crimson text-crimson text-[10px] font-semibold uppercase tracking-wider hover:bg-crimson hover:text-white transition-all"
+ >
+ Add Branch Manager
+ </button>
+ </div>
+
+ <table className="w-full border-collapse">
+ <thead>
+ <tr>
+ <th className="text-left px-4 py-3 text-[10px] uppercase tracking-wider border-b border-gray-light bg-white font-semibold">Manager ID</th>
+ <th className="text-left px-4 py-3 text-[10px] uppercase tracking-wider border-b border-gray-light bg-white font-semibold">Name</th>
+ <th className="text-left px-4 py-3 text-[10px] uppercase tracking-wider border-b border-gray-light bg-white font-semibold">Branch</th>
+ <th className="text-left px-4 py-3 text-[10px] uppercase tracking-wider border-b border-gray-light bg-white font-semibold">Phone</th>
+ <th className="text-left px-4 py-3 text-[10px] uppercase tracking-wider border-b border-gray-light bg-white font-semibold">Email</th>
+ <th className="text-left px-4 py-3 text-[10px] uppercase tracking-wider border-b border-gray-light bg-white font-semibold">Status</th>
+ <th className="text-left px-4 py-3 text-[10px] uppercase tracking-wider border-b border-gray-light bg-white font-semibold">Actions</th>
+ </tr>
+ </thead>
+ <tbody>
+ {loading ? (
+ <tr>
+ <td colSpan="7" className="px-4 py-8 text-center text-gray-mid">
+ Loading branch managers...
+ </td>
+ </tr>
+ ) : error ? (
+ <tr>
+ <td colSpan="7" className="px-4 py-4 text-center text-crimson">
+ {error}
+ </td>
+ </tr>
+ ) : managers.length === 0 ? (
+ <tr>
+ <td colSpan="7" className="px-4 py-8 text-center text-gray-mid">
+ No branch managers found
+ </td>
+ </tr>
+ ) : (
+ managers.map((manager, index) => (
+ <tr key={manager.id} className="hover:bg-gray-bg transition-colors">
+ <td className={`px-4 py-4 text-[13px] ${index !== managers.length - 1 ? 'border-b border-gray-light' : ''}`}>
+ MGR-{manager.id.toString().padStart(3, '0')}
+ </td>
+ <td className={`px-4 py-4 text-[13px] ${index !== managers.length - 1 ? 'border-b border-gray-light' : ''}`}>
+ {manager.first_name} {manager.last_name}
+ </td>
+ <td className={`px-4 py-4 text-[13px] ${index !== managers.length - 1 ? 'border-b border-gray-light' : ''}`}>
+ {manager.branch_name || '-'}
+ </td>
+ <td className={`px-4 py-4 text-[13px] ${index !== managers.length - 1 ? 'border-b border-gray-light' : ''}`}>
+ {manager.phone || '-'}
+ </td>
+ <td className={`px-4 py-4 text-[13px] ${index !== managers.length - 1 ? 'border-b border-gray-light' : ''}`}>
+ {manager.email || '-'}
+ </td>
+ <td className={`px-4 py-4 ${index !== managers.length - 1 ? 'border-b border-gray-light' : ''}`}>
+ {getStatusBadge(manager.is_active ? 'active' : 'inactive')}
+ </td>
+ <td className={`px-4 py-4 ${index !== managers.length - 1 ? 'border-b border-gray-light' : ''}`}>
+ <button className="px-4 py-2 border-2 border-black bg-white text-black text-[10px] font-semibold uppercase tracking-wider hover:bg-black hover:text-white transition-all">
+ Edit
+ </button>
+ </td>
+ </tr>
+ ))
+ )}
+ </tbody>
+ </table>
+ </div>
+ )}
+
{/* Modals */}
{isAddBranchModalOpen && (
<AddBranchModal
@@ -261,6 +374,13 @@ function Administration() {
/>
)}
+ {isAddManagerModalOpen && (
+ <AddBranchManagerModal
+ onClose={() => setIsAddManagerModalOpen(false)}
+ onSuccess={fetchManagers}
+ />
+ )}
+
{/* Settings Tab */}
{activeTab === 'settings-tab' && (
<div className="max-w-[600px]">
diff --git a/src/components/AppointmentDetailModal.jsx b/src/components/AppointmentDetailModal.jsx
index 843517a..9425cae 100644
--- a/src/components/AppointmentDetailModal.jsx
+++ b/src/components/AppointmentDetailModal.jsx
@@ -1,15 +1,26 @@
import { useState, useEffect } from 'react'
import { api, API_ENDPOINTS } from '../config/api'
+import CancelAppointmentModal from './CancelAppointmentModal'
+import NoShowModal from './NoShowModal'
+import TreatmentRecordModal from './TreatmentRecordModal'
+import CompleteAppointmentModal from './CompleteAppointmentModal'
function AppointmentDetailModal({ appointmentId, onClose, onSuccess }) {
const [appointment, setAppointment] = useState(null)
const [loading, setLoading] = useState(true)
const [error, setError] = useState(null)
const [updating, setUpdating] = useState(false)
+ const [treatmentRecords, setTreatmentRecords] = useState([])
+ const [isCancelModalOpen, setIsCancelModalOpen] = useState(false)
+ const [isNoShowModalOpen, setIsNoShowModalOpen] = useState(false)
+ const [isTreatmentModalOpen, setIsTreatmentModalOpen] = useState(false)
+ const [isCompleteModalOpen, setIsCompleteModalOpen] = useState(false)
+ const [invoice, setInvoice] = useState(null)
useEffect(() => {
if (appointmentId) {
fetchAppointmentDetails()
+ fetchTreatmentRecords()
}
}, [appointmentId])
@@ -17,6 +28,11 @@ function AppointmentDetailModal({ appointmentId, onClose, onSuccess }) {
try {
const data = await api.get(API_ENDPOINTS.APPOINTMENT_BY_ID(appointmentId))
setAppointment(data)
+
+ // Fetch invoice if appointment is completed
+ if (data.status === 'Completed') {
+ fetchInvoice()
+ }
} catch (err) {
setError(err.message)
console.error('Error fetching appointment details:', err)
@@ -25,6 +41,26 @@ function AppointmentDetailModal({ appointmentId, onClose, onSuccess }) {
}
}
+ const fetchTreatmentRecords = async () => {
+ try {
+ const data = await api.get(API_ENDPOINTS.APPOINTMENT_TREATMENTS(appointmentId))
+ setTreatmentRecords(data)
+ } catch (err) {
+ console.error('Error fetching treatment records:', err)
+ }
+ }
+
+ const fetchInvoice = async () => {
+ try {
+ const invoices = await api.get(`${API_ENDPOINTS.INVOICES}?appointment_id=${appointmentId}`)
+ if (invoices && invoices.length > 0) {
+ setInvoice(invoices[0])
+ }
+ } catch (err) {
+ console.error('Error fetching invoice:', err)
+ }
+ }
+
const handleStatusChange = async (newStatus) => {
setUpdating(true)
setError(null)
@@ -45,6 +81,86 @@ function AppointmentDetailModal({ appointmentId, onClose, onSuccess }) {
}
}
+ const handleCancelAppointment = async (cancellationReason) => {
+ setUpdating(true)
+ try {
+ await api.put(API_ENDPOINTS.APPOINTMENT_CANCEL(appointmentId), {
+ cancellation_reason: cancellationReason
+ })
+ await fetchAppointmentDetails()
+ if (onSuccess) onSuccess()
+ setIsCancelModalOpen(false)
+ } catch (err) {
+ setError(err.message)
+ console.error('Error cancelling appointment:', err)
+ throw err
+ } finally {
+ setUpdating(false)
+ }
+ }
+
+ const handleMarkNoShow = async (notes) => {
+ setUpdating(true)
+ try {
+ await api.put(API_ENDPOINTS.APPOINTMENT_NO_SHOW(appointmentId), {
+ notes: notes
+ })
+ await fetchAppointmentDetails()
+ if (onSuccess) onSuccess()
+ setIsNoShowModalOpen(false)
+ } catch (err) {
+ setError(err.message)
+ console.error('Error marking no-show:', err)
+ throw err
+ } finally {
+ setUpdating(false)
+ }
+ }
+
+ const handleTreatmentSuccess = () => {
+ fetchTreatmentRecords()
+ setIsTreatmentModalOpen(false)
+ }
+
+ const handleCompleteAppointment = async () => {
+ setUpdating(true)
+ try {
+ const response = await api.post(API_ENDPOINTS.APPOINTMENT_COMPLETE(appointmentId))
+ await fetchAppointmentDetails()
+ await fetchTreatmentRecords()
+ setInvoice(response.invoice)
+ if (onSuccess) onSuccess()
+ setIsCompleteModalOpen(false)
+ } catch (err) {
+ setError(err.message)
+ console.error('Error completing appointment:', err)
+ throw err
+ } finally {
+ setUpdating(false)
+ }
+ }
+
+ const handleDeleteTreatment = async (treatmentRecordId) => {
+ if (!window.confirm('Are you sure you want to remove this treatment?')) {
+ return
+ }
+
+ try {
+ await api.delete(API_ENDPOINTS.APPOINTMENT_TREATMENT_RECORD(appointmentId, treatmentRecordId))
+ fetchTreatmentRecords()
+ } catch (err) {
+ setError(err.message)
+ console.error('Error deleting treatment:', err)
+ }
+ }
+
+ const handleViewInvoice = () => {
+ if (invoice) {
+ // Open invoice modal or navigate to invoice
+ window.open(`#/invoices/${invoice.id}`, '_blank')
+ }
+ }
+
const handleBackdropClick = (e) => {
if (e.target === e.currentTarget) {
onClose()
@@ -232,40 +348,203 @@ function AppointmentDetailModal({ appointmentId, onClose, onSuccess }) {
</div>
)}
+ {/* Treatment Records Section */}
+ {appointment.status === 'In Progress' && (
+ <div className="mb-6">
+ <div className="flex justify-between items-center mb-4 border-b border-gray-light pb-2">
+ <h3 className="text-sm font-semibold uppercase tracking-wider">
+ Recorded Treatments
+ </h3>
+ <button
+ onClick={() => setIsTreatmentModalOpen(true)}
+ disabled={updating}
+ className="px-4 py-2 border-2 border-purple text-purple text-[10px] font-semibold uppercase tracking-wider hover:bg-purple hover:text-white transition-all disabled:opacity-50"
+ >
+ Record Treatments
+ </button>
+ </div>
+
+ {treatmentRecords.length === 0 ? (
+ <div className="p-4 border border-gray-light bg-gray-bg text-center">
+ <p className="text-sm text-gray-mid">No treatments recorded yet</p>
+ <p className="text-xs text-gray-mid mt-1">Click "Record Treatments" to add treatments</p>
+ </div>
+ ) : (
+ <div className="space-y-3">
+ {treatmentRecords.map(record => (
+ <div key={record.id} className="p-3 border border-gray-light bg-gray-bg">
+ <div className="flex justify-between items-start">
+ <div className="flex-1">
+ <p className="text-sm font-semibold">{record.treatment_name}</p>
+ <p className="text-xs text-gray-mid">{record.category_name}</p>
+ {record.consultation_notes && (
+ <p className="text-xs text-gray-mid mt-1 italic">
+ Note: {record.consultation_notes}
+ </p>
+ )}
+ </div>
+ <div className="text-right ml-3">
+ <p className="text-xs text-gray-mid">
+ {record.quantity} × LKR {parseFloat(record.unit_price).toFixed(2)}
+ </p>
+ <p className="text-sm font-semibold">
+ LKR {parseFloat(record.total_price).toFixed(2)}
+ </p>
+ </div>
+ </div>
+ <div className="mt-2 pt-2 border-t border-gray-light">
+ <button
+ onClick={() => handleDeleteTreatment(record.id)}
+ className="text-xs text-crimson hover:underline font-semibold"
+ >
+ Remove
+ </button>
+ </div>
+ </div>
+ ))}
+
+ {/* Running Total */}
+ <div className="p-3 border-2 border-black bg-black text-white">
+ <div className="flex justify-between items-center">
+ <span className="text-xs font-semibold uppercase tracking-wider">Total</span>
+ <span className="text-lg font-bold">
+ LKR {treatmentRecords.reduce((sum, r) => sum + parseFloat(r.total_price), 0).toFixed(2)}
+ </span>
+ </div>
+ </div>
+
+ {/* Complete Consultation Button */}
+ <button
+ onClick={() => setIsCompleteModalOpen(true)}
+ disabled={updating}
+ className="w-full px-6 py-3 border-2 border-black bg-black text-white text-xs font-semibold uppercase tracking-wider hover:bg-white hover:text-black transition-all disabled:opacity-50"
+ >
+ Complete Consultation
+ </button>
+ </div>
+ )}
+ </div>
+ )}
+
+ {/* Completed Appointment - Show Invoice Info */}
+ {appointment.status === 'Completed' && (
+ <div className="mb-6">
+ <h3 className="text-sm font-semibold uppercase tracking-wider mb-4 border-b border-gray-light pb-2">
+ Invoice Information
+ </h3>
+
+ {invoice ? (
+ <div className="space-y-4">
+ <div className="p-4 border border-black bg-gray-bg">
+ <div className="grid grid-cols-2 gap-4 mb-3">
+ <div>
+ <label className="block text-[10px] uppercase tracking-wider text-gray-mid mb-1">
+ Invoice Number
+ </label>
+ <p className="text-sm font-semibold">{invoice.invoice_number}</p>
+ </div>
+ <div>
+ <label className="block text-[10px] uppercase tracking-wider text-gray-mid mb-1">
+ Total Amount
+ </label>
+ <p className="text-sm font-semibold">LKR {parseFloat(invoice.total_amount).toFixed(2)}</p>
+ </div>
+ <div>
+ <label className="block text-[10px] uppercase tracking-wider text-gray-mid mb-1">
+ Status
+ </label>
+ <span className="inline-block px-2 py-1 text-[10px] font-semibold uppercase tracking-wider border border-gray-mid text-gray-mid">
+ {invoice.status}
+ </span>
+ </div>
+ <div>
+ <label className="block text-[10px] uppercase tracking-wider text-gray-mid mb-1">
+ Created
+ </label>
+ <p className="text-sm">{new Date(invoice.created_at).toLocaleDateString()}</p>
+ </div>
+ </div>
+ <button
+ onClick={handleViewInvoice}
+ className="w-full px-4 py-2 border-2 border-purple text-purple text-[10px] font-semibold uppercase tracking-wider hover:bg-purple hover:text-white transition-all"
+ >
+ View Invoice
+ </button>
+ </div>
+
+ {/* Treatment Summary (Read-only) */}
+ {treatmentRecords.length > 0 && (
+ <div>
+ <h4 className="text-xs font-semibold uppercase tracking-wider mb-3">Treatment Summary</h4>
+ <div className="space-y-2">
+ {treatmentRecords.map(record => (
+ <div key={record.id} className="p-2 border border-gray-light bg-white text-xs">
+ <div className="flex justify-between">
+ <span>{record.treatment_name}</span>
+ <span className="font-semibold">
+ {record.quantity} × LKR {parseFloat(record.unit_price).toFixed(2)}
+ </span>
+ </div>
+ </div>
+ ))}
+ </div>
+ </div>
+ )}
+ </div>
+ ) : (
+ <p className="text-sm text-gray-mid">Loading invoice information...</p>
+ )}
+ </div>
+ )}
+
{/* Status Actions */}
- {appointment.status !== 'Completed' && appointment.status !== 'Cancelled' && (
+ {appointment.status !== 'Completed' && appointment.status !== 'Cancelled' && appointment.status !== 'No Show' && (
<div className="mb-6 p-4 border border-gray-light bg-gray-bg">
<h3 className="text-xs font-semibold uppercase tracking-wider mb-3">
Update Status
</h3>
- <div className="flex gap-2">
+ <div className="flex gap-2 flex-wrap">
{appointment.status === 'Scheduled' && (
- <button
- onClick={() => handleStatusChange('Confirmed')}
- disabled={updating}
- className="px-4 py-2 border-2 border-blue-500 text-blue-500 text-[10px] font-semibold uppercase tracking-wider hover:bg-blue-500 hover:text-white transition-all disabled:opacity-50"
- >
- Confirm
- </button>
+ <>
+ <button
+ onClick={() => handleStatusChange('Confirmed')}
+ disabled={updating}
+ className="px-4 py-2 border-2 border-blue-500 text-blue-500 text-[10px] font-semibold uppercase tracking-wider hover:bg-blue-500 hover:text-white transition-all disabled:opacity-50"
+ >
+ Confirm
+ </button>
+ <button
+ onClick={() => handleStatusChange('In Progress')}
+ disabled={updating}
+ className="px-4 py-2 border-2 border-purple text-purple text-[10px] font-semibold uppercase tracking-wider hover:bg-purple hover:text-white transition-all disabled:opacity-50"
+ >
+ Start Consultation
+ </button>
+ </>
)}
- {(appointment.status === 'Confirmed' || appointment.status === 'In Progress') && (
+ {appointment.status === 'Confirmed' && (
<button
- onClick={() => handleStatusChange('Completed')}
+ onClick={() => handleStatusChange('In Progress')}
disabled={updating}
- className="px-4 py-2 border-2 border-black bg-black text-white text-[10px] font-semibold uppercase tracking-wider hover:bg-white hover:text-black transition-all disabled:opacity-50"
+ className="px-4 py-2 border-2 border-purple text-purple text-[10px] font-semibold uppercase tracking-wider hover:bg-purple hover:text-white transition-all disabled:opacity-50"
>
- Mark Complete
- </button>
- )}
- {appointment.status !== 'In Progress' && (
- <button
- onClick={() => handleStatusChange('Cancelled')}
- disabled={updating}
- className="px-4 py-2 border-2 border-crimson text-crimson text-[10px] font-semibold uppercase tracking-wider hover:bg-crimson hover:text-white transition-all disabled:opacity-50"
- >
- Cancel
+ Start Consultation
</button>
)}
+ <button
+ onClick={() => setIsCancelModalOpen(true)}
+ disabled={updating}
+ className="px-4 py-2 border-2 border-crimson text-crimson text-[10px] font-semibold uppercase tracking-wider hover:bg-crimson hover:text-white transition-all disabled:opacity-50"
+ >
+ Cancel Appointment
+ </button>
+ <button
+ onClick={() => setIsNoShowModalOpen(true)}
+ disabled={updating}
+ className="px-4 py-2 border-2 border-gray-400 text-gray-400 text-[10px] font-semibold uppercase tracking-wider hover:bg-gray-400 hover:text-white transition-all disabled:opacity-50"
+ >
+ Mark No Show
+ </button>
</div>
</div>
)}
@@ -282,6 +561,45 @@ function AppointmentDetailModal({ appointmentId, onClose, onSuccess }) {
</button>
</div>
</div>
+
+ {/* Cancel Appointment Modal */}
+ {isCancelModalOpen && (
+ <CancelAppointmentModal
+ onClose={() => setIsCancelModalOpen(false)}
+ onConfirm={handleCancelAppointment}
+ />
+ )}
+
+ {/* No Show Modal */}
+ {isNoShowModalOpen && (
+ <NoShowModal
+ onClose={() => setIsNoShowModalOpen(false)}
+ onConfirm={handleMarkNoShow}
+ />
+ )}
+
+ {/* Treatment Record Modal */}
+ {isTreatmentModalOpen && (
+ <TreatmentRecordModal
+ appointmentId={appointmentId}
+ onClose={() => setIsTreatmentModalOpen(false)}
+ onSuccess={handleTreatmentSuccess}
+ />
+ )}
+
+ {/* Complete Appointment Modal */}
+ {isCompleteModalOpen && (
+ <CompleteAppointmentModal
+ appointmentId={appointmentId}
+ treatmentRecords={treatmentRecords}
+ onClose={() => setIsCompleteModalOpen(false)}
+ onConfirm={handleCompleteAppointment}
+ onBack={() => {
+ setIsCompleteModalOpen(false)
+ setIsTreatmentModalOpen(true)
+ }}
+ />
+ )}
</div>
)
}
diff --git a/src/components/Billing.jsx b/src/components/Billing.jsx
index b168cfe..50cbbd8 100644
--- a/src/components/Billing.jsx
+++ b/src/components/Billing.jsx
@@ -20,12 +20,32 @@ function Billing() {
// Filters
const [statusFilter, setStatusFilter] = useState('')
const [patientFilter, setPatientFilter] = useState('')
+ const [markingOverdue, setMarkingOverdue] = useState(false)
useEffect(() => {
fetchBillingStats()
fetchInvoices()
}, [statusFilter, patientFilter])
+ const handleMarkOverdue = async () => {
+ if (!window.confirm('Mark all eligible invoices (Sent > 30 days) as Overdue?')) {
+ return
+ }
+
+ setMarkingOverdue(true)
+ try {
+ const response = await api.post(API_ENDPOINTS.INVOICE_MARK_OVERDUE)
+ alert(response.message || 'Invoices marked as overdue')
+ fetchInvoices()
+ fetchBillingStats()
+ } catch (err) {
+ setError(err.message)
+ console.error('Error marking overdue:', err)
+ } finally {
+ setMarkingOverdue(false)
+ }
+ }
+
const fetchBillingStats = async () => {
try {
const data = await api.get(API_ENDPOINTS.DASHBOARD_STATS)
@@ -125,6 +145,34 @@ function Billing() {
</div>
</div>
+ {/* Filters */}
+ <div className="flex gap-4 mb-6 p-4 bg-gray-bg border border-gray-light">
+ <div className="flex-1">
+ <label className="block text-[10px] uppercase mb-1 font-semibold">Status Filter</label>
+ <select
+ value={statusFilter}
+ onChange={(e) => setStatusFilter(e.target.value)}
+ className="w-full px-2 py-2 border border-black text-xs focus:outline-2 focus:outline-black focus:outline-offset-2"
+ >
+ <option value="">All Statuses</option>
+ <option value="Draft">Draft</option>
+ <option value="Sent">Sent</option>
+ <option value="Paid">Paid</option>
+ <option value="Overdue">Overdue</option>
+ <option value="Cancelled">Cancelled</option>
+ </select>
+ </div>
+ <div className="flex items-end">
+ <button
+ onClick={handleMarkOverdue}
+ disabled={markingOverdue}
+ className="px-4 py-2 border-2 border-crimson text-crimson text-[10px] font-semibold uppercase tracking-wider hover:bg-crimson hover:text-white transition-all disabled:opacity-50"
+ >
+ {markingOverdue ? 'Processing...' : 'Mark Overdue'}
+ </button>
+ </div>
+ </div>
+
{/* Recent Invoices */}
<div className="border border-black mb-6">
<div className="px-4 py-4 border-b border-black flex justify-between items-center bg-gray-bg">
@@ -172,10 +220,11 @@ function Billing() {
invoices.map((invoice, index) => {
const totalPaid = invoice.payments?.reduce((sum, p) => sum + parseFloat(p.amount), 0) || 0
const outstanding = parseFloat(invoice.total_amount) - totalPaid
+ const isOverdue = invoice.status === 'Overdue'
return (
- <tr key={invoice.id} className="hover:bg-gray-bg transition-colors">
- <td className={`px-4 py-4 text-[13px] ${index !== invoices.length - 1 ? 'border-b border-gray-light' : ''}`}>
+ <tr key={invoice.id} className={`hover:bg-gray-bg transition-colors ${isOverdue ? 'bg-crimson/5' : ''}`}>
+ <td className={`px-4 py-4 text-[13px] ${isOverdue ? 'font-semibold' : ''} ${index !== invoices.length - 1 ? 'border-b border-gray-light' : ''}`}>
{invoice.invoice_number}
</td>
<td className={`px-4 py-4 text-[13px] ${index !== invoices.length - 1 ? 'border-b border-gray-light' : ''}`}>
@@ -316,6 +365,10 @@ function Billing() {
setIsInvoiceModalOpen(false)
setIsPaymentModalOpen(true)
}}
+ onSuccess={() => {
+ fetchInvoices()
+ fetchBillingStats()
+ }}
/>
)}
</>
diff --git a/src/components/BookAppointmentModal.jsx b/src/components/BookAppointmentModal.jsx
index 4b6db87..7cb3284 100644
--- a/src/components/BookAppointmentModal.jsx
+++ b/src/components/BookAppointmentModal.jsx
@@ -1,15 +1,23 @@
import { useState, useEffect } from 'react'
import { api, API_ENDPOINTS } from '../config/api'
-function BookAppointmentModal({ onClose, onSuccess }) {
+function BookAppointmentModal({ onClose, onSuccess, preSelectedPatientId }) {
const [patients, setPatients] = useState([])
+ const [patientSearchResults, setPatientSearchResults] = useState([])
+ const [patientSearchTerm, setPatientSearchTerm] = useState('')
+ const [isSearching, setIsSearching] = useState(false)
+ const [showSearchResults, setShowSearchResults] = useState(false)
const [branches, setBranches] = useState([])
const [doctors, setDoctors] = useState([])
+ const [allDoctors, setAllDoctors] = useState([])
+ const [specialties, setSpecialties] = useState([])
+ const [selectedSpecialty, setSelectedSpecialty] = useState('')
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
+ const [validationErrors, setValidationErrors] = useState({})
const [formData, setFormData] = useState({
- patient_id: '',
+ patient_id: preSelectedPatientId || '',
branch_id: '',
doctor_id: '',
appointment_datetime: '',
@@ -18,11 +26,27 @@ function BookAppointmentModal({ onClose, onSuccess }) {
})
useEffect(() => {
- fetchPatients()
+ if (preSelectedPatientId) {
+ fetchPatientById(preSelectedPatientId)
+ }
fetchBranches()
fetchDoctors()
+ fetchSpecialties()
}, [])
+ useEffect(() => {
+ // Debounced patient search
+ if (patientSearchTerm.length >= 2) {
+ const timer = setTimeout(() => {
+ performPatientSearch()
+ }, 300)
+ return () => clearTimeout(timer)
+ } else {
+ setPatientSearchResults([])
+ setShowSearchResults(false)
+ }
+ }, [patientSearchTerm])
+
const fetchPatients = async () => {
try {
const data = await api.get(API_ENDPOINTS.PATIENTS)
@@ -32,6 +56,35 @@ function BookAppointmentModal({ onClose, onSuccess }) {
}
}
+ const fetchPatientById = async (id) => {
+ try {
+ const data = await api.get(API_ENDPOINTS.PATIENT_BY_ID(id))
+ setPatientSearchTerm(`${data.first_name} ${data.last_name} - ${data.phone}`)
+ } catch (err) {
+ console.error('Error fetching patient:', err)
+ }
+ }
+
+ const performPatientSearch = async () => {
+ setIsSearching(true)
+ try {
+ const data = await api.get(API_ENDPOINTS.PATIENT_SEARCH(patientSearchTerm))
+ setPatientSearchResults(data)
+ setShowSearchResults(true)
+ } catch (err) {
+ console.error('Error searching patients:', err)
+ } finally {
+ setIsSearching(false)
+ }
+ }
+
+ const handleSelectPatient = (patient) => {
+ setFormData(prev => ({ ...prev, patient_id: patient.id }))
+ setPatientSearchTerm(`${patient.first_name} ${patient.last_name} - ${patient.phone}`)
+ setShowSearchResults(false)
+ setValidationErrors(prev => ({ ...prev, patient: null }))
+ }
+
const fetchBranches = async () => {
try {
const data = await api.get(API_ENDPOINTS.BRANCHES)
@@ -45,11 +98,41 @@ function BookAppointmentModal({ onClose, onSuccess }) {
try {
const data = await api.get(`${API_ENDPOINTS.STAFF}?role=Doctor&is_active=true`)
setDoctors(data)
+ setAllDoctors(data)
} catch (err) {
console.error('Error fetching doctors:', err)
}
}
+ const fetchSpecialties = async () => {
+ try {
+ const data = await api.get(API_ENDPOINTS.SPECIALTIES)
+ setSpecialties(data)
+ } catch (err) {
+ console.error('Error fetching specialties:', err)
+ }
+ }
+
+ const handleSpecialtyFilter = (specialtyId) => {
+ setSelectedSpecialty(specialtyId)
+ if (specialtyId) {
+ const filtered = allDoctors.filter(doc => doc.specialty_id === parseInt(specialtyId))
+ setDoctors(filtered)
+ } else {
+ setDoctors(allDoctors)
+ }
+ // Reset doctor selection if current doctor doesn't match filter
+ if (formData.doctor_id) {
+ const doctorStillAvailable = (specialtyId ?
+ allDoctors.find(d => d.id === parseInt(formData.doctor_id) && d.specialty_id === parseInt(specialtyId)) :
+ true
+ )
+ if (!doctorStillAvailable) {
+ setFormData(prev => ({ ...prev, doctor_id: '' }))
+ }
+ }
+ }
+
const handleBackdropClick = (e) => {
if (e.target === e.currentTarget) {
onClose()
@@ -64,6 +147,7 @@ function BookAppointmentModal({ onClose, onSuccess }) {
const handleConfirm = async () => {
setLoading(true)
setError(null)
+ setValidationErrors({})
try {
if (!formData.patient_id || !formData.branch_id || !formData.doctor_id || !formData.appointment_datetime) {
@@ -74,6 +158,11 @@ function BookAppointmentModal({ onClose, onSuccess }) {
if (onSuccess) onSuccess()
onClose()
} catch (err) {
+ // Handle specific validation errors from backend
+ if (err.response?.data?.validation) {
+ const validationType = err.response.data.validation
+ setValidationErrors({ [validationType]: err.response.data.error || err.message })
+ }
setError(err.message)
console.error('Error booking appointment:', err)
} finally {
@@ -108,24 +197,46 @@ function BookAppointmentModal({ onClose, onSuccess }) {
{/* Form Fields */}
<div className="space-y-6">
+ {/* Patient Search */}
<div>
<label className="block text-[11px] uppercase tracking-wider mb-2 font-semibold">
- Select Patient *
+ Search Patient *
</label>
- <select
- name="patient_id"
- value={formData.patient_id}
- onChange={handleChange}
- required
- className="w-full px-3 py-3 border border-black bg-white text-sm focus:outline-2 focus:outline-black focus:outline-offset-2"
- >
- <option value="">Select a patient...</option>
- {patients.map(patient => (
- <option key={patient.id} value={patient.id}>
- {patient.first_name} {patient.last_name} - {patient.phone}
- </option>
- ))}
- </select>
+ <div className="relative">
+ <input
+ type="text"
+ value={patientSearchTerm}
+ onChange={(e) => setPatientSearchTerm(e.target.value)}
+ onFocus={() => patientSearchResults.length > 0 && setShowSearchResults(true)}
+ placeholder="Search by name, ID, or phone..."
+ className={`w-full px-3 py-3 border ${validationErrors.patient ? 'border-crimson' : 'border-black'} bg-white text-sm focus:outline-2 focus:outline-black focus:outline-offset-2`}
+ />
+ {isSearching && (
+ <div className="absolute right-3 top-3 text-xs text-gray-mid">Searching...</div>
+ )}
+
+ {/* Search Results Dropdown */}
+ {showSearchResults && patientSearchResults.length > 0 && (
+ <div className="absolute z-10 w-full mt-1 bg-white border-2 border-black max-h-60 overflow-y-auto">
+ {patientSearchResults.map(patient => (
+ <button
+ key={patient.id}
+ onClick={() => handleSelectPatient(patient)}
+ className="w-full px-3 py-2 text-left hover:bg-gray-bg transition-colors border-b border-gray-light text-sm"
+ >
+ <div className="font-semibold">{patient.first_name} {patient.last_name}</div>
+ <div className="text-xs text-gray-mid">ID: {patient.id} | Phone: {patient.phone}</div>
+ </button>
+ ))}
+ </div>
+ )}
+ </div>
+ {validationErrors.patient && (
+ <p className="text-xs text-crimson mt-1">{validationErrors.patient}</p>
+ )}
+ {formData.patient_id && (
+ <p className="text-xs text-black mt-1">✓ Patient selected (ID: {formData.patient_id})</p>
+ )}
</div>
<div>
@@ -148,6 +259,26 @@ function BookAppointmentModal({ onClose, onSuccess }) {
</select>
</div>
+ {/* Specialty Filter */}
+ <div>
+ <label className="block text-[11px] uppercase tracking-wider mb-2 font-semibold">
+ Filter by Specialty (Optional)
+ </label>
+ <select
+ value={selectedSpecialty}
+ onChange={(e) => handleSpecialtyFilter(e.target.value)}
+ className="w-full px-3 py-3 border border-gray-mid bg-gray-bg text-sm focus:outline-2 focus:outline-black focus:outline-offset-2"
+ >
+ <option value="">All Specialties</option>
+ {specialties.map(specialty => (
+ <option key={specialty.id} value={specialty.id}>
+ {specialty.name}
+ </option>
+ ))}
+ </select>
+ </div>
+
+ {/* Doctor Selection */}
<div>
<label className="block text-[11px] uppercase tracking-wider mb-2 font-semibold">
Doctor *
@@ -157,7 +288,7 @@ function BookAppointmentModal({ onClose, onSuccess }) {
value={formData.doctor_id}
onChange={handleChange}