Lines
100 %
Functions
50 %
Branches
//! `transaction create --template`: the template's draft with the given values over it.
use forms::{FormValues, TRANSACTION_CREATE, now_instant};
use rpc::Session;
use super::{apply_form, eval_text, submit_call};
use crate::CommandError;
use crate::flat::load_values;
use crate::forms::map_references;
/// Create a transaction from the draft of template `template_id`; `overrides`
/// replace its date and note and merge into its rows, and the date is now
/// when neither gives one.
///
/// # Errors
/// The server's refusal of the template load, [`CommandError::Argument`] for
/// an unknown or ambiguous reference, or [`CommandError::FormInvalid`] from the submit.
pub async fn create_from_template(
session: &mut Session,
template_id: &str,
overrides: &FormValues,
) -> Result<String, CommandError> {
let loaded = load_values(session, &TRANSACTION_CREATE, template_id).await?;
let overrides = map_references(session, &TRANSACTION_CREATE, overrides).await?;
let mut merged = apply_form(&TRANSACTION_CREATE, &loaded, &overrides, &[])?;
if merged.text("date").is_none() {
merged.set("date", &now_instant());
}
eval_text(session, &submit_call(&TRANSACTION_CREATE, &merged)).await