Search in sources :

Example 26 with RepairAdjInvoiceItem

use of org.killbill.billing.invoice.model.RepairAdjInvoiceItem in project killbill by killbill.

the class TestSubscriptionItemTree method testMergeMonthlyToAnnualWithNoProRation.

@Test(groups = "fast")
public void testMergeMonthlyToAnnualWithNoProRation() {
    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, planName, phaseName, startDate, endMonthly1, monthlyAmount, monthlyRate, currency);
    final InvoiceItem monthly2 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, endMonthly1, endMonthly2, monthlyAmount, monthlyRate, currency);
    // First test with items in order
    final SubscriptionItemTree tree = new SubscriptionItemTree(subscriptionId, invoiceId);
    tree.addItem(monthly1);
    tree.addItem(monthly2);
    tree.flatten(true);
    final InvoiceItem proposed = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, switchToAnnualDate, endDate, yearlyAmount, yearlyRate, currency);
    final InvoiceItem proposedMonthly1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, endMonthly1, monthlyAmount, monthlyRate, currency);
    tree.mergeProposedItem(proposedMonthly1);
    final InvoiceItem proRatedmonthly2 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, endMonthly1, switchToAnnualDate, monthlyAmount, monthlyRate, currency);
    tree.mergeProposedItem(proRatedmonthly2);
    tree.mergeProposedItem(proposed);
    tree.buildForMerge();
    final List<InvoiceItem> expectedResult = Lists.newLinkedList();
    final InvoiceItem repair = new RepairAdjInvoiceItem(invoiceId, accountId, switchToAnnualDate, endMonthly2, new BigDecimal("-2.57"), currency, monthly2.getId());
    expectedResult.add(proposed);
    expectedResult.add(repair);
    verifyResult(tree.getView(), expectedResult);
}
Also used : FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) Test(org.testng.annotations.Test)

Example 27 with RepairAdjInvoiceItem

use of org.killbill.billing.invoice.model.RepairAdjInvoiceItem in project killbill by killbill.

the class TestSubscriptionItemTree method testMergeCancellationWithFinalRepair.

@Test(groups = "fast")
public void testMergeCancellationWithFinalRepair() {
    final LocalDate startDate = new LocalDate(2014, 1, 1);
    final LocalDate cancelDate = new LocalDate(2014, 1, 25);
    final LocalDate endDate = new LocalDate(2014, 2, 1);
    final BigDecimal monthlyRate1 = new BigDecimal("12.00");
    final BigDecimal monthlyAmount1 = monthlyRate1;
    final SubscriptionItemTree tree = new SubscriptionItemTree(subscriptionId, invoiceId);
    final InvoiceItem monthly1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, endDate, monthlyAmount1, monthlyRate1, currency);
    tree.addItem(monthly1);
    tree.flatten(true);
    final InvoiceItem proposed1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, cancelDate, monthlyAmount1, monthlyRate1, currency);
    tree.mergeProposedItem(proposed1);
    tree.buildForMerge();
    final List<InvoiceItem> expectedResult = Lists.newLinkedList();
    final InvoiceItem repair = new RepairAdjInvoiceItem(invoiceId, accountId, cancelDate, endDate, new BigDecimal("-2.71"), currency, monthly1.getId());
    expectedResult.add(repair);
    verifyResult(tree.getView(), expectedResult);
}
Also used : FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) Test(org.testng.annotations.Test)

Example 28 with RepairAdjInvoiceItem

use of org.killbill.billing.invoice.model.RepairAdjInvoiceItem in project killbill by killbill.

the class TestSubscriptionItemTree method testPartialProRation.

@Test(groups = "fast")
public void testPartialProRation() {
    final LocalDate startDate = new LocalDate(2014, 1, 1);
    final LocalDate cancelDate = new LocalDate(2014, 1, 25);
    final LocalDate endDate = new LocalDate(2014, 2, 1);
    final BigDecimal monthlyRate1 = new BigDecimal("12.00");
    final BigDecimal monthlyAmount1 = monthlyRate1;
    final SubscriptionItemTree tree = new SubscriptionItemTree(subscriptionId, invoiceId);
    final InvoiceItem existing1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, endDate, monthlyAmount1, monthlyRate1, currency);
    tree.addItem(existing1);
    // Partially item adjust the recurring item
    final InvoiceItem existingItemAdj1 = new ItemAdjInvoiceItem(existing1, startDate, monthlyRate1.negate().add(BigDecimal.ONE), currency);
    tree.addItem(existingItemAdj1);
    tree.flatten(true);
    final InvoiceItem proposed1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, cancelDate, monthlyAmount1, monthlyRate1, currency);
    tree.mergeProposedItem(proposed1);
    tree.buildForMerge();
    final InvoiceItem repair = new RepairAdjInvoiceItem(invoiceId, accountId, cancelDate, endDate, BigDecimal.ONE.negate(), Currency.USD, existing1.getId());
    final List<InvoiceItem> expectedResult = ImmutableList.<InvoiceItem>of(repair);
    verifyResult(tree.getView(), expectedResult);
}
Also used : FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) Test(org.testng.annotations.Test)

Example 29 with RepairAdjInvoiceItem

use of org.killbill.billing.invoice.model.RepairAdjInvoiceItem in project killbill by killbill.

the class TestSubscriptionItemTree method testRepairWithLargeItemAdjustment.

@Test(groups = "fast")
public void testRepairWithLargeItemAdjustment() {
    final LocalDate startDate = new LocalDate(2014, 1, 1);
    final LocalDate itemAdjDate = new LocalDate(2014, 1, 2);
    final LocalDate endDate = new LocalDate(2014, 2, 1);
    final LocalDate cancelDate = new LocalDate(2014, 1, 23);
    final BigDecimal rate1 = new BigDecimal("12.00");
    final BigDecimal amount1 = rate1;
    final SubscriptionItemTree tree = new SubscriptionItemTree(subscriptionId, invoiceId);
    final InvoiceItem initial = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, endDate, amount1, rate1, currency);
    final InvoiceItem itemAdj = new ItemAdjInvoiceItem(initial, itemAdjDate, new BigDecimal("-10.00"), currency);
    tree.addItem(initial);
    tree.addItem(itemAdj);
    tree.flatten(true);
    final InvoiceItem proposed1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, cancelDate, amount1, rate1, currency);
    tree.mergeProposedItem(proposed1);
    tree.buildForMerge();
    final List<InvoiceItem> expectedResult = Lists.newLinkedList();
    final InvoiceItem repair1 = new RepairAdjInvoiceItem(invoiceId, accountId, cancelDate, endDate, new BigDecimal("-2.00"), currency, initial.getId());
    expectedResult.add(repair1);
    verifyResult(tree.getView(), expectedResult);
}
Also used : FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) Test(org.testng.annotations.Test)

Example 30 with RepairAdjInvoiceItem

use of org.killbill.billing.invoice.model.RepairAdjInvoiceItem 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, planName, phaseName, 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 maxAvailableAmountForRepair = getNetAmount();
        final BigDecimal positiveAmountForRepair = positiveAmount.compareTo(maxAvailableAmountForRepair) <= 0 ? positiveAmount : maxAvailableAmountForRepair;
        return positiveAmountForRepair.compareTo(BigDecimal.ZERO) > 0 ? new RepairAdjInvoiceItem(targetInvoiceId, accountId, newStartDate, newEndDate, positiveAmountForRepair.negate(), currency, linkedId) : null;
    }
}
Also used : RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) BigDecimal(java.math.BigDecimal) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem)

Aggregations

RepairAdjInvoiceItem (org.killbill.billing.invoice.model.RepairAdjInvoiceItem)50 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)48 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)48 Test (org.testng.annotations.Test)47 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)46 BigDecimal (java.math.BigDecimal)45 LocalDate (org.joda.time.LocalDate)45 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)45 UUID (java.util.UUID)19 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)19 Invoice (org.killbill.billing.invoice.api.Invoice)18 LinkedList (java.util.LinkedList)11 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)11 BillingEventSet (org.killbill.billing.junction.BillingEventSet)11 SubscriptionFutureNotificationDates (org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates)10 DefaultPrice (org.killbill.billing.catalog.DefaultPrice)9 MockInternationalPrice (org.killbill.billing.catalog.MockInternationalPrice)9 MockPlan (org.killbill.billing.catalog.MockPlan)9 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)9 Plan (org.killbill.billing.catalog.api.Plan)9