71 lines
2.1 KiB
Python
71 lines
2.1 KiB
Python
from datetime import datetime, timedelta, date
|
|
|
|
from fastapi import APIRouter, Depends, Security
|
|
from sqlalchemy import func
|
|
from sqlalchemy.orm import Session
|
|
|
|
from barker.core.config import settings
|
|
from ...models import Voucher, VoucherType, Inventory, Kot, Tax
|
|
from ...schemas.auth import UserToken
|
|
from ...core.security import get_current_active_user as get_user
|
|
from ...db.session import SessionLocal
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
# Dependency
|
|
def get_db() -> Session:
|
|
try:
|
|
db = SessionLocal()
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@router.get("")
|
|
def get_tax_report(
|
|
s: str = None,
|
|
f: str = None,
|
|
db: Session = Depends(get_db),
|
|
user: UserToken = Security(get_user, scopes=["tax-report"]),
|
|
):
|
|
start_date = date.today() if s is None else datetime.strptime(s, "%d-%b-%Y").date()
|
|
finish_date = date.today() if f is None else datetime.strptime(f, "%d-%b-%Y").date()
|
|
if (date.today() - start_date).days > 5 and "accounts-audit" not in user.permissions:
|
|
raise ValueError("Accounts Audit")
|
|
|
|
return {
|
|
"startDate": start_date.strftime("%d-%b-%Y"),
|
|
"finishDate": finish_date.strftime("%d-%b-%Y"),
|
|
"amounts": get_tax(start_date, finish_date, db),
|
|
}
|
|
|
|
|
|
def get_tax(s: date, f: date, db: Session):
|
|
start_date = s + timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)
|
|
finish_date = f + timedelta(days=1, minutes=settings.NEW_DAY_OFFSET_MINUTES)
|
|
|
|
amounts = (
|
|
db.query(
|
|
Tax.name,
|
|
Inventory.tax_rate,
|
|
func.coalesce(func.sum(Inventory.net), 0),
|
|
func.coalesce(func.sum(Inventory.tax_amount), 0),
|
|
)
|
|
.join(Voucher.kots)
|
|
.join(Kot.inventories)
|
|
.join(Inventory.tax)
|
|
.filter(
|
|
Voucher.date >= start_date,
|
|
Voucher.date <= finish_date,
|
|
Voucher.voucher_type == VoucherType.REGULAR_BILL.value,
|
|
)
|
|
.group_by(Tax.name, Inventory.tax_rate)
|
|
.order_by(Tax.name, Inventory.tax_rate)
|
|
.all()
|
|
)
|
|
return [
|
|
{"name": "{0} - {1:.2%}".format(i[0], i[1]), "taxRate": i[1], "saleAmount": i[2], "amount": i[3],}
|
|
for i in amounts
|
|
]
|