728 lines
27 KiB
C++
728 lines
27 KiB
C++
#include "calculator_functions.h"
|
|
#include "payment_option.h"
|
|
#include "utilities.h"
|
|
#include "tariff_log.h"
|
|
#include "tariff_time_range.h"
|
|
#include "ticket.h"
|
|
|
|
#include <sstream>
|
|
#include <algorithm>
|
|
#include <QDateTime>
|
|
#include <QScopedArrayPointer>
|
|
#include <QDebug>
|
|
|
|
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) {
|
|
LOG_ERROR("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 weekdayId = 0;
|
|
weekdayId = Utilities::ZellersAlgorithm(inputDateTime.date().day(),inputDateTime.date().month(),inputDateTime.date().year());
|
|
|
|
// If no working day found, skip it (recursively call method again)
|
|
size_t found = 0;
|
|
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();
|
|
}
|
|
/// <inheritdoc/>
|
|
std::string Calculator::GetDurationFromCost(Configuration* cfg,
|
|
uint8_t payment_option,
|
|
char const* start_datetime, // given in local time
|
|
double price,
|
|
bool nextDay,
|
|
bool prepaid)
|
|
{
|
|
|
|
// Get input date
|
|
QDateTime inputDate = QDateTime::fromString(start_datetime,Qt::ISODate);
|
|
|
|
// use tariff with structure as for instance Schnau, Koenigsee:
|
|
// without given YearPeriod, SpecialDays and SpecialDaysWorktime
|
|
if (cfg->YearPeriod.size() == 0
|
|
&& cfg->SpecialDays.size() == 0
|
|
&& cfg->SpecialDaysWorktime.size() == 0)
|
|
{
|
|
inputDate = inputDate.addSecs(GetDurationForPrice(cfg, price) * 60);
|
|
|
|
return inputDate.toString(Qt::ISODate).toStdString();
|
|
}
|
|
|
|
|
|
// Get day of week
|
|
int weekdayId = 0;
|
|
weekdayId = Utilities::ZellersAlgorithm(inputDate.date().day(),inputDate.date().month(),inputDate.date().year());
|
|
|
|
//Get min and max time defined in JSON
|
|
double minMin = 0;
|
|
minMin = cfg->getPaymentOptions().pop_min_time;
|
|
|
|
double maxMin = 0;
|
|
maxMin = cfg->getPaymentOptions().pop_max_time;
|
|
|
|
double min_price = 0;
|
|
min_price = cfg->getPaymentOptions().pop_min_price;
|
|
|
|
if(price < min_price)
|
|
{
|
|
return "PARKING NOT ALLOWED";
|
|
}
|
|
|
|
if (minMin < 0) minMin = 0;
|
|
if (maxMin < 0) maxMin = 0;
|
|
if (minMin >= maxMin)
|
|
{
|
|
LOG_ERROR("Error: min_min cannot be greater or equal to max_min");
|
|
return "PARKING NOT ALLOWED";
|
|
}
|
|
|
|
if (maxMin <= minMin)
|
|
{
|
|
LOG_ERROR("Error: max_min cannot be lower or equal than min_min");
|
|
return "PARKING NOT ALLOWED";
|
|
}
|
|
|
|
|
|
// Get payment method
|
|
uint8_t p_method = PaymentMethod::Undefined;
|
|
p_method = payment_option;
|
|
LOG_DEBUG("Payment method id: ", (unsigned)p_method);
|
|
|
|
// Check special day
|
|
double day_price = 0.0f;
|
|
int current_special_day_id = -1;
|
|
bool is_special_day = Utilities::CheckSpecialDay(cfg, inputDate.toString(Qt::ISODate).toStdString().c_str(), ¤t_special_day_id, &day_price);
|
|
LOG_DEBUG("Special day: ", is_special_day);
|
|
|
|
double money_left = price;
|
|
double price_per_unit = 0.0f;
|
|
|
|
QTime worktime_from;
|
|
QTime worktime_to;
|
|
|
|
if(is_special_day)
|
|
{
|
|
// Set special day price
|
|
price_per_unit = Utilities::CalculatePricePerUnit(day_price);
|
|
worktime_from = QTime::fromString(cfg->SpecialDaysWorktime.find(current_special_day_id)->second.pedwt_time_from.c_str());
|
|
worktime_to = QTime::fromString(cfg->SpecialDaysWorktime.find(current_special_day_id)->second.pedwt_time_to.c_str());
|
|
}
|
|
else
|
|
{
|
|
// Set new price for the normal day
|
|
int pop_id = cfg->PaymentOption.find(payment_option)->second.pop_id;
|
|
day_price = cfg->PaymentRate.find(pop_id)->second.pra_price;
|
|
|
|
int durationId = cfg->PaymentRate.find(pop_id)->second.pra_payment_unit_id;
|
|
double durationUnit = cfg->Duration.find(durationId)->second.pun_duration;
|
|
price_per_unit = Utilities::CalculatePricePerUnit(day_price,durationUnit);
|
|
|
|
// If no working day found, skip it (recursively call method again)
|
|
size_t found = 0;
|
|
found = cfg->WeekDaysWorktime.count(weekdayId);
|
|
|
|
// When no workday found, go to next available day
|
|
if(found <=0)
|
|
{
|
|
LOG_DEBUG("- No workday found, trying to find next available day");
|
|
inputDate = inputDate.addDays(1);
|
|
return GetDurationFromCost(cfg, payment_option, inputDate.toString(Qt::ISODate).toStdString().c_str(), money_left,true,prepaid);
|
|
}
|
|
worktime_from = QTime::fromString(cfg->WeekDaysWorktime.find(weekdayId)->second.pwd_time_from.c_str());
|
|
worktime_to = QTime::fromString(cfg->WeekDaysWorktime.find(weekdayId)->second.pwd_time_to.c_str());
|
|
}
|
|
|
|
if (price_per_unit < 0) price_per_unit = 1.0f;
|
|
|
|
// if((price/price_per_unit) < minMin) return "PARKING NOT ALLOWED";
|
|
LOG_DEBUG("Calculated price per minute: ", price_per_unit);
|
|
|
|
// If overtime flag is set
|
|
if (overtime || nextDay)
|
|
{
|
|
inputDate.setTime(worktime_from);
|
|
overtime = false;
|
|
}
|
|
|
|
// Check prepaid
|
|
if (!prepaid)
|
|
{
|
|
if ((inputDate.time() < worktime_from) || (inputDate.time() > worktime_to))
|
|
{
|
|
LOG_DEBUG("[STOP] * Ticket is not valid * ");
|
|
return "PARKING NOT ALLOWED";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
LOG_DEBUG("* PREPAID MODE ACTIVE *");
|
|
if (inputDate.time() < worktime_from)
|
|
{
|
|
inputDate.setTime(worktime_from);
|
|
}
|
|
else if(inputDate.time() > worktime_to)
|
|
{
|
|
LOG_DEBUG(" *** PREPAID *** Current time is past the time range end, searching for next available day");
|
|
inputDate = inputDate.addDays(1);
|
|
return GetDurationFromCost(cfg, payment_option, inputDate.toString(Qt::ISODate).toStdString().c_str(), money_left, true);
|
|
}
|
|
}
|
|
|
|
while(true)
|
|
{
|
|
if((int)money_left <= 0) break;
|
|
|
|
// Check year period
|
|
bool isYearPeriodActive = false;
|
|
|
|
//// Parse input date
|
|
int dayCurrent = inputDate.date().day();
|
|
int monthCurrent = inputDate.date().month();
|
|
|
|
// Current date time
|
|
int cdt = (monthCurrent * 100) + dayCurrent;
|
|
|
|
multimap<int, ATBPeriodYear>::iterator year_period_itr;
|
|
for (year_period_itr = cfg->YearPeriod.begin(); year_period_itr != cfg->YearPeriod.end(); ++year_period_itr)
|
|
{
|
|
int dStart = year_period_itr->second.pye_start_day;
|
|
int dEnd = year_period_itr->second.pye_end_day;
|
|
|
|
int mStart = year_period_itr->second.pye_start_month;
|
|
int mEnd = year_period_itr->second.pye_end_month;
|
|
|
|
int start = (mStart * 100) + dStart;
|
|
int end = (mEnd * 100) + dEnd;
|
|
|
|
if (cdt >= start && cdt <= end) {
|
|
isYearPeriodActive = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!isYearPeriodActive)
|
|
{
|
|
LOG_DEBUG("Year period is not valid");
|
|
return "PARKING NOT ALLOWED";
|
|
}
|
|
|
|
if(total_duration_min > maxMin)
|
|
{
|
|
total_duration_min = maxMin;
|
|
break;
|
|
}
|
|
|
|
// If reached end of worktime go to next day
|
|
if(inputDate.time() >= worktime_to)
|
|
{
|
|
int carry_over_status = 0;
|
|
carry_over_status = cfg->PaymentOption.find(payment_option)->second.pop_carry_over;
|
|
if (carry_over_status < 1) break;
|
|
|
|
inputDate = inputDate.addDays(1);
|
|
overtime = true;
|
|
return GetDurationFromCost(cfg, payment_option, inputDate.toString(Qt::ISODate).toStdString().c_str(), money_left ,true, prepaid);
|
|
}
|
|
|
|
if(money_left > 1)
|
|
inputDate = inputDate.addSecs(60);
|
|
|
|
if(price_per_unit > 0) total_duration_min +=1;
|
|
money_left -= price_per_unit;
|
|
|
|
//qDebug() <<"Timestamp:" << inputDate << ", total duration min: " << total_duration_min << ", money left = " << money_left;
|
|
}
|
|
|
|
// if ((total_duration_min < minMin) || (price / price_per_unit) < minMin)
|
|
// {
|
|
// LOG_DEBUG("Total duration is lower than min_min");
|
|
// inputDate.time() = worktime_from;
|
|
// total_duration_min = 0;
|
|
// }
|
|
|
|
double ret_val = 0;
|
|
// double calc_price = (int)total_duration_min - (int)price / price_per_unit;
|
|
|
|
//if (calc_price > 0 && total_duration_min > 0)
|
|
//{
|
|
// inputDate = inputDate.addSecs(-(int)ceil(calc_price) * 60);
|
|
//}
|
|
|
|
if(price >= min_price && total_duration_min >= minMin)
|
|
qDebug() << "GetDurationFromCost(): Valid until: " << inputDate.toString(Qt::ISODate);
|
|
else
|
|
{
|
|
qDebug() << "Parking not allowed";
|
|
total_duration_min = 0;
|
|
}
|
|
|
|
ret_val = total_duration_min;
|
|
if(ret_val < 0) ret_val = 0;
|
|
qDebug() << "Duration: " << ret_val;
|
|
if (ret_val <= 0) return "PARKING NOT ALLOWED";
|
|
|
|
total_duration_min = 0;
|
|
return inputDate.toString(Qt::ISODate).toStdString();
|
|
}
|
|
|
|
///////////////////////////////////////
|
|
|
|
/// <inheritdoc/>
|
|
///
|
|
|
|
uint32_t Calculator::GetCostFromDuration(Configuration *cfg,
|
|
QDateTime const &start,
|
|
quint64 timeStepInMinutes) const {
|
|
// for instance, a tariff as used in Schoenau, Koenigssee: only steps, no
|
|
// special days, nonstop.
|
|
if (cfg->YearPeriod.size() == 0
|
|
&& cfg->SpecialDays.size() == 0
|
|
&& cfg->SpecialDaysWorktime.size() == 0) {
|
|
QDateTime const end = start.addSecs(timeStepInMinutes*60);
|
|
return GetCostFromDuration(cfg, start, end);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
uint32_t Calculator::GetCostFromDuration(Configuration * cfg,
|
|
QDateTime const &start,
|
|
QDateTime const &end) const {
|
|
if (cfg->YearPeriod.size() == 0
|
|
&& cfg->SpecialDays.size() == 0
|
|
&& cfg->SpecialDaysWorktime.size() == 0) {
|
|
int const timeStepInMinutes = start.secsTo(end) / 60;
|
|
return GetPriceForTimeStep(cfg, timeStepInMinutes);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////
|
|
|
|
/// <inheritdoc/>
|
|
double Calculator::GetCostFromDuration(Configuration* cfg,
|
|
uint8_t payment_option,
|
|
const QDateTime start_datetime,
|
|
QDateTime &end_datetime,
|
|
int durationMinutes,
|
|
bool nextDay,
|
|
bool prepaid) {
|
|
if (cfg->YearPeriod.size() == 0
|
|
&& cfg->SpecialDays.size() == 0
|
|
&& cfg->SpecialDaysWorktime.size() == 0)
|
|
{
|
|
end_datetime = start_datetime.addSecs(durationMinutes*60);
|
|
return GetCostFromDuration(cfg, start_datetime, end_datetime);
|
|
}
|
|
|
|
QDateTime start = start_datetime;
|
|
|
|
Ticket t = private_GetCostFromDuration(cfg, start,
|
|
durationMinutes,
|
|
prepaid);
|
|
if (t) {
|
|
qCritical().noquote() << t;
|
|
}
|
|
|
|
end_datetime = t.getValidUntil();
|
|
|
|
return t.getPrice();
|
|
}
|
|
|
|
int Calculator::getMinimalParkingTime(Configuration const *cfg, PaymentMethod methodId) {
|
|
return std::max((int)cfg->PaymentOption.find(methodId)->second.pop_min_time, 0);
|
|
}
|
|
|
|
int Calculator::getMaximalParkingTime(Configuration const *cfg, PaymentMethod methodId) {
|
|
return std::max((int)cfg->PaymentOption.find(methodId)->second.pop_max_time, 0);
|
|
}
|
|
|
|
bool Calculator::checkDurationMinutes(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<TariffTimeRange> 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<TariffTimeRange> 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;
|
|
continue;
|
|
}
|
|
}
|
|
return nextWorkTimeRange;
|
|
}
|
|
|
|
uint32_t Calculator::computeWeekDaysPrice(Configuration const *cfg, PaymentMethod id) const {
|
|
int pop_id = cfg->PaymentOption.find(id)->second.pop_id;
|
|
return cfg->PaymentRate.find(pop_id)->second.pra_price;
|
|
}
|
|
|
|
double Calculator::computeWeekDaysDurationUnit(Configuration const *cfg, PaymentMethod id) const {
|
|
int pop_id = cfg->PaymentOption.find(id)->second.pop_id;
|
|
int durationId = cfg->PaymentRate.find(pop_id)->second.pra_payment_unit_id;
|
|
return (double)(cfg->Duration.find(durationId)->second.pun_duration);
|
|
}
|
|
|
|
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 = getMinimalParkingTime(cfg, paymentMethodId);
|
|
static const int maxParkingTimeMinutes = getMaximalParkingTime(cfg, paymentMethodId);
|
|
static const bool checkMinMaxMinutes = (minParkingTimeMinutes < maxParkingTimeMinutes);
|
|
static const int durationMinutesNetto = durationMinutes;
|
|
static const uint32_t weekDaysPrice = computeWeekDaysPrice(cfg, paymentMethodId);
|
|
static const double weekDaysDurationUnit = 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<TariffTimeRange> 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<int, ATBWeekDaysWorktime>::const_iterator;
|
|
std::pair<WTIterator, WTIterator> 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...
|
|
currentRange = findWorkTimeRange(current, worktime, ranges);
|
|
if (currentRange == -1) { // no...
|
|
if (!prepaid) { // 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
|
|
currentRange = findNextWorkTimeRange(current, worktime, ranges);
|
|
current.setTime(worktime[currentRange].getTimeFrom());
|
|
}
|
|
}
|
|
}
|
|
|
|
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) {
|
|
// 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) || (current.time() > worktime_to)) {
|
|
qDebug() << "[STOP] * Ticket is not valid * ";
|
|
return Ticket();
|
|
}
|
|
} else {
|
|
qDebug() << "* PREPAID MODE ACTIVE *";
|
|
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 {
|
|
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;
|
|
} 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,
|
|
llround(Utilities::CalculatePricePerUnit(costFromDuration, durationUnit)),
|
|
Ticket::s[VALID]);
|
|
}
|
|
|
|
|
|
|
|
QList<int> Calculator::GetTimeSteps(Configuration *cfg) const {
|
|
QList<int> timeSteps;
|
|
|
|
int const pop_id = cfg->getPaymentOptions().pop_id;
|
|
|
|
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;
|
|
timeSteps << durationUnit;
|
|
}
|
|
|
|
return timeSteps;
|
|
}
|
|
|
|
uint32_t Calculator::GetPriceForTimeStep(Configuration *cfg, int timeStep) const {
|
|
|
|
int const pop_id = cfg->getPaymentOptions().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;
|
|
}
|