MOBILISIS-Calculator/library/src/calculate_price.cpp

97 lines
2.8 KiB
C++
Raw Normal View History

#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() {
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();
}
}
}
return -1;
}
bool CALCULATE_LIBRARY_API init_tariff(parking_tariff_t **tariff, char const *config_file) {
*tariff = new Configuration();
int const zone = get_zone_nr();
2023-05-05 13:19:02 +02:00
// DEBUG
qCritical() << "init_tariff:";
qCritical() << " ... zone = " << zone;
if (zone <= 0) {
return false;
}
QString confFile(config_file);
if (!confFile.endsWith(QChar('/'))) {
confFile += "/";
}
2023-05-05 13:19:02 +02:00
char buffer[32];
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;
}
void CALCULATE_LIBRARY_API free_tariff(parking_tariff_t *tariff) {
delete tariff;
}
bool 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) {
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;
}