grid_tariffs/
price_list.rs

1use chrono::NaiveDate;
2use serde::Serialize;
3
4use crate::{
5    FeedInRevenueSimplified, Money, TransferFeeSimplified, costs::Cost, fees::TransferFee, helpers,
6    power_tariffs::PowerTariff, revenues::FeedInRevenue,
7};
8
9#[derive(Debug, Clone, Serialize)]
10#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
11pub struct PriceList {
12    variant: Option<&'static str>,
13    from_date: NaiveDate,
14    /// Fixed monthly fee
15    monthly_fee: Cost,
16    /// Fixed monthly fee for allowing energy production
17    monthly_production_fee: Cost,
18    transfer_fee: TransferFee,
19    feed_in_revenue: FeedInRevenue,
20    power_tariff: PowerTariff,
21}
22
23impl PriceList {
24    pub(crate) const fn builder() -> PriceListBuilder {
25        PriceListBuilder::new()
26    }
27
28    pub const fn variant(&self) -> Option<&'static str> {
29        self.variant
30    }
31
32    pub const fn from_date(&self) -> NaiveDate {
33        self.from_date
34    }
35
36    pub const fn monthly_fee(&self) -> &Cost {
37        &self.monthly_fee
38    }
39
40    pub const fn monthly_production_fee(&self) -> &Cost {
41        &self.monthly_production_fee
42    }
43
44    pub const fn transfer_fee(&self) -> &TransferFee {
45        &self.transfer_fee
46    }
47
48    pub const fn feed_in_revenue(&self) -> &FeedInRevenue {
49        &self.feed_in_revenue
50    }
51
52    pub const fn power_tariff(&self) -> &PowerTariff {
53        &self.power_tariff
54    }
55
56    pub fn simplified(&self, fuse_size: u16, yearly_consumption: u32) -> PriceListSimplified {
57        PriceListSimplified::new(self, fuse_size, yearly_consumption)
58    }
59}
60
61#[derive(Debug, Clone)]
62pub(crate) struct PriceListBuilder {
63    variant: Option<&'static str>,
64    from_date: Option<NaiveDate>,
65    /// Fixed monthly fee
66    monthly_fee: Option<Cost>,
67    /// Fixed monthly fee for allowing energy production
68    monthly_production_fee: Option<Cost>,
69    transfer_fee: Option<TransferFee>,
70    feed_in_revenue: Option<FeedInRevenue>,
71    power_tariff: Option<PowerTariff>,
72}
73
74impl PriceListBuilder {
75    pub(crate) const fn new() -> Self {
76        Self {
77            variant: None,
78            from_date: None,
79            monthly_fee: None,
80            monthly_production_fee: None,
81            transfer_fee: None,
82            feed_in_revenue: None,
83            power_tariff: None,
84        }
85    }
86
87    pub(crate) const fn build(self) -> PriceList {
88        PriceList {
89            variant: self.variant,
90            from_date: self.from_date.expect("`from_date` required"),
91            monthly_fee: self.monthly_fee.expect("`monthly_fee` required"),
92            monthly_production_fee: self
93                .monthly_production_fee
94                .expect("`monthly_production_fee` required"),
95            transfer_fee: self.transfer_fee.expect("`transfer_fee` required"),
96            feed_in_revenue: self.feed_in_revenue.expect("`feed_in_revenue` required"),
97            power_tariff: self.power_tariff.expect("`grid_tariff` required"),
98        }
99    }
100
101    pub(crate) const fn variant(mut self, name: &'static str) -> Self {
102        self.variant = Some(name);
103        self
104    }
105
106    pub(crate) const fn from_date(mut self, year: i32, month: u32, day: u32) -> Self {
107        self.from_date = Some(helpers::date(year, month, day));
108        self
109    }
110
111    pub(crate) const fn monthly_fee(mut self, monthly_fee: Cost) -> Self {
112        self.monthly_fee = Some(monthly_fee);
113        self
114    }
115
116    pub(crate) const fn monthly_production_fee(mut self, monthly_production_fee: Cost) -> Self {
117        self.monthly_production_fee = Some(monthly_production_fee);
118        self
119    }
120
121    pub(crate) const fn transfer_fee(mut self, transfer_fee: TransferFee) -> Self {
122        self.transfer_fee = Some(transfer_fee);
123        self
124    }
125
126    pub(crate) const fn feed_in_revenue(mut self, feed_in_revenue: FeedInRevenue) -> Self {
127        self.feed_in_revenue = Some(feed_in_revenue);
128        self
129    }
130
131    pub(crate) const fn power_tariff(mut self, power_tariff: PowerTariff) -> Self {
132        self.power_tariff = Some(power_tariff);
133        self
134    }
135}
136
137#[derive(Debug, Clone, Serialize)]
138#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
139pub struct PriceListSimplified {
140    variant: Option<&'static str>,
141    fuse_size: u16,
142    /// If the energy consumption of the household affects the costs and/or fees for this fuse size
143    yearly_consumption_based: bool,
144    from_date: NaiveDate,
145    /// Fixed monthly fee
146    monthly_fee: Option<Money>,
147    /// Fixed monthly fee for allowing energy production
148    monthly_production_fee: Option<Money>,
149    transfer_fee: TransferFeeSimplified,
150    feed_in_revenue: FeedInRevenueSimplified,
151    power_tariff: PowerTariff,
152}
153
154impl PriceListSimplified {
155    fn new(pl: &PriceList, fuse_size: u16, yearly_consumption: u32) -> Self {
156        Self {
157            variant: pl.variant,
158            fuse_size,
159            yearly_consumption_based: pl.monthly_fee.is_yearly_consumption_based(fuse_size)
160                || pl
161                    .monthly_production_fee
162                    .is_yearly_consumption_based(fuse_size)
163                || pl.transfer_fee.is_yearly_consumption_based(fuse_size),
164            from_date: pl.from_date,
165            monthly_fee: pl.monthly_fee.cost_for(fuse_size, yearly_consumption),
166            monthly_production_fee: pl
167                .monthly_production_fee
168                .cost_for(fuse_size, yearly_consumption),
169            transfer_fee: pl.transfer_fee.simplified(fuse_size, yearly_consumption),
170            feed_in_revenue: pl.feed_in_revenue.simplified(fuse_size, yearly_consumption),
171            power_tariff: pl.power_tariff.to_owned(),
172        }
173    }
174}