Ejemplo n.º 1
0
def prepare_invoice(workspace_name, lineitems, title):
    workspace = Workspace.get(workspace_name)
    if lineitems is None:
        return jsonify({'error': 'No line items specified'})

    invoice = Invoice(workspace=workspace, user=g.user, status=INVOICE_STATUS.ESTIMATE)
    invoice.title = title
    invoice.make_name()
    invoice.addressee = invoice.user.fullname
    db.session.commit()
    for idx, l in enumerate(lineitems):
        lineitem = LineItem()
        db.session.add(lineitem)

        lineitem.invoice_id = invoice.id
        lineitem.category_id = l.get('category_id', None)
        lineitem.description = l.get('event', None) + ' ' + l.get('category_desc', None)
        lineitem.tax_rate = l.get('tax_rate', None)
        lineitem.pat = l.get('pat', None)
        lineitem.quantity = l.get('quantity', None)
        db.session.commit()

        lineitem.update_total()
        invoice.update_total()
        db.session.commit()
    return jsonify(
        invoice=invoice.url_name,
        url=app.config['SITE_URL'] + url_for('select_address',
        workspace=workspace.name,
        invoice=invoice.url_name))
Ejemplo n.º 2
0
def invoice(workspace, invoice):
    workflow = invoice.workflow()
    if not workflow.can_view():
        abort(403)
    lineitemform = LineItemForm()
    lineitemform.category.choices = [(c.id, c.title) for c in Category.get_by_status(workspace, CATEGORY_STATUS.LIVE)]
    lineitemform.invoice = invoice
    if lineitemform.validate_on_submit():
        if lineitemform.id.data:
            lineitem = LineItem.query.get(lineitemform.id.data)
        else:
            lineitem = LineItem()
            db.session.add(lineitem)

        lineitem.invoice_id = lineitemform.invoice.id
        lineitem.category_id = lineitemform.category.data
        category = Category.get_by_id(workspace, lineitem.category_id).first()
        lineitem.description = category.title
        lineitem.tax_rate = category.tax_rate
        lineitem.pat = category.pat
        lineitem.quantity = lineitemform.quantity.data
        db.session.commit()

        lineitem.update_total()
        invoice.update_total()
        db.session.commit()

        if request.is_xhr:
            lineitemform = LineItemForm(MultiDict())
            lineitemform.category.choices = [(c.id, c.title) for c in Category.get_by_status(workspace, CATEGORY_STATUS.LIVE)]
            return render_template("lineitem.html.jinja2", workspace=workspace, invoice=invoice.url_name, lineitemform=lineitemform)
        else:
            return redirect(url_for('invoice', workspace=workspace.name, invoice=invoice.url_name, lineitemform=lineitemform), code=303)
    if request.is_xhr:
        return render_template("lineitem.html.jinja2",  workspace=workspace, invoice=invoice, lineitemform=lineitemform)
    return render_template('invoice.html.jinja2',
        workspace=workspace,
        invoice=invoice,
        lineitemform=lineitemform,
        workflow=workflow,
        transitions=workflow.transitions())