From a72f5a50193d760980491077bcffabd3b70ec312 Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Wed, 2 Oct 2024 15:15:19 +0200 Subject: [PATCH] getPrepaid(): Check for special days (holidays) with prepaid sections. Holidays have higher priority than usual days. --- library/src/calculator_functions.cpp | 41 ++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/library/src/calculator_functions.cpp b/library/src/calculator_functions.cpp index 305464e..c717675 100644 --- a/library/src/calculator_functions.cpp +++ b/library/src/calculator_functions.cpp @@ -131,17 +131,52 @@ std::optional getPrepaid(Configuration const *cfg, QDateTime c std::optional value = std::nullopt; int weekDay = dt.date().dayOfWeek(); + + // qCritical() << __func__ << ":" << __LINE__ << dt.toString(Qt::ISODate) << weekDay; + ATBTime inputTime(dt.time()); + QDate d; // check if a special date is configured in tariff-file for this day auto const &prepaidRange = cfg->TariffPrepaids.equal_range(weekDay); + for (auto i = prepaidRange.first; i != prepaidRange.second; ++i) { ATBTariffPrepaid const &prepaid = i->second; - TimeRange const &prepaidTimeRange = prepaid.m_range; - if (inputTime >= prepaidTimeRange.m_start && inputTime < prepaidTimeRange.m_end) { - value = value.value_or(i->second); + if (!prepaid.m_date.isNull() && prepaid.m_date.isValid() && prepaid.m_date == dt.date()) { + d = dt.date(); + // qCritical() << __func__ << ":" << __LINE__ << "found special day" << d.toString(Qt::ISODate); break; } } + if (!d.isNull() && d.isValid()) { + for (auto i = prepaidRange.first; i != prepaidRange.second; ++i) { + ATBTariffPrepaid const &prepaid = i->second; + if (!prepaid.m_date.isNull() && prepaid.m_date.isValid() && prepaid.m_date == d) { + TimeRange const &prepaidTimeRange = prepaid.m_range; + if (inputTime >= prepaidTimeRange.m_start && inputTime < prepaidTimeRange.m_end) { + // qCritical() << __func__ << ":" << __LINE__ << prepaidTimeRange.m_start.toString(Qt::ISODate); + // qCritical() << __func__ << ":" << __LINE__ << prepaidTimeRange.m_end.toString(Qt::ISODate); + value = value.value_or(i->second); + break; + } + } + } + } else { + // qCritical() << __func__ << ":" << __LINE__ << "no special day" << dt.date().toString(Qt::ISODate); + for (auto i = prepaidRange.first; i != prepaidRange.second; ++i) { + ATBTariffPrepaid const &prepaid = i->second; + if (prepaid.m_date.isNull() || !prepaid.m_date.isValid()) { + qCritical() << __func__ << ":" << __LINE__ << "default"; + TimeRange const &prepaidTimeRange = prepaid.m_range; + if (inputTime >= prepaidTimeRange.m_start && inputTime < prepaidTimeRange.m_end) { + // qCritical() << __func__ << ":" << __LINE__ << prepaidTimeRange.m_start.toString(Qt::ISODate); + // qCritical() << __func__ << ":" << __LINE__ << prepaidTimeRange.m_end.toString(Qt::ISODate); + value = value.value_or(i->second); + break; + } + } + } + } + return value; }