Voucher basic working running tables shifted to cards from buttons, this gives us immense styling oportunities
130 lines
4.5 KiB
Python
130 lines
4.5 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.exceptions import ValidationFailure
|
|
from barker.models import Voucher, Kot, Inventory, InventoryModifier, Overview
|
|
from barker.views.voucher import get_st_vat, get_settlements
|
|
from barker.views.voucher.show import voucher_info
|
|
|
|
|
|
@view_config(
|
|
request_method="POST", route_name="v1_vouchers_id", renderer="json", trans=True
|
|
)
|
|
def update(request):
|
|
now = datetime.now()
|
|
update_table = request.GET["u"]
|
|
json = request.json_body
|
|
id = uuid.UUID(request.matchdict["id"])
|
|
item = request.dbsession.query(Voucher).filter(Voucher.id == id).first()
|
|
|
|
if not json["Printed"] and "Print Kot" not in request.effective_principals:
|
|
raise HTTPForbidden("You are not allowed to print a kot")
|
|
|
|
if json["Printed"] and "Print Bill" not in request.effective_principals:
|
|
raise HTTPForbidden("You are not allowed to print bill")
|
|
|
|
if item.is_printed and "Edit Printed Bill" not in request.effective_principals:
|
|
raise HTTPForbidden("You are not allowed to edit a printed bill")
|
|
|
|
if item.is_printed and not json["Printed"]:
|
|
transaction.abort()
|
|
raise ValidationFailure("This Bill is already printed\nCannot add a Kot to it.")
|
|
|
|
if item.is_void:
|
|
transaction.abort()
|
|
raise ValidationFailure(
|
|
"This Bill is already void.\nReason: {0}".format(item.void_reason)
|
|
)
|
|
|
|
if item.is_printed and not json["Printed"]:
|
|
transaction.abort()
|
|
raise ValidationFailure("This Bill is already printed\nCannot add a Kot to it.")
|
|
|
|
item.pax = json["Pax"]
|
|
item.food_table_id = json["Table"]["FoodTableID"]
|
|
item.customer_id = json["Customer"]["CustomerID"]
|
|
item.voucher_type = json["VoucherType"]
|
|
if not item.is_printed and json["Printed"]:
|
|
item.date = now
|
|
type = [1, 3] if item.voucher_type in [1, 3] else [item.voucher_type]
|
|
item.bill_id = (
|
|
request.dbsession.query(func.coalesce(func.max(Voucher.bill_id), 0) + 1)
|
|
.filter(Voucher.voucher_type.in_(type))
|
|
.scalar()
|
|
)
|
|
|
|
item.is_printed = item.is_printed or json["Printed"]
|
|
item.user_id = json["User"]["UserID"]
|
|
item.last_edit_date = now
|
|
for k in item.kots:
|
|
for i in k.inventories:
|
|
i.service_tax_rate, i.vat_rate = get_st_vat(
|
|
i.service_tax_rate, i.vat_rate, item.voucher_type
|
|
)
|
|
i.discount = next(
|
|
Decimal(inv["Discount"])
|
|
for ko in json["Kots"]
|
|
for inv in ko["Inventories"]
|
|
if uuid.UUID(inv["InventoryID"]) == i.id
|
|
)
|
|
for k in (
|
|
k for k in json["Kots"] if k["KotID"] == "00000000-0000-0000-0000-000000000000"
|
|
):
|
|
kot = Kot(
|
|
item.id,
|
|
item.food_table_id,
|
|
item.date,
|
|
item.user_id,
|
|
dbsession=request.dbsession,
|
|
)
|
|
item.kots.append(kot)
|
|
request.dbsession.add(kot)
|
|
for index, i in enumerate(k["Inventories"]):
|
|
st, vat = get_st_vat(i["ServiceTaxRate"], i["VatRate"], json["VoucherType"])
|
|
inv = Inventory(
|
|
kot.id,
|
|
uuid.UUID(i["ProductID"]),
|
|
i["Quantity"],
|
|
i["Price"],
|
|
i["Discount"],
|
|
i["IsHappyHour"],
|
|
i["ServiceTaxID"],
|
|
st,
|
|
i["VatID"],
|
|
vat,
|
|
index,
|
|
)
|
|
kot.inventories.append(inv)
|
|
request.dbsession.add(inv)
|
|
for m in i["Modifiers"]:
|
|
mod = InventoryModifier(None, uuid.UUID(m["ModifierID"]), 0)
|
|
inv.modifiers.append(mod)
|
|
request.dbsession.add(mod)
|
|
get_settlements(item, request.dbsession)
|
|
if update_table:
|
|
vft = (
|
|
request.dbsession.query(Overview)
|
|
.filter(Overview.voucher_id == item.id)
|
|
.first()
|
|
)
|
|
status = "printed" if item.is_printed else "running"
|
|
if vft is None:
|
|
item.status = Overview(
|
|
voucher_id=None, food_table_id=item.food_table_id, status=status
|
|
)
|
|
request.dbsession.add(item.status)
|
|
else:
|
|
vft.status = status
|
|
vft.food_table_id = item.food_table_id
|
|
transaction.commit()
|
|
item = request.dbsession.query(Voucher).filter(Voucher.id == item.id).first()
|
|
return voucher_info(item)
|
|
|
|
|