fix(core,mcp,store): a phone number is kept and a ZIP+4 is kept; only a tax id's printed shapes are refused in a phone

A telephone is ten digits or more, so the strict reading of a party's fields
(any run of nine digits) refused every phone number: the bill-to snapshot's
check, new with the free-text change, refused `503-555-0100` and failed the
window's invoice test. The party's own phone (since D-55) and the tool's
bill-to phone had always been refused the same way; only 7-digit test
numbers hid it.

A phone field (a party's, a bill-to's, the seller's) is now its own class,
read as a reference is: refuse the printed SSN 3-2-4 and EIN 2-7 and nothing
else, since a nine-digit national number is ordinary. A postal address
keeps the strict reading except a ZIP+4 (`98101-1234`, five and four, nine
digits), a postal code. The formation state's entity number is a
registration number (a Washington UBI is nine digits) and is read as a
reference. The tools' table, the engine, and the audit's keys agree; the
audit reads `phone` and `fax` as a phone.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-21 20:50:23 -07:00
1 parent 1dbca94bd2
commit c206652abe
12 files changed
+725 -52

No files matched your search

+44 -15
View File
@@ -1285,35 +1285,64 @@ pub fn check_document_text(
}
/// The party details a new invoice prints: the customer's (a party's
/// fields: strict, no run of nine digits, [`crate::parties::check_text`])
/// and the seller's (the company's own, free text: no tax id's shape).
/// fields: strict, no run of nine digits, [`crate::parties::check_text`];
/// an address allows a ZIP+4, [`crate::parties::check_address`]; a phone
/// is refused only in a tax id's printed shapes,
/// [`crate::parties::check_phone`]) and the seller's (the company's own,
/// free text: no tax id's shape; its phone as any phone).
pub fn check_snapshots(
bill_to: Option<&PartySnapshot>,
seller: Option<&PartySnapshot>,
) -> Result<(), LedgerError> {
let fields = |s: &'_ PartySnapshot| -> [(&'static str, Option<String>); 8] {
/// How a field of a snapshot is read.
#[derive(Clone, Copy, PartialEq)]
enum Kind {
Plain,
Address,
Phone,
}
let fields = |s: &'_ PartySnapshot| -> [(&'static str, Kind, Option<String>); 8] {
[
("the bill-to name", Some(s.name.clone())),
("the bill-to address", s.address.clone()),
("the bill-to email", s.email.clone()),
("the bill-to phone", s.phone.clone()),
("the bill-to contact", s.contact.clone()),
("the billing address", s.billing_address.clone()),
("the shipping address", s.shipping_address.clone()),
("the billing email", s.billing_email.clone()),
("the bill-to name", Kind::Plain, Some(s.name.clone())),
("the bill-to address", Kind::Address, s.address.clone()),
("the bill-to email", Kind::Plain, s.email.clone()),
("the bill-to phone", Kind::Phone, s.phone.clone()),
("the bill-to contact", Kind::Plain, s.contact.clone()),
(
"the billing address",
Kind::Address,
s.billing_address.clone(),
),
(
"the shipping address",
Kind::Address,
s.shipping_address.clone(),
),
("the billing email", Kind::Plain, s.billing_email.clone()),
]
};
if let Some(s) = bill_to {
for (field, text) in fields(s) {
for (field, kind, text) in fields(s) {
if let Some(t) = text {
crate::parties::check_text(field, &t).map_err(LedgerError::InvalidParty)?;
match kind {
Kind::Plain => crate::parties::check_text(field, &t),
Kind::Address => crate::parties::check_address(field, &t),
Kind::Phone => crate::parties::check_phone(field, &t),
}
.map_err(LedgerError::InvalidParty)?;
}
}
}
if let Some(s) = seller {
for (field, text) in fields(s) {
for (field, kind, text) in fields(s) {
if let Some(t) = text {
LedgerError::free_text(&field.replace("bill-to", "seller"), &t)?;
let field = field.replace("bill-to", "seller");
if kind == Kind::Phone {
crate::settings::check_phone_text(&field, &t)
.map_err(LedgerError::FreeTextHoldsTin)?;
} else {
LedgerError::free_text(&field, &t)?;
}
}
}
}
+17 -6
View File
@@ -5070,6 +5070,16 @@ impl Ledger {
Some(t) => crate::parties::check_text(f, t).map_err(LedgerError::InvalidParty),
None => Ok(()),
};
// A postal address allows a ZIP+4; a phone or fax number is refused
// only in a tax id's printed shapes (WB-partner-15).
let address = |f: &str, v: &Option<String>| match v {
Some(t) => crate::parties::check_address(f, t).map_err(LedgerError::InvalidParty),
None => Ok(()),
};
let phone = |f: &str, v: &Option<String>| match v {
Some(t) => crate::parties::check_phone(f, t).map_err(LedgerError::InvalidParty),
None => Ok(()),
};
match field {
Name(n) => {
if n.trim().is_empty() {
@@ -5087,7 +5097,7 @@ impl Ledger {
}
DisplayName(v) => text("display_name", v)?,
Website(v) => text("website", v)?,
Address(v) => text("address", v)?,
Address(v) => address("address", v)?,
Notes(v) => text("notes", v)?,
Archived(false) => {
if let Some(other) = self.party_by_name(&p.name)
@@ -5105,12 +5115,13 @@ impl Ledger {
crate::parties::check_email("email", e).map_err(LedgerError::InvalidParty)?;
}
}
// The D-55 contact block: two addresses and a phone like any
// other text (no full TIN), the billing email an address.
Phone(v) => text("phone", v)?,
// The D-55 contact block: two addresses (a ZIP+4 allowed) and a
// phone (refused only in a tax id's printed shapes), the billing
// email an address.
Phone(v) => phone("phone", v)?,
Contact(v) => text("contact", v)?,
BillingAddress(v) => text("billing_address", v)?,
ShippingAddress(v) => text("shipping_address", v)?,
BillingAddress(v) => address("billing_address", v)?,
ShippingAddress(v) => address("shipping_address", v)?,
BillingEmail(v) => {
if let Some(e) = v {
crate::parties::check_email("billing_email", e)
+44
View File
@@ -717,6 +717,50 @@ pub fn check_text(field: &str, text: &str) -> Result<(), String> {
Ok(())
}
/// [`has_full_tin`] for a postal address: the same strict reading, except
/// that a ZIP+4 (five digits, a hyphen, four digits, standing alone:
/// `98101-1234`) is a postal code, not a tax id, though it is nine digits.
pub fn has_full_tin_in_address(text: &str) -> bool {
let chars: Vec<char> = text.chars().collect();
let runs = if REPLAYING.with(std::cell::Cell::get) {
journaled_tin_runs(&chars)
} else {
full_tin_runs(&chars)
};
runs.iter().any(|&(first, last)| {
let run = &chars[first..=last];
let zip_plus_4 = run.len() == 10
&& run[5] == '-'
&& run
.iter()
.enumerate()
.all(|(i, c)| i == 5 || c.is_ascii_digit());
!zip_plus_4
})
}
/// [`check_text`] for a postal address: a ZIP+4 is allowed
/// ([`has_full_tin_in_address`]); every other run of nine digits is refused.
pub fn check_address(field: &str, text: &str) -> Result<(), String> {
if has_full_tin_in_address(text) {
return Err(format!(
"{field} looks like it holds a full taxpayer identification number; \
the books keep only its last four digits (the full number belongs in the vault)"
));
}
// The length and control-character bounds are the other text's.
check_note_form(field, text)
}
/// Check a telephone or fax number: not a tax id's printed shapes (an SSN's
/// 3-2-4, an EIN's 2-7: [`crate::settings::check_phone_text`]); every other
/// number is a phone's, whatever its digits or grouping. At most 500
/// characters, no control characters except newline.
pub fn check_phone(field: &str, text: &str) -> Result<(), String> {
crate::settings::check_phone_text(field, text)?;
check_note_form(field, text)
}
/// Check a free text field: no tax id's shape (a nine-digit number standing
/// alone, an SSN's 3-2-4, an EIN's 2-7: [`crate::settings::check_free_text`]),
/// at most 500 characters, no control characters except newline. For a
+31 -1
View File
@@ -759,6 +759,19 @@ pub fn holds_printed_tin_in_reference(text: &str) -> bool {
!printed_tin_spans(text, Reading::Reference).1.is_empty()
}
/// [`holds_printed_tin`] for a telephone or fax number: only the printed
/// SSN (3-2-4) and EIN (2-7) forms count. A phone number is ten digits or
/// more (a country code and an area code), and a national number of nine
/// is ordinary in many countries, so a run of nine or more digits proves
/// nothing there; `206-555-0142`, `(206) 555-0142`, `+1 206 555 0142`,
/// `+44 20 7946 0958` and an extension are all kept (WB-partner-15). It
/// reads as a reference does; it has its own name so that the fields that
/// hold a phone (a party's, a bill-to's, the tools' table, the audit's
/// keys) say so, and one change here moves them all.
pub fn holds_printed_tin_in_phone(text: &str) -> bool {
holds_printed_tin_in_reference(text)
}
/// [`holds_printed_tin`] for a web address: the printed SSN (3-2-4) and
/// EIN (2-7) forms count as there, but a bare run of nine digits joined
/// to a letter or to the URL punctuation `/ = ? & #` does not: it is an
@@ -1012,6 +1025,16 @@ pub fn check_reference_text(field: &str, text: &str) -> Result<(), String> {
Ok(())
}
/// A telephone or fax number that may be kept: not a tax id's printed
/// shapes ([`holds_printed_tin_in_phone`]). Returns why not, in
/// [`tin_refusal`]'s words. Never refuses during a replay (BS-entry-4).
pub fn check_phone_text(field: &str, text: &str) -> Result<(), String> {
if !crate::parties::is_replaying() && holds_printed_tin_in_phone(text) {
return Err(tin_refusal(field));
}
Ok(())
}
fn no_tin(what: &str, s: &str) -> Result<(), String> {
if holds_printed_tin(s) {
return Err(format!(
@@ -1287,7 +1310,14 @@ pub fn check(key: &Key, v: &Value) -> Result<Value, String> {
let name = key.name;
match key.kind {
Kind::Text => {
let text = line_text(name, want_str(name, v)?, MAX_TEXT)?;
// The state's entity number is a registration number (a
// Washington UBI is nine digits): a reference, refused only in
// a tax id's printed shapes (WB-partner-15).
let text = if name == "company.state_entity_number" {
reference_line_text(name, want_str(name, v)?, MAX_TEXT)?
} else {
line_text(name, want_str(name, v)?, MAX_TEXT)?
};
// One key's own bound: an invoice number's prefix is short and
// visible (a space or a control character would break the
// number's reading, and the file's).