1
//! `transaction create --template`: the template's draft with the given values over it.
2

            
3
use forms::{FormValues, TRANSACTION_CREATE, now_instant};
4
use rpc::Session;
5

            
6
use super::{apply_form, eval_text, submit_call};
7
use crate::CommandError;
8
use crate::flat::load_values;
9
use crate::forms::map_references;
10

            
11
/// Create a transaction from the draft of template `template_id`; `overrides`
12
/// replace its date and note and merge into its rows, and the date is now
13
/// when neither gives one.
14
///
15
/// # Errors
16
/// The server's refusal of the template load, [`CommandError::Argument`] for
17
/// an unknown or ambiguous reference, or [`CommandError::FormInvalid`] from the submit.
18
5
pub async fn create_from_template(
19
5
    session: &mut Session,
20
5
    template_id: &str,
21
5
    overrides: &FormValues,
22
5
) -> Result<String, CommandError> {
23
5
    let loaded = load_values(session, &TRANSACTION_CREATE, template_id).await?;
24
4
    let overrides = map_references(session, &TRANSACTION_CREATE, overrides).await?;
25
4
    let mut merged = apply_form(&TRANSACTION_CREATE, &loaded, &overrides, &[])?;
26
4
    if merged.text("date").is_none() {
27
1
        merged.set("date", &now_instant());
28
3
    }
29
4
    eval_text(session, &submit_call(&TRANSACTION_CREATE, &merged)).await
30
5
}