Skip to main content

bo4e_core/com/
discount.rs

1//! Discount (Rabatt) component.
2
3use serde::{Deserialize, Serialize};
4
5use crate::enums::{Currency, SurchargeType};
6use crate::traits::{Bo4eMeta, Bo4eObject};
7
8/// A discount applied to a price.
9///
10/// German: Rabatt
11///
12/// # Example
13///
14/// ```rust
15/// use bo4e_core::com::Discount;
16///
17/// let discount = Discount {
18///     description: Some("Stammkundenrabatt".to_string()),
19///     value: Some(10.0),
20///     ..Default::default()
21/// };
22/// ```
23#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
24#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
25#[cfg_attr(feature = "json-schema", schemars(rename = "Rabatt"))]
26#[serde(rename_all = "camelCase")]
27pub struct Discount {
28    /// BO4E metadata
29    #[serde(flatten)]
30    pub meta: Bo4eMeta,
31
32    /// Description/name of the discount (Bezeichnung)
33    #[serde(skip_serializing_if = "Option::is_none")]
34    #[cfg_attr(feature = "json-schema", schemars(rename = "bezeichnung"))]
35    pub description: Option<String>,
36
37    /// Type of discount (Rabatttyp)
38    #[serde(skip_serializing_if = "Option::is_none")]
39    #[cfg_attr(feature = "json-schema", schemars(rename = "rabatttyp"))]
40    pub discount_type: Option<SurchargeType>,
41
42    /// Discount value (Wert)
43    #[serde(skip_serializing_if = "Option::is_none")]
44    #[cfg_attr(feature = "json-schema", schemars(rename = "wert"))]
45    pub value: Option<f64>,
46
47    /// Currency (Waehrung)
48    #[serde(skip_serializing_if = "Option::is_none")]
49    #[cfg_attr(feature = "json-schema", schemars(rename = "waehrung"))]
50    pub currency: Option<Currency>,
51
52    /// Conditions for the discount (Bedingungen)
53    #[serde(skip_serializing_if = "Option::is_none")]
54    #[cfg_attr(feature = "json-schema", schemars(rename = "bedingungen"))]
55    pub conditions: Option<String>,
56}
57
58impl Bo4eObject for Discount {
59    fn type_name_german() -> &'static str {
60        "Rabatt"
61    }
62
63    fn type_name_english() -> &'static str {
64        "Discount"
65    }
66
67    fn meta(&self) -> &Bo4eMeta {
68        &self.meta
69    }
70
71    fn meta_mut(&mut self) -> &mut Bo4eMeta {
72        &mut self.meta
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_percentage_discount() {
82        let discount = Discount {
83            description: Some("10% Rabatt".to_string()),
84            discount_type: Some(SurchargeType::Relative),
85            value: Some(10.0),
86            ..Default::default()
87        };
88
89        assert_eq!(discount.discount_type, Some(SurchargeType::Relative));
90        assert_eq!(discount.value, Some(10.0));
91    }
92
93    #[test]
94    fn test_absolute_discount() {
95        let discount = Discount {
96            description: Some("50 EUR Rabatt".to_string()),
97            discount_type: Some(SurchargeType::Absolute),
98            value: Some(50.0),
99            currency: Some(Currency::Eur),
100            ..Default::default()
101        };
102
103        assert_eq!(discount.discount_type, Some(SurchargeType::Absolute));
104    }
105
106    #[test]
107    fn test_default() {
108        let discount = Discount::default();
109        assert!(discount.description.is_none());
110        assert!(discount.value.is_none());
111    }
112
113    #[test]
114    fn test_roundtrip() {
115        let discount = Discount {
116            description: Some("Frühbucherrabatt".to_string()),
117            discount_type: Some(SurchargeType::Relative),
118            value: Some(15.0),
119            conditions: Some("Bei Buchung bis 31.12.".to_string()),
120            ..Default::default()
121        };
122
123        let json = serde_json::to_string(&discount).unwrap();
124        let parsed: Discount = serde_json::from_str(&json).unwrap();
125        assert_eq!(discount, parsed);
126    }
127
128    #[test]
129    fn test_bo4e_object_impl() {
130        assert_eq!(Discount::type_name_german(), "Rabatt");
131        assert_eq!(Discount::type_name_english(), "Discount");
132    }
133}