barker/barker/views/voucher/__init__.py

56 lines
2.0 KiB
Python

from barker.models import VoucherType, Settlement, SettleOption
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_settlements(voucher, dbsession):
amount = voucher.amount
so_amount = [s for s in voucher.settlements if s.settled == SettleOption.AMOUNT()]
if len(so_amount) == 1:
so_amount[0].amount = amount
else:
s = Settlement(voucher.id, SettleOption.AMOUNT(), amount)
voucher.settlements.append(s)
dbsession.add(s)
round_off = round(amount) - amount
so_round_off = [
s for s in voucher.settlements if s.settled == SettleOption.ROUND_OFF()
]
if len(so_round_off) == 1 and round_off != 0:
so_round_off[0].amount = round_off
elif len(so_round_off) == 1 and round_off == 0:
voucher.settlements.remove(so_round_off[0])
dbsession.delete(so_round_off[0])
elif len(so_round_off) == 0 and round_off != 0:
s = Settlement(voucher.id, SettleOption.ROUND_OFF(), round_off)
voucher.settlements.append(s)
dbsession.add(s)
unsettled = sum(
s.amount for s in voucher.settlements if s.settled != SettleOption.UNSETTLED()
)
so_unsettled = [
s for s in voucher.settlements if s.settled == SettleOption.UNSETTLED()
]
if len(so_unsettled) == 1 and unsettled != 0:
so_unsettled[0].amount = unsettled
elif len(so_unsettled) == 1 and unsettled == 0:
voucher.settlements.remove(so_unsettled[0])
dbsession.delete(so_unsettled[0])
elif len(so_unsettled) == 0 and unsettled != 0:
s = Settlement(voucher.id, SettleOption.UNSETTLED(), unsettled)
voucher.settlements.append(s)
dbsession.add(s)
return unsettled