Fix with min time in GetDurationFromCost

This commit is contained in:
Moran Rod 2023-05-14 17:19:24 +02:00
parent 1142efaec2
commit 6b3c1cbf0c
2 changed files with 73 additions and 70 deletions

View File

@ -43,6 +43,7 @@ std::string Calculator::GetDurationFromCost(Configuration* cfg,
double min_price = 0; double min_price = 0;
min_price = cfg->PaymentOption.find(payment_option)->second.pop_min_price; min_price = cfg->PaymentOption.find(payment_option)->second.pop_min_price;
if(price < min_price) if(price < min_price)
{ {
return "PARKING NOT ALLOWED"; return "PARKING NOT ALLOWED";
@ -109,6 +110,8 @@ std::string Calculator::GetDurationFromCost(Configuration* cfg,
} }
if (price_per_unit < 0) price_per_unit = 1.0f; if (price_per_unit < 0) price_per_unit = 1.0f;
if((price/price_per_unit) < minMin) return "PARKING NOT ALLOWED";
LOG_DEBUG("Calculated price per minute: ", price_per_unit); LOG_DEBUG("Calculated price per minute: ", price_per_unit);
if (price_per_unit < 0) if (price_per_unit < 0)

View File

@ -34,9 +34,9 @@ int main() {
if (init_tariff(&tariff, "C:\\Users\\MROD\\Documents\\QtCreator\\build-MOBILISIS-Calculator-Desktop_Qt_5_12_12_MSVC2017_32bit-Debug\\main\\etc\\psa_tariff\\zone1.json")) { if (init_tariff(&tariff, "C:\\Users\\MROD\\Documents\\QtCreator\\build-MOBILISIS-Calculator-Desktop_Qt_5_12_12_MSVC2017_32bit-Debug\\main\\etc\\psa_tariff\\zone1.json")) {
struct price_t price; struct price_t price;
memset(&price, 0x00, sizeof(price)); memset(&price, 0x00, sizeof(price));
QDateTime start = QDateTime::fromString("2023-05-12T15:00:34",Qt::ISODate); //QDateTime::currentDateTime(); QDateTime start = QDateTime::fromString("2023-05-12T15:00:38",Qt::ISODate); //QDateTime::currentDateTime();
time_t start_parking_time = start.toSecsSinceEpoch() / 60; time_t start_parking_time = start.toSecsSinceEpoch() / 60;
time_t end_parking_time = start_parking_time + 610; time_t end_parking_time = start_parking_time + 15;
if (compute_price_for_parking_ticket(tariff, if (compute_price_for_parking_ticket(tariff,
start_parking_time, start_parking_time,
@ -46,75 +46,75 @@ int main() {
} }
QString duration; QString duration;
if(compute_duration_for_parking_ticket(tariff,start_parking_time,188,duration)) if(compute_duration_for_parking_ticket(tariff,start_parking_time,37.5,duration))
{ {
qDebug() << "GetDurationFromCost() => duration=" << duration; qDebug() << "GetDurationFromCost() => duration=" << duration;
} }
// // tests // // tests
// struct tm now; // struct tm now;
// memset(&now, 0, sizeof(now)); // memset(&now, 0, sizeof(now));
// // 3.Jan 2023 -> Tuesday // // 3.Jan 2023 -> Tuesday
// strptime("2023-01-03T14:00:00", "%Y-%m-%dT%H:%M:%S", &now); // strptime("2023-01-03T14:00:00", "%Y-%m-%dT%H:%M:%S", &now);
// for (int i = 0; i < 600; ++i) { // for (int i = 0; i < 600; ++i) {
// start_parking_time = mktime(&now); // start_parking_time = mktime(&now);
// end_parking_time = start_parking_time + 240; // duration == 240 // end_parking_time = start_parking_time + 240; // duration == 240
// if (compute_price_for_parking_ticket(tariff, // if (compute_price_for_parking_ticket(tariff,
// start_parking_time, // start_parking_time,
// end_parking_time, // end_parking_time,
// &price)) { // &price)) {
// int const zone = get_zone_nr(1); // int const zone = get_zone_nr(1);
// switch (zone) { // switch (zone) {
// case 1: // case 1:
// assert(price.netto == 879); // expected value: 880 // assert(price.netto == 879); // expected value: 880
// break; // break;
// case 2: // case 2:
// /* fall through */ // /* fall through */
// case 3: // case 3:
// assert(price.netto == 1920); // assert(price.netto == 1920);
// break; // break;
// } // }
// } // }
// time_t t = start_parking_time + 60; // time_t t = start_parking_time + 60;
// now = *localtime(&t); // now = *localtime(&t);
// } // }
// // // //
// // test May 1st 2023 // // test May 1st 2023
// // // //
// memset(&now, 0, sizeof(now)); // memset(&now, 0, sizeof(now));
// strptime("2023-04-30T06:00:00", "%Y-%m-%dT%H:%M:%S", &now); // strptime("2023-04-30T06:00:00", "%Y-%m-%dT%H:%M:%S", &now);
// now.tm_hour -= 1; // for ctime // now.tm_hour -= 1; // for ctime
// // for (int i=0; i<6*24; ++i) { // // for (int i=0; i<6*24; ++i) {
// for (int i=0; i<1; ++i) { // for (int i=0; i<1; ++i) {
// int const duration = 120; // int const duration = 120;
// time_t t = mktime(&now); // time_t t = mktime(&now);
// start_parking_time = t / 60; // start_parking_time = t / 60;
// end_parking_time = start_parking_time + duration; // end_parking_time = start_parking_time + duration;
// if (compute_price_for_parking_ticket(tariff, // if (compute_price_for_parking_ticket(tariff,
// start_parking_time, // start_parking_time,
// end_parking_time, // end_parking_time,
// &price)) { // &price)) {
// int const zone = get_zone_nr(); // int const zone = get_zone_nr();
// switch (zone) { // switch (zone) {
// case 1: // case 1:
// qDebug() << i << zone << ctime(&t) << price.netto << " FT"; // qDebug() << i << zone << ctime(&t) << price.netto << " FT";
// assert(price.netto == 440); // assert(price.netto == 440);
// break; // break;
// case 2: // case 2:
// /* fall through */ // /* fall through */
// case 3: // case 3:
// qDebug() << i << zone << ctime(&t) << price.netto << " FT"; // qDebug() << i << zone << ctime(&t) << price.netto << " FT";
// assert(price.netto == 960); // assert(price.netto == 960);
// break; // break;
// } // }
// } // }
// t = (start_parking_time + 60)*60; // t = (start_parking_time + 60)*60;
// now = *localtime(&t); // now = *localtime(&t);
// } // }
free_tariff(tariff); free_tariff(tariff);
} }
@ -151,7 +151,7 @@ int main() {
struct tm now; // = Utilities::DateTimeToStructTm("2023-03-01T16:00:00"); struct tm now; // = Utilities::DateTimeToStructTm("2023-03-01T16:00:00");
memset(&now, 0, sizeof(now)); memset(&now, 0, sizeof(now));
char buffer[64]; char buffer[64];
//#if 0 //#if 0
// 3.Jan 2023 -> Tuesday // 3.Jan 2023 -> Tuesday
strptime("2023-01-03T14:00:00", "%Y-%m-%dT%H:%M:%S", &now); strptime("2023-01-03T14:00:00", "%Y-%m-%dT%H:%M:%S", &now);
for (int i = 0; i < 600; ++i) { for (int i = 0; i < 600; ++i) {
@ -226,8 +226,8 @@ int main() {
int const duration = 120; int const duration = 120;
double cost = calculator.GetCostFromDuration(&cfg, double cost = calculator.GetCostFromDuration(&cfg,
PaymentOption::Option1, buffer, duration, PaymentOption::Option1, buffer, duration,
false, true); false, true);
switch (zone) { switch (zone) {
case 1: case 1:
@ -265,8 +265,8 @@ int main() {
int const duration = 120; int const duration = 120;
double cost = calculator.GetCostFromDuration(&cfg, double cost = calculator.GetCostFromDuration(&cfg,
PaymentOption::Option1, buffer, duration, PaymentOption::Option1, buffer, duration,
false, true); false, true);
switch (zone) { switch (zone) {
case 1: case 1:
@ -299,8 +299,8 @@ int main() {
double const compCost = (duration < 15) ? 0 : duration * ((zone == 1) ? 3.6666 : 8.0); double const compCost = (duration < 15) ? 0 : duration * ((zone == 1) ? 3.6666 : 8.0);
double cost = calculator.GetCostFromDuration(&cfg, double cost = calculator.GetCostFromDuration(&cfg,
PaymentOption::Option1, buffer, duration, PaymentOption::Option1, buffer, duration,
false, true); false, true);
if (fabs(cost - compCost) > 1.0) { // zone 1 has rounding errors if (fabs(cost - compCost) > 1.0) { // zone 1 has rounding errors
cout << "ERROR ===> [" << i << "] " << asctime(&now) cout << "ERROR ===> [" << i << "] " << asctime(&now)
<< " - Total cost is: " << cost << " FT (computed=" << " - Total cost is: " << cost << " FT (computed="