25 lines
773 B
Python
25 lines
773 B
Python
from __future__ import annotations
|
|
from dataclasses import dataclass
|
|
from typing import Dict
|
|
from .models import Invoice
|
|
|
|
@dataclass(frozen=True)
|
|
class Totals:
|
|
net_total: float
|
|
tax_total: float
|
|
gross_total: float
|
|
tax_by_rate: Dict[float, float]
|
|
|
|
def compute_totals(inv: Invoice) -> Totals:
|
|
net = 0.0
|
|
tax_total = 0.0
|
|
tax_by_rate: Dict[float, float] = {}
|
|
for it in inv.items:
|
|
line_net = float(it.quantity) * float(it.unit_price_net)
|
|
net += line_net
|
|
tax = line_net * float(it.tax_rate) / 100.0
|
|
tax_total += tax
|
|
tax_by_rate[it.tax_rate] = tax_by_rate.get(it.tax_rate, 0.0) + tax
|
|
gross = net + tax_total
|
|
return Totals(net_total=net, tax_total=tax_total, gross_total=gross, tax_by_rate=tax_by_rate)
|