277 lines
10 KiB
C++
277 lines
10 KiB
C++
#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;
|
|
|
|
int CALCULATE_LIBRARY_API get_zone_nr(int zone)
|
|
{
|
|
if(zone > -1) return zone;
|
|
else
|
|
{
|
|
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)) {
|
|
QTextStream in(&zone);
|
|
return in.readLine(100).toInt();
|
|
}
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
CalcState CALCULATE_LIBRARY_API init_tariff(parking_tariff_t **tariff, char const *config_file) {
|
|
*tariff = new Configuration();
|
|
|
|
CalcState calcState;
|
|
#if __linux__
|
|
|
|
int const zone = get_zone_nr();
|
|
|
|
// DEBUG
|
|
qCritical() << "init_tariff:";
|
|
qCritical() << " ... zone = " << zone;
|
|
|
|
if (zone <= 0) {
|
|
delete *tariff;
|
|
*tariff = nullptr;
|
|
return calcState.set(CalcState::State::ERROR_PARSING_ZONE_NR);
|
|
}
|
|
|
|
QString confFile(config_file);
|
|
if (!confFile.endsWith(QChar('/'))) {
|
|
confFile += "/";
|
|
}
|
|
|
|
char buffer[32];
|
|
memset(buffer, 0x00, sizeof(buffer));
|
|
snprintf(buffer, sizeof(buffer)-1, "tariff%02d.json", zone);
|
|
confFile += buffer;
|
|
#else // windows
|
|
QString confFile(config_file);
|
|
#endif
|
|
|
|
// DEBUG
|
|
qCritical() << " ... confFile = " << confFile;
|
|
|
|
QFile fname(confFile);
|
|
if (fname.exists() &&
|
|
fname.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
// DEBUG
|
|
qCritical() << " ... confFile is open";
|
|
|
|
QString json = fname.readAll();
|
|
if (! (*tariff)->ParseJson(*tariff, json.toStdString().c_str())) {
|
|
delete *tariff;
|
|
*tariff = nullptr;
|
|
return calcState.set(CalcState::State::ERROR_PARSING_TARIFF);
|
|
}
|
|
} else {
|
|
delete *tariff;
|
|
*tariff = nullptr;
|
|
return calcState.set(CalcState::State::ERROR_LOADING_TARIFF);
|
|
}
|
|
|
|
qCritical() << "init_tariff: Parsing tariff config (" << confFile << ")";
|
|
|
|
return calcState;
|
|
}
|
|
|
|
void CALCULATE_LIBRARY_API free_tariff(parking_tariff_t *tariff) {
|
|
if (tariff != nullptr) {
|
|
delete tariff;
|
|
}
|
|
}
|
|
|
|
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) {
|
|
calcState.setDesc(QString("minMin=%1, maxMin=%2").arg(minMin).arg(maxMin));
|
|
return calcState.set(CalcState::State::WRONG_PARAM_VALUES);
|
|
}
|
|
|
|
int const duration = end_parking_time - start_parking_time;
|
|
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) {
|
|
calcState.setDesc(QString("duration=%1, maxMin=%2").arg(duration).arg(maxMin));
|
|
return calcState.set(CalcState::State::ABOVE_MAX_PARKING_TIME);
|
|
}
|
|
if (duration < minMin) {
|
|
calcState.setDesc(QString("duration=%1, minMin=%2").arg(duration).arg(minMin));
|
|
return calcState.set(CalcState::State::BELOW_MIN_PARKING_TIME);
|
|
}
|
|
if (duration == 0) {
|
|
memset(price, 0x00, sizeof(*price));
|
|
return calcState.set(CalcState::State::SUCCESS);
|
|
}
|
|
|
|
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) {
|
|
calcState.setDesc(QString("minCost=%1, cost=%2").arg(minCost).arg(cost));
|
|
return calcState.set(CalcState::State::BELOW_MIN_PARKING_PRICE);
|
|
}
|
|
price->units = cost;
|
|
price->netto = cost;
|
|
} else {
|
|
return calcState.set(CalcState::State::INVALID_START_DATE);
|
|
}
|
|
|
|
return calcState.set(CalcState::State::SUCCESS);
|
|
}
|
|
|
|
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;
|
|
|
|
// 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;
|
|
|
|
|
|
int const duration = (end_parking_time.toSecsSinceEpoch() -
|
|
start_parking_time.toSecsSinceEpoch()) / 60;
|
|
|
|
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);
|
|
}
|
|
|
|
// DEBUG
|
|
qCritical() << " -> calculated cost (price->netto) = " << cost;
|
|
|
|
price->units = cost;
|
|
price->netto = cost;
|
|
} 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,
|
|
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);
|
|
|
|
// DEBUG
|
|
qCritical() << "compute_duration_for_parking_ticket(): ";
|
|
qCritical() << " start (cs): " << cs;
|
|
qCritical() << " price: " << price;
|
|
|
|
duration = calculator.GetDurationFromCost(tariff, PaymentOption::Option1,
|
|
cs.toLocal8Bit().constData(),
|
|
price, false, true).c_str();
|
|
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();
|
|
ticketEndTime = QDateTime::fromString(endTime,Qt::ISODate);
|
|
|
|
// DEBUG
|
|
qCritical() << "compute_duration_for_parking_ticket(): ";
|
|
qCritical() << " endTime: " << endTime;
|
|
qCritical() << " ticketEndTime: " << ticketEndTime;
|
|
|
|
if (!ticketEndTime.isValid()) {
|
|
calcState.setDesc(QString("ticketEndTime=%1").arg(endTime));
|
|
return calcState.set(CalcState::State::WRONG_ISO_TIME_FORMAT);
|
|
}
|
|
} else {
|
|
return calcState.set(CalcState::State::INVALID_START_DATE);
|
|
}
|
|
|
|
return calcState.set(CalcState::State::SUCCESS);
|
|
}
|