bo4e_core/com/
cost_position.rs1use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6use crate::enums::Unit;
7use crate::traits::{Bo4eMeta, Bo4eObject};
8
9use super::{Amount, Price};
10
11#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
27#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
28#[cfg_attr(feature = "json-schema", schemars(rename = "Kostenposition"))]
29#[serde(rename_all = "camelCase")]
30pub struct CostPosition {
31 #[serde(flatten)]
33 pub meta: Bo4eMeta,
34
35 #[serde(skip_serializing_if = "Option::is_none")]
37 #[cfg_attr(feature = "json-schema", schemars(rename = "positionstitel"))]
38 pub title: Option<String>,
39
40 #[serde(skip_serializing_if = "Option::is_none")]
42 #[cfg_attr(feature = "json-schema", schemars(rename = "betragKostenposition"))]
43 pub amount: Option<Amount>,
44
45 #[serde(skip_serializing_if = "Option::is_none")]
47 #[cfg_attr(feature = "json-schema", schemars(rename = "artikelbezeichnung"))]
48 pub article_description: Option<String>,
49
50 #[serde(skip_serializing_if = "Option::is_none")]
52 #[cfg_attr(feature = "json-schema", schemars(rename = "einzelpreis"))]
53 pub unit_price: Option<Price>,
54
55 #[serde(skip_serializing_if = "Option::is_none")]
57 #[cfg_attr(feature = "json-schema", schemars(rename = "von"))]
58 pub start_date: Option<DateTime<Utc>>,
59
60 #[serde(skip_serializing_if = "Option::is_none")]
62 #[cfg_attr(feature = "json-schema", schemars(rename = "bis"))]
63 pub end_date: Option<DateTime<Utc>>,
64
65 #[serde(skip_serializing_if = "Option::is_none")]
67 #[cfg_attr(feature = "json-schema", schemars(rename = "mengeWert"))]
68 pub quantity_value: Option<f64>,
69
70 #[serde(skip_serializing_if = "Option::is_none")]
72 #[cfg_attr(feature = "json-schema", schemars(rename = "mengeEinheit"))]
73 pub quantity_unit: Option<Unit>,
74
75 #[serde(skip_serializing_if = "Option::is_none")]
77 #[cfg_attr(feature = "json-schema", schemars(rename = "zeitmengeWert"))]
78 pub time_quantity_value: Option<f64>,
79
80 #[serde(skip_serializing_if = "Option::is_none")]
82 #[cfg_attr(feature = "json-schema", schemars(rename = "zeitmengeEinheit"))]
83 pub time_quantity_unit: Option<Unit>,
84
85 #[serde(skip_serializing_if = "Option::is_none")]
87 #[cfg_attr(feature = "json-schema", schemars(rename = "artikeldetail"))]
88 pub article_detail: Option<String>,
89}
90
91impl Bo4eObject for CostPosition {
92 fn type_name_german() -> &'static str {
93 "Kostenposition"
94 }
95
96 fn type_name_english() -> &'static str {
97 "CostPosition"
98 }
99
100 fn meta(&self) -> &Bo4eMeta {
101 &self.meta
102 }
103
104 fn meta_mut(&mut self) -> &mut Bo4eMeta {
105 &mut self.meta
106 }
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112 use crate::enums::Currency;
113
114 #[test]
115 fn test_cost_position() {
116 let position = CostPosition {
117 title: Some("Netznutzung".to_string()),
118 article_description: Some("Arbeitspreis".to_string()),
119 unit_price: Some(Price::eur_per_kwh(0.0582)),
120 quantity_value: Some(3660.0),
121 quantity_unit: Some(Unit::KilowattHour),
122 amount: Some(Amount::eur(213.01)),
123 ..Default::default()
124 };
125
126 assert_eq!(position.title, Some("Netznutzung".to_string()));
127 assert_eq!(position.quantity_value, Some(3660.0));
128 }
129
130 #[test]
131 fn test_default() {
132 let position = CostPosition::default();
133 assert!(position.title.is_none());
134 assert!(position.amount.is_none());
135 }
136
137 #[test]
138 fn test_serialize() {
139 let position = CostPosition {
140 title: Some("Stromsteuer".to_string()),
141 amount: Some(Amount {
142 value: Some(100.0),
143 currency: Some(Currency::Eur),
144 ..Default::default()
145 }),
146 ..Default::default()
147 };
148
149 let json = serde_json::to_string(&position).unwrap();
150 assert!(json.contains(r#""title":"Stromsteuer""#));
151 }
152
153 #[test]
154 fn test_roundtrip() {
155 let position = CostPosition {
156 title: Some("Netzentgelt".to_string()),
157 article_description: Some("Leistungspreis".to_string()),
158 unit_price: Some(Price::eur_per_month(55.0)),
159 amount: Some(Amount::eur(660.0)),
160 ..Default::default()
161 };
162
163 let json = serde_json::to_string(&position).unwrap();
164 let parsed: CostPosition = serde_json::from_str(&json).unwrap();
165 assert_eq!(position, parsed);
166 }
167
168 #[test]
169 fn test_bo4e_object_impl() {
170 assert_eq!(CostPosition::type_name_german(), "Kostenposition");
171 assert_eq!(CostPosition::type_name_english(), "CostPosition");
172 }
173}