59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
import uuid
|
|
from decimal import Decimal
|
|
|
|
import transaction
|
|
from pyramid.view import view_config
|
|
|
|
from barker.models import SettleOption, Voucher, Settlement, Overview
|
|
from barker.models.validation_exception import ValidationError
|
|
|
|
|
|
@view_config(
|
|
request_method="POST",
|
|
route_name="v1_vouchers_id",
|
|
renderer="json",
|
|
request_param="receive-payment",
|
|
permission="Settle Bill",
|
|
trans=False,
|
|
)
|
|
def receive_payment(request):
|
|
update_table = request.GET["u"]
|
|
json = request.json_body
|
|
for item in json:
|
|
item["amount"] = round(Decimal(item["amount"]), 0)
|
|
id_ = uuid.UUID(request.matchdict["id"])
|
|
item = request.dbsession.query(Voucher).filter(Voucher.id == id_).first()
|
|
|
|
total_amount = item.amount
|
|
json.append({"id": SettleOption.AMOUNT(), "amount": -total_amount})
|
|
round_off = round(total_amount) - total_amount
|
|
if round_off != 0:
|
|
json.append({"id": SettleOption.ROUND_OFF(), "amount": -round_off})
|
|
if sum([i["amount"] for i in json]) != 0:
|
|
raise ValidationError("Payment received is not equal to bill amount")
|
|
|
|
for i in (j for j in json if j["amount"] != 0):
|
|
amount = i["amount"]
|
|
settlement_type_id = i["id"]
|
|
old = [s for s in item.settlements if s.settled == settlement_type_id]
|
|
if len(old) == 1:
|
|
old[0].amount = amount
|
|
else:
|
|
s = Settlement(item.id, settlement_type_id, amount)
|
|
item.settlements.append(s)
|
|
request.dbsession.add(s)
|
|
|
|
allowed = [a["id"] for a in json if a["amount"] != 0]
|
|
for i in (s for s in item.settlements if s.settled not in allowed):
|
|
item.settlements.remove(i)
|
|
request.dbsession.delete(i)
|
|
|
|
if update_table:
|
|
request.dbsession.query(Overview).filter(
|
|
Overview.voucher_id == item.id
|
|
).delete()
|
|
transaction.commit()
|
|
return True
|
|
|
|
|