Search in sources :

Example 16 with AccountInvoices

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

the class TestDefaultInvoiceGenerator method testRepairForPaidInvoice.

@Test(groups = "fast")
public void testRepairForPaidInvoice() throws CatalogApiException, InvoiceApiException {
    // create an invoice
    final LocalDate april25 = new LocalDate(2012, 4, 25);
    // create a base plan on April 25th
    final SubscriptionBase originalSubscription = createSubscription();
    final Plan originalPlan = new MockPlan("original plan");
    final MockInternationalPrice price10 = new MockInternationalPrice(new DefaultPrice(TEN, Currency.USD));
    final PlanPhase originalPlanEvergreen = new MockPlanPhase(price10, null, BillingPeriod.MONTHLY, PhaseType.EVERGREEN);
    final BillingEventSet events = new MockBillingEventSet();
    events.add(createBillingEvent(originalSubscription.getId(), originalSubscription.getBundleId(), april25, originalPlan, originalPlanEvergreen, 25));
    final InvoiceWithMetadata invoiceWithMetadata1 = generator.generateInvoice(account, events, new AccountInvoices(), null, april25, Currency.USD, null, internalCallContext);
    final Invoice invoice1 = invoiceWithMetadata1.getInvoice();
    printDetailInvoice(invoice1);
    assertEquals(invoice1.getNumberOfItems(), 1);
    final List<Invoice> invoices = new ArrayList<Invoice>();
    invoices.add(invoice1);
    // pay the invoice
    invoice1.addPayment(new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, UUID.randomUUID(), invoice1.getId(), april25.toDateTimeAtCurrentTime(), TEN, Currency.USD, Currency.USD, null, true));
    assertEquals(invoice1.getBalance().compareTo(ZERO), 0);
    // change the plan (i.e. repair) on start date
    events.clear();
    final SubscriptionBase newSubscription = createSubscription();
    final Plan newPlan = new MockPlan("new plan");
    final MockInternationalPrice price5 = new MockInternationalPrice(new DefaultPrice(FIVE, Currency.USD));
    final PlanPhase newPlanEvergreen = new MockPlanPhase(price5, null, BillingPeriod.MONTHLY, PhaseType.EVERGREEN);
    events.add(createBillingEvent(newSubscription.getId(), originalSubscription.getBundleId(), april25, newPlan, newPlanEvergreen, 25));
    // generate a new invoice
    final InvoiceWithMetadata invoiceWithMetadata2 = generator.generateInvoice(account, events, new AccountInvoices(null, null, invoices), null, april25, Currency.USD, null, internalCallContext);
    final Invoice invoice2 = invoiceWithMetadata2.getInvoice();
    printDetailInvoice(invoice2);
    assertEquals(invoice2.getNumberOfItems(), 2);
    invoices.add(invoice2);
    // move items to the correct invoice (normally, the dao calls will sort that out)
    distributeItems(invoices);
    // ensure that the original invoice balance is zero
    assertEquals(invoice1.getBalance().compareTo(BigDecimal.ZERO), 0);
    // ensure that the account balance is correct
    assertEquals(invoice2.getBalance().compareTo(new BigDecimal("-5.0")), 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) ArrayList(java.util.ArrayList) 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) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) 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) DefaultPrice(org.killbill.billing.catalog.DefaultPrice) Test(org.testng.annotations.Test)

Example 17 with AccountInvoices

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

the class TestDefaultInvoiceGenerator method testTargetDateRestrictionFailure.

@Test(groups = "fast", expectedExceptions = { InvoiceApiException.class })
public void testTargetDateRestrictionFailure() throws InvoiceApiException, CatalogApiException {
    final LocalDate targetDate = clock.getUTCToday().plusMonths(60);
    final BillingEventSet events = new MockBillingEventSet();
    final Plan plan1 = new MockPlan();
    final PlanPhase phase1 = createMockMonthlyPlanPhase(null, ZERO, PhaseType.TRIAL);
    events.add(createBillingEvent(UUID.randomUUID(), UUID.randomUUID(), clock.getUTCToday(), plan1, phase1, 1));
    generator.generateInvoice(account, events, new AccountInvoices(), null, targetDate, Currency.USD, null, internalCallContext);
}
Also used : AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) 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) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 18 with AccountInvoices

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

the class TestDefaultInvoiceGenerator method testEndDateIsCorrect.

@Test(groups = "fast")
public void testEndDateIsCorrect() throws InvoiceApiException, CatalogApiException {
    final Plan plan = new MockPlan();
    final PlanPhase planPhase = createMockMonthlyPlanPhase(ONE);
    final BillingEventSet events = new MockBillingEventSet();
    final LocalDate startDate = clock.getUTCToday().minusDays(1);
    final LocalDate targetDate = startDate.plusDays(1);
    events.add(createBillingEvent(UUID.randomUUID(), UUID.randomUUID(), startDate, plan, planPhase, startDate.getDayOfMonth()));
    final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, new AccountInvoices(), null, targetDate, Currency.USD, null, internalCallContext);
    final Invoice invoice = invoiceWithMetadata.getInvoice();
    final RecurringInvoiceItem item = (RecurringInvoiceItem) invoice.getInvoiceItems().get(0);
    // end date of the invoice item should be equal to exactly one month later (rounded)
    assertEquals(item.getEndDate(), startDate.plusMonths(1));
}
Also used : AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) MockPlan(org.killbill.billing.catalog.MockPlan) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) 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) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 19 with AccountInvoices

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

the class TestDefaultInvoiceGenerator method testWithFullRepairInvoiceGeneration.

@Test(groups = "fast")
public void testWithFullRepairInvoiceGeneration() throws CatalogApiException, InvoiceApiException {
    final LocalDate april25 = new LocalDate(2012, 4, 25);
    // create a base plan on April 25th
    final SubscriptionBase baseSubscription = createSubscription();
    final Plan basePlan = new MockPlan("base Plan");
    final MockInternationalPrice price5 = new MockInternationalPrice(new DefaultPrice(FIVE, Currency.USD));
    final MockInternationalPrice price10 = new MockInternationalPrice(new DefaultPrice(TEN, Currency.USD));
    final MockInternationalPrice price20 = new MockInternationalPrice(new DefaultPrice(TWENTY, Currency.USD));
    final PlanPhase basePlanEvergreen = new MockPlanPhase(price10, null, BillingPeriod.MONTHLY, PhaseType.EVERGREEN);
    final BillingEventSet events = new MockBillingEventSet();
    events.add(createBillingEvent(baseSubscription.getId(), baseSubscription.getBundleId(), april25, basePlan, basePlanEvergreen, 25));
    // generate invoice
    final InvoiceWithMetadata invoiceWithMetadata1 = generator.generateInvoice(account, events, new AccountInvoices(), null, april25, Currency.USD, null, internalCallContext);
    final Invoice invoice1 = invoiceWithMetadata1.getInvoice();
    assertNotNull(invoice1);
    assertEquals(invoice1.getNumberOfItems(), 1);
    assertEquals(invoice1.getBalance().compareTo(TEN), 0);
    final List<Invoice> invoices = new ArrayList<Invoice>();
    invoices.add(invoice1);
    // create 2 add ons on April 28th
    final LocalDate april28 = new LocalDate(2012, 4, 28);
    final SubscriptionBase addOnSubscription1 = createSubscription();
    final Plan addOn1Plan = new MockPlan("add on 1");
    final PlanPhase addOn1PlanPhaseEvergreen = new MockPlanPhase(price5, null, BillingPeriod.MONTHLY, PhaseType.EVERGREEN);
    events.add(createBillingEvent(addOnSubscription1.getId(), baseSubscription.getBundleId(), april28, addOn1Plan, addOn1PlanPhaseEvergreen, 25));
    final SubscriptionBase addOnSubscription2 = createSubscription();
    final Plan addOn2Plan = new MockPlan("add on 2");
    final PlanPhase addOn2PlanPhaseEvergreen = new MockPlanPhase(price20, null, BillingPeriod.MONTHLY, PhaseType.EVERGREEN);
    events.add(createBillingEvent(addOnSubscription2.getId(), baseSubscription.getBundleId(), april28, addOn2Plan, addOn2PlanPhaseEvergreen, 25));
    // generate invoice
    final InvoiceWithMetadata invoiceWithMetadata2 = generator.generateInvoice(account, events, new AccountInvoices(null, null, invoices), null, april28, Currency.USD, null, internalCallContext);
    final Invoice invoice2 = invoiceWithMetadata2.getInvoice();
    invoices.add(invoice2);
    assertNotNull(invoice2);
    assertEquals(invoice2.getNumberOfItems(), 2);
    assertEquals(invoice2.getBalance().compareTo(KillBillMoney.of(TWENTY_FIVE.multiply(new BigDecimal("0.9")), invoice2.getCurrency())), 0);
    // perform a repair (change base plan; remove one add-on)
    // event stream should include just two plans
    final MockBillingEventSet newEvents = new MockBillingEventSet();
    final Plan basePlan2 = new MockPlan("base plan 2");
    final MockInternationalPrice price13 = new MockInternationalPrice(new DefaultPrice(THIRTEEN, Currency.USD));
    final PlanPhase basePlan2Phase = new MockPlanPhase(price13, null, BillingPeriod.MONTHLY, PhaseType.EVERGREEN);
    newEvents.add(createBillingEvent(baseSubscription.getId(), baseSubscription.getBundleId(), april25, basePlan2, basePlan2Phase, 25));
    newEvents.add(createBillingEvent(addOnSubscription1.getId(), baseSubscription.getBundleId(), april28, addOn1Plan, addOn1PlanPhaseEvergreen, 25));
    // generate invoice
    final LocalDate may1 = new LocalDate(2012, 5, 1);
    final InvoiceWithMetadata invoiceWithMetadata3 = generator.generateInvoice(account, newEvents, new AccountInvoices(null, null, invoices), null, may1, Currency.USD, null, internalCallContext);
    final Invoice invoice3 = invoiceWithMetadata3.getInvoice();
    assertNotNull(invoice3);
    assertEquals(invoice3.getNumberOfItems(), 3);
    // -4.50 -18 - 10 (to correct the previous 2 invoices) + 4.50 + 13
    assertEquals(invoice3.getBalance().compareTo(FIFTEEN.negate()), 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) ArrayList(java.util.ArrayList) 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) 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) DefaultPrice(org.killbill.billing.catalog.DefaultPrice) Test(org.testng.annotations.Test)

Example 20 with AccountInvoices

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

the class TestDefaultInvoiceGenerator method testTwoMonthlySubscriptionsWithAlignedBillingDates.

@Test(groups = "fast")
public void testTwoMonthlySubscriptionsWithAlignedBillingDates() throws InvoiceApiException, CatalogApiException {
    final BillingEventSet events = new MockBillingEventSet();
    final Plan plan1 = new MockPlan();
    final BigDecimal rate1 = FIVE;
    final PlanPhase phase1 = createMockMonthlyPlanPhase(rate1);
    final Plan plan2 = new MockPlan();
    final BigDecimal rate2 = TEN;
    final PlanPhase phase2 = createMockMonthlyPlanPhase(rate2);
    final SubscriptionBase sub = createSubscription();
    final BillingEvent event1 = createBillingEvent(sub.getId(), sub.getBundleId(), invoiceUtil.buildDate(2011, 9, 1), plan1, phase1, 1);
    events.add(event1);
    final BillingEvent event2 = createBillingEvent(sub.getId(), sub.getBundleId(), invoiceUtil.buildDate(2011, 10, 1), plan2, phase2, 1);
    events.add(event2);
    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(rate1.add(rate2), invoice.getCurrency()));
}
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)

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