yuki-cli 0.1.2

CLI client for the Yuki bookkeeping SOAP API
Documentation
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use crate::cli::setup_domain;
use crate::client::accounting::AccountingClient;
use crate::client::archive::ArchiveClient;
use crate::client::soap_client::SoapClient;
use crate::client::vat::VatClient;
use crate::config::Config;
use crate::error::YukiError;
use crate::output::{OutputFormat, format_json, format_table, is_tty};
use crate::period::parse_period;

pub async fn btw(
    config: &Config,
    admin: Option<&str>,
    period: Option<&str>,
    format: Option<&str>,
    quiet: bool,
) -> Result<(), YukiError> {
    let (start, end) = resolve_period(period)?;

    if !quiet {
        eprintln!("[1/3] Fetching VAT return list...");
    }
    let mut vat_client = VatClient::new();
    vat_client.authenticate(&config.api_key).await?;
    let (accounting_client, entry) = setup_domain(config, admin).await?;
    let vat_returns = vat_client.vat_return_list(&entry.admin_id).await?;

    if !quiet {
        eprintln!("[2/3] Fetching outstanding debtor items...");
    }
    let debtors = accounting_client
        .outstanding_debtor_items_by_date(&entry.admin_id, &start, &end)
        .await?;

    if !quiet {
        eprintln!("[3/3] Fetching outstanding creditor items...");
    }
    let creditors = accounting_client
        .outstanding_creditor_items_by_date(&entry.admin_id, &start, &end)
        .await?;

    // Build report: VAT returns in period + outstanding items
    let headers = vec![
        "Type".into(),
        "Contact".into(),
        "Description".into(),
        "Date".into(),
        "Amount".into(),
        "Open".into(),
    ];
    let mut rows: Vec<Vec<String>> = Vec::new();

    for r in &vat_returns {
        if r.start_date >= start && r.end_date <= end {
            rows.push(vec![
                "VAT Return".into(),
                String::new(),
                format!("Period {} ({})", r.period, r.status),
                r.start_date.clone(),
                String::new(),
                String::new(),
            ]);
        }
    }

    for item in &debtors {
        rows.push(vec![
            "Debtor".into(),
            item.contact_name.clone(),
            item.description.clone(),
            item.date.clone(),
            item.amount.clone(),
            item.open_amount.clone(),
        ]);
    }

    for item in &creditors {
        rows.push(vec![
            "Creditor".into(),
            item.contact_name.clone(),
            item.description.clone(),
            item.date.clone(),
            item.amount.clone(),
            item.open_amount.clone(),
        ]);
    }

    let fmt = OutputFormat::from_flag(format, is_tty());
    match fmt {
        OutputFormat::Table => println!("{}", format_table(&headers, &rows)),
        OutputFormat::Json => println!("{}", format_json(&headers, &rows)),
    }
    Ok(())
}

/// Extract the counterparty name from a SEPA bank transaction description.
///
/// SEPA descriptions encode counterparty info in the format:
/// `/CNTP/<iban>/<bic>/<name>/`
///
/// Falls back to the first 50 characters of the description when the field is absent.
fn parse_counterparty(description: &str) -> String {
    if let Some(cntp_start) = description.find("/CNTP/") {
        let after_cntp = &description[cntp_start + 6..];
        let parts: Vec<&str> = after_cntp.splitn(4, '/').collect();
        if parts.len() >= 3 {
            return parts[2].trim().to_string();
        }
    }
    description
        .chars()
        .take(50)
        .collect::<String>()
        .trim()
        .to_string()
}

/// Find bank transactions on a given GL account that have no matching invoice.
///
/// Each bank debit (negative amount) is first matched by absolute amount against outstanding
/// creditor items. Any remaining unmatched debits are then checked against booked invoices in
/// the archive for the same period. Only transactions that match neither source are reported.
pub async fn unmatched(
    config: &Config,
    admin: Option<&str>,
    period: Option<&str>,
    bank_account: &str,
    format: Option<&str>,
    quiet: bool,
) -> Result<(), YukiError> {
    let (start, end) = resolve_period(period)?;

    if !quiet {
        eprintln!("[1/3] Fetching bank transactions (GL {bank_account})...");
    }
    let mut accounting_client = AccountingClient::new();
    accounting_client.authenticate(&config.api_key).await?;
    let (accounting_client, entry) = {
        let entry = config.resolve_admin(admin)?;
        accounting_client
            .set_current_domain(&entry.domain_id)
            .await?;
        (accounting_client, entry)
    };

    let transactions = accounting_client
        .gl_account_transactions_and_contact(&entry.admin_id, bank_account, &start, &end)
        .await?;

    if !quiet {
        eprintln!("[2/3] Fetching outstanding creditor items...");
    }
    let creditor_items = accounting_client
        .outstanding_creditor_items(&entry.admin_id)
        .await?;

    if !quiet {
        eprintln!("[3/3] Fetching booked invoices from archive...");
    }
    let mut archive_client = ArchiveClient::new();
    archive_client.authenticate(&config.api_key).await?;
    let archive_docs = archive_client.search_documents("", &start, &end).await?;

    if !quiet {
        eprintln!("API calls made: 4");
    }

    // Build a pool of creditor open amounts for single-pass matching.
    // Key: canonical amount string (absolute value), value: remaining count.
    let mut creditor_pool: std::collections::HashMap<String, usize> =
        std::collections::HashMap::new();
    for item in &creditor_items {
        let key = item.open_amount.trim().to_string();
        *creditor_pool.entry(key).or_insert(0) += 1;
    }

    // Build a pool of archive document amounts for matching booked invoices.
    // Only positive amounts (purchase invoices) are relevant.
    let mut archive_pool: std::collections::HashMap<String, usize> =
        std::collections::HashMap::new();
    for doc in &archive_docs {
        if let Ok(amt) = doc.amount.trim().parse::<f64>()
            && amt > 0.0
        {
            let key = format!("{amt:.2}");
            *archive_pool.entry(key).or_insert(0) += 1;
        }
    }

    // Build a set of normalized contact names from the archive for fallback name matching.
    // This catches batched or split charges where the amount differs but the supplier is known.
    let archive_names: std::collections::HashSet<String> = archive_docs
        .iter()
        .filter(|d| !d.contact_name.is_empty())
        .map(|d| normalize_name(&d.contact_name))
        .filter(|n| !n.is_empty())
        .collect();

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

    for tx in &transactions {
        let amount: f64 = tx.amount.trim().parse().unwrap_or(0.0);
        if amount >= 0.0 {
            // Only debits (payments out) are relevant.
            continue;
        }
        // Represent the absolute value as a rounded-cent string for matching.
        let abs_amount = format!("{:.2}", amount.abs());

        // Check outstanding creditor items first.
        if let Some(count) = creditor_pool.get_mut(&abs_amount)
            && *count > 0
        {
            *count -= 1;
            continue;
        }

        // Check booked invoices in the archive by amount.
        if let Some(count) = archive_pool.get_mut(&abs_amount)
            && *count > 0
        {
            *count -= 1;
            continue;
        }

        // Use API-provided contact name when available, fall back to SEPA parsing.
        let counterparty = if tx.contact_name.is_empty() {
            parse_counterparty(&tx.description)
        } else {
            tx.contact_name.clone()
        };

        // Skip counterparties matching the configured ignore list.
        let cp_lower = counterparty.to_lowercase();
        if config
            .unmatched_ignore
            .iter()
            .any(|pat| cp_lower.contains(&pat.to_lowercase()))
        {
            continue;
        }

        // Fallback: check if the counterparty name is known in the archive.
        // Handles batched or split charges where the amount may differ.
        if archive_names.iter().any(|n| names_match(&counterparty, n)) {
            continue;
        }

        unmatched_rows.push(vec![
            tx.id.clone(),
            tx.date.clone(),
            format!("-{abs_amount}"),
            counterparty,
            tx.description.chars().take(80).collect::<String>(),
        ]);
    }

    let headers = vec![
        "ID".into(),
        "Date".into(),
        "Amount".into(),
        "Counterparty".into(),
        "Description".into(),
    ];

    let fmt = OutputFormat::from_flag(format, is_tty());
    match fmt {
        OutputFormat::Table => println!("{}", format_table(&headers, &unmatched_rows)),
        OutputFormat::Json => println!("{}", format_json(&headers, &unmatched_rows)),
    }
    Ok(())
}

/// Check if a specific invoice reference is still outstanding.
pub async fn outstanding(
    config: &Config,
    admin: Option<&str>,
    reference: &str,
    format: Option<&str>,
) -> Result<(), YukiError> {
    let (client, entry) = setup_domain(config, admin).await?;
    let xml = client
        .check_outstanding_item_admin(&entry.admin_id, reference)
        .await?;
    let result =
        SoapClient::parse_single_result(&xml, "CheckOutstandingItemAdminResult").unwrap_or(xml);

    let headers = vec!["Reference".into(), "Result".into()];
    let rows = vec![vec![reference.to_string(), result]];

    let fmt = OutputFormat::from_flag(format, is_tty());
    match fmt {
        OutputFormat::Table => println!("{}", format_table(&headers, &rows)),
        OutputFormat::Json => println!("{}", format_json(&headers, &rows)),
    }
    Ok(())
}

/// Normalize a company name for fuzzy matching.
///
/// Lowercases the name, removes "via ..." suffixes, strips common legal suffixes
/// and punctuation, and trims whitespace. This allows loose matching between bank
/// counterparty names and Yuki contact names despite formatting differences.
fn normalize_name(name: &str) -> String {
    let lower = name.to_lowercase();
    // Remove "via ..." suffix (e.g. "Vimexx via Mollie" -> "vimexx")
    let base = lower.split(" via ").next().unwrap_or(&lower);
    base.replace("b.v.", "")
        .replace("bv", "")
        .replace("gmbh", "")
        .replace("inc", "")
        .replace("ltd", "")
        .replace("s.a.", "")
        .replace(['.', ','], "")
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
}

/// Check whether two company names refer to the same entity.
///
/// Returns true if one normalized name contains the other, allowing for
/// abbreviations or partial matches.
fn names_match(bank_name: &str, archive_name: &str) -> bool {
    let a = normalize_name(bank_name);
    let b = normalize_name(archive_name);
    if a.is_empty() || b.is_empty() {
        return false;
    }
    a.contains(b.as_str()) || b.contains(a.as_str())
}

/// Resolve an optional period string to (start_date, end_date).
///
/// Defaults to the current calendar year when no period is given.
fn resolve_period(period: Option<&str>) -> Result<(String, String), YukiError> {
    match period {
        Some(p) => parse_period(p),
        None => {
            let year = current_year();
            Ok((format!("{year}-01-01"), format!("{year}-12-31")))
        }
    }
}

fn current_year() -> u32 {
    use std::time::{SystemTime, UNIX_EPOCH};
    let secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    1970 + (secs / 31_557_600) as u32
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_counterparty_extracts_cntp_name() {
        let desc = "/TRTP/SEPA/CNTP/NL01ABNA0001234567/ABNANL2A/Vimexx B.V./REMI/Hosting";
        assert_eq!(parse_counterparty(desc), "Vimexx B.V.");
    }

    #[test]
    fn parse_counterparty_falls_back_to_description() {
        assert_eq!(
            parse_counterparty("ING bankkosten maart"),
            "ING bankkosten maart"
        );
    }

    #[test]
    fn parse_counterparty_truncates_long() {
        let desc = "A".repeat(100);
        assert_eq!(parse_counterparty(&desc).len(), 50);
    }

    #[test]
    fn normalize_name_strips_legal_suffixes() {
        assert_eq!(normalize_name("Vimexx B.V."), "vimexx");
        assert_eq!(normalize_name("Hetzner GmbH"), "hetzner");
        assert_eq!(normalize_name("Amazon Inc"), "amazon");
    }

    #[test]
    fn normalize_name_removes_via_suffix() {
        assert_eq!(normalize_name("Vimexx via Mollie"), "vimexx");
    }

    #[test]
    fn normalize_name_handles_empty() {
        assert_eq!(normalize_name(""), "");
    }

    #[test]
    fn names_match_bidirectional_substring() {
        assert!(names_match("Hetzner Online GmbH", "Hetzner"));
        assert!(names_match("Hetzner", "Hetzner Online GmbH"));
    }

    #[test]
    fn names_match_case_insensitive() {
        assert!(names_match("HETZNER", "hetzner online"));
    }

    #[test]
    fn names_match_strips_legal_suffixes() {
        assert!(names_match("Vimexx B.V.", "Vimexx via Mollie"));
    }

    #[test]
    fn names_match_rejects_empty() {
        assert!(!names_match("", "Hetzner"));
        assert!(!names_match("Hetzner", ""));
    }

    #[test]
    fn names_match_rejects_unrelated() {
        assert!(!names_match("Hetzner", "Amazon"));
    }
}