From ab6a1470e6f2aaaa353ef9cff7525a40c965c0cd Mon Sep 17 00:00:00 2001 From: Remo <45837188+remkop22@users.noreply.github.com> Date: Mon, 19 Aug 2024 17:35:11 +0200 Subject: [PATCH] Only allow one flat fee per session (#72) * only allow one flat fee per session * add test * update changelog and ammend readme --- CHANGELOG.md | 5 +++ Cargo.lock | 4 +- README.md | 18 ++++++++ ocpi-tariffs/src/pricer.rs | 12 ++++- .../test_data/multiple_flat_fees/cdr.json | 45 +++++++++++++++++++ .../test_data/multiple_flat_fees/tariff.json | 15 +++++++ 6 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 ocpi-tariffs/test_data/multiple_flat_fees/cdr.json create mode 100644 ocpi-tariffs/test_data/multiple_flat_fees/tariff.json diff --git a/CHANGELOG.md b/CHANGELOG.md index a121368..2e89ec7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to this project will be documented in this file. +## Unreleased + +- Fixed bug where multiple flat fees are generated during the session. Instead + we now only use the first time a flat fee becomes active. + ## 0.6.0 2024-06-04 - Upgrade dependencies. diff --git a/Cargo.lock b/Cargo.lock index a69bb02..8875df1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -368,7 +368,7 @@ dependencies = [ [[package]] name = "ocpi-tariffs" -version = "0.5.2" +version = "0.6.0" dependencies = [ "chrono", "chrono-tz", @@ -381,7 +381,7 @@ dependencies = [ [[package]] name = "ocpi-tariffs-cli" -version = "0.5.2" +version = "0.6.0" dependencies = [ "chrono", "chrono-tz", diff --git a/README.md b/README.md index ad0e1c0..d3dec82 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,24 @@ classDiagram TariffElement "1" --o "0..1" TariffRestriction ``` +## Interpretation + +This implementation aims to follow the OCPI specification as closely as +possible. However, as with any specification, details might be left open to +interpretation. The following is a list of assumptions that have been made in +this implementation: + +- We assume that a `FLAT` price component can only be active once in a session. + The first time a `FLAT` price component becomes active it will be used and + subsequent active `FLAT` components will be ignored. Although this is not + explicitly mentioned in the OCPI 2.*.* specs, we feel it is the correct + interpretation. + +- We assume that the charging periods provided as input are well-formed. + Meaning, each period in the session that has a different price must be + provided as a separate charging period in the CDR. No attempt will be made + to subdivide or interpolate data inside a single provided period. + ## Contributing We welcome community contributions to this project. diff --git a/ocpi-tariffs/src/pricer.rs b/ocpi-tariffs/src/pricer.rs index 33df279..029f54e 100644 --- a/ocpi-tariffs/src/pricer.rs +++ b/ocpi-tariffs/src/pricer.rs @@ -115,8 +115,18 @@ impl<'a> Pricer<'a> { let mut total_charging_time = HoursDecimal::zero(); let mut total_parking_time = HoursDecimal::zero(); + let mut has_flat_fee = false; + for (index, period) in cdr.periods.iter().enumerate() { - let components = tariff.active_components(period); + let mut components = tariff.active_components(period); + + if components.flat.is_some() { + if has_flat_fee { + components.flat = None; + } else { + has_flat_fee = true; + } + } step_size.update(index, &components, period); diff --git a/ocpi-tariffs/test_data/multiple_flat_fees/cdr.json b/ocpi-tariffs/test_data/multiple_flat_fees/cdr.json new file mode 100644 index 0000000..3b9c0d3 --- /dev/null +++ b/ocpi-tariffs/test_data/multiple_flat_fees/cdr.json @@ -0,0 +1,45 @@ +{ + "country_code": "BE", + "start_date_time": "2015-06-29T15:00:00Z", + "end_date_time": "2015-06-29T16:30:00Z", + "currency": "EUR", + "tariffs": [], + "cdr_location": { + "country": "NLD" + }, + "charging_periods": [ + { + "start_date_time": "2015-06-29T15:00:00Z", + "dimensions": [{ + "type": "TIME", + "volume": 0.5 + }] + }, + { + "start_date_time": "2015-06-29T15:30:00Z", + "dimensions": [{ + "type": "TIME", + "volume": 0.5 + }] + }, + { + "start_date_time": "2015-06-29T16:00:00Z", + "dimensions": [{ + "type": "TIME", + "volume": 0.5 + }] + } + ], + "total_fixed_cost": { + "excl_vat": 1.0, + "incl_vat": 1.0 + }, + "total_cost": { + "excl_vat": 1.0, + "incl_vat": 1.0 + }, + "total_energy": 0, + "total_time": 1.5, + "total_parking_time": 0, + "last_updated": "2015-06-29T22:01:13Z" +} diff --git a/ocpi-tariffs/test_data/multiple_flat_fees/tariff.json b/ocpi-tariffs/test_data/multiple_flat_fees/tariff.json new file mode 100644 index 0000000..6ba6c18 --- /dev/null +++ b/ocpi-tariffs/test_data/multiple_flat_fees/tariff.json @@ -0,0 +1,15 @@ +{ + "country_code": "DE", + "party_id": "ALL", + "id": "12", + "currency": "EUR", + "elements": [{ + "price_components": [{ + "type": "FLAT", + "price": 1.00, + "vat": 0.0, + "step_size": 0 + }] + }], + "last_updated": "2015-06-29T20:39:09Z" +}