diff --git a/brewman/brewman/routers/__init__.py b/brewman/brewman/routers/__init__.py index 63d397bc..86bb4152 100644 --- a/brewman/brewman/routers/__init__.py +++ b/brewman/brewman/routers/__init__.py @@ -46,20 +46,16 @@ def get_lock_info( data = sorted(data, key=lambda d: d["index"], reverse=True) for it in data: li: LockInformation = LockInformation( - voucherTypes=[ - VoucherTypesSelected(id=vt["id_"], name=VoucherType.by_id(vt["id_"]).name) for vt in it["voucher_types"] - ], - accountTypes=[ - AccountTypesSelected(id=at["id_"], name=AccountType.by_id(at["id_"]).name) for at in it["account_types"] - ], + voucherTypes=[VoucherTypesSelected(id=vt["id_"], name=vt["name"]) for vt in it["voucher_types"]], + accountTypes=[AccountTypesSelected(id=at["id_"], name=at["name"]) for at in it["account_types"]], start=LockDate(days=it["start"]["days"], date=it["start"]["date_"]), finish=LockDate(days=it["finish"]["days"], date=it["finish"]["date_"]), index=it["index"], ) # 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 - 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 if li.start.days is not None: diff --git a/brewman/brewman/routers/employee_benefit.py b/brewman/brewman/routers/employee_benefit.py index 58f227e0..c2b6e3b7 100644 --- a/brewman/brewman/routers/employee_benefit.py +++ b/brewman/brewman/routers/employee_benefit.py @@ -61,8 +61,14 @@ def save_route( def save(data: schema_in.EmployeeBenefitIn, date_: date, user: UserToken, db: Session) -> Voucher: - account_types = db.execute( - select(distinct(AccountBase.type)).where(AccountBase.id.in_([dj.employee.id_ for dj in data.employee_benefits])) + account_types = ( + 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) 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: voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() - account_types = db.execute( - select(distinct(AccountBase.type)).where( - AccountBase.id.in_( - [dj.employee.id_ for dj in data.employee_benefits] + [vj.account_id for vj in voucher.journals] + account_types = ( + db.execute( + select(distinct(AccountBase.type)).where( + 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) if not allowed: diff --git a/brewman/brewman/routers/incentive.py b/brewman/brewman/routers/incentive.py index c9d57e80..b23ac43f 100644 --- a/brewman/brewman/routers/incentive.py +++ b/brewman/brewman/routers/incentive.py @@ -61,8 +61,12 @@ def save_route( def save(data: schema_in.IncentiveIn, user: UserToken, db: Session) -> Voucher: - account_types = db.execute( - select(distinct(AccountBase.type)).where(AccountBase.id.in_([dj.employee_id for dj in data.incentives])) + account_types = ( + 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) 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: voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() - account_types = db.execute( - select(distinct(AccountBase.type)).where( - AccountBase.id.in_([dj.employee_id for dj in data.incentives] + [vj.account_id for vj in voucher.journals]) + account_types = ( + db.execute( + 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) if not allowed: diff --git a/brewman/brewman/routers/issue.py b/brewman/brewman/routers/issue.py index cd6b20d7..0e9315b3 100644 --- a/brewman/brewman/routers/issue.py +++ b/brewman/brewman/routers/issue.py @@ -63,7 +63,9 @@ def save_route( 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])) - 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) if not allowed: 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]): 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])) - account_types = db.execute( - select(distinct(AccountBase.type)).where( - or_( - AccountBase.id.in_([vj.account_id for vj in voucher.journals]), - AccountBase.id.in_(product_accounts), + account_types = ( + db.execute( + select(distinct(AccountBase.type)).where( + or_( + 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) if not allowed: diff --git a/brewman/brewman/routers/journal.py b/brewman/brewman/routers/journal.py index d3cfe70a..45950fa7 100644 --- a/brewman/brewman/routers/journal.py +++ b/brewman/brewman/routers/journal.py @@ -54,8 +54,12 @@ def save_route( def save(data: schema_in.JournalIn, user: UserToken, db: Session) -> Voucher: - account_types = db.execute( - select(distinct(AccountBase.type)).where(AccountBase.id.in_([dj.account.id_ for dj in data.journals])) + account_types = ( + 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) 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: voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() - account_types = db.execute( - select(distinct(AccountBase.type)).where( - AccountBase.id.in_([dj.account.id_ for dj in data.journals] + [vj.account_id for vj in voucher.journals]) + account_types = ( + db.execute( + 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) if not allowed: diff --git a/brewman/brewman/routers/purchase.py b/brewman/brewman/routers/purchase.py index a8d9a432..3a921c04 100644 --- a/brewman/brewman/routers/purchase.py +++ b/brewman/brewman/routers/purchase.py @@ -62,13 +62,17 @@ def save_route( 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])) - account_types = db.execute( - select(distinct(AccountBase.type)).where( - or_( - AccountBase.id == data.vendor.id_, - AccountBase.id.in_(product_accounts), + account_types = ( + db.execute( + select(distinct(AccountBase.type)).where( + or_( + 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) 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: 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])) - account_types = db.execute( - select(distinct(AccountBase.type)).where( - or_( - AccountBase.id.in_([data.vendor.id_] + [vj.account_id for vj in voucher.journals]), - AccountBase.id.in_(product_accounts), + account_types = ( + db.execute( + select(distinct(AccountBase.type)).where( + or_( + 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) if not allowed: diff --git a/brewman/brewman/routers/purchase_return.py b/brewman/brewman/routers/purchase_return.py index 6f488e02..be8bed11 100644 --- a/brewman/brewman/routers/purchase_return.py +++ b/brewman/brewman/routers/purchase_return.py @@ -62,13 +62,17 @@ def save_route( 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])) - account_types = db.execute( - select(distinct(AccountBase.type)).where( - or_( - AccountBase.id == data.vendor.id_, - AccountBase.id.in_(product_accounts), + account_types = ( + db.execute( + select(distinct(AccountBase.type)).where( + or_( + 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) 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: 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])) - account_types = db.execute( - select(distinct(AccountBase.type)).where( - or_( - AccountBase.id.in_([data.vendor.id_] + [vj.account_id for vj in voucher.journals]), - AccountBase.id.in_(product_accounts), + account_types = ( + db.execute( + select(distinct(AccountBase.type)).where( + or_( + 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) if not allowed: diff --git a/brewman/brewman/routers/voucher.py b/brewman/brewman/routers/voucher.py index 00ef1914..716fc09e 100644 --- a/brewman/brewman/routers/voucher.py +++ b/brewman/brewman/routers/voucher.py @@ -41,8 +41,14 @@ def post_voucher( try: with SessionFuture() as db: voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() - account_types = db.execute( - select(distinct(AccountBase.type)).where(AccountBase.id.in_([vj.account_id for vj in voucher.journals])) + account_types = ( + 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) if not allowed: @@ -91,8 +97,12 @@ def delete_voucher( 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() check_delete_permissions(voucher, user) - account_types = db.execute( - select(distinct(AccountBase.type)).where(AccountBase.id.in_([vj.account_id for vj in voucher.journals])) + account_types = ( + 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) if not allowed: