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