GetDurationFromCost(): in case of progressive tariff

-> implement analogously to GetCostFromDuration().
This commit is contained in:
Gerhard Hoffmann 2024-02-19 11:59:06 +01:00
parent c0f0648a74
commit 9ea2037eca

View File

@ -115,7 +115,7 @@ QDateTime Calculator::GetDailyTicketDuration(Configuration* cfg, const QDateTime
/// <inheritdoc/>
std::string Calculator::GetDurationFromCost(Configuration* cfg,
uint8_t payment_option,
char const *start_datetime, // given in local time
char const *startDatetimePassed, // given in local time
double cost,
bool nextDay,
bool prepaid)
@ -124,7 +124,7 @@ std::string Calculator::GetDurationFromCost(Configuration* cfg,
Q_UNUSED(nextDay);
// Get input date
QDateTime inputDate = QDateTime::fromString(start_datetime,Qt::ISODate);
QDateTime inputDate = QDateTime::fromString(startDatetimePassed,Qt::ISODate);
static const PaymentMethod paymentMethodId = Utilities::getPaymentMethodId(cfg);
if (paymentMethodId == PaymentMethod::Steps) {
@ -152,44 +152,37 @@ std::string Calculator::GetDurationFromCost(Configuration* cfg,
// started with Neuhauser, Kirchdorf: merge into main algo. later
// for now try out some ideas
// started with Neuhauser, Kirchdorf: merge into main algo. later
// for now try out some ideas
static const bool carryOverNotSet = Utilities::isCarryOverNotSet(cfg, paymentMethodId);
static const uint minParkingPrice = Utilities::getMinimalParkingPrice(cfg, paymentMethodId);
Q_ASSERT_X(carryOverNotSet, __func__, "CARRYOVER SET (FOR KIRCHDORF)");
Q_ASSERT_X(prepaid, __func__, "PREPAID NOT SET (FOR KIRCHDORF)");
QDateTime start = QDateTime::fromString(QString(start_datetime));
QDateTime end = QDateTime();
QDateTime start_datetime = QDateTime::fromString(QString(startDatetimePassed), Qt::ISODate);
QDateTime start = start_datetime;
QDateTime end_datetime = QDateTime();
int weekdayId = -1;
int weekdayIdLast = -1;
int timeRanges = 0;
int durationMinutes = Utilities::getMaximalParkingTime(cfg, paymentMethodId);
int durationMinutesBrutto = 0;
int durationMinutes = Utilities::getMaximalParkingPrice(cfg, paymentMethodId);
QDateTime current = QDateTime::fromString(QString(start_datetime), Qt::ISODate);
#define DEBUG 1
//#define DEBUG 0
#if DEBUG==1
qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
<< "current" << current.time().toString(Qt::ISODate)
<< "durationMinutes" << durationMinutes
<< "durationMinutesBrutto" << durationMinutesBrutto;
#endif
QDateTime current = start;
int days = 7;
while (--days > 0) {
weekdayId = current.date().dayOfWeek();
weekdayIdLast = weekdayId; // TODO: some end condition in json-file
while ((timeRanges = cfg->WeekDaysWorktime.count(weekdayId)) == 0) {
while (cfg->WeekDaysWorktime.count(weekdayId) == 0) {
current = current.addDays(1);
weekdayId = current.date().dayOfWeek();
if (weekdayId == weekdayIdLast) {
qCritical() << "ERROR: NO VALID WORKDAY-TIMES DEFINED";
return 0;
return QDateTime().toString(Qt::ISODate).toStdString();
}
}
@ -205,29 +198,24 @@ std::string Calculator::GetDurationFromCost(Configuration* cfg,
}
}
if (current.time() >= to) {
if (carryOverNotSet) {
return end_datetime.toString(Qt::ISODate).toStdString();
} else {
QDateTime const dt = start;
start = start.addDays(1);
start.setTime(QTime(0, 0, 0));
durationMinutesBrutto += dt.secsTo(start) / 60;
#if DEBUG==1
qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
<< "start" << start.time().toString(Qt::ISODate)
<< "durationMinutes" << durationMinutes
<< "durationMinutesBrutto" << durationMinutesBrutto;
#endif
current = start;
}
} else {
break;
}
}
int durationMinutesNetto = 0;
uint price = cost;
uint price = 0;
if (carryOverNotSet) {
int range = 0;
@ -259,17 +247,8 @@ std::string Calculator::GetDurationFromCost(Configuration* cfg,
uint const minutesMoved = lastCurrent.secsTo(current) / 60;
durationMinutesBrutto += minutesMoved;
#if DEBUG==1
qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
<< "lastCurrent" << lastCurrent.time().toString(Qt::ISODate)
<< "current" << current.time().toString(Qt::ISODate)
<< "durationMinutes" << durationMinutes
<< "durationMinutesBrutto" << durationMinutesBrutto;
#endif
if (range == 1) {
// TODO
// start_datetime = current;
start_datetime = current;
}
}
}
@ -282,15 +261,7 @@ std::string Calculator::GetDurationFromCost(Configuration* cfg,
int duration = timeRange.time_range_to_in_minutes_from_start -
timeRange.time_range_from_in_minutes_from_start;
#if DEBUG==1
qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
<< "duration" << duration
<< "durationMinutes" << durationMinutes
<< "durationMinutesBrutto" << durationMinutesBrutto;
#endif
if (current.addSecs(duration * 60).time() <= to) {
for(const auto &x: cfg->PaymentRate) {
ATBPaymentRate const rate = x.second;
if (rate.pra_payment_unit_id == timeRange.time_range_payment_type_id) {
@ -300,57 +271,32 @@ std::string Calculator::GetDurationFromCost(Configuration* cfg,
durationMinutesBrutto += minsToCarryOver;
current = current.addSecs(minsToCarryOver*60);
minsToCarryOver = 0;
#if DEBUG==1
qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
<< "price" << price
<< "durationMinutes" << durationMinutes
<< "durationMinutesBrutto" << durationMinutesBrutto;
#endif
} else {
price -= (uint)rate.pra_price;
if (price >= cost) {
end_datetime = current;
return end_datetime.toString(Qt::ISODate).toStdString();
}
price += (uint)rate.pra_price;
durationMinutes -= duration;
durationMinutesNetto += duration;
durationMinutesBrutto += duration;
#if DEBUG==1
qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
<< "price" << price
<< "durationMinutes" << durationMinutes
<< "durationMinutesBrutto" << durationMinutesBrutto;
#endif
current = current.addSecs(duration * 60);
if (price <= 0) {
//end = start.addSecs(durationMinutesBrutto * 60);
end = current;
#if DEBUG==1
qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
<< "durationMinutesBrutto" << durationMinutesBrutto
<< "price" << price
<< "current" << current.toString(Qt::ISODate)
<< "end" << end.toString(Qt::ISODate);
#endif
return end.toString(Qt::ISODate).toStdString();
if (price >= cost) {
end_datetime = current;
return end_datetime.toString(Qt::ISODate).toStdString();
}
}
break;
}
}
#if DEBUG==1
qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
<< durationMinutes << durationMinutes;
#endif
if (durationMinutes <= 0) {
end = current;
return end.toString(Qt::ISODate).toStdString();
end_datetime = current;
return end_datetime.toString(Qt::ISODate).toStdString();
}
++timeRangeIt;
@ -369,33 +315,16 @@ std::string Calculator::GetDurationFromCost(Configuration* cfg,
durationMinutesNetto += minsLeft;
durationMinutesBrutto += minsLeft;
#if DEBUG==1
qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
<< "lastCurrent" << lastCurrent.time().toString(Qt::ISODate)
<< "current" << current.time().toString(Qt::ISODate)
<< "minsLeft" << minsLeft
<< "durationMinutes" << durationMinutes
<< "durationMinutesBrutto" << durationMinutesBrutto;
#endif
if (minsLeft > 0) {
for(const auto &x: cfg->PaymentRate) {
ATBPaymentRate const rate = x.second;
if (rate.pra_payment_unit_id == timeRange.time_range_payment_type_id) {
price -= (uint)rate.pra_price;
if (price <= 0) {
end = lastCurrent;
#if DEBUG==1
qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
<< "lastCurrent" << lastCurrent.time().toString(Qt::ISODate)
<< "current" << current.time().toString(Qt::ISODate)
<< "price" << price
<< "end" << end.toString(Qt::ISODate);
#endif
// return end.toString(Qt::ISODate).toStdString();
price += (uint)rate.pra_price;
if (price >= cost) {
end_datetime = current;
// return end_datetime.toString(Qt::ISODate).toStdString();
}
break;
} else break;
}
}
}
@ -405,21 +334,13 @@ std::string Calculator::GetDurationFromCost(Configuration* cfg,
}
}
end = current;
#if DEBUG==1
qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
<< "end" << end.toString(Qt::ISODate)
<< "durationMinutes" << durationMinutes
<< "durationMinutesBrutto" << durationMinutesBrutto;
#endif
return end.toString(Qt::ISODate).toStdString();
end_datetime = start.addSecs(durationMinutesBrutto * 60);
return end_datetime.toString(Qt::ISODate).toStdString();
}
}
end = QDateTime();
return end.toString(Qt::ISODate).toStdString();
end_datetime = QDateTime();
return end_datetime.toString(Qt::ISODate).toStdString();
}
Ticket t = private_GetDurationFromCost(cfg, inputDate, cost, prepaid);