Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import org.killbill.billing.osgi.libs.killbill.OSGIKillbillAPI;
import org.killbill.billing.payment.api.PluginProperty;
import org.killbill.billing.plugin.api.PluginProperties;
import org.killbill.billing.plugin.api.invoice.PluginInvoiceItem;
import org.killbill.billing.plugin.api.invoice.PluginInvoiceItem.Builder;
import org.killbill.billing.plugin.api.invoice.PluginTaxCalculator;
import org.killbill.billing.plugin.vertex.dao.VertexDao;
import org.killbill.billing.plugin.vertex.gen.ApiException;
Expand Down Expand Up @@ -298,7 +300,38 @@ private Collection<InvoiceItem> toInvoiceItems(final UUID invoiceId,
final BigDecimal calculatedTax = transactionLineDetailModel.getCalculatedTax() != null ? BigDecimal.valueOf(transactionLineDetailModel.getCalculatedTax()) : null;
final InvoiceItem taxItem = buildTaxItem(taxableItem, invoiceId, adjustmentItem, calculatedTax, description);
if (taxItem != null) {
invoiceItems.add(taxItem);
final InvoiceItem taxItemWithEffectiveRate = transactionLineDetailModel.getEffectiveRate() != null
? new PluginInvoiceItem(new Builder<>()
.withId(taxItem.getId())
.withInvoiceItemType(taxItem.getInvoiceItemType())
.withInvoiceId(taxItem.getInvoiceId())
.withAccountId(taxItem.getAccountId())
.withChildAccountId(taxItem.getChildAccountId())
.withStartDate(taxItem.getStartDate())
.withEndDate(taxItem.getEndDate())
.withAmount(taxItem.getAmount())
.withCurrency(taxItem.getCurrency())
.withDescription(taxItem.getDescription())
.withSubscriptionId(taxItem.getSubscriptionId())
.withBundleId(taxItem.getBundleId())
.withCatalogEffectiveDate(taxItem.getCatalogEffectiveDate())
.withProductName(taxItem.getProductName())
.withPrettyProductName(taxItem.getPrettyProductName())
.withPlanName(taxItem.getPlanName())
.withPrettyPlanName(taxItem.getPrettyPlanName())
.withPhaseName(taxItem.getPhaseName())
.withPrettyPhaseName(taxItem.getPrettyPhaseName())
.withRate(taxItem.getRate())
.withLinkedItemId(taxItem.getLinkedItemId())
.withUsageName(taxItem.getUsageName())
.withPrettyUsageName(taxItem.getPrettyUsageName())
.withQuantity(taxItem.getQuantity())
.withItemDetails(String.valueOf(transactionLineDetailModel.getEffectiveRate()))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we discussed to save it as json considering we may want to add additional details in future.
Current format won't support that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vlaskhilkevich This PR was experimental. Feel free to create a separate PR with your changes.

.withCreatedDate(taxItem.getCreatedDate())
.withUpdatedDate(taxItem.getUpdatedDate())
.validate().build())
Comment on lines +303 to +332
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we move this to a separate function. Something like below -

final String taxRate = String.valueOf(transactionLineDetailModel.getEffectiveRate())
final InvoiceItem taxItem = createTaxInvoiceItem(taxableItem, invoiceId, adjustmentItem, calculatedTax, description, taxRate)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having value "null" as a result of valueOf(null) is not what we want in this case

: taxItem;
invoiceItems.add(taxItemWithEffectiveRate);
}
}
return invoiceItems;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
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 @@ -157,7 +158,6 @@ public void testCompute() throws Exception {
//given
given(taxResponse.getData()).willReturn(apiResponseData);
given(invoice.getInvoiceItems()).willReturn(Collections.singletonList(taxableInvoiceItem));

final boolean isDryRun = false;

//when
Expand Down Expand Up @@ -231,6 +231,40 @@ public void testTaxDescription() throws Exception {
assertEquals("CA STATE TAX", result.get(0).getDescription());
}

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

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

//then it persisted in item details invoice item field
List<InvoiceItem> result = vertexTaxCalculator.compute(account, invoice, true, Collections.emptyList(), tenantContext);
assertEquals("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));
}

private void checkTaxItemFields(final InvoiceItem taxItem) {
assertEquals(InvoiceItemType.TAX, taxItem.getInvoiceItemType());
assertEquals("Tax", taxItem.getDescription());
assertEquals(INVOICE_ID, taxItem.getInvoiceId());
assertEquals(INVOICE_DATE, taxItem.getStartDate());
assertEquals(INVOICE_DATE.plusMonths(1), taxItem.getEndDate());
}

@Test(groups = "fast", expectedExceptions = {IllegalStateException.class})
public void testComputeWithAnomalousAdjustmentsException() throws Exception {
//given
Expand Down