-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappointmentdialog.cpp
More file actions
251 lines (212 loc) · 8.13 KB
/
Copy pathappointmentdialog.cpp
File metadata and controls
251 lines (212 loc) · 8.13 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
#include "appointmentdialog.h"
AppointmentDialog::AppointmentDialog(QSqlRelationalTableModel *model, QWidget *parent)
: QDialog(parent), appointmentModel(model)
{
initSetup();
connect(okButton, SIGNAL(clicked()), this, SLOT(addAppointment()));
}
AppointmentDialog::AppointmentDialog(QSqlRelationalTableModel *model,
QSqlRecord &record, int row, QWidget *parent)
:QDialog(parent), appointmentModel(model), appointmentRecord(record), appointmentRow(row)
{
initSetup();
fillFieldInputs();
connect(okButton, SIGNAL(clicked()), this, SLOT(editAppointment()));
}
void AppointmentDialog::initSetup()
{
setupLayout();
setupAppointmentModel();
connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
}
void AppointmentDialog::setupLayout()
{
startDateEdit = new QDateEdit;
endDateEdit = new QDateEdit;
startDateEdit->setDateTime(QDateTime::currentDateTime());
endDateEdit->setDateTime(QDateTime::currentDateTime());
startDateEdit->setDisplayFormat("MMMM dd yyyy");
endDateEdit->setDisplayFormat("MMMM dd yyyy");
startDateEdit->setCalendarPopup(true);
endDateEdit->setCalendarPopup(true);
startTimeEdit = new QTimeEdit;
startTimeEdit->setDateTime(QDateTime::currentDateTime());
endTimeEdit = new QTimeEdit;
endTimeEdit->setDateTime(QDateTime::currentDateTime().addSecs(60*30));
patientComboBox = new QComboBox;
employeeComboBox = new QComboBox;
visitComboBox = new QComboBox;
okButton = new QPushButton(tr("&OK"));
cancelButton = new QPushButton(tr("&Cancel"));
buttonBox = new QDialogButtonBox;
buttonBox->addButton(okButton, QDialogButtonBox::AcceptRole);
buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);
QFormLayout *formLayout = new QFormLayout;
formLayout->addRow(tr("Start Date:"), startDateEdit);
formLayout->addRow(tr("Start Time:"), startTimeEdit);
formLayout->addRow(tr("End Date:"), endDateEdit);
formLayout->addRow(tr("End Time:"), endTimeEdit);
formLayout->addRow(tr("Patient:"), patientComboBox);
formLayout->addRow(tr("Employee:"), employeeComboBox);
formLayout->addRow(tr("Visit Type: "), visitComboBox);
formLayout->addWidget(buttonBox);
setLayout(formLayout);
}
void AppointmentDialog::setupAppointmentModel()
{
QSqlDatabase db = appointmentModel->database();
patientModel = new QSqlTableModel(this, db);
patientModel->setTable("patients");
patientModel->select();
patientComboBox->clear();
for(int i = 0; i < patientModel->rowCount(); i++)
{
patientComboBox->addItem(patientModel->record(i).field("full_name").value().toString(),
patientModel->record(i).field("id").value());
}
employeeModel = new QSqlTableModel(this, db);
employeeModel->setTable("employees");
employeeModel->select();
employeeComboBox->clear();
for(int i = 0; i < employeeModel->rowCount(); i++)
{
employeeComboBox->addItem(employeeModel->record(i).field("full_name").value().toString(),
employeeModel->record(i).field("id").value());
}
visitModel = new QSqlTableModel(this, db);
visitModel->setTable("visits");
visitModel->select();
visitComboBox->clear();
for(int i = 0; i < visitModel->rowCount(); i++)
{
visitComboBox->addItem(visitModel->record(i).field("type").value().toString(),
visitModel->record(i).field("id").value());
}
}
void AppointmentDialog::fillFieldInputs()
{
startDateEdit->setDate(appointmentRecord.field("start").value().toDateTime().date());
startTimeEdit->setTime(appointmentRecord.field("start").value().toDateTime().time());
endDateEdit->setDate(appointmentRecord.field("end").value().toDateTime().date());
endTimeEdit->setTime(appointmentRecord.field("end").value().toDateTime().time());
patientComboBox->setCurrentIndex(patientComboBox->findText(appointmentRecord
.field("patients_full_name_2")
.value().toString()));
employeeComboBox->setCurrentIndex(employeeComboBox->findText(appointmentRecord
.field("full_name")
.value().toString()));
visitComboBox->setCurrentIndex(visitComboBox->findText(appointmentRecord
.field("type")
.value().toString()));
}
void AppointmentDialog::addAppointment()
{
QSqlRecord record;
QSqlField id("id", QVariant::Int);
QSqlField start("start", QVariant::DateTime);
QSqlField end("end", QVariant::DateTime);
QSqlField employeeId("employee_id", QVariant::Int);
QSqlField patientId("patient_id", QVariant::Int);
QSqlField visitId("visit_id", QVariant::Int);
id.setAutoValue(true);
start.setValue(QVariant(getStart()));
end.setValue(QVariant(getEnd()));
patientId.setValue(QVariant(getPatientId()));
employeeId.setValue(QVariant(getEmployeeId()));
visitId.setValue(QVariant(getVisitId()));
record.append(id);
record.append(start);
record.append(end);
record.append(patientId);
record.append(employeeId);
record.append(visitId);
if(isFieldInputValid() && appointmentModel->insertRecord(-1, record))
{
accept();
}
else
{
appointmentModel->revertAll();
}
}
bool AppointmentDialog::isFieldInputValid()
{
if(startDateEdit->date() > endDateEdit->date())
{
QMessageBox::information(this, tr("Input Validation"),
tr("Start date must be less then End date"));
return false;
}
if(startTimeEdit->time() > endTimeEdit->time())
{
QMessageBox::information(this, tr("Input Validation"),
tr("Start time must be less then End time"));
return false;
}
if(patientComboBox->count() <= 0)
{
QMessageBox::information(this, tr("Input Validation"),
tr("There are no patients. Please add some patients."));
return false;
}
if(employeeComboBox->count() <= 0)
{
QMessageBox::information(this, tr("Input Validation"),
tr("There are no employees. Please add some employees"));
return false;
}
return true;
}
void AppointmentDialog::editAppointment()
{
appointmentRecord.setValue("start", QVariant(getStart()));
appointmentRecord.setValue("end", QVariant(getEnd()));
QSqlField patientId("patient_id", QVariant::Int);
patientId.setValue(QVariant(getPatientId()));
QSqlField employeeId("employee_id", QVariant::Int);
employeeId.setValue(QVariant(getEmployeeId()));
QSqlField visitId("visit_id", QVariant::Int);
visitId.setValue(QVariant(getVisitId()));
appointmentRecord.append(patientId);
appointmentRecord.append(employeeId);
appointmentRecord.append(visitId);
if(isFieldInputValid() &&
appointmentModel->setRecord(appointmentRow, appointmentRecord))
{
appointmentModel->submitAll();
accept();
}
else
{
appointmentModel->revertAll();
}
}
int AppointmentDialog::getPatientId() const
{
int id = patientComboBox->itemData(patientComboBox->currentIndex()).toInt();
return id;
}
int AppointmentDialog::getEmployeeId() const
{
int id = employeeComboBox->itemData(employeeComboBox->currentIndex()).toInt();
return id;
}
int AppointmentDialog::getVisitId() const
{
int id = visitComboBox->itemData(visitComboBox->currentIndex()).toInt();
return id;
}
QDateTime AppointmentDialog::getStart() const
{
QDateTime startDateTime;
startDateTime.setDate(startDateEdit->date());
startDateTime.setTime(startTimeEdit->time());
return startDateTime;
}
QDateTime AppointmentDialog::getEnd() const
{
QDateTime endDateTime;
endDateTime.setDate(endDateEdit->date());
endDateTime.setTime(endTimeEdit->time());
return endDateTime;
}