Nothing much

This commit is contained in:
Amritanshu Agrawal 2021-11-11 21:17:02 +05:30
parent 65d75bcaad
commit 1159db5007
4 changed files with 34 additions and 17 deletions

View File

@ -1,4 +1,4 @@
[flake8]
ignore = E203
ignore = E203, W503
max-line-length = 120
exclude = .git,__pycache__,__init__.py,.mypy_cache,.pytest_cache

View File

@ -171,7 +171,7 @@ def balance(id_: uuid.UUID, date, db: Session) -> Decimal:
bal = bal.where(Voucher.date <= date)
bal = bal.where(Voucher.voucher_type != VoucherType.ISSUE).where(Journal.account_id == id_)
result: Decimal = db.execute(bal).scalar()
result: Decimal = db.execute(bal).scalar_one_or_none()
return Decimal(0) if result is None else result

View File

@ -1,4 +1,5 @@
from datetime import datetime
from datetime import date, datetime
from typing import List
from fastapi import APIRouter, Security
from sqlalchemy import select
@ -8,29 +9,30 @@ from ..core.security import get_current_active_user as get_user
from ..db.session import SessionFuture
from ..models.voucher import Voucher
from ..models.voucher_type import VoucherType
from ..schemas.issue_grid_item import IssueGridItem
from ..schemas.user import UserToken
router = APIRouter()
@router.get("/{date}")
@router.get("/{date_}", response_model=List[IssueGridItem])
def grid_date(
date: str,
date_: str,
user: UserToken = Security(get_user, scopes=["issue"]),
):
date = datetime.strptime(date, "%d-%b-%Y")
) -> List[IssueGridItem]:
date_obj = datetime.strptime(date_, "%d-%b-%Y")
with SessionFuture() as db:
return get_grid(date, db)
return get_grid(date_obj, db)
def get_grid(date, db: Session):
list_ = []
def get_grid(date_: date, db: Session) -> List[IssueGridItem]:
list_: List[IssueGridItem] = []
query = (
db.execute(
select(Voucher)
.join(Voucher.journals)
.where(Voucher.date == date, Voucher.voucher_type == VoucherType.ISSUE)
.where(Voucher.date == date_, Voucher.voucher_type == VoucherType.ISSUE)
.order_by(Voucher.creation_date)
)
.unique()
@ -41,11 +43,11 @@ def get_grid(date, db: Session):
for voucher in query:
amount = voucher.journals[0].amount
list_.append(
{
"id": str(voucher.id),
"amount": amount,
"source": [j.cost_centre.name for j in voucher.journals if j.debit == -1],
"destination": [j.cost_centre.name for j in voucher.journals if j.debit == 1],
}
IssueGridItem(
id=str(voucher.id),
amount=amount,
source=next(j.cost_centre.name for j in voucher.journals if j.debit == -1),
destination=next(j.cost_centre.name for j in voucher.journals if j.debit == 1),
)
)
return list_

View File

@ -0,0 +1,15 @@
import uuid
from decimal import Decimal
from pydantic import BaseModel
class IssueGridItem(BaseModel):
id_: uuid.UUID
amount: Decimal
source: str
destination: str
class Config:
fields = {"id_": "id"}