Started re-implementation of GetCostFromDuration() using

private_GetCostFromDuration().
This commit is contained in:
Gerhard Hoffmann 2023-11-26 19:55:21 +01:00
parent c749de2bf9
commit 7ac033720e

View File

@ -395,8 +395,7 @@ double Calculator::GetCostFromDuration(Configuration* cfg,
QDateTime &end_datetime, QDateTime &end_datetime,
int durationMinutes, int durationMinutes,
bool nextDay, bool nextDay,
bool prepaid) bool prepaid) {
{
if (cfg->YearPeriod.size() == 0 if (cfg->YearPeriod.size() == 0
&& cfg->SpecialDays.size() == 0 && cfg->SpecialDays.size() == 0
&& cfg->SpecialDaysWorktime.size() == 0) && cfg->SpecialDaysWorktime.size() == 0)
@ -405,58 +404,91 @@ double Calculator::GetCostFromDuration(Configuration* cfg,
return GetCostFromDuration(cfg, start_datetime, end_datetime); return GetCostFromDuration(cfg, start_datetime, end_datetime);
} }
//Get min and max time defined in JSON return private_GetCostFromDuration(cfg, start_datetime,
static int const minMin = std::max((int)cfg->PaymentOption.find(payment_option)->second.pop_min_time, 0); end_datetime, durationMinutes,
static int const maxMin = std::max((int)cfg->PaymentOption.find(payment_option)->second.pop_max_time, 0); nextDay, prepaid);
}
static const bool checkMinMaxMinutes = [](int minMin, int maxMin){ return (minMin < maxMin) ? true : false; }(minMin, maxMin); int Calculator::getMinimalParkingTime(Configuration const *cfg, PaymentMethod methodId) {
return std::max((int)cfg->PaymentOption.find(methodId)->second.pop_min_time, 0);
}
int Calculator::getMaximalParkingTime(Configuration const *cfg, PaymentMethod methodId) {
return std::max((int)cfg->PaymentOption.find(methodId)->second.pop_max_time, 0);
}
bool Calculator::checkDurationMinutes(bool overtime,
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;
}
if (durationMinutes < minParkingTime) {
qWarning() << QString("Total duration <= minMin (%1 <= %2)").arg(durationMinutes).arg(minParkingTime);
return false;
}
}
return true;
}
using namespace Utilities;
uint32_t Calculator::private_GetCostFromDuration(Configuration const* cfg,
QDateTime const &start,
QDateTime &end,
int durationMinutes,
bool nextDay,
bool prepaid,
bool overtime) {
// TODO
static const PaymentMethod paymentMethodId = PaymentMethod::Linear;
static int const minParkingTimeMinutes = getMinimalParkingTime(cfg, paymentMethodId);
static int const maxParkingTimeMinutes = getMaximalParkingTime(cfg, paymentMethodId);
static bool const checkMinMaxMinutes = (minParkingTimeMinutes < maxParkingTimeMinutes);
if (!checkMinMaxMinutes) { if (!checkMinMaxMinutes) {
qCritical() << QString("ERROR: CONDITION minMin < maxMin (%1 < %2) IS NOT VALID").arg(minMin).arg(maxMin); qCritical() << QString(
return 0.0; "ERROR: CONDITION minMin < maxMin (%1 < %2) IS NOT VALID")
.arg(minParkingTimeMinutes).arg(maxParkingTimeMinutes);
return 0;
}
if (!checkDurationMinutes(overtime, minParkingTimeMinutes,
maxParkingTimeMinutes, durationMinutes)) {
return 0;
} }
// Get input date // Get input date
QDateTime inputDate = start_datetime; QDateTime inputDate = start;
// Get day of week // Get day of week
int const weekdayId = inputDate.date().dayOfWeek(); int const weekdayId = inputDate.date().dayOfWeek();
// weekdayId = Utilities::ZellersAlgorithm(inputDate.date().day(),inputDate.date().month(),inputDate.date().year());
// Check overtime uint32_t day_price = 0;
if (!overtime) { uint32_t price_per_unit = 0;
if (durationMinutes > maxMin) {
qWarning() << QString("Total duration >= max_min (%1 >= %2)").arg(durationMinutes).arg(maxMin);
return maxMin;
}
if (durationMinutes < minMin) {
qWarning() << QString("Total duration <= minMin (%1 <= %2)").arg(durationMinutes).arg(minMin);
return 0.0f;
}
}
double day_price = 0.0;
int current_special_day_id = -1; int current_special_day_id = -1;
double price_per_unit = 0.0f;
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 index = 0;
if(Utilities::CheckSpecialDay(cfg, if(Utilities::CheckSpecialDay(cfg, inputDate, &current_special_day_id, &day_price)) {
inputDate.toString(Qt::ISODate).toStdString().c_str(), // Set special day price:
&current_special_day_id, // ACHTUNG: price_per_unit ist eigentlich immer preis pro minute !!!
&day_price)) { price_per_unit = CalculatePricePerUnit(day_price);
// Set special day price worktime[index].setTimeRange(SpecialDaysWorkTimeFrom(cfg, current_special_day_id),
price_per_unit = Utilities::CalculatePricePerUnit(day_price); SpecialDaysWorkTimeUntil(cfg, current_special_day_id));
worktime[index].setTimeRange(QTime::fromString(cfg->SpecialDaysWorktime.find(current_special_day_id)->second.pedwt_time_from.c_str()),
QTime::fromString(cfg->SpecialDaysWorktime.find(current_special_day_id)->second.pedwt_time_to.c_str()));
} else { } else {
// Set new price for the normal day // Set new price for the normal day
int pop_id = cfg->PaymentOption.find(payment_option)->second.pop_id; int pop_id = cfg->PaymentOption.find(paymentMethodId)->second.pop_id;
day_price = cfg->PaymentRate.find(pop_id)->second.pra_price; day_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;
@ -468,7 +500,7 @@ double Calculator::GetCostFromDuration(Configuration* cfg,
// When no workday found, go to next available day // 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); inputDate = inputDate.addDays(1);
return floor(GetCostFromDuration(cfg, payment_option, inputDate, end_datetime, durationMinutes, true, prepaid)); return private_GetCostFromDuration(cfg, inputDate, end, durationMinutes, true, prepaid, overtime);
} }
for (auto[itr, rangeEnd] = cfg->WeekDaysWorktime.equal_range(weekdayId); itr != rangeEnd; ++itr) { for (auto[itr, rangeEnd] = cfg->WeekDaysWorktime.equal_range(weekdayId); itr != rangeEnd; ++itr) {
@ -490,9 +522,9 @@ double Calculator::GetCostFromDuration(Configuration* cfg,
if (price_per_unit == 0) { if (price_per_unit == 0) {
inputDate = inputDate.addDays(1); inputDate = inputDate.addDays(1);
inputDate.setTime(worktime_from); inputDate.setTime(worktime_from);
double const partialCost = GetCostFromDuration(cfg, payment_option, inputDate, end_datetime, durationMinutes, true, prepaid); uint32_t const partialCost = private_GetCostFromDuration(cfg, inputDate, end, durationMinutes, true, prepaid);
if (partialCost <= __DBL_MIN__) { if (partialCost == 0) {
return 0.0; return 0;
} }
costFromDuration += partialCost; costFromDuration += partialCost;
continue; continue;
@ -517,9 +549,9 @@ double Calculator::GetCostFromDuration(Configuration* cfg,
} else if(inputDate.time() > worktime_to) { } else if(inputDate.time() > worktime_to) {
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";
inputDate = inputDate.addDays(1); inputDate = inputDate.addDays(1);
double const partialCost = GetCostFromDuration(cfg, payment_option, inputDate, end_datetime, durationMinutes, true, prepaid); uint32_t const partialCost = private_GetCostFromDuration(cfg, inputDate, end, durationMinutes, true, prepaid);
if (partialCost < __DBL_MIN__) { if (partialCost == 0) {
return 0.0; return 0;
} }
costFromDuration += partialCost; costFromDuration += partialCost;
continue; continue;
@ -545,7 +577,7 @@ double Calculator::GetCostFromDuration(Configuration* cfg,
// Go to next day if minutes not spent // Go to next day if minutes not spent
if(inputDate.time() >= worktime_to) { if(inputDate.time() >= worktime_to) {
// check for carry_over status // check for carry_over status
if (cfg->PaymentOption.find(payment_option)->second.pop_carry_over < 1) { if (cfg->PaymentOption.find(paymentMethodId)->second.pop_carry_over < 1) {
break; break;
} }
@ -553,9 +585,9 @@ double Calculator::GetCostFromDuration(Configuration* cfg,
inputDate = inputDate.addDays(1); inputDate = inputDate.addDays(1);
overtime = true; overtime = true;
double const partialCost = GetCostFromDuration(cfg, payment_option, inputDate, end_datetime, durationMinutes, false, false); uint32_t const partialCost = private_GetCostFromDuration(cfg, inputDate, end, durationMinutes, true, prepaid, overtime);
if (partialCost < __DBL_MIN__) { if (partialCost == 0) {
return 0.0; return 0;
} }
costFromDuration += partialCost; costFromDuration += partialCost;
break; // stop while, and continue in outer loop break; // stop while, and continue in outer loop
@ -570,7 +602,7 @@ double Calculator::GetCostFromDuration(Configuration* cfg,
qDebug() << "GetCostFromDuration(): Valid until:" << inputDate.toString(Qt::ISODate); qDebug() << "GetCostFromDuration(): Valid until:" << inputDate.toString(Qt::ISODate);
end_datetime = inputDate; end = inputDate;
//double ret_val = total_cost; //double ret_val = total_cost;
//total_cost = 0.0f; //total_cost = 0.0f;