diff --git a/crates/ledger-cli/tests/oracle_mcp.rs b/crates/ledger-cli/tests/oracle_mcp.rs index 061c7bc3..539f732d 100644 --- a/crates/ledger-cli/tests/oracle_mcp.rs +++ b/crates/ledger-cli/tests/oracle_mcp.rs @@ -1546,6 +1546,43 @@ fn iif_declares_accounts_by_role_and_balances_every_transaction() { ); } +/// BS-format-2 item 7 through the server: an entry with lines on both the +/// receivables and payables control accounts makes `books.export` iif and +/// `books.export_cpa_package` refuse, naming the entry, rather than hand +/// over a file QuickBooks Desktop will not import. +#[test] +fn iif_and_the_package_refuse_an_entry_on_two_receivable_or_payable_accounts() { + let dir = test_dir("iif-two-receivable-payable"); + let mut s = new_store(&dir); + let netted = ledger_core::parties::replaying(|| { + post( + &mut s, + "2025-04-05", + "Netted", + &[("2000", Dr, "80.00"), ("1200", Cr, "80.00")], + ) + }); + drop(s); + let mut m = Mcp::start(&dir, "1000000.00"); + for (tool, args) in [ + ( + "books.export", + json!({"format": "iif", "report": "journal", "as_of": END}), + ), + ( + "books.export_cpa_package", + json!({"as_of": END, "cash_accounts": ["1000"], "include_receipts": false, "include_tins": false}), + ), + ] { + let why = m.refuse(tool, args); + assert!(why.contains(&format!("entry {netted}: ")), "{tool}: {why}"); + assert!( + why.contains("more than one accounts receivable or accounts payable account"), + "{tool}: {why}" + ); + } +} + /// P3 review R5: books.export iif without as_of is as of today, like the /// trial balance: an entry dated later is left out, and the two exports /// made with no dates tie, account by account. diff --git a/crates/ledger-import/src/iif.rs b/crates/ledger-import/src/iif.rs index 57e56b6b..1f99475b 100644 --- a/crates/ledger-import/src/iif.rs +++ b/crates/ledger-import/src/iif.rs @@ -62,7 +62,12 @@ //! asks for one on a journal entry to accounts receivable or payable //! and takes one such account a transaction (Intuit staff and help; //! sources/intuit-iif.md). [`Iif::receivable_payable`] lists the -//! entries with such a line, for the human to be told. +//! entries with such a line, for the human to be told. An entry with +//! lines on more than one A/R or A/P account (two receivable, two +//! payable, or one of each) MUST be refused: QuickBooks Desktop will +//! not record such a transaction, and splitting the entry would +//! change what it says, so the export names the entry and leaves the +//! correction to the books. //! 9. Each line's class (a project) MUST be in its CLASS field, and the //! classes declared first (`!CLASS`: NAME), after the accounts and //! before the transactions, BECAUSE the kit puts every list before @@ -431,6 +436,17 @@ impl Iif { } } +/// "entry 3" or "entries 3, 5", the ids in order, each once. +fn entries_of(mut entries: Vec) -> String { + entries.sort_unstable(); + entries.dedup(); + let ids: Vec = entries.iter().map(ToString::to_string).collect(); + match ids.as_slice() { + [one] => format!("entry {one}"), + _ => format!("entries {}", ids.join(", ")), + } +} + /// "the memo of entry 3" or "the memos of entries 3, 5, 9", the ids in /// order, each once. fn memos_of(mut entries: Vec) -> String { @@ -454,6 +470,16 @@ pub enum IifError { /// The account. account: AccountId, }, + /// An entry has lines on more than one accounts receivable or accounts + /// payable account (INVARIANT 8): QuickBooks Desktop records at most + /// one such account in a transaction. + #[error( + "{entries}: lines on more than one accounts receivable or accounts payable account; QuickBooks Desktop records at most one A/R or A/P account in a transaction, so reverse and post as two entries through a clearing account (splitting one would change what it says)" + )] + SeveralReceivablePayableAccounts { + /// The entries, as "entry 3" or "entries 3, 5". + entries: String, + }, /// Two or more accounts would have one name in QuickBooks /// (INVARIANT 6). #[error( @@ -727,8 +753,10 @@ fn parents_first( /// class in CLASS; every text the file writes differently from the books, /// and the entries with a line on accounts receivable or payable /// (INVARIANTS 1-9). Refused when an entry uses an account `accounts` -/// lacks or a class `classes` lacks, two accounts or two classes would -/// have one name, or a name has a level too long for QuickBooks. +/// lacks or a class `classes` lacks, an entry has lines on more than one +/// accounts receivable or payable account (QuickBooks Desktop records at +/// most one a transaction), two accounts or two classes would have one +/// name, or a name has a level too long for QuickBooks. pub fn journal_to_iif( accounts: &[IifAccount], classes: &[IifClass], @@ -892,6 +920,7 @@ pub fn journal_to_iif( )); out.push_str(&format!("!ENDTRNS{EOL}")); let mut receivable_payable = Vec::new(); + let mut several_receivable_payable = Vec::new(); for entry in entries { let docnum = entry.id(); let d = entry.date(); @@ -913,6 +942,7 @@ pub fn journal_to_iif( Side::Credit => 1, }); let mut named = false; + let mut receivable_payable_accounts = BTreeSet::new(); for (n, line) in lines.iter().enumerate() { let (account, qb_type) = by_id @@ -921,10 +951,13 @@ pub fn journal_to_iif( entry: entry.id(), account: line.account, })?; - named |= matches!( + if matches!( qb_type, QbAccountType::AccountsReceivable | QbAccountType::AccountsPayable - ); + ) { + named = true; + receivable_payable_accounts.insert(line.account); + } let class = match line.class_id { None => "", Some(c) => class_of.get(&c).ok_or(IifError::UndeclaredClass { @@ -942,6 +975,14 @@ pub fn journal_to_iif( if named { receivable_payable.push(docnum); } + if receivable_payable_accounts.len() > 1 { + several_receivable_payable.push(docnum); + } + } + if !several_receivable_payable.is_empty() { + return Err(IifError::SeveralReceivablePayableAccounts { + entries: entries_of(several_receivable_payable), + }); } Ok(Iif { text: out, diff --git a/crates/ledger-import/tests/spec_import.rs b/crates/ledger-import/tests/spec_import.rs index 8c94e134..be8333d8 100644 --- a/crates/ledger-import/tests/spec_import.rs +++ b/crates/ledger-import/tests/spec_import.rs @@ -1813,6 +1813,147 @@ fn iif_refuses_an_undeclared_account() { ); } +/// iif INVARIANT 8 (BS-format-2 item 7): an entry with a line on one +/// accounts receivable account still writes one transaction, and the entry +/// is named to the human so they can add QuickBooks' customer or vendor. +#[test] +fn iif_writes_an_entry_on_one_receivable_or_payable_account() { + let mut l = Ledger::new(Currency::USD); + let ar = l + .add_account( + "1200", + "Accounts Receivable", + AccountType::Asset, + None, + false, + ) + .unwrap(); + let sales = l + .add_account("4000", "Sales", AccountType::Revenue, None, false) + .unwrap(); + let e = ledger_core::parties::replaying(|| { + let e = l.create_entry(d(2026, 4, 1), "Invoice 7"); + l.add_line(e, ar, Side::Debit, usd(50_000)).unwrap(); + l.add_line(e, sales, Side::Credit, usd(50_000)).unwrap(); + l.post_entry(e).unwrap(); + e + }); + let accounts = iif_accounts(&l, |a| match a.code.as_str() { + "1200" => QbAccountType::AccountsReceivable, + _ => QbAccountType::Income, + }); + let entries: Vec<&JournalEntry> = l.posted_entries().collect(); + let iif = journal_to_iif(&accounts, &[], &entries).unwrap(); + check_iif(&iif.text, &accounts, &entries); + assert!( + iif.text.contains(&format!( + "TRNS\t\tGENERAL JOURNAL\t4/1/2026\tAccounts Receivable\t\t500.00\t{e}\tInvoice 7\r\n" + )), + "{}", + iif.text + ); + assert_eq!(iif.receivable_payable, [e]); +} + +/// iif INVARIANT 8 (BS-format-2 item 7): an entry with lines on both the +/// receivables and payables control accounts is refused, naming its id: +/// QuickBooks Desktop records at most one A/R or A/P account a +/// transaction, and splitting the entry would change what it says. +#[test] +fn iif_refuses_an_entry_on_two_receivable_or_payable_accounts() { + let mut l = Ledger::new(Currency::USD); + let ar = l + .add_account( + "1200", + "Accounts Receivable", + AccountType::Asset, + None, + false, + ) + .unwrap(); + let ap = l + .add_account( + "2000", + "Accounts Payable", + AccountType::Liability, + None, + false, + ) + .unwrap(); + let e = ledger_core::parties::replaying(|| { + let e = l.create_entry(d(2026, 4, 3), "Netted"); + l.add_line(e, ap, Side::Debit, usd(8_000)).unwrap(); + l.add_line(e, ar, Side::Credit, usd(8_000)).unwrap(); + l.post_entry(e).unwrap(); + e + }); + let accounts = iif_accounts(&l, |a| match a.code.as_str() { + "1200" => QbAccountType::AccountsReceivable, + "2000" => QbAccountType::AccountsPayable, + _ => QbAccountType::OtherCurrentAsset, + }); + let entries: Vec<&JournalEntry> = l.posted_entries().collect(); + let err = journal_to_iif(&accounts, &[], &entries).unwrap_err(); + assert_eq!( + err, + IifError::SeveralReceivablePayableAccounts { + entries: format!("entry {e}") + } + ); + let why = err.to_string(); + assert!(why.starts_with(&format!("entry {e}: ")), "{why}"); + assert!( + why.contains("QuickBooks Desktop records at most one A/R or A/P account in a transaction"), + "{why}" + ); + assert!( + why.contains("reverse and post as two entries through a clearing account"), + "{why}" + ); +} + +/// iif INVARIANT 8 (BS-format-2 item 7): two distinct receivable accounts +/// in one entry are refused too (its `qb_type` decides, whatever the +/// callers call them), and every offending entry is named. +#[test] +fn iif_refuses_an_entry_on_two_receivable_accounts() { + let mut l = Ledger::new(Currency::USD); + let first = l + .add_account( + "1200", + "Accounts Receivable", + AccountType::Asset, + None, + false, + ) + .unwrap(); + let second = l + .add_account("1210", "Customer Deposits", AccountType::Asset, None, false) + .unwrap(); + let (one, two) = ledger_core::parties::replaying(|| { + let mut post = |memo: &str| { + let e = l.create_entry(d(2026, 5, 1), memo); + l.add_line(e, first, Side::Debit, usd(100)).unwrap(); + l.add_line(e, second, Side::Credit, usd(100)).unwrap(); + l.post_entry(e).unwrap(); + e + }; + (post("First"), post("Second")) + }); + let accounts = iif_accounts(&l, |a| match a.code.as_str() { + "1200" | "1210" => QbAccountType::AccountsReceivable, + _ => QbAccountType::OtherCurrentAsset, + }); + let entries: Vec<&JournalEntry> = l.posted_entries().collect(); + let err = journal_to_iif(&accounts, &[], &entries).unwrap_err(); + assert_eq!( + err, + IifError::SeveralReceivablePayableAccounts { + entries: format!("entries {one}, {two}") + } + ); +} + /// A ledger with Cash 1000, Revenue 4000 and Fees 6000. fn small_ledger() -> (Ledger, AccountId, AccountId, AccountId) { let mut l = Ledger::new(Currency::USD); diff --git a/crates/ledger-package/tests/package.rs b/crates/ledger-package/tests/package.rs index fce40c88..a822f0e3 100644 --- a/crates/ledger-package/tests/package.rs +++ b/crates/ledger-package/tests/package.rs @@ -1926,8 +1926,7 @@ fn the_readme_lists_what_the_iif_writes_differently() { /// for one on a journal entry to accounts receivable or payable, and takes /// one such account a transaction: sources/intuit-iif.md), so the README /// names the entries with a line on either, in file order, and says what -/// QuickBooks may ask. An entry on both is among them. Books with neither -/// get no such sentence. +/// QuickBooks may ask. Books with neither get no such sentence. #[test] fn the_readme_names_the_entries_on_receivables_and_payables() { let dir = tempfile::tempdir().unwrap(); @@ -1942,7 +1941,7 @@ fn the_readme_names_the_entries_on_receivables_and_payables() { // Hand lines on receivables and payables, as books an earlier version // wrote hold them (the control guard, rules.md B-38, refuses them as // new work): posted as replay posts them. - let (invoiced, billed, netted) = ledger_core::parties::replaying(|| { + let (invoiced, billed) = ledger_core::parties::replaying(|| { let invoiced = post( &mut s, "2026-04-01", @@ -1961,16 +1960,7 @@ fn the_readme_names_the_entries_on_receivables_and_payables() { ("2000", Side::Credit, "80.00"), ], ); - let netted = post( - &mut s, - "2026-04-03", - "Netted", - &[ - ("2000", Side::Debit, "80.00"), - ("1200", Side::Credit, "80.00"), - ], - ); - (invoiced, billed, netted) + (invoiced, billed) }); let company = company(false); let cash = cash(); @@ -1990,12 +1980,12 @@ fn the_readme_names_the_entries_on_receivables_and_payables() { ); assert_eq!( iif_txns(iif).iter().map(|t| t.0).collect::>(), - [rent, invoiced, billed, netted] + [rent, invoiced, billed] ); let readme = text(&p, "README.txt"); assert!( readme.contains(&format!( - "\nIt names no customer or vendor on its lines to accounts receivable or payable, in entries {invoiced}, {billed}, {netted}: QuickBooks Desktop asks for one on a journal entry to A/R or A/P (and takes only one A/R or A/P account a transaction); add it there if QuickBooks asks.\n" + "\nIt names no customer or vendor on its lines to accounts receivable or payable, in entries {invoiced}, {billed}: QuickBooks Desktop asks for one on a journal entry to A/R or A/P (and takes only one A/R or A/P account a transaction); add it there if QuickBooks asks.\n" )), "{readme}" ); @@ -2032,6 +2022,42 @@ fn the_readme_names_the_entries_on_receivables_and_payables() { assert!(!text(&p, "README.txt").contains("customer or vendor")); } +/// BS-format-2 item 7: an entry with lines on both the receivables and +/// payables control accounts is refused, naming its id, and the package is +/// not built: QuickBooks Desktop records at most one A/R or A/P account a +/// transaction, and splitting the entry would change what it says. +#[test] +fn the_package_refuses_an_entry_on_two_receivable_or_payable_accounts() { + let dir = tempfile::tempdir().unwrap(); + let (mut s, _) = plain_books(dir.path()); + for (code, name, ty) in [ + ("1200", "Accounts Receivable", AccountType::Asset), + ("2000", "Accounts Payable", AccountType::Liability), + ("4000", "Sales", AccountType::Revenue), + ] { + s.add_account(code, name, ty, None, false).unwrap(); + } + let netted = ledger_core::parties::replaying(|| { + post( + &mut s, + "2026-04-03", + "Netted", + &[ + ("2000", Side::Debit, "80.00"), + ("1200", Side::Credit, "80.00"), + ], + ) + }); + let company = company(false); + let cash = cash(); + let err = build_package(&s, &settings(&company, &cash), &request(false, None)).unwrap_err(); + assert!(err.starts_with(&format!("entry {netted}: ")), "{err}"); + assert!( + err.contains("more than one accounts receivable or accounts payable account"), + "{err}" + ); +} + /// Every text the package's XLSX `name` holds: its XML parts (the shared /// strings and the sheets), concatenated, `&` read as `&`. fn xlsx_strings(p: &Package, name: &str) -> String { diff --git a/docs/spec/claims/format.md b/docs/spec/claims/format.md index 04f99712..00606017 100644 --- a/docs/spec/claims/format.md +++ b/docs/spec/claims/format.md @@ -260,12 +260,21 @@ by the as-of day, otherwise by wanderbooks type `OCASSET` (asset), `OCLIAB` `COGS`, `EXINC` and `EXEXP` are in `ledger_import::QbAccountType` but nothing in the books maps to them yet. -**Gap:** an entry with lines on more than one A/R or A/P account (the -receivables and payables roles are two accounts, so one entry can hold both) -must be refused by the spec (Intuit: QuickBooks will not record it), but -`journal_to_iif` writes it and only lists it in `Iif::receivable_payable` -(a flag for any entry with such a line). Unenforced until the engine refuses -it. +**Enforced:** `ledger_import::iif::journal_to_iif` refuses an entry with +lines on more than one A/R or A/P account +(`IifError::SeveralReceivablePayableAccounts`), naming every such entry and +saying QuickBooks Desktop records at most one A/R or A/P account a +transaction and that splitting the entry would change what it says; +`ledger_package::export_iif` and `build_package` and `ledger-mcp` +`books.export` and `books.export_cpa_package` return that refusal instead of +a file. **Checked:** `ledger-import` `tests/spec_import.rs` +(`iif_writes_an_entry_on_one_receivable_or_payable_account`, +`iif_refuses_an_entry_on_two_receivable_or_payable_accounts`, +`iif_refuses_an_entry_on_two_receivable_accounts`); `ledger-package` +`tests/package.rs` +(`the_package_refuses_an_entry_on_two_receivable_or_payable_accounts`); +`ledger-cli` `oracle_mcp.rs` +(`iif_and_the_package_refuse_an_entry_on_two_receivable_or_payable_accounts`). **Enforced:** `ledger_import::iif` (`field`, `iif_accounts`, `iif_classes`, `journal_to_iif`, `Iif::warnings`, `Iif::changes`, `Iif::receivable_payable`),