From 356e451839973d9dde9b20ac7f67376de11a92ba Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Fri, 27 Sep 2024 13:35:03 +0200 Subject: [PATCH] Add structures to encode service and out-of-service times --- .../include/mobilisis/tariff_out_of_service.h | 79 ++++++++++++++++++ library/include/mobilisis/tariff_service.h | 81 +++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 library/include/mobilisis/tariff_out_of_service.h create mode 100644 library/include/mobilisis/tariff_service.h diff --git a/library/include/mobilisis/tariff_out_of_service.h b/library/include/mobilisis/tariff_out_of_service.h new file mode 100644 index 0000000..f3ebe1e --- /dev/null +++ b/library/include/mobilisis/tariff_out_of_service.h @@ -0,0 +1,79 @@ +#ifndef TARIFF_OUT_OF_SERVICE_H_INCLUDED +#define TARIFF_OUT_OF_SERVICE_H_INCLUDED + +#include +#include + +#include "time_range.h" + +enum class ApplyOutOfService { + NEVER = 0, + MATCH_PREV_DAY = 1, + MATCH_NEXT_DAY = 2, + ALWAYS = 3 +}; + +struct ATBTariffOutOfService { + int m_id; + QString m_weekDay; + QDate m_date; + TimeRange m_range; + ApplyOutOfService m_outOfServiceIf; + + explicit ATBTariffOutOfService() + : m_id(-1) + , m_outOfServiceIf(ApplyOutOfService::NEVER) { + } + + void setOutOfServiceIf(QString const &oosif) { + if (oosif == "never") { + m_outOfServiceIf = ApplyOutOfService::NEVER; + } else + if (oosif == "match_prev_day") { + m_outOfServiceIf = ApplyOutOfService::MATCH_PREV_DAY; + } else + if (oosif == "match_next_day") { + m_outOfServiceIf = ApplyOutOfService::MATCH_NEXT_DAY; + } else + if (oosif == "always") { + m_outOfServiceIf = ApplyOutOfService::ALWAYS; + } else { + qCritical() << "ERROR unknown servcie application" << oosif; + } + } + + ApplyOutOfService outOfServiceIf() const { + return m_outOfServiceIf; + } + + QString outOfServiceIfStr() const { + if (m_outOfServiceIf == ApplyOutOfService::NEVER) { + return "never"; + } + if (m_outOfServiceIf == ApplyOutOfService::ALWAYS) { + return "always"; + } + if (m_outOfServiceIf == ApplyOutOfService::MATCH_PREV_DAY) { + return "match prev day"; + } + if (m_outOfServiceIf == ApplyOutOfService::MATCH_NEXT_DAY) { + return "match next day"; + } + return QString("ERROR unknown out of service application: %1").arg(static_cast(m_outOfServiceIf)); + } + friend QDebug operator<<(QDebug debug, ATBTariffOutOfService const &oos) { + QDebugStateSaver saver(debug); + + debug.nospace() + << "\nTariffOutOfService:\n" + << " week day: " << oos.m_weekDay << "\n" + << " date: " << oos.m_date.toString(Qt::ISODate) << "\n" + << " id: " << oos.m_id << "\n" + << " start: " << oos.m_range.m_start << "\n" + << " end: " << oos.m_range.m_end << "\n" + << " duration: " << oos.m_range.m_duration << endl; + return debug; + } +}; + +#endif // TARIFF_SERVICE_H_INCLUDED diff --git a/library/include/mobilisis/tariff_service.h b/library/include/mobilisis/tariff_service.h new file mode 100644 index 0000000..601fbe2 --- /dev/null +++ b/library/include/mobilisis/tariff_service.h @@ -0,0 +1,81 @@ +#ifndef TARIFF_SERVICE_H_INCLUDED +#define TARIFF_SERVICE_H_INCLUDED + +#include +#include + +#include "time_range.h" + +enum class ApplyService { + NEVER = 0, + MATCH_PREV_DAY = 1, + MATCH_NEXT_DAY = 2, + ALWAYS = 3 +}; + +struct ATBTariffService { + int m_id; + QString m_weekDay; + QDate m_date; + TimeRange m_range; + ApplyService m_serviceIf; + + explicit ATBTariffService() + : m_id(-1) + , m_serviceIf(ApplyService::NEVER) { + } + + void setServiceIf(QString const &sif) { + if (sif == "never") { + m_serviceIf = ApplyService::NEVER; + } else + if (sif == "match_prev_day") { + m_serviceIf = ApplyService::MATCH_PREV_DAY; + } else + if (sif == "match_next_day") { + m_serviceIf = ApplyService::MATCH_NEXT_DAY; + } else + if (sif == "always") { + m_serviceIf = ApplyService::ALWAYS; + } else { + qCritical() << "ERROR unknown servcie application" << sif; + } + } + + ApplyService serviceIf() const { + return m_serviceIf; + } + + QString serviceIfStr() const { + if (m_serviceIf == ApplyService::NEVER) { + return "never"; + } + if (m_serviceIf == ApplyService::ALWAYS) { + return "always"; + } + if (m_serviceIf == ApplyService::MATCH_PREV_DAY) { + return "match prev day"; + } + if (m_serviceIf == ApplyService::MATCH_NEXT_DAY) { + return "match next day"; + } + return QString("ERROR unknown service application: %1").arg(static_cast(m_serviceIf)); + } + + friend QDebug operator<<(QDebug debug, ATBTariffService const &ts) { + QDebugStateSaver saver(debug); + + debug.nospace() + << "\nTariffService:\n" + << " week day: " << ts.m_weekDay << "\n" + << " date: " << ts.m_date.toString(Qt::ISODate) << "\n" + << " id: " << ts.m_id << "\n" + << " start: " << ts.m_range.m_start << "\n" + << " end: " << ts.m_range.m_end << "\n" + << " duration: " << ts.m_range.m_duration << "\n" + << " prepaid if: " << ts.serviceIfStr() << endl; + return debug; + } +}; + +#endif // TARIFF_SERVICE_H_INCLUDED