2018-11-16 22:01:13 +01:00
|
|
|
#include "exportdialog.h"
|
|
|
|
|
#include "ui_exportdialog.h"
|
|
|
|
|
|
2018-11-16 23:15:02 +01:00
|
|
|
// Qt includes
|
|
|
|
|
#include <QFileDialog>
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
2018-12-13 23:17:22 +01:00
|
|
|
// dbcorelib includes
|
|
|
|
|
#include "utils/timeutils.h"
|
|
|
|
|
|
2018-11-16 23:15:02 +01:00
|
|
|
// zeiterfassungcorelib includes
|
|
|
|
|
#include "utils/timeutils.h"
|
|
|
|
|
|
|
|
|
|
// zeiterfassungguilib includes
|
2018-11-16 22:01:13 +01:00
|
|
|
#include "mainwindow.h"
|
|
|
|
|
|
2018-11-16 23:15:02 +01:00
|
|
|
// zeiterfassungnetworklib includes
|
|
|
|
|
#include "zeiterfassungapi.h"
|
|
|
|
|
#include "replies/getbookingsreply.h"
|
|
|
|
|
|
2018-11-16 22:01:13 +01:00
|
|
|
ExportDialog::ExportDialog(MainWindow &mainWindow, QWidget *parent) :
|
|
|
|
|
ZeiterfassungDialog(parent),
|
|
|
|
|
ui(new Ui::ExportDialog),
|
|
|
|
|
m_mainWindow(mainWindow)
|
|
|
|
|
{
|
|
|
|
|
ui->setupUi(this);
|
2018-11-16 23:15:02 +01:00
|
|
|
|
|
|
|
|
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)));
|
2018-11-16 22:01:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExportDialog::~ExportDialog()
|
|
|
|
|
{
|
|
|
|
|
delete ui;
|
|
|
|
|
}
|
2018-11-16 23:15:02 +01:00
|
|
|
|
|
|
|
|
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<QFile>(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);
|
|
|
|
|
|
2018-12-13 23:17:22 +01:00
|
|
|
m_textStream.setDevice(m_file.get());
|
2018-11-16 23:15:02 +01:00
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
2018-12-13 23:17:22 +01:00
|
|
|
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("<b>%0</b> %1<br/>").arg(date, msg));
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-16 23:15:02 +01:00
|
|
|
if(m_reply->success())
|
|
|
|
|
{
|
2018-12-13 23:17:22 +01:00
|
|
|
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)
|
2018-11-16 23:15:02 +01:00
|
|
|
{
|
2018-12-13 23:17:22 +01:00
|
|
|
if(iter == m_reply->bookings().constEnd())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
const auto &startBooking = *(iter++);
|
|
|
|
|
logBooking(startBooking);
|
|
|
|
|
|
|
|
|
|
if(startBooking.type != "K")
|
|
|
|
|
{
|
|
|
|
|
log("<span style=\"color: red;\">unexpected booking type!</span>");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(iter == m_reply->bookings().constEnd())
|
|
|
|
|
{
|
|
|
|
|
log("<span style=\"color: red;\">missing end booking!</span>");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto &endBooking = *(iter++);
|
|
|
|
|
logBooking(endBooking);
|
|
|
|
|
|
|
|
|
|
if(endBooking.type != "G")
|
|
|
|
|
{
|
|
|
|
|
log("<span style=\"color: red;\">unexpected booking type!</span>");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
totalSeconds += startBooking.time.secsTo(endBooking.time);
|
2018-11-16 23:15:02 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-13 23:17:22 +01:00
|
|
|
const auto totalTime = QTime(0, 0).addSecs(totalSeconds).toString(QStringLiteral("HH:mm:ss"));
|
|
|
|
|
|
|
|
|
|
m_textStream << date << ';' << totalTime << endl;
|
|
|
|
|
|
|
|
|
|
log(QString("<span style=\"color: darkgreen;\">succeeded (%0)</span>!").arg(totalTime));
|
2018-11-16 23:15:02 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-13 23:17:22 +01:00
|
|
|
log(QString("<span style=\"color: red;\">failed to load</span>: %1").arg(m_reply->message()));
|
2018-11-16 23:15:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|