grid_tariffs/
price_list.rs

1use chrono::{DateTime, NaiveDate, TimeZone};
2use serde::Serialize;
3
4use crate::{
5    FeedInRevenueSimplified, Language, Money, PowerTariff, PowerTariffSimplified,
6    TransferFeeSimplified, costs::Cost, feed_in_revenue::FeedInRevenue, helpers,
7    transfer_fee::TransferFee,
8};
9
10#[derive(Debug, Clone, Serialize)]
11#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
12pub struct PriceList {
13    variant: Option<&'static str>,
14    from_date: NaiveDate,
15    /// Fixed monthly fee
16    monthly_fee: Cost,
17    /// Fixed monthly fee for allowing energy production
18    monthly_production_fee: Cost,
19    transfer_fee: TransferFee,
20    feed_in_revenue: FeedInRevenue,
21    power_tariff: PowerTariff,
22}
23
24impl PriceList {
25    pub(crate) const fn builder() -> PriceListBuilder {
26        PriceListBuilder::new()
27    }
28
29    pub const fn variant(&self) -> Option<&'static str> {
30        self.variant
31    }
32
33    pub const fn from_date(&self) -> NaiveDate {
34        self.from_date
35    }
36
37    pub const fn monthly_fee(&self) -> &Cost {
38        &self.monthly_fee
39    }
40
41    pub const fn monthly_production_fee(&self) -> &Cost {
42        &self.monthly_production_fee
43    }
44
45    pub const fn transfer_fee(&self) -> &TransferFee {
46        &self.transfer_fee
47    }
48
49    pub const fn feed_in_revenue(&self) -> &FeedInRevenue {
50        &self.feed_in_revenue
51    }
52
53    pub const fn power_tariff(&self) -> &PowerTariff {
54        &self.power_tariff
55    }
56
57    /// Total fee per kWh
58    ///
59    /// Please note that taxes are not included.
60    ///
61    pub fn kwh_fee<Tz: TimeZone>(
62        &self,
63        timestamp: DateTime<Tz>,
64        spotprice: Money,
65        fuse_size: u16,
66        yearly_consumption: u32,
67    ) -> Money
68    where
69        DateTime<Tz>: Copy,
70    {
71        self.transfer_fee()
72            .kwh_cost(timestamp, spotprice, fuse_size, yearly_consumption)
73    }
74
75    /// Total revenue per kWh
76    ///
77    /// Please note that tax reductions are not included.
78    ///
79    pub fn kwh_revenue<Tz: TimeZone>(
80        &self,
81        timestamp: DateTime<Tz>,
82        spotprice: Money,
83        fuse_size: u16,
84        yearly_consumption: u32,
85    ) -> Money
86    where
87        DateTime<Tz>: Copy,
88    {
89        self.feed_in_revenue()
90            .kwh_revenue(timestamp, spotprice, fuse_size, yearly_consumption)
91    }
92
93    pub fn simplified(
94        &self,
95        fuse_size: u16,
96        yearly_consumption: u32,
97        language: Language,
98    ) -> PriceListSimplified {
99        PriceListSimplified::new(self, fuse_size, yearly_consumption, language)
100    }
101}
102
103#[derive(Debug, Clone)]
104pub(crate) struct PriceListBuilder {
105    variant: Option<&'static str>,
106    from_date: Option<NaiveDate>,
107    /// Fixed monthly fee
108    monthly_fee: Option<Cost>,
109    /// Fixed monthly fee for allowing energy production
110    monthly_production_fee: Option<Cost>,
111    transfer_fee: Option<TransferFee>,
112    feed_in_revenue: Option<FeedInRevenue>,
113    power_tariff: Option<PowerTariff>,
114}
115
116impl PriceListBuilder {
117    pub(crate) const fn new() -> Self {
118        Self {
119            variant: None,
120            from_date: None,
121            monthly_fee: None,
122            monthly_production_fee: None,
123            transfer_fee: None,
124            feed_in_revenue: None,
125            power_tariff: None,
126        }
127    }
128
129    pub(crate) const fn build(self) -> PriceList {
130        PriceList {
131            variant: self.variant,
132            from_date: self.from_date.expect("`from_date` required"),
133            monthly_fee: self.monthly_fee.expect("`monthly_fee` required"),
134            monthly_production_fee: self
135                .monthly_production_fee
136                .expect("`monthly_production_fee` required"),
137            transfer_fee: self.transfer_fee.expect("`transfer_fee` required"),
138            feed_in_revenue: self.feed_in_revenue.expect("`feed_in_revenue` required"),
139            power_tariff: self.power_tariff.expect("`grid_tariff` required"),
140        }
141    }
142
143    pub(crate) const fn variant(mut self, name: &'static str) -> Self {
144        self.variant = Some(name);
145        self
146    }
147
148    #[allow(clippy::wrong_self_convention)]
149    pub(crate) const fn from_date(mut self, year: i32, month: u32, day: u32) -> Self {
150        self.from_date = Some(helpers::date(year, month, day));
151        self
152    }
153
154    pub(crate) const fn monthly_fee(mut self, monthly_fee: Cost) -> Self {
155        self.monthly_fee = Some(monthly_fee);
156        self
157    }
158
159    pub(crate) const fn monthly_production_fee(mut self, monthly_production_fee: Cost) -> Self {
160        self.monthly_production_fee = Some(monthly_production_fee);
161        self
162    }
163
164    pub(crate) const fn transfer_fee(mut self, transfer_fee: TransferFee) -> Self {
165        self.transfer_fee = Some(transfer_fee);
166        self
167    }
168
169    pub(crate) const fn feed_in_revenue(mut self, feed_in_revenue: FeedInRevenue) -> Self {
170        self.feed_in_revenue = Some(feed_in_revenue);
171        self
172    }
173
174    pub(crate) const fn power_tariff(mut self, power_tariff: PowerTariff) -> Self {
175        self.power_tariff = Some(power_tariff);
176        self
177    }
178}
179
180#[derive(Debug, Clone, Serialize)]
181#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
182pub struct PriceListSimplified {
183    variant: Option<&'static str>,
184    fuse_size: u16,
185    /// If the energy consumption of the household affects the costs and/or fees for this fuse size
186    yearly_consumption_based: bool,
187    from_date: NaiveDate,
188    /// Fixed monthly fee
189    monthly_fee: Option<Money>,
190    /// Fixed monthly fee for allowing energy production
191    monthly_production_fee: Option<Money>,
192    transfer_fee: TransferFeeSimplified,
193    feed_in_revenue: FeedInRevenueSimplified,
194    power_tariff: PowerTariffSimplified,
195}
196
197impl PriceListSimplified {
198    fn new(pl: &PriceList, fuse_size: u16, yearly_consumption: u32, language: Language) -> Self {
199        Self {
200            variant: pl.variant,
201            fuse_size,
202            yearly_consumption_based: pl.monthly_fee.is_yearly_consumption_based(fuse_size)
203                || pl
204                    .monthly_production_fee
205                    .is_yearly_consumption_based(fuse_size)
206                || pl.transfer_fee.is_yearly_consumption_based(fuse_size),
207            from_date: pl.from_date,
208            monthly_fee: pl.monthly_fee.cost_for(fuse_size, yearly_consumption),
209            monthly_production_fee: pl
210                .monthly_production_fee
211                .cost_for(fuse_size, yearly_consumption),
212            transfer_fee: pl
213                .transfer_fee
214                .simplified(fuse_size, yearly_consumption, language),
215            feed_in_revenue: pl
216                .feed_in_revenue
217                .simplified(fuse_size, yearly_consumption, language),
218            power_tariff: pl
219                .power_tariff
220                .simplified(fuse_size, yearly_consumption, language),
221        }
222    }
223}