1+ import Billing from '../models/billing.js' ;
2+ import billingService from '../services/billing.service.js' ;
3+
4+ const createBilling = async ( req , res , next ) => {
5+ try {
6+ const { patient, appointment, amount } = req . body ;
7+
8+ if ( ! patient || ! amount ) {
9+ return res . status ( 400 ) . json ( {
10+ success : false ,
11+ message : "Patient and amount are required" ,
12+ } ) ;
13+ }
14+
15+ const billing = await Billing . create ( {
16+ patient,
17+ appointment,
18+ amount,
19+ createdBy : req . user . id ,
20+ } ) ;
21+
22+ res . status ( 201 ) . json ( {
23+ success : true ,
24+ data : billing ,
25+ } ) ;
26+ } catch ( error ) {
27+ next ( error ) ;
28+ }
29+ } ;
30+
31+ const getAllBilling = async ( req , res , next ) => {
32+ try {
33+ const page = parseInt ( req . query . page ) || 1 ;
34+ const limit = parseInt ( req . query . limit ) || 10 ;
35+ const skip = ( page - 1 ) * limit ;
36+
37+ const bills = await Billing . find ( )
38+ . populate ( "patient" , "name email" )
39+ . sort ( { createdAt : - 1 } )
40+ . skip ( skip )
41+ . limit ( limit ) ;
42+
43+ const total = await Billing . countDocuments ( ) ;
44+
45+ res . status ( 200 ) . json ( {
46+ success : true ,
47+ data : {
48+ bills,
49+ total,
50+ page,
51+ pages : Math . ceil ( total / limit ) ,
52+ } ,
53+ } ) ;
54+ } catch ( error ) {
55+ next ( error ) ;
56+ }
57+ } ;
58+
59+ const updateBillingStatus = async ( req , res , next ) => {
60+ try {
61+ const { status, paymentMethod, transactionId } = req . body ;
62+
63+ const bill = await Billing . findById ( req . params . id ) ;
64+
65+ if ( ! bill ) {
66+ return res . status ( 404 ) . json ( {
67+ success : false ,
68+ message : "Billing record not found" ,
69+ } ) ;
70+ }
71+
72+ if ( ! [ "pending" , "paid" ] . includes ( status ) ) {
73+ return res . status ( 400 ) . json ( {
74+ success : false ,
75+ message : "Invalid status value" ,
76+ } ) ;
77+ }
78+
79+ if ( bill . status === "paid" && status === "pending" ) {
80+ return res . status ( 400 ) . json ( {
81+ success : false ,
82+ message : "Cannot revert paid billing to pending" ,
83+ } ) ;
84+ }
85+
86+ bill . status = status ;
87+
88+ if ( status === "paid" ) {
89+ bill . paymentMethod = paymentMethod ;
90+ bill . transactionId = transactionId ;
91+ bill . paidAt = new Date ( ) ;
92+ }
93+
94+ await bill . save ( ) ;
95+
96+ res . status ( 200 ) . json ( {
97+ success : true ,
98+ data : bill ,
99+ } ) ;
100+ } catch ( error ) {
101+ next ( error ) ;
102+ }
103+ } ;
104+
105+ const deleteBilling = async ( req , res , next ) => {
106+ try {
107+ const bill = await Billing . findById ( req . params . id ) ;
108+
109+ if ( ! bill ) {
110+ return res . status ( 404 ) . json ( {
111+ success : false ,
112+ message : "Billing record not found" ,
113+ } ) ;
114+ }
115+
116+ await bill . deleteOne ( ) ;
117+
118+ res . status ( 200 ) . json ( {
119+ success : true ,
120+ message : "Billing deleted" ,
121+ } ) ;
122+ } catch ( error ) {
123+ next ( error ) ;
124+ }
125+ } ;
126+
127+ // Staff CRUD for admin
128+
129+ const createBillingStaff = async ( req , res , next ) => {
130+ try {
131+ const billing = await billingService . createBillingStaff ( req . body ) ;
132+ res . status ( 201 ) . json ( billing ) ;
133+ } catch ( error ) {
134+ next ( error )
135+ }
136+ } ;
137+
138+ const changePassword = async ( req , res , next ) => {
139+ try {
140+ const { oldPassword, newPassword } = req . body ;
141+ const result = await billingService . changePassword (
142+ req . user . id ,
143+ oldPassword ,
144+ newPassword ) ;
145+ res . status ( 200 ) . json ( result ) ;
146+ } catch ( err ) {
147+ next ( err )
148+ }
149+ } ;
150+
151+ const getAllBillingStaff = async ( req , res , next ) => {
152+ try {
153+ const billing = await billingService . getAllBillingStaff ( ) ;
154+ res . json ( billing ) ;
155+ } catch ( err ) {
156+ next ( err )
157+ }
158+ } ;
159+
160+ const updateBillingStaff = async ( req , res , next ) => {
161+ try {
162+ const { id } = req . params ;
163+ const billing = await billingService . updateBillingStaff ( id , req . body ) ;
164+ res . json ( billing ) ;
165+ } catch ( err ) {
166+ next ( err )
167+ }
168+ } ;
169+
170+ const deleteBillingStaff = async ( req , res , next ) => {
171+ try {
172+ const { id } = req . params ;
173+ const result = await billingService . deleteBillingStaff ( id ) ;
174+ res . json ( result ) ;
175+ } catch ( err ) {
176+ next ( err )
177+ }
178+ } ;
179+
180+ export {
181+ // Billing operations
182+ createBilling ,
183+ getAllBilling ,
184+ updateBillingStatus ,
185+ deleteBilling ,
186+
187+ // Staff CRUD for admin
188+
189+ createBillingStaff ,
190+ changePassword ,
191+ getAllBillingStaff ,
192+ updateBillingStaff ,
193+ deleteBillingStaff
194+ }
0 commit comments