Skip to content

Commit

Permalink
Make taxRate part of item details for all tax items added to the invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
vlaskhilkevich committed Jul 25, 2024
1 parent cd9bdb3 commit 2c49cc4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.killbill.billing.plugin.vertex;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -315,8 +316,12 @@ private Collection<InvoiceItem> toInvoiceItems(final UUID invoiceId,

private InvoiceItem createTaxInvoiceItem(final InvoiceItem taxableItem, final UUID invoiceId, @Nullable final InvoiceItem adjustmentItem, final BigDecimal calculatedTax, @Nullable final String description, @Nullable Double taxRate) {
final InvoiceItem taxItem = buildTaxItem(taxableItem, invoiceId, adjustmentItem, calculatedTax, description);
if (taxItem == null || taxRate == null) {
return taxItem;
if (taxItem == null) {
return null;
}

if (taxRate == null) {
taxRate = calculatedTax.divide(taxableItem.getAmount(), 5, RoundingMode.HALF_UP).doubleValue();
}

final String taxItemDetails = createTaxItemDetails(ImmutableMap.of("taxRate", taxRate));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNull;

public class VertexTaxCalculatorTest {

Expand Down Expand Up @@ -247,52 +246,30 @@ public void testTaxEffectiveRate() throws Exception {
List<InvoiceItem> result = vertexTaxCalculator.compute(account, invoice, true, Collections.emptyList(), tenantContext);
assertEquals("{\"taxRate\":0.09975}", result.get(0).getItemDetails());
checkTaxItemFields(result.get(0));

//given effective rate is not present
taxesType.setEffectiveRate(null);

//then no item details persisted
result = vertexTaxCalculator.compute(account, invoice, true, Collections.emptyList(), tenantContext);
assertNull(result.get(0).getItemDetails());
checkTaxItemFields(result.get(0));
}

@Test(groups = "fast")
public void testComputeWhenTaxEffectiveRateIsNotPresented() throws Exception {
public void testComputeWhenTaxEffectiveRateIsNotPresentedByVertex() throws Exception {
//given
given(invoice.getInvoiceItems()).willReturn(Collections.singletonList(taxableInvoiceItem));
final BigDecimal taxableAmount = BigDecimal.valueOf(0.97d);
given(taxableInvoiceItem.getAmount()).willReturn(taxableAmount);
given(taxResponse.getData()).willReturn(apiResponseData);

final BigDecimal expectedTaxRate = BigDecimal.valueOf(0.09975);
final TaxesType taxesType = new TaxesType();
taxesType.setCalculatedTax(MOCK_TAX_AMOUNT_1_01);
taxesType.setCalculatedTax(taxableAmount.multiply(expectedTaxRate).doubleValue());
given(responseLineItem.getTaxes()).willReturn(Collections.singletonList(taxesType));

//given tax effective rate is not present
taxesType.setEffectiveRate(null);

//then it persisted in item details invoice item field
//tax rate is calculated and provided in item details in the result
List<InvoiceItem> result = vertexTaxCalculator.compute(account, invoice, true, Collections.emptyList(), tenantContext);
assertEquals(1, result.size());
assertNull(result.get(0).getItemDetails());
assertEquals("{\"taxRate\":" + expectedTaxRate + "}", result.get(0).getItemDetails());
checkTaxItemFields(result.get(0));
}

@Test(groups = "fast")
public void testComputeWhenEffectiveTaxRateZero() throws Exception {
//given
given(invoice.getInvoiceItems()).willReturn(Collections.singletonList(taxableInvoiceItem));
given(taxResponse.getData()).willReturn(apiResponseData);
final TaxesType taxesType = new TaxesType();
taxesType.setCalculatedTax(0d);
given(responseLineItem.getTaxes()).willReturn(Collections.singletonList(taxesType));

//given tax effective rate is present
taxesType.setEffectiveRate(0d);

//then it persisted in item details invoice item field
List<InvoiceItem> result = vertexTaxCalculator.compute(account, invoice, true, Collections.emptyList(), tenantContext);
assertEquals(0, result.size());
}

@Test(groups = "fast")
public void testComputeWhenEffectiveTaxRateZeroButAmountIsNot() throws Exception {
//given
Expand All @@ -302,10 +279,9 @@ public void testComputeWhenEffectiveTaxRateZeroButAmountIsNot() throws Exception
taxesType.setCalculatedTax(MOCK_TAX_AMOUNT_1_01);
given(responseLineItem.getTaxes()).willReturn(Collections.singletonList(taxesType));

//given tax effective rate is present
taxesType.setEffectiveRate(0d);

//then it persisted in item details invoice item field
//tax item with tax rate in item details is added to the result
List<InvoiceItem> result = vertexTaxCalculator.compute(account, invoice, true, Collections.emptyList(), tenantContext);
assertEquals(1, result.size());
assertEquals("{\"taxRate\":0.0}", result.get(0).getItemDetails());
Expand Down

0 comments on commit 2c49cc4

Please sign in to comment.