From 75c98c18ad806eb906aa1ca810081ee9b579cd73 Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Tue, 13 Aug 2024 14:55:05 +0200 Subject: [PATCH] Added: Calculator::computeBruttoDurationInSecs() and Calculator::computeBruttoDateTime() (based on it). --- library/src/calculator_functions.cpp | 111 +++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/library/src/calculator_functions.cpp b/library/src/calculator_functions.cpp index fe133f1..6a92ee3 100644 --- a/library/src/calculator_functions.cpp +++ b/library/src/calculator_functions.cpp @@ -117,6 +117,117 @@ QDateTime Calculator::GetDailyTicketDuration(Configuration* cfg, const QDateTime return QDateTime(); } + +int Calculator::computeBruttoDurationInSecs(Configuration const *cfg, + QDateTime const &start, + int nettoDurationInSecs, + bool maxBrutto) { + QDateTime from(start); + int bruttoDurationInSecs = 0; + + if (DBG_LEVEL >= DBG_DEBUG) { + qCritical() << DBG_HEADER << "netto duration in secs" << nettoDurationInSecs; + } + + while (nettoDurationInSecs > 0) { + int day_in_week_id = from.date().dayOfWeek(); + int count = cfg->WeekDaysWorktime.count(day_in_week_id); + + if (DBG_LEVEL >= DBG_DEBUG) { + qCritical() << DBG_HEADER << "count for " << day_in_week_id << ": " << count; + } + + if (count > 0) { + for (auto[iter, rEnd] = cfg->WeekDaysWorktime.equal_range(day_in_week_id); iter != rEnd; ++iter) { + QTime const &pwd_time_from = QTime::fromString(QString::fromStdString(iter->second.pwd_time_from), Qt::ISODate); + QTime const &pwd_time_to = QTime::fromString(QString::fromStdString(iter->second.pwd_time_to), Qt::ISODate); + QTime fromTime = from.time(); + + if (DBG_LEVEL >= DBG_DEBUG) { + qCritical() << DBG_HEADER; + qCritical() << DBG_HEADER << " wd-from" << pwd_time_from.toString(Qt::ISODate); + qCritical() << DBG_HEADER << " wd-to" << pwd_time_to.toString(Qt::ISODate); + qCritical() << DBG_HEADER << "fromTime" << fromTime.toString(Qt::ISODate); + qCritical() << DBG_HEADER << " from" << from.toString(Qt::ISODate); + qCritical() << DBG_HEADER; + } + + if (fromTime <= pwd_time_from) { + int secs = fromTime.secsTo(pwd_time_from); + bruttoDurationInSecs += secs; + fromTime = pwd_time_from; + from.setTime(pwd_time_from); + } + + if (DBG_LEVEL >= DBG_DEBUG) { + qCritical() << DBG_HEADER << "fromTime" << fromTime.toString(Qt::ISODate); + qCritical() << DBG_HEADER << " from" << from.toString(Qt::ISODate); + qCritical() << DBG_HEADER << " netto" << nettoDurationInSecs; + qCritical() << DBG_HEADER << " brutto" << bruttoDurationInSecs; + qCritical() << DBG_HEADER << " br-date" << start.addSecs(bruttoDurationInSecs); + } + + while (fromTime < pwd_time_to && nettoDurationInSecs > 0) { + bruttoDurationInSecs += 60; + nettoDurationInSecs -= 60; + fromTime = fromTime.addSecs(60); + from = from.addSecs(60); + } + + if (DBG_LEVEL >= DBG_DEBUG) { + qCritical() << DBG_HEADER; + qCritical() << DBG_HEADER << " netto" << nettoDurationInSecs; + qCritical() << DBG_HEADER << " brutto" << bruttoDurationInSecs; + qCritical() << DBG_HEADER << " br-date" << start.addSecs(bruttoDurationInSecs); + qCritical() << DBG_HEADER << "fromTime" << fromTime.toString(Qt::ISODate); + qCritical() << DBG_HEADER << " from" << from.toString(Qt::ISODate); + qCritical() << DBG_HEADER; + } + + if (--count == 0 && fromTime == pwd_time_to) { + // handle rest + QDateTime midnight(from); + midnight = midnight.addDays(1); + midnight.setTime(QTime(0, 0, 0)); + int secs = from.secsTo(midnight); + bruttoDurationInSecs += secs; + from = midnight; + + if (DBG_LEVEL >= DBG_DEBUG) { + qCritical() << DBG_HEADER; + qCritical() << DBG_HEADER << " netto" << nettoDurationInSecs; + qCritical() << DBG_HEADER << " brutto" << bruttoDurationInSecs; + qCritical() << DBG_HEADER << " br-date" << start.addSecs(bruttoDurationInSecs); + qCritical() << DBG_HEADER << " from" << from.toString(Qt::ISODate); + qCritical() << DBG_HEADER; + } + } + } + + } else { + bruttoDurationInSecs += (24 * 3600); + from = from.addDays(1); + } + } + + if (maxBrutto) { + qCritical() << DBG_HEADER << "TODO"; + } + + if (DBG_LEVEL >= DBG_DEBUG) { + qCritical() << DBG_HEADER << " br-date" << start.addSecs(bruttoDurationInSecs); + } + + return bruttoDurationInSecs; +} + +QDateTime Calculator::computeBruttoDateTime(Configuration const *cfg, + QDateTime const &from, + int durationInSecs, + bool maxBrutto) { + return from.addSecs(computeBruttoDurationInSecs(cfg, from, durationInSecs, maxBrutto)); +} + /// std::string Calculator::GetDurationFromCost(Configuration* cfg, uint8_t payment_option,