Product Sale Module done (Sales Detail) -- need to rename the permission
This commit is contained in:
@ -362,6 +362,7 @@ def includeme(config):
|
||||
config.add_route("v1_sa_tax", "/v1/sale-analysis/tax")
|
||||
config.add_route("v1_sale_analysis", "/v1/sale-analysis")
|
||||
|
||||
config.add_route("v1_product_sale_report", "/v1/product-sale-report")
|
||||
# Done till here
|
||||
|
||||
config.add_route("customer", "/Customer.json")
|
||||
|
||||
88
barker/views/reports/product_sale_report.py
Normal file
88
barker/views/reports/product_sale_report.py
Normal file
@ -0,0 +1,88 @@
|
||||
from datetime import datetime, timedelta
|
||||
from pyramid.view import view_config
|
||||
from sqlalchemy import func
|
||||
|
||||
from barker.models import (
|
||||
Inventory,
|
||||
Kot,
|
||||
Product,
|
||||
Voucher,
|
||||
SaleCategory,
|
||||
VoucherType,
|
||||
MenuCategory,
|
||||
)
|
||||
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_product_sale_report",
|
||||
renderer="json",
|
||||
permission="Sales Detail",
|
||||
)
|
||||
def product_sale_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.today() - start_date.replace(hour=0)
|
||||
).days > 5 and "Accounts Audit" not in request.effective_principals:
|
||||
raise ValidationError("Accounts Audit")
|
||||
|
||||
return {
|
||||
"startDate": start_date.date().strftime("%d-%b-%Y"),
|
||||
"finishDate": (finish_date - timedelta(days=1)).date().strftime("%d-%b-%Y"),
|
||||
"amounts": product_sale_report(start_date, finish_date, request.dbsession),
|
||||
}
|
||||
|
||||
|
||||
def product_sale_report(start_date, finish_date, dbsession):
|
||||
list_ = (
|
||||
dbsession.query(
|
||||
Product.id,
|
||||
Product.full_name,
|
||||
Voucher.voucher_type,
|
||||
Inventory.is_happy_hour,
|
||||
func.sum(Inventory.quantity),
|
||||
)
|
||||
.join(Inventory.kot)
|
||||
.join(Kot.voucher)
|
||||
.join(Inventory.product)
|
||||
.join(Product.sale_category)
|
||||
.join(Product.menu_category)
|
||||
.filter(Voucher.date >= start_date, Voucher.date <= finish_date)
|
||||
.group_by(
|
||||
SaleCategory.name,
|
||||
MenuCategory.name,
|
||||
Product.id,
|
||||
Product.full_name,
|
||||
Voucher.voucher_type,
|
||||
Inventory.is_happy_hour,
|
||||
)
|
||||
.order_by(SaleCategory.name, MenuCategory.name)
|
||||
.all()
|
||||
)
|
||||
info = []
|
||||
for id_, name, type_, hh, quantity in list_:
|
||||
type_ = to_camel_case(VoucherType(type_).name)
|
||||
old = [i for i in info if i["id"] == id_ and i["isHappyHour"] == hh]
|
||||
if len(old):
|
||||
old[0][type_] = quantity
|
||||
else:
|
||||
info.append(
|
||||
{
|
||||
"id": id_,
|
||||
"name": "H H " + name if hh else name,
|
||||
"isHappyHour": hh,
|
||||
type_: quantity,
|
||||
}
|
||||
)
|
||||
return info
|
||||
|
||||
|
||||
def to_camel_case(snake_str):
|
||||
components = snake_str.split("_")
|
||||
# We capitalize the first letter of each component except the first one
|
||||
# with the 'title' method and join them together.
|
||||
return components[0].lower() + "".join(x.title() for x in components[1:])
|
||||
Reference in New Issue
Block a user