Skip to content

Commit

Permalink
Support consistency with existing KB invoice items where tierDetails …
Browse files Browse the repository at this point in the history
…is presented
  • Loading branch information
vlaskhilkevich committed Aug 10, 2024
1 parent f482962 commit 3e98e9b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<properties>
<check.spotbugs-exclude-filter-file>spotbugs-exclude.xml</check.spotbugs-exclude-filter-file>
<jooq.version>3.14.15</jooq.version>
<killbill.version>0.22.32</killbill.version>
<osgi.private>org.killbill.billing.plugin.vertex.*</osgi.private>
</properties>
<dependencies>
Expand Down Expand Up @@ -155,6 +156,11 @@
<artifactId>killbill-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.kill-bill.billing</groupId>
<artifactId>killbill-invoice</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.kill-bill.billing</groupId>
<artifactId>killbill-platform-osgi-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2020-2024 Equinix, Inc
* Copyright 2020-2024 The Billing Project, LLC
*
* The Billing Project licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package org.killbill.billing.plugin.vertex;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.killbill.billing.invoice.usage.details.UsageConsumableInArrearAggregate;
import org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate;

/**
* Wrapper to add taxRate details into itemDetails
*/
public class TaxItemDetails {

private final UsageConsumableInArrearAggregate usageDetail;
private final Double taxRate;

public TaxItemDetails(@Nonnull final Double taxRate, @Nullable final UsageConsumableInArrearAggregate usageDetail) {
this.usageDetail = usageDetail;
this.taxRate = taxRate;
}

public List<UsageConsumableInArrearTierUnitAggregate> getTierDetails() {
return usageDetail != null ? usageDetail.getTierDetails() : null;
}

public BigDecimal getAmount() {
return usageDetail != null ? usageDetail.getAmount() : null;
}

public Double getTaxRate() {
return taxRate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.killbill.billing.account.api.Account;
import org.killbill.billing.invoice.api.Invoice;
import org.killbill.billing.invoice.api.InvoiceItem;
import org.killbill.billing.invoice.usage.details.UsageConsumableInArrearAggregate;
import org.killbill.billing.osgi.libs.killbill.OSGIKillbillAPI;
import org.killbill.billing.payment.api.PluginProperty;
import org.killbill.billing.plugin.api.PluginProperties;
Expand Down Expand Up @@ -68,13 +69,14 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;

public class VertexTaxCalculator extends PluginTaxCalculator {
Expand Down Expand Up @@ -116,7 +118,7 @@ public VertexTaxCalculator(final VertexApiConfigurationHandler vertexApiConfigur
this.vertexApiConfigurationHandler = vertexApiConfigurationHandler;
this.clock = clock;
this.dao = dao;
this.objectMapper = new ObjectMapper();
this.objectMapper = new ObjectMapper().setSerializationInclusion(Include.NON_NULL);
}

public List<InvoiceItem> compute(final Account account,
Expand Down Expand Up @@ -325,7 +327,10 @@ private InvoiceItem createTaxInvoiceItem(final InvoiceItem taxableItem, final UU
taxRate = calculatedTax.divide(taxableItem.getAmount(), 5, RoundingMode.FLOOR).doubleValue();
}

final String taxItemDetails = createTaxItemDetails(ImmutableMap.of("taxRate", taxRate));
final UsageConsumableInArrearAggregate usageDetail = getUsageDetailFromItemDetails(taxItem);

final String taxItemDetails = createTaxItemDetails(new TaxItemDetails(taxRate, usageDetail));

if (taxItemDetails == null) {
return taxItem;
}
Expand Down Expand Up @@ -361,7 +366,22 @@ private InvoiceItem createTaxInvoiceItem(final InvoiceItem taxableItem, final UU
.validate().build());
}

private String createTaxItemDetails(@Nonnull final Map<String, Object> taxItemDetails) {
private UsageConsumableInArrearAggregate getUsageDetailFromItemDetails(final InvoiceItem taxItem) {
if (taxItem.getItemDetails() == null) {
return null;
}

UsageConsumableInArrearAggregate usageDetail = null;
try {
usageDetail = objectMapper.readValue(taxItem.getItemDetails(), new TypeReference<UsageConsumableInArrearAggregate>() {});
} catch (JsonProcessingException e) {
logger.error("Failed to parse the item details for tax item with ID: {}", taxItem.getId(), e);
}

return usageDetail;
}

private String createTaxItemDetails(@Nonnull final TaxItemDetails taxItemDetails) {
try {
return objectMapper.writeValueAsString(taxItemDetails);
} catch (JsonProcessingException exception) {
Expand Down

0 comments on commit 3e98e9b

Please sign in to comment.