diff --git a/exportdialog.cpp b/exportdialog.cpp index 70c2315..22e5e5a 100644 --- a/exportdialog.cpp +++ b/exportdialog.cpp @@ -1,17 +1,113 @@ #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); +} diff --git a/exportdialog.h b/exportdialog.h index 07ca957..a41a513 100644 --- a/exportdialog.h +++ b/exportdialog.h @@ -2,8 +2,19 @@ #include "zeiterfassungdialog.h" +// Qt includes +#include +#include + +// system includes +#include + +// forward declarations +class QFile; + namespace Ui { class ExportDialog; } class MainWindow; +class GetBookingsReply; class ExportDialog : public ZeiterfassungDialog { @@ -13,8 +24,21 @@ public: explicit ExportDialog(MainWindow &mainWindow, QWidget *parent = Q_NULLPTR); ~ExportDialog(); +private Q_SLOTS: + void start(); + void requestFinished(); + private: + void nextDate(); + Ui::ExportDialog *ui; MainWindow &m_mainWindow; + + std::unique_ptr m_file; + QTextStream m_textStream; + QDate m_date; + QDate m_toDate; + + std::unique_ptr m_reply; }; diff --git a/exportdialog.ui b/exportdialog.ui index d8ae0b2..c72c033 100644 --- a/exportdialog.ui +++ b/exportdialog.ui @@ -6,17 +6,64 @@ 0 0 - 1024 - 480 + 586 + 349 Export - + + + + + + + From: + + + + + + + + + + To: + + + + + + + + + + Start + + + + + + + false + + + 0 + + + + + + + + + true + + + - - + diff --git a/translations/exportplugin_de.ts b/translations/exportplugin_de.ts index 270d267..968d810 100644 --- a/translations/exportplugin_de.ts +++ b/translations/exportplugin_de.ts @@ -7,6 +7,54 @@ Export + + From: + + + + To: + + + + Start + + + + Invalid date selection! + + + + Invalid date selection! + +%0 + + + + From date cannot be after to date! + + + + Export finished! + + + + Save Export as + + + + CSV-File + + + + Could not open export file! + + + + Could not open export file! + +%0 + + ExportPlugin diff --git a/translations/exportplugin_en.ts b/translations/exportplugin_en.ts index 2620668..4e18a57 100644 --- a/translations/exportplugin_en.ts +++ b/translations/exportplugin_en.ts @@ -7,6 +7,54 @@ Export + + From: + + + + To: + + + + Start + + + + Invalid date selection! + + + + Invalid date selection! + +%0 + + + + From date cannot be after to date! + + + + Export finished! + + + + Save Export as + + + + CSV-File + + + + Could not open export file! + + + + Could not open export file! + +%0 + + ExportPlugin