Receive payment working.
TODO: Nested subscribe in Receive Payment in sales-home-component.ts should be refactored into something more readable.
This commit is contained in:
@ -191,7 +191,7 @@ class Voucher(Base):
|
||||
|
||||
@property
|
||||
def amount(self):
|
||||
return sum(i.amount for k in self.kots for i in k.inventories)
|
||||
return round(sum(i.amount for k in self.kots for i in k.inventories), 2)
|
||||
|
||||
|
||||
class Kot(Base):
|
||||
|
||||
@ -393,7 +393,6 @@ def includeme(config):
|
||||
config.add_route("sa_tax", "/SaleAnalysis/Tax.json")
|
||||
|
||||
config.add_route("voucher_reprint", "/ReprintVoucher/{id}.json")
|
||||
config.add_route("voucher_settle", "/Settle/{id}.json")
|
||||
config.add_route("voucher_split", "/Split/{id}.json")
|
||||
config.add_route("voucher_void", "/Void/{id}.json")
|
||||
|
||||
|
||||
@ -12,9 +12,7 @@ def get_tax(tax, voucher_type):
|
||||
|
||||
|
||||
def get_settlements(voucher, dbsession):
|
||||
amount = round(
|
||||
-1 * sum(sum(i.amount for i in k.inventories) for k in voucher.kots), 5
|
||||
)
|
||||
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
|
||||
|
||||
60
barker/views/voucher/receive_payment.py
Normal file
60
barker/views/voucher/receive_payment.py
Normal file
@ -0,0 +1,60 @@
|
||||
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
|
||||
from barker.views.voucher.show import voucher_info
|
||||
|
||||
|
||||
@view_config(
|
||||
request_method="POST",
|
||||
route_name="v1_vouchers_id",
|
||||
renderer="json",
|
||||
request_param="r",
|
||||
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()
|
||||
item = request.dbsession.query(Voucher).filter(Voucher.id == item.id).first()
|
||||
return voucher_info(item)
|
||||
|
||||
|
||||
@ -1,53 +0,0 @@
|
||||
import uuid
|
||||
|
||||
import transaction
|
||||
from pyramid.view import view_config
|
||||
|
||||
from barker.models import SettleOption, Voucher, Settlement, Overview
|
||||
from barker.views.voucher import get_settlements
|
||||
from barker.views.voucher.show import voucher_info
|
||||
|
||||
|
||||
@view_config(
|
||||
request_method="POST",
|
||||
route_name="voucher_settle",
|
||||
renderer="json",
|
||||
permission="Settle Bill",
|
||||
trans=False,
|
||||
)
|
||||
def settle_voucher(request):
|
||||
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()
|
||||
|
||||
allowed = [
|
||||
SettleOption.AMOUNT(),
|
||||
SettleOption.ROUND_OFF(),
|
||||
SettleOption.UNSETTLED(),
|
||||
]
|
||||
for i in (j for j in json if j["Amount"] != 0):
|
||||
amount = i["Amount"]
|
||||
so = i["Settled"]
|
||||
so_settled = [s for s in item.settlements if s.settled == so]
|
||||
if len(so_settled) == 1:
|
||||
so_settled[0].amount = amount
|
||||
else:
|
||||
s = Settlement(item.id, so, amount)
|
||||
item.settlements.append(s)
|
||||
request.dbsession.add(s)
|
||||
allowed.append(so)
|
||||
for i in (s for s in item.settlements if s.settled not in allowed):
|
||||
item.settlements.remove(i)
|
||||
request.dbsession.delete(i)
|
||||
unsettled = get_settlements(item, request.dbsession)
|
||||
|
||||
if unsettled == 0 and update_table:
|
||||
request.dbsession.query(Overview).filter(
|
||||
Overview.voucher_id == item.id
|
||||
).delete()
|
||||
transaction.commit()
|
||||
item = request.dbsession.query(Voucher).filter(Voucher.id == item.id).first()
|
||||
return voucher_info(item)
|
||||
|
||||
|
||||
@ -97,7 +97,7 @@ def voucher_info(item):
|
||||
"narration": item.narration,
|
||||
"void": item.is_void,
|
||||
"voidReason": item.void_reason,
|
||||
"voucherType": item.voucher_type.value,
|
||||
"voucherType": item.voucher_type.name,
|
||||
"kotId": item.kot_id,
|
||||
"kots": [
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user