Example #1
0
def _getTemplateArguments(invoice):
    rows = []
    composite_rows = []
    simple_rows = []

    total = Decimal(0)
    total_vat = Decimal(0)
    for row in invoice.rows:
        rowData = {
            "title": row.title,
            "num_units": formatNumber(row.getNumberOfUnits()),
            "vat": formatNumber(row.vat),
            "vat_percentage": formatNumber(Decimal(row.vat) * 100),
            "total": formatNumber(Decimal(row.getTotal())),
            "total_vat": formatNumber(row.getTotal() * Decimal(row.vat)),
            "total_with_vat": formatNumber(row.getTotal() * (1 + Decimal(row.vat))),
        }
        if row.getPricePerUnit():
            rowData["price_per_unit"] = formatNumber(row.getPricePerUnit())
        if isinstance(row, CompositeRow):
            rowData["subrows"] = []
            for subrow in row.subrows:
                row_total = Decimal(subrow.num_units) * Decimal(subrow.price_per_unit)
                rowData["subrows"].append(
                    {
                        "title": subrow.title,
                        "price_per_unit": formatNumber(subrow.price_per_unit),
                        "num_units": formatNumber(subrow.num_units),
                        "vat": formatNumber(row.vat),
                        "vat_percentage": formatNumber(Decimal(row.vat) * 100),
                        "total": formatNumber(row_total),
                        "total_with_vat": formatNumber(row_total * (1 + Decimal(row.vat))),
                    }
                )
            composite_rows.append(rowData)
        else:
            simple_rows.append(rowData)

        rows.append(rowData)

        total += row.getTotal()
        total_vat += row.getTotal() * Decimal(row.vat)

    lokki_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

    return {
        "rows": rows,
        "has_simple_rows": len(simple_rows) > 0,
        "simple_rows": simple_rows,
        "has_composite_rows": len(composite_rows) > 0,
        "composite_rows": composite_rows,
        "invoice": invoice,
        "n": "%05d" % invoice.invoice_number,
        "d": "%02d" % datetime.today().day,
        "m": "%02d" % datetime.today().month,
        "y": "%04d" % datetime.today().year,
        "invoice_d": "%02d" % invoice.date.day,
        "invoice_m": "%02d" % invoice.date.month,
        "invoice_y": "%04d" % invoice.date.year,
        "due_d": "%02d" % invoice.due_date.day,
        "due_m": "%02d" % invoice.due_date.month,
        "due_y": "%04d" % invoice.due_date.year,
        "lokki_dir": lokki_dir,
        "due_days": (invoice.due_date - invoice.date).days,
        "total": formatNumber(total),
        "total_vat": formatNumber(total_vat),
        "total_with_vat": formatNumber(total + total_vat),
        "currency": "€",
    }
Example #2
0
def commandInvoiceShow(args, session):
    dieIf(not isConfigurationValid(session), "Cannot execute invoicing commands with incomplete configuration.")

    invoice = findInvoice(session, args)

    if args.json:
        _jsonPrintInvoice(invoice)
    else:
        table = PrettyTable(["Seller info", ""])
        table.align["Seller info"] = "r"
        table.align[""] = "l"

        table.add_row(["Name", invoice.seller_name])
        table.add_row(["Address", invoice.seller_address])
        table.add_row(["ZIP code", invoice.seller_zip_code])
        table.add_row(["City", invoice.seller_city])
        if invoice.seller_country:
            table.add_row(["Country", invoice.seller_country])
        if invoice.seller_company_number:
            table.add_row(["Company number", invoice.seller_company_number])
        if invoice.seller_vat_number:
            table.add_row(["VAT number", invoice.seller_vat_number])

        print(table)
        print("")

        table = PrettyTable(["Client info", ""])
        table.align["Client info"] = "r"
        table.align[""] = "l"

        table.add_row(["Name", invoice.client_name])
        table.add_row(["Client number", invoice.client_number])
        table.add_row(["Address", invoice.client_address])
        table.add_row(["ZIP code", invoice.client_zip_code])
        table.add_row(["City", invoice.client_city])
        if invoice.client_country:
            table.add_row(["Country", invoice.client_country])
        if invoice.client_company_number:
            table.add_row(["Company number", invoice.client_company_number])
        if invoice.client_vat_number:
            table.add_row(["VAT number", invoice.client_vat_number])

        print(table)
        print("")

        table = PrettyTable(["Invoice", ""])
        table.align["Invoice"] = "r"
        table.align[""] = "l"

        table.add_row(["Created", invoice.time_added.strftime("%Y-%m-%d %H:%M:%S")])
        table.add_row(["Date", invoice.date.strftime("%Y-%m-%d")])
        table.add_row(["Due date", invoice.due_date.strftime("%Y-%m-%d")])
        table.add_row(["IBAN", invoice.seller_iban])
        table.add_row(["Invoice number", invoice.invoice_number])
        table.add_row(["Reference", invoice.reference])
        table.add_row(["Is billed", "Yes" if invoice.is_billed else "Not billed"])

        print(table)

        table = PrettyTable(["N", "Item", "Units", "Price/unit", "Total", "*"])
        table.align["N"] = "l"
        table.align["Item"] = "l"
        table.align["Units"] = "l"
        table.align["Price/unit"] = "l"
        table.align["Total"] = "l"
        table.align["*"] = "l"

        for row in invoice.rows:
            extra = []
            if row.note:
                extra.append("note")
            if isinstance(row, CompositeRow):
                extra.append("composite")
            if row.external_source != "lk":
                extra.append(row.external_source)
            table.add_row(
                [
                    row.index,
                    row.title,
                    formatNumber(row.getNumberOfUnits()),
                    formatNumber(row.getPricePerUnit()),
                    formatNumber(row.getTotal()),
                    ", ".join(extra),
                ]
            )

        print("")
        print(table)

        print("")
        print("Total without VAT:   " + formatNumber(invoice.getTotal()))
        print("Total VAT:           " + formatNumber(invoice.getTotalVAT()))
        print("Total with VAT       " + formatNumber(invoice.getTotalWithVAT()))