bo4e_core/bo/
bundle_contract.rs1use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6use crate::com::TimePeriod;
7use crate::enums::{ContractStatus, Division};
8use crate::traits::{Bo4eMeta, Bo4eObject};
9
10#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
28#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
29#[cfg_attr(feature = "json-schema", schemars(rename = "Buendelvertrag"))]
30#[serde(rename_all = "camelCase")]
31pub struct BundleContract {
32 #[serde(flatten)]
34 pub meta: Bo4eMeta,
35
36 #[serde(skip_serializing_if = "Option::is_none")]
38 #[cfg_attr(feature = "json-schema", schemars(rename = "buendelvertragsnummer"))]
39 pub bundle_contract_number: Option<String>,
40
41 #[serde(skip_serializing_if = "Option::is_none")]
43 #[cfg_attr(feature = "json-schema", schemars(rename = "beschreibung"))]
44 pub description: Option<String>,
45
46 #[serde(skip_serializing_if = "Option::is_none")]
48 #[cfg_attr(feature = "json-schema", schemars(rename = "vertragsstatus"))]
49 pub status: Option<ContractStatus>,
50
51 #[serde(skip_serializing_if = "Option::is_none")]
53 #[cfg_attr(feature = "json-schema", schemars(rename = "sparte"))]
54 pub division: Option<Division>,
55
56 #[serde(skip_serializing_if = "Option::is_none")]
58 #[cfg_attr(feature = "json-schema", schemars(rename = "vertragsbeginn"))]
59 pub contract_start: Option<DateTime<Utc>>,
60
61 #[serde(skip_serializing_if = "Option::is_none")]
63 #[cfg_attr(feature = "json-schema", schemars(rename = "vertragsende"))]
64 pub contract_end: Option<DateTime<Utc>>,
65
66 #[serde(skip_serializing_if = "Option::is_none")]
68 #[cfg_attr(feature = "json-schema", schemars(rename = "gueltigkeitszeitraum"))]
69 pub validity_period: Option<TimePeriod>,
70
71 #[serde(default, skip_serializing_if = "Vec::is_empty")]
73 #[cfg_attr(feature = "json-schema", schemars(rename = "einzelvertraege"))]
74 pub individual_contracts: Vec<Box<super::Contract>>,
75
76 #[serde(skip_serializing_if = "Option::is_none")]
78 #[cfg_attr(feature = "json-schema", schemars(rename = "vertragspartner"))]
79 pub contract_partner: Option<Box<super::BusinessPartner>>,
80}
81
82impl Bo4eObject for BundleContract {
83 fn type_name_german() -> &'static str {
84 "Buendelvertrag"
85 }
86
87 fn type_name_english() -> &'static str {
88 "BundleContract"
89 }
90
91 fn meta(&self) -> &Bo4eMeta {
92 &self.meta
93 }
94
95 fn meta_mut(&mut self) -> &mut Bo4eMeta {
96 &mut self.meta
97 }
98}
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103 use crate::bo::Contract;
104 use crate::enums::ContractType;
105
106 #[test]
107 fn test_bundle_contract_creation() {
108 let bundle = BundleContract {
109 bundle_contract_number: Some("BV-2024-001".to_string()),
110 status: Some(ContractStatus::Active),
111 division: Some(Division::Electricity),
112 ..Default::default()
113 };
114
115 assert_eq!(bundle.status, Some(ContractStatus::Active));
116 }
117
118 #[test]
119 fn test_bundle_with_contracts() {
120 let contract1 = Box::new(Contract {
121 contract_number: Some("V-001".to_string()),
122 contract_type: Some(ContractType::EnergySupplyContract),
123 ..Default::default()
124 });
125
126 let contract2 = Box::new(Contract {
127 contract_number: Some("V-002".to_string()),
128 contract_type: Some(ContractType::NetworkUsageContract),
129 ..Default::default()
130 });
131
132 let bundle = BundleContract {
133 bundle_contract_number: Some("BV-2024-001".to_string()),
134 individual_contracts: vec![contract1, contract2],
135 ..Default::default()
136 };
137
138 assert_eq!(bundle.individual_contracts.len(), 2);
139 }
140
141 #[test]
142 fn test_serialize() {
143 let bundle = BundleContract {
144 meta: Bo4eMeta::with_type("Buendelvertrag"),
145 bundle_contract_number: Some("BV-123".to_string()),
146 status: Some(ContractStatus::Active),
147 ..Default::default()
148 };
149
150 let json = serde_json::to_string(&bundle).unwrap();
151 assert!(json.contains(r#""bundleContractNumber":"BV-123""#));
152 }
153
154 #[test]
155 fn test_roundtrip() {
156 let bundle = BundleContract {
157 meta: Bo4eMeta::with_type("Buendelvertrag"),
158 bundle_contract_number: Some("BV-123".to_string()),
159 description: Some("Test bundle".to_string()),
160 status: Some(ContractStatus::Active),
161 division: Some(Division::Electricity),
162 ..Default::default()
163 };
164
165 let json = serde_json::to_string(&bundle).unwrap();
166 let parsed: BundleContract = serde_json::from_str(&json).unwrap();
167 assert_eq!(bundle, parsed);
168 }
169
170 #[test]
171 fn test_bo4e_object_impl() {
172 assert_eq!(BundleContract::type_name_german(), "Buendelvertrag");
173 assert_eq!(BundleContract::type_name_english(), "BundleContract");
174 }
175}