brewman/brewman/views/reports/unposted.py

56 lines
1.9 KiB
Python
Raw Normal View History

import pkg_resources
from pyramid.response import FileResponse
from pyramid.view import view_config
from sqlalchemy.orm import joinedload_all
from brewman.models.voucher import Voucher, Journal, VoucherType
from brewman.views.services.voucher import get_edit_url
@view_config(request_method='GET', route_name='unposted', permission='Post Vouchers')
def html(request):
package, resource = 'brewman:static/index.html'.split(':', 1)
file = pkg_resources.resource_filename(package, resource)
return FileResponse(file, request=request)
@view_config(request_method='GET', route_name='api_unposted', renderer='json', permission='Post Vouchers')
def report_data(request):
2012-10-16 20:02:01 +00:00
return build_report(request)
def build_report(request):
2012-10-16 20:02:01 +00:00
body = []
query = request.dbsession.query(
Voucher
).options(
joinedload_all(Voucher.journals, Journal.account, innerjoin=True)
).filter(
Voucher.posted == False
).filter(
Voucher.type != VoucherType.by_name('Issue').id
).order_by(Voucher.date).order_by(Voucher.last_edit_date).all()
for voucher in query:
debit = 0
credit = 0
name_debit = ""
name_credit = ""
for journal in voucher.journals:
if journal.debit == 1:
debit += journal.amount
name_debit += "{0} / ".format(journal.account.name)
else:
credit += journal.amount
name_credit += "{0} / ".format(journal.account.name)
name_debit = name_debit[:-3]
name_credit = name_credit[:-3]
body.append({'date': voucher.date.strftime('%d-%b-%Y'), 'Url': get_edit_url(request, voucher),
'voucherType': VoucherType.by_id(voucher.type).name,
'narration': voucher.narration, 'isPosted': voucher.posted,
'debitName': name_debit, 'debitAmount': debit, 'creditName': name_credit, 'creditAmount': credit})
return body