git.schokokeks.org
Repositories
Help
Report an Issue
invoice.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
11c4134
Branches
Tags
factur-x_localdata
master
invoice.git
Invoice
InvoiceToZUGFeRD.py
WiP
Bernd Wurst
commited
11c4134
at 2024-02-14 08:42:39
InvoiceToZUGFeRD.py
Blame
History
Raw
# -*- coding: utf-8 -*- # (C) 2011 by Bernd Wurst <bernd@schokokeks.org> # This file is part of Bib2011. # # Bib2011 is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Bib2011 is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Bib2011. If not, see <http://www.gnu.org/licenses/>. import os.path, sys from decimal import Decimal import datetime import re # Search for included submodule python-drafthorse atoms = os.path.abspath(os.path.dirname(__file__)).split('/') dir = '' while atoms: candidate = os.path.join('/'.join(atoms), 'external/python-drafthorse') if os.path.exists(candidate): dir = candidate break atoms = atoms[:-1] sys.path.insert(0, dir) from drafthorse.models.document import Document from drafthorse.models.accounting import ApplicableTradeTax from drafthorse.models.tradelines import LineItem from .InvoiceObjects import InvoiceTable, InvoiceText, InvoiceImage, RECHNUNG, GUTSCHRIFT, KORREKTUR, \ VAT_REGULAR, VAT_KLEINUNTERNEHMER, VAT_INNERGEM, PAYMENT_UEBERWEISUNG, PAYMENT_LASTSCHRIFT from drafthorse.models.party import TaxRegistration, URIUniversalCommunication from drafthorse.models.payment import PaymentTerms from drafthorse.models.note import IncludedNote from drafthorse.pdf import attach_xml def InvoiceToXML(invoice): doc = Document() doc.context.guideline_parameter.id = "urn:cen.eu:en16931:2017#conformant#urn:factur-x.eu:1p0:en16931" doc.header.id = invoice.id # Typecodes: # 380: Handelsrechnungen # 381: Gutschrift # 384: Korrekturrechnung # 389: Eigenrechnung (vom Käufer im Namen des Lieferanten erstellt). # 261: Selbstverfasste Gutschrift. # 386: Vorauszahlungsrechnung # 326: Teilrechnung # 751: Rechnungsinformation - KEINE RECHNUNG if invoice.type == RECHNUNG: doc.header.type_code = "380" elif invoice.type == GUTSCHRIFT: doc.header.type_code = "381" elif invoice.type == KORREKTUR: doc.header.type_code = "384" else: raise TypeError("Unbekannter Rechnungstyp, kann kein XML erstellen") doc.header.issue_date_time = invoice.date # Seller-Address if invoice.seller['trade_name']: pass # FIXME: specified_legal_organization ist in der Library nicht implementiert, pull request ist vorhanden #doc.trade.agreement.seller.specified_legal_organization.trade_name = invoice.seller['trade_name'] doc.trade.agreement.seller.name = invoice.seller['name'] doc.trade.agreement.seller.address.country_id = invoice.seller['address']['country_id'] doc.trade.agreement.seller.address.postcode = invoice.seller['address']['postcode'] doc.trade.agreement.seller.address.city_name = invoice.seller['address']['city_name'] doc.trade.agreement.seller.address.line_one = invoice.seller['address']['line1'] doc.trade.agreement.seller.address.line_two = invoice.seller['address']['line2'] doc.trade.agreement.seller.address.line_three = invoice.seller['address']['line3'] if invoice.seller_vat_id: tax_reg = TaxRegistration() tax_reg.id = ('VA', invoice.seller_vat_id) doc.trade.agreement.seller.tax_registrations.add(tax_reg) if invoice.seller['email']: email = URIUniversalCommunication() email.uri_ID = ('EM', invoice.seller['email']) # FIXME: Typo in der Library ("adress")? doc.trade.agreement.seller.electronic_adress.add(email) # Buyer-Address doc.trade.agreement.buyer.name = invoice.customer['name'] doc.trade.agreement.buyer.address.country_id = invoice.customer['address']['country_id'] doc.trade.agreement.buyer.address.postcode = invoice.customer['address']['postcode'] doc.trade.agreement.buyer.address.city_name = invoice.customer['address']['city_name'] doc.trade.agreement.buyer.address.line_one = invoice.customer['address']['line1'] doc.trade.agreement.buyer.address.line_two = invoice.customer['address']['line2'] doc.trade.agreement.buyer.address.line_three = invoice.customer['address']['line3'] # Line Items summe_netto = 0.0 summe_brutto = 0.0 summe_bezahlt = 0.0 summe_ust = 0.0 line_id_count = 0 textparts = [] for part in invoice.parts: if type(part) == InvoiceText: textparts += part.paragraphs if type(part) == InvoiceTable: for el in part.entries: line_id_count += 1 li = LineItem() li.document.line_id = f"{line_id_count}" li.product.name = el['subject'] if 'desc' in el and el['desc'] != '': desc = li.product.description = el['desc'] if 'period_start' in el and el['period_start']: if 'period_end' in el and el['period_end']: li.settlement.period.start = el['period_start'] li.settlement.period.end = el['period_end'] else: li.delivery.event.occurrence = el['period_start'] # FIXME: Hier sollte der passende Code benutzt werden (z.B. Monat) li.delivery.billed_quantity = (Decimal(el['count']), 'H87') # LTR = Liter (1 dm3) # MTQ = cubic meter # KGM = Kilogram # MTR = Meter # H87 = Piece # TNE = Tonne # MON = Month li.settlement.trade_tax.type_code = "VAT" if invoice.vat_type == VAT_REGULAR: li.settlement.trade_tax.category_code = "S" elif invoice.vat_type == VAT_KLEINUNTERNEHMER: li.settlement.trade_tax.category_code = "E" elif invoice.vat_type == VAT_INNERGEM: li.settlement.trade_tax.category_code = "K" # FIXME: Typ bei uns nur global gesetzt, nicht pro Artikel # S = Standard VAT rate # Z = Zero rated goods # E = VAT exempt # AE = Reverse charge # K = Intra-Community supply (specific reverse charge) # G = Exempt VAT for Export outside EU # O = Outside VAT scope li.settlement.trade_tax.rate_applicable_percent = Decimal(f"{el['vat']*100:.1f}") nettopreis = el['price'] if part.vatType == 'gross': nettopreis = el['price'] / (1 + el['vat']) li.agreement.net.amount = Decimal(f"{nettopreis:.2f}") nettosumme = el['total'] if part.vatType == 'gross': nettosumme = el['total'] / (1 + el['vat']) li.settlement.monetary_summation.total_amount = Decimal(f"{nettosumme:.2f}") summe_netto += nettosumme summe_brutto += el['total'] doc.trade.items.add(li) for pay in part.payments: summe_bezahlt += pay['amount'] for vat, vatdata in part.vat.items(): trade_tax = ApplicableTradeTax() # Steuerbetrag dieses Steuersatzes trade_tax.calculated_amount = Decimal(f"{(vatdata[0] / (vat + 1)) * vat:.2f}") # Nettosumme dieses Steuersatzes trade_tax.basis_amount = Decimal(f"{(vatdata[0] / (vat + 1)):.2f}") trade_tax.type_code = "VAT" if invoice.vat_type == VAT_REGULAR: trade_tax.category_code = "S" elif invoice.vat_type == VAT_KLEINUNTERNEHMER: trade_tax.category_code = "E" trade_tax.exemption_reason = 'Als Kleinunternehmer wird gemäß §19 UStG keine USt in Rechnung gestellt.' elif invoice.vat_type == VAT_INNERGEM: trade_tax.category_code = "K" trade_tax.rate_applicable_percent = Decimal(f"{vat*100:.1f}") summe_ust += (vatdata[0] / (vat + 1)) * vat doc.trade.settlement.trade_tax.add(trade_tax) for paragraph in textparts: note = IncludedNote() note.content.add(paragraph) doc.header.notes.add(note) rest = summe_brutto - summe_bezahlt if invoice.creditor_reference_id: # Gläubiger-ID für SEPA doc.trade.settlement.creditor_reference_id = invoice.creditor_reference_id doc.trade.settlement.payment_reference = invoice.id doc.trade.settlement.currency_code = 'EUR' if invoice.payment_type: doc.trade.settlement.payment_means.type_code = invoice.payment_type if invoice.seller_bank_data['iban'] and invoice.payment_type == PAYMENT_UEBERWEISUNG: doc.trade.settlement.payment_means.payee_account.iban = invoice.seller_bank_data['iban'] doc.trade.settlement.payment_means.payee_institution.bic = "GENODES1VBK" # Ist in der Library vorhanden, validiert aber nicht im XML?! if invoice.buyer_bank_data['iban'] and invoice.payment_type == PAYMENT_LASTSCHRIFT: # Kunden-Bankverbindung bei Lastschrift doc.trade.settlement.payment_means.payer_account.iban = invoice.buyer_bank_data['iban'] terms = PaymentTerms() if invoice.due_date and invoice.payment_type == PAYMENT_UEBERWEISUNG: terms.description = f"Bitte begleichen Sie den Betrag bis zum {invoice.due_date.strftime('%d.%m.%Y')} ohne Abzüge." terms.due = invoice.due_date if invoice.type == GUTSCHRIFT: terms.description = f"Wir überweisen den Betrag auf Ihr Konto." elif invoice.debit: if invoice.debit_mandate_id: # Mandatsreferenz für Lastschrift terms.debit_mandate_id = invoice.debit_mandate_id terms.description = 'Wir buchen von Ihrem Konto ab.' doc.trade.settlement.terms.add(terms) doc.trade.settlement.monetary_summation.line_total = Decimal(f"{summe_netto:.2f}") doc.trade.settlement.monetary_summation.charge_total = Decimal("0.00") doc.trade.settlement.monetary_summation.allowance_total = Decimal("0.00") doc.trade.settlement.monetary_summation.tax_basis_total = Decimal(f"{summe_netto:.2f}") doc.trade.settlement.monetary_summation.tax_total = (Decimal(f"{summe_ust:.2f}"), "EUR") doc.trade.settlement.monetary_summation.prepaid_total = Decimal(f"{summe_bezahlt:.2f}") doc.trade.settlement.monetary_summation.grand_total = Decimal(f"{summe_brutto:.2f}") doc.trade.settlement.monetary_summation.due_amount = Decimal(f"{rest:.2f}") # Generate XML file xml = doc.serialize(schema="FACTUR-X_EN16931") return xml if __name__ == '__main__': import datetime from lib.Speicher import Speicher from lib.BelegRechnung import BelegRechnung s = Speicher() import sys, os renr = 'R2024-3149' if len(sys.argv) > 1: renr=sys.argv[1] kb = s.getKassenbeleg(renr=renr) filename = BelegRechnung(kb) print(filename)