Files
StationManager/printwidget.cpp
2026-04-05 16:14:54 +03:00

146 lines
6.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "printwidget.h"
#include "database.h"
#include <QTableWidget>
#include <QHeaderView>
#include <QFileDialog>
#include <QMessageBox>
#include <QTextStream>
#include <QDateTime>
PrintWidget::PrintWidget(QWidget* parent) : QWidget(parent) {
QVBoxLayout* mainLayout = new QVBoxLayout(this);
QLabel* title = new QLabel("Печать списка билетов", this);
title->setStyleSheet("font-size: 20px; font-weight: bold;");
mainLayout->addWidget(title);
QHBoxLayout* filterLayout = new QHBoxLayout();
QLabel* routeLabel = new QLabel("Выберите рейс:", this);
m_routeCombo = new QComboBox(this);
filterLayout->addWidget(routeLabel);
filterLayout->addWidget(m_routeCombo);
filterLayout->addStretch();
mainLayout->addLayout(filterLayout);
m_table = new QTableWidget(this);
m_table->setColumnCount(8);
m_table->setHorizontalHeaderLabels({"ID", "Маршрут", "Время", "Пассажир", "Документ", "Билетов", "Сумма", "Кассир"});
m_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
m_table->setSelectionBehavior(QAbstractItemView::SelectRows);
m_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
mainLayout->addWidget(m_table);
m_totalLabel = new QLabel("Итого: 0 билетов на сумму 0.00 руб.", this);
m_totalLabel->setStyleSheet("font-weight: bold; font-size: 14px;");
mainLayout->addWidget(m_totalLabel);
QHBoxLayout* btnLayout = new QHBoxLayout();
QPushButton* refreshBtn = new QPushButton("Обновить", this);
QPushButton* printBtn = new QPushButton("Экспорт в файл", this);
btnLayout->addWidget(refreshBtn);
btnLayout->addWidget(printBtn);
btnLayout->addStretch();
mainLayout->addLayout(btnLayout);
connect(m_routeCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &PrintWidget::onRouteChanged);
connect(refreshBtn, &QPushButton::clicked, this, [this]() {
refreshRoutes();
onRouteChanged(m_routeCombo->currentIndex());
});
connect(printBtn, &QPushButton::clicked, this, &PrintWidget::onPrintClicked);
}
void PrintWidget::refreshRoutes() {
m_routeCombo->clear();
QVector<Route> routes = Database::instance().getAllRoutes();
for (const auto& route : routes) {
m_routeCombo->addItem(QString("%1 - %2").arg(route.routeName).arg(route.departureTime), route.id);
}
}
void PrintWidget::onRouteChanged(int index) {
if (index < 0) return;
int routeId = m_routeCombo->currentData().toInt();
loadTickets(routeId);
}
void PrintWidget::loadTickets(int routeId) {
QVector<Ticket> tickets = Database::instance().getTicketsByRoute(routeId);
m_table->setRowCount(tickets.size());
qDebug() << "TICKET COUNT:" + std::to_string(tickets.length()) + " " + std::to_string(routeId);
double total = 0;
int totalSeats = 0;
for (int i = 0; i < tickets.size(); ++i) {
const Ticket& ticket = tickets[i];
m_table->setItem(i, 0, new QTableWidgetItem(QString::number(ticket.id)));
m_table->setItem(i, 1, new QTableWidgetItem(ticket.routeName));
m_table->setItem(i, 2, new QTableWidgetItem(ticket.saleDate));
m_table->setItem(i, 3, new QTableWidgetItem(ticket.passengerName));
m_table->setItem(i, 4, new QTableWidgetItem(ticket.documentNumber));
m_table->setItem(i, 5, new QTableWidgetItem(QString::number(ticket.seatCount)));
m_table->setItem(i, 6, new QTableWidgetItem(QString::number(ticket.totalPrice, 'f', 2)));
m_table->setItem(i, 7, new QTableWidgetItem(ticket.sellerName));
total += ticket.totalPrice;
totalSeats += ticket.seatCount;
}
m_totalLabel->setText(QString("Итого: %1 билетов на сумму %2 руб.")
.arg(totalSeats)
.arg(total, 0, 'f', 2));
}
void PrintWidget::onPrintClicked() {
if (m_table->rowCount() == 0) {
QMessageBox::warning(this, "Внимание", "Нет данных для печати!");
return;
}
QString fileName = QFileDialog::getSaveFileName(
this, "Сохранить список билетов",
QString("tickets_%1.txt").arg(QDateTime::currentDateTime().toString("yyyyMMdd_HHmmss")),
"Text Files (*.txt)");
if (fileName.isEmpty()) return;
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::critical(this, "Ошибка", "Не удалось создать файл!");
return;
}
QTextStream out(&file);
out << "===========================================\n";
out << " СПИСОК ПРОДАННЫХ БИЛЕТОВ\n";
out << "===========================================\n";
out << "Рейс: " << m_routeCombo->currentText() << "\n";
out << "Дата формирования: " << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss") << "\n";
out << "===========================================\n\n";
int totalSeats = 0;
double totalSum = 0;
for (int i = 0; i < m_table->rowCount(); ++i) {
out << QString("Билет #%1\n").arg(m_table->item(i, 0)->text());
out << QString(" Пассажир: %1\n").arg(m_table->item(i, 3)->text());
out << QString(" Документ: %1\n").arg(m_table->item(i, 4)->text());
out << QString(" Количество: %1 шт.\n").arg(m_table->item(i, 5)->text());
out << QString(" Сумма: %1 руб.\n").arg(m_table->item(i, 6)->text());
out << QString(" Продал: %1 (%2)\n").arg(m_table->item(i, 7)->text()).arg(m_table->item(i, 2)->text());
out << "-----------------------------------------\n";
totalSeats += m_table->item(i, 5)->text().toInt();
totalSum += m_table->item(i, 6)->text().toDouble();
}
out << "\n===========================================\n";
out << QString("ИТОГО: %1 билетов на сумму %2 руб.\n").arg(totalSeats).arg(totalSum, 0, 'f', 2);
out << "===========================================\n";
file.close();
QMessageBox::information(this, "Успех", QString("Файл сохранен:\n%1").arg(fileName));
}