diff --git a/barker/barker/routers/reports/tax_report.py b/barker/barker/routers/reports/tax_report.py index 5c1453e..d34a883 100644 --- a/barker/barker/routers/reports/tax_report.py +++ b/barker/barker/routers/reports/tax_report.py @@ -1,4 +1,7 @@ +import re + from datetime import date, datetime, time, timedelta +from decimal import Decimal from typing import List from fastapi import APIRouter, Depends, Security @@ -62,4 +65,21 @@ def get_tax(s: date, f: date, db: Session) -> List[TaxReportItem]: .group_by(Tax.name, Inventory.tax_rate) .order_by(Tax.name, Inventory.tax_rate) ).all() - return [TaxReportItem(name=f"{i[0]} - {i[1]:.2%}", taxRate=i[1], saleAmount=i[2], amount=i[3]) for i in amounts] + return sum([get_tax_item(*i) for i in amounts], []) + + +def get_tax_item(name: str, rate: Decimal, net: Decimal, amount: Decimal) -> List[TaxReportItem]: + items = name.split(";") + if len(items) == 1: + return [TaxReportItem(name=name, taxRate=rate, saleAmount=net, amount=amount)] + else: + r = [] + for i, item in enumerate(it.strip() for it in items): + match = re.match(r"(^.*)\s+\((.*?)/(.*?)\)[^(]*$", item) + if not match or len(match.groups()) != 3: + raise Exception("Error in tax as it has multiple items, but the format is wrong.") + ratio = Decimal(match.group(2)) / Decimal(match.group(3)) + sub_net = round(net * ratio, 2) + sub_amount = round(amount * ratio, 2) + r.append(TaxReportItem(name=f"{match.group(1)}", taxRate=rate, saleAmount=sub_net, amount=sub_amount)) + return r