2023-04-24 15:31:46 +02:00
|
|
|
#include "calculate_price.h"
|
|
|
|
#include "configuration.h"
|
|
|
|
#include "calculator_functions.h"
|
|
|
|
#include "payment_option.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
static Calculator calculator;
|
|
|
|
|
2023-05-02 09:46:17 +02:00
|
|
|
int CALCULATE_LIBRARY_API get_zone_nr() {
|
2023-04-24 15:31:46 +02:00
|
|
|
QFile zone("/etc/zone_nr");
|
|
|
|
if (zone.exists()) {
|
|
|
|
QFileInfo finfo(zone);
|
|
|
|
if (finfo.size() <= 4) { // decimal 000\n
|
|
|
|
if (zone.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2023-05-05 13:19:02 +02:00
|
|
|
QTextStream in(&zone);
|
|
|
|
return in.readLine(100).toInt();
|
2023-04-24 15:31:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-05-08 12:34:01 +02:00
|
|
|
CalcState CALCULATE_LIBRARY_API init_tariff(parking_tariff_t **tariff, char const *config_file) {
|
2023-04-24 15:31:46 +02:00
|
|
|
*tariff = new Configuration();
|
|
|
|
|
2023-05-08 12:34:01 +02:00
|
|
|
CalcState calcState;
|
2023-04-24 15:31:46 +02:00
|
|
|
int const zone = get_zone_nr();
|
2023-05-05 13:19:02 +02:00
|
|
|
|
|
|
|
// DEBUG
|
|
|
|
qCritical() << "init_tariff:";
|
|
|
|
qCritical() << " ... zone = " << zone;
|
|
|
|
|
|
|
|
if (zone <= 0) {
|
2023-05-09 13:05:02 +02:00
|
|
|
delete *tariff;
|
|
|
|
*tariff = nullptr;
|
2023-05-08 12:34:01 +02:00
|
|
|
return calcState.set(CalcState::State::ERROR_PARSING_ZONE_NR);
|
2023-04-24 15:31:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString confFile(config_file);
|
|
|
|
if (!confFile.endsWith(QChar('/'))) {
|
|
|
|
confFile += "/";
|
|
|
|
}
|
|
|
|
|
2023-05-05 13:19:02 +02:00
|
|
|
char buffer[32];
|
2023-04-24 15:31:46 +02:00
|
|
|
memset(buffer, 0x00, sizeof(buffer));
|
|
|
|
snprintf(buffer, sizeof(buffer)-1, "tariff%02d.json", zone);
|
|
|
|
confFile += buffer;
|
|
|
|
|
2023-05-05 13:19:36 +02:00
|
|
|
// DEBUG
|
|
|
|
qCritical() << " ... confFile = " << confFile;
|
|
|
|
|
2023-04-24 15:31:46 +02:00
|
|
|
QFile fname(confFile);
|
2023-05-08 12:34:01 +02:00
|
|
|
if (fname.exists() &&
|
|
|
|
fname.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2023-05-05 13:19:36 +02:00
|
|
|
// DEBUG
|
2023-05-08 12:34:01 +02:00
|
|
|
qCritical() << " ... confFile is open";
|
2023-05-05 13:19:36 +02:00
|
|
|
|
2023-05-08 12:34:01 +02:00
|
|
|
QString json = fname.readAll();
|
|
|
|
if (! (*tariff)->ParseJson(*tariff, json.toStdString().c_str())) {
|
2023-05-09 13:05:02 +02:00
|
|
|
delete *tariff;
|
|
|
|
*tariff = nullptr;
|
2023-05-09 11:52:17 +02:00
|
|
|
return calcState.set(CalcState::State::ERROR_PARSING_TARIFF);
|
2023-04-24 15:31:46 +02:00
|
|
|
}
|
2023-05-08 12:34:01 +02:00
|
|
|
} else {
|
2023-05-09 13:05:02 +02:00
|
|
|
delete *tariff;
|
|
|
|
*tariff = nullptr;
|
2023-05-09 11:52:17 +02:00
|
|
|
return calcState.set(CalcState::State::ERROR_LOADING_TARIFF);
|
2023-04-24 15:31:46 +02:00
|
|
|
}
|
|
|
|
|
2023-05-09 11:52:17 +02:00
|
|
|
qCritical() << "init_tariff: Parsing tariff config (" << confFile << ")";
|
2023-05-05 13:19:36 +02:00
|
|
|
|
2023-05-08 12:34:01 +02:00
|
|
|
return calcState;
|
2023-04-24 15:31:46 +02:00
|
|
|
}
|
|
|
|
|
2023-05-02 09:46:17 +02:00
|
|
|
void CALCULATE_LIBRARY_API free_tariff(parking_tariff_t *tariff) {
|
2023-05-09 13:05:02 +02:00
|
|
|
if (tariff != nullptr) {
|
|
|
|
delete tariff;
|
|
|
|
}
|
2023-04-24 15:31:46 +02:00
|
|
|
}
|
|
|
|
|
2023-05-08 12:34:01 +02:00
|
|
|
CalcState CALCULATE_LIBRARY_API compute_price_for_parking_ticket(
|
|
|
|
parking_tariff_t *tariff,
|
|
|
|
time_t start_parking_time, // in minutes
|
|
|
|
time_t end_parking_time, // in minutes
|
|
|
|
struct price_t *price) {
|
|
|
|
CalcState calcState;
|
|
|
|
double minMin = tariff->PaymentOption.find(PaymentOption::Option1)->second.pop_min_time;
|
|
|
|
double maxMin = tariff->PaymentOption.find(PaymentOption::Option1)->second.pop_max_time;
|
|
|
|
|
|
|
|
if (minMin < 0 || maxMin < 0 || maxMin < minMin) {
|
2023-05-10 13:33:28 +02:00
|
|
|
calcState.setDesc(QString("minMin=%1, maxMin=%2").arg(minMin).arg(maxMin));
|
2023-05-08 12:34:01 +02:00
|
|
|
return calcState.set(CalcState::State::WRONG_PARAM_VALUES);
|
|
|
|
}
|
|
|
|
|
2023-04-24 15:31:46 +02:00
|
|
|
int const duration = end_parking_time - start_parking_time;
|
2023-05-08 12:34:01 +02:00
|
|
|
if (duration < 0) {
|
|
|
|
calcState.setDesc(QString("end=%1, start=%2")
|
|
|
|
.arg(end_parking_time, start_parking_time));
|
|
|
|
return calcState.set(CalcState::State::NEGATIVE_PARING_TIME);
|
|
|
|
}
|
|
|
|
if (duration > maxMin) {
|
2023-05-10 13:33:28 +02:00
|
|
|
calcState.setDesc(QString("duration=%1, maxMin=%2").arg(duration).arg(maxMin));
|
2023-05-08 12:34:01 +02:00
|
|
|
return calcState.set(CalcState::State::ABOVE_MAX_PARKING_TIME);
|
|
|
|
}
|
|
|
|
if (duration < minMin) {
|
2023-05-10 13:33:28 +02:00
|
|
|
calcState.setDesc(QString("duration=%1, minMin=%2").arg(duration).arg(minMin));
|
2023-05-08 12:34:01 +02:00
|
|
|
return calcState.set(CalcState::State::BELOW_MIN_PARKING_TIME);
|
|
|
|
}
|
2023-04-24 15:31:46 +02:00
|
|
|
if (duration == 0) {
|
|
|
|
memset(price, 0x00, sizeof(*price));
|
2023-05-08 12:34:01 +02:00
|
|
|
return calcState.set(CalcState::State::SUCCESS);
|
2023-04-24 15:31:46 +02:00
|
|
|
}
|
2023-05-08 12:34:01 +02:00
|
|
|
|
|
|
|
QDate const d(1970, 1, 1);
|
|
|
|
QTime const t(0, 0, 0);
|
|
|
|
QDateTime start(d, t, Qt::UTC);
|
|
|
|
start = start.toLocalTime().addSecs(start_parking_time * 60);
|
|
|
|
if (start.isValid()) {
|
|
|
|
QString cs = start.toString(Qt::ISODate);
|
|
|
|
double cost = calculator.GetCostFromDuration(
|
|
|
|
tariff, PaymentOption::Option1,
|
|
|
|
cs.toLocal8Bit().constData(),
|
|
|
|
duration, false, true);
|
|
|
|
double minCost = tariff->PaymentOption.find(PaymentOption::Option1)->second.pop_min_price;
|
|
|
|
if (cost < minCost) {
|
2023-05-10 13:33:28 +02:00
|
|
|
calcState.setDesc(QString("minCost=%1, cost=%2").arg(minCost).arg(cost));
|
2023-05-08 12:34:01 +02:00
|
|
|
return calcState.set(CalcState::State::BELOW_MIN_PARKING_PRICE);
|
|
|
|
}
|
|
|
|
price->units = cost;
|
|
|
|
price->netto = cost;
|
2023-05-09 11:40:14 +02:00
|
|
|
} else {
|
|
|
|
return calcState.set(CalcState::State::INVALID_START_DATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return calcState.set(CalcState::State::SUCCESS);
|
|
|
|
}
|
|
|
|
|
2023-05-10 16:16:24 +02:00
|
|
|
CalcState CALCULATE_LIBRARY_API compute_price_for_parking_ticket(
|
|
|
|
parking_tariff_t *tariff,
|
|
|
|
QDateTime const &start_parking_time,
|
|
|
|
QDateTime const &end_parking_time,
|
|
|
|
struct price_t *price) {
|
|
|
|
CalcState calcState;
|
|
|
|
double minMin = tariff->PaymentOption.find(PaymentOption::Option1)->second.pop_min_time;
|
|
|
|
double maxMin = tariff->PaymentOption.find(PaymentOption::Option1)->second.pop_max_time;
|
|
|
|
|
2023-05-11 09:58:15 +02:00
|
|
|
// DEBUG
|
|
|
|
qCritical() << "compute_price_for_parking_ticket() " << endl
|
|
|
|
<< " start_parking_time: " << start_parking_time << endl
|
|
|
|
<< " end_parking_time: " << end_parking_time << endl
|
|
|
|
<< " minMin: " << minMin << endl
|
|
|
|
<< " maxMin: " << maxMin;
|
|
|
|
|
|
|
|
|
2023-05-10 16:16:24 +02:00
|
|
|
int const duration = (end_parking_time.toSecsSinceEpoch() -
|
2023-05-11 09:58:54 +02:00
|
|
|
start_parking_time.toSecsSinceEpoch()) / 60;
|
2023-05-10 16:16:24 +02:00
|
|
|
|
|
|
|
if (duration < 0) {
|
|
|
|
calcState.setDesc(QString("end=%1, start=%2")
|
|
|
|
.arg(end_parking_time.toString(Qt::ISODate),
|
|
|
|
start_parking_time.toString(Qt::ISODate)));
|
|
|
|
return calcState.set(CalcState::State::NEGATIVE_PARING_TIME);
|
|
|
|
}
|
|
|
|
if (duration > maxMin) {
|
|
|
|
calcState.setDesc(QString("duration=%1, maxMin=%2").arg(duration, maxMin));
|
|
|
|
return calcState.set(CalcState::State::ABOVE_MAX_PARKING_TIME);
|
|
|
|
}
|
|
|
|
if (duration < minMin) {
|
|
|
|
calcState.setDesc(QString("duration=%1, minMin=%2").arg(duration, minMin));
|
|
|
|
return calcState.set(CalcState::State::BELOW_MIN_PARKING_TIME);
|
|
|
|
}
|
|
|
|
if (duration == 0) {
|
|
|
|
memset(price, 0x00, sizeof(*price));
|
|
|
|
return calcState.set(CalcState::State::SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start_parking_time.isValid()) {
|
|
|
|
QString cs = start_parking_time.toString(Qt::ISODate);
|
|
|
|
double cost = calculator.GetCostFromDuration(
|
|
|
|
tariff, PaymentOption::Option1,
|
|
|
|
cs.toLocal8Bit().constData(),
|
|
|
|
duration, false, true);
|
|
|
|
double minCost = tariff->PaymentOption.find(PaymentOption::Option1)->second.pop_min_price;
|
|
|
|
if (cost < minCost) {
|
|
|
|
calcState.setDesc(QString("minCost=%1, cost=%2").arg(minCost, cost));
|
|
|
|
return calcState.set(CalcState::State::BELOW_MIN_PARKING_PRICE);
|
|
|
|
}
|
2023-05-11 09:58:15 +02:00
|
|
|
|
|
|
|
// DEBUG
|
|
|
|
qCritical() << " -> calculated cost (price->netto) = " << cost;
|
|
|
|
|
2023-05-10 16:16:24 +02:00
|
|
|
price->units = cost;
|
|
|
|
price->netto = cost;
|
|
|
|
} else {
|
|
|
|
return calcState.set(CalcState::State::INVALID_START_DATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return calcState.set(CalcState::State::SUCCESS);
|
|
|
|
}
|
|
|
|
|
2023-05-09 11:40:14 +02:00
|
|
|
CalcState CALCULATE_LIBRARY_API compute_duration_for_parking_ticket(
|
|
|
|
parking_tariff_t *tariff,
|
|
|
|
time_t start_parking_time,
|
|
|
|
double price,
|
|
|
|
QString &duration) {
|
|
|
|
CalcState calcState;
|
|
|
|
QDate const d(1970, 1, 1);
|
|
|
|
QTime const t(0, 0, 0);
|
|
|
|
QDateTime start(d, t, Qt::UTC);
|
|
|
|
start = start.toLocalTime().addSecs(start_parking_time * 60);
|
|
|
|
if (start.isValid()) {
|
|
|
|
QString cs = start.toString(Qt::ISODate);
|
2023-05-11 09:58:15 +02:00
|
|
|
|
|
|
|
// DEBUG
|
|
|
|
qCritical() << "compute_duration_for_parking_ticket(): ";
|
|
|
|
qCritical() << " start (cs): " << cs;
|
|
|
|
qCritical() << " price: " << price;
|
|
|
|
|
2023-05-09 11:40:14 +02:00
|
|
|
duration = calculator.GetDurationFromCost(tariff, PaymentOption::Option1,
|
|
|
|
cs.toLocal8Bit().constData(),
|
|
|
|
price, false, true).c_str();
|
2023-05-10 16:16:24 +02:00
|
|
|
QDateTime d = QDateTime::fromString(duration);
|
|
|
|
if (!d.isValid()) {
|
|
|
|
calcState.setDesc(QString("ticketEndTime=%1").arg(duration));
|
|
|
|
return calcState.set(CalcState::State::WRONG_ISO_TIME_FORMAT);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return calcState.set(CalcState::State::INVALID_START_DATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return calcState.set(CalcState::State::SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
CalcState CALCULATE_LIBRARY_API compute_duration_for_parking_ticket(
|
|
|
|
parking_tariff_t *tariff,
|
|
|
|
QDateTime const &start_parking_time,
|
|
|
|
double price,
|
|
|
|
QDateTime &ticketEndTime) {
|
|
|
|
CalcState calcState;
|
|
|
|
if (start_parking_time.isValid()) {
|
|
|
|
QString cs = start_parking_time.toString(Qt::ISODate);
|
|
|
|
QString endTime = calculator.GetDurationFromCost(
|
|
|
|
tariff, PaymentOption::Option1,
|
|
|
|
cs.toLocal8Bit().constData(),
|
|
|
|
price, false, true).c_str();
|
2023-05-11 09:59:37 +02:00
|
|
|
ticketEndTime = QDateTime::fromString(endTime,Qt::ISODate);
|
2023-05-11 09:58:15 +02:00
|
|
|
|
|
|
|
// DEBUG
|
|
|
|
qCritical() << "compute_duration_for_parking_ticket(): ";
|
|
|
|
qCritical() << " endTime: " << endTime;
|
|
|
|
qCritical() << " ticketEndTime: " << ticketEndTime;
|
|
|
|
|
2023-05-10 16:16:24 +02:00
|
|
|
if (!ticketEndTime.isValid()) {
|
|
|
|
calcState.setDesc(QString("ticketEndTime=%1").arg(endTime));
|
|
|
|
return calcState.set(CalcState::State::WRONG_ISO_TIME_FORMAT);
|
|
|
|
}
|
2023-05-09 11:40:14 +02:00
|
|
|
} else {
|
|
|
|
return calcState.set(CalcState::State::INVALID_START_DATE);
|
2023-05-08 12:34:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return calcState.set(CalcState::State::SUCCESS);
|
2023-04-24 15:31:46 +02:00
|
|
|
}
|