ATBTariffCalculator/CalculatorCInterface/tariff_editor.cpp

424 lines
16 KiB
C++
Raw Normal View History

2024-03-04 16:31:14 +01:00
#include "tariff_editor.h"
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QDateTime>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
QByteArray TariffEditor::readJson(QString const &filename) {
QFile f(filename);
if (f.exists()) {
if (f.open(QFile::ReadOnly | QFile::Text)) {
QTextStream in(&f);
return in.readAll().toUtf8();
} else {
qCritical() << filename << "not readable";
}
} else {
qCritical() << filename << "does not exist";
}
return QByteArray();
}
TariffEditor::TariffEditor() {
// QByteArray ba = readJson("/opt/ptu5/opt/ATBTariffCalculator/Utilities/tariff.template.json");
//QJsonParseError pe;
//m_jsonDoc = QJsonDocument::fromJson(ba, &pe);
//if (pe.error == QJsonParseError::NoError) {
// qCritical().noquote() << m_jsonDoc.toJson(QJsonDocument::JsonFormat::Indented);
//} else {
// qCritical() << __func__ << pe.errorString();
//
//}
m_a = QJsonArray();
createJsonValue("Project", "Szeged");
createJsonValue("Version", "1.0.0");
createJsonValue("Date", "01.01.1970");
createJsonValue("Committer", "");
createJsonValue("Info", "");
createJsonValue("Products");
createJsonValue("Accuracy");
createJsonValue("Type");
createJsonValue("Price");
createJsonValue("Config");
createJsonValue("TimeRanges");
createJsonValue("Periods");
createJsonValue("DayConfigs");
createJsonValue("Days");
m_jsonDoc.setArray(m_a);
qCritical().noquote() << m_jsonDoc.toJson(QJsonDocument::JsonFormat::Indented);
}
void TariffEditor::createJsonValue(QString const &key, QString const &value) {
if ((key.compare("Project", Qt::CaseInsensitive) == 0)
|| (key.compare("Version", Qt::CaseInsensitive) == 0)
|| (key.compare("Date", Qt::CaseInsensitive) == 0)
|| (key.compare("Committer", Qt::CaseInsensitive) == 0)
|| (key.compare("Info", Qt::CaseInsensitive) == 0)) {
m_a.push_back(QJsonValue({QPair(key, QJsonValue(value))}));
} else
if (key.compare("Accuracy", Qt::CaseInsensitive) == 0) {
QJsonObject o({
QPair(QString("accuracy_id"), QJsonValue(1)),
QPair(QString("accuracy_label"), QJsonValue(QString("computation-to-the-minute"))),
QPair(QString("accuracy_value"), QJsonValue(1)),
QPair(QString("comment"), QJsonValue(QString("accuracy of tariff computation is individual minutes")))});
m_a.push_back(QJsonObject({QPair(key, QJsonValue(o))}));
} else
if (key.compare("Type", Qt::CaseInsensitive) == 0) {
QJsonObject o({
QPair(QString("type_id"), QJsonValue(1)),
QPair(QString("type_label"), QJsonValue(QString("uniform-minutes"))),
QPair(QString("comment"), QJsonValue(QString("same price for all minutes")))});
m_a.push_back(QJsonObject({QPair(key, QJsonValue(o))}));
} else
if (key.compare("Price", Qt::CaseInsensitive) == 0) {
QJsonObject o1({
QPair(QString("price_id"), QJsonValue(1)),
QPair(QString("price"), QJsonValue(165)),
QPair(QString("editable"), QJsonValue(true)),
QPair(QString("comment"), QJsonValue(QString("price per hour as provided by customer")))});
QJsonObject o2({
QPair(QString("price_id"), QJsonValue(2)),
QPair(QString("price"), QJsonValue(990)),
QPair(QString("editable"), QJsonValue(true)),
QPair(QString("comment"), QJsonValue(QString("price for dayticket as provided by customer")))});
QJsonArray a;
a.push_back(o1);
a.push_back(o2);
m_a.push_back(QJsonObject({QPair(key, QJsonValue(a))}));
} else
if (key.compare("Config", Qt::CaseInsensitive) == 0) {
QJsonObject o1({
QPair(QString("value"), QJsonValue(15)),
QPair(QString("editable"), QJsonValue(true)),
QPair(QString("comment"), QJsonValue(QString("minimal parking time in minutes (net)")))});
QJsonObject o2({
QPair(QString("value"), QJsonValue(360)),
QPair(QString("editable"), QJsonValue(true)),
QPair(QString("comment"), QJsonValue(QString("maximal parking time in minutes (net)")))});
QJsonObject o3({
QPair(QString("value"), QJsonValue(1)),
QPair(QString("comment"), QJsonValue(QString("price-id of customer entered price (for hour) expressed in minimal time: 15*(165/60) = 41.25")))});
QJsonObject o4({
QPair(QString("value"), QJsonValue(1)),
QPair(QString("comment"), QJsonValue(QString("price-id of customer entered price (for hour) expressed in maximal time: 360*(165/60) = 990")))});
QJsonObject o5({
QPair(QString("comment"), QJsonValue(QString("general tariff config"))),
QPair(QString("config_id"), QJsonValue(1)),
QPair(QString("config_label"), QJsonValue("Tariff Config 1")),
QPair(QString("config_tariff_accuracy"), QJsonValue(1)),
QPair(QString("config_tariff_type"), QJsonValue(1)),
QPair(QString("config_carry_over"), QJsonValue(true)),
QPair(QString("config_prepaid"), QJsonValue(true)),
QPair(QString("MinimalTime"), QJsonValue(o1)),
QPair(QString("MaximalTime"), QJsonValue(o2)),
QPair(QString("MinimalPrice"), QJsonValue(o3)),
QPair(QString("MinimalPrice"), QJsonValue(o4))});
m_a.push_back(QJsonObject({QPair(key, QJsonValue(o5))}));
} else
if (key.compare("Products", Qt::CaseInsensitive) == 0) {
QJsonObject o1({
QPair(QString("product_id"), QJsonValue(1)),
QPair(QString("product_price_id"), QJsonValue(1)),
QPair(QString("product_label"), QJsonValue(QString("SHORT_TIME_PARKING")))});
QJsonObject o2({
QPair(QString("product_id"), QJsonValue(2)),
QPair(QString("product_price_id"), QJsonValue(2)),
QPair(QString("product_time_ranges"), QJsonArray({QJsonValue(100001)})),
QPair(QString("product_label"), QJsonValue(QString("DAY_TICKET")))});
QJsonArray a;
a.push_back(o1);
a.push_back(o2);
m_a.push_back(QJsonObject({QPair(key, QJsonValue(a))}));
} else
if (key.compare("TimeRanges", Qt::CaseInsensitive) == 0) {
QJsonObject o1({
QPair(QString("range_id"), QJsonValue(1)),
QPair(QString("range_price_id"), QJsonValue(0)),
QPair(QString("range_start"), QJsonValue(QString("00:00:00"))),
QPair(QString("range_end"), QJsonValue(QString("00:00:00"))),
QPair(QString("comment"), QJsonValue(QString("[ 00:00-00:00 [ = [ 00:00-24:00 [")))});
QJsonObject o2({
QPair(QString("range_id"), QJsonValue(1)),
QPair(QString("range_price_id"), QJsonValue(1)),
QPair(QString("range_start"), QJsonValue(QString("00:00:00"))),
QPair(QString("range_end"), QJsonValue(QString("08:00:00"))),
QPair(QString("comment"), QJsonValue(QString("[ 00:00-08:00 [")))});
QJsonObject o3({
QPair(QString("range_id"), QJsonValue(1)),
QPair(QString("range_price_id"), QJsonValue(2)),
QPair(QString("range_start"), QJsonValue(QString("18:00:00"))),
QPair(QString("range_end"), QJsonValue(QString("00:00:00"))),
QPair(QString("comment"), QJsonValue(QString("[ 18:00-00:00 [")))});
QJsonObject o4({
QPair(QString("range_id"), QJsonValue(1)),
QPair(QString("range_price_id"), QJsonValue(3)),
QPair(QString("range_start"), QJsonValue(QString("12:00:00"))),
QPair(QString("range_end"), QJsonValue(QString("00:00:00"))),
QPair(QString("comment"), QJsonValue(QString("[ 12:00-00:00 [")))});
QJsonArray a;
a.push_back(o1);
a.push_back(o2);
a.push_back(o3);
a.push_back(o4);
QTime const t(8,0,0);
QJsonArray b;
for (int i=0; i<600; ++i) {
QTime const &start = t.addSecs(i*60);
QTime const &end = t.addSecs((i+1)*60);
QJsonObject o({
QPair(QString("range_id"), QJsonValue(i)),
QPair(QString("range_price_id"), QJsonValue(0)),
QPair(QString("range_start"), QJsonValue(start.toString(Qt::ISODate))),
QPair(QString("range_end"), QJsonValue(end.toString(Qt::ISODate)))});
b.push_back(o);
}
a.push_back(b);
m_a.push_back(QJsonObject({QPair(key, QJsonValue(a))}));
} else
if (key.compare("Periods", Qt::CaseInsensitive) == 0) {
QJsonObject o1({
QPair(QString("period_id"), QJsonValue(1)),
QPair(QString("period_label"), QJsonValue("1st quarter")),
QPair(QString("period_from"), QJsonValue(QString("1970-01-01"))),
QPair(QString("period_until"), QJsonValue(QString("1970-03-31")))});
QJsonObject o2({
QPair(QString("period_id"), QJsonValue(2)),
QPair(QString("period_label"), QJsonValue("2nd quarter")),
QPair(QString("period_from"), QJsonValue(QString("1970-04-01"))),
QPair(QString("period_until"), QJsonValue(QString("1970-06-30")))});
QJsonObject o3({
QPair(QString("period_id"), QJsonValue(3)),
QPair(QString("period_label"), QJsonValue("3rd quarter")),
QPair(QString("period_from"), QJsonValue(QString("1970-07-01"))),
QPair(QString("period_until"), QJsonValue(QString("1970-09-30")))});
QJsonObject o4({
QPair(QString("period_id"), QJsonValue(4)),
QPair(QString("period_label"), QJsonValue("4th quarter")),
QPair(QString("period_from"), QJsonValue(QString("1970-10-01"))),
QPair(QString("period_until"), QJsonValue(QString("1970-12-31")))});
QJsonObject o5({
QPair(QString("period_id"), QJsonValue(5)),
QPair(QString("period_label"), QJsonValue("1st half-year")),
QPair(QString("period_from"), QJsonValue(QString("1970-01-01"))),
QPair(QString("period_until"), QJsonValue(QString("1970-06-30")))});
QJsonObject o6({
QPair(QString("period_id"), QJsonValue(6)),
QPair(QString("period_label"), QJsonValue("2nd half-year")),
QPair(QString("period_from"), QJsonValue(QString("1970-07-01"))),
QPair(QString("period_until"), QJsonValue(QString("1970-12-31")))});
QJsonArray a;
a.push_back(o1);
a.push_back(o2);
a.push_back(o3);
a.push_back(o4);
a.push_back(o5);
a.push_back(o6);
m_a.push_back(QJsonObject({QPair(key, QJsonValue(a))}));
} else
if (key.compare("DayConfigs", Qt::CaseInsensitive) == 0) {
QJsonArray b;
b.push_back(QJsonValue(100002));
for (int i=0; i<600; ++i) {
b.push_back(QJsonValue(i));
}
b.push_back(QJsonValue(100003));
QJsonArray c;
c.push_back(QJsonValue(100001));
QJsonObject o1({
QPair(QString("day_config_id"), QJsonValue(1)),
QPair(QString("day_config_range"), QJsonValue(6)),
QPair(QString("day_config_product_ids"), QJsonArray({QJsonValue(0), QJsonValue(1)})),
QPair(QString("day_config_short_time_parking"), b),
QPair(QString("day_config_day_ticket"), c)});
QJsonObject o2({QPair(QString("DayConfig_1"), QJsonValue(o1))});
QJsonObject o3({
QPair(QString("day_config_id"), QJsonValue(2)),
QPair(QString("day_config_range"), QJsonValue(6)),
QPair(QString("day_config_product_ids"), QJsonArray({QJsonValue(1)})),
QPair(QString("day_config_day_ticket"), c)});
QJsonObject o4({QPair(QString("DayConfig_2"), QJsonValue(o3))});
QJsonArray a;
a.push_back(o2);
a.push_back(o4);
m_a.push_back(QJsonObject({QPair(key, QJsonValue(a))}));
} else
if (key.compare("Days", Qt::CaseInsensitive) == 0) {
QJsonArray b;
for (int i=1; i < 8; ++i) {
QJsonObject o1({
QPair(QString("day_id"), QJsonValue(1)),
QPair(QString("day_config_id"), QJsonValue(0))});
switch(i) {
case Qt::Monday: {
QJsonObject o2({QPair(QString("Mon"), QJsonValue(o1))});
b.push_back(o2);
} break;
case Qt::Tuesday: {
QJsonObject o2({QPair(QString("Tue"), QJsonValue(o1))});
b.push_back(o2);
} break;
case Qt::Wednesday: {
QJsonObject o2({QPair(QString("Wed"), QJsonValue(o1))});
b.push_back(o2);
} break;
case Qt::Thursday: {
QJsonObject o2({QPair(QString("Thu"), QJsonValue(o1))});
b.push_back(o2);
} break;
case Qt::Friday: {
QJsonObject o2({QPair(QString("Fri"), QJsonValue(o1))});
b.push_back(o2);
} break;
case Qt::Saturday: {
QJsonObject o2({QPair(QString("Sat"), QJsonValue(o1))});
b.push_back(o2);
} break;
case Qt::Sunday: {
QJsonObject o2({QPair(QString("Sun"), QJsonValue(o1))});
b.push_back(o2);
} break;
}
}
QJsonArray c;
QJsonObject o1({
QPair(QString("day_id"), QJsonValue(1)),
QPair(QString("day_config_id"), QJsonValue(1)),
QPair(QString("day_date"), QJsonValue(QString("2024-12-25"))),
QPair(QString("day_movable"), QJsonValue(false))});
QJsonObject o2({
QPair(QString("day_id"), QJsonValue(2)),
QPair(QString("day_config_id"), QJsonValue(1)),
QPair(QString("day_date"), QJsonValue(QString("2024-12-26"))),
QPair(QString("day_movable"), QJsonValue(false))});
c.push_back(o1);
c.push_back(o2);
QJsonObject o3({QPair(QString("Weekdays"), b),
QPair(QString("Holidays"), c)});
//"Christmas_1st_day": {
// "day_id": 8,
// "day_config_id": 1,
// "day_date": "2024-12-25",
// "day_moveable": false
//},
//"Christmas_2nd_day": {
// "day_id": 9,
// "day_config_id": 1,
// "day_date": "2024-12-26",
// "day_moveable": false
//}
m_a.push_back(QJsonObject({QPair(key, QJsonValue(o3))}));
}
#if 0
"TariffDays": [
"Mon": {
"day_id": 1,
"day_config_id": 0
},
"Tue": {
"day_id": 2,
"day_config_id": 0
},
"Wed": {
"day_id": 3,
"day_config_id": 0
},
"Thu": {
"day_id": 4,
"day_config_id": 0
},
"Fri": {
"day_id": 5,
"day_config_id": 0
},
"Sat": {
"day_id": 6,
"day_config_id": 0
},
"Sun": {
"day_id": 7,
"day_config_id": 0
},
"Christmas_1st_day": {
"day_id": 8,
"day_config_id": 1,
"day_date": "2024-12-25",
"day_moveable": false
},
"Christmas_2nd_day": {
"day_id": 9,
"day_config_id": 1,
"day_date": "2024-12-26",
"day_moveable": false
}
// usw. die anderen Feiertage un
"DayConfig_1": {
"day_config_id": 0,
"day_config_date_range": 6,
"comment_1": "day config is valid for the whole year",
"day_config_product_ids": [0, 1],
"comment_2": "short-time-parking or day-ticket on this day",
#endif
}