start repository for mobilisis tariff-calculation
This commit is contained in:
87
library/src/calculate_price.cpp
Normal file
87
library/src/calculate_price.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#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 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)) {
|
||||
return zone.readLine().toInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool init_tariff(parking_tariff_t **tariff, char const *config_file) {
|
||||
*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;
|
||||
}
|
||||
|
||||
void free_tariff(parking_tariff_t *tariff) {
|
||||
delete tariff;
|
||||
}
|
||||
|
||||
bool 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;
|
||||
}
|
Reference in New Issue
Block a user