2023-04-24 15:31:46 +02:00
|
|
|
#include "calculator_functions.h"
|
2023-11-22 08:55:00 +01:00
|
|
|
#include "payment_option.h"
|
2023-04-24 15:31:46 +02:00
|
|
|
#include "utilities.h"
|
|
|
|
#include "tariff_log.h"
|
|
|
|
|
|
|
|
#include <sstream>
|
2023-05-12 09:20:46 +02:00
|
|
|
#include <QDateTime>
|
|
|
|
#include <qdebug.h>
|
2023-04-24 15:31:46 +02:00
|
|
|
|
|
|
|
double total_duration_min = 0.0f;
|
|
|
|
double total_cost = 0.0f;
|
|
|
|
bool overtime = false;
|
|
|
|
|
2023-05-12 09:20:46 +02:00
|
|
|
#ifdef _WIN32
|
2023-05-12 11:55:35 +02:00
|
|
|
inline struct tm* localtime_r(const time_t *clock, struct tm* result){
|
2023-05-12 09:20:46 +02:00
|
|
|
if(!clock || !result) return NULL;
|
|
|
|
memcpy(result,localtime(clock),sizeof(*result));
|
|
|
|
return result;
|
2023-05-12 11:55:35 +02:00
|
|
|
}
|
2023-05-12 09:20:46 +02:00
|
|
|
#endif
|
|
|
|
|
2023-05-16 16:43:45 +02:00
|
|
|
QDateTime Calculator::GetDailyTicketDuration(Configuration* cfg, const QDateTime start_datetime, uint8_t payment_option, bool carry_over)
|
2023-05-15 14:05:55 +02:00
|
|
|
{
|
2023-05-16 16:43:45 +02:00
|
|
|
if(!start_datetime.isValid()) {
|
|
|
|
return QDateTime();
|
|
|
|
}
|
2023-05-15 14:05:55 +02:00
|
|
|
|
|
|
|
double day_price = 0.0f;
|
|
|
|
int current_special_day_id = -1;
|
2023-05-16 16:43:45 +02:00
|
|
|
bool is_special_day = Utilities::CheckSpecialDay(cfg, start_datetime.toString(Qt::ISODate).toStdString().c_str(), ¤t_special_day_id, &day_price);
|
2023-05-15 14:05:55 +02:00
|
|
|
|
2023-05-16 16:43:45 +02:00
|
|
|
QDateTime inputDateTime = start_datetime;
|
2023-05-15 14:05:55 +02:00
|
|
|
QTime worktime_from;
|
|
|
|
QTime worktime_to;
|
|
|
|
|
|
|
|
int daily_card_price = cfg->PaymentOption.find(payment_option)->second.pop_daily_card_price;
|
2023-05-16 16:43:45 +02:00
|
|
|
if(daily_card_price <= 0) {
|
|
|
|
LOG_ERROR("Calculator::GetDailyTicketDuration(): Daily ticket price zero or less");
|
|
|
|
return QDateTime();
|
|
|
|
}
|
2023-05-15 14:05:55 +02:00
|
|
|
|
|
|
|
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);
|
2023-05-16 16:43:45 +02:00
|
|
|
return GetDailyTicketDuration(cfg,inputDateTime, payment_option,true);
|
2023-05-15 14:05:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(day_price <=0)
|
|
|
|
{
|
|
|
|
// Go to next day if special day price is 0
|
|
|
|
inputDateTime = inputDateTime.addSecs(86400);
|
2023-05-16 16:43:45 +02:00
|
|
|
return GetDailyTicketDuration(cfg,inputDateTime, payment_option,true);
|
2023-05-15 14:05:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2023-05-16 16:43:45 +02:00
|
|
|
return inputDateTime;
|
2023-05-15 14:05:55 +02:00
|
|
|
}
|
|
|
|
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);
|
2023-05-16 16:43:45 +02:00
|
|
|
return GetDailyTicketDuration(cfg,inputDateTime, payment_option,true);
|
2023-05-15 14:05:55 +02:00
|
|
|
}
|
|
|
|
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);
|
2023-05-16 16:43:45 +02:00
|
|
|
return GetDailyTicketDuration(cfg,inputDateTime, payment_option,true);
|
2023-05-15 14:05:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int diff = abs(inputDateTime.time().secsTo(worktime_to));
|
|
|
|
inputDateTime = inputDateTime.addSecs(diff);
|
|
|
|
|
2023-05-16 16:43:45 +02:00
|
|
|
//qDebug() << "Ticket is valid until: " << inputDateTime.toString(Qt::ISODate) << "price = " << daily_card_price << ", duration = " << diff / 60;
|
|
|
|
return inputDateTime;
|
2023-05-15 14:05:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-16 16:43:45 +02:00
|
|
|
return QDateTime();
|
2023-05-15 14:05:55 +02:00
|
|
|
}
|
2023-04-24 15:31:46 +02:00
|
|
|
/// <inheritdoc/>
|
2023-05-10 13:08:28 +02:00
|
|
|
std::string Calculator::GetDurationFromCost(Configuration* cfg,
|
|
|
|
uint8_t payment_option,
|
|
|
|
char const* start_datetime, // given in local time
|
|
|
|
double price,
|
|
|
|
bool nextDay,
|
|
|
|
bool prepaid)
|
2023-04-24 15:31:46 +02:00
|
|
|
{
|
2023-11-23 11:14:21 +01:00
|
|
|
|
|
|
|
// Get input date
|
|
|
|
QDateTime inputDate = QDateTime::fromString(start_datetime,Qt::ISODate);
|
|
|
|
|
2023-11-23 09:38:34 +01:00
|
|
|
// 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)
|
|
|
|
{
|
2023-11-23 11:14:21 +01:00
|
|
|
inputDate = inputDate.addSecs(GetDurationForPrice(cfg, price) * 60);
|
|
|
|
|
|
|
|
return inputDate.toString(Qt::ISODate).toStdString();
|
2023-11-23 09:38:34 +01:00
|
|
|
}
|
|
|
|
|
2023-05-10 13:08:28 +02:00
|
|
|
|
2023-05-12 11:55:35 +02:00
|
|
|
// 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;
|
2023-11-23 08:42:35 +01:00
|
|
|
minMin = cfg->getPaymentOptions().pop_min_time;
|
2023-05-12 11:55:35 +02:00
|
|
|
|
|
|
|
double maxMin = 0;
|
2023-11-23 08:42:35 +01:00
|
|
|
maxMin = cfg->getPaymentOptions().pop_max_time;
|
2023-05-10 13:08:28 +02:00
|
|
|
|
2023-05-12 11:55:35 +02:00
|
|
|
double min_price = 0;
|
2023-11-23 08:42:35 +01:00
|
|
|
min_price = cfg->getPaymentOptions().pop_min_price;
|
2023-05-14 17:19:24 +02:00
|
|
|
|
2023-05-12 11:55:35 +02:00
|
|
|
if(price < min_price)
|
|
|
|
{
|
|
|
|
return "PARKING NOT ALLOWED";
|
|
|
|
}
|
2023-05-10 13:08:28 +02:00
|
|
|
|
2023-05-12 11:55:35 +02:00
|
|
|
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";
|
2023-05-10 13:08:28 +02:00
|
|
|
}
|
|
|
|
|
2023-05-12 11:55:35 +02:00
|
|
|
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;
|
2023-05-09 13:03:51 +02:00
|
|
|
|
2023-05-12 11:55:35 +02:00
|
|
|
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
|
2023-05-17 10:08:02 +02:00
|
|
|
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);
|
2023-05-12 11:55:35 +02:00
|
|
|
|
|
|
|
// 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;
|
2023-05-14 17:19:24 +02:00
|
|
|
|
2023-06-13 12:12:23 +02:00
|
|
|
// if((price/price_per_unit) < minMin) return "PARKING NOT ALLOWED";
|
2023-05-12 11:55:35 +02:00
|
|
|
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)
|
|
|
|
{
|
2023-05-14 16:15:37 +02:00
|
|
|
if((int)money_left <= 0) break;
|
2023-05-12 11:55:35 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2023-05-12 12:40:25 +02:00
|
|
|
if(money_left > 1)
|
2023-05-12 13:02:10 +02:00
|
|
|
inputDate = inputDate.addSecs(60);
|
2023-05-12 12:40:25 +02:00
|
|
|
|
2023-05-12 11:55:35 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-05-12 13:02:10 +02:00
|
|
|
// 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;
|
|
|
|
// }
|
2023-05-12 11:55:35 +02:00
|
|
|
|
|
|
|
double ret_val = 0;
|
2023-06-13 12:12:23 +02:00
|
|
|
// double calc_price = (int)total_duration_min - (int)price / price_per_unit;
|
2023-05-12 11:55:35 +02:00
|
|
|
|
2023-06-13 12:12:23 +02:00
|
|
|
//if (calc_price > 0 && total_duration_min > 0)
|
|
|
|
//{
|
|
|
|
// inputDate = inputDate.addSecs(-(int)ceil(calc_price) * 60);
|
|
|
|
//}
|
2023-05-12 11:55:35 +02:00
|
|
|
|
|
|
|
if(price >= min_price && total_duration_min >= minMin)
|
2023-05-16 15:31:53 +02:00
|
|
|
qDebug() << "GetDurationFromCost(): Valid until: " << inputDate.toString(Qt::ISODate);
|
2023-05-12 13:02:10 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug() << "Parking not allowed";
|
|
|
|
total_duration_min = 0;
|
|
|
|
}
|
2023-05-12 11:55:35 +02:00
|
|
|
|
2023-05-12 13:02:10 +02:00
|
|
|
ret_val = total_duration_min;
|
2023-05-12 11:55:35 +02:00
|
|
|
if(ret_val < 0) ret_val = 0;
|
|
|
|
qDebug() << "Duration: " << ret_val;
|
|
|
|
if (ret_val <= 0) return "PARKING NOT ALLOWED";
|
2023-04-24 15:31:46 +02:00
|
|
|
|
2023-05-12 11:55:35 +02:00
|
|
|
total_duration_min = 0;
|
|
|
|
return inputDate.toString(Qt::ISODate).toStdString();
|
2023-04-24 15:31:46 +02:00
|
|
|
}
|
|
|
|
|
2023-11-22 08:55:00 +01:00
|
|
|
///////////////////////////////////////
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
///
|
|
|
|
|
2023-11-23 09:39:26 +01:00
|
|
|
uint32_t Calculator::GetCostFromDuration(Configuration *cfg,
|
2023-11-22 08:55:00 +01:00
|
|
|
QDateTime const &start,
|
2023-11-23 09:39:26 +01:00
|
|
|
quint64 timeStepInMinutes) const {
|
2023-11-22 08:55:00 +01:00
|
|
|
// for instance, a tariff as used in Schoenau, Koenigssee: only steps, no
|
|
|
|
// special days, nonstop.
|
2023-11-23 09:39:26 +01:00
|
|
|
if (cfg->YearPeriod.size() == 0
|
2023-11-22 08:55:00 +01:00
|
|
|
&& cfg->SpecialDays.size() == 0
|
|
|
|
&& cfg->SpecialDaysWorktime.size() == 0) {
|
|
|
|
QDateTime const end = start.addSecs(timeStepInMinutes*60);
|
2023-11-23 09:39:26 +01:00
|
|
|
return GetCostFromDuration(cfg, start, end);
|
2023-11-22 08:55:00 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-23 09:39:26 +01:00
|
|
|
uint32_t Calculator::GetCostFromDuration(Configuration * cfg,
|
2023-11-22 08:55:00 +01:00
|
|
|
QDateTime const &start,
|
2023-11-23 09:39:26 +01:00
|
|
|
QDateTime const &end) const {
|
|
|
|
if (cfg->YearPeriod.size() == 0
|
2023-11-22 08:55:00 +01:00
|
|
|
&& cfg->SpecialDays.size() == 0
|
|
|
|
&& cfg->SpecialDaysWorktime.size() == 0) {
|
|
|
|
int const timeStepInMinutes = start.secsTo(end) / 60;
|
2023-11-23 09:39:26 +01:00
|
|
|
return GetPriceForTimeStep(cfg, timeStepInMinutes);
|
2023-11-22 08:55:00 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-04-24 15:31:46 +02:00
|
|
|
///////////////////////////////////////
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2023-05-16 15:31:53 +02:00
|
|
|
double Calculator::GetCostFromDuration(Configuration* cfg, uint8_t payment_option, const QDateTime start_datetime, QDateTime & end_datetime, double durationMin, bool nextDay, bool prepaid)
|
2023-04-24 15:31:46 +02:00
|
|
|
{
|
2023-11-23 09:39:26 +01:00
|
|
|
if (cfg->YearPeriod.size() == 0
|
|
|
|
&& cfg->SpecialDays.size() == 0
|
2023-11-22 08:55:00 +01:00
|
|
|
&& cfg->SpecialDaysWorktime.size() == 0)
|
|
|
|
{
|
2023-11-22 16:27:41 +01:00
|
|
|
end_datetime = start_datetime.addSecs(durationMin*60);
|
2023-11-23 09:39:26 +01:00
|
|
|
return GetCostFromDuration(cfg, start_datetime, end_datetime);
|
2023-11-22 08:55:00 +01:00
|
|
|
}
|
|
|
|
|
2023-05-12 09:20:46 +02:00
|
|
|
// Get input date
|
2023-05-16 15:31:53 +02:00
|
|
|
QDateTime inputDate = start_datetime;
|
2023-05-12 09:20:46 +02:00
|
|
|
|
|
|
|
// 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->PaymentOption.find(payment_option)->second.pop_min_time;
|
|
|
|
|
|
|
|
double maxMin = 0;
|
|
|
|
maxMin = cfg->PaymentOption.find(payment_option)->second.pop_max_time;
|
|
|
|
|
|
|
|
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 0.0f;
|
|
|
|
}
|
|
|
|
if (maxMin <= minMin)
|
|
|
|
{
|
|
|
|
LOG_ERROR("Error: max_min cannot be lower or equal than min_min");
|
|
|
|
return 0.0f;
|
|
|
|
}
|
2023-04-24 15:31:46 +02:00
|
|
|
|
2023-05-12 09:20:46 +02:00
|
|
|
// Check overtime
|
|
|
|
if (!overtime)
|
|
|
|
{
|
|
|
|
if (durationMin > maxMin)
|
|
|
|
{
|
|
|
|
LOG_WARNING("Total duration is greater or equal to max_min");
|
|
|
|
return maxMin;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (durationMin < minMin)
|
|
|
|
{
|
|
|
|
LOG_WARNING("Total duration is lower or equal to min_min");
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
}
|
2023-04-24 15:31:46 +02:00
|
|
|
|
2023-05-12 09:20:46 +02:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
total_duration_min = durationMin;
|
|
|
|
LOG_DEBUG("Total min:", total_duration_min);
|
|
|
|
|
|
|
|
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
|
2023-05-17 10:08:02 +02:00
|
|
|
|
|
|
|
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);
|
2023-05-12 09:20:46 +02:00
|
|
|
|
|
|
|
// 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);
|
2023-05-16 15:31:53 +02:00
|
|
|
return floor(GetCostFromDuration(cfg, payment_option, inputDate, end_datetime, durationMin, true, prepaid));
|
2023-05-12 09:20:46 +02:00
|
|
|
}
|
|
|
|
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());
|
|
|
|
}
|
2023-04-24 15:31:46 +02:00
|
|
|
|
2023-05-12 09:20:46 +02:00
|
|
|
if (price_per_unit < 0) price_per_unit = 1.0f;
|
|
|
|
LOG_DEBUG("Calculated price per minute: ", price_per_unit);
|
2023-04-24 15:31:46 +02:00
|
|
|
|
2023-05-12 09:20:46 +02:00
|
|
|
if (price_per_unit == 0)
|
|
|
|
{
|
|
|
|
inputDate = inputDate.addDays(1);
|
|
|
|
inputDate.setTime(worktime_from);
|
2023-05-16 15:31:53 +02:00
|
|
|
return GetCostFromDuration(cfg, payment_option, inputDate, end_datetime, durationMin, true, prepaid);
|
2023-05-12 09:20:46 +02:00
|
|
|
}
|
2023-04-24 15:31:46 +02:00
|
|
|
|
2023-05-12 09:20:46 +02:00
|
|
|
// If overtime flag is set
|
|
|
|
if (overtime || nextDay)
|
|
|
|
{
|
|
|
|
inputDate.setTime(worktime_from);
|
|
|
|
overtime = false;
|
|
|
|
}
|
2023-04-24 15:31:46 +02:00
|
|
|
|
2023-05-12 09:20:46 +02:00
|
|
|
// Check prepaid
|
|
|
|
if (!prepaid)
|
|
|
|
{
|
|
|
|
if ((inputDate.time() < worktime_from) || (inputDate.time() > worktime_to))
|
|
|
|
{
|
|
|
|
LOG_DEBUG("[STOP] * Ticket is not valid * ");
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
2023-05-16 15:31:53 +02:00
|
|
|
return GetCostFromDuration(cfg, payment_option, inputDate, end_datetime, durationMin, true, prepaid);
|
2023-05-12 09:20:46 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-24 15:31:46 +02:00
|
|
|
|
2023-05-12 09:20:46 +02:00
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
if(total_duration_min <= 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 0.0f;
|
|
|
|
}
|
|
|
|
|
2023-05-17 10:08:02 +02:00
|
|
|
int carry_over_status = 0;
|
|
|
|
carry_over_status = cfg->PaymentOption.find(payment_option)->second.pop_carry_over;
|
|
|
|
|
2023-05-12 09:20:46 +02:00
|
|
|
// Go to next day if minutes not spent
|
|
|
|
if(inputDate.time() >= worktime_to)
|
|
|
|
{
|
|
|
|
if (carry_over_status < 1) break;
|
|
|
|
|
|
|
|
LOG_DEBUG("Reached end of worktime, searching for the next working day");
|
|
|
|
inputDate = inputDate.addDays(1);
|
|
|
|
overtime = true;
|
2023-05-16 15:31:53 +02:00
|
|
|
return GetCostFromDuration(cfg, payment_option, inputDate, end_datetime, total_duration_min);
|
2023-05-12 09:20:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Increment input date minutes for each monetary unit
|
|
|
|
inputDate = inputDate.addSecs(60);
|
|
|
|
total_duration_min -=1;
|
|
|
|
total_cost += price_per_unit;
|
2023-04-24 15:31:46 +02:00
|
|
|
|
2023-05-12 09:20:46 +02:00
|
|
|
}
|
2023-05-16 15:31:53 +02:00
|
|
|
qDebug() << "GetCostFromDuration(): Valid until:" << inputDate.toString(Qt::ISODate).toStdString().c_str();
|
|
|
|
|
|
|
|
end_datetime = inputDate;
|
|
|
|
|
2023-05-12 09:20:46 +02:00
|
|
|
double ret_val = total_cost;
|
|
|
|
total_cost = 0.0f;
|
2023-05-09 11:52:17 +02:00
|
|
|
return ceil(ret_val);
|
2023-04-24 15:31:46 +02:00
|
|
|
}
|
2023-11-22 08:55:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-23 09:41:40 +01:00
|
|
|
QList<int> Calculator::GetTimeSteps(Configuration *cfg) const {
|
2023-11-22 08:55:00 +01:00
|
|
|
QList<int> timeSteps;
|
|
|
|
|
2023-11-23 09:41:40 +01:00
|
|
|
int const pop_id = cfg->getPaymentOptions().pop_id;
|
2023-11-22 08:55:00 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-11-23 09:41:40 +01:00
|
|
|
uint32_t Calculator::GetPriceForTimeStep(Configuration *cfg, int timeStep) const {
|
2023-11-22 08:55:00 +01:00
|
|
|
|
2023-11-23 09:41:40 +01:00
|
|
|
int const pop_id = cfg->getPaymentOptions().pop_id;
|
2023-11-22 08:55:00 +01:00
|
|
|
|
|
|
|
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) {
|
2023-11-23 09:41:02 +01:00
|
|
|
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;
|
|
|
|
|
2023-11-23 11:14:21 +01:00
|
|
|
|
2023-11-23 09:41:02 +01:00
|
|
|
for (auto[itr, rangeEnd] = cfg->PaymentRate.equal_range(pop_id); itr != rangeEnd; ++itr)
|
|
|
|
{
|
|
|
|
int const payment_unit_id = itr->second.pra_payment_unit_id;
|
2023-11-23 11:14:21 +01:00
|
|
|
int const pra_price = itr->second.pra_price;
|
2023-11-23 09:41:02 +01:00
|
|
|
|
|
|
|
if (price == pra_price) {
|
|
|
|
int const durationId = itr->second.pra_payment_unit_id;
|
|
|
|
int const durationUnit = cfg->Duration.find(durationId)->second.pun_duration;
|
2023-11-23 11:14:21 +01:00
|
|
|
|
2023-11-23 09:41:02 +01:00
|
|
|
return durationUnit;
|
2023-11-22 08:55:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|