Skip to main content

bo4e_core/bo/
hardware_price_sheet.rs

1//! Hardware price sheet (PreisblattHardware) business object.
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6use crate::com::{Hardware, Price, TimePeriod};
7use crate::enums::Division;
8use crate::traits::{Bo4eMeta, Bo4eObject};
9
10/// A price sheet for hardware (meters, communication devices, etc.).
11///
12/// German: PreisblattHardware
13///
14/// # Example
15///
16/// ```rust
17/// use bo4e_core::bo::HardwarePriceSheet;
18/// use bo4e_core::enums::Division;
19///
20/// let price_sheet = HardwarePriceSheet {
21///     designation: Some("Hardware Preisblatt 2024".to_string()),
22///     division: Some(Division::Electricity),
23///     ..Default::default()
24/// };
25/// ```
26#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
27#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
28#[cfg_attr(feature = "json-schema", schemars(rename = "PreisblattHardware"))]
29#[serde(rename_all = "camelCase")]
30pub struct HardwarePriceSheet {
31    /// BO4E metadata
32    #[serde(flatten)]
33    pub meta: Bo4eMeta,
34
35    /// Name/designation of the price sheet (Bezeichnung)
36    #[serde(skip_serializing_if = "Option::is_none")]
37    #[cfg_attr(feature = "json-schema", schemars(rename = "bezeichnung"))]
38    pub designation: Option<String>,
39
40    /// Description (Beschreibung)
41    #[serde(skip_serializing_if = "Option::is_none")]
42    #[cfg_attr(feature = "json-schema", schemars(rename = "beschreibung"))]
43    pub description: Option<String>,
44
45    /// Energy division (Sparte)
46    #[serde(skip_serializing_if = "Option::is_none")]
47    #[cfg_attr(feature = "json-schema", schemars(rename = "sparte"))]
48    pub division: Option<Division>,
49
50    /// Price sheet number/identifier (Preisblattnummer)
51    #[serde(skip_serializing_if = "Option::is_none")]
52    #[cfg_attr(feature = "json-schema", schemars(rename = "preisblattnummer"))]
53    pub price_sheet_number: Option<String>,
54
55    /// Validity period (Gueltigkeitszeitraum)
56    #[serde(skip_serializing_if = "Option::is_none")]
57    #[cfg_attr(feature = "json-schema", schemars(rename = "gueltigkeitszeitraum"))]
58    pub validity_period: Option<TimePeriod>,
59
60    /// Valid from date (Gueltig ab)
61    #[serde(skip_serializing_if = "Option::is_none")]
62    #[cfg_attr(feature = "json-schema", schemars(rename = "gueltigAb"))]
63    pub valid_from: Option<DateTime<Utc>>,
64
65    /// Valid until date (Gueltig bis)
66    #[serde(skip_serializing_if = "Option::is_none")]
67    #[cfg_attr(feature = "json-schema", schemars(rename = "gueltigBis"))]
68    pub valid_until: Option<DateTime<Utc>>,
69
70    /// Hardware items with pricing (Hardware)
71    #[serde(default, skip_serializing_if = "Vec::is_empty")]
72    #[cfg_attr(feature = "json-schema", schemars(rename = "hardware"))]
73    pub hardware_items: Vec<Hardware>,
74
75    /// Installation price (Installationspreis)
76    #[serde(skip_serializing_if = "Option::is_none")]
77    #[cfg_attr(feature = "json-schema", schemars(rename = "installationspreis"))]
78    pub installation_price: Option<Price>,
79
80    /// Rental price per unit (Mietpreis)
81    #[serde(skip_serializing_if = "Option::is_none")]
82    #[cfg_attr(feature = "json-schema", schemars(rename = "mietpreis"))]
83    pub rental_price: Option<Price>,
84
85    /// Purchase price (Kaufpreis)
86    #[serde(skip_serializing_if = "Option::is_none")]
87    #[cfg_attr(feature = "json-schema", schemars(rename = "kaufpreis"))]
88    pub purchase_price: Option<Price>,
89
90    /// Hardware provider
91    #[serde(skip_serializing_if = "Option::is_none")]
92    #[cfg_attr(feature = "json-schema", schemars(rename = "hardwareanbieter"))]
93    pub provider: Option<Box<super::BusinessPartner>>,
94}
95
96impl Bo4eObject for HardwarePriceSheet {
97    fn type_name_german() -> &'static str {
98        "PreisblattHardware"
99    }
100
101    fn type_name_english() -> &'static str {
102        "HardwarePriceSheet"
103    }
104
105    fn meta(&self) -> &Bo4eMeta {
106        &self.meta
107    }
108
109    fn meta_mut(&mut self) -> &mut Bo4eMeta {
110        &mut self.meta
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117    use crate::com::Price;
118
119    #[test]
120    fn test_hardware_price_sheet_creation() {
121        let price_sheet = HardwarePriceSheet {
122            designation: Some("Hardware Preisblatt 2024".to_string()),
123            division: Some(Division::Electricity),
124            price_sheet_number: Some("HW-2024-001".to_string()),
125            installation_price: Some(Price::eur_per_month(5.0)),
126            ..Default::default()
127        };
128
129        assert_eq!(
130            price_sheet.designation,
131            Some("Hardware Preisblatt 2024".to_string())
132        );
133    }
134
135    #[test]
136    fn test_serialize() {
137        let price_sheet = HardwarePriceSheet {
138            meta: Bo4eMeta::with_type("PreisblattHardware"),
139            designation: Some("Meter Hardware".to_string()),
140            ..Default::default()
141        };
142
143        let json = serde_json::to_string(&price_sheet).unwrap();
144        assert!(json.contains(r#""designation":"Meter Hardware""#));
145        assert!(json.contains(r#""_typ":"PreisblattHardware""#));
146    }
147
148    #[test]
149    fn test_roundtrip() {
150        let price_sheet = HardwarePriceSheet {
151            meta: Bo4eMeta::with_type("PreisblattHardware"),
152            designation: Some("Smart Meter Prices".to_string()),
153            description: Some("Prices for smart meter hardware".to_string()),
154            division: Some(Division::Electricity),
155            price_sheet_number: Some("SM-2024".to_string()),
156            ..Default::default()
157        };
158
159        let json = serde_json::to_string(&price_sheet).unwrap();
160        let parsed: HardwarePriceSheet = serde_json::from_str(&json).unwrap();
161        assert_eq!(price_sheet, parsed);
162    }
163
164    #[test]
165    fn test_bo4e_object_impl() {
166        assert_eq!(HardwarePriceSheet::type_name_german(), "PreisblattHardware");
167        assert_eq!(
168            HardwarePriceSheet::type_name_english(),
169            "HardwarePriceSheet"
170        );
171    }
172}