Search in sources :

Example 46 with AccountInvoices

use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices 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(), null, plan.getName(), planPhase.getName(), null, 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, 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)

Example 47 with AccountInvoices

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

the class TestInvoiceDao method testMixedModeInvoicePersistence.

@Test(groups = "slow")
public void testMixedModeInvoicePersistence() throws InvoiceApiException, CatalogApiException, EntityPersistenceException {
    final Currency currency = Currency.USD;
    final DefaultPrice zeroPrice = new DefaultPrice(BigDecimal.ZERO, Currency.USD);
    final MockInternationalPrice fixedPrice = new MockInternationalPrice(zeroPrice);
    final MockPlanPhase phase1 = new MockPlanPhase(null, fixedPrice);
    final BigDecimal cheapAmount = new BigDecimal("24.95");
    final DefaultPrice cheapPrice = new DefaultPrice(cheapAmount, Currency.USD);
    final MockInternationalPrice recurringPrice = new MockInternationalPrice(cheapPrice);
    final MockPlanPhase phase2 = new MockPlanPhase(recurringPrice, null);
    final MockPlan plan = new MockPlan();
    final SubscriptionBase subscription = getZombieSubscription();
    final DateTime effectiveDate1 = invoiceUtil.buildDate(2011, 1, 1).toDateTimeAtStartOfDay();
    final BillingEvent event1 = invoiceUtil.createMockBillingEvent(null, subscription, effectiveDate1, plan, phase1, fixedPrice.getPrice(currency), null, currency, BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "testEvent1", 1L, SubscriptionBaseTransitionType.CREATE);
    final BillingEventSet events = new MockBillingEventSet();
    events.add(event1);
    final DateTime effectiveDate2 = effectiveDate1.plusDays(30);
    final BillingEvent event2 = invoiceUtil.createMockBillingEvent(null, subscription, effectiveDate2, plan, phase2, null, recurringPrice.getPrice(currency), currency, BillingPeriod.MONTHLY, 31, BillingMode.IN_ADVANCE, "testEvent2", 2L, SubscriptionBaseTransitionType.CHANGE);
    events.add(event2);
    final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, new AccountInvoices(), null, new LocalDate(effectiveDate2), Currency.USD, null, context);
    final Invoice invoice = invoiceWithMetadata.getInvoice();
    assertNotNull(invoice);
    assertEquals(invoice.getNumberOfItems(), 2);
    assertEquals(invoice.getBalance().compareTo(cheapAmount), 0);
    invoiceUtil.createInvoice(invoice, context);
    final InvoiceModelDao savedInvoice = invoiceDao.getById(invoice.getId(), context);
    assertNotNull(savedInvoice);
    assertEquals(savedInvoice.getInvoiceItems().size(), 2);
    assertEquals(InvoiceModelDaoHelper.getRawBalanceForRegularInvoice(savedInvoice).compareTo(cheapAmount), 0);
}
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) InvoiceWithMetadata(org.killbill.billing.invoice.generator.InvoiceWithMetadata) LocalDate(org.joda.time.LocalDate) MockInternationalPrice(org.killbill.billing.catalog.MockInternationalPrice) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) MockPlan(org.killbill.billing.catalog.MockPlan) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) Currency(org.killbill.billing.catalog.api.Currency) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultPrice(org.killbill.billing.catalog.DefaultPrice) Test(org.testng.annotations.Test)

Example 48 with AccountInvoices

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

the class TestInvoiceDao method testInvoiceGenerationForImmediateChanges.

/*
     *
     * this test verifies that immediate changes give the correct results
     *
     */
@Test(groups = "slow")
public void testInvoiceGenerationForImmediateChanges() throws InvoiceApiException, CatalogApiException, EntityPersistenceException {
    final UUID accountId = account.getId();
    final List<Invoice> invoiceList = new ArrayList<Invoice>();
    final LocalDate targetDate = new LocalDate(2011, 2, 16);
    final Currency currency = Currency.USD;
    // generate first invoice
    final DefaultPrice price1 = new DefaultPrice(TEN, Currency.USD);
    final MockInternationalPrice recurringPrice = new MockInternationalPrice(price1);
    final MockPlanPhase phase1 = new MockPlanPhase(recurringPrice, null, BillingPeriod.MONTHLY, PhaseType.TRIAL);
    final MockPlan plan1 = new MockPlan(phase1);
    final SubscriptionBase subscription = getZombieSubscription();
    final DateTime effectiveDate1 = new DateTime(2011, 2, 1, 0, 0, 0);
    final BillingEvent event1 = invoiceUtil.createMockBillingEvent(null, subscription, effectiveDate1, plan1, phase1, null, recurringPrice.getPrice(currency), currency, BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "testEvent1", 1L, SubscriptionBaseTransitionType.CREATE);
    final BillingEventSet events = new MockBillingEventSet();
    events.add(event1);
    final InvoiceWithMetadata invoiceWithMetadata1 = generator.generateInvoice(account, events, new AccountInvoices(null, null, invoiceList), null, targetDate, Currency.USD, null, context);
    final Invoice invoice1 = invoiceWithMetadata1.getInvoice();
    assertEquals(invoice1.getBalance(), KillBillMoney.of(TEN, invoice1.getCurrency()));
    invoiceList.add(invoice1);
    // generate second invoice
    final DefaultPrice price2 = new DefaultPrice(TWENTY, Currency.USD);
    final MockInternationalPrice recurringPrice2 = new MockInternationalPrice(price2);
    final MockPlanPhase phase2 = new MockPlanPhase(recurringPrice, null, BillingPeriod.MONTHLY, PhaseType.TRIAL);
    final MockPlan plan2 = new MockPlan(phase2);
    final DateTime effectiveDate2 = new DateTime(2011, 2, 15, 0, 0, 0);
    final BillingEvent event2 = invoiceUtil.createMockBillingEvent(null, subscription, effectiveDate2, plan2, phase2, null, recurringPrice2.getPrice(currency), currency, BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "testEvent2", 2L, SubscriptionBaseTransitionType.CREATE);
    events.add(event2);
    // second invoice should be for one half (14/28 days) the difference between the rate plans
    // this is a temporary state, since it actually contains an adjusting item that properly belong to invoice 1
    final InvoiceWithMetadata invoiceWithMetadata2 = generator.generateInvoice(account, events, new AccountInvoices(null, null, invoiceList), null, targetDate, Currency.USD, null, context);
    final Invoice invoice2 = invoiceWithMetadata2.getInvoice();
    assertEquals(invoice2.getBalance(), KillBillMoney.of(FIVE, invoice2.getCurrency()));
    invoiceList.add(invoice2);
    invoiceUtil.createInvoice(invoice1, context);
    invoiceUtil.createInvoice(invoice2, context);
    final InvoiceModelDao savedInvoice1 = invoiceDao.getById(invoice1.getId(), context);
    assertEquals(InvoiceModelDaoHelper.getRawBalanceForRegularInvoice(savedInvoice1), KillBillMoney.of(TEN, savedInvoice1.getCurrency()));
    final InvoiceModelDao savedInvoice2 = invoiceDao.getById(invoice2.getId(), context);
    assertEquals(InvoiceModelDaoHelper.getRawBalanceForRegularInvoice(savedInvoice2), KillBillMoney.of(FIVE, savedInvoice2.getCurrency()));
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) ArrayList(java.util.ArrayList) InvoiceWithMetadata(org.killbill.billing.invoice.generator.InvoiceWithMetadata) 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) Currency(org.killbill.billing.catalog.api.Currency) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) BillingEvent(org.killbill.billing.junction.BillingEvent) UUID(java.util.UUID) DefaultPrice(org.killbill.billing.catalog.DefaultPrice) Test(org.testng.annotations.Test)

Example 49 with AccountInvoices

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

the class TestInvoiceDao method testInvoiceForEmptyEventSet.

@Test(groups = "slow")
public void testInvoiceForEmptyEventSet() throws InvoiceApiException {
    final BillingEventSet events = new MockBillingEventSet();
    final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, new AccountInvoices(), null, new LocalDate(), Currency.USD, null, context);
    final Invoice invoice = invoiceWithMetadata.getInvoice();
    assertNull(invoice);
}
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) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) InvoiceWithMetadata(org.killbill.billing.invoice.generator.InvoiceWithMetadata) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 50 with AccountInvoices

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

the class TestInvoiceDao method testInvoiceNumber.

@Test(groups = "slow")
public void testInvoiceNumber() throws InvoiceApiException, EntityPersistenceException {
    final Currency currency = Currency.USD;
    final DateTime targetDate1 = clock.getUTCNow().plusMonths(1);
    final DateTime targetDate2 = clock.getUTCNow().plusMonths(2);
    final SubscriptionBase subscription = getZombieSubscription();
    final Product product = Mockito.mock(Product.class);
    Mockito.when(product.getName()).thenReturn("product");
    final Plan plan = Mockito.mock(Plan.class);
    Mockito.when(plan.getName()).thenReturn("plan");
    Mockito.when(plan.getProduct()).thenReturn(product);
    Mockito.when(plan.getRecurringBillingMode()).thenReturn(BillingMode.IN_ADVANCE);
    final PlanPhase phase1 = Mockito.mock(PlanPhase.class);
    Mockito.when(phase1.getName()).thenReturn("plan-phase1");
    final PlanPhase phase2 = Mockito.mock(PlanPhase.class);
    Mockito.when(phase2.getName()).thenReturn("plan-phase2");
    final BillingEventSet events = new MockBillingEventSet();
    final List<Invoice> invoices = new ArrayList<Invoice>();
    final BillingEvent event1 = invoiceUtil.createMockBillingEvent(null, subscription, targetDate1, plan, phase1, null, TEN, currency, BillingPeriod.MONTHLY, 31, BillingMode.IN_ADVANCE, "testEvent1", 1L, SubscriptionBaseTransitionType.CHANGE);
    events.add(event1);
    InvoiceWithMetadata invoiceWithMetadata1 = generator.generateInvoice(account, events, new AccountInvoices(null, null, invoices), null, new LocalDate(targetDate1), Currency.USD, null, context);
    Invoice invoice1 = invoiceWithMetadata1.getInvoice();
    invoices.add(invoice1);
    invoiceUtil.createInvoice(invoice1, context);
    invoice1 = new DefaultInvoice(invoiceDao.getById(invoice1.getId(), context));
    assertNotNull(invoice1.getInvoiceNumber());
    final BillingEvent event2 = invoiceUtil.createMockBillingEvent(null, subscription, targetDate1, plan, phase2, null, TWENTY, currency, BillingPeriod.MONTHLY, 31, BillingMode.IN_ADVANCE, "testEvent2", 2L, SubscriptionBaseTransitionType.CHANGE);
    events.add(event2);
    InvoiceWithMetadata invoiceWithMetadata2 = generator.generateInvoice(account, events, new AccountInvoices(null, null, invoices), null, new LocalDate(targetDate2), Currency.USD, null, context);
    Invoice invoice2 = invoiceWithMetadata2.getInvoice();
    invoiceUtil.createInvoice(invoice2, context);
    invoice2 = new DefaultInvoice(invoiceDao.getById(invoice2.getId(), context));
    assertNotNull(invoice2.getInvoiceNumber());
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) ArrayList(java.util.ArrayList) Product(org.killbill.billing.catalog.api.Product) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) InvoiceWithMetadata(org.killbill.billing.invoice.generator.InvoiceWithMetadata) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) Currency(org.killbill.billing.catalog.api.Currency) 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) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) 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