Implemented calculating TotalAmounts from invoice items
This commit is contained in:
parent
abf63e4492
commit
1b6e753f3c
|
@ -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<InvoiceItem>) =
|
||||
calculateTotalAmounts(Invoice(invoiceDetails, party, party, items))
|
||||
|
||||
open fun calculateTotalAmounts(invoice: Invoice) =
|
||||
calculateTotalAmounts(mapper.mapToTransaction(invoice))
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue