This repository was archived by the owner on Apr 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddrentaldialog.cpp
More file actions
225 lines (184 loc) · 8.47 KB
/
Copy pathaddrentaldialog.cpp
File metadata and controls
225 lines (184 loc) · 8.47 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
#include "addrentaldialog.h"
#include <QVBoxLayout>
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
#include <QStandardItem>
#include <QDateTime>
AddRentalDialog::AddRentalDialog(QWidget *parent)
: QDialog(parent),
customerSearchResultsModel(new QStringListModel(this)),
movieSearchResultsModel(new QStringListModel(this)) {
customerSearchLineEdit = new QLineEdit(this);
movieSearchLineEdit = new QLineEdit(this);
customerSearchResultsListView = new QListView(this);
movieSearchResultsListView = new QListView(this);
rentalDateEdit = new QDateEdit(QDate::currentDate(), this);
returnDateEdit = new QDateEdit(QDate::currentDate().addDays(7), this); // Default rental period of 7 days
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
customerSearchLineEdit->setStyleSheet("QLineEdit {border-width: 5px; border-radius: 8px; border-color: #81B2D9;}");
movieSearchLineEdit->setStyleSheet("QLineEdit {border-width: 5px; border-radius: 8px; border-color: #81B2D9;}");
customerSearchResultsListView->setStyleSheet("QListView {border-width: 5px; border-radius: 8px; border-color: #81B2D9;}");
movieSearchResultsListView->setStyleSheet("QListView {border-width: 5px; border-radius: 8px; border-color: #81B2D9;}");
QVBoxLayout *mainLayout = new QVBoxLayout(this);
QFormLayout *formLayout = new QFormLayout();
formLayout->addRow(tr("Customer Search:"), customerSearchLineEdit);
formLayout->addRow(tr("Customer Results:"), customerSearchResultsListView);
formLayout->addRow(tr("Movie Search:"), movieSearchLineEdit);
formLayout->addRow(tr("Movie Results:"), movieSearchResultsListView);
formLayout->addRow(tr("Rental Date:"), rentalDateEdit);
formLayout->addRow(tr("Return Date:"), returnDateEdit);
mainLayout->addLayout(formLayout);
mainLayout->addWidget(buttonBox);
customerSearchResultsListView->setModel(customerSearchResultsModel);
movieSearchResultsListView->setModel(movieSearchResultsModel);
connect(customerSearchLineEdit, &QLineEdit::textChanged, this, &AddRentalDialog::updateCustomerSearchResults);
connect(movieSearchLineEdit, &QLineEdit::textChanged, this, &AddRentalDialog::updateMovieSearchResults);
connect(customerSearchResultsListView, &QListView::clicked, this, &AddRentalDialog::customerSelected);
connect(movieSearchResultsListView, &QListView::clicked, this, &AddRentalDialog::movieSelected);
connect(buttonBox, &QDialogButtonBox::accepted, this, &AddRentalDialog::onAddClicked);
connect(buttonBox, &QDialogButtonBox::rejected, this, &AddRentalDialog::reject);
loadCustomersFromFile();
loadMoviesFromFile();
setLayout(mainLayout);
}
AddRentalDialog::~AddRentalDialog() {
}
void AddRentalDialog::onAddClicked() {
if (selectedCustomerName.isEmpty() || selectedMovieName.isEmpty()) { // Changed from selectedMovieSKU to selectedMovieName
QMessageBox::warning(this, tr("Input Error"), tr("Please select a customer and a movie."));
return;
}
// Call updateInventory here with the movie name
updateInventory(selectedMovieName, -1); // Assuming decrement by 1 for rental
// Now append the rental to rentals.csv
appendRentalToCsv(selectedCustomerName, selectedMovieName, rentalDateEdit->date(), returnDateEdit->date()); // Change to pass name instead of SKU
accept(); // Close the dialog
}
void AddRentalDialog::updateCustomerSearchResults(const QString &text) {
customerSearchResultsModel->setStringList(getCustomerSearchResults(text));
}
void AddRentalDialog::updateMovieSearchResults(const QString &text) {
movieSearchResultsModel->setStringList(getMovieSearchResults(text));
}
QStringList AddRentalDialog::getCustomerSearchResults(const QString &searchText) {
QStringList results;
for (const QString &customer : customers) {
if (customer.contains(searchText, Qt::CaseInsensitive)) {
results.append(customer);
}
}
return results;
}
QStringList AddRentalDialog::getMovieSearchResults(const QString &searchText) {
QStringList results;
for (const QString &movie : movies) {
// Adjust condition to search within movie names
if (movie.contains(searchText, Qt::CaseInsensitive)) {
results.append(movie);
}
}
return results;
}
void AddRentalDialog::customerSelected(const QModelIndex &index) {
selectedCustomerName = index.data(Qt::DisplayRole).toString();
}
void AddRentalDialog::movieSelected(const QModelIndex &index) {
selectedMovieName = index.data(Qt::DisplayRole).toString();
}
void AddRentalDialog::loadCustomersFromFile() {
QFile file("users.csv");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::critical(this, "Error", "Could not open users.csv");
return;
}
QTextStream in(&file);
in.readLine(); // Skip the header line
while (!in.atEnd()) {
QString line = in.readLine();
QStringList fields = line.split(',');
if (fields.size() > 0)
customers.append(fields.at(0)); // Assuming the name is in the first column
}
file.close();
}
void AddRentalDialog::loadMoviesFromFile() {
QFile file("inventory.csv");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::critical(this, "Error", "Could not open inventory.csv");
return;
}
QTextStream in(&file);
in.readLine(); // Skip the header line
while (!in.atEnd()) {
QString line = in.readLine();
QStringList fields = line.split(',');
// Assuming the name is the first column (index 0)
if (fields.size() > 1)
movies.append(fields.at(0)); // Adjust the index as needed
}
file.close();
}
void AddRentalDialog::appendRentalToCsv(const QString &customerName, const QString &movieName, const QDate &rentalDate, const QDate &returnDate) {
QFile file("rentals.csv");
if (!file.open(QIODevice::Append | QIODevice::Text)) {
QMessageBox::critical(this, "Error", "Could not open rentals.csv");
return;
}
QTextStream out(&file);
// Generate a unique rental ID, you can use QDateTime for simplicity
QString rentalID = QDateTime::currentDateTime().toString("yyyyMMddHHmmss");
out << rentalID << ","
<< customerName << ","
<< movieName << ","
<< rentalDate.toString("yyyy-MM-dd") << ","
<< returnDate.toString("yyyy-MM-dd") << ","
<< "Rented" << "\n";
file.close();
}
void AddRentalDialog::updateInventory(const QString &movieName, int quantityChange) {
QFile file("inventory.csv");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::critical(this, "Error", "Could not open inventory.csv");
return;
}
QString data;
QTextStream in(&file);
QStringList lines = in.readAll().split("\n");
file.close();
bool inventoryUpdated = false;
for (int i = 1; i < lines.size(); ++i) { // Skip header line
QString line = lines.at(i);
QStringList fields = line.split(',');
if (!fields.isEmpty() && fields.at(0) == movieName) { // Assuming movie name is in the first column
int currentQuantity = fields.at(3).toInt(); // Assuming quantity is in the fourth column
int newQuantity = currentQuantity + quantityChange;
fields[3] = QString::number(qMax(0, newQuantity)); // Prevent negative quantity
// Update the Rental Status based on the new quantity
if (newQuantity <= 0) {
fields[4] = "Unavailable"; // Assuming rental status is in the fifth column
} else {
fields[4] = "Available"; // Or any other logic you have to set availability
}
lines[i] = fields.join(',');
inventoryUpdated = true;
break; // Assuming unique movie names, so stop after finding the movie
}
}
if (inventoryUpdated) {
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::critical(this, "Error", "Could not write to inventory.csv");
return;
}
QTextStream out(&file);
// Write the header back first
out << lines.first() << "\n"; // Assuming the first line is the header
// Then write the updated inventory data
for (int i = 1; i < lines.size(); ++i) {
out << lines[i] << "\n";
}
file.close();
} else {
QMessageBox::information(this, "Not Found", "Movie not found in inventory.");
}
}