1
//! `load-form "transaction-edit"`: the stored transaction as submit payload
2
//! values with hidden row data.
3

            
4
use chrono::SecondsFormat;
5
use finance::price::Price;
6
use finance::tag::Tag;
7
use forms::{FormValues, Row};
8
use server::logical::RaisedRow;
9
use uuid::Uuid;
10

            
11
use super::hidden::Hidden;
12
use super::transaction_rows::to_commodity_cell;
13
use super::transaction_split_tags::split_tag_rows;
14
use super::transaction_stored::{Stored, load_stored};
15
use super::{Entity, Failure};
16
use crate::resolve::ResolvedIds;
17

            
18
12115
pub(super) async fn load_transaction_edit(user_id: Uuid, id: &str) -> Result<FormValues, Failure> {
19
12115
    let transaction_id = Uuid::parse_str(id.trim()).map_err(|_| Failure::MalformedId {
20
324
        entity: Entity::Transaction,
21
324
        id: id.to_string(),
22
324
    })?;
23
11791
    let stored = load_stored(user_id, transaction_id)
24
11791
        .await?
25
11138
        .ok_or(Failure::Missing(Entity::Transaction))?;
26
10815
    Ok(edit_values(&stored))
27
12115
}
28

            
29
/// The commodity price of the row's split pair carries its observation date.
30
15474
fn price_date(prices: &[Price], raised: &RaisedRow) -> Option<chrono::DateTime<chrono::Utc>> {
31
15474
    prices
32
15474
        .iter()
33
12822
        .find(|price| {
34
12822
            price.currency_split == Some(raised.from_split)
35
8497
                && price.commodity_split == Some(raised.to_split)
36
12822
        })
37
8497
        .map(|price| price.date)
38
15474
}
39

            
40
15474
fn hidden_of(raised: &RaisedRow, prices: &[Price]) -> Hidden {
41
15474
    Hidden {
42
15474
        ids: ResolvedIds {
43
15474
            from: raised.from_account,
44
15474
            to: raised.to_account,
45
15474
            from_commodity: raised.from_commodity,
46
15474
            to_commodity: raised.to_commodity,
47
15474
        },
48
15474
        value: raised.value,
49
15474
        to_amount: raised.to_amount,
50
15474
        price_date: price_date(prices, raised),
51
15474
        reconcile: [raised.from_reconcile, raised.to_reconcile],
52
15474
    }
53
15474
}
54

            
55
15474
fn row_values(key: &str, raised: &RaisedRow, prices: &[Price]) -> Row {
56
15474
    let hidden = hidden_of(raised, prices);
57
15474
    let mut row = Row::from([
58
15474
        ("key".to_string(), key.to_string()),
59
15474
        ("from".to_string(), raised.from_account.to_string()),
60
15474
        ("to".to_string(), raised.to_account.to_string()),
61
15474
        (
62
15474
            "from-commodity".to_string(),
63
15474
            raised.from_commodity.to_string(),
64
15474
        ),
65
15474
        (
66
15474
            "to-commodity".to_string(),
67
15474
            to_commodity_cell(&raised.from_commodity, &raised.to_commodity),
68
15474
        ),
69
15474
        (
70
15474
            "value".to_string(),
71
15474
            amount::display(*hidden.value.numer(), *hidden.value.denom()),
72
15474
        ),
73
15474
        ("hidden".to_string(), hidden.to_sexpr()),
74
15474
    ]);
75
15474
    if let Some(to_amount) = hidden.to_amount {
76
8497
        row.insert(
77
8497
            "to-amount".to_string(),
78
8497
            amount::display(*to_amount.numer(), *to_amount.denom()),
79
8497
        );
80
6977
    }
81
15474
    row
82
15474
}
83

            
84
36703
fn tag_row(tag: &Tag) -> Row {
85
36703
    let mut row = Row::from([
86
36703
        ("name".to_string(), tag.normalized().0.to_string()),
87
36703
        ("value".to_string(), tag.normalized().1.to_string()),
88
36703
    ]);
89
36703
    if let Some(description) = &tag.description {
90
27239
        row.insert("description".to_string(), description.clone());
91
9464
    }
92
36703
    row
93
36703
}
94

            
95
10815
pub(super) fn edit_values(stored: &Stored) -> FormValues {
96
10815
    let transaction = &stored.detail.transaction;
97
10815
    let mut values = FormValues::default();
98
10815
    values.set("id", &transaction.id.to_string());
99
10815
    values.set("revision", &stored.revision);
100
10815
    values.set(
101
10815
        "date",
102
10815
        &transaction
103
10815
            .post_date
104
10815
            .to_rfc3339_opts(SecondsFormat::AutoSi, true),
105
10815
    );
106
10815
    values.set("note", stored.detail.note.as_deref().unwrap_or_default());
107
10815
    values.set_rows("tags");
108
10815
    for tag in &stored.tags {
109
23553
        values.push_row("tags", tag_row(tag));
110
    }
111
10815
    values.set_rows("split-tags");
112
10815
    for (index, (raised, tags)) in stored.rows.iter().enumerate() {
113
15474
        let key = format!("r{}", index + 1);
114
15474
        let row = row_values(&key, raised, &stored.detail.prices);
115
15474
        values.push_row("splits", row);
116
15474
        for row in split_tag_rows(&key, tags, tag_row) {
117
13150
            values.push_row("split-tags", row);
118
        }
119
    }
120
10815
    values
121
10815
}