A close waited, unbounded, for a running books.back_up_now (git only Backups::shut could cancel), for a restore, create or adopt (they hold the one lifecycle lock, and at the server's close admin_jobs.wait() waited for them before anything was released), and, through the store lock, for a verification or a package reading every stored file. The bound's T_calls term was not finite. Now a close calls hurry at its start, and the stoppable work looks at it: books.back_up_now and timer-asked backups (git stopped, a stopped run records 'stopped' and no result of its own), books.verify (per journal line and file; a heal of a drifted database is begun only if no stop came before it: stopped half way it would leave the live ledger part replayed), books.export_cpa_package (per stored file). Such a call answers that it was stopped because the books are closing, and its access record says refused: stopped_by_close. On the admin socket restore, adopt, create and open take a JobStop: asked at each phase and file copied, they remove what they wrote and leave the root byte for byte as it was; the renames that put the books in place are the point of no return, after which they finish (two renames and two syncs). A close of any book, and the server's own, raises the flag before it waits for the lifecycle lock. A close whose last backup had to be killed in the owner's folder now says which file was left, and the target's backup is marked failed by shut when the run's future was dropped at its bound. INVARIANT 10 is rewritten so each term of the bound is enforced by code, and lists the calls that can run long (and the one known exception, books.import_bank_csv); INVARIANTS 8 and 9 say exactly which refusals are recorded and why an unrecorded one never ran; the format upgrade's wording says a stop is honoured up to its final look. Tests: the close is held to the bound while a backup call, a verification, a package and each admin job is running and would never end; the stopped job leaves the root byte for byte as it was; a lock left in a backup folder is reported and never removed. Sleeps that stood for 'the other side got there' now wait on the condition (Expect: 100-continue for a half-sent body, a writable named pipe for a fetch that is reading). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
734 lines
34 KiB
Rust
734 lines
34 KiB
Rust
//! The CPA handoff package: every statement, ledger, and worksheet the CPA
|
|
//! needs for the return, with the evidence behind the year's entries, in
|
|
//! one zip.
|
|
//!
|
|
//! ```text
|
|
//! GOAL: everything the CPA needs for the return in one zip, the K-1s'
|
|
//! and 1099s' TINs included when asked, built the same by the
|
|
//! server and the viewer.
|
|
//! SOURCE: docs/mcp-interface.md; BS-partner-2; BS-statement-4 (as of one day); BS-format-2 (the
|
|
//! IIF); DECISION (owner, 2026-10-15): "it needs to be in the damn
|
|
//! CPA package."
|
|
//! INVARIANTS:
|
|
//! 1. The statements (profit and loss, balance sheet, cash flow, members'
|
|
//! equity with members, tax summary, trial balance) each as PDF and
|
|
//! XLSX, for the fiscal year containing as_of, through as_of; the
|
|
//! fixed asset register when an asset was bought by as_of; the
|
|
//! general ledger and journal (XLSX); the journal through as_of as
|
|
//! IIF; the 1099 worksheet for as_of's calendar year through as_of,
|
|
//! books.vendor_1099's for the package's cash and financing accounts
|
|
//! (INVARIANT 7), or why it can't be made; with members, the K-1
|
|
//! worksheet; a README first.
|
|
//! 2. With members, k1-members.csv has one row per member: name, party,
|
|
//! share, capital account, TIN and type (or last four without a TIN
|
|
//! source), and the K-1 worksheet's capital figures (ledger_core::k1)
|
|
//! for the fiscal year to date; the members' total row with more than
|
|
//! one. Beside it, k1-tax-basis-capital.csv: each member's capital
|
|
//! on the tax basis (Item L), line by line beside the book figures,
|
|
//! with the reconciliation (crate::k1_tax). The 1099 worksheet is
|
|
//! books.vendor_1099's CSV (crate::vendor_1099_csv), each payee's
|
|
//! TIN and type added with a TIN source.
|
|
//! 3. A TIN the source can't give never fails the package: the row says
|
|
//! why, and so do the README and tin_gaps (members, and 1099 payees
|
|
//! a 1099 is or may be due for).
|
|
//! 4. With include_receipts: every current file behind the year's
|
|
//! posted entries, filed on them or linked to them, once per entry,
|
|
//! each with a row in receipts/index.csv; and the company's
|
|
//! documents, the files behind no entry (loose receipts aside).
|
|
//! Withdrawn files never.
|
|
//! 5. Read-only (crate INVARIANT 1): the package is returned, its files
|
|
//! and the zip, and written nowhere.
|
|
//! 6. As of one day (BS-statement-4): no figure, entry, receipt or worksheet row
|
|
//! reflects anything dated after as_of; the chart and documents/ are
|
|
//! as they stand when the package is made.
|
|
//! 7. One list of cash accounts and one of financing accounts per
|
|
//! package: the request's, else the settings'. The cash flow, the K-1
|
|
//! worksheet, the IIF's BANK accounts and the 1099 worksheet's
|
|
//! payments all read the same.
|
|
//! 8. Every account name, number or place and every memo
|
|
//! general-journal.iif writes differently from the books (it holds
|
|
//! plain ASCII, within QuickBooks Desktop's limits and its rule that
|
|
//! a subaccount has its parent's type: BS-format-2) is in `warnings` and the
|
|
//! README, the memos grouped by what is written differently; the
|
|
//! README also names the entries with a line on accounts receivable
|
|
//! or payable, which the file gives no customer or vendor.
|
|
//! ```
|
|
|
|
use chrono::NaiveDate;
|
|
use ledger_core::{AccountId, Side, reports};
|
|
use rust_decimal::Decimal;
|
|
|
|
use crate::books::PackageBooks;
|
|
use crate::iif::export_iif;
|
|
use crate::reports::{
|
|
Settings, bs_statement, cash_flow_for, member_capitals, pnl_statement, report_members,
|
|
report_meta, year_earnings,
|
|
};
|
|
use crate::text::{csv_escape, money, share_text, total_text};
|
|
use crate::tins::{TinSource, tin_type_key};
|
|
use crate::vendor_1099::{vendor_1099_csv, vendor_1099_csv_name, vendor_1099_worksheet};
|
|
|
|
/// What the package is built for.
|
|
#[derive(Clone, Copy)]
|
|
pub struct PackageRequest<'a> {
|
|
/// The day it is as of: the balance sheet's date, the end of every
|
|
/// period report (the fiscal year to date).
|
|
pub as_of: NaiveDate,
|
|
/// The day it is prepared, for the reports' headers.
|
|
pub prepared_on: NaiveDate,
|
|
/// Cash account codes the cash flow statement reconciles to (empty:
|
|
/// the settings' cash accounts).
|
|
pub cash_accounts: &'a [String],
|
|
/// Financing account codes (empty: the settings' financing accounts).
|
|
pub financing_accounts: &'a [String],
|
|
/// Investing account codes (empty: the settings' investing accounts).
|
|
pub investing_accounts: &'a [String],
|
|
/// Whether the receipts and the company's documents go in
|
|
/// (INVARIANT 4).
|
|
pub include_receipts: bool,
|
|
/// Where the worksheets' full TINs come from: none, last fours only.
|
|
pub tins: Option<TinSource<'a>>,
|
|
}
|
|
|
|
/// A built package.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct Package {
|
|
/// The zip's name: `cpa-package-<as_of>.zip`.
|
|
pub file_name: String,
|
|
/// Every file in the zip, in its order: (name in the zip, bytes).
|
|
pub files: Vec<(String, Vec<u8>)>,
|
|
/// The zip.
|
|
pub zip: Vec<u8>,
|
|
/// With a TIN source: each member, or 1099 payee a 1099 is or may be
|
|
/// due for, whose TIN the package lacks, and why (INVARIANT 3).
|
|
pub tin_gaps: Vec<String>,
|
|
/// The receipts in receipts/ (one per entry they are behind).
|
|
pub receipt_count: usize,
|
|
/// What general-journal.iif writes differently from the books to fit
|
|
/// QuickBooks Desktop (INVARIANT 8), as `ledger_import::Iif::warnings`
|
|
/// says it: each account name and number a line, each thing written
|
|
/// differently in the memos a line with the entries it is in.
|
|
pub warnings: Vec<String>,
|
|
/// How many account names, numbers and memos general-journal.iif
|
|
/// writes differently (each one the warnings cover).
|
|
pub iif_changes: usize,
|
|
}
|
|
|
|
/// The package's file name for `as_of`.
|
|
pub fn package_file_name(as_of: NaiveDate) -> String {
|
|
format!("cpa-package-{as_of}.zip")
|
|
}
|
|
|
|
/// Build the CPA package from `books` (INVARIANTS 1-5).
|
|
pub fn build_package<B: PackageBooks + ?Sized>(
|
|
books: &B,
|
|
settings: &Settings<'_>,
|
|
req: &PackageRequest<'_>,
|
|
) -> Result<Package, String> {
|
|
use ledger_import::{XlsxReport, report, report_to_xlsx, statement_pdf};
|
|
// A stop asked for before any of the work (the books server's close).
|
|
ledger_store::cancel::check().map_err(|e| e.to_string())?;
|
|
let company = settings.company;
|
|
let (as_of, today) = (req.as_of, req.prepared_on);
|
|
let meta = report_meta(company, req.prepared_on, books.journal_seq());
|
|
let ledger = books.ledger();
|
|
let tins = req.tins;
|
|
let mut tin_gaps: Vec<String> = Vec::new();
|
|
|
|
// The fiscal year to date: the tax year holding as_of (IRS Pub. 538),
|
|
// the fiscal-year fact itself, not "what omitted dates mean" (
|
|
// BS-period-3), so the package's year never follows a default.
|
|
let fy = ledger_core::calendar::fiscal_year_start_on(company.fiscal_year_start, as_of);
|
|
let (fy_start, as_of_s) = (fy.to_string(), as_of.to_string());
|
|
let xlsx = |r: XlsxReport<'_>| report_to_xlsx(&r, &meta).map_err(|e| e.to_string());
|
|
let pdf = |st: &ledger_import::Statement| statement_pdf(st, &meta).map_err(|e| e.to_string());
|
|
let members = report_members(company);
|
|
// One list of each per package (INVARIANT 7).
|
|
let payments = settings.with_accounts(req.cash_accounts, req.financing_accounts);
|
|
let (cash_codes, financing_codes) = (payments.cash_accounts, payments.financing_accounts);
|
|
|
|
// The financial statements, each as PDF and XLSX.
|
|
let mut statements: Vec<(&str, ledger_import::Statement)> = vec![
|
|
(
|
|
"profit-and-loss",
|
|
report::profit_and_loss(&pnl_statement(
|
|
ledger,
|
|
company,
|
|
Some(&fy_start),
|
|
Some(&as_of_s),
|
|
None,
|
|
today,
|
|
)?),
|
|
),
|
|
(
|
|
"balance-sheet",
|
|
report::balance_sheet(
|
|
&bs_statement(ledger, company, Some(&as_of_s), today)?,
|
|
year_earnings(ledger, company, as_of)?,
|
|
&members,
|
|
),
|
|
),
|
|
(
|
|
"cash-flow",
|
|
// The package's one list of each (INVARIANT 7); investing
|
|
// accounts are the cash flow's alone (the request's, else the
|
|
// settings').
|
|
report::cash_flow(&cash_flow_for(
|
|
ledger,
|
|
settings,
|
|
Some(&fy_start),
|
|
Some(&as_of_s),
|
|
cash_codes,
|
|
financing_codes,
|
|
req.investing_accounts,
|
|
today,
|
|
)?),
|
|
),
|
|
];
|
|
let mut k1_csv: Option<(String, Vec<u8>)> = None;
|
|
let mut k1_tax: Option<(String, Vec<u8>)> = None;
|
|
if !company.members.is_empty() {
|
|
let capitals = member_capitals(ledger, &company.members)?;
|
|
let me =
|
|
ledger_core::members_equity(ledger, fy, as_of, &capitals).map_err(|e| e.to_string())?;
|
|
statements.push(("members-equity", report::members_equity(&me)));
|
|
// The K-1 worksheet: each member's capital account for the
|
|
// year (Schedule K-1 Item L) and their TIN (INVARIANT 2).
|
|
let cash: Vec<AccountId> = cash_codes
|
|
.iter()
|
|
.filter_map(|c| ledger.chart().find_by_code(c).map(|a| a.id))
|
|
.collect();
|
|
k1_csv = Some(
|
|
match ledger_core::k1::k1_worksheet(ledger, fy, as_of, &capitals, &cash)
|
|
.map_err(|e| e.to_string())
|
|
.and_then(|rows| {
|
|
// A row per member, in their order, or none: a row is
|
|
// never paired with another member.
|
|
if rows.len() == company.members.len() {
|
|
Ok(rows)
|
|
} else {
|
|
Err(format!(
|
|
"the K-1 worksheet has {} rows for {} members",
|
|
rows.len(),
|
|
company.members.len()
|
|
))
|
|
}
|
|
}) {
|
|
Ok(rows) => {
|
|
let mut w = String::from("member,party_id,share,capital_account,");
|
|
w.push_str(if tins.is_some() {
|
|
"tin,tin_type,tin_note,"
|
|
} else {
|
|
"tin_last4,tin_type,"
|
|
});
|
|
w.push_str("beginning_capital,contributions_cash,contributions_property,net_income,distributions,other,ending_capital\n");
|
|
for (row, m) in rows.iter().zip(&company.members) {
|
|
let party = ledger_core::k1::member_party(ledger, &m.name);
|
|
let facts = party
|
|
.and_then(|id| ledger.get_party(id))
|
|
.and_then(|p| p.tin.clone());
|
|
let mut cells = vec![
|
|
csv_escape(&m.name),
|
|
party.map(|id| id.to_string()).unwrap_or_default(),
|
|
share_text(m.share),
|
|
m.capital_account.clone(),
|
|
];
|
|
match tins {
|
|
Some(tins) => {
|
|
let got = match party {
|
|
None => Err(format!(
|
|
"TIN not in the vault: no party named {:?} with the member role (books.add_party with role member, or books.set_party_roles, then books.set_tin)",
|
|
m.name
|
|
)),
|
|
Some(id) => tins(id),
|
|
};
|
|
match got {
|
|
Ok(t) => {
|
|
cells.push(t.formatted());
|
|
cells.push(tin_type_key(t.kind).into());
|
|
cells.push(String::new());
|
|
}
|
|
Err(why) => {
|
|
tin_gaps.push(format!("member {}: {why}", m.name));
|
|
cells.push(String::new());
|
|
cells.push(
|
|
facts
|
|
.map(|f| f.tin_type.key().to_string())
|
|
.unwrap_or_default(),
|
|
);
|
|
cells.push(csv_escape(&why));
|
|
}
|
|
}
|
|
}
|
|
None => {
|
|
cells.push(
|
|
party
|
|
.and_then(|id| ledger.get_party(id))
|
|
.and_then(|p| p.tin_last4())
|
|
.unwrap_or_default()
|
|
.to_string(),
|
|
);
|
|
cells.push(
|
|
facts
|
|
.map(|f| f.tin_type.key().to_string())
|
|
.unwrap_or_default(),
|
|
);
|
|
}
|
|
}
|
|
for v in [
|
|
row.beginning,
|
|
row.contributions_cash,
|
|
row.contributions_property,
|
|
row.net_income,
|
|
row.distributions,
|
|
row.other,
|
|
row.ending,
|
|
] {
|
|
cells.push(money(v));
|
|
}
|
|
w.push_str(&cells.join(","));
|
|
w.push('\n');
|
|
}
|
|
// The members together: ties to the 1065's Schedule L
|
|
// capital and the statement of members' equity. Not
|
|
// a K-1 (each member gets their own).
|
|
if rows.len() > 1 {
|
|
let sum = |f: fn(&ledger_core::k1::K1Row) -> Decimal| {
|
|
ledger_core::kernel::total_of(&rows, f)
|
|
};
|
|
let share = ledger_core::kernel::total_of(&company.members, |m| m.share);
|
|
let mut cells = vec![
|
|
csv_escape("Total, all members (not a K-1)"),
|
|
String::new(),
|
|
share.map_or_else(|| total_text(None), share_text),
|
|
String::new(),
|
|
String::new(),
|
|
String::new(),
|
|
];
|
|
if tins.is_some() {
|
|
cells.push(String::new());
|
|
}
|
|
for v in [
|
|
sum(|r| r.beginning),
|
|
sum(|r| r.contributions_cash),
|
|
sum(|r| r.contributions_property),
|
|
sum(|r| r.net_income),
|
|
sum(|r| r.distributions),
|
|
sum(|r| r.other),
|
|
sum(|r| r.ending),
|
|
] {
|
|
cells.push(total_text(v));
|
|
}
|
|
w.push_str(&cells.join(","));
|
|
w.push('\n');
|
|
}
|
|
("k1-members.csv".into(), w.into_bytes())
|
|
}
|
|
Err(e) => (
|
|
"k1-members-NOT-INCLUDED.txt".into(),
|
|
format!("{e}\n").into_bytes(),
|
|
),
|
|
},
|
|
);
|
|
// The K-1 worksheet on the tax basis (Item L's tax-basis method,
|
|
// D-41), beside the book figures: a row per member or none.
|
|
let names: Vec<String> = company.members.iter().map(|m| m.name.clone()).collect();
|
|
k1_tax = Some(
|
|
match ledger_core::tax_capital::tax_basis_capital(ledger, fy, as_of, &capitals, &cash)
|
|
.map_err(|e| e.to_string())
|
|
.and_then(|tc| {
|
|
if tc.rows.len() == names.len() {
|
|
Ok(tc)
|
|
} else {
|
|
Err(format!(
|
|
"the tax-basis worksheet has {} rows for {} members",
|
|
tc.rows.len(),
|
|
names.len()
|
|
))
|
|
}
|
|
}) {
|
|
Ok(tc) => (
|
|
crate::k1_tax::FILE_NAME.into(),
|
|
crate::k1_tax::tax_basis_csv(ledger, &names, &tc).into_bytes(),
|
|
),
|
|
Err(e) => (
|
|
"k1-tax-basis-capital-NOT-INCLUDED.txt".into(),
|
|
format!("{e}\n").into_bytes(),
|
|
),
|
|
},
|
|
);
|
|
}
|
|
statements.push((
|
|
"tax-summary",
|
|
report::tax_summary(
|
|
&ledger_core::tax_summary(ledger, fy, as_of, company.tax_form)
|
|
.map_err(|e| e.to_string())?,
|
|
),
|
|
));
|
|
statements.push((
|
|
"trial-balance",
|
|
report::trial_balance(&reports::trial_balance_as_of(ledger, as_of), as_of),
|
|
));
|
|
let mut files: Vec<(String, Vec<u8>)> = Vec::new();
|
|
for (name, st) in &statements {
|
|
files.push((format!("{name}.pdf"), pdf(st)?));
|
|
files.push((format!("{name}.xlsx"), xlsx(XlsxReport::Statement(st))?));
|
|
}
|
|
// The receivables and payables agings, each with its tie-out line to
|
|
// the control account (BS-ledger-8, 8.3.10): the same CSV books.export and
|
|
// the viewer make.
|
|
files.push((
|
|
"ar_aging.csv".into(),
|
|
crate::aging::aging_csv(ledger, true, as_of)?.into_bytes(),
|
|
));
|
|
files.push((
|
|
"ap_aging.csv".into(),
|
|
crate::aging::aging_csv(ledger, false, as_of)?.into_bytes(),
|
|
));
|
|
// The fixed asset register, when an asset was acquired by as_of (one
|
|
// acquired later is not in it, so neither is an empty register).
|
|
if ledger
|
|
.registered_assets()
|
|
.any(|a| ledger.asset_acquired_date(a.id).is_some_and(|d| d <= as_of))
|
|
{
|
|
let r = ledger_core::fixed_asset_register(ledger, as_of).map_err(|e| e.to_string())?;
|
|
files.push((
|
|
"fixed-asset-register.pdf".into(),
|
|
ledger_import::asset_register_pdf(&r, &meta).map_err(|e| e.to_string())?,
|
|
));
|
|
files.push((
|
|
"fixed-asset-register.xlsx".into(),
|
|
ledger_import::asset_register_xlsx(&r, &meta).map_err(|e| e.to_string())?,
|
|
));
|
|
}
|
|
// The books behind them: by account, and entry by entry.
|
|
let gl = ledger_core::general_ledger(ledger, fy, as_of).map_err(|e| e.to_string())?;
|
|
files.push((
|
|
"general-ledger.xlsx".into(),
|
|
xlsx(XlsxReport::GeneralLedger(&gl))?,
|
|
));
|
|
files.push((
|
|
"general-journal.xlsx".into(),
|
|
xlsx(XlsxReport::GeneralJournal(ledger, fy, as_of))?,
|
|
));
|
|
// 1099 worksheet for the calendar year of as_of, through as_of
|
|
// (INVARIANT 6): the payments from January 1, out of the package's
|
|
// cash and financing accounts (INVARIANT 7), the same worksheet and
|
|
// CSV books.vendor_1099 and the viewer hand over for those accounts
|
|
// and that day (one builder, one figure).
|
|
let year = chrono::Datelike::year(&as_of);
|
|
let mut worksheet_1099 = false;
|
|
match vendor_1099_worksheet(ledger, &payments, year, None, Some(as_of), tins) {
|
|
Ok(w) => {
|
|
// A payee a 1099 is or may be due for, whose TIN the package
|
|
// lacks (INVARIANT 3).
|
|
for l in w.lines.iter().filter(|l| l.may_file()) {
|
|
if let Some(why) = &l.tin_note {
|
|
tin_gaps.push(format!("1099 payee {}: {why}", l.name));
|
|
}
|
|
}
|
|
files.push((
|
|
vendor_1099_csv_name(year, false),
|
|
vendor_1099_csv(&w, tins.is_some()).into_bytes(),
|
|
));
|
|
worksheet_1099 = true;
|
|
}
|
|
// The worksheet can't be made (the year's threshold isn't known,
|
|
// or the package names no cash accounts): never guess (
|
|
// BS-1099-1); say so in the package instead.
|
|
Err(e) => files.push((
|
|
format!("1099-worksheet-{year}-NOT-INCLUDED.txt"),
|
|
format!("{e}\n").into_bytes(),
|
|
)),
|
|
}
|
|
// The evidence: every current file behind the year's posted
|
|
// entries, filed on them or linked to them (a file linked to an
|
|
// entry counts as its receipt), one copy per entry with a row in
|
|
// the index; and the company's documents, the files behind no
|
|
// entry (INVARIANT 4).
|
|
let mut receipt_count = 0;
|
|
let mut document_count = 0;
|
|
// Each file's name in the zip is its own: a second `r.pdf` is
|
|
// `r (2).pdf` (compared ignoring case, so it unpacks the same on every
|
|
// system), and the index names the file as it is stored.
|
|
let mut used: std::collections::HashSet<String> =
|
|
files.iter().map(|(n, _)| n.to_lowercase()).collect();
|
|
let mut unique = |name: String| -> String {
|
|
// The extension is the last segment's own: a folder with a dot
|
|
// ("a.b/notes") keeps its name, and a name that is all extension
|
|
// (".env") has none.
|
|
let cut = name.rfind('/').map_or(0, |i| i + 1);
|
|
let (stem, ext) = match name[cut..].rfind('.') {
|
|
Some(i) if i > 0 => (name[..cut + i].to_string(), name[cut + i..].to_string()),
|
|
_ => (name.clone(), String::new()),
|
|
};
|
|
let mut n = 1;
|
|
let mut candidate = name;
|
|
while !used.insert(candidate.to_lowercase()) {
|
|
n += 1;
|
|
candidate = format!("{stem} ({n}){ext}");
|
|
}
|
|
candidate
|
|
};
|
|
if req.include_receipts {
|
|
let clean = |s: &str| -> String {
|
|
let c: String = s
|
|
.chars()
|
|
.map(|c| {
|
|
if c.is_alphanumeric() || ".-_ ".contains(c) {
|
|
c
|
|
} else {
|
|
'_'
|
|
}
|
|
})
|
|
.collect();
|
|
// A name of dots alone ("", ".", "..") would name no file, or
|
|
// climb out of its folder when the zip is unpacked.
|
|
if c.chars().all(|c| c == '.') {
|
|
"_".repeat(c.len().max(1))
|
|
} else {
|
|
c
|
|
}
|
|
};
|
|
let mut index = String::from("entry_id,date,description,amount,file,sha256\n");
|
|
for a in books.all_attachments().map_err(|e| e.to_string())? {
|
|
// A stop asked for (the books server's close) is looked at once
|
|
// per stored file read: a package of many files isn't held to
|
|
// its end by it (ledger_store::cancel).
|
|
ledger_store::cancel::check().map_err(|e| e.to_string())?;
|
|
if a.withdrawn_at.is_some() {
|
|
continue;
|
|
}
|
|
let mut behind: Vec<u64> = a.entry_id.into_iter().collect();
|
|
for sub in books.links_of(a.id).map_err(|e| e.to_string())? {
|
|
if let ledger_store::Subject::Entry(e) = sub
|
|
&& !behind.contains(&e)
|
|
{
|
|
behind.push(e);
|
|
}
|
|
}
|
|
behind.sort_unstable();
|
|
let bytes = books.attachment_bytes(a.id).map_err(|e| e.to_string())?;
|
|
if behind.is_empty() {
|
|
if a.category != "receipts" {
|
|
files.push((
|
|
unique(format!(
|
|
"documents/{}/{}",
|
|
clean(&a.category),
|
|
clean(&a.file_name)
|
|
)),
|
|
bytes,
|
|
));
|
|
document_count += 1;
|
|
}
|
|
continue;
|
|
}
|
|
for e in behind.iter().filter_map(|&e| ledger.get_entry(e)) {
|
|
if e.status() != ledger_core::EntryStatus::Posted
|
|
|| e.date() < fy
|
|
|| e.date() > as_of
|
|
{
|
|
continue;
|
|
}
|
|
let name = unique(format!(
|
|
"receipts/{}-entry-{}-{}",
|
|
e.date(),
|
|
e.id(),
|
|
clean(&a.file_name)
|
|
));
|
|
let amount = ledger_core::kernel::total_of(
|
|
e.lines().iter().filter(|l| l.side == Side::Debit),
|
|
|l| l.amount.amount,
|
|
);
|
|
index.push_str(&format!(
|
|
"{},{},\"{}\",{},{},{}\n",
|
|
e.id(),
|
|
e.date(),
|
|
e.description().replace('"', "\"\""),
|
|
total_text(amount),
|
|
name,
|
|
a.blob_sha
|
|
));
|
|
files.push((name, bytes.clone()));
|
|
receipt_count += 1;
|
|
}
|
|
}
|
|
files.push(("receipts/index.csv".into(), index.into_bytes()));
|
|
}
|
|
let k1_included = k1_csv
|
|
.as_ref()
|
|
.is_some_and(|(name, _)| name == "k1-members.csv");
|
|
if let Some(k1) = k1_csv {
|
|
files.push(k1);
|
|
}
|
|
let k1_tax_included = k1_tax
|
|
.as_ref()
|
|
.is_some_and(|(name, _)| name == crate::k1_tax::FILE_NAME);
|
|
if let Some(k1) = k1_tax {
|
|
files.push(k1);
|
|
}
|
|
let seq = books.journal_seq();
|
|
// The journal through as_of for QuickBooks, closes left out, and what
|
|
// it writes differently (INVARIANT 8).
|
|
let iif = export_iif(ledger, cash_codes, as_of).map_err(|e| e.to_string())?;
|
|
let (warnings, iif_changes) = (iif.warnings(), iif.changes.len());
|
|
let receivable_payable: Vec<String> = iif
|
|
.receivable_payable
|
|
.iter()
|
|
.map(ToString::to_string)
|
|
.collect();
|
|
files.push(("general-journal.iif".into(), iif.text.into_bytes()));
|
|
let year_end = NaiveDate::from_ymd_opt(year, 12, 31) == Some(as_of);
|
|
let include_tins = tins.is_some();
|
|
let readme = format!(
|
|
"{name} — books package for the CPA\n\nFiscal year to date: {fy_start} through {as_of_s}. {basis} basis.\nKept in wanderbooks: double-entry books from an append-only, hash-chained journal ({seq} events, verified).\n\nStatements (PDF and XLSX): profit and loss, balance sheet, cash flow, {members}tax summary, trial balance.\nLedgers (XLSX): general ledger by account; general journal entry by entry.\nAgings (CSV): ar_aging.csv and ap_aging.csv, one row per open invoice or bill, each ending with a TOTAL line that ties to its control account (BS-ledger-8). Early-payment discounts are taken by the gross method (A-8); bad debts are written off directly (A-5).\n{iif}{worksheet}{k1}{receipts}{tins}\nThe tax summary maps accounts to return categories; it's a worksheet, not a filing. Questions go to the owners, who can ask their bookkeeping agent to trace any number to its entries and receipts.\n",
|
|
name = company.name,
|
|
basis = company.basis,
|
|
members = if company.members.is_empty() {
|
|
""
|
|
} else {
|
|
"members' equity, "
|
|
},
|
|
iif = {
|
|
let mut t = format!(
|
|
"general-journal.iif: every posted entry from the first through {as_of_s}, for import into QuickBooks Desktop. Plain ASCII text (its import turns anything else into \"?\"), CR LF line endings; DOCNUM is each entry's id in these books{}. Period-closing entries are left out: QuickBooks keeps none and works out retained earnings itself, so an imported close would zero its period's profit; trial-balance includes them{}.\n",
|
|
if ledger.list_classes().is_empty() {
|
|
""
|
|
} else {
|
|
"; CLASS is each line's project, the projects declared first as QuickBooks classes"
|
|
},
|
|
if company.members.is_empty() {
|
|
""
|
|
} else {
|
|
", and the members' equity statement and the K-1 worksheet show each member's share"
|
|
}
|
|
);
|
|
if !receivable_payable.is_empty() {
|
|
t.push_str(&format!(
|
|
"It names no customer or vendor on its lines to accounts receivable or payable, in {} {}: 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",
|
|
if receivable_payable.len() == 1 { "entry" } else { "entries" },
|
|
receivable_payable.join(", ")
|
|
));
|
|
}
|
|
if !warnings.is_empty() {
|
|
t.push_str(&format!(
|
|
"Written differently in general-journal.iif to fit QuickBooks Desktop (plain ASCII; account names at most {} characters a level, no two alike; a subaccount of another QuickBooks type than its parent's at the top level; account numbers at most {} characters, no two alike; class (project) names at most {} characters, no two alike; memos at most {} characters); every other file keeps the books' text:\n",
|
|
ledger_import::NAME_MAX,
|
|
ledger_import::NUMBER_MAX,
|
|
ledger_import::NAME_MAX,
|
|
ledger_import::MEMO_MAX
|
|
));
|
|
for w in &warnings {
|
|
t.push_str(&format!("- {w}\n"));
|
|
}
|
|
}
|
|
t
|
|
},
|
|
worksheet = if worksheet_1099 {
|
|
format!(
|
|
"1099-worksheet-{year}.csv: each vendor paid in {year}{} or decided for a 1099: what counts toward its 1099 against the year's threshold, what was paid by card or payment network (reported by the processor on Form 1099-K, Treas. Reg. §1.6041-1(a)(1)(iv)), what can't be classified until its payment method is recorded, and its 1099 decision with the reason, who made it, and when it took effect{}.\n",
|
|
if year_end {
|
|
String::new()
|
|
} else {
|
|
format!(" from January 1 through {as_of_s}")
|
|
},
|
|
if include_tins {
|
|
", with each payee's TIN"
|
|
} else {
|
|
""
|
|
}
|
|
)
|
|
} else {
|
|
format!(
|
|
"1099-worksheet-{year}-NOT-INCLUDED.txt: why the year's 1099 worksheet isn't here.\n"
|
|
)
|
|
},
|
|
k1 = if company.members.is_empty() {
|
|
String::new()
|
|
} else if !k1_included {
|
|
"k1-members-NOT-INCLUDED.txt: why the members' K-1 worksheet isn't here.\n".to_string()
|
|
} else {
|
|
let mut k = format!(
|
|
"k1-members.csv: each member's capital account for the year (Schedule K-1 Item L: beginning, contributions in cash and in property, share of income, distributions, ending), with their {}{}.\n",
|
|
if include_tins {
|
|
"taxpayer ID"
|
|
} else {
|
|
"taxpayer ID's last four"
|
|
},
|
|
if company.members.len() > 1 {
|
|
"; the last row totals the members (not a K-1) to tie to Schedule L and the statement of members' equity"
|
|
} else {
|
|
""
|
|
}
|
|
);
|
|
k.push_str(if k1_tax_included {
|
|
"k1-tax-basis-capital.csv: each member's capital account on the tax basis, as Item L reports it (Form 1065 instructions: the tax-basis method), line by line beside the book figures: cash contributed, property at its adjusted basis (IRC §723; a prepaid contract handed over at its book amount, the CPA to confirm), distributions at tax basis, and the reconciliation from book to tax capital (the §704(c) built-in gain or loss of contributed property). A line the books can't know is left blank, marked open, and says what the books know so far and what is needed: the share of income as figured for tax (always the return's), so the ending too; tax depreciation; the tax gain on a disposal (with a contributed property's built-in gain or loss); a basis the books don't hold.\n"
|
|
} else {
|
|
"k1-tax-basis-capital-NOT-INCLUDED.txt: why the tax-basis capital worksheet isn't here.\n"
|
|
});
|
|
let members = &company.members;
|
|
for (i, m) in members.iter().enumerate() {
|
|
if let Some(j) = &m.files_jointly_with
|
|
&& members[..i].iter().all(|o| o.name != *j)
|
|
{
|
|
k.push_str(&format!(
|
|
"{} and {j} file a joint individual return: each receives their own Schedule K-1; both are reported on the one Form 1040 (Schedule E, Part II).\n",
|
|
m.name
|
|
));
|
|
}
|
|
}
|
|
k
|
|
},
|
|
tins = if include_tins {
|
|
let mut t = String::from(
|
|
"\nThis package holds full taxpayer identification numbers: keep it out of email and shared folders, and delete copies once filed.\n",
|
|
);
|
|
if !tin_gaps.is_empty() {
|
|
t.push_str("TINs not included:\n");
|
|
for g in &tin_gaps {
|
|
t.push_str(&format!("- {g}\n"));
|
|
}
|
|
}
|
|
t
|
|
} else {
|
|
String::new()
|
|
},
|
|
receipts = if req.include_receipts {
|
|
format!(
|
|
"receipts/: the {receipt_count} receipts behind the period's entries; index.csv ties each to its entry, date, amount, and SHA-256.\n{}",
|
|
if document_count > 0 {
|
|
"documents/: the company's formation, legal, and tax documents.\n"
|
|
} else {
|
|
""
|
|
}
|
|
)
|
|
} else {
|
|
String::new()
|
|
},
|
|
);
|
|
files.insert(0, ("README.txt".into(), readme.into_bytes()));
|
|
|
|
// Zip it.
|
|
let mut zip_buf = Vec::new();
|
|
{
|
|
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(&mut zip_buf));
|
|
let opts = zip::write::SimpleFileOptions::default();
|
|
for (name, bytes) in &files {
|
|
zip.start_file(name.as_str(), opts)
|
|
.map_err(|e| e.to_string())?;
|
|
use std::io::Write;
|
|
zip.write_all(bytes).map_err(|e| e.to_string())?;
|
|
}
|
|
zip.finish().map_err(|e| e.to_string())?;
|
|
}
|
|
Ok(Package {
|
|
file_name: package_file_name(as_of),
|
|
files,
|
|
zip: zip_buf,
|
|
tin_gaps,
|
|
receipt_count,
|
|
warnings,
|
|
iif_changes,
|
|
})
|
|
}
|