from __future__ import annotations import re from typing import Any TAX_INSTRUCTION = " A separately printed tax row is present even when its amount is zero or negative, including a tax refund. Copy every matching printed tax row. Do not return null or end the list while a matching printed tax row remains." FEE_INSTRUCTION = " A separately printed fee or surcharge row is present even when its amount is zero or negative, including a fee refund. Copy every matching printed fee or surcharge row. Do not return null or end the list while a matching printed fee or surcharge row remains." SERVICE_INSTRUCTION = " Use the row's own printed label. Include an ordinary billed product or service charge. Exclude a row whose own label identifies it as a separately printed tax, fee, surcharge, subtotal, total or payment. A neighboring fee or tax does not exclude an otherwise eligible service row." def prepare( fields: dict[str, dict[str, Any]], *, has_media: bool = False, fee_guidance: bool = False, service_guidance: bool = False, ) -> dict[str, dict[str, Any]]: if has_media: return fields result = dict(fields) for name, spec in fields.items(): item = spec.get("items") instructions = spec.get("instructions", "") if ( spec.get("type") != "list" or not isinstance(item, dict) or item.get("type") != "object" or (not isinstance(instructions, str)) or (not isinstance(item.get("properties"), dict)) ): continue properties = list(item["properties"].values()) if ( len(properties) != 2 or not all((isinstance(child, dict) for child in properties)) or sum((child.get("type") == "number" for child in properties)) != 1 or (sum((child.get("type") == "text" for child in properties)) != 1) or spec.get("knowledge") or any((child.get("knowledge") for child in properties)) ): continue positive = re.split( "[.\\n]|\\bExclude\\b", instructions, maxsplit=1, flags=re.IGNORECASE )[0] if re.match( "\\s*Only\\b[^.\\n]*\\btax(?:es)?\\b[^.\\n]*\\brows\\b", positive, re.IGNORECASE, ) and ( not re.search( "\\b(?:fees?|products?|services?|goods|summar\\w*|calculat\\w*|group\\w*)\\b", positive, re.IGNORECASE, ) ): result[name] = {**spec, "instructions": instructions + TAX_INSTRUCTION} elif ( fee_guidance and re.match( "\\s*Only\\b[^.\\n]*\\bfees?\\b[^.\\n]*\\brows\\b", positive, re.IGNORECASE, ) and ( not re.search( "\\b(?:tax(?:es)?|products?|services?|goods|summar\\w*|calculat\\w*|group\\w*|deduplicat\\w*|aggregat\\w*|combin\\w*|normaliz\\w*)\\b", positive, re.IGNORECASE, ) ) ): result[name] = {**spec, "instructions": instructions + FEE_INSTRUCTION} elif ( service_guidance and re.match( "\\s*Only\\s+billed\\b[^.\\n]*\\brows\\b", positive, re.IGNORECASE ) and re.search( "\\b(?:goods|products?|services?)\\b", positive, re.IGNORECASE ) and ( not re.search( "\\b(?:tax(?:es)?|fees?|surcharges?|summar\\w*|calculat\\w*|group\\w*|deduplicat\\w*|aggregat\\w*|combin\\w*|normaliz\\w*)\\b", positive, re.IGNORECASE, ) ) and re.search( "\\bExclude\\b[^.\\n]*\\b(?:fees?|surcharges?)\\b", instructions, re.IGNORECASE, ) ): result[name] = {**spec, "instructions": instructions + SERVICE_INSTRUCTION} return result