Search in sources :

Example 11 with ItemAdjInvoiceItem

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

the class TestDefaultInvoiceGenerator method testCancelEOTWithFullItemAdjustment.

@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/654")
public void testCancelEOTWithFullItemAdjustment() throws CatalogApiException, InvoiceApiException {
    final BigDecimal rate = new BigDecimal("39.95");
    final BillingEventSet events = new MockBillingEventSet();
    final SubscriptionBase sub = createSubscription();
    final LocalDate startDate = invoiceUtil.buildDate(2016, 10, 9);
    final LocalDate endDate = invoiceUtil.buildDate(2016, 11, 9);
    final Plan plan = new MockPlan();
    final PlanPhase phase = createMockMonthlyPlanPhase(rate);
    final BillingEvent event = createBillingEvent(sub.getId(), sub.getBundleId(), startDate, plan, phase, 9);
    events.add(event);
    final LocalDate targetDate = invoiceUtil.buildDate(2016, 10, 9);
    final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, targetDate, Currency.USD, internalCallContext);
    final Invoice invoice = invoiceWithMetadata.getInvoice();
    assertNotNull(invoice);
    assertEquals(invoice.getNumberOfItems(), 1);
    assertEquals(invoice.getBalance(), KillBillMoney.of(rate, invoice.getCurrency()));
    assertEquals(invoice.getInvoiceItems().get(0).getSubscriptionId(), sub.getId());
    assertEquals(invoice.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.RECURRING);
    assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), startDate);
    assertEquals(invoice.getInvoiceItems().get(0).getEndDate(), endDate);
    // Cancel EOT and Add the item adjustment
    final BillingEvent event2 = invoiceUtil.createMockBillingEvent(account, sub, endDate.toDateTimeAtStartOfDay(), null, phase, ZERO, null, Currency.USD, BillingPeriod.NO_BILLING_PERIOD, 9, BillingMode.IN_ADVANCE, "Cancel", 2L, SubscriptionBaseTransitionType.CANCEL);
    events.add(event2);
    final InvoiceItem itemAdj = new ItemAdjInvoiceItem(invoice.getInvoiceItems().get(0), new LocalDate(2016, 10, 12), rate.negate(), Currency.USD);
    invoice.addInvoiceItem(itemAdj);
    final List<Invoice> existingInvoices = new ArrayList<Invoice>();
    existingInvoices.add(invoice);
    final InvoiceWithMetadata invoiceWithMetadata2 = generator.generateInvoice(account, events, existingInvoices, targetDate, Currency.USD, internalCallContext);
    final Invoice invoice2 = invoiceWithMetadata2.getInvoice();
    assertNull(invoice2);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) 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) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) ArrayList(java.util.ArrayList) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) MockPlan(org.killbill.billing.catalog.MockPlan) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) BillingEvent(org.killbill.billing.junction.BillingEvent) Test(org.testng.annotations.Test)

Example 12 with ItemAdjInvoiceItem

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

the class TestFixedAndRecurringInvoiceItemGenerator method testItemPartiallyRepairedAndPartiallyAdjusted.

@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testItemPartiallyRepairedAndPartiallyAdjusted() throws InvoiceApiException {
    final LocalDate startDate = new LocalDate("2016-01-01");
    final BillingEventSet events = new MockBillingEventSet();
    final BigDecimal amount = BigDecimal.TEN;
    final MockInternationalPrice price = new MockInternationalPrice(new DefaultPrice(amount, account.getCurrency()));
    final Plan plan = new MockPlan("my-plan");
    final PlanPhase planPhase = new MockPlanPhase(price, null, BillingPeriod.MONTHLY, PhaseType.EVERGREEN);
    final BillingEvent event1 = invoiceUtil.createMockBillingEvent(account, subscription, startDate.toDateTimeAtStartOfDay(), plan, planPhase, null, amount, account.getCurrency(), BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 1L, SubscriptionBaseTransitionType.CREATE);
    events.add(event1);
    final BillingEvent event2 = invoiceUtil.createMockBillingEvent(account, subscription, startDate.plusDays(1).toDateTimeAtStartOfDay(), plan, planPhase, null, null, Currency.USD, BillingPeriod.NO_BILLING_PERIOD, 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 2L, SubscriptionBaseTransitionType.CANCEL);
    events.add(event2);
    // Subscription incorrectly invoiced
    final List<Invoice> existingInvoices = new LinkedList<Invoice>();
    final Invoice invoice = new DefaultInvoice(account.getId(), clock.getUTCToday(), startDate, account.getCurrency());
    invoice.addInvoiceItem(new RecurringInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), plan.getName(), planPhase.getName(), startDate, startDate.plusMonths(1), amount, amount, account.getCurrency()));
    // Repaired by the system
    invoice.addInvoiceItem(new RepairAdjInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), startDate.plusDays(1), startDate.plusMonths(1), new BigDecimal("9.68").negate(), account.getCurrency(), invoice.getInvoiceItems().get(0).getId()));
    // Item adjust the remaining
    invoice.addInvoiceItem(new ItemAdjInvoiceItem(invoice.getInvoiceItems().get(0), startDate, new BigDecimal("0.32").negate(), account.getCurrency()));
    existingInvoices.add(invoice);
    final List<InvoiceItem> generatedItems = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices, startDate, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), internalCallContext);
    assertTrue(generatedItems.isEmpty());
}
Also used : SubscriptionFutureNotificationDates(org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) 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) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) MockInternationalPrice(org.killbill.billing.catalog.MockInternationalPrice) LinkedList(java.util.LinkedList) MockPlan(org.killbill.billing.catalog.MockPlan) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultPrice(org.killbill.billing.catalog.DefaultPrice) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) Test(org.testng.annotations.Test)

Example 13 with ItemAdjInvoiceItem

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

the class TestSubscriptionItemTree method testMaxedOutProRation.

@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/286")
public void testMaxedOutProRation() throws IOException {
    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);
    // Fully item adjust the recurring item
    final InvoiceItem existingItemAdj1 = new ItemAdjInvoiceItem(existing1, startDate, monthlyRate1.negate(), currency);
    tree.addItem(existingItemAdj1);
    tree.flatten(true);
    //printTree(tree);
    final InvoiceItem proposed1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, cancelDate, monthlyAmount1, monthlyRate1, currency);
    tree.mergeProposedItem(proposed1);
    tree.buildForMerge();
    //printTree(tree);
    // We expect the proposed item because item adjustments don't change the subscription view of invoice
    final List<InvoiceItem> expectedResult = ImmutableList.<InvoiceItem>of(proposed1);
    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) Test(org.testng.annotations.Test)

Example 14 with ItemAdjInvoiceItem

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

the class TestSubscriptionItemTree method testWithWrongInitialItemInLoop.

@Test(groups = "fast")
public void testWithWrongInitialItemInLoop() {
    final LocalDate wrongStartDate = new LocalDate(2016, 9, 9);
    final LocalDate correctStartDate = new LocalDate(2016, 9, 8);
    final LocalDate endDate = new LocalDate(2016, 10, 8);
    final BigDecimal rate1 = new BigDecimal("12.00");
    final BigDecimal amount1 = rate1;
    final List<InvoiceItem> existingItems = new ArrayList<InvoiceItem>();
    final List<InvoiceItem> proposedItems = new ArrayList<InvoiceItem>();
    final InvoiceItem wrongInitialItem = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, wrongStartDate, endDate, amount1, rate1, currency);
    proposedItems.add(wrongInitialItem);
    int previousExistingSize = existingItems.size();
    int iteration = 0;
    do {
        SubscriptionItemTree tree = new SubscriptionItemTree(subscriptionId, invoiceId);
        for (InvoiceItem e : existingItems) {
            tree.addItem(e);
        }
        tree.build();
        tree.flatten(true);
        for (InvoiceItem p : proposedItems) {
            tree.mergeProposedItem(p);
        }
        tree.buildForMerge();
        existingItems.addAll(tree.getView());
        if (iteration == 0) {
            final InvoiceItem itemAdj = new ItemAdjInvoiceItem(wrongInitialItem, new LocalDate(2016, 10, 2), amount1.negate(), currency);
            existingItems.add(itemAdj);
        }
        previousExistingSize = existingItems.size();
        proposedItems.clear();
        final InvoiceItem correctInitialItem = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, correctStartDate, endDate, amount1, rate1, currency);
        proposedItems.add(correctInitialItem);
        iteration++;
    } while (iteration < 10);
    // We have repaired wrongInitialItem and generated the correctInitialItem and stopped
    Assert.assertEquals(previousExistingSize, 3);
}
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) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 15 with ItemAdjInvoiceItem

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

the class TestSubscriptionItemTree method testWithWrongInitialItem.

@Test(groups = "fast")
public void testWithWrongInitialItem() throws IOException {
    final LocalDate wrongStartDate = new LocalDate(2016, 9, 9);
    final LocalDate correctStartDate = new LocalDate(2016, 9, 8);
    final LocalDate endDate = new LocalDate(2016, 10, 8);
    final BigDecimal rate = new BigDecimal("12.00");
    final BigDecimal amount = rate;
    final SubscriptionItemTree tree = new SubscriptionItemTree(subscriptionId, invoiceId);
    final InvoiceItem wrongInitialItem = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, wrongStartDate, endDate, amount, rate, currency);
    tree.addItem(wrongInitialItem);
    final InvoiceItem itemAdj = new ItemAdjInvoiceItem(wrongInitialItem, new LocalDate(2016, 10, 2), amount.negate(), currency);
    tree.addItem(itemAdj);
    final InvoiceItem correctInitialItem = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, correctStartDate, endDate, amount, rate, currency);
    tree.addItem(correctInitialItem);
    assertEquals(tree.getRoot().getStart(), correctStartDate);
    assertEquals(tree.getRoot().getEnd(), endDate);
    assertEquals(tree.getRoot().getLeftChild().getStart(), correctStartDate);
    assertEquals(tree.getRoot().getLeftChild().getEnd(), endDate);
    assertEquals(tree.getRoot().getLeftChild().getLeftChild().getStart(), wrongStartDate);
    assertEquals(tree.getRoot().getLeftChild().getLeftChild().getEnd(), endDate);
    assertNull(tree.getRoot().getLeftChild().getLeftChild().getLeftChild());
    assertNull(tree.getRoot().getLeftChild().getLeftChild().getRightSibling());
    assertNull(tree.getRoot().getLeftChild().getRightSibling());
    assertNull(tree.getRoot().getRightSibling());
    tree.flatten(true);
    assertEquals(tree.getRoot().getStart(), correctStartDate);
    assertEquals(tree.getRoot().getEnd(), endDate);
    assertEquals(tree.getRoot().getLeftChild().getStart(), correctStartDate);
    assertEquals(tree.getRoot().getLeftChild().getEnd(), endDate);
    assertNull(tree.getRoot().getLeftChild().getLeftChild());
    assertNull(tree.getRoot().getLeftChild().getRightSibling());
    assertNull(tree.getRoot().getRightSibling());
//printTree(tree);
}
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) Test(org.testng.annotations.Test)

Aggregations

ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)15 BigDecimal (java.math.BigDecimal)14 LocalDate (org.joda.time.LocalDate)13 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)13 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)13 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)13 RepairAdjInvoiceItem (org.killbill.billing.invoice.model.RepairAdjInvoiceItem)13 Test (org.testng.annotations.Test)13 UUID (java.util.UUID)7 Invoice (org.killbill.billing.invoice.api.Invoice)7 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)7 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)6 BillingEventSet (org.killbill.billing.junction.BillingEventSet)6 LinkedList (java.util.LinkedList)5 SubscriptionFutureNotificationDates (org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates)5 MockPlan (org.killbill.billing.catalog.MockPlan)4 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)4 Plan (org.killbill.billing.catalog.api.Plan)4 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)4 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)4