Implement private_GetCostFromDuration() using a loop and not using

recursive calls.
This commit is contained in:
Gerhard Hoffmann 2023-11-28 15:25:37 +01:00
parent 9abc165a7c
commit 64c005cd70

View File

@ -408,15 +408,15 @@ double Calculator::GetCostFromDuration(Configuration* cfg,
QDateTime start = start_datetime;
Ticket t = private_GetCostFromDuration(cfg, start,
end_datetime, durationMinutes,
nextDay, prepaid);
durationMinutes,
prepaid);
if (t) {
qCritical().noquote() << t;
return t.getPrice();
}
return -1;
end_datetime = t.getValidUntil();
return t.getPrice();
}
int Calculator::getMinimalParkingTime(Configuration const *cfg, PaymentMethod methodId) {
@ -427,11 +427,9 @@ int Calculator::getMaximalParkingTime(Configuration const *cfg, PaymentMethod me
return std::max((int)cfg->PaymentOption.find(methodId)->second.pop_max_time, 0);
}
bool Calculator::checkDurationMinutes(bool overtime,
int minParkingTime,
bool Calculator::checkDurationMinutes(int minParkingTime,
int maxParkingTime,
int durationMinutes) {
if (!overtime) {
if (durationMinutes > maxParkingTime) {
qWarning() << QString("Total duration >= max_min (%1 >= %2)").arg(durationMinutes).arg(maxParkingTime);
return false;
@ -440,7 +438,7 @@ bool Calculator::checkDurationMinutes(bool overtime,
qWarning() << QString("Total duration <= minMin (%1 <= %2)").arg(durationMinutes).arg(minParkingTime);
return false;
}
}
return true;
}
@ -488,20 +486,18 @@ using namespace Utilities;
Ticket Calculator::private_GetCostFromDuration(Configuration const* cfg,
QDateTime const &start,
QDateTime &end,
int &durationMinutes,
bool nextDay,
bool prepaid,
bool overtime) {
int durationMinutes, // Netto
bool prepaid) {
static const PaymentMethod paymentMethodId = Utilities::getPaymentMethodId(cfg);
static const bool carryOverNotSet = isCarryOverNotSet(cfg, paymentMethodId);
static int const minParkingTimeMinutes = getMinimalParkingTime(cfg, paymentMethodId);
static int const maxParkingTimeMinutes = getMaximalParkingTime(cfg, paymentMethodId);
static bool const checkMinMaxMinutes = (minParkingTimeMinutes < maxParkingTimeMinutes);
static const int minParkingTimeMinutes = getMinimalParkingTime(cfg, paymentMethodId);
static const int maxParkingTimeMinutes = getMaximalParkingTime(cfg, paymentMethodId);
static const bool checkMinMaxMinutes = (minParkingTimeMinutes < maxParkingTimeMinutes);
static const int durationMinutesNetto = durationMinutes;
static const uint32_t weekDaysPrice = computeWeekDaysPrice(cfg, paymentMethodId);
static const double weekDaysDurationUnit = computeWeekDaysDurationUnit(cfg, paymentMethodId);
static const double specialDaysDurationUnit = 60.0;
if (!checkMinMaxMinutes) {
qCritical() << QString(
@ -510,47 +506,50 @@ Ticket Calculator::private_GetCostFromDuration(Configuration const* cfg,
return Ticket();
}
if (!checkDurationMinutes(overtime, minParkingTimeMinutes,
if (!checkDurationMinutes(minParkingTimeMinutes,
maxParkingTimeMinutes, durationMinutes)) {
return Ticket();
}
QDateTime inputDate = start;
int const weekdayId = inputDate.date().dayOfWeek();
uint32_t price = 0;
double durationUnit = 60.0;
uint32_t costFromDuration = 0;
double durationUnit = 0.0;
int specialDayId = -1;
bool isSpecialDay = false;
Ticket ticket;
QDateTime end = start;
QDateTime current;
int totalTimeRanges = 0;
int current_special_day_id = -1;
for (current = start; durationMinutes > 0; current = current.addDays(1)) {
int const weekdayId = current.date().dayOfWeek();
// there might be more than 1 worktime ranges per day
specialDayId = -1;
// find worktime ranges for the current day
int const timeRanges = std::max((int)cfg->WeekDaysWorktime.count(weekdayId), 1);
QScopedArrayPointer<TariffTimeRange> worktime(new TariffTimeRange[timeRanges]);
int ranges = 0;
if(Utilities::CheckSpecialDay(cfg, inputDate, &current_special_day_id, &price)) {
if((isSpecialDay = Utilities::CheckSpecialDay(cfg, current, &specialDayId, &price))) {
// Set special day price:
durationUnit = 60.0;
worktime[ranges].setTimeRange(SpecialDaysWorkTimeFrom(cfg, current_special_day_id),
SpecialDaysWorkTimeUntil(cfg, current_special_day_id));
durationUnit = specialDaysDurationUnit;
worktime[ranges].setTimeRange(SpecialDaysWorkTimeFrom(cfg, specialDayId),
SpecialDaysWorkTimeUntil(cfg, specialDayId));
ranges = 1;
} else {
// Set new price for the normal day: do not use a floating-point type
// for the price, rather compute with integers. Only at the very end of
// the computation the price is divided by durationUnit.
int pop_id = cfg->PaymentOption.find(paymentMethodId)->second.pop_id;
price = cfg->PaymentRate.find(pop_id)->second.pra_price;
price = weekDaysPrice;
durationUnit = weekDaysDurationUnit;
int durationId = cfg->PaymentRate.find(pop_id)->second.pra_payment_unit_id;
durationUnit = cfg->Duration.find(durationId)->second.pun_duration;
// If no working day found, skip it (recursively call method again)
// If no working day found, skip it (epsecially Sundays!)
if (cfg->WeekDaysWorktime.count(weekdayId) <= 0) {
// When no workday found, go to next available day
qDebug() << "No workday found, trying to find next available day";
inputDate = inputDate.addDays(1);
return private_GetCostFromDuration(cfg, inputDate, end, durationMinutes, true, prepaid, overtime);
end = current;
current.setTime(QTime()); // start at midnight on the next day
continue;
}
using WTIterator = std::multimap<int, ATBWeekDaysWorktime>::const_iterator;
@ -563,150 +562,105 @@ Ticket Calculator::private_GetCostFromDuration(Configuration const* cfg,
}
}
uint32_t costFromDuration = 0;
QTime const &lastWorktimeTo = worktime[ranges-1].getTimeUntil();
int currentRange = -1;
if (nextDay) { // this means the function has been called recursively
currentRange = 0;
inputDate.setTime(worktime[currentRange].getTimeFrom());
// find worktime range to start with
int currentRange = 0;
if (!isSpecialDay) {
if (start != current) { // on next day
current.setTime(worktime[currentRange].getTimeFrom());
} else {
// check if inputDate is located inside a valid worktime-range...
currentRange = findWorkTimeRange(inputDate, worktime, ranges);
currentRange = findWorkTimeRange(current, worktime, ranges);
if (currentRange == -1) { // no...
if (!prepaid) { // parking is not allowed
return Ticket(start, end, durationMinutesNetto, 0,
return Ticket(start, QDateTime(), durationMinutesNetto, 0,
0, Ticket::s[INVALID_FROM_DATETIME]);
}
// find the next worktime-range (on the same day), and start from there
currentRange = findNextWorkTimeRange(inputDate, worktime, ranges);
inputDate.setTime(worktime[currentRange].getTimeFrom());
currentRange = findNextWorkTimeRange(current, worktime, ranges);
current.setTime(worktime[currentRange].getTimeFrom());
}
}
}
for (int w = currentRange; w < ranges; ++w) {
for (int w = currentRange; w < ranges; ++w, ++totalTimeRanges) {
if (durationMinutes > 0) {
QTime const &worktime_from = worktime[w].getTimeFrom();
QTime const &worktime_to = worktime[w].getTimeUntil();
qCritical() << "from" << worktime_from;
qCritical() << "until" << worktime_to;
//if (w > 0) { // durationMinutes are always meant as netto time and
// // the time between worktime-ranges are free.
// inputDate.setTime(worktime_from);
//}
// TODO: hier muss dann der preis hin
if (price == 0) {
inputDate = inputDate.addDays(1);
inputDate.setTime(worktime_from);
Ticket t = private_GetCostFromDuration(
cfg, // TODO: erklaerung
inputDate,
end, durationMinutes,
true, prepaid);
if (!t.isValid()) {
return t;
if (totalTimeRanges) {
// durationMinutes are always meant as netto time and
// the time between worktime-ranges are free.
current.setTime(worktime_from);
}
costFromDuration += t.getPrice();
if (price == 0) {
// inputDate = inputDate.addDays(1);
// inputDate.setTime(worktime_from);
end = current;
current.setTime(QTime());
continue;
}
// If overtime flag is set
// TODO: ueberpruefen, kann man wohl nach oben ziehen
//if (overtime || nextDay) {
// inputDate.setTime(worktime_from);
// overtime = false;
//}
qCritical() << "inputDate.time()=" << inputDate.time();
if (current.time() == worktime_to) {
end = current;
current.setTime(QTime());
continue;
}
// Check prepaid
if (!prepaid) {
if ((inputDate.time() < worktime_from) || (inputDate.time() > worktime_to)) {
if ((current.time() < worktime_from) || (current.time() > worktime_to)) {
qDebug() << "[STOP] * Ticket is not valid * ";
return Ticket();
}
} else {
qDebug() << "* PREPAID MODE ACTIVE *";
if (inputDate.time() < worktime_from) {
inputDate.setTime(worktime_from);
//} else if(inputDate.time() > worktime_to) {
} else if(inputDate.time() > lastWorktimeTo) {
if (current.time() < worktime_from) {
current.setTime(worktime_from);
end = current;
} else if(current.time() > lastWorktimeTo) {
qDebug() << " *** PREPAID *** Current time is past the time range end, searching for next available day";
// wieso ist hier overtime nicht gesetzt
inputDate = inputDate.addDays(1);
Ticket t = private_GetCostFromDuration(
cfg, inputDate, end,
durationMinutes, true, prepaid);
if (!t.isValid()) {
return t;
}
costFromDuration += t.getPrice();
end = current;
current.setTime(QTime());
continue;
}
}
while(durationMinutes > 0) {
// Check for active year period
if (!IsYearPeriodActive(cfg, inputDate)) {
if (!IsYearPeriodActive(cfg, current)) {
return Ticket();
}
qDebug() << "inputDate" << inputDate.toString(Qt::ISODate);
if(inputDate.time() >= lastWorktimeTo) {
if(current.time() >= lastWorktimeTo) {
// Go to next day if minutes not spent
if (carryOverNotSet) {
// no carry_over, so stop computation
break;
}
qDebug() << "Reached end of worktime, searching for the next working day";
inputDate = inputDate.addDays(1);
inputDate.setTime(QTime());
overtime = true;
Ticket t = private_GetCostFromDuration(
cfg, inputDate, end,
durationMinutes, true, prepaid, overtime);
if (!t.isValid()) {
return t;
}
costFromDuration += t.getPrice();
current.setTime(QTime());
break; // stop while, and continue in outer loop
} else {
if(inputDate.time() < worktime_to) {
if(current.time() < worktime_to) {
// Increment input date minutes for each monetary unit
inputDate = inputDate.addSecs(60);
qDebug() << "inputDate" << inputDate.toString(Qt::ISODate);
current = current.addSecs(60);
end = current;
durationMinutes -= 1;
//costFromDuration += price_per_unit;
costFromDuration += price;
} else break;
}
}
}
}
} // while(durationMinutes > 0) {
} // if (durationMinutes > 0) {
} // for (int w = currentRange; w < ranges; ++w, ++totalTimeRanges) {
} // for (current = start; durationMinutes > 0; current = current.addDays(1)) {
if (inputDate >= end) {
end = inputDate;
}
if (nextDay == false) {
qDebug() << "GetCostFromDuration(): Valid until:" << end.toString(Qt::ISODate);
}
int durationMinutesBrutto = start.secsTo(end) / 60;
return
Ticket(start, end,
durationMinutesNetto,
start.secsTo(end) / 60,
nextDay ?
costFromDuration :
Ticket(start, end, durationMinutesNetto, durationMinutesBrutto,
llround(Utilities::CalculatePricePerUnit(costFromDuration, durationUnit)),
Ticket::s[VALID]);
}