Skip to content

Commit

Permalink
Issue#839 - refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
wwjbatista committed Feb 1, 2018
1 parent 3280ece commit 7b63798
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 86 deletions.
Expand Up @@ -180,10 +180,11 @@ public UsageDetailMode getItemResultBehaviorMode(final InternalTenantContext ten
return UsageDetailMode.valueOf(result);
}

if (mode.compareTo(UsageDetailMode.AGGREGATE) != 0 || mode.compareTo(UsageDetailMode.DETAIL) != 0) {
return UsageDetailMode.AGGREGATE;
if (mode == UsageDetailMode.AGGREGATE || mode == UsageDetailMode.DETAIL) {
return mode;
}
return mode;

return UsageDetailMode.AGGREGATE;
}

@Override
Expand Down
Expand Up @@ -72,21 +72,21 @@ public InvoiceItemBase(final UUID id, @Nullable final DateTime createdDate, fina

// For parent invoices
public InvoiceItemBase(final UUID id, @Nullable final DateTime createdDate, final UUID invoiceId, final UUID accountId, final UUID childAccountId,
final BigDecimal amount, final Currency currency, final String description) {
final BigDecimal amount, final Currency currency, final String description) {
this(id, createdDate, invoiceId, accountId, childAccountId, null, null, description, null, null, amount, null, currency, null, null, null);
}

public InvoiceItemBase(final UUID id, @Nullable final DateTime createdDate, final UUID invoiceId, final UUID accountId, @Nullable final UUID childAccountId, @Nullable final UUID bundleId,
@Nullable final UUID subscriptionId, @Nullable final String description,
@Nullable final LocalDate startDate, final LocalDate endDate, final BigDecimal amount, final BigDecimal rate, final Currency currency,
final UUID reversedItemId){
@Nullable final UUID subscriptionId, @Nullable final String description,
@Nullable final LocalDate startDate, final LocalDate endDate, final BigDecimal amount, final BigDecimal rate, final Currency currency,
final UUID reversedItemId) {
this(id, createdDate, invoiceId, accountId, childAccountId, bundleId, subscriptionId, description, startDate, endDate, amount, rate, currency, reversedItemId, null, null);
}

private InvoiceItemBase(final UUID id, @Nullable final DateTime createdDate, final UUID invoiceId, final UUID accountId, @Nullable final UUID childAccountId, @Nullable final UUID bundleId,
@Nullable final UUID subscriptionId, @Nullable final String description,
@Nullable final LocalDate startDate, final LocalDate endDate, final BigDecimal amount, final BigDecimal rate, final Currency currency,
final UUID reversedItemId,@Nullable final Integer quantity,@Nullable final String itemDetails) {
final UUID reversedItemId, @Nullable final Integer quantity, @Nullable final String itemDetails) {
super(id, createdDate, createdDate);
this.invoiceId = invoiceId;
this.accountId = accountId;
Expand Down Expand Up @@ -154,13 +154,11 @@ public UUID getLinkedItemId() {
return linkedItemId;
}


@Override
public UUID getChildAccountId() {
return childAccountId;
}


@Override
public String getPlanName() {
return null;
Expand Down Expand Up @@ -192,10 +190,14 @@ public String getPrettyUsageName() {
}

@Override
public Integer getQuantity() { return quantity; }
public Integer getQuantity() {
return quantity;
}

@Override
public String getItemDetails() { return itemDetails; }
public String getItemDetails() {
return itemDetails;
}

@Override
public boolean equals(final Object o) {
Expand Down
Expand Up @@ -49,7 +49,6 @@
import org.killbill.billing.usage.RawUsage;
import org.killbill.billing.usage.api.RolledUpUnit;
import org.killbill.billing.usage.api.RolledUpUsage;
import org.killbill.billing.util.config.definition.InvoiceConfig;
import org.killbill.billing.util.config.definition.InvoiceConfig.UsageDetailMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -95,6 +94,7 @@ public class ContiguousIntervalUsageInArrear {
private final LocalDate rawUsageStartDate;
private final InternalTenantContext internalTenantContext;
private final UsageDetailMode usageDetailMode;
private static final ObjectMapper objectMapper = new ObjectMapper();

public ContiguousIntervalUsageInArrear(final Usage usage,
final UUID accountId,
Expand Down Expand Up @@ -196,7 +196,7 @@ public UsageInArrearItemsAndNextNotificationDate computeMissingItemsAndNextNotif
final List<RolledUpUsage> allUsage = getRolledUpUsage();
for (final RolledUpUsage ru : allUsage) {
int tierNum = 1;
List<ConsumableInArrearDetail> toBeBilledUsageDetails = Lists.newLinkedList();
List<UsageInArrearDetail> toBeBilledUsageDetails = Lists.newLinkedList();
BigDecimal toBeBilledUsage = BigDecimal.ZERO;
if (usage.getUsageType() == UsageType.CAPACITY) {
toBeBilledUsageDetails.addAll(computeToBeBilledCapacityInArrear(ru.getRolledUpUnits(), tierNum));
Expand Down Expand Up @@ -225,8 +225,8 @@ public UsageInArrearItemsAndNextNotificationDate computeMissingItemsAndNextNotif
final BigDecimal amountToBill = toBeBilledForUnit(toBeBilledUsageDetails);

if (amountToBill.compareTo(BigDecimal.ZERO) > 0) {
if (UsageDetailMode.DETAIL.compareTo(usageDetailMode) == 0){
for (ConsumableInArrearDetail toBeBilledUsageDetail : toBeBilledUsageDetails){
if (UsageDetailMode.DETAIL == usageDetailMode){
for (UsageInArrearDetail toBeBilledUsageDetail : toBeBilledUsageDetails){
final InvoiceItem item = new UsageInvoiceItem(invoiceId, accountId, getBundleId(), getSubscriptionId(), getPlanName(),
getPhaseName(), usage.getName(), ru.getStart(), ru.getEnd(), toBeBilledUsageDetail.getAmount(), toBeBilledUsageDetail.getTierPrice(), getCurrency(),toBeBilledUsageDetail.getQuantity(),null);
result.add(item);
Expand Down Expand Up @@ -386,13 +386,13 @@ private Limit getTierLimit(final Tier tier, final String unitType) {
* @throws CatalogApiException
*/
@VisibleForTesting
List<ConsumableInArrearDetail> computeToBeBilledCapacityInArrear(final List<RolledUpUnit> roUnits, int tierNum) throws CatalogApiException {
List<UsageInArrearDetail> computeToBeBilledCapacityInArrear(final List<RolledUpUnit> roUnits, int tierNum) throws CatalogApiException {
Preconditions.checkState(isBuilt.get());

final List<Tier> tiers = getCapacityInArrearTier(usage);

for (final Tier cur : tiers) {
List<ConsumableInArrearDetail> toBeBilledDetails = Lists.newLinkedList();
List<UsageInArrearDetail> toBeBilledDetails = Lists.newLinkedList();
boolean complies = true;
for (final RolledUpUnit ro : roUnits) {
final Limit tierLimit = getTierLimit(cur, ro.getUnitType());
Expand All @@ -402,7 +402,7 @@ List<ConsumableInArrearDetail> computeToBeBilledCapacityInArrear(final List<Roll
complies = false;
break;
}
toBeBilledDetails.add(new ConsumableInArrearDetail(tierNum++, ro.getUnitType(), cur.getRecurringPrice().getPrice(getCurrency()), ro.getAmount().intValue(), BigDecimal.ZERO, null, null));
toBeBilledDetails.add(new UsageInArrearDetail(tierNum++, ro.getUnitType(), cur.getRecurringPrice().getPrice(getCurrency()), ro.getAmount().intValue(), BigDecimal.ZERO, null, null));

}
if (complies) {
Expand All @@ -423,7 +423,7 @@ List<ConsumableInArrearDetail> computeToBeBilledCapacityInArrear(final List<Roll
* @throws CatalogApiException
*/
@VisibleForTesting
List<ConsumableInArrearDetail> computeToBeBilledConsumableInArrear(final RolledUpUnit roUnit, int tierNum) throws CatalogApiException {
List<UsageInArrearDetail> computeToBeBilledConsumableInArrear(final RolledUpUnit roUnit, int tierNum) throws CatalogApiException {

Preconditions.checkState(isBuilt.get());
final List<TieredBlock> tieredBlocks = getConsumableInArrearTieredBlocks(usage, roUnit.getUnitType());
Expand All @@ -439,9 +439,9 @@ List<ConsumableInArrearDetail> computeToBeBilledConsumableInArrear(final RolledU
}


List<ConsumableInArrearDetail> computeToBeBilledConsumableInArrearWith_ALL_TIERS(final List<TieredBlock> tieredBlocks, final Long units, int tierNum) throws CatalogApiException {
List<UsageInArrearDetail> computeToBeBilledConsumableInArrearWith_ALL_TIERS(final List<TieredBlock> tieredBlocks, final Long units, int tierNum) throws CatalogApiException {

List<ConsumableInArrearDetail> toBeBilledDetails = Lists.newLinkedList();
List<UsageInArrearDetail> toBeBilledDetails = Lists.newLinkedList();
BigDecimal result = BigDecimal.ZERO;
int remainingUnits = units.intValue();
for (final TieredBlock tieredBlock : tieredBlocks) {
Expand All @@ -456,12 +456,12 @@ List<ConsumableInArrearDetail> computeToBeBilledConsumableInArrearWith_ALL_TIERS
nbUsedTierBlocks = tmp;
remainingUnits = 0;
}
toBeBilledDetails.add(new ConsumableInArrearDetail(tierNum, tieredBlock.getUnit().getName(), tieredBlock.getPrice().getPrice(getCurrency()), nbUsedTierBlocks));
toBeBilledDetails.add(new UsageInArrearDetail(tierNum, tieredBlock.getUnit().getName(), tieredBlock.getPrice().getPrice(getCurrency()), nbUsedTierBlocks));
}
return toBeBilledDetails;
}

ConsumableInArrearDetail computeToBeBilledConsumableInArrearWith_TOP_TIER(final List<TieredBlock> tieredBlocks, final Long units, int tierNum) throws CatalogApiException {
UsageInArrearDetail computeToBeBilledConsumableInArrearWith_TOP_TIER(final List<TieredBlock> tieredBlocks, final Long units, int tierNum) throws CatalogApiException {

int remainingUnits = units.intValue();

Expand All @@ -482,7 +482,7 @@ ConsumableInArrearDetail computeToBeBilledConsumableInArrearWith_TOP_TIER(final
final int lastBlockTierSize = targetBlock.getSize().intValue();
final int nbBlocks = units.intValue() / lastBlockTierSize + (units.intValue() % lastBlockTierSize == 0 ? 0 : 1);

return new ConsumableInArrearDetail(tierNum, targetBlock.getUnit().getName(), targetBlock.getPrice().getPrice(getCurrency()), nbBlocks);
return new UsageInArrearDetail(tierNum, targetBlock.getUnit().getName(), targetBlock.getPrice().getPrice(getCurrency()), nbBlocks);
}


Expand Down Expand Up @@ -589,68 +589,64 @@ public LocalDate getNextNotificationDate() {
}
}

public BigDecimal toBeBilledForUnit(List<ConsumableInArrearDetail> toBeBilledDetails){
public BigDecimal toBeBilledForUnit(List<UsageInArrearDetail> toBeBilledDetails){
BigDecimal result = BigDecimal.ZERO;
for (ConsumableInArrearDetail toBeBilled: toBeBilledDetails){
for (UsageInArrearDetail toBeBilled: toBeBilledDetails){
result = result.add(toBeBilled.getAmount());
}
return result;
}

private List<ConsumableInArrearDetail> reconcileExistedBilledWithToBeBilled(Iterable<InvoiceItem> billedItems, List<ConsumableInArrearDetail> toBeBilledConsumableInArrearDetails) {
private List<UsageInArrearDetail> reconcileExistedBilledWithToBeBilled(Iterable<InvoiceItem> billedItems, List<UsageInArrearDetail> toBeBilledUsageInArrearDetails) {
for (final InvoiceItem bi : billedItems) {
List<ConsumableInArrearDetail> billedUsageItemDetails = fromJson(bi.getItemDetails());
List<UsageInArrearDetail> billedUsageItemDetails = fromJson(bi.getItemDetails());

if (billedUsageItemDetails != null && billedUsageItemDetails.size() > 0) {

for (final ConsumableInArrearDetail toBeBilledConsumable : toBeBilledConsumableInArrearDetails) {
for (final UsageInArrearDetail toBeBilledConsumable : toBeBilledUsageInArrearDetails) {
billedUsageItemDetails = toBeBilledConsumable.reconcile(billedUsageItemDetails);
}

if (billedUsageItemDetails != null && billedUsageItemDetails.size() > 0) {
for (final ConsumableInArrearDetail billedUsage : billedUsageItemDetails) {
toBeBilledConsumableInArrearDetails.add(new ConsumableInArrearDetail(billedUsage.getTier(), billedUsage.getTierUnit(), billedUsage.getTierPrice(),
billedUsage.getQuantity(), billedUsage.getAmount().negate(), null, bi.getId().toString()));
for (final UsageInArrearDetail billedUsage : billedUsageItemDetails) {
toBeBilledUsageInArrearDetails.add(new UsageInArrearDetail(billedUsage.getTier(), billedUsage.getTierUnit(), billedUsage.getTierPrice(),
billedUsage.getQuantity(), billedUsage.getAmount().negate(), null, bi.getId().toString()));
}
}
} else {
toBeBilledConsumableInArrearDetails.add(new ConsumableInArrearDetail(bi.getRate(), bi.getQuantity(), bi.getAmount().negate(), bi.getId().toString()));
toBeBilledUsageInArrearDetails.add(new UsageInArrearDetail(bi.getRate(), bi.getQuantity(), bi.getAmount().negate(), bi.getId().toString()));
}
}

return toBeBilledConsumableInArrearDetails;
return toBeBilledUsageInArrearDetails;
}

public static final String toJson(List<ConsumableInArrearDetail> toBeBilledConsumableInArrearDetails) {
private static final String toJson(List<UsageInArrearDetail> toBeBilledUsageInArrearDetails) {
String result = null;
if (toBeBilledConsumableInArrearDetails != null && toBeBilledConsumableInArrearDetails.size() > 0){
ObjectMapper objectMapper = new ObjectMapper();
if (toBeBilledUsageInArrearDetails != null && toBeBilledUsageInArrearDetails.size() > 0){
try {
result = objectMapper.writeValueAsString(toBeBilledConsumableInArrearDetails);
result = objectMapper.writeValueAsString(toBeBilledUsageInArrearDetails);
} catch (JsonProcessingException e) {
Preconditions.checkState(false, e.getMessage());
}
}
return result;
}

public static final List<ConsumableInArrearDetail> fromJson(String itemDetails){
List<ConsumableInArrearDetail> toBeBilledConsumableInArrearDetails = null;
private static final List<UsageInArrearDetail> fromJson(String itemDetails){
List<UsageInArrearDetail> toBeBilledUsageInArrearDetails = null;
if (itemDetails != null){
ObjectMapper objectMapper = new ObjectMapper();
try {
toBeBilledConsumableInArrearDetails = objectMapper.reader()
.forType(new TypeReference<List<ConsumableInArrearDetail>>() {})
.readValue(itemDetails);
toBeBilledUsageInArrearDetails = objectMapper.readValue(itemDetails, new TypeReference<List<UsageInArrearDetail>>() {});
} catch (IOException e) {
Preconditions.checkState(false, e.getMessage());
}
}

return toBeBilledConsumableInArrearDetails;
return toBeBilledUsageInArrearDetails;
}

public class ConsumableInArrearDetail {
public class UsageInArrearDetail {

private final int tier;
private final String tierUnit;
Expand All @@ -660,19 +656,19 @@ public class ConsumableInArrearDetail {
private BigDecimal existingUsageAmount;
private BigDecimal amount;

public ConsumableInArrearDetail(BigDecimal tierPrice, Integer quantity, BigDecimal existingUsageAmount, String reference){
public UsageInArrearDetail(BigDecimal tierPrice, Integer quantity, BigDecimal existingUsageAmount, String reference) {
this(0, null, tierPrice, quantity, existingUsageAmount, BigDecimal.ZERO, reference);
}

public ConsumableInArrearDetail(int tier, String tierUnit, BigDecimal tierPrice, Integer quantity){
public UsageInArrearDetail(int tier, String tierUnit, BigDecimal tierPrice, Integer quantity) {
this(tier, tierUnit, tierPrice, quantity, tierPrice.multiply(new BigDecimal(quantity)), null, null);
}

@JsonCreator
public ConsumableInArrearDetail(@JsonProperty("tier") int tier, @JsonProperty("tierUnit") String tierUnit,
@JsonProperty("tierPrice") BigDecimal tierPrice, @JsonProperty("quantity") Integer quantity,
@JsonProperty("amount") BigDecimal amount, @JsonProperty("existingUsageAmount") BigDecimal existingUsageAmount,
@JsonProperty("reference") String reference){
public UsageInArrearDetail(@JsonProperty("tier") int tier, @JsonProperty("tierUnit") String tierUnit,
@JsonProperty("tierPrice") BigDecimal tierPrice, @JsonProperty("quantity") Integer quantity,
@JsonProperty("amount") BigDecimal amount, @JsonProperty("existingUsageAmount") BigDecimal existingUsageAmount,
@JsonProperty("reference") String reference) {
this.tier = tier;
this.tierUnit = tierUnit;
this.tierPrice = tierPrice;
Expand All @@ -685,37 +681,43 @@ public ConsumableInArrearDetail(@JsonProperty("tier") int tier, @JsonProperty("t
public int getTier() {
return tier;
}

public String getTierUnit() {
return tierUnit;
}
public BigDecimal getTierPrice() {

public BigDecimal getTierPrice() {
return tierPrice;
}

public Integer getQuantity() {
return quantity;
}

public BigDecimal getExistingUsageAmount() {
return existingUsageAmount;
}

public void setExistingUsageAmount(BigDecimal existingUsageAmount) {
this.existingUsageAmount = existingUsageAmount;
}

public BigDecimal getAmount() {
return amount;
}

public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public void setExistingUsageAmount(BigDecimal existingUsageAmount) {
this.existingUsageAmount = existingUsageAmount;
}

public void setReference(String reference) {
this.reference = reference;
}

public List<ConsumableInArrearDetail> reconcile(List<ConsumableInArrearDetail> billedUsageItemDetails){
List<ConsumableInArrearDetail> unreconciledUsage = Lists.newLinkedList();
for (ConsumableInArrearDetail billedUsageDetail : billedUsageItemDetails){
if( tierUnit == billedUsageDetail.tierUnit ) {
public List<UsageInArrearDetail> reconcile(List<UsageInArrearDetail> billedUsageItemDetails) {
List<UsageInArrearDetail> unreconciledUsage = Lists.newLinkedList();
for (UsageInArrearDetail billedUsageDetail : billedUsageItemDetails) {
if (tierUnit.equals(billedUsageDetail.tierUnit)) {
existingUsageAmount = billedUsageDetail.amount.abs();
amount = amount.subtract(existingUsageAmount);
} else {
Expand Down

0 comments on commit 7b63798

Please sign in to comment.