Discount Report Done

This commit is contained in:
Amritanshu
2019-08-21 12:31:52 +05:30
parent 8f6c5930ee
commit 2b04b624b3
18 changed files with 456 additions and 28 deletions

View File

@ -365,13 +365,13 @@ def includeme(config):
config.add_route("v1_product_sale_report", "/v1/product-sale-report")
config.add_route("v1_bill_settlement_report", "/v1/bill-settlement-report")
config.add_route("v1_beer_consumption_report", "/v1/beer-consumption-report")
config.add_route("v1_discount_report", "/v1/discount-report")
# Done till here
config.add_route("customer", "/Customer.json")
config.add_route("customer_list", "/Customers.json")
config.add_route("customer_id", "/Customer/{id}.json")
config.add_route("discount_report", "/DiscountReport.json")
config.add_route("location_list", "/Locations.json")

View File

@ -1,36 +1,56 @@
import datetime
from datetime import datetime, timedelta
from pyramid.httpexceptions import HTTPForbidden
from pyramid.view import view_config
from sqlalchemy import func
from barker.models import Inventory, Kot, Product, MenuCategory, Settlement, SettleOption, Voucher
from barker.models import Inventory, Kot, Product, Voucher, SaleCategory, VoucherType
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='discount_report', renderer='json', permission='Discount Report',
request_param=('s', 'f'))
def discount_report(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')
@view_config(
request_method="GET",
route_name="v1_discount_report",
renderer="json",
permission="Discount Report",
)
def discount_report_view(request):
start_date = get_start_date(request.GET.get("s", None))
finish_date = get_finish_date(request.GET.get("f", None))
if (datetime.date.today() - start_date.date()).days > 5 and 'Accounts Audit' not in request.effective_principals:
raise HTTPForbidden("Accounts Audit")
if (
datetime.today() - start_date.replace(hour=0)
).days > 5 and "Accounts Audit" not in request.effective_principals:
raise ValidationError("Accounts Audit")
amount = func.sum(Inventory.quantity * Inventory.effective_price * Inventory.discount).label('Amount')
list = request.dbsession.query(
MenuCategory.group_type, amount
).join(Voucher.kots).join(Kot.inventories).join(Inventory.product).join(Product.menu_category).filter(
Voucher.date >= start_date,
Voucher.date <= finish_date,
Voucher.is_void == False,
Inventory.discount != 0,
Voucher.settlements.any(~Settlement.settled.in_(
[SettleOption.CASH(), SettleOption.CREDIT_CARD(), SettleOption.BILL_TO_COMPANY(),
SettleOption.UNSETTLED()]))
).group_by(
MenuCategory.group_type
).order_by(
MenuCategory.group_type
).all()
return {
"startDate": start_date.date().strftime("%d-%b-%Y"),
"finishDate": (finish_date - timedelta(days=1)).date().strftime("%d-%b-%Y"),
"amounts": discount_report(start_date, finish_date, request.dbsession),
}
return [{'GroupType': pg, 'Amount': amt} for pg, amt in list]
def discount_report(start_date, finish_date, dbsession):
amount = func.sum(
Inventory.quantity * Inventory.effective_price * Inventory.discount
).label("Amount")
list_ = (
dbsession.query(SaleCategory.name, amount)
.join(Voucher.kots)
.join(Kot.inventories)
.join(Inventory.product)
.join(Product.sale_category)
.filter(
Inventory.discount != 0,
Voucher.date >= start_date,
Voucher.date <= finish_date,
Voucher.voucher_type.in_(
[VoucherType.REGULAR_BILL.value, VoucherType.KOT.value]
),
)
.group_by(SaleCategory.name)
.order_by(SaleCategory.name)
.all()
)
return [{"name": pg, "amount": amt} for pg, amt in list_]