-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment.cpp
More file actions
204 lines (168 loc) · 5.48 KB
/
payment.cpp
File metadata and controls
204 lines (168 loc) · 5.48 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
#include "payment.h"
// Get payment by ID
bool getPaymentById(int &payment_id, Payment &payment) {
for (Payment p : payments) {
if (p.payment_id == payment_id) {
payment = p;
return true;
}
}
return false;
}
bool getPaymentByBookingId(int &booking_id, Payment &payment) {
for (Payment p : payments) {
if (p.booking_id == booking_id) {
payment = p;
return true;
}
}
return false;
}
// Add payment to the database
Payment addPayment() {
Payment payment;
Booking booking;
cout << "Booking Id: "; cin >> payment.booking_id;
cout << "Amount: "; cin >> payment.amount;
if (cin.fail()) {
showInputError();
return Payment();
}
if(payment.amount < 0){
showError("Invalid amount ");
return Payment();
}
if(getPaymentByBookingId(payment.booking_id, payment)){
showWarning("Payment already exists for this Booking. ");
return Payment();
}
if(!getBookingById(payment.booking_id, booking)){
showWarning("Booking not found.");
return Payment();
}
try {
string insertQuery =
"INSERT INTO Payments (booking_id, amount) VALUES (" +
to_string(payment.booking_id) + ", " + to_string(payment.amount) + ");";
// Use g_database instance to call insertObject
if (!insertObject(insertQuery, "Payments", PaymentCallback, &payments)) {
throw runtime_error("Failed to create payment in database");
}
showSuccess("Payment of $" + to_string(payment.amount) + " received for Booking ID " + to_string(payment.booking_id));
return payments.back();
}
catch (const exception &e) {
showError("Error: " + string(e.what()));
exitProgram();
return Payment(); // Return an empty Payment object in case of error
}
}
void showPaymentByBookingId(){
int booking_id;
cout << "Enter the booking id: ";
cin >> booking_id;
if (cin.fail()) {
showInputError();
return;
}
Payment payment;
if(!getPaymentByBookingId(booking_id, payment)){
showWarning("Payment not found for this booking.");
return;
}
cout << "Payment ID: " << payment.payment_id << endl;
cout << "Booking ID: " << payment.booking_id << endl;
cout << "Amount: $" << payment.amount << endl;
cout << "Payment Date: " << payment.payment_date << endl;
}
void showPaymentHistory() {
if (payments.empty()) {
showWarning("No payments recorded.");
return;
}
showHighlight("--- Payment History ---");
cout << left << setw(15) << "Payment ID" << setw(15) << "Booking ID" << setw(15)
<< "Amount" << setw(15) << "Payment Date" << endl;
for (int i = payments.size() - 1; i > -1; --i) {
const Payment &payment = payments.at(i);
cout << left << setw(15) << payment.payment_id <<
setw(15) << payment.booking_id <<
setw(15) << payment.amount << setw(15) << payment.payment_date << endl;
}
}
void generateInvoice() {
int payment_id;
cout << "Enter the payment id: ";
cin >> payment_id;
if(cin.fail()) {
showInputError();
return;
}
Payment payment;
Booking booking;
Customer customer;
Room room;
RoomType roomtype;
Staff staff;
if (!getPaymentById(payment_id, payment)) {
showWarning("Payment not found");
return;
}
if (!getBookingById(payment.booking_id, booking)) {
showWarning("Booking not found");
return;
}
if(!getRoomById(booking.room_id, room)){
showWarning("Room not found ");
return;
}
if(!getRoomTypeById(room.room_type_id, roomtype)){
showWarning("Room Type not found ");
return;
}
if(!getCustomerById(booking.customer_id, customer)){
showWarning("Customer not found");
}
if (booking.staff_id != - 1 && !getStaffById(booking.staff_id, staff)) {
showWarning("Staff not found");
return;
}
showHighlight("----- Invoice -----");
cout << "(Service Provider) Staff ID: " << (staff.staff_id != -1 ? to_string(staff.staff_id) : " - ") << endl;
cout << "Staff Name: " << (staff.staff_id != -1 ? staff.name : " - ") << endl;
cout << "Staff Email: " << (staff.staff_id != -1 ? staff.email : " - ") << endl << endl << endl;
cout << "Payment ID: " << payment.payment_id << endl;
cout << "Booking ID: " << payment.booking_id << endl << endl;
cout << "Customer ID: " << booking.customer_id << endl;
cout << "Customer Name: " << customer.name << endl;
cout << "Customer Email: " << customer.email << endl << endl << endl;
cout << "Room Number: " << room.room_number << endl;
cout << "Room Type: " << roomtype.type_name << endl << endl << endl;
cout << "Check-in Date: " << booking.check_in << endl;
cout << "Check-out Date: " << booking.check_out << endl << endl << endl;
cout << "Price: $" << payment.amount << endl;
cout << "Payment Date: " << payment.payment_date << endl;
}
// database functions
void getPayments() {
try {
string query = "SELECT * FROM Payments;";
if (!getObjects(query, PaymentCallback, &payments)) {
throw runtime_error("Error when retrieving all the payments");
}
} catch (const exception &e) {
showError("Error: " + string(e.what()));
exitProgram();
}
}
int PaymentCallback(void *data, int columns, char **values,
char **column_names) {
vector<Payment> *payments = static_cast<vector<Payment> *>(data);
Payment payment;
payment.payment_id = values[0] ? atoi(values[0]) : -1;
payment.booking_id = values[1] ? atoi(values[1]) : -1;
payment.amount = values[2] ? atof(values[2]) : -1;
payment.payment_date = values[3] ? values[3] : "";
payments->push_back(payment);
return 0;
}