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