#include "calculator_functions.h" #include "payment_option.h" #include "utilities.h" #include "tariff_log.h" #include "tariff_time_range.h" #include "ticket.h" #include "tariff_global_defines.h" #include #include #include #include #include double total_duration_min = 0.0f; double total_cost = 0.0f; bool overtime = false; #ifdef _WIN32 inline struct tm* localtime_r(const time_t *clock, struct tm* result){ if(!clock || !result) return NULL; memcpy(result,localtime(clock),sizeof(*result)); return result; } #endif QDateTime Calculator::GetDailyTicketDuration(Configuration* cfg, const QDateTime start_datetime, uint8_t payment_option, bool carry_over) { if(!start_datetime.isValid()) { return QDateTime(); } double day_price = 0.0f; int current_special_day_id = -1; bool is_special_day = Utilities::CheckSpecialDay(cfg, start_datetime.toString(Qt::ISODate).toStdString().c_str(), ¤t_special_day_id, &day_price); QDateTime inputDateTime = start_datetime; QTime worktime_from; QTime worktime_to; int daily_card_price = cfg->PaymentOption.find(payment_option)->second.pop_daily_card_price; if(daily_card_price <= 0) { qCritical() << "Calculator::GetDailyTicketDuration(): Daily ticket price zero or less"; return QDateTime(); } if(is_special_day) { worktime_from = QTime::fromString(cfg->SpecialDaysWorktime.find(current_special_day_id)->second.pedwt_time_from.c_str(), Qt::ISODate); worktime_to = QTime::fromString(cfg->SpecialDaysWorktime.find(current_special_day_id)->second.pedwt_time_to.c_str(),Qt::ISODate); if(inputDateTime.time() < worktime_from) inputDateTime.setTime(worktime_from); if(carry_over) inputDateTime.setTime(worktime_from); if(inputDateTime.time() >= worktime_to) { // Go to next day if outside worktime inputDateTime = inputDateTime.addSecs(86400); return GetDailyTicketDuration(cfg,inputDateTime, payment_option,true); } if(day_price <=0) { // Go to next day if special day price is 0 inputDateTime = inputDateTime.addSecs(86400); return GetDailyTicketDuration(cfg,inputDateTime, payment_option,true); } int diff = abs(inputDateTime.time().secsTo(worktime_to)); inputDateTime = inputDateTime.addSecs(diff); //qDebug() << "Ticket is valid until: " << inputDateTime.toString(Qt::ISODate) << "price = " << daily_card_price << ", duration = " << diff / 60; return inputDateTime; } else { // Get day of week int const weekdayId = inputDateTime.date().dayOfWeek(); // If no working day found, skip it (recursively call method again) size_t found = cfg->WeekDaysWorktime.count(weekdayId); // When no workday found, go to next available day if(found <=0) { inputDateTime = inputDateTime.addSecs(86400); return GetDailyTicketDuration(cfg,inputDateTime, payment_option,true); } else { worktime_from = QTime::fromString(cfg->WeekDaysWorktime.find(weekdayId)->second.pwd_time_from.c_str(),Qt::ISODate); worktime_to = QTime::fromString(cfg->WeekDaysWorktime.find(weekdayId)->second.pwd_time_to.c_str(),Qt::ISODate); if(inputDateTime.time() < worktime_from) inputDateTime.setTime(worktime_from); if(carry_over) inputDateTime.setTime(worktime_from); if(inputDateTime.time() >= worktime_to) { // Go to next day if outside worktime inputDateTime = inputDateTime.addSecs(86400); return GetDailyTicketDuration(cfg,inputDateTime, payment_option,true); } int diff = abs(inputDateTime.time().secsTo(worktime_to)); inputDateTime = inputDateTime.addSecs(diff); //qDebug() << "Ticket is valid until: " << inputDateTime.toString(Qt::ISODate) << "price = " << daily_card_price << ", duration = " << diff / 60; return inputDateTime; } } return QDateTime(); } /// std::string Calculator::GetDurationFromCost(Configuration* cfg, uint8_t payment_option, char const *startDatetimePassed, // given in local time double cost, bool nextDay, bool prepaid) { Q_UNUSED(payment_option); Q_UNUSED(nextDay); // Get input date QDateTime inputDate = QDateTime::fromString(startDatetimePassed,Qt::ISODate); static const PaymentMethod paymentMethodId = Utilities::getPaymentMethodId(cfg); if (paymentMethodId == PaymentMethod::Steps) { if (tariffIs24_7(cfg)) { // use tariff with structure as for instance Schoenau, Koenigsee: // without given YearPeriod, SpecialDays and SpecialDaysWorktime inputDate = inputDate.addSecs(GetDurationForPrice(cfg, cost) * 60); return inputDate.toString(Qt::ISODate).toStdString(); } else { if (Utilities::IsYearPeriodActive(cfg, inputDate)) { if (!prepaid) { CalcState cs = isParkingAllowed(cfg, inputDate); if (cs) { inputDate.setTime(cs.getAllowedTimeRange().getTimeUntil()); return inputDate.toString(Qt::ISODate).toStdString(); } } // TODO: man braucht den richtigen Index int paymentOptionIndex = 0; int const pop_id = cfg->getPaymentOptions(paymentOptionIndex).pop_id; int const pop_max_price = cfg->getPaymentOptions(paymentOptionIndex).pop_max_price; int const pop_min_price = cfg->getPaymentOptions(paymentOptionIndex).pop_min_price; if (cost > pop_max_price) { qCritical() << DBG_HEADER << "MAX-PARKING-PRICE" << pop_max_price << ", COST" << cost; return CalcState::OVERPAID.toStdString(); } if (cost < pop_min_price) { qCritical() << DBG_HEADER << "MIN-PARKING-PRICE" << pop_min_price << ", COST" << cost; return CalcState::BELOW_MIN_PARKING_PRICE.toStdString(); } // int const pop_pre_paid = 1; if (prepaid) { // no limits on pre-pay-option, i.e. pre-pay-ranges are exactly // the complements of operational-ranges // find out if we are in a pre-pay-range. // in this case, adapt inputDate accordingly. qCritical() << DBG_HEADER << "PRE-PAID-OPTION: ADAPT-INPUT-DATE" << inputDate.toString(Qt::ISODate); QTime currentTime = inputDate.time(); int pwd_period_day_in_week_id = inputDate.date().dayOfWeek(); bool useWeekDaysWorkTimeOfOtherDay = true; for (auto[iter, rEnd] = cfg->WeekDaysWorktime.equal_range(pwd_period_day_in_week_id); iter != rEnd; ++iter) { QTime pwd_time_from = QTime::fromString(QString::fromStdString(iter->second.pwd_time_from), Qt::ISODate); QTime pwd_time_to = QTime::fromString(QString::fromStdString(iter->second.pwd_time_to), Qt::ISODate); if (inputDate.time() < pwd_time_from) { inputDate.setTime(pwd_time_from); useWeekDaysWorkTimeOfOtherDay = false; break; } if (currentTime <= pwd_time_to) { useWeekDaysWorkTimeOfOtherDay = false; break; } } if (useWeekDaysWorkTimeOfOtherDay) {// for the current day, we are above // the latest worktime-range -> find the next valid range QTime pwd_time_from_next_valid_range(0, 0, 0); int pwd_period_next_day_in_week_id = pwd_period_day_in_week_id; for (int days = 1; days < 8; ++days) { pwd_period_next_day_in_week_id += 1; if (pwd_period_next_day_in_week_id > (int)Qt::Sunday) { pwd_period_next_day_in_week_id = Qt::Monday; } if (cfg->WeekDaysWorktime.count(pwd_period_next_day_in_week_id) > 0) { for (auto[iter, rEnd] = cfg->WeekDaysWorktime.equal_range(pwd_period_next_day_in_week_id); iter != rEnd; ++iter) { QTime pwd_time_from = QTime::fromString(QString::fromStdString(iter->second.pwd_time_from), Qt::ISODate); if (pwd_time_from_next_valid_range < pwd_time_from) { pwd_time_from_next_valid_range = pwd_time_from; break; } } inputDate = inputDate.addDays(days); inputDate.setTime(pwd_time_from_next_valid_range); break; } } } } qCritical() << DBG_HEADER << "(ADAPTED) INPUT-DATE" << inputDate.toString(Qt::ISODate); // inputDate is now located in a valid operational-working-range // find this working-time-range int pwd_period_day_in_week_id = inputDate.date().dayOfWeek(); if (cfg->WeekDaysWorktime.count(pwd_period_day_in_week_id) == 0) { qCritical() << DBG_HEADER << "ERROR" << inputDate.toString(Qt::ISODate) << "NOT IN VALID WORKING TIME-RANGE"; return ""; } QTime current_working_time_from; QTime current_working_time_to; for (auto[iter, rEnd] = cfg->WeekDaysWorktime.equal_range(pwd_period_day_in_week_id); iter != rEnd; ++iter) { QTime pwd_time_from = QTime::fromString(QString::fromStdString(iter->second.pwd_time_from), Qt::ISODate); QTime pwd_time_to = QTime::fromString(QString::fromStdString(iter->second.pwd_time_to), Qt::ISODate); if (pwd_time_from <= inputDate.time() && inputDate.time() <= pwd_time_to) { current_working_time_from = pwd_time_from; current_working_time_to = pwd_time_to; } } if (current_working_time_from.isNull() || current_working_time_to.isNull()) { // can never happen qCritical() << DBG_HEADER << "ERROR" << inputDate.toString(Qt::ISODate) << "NOT IN VALID WORKING TIME-RANGE"; return ""; } qCritical() << DBG_HEADER << "CURRENT WORKING-TIME-FROM" << current_working_time_from.toString(Qt::ISODate); qCritical() << DBG_HEADER << " CURRENT WORKING-TIME-TO" << current_working_time_to.toString(Qt::ISODate); for (auto[itr, rangeEnd] = cfg->PaymentRate.equal_range(pop_id); itr != rangeEnd; ++itr) { int const pra_price = itr->second.pra_price; if ((double)pra_price == cost) { int const durationId = itr->second.pra_payment_unit_id; auto search = cfg->Duration.find(durationId); if (search != cfg->Duration.end()) { // found now the duration in minutes // check if we are still inside the working-time-range ATBDuration duration = search->second; int durationInSecs = duration.pun_duration * 60; QDateTime current_working_date_time_to = inputDate; current_working_date_time_to.setTime(current_working_time_to); qCritical() << DBG_HEADER << "DURATION IN MINUTES" << duration.pun_duration; qCritical() << DBG_HEADER << "DURATION IN SECONDS" << duration.pun_duration * 60; qCritical() << DBG_HEADER << "CURRENT-WORKING-DATE-TIME-TO" << current_working_date_time_to.toString(Qt::ISODate); qCritical() << DBG_HEADER << "NEW INPUT DATE" << inputDate.addSecs(durationInSecs).toString(Qt::ISODate); if (inputDate.addSecs(durationInSecs) > current_working_date_time_to) { qCritical() << DBG_HEADER; QTime next_working_time_from; if (cfg->getPaymentOptions(paymentOptionIndex).pop_carry_over != 0) { qCritical() << DBG_HEADER << "CARRY-OVER SET"; // check for next working-time-range on same day int day_in_week_id = inputDate.date().dayOfWeek(); for (auto[iter, rEnd] = cfg->WeekDaysWorktime.equal_range(day_in_week_id); iter != rEnd; ++iter) { QTime pwd_time_from = QTime::fromString(QString::fromStdString(iter->second.pwd_time_from), Qt::ISODate); if (pwd_time_from > current_working_time_to) { next_working_time_from = pwd_time_from; qCritical() << DBG_HEADER << "NEXT-WORKING-TIME-FROM" << next_working_time_from.toString(Qt::ISODate); break; } } // check for next working-time-range on following day(s) if (next_working_time_from.isNull()) { next_working_time_from = QTime(0, 0, 0); for (int days = 1; days < 8; ++days) { day_in_week_id += 1; if (day_in_week_id > (int)Qt::Sunday) { day_in_week_id = Qt::Monday; } if (cfg->WeekDaysWorktime.count(day_in_week_id) > 0) { for (auto[iter, rEnd] = cfg->WeekDaysWorktime.equal_range(day_in_week_id); iter != rEnd; ++iter) { QTime pwd_time_from = QTime::fromString(QString::fromStdString(iter->second.pwd_time_from), Qt::ISODate); if (next_working_time_from < pwd_time_from) { next_working_time_from = pwd_time_from; break; } } } QDateTime upper = inputDate.addDays(days); upper.setTime(next_working_time_from); QDateTime lower = inputDate; lower.setTime(current_working_time_to); inputDate = inputDate.addSecs(lower.secsTo(upper) + durationInSecs); qCritical() << DBG_HEADER << "TICKET-END" << inputDate.toString(Qt::ISODate); } } else { // next working-time is on same day QDateTime upper = inputDate; upper.setTime(next_working_time_from); QDateTime lower = inputDate; lower.setTime(current_working_time_to); inputDate = inputDate.addSecs(lower.secsTo(upper) + durationInSecs); qCritical() << DBG_HEADER << "TICKET-END" << inputDate.toString(Qt::ISODate); } } } else { inputDate = inputDate.addSecs(duration.pun_duration * 60); qCritical() << DBG_HEADER << "INPUT-DATE" << inputDate.toString(Qt::ISODate); } QString const &s = inputDate.toString(Qt::ISODate); qCritical() << DBG_HEADER << "TICKET-END" << s; return s.toStdString(); } } } return ""; } } } else if (paymentMethodId == PaymentMethod::Progressive) { // 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); static const uint maxParkingPrice = Utilities::getMaximalParkingPrice(cfg, paymentMethodId); if (cost < minParkingPrice) { qCritical() << QString("ERROR: COST < MIN_PARKING_PRICE (%1 < %2)").arg(cost).arg(minParkingPrice); return QDateTime().toString(Qt::ISODate).toStdString(); } if (cost > maxParkingPrice) { qCritical() << QString("WARN: COST > MAX_PARKING_PRICE (%1 > %2)").arg(cost).arg(maxParkingPrice); cost = maxParkingPrice; } Q_ASSERT_X(carryOverNotSet, __func__, "CARRYOVER SET (FOR KIRCHDORF)"); Q_ASSERT_X(prepaid, __func__, "PREPAID NOT SET (FOR KIRCHDORF)"); QDateTime start_datetime = QDateTime::fromString(QString(startDatetimePassed), Qt::ISODate); QDateTime start = start_datetime; QDateTime end_datetime = QDateTime(); int weekdayId = -1; int weekdayIdLast = -1; int durationMinutes = Utilities::getMaximalParkingTime(cfg, paymentMethodId); int durationMinutesBrutto = 0; #ifdef _DEBUG_ #undef _DEBUG_ #endif //#define _DEBUG_ 1 #define _DEBUG_ 0 QDateTime current = start; int days = 7; while (--days > 0) { weekdayId = current.date().dayOfWeek(); weekdayIdLast = weekdayId; // TODO: some end condition in json-file 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 QDateTime().toString(Qt::ISODate).toStdString(); } } using WTIterator = std::multimap::const_iterator; std::pair p = cfg->WeekDaysWorktime.equal_range(weekdayId); QTime to = QTime(0, 0, 0); for (WTIterator itr = p.first; itr != p.second; ++itr) { QTime const &t = Utilities::WeekDaysWorkTimeUntil(itr); if (to < t) { to = t; } } 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; current = start; } } else { break; } } int durationMinutesNetto = 0; uint price = 0; if (carryOverNotSet) { int minsToCarryOver = 0; // from one work-time to the other on the same day int minsUsed = 0; QDateTime lastCurrent = QDateTime(); QVector weekDayWorkTimeRanges; using WTIterator = std::multimap::const_iterator; std::pair p = cfg->WeekDaysWorktime.equal_range(weekdayId); for (WTIterator itr = p.first; itr != p.second; ++itr) { weekDayWorkTimeRanges.append(itr->second); // working with vector is easier } int weekDayWorkTimeIndex = 0; bool moveToNextTimeRange = false; // time ranges for Neuhauser-Kirchdorf (743): 30, 5, 5, ... 5 auto timeRangeIt = cfg->TimeRange.cbegin(); while (timeRangeIt != cfg->TimeRange.cend()) { // ; ++timeRangeIt) { if (weekDayWorkTimeIndex >= weekDayWorkTimeRanges.size()) { #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "weekDayWorkTimeIndex" << weekDayWorkTimeIndex << "weekDayWorkTimeRanges.size()" << weekDayWorkTimeRanges.size() << "price" << price; #endif end_datetime = current; return end_datetime.toString(Qt::ISODate).toStdString(); } QTime const &from = QTime::fromString(weekDayWorkTimeRanges[weekDayWorkTimeIndex].pwd_time_from.c_str(), Qt::ISODate); QTime const &to = QTime::fromString(weekDayWorkTimeRanges[weekDayWorkTimeIndex].pwd_time_to.c_str(), Qt::ISODate); #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "current" << current.toString(Qt::ISODate) << "from" << from.toString(Qt::ISODate) << "to" << to.toString(Qt::ISODate); #endif Q_ASSERT_X(from < to, __func__, "MISCONFIGURED WORK-TIMES"); if (current.time() >= to) { ++weekDayWorkTimeIndex; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "try to use next available work-time with" << "weekDayWorkTimeIndex" << weekDayWorkTimeIndex << "price" << price; #endif // time range is not affected continue; } else if (current.time() <= from) { if (prepaid) { lastCurrent = current; current.setTime(from); // move current forward (range==1), // as prepaid is set uint const minutesMoved = lastCurrent.secsTo(current) / 60; durationMinutesBrutto += minutesMoved; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "lastCurrent" << lastCurrent.toString(Qt::ISODate) << "current" << current.toString(Qt::ISODate) << "minutesMoved" << minutesMoved << "durationMinutesBrutto" << durationMinutesBrutto; #endif if (weekDayWorkTimeIndex == 0) { start_datetime = current; } } } ATBTimeRange timeRange = timeRangeIt->second; timeRange.computeQTimes(current.time()); int duration = timeRange.time_range_to_in_minutes_from_start - timeRange.time_range_from_in_minutes_from_start; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "current" << current.toString(Qt::ISODate) << "duration" << duration << "minsUsed" << minsUsed << "minsToCarryOver" << minsToCarryOver << "weekDayWorkTimeIndex" << weekDayWorkTimeIndex << "price" << price; #endif if (minsToCarryOver > 0) { // the price for this time range // has been is paid already Q_ASSERT_X(weekDayWorkTimeIndex > 0, __func__, "WRONG-WORK-TIME"); current = current.addSecs(minsToCarryOver*60); durationMinutes -= minsToCarryOver; durationMinutesNetto += minsToCarryOver; durationMinutesBrutto += minsToCarryOver; minsToCarryOver = 0; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "current" << current.toString(Qt::ISODate) << "duration" << duration << "durationMinutes" << durationMinutes << "durationMinutesNetto" << durationMinutesNetto << "durationMinutesBrutto" << durationMinutesBrutto << "price" << price; #endif if (price >= cost) { end_datetime = current; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "end_datetime" << end_datetime.toString(Qt::ISODate) << "price" << price; #endif return end_datetime.toString(Qt::ISODate).toStdString(); } } if (current.addSecs(duration * 60).time() <= to) { #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "current" << current.toString(Qt::ISODate) << "current.addSecs(" << duration * 60 << ")" << current.addSecs(duration*60).time().toString(Qt::ISODate) << "duration" << duration << "to" << to.toString(Qt::ISODate) << "minsUsed" << minsUsed << "minsToCarryOver" << minsToCarryOver << "weekDayWorkTimeIndex" << weekDayWorkTimeIndex << "durationMinutes" << durationMinutes; #endif moveToNextTimeRange = false; 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 >= maxParkingPrice) { price = maxParkingPrice; } durationMinutes -= duration; durationMinutesNetto += duration; durationMinutesBrutto += duration; current = current.addSecs(duration * 60); if (price >= cost) { end_datetime = current; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "end_datetime" << end_datetime.toString(Qt::ISODate) << "price" << price; #endif return end_datetime.toString(Qt::ISODate).toStdString(); } // price has been updated; use next time range moveToNextTimeRange = true; break; } } #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "current" << current.toString(Qt::ISODate) << "duration" << duration << "to" << to.toString(Qt::ISODate) << "price" << price << "minsToCarryOver" << minsToCarryOver << "weekDayWorkTimeIndex" << weekDayWorkTimeIndex << "durationMinutes" << durationMinutes << "durationMinutesNetto" << durationMinutesNetto << "durationMinutesBrutto" << durationMinutesBrutto; #endif if (price >= cost) { end_datetime = current; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "end_datetime" << end_datetime.toString(Qt::ISODate) << "price" << price; #endif return end_datetime.toString(Qt::ISODate).toStdString(); } if (moveToNextTimeRange) { if (++timeRangeIt != cfg->TimeRange.cend()) { continue; } } // havin a new time range means that we always have a new // work-time-range // ++timeRangeIt; } else { // current.addSecs(duration * 60).time() > to #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "current" << current.toString(Qt::ISODate) << "current.addSecs(" << duration * 60 << ")" << current.addSecs(duration*60).time().toString(Qt::ISODate) << "duration" << duration << ", to:" << to.toString(Qt::ISODate); #endif lastCurrent = current; current.setTime(to); minsUsed = lastCurrent.secsTo(current) / 60; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "lastCurrent" << lastCurrent.toString(Qt::ISODate) << "current" << current.toString(Qt::ISODate) << "duration" << duration << "to" << to.toString(Qt::ISODate) << "minsUsed" << minsUsed << "minsToCarryOver" << minsToCarryOver << "weekDayWorkTimeIndex" << weekDayWorkTimeIndex << "durationMinutes" << durationMinutes; #endif // mod duration: possibly discard some minutes in // the next time-range if (duration >= minsUsed) { minsToCarryOver = duration - minsUsed; } durationMinutes -= minsUsed; durationMinutesNetto += minsUsed; durationMinutesBrutto += minsUsed; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "lastCurrent" << lastCurrent.toString(Qt::ISODate) << "current" << current.toString(Qt::ISODate) << "duration" << duration << "to" << to.toString(Qt::ISODate) << "minsUsed" << minsUsed << "minsToCarryOver" << minsToCarryOver << "weekDayWorkTimeIndex" << weekDayWorkTimeIndex << "durationMinutes" << durationMinutes << "price" << price; #endif moveToNextTimeRange = false; if (minsUsed > 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 >= maxParkingPrice) { price = maxParkingPrice; } if (price >= cost) { end_datetime = current; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "end_datetime" << end_datetime.toString(Qt::ISODate) << "price" << price; #endif // return end_datetime.toString(Qt::ISODate).toStdString(); } // price has been updated; use next time range moveToNextTimeRange = true; break; } } } #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "current" << current.toString(Qt::ISODate) << "duration" << duration << "minsUsed" << minsUsed << "durationMinutes" << durationMinutes << "moveToNextTimeRange" << moveToNextTimeRange << "price" << price; #endif if (moveToNextTimeRange) { if (++timeRangeIt != cfg->TimeRange.cend()) { continue; } // no valid time range left end_datetime = current; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "end_datetime" << end_datetime.toString(Qt::ISODate) << "price" << price; #endif return end_datetime.toString(Qt::ISODate).toStdString(); } } end_datetime = start.addSecs(durationMinutesBrutto * 60); #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "start" << start.toString(Qt::ISODate) << "end_datetime" << end_datetime.toString(Qt::ISODate) << "final price" << std::max(price, minParkingPrice); #endif return end_datetime.toString(Qt::ISODate).toStdString(); } // while (timeRangeIt != cfg->TimeRange.cend()) { } #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "INVALID END TIME"; #endif end_datetime = QDateTime(); return end_datetime.toString(Qt::ISODate).toStdString(); } Ticket t = private_GetDurationFromCost(cfg, inputDate, cost, prepaid); // qCritical().noquote() << t; // TODO: im fehlerfall return t.getValidUntil().toString(Qt::ISODate).toStdString(); } #undef _DEBUG_ /////////////////////////////////////// /// /// uint32_t Calculator::GetCostFromDuration(Configuration *cfg, QDateTime const &start, quint64 timeStepInMinutes, int paymentOptionIndex) const { // for instance, a tariff as used in Schoenau, Koenigssee: only steps, no // special days, nonstop. static const PaymentMethod paymentMethodId = Utilities::getPaymentMethodId(cfg); if (paymentMethodId == PaymentMethod::Steps) { QDateTime const end = start.addSecs(timeStepInMinutes*60); return GetCostFromDuration(cfg, start, end, paymentOptionIndex); } return 0; } uint32_t Calculator::GetCostFromDuration(Configuration * cfg, QDateTime const &start, QDateTime const &end, int paymentOptionIndex) const { static const PaymentMethod paymentMethodId = Utilities::getPaymentMethodId(cfg); if (paymentMethodId == PaymentMethod::Steps) { int const timeStepInMinutes = start.secsTo(end) / 60; return GetPriceForTimeStep(cfg, timeStepInMinutes, paymentOptionIndex); } return 0; } CalcState Calculator::isParkingAllowedForWeekDay(Configuration const *cfg, QDateTime const &start, int netto_parking_time, int paymentOptionIndex) { qCritical() << DBG_HEADER << "start" << start.toString(Qt::ISODate) << "paymentOptionIndex" << paymentOptionIndex; QString errorStr = "UNKNOWN ERROR"; PaymentMethod const paymentMethodId = Utilities::getPaymentMethodId(cfg); if (paymentMethodId == PaymentMethod::Steps) { uint64_t const businessHours = cfg->getPaymentOptions(paymentOptionIndex).pop_business_hours; if (cfg->isDayIncluded(businessHours, start)) { if (businessHours == BusinessHours::NO_RESTRICTION_24_7) { return CalcState(CalcState::State::SUCCESS, "PARKING_ALLOWED", QTime(0, 0, 0), QTime(23, 59, 59)); } int const weekdayId = start.date().dayOfWeek(); // qCritical() << DBG_HEADER // << "weekdayId" << weekdayId // << "count" << cfg->WeekDaysWorktime.count(weekdayId); if (cfg->WeekDaysWorktime.count(weekdayId) > 0) { using WTIterator = std::multimap::const_iterator; std::pair p = cfg->WeekDaysWorktime.equal_range(weekdayId); for (WTIterator itr = p.first; itr != p.second; ++itr) { QTime const &from = Utilities::WeekDaysWorkTimeFrom(itr); QTime const &until = Utilities::WeekDaysWorkTimeUntil(itr); qCritical() << DBG_HEADER << "CHECK IF PARKING IS ALLOWED IN TIME-RANGE (" << from.toString(Qt::ISODate) << "->" << until.toString(Qt::ISODate) << ") ..."; QTime const &startTime = start.time(); if (startTime >= from && startTime <= until) { QDateTime const end = start.addSecs(netto_parking_time*60); QTime const endTime = end.time(); if (endTime <= until && start.date().dayOfWeek() == end.date().dayOfWeek()) { qCritical() << DBG_HEADER; return CalcState(CalcState::State::SUCCESS, "PARKING_ALLOWED", from, until); } else { errorStr = QString("%1 startTime not in range (%2 not in [%3, %4))") .arg(__LINE__) .arg(startTime.toString(Qt::ISODate)) .arg(from.toString(Qt::ISODate)) .arg(endTime.toString(Qt::ISODate)); } } else { errorStr = QString("%1 startTime not in range (%2 not in [%3, %4))") .arg(__LINE__) .arg(startTime.toString(Qt::ISODate)) .arg(from.toString(Qt::ISODate)) .arg(until.toString(Qt::ISODate)); } int const pop_carry_over = cfg->getPaymentOptions(paymentOptionIndex).pop_carry_over; // qCritical() << DBG_HEADER // << "paymentOptionIndex" << paymentOptionIndex // << "pop_carry_over" << pop_carry_over; if (pop_carry_over == 1) { // qCritical() << DBG_HEADER // << "NO. CHECK IF PARKING IS ALLOWED WITH CARRY-OVER ..."; int const pop_carry_over_start_time_range = cfg->getPaymentOptions(paymentOptionIndex).pop_carry_over_start_time_range; int const pop_carry_over_end_time_range = cfg->getPaymentOptions(paymentOptionIndex).pop_carry_over_end_time_range; // qCritical() << DBG_HEADER // << "pop_carry_over_start_time_range" << pop_carry_over_start_time_range // << "pop_carry_over_end_time_range" << pop_carry_over_end_time_range; if ((int)cfg->TimeRange.count(pop_carry_over_start_time_range) <= 0 && (int)cfg->TimeRange.count(pop_carry_over_end_time_range) <= 0) { qCritical() << DBG_HEADER << "PARKING_ALLOWED. startTime" << startTime.toString(Qt::ISODate); return CalcState(CalcState::State::SUCCESS, "PARKING_ALLOWED", startTime); } else // search entry in time-range-field of tariff-file if (cfg->TimeRange.count(pop_carry_over_start_time_range) == 1 && cfg->TimeRange.count(pop_carry_over_end_time_range) == 1) { ATBTimeRange s = cfg->TimeRange.find(pop_carry_over_start_time_range)->second; ATBTimeRange e = cfg->TimeRange.find(pop_carry_over_end_time_range)->second; // qCritical() << DBG_HEADER // << "startTime" << startTime.toString(Qt::ISODate); if (startTime >= s.getTimeFrom() && startTime <= s.getTimeUntil()) { QDateTime sd = start; sd.setTime(s.getTimeUntil()); // qCritical() << DBG_HEADER << "jumpFrom" << sd.toString(Qt::ISODate); QDateTime ed = start.addDays(1); ed.setTime(e.getTimeFrom()); // qCritical() << DBG_HEADER << "to" << ed.toString(Qt::ISODate); int const jumpSecs = sd.secsTo(ed); // qCritical() << DBG_HEADER << "jumpSecs" << jumpSecs; QDateTime const end = start.addSecs(netto_parking_time*60 + jumpSecs); // qCritical() << DBG_HEADER << "new end" << end.toString(Qt::ISODate); if (end.time() <= e.getTimeUntil()) { qCritical() << DBG_HEADER << "PARKING IS ALLOWED WITH CARRY-OVER (" << start.toString(Qt::ISODate) << "->" << ed.toString(Qt::ISODate) << ")"; return CalcState(CalcState::State::SUCCESS, "PARKING_ALLOWED", startTime, end.time()); } else { errorStr = QString("endTime %1 outside [%2, %3))") .arg(end.toString(Qt::ISODate)) .arg(sd.toString(Qt::ISODate)) .arg(ed.toString(Qt::ISODate)); } } else { errorStr = QString("startTime %1 outside [%2, %3))") .arg(startTime.toString(Qt::ISODate)) .arg(s.getTimeFrom().toString(Qt::ISODate)) .arg(s.getTimeUntil().toString(Qt::ISODate)); } } else { errorStr = "no carry-over limits configured"; } } else { errorStr = "no carry-over configured"; } } } else { errorStr = QString("no weekday configured for day-id %1").arg(weekdayId); } } else { errorStr = QString("start %1 not contained in business hours %2") .arg(start.toString(Qt::ISODate)) .arg(businessHours); } } qCritical() << DBG_HEADER << "errorStr" << errorStr; return CalcState(CalcState::State::OUTSIDE_ALLOWED_PARKING_TIME, errorStr, QTime(), QTime()); } CalcState Calculator::isParkingAllowedForSpecialDay(Configuration const *cfg, QDateTime const &start, int netto_parking_time, int paymentOptionIndex) { QString errorStr = "UNKNOWN ERROR"; qCritical() << DBG_HEADER << "start" << start.toString(Qt::ISODate) << "paymentOptionIndex" << paymentOptionIndex; PaymentMethod const paymentMethodId = Utilities::getPaymentMethodId(cfg); if (paymentMethodId == PaymentMethod::Steps) { //uint64_t const businessHours = cfg->getPaymentOptions(paymentOptionIndex).pop_business_hours; int const specialDayId = cfg->specialDayId(start); if ((specialDayId > 0) && (cfg->SpecialDaysWorktime.count(specialDayId) > 0)) { using SDIterator = Configuration::SpecialDaysWorktimeType::const_iterator; std::pair p = cfg->SpecialDaysWorktime.equal_range(specialDayId); for (SDIterator it = p.first; it != p.second; ++it) { QTime const &from = Utilities::SpecialDaysWorkTimeFrom(it); QTime const &until = Utilities::SpecialDaysWorkTimeUntil(it); qCritical() << DBG_HEADER << "CHECK IF PARKING IS ALLOWED IN TIME-RANGE (" << from.toString(Qt::ISODate) << "->" << until.toString(Qt::ISODate) << ") ..."; QTime const &startTime = start.time(); if (startTime >= from && startTime <= until) { QTime const endTime = start.addSecs(netto_parking_time*60).time(); if (endTime <= until) { return CalcState(CalcState::State::SUCCESS, "PARKING_ALLOWED", from, until); } else { errorStr = QString("%1 startTime not in range (%2 not in [%3, %4))") .arg(__LINE__) .arg(startTime.toString(Qt::ISODate)) .arg(from.toString(Qt::ISODate)) .arg(endTime.toString(Qt::ISODate)); } } else { errorStr = QString("%1 startTime not in range (%2 not in [%3, %4))") .arg(__LINE__) .arg(startTime.toString(Qt::ISODate)) .arg(from.toString(Qt::ISODate)) .arg(until.toString(Qt::ISODate)); } int const pop_carry_over = cfg->getPaymentOptions(paymentOptionIndex).pop_carry_over; if (pop_carry_over == 1) { qCritical() << DBG_HEADER << "NO. CHECK IF PARKING IS ALLOWED WITH CARRY-OVER ..."; int const pop_carry_over_start_time_range = cfg->getPaymentOptions(paymentOptionIndex).pop_carry_over_start_time_range; int const pop_carry_over_end_time_range = cfg->getPaymentOptions(paymentOptionIndex).pop_carry_over_end_time_range; if (cfg->TimeRange.count(pop_carry_over_start_time_range) == 1 && cfg->TimeRange.count(pop_carry_over_end_time_range) == 1) { ATBTimeRange s = cfg->TimeRange.find(pop_carry_over_start_time_range)->second; ATBTimeRange e = cfg->TimeRange.find(pop_carry_over_end_time_range)->second; if (startTime >= s.getTimeFrom() && startTime <= s.getTimeUntil()) { QDateTime sd = start; sd.setTime(s.getTimeUntil()); QDateTime ed = start.addDays(1); ed.setTime(e.getTimeFrom()); int const jumpSecs = sd.secsTo(ed); QDateTime const end = start.addSecs(netto_parking_time*60 + jumpSecs); if (end.time() <= e.getTimeUntil()) { ed.setTime(e.getTimeUntil()); // for printing qCritical() << DBG_HEADER << "PARKING IS ALLOWED WITH CARRY-OVER (" << start.toString(Qt::ISODate) << "->" << ed.toString(Qt::ISODate) << ")"; return CalcState(CalcState::State::SUCCESS, "PARKING_ALLOWED", startTime, end.time()); } else { ed.setTime(e.getTimeUntil()); // for printing errorStr = QString("endTime %1 exceeds [%2, %3))") .arg(end.toString(Qt::ISODate)) .arg(sd.toString(Qt::ISODate)) .arg(ed.toString(Qt::ISODate)); } } else { errorStr = QString("startTime %1 exceeds [%2, %3))") .arg(startTime.toString(Qt::ISODate)) .arg(s.getTimeFrom().toString(Qt::ISODate)) .arg(s.getTimeUntil().toString(Qt::ISODate)); } } else { errorStr = "no carry-over limits configured"; } } else { errorStr = "no carry-over configured"; } } } } return CalcState(CalcState::State::OUTSIDE_ALLOWED_PARKING_TIME, errorStr, QTime(), QTime()); } CalcState Calculator::isParkingAllowed(Configuration const *cfg, QDateTime const &start, int netto_parking_time, int paymentOptionIndex) { qCritical() << DBG_HEADER << "CHECK IF PARKING IS ALLOWED AT" << start.toString(Qt::ISODate) << "..."; CalcState cs; if ((cs = isParkingAllowedForWeekDay(cfg, start, netto_parking_time, paymentOptionIndex))) { return cs; } qCritical() << DBG_HEADER << QString(cs); if ((cs = isParkingAllowedForSpecialDay(cfg, start, netto_parking_time, paymentOptionIndex))) { return cs; } qCritical() << DBG_HEADER << QString(cs); return CalcState(CalcState::State::OUTSIDE_ALLOWED_PARKING_TIME, "UNKNOWN ERROR", QTime(), QTime()); } CalcState Calculator::isParkingAllowed(Configuration const *cfg, QDateTime const &start) { static const PaymentMethod paymentMethodId = Utilities::getPaymentMethodId(cfg); if (paymentMethodId == PaymentMethod::Steps) { int const weekdayId = start.date().dayOfWeek(); BusinessHours businessHours = Utilities::getBusinessHours(cfg, paymentMethodId); if (businessHours == BusinessHours::NoRestriction_24_7) { return CalcState(CalcState::State::SUCCESS, "PARKING_ALLOWED", QTime(0, 0, 0), QTime(23, 59, 59)); } else if (businessHours == BusinessHours::OnlyWeekDays) { if (weekdayId != (int)Qt::Saturday && weekdayId != (int)Qt::Sunday) { // e.g. Neuhauser, Linsinger Maschinenbau (741) if (cfg->WeekDaysWorktime.count(weekdayId) > 0) { using WTIterator = std::multimap::const_iterator; std::pair p = cfg->WeekDaysWorktime.equal_range(weekdayId); for (WTIterator itr = p.first; itr != p.second; ++itr) { QTime const &from = Utilities::WeekDaysWorkTimeFrom(itr); QTime const &until = Utilities::WeekDaysWorkTimeUntil(itr); QTime const &startTime = start.time(); if (from > startTime) { return CalcState(CalcState::State::OUTSIDE_ALLOWED_PARKING_TIME, QString("%1 < %2").arg(from.toString(Qt::ISODate)) .arg(startTime.toString(Qt::ISODate)), from, until); } else if (startTime >= until) { return CalcState(CalcState::State::OUTSIDE_ALLOWED_PARKING_TIME, QString("%1 >= %2").arg(startTime.toString(Qt::ISODate)) .arg(until.toString(Qt::ISODate)), from, until); } return CalcState(CalcState::State::SUCCESS, "PARKING ALLOWED", from, until); } } } } else if (businessHours == BusinessHours::AllDaysWithRestrictedHours) { // e.g. for Neuhauser, NAZ (744) if (cfg->WeekDaysWorktime.count(weekdayId) > 0) { using WTIterator = std::multimap::const_iterator; std::pair p = cfg->WeekDaysWorktime.equal_range(weekdayId); for (WTIterator itr = p.first; itr != p.second; ++itr) { QTime const &from = Utilities::WeekDaysWorkTimeFrom(itr); QTime const &until = Utilities::WeekDaysWorkTimeUntil(itr); QTime const &startTime = start.time(); if (from > startTime) { return CalcState(CalcState::State::OUTSIDE_ALLOWED_PARKING_TIME, QString("%1 < %2").arg(from.toString(Qt::ISODate)) .arg(startTime.toString(Qt::ISODate)), from, until); } else if (startTime >= until) { return CalcState(CalcState::State::OUTSIDE_ALLOWED_PARKING_TIME, QString("%1 >= %2").arg(startTime.toString(Qt::ISODate)) .arg(until.toString(Qt::ISODate)), from, until); } return CalcState(CalcState::State::SUCCESS, "PARKING ALLOWED", from, until); } } } } return CalcState(CalcState::State::OUTSIDE_ALLOWED_PARKING_TIME, "UNKNOWN ERROR", QTime(), QTime()); } /////////////////////////////////////// /// double Calculator::GetCostFromDuration(Configuration* cfg, uint8_t payment_option, QDateTime &start_datetime, QDateTime &end_datetime, int durationMinutes, bool nextDay, bool prepaid) { Q_UNUSED(payment_option); Q_UNUSED(nextDay); static const PaymentMethod paymentMethodId = Utilities::getPaymentMethodId(cfg); if (paymentMethodId == PaymentMethod::Steps) { if (tariffIs24_7(cfg)) { end_datetime = start_datetime.addSecs(durationMinutes*60); return GetCostFromDuration(cfg, start_datetime, end_datetime); } else { if (Utilities::IsYearPeriodActive(cfg, start_datetime)) { if (!prepaid) { CalcState cs = isParkingAllowed(cfg, start_datetime); if (cs) { end_datetime = start_datetime.addSecs(durationMinutes*60); double cost = GetCostFromDuration(cfg, start_datetime, end_datetime); end_datetime = start_datetime; end_datetime.setTime(cs.getAllowedTimeRange().getTimeUntil()); return cost; } } else { // it might be that in such a case even prepaid ("vorkauf") // is not allowed at any moment } qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "NOT YET IMPLEMENTED"; end_datetime = QDateTime(); return 0; } } } else if (paymentMethodId == PaymentMethod::Progressive) { // 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 = start_datetime; int weekdayId = -1; int weekdayIdLast = -1; int durationMinutesBrutto = 0; #ifdef _DEBUG_ #undef _DEBUG_ #endif //#define _DEBUG_ 1 #define _DEBUG_ 0 QDateTime current = start; int days = 7; while (--days > 0) { weekdayId = current.date().dayOfWeek(); weekdayIdLast = weekdayId; // TODO: some end condition in json-file 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; } } using WTIterator = std::multimap::const_iterator; std::pair p = cfg->WeekDaysWorktime.equal_range(weekdayId); QTime to = QTime(0, 0, 0); for (WTIterator itr = p.first; itr != p.second; ++itr) { QTime const &t = Utilities::WeekDaysWorkTimeUntil(itr); if (to < t) { to = t; } } if (current.time() >= to) { if (carryOverNotSet) { end_datetime = start; return 0; } else { QDateTime const dt = start; start = start.addDays(1); start.setTime(QTime(0, 0, 0)); durationMinutesBrutto += dt.secsTo(start) / 60; current = start; } } else { break; } } int durationMinutesNetto = 0; uint price = 0; if (carryOverNotSet) { int minsToCarryOver = 0; // from one work-time to the other on the same day int minsUsed = 0; QDateTime lastCurrent = QDateTime(); QVector weekDayWorkTimeRanges; using WTIterator = std::multimap::const_iterator; std::pair p = cfg->WeekDaysWorktime.equal_range(weekdayId); for (WTIterator itr = p.first; itr != p.second; ++itr) { weekDayWorkTimeRanges.append(itr->second); // working with vector is easier } int weekDayWorkTimeIndex = 0; bool moveToNextTimeRange = false; // time ranges for Neuhauser-Kirchdorf (743): 30, 5, 5, ... 5 auto timeRangeIt = cfg->TimeRange.cbegin(); while (timeRangeIt != cfg->TimeRange.cend()) { // ; ++timeRangeIt) { if (weekDayWorkTimeIndex >= weekDayWorkTimeRanges.size()) { #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "weekDayWorkTimeIndex" << weekDayWorkTimeIndex << "weekDayWorkTimeRanges.size()" << weekDayWorkTimeRanges.size(); #endif end_datetime = current; return price; } QTime const &from = QTime::fromString(weekDayWorkTimeRanges[weekDayWorkTimeIndex].pwd_time_from.c_str(), Qt::ISODate); QTime const &to = QTime::fromString(weekDayWorkTimeRanges[weekDayWorkTimeIndex].pwd_time_to.c_str(), Qt::ISODate); #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "current" << current.toString(Qt::ISODate) << "from" << from.toString(Qt::ISODate) << "to" << to.toString(Qt::ISODate); #endif Q_ASSERT_X(from < to, __func__, "MISCONFIGURED WORK-TIMES"); if (current.time() >= to) { ++weekDayWorkTimeIndex; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "try to use next available work-time with" << "weekDayWorkTimeIndex" << weekDayWorkTimeIndex << "price" << price; #endif // time range is not affected continue; } else if (current.time() <= from) { if (prepaid) { lastCurrent = current; current.setTime(from); // move current forward (range==1), // as prepaid is set uint const minutesMoved = lastCurrent.secsTo(current) / 60; durationMinutesBrutto += minutesMoved; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "lastCurrent" << lastCurrent.toString(Qt::ISODate) << "current" << current.toString(Qt::ISODate) << "minutesMoved" << minutesMoved << "durationMinutesBrutto" << durationMinutesBrutto; #endif if (weekDayWorkTimeIndex == 0) { start_datetime = current; } } } ATBTimeRange timeRange = timeRangeIt->second; timeRange.computeQTimes(current.time()); int duration = timeRange.time_range_to_in_minutes_from_start - timeRange.time_range_from_in_minutes_from_start; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "current" << current.toString(Qt::ISODate) << "duration" << duration << "minsUsed" << minsUsed << "minsToCarryOver" << minsToCarryOver << "weekDayWorkTimeIndex" << weekDayWorkTimeIndex << "price" << price; #endif if (minsToCarryOver > 0) { // the price for this time range // has been is paid already Q_ASSERT_X(weekDayWorkTimeIndex > 0, __func__, "WRONG-WORK-TIME"); current = current.addSecs(minsToCarryOver*60); durationMinutes -= minsToCarryOver; durationMinutesNetto += minsToCarryOver; durationMinutesBrutto += minsToCarryOver; minsToCarryOver = 0; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "current" << current.toString(Qt::ISODate) << "duration" << duration << "durationMinutes" << durationMinutes << "durationMinutesNetto" << durationMinutesNetto << "durationMinutesBrutto" << durationMinutesBrutto; #endif if (durationMinutes <= 0) { end_datetime = current; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "end_datetime" << end_datetime.toString(Qt::ISODate) << "price" << price; #endif return price; } } if (current.addSecs(duration * 60).time() <= to) { #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "current" << current.toString(Qt::ISODate) << "current.addSecs(" << duration * 60 << ")" << current.addSecs(duration*60).time().toString(Qt::ISODate) << "duration" << duration << "to" << to.toString(Qt::ISODate) << "minsUsed" << minsUsed << "minsToCarryOver" << minsToCarryOver << "weekDayWorkTimeIndex" << weekDayWorkTimeIndex << "durationMinutes" << durationMinutes; #endif moveToNextTimeRange = false; 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; durationMinutes -= duration; durationMinutesNetto += duration; durationMinutesBrutto += duration; current = current.addSecs(duration * 60); // price has been updated; use next time range moveToNextTimeRange = true; break; } } #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "current" << current.toString(Qt::ISODate) << "duration" << duration << "to" << to.toString(Qt::ISODate) << "price" << price << "minsToCarryOver" << minsToCarryOver << "weekDayWorkTimeIndex" << weekDayWorkTimeIndex << "durationMinutes" << durationMinutes << "durationMinutesNetto" << durationMinutesNetto << "durationMinutesBrutto" << durationMinutesBrutto; #endif if (durationMinutes <= 0) { end_datetime = current; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "end_datetime" << end_datetime.toString(Qt::ISODate) << "price" << price; #endif return price; } if (moveToNextTimeRange) { if (++timeRangeIt != cfg->TimeRange.cend()) { continue; } } // havin a new time range means that we always have a new // work-time-range // ++timeRangeIt; } else { // current.addSecs(duration * 60).time() > to #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "current" << current.toString(Qt::ISODate) << "current.addSecs(" << duration * 60 << ")" << current.addSecs(duration*60).time().toString(Qt::ISODate) << "duration" << duration << ", to:" << to.toString(Qt::ISODate); #endif lastCurrent = current; current.setTime(to); minsUsed = lastCurrent.secsTo(current) / 60; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "lastCurrent" << lastCurrent.toString(Qt::ISODate) << "current" << current.toString(Qt::ISODate) << "duration" << duration << "to" << to.toString(Qt::ISODate) << "minsUsed" << minsUsed << "minsToCarryOver" << minsToCarryOver << "weekDayWorkTimeIndex" << weekDayWorkTimeIndex << "durationMinutes" << durationMinutes; #endif // mod duration: possibly discard some minutes in // the next time-range if (duration >= minsUsed) { minsToCarryOver = duration - minsUsed; } durationMinutes -= minsUsed; durationMinutesNetto += minsUsed; durationMinutesBrutto += minsUsed; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "lastCurrent" << lastCurrent.toString(Qt::ISODate) << "current" << current.toString(Qt::ISODate) << "duration" << duration << "to" << to.toString(Qt::ISODate) << "minsUsed" << minsUsed << "minsToCarryOver" << minsToCarryOver << "weekDayWorkTimeIndex" << weekDayWorkTimeIndex << "durationMinutes" << durationMinutes << "price" << price; #endif moveToNextTimeRange = false; if (minsUsed > 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; // price has been updated; use next time range moveToNextTimeRange = true; break; } } } #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "current" << current.toString(Qt::ISODate) << "duration" << duration << "minsUsed" << minsUsed << "durationMinutes" << durationMinutes << "moveToNextTimeRange" << moveToNextTimeRange << "price" << price; #endif if (moveToNextTimeRange) { if (++timeRangeIt != cfg->TimeRange.cend()) { continue; } // no valid time range left end_datetime = current; #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "end_datetime" << end_datetime.toString(Qt::ISODate) << "price" << price; #endif return price; } } end_datetime = start.addSecs(durationMinutesBrutto * 60); #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "start" << start.toString(Qt::ISODate) << "end_datetime" << end_datetime.toString(Qt::ISODate) << "final price" << std::max(price, minParkingPrice); #endif return std::max(price, minParkingPrice); } // while (timeRangeIt != cfg->TimeRange.cend()) { } #if _DEBUG_==1 qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "INVALID END TIME"; #endif end_datetime = QDateTime(); return 0; } QDateTime start = start_datetime; Ticket t = private_GetCostFromDuration(cfg, start, durationMinutes, prepaid); if (t) { // qCritical().noquote() << t; } end_datetime = t.getValidUntil(); return t.getPrice(); } bool Calculator::checkDurationMinutes(int minParkingTime, int maxParkingTime, int durationMinutes) { 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; } int Calculator::findWorkTimeRange(QDateTime const &dt, QScopedArrayPointer const &worktime, size_t size) { for (size_t w = 0; w < size; ++w) { QTime const &worktime_from = worktime[w].getTimeFrom(); QTime const &worktime_to = worktime[w].getTimeUntil(); if ((dt.time() >= worktime_from) && (dt.time() < worktime_to)) { return w; } } return -1; } int Calculator::findNextWorkTimeRange(QDateTime const &dt, QScopedArrayPointer const &worktime, size_t size) { int nextWorkTimeRange = -1; for (size_t w = 0; w < size; ++w) { QTime const &worktime_from = worktime[w].getTimeFrom(); if (dt.time() < worktime_from) { nextWorkTimeRange = w; break; } } return nextWorkTimeRange; } using namespace Utilities; Ticket Calculator::private_GetCostFromDuration(Configuration const* cfg, QDateTime const &start, int durationMinutes, // Netto bool prepaid) { static const PaymentMethod paymentMethodId = Utilities::getPaymentMethodId(cfg); static const bool carryOverNotSet = isCarryOverNotSet(cfg, paymentMethodId); static const int minParkingTimeMinutes = Utilities::getMinimalParkingTime(cfg, paymentMethodId); static const int maxParkingTimeMinutes = Utilities::getMaximalParkingTime(cfg, paymentMethodId); static const bool checkMinMaxMinutes = (minParkingTimeMinutes < maxParkingTimeMinutes); static const int durationMinutesNetto = durationMinutes; static const uint32_t weekDaysPrice = Utilities::computeWeekDaysPrice(cfg, paymentMethodId); static const double weekDaysDurationUnit = Utilities::computeWeekDaysDurationUnit(cfg, paymentMethodId); static const double specialDaysDurationUnit = 60.0; if (!checkMinMaxMinutes) { qCritical() << QString( "ERROR: CONDITION minMin < maxMin (%1 < %2) IS NOT VALID") .arg(minParkingTimeMinutes).arg(maxParkingTimeMinutes); return Ticket(); } if (!checkDurationMinutes(minParkingTimeMinutes, maxParkingTimeMinutes, durationMinutes)) { return Ticket(); } uint32_t price = 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; for (current = start; durationMinutes > 0; current = current.addDays(1)) { int const weekdayId = current.date().dayOfWeek(); specialDayId = -1; // find worktime ranges for the current day int const timeRanges = std::max((int)cfg->WeekDaysWorktime.count(weekdayId), 1); QScopedArrayPointer worktime(new TariffTimeRange[timeRanges]); int ranges = 0; if((isSpecialDay = Utilities::CheckSpecialDay(cfg, current, &specialDayId, &price))) { // Set special day price: durationUnit = specialDaysDurationUnit; worktime[ranges].setTimeRange(SpecialDaysWorkTimeFrom(cfg, specialDayId), SpecialDaysWorkTimeUntil(cfg, specialDayId)); ranges = 1; } else { // 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. price = weekDaysPrice; durationUnit = weekDaysDurationUnit; // If no working day found, skip it (epsecially Sundays!) if (cfg->WeekDaysWorktime.count(weekdayId) <= 0) { qDebug() << "No workday found, trying to find next available day"; end = current; current.setTime(QTime()); // start at midnight on the next day continue; } using WTIterator = std::multimap::const_iterator; std::pair p = cfg->WeekDaysWorktime.equal_range(weekdayId); for (WTIterator itr = p.first; itr != p.second; ++itr) { worktime[ranges].setTimeRange(WeekDaysWorkTimeFrom(itr), WeekDaysWorkTimeUntil(itr)); ranges += 1; } } QTime const &lastWorktimeTo = worktime[ranges-1].getTimeUntil(); // qCritical() << "start" << start.toString(Qt::ISODate) // << "current" << current.toString(Qt::ISODate) << lastWorktimeTo; // find worktime range to start with int currentRange = 0; if (!isSpecialDay) { if (start != current) { // on next day current.setTime(worktime[currentRange].getTimeFrom()); } else { // check if inputDate is located inside a valid worktime-range... if ((currentRange = findWorkTimeRange(current, worktime, ranges)) == -1) { if (!prepaid && carryOverNotSet) { // parking is not allowed return Ticket(start, QDateTime(), durationMinutesNetto, 0, 0, Ticket::s[INVALID_FROM_DATETIME]); } // find the next worktime-range (on the same day), and start from there if ((currentRange = findNextWorkTimeRange(current, worktime, ranges)) == -1) { end = current; continue; } current.setTime(worktime[currentRange].getTimeFrom()); } } } // qCritical() << "current" << current.toString(Qt::ISODate); for (int w = currentRange; w < ranges; ++w, ++totalTimeRanges) { if (durationMinutes > 0) { QTime const &worktime_from = worktime[w].getTimeFrom(); QTime const &worktime_to = worktime[w].getTimeUntil(); if (totalTimeRanges) { // durationMinutes are always meant as netto time and // the time between worktime-ranges are free. current.setTime(worktime_from); } if (price == 0) { end = current; current.setTime(QTime()); continue; } if (current.time() == worktime_to) { end = current; current.setTime(QTime()); continue; } // Check prepaid if (!prepaid) { if ((current.time() < worktime_from) || (current.time() > worktime_to)) { qDebug() << "[STOP] * Ticket is not valid * "; return Ticket(); } } else { //qDebug() << "* PREPAID MODE ACTIVE *"; //qCritical() << "current" << current.toString(Qt::ISODate) << worktime_from << lastWorktimeTo; if (current.time() < worktime_from) { current.setTime(worktime_from); end = current; } else if(current.time() > lastWorktimeTo) { //qDebug() << " *** PREPAID *** Current time is past the time range end, searching for next available day"; end = current; current.setTime(QTime()); continue; } } while(durationMinutes > 0) { // Check for active year period if (!IsYearPeriodActive(cfg, current)) { return Ticket(); } if(current.time() >= lastWorktimeTo) { // Go to next day if minutes not spent if (carryOverNotSet) { // no carry_over, so stop computation break; } current.setTime(QTime()); break; // stop while, and continue in outer loop } else { //qCritical() << "current" << current.toString(Qt::ISODate) << worktime_to; if(current.time() < worktime_to) { // Increment input date minutes for each monetary unit current = current.addSecs(60); end = current; durationMinutes -= 1; //costFromDuration += price_per_unit; costFromDuration += price; //qCritical() << "current" << current.toString(Qt::ISODate); } 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; return Ticket(start, end, durationMinutesNetto, durationMinutesBrutto, ceil(Utilities::CalculatePricePerUnit(costFromDuration, durationUnit)), Ticket::s[VALID]); } Ticket Calculator::private_GetDurationFromCost(Configuration *cfg, QDateTime const &start, uint32_t cost, bool prepaid) { // Get input date QDateTime current = start; static const PaymentMethod paymentMethodId = Utilities::getPaymentMethodId(cfg); static const bool carryOverNotSet = isCarryOverNotSet(cfg, paymentMethodId); static const uint32_t minParkingTimeMinutes = std::max(Utilities::getMinimalParkingTime(cfg, paymentMethodId), 0); static const uint32_t maxParkingTimeMinutes = std::max(Utilities::getMaximalParkingTime(cfg, paymentMethodId), 0); static const uint32_t minParkingPrice = getMinimalParkingPrice(cfg, paymentMethodId); // static const bool checkMinMaxMinutes = (minParkingTimeMinutes < maxParkingTimeMinutes); static const uint32_t weekDaysPrice = Utilities::computeWeekDaysPrice(cfg, paymentMethodId); static const uint32_t weekDaysDurationUnit = Utilities::computeWeekDaysDurationUnit(cfg, paymentMethodId); static const uint32_t specialDaysDurationUnit = 60; if(cost < minParkingPrice) { uint64_t const durationMinutes = GetDurationForPrice(cfg, cost); return Ticket(start, current, durationMinutes, durationMinutes, cost, Ticket::s[INVALID_PRICE]); } if (minParkingTimeMinutes >= maxParkingTimeMinutes) { // TODO return Ticket(); } if (maxParkingTimeMinutes <= minParkingTimeMinutes) { // TODO return Ticket(); } uint32_t durationMinutesNetto = 0; double moneyLeft = cost; double durationUnit = 1; int specialDayId = -1; bool isSpecialDay = false; QDateTime end = start; int totalTimeRanges = 0; double price = 0; for (current = start; moneyLeft > 0 && moneyLeft >= price; current = current.addDays(1)) { int const weekdayId = current.date().dayOfWeek(); specialDayId = -1; // find worktime ranges for the current day int const timeRanges = std::max((int)cfg->WeekDaysWorktime.count(weekdayId), 1); QScopedArrayPointer worktime(new TariffTimeRange[timeRanges]); int ranges = 0; uint32_t p = 0; if((isSpecialDay = Utilities::CheckSpecialDay(cfg, current, &specialDayId, &p))) { // Set special day price: durationUnit = specialDaysDurationUnit; price = p / durationUnit; price = std::round(price * 1000.0) / 1000.0; worktime[ranges].setTimeRange(SpecialDaysWorkTimeFrom(cfg, specialDayId), SpecialDaysWorkTimeUntil(cfg, specialDayId)); ranges = 1; } else { // 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. price = weekDaysPrice; durationUnit = weekDaysDurationUnit; price /= durationUnit; price = std::round(price * 1000.0) / 1000.0; // round to 3 decimals // If no working day found, skip it (epsecially Sundays!) if (cfg->WeekDaysWorktime.count(weekdayId) <= 0) { // qDebug() << "No workday found, trying to find next available day"; end = current; current.setTime(QTime()); // start at midnight on the next day continue; } using WTIterator = std::multimap::const_iterator; std::pair p = cfg->WeekDaysWorktime.equal_range(weekdayId); for (WTIterator itr = p.first; itr != p.second; ++itr) { worktime[ranges].setTimeRange(WeekDaysWorkTimeFrom(itr), WeekDaysWorkTimeUntil(itr)); ranges += 1; } } QTime const &lastWorktimeTo = worktime[ranges-1].getTimeUntil(); // find worktime range to start with int currentRange = 0; if (!isSpecialDay) { if (start != current) { // on next day current.setTime(worktime[currentRange].getTimeFrom()); } else { // check if inputDate is located inside a valid worktime-range... if ((currentRange = findWorkTimeRange(current, worktime, ranges)) == -1) { if (!prepaid && carryOverNotSet) { // parking is not allowed return Ticket(start, QDateTime(), durationMinutesNetto, 0, 0, Ticket::s[INVALID_FROM_DATETIME]); } // find the next worktime-range (on the same day), and start from there if ((currentRange = findNextWorkTimeRange(current, worktime, ranges)) == -1) { end = current; continue; } current.setTime(worktime[currentRange].getTimeFrom()); } } } for (int w = currentRange; w < ranges; ++w, ++totalTimeRanges) { if (moneyLeft > 0) { QTime const &worktime_from = worktime[w].getTimeFrom(); QTime const &worktime_to = worktime[w].getTimeUntil(); if (totalTimeRanges) { // durationMinutes are always meant as netto time and // the time between worktime-ranges are free. current.setTime(worktime_from); } if (price == 0) { // inputDate = inputDate.addDays(1); // inputDate.setTime(worktime_from); end = current; current.setTime(QTime()); continue; } if (current.time() == worktime_to) { end = current; current.setTime(QTime()); continue; } // Check prepaid if (!prepaid) { if (current.time() < worktime_from) { qDebug() << "[STOP] TICKET IS NOT VALID: " << QString("%1 (current) < %2 (start)") .arg(current.toString(Qt::ISODate) .arg(worktime_from.toString(Qt::ISODate))); return Ticket(); } else if (current.time() > worktime_to) { qDebug() << "[STOP] TICKET IS NOT VALID: " << QString("%1 (current) > %2 (end)") .arg(current.toString(Qt::ISODate) .arg(worktime_to.toString(Qt::ISODate))); return Ticket(); } } else { if (current.time() < worktime_from) { qDebug() << "*** PREPAID *** Current time is before time range start, fast-forward to start" << worktime_from.toString(Qt::ISODate); current.setTime(worktime_from); end = current; } else if(current.time() > lastWorktimeTo) { qDebug() << " *** PREPAID *** Current time is past the time range end, searching for next available day"; end = current; current.setTime(QTime()); continue; } } while(moneyLeft >= price) { // Check for active year period if (!IsYearPeriodActive(cfg, current)) { return Ticket(); } // if(durationMinutesNetto >= maxParkingTimeMinutes) { // might be useful for overpayment // durationMinutesNetto = maxParkingTimeMinutes; // int durationMinutesBrutto = start.secsTo(end) / 60; // // return // Ticket(start, end, durationMinutesNetto, // durationMinutesBrutto, cost, Ticket::s[INVALID_PRICE]); // // } if(current.time() >= lastWorktimeTo) { // Go to next day if minutes not spent if (carryOverNotSet) { // no carry_over, so stop computation break; } current.setTime(QTime()); break; // stop while, and continue in outer loop } else { if(current.time() < worktime_to) { // Increment input date minutes for each monetary unit durationMinutesNetto += 1; moneyLeft -= price; moneyLeft = std::round(moneyLeft * 1000.0) / 1000.0; current = current.addSecs(60); //qCritical() << "moneyLeft" << moneyLeft // << "durationMinutesNetto" << durationMinutesNetto // << "current" << current.toString(Qt::ISODate); if(durationMinutesNetto <= maxParkingTimeMinutes) { // stop updating of end-date if parking time is // overshot end = current; } } 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; //qCritical() << "start" << start.toString(Qt::ISODate) << "end" // << end.toString(Qt::ISODate) << durationMinutesBrutto; return Ticket(start, end, durationMinutesNetto, durationMinutesBrutto, cost, Ticket::s[VALID]); } QList Calculator::GetPriceSteps(Configuration * /*cfg*/) const { return QList(); } QList Calculator::GetTimeSteps(Configuration *cfg, int paymentOptionIndex) const { qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "paymentOptionIndex:" << paymentOptionIndex; if (m_timeSteps.size() > paymentOptionIndex) { //qCritical() << __PRETTY_FUNCTION__ << "timeSteps:" << m_timeSteps; return m_timeSteps[paymentOptionIndex]; } else { m_timeSteps.push_back(QList()); } QDateTime start = QDateTime::currentDateTime(); start.setTime(QTime(start.time().hour(), start.time().minute(), 0)); int const pop_id = cfg->getPaymentOptions(paymentOptionIndex).pop_id; int const pop_carry_over = cfg->getPaymentOptions(paymentOptionIndex).pop_carry_over; int const pop_time_step_config = cfg->getPaymentOptions(paymentOptionIndex).pop_time_step_config; static PaymentMethod const paymentMethodId = Utilities::getPaymentMethodId(cfg); qCritical() << "(" << __func__ << ":" << __LINE__ << ") start parking time:" << start.toString(Qt::ISODate); qCritical() << "(" << __func__ << ":" << __LINE__ << ") payment option id:" << pop_id; qCritical() << "(" << __func__ << ":" << __LINE__ << ") payment option carry over:" << pop_carry_over; if (pop_time_step_config == (int)ATBTimeStepConfig::TimeStepConfig::DYNAMIC) { //qCritical() << __PRETTY_FUNCTION__ << "payment option time step config:" << "TimeStepConfig::DYNAMIC"; if (paymentMethodId == PaymentMethod::Progressive) { // e.g. neuhauser kirchdorf (743) std::size_t const s = cfg->TimeRange.size(); for (std::size_t id = 1; id <= s; ++id) { int const step = Utilities::getTimeRangeStep(cfg, id, paymentMethodId); m_timeSteps[paymentOptionIndex].append(step); } } else { uint16_t timeStepCompensation = 0; if (pop_carry_over) { int const pop_carry_over_time_range_id = cfg->getPaymentOptions(paymentOptionIndex).pop_carry_over_time_range_id; QTime const carryOverTimeRangeFrom = cfg->TimeRange.find(pop_carry_over_time_range_id)->second.time_range_from; QTime const carryOverTimeRangeTo = cfg->TimeRange.find(pop_carry_over_time_range_id)->second.time_range_to; if (carryOverTimeRangeFrom.secsTo(carryOverTimeRangeTo) <= 60) { // carry over time point, usually 00:00:00 if (carryOverTimeRangeFrom == QTime(0, 0, 0)) { for (auto[itr, rangeEnd] = cfg->PaymentRate.equal_range(pop_id); itr != rangeEnd; ++itr) { int const durationId = itr->second.pra_payment_unit_id; auto search = cfg->Duration.find(durationId); if (search != cfg->Duration.end()) { ATBDuration duration = search->second; if (durationId == 1) { QDateTime carryOver = start; carryOver = carryOver.addDays(1); carryOver.setTime(QTime(0, 0, 0)); int const timeStep = std::ceil(start.secsTo(carryOver) / 60.0); if (timeStep < duration.pun_duration_min || timeStep > duration.pun_duration_max) { qCritical() << QString("ERROR timeStep (%1) < durationMin (%2) || timeStep (%3)) > durationMax (%4)") .arg(timeStep).arg(duration.pun_duration_min) .arg(timeStep).arg(duration.pun_duration_max); break; } qCritical() << "(" << __func__ << ":" << __LINE__ << ") configured minimal parking time:" << cfg->getPaymentOptions(paymentOptionIndex).pop_min_time; // set dynamic minimal parking time cfg->getPaymentOptions(paymentOptionIndex).pop_min_time = timeStep; qCritical() << "(" << __func__ << ":" << __LINE__ << ") computed minimal parking time:" << cfg->getPaymentOptions(paymentOptionIndex).pop_min_time; duration.pun_duration = timeStep; timeStepCompensation = duration.pun_duration_max - duration.pun_duration; m_timeSteps[paymentOptionIndex] << duration.pun_duration; } else { duration.pun_duration = duration.pun_duration_max - timeStepCompensation; m_timeSteps[paymentOptionIndex] << duration.pun_duration;; } cfg->Duration.erase(search); cfg->Duration.insert(pair(duration.pun_id, duration)); } else { // if (search != cfg->Duration.end()) { // TODO } } } else { // if (carryOverTimeRangeFrom == QTime(0, 0, 0)) { // TODO } } else { // if (carryOverTimeRangeFrom == carryOverTimeRangeTo) { // TODO } } else { // if (pop_carry_over) { // TODO } } } else { qCritical() << "(" << __func__ << ":" << __LINE__ << ") payment option time step config:" << "TimeStepConfig::STATIC"; for (auto[itr, rangeEnd] = cfg->PaymentRate.equal_range(pop_id); itr != rangeEnd; ++itr) { int const durationId = itr->second.pra_payment_unit_id; int const durationUnit = cfg->Duration.find(durationId)->second.pun_duration; int size = m_timeSteps.size(); while (size <= paymentOptionIndex) { m_timeSteps.push_back(QList()); size = m_timeSteps.size(); } m_timeSteps[paymentOptionIndex] << durationUnit; } } qCritical() << "(" << __func__ << ":" << __LINE__ << ")" << "NEW timeSteps:" << m_timeSteps; return m_timeSteps[paymentOptionIndex]; } uint32_t Calculator::GetPriceForTimeStep(Configuration *cfg, int timeStep, int paymentOptionIndex) const { int const pop_id = cfg->getPaymentOptions(paymentOptionIndex).pop_id; for (auto[itr, rangeEnd] = cfg->PaymentRate.equal_range(pop_id); itr != rangeEnd; ++itr) { int const payment_unit_id = itr->second.pra_payment_unit_id; int const pun_id = cfg->Duration.find(payment_unit_id)->second.pun_id; Q_ASSERT(pun_id == payment_unit_id); int const pun_duration = cfg->Duration.find(payment_unit_id)->second.pun_duration; if (timeStep == pun_duration) { return (uint32_t)(itr->second.pra_price); } } return 0; } uint32_t Calculator::GetDurationForPrice(Configuration *cfg, int price) const { int const pop_id = cfg->getPaymentOptions().pop_id; uint32_t duration = 0; for (auto[itr, rangeEnd] = cfg->PaymentRate.equal_range(pop_id); itr != rangeEnd; ++itr) { int const durationId = itr->second.pra_payment_unit_id; int const pra_price = itr->second.pra_price; uint32_t const durationUnit = cfg->Duration.find(durationId)->second.pun_duration; if (pra_price == price) { return durationUnit; } if (pra_price < price) { duration = durationUnit; } } return duration; } std::optional Calculator::GetDailyTicketPrice(Configuration* cfg, QDateTime const &startDatetime, QDateTime &endTime, PERMIT_TYPE permitType) { struct price_t price; std::optional value; std::optional workTime = cfg->getWeekDayWorkTime(startDatetime.time(), (Qt::DayOfWeek)startDatetime.date().dayOfWeek()); if (workTime) { ATBWeekDaysWorktime const &wt = workTime.value(); endTime = startDatetime; endTime.setTime(QTime::fromString(wt.pwd_time_to.c_str(), Qt::ISODate)); std::optional> dailyTickets = cfg->getDailyTicketsForAllKeys(); if (dailyTickets) { QVector const tickets = dailyTickets.value(); switch (permitType) { case PERMIT_TYPE::TWENTY_FOUR_HOURS_TICKET: { // TODO } break; case PERMIT_TYPE::FOOD_STAMP: { // TODO } break; case PERMIT_TYPE::DAY_TICKET_ADULT: { std::optional c = cfg->getCustomerForType(ATBCustomer::CustomerType::ADULT); if (c) { for (QVector::size_type i=0; i const &paymentOptions = cfg->getAllPaymentOptions(); for (QVector::size_type j=0; j < paymentOptions.size(); ++j) { int const pop_id = paymentOptions.at(j).pop_id; std::optional> const &paymentRates = cfg->getPaymentRateForKey(pop_id); if (paymentRates) { QVector const &pr = paymentRates.value(); for (QVector::size_type k=0; k < pr.size(); ++k) { if (pr.at(k).pra_payment_option_id == pop_id) { if (priceId == pr.at(k).pra_payment_unit_id) { price.netto = pr.at(k).pra_price; value = value.value_or(price); } } } } } } } } } break; case PERMIT_TYPE::DAY_TICKET_TEEN: { std::optional c = cfg->getCustomerForType(ATBCustomer::CustomerType::TEEN); if (c) { for (QVector::size_type i=0; i const &paymentOptions = cfg->getAllPaymentOptions(); for (QVector::size_type j=0; j < paymentOptions.size(); ++j) { int const pop_id = paymentOptions.at(j).pop_id; std::optional> const &paymentRates = cfg->getPaymentRateForKey(pop_id); if (paymentRates) { QVector const &pr = paymentRates.value(); for (QVector::size_type k=0; k < pr.size(); ++k) { if (pr.at(k).pra_payment_option_id == pop_id) { if (priceId == pr.at(k).pra_payment_unit_id) { price.netto = pr.at(k).pra_price; value = value.value_or(price); } } } } } } } } } break; case PERMIT_TYPE::DAY_TICKET_CHILD: { std::optional c = cfg->getCustomerForType(ATBCustomer::CustomerType::CHILD); if (c) { for (QVector::size_type i=0; i const &paymentOptions = cfg->getAllPaymentOptions(); for (QVector::size_type j=0; j < paymentOptions.size(); ++j) { int const pop_id = paymentOptions.at(j).pop_id; std::optional> const &paymentRates = cfg->getPaymentRateForKey(pop_id); if (paymentRates) { QVector const &pr = paymentRates.value(); for (QVector::size_type k=0; k < pr.size(); ++k) { if (pr.at(k).pra_payment_option_id == pop_id) { if (priceId == pr.at(k).pra_payment_unit_id) { price.netto = pr.at(k).pra_price; value = value.value_or(price); } } } } } } } } } // [[fallthrough]]; case PERMIT_TYPE::SHORT_TERM_PARKING: { } // [[fallthrough]]; case PERMIT_TYPE::DAY_TICKET: { } // [[fallthrough]]; case PERMIT_TYPE::SZEGED_START: // [[fallthrough]]; case PERMIT_TYPE::SZEGED_STOP: // [[fallthrough]]; case PERMIT_TYPE::INVALID: break; } } else { // for projects which have not defined a daily ticket in their // tariff-files (e.g. szeged) price.netto = cfg->getPaymentOptions().pop_daily_card_price; qCritical() << "( GetDailyTicketPrice():" << __LINE__ << ")"; qCritical() << " start:" << startDatetime.toString(Qt::ISODate); qCritical() << " workTime from:" << QTime::fromString(QString(wt.pwd_time_from.c_str()), Qt::ISODate); qCritical() << " workTime to:" << QTime::fromString(QString(wt.pwd_time_to.c_str()), Qt::ISODate); qCritical() << "daily_ticket_card_price:" << price.netto; value = value.value_or(price); } } return value; }