Bill Settlement Report Done (Bill Details)

TODO: Rename Bill Details Permission to Bill Settlement Report
This commit is contained in:
Amritanshu
2019-08-21 10:02:28 +05:30
parent 0f00d96dc9
commit 243c3cb280
19 changed files with 494 additions and 54 deletions

View File

@ -363,6 +363,7 @@ def includeme(config):
config.add_route("v1_sale_analysis", "/v1/sale-analysis")
config.add_route("v1_product_sale_report", "/v1/product-sale-report")
config.add_route("v1_bill_settlement_report", "/v1/bill-settlement-report")
# Done till here
config.add_route("customer", "/Customer.json")
@ -402,6 +403,5 @@ def includeme(config):
# config.add_route('customer_id', '/Customer/{id}.json')
config.add_route("beer_consumption", "/BeerConsumption.json")
config.add_route("bill_details", "/BillDetails.json")
config.add_static_view("", "barker:static")

View File

@ -1,53 +0,0 @@
import datetime
from pyramid.httpexceptions import HTTPForbidden
from pyramid.view import view_config
from sqlalchemy.orm import joinedload
from barker.models import Settlement, SettleOption, Voucher
@view_config(request_method='GET', route_name='bill_details', renderer='json', permission='Bill Details',
request_param=('s', 'f'))
def bill_details(request):
start_date = datetime.datetime.strptime(request.GET['s'], '%d-%b-%Y %H:%M')
finish_date = datetime.datetime.strptime(request.GET['f'], '%d-%b-%Y %H:%M')
if (datetime.date.today() - start_date.date()).days > 5 and 'Accounts Audit' not in request.effective_principals:
raise HTTPForbidden("Accounts Audit")
# TODO: Should be left outer join incase some bill is missing the settlements
vouchers = request.dbsession.query(
Voucher
).options(
joinedload(Voucher.settlements, innerjoin=True).joinedload(Settlement.settle_option, innerjoin=True),
joinedload(Voucher.customer, innerjoin=True)
).filter(
Voucher.date >= start_date,
Voucher.date <= finish_date
).order_by(
Voucher.voucher_type
).order_by(
Voucher.bill_id
).all()
report = []
for item in vouchers:
if item.is_void:
report.append({
'Date': item.date.strftime('%d-%b-%Y %H:%M:%S'),
'BillID': item.full_bill_id,
'Settlement': "Void: {0}".format(item.void_reason),
'Amount': round(next(
s.amount for s in item.settlements if s.settled == SettleOption.AMOUNT()
) * -1, 2)
})
else:
for so in (so for so in item.settlements if so.settle_option.show_in_choices):
report.append({
'Date': item.date.strftime('%d-%b-%Y %H:%M:%S'),
'BillID': item.full_bill_id,
'Settlement': so.settle_option.name,
'Amount': round(so.amount, 2)
})
return report

View File

@ -0,0 +1,70 @@
from datetime import datetime, timedelta
from pyramid.view import view_config
from barker.models import Settlement, SettleOption, Voucher
from barker.models.validation_exception import ValidationError
from barker.views.reports import get_start_date, get_finish_date
@view_config(
request_method="GET",
route_name="v1_bill_settlement_report",
renderer="json",
permission="Bill Details",
)
def bill_details(request):
start_date = get_start_date(request.GET.get("s", None))
finish_date = get_finish_date(request.GET.get("f", None))
if (
datetime.today() - start_date.replace(hour=0)
).days > 5 and "Accounts Audit" not in request.effective_principals:
raise ValidationError("Accounts Audit")
vouchers = (
request.dbsession.query(Voucher)
.join(Voucher.settlements)
.join(Settlement.settle_option)
.filter(Voucher.date >= start_date, Voucher.date <= finish_date)
.order_by(Voucher.voucher_type)
.order_by(Voucher.bill_id)
.all()
)
report = []
for item in vouchers:
if item.is_void:
amount = (
next(
s.amount
for s in item.settlements
if s.settled == SettleOption.AMOUNT()
)
* -1
)
report.append(
{
"date": item.date.strftime("%d-%b-%Y %H:%M:%S"),
"billId": item.full_bill_id,
"settlement": "Void: {0}".format(item.void_reason),
"amount": round(amount, 2),
}
)
else:
for so in (
so for so in item.settlements if so.settle_option.show_in_choices
):
report.append(
{
"date": item.date.strftime("%d-%b-%Y %H:%M:%S"),
"billId": item.full_bill_id,
"settlement": so.settle_option.name,
"amount": round(so.amount, 2),
}
)
return {
"startDate": start_date.date().strftime("%d-%b-%Y"),
"finishDate": (finish_date - timedelta(days=1)).date().strftime("%d-%b-%Y"),
"amounts": report,
}