Compare commits

...

4 Commits

Author SHA1 Message Date
d86a688836 GetTimeSteps():
unifiy implementation for Fuchs/Muehlbach and Fuchs/Schnals.
2024-08-18 19:29:48 +02:00
171b23eebb added parsing for new duration fields 2024-08-18 19:27:06 +02:00
ae9a54bf02 Added:
pun_mutable:
        duration might be changed during runtime
    pun_round_to_next_24h_boundary
        duration has to be aligned with next possible 24h boundary
    pun_round_to_prev_24h_boundary
        duration has to be aligned with previous 24h boundary
    pun_align_with_timepoint
        duration has to be aligned with dedicated time point

Removed:
    pun_requires_change
        Replaced by pu_mutable
2024-08-18 19:23:12 +02:00
84e61844ab test case schnals 2024-08-18 19:22:07 +02:00
4 changed files with 255 additions and 143 deletions

View File

@ -1,6 +1,7 @@
#include <string>
#include <QDebug>
#include <QDateTime>
class ATBDuration
{
@ -16,7 +17,10 @@ public:
, pun_netto(false)
, pun_brutto(false)
, pun_fixed(false)
, pun_requires_change(false)
, pun_mutable(false)
, pun_round_to_next_24h_boundary(false)
, pun_round_to_prev_24h_boundary(false)
, pun_align_with_timepoint(QDateTime())
, pun_next_step_correction(0) {
}
@ -24,17 +28,20 @@ public:
QDebugStateSaver saver(debug);
debug.nospace()
<< " pun_id: " << td.pun_id << "\n"
<< " pun_label: " << QString::fromStdString(td.pun_label) << "\n"
<< " pun_duration: " << td.pun_duration << "\n"
<< " pun_duration_saved: " << td.pun_duration_saved << "\n"
<< " pun_duration_min: " << td.pun_duration_min << "\n"
<< " pun_duration_max: " << td.pun_duration_max << "\n"
<< " pun_netto: " << td.pun_netto << "\n"
<< " pun_brutto: " << td.pun_brutto << "\n"
<< " pun_fixed: " << td.pun_fixed << "\n"
<< " pun_requires_change: " << td.pun_requires_change << "\n"
<< "pun_interpolation_id: " << td.pun_interpolation_id << "\n";
<< " pun_id: " << td.pun_id << "\n"
<< " pun_label: " << QString::fromStdString(td.pun_label) << "\n"
<< " pun_duration: " << td.pun_duration << "\n"
<< " pun_duration_saved: " << td.pun_duration_saved << "\n"
<< " pun_duration_min: " << td.pun_duration_min << "\n"
<< " pun_duration_max: " << td.pun_duration_max << "\n"
<< " pun_interpolation_id: " << td.pun_interpolation_id << "\n"
<< " pun_netto: " << td.pun_netto << "\n"
<< " pun_brutto: " << td.pun_brutto << "\n"
<< " pun_fixed: " << td.pun_fixed << "\n"
<< " pun_mutable: " << td.pun_mutable << "\n"
<< "pun_round_to_next_24h_boundary: " << td.pun_round_to_next_24h_boundary << "\n"
<< "pun_round_to_prev_24h_boundary: " << td.pun_round_to_prev_24h_boundary << "\n"
<< " pun_align_with_timepoint: " << td.pun_align_with_timepoint << endl;
return debug;
}
@ -49,6 +56,9 @@ public:
bool pun_netto; // the timestep expressed by this duration is a netto timestep
bool pun_brutto; // the timestep expressed by this duration is a brutto timestep
bool pun_fixed; // the value given in tariff-file is fixed (constant)
bool pun_requires_change; // the value has to be changes (controlled by other parameters)
bool pun_mutable; // the value could change
bool pun_round_to_next_24h_boundary;
bool pun_round_to_prev_24h_boundary;
QDateTime pun_align_with_timepoint;
int pun_next_step_correction;
};

View File

@ -10,9 +10,12 @@
#include <sstream>
#include <algorithm>
#include <optional>
#include <iterator>
#include <QDateTime>
#include <QScopedArrayPointer>
#include <QDebug>
#include <QTimeZone>
double total_duration_min = 0.0f;
double total_cost = 0.0f;
@ -2787,151 +2790,230 @@ QList<int> &Calculator::GetTimeSteps(Configuration *cfg, int paymentOptionIndex,
int const pop_accumulate_durations = cfg->getPaymentOptions(paymentOptionIndex).pop_accumulate_durations;
m_timeSteps[paymentOptionIndex].clear();
Configuration::TariffDurationType::iterator prev_it = cfg->Duration.end();
start = s;
start.setTime(QTime(s.time().hour(), s.time().minute(), 0));
QDateTime const start(s.date(),
QTime(s.time().hour(), s.time().minute(), 0),
s.timeZone());
// qCritical() << "(" << __func__ << ":" << __LINE__ << ") start:" << start.toString(Qt::ISODate);
if (DBG_LEVEL >= DBG_DEBUG) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ") start:" << start.toString(Qt::ISODate);
}
QDateTime current(start);
bool casePrepay = false;
int const weekDay = start.date().dayOfWeek();
int const pop_prepay_option_id = cfg->getPaymentOptions(paymentOptionIndex).pop_prepay_option_id;
// compute prepay settings
// assumption: prepay settings the same for all days
QDateTime prepayStart(start);
QDateTime prepayEnd(start);
prepayStart.setTime(cfg->TariffPrepayOptions.find(pop_prepay_option_id)->second.prepay[weekDay].start);
prepayEnd.setTime(cfg->TariffPrepayOptions.find(pop_prepay_option_id)->second.prepay[weekDay].end);
if (start <= prepayEnd) {
prepayStart = prepayStart.addDays(-1);
} else {
prepayEnd = prepayEnd.addDays(1);
}
if (DBG_LEVEL >= DBG_DEBUG) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ") prepay start" << prepayStart.toString(Qt::ISODate);
qCritical() << "(" << __func__ << ":" << __LINE__ << ") prepay end" << prepayEnd.toString(Qt::ISODate);
}
int const prepayDurationInMinutes = start.secsTo(prepayEnd) / 60;
// compute carryover settings
// assumption: carryover settings the same for all days
int const pop_carry_over_option_id = cfg->getPaymentOptions(paymentOptionIndex).pop_carry_over_option_id;
QDateTime carryOverStart(start);
QDateTime carryOverEnd(start);
carryOverStart.setTime(cfg->TariffCarryOverOptions.find(pop_carry_over_option_id)->second.carryover[weekDay].static_start);
carryOverEnd.setTime(cfg->TariffCarryOverOptions.find(pop_carry_over_option_id)->second.carryover[weekDay].static_end);
if (start <= carryOverEnd) {
carryOverStart = carryOverStart.addDays(-1);
} else {
carryOverEnd = carryOverEnd.addDays(1);
}
if (DBG_LEVEL >= DBG_DEBUG) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ") carry over start" << carryOverStart.toString(Qt::ISODate);
qCritical() << "(" << __func__ << ":" << __LINE__ << ") carry over end" << carryOverEnd.toString(Qt::ISODate);
}
// int cnt = 0;
for (Configuration::TariffDurationType::iterator it = cfg->Duration.begin();
it != cfg->Duration.end();
++it) {
it->second.pun_duration = it->second.pun_duration_saved;
if (it->second.pun_requires_change) {
if (it->second.pun_netto && !it->second.pun_brutto) {
int weekDay = start.date().dayOfWeek();
bool casePrepay = false;
if (m_timeSteps[paymentOptionIndex].isEmpty()) {
// handle possible prepay-condition, which applies only
// for the very first step
// if (++cnt > 4) continue;
int const pop_prepay_option_id = cfg->getPaymentOptions(paymentOptionIndex).pop_prepay_option_id;
QDateTime prepayStart(start);
QDateTime prepayEnd(start);
if (DBG_LEVEL >= DBG_DEBUG) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ") pun_id" << it->second.pun_id;
}
prepayStart.setTime(cfg->TariffPrepayOptions.find(pop_prepay_option_id)->second.prepay[weekDay].start);
prepayEnd.setTime(cfg->TariffPrepayOptions.find(pop_prepay_option_id)->second.prepay[weekDay].end);
if (it->second.pun_mutable) {
if (it->second.pun_netto) {
if (it->second.pun_round_to_next_24h_boundary == false) {
//int weekDay = start.date().dayOfWeek();
casePrepay = false;
if (m_timeSteps[paymentOptionIndex].isEmpty()) {
// handle possible prepay-condition, which applies only
// for the very first step
if (start <= prepayEnd) {
prepayStart = prepayStart.addDays(-1);
} else {
prepayEnd = prepayEnd.addDays(1);
}
if (prepayStart <= start && start <= prepayEnd) {
it->second.pun_duration = prepayDurationInMinutes;
it->second.pun_duration += it->second.pun_duration_saved ;
casePrepay = true;
}
qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << start.toString(Qt::ISODate);
qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << prepayStart.toString(Qt::ISODate);
qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << prepayEnd.toString(Qt::ISODate);
if (prepayStart <= start && start <= prepayEnd) {
it->second.pun_duration = start.secsTo(prepayEnd) / 60;
it->second.pun_duration += it->second.pun_duration_saved ;
casePrepay = true;
}
//if (start >= prepayStart && ) {
// it->second.pun_duration = start.secsTo(prepayEnd) / 60;
// it->second.pun_duration += it->second.pun_duration_saved ;
// casePrepay = true;
//}
qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << it->second.pun_duration;
}
if (!casePrepay) {
// handle possible carryover-condition
int const pop_carry_over_option_id = cfg->getPaymentOptions(paymentOptionIndex).pop_carry_over_option_id;
QDateTime carryOverStart(start);
QDateTime carryOverEnd(start);
qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << carryOverStart.toString(Qt::ISODate);
carryOverStart.setTime(cfg->TariffCarryOverOptions.find(pop_carry_over_option_id)->second.carryover[weekDay].static_start);
carryOverEnd.setTime(cfg->TariffCarryOverOptions.find(pop_carry_over_option_id)->second.carryover[weekDay].static_end);
//while (carryOverStart > carryOverEnd) {
// carryOverEnd = carryOverEnd.addDays(1);
//}
if (start <= carryOverEnd) {
carryOverStart = carryOverStart.addDays(-1);
} else {
carryOverEnd = carryOverEnd.addDays(1);
}
int durationInSecs = it->second.pun_duration_saved * 60;
//qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << durationInSecs;
//qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << start.addSecs(durationInSecs).toString(Qt::ISODate);
//qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << carryOverStart.toString(Qt::ISODate);
it->second.pun_duration = it->second.pun_duration_saved;
if (start >= carryOverStart) {
it->second.pun_duration += start.secsTo(carryOverEnd) / 60;
} else {
// start < carryOverStart
if (start.addSecs(durationInSecs) >= carryOverStart) {
// cross carry over section
it->second.pun_next_step_correction = carryOverStart.secsTo(carryOverEnd) / 60;
it->second.pun_duration += it->second.pun_next_step_correction;
prev_it = it;
if (DBG_LEVEL >= DBG_DEBUG) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << it->second.pun_duration;
qCritical() << "(" << __func__ << ":" << __LINE__ << ") prepay" << casePrepay;
}
}
if (!casePrepay) {
// handle possible carryover-condition
int durationInSecs = it->second.pun_duration_saved * 60;
if (DBG_LEVEL >= DBG_DEBUG) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << durationInSecs;
qCritical() << "(" << __func__ << ":" << __LINE__ << ") start" << start.toString(Qt::ISODate);
qCritical() << "(" << __func__ << ":" << __LINE__ << ") current" << current.toString(Qt::ISODate);
qCritical() << "(" << __func__ << ":" << __LINE__ << ") current next" << current.addSecs(durationInSecs).toString(Qt::ISODate);
}
it->second.pun_duration = it->second.pun_duration_saved;
if (current >= carryOverStart) {
it->second.pun_duration += current.secsTo(carryOverEnd) / 60;
} else {
// start < carryOverStart
QDateTime const &newCurrent = current.addSecs(durationInSecs);
if (newCurrent > carryOverStart) {
// cross carry over section
it->second.pun_next_step_correction = carryOverStart.secsTo(carryOverEnd) / 60;
it->second.pun_duration += it->second.pun_next_step_correction;
}
}
} else {
// qCritical() << "(" << __func__ << ":" << __LINE__ << ")";
}
if (pop_accumulate_durations) {
m_timeSteps[paymentOptionIndex] << it->second.pun_duration;
} else {
if (!m_timeSteps[paymentOptionIndex].isEmpty()) {
int d = m_timeSteps[paymentOptionIndex].last();
m_timeSteps[paymentOptionIndex] << (d + it->second.pun_duration);
}
}
current = current.addSecs(m_timeSteps[paymentOptionIndex].last() * 60);
if (DBG_LEVEL >= DBG_DEBUG) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << m_timeSteps[paymentOptionIndex];
qCritical() << "(" << __func__ << ":" << __LINE__ << ") current" << current.toString(Qt::ISODate);
}
} else { // if (it->pun_round_to_next_24h_boundary == false) {
if (DBG_LEVEL >= DBG_DEBUG) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ") round-to-next-24h-boundary";
qCritical() << "(" << __func__ << ":" << __LINE__ << ") prepay" << casePrepay;
qCritical() << "(" << __func__ << ":" << __LINE__ << ") current" << current.toString(Qt::ISODate);
}
if (casePrepay) {
// the last time step contains the (dynamic) prepay duration
// so it is possible to compute the last duration
int lastDuration = m_timeSteps[paymentOptionIndex].last() - prepayDurationInMinutes;
// qCritical() << "(" << __func__ << ":" << __LINE__ << ") last duration " << lastDuration;
int const minutesToAdd = 1440 - lastDuration;
current = current.addSecs(minutesToAdd * 60);
if (DBG_LEVEL >= DBG_DEBUG) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ") minutes to add " << minutesToAdd;
qCritical() << "(" << __func__ << ":" << __LINE__ << ") current" << current.toString(Qt::ISODate);
}
if (pop_accumulate_durations) {
m_timeSteps[paymentOptionIndex] << minutesToAdd;
} else {
if (!m_timeSteps[paymentOptionIndex].isEmpty()) {
int d = m_timeSteps[paymentOptionIndex].last();
m_timeSteps[paymentOptionIndex] << (d + minutesToAdd);
}
}
// qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << m_timeSteps[paymentOptionIndex];
casePrepay = false;
} else {
if (DBG_LEVEL >= DBG_DEBUG) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ") pun_id" << it->second.pun_id;
qCritical() << "(" << __func__ << ":" << __LINE__ << ") current" << current.toString(Qt::ISODate);
qCritical() << "(" << __func__ << ":" << __LINE__ << ") pun_duration" << it->second.pun_duration;
}
int secs = s.secsTo(current);
int const dayInSecs = 1440 * 60;
int const rest = secs % dayInSecs;
int const minutesToAdd = (dayInSecs - rest) / 60;
current = current.addSecs(minutesToAdd * 60);
it->second.pun_duration = minutesToAdd;
if (DBG_LEVEL >= DBG_DEBUG) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ") minutes to add " << minutesToAdd;
qCritical() << "(" << __func__ << ":" << __LINE__ << ") current" << current.toString(Qt::ISODate);
}
if (pop_accumulate_durations) {
m_timeSteps[paymentOptionIndex] << minutesToAdd;
} else {
if (!m_timeSteps[paymentOptionIndex].isEmpty()) {
int d = m_timeSteps[paymentOptionIndex].last();
m_timeSteps[paymentOptionIndex] << (d + minutesToAdd);
}
}
// qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << m_timeSteps[paymentOptionIndex];
}
}
} else { // if (it->second.pun_netto) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ") TODO";
}
} else { // mutable == false
if (it->second.pun_brutto) { // no handling for prepay and carry-over
if (pop_accumulate_durations) {
m_timeSteps[paymentOptionIndex] << it->second.pun_duration;
} else {
if (!m_timeSteps[paymentOptionIndex].isEmpty()) {
int d = m_timeSteps[paymentOptionIndex].last();
m_timeSteps[paymentOptionIndex] << (d + it->second.pun_duration);
} else {
m_timeSteps[paymentOptionIndex] << it->second.pun_duration;
}
}
// qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << m_timeSteps[paymentOptionIndex];
} else {
qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
<< "no change required, but set to brutto, which does not have to be changed";
}
} else {
if (it->second.pun_fixed) { // no change required: must be true
if (it->second.pun_brutto) { // no handling for prepay and carry-over
if (pop_accumulate_durations) {
int correction = 0;
if (prev_it != cfg->Duration.end()) {
correction = prev_it->second.pun_next_step_correction;
prev_it->second.pun_next_step_correction = 0;
}
if (!m_timeSteps[paymentOptionIndex].isEmpty()) {
int d = m_timeSteps[paymentOptionIndex].last();
it->second.pun_duration -= correction;
m_timeSteps[paymentOptionIndex] << (d + it->second.pun_duration);
} else {
it->second.pun_duration -= correction;
m_timeSteps[paymentOptionIndex] << it->second.pun_duration;
}
}
} else
if (it->second.pun_netto) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "TODO";
}
} else {
qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "fixed is false, but no change required";
qCritical() << "(" << __func__ << ":" << __LINE__ << ") TODO";
}
}
}
#if 0
for (int i = 0; i < m_timeSteps[paymentOptionIndex].size(); ++i) {
QDateTime nextTime = start;
// nextTime.setTime(QTime(0, 0, 0));
int const secs = m_timeSteps[paymentOptionIndex][i] * 60;
nextTime = nextTime.addSecs(secs);
qCritical() << "(" << __func__ << ":" << __LINE__ << ") step"
<< i << secs << m_timeSteps[0][i] << "->" << nextTime.toString(Qt::ISODate);
}
#endif
} else {
qCritical() << "(" << __func__ << ":" << __LINE__ << ") payment option time step config:" << "TimeStepConfig::STATIC";
@ -2953,10 +3035,9 @@ QList<int> &Calculator::GetTimeSteps(Configuration *cfg, int paymentOptionIndex,
//qCritical() << "(" << __func__ << ":" << __LINE__ << ") NEW timeSteps:" << m_timeSteps;
if (DBG_LEVEL >= DBG_DEBUG) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ") NEW timeSteps:" << m_timeSteps;
QDateTime nextTime(s);
qCritical() << "(" << __func__ << ":" << __LINE__ << ") start" << nextTime.toString(Qt::ISODate);
for (int i = 0; i < m_timeSteps[paymentOptionIndex].size(); ++i) {
QDateTime nextTime = s;
// nextTime.setTime(QTime(0, 0, 0));
int const secs = m_timeSteps[paymentOptionIndex][i] * 60;
nextTime = nextTime.addSecs(secs);

View File

@ -144,7 +144,7 @@ bool Configuration::ParseJson(Configuration* cfg, const char* json)
continue;
}
qCritical() << " -" << mb_name;
// qCritical() << " -" << mb_name;
// Get array for each JSON object member
auto mb_array = document[mb_name].GetArray();
@ -797,9 +797,25 @@ bool Configuration::ParseJson(Configuration* cfg, const char* json)
Duration.pun_fixed = k->value.GetBool();
}
}
else if (strcmp(inner_obj_name, "pun_requires_change") == 0) {
else if (strcmp(inner_obj_name, "pun_mutable") == 0) {
if (k->value.IsBool()) {
Duration.pun_requires_change = k->value.GetBool();
Duration.pun_mutable = k->value.GetBool();
}
}
else if (strcmp(inner_obj_name, "pun_round_to_next_24h_boundary") == 0) {
if (k->value.IsBool()) {
Duration.pun_round_to_next_24h_boundary = k->value.GetBool();
}
}
else if (strcmp(inner_obj_name, "pun_round_to_prev_24h_boundary") == 0) {
if (k->value.IsBool()) {
Duration.pun_round_to_prev_24h_boundary = k->value.GetBool();
}
}
else if (strcmp(inner_obj_name, "pun_align_with_timepoint") == 0) {
if (k->value.IsString()) {
QDateTime const &dt = QDateTime::fromString(k->value.GetString(), Qt::ISODate);
Duration.pun_align_with_timepoint = dt;
}
}
break;
@ -866,7 +882,7 @@ bool Configuration::ParseJson(Configuration* cfg, const char* json)
} break;
case MemberType::DurationType:
cfg->Duration.insert(pair<int, ATBDuration>(Duration.pun_id, Duration));
// qCritical() << Duration;
//qCritical() << Duration;
break;
case MemberType::SpecialDaysWorktimeType:
cfg->SpecialDaysWorktime.insert(pair<int, ATBSpecialDaysWorktime>(SpecialDaysWorktime.pedwt_period_exc_day_id, SpecialDaysWorktime));
@ -922,7 +938,7 @@ bool Configuration::ParseJson(Configuration* cfg, const char* json)
break;
case MemberType::PrepaidOptionType:
cfg->TariffPrepayOptions.insert(pair<int, ATBPrepay>(TariffPrepayOption.id, TariffPrepayOption));
qCritical() << TariffPrepayOption;
//qCritical() << TariffPrepayOption;
break;
default:
break;

View File

@ -741,7 +741,8 @@ int main() {
input.open("/opt/ptu5/opt/customer_502/etc/psa_tariff/tariff02.json");
}
if (zone == 3) {
//input.open("/opt/ptu5/opt/customer_502/etc/psa_tariff/tariff03.json");
input.open("/opt/ptu5/opt/customer_502/etc/psa_tariff/tariff03.json");
//input.open("/home/linux/customer_502/etc/psa_tariff/tariff01.json");
input.open("/home/linux/customer_502/etc/psa_tariff/tariff03.json");
}
@ -916,7 +917,9 @@ int main() {
QDateTime end;
QDateTime s(QDateTime::currentDateTime());
s.setTime(QTime(18, 0, 0));
s.setTime(QTime(13, 30, 0));
#if 0
pop_min_time = get_minimal_parkingtime(&cfg, PermitType(PERMIT_TYPE::SHORT_TERM_PARKING), paymentOptionIndex);
pop_max_time = get_maximal_parkingtime(&cfg, PermitType(PERMIT_TYPE::SHORT_TERM_PARKING), paymentOptionIndex);
@ -938,11 +941,13 @@ int main() {
qCritical() << " pop_min_price " << pop_min_price;
qCritical() << " pop_max_price " << pop_max_price;
qCritical() << "pop_daily_card_price " << pop_daily_card_price;
#endif
CalcState calcState;
#if 0
for (int i = 1; i < 11; ++i) {
#if 1
qCritical() << "start" << s.toString(Qt::ISODate);
//for (int i = 1; i < 11; ++i) {
for (int i = 1; i < 8; ++i) {
calcState = compute_duration_for_parking_ticket(&cfg, s,
(double)i*200,
end, PermitType(PERMIT_TYPE::SHORT_TERM_PARKING_PKW));