feat(parties): an email and default terms per side (P4b S4.2)

A customer's email is copied onto an invoice's bill-to snapshot at issue,
and a party's default terms (one as our customer, one as our vendor) are
the fallback a document's due date takes when the document itself names
none (A-13, rules.md B-39). Both are journaled as party.updated facts and
guarded by documents_v2: in books that have not declared it the write is
refused with the upgrade remedy and journaling nothing, and replay gives
the same directory back.

An email must be one plain address: one @, no spaces, no full TIN (the
party text rule already refuses nine digits). Terms are typed and already
canonical, so there is no second spelling to check. A party.updated naming
a field this version does not know is now NewerJournal, never corrupt
(B-27): an older version must say update, not damage; a known field with a
malformed value is still corrupt.
This commit is contained in:
swaits committed 2026-10-27 03:44:24 -06:00
1 parent 1fa77cff02
commit 02cb7f0254
8 files changed
+257 -12

No files matched your search

+18
View File
@@ -4450,6 +4450,9 @@ impl Ledger {
display_name: None,
roles: std::collections::BTreeSet::new(),
website: None,
email: None,
default_terms_as_customer: None,
default_terms_as_vendor: None,
address: None,
notes: None,
archived: false,
@@ -4576,6 +4579,9 @@ impl Ledger {
display_name: None,
roles: roles.iter().copied().collect(),
website: None,
email: None,
default_terms_as_customer: None,
default_terms_as_vendor: None,
address: None,
notes: None,
archived: false,
@@ -4647,6 +4653,9 @@ impl Ledger {
Address(v) => p.address = v,
Notes(v) => p.notes = v,
Archived(b) => p.archived = b,
Email(v) => p.email = v,
DefaultTermsAsCustomer(v) => p.default_terms_as_customer = v,
DefaultTermsAsVendor(v) => p.default_terms_as_vendor = v,
}
Ok(())
}
@@ -4693,6 +4702,15 @@ impl Ledger {
}
}
Archived(true) => {}
Email(v) => match v {
Some(e) => {
crate::parties::check_email("email", e).map_err(LedgerError::InvalidParty)?
}
None => {}
},
// Terms are typed and already canonical (`crate::Terms`), so
// there is nothing here a string could get wrong.
DefaultTermsAsCustomer(_) | DefaultTermsAsVendor(_) => {}
}
Ok(())
}
+63
View File
@@ -230,6 +230,18 @@ pub struct Party {
/// Website.
#[serde(default)]
pub website: Option<String>,
/// Email address, where the company writes to them (A-13): copied onto
/// an invoice's bill-to snapshot at issue.
#[serde(default)]
pub email: Option<String>,
/// The terms this party grants the company as a customer: an invoice's
/// due date falls back to these (A-13).
#[serde(default)]
pub default_terms_as_customer: Option<crate::Terms>,
/// The terms this party, as a vendor, gives the company: a bill's due
/// date falls back to these (A-13).
#[serde(default)]
pub default_terms_as_vendor: Option<crate::Terms>,
/// Business address.
#[serde(default)]
pub address: Option<String>,
@@ -687,6 +699,19 @@ pub fn check_text(field: &str, text: &str) -> Result<(), String> {
Ok(())
}
/// Check an email address: one line of party text ([`check_text`], which
/// refuses a full TIN) in a plain address shape, one `@` and no spaces. Not
/// an RFC grammar test: the books only print it, they never send (A-4).
pub fn check_email(field: &str, text: &str) -> Result<(), String> {
check_text(field, text)?;
if text.chars().filter(|c| *c == '@').count() != 1 || text.chars().any(char::is_whitespace) {
return Err(format!(
"{field} {text:?}: expected one address, like billing@example.com"
));
}
Ok(())
}
impl W9Facts {
/// Check the facts' form. Returns why not.
pub fn check(&self) -> Result<(), String> {
@@ -747,6 +772,44 @@ pub enum PartyField {
Notes(Option<String>),
/// Archived (no longer dealt with) or not.
Archived(bool),
/// The email address (A-13; `None` clears it).
Email(Option<String>),
/// The terms it grants the company as a customer (A-13; `None` clears).
DefaultTermsAsCustomer(Option<crate::Terms>),
/// The terms it gives the company as a vendor (A-13; `None` clears).
DefaultTermsAsVendor(Option<crate::Terms>),
}
impl PartyField {
/// Every field's wire name, as `party.updated` journals it.
pub const FIELDS: [&'static str; 9] = [
"name",
"display_name",
"website",
"address",
"notes",
"archived",
"email",
"default_terms_as_customer",
"default_terms_as_vendor",
];
/// Whether `field` is a field this version knows. A `party.updated`
/// naming another was written by a newer version (rules.md B-27).
pub fn known_field(field: &str) -> bool {
Self::FIELDS.contains(&field)
}
/// Whether the field needs the `documents_v2` feature: the three A-13
/// facts an older version would drop.
pub fn needs_documents_v2(&self) -> bool {
matches!(
self,
PartyField::Email(_)
| PartyField::DefaultTermsAsCustomer(_)
| PartyField::DefaultTermsAsVendor(_)
)
}
}
/// Which of a party's totals to report (rules.md B-35 item 4). Not the
+1 -4
View File
@@ -517,9 +517,7 @@ fn payment_methods_scan_every_text_but_ach_and_wire_lines() {
);
// Unknown kind and unknown field are refused.
assert!(methods(json!([{"kind": "barter", "label": "X", "lines": []}])).is_err());
assert!(
methods(json!([{"kind": "other", "label": "X", "lines": [], "note": "y"}])).is_err()
);
assert!(methods(json!([{"kind": "other", "label": "X", "lines": [], "note": "y"}])).is_err());
// At most eight methods.
let pool = |n: usize| {
Value::Array(
@@ -597,4 +595,3 @@ fn replay_shapes_the_new_kinds() {
);
assert!(shape(methods, &json!([{"kind": "ach", "label": "Bank"}])).is_err());
}
+3
View File
@@ -124,6 +124,9 @@ fn bare_party(name: &str) -> Party {
display_name: None,
roles: BTreeSet::new(),
website: None,
email: None,
default_terms_as_customer: None,
default_terms_as_vendor: None,
address: None,
notes: None,
archived: false,