Test
This commit is contained in:
parent
ae898b1681
commit
842f3809f1
99
main.py
99
main.py
|
|
@ -84,7 +84,7 @@ class LLMClient:
|
|||
|
||||
|
||||
# ==========================================
|
||||
# 2. METADATEN SCHREIBSCHEMA
|
||||
# 2. METADATEN SCHREIBSCHEMA (ERWEITERT FÜR VORLAUFTEXTE)
|
||||
# ==========================================
|
||||
class InvoiceMetadata(BaseModel):
|
||||
invoice_number: str = Field(description="Die Rechnungsnummer nach 'Nummer:' (z.B. R26-219)")
|
||||
|
|
@ -97,10 +97,18 @@ class InvoiceMetadata(BaseModel):
|
|||
recipient_city: str = Field(description="Stadt des Kunden")
|
||||
recipient_email: str = Field(description="E-Mail-Adresse des Kunden (LEER lassen, wenn nicht explizit im Text!)")
|
||||
vat_rate_percent: float = Field(description="Der primäre Umsatzsteuersatz in Prozent (z.B. 19.0)")
|
||||
|
||||
# Neue strukturierte Felder für jegliche Daten VOR Position 1
|
||||
delivery_note_id: str = Field(default="", description="Gefundene Lieferscheinnummer (z.B. LS26-035) VOR Position 1. Sonst leer lassen.")
|
||||
order_id: str = Field(default="", description="Gefundene Nummer der Auftragsbestätigung oder Bestellung (z.B. AB26-080) VOR Position 1. Sonst leer lassen.")
|
||||
contract_id: str = Field(default="", description="Gefundene Nummer von Vergabeangebot, Vertrag oder Ausschreibung (z.B. AN26-130) VOR Position 1. Sonst leer lassen.")
|
||||
service_start_date: str = Field(default="", description="Startdatum eines explizit genannten Einsatz- oder Leistungszeitraums VOR Position 1 im Format YYYY-MM-DD. Sonst leer.")
|
||||
service_end_date: str = Field(default="", description="Enddatum eines explizit genannten Einsatz- oder Leistungszeitraums VOR Position 1 im Format YYYY-MM-DD. Sonst leer.")
|
||||
invoice_note: str = Field(default="", description="Sämtlicher unstrukturierter Fließtext, Beschreibungen, Abnahmekriterien (Acceptance Criteria) oder Projektklauseln, die sich VOR Position 1 befinden. Vollständig mit Zeilenumbrüchen sichern.")
|
||||
|
||||
|
||||
# ==========================================
|
||||
# 3. MATHEMATISCHE FUNKTIONEN
|
||||
# 3. MATHEMATISCHE & PARSING-FUNKTIONEN
|
||||
# ==========================================
|
||||
def kaufmaennisches_runden(betrag, nachkommastellen=2):
|
||||
if not betrag:
|
||||
|
|
@ -156,8 +164,11 @@ def extract_invoice_items_pure_python(text):
|
|||
break
|
||||
if any(prev_lower.startswith(w) for w in ["zwischensumme", "endbetrag", "gesamtbetrag", "umsatzsteuer"]):
|
||||
break
|
||||
|
||||
# REPARATUR: Wenn eine reine Positionsnummer (z.B. "1") erreicht wird, MUSS abgebrochen werden,
|
||||
# um nicht in den globalen Kopf- und Vorlauftext darüber hineinzulaufen!
|
||||
if prev_line.isdigit() and len(prev_line) <= 2:
|
||||
continue
|
||||
break
|
||||
|
||||
desc_lines.insert(0, prev_line)
|
||||
|
||||
|
|
@ -180,6 +191,8 @@ def map_unece_unit_code(unit_str):
|
|||
return "C62"
|
||||
|
||||
def parse_to_strict_xml_date(date_str):
|
||||
if not date_str or str(date_str).strip() == "":
|
||||
return ""
|
||||
clean_str = str(date_str).strip()
|
||||
for fmt in ("%Y-%m-%d", "%d.%m.%Y", "%d-%m-%Y"):
|
||||
try: return datetime.strptime(clean_str, fmt).strftime("%Y-%m-%d")
|
||||
|
|
@ -191,14 +204,13 @@ def generate_compliant_xrechnung(data):
|
|||
CBC = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
|
||||
CAC = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
|
||||
|
||||
# Namespaces explizit registrieren
|
||||
ET.register_namespace('ubl', INVOICE_NS)
|
||||
ET.register_namespace('cbc', CBC)
|
||||
ET.register_namespace('cac', CAC)
|
||||
|
||||
root = ET.Element(f"{{{INVOICE_NS}}}Invoice")
|
||||
|
||||
# Offizielle Profil-IDs für XRechnung 3.0 nach Kosit-Vorgabe
|
||||
# Kopfdaten Definitionen nach Kosit-Vorgabe (XRechnung 3.0)
|
||||
ET.SubElement(root, f"{{{CBC}}}CustomizationID").text = "urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0"
|
||||
ET.SubElement(root, f"{{{CBC}}}ProfileID").text = "urn:fdc:peppol.eu:2017:poacc:billing:01:1.0"
|
||||
|
||||
|
|
@ -213,9 +225,35 @@ def generate_compliant_xrechnung(data):
|
|||
ET.SubElement(root, f"{{{CBC}}}DueDate").text = computed_due
|
||||
|
||||
ET.SubElement(root, f"{{{CBC}}}InvoiceTypeCode").text = "380"
|
||||
|
||||
# Globales Freitextfeld BT-22 (Rechnungserläuterung) für unstrukturierte Vorlauftexte / Vodafone-Klauseln
|
||||
if data.get("invoice_note") and str(data["invoice_note"]).strip() != "":
|
||||
ET.SubElement(root, f"{{{CBC}}}Note").text = str(data["invoice_note"]).strip()
|
||||
|
||||
ET.SubElement(root, f"{{{CBC}}}DocumentCurrencyCode").text = "EUR"
|
||||
ET.SubElement(root, f"{{{CBC}}}BuyerReference").text = str(data["buyer_reference"])
|
||||
|
||||
# Globaler Leistungszeitraum (BG-14)
|
||||
if data.get("service_start_date") and data.get("service_end_date"):
|
||||
period_elem = ET.SubElement(root, f"{{{CAC}}}InvoicePeriod")
|
||||
ET.SubElement(period_elem, f"{{{CBC}}}StartDate").text = parse_to_strict_xml_date(data["service_start_date"])
|
||||
ET.SubElement(period_elem, f"{{{CBC}}}EndDate").text = parse_to_strict_xml_date(data["service_end_date"])
|
||||
|
||||
# Auftragsbestätigung / Bestellungs-Referenz (BT-13)
|
||||
if data.get("order_id") and str(data["order_id"]).strip() != "":
|
||||
order_ref = ET.SubElement(root, f"{{{CAC}}}OrderReference")
|
||||
ET.SubElement(order_ref, f"{{{CBC}}}ID").text = str(data["order_id"]).strip()
|
||||
|
||||
# Lieferschein-Referenz (BT-16)
|
||||
if data.get("delivery_note_id") and str(data["delivery_note_id"]).strip() != "":
|
||||
despatch_ref = ET.SubElement(root, f"{{{CAC}}}DespatchDocumentReference")
|
||||
ET.SubElement(despatch_ref, f"{{{CBC}}}ID").text = str(data["delivery_note_id"]).strip()
|
||||
|
||||
# Vertrags- / Ausschreibungsreferenz (BT-12)
|
||||
if data.get("contract_id") and str(data["contract_id"]).strip() != "":
|
||||
contract_ref = ET.SubElement(root, f"{{{CAC}}}ContractDocumentReference")
|
||||
ET.SubElement(contract_ref, f"{{{CBC}}}ID").text = str(data["contract_id"]).strip()
|
||||
|
||||
vat_rate = float(data['vat_rate_percent'])
|
||||
vat_category = "S" if vat_rate > 0.0 else "E"
|
||||
|
||||
|
|
@ -241,7 +279,6 @@ def generate_compliant_xrechnung(data):
|
|||
legal_supp = ET.SubElement(party_supp, f"{{{CAC}}}PartyLegalEntity")
|
||||
ET.SubElement(legal_supp, f"{{{CBC}}}RegistrationName").text = "scitech it solutions GmbH"
|
||||
|
||||
# KORREKTUR: Saubere, valide Erzeugung des Contact-Blocks mit cbc-Namespaces
|
||||
contact_supp = ET.SubElement(party_supp, f"{{{CAC}}}Contact")
|
||||
ET.SubElement(contact_supp, f"{{{CBC}}}Name").text = "Geschäftsführung"
|
||||
ET.SubElement(contact_supp, f"{{{CBC}}}Telephone").text = "+49 3302 4940537"
|
||||
|
|
@ -303,7 +340,6 @@ def generate_compliant_xrechnung(data):
|
|||
item_elem = ET.SubElement(line, f"{{{CAC}}}Item")
|
||||
ET.SubElement(item_elem, f"{{{CBC}}}Name").text = str(item["description"])
|
||||
|
||||
# Saubere, UBL-konforme Schachtelung der Steuerkategorie innerhalb des Items
|
||||
item_tax = ET.SubElement(item_elem, f"{{{CAC}}}ClassifiedTaxCategory")
|
||||
ET.SubElement(item_tax, f"{{{CBC}}}ID").text = vat_category
|
||||
ET.SubElement(item_tax, f"{{{CBC}}}Percent").text = f"{vat_rate:.2f}"
|
||||
|
|
@ -357,7 +393,6 @@ with col2:
|
|||
if st.button("Daten extrahieren", width="stretch"):
|
||||
with st.spinner("Extrahiere Daten über vLLM-API..."):
|
||||
|
||||
# STUFE 1: Stammdaten via API mit Schema-Zwang
|
||||
schema_dict = InvoiceMetadata.model_json_schema()
|
||||
|
||||
sys_prompt = (
|
||||
|
|
@ -373,7 +408,16 @@ with col2:
|
|||
"- recipient_zip\n"
|
||||
"- recipient_city\n"
|
||||
"- recipient_email\n"
|
||||
"- vat_rate_percent (als Zahl, z.B. 19.0)\n\n"
|
||||
"- vat_rate_percent (als Zahl, z.B. 19.0)\n"
|
||||
"- delivery_note_id (Lieferscheinnummer vor Position 1, falls vorhanden)\n"
|
||||
"- order_id (Auftragsbestätigung/Bestellnummer vor Position 1, falls vorhanden)\n"
|
||||
"- contract_id (Vergabeangebot/Vertragsreferenz vor Position 1, falls vorhanden)\n"
|
||||
"- service_start_date (Leistungszeitraum Start vor Position 1, Format YYYY-MM-DD)\n"
|
||||
"- service_end_date (Leistungszeitraum Ende vor Position 1, Format YYYY-MM-DD)\n"
|
||||
"- invoice_note (Sämtlicher unstrukturierter Freitext, Projektbeschreibungen oder Abnahmekriterien vor Position 1)\n\n"
|
||||
"WICHTIGE REGEL: Analysiere den Text direkt VOR der ersten Tabellenposition '1'. "
|
||||
"Sortiere alle dort vorlaufenden Zeilen vollständig in die dafür vorgesehenen ID-, Datums- und Freitext-Felder ein. "
|
||||
"Schreibe diese vorlaufenden Metadaten NIEMALS in die Beschreibung der ersten Position hinein!\n\n"
|
||||
"Antworte ausschließlich mit dem reinen JSON-Objekt. Schreibe keinen anderen Text."
|
||||
)
|
||||
prompt_meta = f"Rechnungstext:\n{pdf_text}"
|
||||
|
|
@ -405,17 +449,40 @@ with col2:
|
|||
if final_data.get("recipient_email") == "office@scitech-gmbh.de":
|
||||
final_data["recipient_email"] = ""
|
||||
|
||||
# STUFE 2: Zeilenbasiertes Python-Parsing für die Tabelle
|
||||
# Zeilenbasiertes Python-Parsing für die Tabelle (stoppt nun korrekt dank 'break' bei Positionsnummern)
|
||||
final_data["items"] = extract_invoice_items_pure_python(pdf_text)
|
||||
st.session_state.extracted_data = final_data
|
||||
|
||||
if st.session_state.extracted_data:
|
||||
data = st.session_state.extracted_data
|
||||
|
||||
# Standard Rechnungsdaten
|
||||
inv_num = st.text_input("Rechnungsnummer", value=data.get("invoice_number", ""))
|
||||
inv_date = st.text_input("Rechnungsdatum (YYYY-MM-DD)", value=data.get("invoice_date", ""))
|
||||
inv_delivery = st.text_input("Lieferdatum (YYYY-MM-DD)", value=data.get("delivery_date", ""))
|
||||
buyer_ref = st.text_input("Ihr Auftrag / Referenz", value=data.get("buyer_reference", ""))
|
||||
|
||||
# NEU: UI-Eingabefelder für die Daten vor Position 1
|
||||
st.markdown("---")
|
||||
st.markdown("🔗 **Globale Referenzen & Vorlauftexte (Vor Position 1):**")
|
||||
c_ref1, c_ref2, c_ref3 = st.columns(3)
|
||||
with c_ref1:
|
||||
dn_id = st.text_input("Lieferscheinnummer (BT-16)", value=data.get("delivery_note_id", ""))
|
||||
with c_ref2:
|
||||
o_id = st.text_input("Auftragsbestätigung / Bestellung (BT-13)", value=data.get("order_id", ""))
|
||||
with c_ref3:
|
||||
c_id = st.text_input("Vertrag / Vergabeangebot (BT-12)", value=data.get("contract_id", ""))
|
||||
|
||||
c_p1, c_p2 = st.columns(2)
|
||||
with c_p1:
|
||||
s_start = st.text_input("Leistungszeitraum Start (YYYY-MM-DD)", value=data.get("service_start_date", ""))
|
||||
with c_p2:
|
||||
s_end = st.text_input("Leistungszeitraum Ende (YYYY-MM-DD)", value=data.get("service_end_date", ""))
|
||||
|
||||
i_note = st.text_area("Globale Rechnungserläuterung / Freitext (BT-22)", value=data.get("invoice_note", ""), height=150)
|
||||
st.markdown("---")
|
||||
|
||||
# Adressdaten
|
||||
c_s, c_r = st.columns(2)
|
||||
with c_s:
|
||||
st.markdown("**Absender (Verkäufer):**")
|
||||
|
|
@ -459,11 +526,19 @@ with col2:
|
|||
"buyer_reference": buyer_ref, "recipient_name": r_name, "recipient_email": r_email,
|
||||
"recipient_street": r_street, "recipient_zip": r_zip, "recipient_city": r_city,
|
||||
"items": edited_items, "total_net_amount": computed_net_amount, "total_vat_amount": computed_vat_amount,
|
||||
"vat_rate_percent": vat_rate, "total_gross_amount": computed_gross_amount, "iban": "DE06120700240456207000"
|
||||
"vat_rate_percent": vat_rate, "total_gross_amount": computed_gross_amount, "iban": "DE06120700240456207000",
|
||||
# Neue Felder für den XML-Builder anhängen
|
||||
"delivery_note_id": dn_id,
|
||||
"order_id": o_id,
|
||||
"contract_id": c_id,
|
||||
"service_start_date": s_start,
|
||||
"service_end_date": s_end,
|
||||
"invoice_note": i_note
|
||||
}
|
||||
st.download_button(label="📥 Download XML", data=generate_compliant_xrechnung(final_invoice_dict), file_name=f"XRechnung_{inv_num}.xml", mime="text/xml")
|
||||
|
||||
# ==========================================
|
||||
# DYNAMISCHER FOOTER (RECHTLICHE HINWEISE & SYSTEM-INFO)
|
||||
# DYNAMISCHER FOOTER
|
||||
# ==========================================
|
||||
st.markdown("---")
|
||||
st.caption(
|
||||
|
|
|
|||
Loading…
Reference in New Issue