Start reimplementation of

private_GetCostFromDuration() in terms of ticket.
This commit is contained in:
Gerhard Hoffmann 2023-11-27 16:21:28 +01:00
parent 215667af61
commit 7a5d797ae0

View File

@ -475,64 +475,64 @@ int Calculator::findNextWorkTimeRange(QDateTime const &dt,
using namespace Utilities; using namespace Utilities;
uint32_t Calculator::private_GetCostFromDuration(Configuration const* cfg, Ticket Calculator::private_GetCostFromDuration(Configuration const* cfg,
QDateTime const &start, QDateTime const &start,
QDateTime &end, QDateTime &end,
int durationMinutes, int &durationMinutes,
bool nextDay, bool nextDay,
bool prepaid, bool prepaid,
bool overtime) { bool overtime) {
// TODO
static const PaymentMethod paymentMethodId = PaymentMethod::Linear;
static const PaymentMethod paymentMethodId = Utilities::getPaymentMethodId(cfg);
static const bool carryOverNotSet = isCarryOverNotSet(cfg, paymentMethodId);
static int const minParkingTimeMinutes = getMinimalParkingTime(cfg, paymentMethodId); static int const minParkingTimeMinutes = getMinimalParkingTime(cfg, paymentMethodId);
static int const maxParkingTimeMinutes = getMaximalParkingTime(cfg, paymentMethodId); static int const maxParkingTimeMinutes = getMaximalParkingTime(cfg, paymentMethodId);
static bool const checkMinMaxMinutes = (minParkingTimeMinutes < maxParkingTimeMinutes); static bool const checkMinMaxMinutes = (minParkingTimeMinutes < maxParkingTimeMinutes);
static const int durationMinutesNetto = durationMinutes;
if (!checkMinMaxMinutes) { if (!checkMinMaxMinutes) {
qCritical() << QString( qCritical() << QString(
"ERROR: CONDITION minMin < maxMin (%1 < %2) IS NOT VALID") "ERROR: CONDITION minMin < maxMin (%1 < %2) IS NOT VALID")
.arg(minParkingTimeMinutes).arg(maxParkingTimeMinutes); .arg(minParkingTimeMinutes).arg(maxParkingTimeMinutes);
return 0; return Ticket();
} }
if (!checkDurationMinutes(overtime, minParkingTimeMinutes, if (!checkDurationMinutes(overtime, minParkingTimeMinutes,
maxParkingTimeMinutes, durationMinutes)) { maxParkingTimeMinutes, durationMinutes)) {
return 0; return Ticket();
} }
// Get input date
QDateTime inputDate = start; QDateTime inputDate = start;
// Get day of week
int const weekdayId = inputDate.date().dayOfWeek(); int const weekdayId = inputDate.date().dayOfWeek();
uint32_t price = 0;
uint32_t day_price = 0; double durationUnit = 60.0;
uint32_t price_per_unit = 0;
int current_special_day_id = -1; int current_special_day_id = -1;
// there might be more than 1 worktime ranges per 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 index = 0; int ranges = 0;
if(Utilities::CheckSpecialDay(cfg, inputDate, &current_special_day_id, &day_price)) { if(Utilities::CheckSpecialDay(cfg, inputDate, &current_special_day_id, &price)) {
// Set special day price: // Set special day price:
// ACHTUNG: price_per_unit ist eigentlich immer preis pro minute !!! durationUnit = 60.0;
price_per_unit = CalculatePricePerUnit(day_price); worktime[ranges].setTimeRange(SpecialDaysWorkTimeFrom(cfg, current_special_day_id),
worktime[index].setTimeRange(SpecialDaysWorkTimeFrom(cfg, current_special_day_id), SpecialDaysWorkTimeUntil(cfg, current_special_day_id));
SpecialDaysWorkTimeUntil(cfg, current_special_day_id)); ranges = 1;
} else { } else {
// Set new price for the normal day // 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; int pop_id = cfg->PaymentOption.find(paymentMethodId)->second.pop_id;
day_price = cfg->PaymentRate.find(pop_id)->second.pra_price; price = cfg->PaymentRate.find(pop_id)->second.pra_price;
int durationId = cfg->PaymentRate.find(pop_id)->second.pra_payment_unit_id; int durationId = cfg->PaymentRate.find(pop_id)->second.pra_payment_unit_id;
double durationUnit = cfg->Duration.find(durationId)->second.pun_duration; durationUnit = cfg->Duration.find(durationId)->second.pun_duration;
price_per_unit = Utilities::CalculatePricePerUnit(day_price,durationUnit);
// If no working day found, skip it (recursively call method again) // If no working day found, skip it (recursively call method again)
if (cfg->WeekDaysWorktime.count(weekdayId) <= 0) { if (cfg->WeekDaysWorktime.count(weekdayId) <= 0) {
@ -542,114 +542,162 @@ uint32_t Calculator::private_GetCostFromDuration(Configuration const* cfg,
return private_GetCostFromDuration(cfg, inputDate, end, durationMinutes, true, prepaid, overtime); return private_GetCostFromDuration(cfg, inputDate, end, durationMinutes, true, prepaid, overtime);
} }
for (auto[itr, rangeEnd] = cfg->WeekDaysWorktime.equal_range(weekdayId); itr != rangeEnd; ++itr) { using WTIterator = std::multimap<int, ATBWeekDaysWorktime>::const_iterator;
qCritical() << itr->first << itr->second.pwd_time_from.c_str() << itr->second.pwd_time_to.c_str(); std::pair<WTIterator, WTIterator> p = cfg->WeekDaysWorktime.equal_range(weekdayId);
worktime[index].setTimeRange(QTime::fromString(itr->second.pwd_time_from.c_str()),
QTime::fromString(itr->second.pwd_time_to.c_str())); for (WTIterator itr = p.first; itr != p.second; ++itr) {
index += 1; worktime[ranges].setTimeRange(WeekDaysWorkTimeFrom(itr),
WeekDaysWorkTimeUntil(itr));
ranges += 1;
} }
} }
if (price_per_unit < 0) price_per_unit = 1.0f; uint32_t costFromDuration = 0;
qDebug() << "Calculated price per minute=" << price_per_unit; QTime const &lastWorktimeTo = worktime[ranges-1].getTimeUntil();
int currentRange = -1;
double costFromDuration = 0.0; if (nextDay) { // this means the function has been called recursively
for (int w = 0; w < index; ++w) { currentRange = 0;
QTime worktime_from = worktime[w].getTimeFrom(); inputDate.setTime(worktime[currentRange].getTimeFrom());
QTime worktime_to = worktime[w].getTimeUntil(); } else {
// check if inputDate is located inside a valid worktime-range...
if (price_per_unit == 0) { currentRange = findWorkTimeRange(inputDate, worktime, ranges);
inputDate = inputDate.addDays(1); if (currentRange == -1) { // no...
inputDate.setTime(worktime_from); if (!prepaid) { // parking is not allowed
uint32_t const partialCost = private_GetCostFromDuration(cfg, inputDate, end, durationMinutes, true, prepaid); return Ticket(start, end, durationMinutesNetto, 0,
if (partialCost == 0) { 0, Ticket::s[INVALID_FROM_DATETIME]);
return 0;
} }
costFromDuration += partialCost; // find the next worktime-range (on the same day), and start from there
continue; currentRange = findNextWorkTimeRange(inputDate, worktime, ranges);
inputDate.setTime(worktime[currentRange].getTimeFrom());
} }
}
// If overtime flag is set for (int w = currentRange; w < ranges; ++w) {
if (overtime || nextDay) { if (durationMinutes > 0) {
inputDate.setTime(worktime_from); QTime const &worktime_from = worktime[w].getTimeFrom();
overtime = false; QTime const &worktime_to = worktime[w].getTimeUntil();
}
// Check prepaid qCritical() << "from" << worktime_from;
if (!prepaid) { qCritical() << "until" << worktime_to;
if ((inputDate.time() < worktime_from) || (inputDate.time() > worktime_to)) {
qDebug() << "[STOP] * Ticket is not valid * "; //if (w > 0) { // durationMinutes are always meant as netto time and
return 0.0f; // // the time between worktime-ranges are free.
} // inputDate.setTime(worktime_from);
} else { //}
qDebug() << "* PREPAID MODE ACTIVE *";
if (inputDate.time() < worktime_from) { // TODO: hier muss dann der preis hin
inputDate.setTime(worktime_from); if (price == 0) {
} else if(inputDate.time() > worktime_to) {
qDebug() << " *** PREPAID *** Current time is past the time range end, searching for next available day";
inputDate = inputDate.addDays(1); inputDate = inputDate.addDays(1);
uint32_t const partialCost = private_GetCostFromDuration(cfg, inputDate, end, durationMinutes, true, prepaid); inputDate.setTime(worktime_from);
if (partialCost == 0) { Ticket t = private_GetCostFromDuration(
return 0; cfg, // TODO: erklaerung
inputDate,
end, durationMinutes,
true, prepaid);
if (!t.isValid()) {
return t;
} }
costFromDuration += partialCost; costFromDuration += t.getPrice();
continue; continue;
} }
}
while(durationMinutes > 0) { // If overtime flag is set
// Check for active year period // TODO: ueberpruefen, kann man wohl nach oben ziehen
if (std::none_of(cfg->YearPeriod.begin(), //if (overtime || nextDay) {
cfg->YearPeriod.end(), // inputDate.setTime(worktime_from);
[&inputDate](std::pair<int, ATBPeriodYear> const &year) { // overtime = false;
QDate const input(2004, // 2004 is a leap year //}
inputDate.date().month(),
inputDate.date().day()); qCritical() << "inputDate.time()=" << inputDate.time();
QDate const s(2004, year.second.pye_start_day, year.second.pye_start_month);
QDate const e(2004, year.second.pye_end_day, year.second.pye_end_month); // Check prepaid
return (input >= s && input <= e); if (!prepaid) {
})) { if ((inputDate.time() < worktime_from) || (inputDate.time() > worktime_to)) {
qCritical() << "NO VALID YEAR PERIOD"; qDebug() << "[STOP] * Ticket is not valid * ";
return 0.0; 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) {
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();
continue;
}
} }
// Go to next day if minutes not spent while(durationMinutes > 0) {
if(inputDate.time() >= worktime_to) { // Check for active year period
// check for carry_over status if (!IsYearPeriodActive(cfg, inputDate)) {
if (cfg->PaymentOption.find(paymentMethodId)->second.pop_carry_over < 1) { return Ticket();
break;
} }
qDebug() << "Reached end of worktime, searching for the next working day"; qDebug() << "inputDate" << inputDate.toString(Qt::ISODate);
inputDate = inputDate.addDays(1); if(inputDate.time() >= lastWorktimeTo) {
overtime = true; // Go to next day if minutes not spent
uint32_t const partialCost = private_GetCostFromDuration(cfg, inputDate, end, durationMinutes, true, prepaid, overtime); if (carryOverNotSet) {
if (partialCost == 0) { // no carry_over, so stop computation
return 0; 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();
break; // stop while, and continue in outer loop
} else {
if(inputDate.time() < worktime_to) {
// Increment input date minutes for each monetary unit
inputDate = inputDate.addSecs(60);
qDebug() << "inputDate" << inputDate.toString(Qt::ISODate);
durationMinutes -= 1;
//costFromDuration += price_per_unit;
costFromDuration += price;
} else break;
} }
costFromDuration += partialCost;
break; // stop while, and continue in outer loop
} else {
// Increment input date minutes for each monetary unit
inputDate = inputDate.addSecs(60);
durationMinutes -= 1;
costFromDuration += price_per_unit;
} }
} }
} }
qDebug() << "GetCostFromDuration(): Valid until:" << inputDate.toString(Qt::ISODate);
end = inputDate; if (inputDate >= end) {
end = inputDate;
}
//double ret_val = total_cost; if (nextDay == false) {
//total_cost = 0.0f; qDebug() << "GetCostFromDuration(): Valid until:" << end.toString(Qt::ISODate);
//return ceil(ret_val); }
// TODO: runden nur falls oberster stack-rahmen return
Ticket(start, end,
return ceil(costFromDuration); durationMinutesNetto,
start.secsTo(end) / 60,
nextDay ?
costFromDuration :
llround(Utilities::CalculatePricePerUnit(costFromDuration, durationUnit)),
Ticket::s[VALID]);
} }