git.schokokeks.org
Repositories
Help
Report an Issue
invoice.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
f2d1796
Branches
Tags
factur-x_localdata
master
invoice.git
src
rechnung
Invoice
InvoiceToText.py
Codestyle-Fixes
Bernd Wurst
commited
f2d1796
at 2024-01-26 14:29:34
InvoiceToText.py
Blame
History
Raw
# -* coding: utf8 *- from __future__ import division from utils import format_price, split_to_width from .InvoiceObjects import Invoice, InvoiceTable, InvoiceText, InvoiceImage, PAYMENT_UEBERWEISUNG def _breakLine(text, width=72): lines = [] paras = text.split('\n') for para in paras: words = para.split(' ') while len(words) > 0: mywords = [words[0], ] del words[0] while len(words) > 0 and len(u' '.join(mywords) + ' ' + words[0]) <= width: mywords.append(words[0]) del words[0] lines.append(' '.join(mywords)) return lines def InvoiceToText(invoice: Invoice): ret = [u'Rechnungsempfänger:', ] addresslines = filter(None, [ invoice.customer['name'].strip(), invoice.customer['address']['line1'] or '', invoice.customer['address']['line2'] or '', invoice.customer['address']['line3'] or '', ((invoice.customer['address']['postcode'] or '') + ' ' + ( invoice.customer['address']['city_name'] or '')).strip(), ]) for line in addresslines: ret.append(f' {line}') ret.append('') ret.append(f'Kundennummer: {invoice.customerno}') ret.append(f'Rechnungsnummer: {invoice.id}') ret.append(f'Rechnungsdatum: {invoice.date.strftime("%d.%m.%Y")}') ret.append('') for part in invoice.parts: if isinstance(part, InvoiceTable): ret.append(InvoiceTableToText(part)) elif isinstance(part, InvoiceText): ret.append(InvoiceTextToText(part)) elif isinstance(part, InvoiceImage): # ignore images pass else: raise NotImplementedError("Cannot handle part of type %s" % type(part)) if invoice.payment_type == PAYMENT_UEBERWEISUNG: ret.append('-' * 72) ret.append('Unsere Bankverbindung:') ret.append(f'{invoice.seller_bank_data["bankname"]}') ret.append(f'IBAN: {invoice.seller_bank_data["iban"]}') return '\n'.join(ret) def InvoiceTableToText(invoiceTable: InvoiceTable): ret = [] if len(invoiceTable.vat) < 2: ret.append(u'Anz Beschreibung Preis Gesamt') ret.append(u'-' * 72) else: ret.append(u'Anz Beschreibung Preis Gesamt') ret.append(u'-' * 72) for el in invoiceTable.entries: unit = '' if el['unit']: unit = ' %s' % el['unit'] if len(invoiceTable.vat) < 2: subject = _breakLine(el['subject'], width=39) ret.append(u'%5.2f %-3s %-39s %10s %10s' % ( el['count'], unit, subject[0], format_price(el['price']), format_price(el['total']))) for i in range(1, len(subject)): ret.append(u' %s' % subject[i]) else: subject = _breakLine(el['subject'], width=41) ret.append(u'%5.2f %-3s %-37s %10s %s %10s' % ( el['count'], unit, subject[0], format_price(el['price']), invoiceTable.vat[el['vat']][1], format_price(el['total']))) for i in range(1, len(subject)): ret.append(u' %s' % subject[i]) if 'period_start' in el and el['period_start']: if 'period_end' in el and el['period_end']: desc = _breakLine('(%s - %s)' % (el['period_start'].strftime('%d.%m.%Y'), el['period_end'].strftime('%d.%m.%Y'))) else: desc = _breakLine('Leistungsdatum: %s' % (el['period_start'].strftime('%d.%m.%Y'))) for line in desc: ret.append(u' %s' % line) if 'desc' in el and el['desc']: desc = _breakLine(el['desc'], 39) for line in desc: ret.append(u' %s' % line) ret.append('-' * 72) if invoiceTable.vatType == 'gross': ret.append((u'Rechnungsbetrag: %11s' % format_price(invoiceTable.sum)).rjust(72)) ret.append('') summaries = [] if len(invoiceTable.vat) == 1: vat = list(invoiceTable.vat.keys())[0] summaries.append(u' Im Rechnungsbetrag sind %.1f%% MwSt enthalten (%s)' % ( vat * 100, format_price((invoiceTable.sum / (vat + 1)) * vat))) else: for vat, vatdata in list(invoiceTable.vat.items()): summaries.append(u' %s: Im Teilbetrag von %s sind %.1f%% MwSt enthalten (%s)' % ( vatdata[1], format_price(vatdata[0]), vat * 100, format_price((vatdata[0] / (vat + 1)) * vat))) summaries.sort() for line in summaries: ret.append(line) else: ret.append((u'Nettobetrag: %11s' % format_price(invoiceTable.sum)).rjust(72)) summaries = [] if len(invoiceTable.vat) == 1: vat = list(invoiceTable.vat.keys())[0] summaries.append((u'zzgl. %.1f%% MwSt: %11s' % (vat * 100, format_price(vat * invoiceTable.sum))).rjust(72)) elif len(invoiceTable.vat) > 1: for vat, vatdata in list(invoiceTable.vat.items()): summaries.append( (u'zzgl. %4.1f%% MwSt (%s): %11s' % (vat * 100, vatdata[1], format_price(vat * vatdata[0]))).rjust( 72)) summaries.sort() for line in summaries: ret.append(line) tablesum = invoiceTable.sum for vat, vatdata in list(invoiceTable.vat.items()): tablesum += vat * vatdata[0] ret.append((u'Rechnungsbetrag: %11s' % format_price(tablesum)).rjust(72)) ret.append('') return '\n'.join(ret) def InvoiceTextToText(invoiceText: InvoiceText): ret = [] for par in invoiceText.paragraphs: for s in split_to_width(par, 72): ret.append(s) ret.append('') return '\n'.join(ret)