2013-10-03 10:12:43 +00:00
|
|
|
import datetime
|
2014-04-29 10:08:44 +00:00
|
|
|
import pkg_resources
|
|
|
|
from pyramid.response import FileResponse
|
2013-10-03 10:12:43 +00:00
|
|
|
from sqlalchemy.orm import joinedload_all
|
2016-04-12 08:27:24 +00:00
|
|
|
from sqlalchemy.sql.expression import func, or_, and_
|
2013-10-03 10:12:43 +00:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
from pyramid.view import view_config
|
|
|
|
import transaction
|
|
|
|
|
|
|
|
from brewman.models.master import LedgerBase
|
|
|
|
|
|
|
|
from brewman.models.voucher import Voucher, Journal, VoucherType
|
|
|
|
from brewman.views.services.session import session_period_start, session_period_finish
|
|
|
|
from brewman.views.services.voucher import get_edit_url
|
|
|
|
|
|
|
|
|
2014-04-29 10:08:44 +00:00
|
|
|
@view_config(request_method='GET', route_name='reconcile_id', permission='Reconcile')
|
|
|
|
@view_config(request_method='GET', route_name='reconcile', permission='Reconcile')
|
2013-10-03 10:12:43 +00:00
|
|
|
def html(request):
|
2018-05-25 13:49:00 +00:00
|
|
|
package, resource = 'brewman:static/index.html'.split(':', 1)
|
2014-04-29 10:08:44 +00:00
|
|
|
file = pkg_resources.resource_filename(package, resource)
|
|
|
|
return FileResponse(file, request=request)
|
2013-10-03 10:12:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@view_config(request_method='GET', route_name='api_reconcile', renderer='json', permission='Reconcile')
|
|
|
|
def show_blank(request):
|
|
|
|
return {'StartDate': session_period_start(request),
|
2016-04-12 08:27:24 +00:00
|
|
|
'FinishDate': session_period_finish(request), 'Ledger': None, 'Body': []}
|
2013-10-03 10:12:43 +00:00
|
|
|
|
|
|
|
|
2016-12-16 13:27:06 +00:00
|
|
|
@view_config(request_method='GET', route_name='api_reconcile_id', renderer='json', permission='Reconcile', trans=True)
|
2013-10-03 10:12:43 +00:00
|
|
|
def show_data(request):
|
2016-12-24 11:41:01 +00:00
|
|
|
account = request.dbsession.query(LedgerBase).filter(LedgerBase.id == uuid.UUID(request.matchdict['id'])).first()
|
2013-10-03 10:12:43 +00:00
|
|
|
start_date = request.GET.get('StartDate', session_period_start(request))
|
|
|
|
finish_date = request.GET.get('FinishDate', session_period_finish(request))
|
|
|
|
info = {'StartDate': start_date, 'FinishDate': finish_date,
|
2016-04-12 08:27:24 +00:00
|
|
|
'Account': {'LedgerID': account.id, 'Name': account.name}, 'Body': []}
|
2016-12-24 11:41:01 +00:00
|
|
|
build_report(request, info, request.dbsession)
|
2013-10-03 10:12:43 +00:00
|
|
|
return info
|
|
|
|
|
|
|
|
|
2016-12-24 11:41:01 +00:00
|
|
|
def build_report(request, info, dbsession):
|
2013-10-03 10:12:43 +00:00
|
|
|
ledger_id = info['Account']['LedgerID']
|
|
|
|
start_date = info['StartDate']
|
|
|
|
finish_date = info['FinishDate']
|
2016-12-24 11:41:01 +00:00
|
|
|
opening = opening_balance(ledger_id, start_date, dbsession)
|
2013-10-03 10:12:43 +00:00
|
|
|
info['Body'].append(opening)
|
|
|
|
|
2016-12-24 11:41:01 +00:00
|
|
|
query = request.dbsession.query(Voucher).options(
|
2016-04-12 08:27:24 +00:00
|
|
|
joinedload_all(Voucher.journals, Journal.ledger, innerjoin=True)
|
|
|
|
).filter(
|
|
|
|
Voucher.journals.any(Journal.ledger_id == ledger_id)
|
|
|
|
).filter(
|
|
|
|
or_(
|
|
|
|
Voucher.is_reconciled == False,
|
|
|
|
and_(
|
|
|
|
Voucher.reconcile_date >= datetime.datetime.strptime(start_date, '%d-%b-%Y'),
|
|
|
|
Voucher.reconcile_date <= datetime.datetime.strptime(finish_date, '%d-%b-%Y')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
).filter(
|
|
|
|
Voucher.type != VoucherType.by_name('Issue').id
|
|
|
|
).order_by(Voucher.is_reconciled).order_by(Voucher.reconcile_date).order_by(Voucher.last_edit_date).all()
|
2013-10-03 10:12:43 +00:00
|
|
|
|
|
|
|
for voucher in query:
|
|
|
|
debit = 0
|
|
|
|
credit = 0
|
|
|
|
journal_debit = 0
|
|
|
|
name = ""
|
|
|
|
for journal in voucher.journals:
|
|
|
|
if journal.ledger_id == ledger_id:
|
|
|
|
journal_debit = journal.debit
|
|
|
|
if journal.debit == 1:
|
|
|
|
debit = journal.amount
|
2016-04-12 08:27:24 +00:00
|
|
|
credit = 0
|
2013-10-03 10:12:43 +00:00
|
|
|
else:
|
|
|
|
credit = journal.amount
|
2016-04-12 08:27:24 +00:00
|
|
|
debit = 0
|
2013-10-03 10:12:43 +00:00
|
|
|
for journal in voucher.journals:
|
|
|
|
if journal.debit != journal_debit:
|
|
|
|
name += "{0} / ".format(journal.ledger.name)
|
|
|
|
name = name[:-3]
|
|
|
|
info['Body'].append(
|
|
|
|
{'VoucherID': voucher.id, 'Date': voucher.date.strftime('%d-%b-%Y'), 'Name': name,
|
|
|
|
'Url': get_edit_url(request, voucher), 'Type': VoucherType.by_id(voucher.type).name,
|
2016-04-12 08:27:24 +00:00
|
|
|
'Narration': voucher.narration, 'Debit': debit, 'Credit': credit, 'IsReconciled': voucher.is_reconciled,
|
|
|
|
'ReconcileDate': voucher.reconcile_date.strftime('%d-%b-%Y')})
|
2013-10-03 10:12:43 +00:00
|
|
|
|
|
|
|
|
2016-12-24 11:41:01 +00:00
|
|
|
def opening_balance(ledger_id, start_date, dbsession):
|
|
|
|
opening = dbsession.query(func.sum(Journal.amount * Journal.debit)) \
|
2013-10-03 10:12:43 +00:00
|
|
|
.join(Journal.voucher) \
|
2016-04-12 08:27:24 +00:00
|
|
|
.filter(Voucher.reconcile_date < datetime.datetime.strptime(start_date, '%d-%b-%Y')) \
|
|
|
|
.filter(Voucher.is_reconciled == True) \
|
2013-10-03 10:12:43 +00:00
|
|
|
.filter(Voucher.type != VoucherType.by_name('Issue').id) \
|
2016-12-24 11:41:01 +00:00
|
|
|
.filter(Journal.ledger_id == ledger_id) \
|
2013-10-03 10:12:43 +00:00
|
|
|
.scalar()
|
|
|
|
opening = 0 if opening is None else opening
|
|
|
|
if opening < 0:
|
|
|
|
credit = opening * -1
|
|
|
|
debit = 0
|
|
|
|
else:
|
|
|
|
debit = opening
|
|
|
|
credit = 0
|
2016-04-14 18:32:28 +00:00
|
|
|
return {'Date': start_date, 'ID': 'OB', 'Name': 'Opening Balance', 'Type': 'Opening Balance', 'Narration': '',
|
|
|
|
'Debit': debit, 'Credit': credit, 'Running': opening, 'IsReconciled': True, 'ReconcileDate': start_date}
|
2013-10-03 10:12:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@view_config(request_method='POST', route_name='api_reconcile_id', renderer='json', permission='Reconcile')
|
|
|
|
def save(request):
|
2016-12-24 11:41:01 +00:00
|
|
|
account = request.dbsession.query(LedgerBase).filter(LedgerBase.id == uuid.UUID(request.matchdict['id'])).first()
|
2013-10-03 10:12:43 +00:00
|
|
|
start_date = request.GET.get('StartDate', session_period_start(request))
|
|
|
|
finish_date = request.GET.get('FinishDate', session_period_finish(request))
|
|
|
|
for item in request.json_body['Body']:
|
|
|
|
if 'VoucherID' not in item:
|
|
|
|
continue
|
2016-12-24 11:41:01 +00:00
|
|
|
|
|
|
|
voucher = request.dbsession.query(Voucher).filter(Voucher.id == uuid.UUID(item['VoucherID'])).first()
|
2013-10-03 10:12:43 +00:00
|
|
|
is_reconciled = item['IsReconciled']
|
|
|
|
reconcile_date = datetime.datetime.strptime(item['ReconcileDate'], '%d-%b-%Y')
|
|
|
|
voucher.is_reconciled = is_reconciled
|
|
|
|
voucher.reconcile_date = reconcile_date
|
|
|
|
transaction.commit()
|
|
|
|
|
|
|
|
info = {'StartDate': start_date, 'FinishDate': finish_date,
|
|
|
|
'Account': {'LedgerID': account.id, 'Name': account.name},
|
|
|
|
'Body': [], 'Footer': {}}
|
2016-12-24 11:41:01 +00:00
|
|
|
build_report(request, info, request.dbsession)
|
2013-10-03 10:12:43 +00:00
|
|
|
return info
|