Merge branch 'moransBranch' of git.mimbach49.de:GerhardHoffmann/MOBILISIS-Calculator into moransBranch
This commit is contained in:
commit
29986e0451
@ -132,6 +132,11 @@ CalcState CALCULATE_LIBRARY_API compute_duration_for_parking_ticket(
|
|||||||
QDateTime const &start_parking_time,
|
QDateTime const &start_parking_time,
|
||||||
double cost,
|
double cost,
|
||||||
QDateTime &ticketEndTime);
|
QDateTime &ticketEndTime);
|
||||||
|
|
||||||
|
CalcState CALCULATE_LIBRARY_API compute_duration_for_daily_ticket(
|
||||||
|
parking_tariff_t *tariff,
|
||||||
|
QString const &start_parking_time,
|
||||||
|
uint8_t paymentMethod);
|
||||||
//#ifdef __cplusplus
|
//#ifdef __cplusplus
|
||||||
//} // extern "C"
|
//} // extern "C"
|
||||||
//#endif
|
//#endif
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "configuration.h"
|
#include "configuration.h"
|
||||||
|
#include <QDateTime>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Calculator
|
class Calculator
|
||||||
@ -26,4 +26,7 @@ public:
|
|||||||
/// <param name="durationMin">Duration of parking in minutes</param>
|
/// <param name="durationMin">Duration of parking in minutes</param>
|
||||||
/// <returns>Returns cost (data type: double)</returns>
|
/// <returns>Returns cost (data type: double)</returns>
|
||||||
double GetCostFromDuration(Configuration* cfg, uint8_t vehicle_type, char const* start_datetime, double durationMin, bool nextDay = false, bool prepaid = false);
|
double GetCostFromDuration(Configuration* cfg, uint8_t vehicle_type, char const* start_datetime, double durationMin, bool nextDay = false, bool prepaid = false);
|
||||||
|
|
||||||
|
// Daily ticket
|
||||||
|
QString GetDailyTicketDuration(Configuration* cfg, QString start_datetime, uint8_t payment_option, bool carry_over);
|
||||||
};
|
};
|
@ -14,4 +14,5 @@ public:
|
|||||||
double pop_max_time;
|
double pop_max_time;
|
||||||
double pop_min_price;
|
double pop_min_price;
|
||||||
int pop_carry_over;
|
int pop_carry_over;
|
||||||
|
int pop_daily_card_price;
|
||||||
};
|
};
|
@ -69,4 +69,5 @@ public:
|
|||||||
/// <param name="pra_price"></param>
|
/// <param name="pra_price"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
static double CalculatePricePerUnit(double pra_price);
|
static double CalculatePricePerUnit(double pra_price);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
TEMPLATE = lib
|
TEMPLATE = lib
|
||||||
TARGET = mobilisis_calc
|
TARGET = mobilisis_calc
|
||||||
# CONFIG += staticlib
|
#CONFIG += staticlib
|
||||||
|
|
||||||
QMAKE_CXXFLAGS += -std=c++17 -g -O0
|
QMAKE_CXXFLAGS += -std=c++17 -g -O0
|
||||||
|
|
||||||
|
@ -274,3 +274,13 @@ CalcState CALCULATE_LIBRARY_API compute_duration_for_parking_ticket(
|
|||||||
|
|
||||||
return calcState.set(CalcState::State::SUCCESS);
|
return calcState.set(CalcState::State::SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CalcState CALCULATE_LIBRARY_API compute_duration_for_daily_ticket(parking_tariff_t *tariff, QString const &start_parking_time,uint8_t paymentMethod)
|
||||||
|
{
|
||||||
|
CalcState calcState;
|
||||||
|
QString result = calculator.GetDailyTicketDuration(tariff, start_parking_time, PaymentOption::Option1,false);
|
||||||
|
qDebug() << "DailyTicket() => " + result;
|
||||||
|
|
||||||
|
return calcState.set(CalcState::State::SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,92 @@ inline struct tm* localtime_r(const time_t *clock, struct tm* result){
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
QString Calculator::GetDailyTicketDuration(Configuration* cfg, QString start_datetime, uint8_t payment_option, bool carry_over)
|
||||||
|
{
|
||||||
|
if(start_datetime.isNull() || start_datetime.isEmpty()) return NULL;
|
||||||
|
|
||||||
|
double day_price = 0.0f;
|
||||||
|
int current_special_day_id = -1;
|
||||||
|
bool is_special_day = Utilities::CheckSpecialDay(cfg, start_datetime.toStdString().c_str(), ¤t_special_day_id, &day_price);
|
||||||
|
|
||||||
|
QDateTime inputDateTime = QDateTime::fromString(start_datetime, Qt::ISODate);
|
||||||
|
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) return "Daily ticket price zero or less";
|
||||||
|
|
||||||
|
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.toString(Qt::ISODate), 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.toString(Qt::ISODate), 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.toString(Qt::ISODate) + ", price = " + to_string(daily_card_price).c_str() + ", duration = " + to_string((diff/60)).c_str();
|
||||||
|
}
|
||||||
|
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.toString(Qt::ISODate), 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.toString(Qt::ISODate), 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.toString(Qt::ISODate) + ", price = " + to_string(daily_card_price).c_str() + ", duration = " + to_string((diff/60)).c_str();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
std::string Calculator::GetDurationFromCost(Configuration* cfg,
|
std::string Calculator::GetDurationFromCost(Configuration* cfg,
|
||||||
uint8_t payment_option,
|
uint8_t payment_option,
|
||||||
@ -43,6 +129,7 @@ std::string Calculator::GetDurationFromCost(Configuration* cfg,
|
|||||||
|
|
||||||
double min_price = 0;
|
double min_price = 0;
|
||||||
min_price = cfg->PaymentOption.find(payment_option)->second.pop_min_price;
|
min_price = cfg->PaymentOption.find(payment_option)->second.pop_min_price;
|
||||||
|
|
||||||
if(price < min_price)
|
if(price < min_price)
|
||||||
{
|
{
|
||||||
return "PARKING NOT ALLOWED";
|
return "PARKING NOT ALLOWED";
|
||||||
@ -109,6 +196,8 @@ std::string Calculator::GetDurationFromCost(Configuration* cfg,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (price_per_unit < 0) price_per_unit = 1.0f;
|
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);
|
LOG_DEBUG("Calculated price per minute: ", price_per_unit);
|
||||||
|
|
||||||
if (price_per_unit < 0)
|
if (price_per_unit < 0)
|
||||||
@ -151,7 +240,7 @@ std::string Calculator::GetDurationFromCost(Configuration* cfg,
|
|||||||
|
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
if(money_left <= 0) break;
|
if((int)money_left <= 0) break;
|
||||||
|
|
||||||
// Check year period
|
// Check year period
|
||||||
bool isYearPeriodActive = false;
|
bool isYearPeriodActive = false;
|
||||||
|
@ -154,6 +154,7 @@ bool Configuration::ParseJson(Configuration* cfg, const char* json)
|
|||||||
else if (strcmp(inner_obj_name, "pop_max_time") == 0) PaymentOption.pop_max_time = k->value.GetDouble();
|
else if (strcmp(inner_obj_name, "pop_max_time") == 0) PaymentOption.pop_max_time = k->value.GetDouble();
|
||||||
else if (strcmp(inner_obj_name, "pop_min_price") == 0) PaymentOption.pop_min_price = k->value.GetDouble();
|
else if (strcmp(inner_obj_name, "pop_min_price") == 0) PaymentOption.pop_min_price = k->value.GetDouble();
|
||||||
else if (strcmp(inner_obj_name, "pop_carry_over") == 0) PaymentOption.pop_carry_over = k->value.GetInt();
|
else if (strcmp(inner_obj_name, "pop_carry_over") == 0) PaymentOption.pop_carry_over = k->value.GetInt();
|
||||||
|
else if (strcmp(inner_obj_name, "pop_daily_card_price") == 0) PaymentOption.pop_daily_card_price = k->value.GetInt();
|
||||||
break;
|
break;
|
||||||
case MemberType::DurationType:
|
case MemberType::DurationType:
|
||||||
if (strcmp(inner_obj_name, "pun_id") == 0) Duration.pun_id = k->value.GetInt();
|
if (strcmp(inner_obj_name, "pun_id") == 0) Duration.pun_id = k->value.GetInt();
|
||||||
|
142
main/main.cpp
142
main/main.cpp
@ -1,10 +1,9 @@
|
|||||||
#include <calculate_price.h>
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <calculate_price.h>
|
||||||
|
|
||||||
|
|
||||||
extern "C" char* strptime(const char* s,
|
extern "C" char* strptime(const char* s,
|
||||||
const char* f,
|
const char* f,
|
||||||
@ -27,94 +26,99 @@ extern "C" char* strptime(const char* s,
|
|||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
parking_tariff_t *tariff = 0;
|
parking_tariff_t *tariff = 0;
|
||||||
if (init_tariff(&tariff, "C:\\Users\\MROD\\Documents\\QtCreator\\build-MOBILISIS-Calculator-Desktop_Qt_5_12_12_MSVC2017_32bit-Debug\\main\\etc\\psa_tariff\\zone1.json")) {
|
if (init_tariff(&tariff, "C:\\Users\\MROD\\Documents\\QtCreator\\build-MOBILISIS-Calculator-Desktop_Qt_5_12_12_MSVC2017_32bit-Debug\\main\\etc\\psa_tariff\\zone1.json"))
|
||||||
|
{
|
||||||
struct price_t price;
|
struct price_t price;
|
||||||
memset(&price, 0x00, sizeof(price));
|
memset(&price, 0x00, sizeof(price));
|
||||||
QDateTime start = QDateTime::fromString("2023-05-11T07:50:00",Qt::ISODate); //QDateTime::currentDateTime();
|
QDateTime start = QDateTime::fromString("2023-05-11T07:50:00",Qt::ISODate); //QDateTime::currentDateTime();
|
||||||
time_t start_parking_time = start.toSecsSinceEpoch() / 60;
|
time_t start_parking_time = start.toSecsSinceEpoch() / 60;
|
||||||
time_t end_parking_time = start_parking_time + 610;
|
time_t end_parking_time = start_parking_time + 1230;
|
||||||
|
|
||||||
if (compute_price_for_parking_ticket(tariff,
|
if (compute_price_for_parking_ticket(tariff,
|
||||||
start_parking_time,
|
start_parking_time,
|
||||||
end_parking_time,
|
end_parking_time,
|
||||||
&price)) {
|
&price))
|
||||||
|
{
|
||||||
qDebug() << "GetCostFromDuration() => price=" << price.netto;
|
qDebug() << "GetCostFromDuration() => price=" << price.netto;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString duration;
|
QString duration;
|
||||||
if(compute_duration_for_parking_ticket(tariff,start_parking_time,1525,duration))
|
if(compute_duration_for_parking_ticket(tariff,start_parking_time,3090,duration))
|
||||||
{
|
{
|
||||||
qDebug() << "GetDurationFromCost() => duration=" << duration;
|
qDebug() << "GetDurationFromCost() => duration=" << duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
// // tests
|
// Daily ticket
|
||||||
// struct tm now;
|
compute_duration_for_daily_ticket(tariff,start.toString(Qt::ISODate),3);
|
||||||
// memset(&now, 0, sizeof(now));
|
|
||||||
|
|
||||||
// // 3.Jan 2023 -> Tuesday
|
//Configuration* cfg, QString start_datetime, uint8_t payment_option, bool carry_over
|
||||||
// strptime("2023-01-03T14:00:00", "%Y-%m-%dT%H:%M:%S", &now);
|
// // tests
|
||||||
// for (int i = 0; i < 600; ++i) {
|
// struct tm now;
|
||||||
// start_parking_time = mktime(&now);
|
// memset(&now, 0, sizeof(now));
|
||||||
// end_parking_time = start_parking_time + 240; // duration == 240
|
|
||||||
|
|
||||||
// if (compute_price_for_parking_ticket(tariff,
|
// // 3.Jan 2023 -> Tuesday
|
||||||
// start_parking_time,
|
// strptime("2023-01-03T14:00:00", "%Y-%m-%dT%H:%M:%S", &now);
|
||||||
// end_parking_time,
|
// for (int i = 0; i < 600; ++i) {
|
||||||
// &price)) {
|
// start_parking_time = mktime(&now);
|
||||||
// int const zone = get_zone_nr(1);
|
// end_parking_time = start_parking_time + 240; // duration == 240
|
||||||
// switch (zone) {
|
|
||||||
// case 1:
|
|
||||||
// assert(price.netto == 879); // expected value: 880
|
|
||||||
// break;
|
|
||||||
// case 2:
|
|
||||||
// /* fall through */
|
|
||||||
// case 3:
|
|
||||||
// assert(price.netto == 1920);
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// time_t t = start_parking_time + 60;
|
|
||||||
// now = *localtime(&t);
|
|
||||||
// }
|
|
||||||
// //
|
|
||||||
// // test May 1st 2023
|
|
||||||
// //
|
|
||||||
// memset(&now, 0, sizeof(now));
|
|
||||||
// strptime("2023-04-30T06:00:00", "%Y-%m-%dT%H:%M:%S", &now);
|
|
||||||
// now.tm_hour -= 1; // for ctime
|
|
||||||
// // for (int i=0; i<6*24; ++i) {
|
|
||||||
// for (int i=0; i<1; ++i) {
|
|
||||||
// int const duration = 120;
|
|
||||||
// time_t t = mktime(&now);
|
|
||||||
// start_parking_time = t / 60;
|
|
||||||
// end_parking_time = start_parking_time + duration;
|
|
||||||
|
|
||||||
// if (compute_price_for_parking_ticket(tariff,
|
// if (compute_price_for_parking_ticket(tariff,
|
||||||
// start_parking_time,
|
// start_parking_time,
|
||||||
// end_parking_time,
|
// end_parking_time,
|
||||||
// &price)) {
|
// &price)) {
|
||||||
// int const zone = get_zone_nr();
|
// int const zone = get_zone_nr(1);
|
||||||
// switch (zone) {
|
// switch (zone) {
|
||||||
// case 1:
|
// case 1:
|
||||||
// qDebug() << i << zone << ctime(&t) << price.netto << " FT";
|
// assert(price.netto == 879); // expected value: 880
|
||||||
// assert(price.netto == 440);
|
// break;
|
||||||
// break;
|
// case 2:
|
||||||
// case 2:
|
// /* fall through */
|
||||||
// /* fall through */
|
// case 3:
|
||||||
// case 3:
|
// assert(price.netto == 1920);
|
||||||
// qDebug() << i << zone << ctime(&t) << price.netto << " FT";
|
// break;
|
||||||
// assert(price.netto == 960);
|
// }
|
||||||
// break;
|
// }
|
||||||
// }
|
// time_t t = start_parking_time + 60;
|
||||||
// }
|
// now = *localtime(&t);
|
||||||
|
// }
|
||||||
|
// //
|
||||||
|
// // test May 1st 2023
|
||||||
|
// //
|
||||||
|
// memset(&now, 0, sizeof(now));
|
||||||
|
// strptime("2023-04-30T06:00:00", "%Y-%m-%dT%H:%M:%S", &now);
|
||||||
|
// now.tm_hour -= 1; // for ctime
|
||||||
|
// // for (int i=0; i<6*24; ++i) {
|
||||||
|
// for (int i=0; i<1; ++i) {
|
||||||
|
// int const duration = 120;
|
||||||
|
// time_t t = mktime(&now);
|
||||||
|
// start_parking_time = t / 60;
|
||||||
|
// end_parking_time = start_parking_time + duration;
|
||||||
|
|
||||||
// t = (start_parking_time + 60)*60;
|
// if (compute_price_for_parking_ticket(tariff,
|
||||||
// now = *localtime(&t);
|
// start_parking_time,
|
||||||
// }
|
// end_parking_time,
|
||||||
|
// &price)) {
|
||||||
|
// int const zone = get_zone_nr();
|
||||||
|
// switch (zone) {
|
||||||
|
// case 1:
|
||||||
|
// qDebug() << i << zone << ctime(&t) << price.netto << " FT";
|
||||||
|
// assert(price.netto == 440);
|
||||||
|
// break;
|
||||||
|
// case 2:
|
||||||
|
// /* fall through */
|
||||||
|
// case 3:
|
||||||
|
// qDebug() << i << zone << ctime(&t) << price.netto << " FT";
|
||||||
|
// assert(price.netto == 960);
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// t = (start_parking_time + 60)*60;
|
||||||
|
// now = *localtime(&t);
|
||||||
|
// }
|
||||||
|
|
||||||
free_tariff(tariff);
|
free_tariff(tariff);
|
||||||
}
|
}
|
||||||
@ -151,7 +155,7 @@ int main() {
|
|||||||
struct tm now; // = Utilities::DateTimeToStructTm("2023-03-01T16:00:00");
|
struct tm now; // = Utilities::DateTimeToStructTm("2023-03-01T16:00:00");
|
||||||
memset(&now, 0, sizeof(now));
|
memset(&now, 0, sizeof(now));
|
||||||
char buffer[64];
|
char buffer[64];
|
||||||
//#if 0
|
//#if 0
|
||||||
// 3.Jan 2023 -> Tuesday
|
// 3.Jan 2023 -> Tuesday
|
||||||
strptime("2023-01-03T14:00:00", "%Y-%m-%dT%H:%M:%S", &now);
|
strptime("2023-01-03T14:00:00", "%Y-%m-%dT%H:%M:%S", &now);
|
||||||
for (int i = 0; i < 600; ++i) {
|
for (int i = 0; i < 600; ++i) {
|
||||||
|
Loading…
Reference in New Issue
Block a user