use of org.killbill.billing.invoice.model.RecurringInvoiceItem in project killbill by killbill.
the class Item method toProratedInvoiceItem.
public InvoiceItem toProratedInvoiceItem(final LocalDate newStartDate, final LocalDate newEndDate) {
int nbTotalDays = Days.daysBetween(startDate, endDate).getDays();
final boolean prorated = !(newStartDate.compareTo(startDate) == 0 && newEndDate.compareTo(endDate) == 0);
// Pro-ration is built by using the startDate, endDate and amount of this item instead of using the rate and a potential full period.
final BigDecimal positiveAmount = prorated ? InvoiceDateUtils.calculateProrationBetweenDates(newStartDate, newEndDate, nbTotalDays).multiply(amount) : amount;
if (action == ItemAction.ADD) {
return new RecurringInvoiceItem(id, createdDate, invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, catalogEffectiveDate, newStartDate, newEndDate, positiveAmount, rate, currency);
} else {
// We first compute the maximum amount after adjustment and that sets the amount limit of how much can be repaired.
final BigDecimal netAmount = getNetAmount();
final BigDecimal maxAmountForRepair = positiveAmount.compareTo(netAmount) <= 0 ? positiveAmount : netAmount;
final BigDecimal resultingAmountForRepair = maxAmountForRepair.compareTo(BigDecimal.ZERO) > 0 ? maxAmountForRepair : BigDecimal.ZERO;
return new RepairAdjInvoiceItem(targetInvoiceId, accountId, newStartDate, newEndDate, resultingAmountForRepair.negate(), currency, linkedId);
}
}
use of org.killbill.billing.invoice.model.RecurringInvoiceItem in project killbill by killbill.
the class InvoicePluginDispatcher method validateAndSanitizeInvoiceItemFromPlugin.
private InvoiceItem validateAndSanitizeInvoiceItemFromPlugin(final UUID originalInvoiceId, final Map<UUID, InvoiceItem> invoiceItemsByItemId, final InvoiceItem additionalInvoiceItem, final InvoicePluginApi invoicePlugin) throws InvoiceApiException {
final InvoiceItem existingItem = invoiceItemsByItemId.get(additionalInvoiceItem.getId());
if (!ALLOWED_INVOICE_ITEM_TYPES.contains(additionalInvoiceItem.getInvoiceItemType()) && existingItem == null) {
log.warn("Ignoring invoice item of type {} from InvoicePlugin {}: {}", additionalInvoiceItem.getInvoiceItemType(), invoicePlugin, additionalInvoiceItem);
throw new InvoiceApiException(ErrorCode.INVOICE_ITEM_TYPE_INVALID, additionalInvoiceItem.getInvoiceItemType());
}
final UUID invoiceId = MoreObjects.firstNonNull(mutableField("invoiceId", existingItem != null ? existingItem.getInvoiceId() : null, additionalInvoiceItem.getInvoiceId(), invoicePlugin), originalInvoiceId);
final UUID additionalInvoiceId = MoreObjects.firstNonNull(additionalInvoiceItem.getId(), UUIDs.randomUUID());
final InvoiceItemCatalogBase tmp = new InvoiceItemCatalogBase(additionalInvoiceId, mutableField("createdDate", existingItem != null ? existingItem.getCreatedDate() : null, additionalInvoiceItem.getCreatedDate(), invoicePlugin), invoiceId, immutableField("accountId", existingItem, existingItem != null ? existingItem.getAccountId() : null, additionalInvoiceItem.getAccountId(), invoicePlugin), immutableField("bundleId", existingItem, existingItem != null ? existingItem.getBundleId() : null, additionalInvoiceItem.getBundleId(), invoicePlugin), immutableField("subscriptionId", existingItem, existingItem != null ? existingItem.getSubscriptionId() : null, additionalInvoiceItem.getSubscriptionId(), invoicePlugin), mutableField("description", existingItem != null ? existingItem.getDescription() : null, additionalInvoiceItem.getDescription(), invoicePlugin), immutableField("productName", existingItem, existingItem != null ? existingItem.getProductName() : null, additionalInvoiceItem.getProductName(), invoicePlugin), immutableField("planName", existingItem, existingItem != null ? existingItem.getPlanName() : null, additionalInvoiceItem.getPlanName(), invoicePlugin), immutableField("phaseName", existingItem, existingItem != null ? existingItem.getPhaseName() : null, additionalInvoiceItem.getPhaseName(), invoicePlugin), immutableField("usageName", existingItem, existingItem != null ? existingItem.getUsageName() : null, additionalInvoiceItem.getUsageName(), invoicePlugin), immutableField("catalogEffectiveDate", existingItem, existingItem != null ? existingItem.getCatalogEffectiveDate() : null, additionalInvoiceItem.getCatalogEffectiveDate(), invoicePlugin), mutableField("prettyProductName", existingItem != null ? existingItem.getPrettyProductName() : null, additionalInvoiceItem.getPrettyProductName(), invoicePlugin), mutableField("prettyPlanName", existingItem != null ? existingItem.getPrettyPlanName() : null, additionalInvoiceItem.getPrettyPlanName(), invoicePlugin), mutableField("prettyPhaseName", existingItem != null ? existingItem.getPrettyPhaseName() : null, additionalInvoiceItem.getPrettyPhaseName(), invoicePlugin), mutableField("prettyUsageName", existingItem != null ? existingItem.getPrettyUsageName() : null, additionalInvoiceItem.getPrettyUsageName(), invoicePlugin), immutableField("startDate", existingItem, existingItem != null ? existingItem.getStartDate() : null, additionalInvoiceItem.getStartDate(), invoicePlugin), immutableField("endDate", existingItem, existingItem != null ? existingItem.getEndDate() : null, additionalInvoiceItem.getEndDate(), invoicePlugin), mutableField("amount", existingItem != null ? existingItem.getAmount() : null, additionalInvoiceItem.getAmount(), invoicePlugin), immutableField("rate", existingItem, existingItem != null ? existingItem.getRate() : null, additionalInvoiceItem.getRate(), invoicePlugin), immutableField("currency", existingItem, existingItem != null ? existingItem.getCurrency() : null, additionalInvoiceItem.getCurrency(), invoicePlugin), immutableField("linkedItemId", existingItem, existingItem != null ? existingItem.getLinkedItemId() : null, additionalInvoiceItem.getLinkedItemId(), invoicePlugin), immutableField("quantity", existingItem, existingItem != null ? existingItem.getQuantity() : null, additionalInvoiceItem.getQuantity(), invoicePlugin), mutableField("itemDetails", existingItem != null ? existingItem.getItemDetails() : null, additionalInvoiceItem.getItemDetails(), invoicePlugin), immutableField("invoiceItemType", existingItem, existingItem != null ? existingItem.getInvoiceItemType() : null, additionalInvoiceItem.getInvoiceItemType(), invoicePlugin));
switch(tmp.getInvoiceItemType()) {
case RECURRING:
return new RecurringInvoiceItem(tmp);
case USAGE:
return new UsageInvoiceItem(tmp);
case FIXED:
return new FixedPriceInvoiceItem(tmp);
case TAX:
return new TaxInvoiceItem(tmp);
case EXTERNAL_CHARGE:
return new ExternalChargeInvoiceItem(tmp);
default:
// As long as there is no explicit cast to the expected type this works
return tmp;
}
}
use of org.killbill.billing.invoice.model.RecurringInvoiceItem in project killbill by killbill.
the class TestSubscriptionItemTree method testAnnualWithBlocking1.
@Test(groups = "fast")
public void testAnnualWithBlocking1() {
final LocalDate startDate = new LocalDate(2014, 1, 1);
final LocalDate endDate = new LocalDate(2015, 1, 1);
final BigDecimal yearlyRate = new BigDecimal("100.00");
final BigDecimal yearlyAmount = yearlyRate;
final InvoiceItem annual = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, null, startDate, endDate, yearlyAmount, yearlyRate, currency);
final SubscriptionItemTree tree = new SubscriptionItemTree(subscriptionId, invoiceId);
tree.addItem(annual);
tree.build();
tree.flatten(true);
final LocalDate startBlock = new LocalDate(2014, 1, 14);
final LocalDate endBlock = new LocalDate(2014, 2, 8);
final LocalDate newEndDate = new LocalDate(2015, 2, 1);
final InvoiceItem proposed1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, null, startDate, startBlock, BigDecimal.TEN, yearlyAmount, currency);
final InvoiceItem proposed2 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, null, endBlock, newEndDate, new BigDecimal("90.0"), yearlyAmount, currency);
tree.mergeProposedItem(proposed1);
tree.mergeProposedItem(proposed2);
tree.buildForMerge();
final InvoiceItem expected1 = new RepairAdjInvoiceItem(invoiceId, accountId, startBlock, endBlock, new BigDecimal("-6.85"), currency, annual.getId());
final InvoiceItem expected2 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, null, endDate, newEndDate, new BigDecimal("7.79"), yearlyAmount, currency);
final List<InvoiceItem> expectedResult = Lists.newLinkedList();
expectedResult.addAll(ImmutableList.of(expected1, expected2));
verifyResult(tree.getView(), expectedResult);
}
use of org.killbill.billing.invoice.model.RecurringInvoiceItem in project killbill by killbill.
the class TestSubscriptionItemTree method testMonthlyToAnnualWithNoProRation.
@Test(groups = "fast")
public void testMonthlyToAnnualWithNoProRation() {
final LocalDate startDate = new LocalDate(2014, 1, 1);
final LocalDate endMonthly1 = new LocalDate(2014, 2, 1);
final LocalDate endMonthly2 = new LocalDate(2014, 3, 1);
final LocalDate switchToAnnualDate = new LocalDate(2014, 2, 23);
final LocalDate endDate = new LocalDate(2015, 2, 23);
final BigDecimal monthlyRate = new BigDecimal("12.00");
final BigDecimal monthlyAmount = monthlyRate;
final BigDecimal yearlyRate = new BigDecimal("100.00");
final BigDecimal yearlyAmount = yearlyRate;
final InvoiceItem monthly1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, null, startDate, endMonthly1, monthlyAmount, monthlyRate, currency);
final InvoiceItem monthly2 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, null, endMonthly1, endMonthly2, monthlyAmount, monthlyRate, currency);
final InvoiceItem repair = new RepairAdjInvoiceItem(invoiceId, accountId, switchToAnnualDate, endMonthly2, monthlyAmount.negate(), currency, monthly2.getId());
final InvoiceItem annual = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, null, switchToAnnualDate, endDate, yearlyAmount, yearlyRate, currency);
final List<InvoiceItem> expectedResult = Lists.newLinkedList();
expectedResult.add(monthly1);
final InvoiceItem monthly2Prorated = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, null, endMonthly1, switchToAnnualDate, new BigDecimal("9.43"), monthlyRate, currency);
expectedResult.add(monthly2Prorated);
expectedResult.add(annual);
// First test with items in order
final SubscriptionItemTree tree = new SubscriptionItemTree(subscriptionId, invoiceId);
tree.addItem(monthly1);
tree.addItem(monthly2);
tree.addItem(repair);
tree.addItem(annual);
tree.build();
verifyResult(tree.getView(), expectedResult);
}
use of org.killbill.billing.invoice.model.RecurringInvoiceItem in project killbill by killbill.
the class TestSubscriptionItemTree method testInvalidRepair.
@Test(groups = "fast")
public void testInvalidRepair() {
final LocalDate startDate = new LocalDate(2014, 1, 1);
final LocalDate endDate = new LocalDate(2014, 2, 1);
final BigDecimal rate = new BigDecimal("12.00");
final InvoiceItem initial = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, null, startDate, endDate, rate, rate, currency);
final InvoiceItem tooEarlyRepair = new RepairAdjInvoiceItem(invoiceId, accountId, startDate.minusDays(1), endDate, rate.negate(), currency, initial.getId());
final InvoiceItem tooLateRepair = new RepairAdjInvoiceItem(invoiceId, accountId, startDate, endDate.plusDays(1), rate.negate(), currency, initial.getId());
List<InvoiceItem> result;
SubscriptionItemTree tree = new SubscriptionItemTree(subscriptionId, invoiceId);
tree.addItem(initial);
tree.addItem(tooEarlyRepair);
tree.build();
result = tree.getView();
Assert.assertEquals(result.size(), 0);
tree = new SubscriptionItemTree(subscriptionId, invoiceId);
tree.addItem(initial);
tree.addItem(tooLateRepair);
tree.build();
result = tree.getView();
Assert.assertEquals(result.size(), 0);
}
Aggregations