-
Notifications
You must be signed in to change notification settings - Fork 0
/
windownewcompany.cpp
188 lines (144 loc) · 6.46 KB
/
windownewcompany.cpp
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
#include <iostream>
#include <QSqlQuery>
#include <vector>
#include "windownewcompany.h"
#include "ui_windownewcompany.h"
// ===================================
// CONST STATIC VARIABLE DEFINITIONS
// ===================================
const QString WindowNewCompany::SSN_WITH_DASHES = "000-00-0000";
const QString WindowNewCompany::SSN_WITHOUT_DASHES = "000000000";
// ===================================
// PRIVATE HELPER FUNCTION DEFINITIONS
// ===================================
// Used to verify the format of the SSN.
bool WindowNewCompany::check_ssn_format(QString ssn) const {
int size = ssn.size();
if (size == SSN_WITH_DASHES.size()) {
// Format is 000-00-0000. Check for dash positions and only numbers.
return ssn.contains(QRegularExpression("\\d{3}-{1}\\d{2}-{1}\\d{4}"));
} else if (size == SSN_WITHOUT_DASHES.size()) {
// Format is 000000000. Check to ensure that input is only numbers.
return ssn.contains(QRegularExpression("\\d{9}"));
}
// Return false if the string is of some other size.
return false;
}
// Used to clear all lineEdit fields.
void WindowNewCompany::clear_all_fields() {
// resetting the input fields to blank
ui->lineEdit_company_name->setText("");
ui->lineEdit_new_username->setText("");
ui->lineEdit_password->setText("");
ui->lineEdit_verify_password->setText("");
ui->lineEdit_first_name->setText("");
ui->lineEdit_last_name->setText("");
ui->lineEdit_ssn->setText("");
}
// ===================================
// PUBLIC FUNCTION DEFINITIONS
// ===================================
WindowNewCompany::WindowNewCompany(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::WindowNewCompany)
{
ui->setupUi(this);
DatabaseConnection *dbc = new DatabaseConnection(this);
db2 = dbc->establishConnection("two");
}
WindowNewCompany::~WindowNewCompany() {
delete ui;
}
void WindowNewCompany::on_pushButtonNewCompanyDone_clicked()
{
QString lineEdit_company_name = ui -> lineEdit_company_name -> text();
QString lineEdit_new_username = ui -> lineEdit_new_username -> text();
QString lineEdit_password = ui -> lineEdit_password -> text();
QString lineEdit_verify_password = ui -> lineEdit_verify_password -> text();
QString lineEdit_first_name = ui -> lineEdit_first_name -> text();
QString lineEdit_last_name = ui -> lineEdit_last_name -> text();
QString lineEdit_employee_id = ui -> lineEdit_employee_id -> text();
QString lineEdit_ssn = ui -> lineEdit_ssn -> text();
// Check that all fields are filled.
std::vector<QString> fields;
fields.push_back(lineEdit_company_name);
fields.push_back(lineEdit_new_username);
fields.push_back(lineEdit_password);
fields.push_back(lineEdit_verify_password);
fields.push_back(lineEdit_first_name);
fields.push_back(lineEdit_last_name);
fields.push_back(lineEdit_employee_id);
fields.push_back(lineEdit_ssn);
bool allFieldsFilled = true;
for (unsigned long long i = 0; i < fields.size(); i++) {
QString field = fields.at(i);
if (field.isEmpty()) {
allFieldsFilled = false;
break;
}
}
// Check that all fields are filled.
if (!allFieldsFilled) {
QMessageBox::information(this, "Error", "Error: Missing field.");
} else {
// We need to check for various input errors.
bool fieldError = false;
// Ensure that the password fields match.
if (lineEdit_password != lineEdit_verify_password) {
QMessageBox::information(this, "Error", "Error: Password fields do not match.");
fieldError = true;
}
// Ensure that the employee ID field is formatted properly.
if (!lineEdit_employee_id.contains(QRegularExpression("^[0-9]+$"))) {
QMessageBox::information(this, "Error", "Error: Employee ID imporperly formatted.");
fieldError = true;
}
// Ensure that the SSN field is formatted properly.
if (!check_ssn_format(lineEdit_ssn)) {
QMessageBox::information(this, "Error", "Error: SSN improperly formatted.");
fieldError = true;
}
// No errors? We are good to go (:
if (!fieldError) {
QString companyName = ui -> lineEdit_company_name -> text();
QString newUsername = ui -> lineEdit_new_username -> text();
QString password = ui -> lineEdit_password -> text();
QString verify_password = ui -> lineEdit_verify_password -> text();
QString firstName = ui -> lineEdit_first_name -> text();
QString lastName = ui -> lineEdit_last_name -> text();
QString employeeID = ui -> lineEdit_employee_id -> text();
QString ssn = ui -> lineEdit_ssn -> text();
QSqlQuery eQuery(QSqlDatabase::database("two"));
// We may need to potentially re-format the SSN if it was inputted with dashes.
if (ssn.size() == SSN_WITH_DASHES.size()) {
// Format is 000-00-0000, reformat to 000000000
ssn.remove(QRegularExpression("-"));
}
// Insert Employee Values
eQuery.prepare("INSERT INTO Employee (EmployeeID, Name_Last, Name_First, SSN, Position_Code, Username, Password) VALUES (:employeeID, :nameLast, :nameFirst, :ssn, :position, :username, :password)");
eQuery.bindValue(":employeeID", employeeID);
eQuery.bindValue(":nameLast", lastName);
eQuery.bindValue(":nameFirst", firstName);
eQuery.bindValue(":ssn", ssn);
eQuery.bindValue(":position", "C");
eQuery.bindValue(":username", newUsername);
eQuery.bindValue(":password", password);
eQuery.exec();
QSqlQuery cQuery(QSqlDatabase::database("two"));
// Insert Company Values
cQuery.prepare("INSERT INTO Company (CompanyName, CEO) VALUES (:companyName, :employeeID)");
cQuery.bindValue(":companyName", companyName);
cQuery.bindValue(":employeeID", employeeID);
cQuery.exec();
// qmessagebox for debugging. delete when we know it works every time for everyone
QMessageBox::information(this, "Creating", "Creating new company...");
// resetting the input fields to blank
clear_all_fields();
this->close();
}
}
}
void WindowNewCompany::on_pushButton_clicked() {
clear_all_fields();
this->hide();
}