Example #1
0
def commandInvoiceUnbill(args, session):
    dieIf(not isConfigurationValid(session), "Cannot execute invoicing commands with incomplete configuration.")

    invoice = findInvoice(session, args)

    dieIf(not invoice.is_billed, "Invoice " + str(invoice.invoice_number) + " was not billed.")

    invoice.is_billed = False
    session.commit()

    triggerEvent(session, "unbill", _getTemplateArguments(invoice))

    print("Marked invoice '" + str(invoice.invoice_number) + "' as not billed.")
Example #2
0
def commandInvoiceGenerate(args, session):
    if args.template:
        template = args.template
    else:
        template = getSetting(session, "default-invoice-template")
        lokki_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
        template = lokki_dir + "/" + template

    dieIf(not template, "No --template provided and default-invoice-template not set.")

    invoice = findInvoice(session, args)

    templateArguments = _getTemplateArguments(invoice)
    templateArguments["show_details"] = not args.hide_details and len(templateArguments["composite_rows"])

    with open(template, "r") as template:
        templateString = template.read()

    renderedInvoice = pystache.render(templateString, templateArguments)

    if args.filename:
        targetPath = args.filename
    else:
        targetFilenameTemplate = getSetting(session, "invoice-filename-template")
        targetPath = pystache.render(targetFilenameTemplate, templateArguments)

    targetPath = os.path.normpath(targetPath)

    templateArguments["output_path"] = targetPath
    templateArguments["output_basename"] = os.path.basename(targetPath)
    (templateArguments["output_path_no_ext"], templateArguments["output_path_ext"]) = os.path.splitext(targetPath)

    with open(targetPath, "w") as output:
        output.write(renderedInvoice)

    triggerEvent(session, "generate", templateArguments)