diff --git a/images/refresh.png b/images/refresh.png new file mode 100644 index 0000000..f3585b7 Binary files /dev/null and b/images/refresh.png differ diff --git a/images/report.png b/images/report.png new file mode 100644 index 0000000..c00f861 Binary files /dev/null and b/images/report.png differ diff --git a/reportsplugin.cpp b/reportsplugin.cpp new file mode 100644 index 0000000..c629073 --- /dev/null +++ b/reportsplugin.cpp @@ -0,0 +1,36 @@ +#include "reportsplugin.h" + +#include +#include +#include +#include +#include + +#include "mainwindow.h" + +#include "reportswidget.h" + +ReportsPlugin::ReportsPlugin(QObject *parent) : + ZeiterfassungPlugin(parent) +{ + qDebug() << "called"; + + static auto dir = QDir(QCoreApplication::applicationDirPath()).absoluteFilePath(QStringLiteral("translations")); + + if(m_translator.load(QLocale(), QStringLiteral("reportsplugin"), QStringLiteral("_"), dir)) + { + if(!QCoreApplication::installTranslator(&m_translator)) + { + qWarning() << "could not install translation reportsplugin"; + } + } + else + { + qWarning() << "could not load translation reportsplugin"; + } +} + +void ReportsPlugin::attachTo(MainWindow &mainWindow) +{ + mainWindow.statusBar()->addPermanentWidget(new ReportsWidget(mainWindow)); +} diff --git a/reportsplugin.h b/reportsplugin.h new file mode 100644 index 0000000..6f58568 --- /dev/null +++ b/reportsplugin.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + +#include "zeiterfassungplugin.h" + +class MainWindow; + +class Q_DECL_EXPORT ReportsPlugin : public ZeiterfassungPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "dbsoftware.zeiterfassung.plugin/1.0" FILE "reportsplugin.json") + Q_INTERFACES(ZeiterfassungPlugin) + +public: + explicit ReportsPlugin(QObject *parent = Q_NULLPTR); + + // ZeiterfassungPlugin interface + void attachTo(MainWindow &mainWindow) Q_DECL_OVERRIDE; + +private: + QTranslator m_translator; +}; diff --git a/reportsplugin.json b/reportsplugin.json new file mode 100644 index 0000000..e69de29 diff --git a/reportsplugin.pro b/reportsplugin.pro new file mode 100644 index 0000000..0258f62 --- /dev/null +++ b/reportsplugin.pro @@ -0,0 +1,22 @@ +QT += core network gui widgets + +DBLIBS += zeiterfassungcore zeiterfassunggui + +TARGET = reportsplugin + +HEADERS += reportsplugin.h \ + reportswidget.h + +SOURCES += reportsplugin.cpp \ + reportswidget.cpp + +FORMS += + +RESOURCES += reportsplugin_resources.qrc + +TRANSLATIONS += translations/reportsplugin_en.ts \ + translations/reportsplugin_de.ts + +OTHER_FILES += reportsplugin.json + +include(../plugin.pri) diff --git a/reportsplugin_resources.qrc b/reportsplugin_resources.qrc new file mode 100644 index 0000000..acd4367 --- /dev/null +++ b/reportsplugin_resources.qrc @@ -0,0 +1,6 @@ + + + images/refresh.png + images/report.png + + diff --git a/reportswidget.cpp b/reportswidget.cpp new file mode 100644 index 0000000..30eba00 --- /dev/null +++ b/reportswidget.cpp @@ -0,0 +1,150 @@ +#include "reportswidget.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mainwindow.h" +#include "zeiterfassungapi.h" + +ReportsWidget::ReportsWidget(MainWindow &mainWindow) : + QLabel(&mainWindow), + m_mainWindow(mainWindow) +{ + connect(&m_mainWindow, &MainWindow::dateChanged, this, &ReportsWidget::dateChanged); + connect(&m_mainWindow, &MainWindow::refreshEverything, this, &ReportsWidget::refresh); + + setFrameShape(QFrame::Panel); + setFrameShadow(QFrame::Sunken); + + m_actionRefreshReport = new QAction(QIcon(QStringLiteral(":/zeiterfassung/plugins/reportsplugin/images/refresh.png")), + tr("Refresh report"), this); + connect(m_actionRefreshReport, &QAction::triggered, this, &ReportsWidget::refresh); + m_mainWindow.menuView()->addAction(m_actionRefreshReport); + + m_actionOpenReport = new QAction(QIcon(QStringLiteral(":/zeiterfassung/plugins/reportsplugin/images/report.png")), + tr("Open report"), this); + connect(m_actionOpenReport, &QAction::triggered, this, &ReportsWidget::openReport); + m_mainWindow.menuTools()->addAction(m_actionOpenReport); + + m_mainWindow.toolBar()->addAction(m_actionOpenReport); + + dateChanged(m_mainWindow.date()); +} + +void ReportsWidget::dateChanged(const QDate &date) +{ + if(!date.isValid()) + { + qWarning() << "invalid date" << date; + return; + } + + auto monthBegin = QDate(date.year(), date.month(), 1); + if(monthBegin != m_date) + { + m_date = monthBegin; + refresh(); + } +} + +void ReportsWidget::refresh() +{ + if(!m_date.isValid()) + { + qWarning() << "invalid date" << m_date; + return; + } + + setText(tr("Balance: %0, Holidays: %1").arg(tr("???")).arg(tr("???"))); + + m_actionRefreshReport->setEnabled(false); + m_actionOpenReport->setEnabled(false); + + m_reply = m_mainWindow.erfassung().doGetReport(m_mainWindow.userInfo().userId, m_date); + connect(m_reply.get(), &ZeiterfassungReply::finished, this, &ReportsWidget::finished); +} + +void ReportsWidget::finished() +{ + if(!m_reply->success()) + { + m_date = QDate(); + QMessageBox::warning(this, tr("Could not load report!"), tr("Could not load report!") % "\n\n" % m_reply->message()); + goto after; + } + + { + auto content = m_reply->content(); + + QString balance; + + { + static QRegularExpression regex(QStringLiteral("Gleitzeit +([0-9]+\\:[0-9]+\\-?) +([0-9]+\\:[0-9]+\\-?)")); + auto match = regex.match(content); + if(match.hasMatch()) + { + balance = match.captured(2); + if(balance.endsWith(QChar('-'))) + { + balance.chop(1); + balance = QChar('-') % balance; + } + } + else + { + balance = tr("n/a"); + qWarning() << "balance not found in PDF"; + } + } + + QString holidays; + + { + static QRegularExpression regex(QStringLiteral("Urlaubsanspruch +(\\-?[0-9]+\\.[0-9]+) +(\\-?[0-9]+\\.[0-9]+)")); + auto match = regex.match(content); + if(match.hasMatch()) + holidays = match.captured(2); + else + { + holidays = tr("n/a"); + qWarning() << "holidays not found in PDF"; + } + } + + setText(tr("Balance: %0, Holidays: %1").arg(balance).arg(holidays)); + + { + QTemporaryFile file(QDir::temp().absoluteFilePath(QStringLiteral("reportXXXXXX.pdf"))); + file.setAutoRemove(false); + if(!file.open()) + { + QMessageBox::warning(this, tr("Could not write report!"), tr("Could not write report!") % "\n\n" % file.errorString()); + goto after; + } + + file.write(content); + + m_url = QUrl::fromLocalFile(file.fileName()); + } + } + + m_actionOpenReport->setEnabled(true); + + after: + m_actionRefreshReport->setEnabled(true); + m_reply = Q_NULLPTR; +} + +void ReportsWidget::openReport() +{ + if(!QDesktopServices::openUrl(m_url)) + QMessageBox::warning(this, tr("Could not launch your default PDF viewer!"), tr("Could not launch your default PDF viewer!")); +} diff --git a/reportswidget.h b/reportswidget.h new file mode 100644 index 0000000..46ad7d9 --- /dev/null +++ b/reportswidget.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include + +#include "replies/getreportreply.h" + +class QAction; + +class MainWindow; + +class ReportsWidget : public QLabel +{ + Q_OBJECT + +public: + explicit ReportsWidget(MainWindow &mainWindow); + +private Q_SLOTS: + void dateChanged(const QDate &date); + void refresh(); + void finished(); + void openReport(); + +private: + MainWindow &m_mainWindow; + + QAction *m_actionOpenReport; + QAction *m_actionRefreshReport; + + QDate m_date; + QUrl m_url; + + std::unique_ptr m_reply; +}; diff --git a/translations/reportsplugin_de.ts b/translations/reportsplugin_de.ts new file mode 100644 index 0000000..14f8202 --- /dev/null +++ b/translations/reportsplugin_de.ts @@ -0,0 +1,49 @@ + + + + + ReportsWidget + + + Refresh report + Auswertung aktualisieren + + + + Open report + Auswertung öffnen + + + + ??? + ??? + + + + + Balance: %0, Holidays: %1 + Gleitzeit: %0, Urlaub: %1 + + + + Could not load report! + Konnte Auswertung nicht laden! + + + + + n/a + n/v + + + + Could not write report! + Konnte Auswertung nicht abspeichern! + + + + Could not launch your default PDF viewer! + Konnte Standard-PDF-Viewer nicht öffnen! + + + diff --git a/translations/reportsplugin_en.ts b/translations/reportsplugin_en.ts new file mode 100644 index 0000000..ffb6a03 --- /dev/null +++ b/translations/reportsplugin_en.ts @@ -0,0 +1,49 @@ + + + + + ReportsWidget + + + Refresh report + + + + + Open report + + + + + ??? + + + + + + Balance: %0, Holidays: %1 + + + + + Could not load report! + + + + + + n/a + + + + + Could not write report! + + + + + Could not launch your default PDF viewer! + + + +