Skip to content

Commit

Permalink
[CBS-24108] - Add parentID to tax item-details
Browse files Browse the repository at this point in the history
  • Loading branch information
singh-sukhvir committed Sep 5, 2024
1 parent 9cda610 commit 0e99ca5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,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);
String parentItemID = adjustmentItem == null ? taxableItem.getId().toString() : adjustmentItem.getId().toString();
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,7 +360,7 @@ 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();

Expand All @@ -375,18 +377,26 @@ 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 e : additionalDetails.entrySet()) {
if (e.getValue() instanceof String) {
existingItemsDetailsJson.put((String) e.getKey(), (String) e.getValue());
} else if (e.getValue() instanceof Double) {
existingItemsDetailsJson.put((String) e.getKey(), (Double) e.getValue());
} else {
logger.warn("Trying to add a value of unsupported type in tax item_details {}", additionalDetails.keySet());
}
}
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 0e99ca5

Please sign in to comment.