diff --git a/library/src/calculator_functions.cpp b/library/src/calculator_functions.cpp index 03213af..4c8df6a 100644 --- a/library/src/calculator_functions.cpp +++ b/library/src/calculator_functions.cpp @@ -453,10 +453,47 @@ Calculator::ComputeDurationFromCost(Configuration *cfg, netto_parking_time_in_minutes = 0; brutto_parking_time_in_minutes = 0; free_parking_time_in_minutes = 0; + int nettoParktimeForCost = 0; - int const nettoParktimeForCost = priceNettoParktime[int(cost)]; - qCritical() << __func__ << ":" << __LINE__ << QString("cost=%1 nettoParkTimeForCost=%2"). - arg(cost).arg(nettoParktimeForCost); + if (priceNettoParktime.contains(int(cost))) { + nettoParktimeForCost = priceNettoParktime[int(cost)]; + qCritical() << __func__ << ":" << __LINE__ << QString("cost=%1 nettoParkTimeForCost=%2"). + arg(cost).arg(nettoParktimeForCost); + } else { + + // cannot find netto-parking time for cost (=price) + // this happens for direct coin input and should not happen otherwise + // take the value for the nearest value of cost and mark result as overpaid + + QList keys = priceNettoParktime.keys(); // keys (=prices) are sorted in ascending order + QSet s; // make keys unique + for (int k = 0; k < keys.size(); ++k) { + if (keys[k] < cost) { + s << keys[k]; + } + } + keys = s.values(); + // sort in descending order + std::sort(std::begin(keys), std::end(keys), std::greater<>()); + // qCritical() << __func__ << ":" << __LINE__ << "keys=" << keys; + if (!keys.isEmpty()) { + int const maxCost = keys[0]; + auto const p = priceNettoParktime.equal_range(maxCost); + for (auto it = p.first; it != p.second; ++it) { + nettoParktimeForCost = std::max(nettoParktimeForCost, it.value()); + } + ATBPaymentOption &po = cfg->getPaymentOptions(paymentOptionIndex); + po.pop_max_price = maxCost; + cfg->getPaymentOptions(paymentOptionIndex).pop_max_price = maxCost; + if (pop_allow_overpay) { + overPaid = true; + } + qCritical() << __func__ << ":" << __LINE__ << QString("cost=%1 -> maxCost=%2 nettoParkTimeForCost=%3"). + arg(cost).arg(maxCost).arg(nettoParktimeForCost); + } else { + qCritical() << __func__ << ":" << __LINE__ << "ERROR empty keys -> check tariff file"; + } + } int cnt = 0; while (++cnt < 20 && netto_parking_time_in_minutes < nettoParktimeForCost) {