From f2df28ca9cddff7dc3d89ceee220f0d2d5242762 Mon Sep 17 00:00:00 2001 From: tanshu Date: Mon, 25 Jan 2021 12:48:36 +0530 Subject: [PATCH] Fix: Incentive takes into account the date chosen Fix: Employee query was querying AccountBase and errored out as it did not have the designation column Fix: Incentive update set_date was wrong Fix: Incentive amount takes into account the current date but not any incentive voucher for the date --- brewman/brewman/routers/employee.py | 3 +-- brewman/brewman/routers/incentive.py | 18 +++++++++++++----- brewman/brewman/routers/login.py | 2 +- brewman/brewman/routers/voucher.py | 16 ++++++++++++---- brewman/brewman/schemas/net_transactions.py | 2 +- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/brewman/brewman/routers/employee.py b/brewman/brewman/routers/employee.py index 6b726245..5dd90037 100644 --- a/brewman/brewman/routers/employee.py +++ b/brewman/brewman/routers/employee.py @@ -13,7 +13,6 @@ from sqlalchemy.orm import Session, joinedload_all from ..core.security import get_current_active_user as get_user from ..db.session import SessionLocal from ..models.account import Account -from ..models.account_base import AccountBase from ..models.cost_centre import CostCentre from ..models.employee import Employee from ..models.journal import Journal @@ -150,7 +149,7 @@ async def show_term( current_user: UserToken = Depends(get_user), ): list_ = [] - for index, item in enumerate(AccountBase.query(q=q, type_=10, db=db)): + for index, item in enumerate(Employee.query(q=q, type_=10, db=db)): list_.append( { "id": item.id, diff --git a/brewman/brewman/routers/incentive.py b/brewman/brewman/routers/incentive.py index 475a51b0..a8bff6ce 100644 --- a/brewman/brewman/routers/incentive.py +++ b/brewman/brewman/routers/incentive.py @@ -8,7 +8,7 @@ import brewman.schemas.input as schema_in import brewman.schemas.voucher as output from fastapi import APIRouter, Depends, HTTPException, Request, Security, status -from sqlalchemy import func, or_ +from sqlalchemy import and_, func, or_ from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.orm import Session @@ -133,7 +133,7 @@ def update_route( update_incentives(item, employees, point_value, db) check_journals_are_valid(item) db.commit() - set_date(request.session, data.date_) + set_date(data.date_.strftime("%d-%b-%Y"), request.session) return voucher_info(item, db) except SQLAlchemyError as e: db.rollback() @@ -254,9 +254,17 @@ def balance(date_: date, voucher_id: Optional[uuid.UUID], db: Session): amount = ( db.query(func.sum(Journal.amount * Journal.debit)) .join(Journal.voucher) - .filter(Voucher.date <= date_) - .filter(Voucher.type != VoucherType.by_name("Issue").id) - .filter(Journal.account_id == Account.incentive_id()) + .filter( + Journal.account_id == Account.incentive_id(), + Voucher.type != VoucherType.by_name("Issue").id, + or_( + Voucher.date <= date_, + and_( + Voucher.date == date_, + Voucher.type != VoucherType.by_name("Incentive").id, + ), + ), + ) ) if voucher_id is not None: amount = amount.filter(Voucher.id != voucher_id) diff --git a/brewman/brewman/routers/login.py b/brewman/brewman/routers/login.py index 1b2f354b..1bf1fbf7 100644 --- a/brewman/brewman/routers/login.py +++ b/brewman/brewman/routers/login.py @@ -98,7 +98,7 @@ async def login_for_access_token( data={ "sub": user.name, "scopes": ["authenticated"] - + list(set([p.name.replace(" ", "-").lower() for r in user.roles for p in r.permissions])), + + list(set([p.name.replace(" ", "-").lower() for r in user.roles for p in r.permissions])), # noqa: W503 "userId": str(user.id), "lockedOut": user.locked_out, "ver": __version__.__version__, diff --git a/brewman/brewman/routers/voucher.py b/brewman/brewman/routers/voucher.py index ffecf39c..c23ffe88 100644 --- a/brewman/brewman/routers/voucher.py +++ b/brewman/brewman/routers/voucher.py @@ -7,7 +7,7 @@ from typing import Optional import brewman.schemas.voucher as output from fastapi import APIRouter, Depends, HTTPException, Security, status -from sqlalchemy import func, or_ +from sqlalchemy import and_, func, or_ from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.orm import Session @@ -377,9 +377,17 @@ def incentive_employees(date_, db: Session): amount = ( db.query(func.sum(Journal.amount * Journal.debit)) .join(Journal.voucher) - .filter(Voucher.date < finish_date) - .filter(Voucher.type != VoucherType.by_name("Issue").id) - .filter(Journal.account_id == Account.incentive_id()) + .filter( + Journal.account_id == Account.incentive_id(), + Voucher.type != VoucherType.by_name("Issue").id, + or_( + Voucher.date <= finish_date, + and_( + Voucher.date == finish_date, + Voucher.type != VoucherType.by_name("Incentive").id, + ), + ), + ) .scalar() ) amount = 0 if amount is None else amount * Decimal(0.9) * -1 diff --git a/brewman/brewman/schemas/net_transactions.py b/brewman/brewman/schemas/net_transactions.py index 7593479f..20523594 100644 --- a/brewman/brewman/schemas/net_transactions.py +++ b/brewman/brewman/schemas/net_transactions.py @@ -3,7 +3,7 @@ from decimal import Decimal from typing import List, Optional from brewman.schemas import to_camel -from pydantic import Field, validator +from pydantic import validator from pydantic.main import BaseModel