Fix: Blank voucher error as incentives was not supplied

This commit is contained in:
Amritanshu Agrawal 2021-11-08 10:08:25 +05:30
parent 40d5914684
commit ff52aa40bc
6 changed files with 52 additions and 44 deletions

View File

@ -384,7 +384,7 @@ def show_blank(
source: str = None,
destination: str = None,
user: UserToken = Security(get_user, scopes=["issue"]),
):
) -> output.Voucher:
date_ = date or get_date(request.session)
additional_info = {"date": date_, "type": VoucherType.ISSUE}
if source:

View File

@ -190,7 +190,7 @@ def show_blank(
request: Request,
a: str = None,
user: UserToken = Security(get_user, scopes=["journal"]),
):
) -> output.Voucher:
if request.scope.get("path") == "/api/payment":
type_ = VoucherType.PAYMENT
elif request.scope.get("path") == "/api/receipt":

View File

@ -362,7 +362,7 @@ def get_id(
def show_blank(
request: Request,
user: UserToken = Security(get_user, scopes=["purchase"]),
):
) -> output.Voucher:
additional_info = {"date": get_date(request.session), "type": VoucherType.PURCHASE}
with SessionFuture() as db:
return blank_voucher(additional_info, db)

View File

@ -328,7 +328,7 @@ def get_id(
def show_blank(
request: Request,
user: UserToken = Security(get_user, scopes=["purchase-return"]),
):
) -> output.Voucher:
additional_info = {"date": get_date(request.session), "type": VoucherType.PURCHASE_RETURN}
with SessionFuture() as db:
return blank_voucher(additional_info, db)

View File

@ -2,7 +2,7 @@ import uuid
from datetime import datetime
from decimal import Decimal
from typing import List, Optional
from typing import List, Optional, Tuple
import brewman.schemas.voucher as output
@ -285,7 +285,7 @@ def voucher_info(voucher, db: Session) -> output.Voucher:
return json_voucher
def blank_voucher(info, db: Session):
def blank_voucher(info, db: Session) -> output.Voucher:
if "type" not in info:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
@ -297,16 +297,18 @@ def blank_voucher(info, db: Session):
status_code=status.HTTP_404_NOT_FOUND,
detail="Date cannot be null",
)
json_voucher = {
"type": type_.name,
"date": info["date"],
"isStarred": False,
"posted": False,
"narration": "",
"journals": [],
"inventories": [],
"employeeBenefits": [],
}
json_voucher = output.Voucher(
type=type_.name,
date=info["date"],
isStarred=False,
posted=False,
narration="",
journals=[],
inventories=[],
incentives=[],
employeeBenefits=[],
files=[],
)
if type_ == VoucherType.JOURNAL:
pass
elif type_ == VoucherType.PAYMENT:
@ -318,10 +320,10 @@ def blank_voucher(info, db: Session):
.one_or_none()
)
if account is not None:
account = {"id": account.id, "name": account.name}
j_account = output.AccountLink(id=account.id, name=account.name)
else:
account = AccountBase.cash_in_hand()
json_voucher["journals"].append({"account": account, "amount": 0, "debit": -1})
j_account = output.AccountLink(id=AccountBase.cash_in_hand()["id"], name=AccountBase.cash_in_hand()["name"])
json_voucher.journals.append(output.Journal(account=j_account, amount=0, debit=-1))
elif type_ == VoucherType.RECEIPT:
account = None
if info and "account" in info and info["account"]:
@ -331,42 +333,44 @@ def blank_voucher(info, db: Session):
.one_or_none()
)
if account is not None:
account = {"id": account.id, "name": account.name}
j_account = output.AccountLink(id=account.id, name=account.name)
else:
account = AccountBase.cash_in_hand()
json_voucher["journals"].append({"account": account, "amount": 0, "debit": 1})
j_account = output.AccountLink(id=AccountBase.cash_in_hand()["id"], name=AccountBase.cash_in_hand()["name"])
json_voucher.journals.append(output.Journal(account=j_account, amount=0, debit=1))
elif type_ == VoucherType.PURCHASE:
json_voucher["vendor"] = AccountBase.local_purchase()
json_voucher.vendor = output.AccountLink(
id=AccountBase.local_purchase()["id"], name=AccountBase.local_purchase()["name"]
)
elif type_ == VoucherType.PURCHASE_RETURN:
json_voucher["vendor"] = AccountBase.local_purchase()
json_voucher.vendor = output.AccountLink(
id=AccountBase.local_purchase()["id"], name=AccountBase.local_purchase()["name"]
)
elif type_ == VoucherType.ISSUE:
if "source" in info:
json_voucher["source"] = {"id": info["source"]}
json_voucher.source = output.CostCentreLink(id=info["source"])
else:
json_voucher["source"] = {"id": CostCentre.cost_centre_purchase()}
json_voucher.source = output.CostCentreLink(id=CostCentre.cost_centre_purchase())
if "destination" in info:
json_voucher["destination"] = {"id": info["destination"]}
json_voucher.destination = output.CostCentreLink(id=info["destination"])
else:
json_voucher["destination"] = {"id": CostCentre.cost_centre_kitchen()}
json_voucher.destination = output.CostCentreLink(id=CostCentre.cost_centre_kitchen())
elif type_ == VoucherType.EMPLOYEE_BENEFIT:
json_voucher["employeeBenefits"] = []
pass
elif type_ == VoucherType.INCENTIVE:
json_voucher["incentives"], json_voucher["incentive"] = incentive_employees(info["date"], db)
json_voucher.incentives, json_voucher.incentive = incentive_employees(info["date"], db)
else:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f'Voucher of type "{type_}" does not exist.',
)
json_voucher["files"] = []
return json_voucher
def incentive_employees(date_, db: Session):
def incentive_employees(date_, db: Session) -> Tuple[List[output.Incentive], Decimal]:
date_ = datetime.strptime(date_, "%d-%b-%Y")
start_date = get_first_day(date_)
finish_date = date_
details = []
details: List[output.Incentive] = []
employees = (
db.execute(
select(Employee)
@ -394,17 +398,17 @@ def incentive_employees(date_, db: Session):
)
days_worked = sum(AttendanceType.by_id(x.attendance_type).value for x in att)
details.append(
{
"employeeId": employee.id,
"name": employee.name,
"designation": employee.designation,
"department": employee.cost_centre.name,
"daysWorked": days_worked,
"points": employee.points,
}
output.Incentive(
employeeId=employee.id,
name=employee.name,
designation=employee.designation,
department=employee.cost_centre.name,
daysWorked=days_worked,
points=employee.points,
)
)
amount = db.execute(
amount: Optional[Decimal] = db.execute(
select(func.sum(Journal.amount * Journal.debit))
.join(Journal.voucher)
.where(
@ -419,7 +423,7 @@ def incentive_employees(date_, db: Session):
),
)
).scalar()
amount = 0 if amount is None else amount * Decimal(0.9) * -1
amount = Decimal(0) if amount is None else amount * Decimal(0.9) * -1
return details, amount

View File

@ -70,12 +70,16 @@ class Voucher(VoucherIn):
@validator("creation_date", pre=True)
def parse_creation_date(cls, value):
if value is None or value == "":
return None
if isinstance(value, datetime):
return value
return datetime.strptime(value, "%d-%b-%Y %H:%M")
@validator("last_edit_date", pre=True)
def parse_last_edit_date(cls, value):
if value is None or value == "":
return None
if isinstance(value, datetime):
return value
return datetime.strptime(value, "%d-%b-%Y %H:%M")