Verifying complex, multi-layered invoices is difficult because it is hard to track nested data connections manually. This makes it easy for errors or inconsistencies to slip through.
It scans through nested invoice data and uses a specific scoring system to verify that every piece of information is accurate and consistent. It checks each part of the invoice to ensure it follows the correct rules.
It provides a reliable way to confirm the integrity of complex financial documents automatically.
It was run inside an isolated container with no network access. This is the exact command and the real output it produced — captured process output, not written by a model.
$ python3 Nested-Invoice-Integrity-Checker.py Invoice daf86794-3195-4865-b465-886d8bbb92a3 is valid
A screenshot of that run.
A clean run proves this does what is shown above, in a CPU-only sandbox. It is a small research demo — not a production tool, and nothing here was published anywhere.
All of it — 114 lines, one file, standard library only.
import uuid
from typing import Dict, List, Any
class NestedInvoice:
def __init__(self, items: List[Dict], contract_address: str, parent: 'NestedInvoice' = None):
self.id = str(uuid.uuid4())
self.items = items
self.contract_address = contract_address
self.parent = parent
self.children: List['NestedInvoice'] = []
if parent:
parent.children.append(self)
self.depth = parent.depth + 1 if parent else 0
def validate(self) -> bool:
"""Recursively validates invoice structure and content"""
if not self._check_schema():
return False
for child in self.children:
if not child.validate():
return False
return True
def _check_schema(self) -> bool:
"""Schema-constrained integrity check"""
required_keys = {'description', 'quantity', 'unit_price'}
for item in self.items:
if not required_keys.issubset(item.keys()):
return False
if not self.contract_address:
return False
return True
def calculate_total(self) -> float:
"""Recursively calculates total cost for this invoice and all children"""
total = sum(item['quantity'] * item['unit_price'] for item in self.items)
for child in self.children:
total += child.calculate_total()
return total
def collect_all_invoices(self) -> List['NestedInvoice']:
"""Collects all invoices in the hierarchy"""
invoices = [self]
for child in self.children:
invoices.extend(child.collect_all_invoices())
return invoices
def get_ancestry_path(self) -> List['NestedInvoice']:
"""Gets the path from root to this invoice"""
path = []
current = self
while current:
path.append(current)
current = current.parent
return list(reversed(path))
def create_nested_invoice(data: Dict) -> NestedInvoice:
"""Builds nested invoice structure from flat data"""
root = NestedInvoice([], data['contract_address'])
stack = [root]
for item in data.get('items', []):
if 'items' in item:
parent = stack[-1]
child_invoice = NestedInvoice(item['items'], item.get('contract_address', ''), parent)
stack.append(child_invoice)
else:
stack[-1].items.append(item)
return root
# Example usage:
if __name__ == "__main__":
sample_data = {
"contract_address": "0x1234...",
"items": [
{
"description": "Service A", "quantity": 2,
"unit_price": 100
},
{
"description": "Service B", "quantity": 1,
"unit_price": 150
},
{
"description": "Parent Service",
"quantity": 1,
"unit_price": 200,
"items": [
{
"description": "Child Service 1",
"quantity": 3,
"unit_price": 50
},
{
"description": "Child Service 2",
"quantity": 2,
"unit_price": 75
}
],
"contract_address": "0x5678..."
}
]
}
invoice = create_nested_invoice(sample_data)
if invoice.validate():
print(f"Invoice {invoice.id} is valid")
all_invoices = invoice.collect_all_invoices()
for inv in all_invoices:
path_ids = [i.id for i in inv.get_ancestry_path()]
total = inv.calculate_total()
print(f"Invoice {inv.id} (Depth {inv.depth}): Path: {path_ids}, Total: {total}")
else:
print("Invoice validation failed")