Files
barker/barker/views/voucher/save.py
Amritanshu c81b92c336 Fix: import script to fit the new structure of voucher table (is_printed field removed, voucher_type != KOT is now assumed to be printed)
Fix: Take-away bill type is now removed
Fix: Table overview now shows the right amounts
Voucher Save and Update should now work
Discounts now working (permissions are not checked)
2019-08-08 13:31:30 +05:30

138 lines
4.3 KiB
Python

import datetime
import uuid
from decimal import Decimal
import transaction
from pyramid.httpexceptions import HTTPForbidden
from pyramid.view import view_config
from sqlalchemy import func
from barker.models import (
Overview,
Voucher,
Kot,
Inventory,
InventoryModifier,
VoucherType,
Product,
GuestBook,
FoodTable,
)
from barker.models.validation_exception import ValidationError
from barker.views.voucher import get_tax, get_settlements
from barker.views.voucher.show import voucher_info
@view_config(
request_method="POST", route_name="v1_vouchers_new", renderer="json", trans=True
)
def save(request):
json = request.json_body
now = datetime.datetime.now()
update_table = request.GET["u"] == "true"
voucher_type = VoucherType[request.GET["p"]]
guest_book = get_guest_book(request.GET.get("g", None), request.dbsession)
table = (
request.dbsession.query(FoodTable)
.filter(FoodTable.id == uuid.UUID(json["table"]["id"]))
.first()
)
check_permissions(voucher_type, request.effective_principals)
bill_id = get_bill_id(voucher_type, request.dbsession)
kot_id = request.dbsession.query(
func.coalesce(func.max(Voucher.kot_id), 0) + 1
).scalar()
item = Voucher(
now,
guest_book.pax if guest_book is not None else table.seats,
bill_id,
kot_id,
json["table"]["id"],
json["customer"]["id"] if "id" in json["customer"] else None,
voucher_type,
uuid.UUID(request.authenticated_userid)
)
request.dbsession.add(item)
add_kots(json, item, voucher_type, request.dbsession)
if len(item.kots) == 0 or len(item.kots[0].inventories) == 0:
raise ValidationError("Please add some products!")
if update_table:
do_update_table(item, voucher_type, guest_book, request.dbsession)
transaction.commit()
item = request.dbsession.query(Voucher).filter(Voucher.id == item.id).first()
return voucher_info(item)
def check_permissions(voucher_type, permissions):
if voucher_type == VoucherType.KOT and "Print Kot" not in permissions:
raise HTTPForbidden("You are not allowed to print a kot")
if voucher_type != VoucherType.KOT and "Print Bill" not in permissions:
raise HTTPForbidden("You are not allowed to print bill")
def get_guest_book(guest_book_id, dbsession):
if guest_book_id is None:
return guest_book_id
return (
dbsession.query(GuestBook)
.filter(GuestBook.id == uuid.UUID(guest_book_id))
.first()
)
def get_bill_id(voucher_type, dbsession):
if voucher_type == VoucherType.KOT:
return None
return (
dbsession.query(func.coalesce(func.max(Voucher.bill_id), 0) + 1)
.filter(Voucher.voucher_type == voucher_type.value)
.scalar()
)
def do_update_table(item, voucher_type, guest_book, dbsession):
status = "running" if voucher_type == VoucherType.KOT else "printed"
item.status = Overview(
voucher_id=None,
food_table_id=item.food_table_id,
guest_book_id=guest_book.id if guest_book is not None else None,
status=status,
)
dbsession.add(item.status)
def add_kots(json, item, voucher_type, dbsession):
for k in json["kots"]:
code = dbsession.query(func.coalesce(func.max(Kot.code), 0) + 1).scalar()
kot = Kot(item.id, code, item.food_table_id, item.date, item.user_id)
item.kots.append(kot)
dbsession.add(kot)
for index, i in enumerate(k["inventories"]):
product = dbsession.query(Product).filter(Product.id == uuid.UUID(i["product"]["id"])).first()
tax_rate = get_tax(product.sale_category.tax.rate, voucher_type)
inv = Inventory(
kot.id,
product.id,
round(Decimal(i["quantity"]), 2),
product.price,
round(Decimal(i["discount"]), 5),
i["isHappyHour"],
product.sale_category.tax_id,
tax_rate,
index,
)
kot.inventories.append(inv)
dbsession.add(inv)
for m in i["modifiers"]:
mod = InventoryModifier(None, uuid.UUID(m["id"]), 0)
inv.modifiers.append(mod)
dbsession.add(mod)
get_settlements(item, dbsession)