GetTimeSteps():

unifiy implementation for Fuchs/Muehlbach and Fuchs/Schnals.
This commit is contained in:
Gerhard Hoffmann 2024-08-18 19:29:48 +02:00
parent 171b23eebb
commit d86a688836

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,27 +2790,24 @@ 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);
}
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) {
QDateTime current(start);
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
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);
@ -2820,117 +2820,199 @@ QList<int> &Calculator::GetTimeSteps(Configuration *cfg, int paymentOptionIndex,
prepayEnd = prepayEnd.addDays(1);
}
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 (DBG_LEVEL >= DBG_DEBUG) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ") prepay start" << prepayStart.toString(Qt::ISODate);
qCritical() << "(" << __func__ << ":" << __LINE__ << ") prepay end" << prepayEnd.toString(Qt::ISODate);
}
if (!casePrepay) {
// handle possible carryover-condition
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);
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);
}
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 (++cnt > 4) continue;
if (DBG_LEVEL >= DBG_DEBUG) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ") pun_id" << it->second.pun_id;
}
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 (prepayStart <= start && start <= prepayEnd) {
it->second.pun_duration = prepayDurationInMinutes;
it->second.pun_duration += it->second.pun_duration_saved ;
casePrepay = true;
}
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;
//qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << durationInSecs;
//qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << start.addSecs(durationInSecs).toString(Qt::ISODate);
//qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << carryOverStart.toString(Qt::ISODate);
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 (start >= carryOverStart) {
it->second.pun_duration += start.secsTo(carryOverEnd) / 60;
if (current >= carryOverStart) {
it->second.pun_duration += current.secsTo(carryOverEnd) / 60;
} else {
// start < carryOverStart
if (start.addSecs(durationInSecs) >= 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;
prev_it = it;
}
}
} 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);
} else {
m_timeSteps[paymentOptionIndex] << it->second.pun_duration;
}
}
} else {
qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
<< "no change required, but set to brutto, which does not have to be changed";
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 (it->second.pun_fixed) { // no change required: must be true
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) {
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;
}
m_timeSteps[paymentOptionIndex] << it->second.pun_duration;
} else {
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";
}
}
}
#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);
// qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << m_timeSteps[paymentOptionIndex];
} else {
qCritical() << "(" << __func__ << ":" << __LINE__ << ") TODO";
}
}
}
#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);