use serde::{Deserialize, Serialize};
use crate::com::{Amount, CostBlock, Price, TimePeriod};
use crate::enums::Division;
use crate::traits::{Bo4eMeta, Bo4eObject};
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "json-schema", schemars(rename = "Tarifkosten"))]
#[serde(rename_all = "camelCase")]
pub struct TariffCosts {
#[serde(flatten)]
pub meta: Bo4eMeta,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "bezeichnung"))]
pub designation: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "beschreibung"))]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "sparte"))]
pub division: Option<Division>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "abrechnungszeitraum"))]
pub period: Option<TimePeriod>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "gesamtbetrag"))]
pub total_amount: Option<Amount>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "grundpreis"))]
pub base_price: Option<Price>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "grundpreiskosten"))]
pub base_price_cost: Option<Amount>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "arbeitspreis"))]
pub working_price: Option<Price>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "arbeitspreiskosten"))]
pub working_price_cost: Option<Amount>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "verbrauchsmenge"))]
pub consumption: Option<f64>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[cfg_attr(feature = "json-schema", schemars(rename = "kostenbloecke"))]
pub cost_blocks: Vec<CostBlock>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "tarif"))]
pub tariff: Option<Box<super::Tariff>>,
}
impl Bo4eObject for TariffCosts {
fn type_name_german() -> &'static str {
"Tarifkosten"
}
fn type_name_english() -> &'static str {
"TariffCosts"
}
fn meta(&self) -> &Bo4eMeta {
&self.meta
}
fn meta_mut(&mut self) -> &mut Bo4eMeta {
&mut self.meta
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tariff_costs_creation() {
let tariff_costs = TariffCosts {
designation: Some("Haushaltstarif Kosten".to_string()),
division: Some(Division::Electricity),
base_price: Some(Price::eur_per_month(9.95)),
base_price_cost: Some(Amount::eur(119.40)),
working_price: Some(Price::eur_per_kwh(0.32)),
working_price_cost: Some(Amount::eur(960.00)),
consumption: Some(3000.0),
total_amount: Some(Amount::eur(1079.40)),
..Default::default()
};
assert_eq!(tariff_costs.consumption, Some(3000.0));
}
#[test]
fn test_serialize() {
let tariff_costs = TariffCosts {
meta: Bo4eMeta::with_type("Tarifkosten"),
designation: Some("Test Tariff Costs".to_string()),
total_amount: Some(Amount::eur(500.0)),
..Default::default()
};
let json = serde_json::to_string(&tariff_costs).unwrap();
assert!(json.contains(r#""designation":"Test Tariff Costs""#));
assert!(json.contains(r#""_typ":"Tarifkosten""#));
}
#[test]
fn test_roundtrip() {
let tariff_costs = TariffCosts {
meta: Bo4eMeta::with_type("Tarifkosten"),
designation: Some("Gas Tariff Costs".to_string()),
description: Some("Annual gas tariff costs".to_string()),
division: Some(Division::Gas),
consumption: Some(15000.0),
total_amount: Some(Amount::eur(1500.0)),
..Default::default()
};
let json = serde_json::to_string(&tariff_costs).unwrap();
let parsed: TariffCosts = serde_json::from_str(&json).unwrap();
assert_eq!(tariff_costs, parsed);
}
#[test]
fn test_bo4e_object_impl() {
assert_eq!(TariffCosts::type_name_german(), "Tarifkosten");
assert_eq!(TariffCosts::type_name_english(), "TariffCosts");
}
}