fix(import): the IIF export refuses an entry on two A/R or A/P accounts
BS-format-2 item 7: QuickBooks Desktop records at most one A/R or A/P account in a transaction (Intuit's own warning, sources/intuit-iif.md and rules/format/BS-format-2.toml), so an entry with lines on two receivable accounts, two payable accounts, or one of each cannot be written as one IIF transaction. journal_to_iif used to write it and merely list it in Iif::receivable_payable. Now the export collects every entry whose lines touch more than one distinct A/R/A/P account and returns IifError::SeveralReceivablePayableAccounts, naming the entries and saying why and what to do (reverse and post as two entries through a clearing account; splitting one would change what it says). It never splits an entry itself. ledger_package::export_iif and build_package pass the refusal through, so books.export and books.export_cpa_package return it before a file is handed over. The README test's entry on both controls moved to its own refusal test; the valid books it covers (one A/R entry, one A/P entry) are unchanged, as are every IIF golden and every test of the file's bytes. Co-Authored-By: DeepSeek V4.1 Flash <noreply@opencode.ai>
This commit is contained in:
1 parent
4b295ee224
commit
45ea103283
5 files changed
+280
-26
No files matched your search
@@ -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<EntryId>) -> String {
|
||||
entries.sort_unstable();
|
||||
entries.dedup();
|
||||
let ids: Vec<String> = 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<EntryId>) -> 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,
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user