#include "exportdialog.h" #include "ui_exportdialog.h" // Qt includes #include #include #include // dbcorelib includes #include "utils/timeutils.h" // 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(m_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)); const auto date = QLocale().toString(m_date, QLocale::ShortFormat); // writes a log line with date prefixed const auto log = [plainTextEdit=ui->plainTextEdit, &date](const QString &msg){ plainTextEdit->appendHtml(QStringLiteral("%0 %1
").arg(date, msg)); }; if(m_reply->success()) { const auto logBooking = [log](const GetBookingsReply::Booking &booking){ log(QString("booking %0 %1").arg(QLocale().toString(booking.time), booking.type)); }; int totalSeconds = 0; auto iter = m_reply->bookings().constBegin(); while(true) { if(iter == m_reply->bookings().constEnd()) break; const auto &startBooking = *(iter++); logBooking(startBooking); if(startBooking.type != "K") { log("unexpected booking type!"); break; } if(iter == m_reply->bookings().constEnd()) { log("missing end booking!"); break; } const auto &endBooking = *(iter++); logBooking(endBooking); if(endBooking.type != "G") { log("unexpected booking type!"); break; } totalSeconds += startBooking.time.secsTo(endBooking.time); } const auto totalTime = QTime(0, 0).addSecs(totalSeconds).toString(QStringLiteral("HH:mm:ss")); m_textStream << date << ';' << totalTime << endl; log(QString("succeeded (%0)!").arg(totalTime)); } else { log(QString("failed to load: %1").arg(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); }