Search in sources :

Example 36 with AccountInvoices

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

the class TestDefaultInvoiceGenerator method testWithEmptyEventSet.

@Test(groups = "fast")
public void testWithEmptyEventSet() throws InvoiceApiException {
    final BillingEventSet events = new MockBillingEventSet();
    final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, new AccountInvoices(), null, clock.getUTCToday(), Currency.USD, null, internalCallContext);
    assertNull(invoiceWithMetadata.getInvoice());
}
Also used : AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) Test(org.testng.annotations.Test)

Example 37 with AccountInvoices

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

the class TestDefaultInvoiceGenerator method testRegressionFor170.

// Regression test for #170 (see https://github.com/killbill/killbill/pull/173)
@Test(groups = "fast")
public void testRegressionFor170() throws EntityPersistenceException, InvoiceApiException, CatalogApiException {
    final UUID accountId = account.getId();
    final Currency currency = Currency.USD;
    final SubscriptionBase subscription = createSubscription();
    final MockInternationalPrice recurringPrice = new MockInternationalPrice(new DefaultPrice(new BigDecimal("2.9500"), Currency.USD));
    final MockPlanPhase phase = new MockPlanPhase(recurringPrice, null);
    final Plan plan = new MockPlan(phase);
    final LocalDate targetDate = new LocalDate(2013, 10, 30);
    final Invoice existingInvoice = new DefaultInvoice(UUID.randomUUID(), accountId, null, clock.getUTCToday(), targetDate, currency, false, InvoiceStatus.COMMITTED);
    // Set the existing recurring invoice item 2013/06/15 - 2013/07/15
    final LocalDate startDate = new LocalDate(2013, 06, 15);
    final LocalDate endDate = new LocalDate(2013, 07, 15);
    final InvoiceItem recurringInvoiceItem = new RecurringInvoiceItem(existingInvoice.getId(), accountId, subscription.getBundleId(), subscription.getId(), null, plan.getName(), phase.getName(), null, startDate, endDate, recurringPrice.getPrice(currency), recurringPrice.getPrice(currency), Currency.USD);
    existingInvoice.addInvoiceItem(recurringInvoiceItem);
    // Set an existing repair item
    final LocalDate repairStartDate = new LocalDate(2013, 06, 21);
    final LocalDate repairEndDate = new LocalDate(2013, 06, 26);
    final BigDecimal repairAmount = new BigDecimal("0.4900").negate();
    final InvoiceItem repairItem = new RepairAdjInvoiceItem(existingInvoice.getId(), accountId, repairStartDate, repairEndDate, repairAmount, currency, recurringInvoiceItem.getId());
    existingInvoice.addInvoiceItem(repairItem);
    // Create the billing event associated with the subscription creation
    // 
    // Note : this is the interesting part of the test; it does not provide the blocking billing events, which force invoice
    // to un repair what was previously repaired.
    final BillingEventSet events = new MockBillingEventSet();
    final BillingEvent event = invoiceUtil.createMockBillingEvent(null, subscription, new DateTime("2013-06-15", DateTimeZone.UTC), plan, phase, null, recurringPrice.getPrice(currency), currency, BillingPeriod.MONTHLY, 15, BillingMode.IN_ADVANCE, "testEvent", 1L, SubscriptionBaseTransitionType.CREATE);
    events.add(event);
    final List<Invoice> existingInvoices = new LinkedList<Invoice>();
    existingInvoices.add(existingInvoice);
    // Generate a new invoice
    final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, new AccountInvoices(null, null, existingInvoices), null, targetDate, currency, null, internalCallContext);
    final Invoice invoice = invoiceWithMetadata.getInvoice();
    assertEquals(invoice.getNumberOfItems(), 5);
    assertEquals(invoice.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.RECURRING);
    assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), new LocalDate(2013, 6, 21));
    assertEquals(invoice.getInvoiceItems().get(0).getEndDate(), new LocalDate(2013, 6, 26));
    assertTrue(repairAmount.negate().compareTo(invoice.getInvoiceItems().get(0).getAmount()) == 0);
    assertEquals(invoice.getInvoiceItems().get(1).getInvoiceItemType(), InvoiceItemType.RECURRING);
    assertEquals(invoice.getInvoiceItems().get(1).getStartDate(), new LocalDate(2013, 7, 15));
    assertEquals(invoice.getInvoiceItems().get(1).getEndDate(), new LocalDate(2013, 8, 15));
    assertEquals(invoice.getInvoiceItems().get(2).getInvoiceItemType(), InvoiceItemType.RECURRING);
    assertEquals(invoice.getInvoiceItems().get(2).getStartDate(), new LocalDate(2013, 8, 15));
    assertEquals(invoice.getInvoiceItems().get(2).getEndDate(), new LocalDate(2013, 9, 15));
    assertEquals(invoice.getInvoiceItems().get(3).getInvoiceItemType(), InvoiceItemType.RECURRING);
    assertEquals(invoice.getInvoiceItems().get(3).getStartDate(), new LocalDate(2013, 9, 15));
    assertEquals(invoice.getInvoiceItems().get(3).getEndDate(), new LocalDate(2013, 10, 15));
    assertEquals(invoice.getInvoiceItems().get(4).getInvoiceItemType(), InvoiceItemType.RECURRING);
    assertEquals(invoice.getInvoiceItems().get(4).getStartDate(), new LocalDate(2013, 10, 15));
    assertEquals(invoice.getInvoiceItems().get(4).getEndDate(), new LocalDate(2013, 11, 15));
    // Add newly generated invoice to existing invoices
    existingInvoices.add(invoice);
    // Generate next invoice (no-op)
    final InvoiceWithMetadata newInvoiceWithMetdata = generator.generateInvoice(account, events, new AccountInvoices(null, null, existingInvoices), null, targetDate, currency, null, internalCallContext);
    final Invoice newInvoice = newInvoiceWithMetdata.getInvoice();
    assertNull(newInvoice);
}
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) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) MockInternationalPrice(org.killbill.billing.catalog.MockInternationalPrice) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) LinkedList(java.util.LinkedList) 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) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) Test(org.testng.annotations.Test)

Example 38 with AccountInvoices

use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices 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, new AccountInvoices(), null, targetDate, Currency.USD, null, 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(), plan, 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, new AccountInvoices(null, null, existingInvoices), null, targetDate, Currency.USD, null, internalCallContext);
    final Invoice invoice2 = invoiceWithMetadata2.getInvoice();
    assertNull(invoice2);
}
Also used : AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) 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 39 with AccountInvoices

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

the class TestDefaultInvoiceGenerator method testWithSingleMonthlyEvent.

@Test(groups = "fast")
public void testWithSingleMonthlyEvent() throws InvoiceApiException, CatalogApiException {
    final BillingEventSet events = new MockBillingEventSet();
    final SubscriptionBase sub = createSubscription();
    final LocalDate startDate = invoiceUtil.buildDate(2011, 9, 1);
    final Plan plan = new MockPlan();
    final BigDecimal rate1 = TEN;
    final PlanPhase phase = createMockMonthlyPlanPhase(rate1);
    final BillingEvent event = createBillingEvent(sub.getId(), sub.getBundleId(), startDate, plan, phase, 1);
    events.add(event);
    final LocalDate targetDate = invoiceUtil.buildDate(2011, 10, 3);
    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.getBalance(), KillBillMoney.of(TWENTY, invoice.getCurrency()));
    assertEquals(invoice.getInvoiceItems().get(0).getSubscriptionId(), sub.getId());
    assertEquals(invoice.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.RECURRING);
    assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), new LocalDate(2011, 9, 1));
    assertEquals(invoice.getInvoiceItems().get(0).getEndDate(), new LocalDate(2011, 10, 1));
    assertEquals(invoice.getInvoiceItems().get(1).getInvoiceItemType(), InvoiceItemType.RECURRING);
    assertEquals(invoice.getInvoiceItems().get(1).getStartDate(), new LocalDate(2011, 10, 1));
    assertEquals(invoice.getInvoiceItems().get(1).getEndDate(), new LocalDate(2011, 11, 1));
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) 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) 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) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 40 with AccountInvoices

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

the class TestDefaultInvoiceGenerator method testWithNullEventSetAndNullInvoiceSet.

@Test(groups = "fast")
public void testWithNullEventSetAndNullInvoiceSet() throws InvoiceApiException {
    final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, null, new AccountInvoices(), null, clock.getUTCToday(), Currency.USD, null, internalCallContext);
    assertNull(invoiceWithMetadata.getInvoice());
}
Also used : AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) 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