Ejemplo n.º 1
0
Archivo: views.py Proyecto: dbaty/Brouz
def reports(request):
    try:
        year = int(request.matchdict['year'])
    except KeyError:  # no matchdict
        year = datetime.datetime.now().year
    session = DBSession()
    if session.query(Transaction.date).\
            filter_by(year=year - 1).first() is None:
        previous_year = None
    else:
        previous_year = year - 1
    if session.query(Transaction.date).\
            filter_by(year=year + 1).first() is None:
        next_year = None
    else:
        next_year = year + 1
    lines = session.query(Transaction).\
        filter_by(year=year, composite=False).all()
    report = _calculate_report(lines)
    _update_report_with_fixed_assets(report, year, session)
    _update_report_with_vat(report, lines)
    report['vat_installments'] = accounting.calculate_vat_installments(
        session.query(Transaction), year)
    # FIXME: move these to separate function(s)?
    report['clients'] = ((party, accounting.Price(net_amount))
        for party, net_amount in session.execute(
        'SELECT party, SUM(net_amount) AS sum '
        'FROM transactions '
        'WHERE category=%d '
        'AND year=%d '
        'GROUP BY party '
        'ORDER BY sum DESC' % (CATEGORY_INCOME_MISC, year)))
    report['providers'] = ((party, accounting.Price(net_amount))
        for party,  net_amount, in session.execute(
        'SELECT party, SUM(net_amount) AS sum '
        'FROM transactions '
        'WHERE category NOT IN (%s, %s) '
        'AND composite = 0 '
        'AND part_of IS NULL '
        'AND year=%d '
        'GROUP BY party '
        'ORDER BY sum DESC' % (
            CATEGORY_INCOME_MISC, CATEGORY_REMUNERATION, year)))
    report['remuneration'] = [(date, accounting.Price(net_amount))
        for date, net_amount in session.query(
        Transaction.date, Transaction.net_amount).\
        filter_by(category=CATEGORY_REMUNERATION, year=year).\
        order_by(Transaction.date)]
    report['total_remuneration'] = sum(
        r[1] for r in report['remuneration'])
    bindings = {'api': TemplateAPI(request, 'reports'),
                'year': year,
                'previous_year': previous_year,
                'next_year': next_year,
                'report': report}
    return render_to_response('templates/reports.pt', bindings)
Ejemplo n.º 2
0
Archivo: views.py Proyecto: dbaty/Brouz
def home(request):
    session = DBSession()
    lines = session.query(Transaction).filter_by(part_of=None, composite=False)
    lines = lines.union(session.query(Transaction).filter_by(composite=True))
    lines = lines.order_by(Transaction.date,
                           Transaction.party,
                           Transaction.title)
    balances = [accounting.Price(2494978)]
    for line in lines:
        balances.append(balances[-1] + line.signed_amount)
    balance = sum((line.signed_amount for line in lines), accounting.Price(0))
    api = TemplateAPI(request, 'home')
    bindings = {'api': api,
                'lines': lines,
                'balance': balance,
                'balances': balances}
    return render_to_response('templates/home.pt', bindings)
Ejemplo n.º 3
0
Archivo: views.py Proyecto: dbaty/Brouz
def edit(request):
    session = DBSession()
    transaction_id = request.matchdict['transaction_id']
    transaction = session.query(Transaction).\
        filter_by(id=transaction_id).one()
    form = make_edit_form(request, transaction)
    try:
        data = form.validate(request.POST.items())
    except ValidationFailure, e:
        return edit_form(request, e)
Ejemplo n.º 4
0
Archivo: views.py Proyecto: dbaty/Brouz
def edit_form(request, form=None):
    if form is None:
        session = DBSession()
        transaction_id = request.matchdict['transaction_id']
        transaction = session.query(Transaction).\
            filter_by(id=transaction_id).one()
        form = make_edit_form(request, transaction)
        if transaction.composite:
            data = transaction.__dict__
            data['lines'] = []
            for txn in session.query(Transaction).\
                    filter_by(part_of=transaction_id).all():
                data['lines'].append(txn.__dict__)
        else:
            data = transaction.__dict__
    api = TemplateAPI(request, 'add')
    bindings = {'api': api,
                'form': form,
                'data': data}
    return render_to_response('templates/edit.pt', bindings)
Ejemplo n.º 5
0
Archivo: views.py Proyecto: dbaty/Brouz
def delete(request):
    transaction_id = request.matchdict['transaction_id']
    if request.POST.get('confirm') != '1':
        msg = _('You did not confirm that you want to delete '
                'this transaction.')
        request.session.flash(msg, 'error')
        url = request.route_url('edit', transaction_id=transaction_id)
        return HTTPSeeOther(url)
    session = DBSession()
    transaction = session.query(Transaction).\
        filter_by(id=transaction_id).one()
    # FIXME: If transaction is composite, we should delete the related
    # transactions: filter_by(part_of=transaction_id)
    session.delete(transaction)
    request.session.flash(_('The transaction has been deleted.'), 'success')
    return HTTPSeeOther(request.route_url('home'))
Ejemplo n.º 6
0
Archivo: views.py Proyecto: dbaty/Brouz
def autocomplete(request):
    field = request.matchdict['field']
    field = getattr(Transaction, field)
    session = DBSession()
    return [i[0] for i in session.query(field).filter(
            field.like('%%%s%%' % request.GET['term'])).distinct()]