git.schokokeks.org
Repositories
Help
Report an Issue
invoice.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
25674c2
Branches
Tags
factur-x_localdata
master
invoice.git
Invoice
text.py
separated from keks-tools
Bernd Wurst
commited
25674c2
at 2024-02-14 08:20:12
text.py
Blame
History
Raw
# -* coding: utf8 *- from __future__ import division from Invoice import Text, Table, Image from utils import format_price, split_to_width 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(iv, bankdata=True): ret = [] ret.append(u'Rechnungsempfänger:') for line in iv.addresslines: ret.append(' %s' % line) ret.append('') ret.append('Kundennummer: %4i' % iv.customerno) ret.append('Rechnungsnummer: %4i Rechnungsdatum: %s' % (iv.id, iv.date.strftime('%d.%m.%Y'))) ret.append('') for part in iv.parts: if type(part) == Table: ret.append(InvoiceTableToText(part)) elif type(part) == Text: ret.append(InvoiceTextToText(part)) elif type(part) == Image: # ignore images pass else: raise NotImplementedError("Cannot handle part of type %s" % type(part)) if bankdata: ret.append('-' * 72) ret.append('Unsere Bankverbindung:') ret.append('Volksbank Backnang, BLZ 602 911 20, Konto-Nr. 41512 006') ret.append('IBAN: DE91602911200041512006, BIC: GENODES1VBK') return '\n'.join(ret) def InvoiceTableToText(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 '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) sum = invoiceTable.sum for vat, vatdata in list(invoiceTable.vat.items()): sum += vat * vatdata[0] ret.append((u'Rechnungsbetrag: %11s' % format_price(sum)).rjust(72)) ret.append('') return '\n'.join(ret) def InvoiceTextToText(invoiceText): ret = [] for par in invoiceText.paragraphs: for s in split_to_width(par, 72): ret.append(s) ret.append('') return '\n'.join(ret)