#include "reportdialog.h" #include "mainwindow.h" #include #include #include #include #include ReportDialog::ReportDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(parent, f) { ui_.setupUi(this); market_ = dynamic_cast(parent)->getMarketplace(); model_ = std::make_unique(market_, ui_.reportView); ui_.reportView->setModel(model_.get()); ui_.reportView->hideColumn(0); connect(ui_.exportCsvButton, &QPushButton::clicked, this, &ReportDialog::onExportCsvButtonClicked); connect(ui_.printReportButton, &QPushButton::clicked, this, &ReportDialog::onPrintReportButtonClicked); } void ReportDialog::onExportCsvButtonClicked() { QSettings settings; int feeInPercent = settings.value("global/feeInPercent").toInt(); int maxFeeInEuro = settings.value("global/maxFeeInEuro").toInt(); QFileDialog fileDialog(this); fileDialog.setFileMode(QFileDialog::AnyFile); fileDialog.setNameFilter("CSV Files (*.csv)"); fileDialog.setAcceptMode(QFileDialog::AcceptSave); if (fileDialog.exec()) { market_->exportReportToCSV(fileDialog.selectedFiles().at(0).toStdString(), feeInPercent, maxFeeInEuro); } } void ReportDialog::onPrintReportButtonClicked() { QPrinter printer; QPrintDialog dialog(&printer, this); dialog.setWindowTitle("Bericht drucken"); if (dialog.exec() != QDialog::Accepted) { return; } QSettings settings; int feeInPercent = settings.value("global/feeInPercent").toInt(); int maxFeeInEuro = settings.value("global/maxFeeInEuro").toInt(); QPainter painter; int height = printer.height(); int width = printer.width(); const double ENTRIES_PER_PAGE = 50.0; const auto& sellers = market_->getSellers(); unsigned int numPages = std::ceil(sellers.size() / ENTRIES_PER_PAGE); painter.begin(&printer); for (unsigned int i = 1; i <= numPages; ++i) { if (i > 1) printer.newPage(); painter.setFont(QFont("Arial", 16, QFont::Bold)); painter.drawText(QRect(0, 0, width, height), Qt::AlignTop | Qt::AlignHCenter, "Auswertung Kindersachenmarkt"); painter.setFont(QFont("monospace", 12)); QString content; content += QString("%1 %2 %3 %4 %5 %6 %7") .arg("Nr.", -3) .arg("Nachname", -14) .arg("Vorname", -14) .arg("geliefert", -9) .arg("verkauft", -8) .arg("Umsatz", -10) .arg("Auszahlung\n", -11); content.append( "---------------------------------------------------------------------------\n"); for (unsigned int j = 0; j < ENTRIES_PER_PAGE && (i * j) < sellers.size(); ++j) { content += QString("%1 %2 %3 %4 %5 %6 %7\n") .arg(sellers.at(i * j)->getSellerNo(), 3) .arg(sellers.at(i * j)->getLastName().c_str(), -14) .arg(sellers.at(i * j)->getFirstName().c_str(), -14) .arg(sellers.at(i * j)->numArticlesOffered(), 9) .arg(sellers.at(i * j)->numArticlesSold(), 8) .arg(sellers.at(i * j)->sumAsString().c_str(), 10) .arg(paymentAsString(sellers.at(i * j)->sumInCents(), feeInPercent, maxFeeInEuro * 100) .c_str(), 11); } painter.drawText(QRect(0, 50, width, height), Qt::AlignLeft, content); } // now print the overall statistics printer.newPage(); painter.setFont(QFont("Arial", 16, QFont::Bold)); painter.drawText(QRect(0, 0, width, height), Qt::AlignTop | Qt::AlignHCenter, "Auswertung Kindersachenmarkt"); painter.setFont(QFont("monospace", 12)); QString content("Gesamtstatistik\n---------------\n\n"); content += QString("Registrierte Verkäufer: %1\n").arg(sellers.size(), 6); content += QString("Verkaufte Artikel: %1\n\n").arg(6, 6); content += QString("Gesamtumsatz: %1\n").arg(market_->getOverallSumAsString().c_str(), 10); content += QString("Ausgezahlt: %1\n") .arg(market_->getOverallPaymentAsString(feeInPercent, maxFeeInEuro * 100).c_str(), 10); content += QString("Verbleibend: %1\n") .arg(market_->getOverallRevenueAsString(feeInPercent, maxFeeInEuro * 100).c_str(), 10); painter.drawText(QRect(0, 50, width, height), Qt::AlignLeft, content); painter.end(); }