diff --git a/main.py b/main.py index 30c569c..09a0364 100644 --- a/main.py +++ b/main.py @@ -502,96 +502,84 @@ with col2: r_zip = st.text_input("PLZ (Kunde)", value=data.get("recipient_zip", "")) r_city = st.text_input("Stadt (Kunde)", value=data.get("recipient_city", "")) -# ========================================== -# 6. MODULARES POSITIONEN-FRONTEND (VOLLBREITE OHNE TRUNCATION) -# ========================================== -if st.session_state.extracted_data: - data = st.session_state.extracted_data - - st.markdown("### 3. Positionen") - - # Button zum manuellen Hinzufügen einer leeren Position - if st.button("➕ Neue Position hinzufügen"): - data["items"].append({"name": "Neue Dienstleistung", "description": "", "quantity": 1.0, "unit": "Stk.", "unit_price": 0.0}) - st.rerun() + # 🔥 NEU: Die Positionen-Karten befinden sich ab jetzt genau hier innerhalb von Spalte 2! + st.markdown("### 3. Positionen") - edited_items = [] - - # Jede Position bekommt eine eigene, vollflächige Karte (Expander) - for idx, item in enumerate(data.get("items", [])): - with st.expander(f"📋 Position {idx+1}: {item.get('name', 'Dienstleistung')}", expanded=True): + if st.button("➕ Neue Position hinzufügen"): + data["items"].append({"name": "Neue Dienstleistung", "description": "", "quantity": 1.0, "unit": "Stk.", "unit_price": 0.0}) + st.rerun() - # Löschen-Button für diese spezifische Position - c_del, _ = st.columns([1, 11]) - with c_del: - if st.button("🗑️ Position löschen", key=f"del_btn_{idx}"): - data["items"].pop(idx) - st.rerun() - - # Zeile 1: Name und Beschreibung nebeneinander (Beschreibung als echtes, umbrechendes Textfeld!) - c_name, c_desc = st.columns([1, 2]) - with c_name: - item_name = st.text_input("Artikelname (BT-153)", value=item.get("name", ""), key=f"item_name_{idx}") - with c_desc: - item_description = st.text_area("Zusatzbeschreibung (BT-154)", value=item.get("description", ""), key=f"item_desc_{idx}", height=68) + edited_items = [] + + for idx, item in enumerate(data.get("items", [])): + with st.expander(f"📋 Position {idx+1}: {item.get('name', 'Dienstleistung')}", expanded=True): - # Zeile 2: Numerische Werte sauber aufgeteilt - c_qty, c_unit, c_price, c_total = st.columns(4) - with c_qty: - item_quantity = st.number_input("Menge", value=float(item.get("quantity") or 0.0), format="%.4f", key=f"item_qty_{idx}") - with c_unit: - item_unit = st.text_input("Einheit", value=item.get("unit", "Stk."), key=f"item_unit_{idx}") - with c_price: - item_price = st.number_input("Einzelpreis (€)", value=float(item.get("unit_price") or 0.0), format="%.2f", key=f"item_price_{idx}") - with c_total: - # Automatische Live-Berechnung des Zeilen-Gesamtwerts - raw_item_total = item_quantity * item_price - item_total_amount = kaufmaennisches_runden(raw_item_total) - st.markdown("**Gesamtbetrag (€):**") - st.markdown(f"#### {item_total_amount:.2f} €") + c_del, _ = st.columns([1, 4]) + with c_del: + if st.button("🗑️ Position löschen", key=f"del_btn_{idx}"): + data["items"].pop(idx) + st.rerun() - # Speichert den editierten Zustand dieses Blocks ab - edited_items.append({ - "name": item_name, - "description": item_description, - "quantity": item_quantity, - "unit": item_unit, - "unit_price": item_price, - "total_amount": item_total_amount - }) - - st.markdown("---") - vat_rate = st.number_input("Steuersatz (%)", value=float(data.get("vat_rate_percent") or 19.0)) - - # Globale Summenberechnung - computed_net_amount = sum(item["total_amount"] for item in edited_items) - raw_vat_amount = computed_net_amount * (vat_rate / 100.0) - computed_vat_amount = kaufmaennisches_runden(raw_vat_amount) - computed_gross_amount = kaufmaennisches_runden(computed_net_amount + computed_vat_amount) - - st.markdown("**Berechnete Summen:**") - c_net_ui, c_vat_ui, c_gross_ui = st.columns(3) - with c_net_ui: st.metric("Netto-Gesamt (€)", f"{computed_net_amount:.2f}") - with c_vat_ui: st.metric("MwSt-Gesamt (€)", f"{computed_vat_amount:.2f}") - with c_gross_ui: st.metric("Brutto-Zahlbetrag (€)", f"{computed_gross_amount:.2f}") - - st.markdown("---") - if st.button("🔒 XRechnung exportieren", type="primary", width="stretch"): - final_invoice_dict = { - "invoice_type_code": selected_type_code, - "invoice_number": inv_num, "invoice_date": inv_date, "delivery_date": inv_delivery, - "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", - "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") + c_name, c_desc = st.columns([1, 1]) + with c_name: + item_name = st.text_input("Artikelname (BT-153)", value=item.get("name", ""), key=f"item_name_{idx}") + with c_desc: + item_description = st.text_area("Zusatzbeschreibung (BT-154)", value=item.get("description", ""), key=f"item_desc_{idx}", height=68) + + c_qty, c_unit, c_price, c_total = st.columns(4) + with c_qty: + # 🔥 OPTIMIERUNG: Format auf %.1f gesetzt für genau eine Nachkommastelle + item_quantity = st.number_input("Menge", value=float(item.get("quantity") or 0.0), format="%.1f", key=f"item_qty_{idx}") + with c_unit: + item_unit = st.text_input("Einheit", value=item.get("unit", "Stk."), key=f"item_unit_{idx}") + with c_price: + item_price = st.number_input("Einzelpreis (€)", value=float(item.get("unit_price") or 0.0), format="%.2f", key=f"item_price_{idx}") + with c_total: + raw_item_total = item_quantity * item_price + item_total_amount = kaufmaennisches_runden(raw_item_total) + st.markdown("**Gesamt betrag (€):**") + st.markdown(f"**{item_total_amount:.2f} €**") + + edited_items.append({ + "name": item_name, + "description": item_description, + "quantity": item_quantity, + "unit": item_unit, + "unit_price": item_price, + "total_amount": item_total_amount + }) + + st.markdown("---") + vat_rate = st.number_input("Steuersatz (%)", value=float(data.get("vat_rate_percent") or 19.0)) + + computed_net_amount = sum(item["total_amount"] for item in edited_items) + raw_vat_amount = computed_net_amount * (vat_rate / 100.0) + computed_vat_amount = kaufmaennisches_runden(raw_vat_amount) + computed_gross_amount = kaufmaennisches_runden(computed_net_amount + computed_vat_amount) + + st.markdown("**Berechnete Summen:**") + c_net_ui, c_vat_ui, c_gross_ui = st.columns(3) + with c_net_ui: st.metric("Netto-Gesamt (€)", f"{computed_net_amount:.2f}") + with c_vat_ui: st.metric("MwSt-Gesamt (€)", f"{computed_vat_amount:.2f}") + with c_gross_ui: st.metric("Brutto-Zahlbetrag (€)", f"{computed_gross_amount:.2f}") + + st.markdown("---") + if st.button("🔒 XRechnung exportieren", type="primary", width="stretch"): + final_invoice_dict = { + "invoice_type_code": selected_type_code, + "invoice_number": inv_num, "invoice_date": inv_date, "delivery_date": inv_delivery, + "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", + "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