180 lines
6.6 KiB
C++
180 lines
6.6 KiB
C++
#ifndef MOBILISIS_COMPACT_TARIFF_H
|
|
#define MOBILISIS_COMPACT_TARIFF_H
|
|
|
|
#include "compact_calendar.h"
|
|
#include <QDateTime>
|
|
#include <QFileInfo>
|
|
#include <QDir>
|
|
#include <QMap>
|
|
|
|
namespace Mobilisis {
|
|
|
|
class CompactTariff {
|
|
public:
|
|
bool load(const QString &path, QString &error)
|
|
{
|
|
QFile file(path);
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
error = file.errorString();
|
|
return false;
|
|
}
|
|
|
|
QJsonParseError parseError;
|
|
const auto root = QJsonDocument::fromJson(file.readAll(), &parseError).object();
|
|
if (parseError.error != QJsonParseError::NoError ||
|
|
root.value("schemaVersion").toDouble() != 2) {
|
|
error = QStringLiteral("Expected compact tariff schemaVersion 2");
|
|
return false;
|
|
}
|
|
|
|
CompactTariff next;
|
|
if (!index(root.value("products"), next.products) ||
|
|
!index(root.value("ratePlans"), next.plans) ||
|
|
!index(root.value("workingDayProfiles"), next.profiles)) {
|
|
error = QStringLiteral("Missing, empty or duplicate compact tariff IDs");
|
|
return false;
|
|
}
|
|
next.seasons = root.value("seasons").toArray();
|
|
|
|
const auto settings = root.value("calendar").toObject();
|
|
// Tariff calendars always follow the machine's local clock.
|
|
// Ignore timezone fields retained in older compact tariff files.
|
|
|
|
for (const auto &product : next.products) {
|
|
for (const auto &id : product.value("workingDayProfileIds").toArray()) {
|
|
if (!next.profiles.contains(id.toString())) {
|
|
error = QStringLiteral("Unknown product working-day profile: %1").arg(id.toString());
|
|
return false;
|
|
}
|
|
}
|
|
if (product.contains("holidayProfileId") &&
|
|
!next.profiles.contains(product.value("holidayProfileId").toString())) {
|
|
error = QStringLiteral("Unknown product holiday profile");
|
|
return false;
|
|
}
|
|
if (!next.validMapping(product.value("ratePlansByPeriod").toObject())) {
|
|
error = QStringLiteral("Invalid base rate-plan mapping");
|
|
return false;
|
|
}
|
|
for (const auto &override : product.value("seasonalRatePlansByPeriod").toObject()) {
|
|
if (!next.validMapping(override.toObject())) {
|
|
error = QStringLiteral("Invalid seasonal rate-plan mapping");
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (settings.contains("holidayFile")) {
|
|
const auto name = settings.value("holidayFile").toString();
|
|
if (name.isEmpty() || QDir::isAbsolutePath(name)) {
|
|
error = QStringLiteral("holidayFile must be a relative path");
|
|
return false;
|
|
}
|
|
if (!next.calendar.load(QFileInfo(path).dir().filePath(name),
|
|
settings.value("holidayProfileId").toString(),
|
|
next.profiles.keys(), error))
|
|
return false;
|
|
}
|
|
*this = next;
|
|
error.clear();
|
|
return true;
|
|
}
|
|
|
|
QString vehicleType(const QString &productId) const
|
|
{
|
|
return products.value(productId).value("vehicleType").toString();
|
|
}
|
|
|
|
bool profileForDate(const QString &productId, const QDate &date,
|
|
QJsonObject &profile, QString &error) const
|
|
{
|
|
profile = QJsonObject();
|
|
if (!products.contains(productId)) {
|
|
error = QStringLiteral("Unknown tariff product: %1").arg(productId);
|
|
return false;
|
|
}
|
|
QString profileId;
|
|
if (!calendar.resolve(date, profileId, error))
|
|
return false;
|
|
if (!profileId.isEmpty()) {
|
|
profile = profiles.value(profileId);
|
|
return true;
|
|
}
|
|
|
|
const QStringList weekdays = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
|
|
const auto day = weekdays.at(date.dayOfWeek() - 1);
|
|
for (const auto &id : products.value(productId).value("workingDayProfileIds").toArray()) {
|
|
const auto candidate = profiles.value(id.toString());
|
|
if (candidate.value("weekdays").toArray().contains(QJsonValue(day))) {
|
|
if (!profile.isEmpty()) {
|
|
error = QStringLiteral("Multiple weekday profiles for %1 on %2").arg(productId, day);
|
|
return false;
|
|
}
|
|
profile = candidate;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
QDateTime localTime(const QDateTime &instant) const
|
|
{
|
|
return instant.toLocalTime();
|
|
}
|
|
|
|
QJsonObject planForPeriod(const QString &productId, const QString &periodId,
|
|
const QDateTime &parkingStart) const
|
|
{
|
|
const auto product = products.value(productId);
|
|
const auto monthDay = localTime(parkingStart).date().toString("MM-dd");
|
|
const auto overrides = product.value("seasonalRatePlansByPeriod").toObject();
|
|
for (const auto &item : seasons) {
|
|
const auto season = item.toObject();
|
|
const auto from = season.value("from").toString();
|
|
const auto to = season.value("to").toString();
|
|
const bool matches = from <= to ? from <= monthDay && monthDay <= to
|
|
: from <= monthDay || monthDay <= to;
|
|
if (matches) {
|
|
const auto planId = overrides.value(season.value("id").toString())
|
|
.toObject().value(periodId).toString();
|
|
if (!planId.isEmpty())
|
|
return plans.value(planId);
|
|
break;
|
|
}
|
|
}
|
|
return plans.value(product.value("ratePlansByPeriod").toObject().value(periodId).toString());
|
|
}
|
|
|
|
public:
|
|
static bool index(const QJsonValue &value, QMap<QString, QJsonObject> &result)
|
|
{
|
|
const auto entries = value.toArray();
|
|
for (const auto &entry : entries) {
|
|
const auto object = entry.toObject();
|
|
const auto id = object.value("id").toString();
|
|
if (id.isEmpty() || result.contains(id))
|
|
return false;
|
|
result.insert(id, object);
|
|
}
|
|
return !entries.isEmpty();
|
|
}
|
|
|
|
bool validMapping(const QJsonObject &mapping) const
|
|
{
|
|
for (const auto &value : mapping) {
|
|
if (!plans.contains(value.toString()))
|
|
return false;
|
|
}
|
|
return !mapping.isEmpty();
|
|
}
|
|
|
|
CompactCalendar calendar;
|
|
QJsonArray seasons;
|
|
QMap<QString, QJsonObject> products;
|
|
QMap<QString, QJsonObject> plans;
|
|
QMap<QString, QJsonObject> profiles;
|
|
};
|
|
|
|
} // namespace Mobilisis
|
|
|
|
#endif
|