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)) {
|
|
|
|
return zone.readLine().toInt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-05-02 09:46:17 +02:00
|
|
|
bool CALCULATE_LIBRARY_API init_tariff(parking_tariff_t **tariff, char const *config_file) {
|
2023-04-24 15:31:46 +02:00
|
|
|
*tariff = new Configuration();
|
|
|
|
|
|
|
|
int const zone = get_zone_nr();
|
|
|
|
if (zone == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString confFile(config_file);
|
|
|
|
if (!confFile.endsWith(QChar('/'))) {
|
|
|
|
confFile += "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[16];
|
|
|
|
memset(buffer, 0x00, sizeof(buffer));
|
|
|
|
snprintf(buffer, sizeof(buffer)-1, "tariff%02d.json", zone);
|
|
|
|
confFile += buffer;
|
|
|
|
|
|
|
|
QFile fname(confFile);
|
|
|
|
if (fname.exists()) {
|
|
|
|
if (fname.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
QString json = fname.readAll();
|
|
|
|
return (*tariff)->ParseJson(*tariff, json.toStdString().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-02 09:46:17 +02:00
|
|
|
void CALCULATE_LIBRARY_API free_tariff(parking_tariff_t *tariff) {
|
2023-04-24 15:31:46 +02:00
|
|
|
delete tariff;
|
|
|
|
}
|
|
|
|
|
2023-05-02 09:46:17 +02:00
|
|
|
bool CALCULATE_LIBRARY_API compute_price_for_parking_ticket(
|
|
|
|
parking_tariff_t *tariff,
|
2023-04-24 15:31:46 +02:00
|
|
|
time_t start_parking_time, // in minutes
|
|
|
|
time_t end_parking_time, // in minutes
|
|
|
|
struct price_t *price) {
|
|
|
|
int const duration = end_parking_time - start_parking_time;
|
|
|
|
if (duration > 0) {
|
|
|
|
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);
|
|
|
|
price->units = cost;
|
|
|
|
price->netto = cost;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
if (duration == 0) {
|
|
|
|
memset(price, 0x00, sizeof(*price));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
qCritical() << "ERROR start_parking_time (" << start_parking_time << ") <"
|
|
|
|
<< "end_parking_time (" << end_parking_time << ")";
|
|
|
|
return false;
|
|
|
|
}
|