new-site/scripts/document_gen/templates/diag_mcs150.py
justin d5e66786a2 mcs150: enrich intake from FMCSA carrier census before PDF fill
The MCS-150 biennial update re-confirms the carrier's existing FMCSA
record. Previously the PDF filler only had whatever the intake form
collected; rescued/sparse orders (or orders where the carrier's data
lives in FMCSA, not the intake) produced near-empty forms. Now we pull
the carrier census (legal name, address, EIN, fleet counts) from the
FMCSA carrier API and merge it under any customer-provided intake values
(customer edits win), so the form is pre-filled with the carrier's
current registered data. Refactored the FMCSA fetch into a shared
_fetch_fmcsa_carrier helper used by both enrichment and status check.
2026-06-10 12:35:52 -05:00

55 lines
2 KiB
Python

"""Diagnostic: inspect the actual MCS-150 fill output.
Reports, per page: the AcroForm fields present (name + value + rect), and the
rendered text, so we can see WHERE data lands vs WHERE the blank/example fields
are. Run in the workers container against the live template + a fresh fill.
"""
import sys, json
sys.path.insert(0, "/app")
from pypdf import PdfReader
TEMPLATE = "/app/docs/MCS-150 Form.pdf"
def field_rows(reader, label):
print(f"\n===== {label} =====")
for i, pg in enumerate(reader.pages):
annots = pg.get("/Annots")
if not annots:
continue
annots = annots.get_object() if hasattr(annots, "get_object") else annots
rows = []
for a in annots:
o = a.get_object()
t = o.get("/T")
if t is None and o.get("/Parent"):
t = o["/Parent"].get_object().get("/T")
v = o.get("/V")
rect = o.get("/Rect")
ft = o.get("/FT")
if t:
rows.append((str(t), str(ft), str(v) if v is not None else "", [round(float(x)) for x in rect] if rect else None))
if rows:
print(f"-- PDF page {i+1} ({len(rows)} fields) --")
for name, ft, val, rect in rows:
vstr = f" = {val!r}" if val else ""
print(f" {name} [{ft}]{vstr} rect={rect}")
# 1) the template as-is — what fields + any example values?
r = PdfReader(TEMPLATE)
field_rows(r, "TEMPLATE (original)")
# 2) a fresh fill
from scripts.document_gen.templates.mcs150_pdf_filler import fill_mcs150
intake = {
"dot_number": "1609564", "legal_name": "MITCHELL W ALLEN",
"dba_name": "", "entity_type": "sole_proprietorship",
"business_street": "123 Main St", "business_city": "Town",
"business_state": "TX", "business_zip": "75001",
"phone": "5125551234", "ein": "123456789",
"signer_name": "Mitchell W Allen", "signer_title": "Owner",
"power_units": "2", "drivers": "1",
}
p = fill_mcs150(intake, order_number="DIAG")
print("\nfilled file:", p)
field_rows(PdfReader(p), "FILLED")