Fix: Blank voucher error as incentives was not supplied
This commit is contained in:
@ -384,7 +384,7 @@ def show_blank(
|
|||||||
source: str = None,
|
source: str = None,
|
||||||
destination: str = None,
|
destination: str = None,
|
||||||
user: UserToken = Security(get_user, scopes=["issue"]),
|
user: UserToken = Security(get_user, scopes=["issue"]),
|
||||||
):
|
) -> output.Voucher:
|
||||||
date_ = date or get_date(request.session)
|
date_ = date or get_date(request.session)
|
||||||
additional_info = {"date": date_, "type": VoucherType.ISSUE}
|
additional_info = {"date": date_, "type": VoucherType.ISSUE}
|
||||||
if source:
|
if source:
|
||||||
|
|||||||
@ -190,7 +190,7 @@ def show_blank(
|
|||||||
request: Request,
|
request: Request,
|
||||||
a: str = None,
|
a: str = None,
|
||||||
user: UserToken = Security(get_user, scopes=["journal"]),
|
user: UserToken = Security(get_user, scopes=["journal"]),
|
||||||
):
|
) -> output.Voucher:
|
||||||
if request.scope.get("path") == "/api/payment":
|
if request.scope.get("path") == "/api/payment":
|
||||||
type_ = VoucherType.PAYMENT
|
type_ = VoucherType.PAYMENT
|
||||||
elif request.scope.get("path") == "/api/receipt":
|
elif request.scope.get("path") == "/api/receipt":
|
||||||
|
|||||||
@ -362,7 +362,7 @@ def get_id(
|
|||||||
def show_blank(
|
def show_blank(
|
||||||
request: Request,
|
request: Request,
|
||||||
user: UserToken = Security(get_user, scopes=["purchase"]),
|
user: UserToken = Security(get_user, scopes=["purchase"]),
|
||||||
):
|
) -> output.Voucher:
|
||||||
additional_info = {"date": get_date(request.session), "type": VoucherType.PURCHASE}
|
additional_info = {"date": get_date(request.session), "type": VoucherType.PURCHASE}
|
||||||
with SessionFuture() as db:
|
with SessionFuture() as db:
|
||||||
return blank_voucher(additional_info, db)
|
return blank_voucher(additional_info, db)
|
||||||
|
|||||||
@ -328,7 +328,7 @@ def get_id(
|
|||||||
def show_blank(
|
def show_blank(
|
||||||
request: Request,
|
request: Request,
|
||||||
user: UserToken = Security(get_user, scopes=["purchase-return"]),
|
user: UserToken = Security(get_user, scopes=["purchase-return"]),
|
||||||
):
|
) -> output.Voucher:
|
||||||
additional_info = {"date": get_date(request.session), "type": VoucherType.PURCHASE_RETURN}
|
additional_info = {"date": get_date(request.session), "type": VoucherType.PURCHASE_RETURN}
|
||||||
with SessionFuture() as db:
|
with SessionFuture() as db:
|
||||||
return blank_voucher(additional_info, db)
|
return blank_voucher(additional_info, db)
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import uuid
|
|||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from typing import List, Optional
|
from typing import List, Optional, Tuple
|
||||||
|
|
||||||
import brewman.schemas.voucher as output
|
import brewman.schemas.voucher as output
|
||||||
|
|
||||||
@ -285,7 +285,7 @@ def voucher_info(voucher, db: Session) -> output.Voucher:
|
|||||||
return json_voucher
|
return json_voucher
|
||||||
|
|
||||||
|
|
||||||
def blank_voucher(info, db: Session):
|
def blank_voucher(info, db: Session) -> output.Voucher:
|
||||||
if "type" not in info:
|
if "type" not in info:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
@ -297,16 +297,18 @@ def blank_voucher(info, db: Session):
|
|||||||
status_code=status.HTTP_404_NOT_FOUND,
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
detail="Date cannot be null",
|
detail="Date cannot be null",
|
||||||
)
|
)
|
||||||
json_voucher = {
|
json_voucher = output.Voucher(
|
||||||
"type": type_.name,
|
type=type_.name,
|
||||||
"date": info["date"],
|
date=info["date"],
|
||||||
"isStarred": False,
|
isStarred=False,
|
||||||
"posted": False,
|
posted=False,
|
||||||
"narration": "",
|
narration="",
|
||||||
"journals": [],
|
journals=[],
|
||||||
"inventories": [],
|
inventories=[],
|
||||||
"employeeBenefits": [],
|
incentives=[],
|
||||||
}
|
employeeBenefits=[],
|
||||||
|
files=[],
|
||||||
|
)
|
||||||
if type_ == VoucherType.JOURNAL:
|
if type_ == VoucherType.JOURNAL:
|
||||||
pass
|
pass
|
||||||
elif type_ == VoucherType.PAYMENT:
|
elif type_ == VoucherType.PAYMENT:
|
||||||
@ -318,10 +320,10 @@ def blank_voucher(info, db: Session):
|
|||||||
.one_or_none()
|
.one_or_none()
|
||||||
)
|
)
|
||||||
if account is not None:
|
if account is not None:
|
||||||
account = {"id": account.id, "name": account.name}
|
j_account = output.AccountLink(id=account.id, name=account.name)
|
||||||
else:
|
else:
|
||||||
account = AccountBase.cash_in_hand()
|
j_account = output.AccountLink(id=AccountBase.cash_in_hand()["id"], name=AccountBase.cash_in_hand()["name"])
|
||||||
json_voucher["journals"].append({"account": account, "amount": 0, "debit": -1})
|
json_voucher.journals.append(output.Journal(account=j_account, amount=0, debit=-1))
|
||||||
elif type_ == VoucherType.RECEIPT:
|
elif type_ == VoucherType.RECEIPT:
|
||||||
account = None
|
account = None
|
||||||
if info and "account" in info and info["account"]:
|
if info and "account" in info and info["account"]:
|
||||||
@ -331,42 +333,44 @@ def blank_voucher(info, db: Session):
|
|||||||
.one_or_none()
|
.one_or_none()
|
||||||
)
|
)
|
||||||
if account is not None:
|
if account is not None:
|
||||||
account = {"id": account.id, "name": account.name}
|
j_account = output.AccountLink(id=account.id, name=account.name)
|
||||||
else:
|
else:
|
||||||
account = AccountBase.cash_in_hand()
|
j_account = output.AccountLink(id=AccountBase.cash_in_hand()["id"], name=AccountBase.cash_in_hand()["name"])
|
||||||
json_voucher["journals"].append({"account": account, "amount": 0, "debit": 1})
|
json_voucher.journals.append(output.Journal(account=j_account, amount=0, debit=1))
|
||||||
elif type_ == VoucherType.PURCHASE:
|
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:
|
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:
|
elif type_ == VoucherType.ISSUE:
|
||||||
if "source" in info:
|
if "source" in info:
|
||||||
json_voucher["source"] = {"id": info["source"]}
|
json_voucher.source = output.CostCentreLink(id=info["source"])
|
||||||
else:
|
else:
|
||||||
json_voucher["source"] = {"id": CostCentre.cost_centre_purchase()}
|
json_voucher.source = output.CostCentreLink(id=CostCentre.cost_centre_purchase())
|
||||||
if "destination" in info:
|
if "destination" in info:
|
||||||
json_voucher["destination"] = {"id": info["destination"]}
|
json_voucher.destination = output.CostCentreLink(id=info["destination"])
|
||||||
else:
|
else:
|
||||||
json_voucher["destination"] = {"id": CostCentre.cost_centre_kitchen()}
|
json_voucher.destination = output.CostCentreLink(id=CostCentre.cost_centre_kitchen())
|
||||||
elif type_ == VoucherType.EMPLOYEE_BENEFIT:
|
elif type_ == VoucherType.EMPLOYEE_BENEFIT:
|
||||||
json_voucher["employeeBenefits"] = []
|
pass
|
||||||
elif type_ == VoucherType.INCENTIVE:
|
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:
|
else:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
detail=f'Voucher of type "{type_}" does not exist.',
|
detail=f'Voucher of type "{type_}" does not exist.',
|
||||||
)
|
)
|
||||||
json_voucher["files"] = []
|
|
||||||
return json_voucher
|
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")
|
date_ = datetime.strptime(date_, "%d-%b-%Y")
|
||||||
start_date = get_first_day(date_)
|
start_date = get_first_day(date_)
|
||||||
finish_date = date_
|
finish_date = date_
|
||||||
details = []
|
details: List[output.Incentive] = []
|
||||||
employees = (
|
employees = (
|
||||||
db.execute(
|
db.execute(
|
||||||
select(Employee)
|
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)
|
days_worked = sum(AttendanceType.by_id(x.attendance_type).value for x in att)
|
||||||
details.append(
|
details.append(
|
||||||
{
|
output.Incentive(
|
||||||
"employeeId": employee.id,
|
employeeId=employee.id,
|
||||||
"name": employee.name,
|
name=employee.name,
|
||||||
"designation": employee.designation,
|
designation=employee.designation,
|
||||||
"department": employee.cost_centre.name,
|
department=employee.cost_centre.name,
|
||||||
"daysWorked": days_worked,
|
daysWorked=days_worked,
|
||||||
"points": employee.points,
|
points=employee.points,
|
||||||
}
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
amount = db.execute(
|
amount: Optional[Decimal] = db.execute(
|
||||||
select(func.sum(Journal.amount * Journal.debit))
|
select(func.sum(Journal.amount * Journal.debit))
|
||||||
.join(Journal.voucher)
|
.join(Journal.voucher)
|
||||||
.where(
|
.where(
|
||||||
@ -419,7 +423,7 @@ def incentive_employees(date_, db: Session):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
).scalar()
|
).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
|
return details, amount
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -70,12 +70,16 @@ class Voucher(VoucherIn):
|
|||||||
|
|
||||||
@validator("creation_date", pre=True)
|
@validator("creation_date", pre=True)
|
||||||
def parse_creation_date(cls, value):
|
def parse_creation_date(cls, value):
|
||||||
|
if value is None or value == "":
|
||||||
|
return None
|
||||||
if isinstance(value, datetime):
|
if isinstance(value, datetime):
|
||||||
return value
|
return value
|
||||||
return datetime.strptime(value, "%d-%b-%Y %H:%M")
|
return datetime.strptime(value, "%d-%b-%Y %H:%M")
|
||||||
|
|
||||||
@validator("last_edit_date", pre=True)
|
@validator("last_edit_date", pre=True)
|
||||||
def parse_last_edit_date(cls, value):
|
def parse_last_edit_date(cls, value):
|
||||||
|
if value is None or value == "":
|
||||||
|
return None
|
||||||
if isinstance(value, datetime):
|
if isinstance(value, datetime):
|
||||||
return value
|
return value
|
||||||
return datetime.strptime(value, "%d-%b-%Y %H:%M")
|
return datetime.strptime(value, "%d-%b-%Y %H:%M")
|
||||||
|
|||||||
Reference in New Issue
Block a user