MOBILISIS-Calculator/library/include/mobilisis/calculate_price.h

355 lines
13 KiB
C++

#ifndef CALCULATE_PRICE_H
#define CALCULATE_PRICE_H
#include <time.h>
#include <inttypes.h>
#include "tariff_time_range.h"
#include <QString>
#include <QDateTime>
#ifdef WIN32
#ifdef CALCULATE_LIBRARY_EXPORTS
#define CALCULATE_LIBRARY_API __declspec(dllexport)
#else
#define CALCULATE_LIBRARY_API __declspec(dllimport)
#endif
#else
#define CALCULATE_LIBRARY_API
#endif
#include "tariff_permit_type.h"
class Configuration;
typedef Configuration parking_tariff_t;
//#ifdef __cplusplus
//extern "C" {
//#endif
struct CALCULATE_LIBRARY_API price_t {
uint32_t units;
double netto;
double brutto;
double vat_percentage;
double vat;
explicit price_t() {
units = 0;
netto = brutto = vat_percentage = vat = 0.0;
}
};
struct CALCULATE_LIBRARY_API CalcState {
static QString const SUCCESS;
static QString const ERROR_PARSING_ZONE_NR;
static QString const ERROR_LOADING_TARIFF;
static QString const ERROR_PARSING_TARIFF;
static QString const NEGATIVE_PARKING_TIME;
static QString const INVALID_START_DATE;
static QString const WRONG_PARAM_VALUES;
static QString const WRONG_ISO_TIME_FORMAT;
static QString const ABOVE_MAX_PARKING_TIME;
static QString const BELOW_MIN_PARKING_TIME;
static QString const BELOW_MIN_PARKING_PRICE;
static QString const ABOVE_MAX_PARKING_PRICE;
static QString const OVERPAID;
static QString const OUTSIDE_ALLOWED_PARKING_TIME;
static QString const SUCCESS_MAXPRICE;
enum class State : uint8_t {
SUCCESS,
ERROR_PARSING_ZONE_NR,
ERROR_LOADING_TARIFF,
ERROR_PARSING_TARIFF,
NEGATIVE_PARKING_TIME,
INVALID_START_DATE,
WRONG_PARAM_VALUES,
WRONG_ISO_TIME_FORMAT,
ABOVE_MAX_PARKING_TIME,
BELOW_MIN_PARKING_TIME,
BELOW_MIN_PARKING_PRICE,
ABOVE_MAX_PARKING_PRICE,
OVERPAID,
OUTSIDE_ALLOWED_PARKING_TIME,
SUCCESS_MAXPRICE
};
State m_status;
QString m_desc;
TariffTimeRange m_allowedTimeRange;
explicit CalcState()
: m_status(State::SUCCESS)
, m_desc("") {
}
explicit CalcState(State state, QString desc = "")
: m_status(state)
, m_desc(desc) {
}
explicit CalcState(State state, QString desc,
QTime const &from,
QTime const &until)
: m_status(state)
, m_desc(desc)
, m_allowedTimeRange(from, until) {
}
explicit operator bool() const noexcept {
return (m_status == State::SUCCESS);
}
QString toString() {
QString s;
switch (m_status) {
case State::SUCCESS:
s = CalcState::SUCCESS;
break;
case State::SUCCESS_MAXPRICE:
s = CalcState::SUCCESS_MAXPRICE;
break;
case State::ERROR_PARSING_ZONE_NR:
s = CalcState::ERROR_PARSING_ZONE_NR;
break;
case State::ERROR_LOADING_TARIFF:
s = CalcState::ERROR_LOADING_TARIFF;
break;
case State::ERROR_PARSING_TARIFF:
s = CalcState::ERROR_PARSING_TARIFF;
break;
case State::NEGATIVE_PARKING_TIME:
s = CalcState::NEGATIVE_PARKING_TIME;
break;
case State::ABOVE_MAX_PARKING_TIME:
s = CalcState::ABOVE_MAX_PARKING_TIME;
break;
case State::WRONG_PARAM_VALUES:
s = CalcState::WRONG_PARAM_VALUES;
break;
case State::BELOW_MIN_PARKING_TIME:
s = CalcState::BELOW_MIN_PARKING_TIME;
break;
case State::BELOW_MIN_PARKING_PRICE:
s = CalcState::BELOW_MIN_PARKING_PRICE;
break;
case State::OVERPAID:
s = CalcState::OVERPAID;
break;
case State::INVALID_START_DATE:
s = CalcState::INVALID_START_DATE;
break;
case State::WRONG_ISO_TIME_FORMAT:
s = CalcState::WRONG_ISO_TIME_FORMAT;
break;
case State::OUTSIDE_ALLOWED_PARKING_TIME:
s = CalcState::OUTSIDE_ALLOWED_PARKING_TIME;
break;
case State::ABOVE_MAX_PARKING_PRICE:
s = CalcState::ABOVE_MAX_PARKING_TIME;
break;
}
if (m_desc.size() > 0) {
return s + ":" + m_desc;
}
return s;
}
explicit operator QString () const noexcept {
QString s;
switch (m_status) {
case State::SUCCESS:
s = CalcState::SUCCESS;
break;
case State::SUCCESS_MAXPRICE:
s = CalcState::SUCCESS_MAXPRICE;
break;
case State::ERROR_PARSING_ZONE_NR:
s = CalcState::ERROR_PARSING_ZONE_NR;
break;
case State::ERROR_LOADING_TARIFF:
s = CalcState::ERROR_LOADING_TARIFF;
break;
case State::ERROR_PARSING_TARIFF:
s = CalcState::ERROR_PARSING_TARIFF;
break;
case State::NEGATIVE_PARKING_TIME:
s = CalcState::NEGATIVE_PARKING_TIME;
break;
case State::ABOVE_MAX_PARKING_TIME:
s = CalcState::ABOVE_MAX_PARKING_TIME;
break;
case State::WRONG_PARAM_VALUES:
s = CalcState::WRONG_PARAM_VALUES;
break;
case State::BELOW_MIN_PARKING_TIME:
s = CalcState::BELOW_MIN_PARKING_TIME;
break;
case State::BELOW_MIN_PARKING_PRICE:
s = CalcState::BELOW_MIN_PARKING_PRICE;
break;
case State::OVERPAID:
s = CalcState::OVERPAID;
break;
case State::INVALID_START_DATE:
s = CalcState::INVALID_START_DATE;
break;
case State::WRONG_ISO_TIME_FORMAT:
s = CalcState::WRONG_ISO_TIME_FORMAT;
break;
case State::OUTSIDE_ALLOWED_PARKING_TIME:
s = CalcState::OUTSIDE_ALLOWED_PARKING_TIME;
break;
case State::ABOVE_MAX_PARKING_PRICE:
s = CalcState::ABOVE_MAX_PARKING_TIME;
break;
}
return s + ":" + m_desc;
}
CalcState &set(State s) { m_status = s; return *this; }
CalcState &setStatus(State s) { return set(s); }
CalcState &setStatus(QString const &desc) {
if (desc == SUCCESS) {
m_status = State::SUCCESS;
} else
if (desc == SUCCESS_MAXPRICE) {
m_status = State::SUCCESS_MAXPRICE;
}
if (desc == ERROR_PARSING_ZONE_NR) {
m_status = State::ERROR_PARSING_ZONE_NR;
} else
if (desc == ERROR_LOADING_TARIFF) {
m_status = State::SUCCESS;
} else
if (desc == ERROR_PARSING_TARIFF) {
m_status = State::ERROR_LOADING_TARIFF;
} else
if (desc == NEGATIVE_PARKING_TIME) {
m_status = State::NEGATIVE_PARKING_TIME;
} else
if (desc == INVALID_START_DATE) {
m_status = State::INVALID_START_DATE;
} else
if (desc == WRONG_PARAM_VALUES) {
m_status = State::WRONG_PARAM_VALUES;
} else
if (desc == WRONG_ISO_TIME_FORMAT) {
m_status = State::WRONG_ISO_TIME_FORMAT;
} else
if (desc == ABOVE_MAX_PARKING_TIME) {
m_status = State::ABOVE_MAX_PARKING_TIME;
} else
if (desc == BELOW_MIN_PARKING_TIME) {
m_status = State::BELOW_MIN_PARKING_TIME;
} else
if (desc == BELOW_MIN_PARKING_PRICE) {
m_status = State::BELOW_MIN_PARKING_PRICE;
} else
if (desc == OVERPAID) {
m_status = State::OVERPAID;
} else
if (desc == OUTSIDE_ALLOWED_PARKING_TIME) {
m_status = State::OUTSIDE_ALLOWED_PARKING_TIME;
} else
if (desc == ABOVE_MAX_PARKING_PRICE) {
m_status = State::ABOVE_MAX_PARKING_PRICE;
}
return *this;
}
State getStatus() const { return m_status; }
CalcState &setDesc(QString const &s) { m_desc = s; return *this; }
void setAllowedTimeRange(QTime const &from, QTime const &until) {
m_allowedTimeRange.setTimeRange(from, until);
}
TariffTimeRange getAllowedTimeRange() {
return m_allowedTimeRange;
}
};
CalcState CALCULATE_LIBRARY_API init_tariff(parking_tariff_t **tariff,
char const *config_file);
void CALCULATE_LIBRARY_API free_tariff(parking_tariff_t *tariff);
int CALCULATE_LIBRARY_API get_zone_nr(int zone = -1);
int CALCULATE_LIBRARY_API compute_next_timestep(parking_tariff_t *tariff, int currentTimeMinutes,
int UpDown, PermitType const &permitType);
QList<int> CALCULATE_LIBRARY_API get_time_steps(Configuration *cfg);
int CALCULATE_LIBRARY_API get_minimal_parkingtime(Configuration const *cfg,
PERMIT_TYPE permitType = PERMIT_TYPE::SHORT_TERM_PARKING,
int paymentOptionIndex=0);
int CALCULATE_LIBRARY_API get_maximal_parkingtime(Configuration const *cfg,
PERMIT_TYPE permitType = PERMIT_TYPE::SHORT_TERM_PARKING,
int paymentOptionIndex=0);
int CALCULATE_LIBRARY_API get_minimal_parkingprice(Configuration *cfg,
PERMIT_TYPE permitType = PERMIT_TYPE::SHORT_TERM_PARKING,
int paymentOptionIndex = 0,
QDateTime const &start = QDateTime::currentDateTime());
int CALCULATE_LIBRARY_API get_maximal_parkingprice(Configuration *cfg,
PERMIT_TYPE permitType = PERMIT_TYPE::SHORT_TERM_PARKING,
int paymentOptionIndex=0);
int CALCULATE_LIBRARY_API compute_product_price(Configuration const *cfg,
PERMIT_TYPE permitType = PERMIT_TYPE::SHORT_TERM_PARKING,
QDateTime const &start = QDateTime::currentDateTime(),
QDateTime *productStart = nullptr,
QDateTime *productEnd = nullptr);
CalcState CALCULATE_LIBRARY_API compute_price_for_parking_ticket( // deprecated
parking_tariff_t *tariff,
time_t start_parking_time,
time_t end_parking_time,
struct price_t *price,
PermitType permitType);
CalcState CALCULATE_LIBRARY_API compute_price_for_parking_ticket(
parking_tariff_t *tariff,
QDateTime &start_parking_time,
int netto_parking_time,
QDateTime &end_parking_time, // return value
struct price_t *price, // return value
PermitType permitType,
bool prepaid = true);
CalcState CALCULATE_LIBRARY_API compute_duration_for_parking_ticket( // deprecated
parking_tariff_t *tariff,
time_t start_parking_time,
double cost,
QString &duration,
PermitType permitType);
CalcState CALCULATE_LIBRARY_API compute_duration_for_parking_ticket(
parking_tariff_t *tariff,
QDateTime const &start_parking_time,
double cost,
QDateTime &ticketEndTime,
PermitType permitType); // return value
CalcState CALCULATE_LIBRARY_API compute_duration_for_daily_ticket(
parking_tariff_t *tariff,
QDateTime const &start_parking_time,
QDateTime &ticketEndTime,
PermitType permitType);
CalcState CALCULATE_LIBRARY_API compute_price_for_daily_ticket(
parking_tariff_t *tariff,
QDateTime const &start_parking_time,
QDateTime &ticketEndTime,
PERMIT_TYPE permitType,
struct price_t *price);
//#ifdef __cplusplus
//} // extern "C"
//#endif
#endif // CALCULATE_PRICE_H