Search in sources :

Example 31 with RepairAdjInvoiceItem

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

the class TestSubscriptionItemTree method testInvalidRepairCausingOverlappingRecurring.

@Test(groups = "fast")
public void testInvalidRepairCausingOverlappingRecurring() {
    final LocalDate startDate = new LocalDate(2014, 1, 1);
    final LocalDate endDate = new LocalDate(2014, 2, 1);
    final LocalDate repairDate1 = new LocalDate(2014, 1, 23);
    final LocalDate repairDate2 = new LocalDate(2014, 1, 26);
    final BigDecimal rate1 = new BigDecimal("12.00");
    final BigDecimal amount1 = rate1;
    final BigDecimal rate2 = new BigDecimal("14.85");
    final BigDecimal amount2 = rate2;
    final BigDecimal rate3 = new BigDecimal("19.23");
    final BigDecimal amount3 = rate3;
    final InvoiceItem initial = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, endDate, amount1, rate1, currency);
    final InvoiceItem newItem1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, repairDate1, endDate, amount2, rate2, currency);
    final InvoiceItem repair1 = new RepairAdjInvoiceItem(invoiceId, accountId, repairDate1, endDate, amount1.negate(), currency, initial.getId());
    final InvoiceItem newItem2 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, repairDate2, endDate, amount3, rate3, currency);
    // This repair should point to newItem1 instead
    final InvoiceItem repair2 = new RepairAdjInvoiceItem(invoiceId, accountId, repairDate2, endDate, amount2.negate(), currency, initial.getId());
    // Out-of-order insertion to show ordering doesn't matter
    final SubscriptionItemTree tree = new SubscriptionItemTree(subscriptionId, invoiceId);
    tree.addItem(repair1);
    tree.addItem(repair2);
    tree.addItem(initial);
    tree.addItem(newItem1);
    tree.addItem(newItem2);
    try {
        tree.build();
        fail();
    } catch (final IllegalStateException e) {
    }
}
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 32 with RepairAdjInvoiceItem

use of org.killbill.billing.invoice.model.RepairAdjInvoiceItem 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, planName, phaseName, 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());
    SubscriptionItemTree tree = new SubscriptionItemTree(subscriptionId, invoiceId);
    tree.addItem(initial);
    tree.addItem(tooEarlyRepair);
    try {
        tree.build();
        fail();
    } catch (final IllegalStateException e) {
    }
    tree = new SubscriptionItemTree(subscriptionId, invoiceId);
    tree.addItem(initial);
    tree.addItem(tooLateRepair);
    try {
        tree.build();
        fail();
    } catch (final IllegalStateException e) {
    }
}
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 33 with RepairAdjInvoiceItem

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

the class TestSubscriptionItemTree method testMergeWithSecondRepair.

@Test(groups = "fast")
public void testMergeWithSecondRepair() {
    final LocalDate startDate = new LocalDate(2012, 5, 1);
    final LocalDate endDate = new LocalDate(2012, 6, 1);
    final LocalDate change1 = new LocalDate(2012, 5, 7);
    final LocalDate change2 = new LocalDate(2012, 5, 8);
    final BigDecimal rate1 = new BigDecimal("599.95");
    final BigDecimal amount1 = rate1;
    final BigDecimal rate2 = new BigDecimal("9.95");
    final BigDecimal proratedAmount2 = new BigDecimal("8.02");
    final BigDecimal rate3 = new BigDecimal("29.95");
    final BigDecimal proratedAmount3 = new BigDecimal("23.19");
    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 newItem1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, "foo", "foo", change1, endDate, proratedAmount2, rate2, currency);
    final InvoiceItem repair1 = new RepairAdjInvoiceItem(invoiceId, accountId, change1, endDate, new BigDecimal("-483.86"), currency, initial.getId());
    tree.addItem(initial);
    tree.addItem(newItem1);
    tree.addItem(repair1);
    tree.flatten(true);
    final InvoiceItem proposed1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, change1, amount1, rate1, currency);
    final InvoiceItem proposed2 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, "foo", "foo", change1, change2, proratedAmount3, rate2, currency);
    final InvoiceItem proposed3 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, "bar", "bar", change2, endDate, proratedAmount3, rate3, currency);
    tree.mergeProposedItem(proposed1);
    tree.mergeProposedItem(proposed2);
    tree.mergeProposedItem(proposed3);
    tree.buildForMerge();
    final List<InvoiceItem> expectedResult = Lists.newLinkedList();
    final InvoiceItem repair2 = new RepairAdjInvoiceItem(invoiceId, accountId, change2, endDate, new BigDecimal("-7.70"), currency, initial.getId());
    expectedResult.add(proposed3);
    expectedResult.add(repair2);
    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 34 with RepairAdjInvoiceItem

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

the class TestDefaultInvoiceFormatter method testMergeItems.

@Test(groups = "fast")
public void testMergeItems() throws Exception {
    // Scenario: single item with payment
    // * $10 item
    // Then, a repair occur:
    // * $-10 repair
    // * $10 generated CBA due to the repair (assume previous payment)
    // Then, the invoice is adjusted for $1:
    // * $-1 credit adjustment
    // * $1 generated CBA due to the credit adjustment
    final FixedPriceInvoiceItem fixedItem = new FixedPriceInvoiceItem(UUID.randomUUID(), UUID.randomUUID(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), new LocalDate(), BigDecimal.TEN, Currency.USD);
    final RepairAdjInvoiceItem repairAdjInvoiceItem = new RepairAdjInvoiceItem(fixedItem.getInvoiceId(), fixedItem.getAccountId(), fixedItem.getStartDate(), fixedItem.getEndDate(), fixedItem.getAmount().negate(), fixedItem.getCurrency(), fixedItem.getId());
    final CreditBalanceAdjInvoiceItem creditBalanceAdjInvoiceItem = new CreditBalanceAdjInvoiceItem(fixedItem.getInvoiceId(), fixedItem.getAccountId(), fixedItem.getStartDate(), fixedItem.getAmount(), fixedItem.getCurrency());
    final CreditAdjInvoiceItem creditAdjInvoiceItem = new CreditAdjInvoiceItem(fixedItem.getInvoiceId(), fixedItem.getAccountId(), fixedItem.getStartDate(), null, BigDecimal.ONE.negate(), fixedItem.getCurrency());
    final CreditBalanceAdjInvoiceItem creditBalanceAdjInvoiceItem2 = new CreditBalanceAdjInvoiceItem(fixedItem.getInvoiceId(), fixedItem.getAccountId(), fixedItem.getStartDate(), creditAdjInvoiceItem.getAmount().negate(), fixedItem.getCurrency());
    final DefaultInvoice invoice = new DefaultInvoice(fixedItem.getInvoiceId(), fixedItem.getAccountId(), null, new LocalDate(), new LocalDate(), Currency.USD, false, InvoiceStatus.COMMITTED);
    invoice.addInvoiceItem(fixedItem);
    invoice.addInvoiceItem(repairAdjInvoiceItem);
    invoice.addInvoiceItem(creditBalanceAdjInvoiceItem);
    invoice.addInvoiceItem(creditAdjInvoiceItem);
    invoice.addInvoiceItem(creditBalanceAdjInvoiceItem2);
    invoice.addPayment(new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, UUID.randomUUID(), invoice.getId(), clock.getUTCNow(), BigDecimal.TEN, Currency.USD, Currency.USD, null, true));
    invoice.addPayment(new DefaultInvoicePayment(InvoicePaymentType.REFUND, UUID.randomUUID(), invoice.getId(), clock.getUTCNow(), BigDecimal.ONE.negate(), Currency.USD, Currency.USD, null, true));
    // Check the scenario
    Assert.assertEquals(invoice.getBalance().doubleValue(), 1.00);
    Assert.assertEquals(invoice.getCreditedAmount().doubleValue(), 11.00);
    Assert.assertEquals(invoice.getRefundedAmount().doubleValue(), -1.00);
    // Verify the merge
    final InvoiceFormatter formatter = new DefaultInvoiceFormatter(config, invoice, Locale.US, null, resourceBundleFactory, internalCallContext);
    final List<InvoiceItem> invoiceItems = formatter.getInvoiceItems();
    Assert.assertEquals(invoiceItems.size(), 4);
    Assert.assertEquals(invoiceItems.get(0).getInvoiceItemType(), InvoiceItemType.FIXED);
    Assert.assertEquals(invoiceItems.get(0).getAmount().doubleValue(), 10.00);
    Assert.assertEquals(invoiceItems.get(1).getInvoiceItemType(), InvoiceItemType.REPAIR_ADJ);
    Assert.assertEquals(invoiceItems.get(1).getAmount().doubleValue(), -10.00);
    Assert.assertEquals(invoiceItems.get(2).getInvoiceItemType(), InvoiceItemType.CBA_ADJ);
    Assert.assertEquals(invoiceItems.get(2).getAmount().doubleValue(), 11.00);
    Assert.assertEquals(invoiceItems.get(3).getInvoiceItemType(), InvoiceItemType.CREDIT_ADJ);
    Assert.assertEquals(invoiceItems.get(3).getAmount().doubleValue(), -1.00);
}
Also used : InvoiceFormatter(org.killbill.billing.invoice.api.formatters.InvoiceFormatter) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) LocalDate(org.joda.time.LocalDate) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) Test(org.testng.annotations.Test)

Example 35 with RepairAdjInvoiceItem

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

the class TestSubscriptionItemTree method testOverlappingRepair.

// The test that first repair (repair1) and new Item (newItem1) end up being ignored.
@Test(groups = "fast")
public void testOverlappingRepair() {
    final LocalDate startDate = new LocalDate(2012, 5, 1);
    final LocalDate endDate = new LocalDate(2013, 5, 1);
    final LocalDate changeDate = new LocalDate(2012, 5, 11);
    final LocalDate monthlyAlignmentDate = new LocalDate(2012, 6, 1);
    final BigDecimal rate1 = new BigDecimal("2400.00");
    final BigDecimal amount1 = rate1;
    final BigDecimal rate2 = new BigDecimal("300.00");
    final BigDecimal amount2 = rate2;
    // Start with a ANNUAL plan (high rate, rate1)
    final InvoiceItem initial = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, endDate, amount1, rate1, currency);
    // Change to MONTHLY plan 10 days later (low rate, rate2)
    final InvoiceItem repair1 = new RepairAdjInvoiceItem(invoiceId, accountId, changeDate, endDate, amount1.negate(), currency, initial.getId());
    final InvoiceItem newItem1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, "someelse", "someelse", changeDate, monthlyAlignmentDate, amount2, rate2, currency);
    // On the same day, now revert back to ANNUAL
    final InvoiceItem repair2 = new RepairAdjInvoiceItem(invoiceId, accountId, changeDate, monthlyAlignmentDate, amount2.negate(), currency, newItem1.getId());
    final InvoiceItem newItem2 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, changeDate, endDate, amount1, rate1, currency);
    final SubscriptionItemTree tree = new SubscriptionItemTree(subscriptionId, invoiceId);
    tree.addItem(initial);
    tree.addItem(newItem1);
    tree.addItem(repair1);
    tree.addItem(newItem2);
    tree.addItem(repair2);
    final List<InvoiceItem> expectedResult = Lists.newLinkedList();
    final InvoiceItem expected1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, changeDate, new BigDecimal("65.75"), rate1, currency);
    expectedResult.add(expected1);
    expectedResult.add(newItem2);
    tree.build();
    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)

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