Unit S-E rewrote each `B-n` citation to its bookspec id but kept the
sub-reference after it ("item 7", "rule 11", "INVARIANT 2"). Those
numbers counted parts of the retired rules.md text, so `BS-statement-5
item 7` pointed at finished and abandoned projects, not at the snapshot
it meant. Bookspec numbers a record's parts from 1 as `statement`,
`invariant` and `scenario`; every sub-reference now says so.
The map was built by content, not by position: each old item,
INVARIANT or SCENARIO was read in `git show ca8d676d5^:docs/spec/rules.md`
(the last full rule text), found in the pinned records
(bookspec bc92cf3) and in docs/spec/claims/, and each reference was
read in its context before it was mapped. Where the old numbering still
survives as the "Rule n" labels inside bookspec's statements (the cash
flow, party totals) the new number is that statement's position; where
a record carries the whole rule differently (the asset depreciation, the
IIF file, the arap invariants) the reference follows the content. Where
bookspec keeps no counterpart, the product rule that states it is cited
instead: WB-statement-5 (a project's facts and names), -7 (the snapshot),
WB-arap-4 (the document_standing feature), -8 (the issued PDF), -15 (a
payment event names its line), WB-ledger-15 (BS-ledger-12 is retired).
A reference that had pointed at the wrong part is corrected, for example
the discount-reason references that said BS-arap-6 INVARIANT 2 (a
bound) now say invariant 6 (the discount cap and date).
Only the reference tokens and their sentence change; no paragraph is
reflowed. A reference wrapped across a comment prefix keeps its wrap.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
143 lines
6.0 KiB
Rust
143 lines
6.0 KiB
Rust
//! `books.write_off_invoice`'s group: bad debts (plan P4b S7;
|
|
//! BS-arap-5). A file of its own (plan 8.2 U0a) so S4.4's document tools and
|
|
//! this one land apart: a tool is added to its group's file and nowhere
|
|
//! else.
|
|
|
|
use super::*;
|
|
|
|
/// An invoice's figures after a write-off, as the tool answers them: read
|
|
/// from its tagged lines that stand (BS-arap-2), at the end of the
|
|
/// books.
|
|
fn write_off_out(
|
|
ledger: &Ledger,
|
|
invoice: u64,
|
|
entry_id: u64,
|
|
) -> Result<WriteOffInvoiceOut, String> {
|
|
let st = ledger
|
|
.doc_state(ledger_core::DocRef::Invoice(invoice), NaiveDate::MAX)
|
|
.map_err(|e| e.to_string())?
|
|
.ok_or_else(|| format!("invoice {invoice} not found"))?;
|
|
Ok(WriteOffInvoiceOut {
|
|
preview: None,
|
|
entry_id,
|
|
document: WriteOffDocumentOut {
|
|
r#ref: format!("invoice:{invoice}"),
|
|
status: st.status.key().to_string(),
|
|
paid: money(st.paid),
|
|
written_off: money(st.written_off),
|
|
outstanding: money(st.outstanding),
|
|
},
|
|
})
|
|
}
|
|
|
|
#[tool_router(router = write_offs_router, vis = "pub(super)")]
|
|
impl BooksServer {
|
|
#[tool(
|
|
name = "books.write_off_invoice",
|
|
title = "Write off an invoice",
|
|
description = "Write off an invoice that will not be collected (a bad debt): Dr Bad Debt Expense 6990 / Cr Accounts Receivable, the receivable line tagged with the invoice, so it reads written_off and its figures stay derived. The evidence of worthlessness (collection correspondence, a bankruptcy notice) is filed with it in the same call: upload_id, or existing_attachment_id for one already filed. amount (default: all it owes) may be partial. Refused on cash-basis books, above what it owes, before its date or in a closed period. To recover it: reverse the write-off (books.reverse_entry; the invoice reopens), then books.receive_payment. Without confirmed=true, a preview.",
|
|
annotations(
|
|
read_only_hint = false,
|
|
destructive_hint = true,
|
|
open_world_hint = false
|
|
)
|
|
)]
|
|
async fn write_off_invoice(
|
|
&self,
|
|
params: Parameters<WriteOffInvoiceParams>,
|
|
) -> Result<Json<WriteOffInvoiceOut>, String> {
|
|
let p = params.0;
|
|
let date = parse_date(&p.date)?;
|
|
let mut store = self.lock()?;
|
|
// The event type is new: books that haven't declared documents_v2
|
|
// refuse it with the upgrade remedy, journaling nothing (BS-format-1).
|
|
store
|
|
.ensure_feature(ledger_store::format::DOCUMENTS_V2)
|
|
.map_err(|e| e.to_string())?;
|
|
let doc = ledger_core::DocRef::Invoice(p.invoice);
|
|
let owed = store
|
|
.ledger()
|
|
.doc_state(doc, date)
|
|
.map_err(|e| e.to_string())?
|
|
.ok_or_else(|| format!("invoice {} is dated after {date}", p.invoice))?
|
|
.outstanding;
|
|
let amount = match p.amount.as_deref() {
|
|
Some(a) => parse_amount(a)?,
|
|
None => owed,
|
|
};
|
|
// The evidence is the point of a write-off (BS-arap-5 statement 9): one
|
|
// of the two, and the preview says so too rather than posting
|
|
// without it later.
|
|
match (p.upload_id.is_some(), p.existing_attachment_id.is_some()) {
|
|
(true, true) | (false, false) => {
|
|
return Err(
|
|
"give the evidence once: upload_id (a file to file with the write-off) or \
|
|
existing_attachment_id (one already filed), exactly one"
|
|
.into(),
|
|
);
|
|
}
|
|
_ => {}
|
|
}
|
|
let digest = self.preview_of(
|
|
&store,
|
|
"books.write_off_invoice",
|
|
p.confirmed.unwrap_or(false),
|
|
p.preview.as_deref(),
|
|
)?;
|
|
if !p.confirmed.unwrap_or(false) {
|
|
// The engine on a clone: every refusal the post would give, and
|
|
// the figures the write-off would leave. Nothing is written.
|
|
let mut probe = store.ledger().clone();
|
|
probe
|
|
.write_off(p.invoice, date, amount, &p.reason)
|
|
.map_err(|e| e.to_string())?;
|
|
let mut out = write_off_out(&probe, p.invoice, 0)?;
|
|
out.preview = Some(digest);
|
|
return Ok(Json(out));
|
|
}
|
|
let entry_id = match p.upload_id.as_deref() {
|
|
Some(upload_id) => {
|
|
let file = self.take_file(files::Given {
|
|
upload_id,
|
|
purposes: files::FILED,
|
|
})?;
|
|
let (file_name, mime_type) = named(None, None, &file)?;
|
|
let (bytes, claim) = (file.bytes, file.claim);
|
|
self.refuse_tin_file(&store, &bytes, &mime_type, &file_name)?;
|
|
let (entry_id, _) = store
|
|
.write_off_invoice(
|
|
p.invoice,
|
|
date,
|
|
amount,
|
|
&p.reason,
|
|
Some(ledger_store::WriteOffEvidence::File {
|
|
file_name: &file_name,
|
|
mime_type: &mime_type,
|
|
bytes: &bytes,
|
|
description: Some("Evidence of worthlessness"),
|
|
}),
|
|
)
|
|
.map_err(|e| e.to_string())?;
|
|
files::filed(claim);
|
|
entry_id
|
|
}
|
|
None => {
|
|
let id = p
|
|
.existing_attachment_id
|
|
.expect("checked: one of the two is given");
|
|
let (entry_id, _) = store
|
|
.write_off_invoice(
|
|
p.invoice,
|
|
date,
|
|
amount,
|
|
&p.reason,
|
|
Some(ledger_store::WriteOffEvidence::Existing(id)),
|
|
)
|
|
.map_err(|e| e.to_string())?;
|
|
entry_id
|
|
}
|
|
};
|
|
Ok(Json(write_off_out(store.ledger(), p.invoice, entry_id)?))
|
|
}
|
|
}
|