From e20eb93abfdd3bb5173fd7810aad0d49da9280e4 Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Fri, 19 Apr 2024 13:21:38 +0200 Subject: [PATCH] Implement SpecialDaysWorkTimeFrom() and SpecialDaysWorkTimeUntil(). Extend getBusinessHours() to include new enum-values. Implement isDayIncluded() as switch-case on dayOfWeek. Implement dumpBusinessHours(). --- library/src/utilities.cpp | 107 +++++++++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 2 deletions(-) diff --git a/library/src/utilities.cpp b/library/src/utilities.cpp index c986208..d44278c 100644 --- a/library/src/utilities.cpp +++ b/library/src/utilities.cpp @@ -1,5 +1,6 @@ #include "utilities.h" #include "tariff_log.h" +#include "tariff_business_hours.h" #include #include @@ -333,6 +334,14 @@ QTime Utilities::SpecialDaysWorkTimeFrom(Configuration const *cfg, int specialDa return QTime::fromString(cfg->SpecialDaysWorktime.find(specialDayId)->second.pedwt_time_from.c_str(), Qt::ISODate); } +QTime Utilities::SpecialDaysWorkTimeFrom(Configuration::SpecialDaysWorktimeType::const_iterator it) { + return QTime::fromString(it->second.pedwt_time_from.c_str(), Qt::ISODate); +} + +QTime Utilities::SpecialDaysWorkTimeUntil(Configuration::SpecialDaysWorktimeType::const_iterator it) { + return QTime::fromString(it->second.pedwt_time_to.c_str(), Qt::ISODate); +} + QTime Utilities::SpecialDaysWorkTimeUntil(Configuration const *cfg, int specialDayId) { return QTime::fromString(cfg->SpecialDaysWorktime.find(specialDayId)->second.pedwt_time_to.c_str(), Qt::ISODate); } @@ -420,10 +429,10 @@ uint32_t Utilities::getFirstDurationStep(Configuration const *cfg, PaymentMethod } BusinessHours Utilities::getBusinessHours(Configuration const *cfg, PaymentMethod methodId) { - int businessHours = cfg->PaymentOption.find(methodId)->second.pop_business_hours; + uint64_t businessHours = cfg->PaymentOption.find(methodId)->second.pop_business_hours; switch (businessHours) { - case NoRestriction_24_7: return BusinessHours::NoRestriction_24_7; + //case NoRestriction_24_7: return BusinessHours::NoRestriction_24_7; case OnlyWorkingDays: return BusinessHours::OnlyWorkingDays; case OnlyWeekDays: return BusinessHours::OnlyWeekDays; case OnlyWeekEnd: return BusinessHours::OnlyWeekEnd; @@ -433,6 +442,21 @@ BusinessHours Utilities::getBusinessHours(Configuration const *cfg, PaymentMetho case SpecialAndSchoolHolidays: return BusinessHours::SpecialAndSchoolHolidays; case OnlyOpenForBusinessDays: return BusinessHours::OnlyOpenForBusinessDays; case AllDaysWithRestrictedHours: return BusinessHours::AllDaysWithRestrictedHours; + case _NO_RESTRICTION_24_7_: return BusinessHours::NO_RESTRICTION_24_7; + case _MON_: return BusinessHours::MON; + case _TUE_: return BusinessHours::TUE; + case _WED_: return BusinessHours::WED; + case _THU_: return BusinessHours::THU; + case _FRI_: return BusinessHours::FRI; + case _SAT_: return BusinessHours::SAT; + case _SUN_: return BusinessHours::SUN; + case _WEEK_DAYS_: return BusinessHours::WEEK_DAYS; + case _WORKING_DAYS_: return BusinessHours::WORKING_DAYS; + case _ALL_DAYS_: return BusinessHours::ALL_DAYS; + case _OFFICIAL_HOLIDAY_: return BusinessHours::OFFICIAL_HOLIDAY; + case _ONLY_WEEKEND_: return BusinessHours::ONLY_WEEKEND; + case _ONLY_OPEN_FOR_BUSINESS_DAYS_: return BusinessHours::ONLY_OPEN_FOR_BUSINESS_DAYS; + case _NOT_DEFINED_: return BusinessHours::NOT_DEFINED; } return BusinessHours::NoBusinessHoursDefined; } @@ -447,3 +471,82 @@ double Utilities::computeWeekDaysDurationUnit(Configuration const *cfg, PaymentM int durationId = cfg->PaymentRate.find(pop_id)->second.pra_payment_unit_id; return (double)(cfg->Duration.find(durationId)->second.pun_duration); } + +bool Utilities::isDayIncluded(uint64_t businessHours, QDateTime const &dt) { + int dayOfWeek = dt.date().dayOfWeek(); + switch (dayOfWeek) { + case Qt::Monday: + return ((businessHours & BusinessHours::MON) == BusinessHours::MON); + case Qt::Tuesday: + return ((businessHours & BusinessHours::TUE) == BusinessHours::TUE); + case Qt::Wednesday: + return ((businessHours & BusinessHours::WED) == BusinessHours::WED); + case Qt::Thursday: + return ((businessHours & BusinessHours::THU) == BusinessHours::THU); + case Qt::Friday: + return ((businessHours & BusinessHours::FRI) == BusinessHours::FRI); + case Qt::Saturday: + return ((businessHours & BusinessHours::SAT) == BusinessHours::SAT); + case Qt::Sunday: + return ((businessHours & BusinessHours::SUN) == BusinessHours::SUN); + default:; + } + + return false; +} + +QStringList Utilities::dumpBusinessHours(uint64_t businessHours) { + QStringList s; + + if ((businessHours & BusinessHours::MON) == BusinessHours::MON) { + if ((businessHours & BusinessHours::OFFICIAL_HOLIDAY) == BusinessHours::OFFICIAL_HOLIDAY) { + s << "MON (Holiday)"; + } else { + s << "MON"; + } + } + if ((businessHours & BusinessHours::TUE) == BusinessHours::TUE) { + if ((businessHours & BusinessHours::OFFICIAL_HOLIDAY) == BusinessHours::OFFICIAL_HOLIDAY) { + s << "TUE (Holiday)"; + } else { + s << "TUE"; + } + } + if ((businessHours & BusinessHours::WED) == BusinessHours::WED) { + if ((businessHours & BusinessHours::OFFICIAL_HOLIDAY) == BusinessHours::OFFICIAL_HOLIDAY) { + s << "WED (Holiday)"; + } else { + s << "WED"; + } + } + if ((businessHours & BusinessHours::THU) == BusinessHours::THU) { + if ((businessHours & BusinessHours::OFFICIAL_HOLIDAY) == BusinessHours::OFFICIAL_HOLIDAY) { + s << "THU (Holiday)"; + } else { + s << "THU"; + } + } + if ((businessHours & BusinessHours::FRI) == BusinessHours::FRI) { + if ((businessHours & BusinessHours::OFFICIAL_HOLIDAY) == BusinessHours::OFFICIAL_HOLIDAY) { + s << "FRI (Holiday)"; + } else { + s << "FRI"; + } + } + if ((businessHours & BusinessHours::SAT) == BusinessHours::SAT) { + if ((businessHours & BusinessHours::OFFICIAL_HOLIDAY) == BusinessHours::OFFICIAL_HOLIDAY) { + s << "SAT (Holiday)"; + } else { + s << "SAT"; + } + } + if ((businessHours & BusinessHours::SUN) == BusinessHours::SUN) { + if ((businessHours & BusinessHours::OFFICIAL_HOLIDAY) == BusinessHours::OFFICIAL_HOLIDAY) { + s << "SUN (Holiday)"; + } else { + s << "SUN"; + } + } + + return s; +}