Search in sources :

Example 1 with AccountInvoices

use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices in project killbill by killbill.

the class TestDefaultInvoiceGenerator method testSimpleWithTimeZone.

@Test(groups = "fast")
public void testSimpleWithTimeZone() throws InvoiceApiException, CatalogApiException {
    final SubscriptionBase sub = createSubscription();
    final Plan plan = new MockPlan();
    final BigDecimal rate = TEN;
    final PlanPhase phase = createMockMonthlyPlanPhase(rate);
    // Start date was the 16 local, but was the 17 UTC
    final int bcdLocal = 16;
    final LocalDate startDate = invoiceUtil.buildDate(2012, 7, bcdLocal);
    final BillingEventSet events = new MockBillingEventSet();
    final BillingEvent event = createBillingEvent(sub.getId(), sub.getBundleId(), startDate, plan, phase, bcdLocal);
    events.add(event);
    // Target date is the next BCD, in local time
    final LocalDate targetDate = invoiceUtil.buildDate(2012, 8, bcdLocal);
    final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, new AccountInvoices(), null, targetDate, Currency.USD, null, internalCallContext);
    final Invoice invoice = invoiceWithMetadata.getInvoice();
    assertNotNull(invoice);
    assertEquals(invoice.getNumberOfItems(), 2);
    assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), invoiceUtil.buildDate(2012, 7, 16));
    assertEquals(invoice.getInvoiceItems().get(0).getEndDate(), invoiceUtil.buildDate(2012, 8, 16));
    assertEquals(invoice.getInvoiceItems().get(1).getStartDate(), invoiceUtil.buildDate(2012, 8, 16));
    assertEquals(invoice.getInvoiceItems().get(1).getEndDate(), invoiceUtil.buildDate(2012, 9, 16));
}
Also used : AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) 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) 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 2 with AccountInvoices

use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices in project killbill by killbill.

the class TestDefaultInvoiceGenerator method testFixedPriceLifeCycle.

@Test(groups = "fast")
public void testFixedPriceLifeCycle() throws InvoiceApiException {
    final SubscriptionBase subscription = createSubscription();
    final Plan plan = new MockPlan("plan 1");
    final MockInternationalPrice zeroPrice = new MockInternationalPrice(new DefaultPrice(ZERO, Currency.USD));
    final MockInternationalPrice cheapPrice = new MockInternationalPrice(new DefaultPrice(ONE, Currency.USD));
    final PlanPhase phase1 = new MockPlanPhase(null, zeroPrice, BillingPeriod.NO_BILLING_PERIOD, PhaseType.TRIAL);
    final PlanPhase phase2 = new MockPlanPhase(cheapPrice, null, BillingPeriod.MONTHLY, PhaseType.DISCOUNT);
    final DateTime changeDate = new DateTime("2012-04-1");
    final BillingEventSet events = new MockBillingEventSet();
    final BillingEvent event1 = invoiceUtil.createMockBillingEvent(null, subscription, new DateTime("2012-01-1"), plan, phase1, ZERO, null, Currency.USD, BillingPeriod.NO_BILLING_PERIOD, 1, BillingMode.IN_ADVANCE, "Test Event 1", 1L, SubscriptionBaseTransitionType.CREATE);
    final BillingEvent event2 = invoiceUtil.createMockBillingEvent(null, subscription, changeDate, plan, phase2, ZERO, null, Currency.USD, BillingPeriod.NO_BILLING_PERIOD, 1, BillingMode.IN_ADVANCE, "Test Event 2", 2L, SubscriptionBaseTransitionType.PHASE);
    events.add(event2);
    events.add(event1);
    final InvoiceWithMetadata invoiceWithMetadata1 = generator.generateInvoice(account, events, new AccountInvoices(), null, new LocalDate("2012-02-01"), Currency.USD, null, internalCallContext);
    final Invoice invoice1 = invoiceWithMetadata1.getInvoice();
    assertNotNull(invoice1);
    assertEquals(invoice1.getNumberOfItems(), 1);
    final List<Invoice> invoiceList = new ArrayList<Invoice>();
    invoiceList.add(invoice1);
    final InvoiceWithMetadata invoiceWithMetadata2 = generator.generateInvoice(account, events, new AccountInvoices(null, null, invoiceList), null, new LocalDate("2012-04-05"), Currency.USD, null, internalCallContext);
    final Invoice invoice2 = invoiceWithMetadata2.getInvoice();
    assertNotNull(invoice2);
    assertEquals(invoice2.getNumberOfItems(), 1);
    final FixedPriceInvoiceItem item = (FixedPriceInvoiceItem) invoice2.getInvoiceItems().get(0);
    assertEquals(item.getStartDate(), changeDate.toLocalDate());
}
Also used : AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) ArrayList(java.util.ArrayList) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) MockInternationalPrice(org.killbill.billing.catalog.MockInternationalPrice) DateTime(org.joda.time.DateTime) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) MockPlan(org.killbill.billing.catalog.MockPlan) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) 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) Test(org.testng.annotations.Test)

Example 3 with AccountInvoices

use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices in project killbill by killbill.

the class TestDefaultInvoiceGenerator method testMixedModeLifeCycle.

@Test(groups = "fast")
public void testMixedModeLifeCycle() throws InvoiceApiException, CatalogApiException {
    // create a SubscriptionBase with a fixed price and recurring price
    final Plan plan1 = new MockPlan();
    final BigDecimal monthlyRate = FIVE;
    final BigDecimal fixedCost = TEN;
    final PlanPhase phase1 = createMockMonthlyPlanPhase(monthlyRate, fixedCost, PhaseType.TRIAL);
    final BillingEventSet events = new MockBillingEventSet();
    final UUID subscriptionId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate startDate = new LocalDate(2011, 1, 1);
    final BillingEvent event1 = createBillingEvent(subscriptionId, bundleId, startDate, plan1, phase1, 1);
    events.add(event1);
    // ensure both components are invoiced
    final InvoiceWithMetadata invoiceWithMetadata1 = generator.generateInvoice(account, events, new AccountInvoices(), null, startDate, Currency.USD, null, internalCallContext);
    final Invoice invoice1 = invoiceWithMetadata1.getInvoice();
    assertNotNull(invoice1);
    assertEquals(invoice1.getNumberOfItems(), 2);
    assertEquals(invoice1.getBalance(), KillBillMoney.of(FIFTEEN, invoice1.getCurrency()));
    final List<Invoice> invoiceList = new ArrayList<Invoice>();
    invoiceList.add(invoice1);
    // move forward in time one billing period
    final LocalDate currentDate = startDate.plusMonths(1);
    // ensure that only the recurring price is invoiced
    final InvoiceWithMetadata invoiceWithMetadata2 = generator.generateInvoice(account, events, new AccountInvoices(null, null, invoiceList), null, currentDate, Currency.USD, null, internalCallContext);
    final Invoice invoice2 = invoiceWithMetadata2.getInvoice();
    assertNotNull(invoice2);
    assertEquals(invoice2.getNumberOfItems(), 1);
    assertEquals(invoice2.getBalance(), KillBillMoney.of(FIVE, invoice2.getCurrency()));
}
Also used : AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) 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) MockPlan(org.killbill.billing.catalog.MockPlan) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) BillingEvent(org.killbill.billing.junction.BillingEvent) UUID(java.util.UUID) Test(org.testng.annotations.Test)

Example 4 with AccountInvoices

use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices in project killbill by killbill.

the class TestFixedAndRecurringInvoiceItemGenerator method testSubscriptionAlreadyDoubleBilledForServicePeriod.

@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testSubscriptionAlreadyDoubleBilledForServicePeriod() 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 event = 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(event);
    // Simulate a bunch of recurring items for that subscription and service period (bad data on disk leading to double billing)
    final List<Invoice> existingInvoices = new LinkedList<Invoice>();
    for (int i = 0; i < 20; i++) {
        final Invoice invoice = new DefaultInvoice(account.getId(), clock.getUTCToday(), startDate.plusMonths(i), account.getCurrency());
        invoice.addInvoiceItem(new RecurringInvoiceItem(UUID.randomUUID(), // Set random dates to verify it doesn't impact double billing detection
        startDate.plusMonths(i).toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), null, event.getPlan().getName(), event.getPlanPhase().getName(), null, startDate, startDate.plusMonths(1), amount, amount, account.getCurrency()));
        existingInvoices.add(invoice);
    }
    try {
        // There will be one proposed item but the tree will refuse the merge because of the bad state on disk
        final List<InvoiceItem> generatedItems = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, new AccountInvoices(null, null, existingInvoices), startDate, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), null, internalCallContext).getItems();
        fail();
    } catch (final InvoiceApiException e) {
        assertEquals(e.getCode(), ErrorCode.UNEXPECTED_ERROR.getCode());
        assertTrue(e.getCause().getMessage().startsWith("Double billing detected"));
    }
}
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) AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) HashMap(java.util.HashMap) 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) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) MockPlan(org.killbill.billing.catalog.MockPlan) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) DefaultBillingEvent(org.killbill.billing.junction.plumbing.billing.DefaultBillingEvent) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultPrice(org.killbill.billing.catalog.DefaultPrice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Test(org.testng.annotations.Test)

Example 5 with AccountInvoices

use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices in project killbill by killbill.

the class TestFixedAndRecurringInvoiceItemGenerator method testItemPartiallyRepairedAndPartiallyAdjustedV2.

@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testItemPartiallyRepairedAndPartiallyAdjustedV2() 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(), null, plan.getName(), planPhase.getName(), null, startDate, startDate.plusMonths(1), amount, amount, account.getCurrency()));
    // Item adjust the remaining
    invoice.addInvoiceItem(new ItemAdjInvoiceItem(invoice.getInvoiceItems().get(0), startDate, BigDecimal.ONE.negate(), account.getCurrency()));
    // Repaired by the system (the system would have consumed all the remaining amount available)
    invoice.addInvoiceItem(new RepairAdjInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), startDate.plusDays(1), startDate.plusMonths(1), new BigDecimal("9").negate(), account.getCurrency(), invoice.getInvoiceItems().get(0).getId()));
    existingInvoices.add(invoice);
    final List<InvoiceItem> generatedItems = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, new AccountInvoices(null, null, existingInvoices), startDate, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), null, internalCallContext).getItems();
    assertTrue(generatedItems.isEmpty());
}
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) AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) HashMap(java.util.HashMap) 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) DefaultBillingEvent(org.killbill.billing.junction.plumbing.billing.DefaultBillingEvent) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultPrice(org.killbill.billing.catalog.DefaultPrice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) Test(org.testng.annotations.Test)

Aggregations

AccountInvoices (org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices)51 Test (org.testng.annotations.Test)48 LocalDate (org.joda.time.LocalDate)47 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)47 Invoice (org.killbill.billing.invoice.api.Invoice)47 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)47 BillingEventSet (org.killbill.billing.junction.BillingEventSet)46 MockPlan (org.killbill.billing.catalog.MockPlan)42 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)42 Plan (org.killbill.billing.catalog.api.Plan)37 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)37 BillingEvent (org.killbill.billing.junction.BillingEvent)35 BigDecimal (java.math.BigDecimal)34 DefaultPrice (org.killbill.billing.catalog.DefaultPrice)22 MockInternationalPrice (org.killbill.billing.catalog.MockInternationalPrice)22 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)21 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)20 RepairAdjInvoiceItem (org.killbill.billing.invoice.model.RepairAdjInvoiceItem)17 HashMap (java.util.HashMap)16 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)16