tackler_core/export/
equity_exporter.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Copyright 2024 E257.FI
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

use crate::export::Export;
use crate::kernel::balance::Balance;
use crate::kernel::report_item_selector::{
    BalanceNonZeroByAccountSelector, BalanceNonZeroSelector, BalanceSelector,
};
use crate::kernel::Settings;
use crate::model::{Transaction, TxnSet};
use itertools::Itertools;
use rust_decimal::Decimal;
use std::error::Error;
use std::io;
use tackler_api::metadata::items::{AccountSelectorChecksum, Text};
use time::format_description::well_known::Rfc3339;
use uuid::Uuid;

#[derive(Debug, Clone)]
pub struct EquitySettings {
    pub eqa: Option<String>,
    pub ras: Vec<String>,
}

impl EquitySettings {
    pub fn from(settings: &Settings) -> Result<EquitySettings, Box<dyn Error>> {
        let bs = EquitySettings {
            eqa: Some(settings.export.equity.equity_account.clone()),
            ras: settings.get_equity_ras(),
        };
        Ok(bs)
    }
}

#[derive(Debug, Clone)]
pub struct EquityExporter {
    pub export_settings: EquitySettings,
}

impl EquityExporter {
    fn get_acc_selector(&self) -> Result<Box<dyn BalanceSelector>, Box<dyn Error>> {
        let v = &self.export_settings.ras;
        if v.is_empty() {
            Ok(Box::new(BalanceNonZeroSelector {}))
        } else {
            let s: Vec<_> = v.iter().map(|s| s.as_str()).collect();
            let ras = BalanceNonZeroByAccountSelector::from(&s)?;

            Ok(Box::new(ras))
        }
    }
}

impl Export for EquityExporter {
    fn write_export<W: io::Write + ?Sized>(
        &self,
        cfg: &Settings,
        writer: &mut W,
        txn_data: &TxnSet,
    ) -> Result<(), Box<dyn Error>> {
        let bal_acc_sel = self.get_acc_selector()?;

        let bal = Balance::from(&String::default(), txn_data, bal_acc_sel.as_ref(), cfg)?;

        if bal.is_empty() {
            // todo: check if this is actually possible?
            return Ok(());
        };

        let eq_txn_indent = "   ";
        let equity_account = "Equity:Default·Account".to_string();

        let hdr_str = |last_txn: Option<&&Transaction>, c: &String| -> String {
            let comm_str = || -> String {
                if c.is_empty() {
                    String::default()
                } else {
                    format!(" for {}", c)
                }
            };
            let txn_uuid_str = |uuid: Option<Uuid>| -> String {
                match uuid {
                    Some(u) => format!(": last txn (uuid): {}", u),
                    None => String::default(),
                }
            };
            match last_txn {
                Some(txn) => {
                    format!(
                        "{} 'Equity{}{}",
                        txn.header.timestamp.format(&Rfc3339).unwrap(/*:ok: predefined frmt string*/),
                        comm_str(),
                        txn_uuid_str(txn.header.uuid)
                    )
                }
                _ => {
                    "Internal logic error".to_string() // todo: fix this
                }
            }
        };

        let last_txn = txn_data.txns.last();

        let acc_sel_checksum = match cfg.get_hash() {
            Some(hash) => Some(AccountSelectorChecksum {
                hash: bal_acc_sel.checksum(hash)?,
            }),
            None => None,
        };

        let equity_txn_str: Vec<String> = bal
            .bal
            .iter()
            .chunk_by(|btn| &btn.acctn.comm.name)
            .into_iter()
            .flat_map(|(c, bs)| {
                let btns: Vec<_> = bs.collect();
                let dsum: Decimal = btns.clone().into_iter().map(|b| b.account_sum).sum();
                let bal_posting = {
                    let value = if c.is_empty() {
                        format!("{}", -dsum)
                    } else {
                        format!("{} {}", -dsum, c)
                    };
                    let ea = match &self.export_settings.eqa {
                        Some(eqa) => eqa,
                        None => &equity_account,
                    };
                    format!("{}{}  {}", eq_txn_indent, ea, value)
                };
                /*
                 * equity transaction per commodity
                 */
                let eq_postings = btns
                    .into_iter()
                    .map(|b| {
                        let comm = &b.acctn.comm;
                        format!(
                            "{}{}  {}{}",
                            eq_txn_indent,
                            b.acctn.atn.account,
                            b.account_sum,
                            match comm.is_some() {
                                true => { format!(" {}", comm.name) },
                                false => String::new(),
                            }
                        )
                    })
                    .collect::<Vec<String>>();

                let mut eq_txn = Vec::<String>::new();

                eq_txn.push(hdr_str(last_txn, c));
                if let Some(md) = &txn_data.metadata {
                        for mdi in md.items.clone() {
                            eq_txn.extend(mdi.text().iter().map(|v| {
                                format!("{}; {}", eq_txn_indent, v)
                            }).collect::<Vec<_>>());
                            eq_txn.push(format!("{}; ", eq_txn_indent));
                        }

                        if let Some(asc) = &acc_sel_checksum {
                            for v in asc.text() {
                                eq_txn.push(format!("{}; {}", eq_txn_indent, &v));
                            }
                            eq_txn.push(format!("{}; ", eq_txn_indent));
                        }
                };
                if dsum.is_zero() {
                    eq_txn.push(format!("{}; WARNING:", eq_txn_indent));
                    eq_txn.push(format!("{}; WARNING: The sum of equity transaction is zero without equity account.", eq_txn_indent));
                    eq_txn.push(format!("{}; WARNING: Therefore there is no equity posting row, and this is probably not right.", eq_txn_indent));
                    eq_txn.push(format!("{}; WARNING: Is the account selector correct for this Equity export?", eq_txn_indent));
                    eq_txn.push(format!("{}; WARNING:", eq_txn_indent));
                }

                eq_txn.extend(eq_postings);
                if !dsum.is_zero() {
                    eq_txn.push(bal_posting);
                }
                eq_txn.push("".to_string());
                eq_txn
            })
            .collect();

        for i in equity_txn_str {
            writeln!(writer, "{}", i)?;
        }

        Ok(())
    }
}