-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadministrator.cpp
More file actions
323 lines (259 loc) · 14.6 KB
/
administrator.cpp
File metadata and controls
323 lines (259 loc) · 14.6 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "administrator.h"
#include "ui_administrator.h"
#include "databasemanager.h"
#include <QMessageBox>
#include <QCryptographicHash>
#include <QLineSeries>
#include <QtCharts>
#include <QDateTimeAxis>
#include <qdatetimeaxis.h>
#include <qlineseries.h>
#include "LocaleManager.h"
Administrator::Administrator(QWidget *parent) :
QWidget(parent),
ui(new Ui::Administrator)
{
ui->setupUi(this);
// Создание регулярного выражения для русских букв
QRegularExpression regExp("[А-Яа-яЁё ]+"); // Разрешает вводить только русские буквы и пробел
// Создание валидатора на основе регулярного выражения
QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp);
QIntValidator *intValidator = new QIntValidator(0, std::numeric_limits<int>::max()); // Ограничение ввода букв
QDoubleValidator *doubleValidator = new QDoubleValidator(0.0, 0.01,std::numeric_limits<double>::max()); // Ограничение ввода букв
ui->lineEditStaffName->setValidator(validator);
ui->lineEdit_WorkDays->setValidator(intValidator);
ui->lineEdit_WorkDone->setValidator(intValidator);
ui->lineEditStaffSalaryAdd->setValidator(doubleValidator);
ui->dateEdit_start->setDate(QDate::currentDate());
ui->dateEdit_end->setDate(QDate::currentDate());
// Подключение к базе данных через DatabaseManager
if (!DatabaseManager::instance().connectToDatabase("localhost", "WoodWorks", "postgres", "1001")) {
QMessageBox::critical(this, "Database Connection", "Ошибка подключения к базе данных");
return;
}
updateStaffList();
}
Administrator::~Administrator()
{
delete ui;
}
void Administrator::updateStaffList()
{
// Сохраняем текущий id и текст (имя и фамилия) выбранного сотрудника
int currentId = ui->comboBox_StaffSelected->currentData().toInt();
QString currentText = ui->comboBox_StaffSelected->currentText();
ui->comboBox_StaffSelected->clear();
auto employees = DatabaseManager::instance().getEmployeeList();
// Сортировка сотрудников по фамилии, а затем по имени
std::sort(employees.begin(), employees.end(), [](const Employee& a, const Employee& b) {
return (a.lastName < b.lastName) || (a.lastName == b.lastName && a.firstName < b.firstName);
});
// Заполняем ComboBox отсортированными значениями
for (const auto& employee : employees) {
QString fullName = employee.firstName + " " + employee.lastName;
ui->comboBox_StaffSelected->addItem(fullName, employee.id);
}
// Восстанавливаем прежний выбор
int index = ui->comboBox_StaffSelected->findData(currentId);
// Если id не найден, устанавливаем на тот, который имеет текстовое совпадение
if (index == -1) {
index = ui->comboBox_StaffSelected->findText(currentText);
}
if (index != -1) {
ui->comboBox_StaffSelected->setCurrentIndex(index);
}
}
void Administrator::updateStuff()
{
int employeeId = ui->comboBox_StaffSelected->currentData().toInt();
auto employee = DatabaseManager::instance().getEmployeeById(employeeId);
int salary = employee.workdays * employee.salary + employee.salary * 0.01 * employee.workdone;
if (employee.id != -1) {
ui->lineEdit_StaffNameInfo->setText(employee.firstName + " " + employee.lastName + " " + employee.middleName);
ui->lineEdit_StaffJobInfo->setText(employee.positionName);
ui->lineEdit_StaffSalary->setText(LocaleManager::getRussianLocale().toString(employee.salary));
ui->lineEdit_WorkDays->setText(LocaleManager::getRussianLocale().toString(employee.workdays));
ui->lineEdit_WorkDone->setText(LocaleManager::getRussianLocale().toString(employee.workdone));
ui->lineEdit_SalarySum->setText(LocaleManager::getRussianLocale().toString(salary));
}
}
void Administrator::on_pushButton_AddStaff_clicked()
{
// Получаем данные из полей ввода
QString fullName = ui->lineEditStaffName->text().trimmed();
QStringList nameParts = fullName.split(" ");
if (nameParts.size() < 2) {
QMessageBox::warning(this, "Ошибка ввода", "Укажите полное имя, или хотя бы имя и фамилию");
return;
}
QString firstName = nameParts[0];
QString lastName = nameParts[1];
QString middleName = (nameParts.size() > 2) ? nameParts[2] : "";
QString positionName = ui->lineEdit_StaffJobAdd->text();
QString salaryStr = ui->lineEditStaffSalaryAdd->text().trimmed();
QString username = ui->lineEditStaffLogin->text().trimmed();
QString password = ui->lineEditStaffPassword->text().trimmed();
QString role = (ui->comboBox_StaffRole->currentText() == "Администрация") ? "admin" : "master";
if (firstName.isEmpty() || lastName.isEmpty() || positionName.isEmpty() ||
salaryStr.isEmpty() || username.isEmpty() || password.isEmpty()) {
QMessageBox::warning(this, "Ошибка ввода", "Заполните все поля");
return;
}
// Хэшируем пароль
QString passwordHash = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Sha256).toHex();
// Преобразуем зарплату из строки в число
double salary = LocaleManager::getRussianLocale().toDouble(salaryStr);
// Используем DatabaseManager для добавления сотрудника
if (!DatabaseManager::instance().addEmployee(firstName, lastName, middleName, positionName, salary, username, passwordHash, role)) {
QMessageBox::critical(this, "Ошибка базы данных", "Не удалось добавить работника");
return;
}
QMessageBox::information(this, "Успех", "Работник успешно добавлен!");
updateStaffList();
// Очистка полей после добавления
ui->lineEditStaffName->clear();
ui->lineEdit_StaffJobAdd->clear();
ui->lineEditStaffSalaryAdd->clear();
ui->lineEditStaffLogin->clear();
ui->lineEditStaffPassword->clear();
}
void Administrator::on_pushButton_SaveStaff_clicked()
{
int employeeId = ui->comboBox_StaffSelected->currentData().toInt();
QStringList nameParts = ui->lineEdit_StaffNameInfo->text().trimmed().split(" ");
if (nameParts.size() < 2) {
QMessageBox::warning(this, "Ошибка ввода", "Укажите полное имя, или хотя бы имя и фамилию");
return;
}
QString firstName = nameParts[0];
QString lastName = nameParts[1];
QString middleName = (nameParts.size() > 2) ? nameParts[2] : "";
QString positionName = ui->lineEdit_StaffJobInfo->text();
QString salaryStr = ui->lineEdit_StaffSalary->text().trimmed();
int workdays = ui->lineEdit_WorkDays->text().toInt();
int workdone = ui->lineEdit_WorkDone->text().toInt();
bool salaryConversionOk;
double salary = LocaleManager::getRussianLocale().toDouble(salaryStr,&salaryConversionOk);
if (!salaryConversionOk) {
QMessageBox::warning(this, "Ошибка ввода", "Некорректное значение зарплаты");
return;
}
if (!DatabaseManager::instance().updateEmployee(employeeId, firstName, lastName, middleName, positionName, salary,workdays,workdone)) {
QMessageBox::critical(this, "Ошибка базы данных", "Не удалось сохранить данные сотрудника");
return;
}
QMessageBox::information(this, "Успех", "Данные сотрудника успешно сохранены!");
updateStaffList(); // Обновляем список сотрудников после изменений
}
void Administrator::on_pushButton_RemoveStaff_clicked()
{
int employeeId = ui->comboBox_StaffSelected->currentData().toInt();
if (QMessageBox::question(this, "Подтверждение удаления", "Вы уверены, что хотите удалить этого сотрудника?") == QMessageBox::Yes) {
if (!DatabaseManager::instance().removeEmployee(employeeId)) {
QMessageBox::critical(this, "Ошибка базы данных", "Не удалось удалить сотрудника");
return;
}
QMessageBox::information(this, "Успех", "Сотрудник успешно удален!");
updateStaffList(); // Обновляем список сотрудников после удаления
}
}
void Administrator::on_comboBox_StaffSelected_currentIndexChanged(int index)
{
updateStuff();
}
void Administrator::on_pushButton_generate_clicked()
{
// Получаем даты из виджетов
QDate startDate = ui->dateEdit_start->date();
QDate endDate = ui->dateEdit_end->date();
if (startDate >= endDate) {
QMessageBox::warning(this, "Invalid Date Range", "Дата начала не может быть позже или в день даты окончания.");
return;
}
// Запрашиваем данные из базы данных для указанного периода
QList<Order> orders = DatabaseManager::instance().getOrdersForPeriod(startDate, endDate);
// Инициализируем переменные для расчета
double totalIncome = 0.0;
double totalOutcome = 0.0;
int completedOrders = 0;
// Проходим по всем заказам и считаем доход, расходы и количество выполненных заказов
for (const Order& order : orders) {
totalIncome += order.price; // Доход - сумма цен завершенных заказов
totalOutcome += order.manufacturePrice; // Расход - сумма затрат на производство
completedOrders++;
}
double totalProfit = totalIncome - totalOutcome; // Прибыль - разница между доходами и расходами
// Для расчета роста компании необходимо сравнить показатели с предыдущим периодом
QDate previousStartDate = startDate.addMonths(-1).addDays(1 - startDate.day()); // Первый день предыдущего месяца
QDate previousEndDate = previousStartDate.addMonths(1).addDays(-1); // Последний день предыдущего месяца
QList<Order> previousOrders = DatabaseManager::instance().getOrdersForPeriod(previousStartDate, previousEndDate);
double previousIncome = 0.0;
for (const Order& order : previousOrders) {
previousIncome += order.price;
}
double growthRate = 0.0;
if (previousIncome > 0) {
growthRate = ((totalIncome - previousIncome) / previousIncome) * 100; // Рост в процентах
}
// Заполняем UI-поля данными
ui->lineEdit_income->setText(LocaleManager::getRussianLocale().toString(totalIncome, 'f', 2)); // Доход за период
ui->lineEdit_orderCompletinoNumber->setText(LocaleManager::getRussianLocale().toString(completedOrders)); // Выполнено заказов
ui->lineEdit_outcome->setText(LocaleManager::getRussianLocale().toString(totalOutcome, 'f', 2)); // Расход за период
ui->lineEdit_profit->setText(LocaleManager::getRussianLocale().toString(totalProfit, 'f', 2)); // Прибыль за период
ui->lineEdit_profitPeriod->setText(LocaleManager::getRussianLocale().toString(growthRate, 'f', 2) + "%"); // Рост компании за предыдущий период
// Построение графика доходов
plotIncomeChart(startDate, endDate);
}
void Administrator::plotIncomeChart(QDate startDate, QDate endDate) {
// Сбор данных для графика
QList<Order> orders = DatabaseManager::instance().getOrdersForPeriod(startDate, endDate);
QMap<QDate, double> incomeData;
// Суммируем доход за каждый день
for (const Order& order : orders) {
QDate date = order.endDate;
incomeData[date] += order.price;
}
QLayout *oldLayout = ui->chartWidget->layout();
if (oldLayout) {
QLayoutItem *item;
while ((item = oldLayout->takeAt(0))) {
if (item->widget()) {
delete item->widget(); // Удаляем виджет
}
delete item; // Удаляем элемент макета
}
delete oldLayout; // Удаляем сам макет
}
// Создаем новую серию данных для графика
QLineSeries *series = new QLineSeries();
// Добавляем данные в серию
for (auto it = incomeData.begin(); it != incomeData.end(); ++it) {
QDate date = it.key();
QDateTime dateTime(date, QTime(0, 0)); // Преобразование QDate в QDateTime
// Проверка и отладка
// qDebug() << "Date:" << date.toString("yyyy-MM-dd") << "Income:" << it.value();
series->append(dateTime.toMSecsSinceEpoch(), it.value());
}
// Создаем новый график и добавляем серию
QChart *chart = new QChart();
chart->addSeries(series);
chart->createDefaultAxes();
// Настройка осей
QDateTimeAxis *axisX = new QDateTimeAxis();
axisX->setFormat("dd.MM.yyyy");
axisX->setTitleText("Дата");
chart->setAxisX(axisX, series);
QValueAxis *axisY = new QValueAxis();
axisY->setTitleText("Доход");
chart->setAxisY(axisY, series);
// Настройка графика
chart->setTitle("Доходы за выбранный период");
// Создаем новый QChartView и добавляем его в макет
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
// Создаем новый макет и добавляем в него QChartView
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(chartView);
// Устанавливаем макет в виджет
ui->chartWidget->setLayout(layout);
}