#include "reportdialog.h" #include "mainwindow.h" #include #include #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); connect(ui_.printSellerReceiptButton, &QPushButton::clicked, this, &ReportDialog::onPrintSellerReceiptButtonClicked); connect(ui_.reportView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ReportDialog::onReportViewSelectionChanged); } 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); QFont fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont); fixedFont.setPointSize(12); 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(fixedFont); 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().substr(0, 14).c_str(), -14) .arg(sellers.at(i * j)->getFirstName().substr(0, 14).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); } // pieces booked on the special account "Sonderkonto" const auto specialSeller = market_->findSellerWithUuid("11111111-1111-1111-1111-111111111111"); if (specialSeller && specialSeller->numArticlesSold() > 0) { printer.newPage(); painter.setFont(QFont("Arial", 16, QFont::Bold)); painter.drawText(QRect(0, 0, width, height), Qt::AlignTop | Qt::AlignHCenter, "Auswertung Kindersachenmarkt"); painter.setFont(fixedFont); QString content("Einzelteile ohne Nummer\n=======================\n\n"); unsigned int lines{0}; unsigned int pages{1}; for (const auto& article : specialSeller->getArticles(true)) { content += QString("- %1:").arg(article->getDescription().substr(0, 45).c_str(), -45); content += QString("%1\n").arg(article->getPriceAsString().c_str(), 11); ++lines; if (lines % static_cast(ENTRIES_PER_PAGE) == 0 && (pages * lines) < specialSeller->getArticles(true).size()) { painter.drawText(QRect(0, 50, width, height), Qt::AlignLeft, content); lines = 0; ++pages; 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)); content = "Einzelteile ohne Nummer\n=======================\n\n"; } } painter.drawText(QRect(0, 50, width, height), Qt::AlignLeft, content); } // 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(fixedFont); 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(); } void ReportDialog::onPrintSellerReceiptButtonClicked() { QSettings settings; int feeInPercent = settings.value("global/feeInPercent").toInt(); int maxFeeInEuro = settings.value("global/maxFeeInEuro").toInt(); auto selModel = ui_.reportView->selectionModel(); if (selModel->hasSelection() == false) return; auto indexes = selModel->selectedRows(); auto& seller = market_->getSellers().at(indexes[0].row()); PosPrinter printer; if (printer.isValid()) printer.printSellerReceipt(seller.get(), feeInPercent, maxFeeInEuro * 100); } void ReportDialog::onReportViewSelectionChanged(const QItemSelection& selected, [[maybe_unused]] const QItemSelection& deselected) { if (selected.size() > 0) { ui_.printSellerReceiptButton->setEnabled(true); } else { ui_.printSellerReceiptButton->setEnabled(false); } }