barker/barker/views/voucher/show.py

162 lines
5.4 KiB
Python

import re
import uuid
from pyramid.view import view_config
from barker.exceptions import ValidationFailure
from barker.models import Voucher, Overview, FoodTable, GuestBook
@view_config(
request_method="GET",
route_name="v1_vouchers_id",
renderer="json",
permission="Authenticated",
)
def show_id(request):
id_ = uuid.UUID(request.matchdict["id"])
item = request.dbsession.query(Voucher).filter(Voucher.id == id_).first()
return voucher_info(item)
@view_config(
request_method="GET",
route_name="v1_vouchers_new",
request_param="b",
renderer="json",
permission="Authenticated",
)
def show(request):
bill_id = request.GET["b"]
item = request.dbsession.query(Voucher)
if re.compile("^\d{2,}-\d{4}$").match(bill_id):
item = item.filter(
Voucher.bill_id == int(bill_id.replace("-", "")),
Voucher.voucher_type.in_([1, 3]),
)
elif re.compile("^NC-\d+$").match(bill_id):
item = item.filter(
Voucher.bill_id == int(bill_id.replace("NC-", "")),
Voucher.voucher_type == 2,
)
elif re.compile("^ST-\d+$").match(bill_id):
item = item.filter(
Voucher.bill_id == int(bill_id.replace("ST-", "")),
Voucher.voucher_type == 4,
)
item = item.first()
if item is None:
return {}
return voucher_info(item)
@view_config(
request_method="GET",
route_name="v1_vouchers_new",
request_param="t",
renderer="json",
permission="Authenticated",
)
def show_for_table(request):
table_id = uuid.UUID(request.GET["t"])
voucher_id = request.GET.get("v", None)
guest_id = request.GET.get("g", None)
if voucher_id is not None:
item = (
request.dbsession.query(Overview)
.filter(
Overview.voucher_id == uuid.UUID(voucher_id),
Overview.food_table_id == table_id,
)
.first()
)
if item is None:
raise ValidationFailure("Bill Not Found")
else:
return voucher_info(item.voucher)
table = request.dbsession.query(FoodTable).filter(FoodTable.id == table_id).first()
if guest_id is not None:
guest = request.dbsession.query(GuestBook).filter(GuestBook.id == guest_id).first()
else:
guest = None
return voucher_blank(table, guest)
def voucher_info(item):
return {
"id": item.id,
"date": item.date.strftime("%d-%b-%Y %H:%M:%S"),
"pax": item.pax,
"user": {"id": item.user_id, "name": item.user.name},
"creationDate": item.creation_date.strftime("%d-%b-%Y %H:%M:%S"),
"lastEditDate": item.last_edit_date.strftime("%d-%b-%Y %H:%M:%S"),
"billId": item.bill_id,
"table": {"id": item.food_table_id, "name": item.food_table.name},
"customer": {"id": item.customer_id, "name": item.customer.name} if item.customer is not None else {},
"settlements": [],
"narration": item.narration,
"void": item.is_void,
"voidReason": item.void_reason,
"voucherType": item.voucher_type.name,
"kotId": item.kot_id,
"kots": [
{
"id": k.id,
"code": k.code,
"date": k.date.strftime("%d-%b-%Y %H:%M:%S"),
"user": {"id": k.user_id, "name": k.user.name},
"inventories": [
{
"id": i.id,
"sortOrder": i.sort_order,
"product": {
"id": i.product_id,
"name": i.product.name,
"units": i.product.units,
"menuCategory": {
"id": i.product.menu_category_id,
"name": i.product.menu_category.name,
"discountLimit": i.product.menu_category.discount_limit,
},
"saleCategory": {
"id": i.product.sale_category_id,
"name": i.product.sale_category.name,
},
},
"quantity": i.quantity,
"price": i.price,
"isHappyHour": i.is_happy_hour,
"taxRate": i.tax_rate,
"tax": {"id": i.tax_id, "name": i.tax.name},
"discount": i.discount,
"inventoryModifier": [
{
"modifier": {
"id": m.modifier.id,
"name": m.modifier.name,
"showInBill": m.modifier.show_in_bill,
},
"price": m.price,
}
for m in i.modifiers
],
}
for i in k.inventories
],
}
for k in item.kots
],
"reprints": [],
}
def voucher_blank(table, guest):
return {
"pax": table.seats if guest is None else guest.pax,
"table": {"id": table.id, "name": table.name},
"customer": {"id": guest.customer_id, "name": guest.customer.name} if guest is not None else {},
"kots": []
}