ocpi-tariffs 0.55.0

OCPI tariff calculations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#![allow(clippy::missing_panics_doc, reason = "tests are allowed to panic")]
#![allow(clippy::panic, reason = "tests are allowed to panic")]

use std::assert_matches;

use rust_decimal_macros::dec;

use crate::{Version, cdr, duration, json, price, price::test::UnwrapReport as _, tariff, test};

#[test]
fn should_bill_charging_time_in_blocks_of_the_time_step_size() {
    test::setup();

    // 21 minutes of charging against a `step_size` of 900 seconds is billed as two blocks of 15
    // minutes. At 2.00 per hour that is 1.00, where the 21 minutes alone cost 0.70. The CDR's own
    // `total_cost` of `0` is never compared, because the assertion reads the calculated total.
    let cdr_json = serde_json::json!({
        "country_code": "NL",
        "party_id": "TDR",
        "start_date_time": "2022-01-13T10:00:00Z",
        "end_date_time": "2022-01-13T10:21:00Z",
        "currency": "EUR",
        "tariffs": [],
        "cdr_location": { "country": "NLD" },
        "charging_periods": [
            {
                "start_date_time": "2022-01-13T10:00:00Z",
                "dimensions": [{ "type": "TIME", "volume": 0.35 }]
            }
        ],
        "total_cost": { "excl_vat": 0.00 },
        "total_time": 0.35,
        "total_energy": 0,
        "last_updated": "2022-01-13T00:00:00Z"
    })
    .to_string();
    let tariff_json = serde_json::json!({
        "id": "T1",
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "elements": [
            {
                "price_components": [
                    { "type": "TIME", "price": 2.00, "vat": 0.0, "step_size": 900 }
                ]
            }
        ],
        "last_updated": "2022-01-01T00:00:00Z"
    })
    .to_string();

    // The CDR and the tariff leave out most of the fields the spec requires, so the schema walk
    // reports them as missing. Any other warning, such as a misspelled key reported as an
    // unexpected field, means the input is not what the test says it is.
    let (cdr, mut cdr_warnings) =
        cdr::check(json::parse_object(&cdr_json).unwrap(), Version::V221).into_parts();
    cdr_warnings.remove_missing_fields();
    test::assert_schema_warnings(&cdr_warnings, &[]);

    let (cdr, cdr_warnings) = cdr::build(cdr).unwrap().into_parts();
    test::assert_build_warnings(&cdr_warnings, &[]);

    let (tariff, mut tariff_warnings) =
        tariff::check(json::parse_object(&tariff_json).unwrap(), Version::V221).into_parts();
    tariff_warnings.remove_missing_fields();
    test::assert_schema_warnings(&tariff_warnings, &[]);

    let (tariff, tariff_warnings) = tariff::build(tariff).unwrap().into_parts();
    test::assert_build_warnings(&tariff_warnings, &[]);

    let report =
        cdr::price(&cdr, &[tariff], &jiff::tz::TimeZone::UTC).unwrap_report(cdr.as_json_str());
    let (report, _) = report.into_parts();

    assert_eq!(
        report.total_cost.calculated.unwrap().excl_vat,
        dec!(1.00).into()
    );
}

#[test]
fn should_bill_a_parking_period_of_40_minutes_stated_as_0_6667_hours_as_40_minutes() {
    test::setup();

    // OCPI states a volume in hours to 4 decimals, so 40 minutes of parking is written as `0.6667`,
    // which is 2400.12 seconds. A pricer that billed the volume as stated would round it up to 41
    // blocks of 60 seconds, 10.25 at 15.00 per hour, where 40 minutes cost 10.00.
    let cdr_json = serde_json::json!({
        "country_code": "NL",
        "party_id": "TDR",
        "start_date_time": "2022-01-13T10:00:00Z",
        "end_date_time": "2022-01-13T10:40:00Z",
        "currency": "EUR",
        "tariffs": [],
        "cdr_location": { "country": "NLD" },
        "charging_periods": [
            {
                "start_date_time": "2022-01-13T10:00:00Z",
                "dimensions": [{ "type": "PARKING_TIME", "volume": 0.6667 }]
            }
        ],
        "total_cost": { "excl_vat": 0.00 },
        "total_time": 0.6667,
        "total_parking_time": 0.6667,
        "total_energy": 0,
        "last_updated": "2022-01-13T00:00:00Z"
    })
    .to_string();
    let tariff_json = serde_json::json!({
        "id": "T1",
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "elements": [
            {
                "price_components": [
                    { "type": "PARKING_TIME", "price": 15.00, "vat": 0.0, "step_size": 60 }
                ]
            }
        ],
        "last_updated": "2022-01-01T00:00:00Z"
    })
    .to_string();

    let (cdr, mut cdr_warnings) =
        cdr::check(json::parse_object(&cdr_json).unwrap(), Version::V221).into_parts();
    cdr_warnings.remove_missing_fields();
    test::assert_schema_warnings(&cdr_warnings, &[]);

    let (cdr, cdr_warnings) = cdr::build(cdr).unwrap().into_parts();
    test::assert_build_warnings(&cdr_warnings, &[]);

    let (tariff, mut tariff_warnings) =
        tariff::check(json::parse_object(&tariff_json).unwrap(), Version::V221).into_parts();
    tariff_warnings.remove_missing_fields();
    test::assert_schema_warnings(&tariff_warnings, &[]);

    let (tariff, tariff_warnings) = tariff::build(tariff).unwrap().into_parts();
    test::assert_build_warnings(&tariff_warnings, &[]);

    let report =
        cdr::price(&cdr, &[tariff], &jiff::tz::TimeZone::UTC).unwrap_report(cdr.as_json_str());
    let (report, _) = report.into_parts();

    assert_eq!(
        report.total_cost.calculated.unwrap().excl_vat,
        dec!(10.00).into()
    );
}

#[test]
fn should_bill_a_charging_time_over_a_step_by_under_a_millisecond_as_two_steps() {
    test::setup();

    // Charging time is rounded up to a whole step even when it passes the step by less
    // than a millisecond. The stated volume is within 0.18 seconds of the period's length, so the
    // pricer bills the period's length rather than the volume.
    let cdr_json = serde_json::json!({
        "country_code": "NL",
        "party_id": "TDR",
        "start_date_time": "2022-01-13T10:00:00Z",
        "end_date_time": "2022-01-13T10:05:00.0004Z",
        "currency": "EUR",
        "tariffs": [],
        "cdr_location": { "country": "NLD" },
        "charging_periods": [
            {
                "start_date_time": "2022-01-13T10:00:00Z",
                "dimensions": [{ "type": "TIME", "volume": 0.0833 }]
            }
        ],
        "total_cost": { "excl_vat": 0.00 },
        "total_time": 0.0833,
        "total_energy": 0,
        "last_updated": "2022-01-13T00:00:00Z"
    })
    .to_string();
    let tariff_json = serde_json::json!({
        "id": "T1",
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "elements": [
            {
                "price_components": [
                    { "type": "TIME", "price": 6.00, "vat": 0.0, "step_size": 300 }
                ]
            }
        ],
        "last_updated": "2022-01-01T00:00:00Z"
    })
    .to_string();

    let (cdr, mut cdr_warnings) =
        cdr::check(json::parse_object(&cdr_json).unwrap(), Version::V221).into_parts();
    cdr_warnings.remove_missing_fields();
    test::assert_schema_warnings(&cdr_warnings, &[]);

    let (cdr, cdr_warnings) = cdr::build(cdr).unwrap().into_parts();
    test::assert_build_warnings(&cdr_warnings, &[]);

    let (tariff, mut tariff_warnings) =
        tariff::check(json::parse_object(&tariff_json).unwrap(), Version::V221).into_parts();
    tariff_warnings.remove_missing_fields();
    test::assert_schema_warnings(&tariff_warnings, &[]);

    let (tariff, tariff_warnings) = tariff::build(tariff).unwrap().into_parts();
    test::assert_build_warnings(&tariff_warnings, &[]);

    let report =
        cdr::price(&cdr, &[tariff], &jiff::tz::TimeZone::UTC).unwrap_report(cdr.as_json_str());
    let (report, _) = report.into_parts();

    assert_eq!(
        report.total_cost.calculated.unwrap().excl_vat,
        dec!(1.00).into()
    );
}

#[test]
fn should_bill_a_parking_time_over_a_step_by_under_a_millisecond_as_two_steps() {
    test::setup();

    // Parking time is rounded up to a whole step even when it passes the step by less
    // than a millisecond. The stated volume is within 0.18 seconds of the period's length, so the
    // pricer bills the period's length rather than the volume.
    let cdr_json = serde_json::json!({
        "country_code": "NL",
        "party_id": "TDR",
        "start_date_time": "2022-01-13T10:00:00Z",
        "end_date_time": "2022-01-13T10:05:00.0004Z",
        "currency": "EUR",
        "tariffs": [],
        "cdr_location": { "country": "NLD" },
        "charging_periods": [
            {
                "start_date_time": "2022-01-13T10:00:00Z",
                "dimensions": [{ "type": "PARKING_TIME", "volume": 0.0833 }]
            }
        ],
        "total_cost": { "excl_vat": 0.00 },
        "total_time": 0.0833,
        "total_parking_time": 0.0833,
        "total_energy": 0,
        "last_updated": "2022-01-13T00:00:00Z"
    })
    .to_string();
    let tariff_json = serde_json::json!({
        "id": "T1",
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "elements": [
            {
                "price_components": [
                    { "type": "PARKING_TIME", "price": 6.00, "vat": 0.0, "step_size": 300 }
                ]
            }
        ],
        "last_updated": "2022-01-01T00:00:00Z"
    })
    .to_string();

    let (cdr, mut cdr_warnings) =
        cdr::check(json::parse_object(&cdr_json).unwrap(), Version::V221).into_parts();
    cdr_warnings.remove_missing_fields();
    test::assert_schema_warnings(&cdr_warnings, &[]);

    let (cdr, cdr_warnings) = cdr::build(cdr).unwrap().into_parts();
    test::assert_build_warnings(&cdr_warnings, &[]);

    let (tariff, mut tariff_warnings) =
        tariff::check(json::parse_object(&tariff_json).unwrap(), Version::V221).into_parts();
    tariff_warnings.remove_missing_fields();
    test::assert_schema_warnings(&tariff_warnings, &[]);

    let (tariff, tariff_warnings) = tariff::build(tariff).unwrap().into_parts();
    test::assert_build_warnings(&tariff_warnings, &[]);

    let report =
        cdr::price(&cdr, &[tariff], &jiff::tz::TimeZone::UTC).unwrap_report(cdr.as_json_str());
    let (report, _) = report.into_parts();

    assert_eq!(
        report.total_cost.calculated.unwrap().excl_vat,
        dec!(1.00).into()
    );
}

#[test]
fn should_convert_the_longest_durations_to_seconds_without_the_fallback() {
    // Every `SignedDuration` converts to seconds exactly, so `delta_as_seconds_dec` never falls
    // back to `Decimal::MAX`. The fallback would bill the most seconds a `Decimal` holds, with a
    // positive sign even for a negative duration.
    assert_eq!(
        price::delta_as_seconds_dec(jiff::SignedDuration::MAX),
        dec!(9223372036854775807.999999999)
    );
    assert_eq!(
        price::delta_as_seconds_dec(jiff::SignedDuration::MIN),
        dec!(-9223372036854775808.999999999)
    );
}

#[test]
fn should_bill_a_charging_time_shorter_than_its_period_as_stated() {
    test::setup();

    // The period lasts 40 minutes and states 15 minutes of charging. A pricer that read every
    // volume as its period's length would bill 40 minutes, 4.00 at 6.00 per hour, where the 15
    // minutes stated cost 1.50.
    let cdr_json = serde_json::json!({
        "country_code": "NL",
        "party_id": "TDR",
        "start_date_time": "2022-01-13T10:00:00Z",
        "end_date_time": "2022-01-13T10:40:00Z",
        "currency": "EUR",
        "tariffs": [],
        "cdr_location": { "country": "NLD" },
        "charging_periods": [
            {
                "start_date_time": "2022-01-13T10:00:00Z",
                "dimensions": [{ "type": "TIME", "volume": 0.25 }]
            }
        ],
        "total_cost": { "excl_vat": 0.00 },
        "total_time": 0.6667,
        "total_energy": 0,
        "last_updated": "2022-01-13T00:00:00Z"
    })
    .to_string();
    let tariff_json = serde_json::json!({
        "id": "T1",
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "elements": [
            {
                "price_components": [
                    { "type": "TIME", "price": 6.00, "vat": 0.0, "step_size": 60 }
                ]
            }
        ],
        "last_updated": "2022-01-01T00:00:00Z"
    })
    .to_string();

    let (cdr, mut cdr_warnings) =
        cdr::check(json::parse_object(&cdr_json).unwrap(), Version::V221).into_parts();
    cdr_warnings.remove_missing_fields();
    test::assert_schema_warnings(&cdr_warnings, &[]);

    let (cdr, cdr_warnings) = cdr::build(cdr).unwrap().into_parts();
    test::assert_build_warnings(&cdr_warnings, &[]);

    let (tariff, mut tariff_warnings) =
        tariff::check(json::parse_object(&tariff_json).unwrap(), Version::V221).into_parts();
    tariff_warnings.remove_missing_fields();
    test::assert_schema_warnings(&tariff_warnings, &[]);

    let (tariff, tariff_warnings) = tariff::build(tariff).unwrap().into_parts();
    test::assert_build_warnings(&tariff_warnings, &[]);

    let report =
        cdr::price(&cdr, &[tariff], &jiff::tz::TimeZone::UTC).unwrap_report(cdr.as_json_str());
    let (report, _) = report.into_parts();

    assert_eq!(
        report.total_cost.calculated.unwrap().excl_vat,
        dec!(1.50).into()
    );
}

#[test]
fn should_bill_nothing_for_a_charging_time_of_0_in_a_period_shorter_than_0_18_seconds() {
    test::setup();

    // The period lasts 150 milliseconds and states a charging time of `0`. 150 milliseconds is
    // within the 0.18 seconds the pricer allows between a volume and its period's length. A pricer
    // that read that `0` as the period's length would bill one 60-second block, 0.10 at 6.00 per
    // hour, for charging the session never did.
    let cdr_json = serde_json::json!({
        "country_code": "NL",
        "party_id": "TDR",
        "start_date_time": "2022-01-13T10:00:00Z",
        "end_date_time": "2022-01-13T10:00:00.150Z",
        "currency": "EUR",
        "tariffs": [],
        "cdr_location": { "country": "NLD" },
        "charging_periods": [
            {
                "start_date_time": "2022-01-13T10:00:00Z",
                "dimensions": [{ "type": "TIME", "volume": 0 }]
            }
        ],
        "total_cost": { "excl_vat": 0.00 },
        "total_time": 0,
        "total_energy": 0,
        "last_updated": "2022-01-13T00:00:00Z"
    })
    .to_string();
    let tariff_json = serde_json::json!({
        "id": "T1",
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "elements": [
            {
                "price_components": [
                    { "type": "TIME", "price": 6.00, "vat": 0.0, "step_size": 60 }
                ]
            }
        ],
        "last_updated": "2022-01-01T00:00:00Z"
    })
    .to_string();

    let (cdr, mut cdr_warnings) =
        cdr::check(json::parse_object(&cdr_json).unwrap(), Version::V221).into_parts();
    cdr_warnings.remove_missing_fields();
    test::assert_schema_warnings(&cdr_warnings, &[]);

    let (cdr, cdr_warnings) = cdr::build(cdr).unwrap().into_parts();
    test::assert_build_warnings(&cdr_warnings, &[]);

    let (tariff, mut tariff_warnings) =
        tariff::check(json::parse_object(&tariff_json).unwrap(), Version::V221).into_parts();
    tariff_warnings.remove_missing_fields();
    test::assert_schema_warnings(&tariff_warnings, &[]);

    let (tariff, tariff_warnings) = tariff::build(tariff).unwrap().into_parts();
    test::assert_build_warnings(&tariff_warnings, &[]);

    let report =
        cdr::price(&cdr, &[tariff], &jiff::tz::TimeZone::UTC).unwrap_report(cdr.as_json_str());
    let (report, _) = report.into_parts();

    assert_eq!(
        report.total_cost.calculated.unwrap().excl_vat,
        dec!(0.00).into()
    );
}

/// A step size that overflows the billed duration refuses the price.
#[test]
fn should_refuse_the_price_when_a_step_size_overflows() {
    test::setup();

    let cdr_json = serde_json::json!({
        "country_code": "NL",
        "party_id": "TDR",
        "start_date_time": "2025-07-21T06:00:00Z",
        "end_date_time": "2025-07-21T08:00:00Z",
        "currency": "EUR",
        "tariffs": [],
        "cdr_location": { "country": "NLD" },
        "charging_periods": [
            {
                "start_date_time": "2025-07-21T06:00:00Z",
                "dimensions": [
                    { "type": "ENERGY", "volume": 20 },
                    { "type": "TIME", "volume": 2 }
                ]
            }
        ],
        "total_cost": { "excl_vat": 10.00 },
        "total_energy": 20,
        "total_time": 2,
        "last_updated": "2025-07-21T08:00:00Z"
    })
    .to_string();
    // A `step_size` of `10_000_000_000_000_000` seconds is more milliseconds than an `i64` holds.
    // Rounding the charging time up to that step overflows, which refuses the price.
    let tariff_json = serde_json::json!({
        "id": "T1",
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "elements": [
            {
                "price_components": [
                    { "type": "TIME", "price": 2.00, "step_size": 10_000_000_000_000_000_u64 }
                ]
            }
        ],
        "last_updated": "2025-01-01T00:00:00Z"
    })
    .to_string();

    let (cdr, mut cdr_warnings) =
        cdr::check(json::parse_object(&cdr_json).unwrap(), Version::V221).into_parts();
    cdr_warnings.remove_missing_fields();
    test::assert_schema_warnings(&cdr_warnings, &[]);

    let (cdr, cdr_warnings) = cdr::build(cdr).unwrap().into_parts();
    test::assert_build_warnings(&cdr_warnings, &[]);

    let (tariff, mut tariff_warnings) =
        tariff::check(json::parse_object(&tariff_json).unwrap(), Version::V221).into_parts();
    tariff_warnings.remove_missing_fields();
    test::assert_schema_warnings(&tariff_warnings, &[]);

    let (tariff, tariff_warnings) = tariff::build(tariff).unwrap().into_parts();
    test::assert_build_warnings(&tariff_warnings, &[]);

    let verdict = cdr::price(&cdr, &[tariff], &jiff::tz::TimeZone::UTC);
    let Err(failure) = verdict else {
        panic!("a step size that overflows should refuse the price");
    };
    let error = failure.unwrap();
    let (error, element) = error.into_parts();

    assert_matches!(error, price::Warning::Duration(duration::Warning::Overflow));
    assert_eq!(element.path.as_str(), "$");
}