Fix: The lock was not working because:

1. The Account Types sent were sqlqueries and not integer literals
2. Voucher Types and Account Types were being compared to VoucherTypesSelected and AccountTypesSelected objects and not their ids.
This commit is contained in:
Amritanshu Agrawal 2021-09-10 21:46:47 +05:30
parent dbf0ecf0d8
commit 637932c67c
8 changed files with 112 additions and 54 deletions

@ -46,20 +46,16 @@ def get_lock_info(
data = sorted(data, key=lambda d: d["index"], reverse=True) data = sorted(data, key=lambda d: d["index"], reverse=True)
for it in data: for it in data:
li: LockInformation = LockInformation( li: LockInformation = LockInformation(
voucherTypes=[ voucherTypes=[VoucherTypesSelected(id=vt["id_"], name=vt["name"]) for vt in it["voucher_types"]],
VoucherTypesSelected(id=vt["id_"], name=VoucherType.by_id(vt["id_"]).name) for vt in it["voucher_types"] accountTypes=[AccountTypesSelected(id=at["id_"], name=at["name"]) for at in it["account_types"]],
],
accountTypes=[
AccountTypesSelected(id=at["id_"], name=AccountType.by_id(at["id_"]).name) for at in it["account_types"]
],
start=LockDate(days=it["start"]["days"], date=it["start"]["date_"]), start=LockDate(days=it["start"]["days"], date=it["start"]["date_"]),
finish=LockDate(days=it["finish"]["days"], date=it["finish"]["date_"]), finish=LockDate(days=it["finish"]["days"], date=it["finish"]["date_"]),
index=it["index"], index=it["index"],
) )
# Data format # Data format
if len(li.voucher_types) != 0 and voucher_type not in li.voucher_types: if len(li.voucher_types) != 0 and voucher_type not in [vt.id_ for vt in li.voucher_types]:
continue continue
if len(li.account_types) != 0 and len(set(account_types) & set(li.account_types)) == 0: if len(li.account_types) != 0 and len(set(account_types) & set([at.id_ for at in li.account_types])) == 0:
continue continue
if li.start.days is not None: if li.start.days is not None:

@ -61,8 +61,14 @@ def save_route(
def save(data: schema_in.EmployeeBenefitIn, date_: date, user: UserToken, db: Session) -> Voucher: def save(data: schema_in.EmployeeBenefitIn, date_: date, user: UserToken, db: Session) -> Voucher:
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where(AccountBase.id.in_([dj.employee.id_ for dj in data.employee_benefits])) db.execute(
select(distinct(AccountBase.type)).where(
AccountBase.id.in_([dj.employee.id_ for dj in data.employee_benefits])
)
)
.scalars()
.all()
) )
allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db) allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db)
if not allowed: if not allowed:
@ -174,12 +180,16 @@ def update_route(
def update_voucher(id_: uuid.UUID, data: schema_in.EmployeeBenefitIn, user: UserToken, db: Session) -> Voucher: def update_voucher(id_: uuid.UUID, data: schema_in.EmployeeBenefitIn, user: UserToken, db: Session) -> Voucher:
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one()
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where( db.execute(
AccountBase.id.in_( select(distinct(AccountBase.type)).where(
[dj.employee.id_ for dj in data.employee_benefits] + [vj.account_id for vj in voucher.journals] AccountBase.id.in_(
[dj.employee.id_ for dj in data.employee_benefits] + [vj.account_id for vj in voucher.journals]
)
) )
) )
.scalars()
.all()
) )
allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db) allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db)
if not allowed: if not allowed:

@ -61,8 +61,12 @@ def save_route(
def save(data: schema_in.IncentiveIn, user: UserToken, db: Session) -> Voucher: def save(data: schema_in.IncentiveIn, user: UserToken, db: Session) -> Voucher:
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where(AccountBase.id.in_([dj.employee_id for dj in data.incentives])) db.execute(
select(distinct(AccountBase.type)).where(AccountBase.id.in_([dj.employee_id for dj in data.incentives]))
)
.scalars()
.all()
) )
allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db) allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db)
if not allowed: if not allowed:
@ -137,10 +141,16 @@ def update_route(
def update_voucher(id_: uuid.UUID, data: schema_in.IncentiveIn, user: UserToken, db: Session) -> Voucher: def update_voucher(id_: uuid.UUID, data: schema_in.IncentiveIn, user: UserToken, db: Session) -> Voucher:
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one()
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where( db.execute(
AccountBase.id.in_([dj.employee_id for dj in data.incentives] + [vj.account_id for vj in voucher.journals]) select(distinct(AccountBase.type)).where(
AccountBase.id.in_(
[dj.employee_id for dj in data.incentives] + [vj.account_id for vj in voucher.journals]
)
)
) )
.scalars()
.all()
) )
allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db) allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db)
if not allowed: if not allowed:

@ -63,7 +63,9 @@ def save_route(
def save(data: schema_in.IssueIn, user: UserToken, db: Session) -> (Voucher, Optional[bool]): def save(data: schema_in.IssueIn, user: UserToken, db: Session) -> (Voucher, Optional[bool]):
product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories])) product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories]))
account_types = db.execute(select(distinct(AccountBase.type)).where(AccountBase.id.in_(product_accounts))) account_types = (
db.execute(select(distinct(AccountBase.type)).where(AccountBase.id.in_(product_accounts))).scalars().all()
)
allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db) allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db)
if not allowed: if not allowed:
raise HTTPException( raise HTTPException(
@ -189,13 +191,17 @@ def update_route(
def update_voucher(id_: uuid.UUID, data: schema_in.IssueIn, user: UserToken, db: Session) -> (Voucher, Optional[bool]): def update_voucher(id_: uuid.UUID, data: schema_in.IssueIn, user: UserToken, db: Session) -> (Voucher, Optional[bool]):
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one()
product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories])) product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories]))
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where( db.execute(
or_( select(distinct(AccountBase.type)).where(
AccountBase.id.in_([vj.account_id for vj in voucher.journals]), or_(
AccountBase.id.in_(product_accounts), AccountBase.id.in_([vj.account_id for vj in voucher.journals]),
AccountBase.id.in_(product_accounts),
)
) )
) )
.scalars()
.all()
) )
allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db) allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db)
if not allowed: if not allowed:

@ -54,8 +54,12 @@ def save_route(
def save(data: schema_in.JournalIn, user: UserToken, db: Session) -> Voucher: def save(data: schema_in.JournalIn, user: UserToken, db: Session) -> Voucher:
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where(AccountBase.id.in_([dj.account.id_ for dj in data.journals])) db.execute(
select(distinct(AccountBase.type)).where(AccountBase.id.in_([dj.account.id_ for dj in data.journals]))
)
.scalars()
.all()
) )
allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db) allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db)
if not allowed: if not allowed:
@ -114,10 +118,16 @@ def update_route(
def update_voucher(id_: uuid.UUID, data: schema_in.JournalIn, user: UserToken, db: Session) -> Voucher: def update_voucher(id_: uuid.UUID, data: schema_in.JournalIn, user: UserToken, db: Session) -> Voucher:
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one()
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where( db.execute(
AccountBase.id.in_([dj.account.id_ for dj in data.journals] + [vj.account_id for vj in voucher.journals]) select(distinct(AccountBase.type)).where(
AccountBase.id.in_(
[dj.account.id_ for dj in data.journals] + [vj.account_id for vj in voucher.journals]
)
)
) )
.scalars()
.all()
) )
allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db) allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db)
if not allowed: if not allowed:

@ -62,13 +62,17 @@ def save_route(
def save(data: schema_in.PurchaseIn, user: UserToken, db: Session) -> Voucher: def save(data: schema_in.PurchaseIn, user: UserToken, db: Session) -> Voucher:
product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories])) product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories]))
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where( db.execute(
or_( select(distinct(AccountBase.type)).where(
AccountBase.id == data.vendor.id_, or_(
AccountBase.id.in_(product_accounts), AccountBase.id == data.vendor.id_,
AccountBase.id.in_(product_accounts),
)
) )
) )
.scalars()
.all()
) )
allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db) allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db)
if not allowed: if not allowed:
@ -170,13 +174,17 @@ def update_route(
def update_voucher(id_: uuid.UUID, data: schema_in.PurchaseIn, user: UserToken, db: Session) -> Voucher: def update_voucher(id_: uuid.UUID, data: schema_in.PurchaseIn, user: UserToken, db: Session) -> Voucher:
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one()
product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories])) product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories]))
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where( db.execute(
or_( select(distinct(AccountBase.type)).where(
AccountBase.id.in_([data.vendor.id_] + [vj.account_id for vj in voucher.journals]), or_(
AccountBase.id.in_(product_accounts), AccountBase.id.in_([data.vendor.id_] + [vj.account_id for vj in voucher.journals]),
AccountBase.id.in_(product_accounts),
)
) )
) )
.scalars()
.all()
) )
allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db) allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db)
if not allowed: if not allowed:

@ -62,13 +62,17 @@ def save_route(
def save(data: schema_in.PurchaseIn, user: UserToken, db: Session) -> Voucher: def save(data: schema_in.PurchaseIn, user: UserToken, db: Session) -> Voucher:
product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories])) product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories]))
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where( db.execute(
or_( select(distinct(AccountBase.type)).where(
AccountBase.id == data.vendor.id_, or_(
AccountBase.id.in_(product_accounts), AccountBase.id == data.vendor.id_,
AccountBase.id.in_(product_accounts),
)
) )
) )
.scalars()
.all()
) )
allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db) allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db)
if not allowed: if not allowed:
@ -177,13 +181,17 @@ def update_route(
def update_voucher(id_: uuid.UUID, data: schema_in.PurchaseIn, user: UserToken, db: Session) -> Voucher: def update_voucher(id_: uuid.UUID, data: schema_in.PurchaseIn, user: UserToken, db: Session) -> Voucher:
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one()
product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories])) product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories]))
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where( db.execute(
or_( select(distinct(AccountBase.type)).where(
AccountBase.id.in_([data.vendor.id_] + [vj.account_id for vj in voucher.journals]), or_(
AccountBase.id.in_(product_accounts), AccountBase.id.in_([data.vendor.id_] + [vj.account_id for vj in voucher.journals]),
AccountBase.id.in_(product_accounts),
)
) )
) )
.scalars()
.all()
) )
allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db) allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db)
if not allowed: if not allowed:

@ -41,8 +41,14 @@ def post_voucher(
try: try:
with SessionFuture() as db: with SessionFuture() as db:
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one()
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where(AccountBase.id.in_([vj.account_id for vj in voucher.journals])) db.execute(
select(distinct(AccountBase.type)).where(
AccountBase.id.in_([vj.account_id for vj in voucher.journals])
)
)
.scalars()
.all()
) )
allowed, message = get_lock_info([voucher.date], voucher.type, account_types, db) allowed, message = get_lock_info([voucher.date], voucher.type, account_types, db)
if not allowed: if not allowed:
@ -91,8 +97,12 @@ def delete_voucher(
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one()
images = db.execute(select(DbImage).where(DbImage.resource_id == voucher.id)).scalars().all() images = db.execute(select(DbImage).where(DbImage.resource_id == voucher.id)).scalars().all()
check_delete_permissions(voucher, user) check_delete_permissions(voucher, user)
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where(AccountBase.id.in_([vj.account_id for vj in voucher.journals])) db.execute(
select(distinct(AccountBase.type)).where(AccountBase.id.in_([vj.account_id for vj in voucher.journals]))
)
.scalars()
.all()
) )
allowed, message = get_lock_info([voucher.date], voucher.type, account_types, db) allowed, message = get_lock_info([voucher.date], voucher.type, account_types, db)
if not allowed: if not allowed: