WiP
Bernd Wurst authored 5 months ago
|
1) # -*- coding: utf-8 -*-
2) # (C) 2011 by Bernd Wurst <bernd@schokokeks.org>
3)
4) # This file is part of Bib2011.
5) #
6) # Bib2011 is free software: you can redistribute it and/or modify
7) # it under the terms of the GNU General Public License as published by
8) # the Free Software Foundation, either version 3 of the License, or
9) # (at your option) any later version.
10) #
11) # Bib2011 is distributed in the hope that it will be useful,
12) # but WITHOUT ANY WARRANTY; without even the implied warranty of
13) # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14) # GNU General Public License for more details.
15) #
16) # You should have received a copy of the GNU General Public License
17) # along with Bib2011. If not, see <http://www.gnu.org/licenses/>.
18)
|
Kontoinhaber im XML einsetzen
Bernd Wurst authored 5 months ago
|
19) import os.path
20) import sys
|
WiP
Bernd Wurst authored 5 months ago
|
21) from decimal import Decimal
22)
23) # Search for included submodule python-drafthorse
24) atoms = os.path.abspath(os.path.dirname(__file__)).split('/')
25) dir = ''
26) while atoms:
27) candidate = os.path.join('/'.join(atoms), 'external/python-drafthorse')
28) if os.path.exists(candidate):
29) dir = candidate
30) break
31) atoms = atoms[:-1]
32) sys.path.insert(0, dir)
33) from drafthorse.models.document import Document
34) from drafthorse.models.accounting import ApplicableTradeTax
35) from drafthorse.models.tradelines import LineItem
|
Codestyle-Fixes
Bernd Wurst authored 5 months ago
|
36) from .InvoiceObjects import InvoiceTable, InvoiceText, RECHNUNG, GUTSCHRIFT, KORREKTUR, \
|
WiP
Bernd Wurst authored 5 months ago
|
37) VAT_REGULAR, VAT_KLEINUNTERNEHMER, VAT_INNERGEM, PAYMENT_UEBERWEISUNG, PAYMENT_LASTSCHRIFT
|
WiP
Bernd Wurst authored 5 months ago
|
38) from drafthorse.models.party import TaxRegistration, URIUniversalCommunication
39) from drafthorse.models.payment import PaymentTerms
40) from drafthorse.models.note import IncludedNote
41) from drafthorse.pdf import attach_xml
42)
43) def InvoiceToXML(invoice):
44) doc = Document()
|
Falscher ID-String
Bernd Wurst authored 4 months ago
|
45) doc.context.guideline_parameter.id = "urn:cen.eu:en16931:2017"
|
WiP
Bernd Wurst authored 5 months ago
|
46) doc.header.id = invoice.id
47) # Typecodes:
48) # 380: Handelsrechnungen
49) # 381: Gutschrift
50) # 384: Korrekturrechnung
51) # 389: Eigenrechnung (vom Käufer im Namen des Lieferanten erstellt).
52) # 261: Selbstverfasste Gutschrift.
53) # 386: Vorauszahlungsrechnung
54) # 326: Teilrechnung
55) # 751: Rechnungsinformation - KEINE RECHNUNG
56) if invoice.type == RECHNUNG:
57) doc.header.type_code = "380"
58) elif invoice.type == GUTSCHRIFT:
59) doc.header.type_code = "381"
60) elif invoice.type == KORREKTUR:
61) doc.header.type_code = "384"
62) else:
63) raise TypeError("Unbekannter Rechnungstyp, kann kein XML erstellen")
64) doc.header.issue_date_time = invoice.date
65)
66) # Seller-Address
67) if invoice.seller['trade_name']:
68) pass
69) # FIXME: specified_legal_organization ist in der Library nicht implementiert, pull request ist vorhanden
|
Kontoinhaber im XML einsetzen
Bernd Wurst authored 5 months ago
|
70) # doc.trade.agreement.seller.specified_legal_organization.trade_name = invoice.seller['trade_name']
|
WiP
Bernd Wurst authored 5 months ago
|
71) doc.trade.agreement.seller.name = invoice.seller['name']
72) doc.trade.agreement.seller.address.country_id = invoice.seller['address']['country_id']
73) doc.trade.agreement.seller.address.postcode = invoice.seller['address']['postcode']
74) doc.trade.agreement.seller.address.city_name = invoice.seller['address']['city_name']
75) doc.trade.agreement.seller.address.line_one = invoice.seller['address']['line1']
76) doc.trade.agreement.seller.address.line_two = invoice.seller['address']['line2']
77) doc.trade.agreement.seller.address.line_three = invoice.seller['address']['line3']
|
Telefonnummer
Bernd Wurst authored 4 months ago
|
78) if invoice.seller['phone']:
79) doc.trade.agreement.seller.contact.telephone.number = invoice.seller['phone']
|
WiP
Bernd Wurst authored 5 months ago
|
80) if invoice.seller_vat_id:
81) tax_reg = TaxRegistration()
82) tax_reg.id = ('VA', invoice.seller_vat_id)
83) doc.trade.agreement.seller.tax_registrations.add(tax_reg)
84) if invoice.seller['email']:
85) email = URIUniversalCommunication()
86) email.uri_ID = ('EM', invoice.seller['email'])
87) # FIXME: Typo in der Library ("adress")?
88) doc.trade.agreement.seller.electronic_adress.add(email)
89)
90) # Buyer-Address
91) doc.trade.agreement.buyer.name = invoice.customer['name']
92) doc.trade.agreement.buyer.address.country_id = invoice.customer['address']['country_id']
93) doc.trade.agreement.buyer.address.postcode = invoice.customer['address']['postcode']
94) doc.trade.agreement.buyer.address.city_name = invoice.customer['address']['city_name']
95) doc.trade.agreement.buyer.address.line_one = invoice.customer['address']['line1']
96) doc.trade.agreement.buyer.address.line_two = invoice.customer['address']['line2']
97) doc.trade.agreement.buyer.address.line_three = invoice.customer['address']['line3']
98)
|
Telefonnummer
Bernd Wurst authored 4 months ago
|
99) if invoice.buyer_reference:
100) # "Leitweg-ID" in XRechnung
101) doc.trade.agreement.buyer_reference = invoice.buyer_reference
|
Referenznummern auf allen A...
Bernd Wurst authored 4 months ago
|
102) if invoice.order_number:
103) doc.trade.agreement.buyer_order.issuer_assigned_id = invoice.order_number
104) if invoice.contract_number:
105) doc.trade.agreement.contract.issuer_assigned_id = invoice.contract_number
|
Telefonnummer
Bernd Wurst authored 4 months ago
|
106)
|
WiP
Bernd Wurst authored 5 months ago
|
107) # Line Items
108) summe_netto = 0.0
109) summe_brutto = 0.0
110) summe_bezahlt = 0.0
111) summe_ust = 0.0
112) line_id_count = 0
113) textparts = []
114) for part in invoice.parts:
|
Codestyle-Fixes
Bernd Wurst authored 5 months ago
|
115) if isinstance(part, InvoiceText):
|
WiP
Bernd Wurst authored 5 months ago
|
116) textparts += part.paragraphs
|
Codestyle-Fixes
Bernd Wurst authored 5 months ago
|
117) if isinstance(part, InvoiceTable):
|
WiP
Bernd Wurst authored 5 months ago
|
118) for el in part.entries:
119) line_id_count += 1
120) li = LineItem()
121) li.document.line_id = f"{line_id_count}"
122) li.product.name = el['subject']
123) if 'desc' in el and el['desc'] != '':
124) desc = li.product.description = el['desc']
125)
126) if 'period_start' in el and el['period_start']:
127) if 'period_end' in el and el['period_end']:
128) li.settlement.period.start = el['period_start']
129) li.settlement.period.end = el['period_end']
130) else:
|
Referenznummern auf allen A...
Bernd Wurst authored 4 months ago
|
131) #li.delivery.event.occurrence = el['period_start']
132) # FIXME: Fehler in drafthorse?! Wird nicht akzeptiert.
133) pass
|
WiP
Bernd Wurst authored 5 months ago
|
134)
135) # FIXME: Hier sollte der passende Code benutzt werden (z.B. Monat)
|
Kleine Korrekturen, mache B...
Bernd Wurst authored 5 months ago
|
136) li.delivery.billed_quantity = (Decimal(el['count']), 'C62')
137) # C62 = ohne Einheit
138) # H87 = Stück
139) # MON = Month
|
WiP
Bernd Wurst authored 5 months ago
|
140) # LTR = Liter (1 dm3)
141) # KGM = Kilogram
142) # MTR = Meter
143) # TNE = Tonne
144)
145) li.settlement.trade_tax.type_code = "VAT"
146) if invoice.vat_type == VAT_REGULAR:
147) li.settlement.trade_tax.category_code = "S"
148) elif invoice.vat_type == VAT_KLEINUNTERNEHMER:
149) li.settlement.trade_tax.category_code = "E"
150) elif invoice.vat_type == VAT_INNERGEM:
151) li.settlement.trade_tax.category_code = "K"
152) # FIXME: Typ bei uns nur global gesetzt, nicht pro Artikel
153) # S = Standard VAT rate
154) # Z = Zero rated goods
155) # E = VAT exempt
156) # AE = Reverse charge
157) # K = Intra-Community supply (specific reverse charge)
158) # G = Exempt VAT for Export outside EU
159) # O = Outside VAT scope
|
Kontoinhaber im XML einsetzen
Bernd Wurst authored 5 months ago
|
160) li.settlement.trade_tax.rate_applicable_percent = Decimal(f"{el['vat'] * 100:.1f}")
|
WiP
Bernd Wurst authored 5 months ago
|
161)
162) nettopreis = el['price']
163) if part.vatType == 'gross':
164) nettopreis = el['price'] / (1 + el['vat'])
165) li.agreement.net.amount = Decimal(f"{nettopreis:.2f}")
166)
167) nettosumme = el['total']
168) if part.vatType == 'gross':
169) nettosumme = el['total'] / (1 + el['vat'])
170) li.settlement.monetary_summation.total_amount = Decimal(f"{nettosumme:.2f}")
171)
172) summe_netto += nettosumme
173) summe_brutto += el['total']
174) doc.trade.items.add(li)
175)
176) for pay in part.payments:
177) summe_bezahlt += pay['amount']
178)
179) for vat, vatdata in part.vat.items():
180) trade_tax = ApplicableTradeTax()
181) # Steuerbetrag dieses Steuersatzes
182) trade_tax.calculated_amount = Decimal(f"{(vatdata[0] / (vat + 1)) * vat:.2f}")
183) # Nettosumme dieses Steuersatzes
184) trade_tax.basis_amount = Decimal(f"{(vatdata[0] / (vat + 1)):.2f}")
185) trade_tax.type_code = "VAT"
186) if invoice.vat_type == VAT_REGULAR:
187) trade_tax.category_code = "S"
188) elif invoice.vat_type == VAT_KLEINUNTERNEHMER:
189) trade_tax.category_code = "E"
190) trade_tax.exemption_reason = 'Als Kleinunternehmer wird gemäß §19 UStG keine USt in Rechnung gestellt.'
191) elif invoice.vat_type == VAT_INNERGEM:
192) trade_tax.category_code = "K"
|
Kontoinhaber im XML einsetzen
Bernd Wurst authored 5 months ago
|
193) trade_tax.rate_applicable_percent = Decimal(f"{vat * 100:.1f}")
|
WiP
Bernd Wurst authored 5 months ago
|
194) summe_ust += (vatdata[0] / (vat + 1)) * vat
195) doc.trade.settlement.trade_tax.add(trade_tax)
196)
197) for paragraph in textparts:
198) note = IncludedNote()
199) note.content.add(paragraph)
200) doc.header.notes.add(note)
201)
202) rest = summe_brutto - summe_bezahlt
203)
204) if invoice.creditor_reference_id:
205) # Gläubiger-ID für SEPA
206) doc.trade.settlement.creditor_reference_id = invoice.creditor_reference_id
207) doc.trade.settlement.payment_reference = invoice.id
208) doc.trade.settlement.currency_code = 'EUR'
|
WiP
Bernd Wurst authored 5 months ago
|
209) if invoice.payment_type:
210) doc.trade.settlement.payment_means.type_code = invoice.payment_type
211)
212) if invoice.seller_bank_data['iban'] and invoice.payment_type == PAYMENT_UEBERWEISUNG:
|
Kontoinhaber im XML einsetzen
Bernd Wurst authored 5 months ago
|
213) doc.trade.settlement.payment_means.payee_account.account_name = \
214) invoice.seller_bank_data['kontoinhaber'] or invoice.seller['trade_name'] or invoice.seller['name']
|
WiP
Bernd Wurst authored 5 months ago
|
215) doc.trade.settlement.payment_means.payee_account.iban = invoice.seller_bank_data['iban']
|
Kleine Korrekturen, mache B...
Bernd Wurst authored 5 months ago
|
216) if invoice.seller_bank_data['bic']:
217) doc.trade.settlement.payment_means.payee_institution.bic = invoice.seller_bank_data['bic']
|
WiP
Bernd Wurst authored 5 months ago
|
218) if invoice.buyer_bank_data['iban'] and invoice.payment_type == PAYMENT_LASTSCHRIFT:
|
WiP
Bernd Wurst authored 5 months ago
|
219) # Kunden-Bankverbindung bei Lastschrift
220) doc.trade.settlement.payment_means.payer_account.iban = invoice.buyer_bank_data['iban']
221)
222) terms = PaymentTerms()
|
WiP
Bernd Wurst authored 5 months ago
|
223) if invoice.due_date and invoice.payment_type == PAYMENT_UEBERWEISUNG:
|
WiP
Bernd Wurst authored 5 months ago
|
224) terms.description = f"Bitte begleichen Sie den Betrag bis zum {invoice.due_date.strftime('%d.%m.%Y')} ohne Abzüge."
225) terms.due = invoice.due_date
|
Kleine Korrekturen, mache B...
Bernd Wurst authored 5 months ago
|
226) if invoice.type in [GUTSCHRIFT, KORREKTUR]:
|
WiP
Bernd Wurst authored 5 months ago
|
227) terms.description = f"Wir überweisen den Betrag auf Ihr Konto."
228) elif invoice.debit:
|
WiP
Bernd Wurst authored 5 months ago
|
229) if invoice.debit_mandate_id:
230) # Mandatsreferenz für Lastschrift
231) terms.debit_mandate_id = invoice.debit_mandate_id
232) terms.description = 'Wir buchen von Ihrem Konto ab.'
233) doc.trade.settlement.terms.add(terms)
234)
235) doc.trade.settlement.monetary_summation.line_total = Decimal(f"{summe_netto:.2f}")
236) doc.trade.settlement.monetary_summation.charge_total = Decimal("0.00")
237) doc.trade.settlement.monetary_summation.allowance_total = Decimal("0.00")
238) doc.trade.settlement.monetary_summation.tax_basis_total = Decimal(f"{summe_netto:.2f}")
239) doc.trade.settlement.monetary_summation.tax_total = (Decimal(f"{summe_ust:.2f}"), "EUR")
240) doc.trade.settlement.monetary_summation.prepaid_total = Decimal(f"{summe_bezahlt:.2f}")
241) doc.trade.settlement.monetary_summation.grand_total = Decimal(f"{summe_brutto:.2f}")
242) doc.trade.settlement.monetary_summation.due_amount = Decimal(f"{rest:.2f}")
243)
244) # Generate XML file
|
WiP
Bernd Wurst authored 5 months ago
|
245) xml = doc.serialize(schema="FACTUR-X_EN16931")
|