Skip to content

Commit

Permalink
Merge pull request #46 from killbill/cbs-24108-add-parent
Browse files Browse the repository at this point in the history
[CBS-24108] - Add parentID to tax item-details
  • Loading branch information
singh-sukhvir authored Sep 12, 2024
2 parents 9cda610 + 5895453 commit a7462a0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public class VertexTaxCalculator extends PluginTaxCalculator {
public static final String KB_TRANSACTION_PREFIX = "kb_";

private static final Logger logger = LoggerFactory.getLogger(VertexTaxCalculator.class);
private static final ObjectMapper objectMapper = new ObjectMapper();

private final VertexApiConfigurationHandler vertexApiConfigurationHandler;
private final VertexDao dao;
Expand Down Expand Up @@ -325,7 +326,9 @@ private InvoiceItem createTaxInvoiceItem(final InvoiceItem taxableItem, final UU
taxRate = calculatedTax.divide(taxableItem.getAmount(), 5, RoundingMode.FLOOR).doubleValue();
}

final String taxItemDetails = addTaxRateToItemDetails(taxItem.getItemDetails(), taxRate);
final String parentItemID = adjustmentItem == null ? taxableItem.getId().toString() : adjustmentItem.getId().toString();
final Map<String, Object> additionalDetails = ImmutableMap.of("taxRate", taxRate, "parentItemId", parentItemID);
final String taxItemDetails = appendItemDetails(taxItem.getItemDetails(), additionalDetails);

return new PluginInvoiceItem(new Builder<>()
.withId(taxItem.getId())
Expand Down Expand Up @@ -358,9 +361,8 @@ private InvoiceItem createTaxInvoiceItem(final InvoiceItem taxableItem, final UU
.validate().build());
}

private String addTaxRateToItemDetails(@Nullable final String itemDetails, @Nonnull final Double taxRate) {
private String appendItemDetails(@Nullable final String itemDetails, @Nonnull final Map<String, Object> additionalDetails) {
ObjectNode existingItemsDetailsJson = null;
final ObjectMapper objectMapper = new ObjectMapper();

if (itemDetails != null && !itemDetails.isEmpty()) {
try {
Expand All @@ -375,18 +377,20 @@ private String addTaxRateToItemDetails(@Nullable final String itemDetails, @Nonn
}
}

final Object itemDetailsWithTaxRate;
final Object itemDetailsWithAdditionalInfo;
if (existingItemsDetailsJson != null) {
existingItemsDetailsJson.put("taxRate", taxRate);
itemDetailsWithTaxRate = existingItemsDetailsJson;
for(Map.Entry<String, Object> detail : additionalDetails.entrySet()) {
existingItemsDetailsJson.putPOJO(detail.getKey(), detail.getValue());
}
itemDetailsWithAdditionalInfo = existingItemsDetailsJson;
} else {
itemDetailsWithTaxRate = ImmutableMap.of("taxRate", taxRate);
itemDetailsWithAdditionalInfo = additionalDetails;
}

try {
return objectMapper.writeValueAsString(itemDetailsWithTaxRate);
return objectMapper.writeValueAsString(itemDetailsWithAdditionalInfo);
} catch (JsonProcessingException exception) {
logger.error("Couldn't serialize the tax item details {} with tax rate: {}", itemDetailsWithTaxRate, taxRate, exception);
logger.error("Couldn't serialize the tax item_details {} with additional info: {}", itemDetailsWithAdditionalInfo, additionalDetails, exception);
return itemDetails;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public void testTaxEffectiveRate() throws Exception {

//then it persisted in item details invoice item field
List<InvoiceItem> result = vertexTaxCalculator.compute(account, invoice, true, Collections.emptyList(), tenantContext);
assertEquals("{\"taxRate\":0.09975}", result.get(0).getItemDetails());
assertEquals(String.format("{\"taxRate\":0.09975,\"parentItemId\":\"%s\"}", TAX_ITEM_ID), result.get(0).getItemDetails());
checkTaxItemFields(result.get(0));
}

Expand All @@ -265,10 +265,10 @@ public void testTaxItemDetailsWhenUsageDetailsExists() throws Exception {
"\"tierBlockSize\":1,\"quantity\":1,\"amount\":0.500000000}],\"amount\":0.500000000}";
given(taxableInvoiceItem.getItemDetails()).willReturn(itemDetailsWithTierInfo);

final String expectedItemDetailsWithTaxRate =
final String expectedItemDetailsWithTaxRate = String.format(
"{\"tierDetails\":[{\"tier\":1,\"tierUnit\":\"hour\",\"tierPrice\":0.500000000," +
"\"tierBlockSize\":1,\"quantity\":1,\"amount\":0.500000000}],\"amount\":0.500000000," +
"\"taxRate\":" + taxRate + "}";
"\"taxRate\":%f,\"parentItemId\":\"%s\"}",taxRate, TAX_ITEM_ID.toString());

//when
List<InvoiceItem> result = vertexTaxCalculator.compute(account, invoice, true, Collections.emptyList(), tenantContext);
Expand Down Expand Up @@ -313,7 +313,7 @@ public void testComputeWhenTaxEffectiveRateIsNotPresentedByVertex() throws Excep

//then
assertEquals(1, result.size());
assertEquals("{\"taxRate\":" + expectedTaxRate + "}", result.get(0).getItemDetails());
assertEquals(String.format("{\"taxRate\":0.09975,\"parentItemId\":\"%s\"}", TAX_ITEM_ID), result.get(0).getItemDetails());
checkTaxItemFields(result.get(0));
}

Expand All @@ -327,7 +327,7 @@ public void testComputeWhenEffectiveTaxRateZero() throws Exception {

//then
assertEquals(1, result.size());
assertEquals("{\"taxRate\":0.0}", result.get(0).getItemDetails());
assertEquals(String.format("{\"taxRate\":0.0,\"parentItemId\":\"%s\"}", TAX_ITEM_ID), result.get(0).getItemDetails());
checkTaxItemFields(result.get(0));
}

Expand Down

0 comments on commit a7462a0

Please sign in to comment.