From 1b6e753f3c1a59c849437dade1724dc22734a2b8 Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 12 Dec 2024 09:50:02 +0100 Subject: [PATCH] Implemented calculating TotalAmounts from invoice items --- .../invoicing/calculator/AmountsCalculator.kt | 11 +++++- .../calculator/AmountsCalculatorTest.kt | 38 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 e-invoice-domain/src/test/kotlin/net/codinux/invoicing/calculator/AmountsCalculatorTest.kt diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/calculator/AmountsCalculator.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/calculator/AmountsCalculator.kt index fe67927..e5d5114 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/calculator/AmountsCalculator.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/calculator/AmountsCalculator.kt @@ -1,16 +1,23 @@ package net.codinux.invoicing.calculator import net.codinux.invoicing.mapper.MustangMapper -import net.codinux.invoicing.model.Invoice -import net.codinux.invoicing.model.TotalAmounts +import net.codinux.invoicing.model.* import org.mustangproject.ZUGFeRD.IExportableTransaction import org.mustangproject.ZUGFeRD.TransactionCalculator import java.math.BigDecimal +import java.time.LocalDate open class AmountsCalculator { protected open val mapper by lazy { MustangMapper() } // lazy to avoid circular dependency creation with MustangMapper + private val invoiceDetails by lazy { InvoiceDetails("", LocalDate.now()) } + + private val party by lazy { Party("", "", null, null, "") } + + + open fun calculateTotalAmounts(items: List) = + calculateTotalAmounts(Invoice(invoiceDetails, party, party, items)) open fun calculateTotalAmounts(invoice: Invoice) = calculateTotalAmounts(mapper.mapToTransaction(invoice)) diff --git a/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/calculator/AmountsCalculatorTest.kt b/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/calculator/AmountsCalculatorTest.kt new file mode 100644 index 0000000..e2a41e6 --- /dev/null +++ b/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/calculator/AmountsCalculatorTest.kt @@ -0,0 +1,38 @@ +package net.codinux.invoicing.calculator + +import assertk.assertThat +import assertk.assertions.isEqualByComparingTo +import assertk.assertions.isEqualTo +import net.codinux.invoicing.model.InvoiceItem +import java.math.BigDecimal +import java.math.RoundingMode +import kotlin.test.Test + +class AmountsCalculatorTest { + + private val underTest = AmountsCalculator() + + + @Test + fun calculateTotalAmounts() { + val items = listOf( + InvoiceItem("", BigDecimal(7), "", BigDecimal(5), BigDecimal(19)), + InvoiceItem("", BigDecimal(20), "", BigDecimal(5), BigDecimal(7)), + ) + + val result = underTest.calculateTotalAmounts(items) + + val expectedNetAmount = BigDecimal(7 * 5 + 20 * 5).setScale(2) + val expectedVatAmount = BigDecimal(7 * 5 * 0.19 + 20 * 5 * 0.07).setScale(2, RoundingMode.DOWN) + val expectedTotalAmount = expectedNetAmount + expectedVatAmount + + assertThat(result.lineTotalAmount).isEqualByComparingTo(expectedNetAmount) + assertThat(result.taxBasisTotalAmount).isEqualByComparingTo(expectedNetAmount) + + assertThat(result.taxTotalAmount).isEqualTo(expectedVatAmount) + + assertThat(result.grandTotalAmount).isEqualTo(expectedTotalAmount) + assertThat(result.duePayableAmount).isEqualTo(expectedTotalAmount) + } + +} \ No newline at end of file