100 lines
3.4 KiB
Python
100 lines
3.4 KiB
Python
import uuid
|
|
|
|
from sqlalchemy import func
|
|
|
|
from barker.exceptions import ValidationFailure
|
|
from barker.models import VoucherType, Settlement, SettleOption, Voucher, Overview, GuestBook
|
|
from barker.models.validation_exception import ValidationError
|
|
|
|
|
|
def get_tax(tax, voucher_type):
|
|
if voucher_type in [VoucherType.STAFF, VoucherType.NO_CHARGE]:
|
|
return 0
|
|
elif voucher_type in [VoucherType.KOT, VoucherType.REGULAR_BILL]:
|
|
return tax
|
|
else:
|
|
raise ValidationError("Unexpected Voucher Type")
|
|
|
|
|
|
def get_bill_id(voucher_type, dbsession):
|
|
if voucher_type == VoucherType.KOT:
|
|
return None
|
|
bill_id = (
|
|
dbsession.query(func.coalesce(func.max(Voucher.bill_id), 0) + 1)
|
|
.filter(Voucher.voucher_type == voucher_type.value)
|
|
.scalar()
|
|
)
|
|
if voucher_type == VoucherType.REGULAR_BILL and bill_id % 10000 == 0:
|
|
bill_id += 1
|
|
return bill_id
|
|
|
|
|
|
def do_update_table(item, guest_book, dbsession):
|
|
status = "running" if item.voucher_type == VoucherType.KOT else "printed"
|
|
if item.status is None:
|
|
item.status = Overview(
|
|
voucher_id=item.id,
|
|
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)
|
|
else:
|
|
item.status.status = status
|
|
|
|
|
|
def check_permissions(item, voucher_type, permissions):
|
|
if voucher_type == VoucherType.KOT and "Print Kot" not in permissions:
|
|
raise ValidationFailure("You are not allowed to print a kot")
|
|
|
|
if voucher_type != VoucherType.KOT and "Print Bill" not in permissions:
|
|
raise ValidationFailure("You are not allowed to print bill")
|
|
|
|
if item is None:
|
|
return
|
|
if item.voucher_type != VoucherType.KOT and "Edit Printed Bill" not in permissions:
|
|
raise ValidationFailure("You are not allowed to edit a printed bill")
|
|
|
|
if item.voucher_type != VoucherType.KOT and voucher_type == VoucherType.KOT:
|
|
raise ValidationFailure("This Bill is already printed\nCannot add a Kot to it.")
|
|
|
|
if item.is_void:
|
|
raise ValidationFailure(
|
|
"This Bill is already void.\nReason: {0}".format(item.void_reason)
|
|
)
|
|
|
|
|
|
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 do_update_settlements(voucher, dbsession):
|
|
settlements = []
|
|
total_amount = voucher.amount
|
|
settlements.append({"id": SettleOption.AMOUNT(), "amount": -total_amount})
|
|
round_off = round(total_amount) - total_amount
|
|
if round_off != 0:
|
|
settlements.append({"id": SettleOption.ROUND_OFF(), "amount": -round_off})
|
|
settlements.append({"id": SettleOption.UNSETTLED(), "amount": round(total_amount)})
|
|
|
|
for i in settlements:
|
|
amount = i["amount"]
|
|
settlement_type_id = i["id"]
|
|
old = [s for s in voucher.settlements if s.settled == settlement_type_id]
|
|
if len(old) == 1:
|
|
old[0].amount = amount
|
|
else:
|
|
s = Settlement(voucher.id, settlement_type_id, amount)
|
|
voucher.settlements.append(s)
|
|
dbsession.add(s)
|
|
|
|
for i in (i for i in voucher.settlements if i.settled not in [x["id"] for x in settlements]):
|
|
voucher.settlements.remove(i)
|
|
dbsession.delete(i)
|