kima2/src/gui/reportdialog.cpp

221 lines
No EOL
9.2 KiB
C++

#include "reportdialog.h"
#include "mainwindow.h"
#include <posprinter.h>
#include <utils.h>
#include <filesystem>
#include <QFileDialog>
#include <QFontDatabase>
#include <QPainter>
#include <QSettings>
#include <QtPrintSupport/QPrintDialog>
#include <QtPrintSupport/QPrinter>
namespace fs = std::filesystem;
ReportDialog::ReportDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(parent, f)
{
ui_.setupUi(this);
market_ = dynamic_cast<MainWindow*>(parent)->getMarketplace();
model_ = std::make_unique<ReportModel>(market_, ui_.reportView);
ui_.reportView->setModel(model_.get());
ui_.reportView->hideColumn(0);
ui_.reportView->setRowHidden(0, true); // hide the special "Sonderkonto" user
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();
auto filename = QFileDialog::getSaveFileName(this, "Verkäufer exportieren", QString(),
"CSV Dateien (*.csv)");
if (filename.isEmpty())
return;
fs::path filePath(filename.toStdWString());
market_->exportReportToCSV(filePath, 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 = 51;
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 - 1) * ENTRIES_PER_PAGE + j < sellers.size(); ++j) {
int idx = (i - 1) * ENTRIES_PER_PAGE + j;
if (sellers.at(idx)->getUuidAsString() == "11111111-1111-1111-1111-111111111111") {
continue;
}
content += QString("%1 %2 %3 %4 %5 %6 %7\n")
.arg(sellers.at(idx)->getSellerNo(), 3)
.arg(sellers.at(idx)->getLastName().substr(0, 14).c_str(), -14)
.arg(sellers.at(idx)->getFirstName().substr(0, 14).c_str(), -14)
.arg(sellers.at(idx)->numArticlesOffered(), 9)
.arg(sellers.at(idx)->numArticlesSold(), 8)
.arg(sellers.at(idx)->sumAsString().c_str(), 10)
.arg(paymentAsString(sellers.at(idx)->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<int>(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");
int numArticlesOffered = market_->getNumArticlesOffered();
int numArticlesSold = market_->getNumArticlesSold();
double percentArticlesSold =
(static_cast<double>(numArticlesSold) / static_cast<double>(numArticlesOffered)) * 100;
content += QString("Registrierte Verkäufer: %1\n").arg(sellers.size() - 1, 6);
content += QString("Angelieferte Artikel: %1\n").arg(numArticlesOffered, 6);
content += QString("Verkaufte Artikel: %1 (%L2 %)\n")
.arg(numArticlesSold, 6)
.arg(percentArticlesSold, 0, 'f', 2);
content += QString("Anzahl Kunden: %1\n\n").arg(market_->getSales().size(), 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\n")
.arg(market_->getOverallRevenueAsString(feeInPercent, maxFeeInEuro * 100).c_str(), 10);
content += QString("(Einbehaltener Prozentsatz: %1 %)\n").arg(feeInPercent, 3);
content += QString("(Maximal einbehaltener Betrag: %1 €)\n").arg(maxFeeInEuro, 3);
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();
QString posPrinterDevice = settings.value("global/posPrinterDevice", "").toString();
QString posPrinterEndpoint = settings.value("global/posPrinterEndpoint", "").toString();
auto selModel = ui_.reportView->selectionModel();
if (selModel->hasSelection() == false)
return;
auto indexes = selModel->selectedRows();
auto& seller = market_->getSellers().at(indexes[0].row());
auto printerDevice =
convertToPosPrinterDevice(posPrinterDevice.toStdString(), posPrinterEndpoint.toStdString());
std::unique_ptr<PosPrinter> printer;
if (printerDevice) {
printer = std::make_unique<PosPrinter>(*printerDevice);
} else {
printer = std::make_unique<PosPrinter>();
}
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);
}
}