#include "exportdialog.h" #include "ui_exportdialog.h" // Qt includes #include #include #include // zeiterfassungcorelib includes #include "utils/timeutils.h" // zeiterfassungguilib includes #include "mainwindow.h" // zeiterfassungnetworklib includes #include "zeiterfassungapi.h" #include "replies/getbookingsreply.h" ExportDialog::ExportDialog(MainWindow &mainWindow, QWidget *parent) : ZeiterfassungDialog(parent), ui(new Ui::ExportDialog), m_mainWindow(mainWindow) { ui->setupUi(this); connect(ui->dateEditFrom, &QDateEdit::dateChanged, ui->dateEditTo, &QDateEdit::setMinimumDate); connect(ui->pushButton, &QAbstractButton::pressed, this, &ExportDialog::start); ui->dateEditFrom->setDate(beginOfMonth(QDate::currentDate().addMonths(-1))); ui->dateEditTo->setDate(endOfMonth(QDate::currentDate().addMonths(-1))); } ExportDialog::~ExportDialog() { delete ui; } void ExportDialog::start() { if(ui->dateEditFrom->date() > ui->dateEditTo->date()) { QMessageBox::warning(this, tr("Invalid date selection!"), tr("Invalid date selection!\n\n%0").arg(tr("From date cannot be after to date!"))); return; } const auto filename = QFileDialog::getSaveFileName(this, tr("Save Export as"), QString(), QString("%0 (*.csv)").arg(tr("CSV-File"))); auto file = std::make_unique(filename); if(!file->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { QMessageBox::warning(this, tr("Could not open export file!"), tr("Could not open export file!\n\n%0").arg(file->errorString())); return; } m_file = std::move(file); m_textStream.setDevice(file.get()); m_date = ui->dateEditFrom->date(); m_toDate = ui->dateEditTo->date(); ui->pushButton->setEnabled(false); ui->progressBar->setMaximum(m_date.daysTo(m_toDate) + 1); ui->progressBar->setValue(0); ui->progressBar->setEnabled(true); ui->plainTextEdit->clear(); nextDate(); } void ExportDialog::requestFinished() { ui->progressBar->setValue(ui->progressBar->maximum() - m_date.daysTo(m_toDate)); if(m_reply->success()) { for(const auto &booking : m_reply->bookings()) { ui->plainTextEdit->appendHtml(QStringLiteral("%0 booking %1 %2
") .arg(QLocale().toString(m_date, QLocale::ShortFormat), QLocale().toString(booking.time), booking.type)); } ui->plainTextEdit->appendHtml(QStringLiteral("%0 succeeded!
") .arg(QLocale().toString(m_date, QLocale::ShortFormat))); } else { ui->plainTextEdit->appendHtml(QStringLiteral("%0 failed to load: %1
") .arg(QLocale().toString(m_date, QLocale::ShortFormat), m_reply->message())); } if(m_date < m_toDate) { m_date = m_date.addDays(1); nextDate(); } else { m_file = nullptr; m_textStream.setDevice(nullptr); m_reply = nullptr; QMessageBox::information(this, tr("Export finished!"), tr("Export finished!")); ui->pushButton->setEnabled(true); } } void ExportDialog::nextDate() { m_reply = m_mainWindow.erfassung().doGetBookings(m_mainWindow.userInfo().userId, m_date, m_date); connect(m_reply.get(), &ZeiterfassungReply::finished, this, &ExportDialog::requestFinished); }