Search in sources :

Example 86 with BillingEvent

use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.

the class TestDefaultInvoiceGenerator method testFixedModePlanChange.

@Test(groups = "fast")
public void testFixedModePlanChange() throws InvoiceApiException, CatalogApiException {
    // create a SubscriptionBase with a fixed price and recurring price
    final Plan plan1 = new MockPlan();
    final BigDecimal fixedCost1 = TEN;
    final BigDecimal fixedCost2 = TWENTY;
    final PlanPhase phase1 = createMockMonthlyPlanPhase(null, fixedCost1, PhaseType.TRIAL);
    final PlanPhase phase2 = createMockMonthlyPlanPhase(null, fixedCost2, PhaseType.EVERGREEN);
    final BillingEventSet events = new MockBillingEventSet();
    final UUID subscriptionId = UUID.randomUUID();
    final UUID accountId = 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 that a single invoice item is generated for the fixed cost
    final InvoiceWithMetadata invoiceWithMetadata1 = generator.generateInvoice(account, events, null, startDate, Currency.USD, internalCallContext);
    final Invoice invoice1 = invoiceWithMetadata1.getInvoice();
    assertNotNull(invoice1);
    assertEquals(invoice1.getNumberOfItems(), 1);
    assertEquals(invoice1.getBalance(), KillBillMoney.of(fixedCost1, invoice1.getCurrency()));
    final List<Invoice> invoiceList = new ArrayList<Invoice>();
    invoiceList.add(invoice1);
    // move forward in time one billing period
    final LocalDate phaseChangeDate = startDate.plusMonths(1);
    final BillingEvent event2 = createBillingEvent(subscriptionId, bundleId, phaseChangeDate, plan1, phase2, 1);
    events.add(event2);
    // ensure that a single invoice item is generated for the fixed cost
    final InvoiceWithMetadata invoiceWithMetadata2 = generator.generateInvoice(account, events, invoiceList, phaseChangeDate, Currency.USD, internalCallContext);
    final Invoice invoice2 = invoiceWithMetadata2.getInvoice();
    assertEquals(invoice2.getNumberOfItems(), 1);
    assertEquals(invoice2.getBalance(), KillBillMoney.of(fixedCost2, invoice2.getCurrency()));
}
Also used : 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 87 with BillingEvent

use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.

the class TestDefaultInvoiceGenerator method testAutoInvoiceOffWithCredits.

@Test(groups = "fast")
public void testAutoInvoiceOffWithCredits() throws CatalogApiException, InvoiceApiException {
    final Currency currency = Currency.USD;
    final List<Invoice> invoices = new ArrayList<Invoice>();
    final MockBillingEventSet eventSet = new MockBillingEventSet();
    final UUID accountId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate startDate = new LocalDate(2012, 1, 1);
    // add first SubscriptionBase creation event
    final UUID subscriptionId1 = UUID.randomUUID();
    final Plan plan1 = new MockPlan();
    final PlanPhase plan1phase1 = createMockMonthlyPlanPhase(FIFTEEN, null, PhaseType.DISCOUNT);
    final BillingEvent subscription1creation = createBillingEvent(subscriptionId1, bundleId, startDate, plan1, plan1phase1, 1);
    eventSet.add(subscription1creation);
    // add second SubscriptionBase creation event
    final UUID subscriptionId2 = UUID.randomUUID();
    final Plan plan2 = new MockPlan();
    final PlanPhase plan2phase1 = createMockMonthlyPlanPhase(TWELVE, null, PhaseType.EVERGREEN);
    eventSet.add(createBillingEvent(subscriptionId2, bundleId, startDate, plan2, plan2phase1, 1));
    // generate the first invoice
    final InvoiceWithMetadata invoiceWithMetadata1 = generator.generateInvoice(account, eventSet, invoices, startDate, currency, internalCallContext);
    final Invoice invoice1 = invoiceWithMetadata1.getInvoice();
    assertNotNull(invoice1);
    assertTrue(invoice1.getBalance().compareTo(FIFTEEN.add(TWELVE)) == 0);
    invoices.add(invoice1);
    // set auto invoice off for first SubscriptionBase (i.e. remove event from BillingEventSet and add SubscriptionBase id to the list
    // generate invoice
    eventSet.remove(subscription1creation);
    eventSet.addSubscriptionWithAutoInvoiceOff(subscriptionId1);
    final LocalDate targetDate2 = startDate.plusMonths(1);
    final InvoiceWithMetadata invoiceWithMetadata2 = generator.generateInvoice(account, eventSet, invoices, targetDate2, currency, internalCallContext);
    final Invoice invoice2 = invoiceWithMetadata2.getInvoice();
    assertNotNull(invoice2);
    assertTrue(invoice2.getBalance().compareTo(TWELVE) == 0);
    invoices.add(invoice2);
    final LocalDate targetDate3 = targetDate2.plusMonths(1);
    eventSet.clearSubscriptionsWithAutoInvoiceOff();
    eventSet.add(subscription1creation);
    final InvoiceWithMetadata invoiceWithMetadata3 = generator.generateInvoice(account, eventSet, invoices, targetDate3, currency, internalCallContext);
    final Invoice invoice3 = invoiceWithMetadata3.getInvoice();
    assertNotNull(invoice3);
    assertTrue(invoice3.getBalance().compareTo(FIFTEEN.multiply(TWO).add(TWELVE)) == 0);
}
Also used : 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) Currency(org.killbill.billing.catalog.api.Currency) ArrayList(java.util.ArrayList) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) BillingEvent(org.killbill.billing.junction.BillingEvent) UUID(java.util.UUID) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 88 with BillingEvent

use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.

the class TestDefaultInvoiceGenerator method testSingleEventWithExistingInvoice.

@Test(groups = "fast")
public void testSingleEventWithExistingInvoice() throws InvoiceApiException, CatalogApiException {
    final BillingEventSet events = new MockBillingEventSet();
    final SubscriptionBase sub = createSubscription();
    final LocalDate startDate = invoiceUtil.buildDate(2011, 9, 1);
    final Plan plan1 = new MockPlan();
    final BigDecimal rate = FIVE;
    final PlanPhase phase1 = createMockMonthlyPlanPhase(rate);
    final BillingEvent event1 = createBillingEvent(sub.getId(), sub.getBundleId(), startDate, plan1, phase1, 1);
    events.add(event1);
    LocalDate targetDate = invoiceUtil.buildDate(2011, 12, 1);
    final InvoiceWithMetadata invoiceWithMetadata1 = generator.generateInvoice(account, events, null, targetDate, Currency.USD, internalCallContext);
    final List<Invoice> existingInvoices = new ArrayList<Invoice>();
    final Invoice invoice1 = invoiceWithMetadata1.getInvoice();
    existingInvoices.add(invoice1);
    targetDate = invoiceUtil.buildDate(2011, 12, 3);
    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) 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) 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 89 with BillingEvent

use of org.killbill.billing.junction.BillingEvent 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 90 with BillingEvent

use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.

the class TestInvoiceHelper method createMockBillingEvent.

public BillingEvent createMockBillingEvent(@Nullable final Account account, final SubscriptionBase subscription, final DateTime effectiveDate, final Plan plan, final PlanPhase planPhase, @Nullable final BigDecimal fixedPrice, @Nullable final BigDecimal recurringPrice, final Currency currency, final BillingPeriod billingPeriod, final int billCycleDayLocal, final BillingMode billingMode, final String description, final long totalOrdering, final SubscriptionBaseTransitionType type) {
    final Account mockAccount = Mockito.mock(Account.class);
    Mockito.when(mockAccount.getTimeZone()).thenReturn(DateTimeZone.UTC);
    return new BillingEvent() {

        @Override
        public int getBillCycleDayLocal() {
            return billCycleDayLocal;
        }

        @Override
        public SubscriptionBase getSubscription() {
            return subscription;
        }

        @Override
        public DateTime getEffectiveDate() {
            return effectiveDate;
        }

        @Override
        public PlanPhase getPlanPhase() {
            return planPhase;
        }

        @Override
        public Plan getPlan() {
            return plan;
        }

        @Override
        public BillingPeriod getBillingPeriod() {
            return billingPeriod;
        }

        @Override
        public String getDescription() {
            return description;
        }

        @Override
        public BigDecimal getFixedPrice() {
            return fixedPrice;
        }

        @Override
        public BigDecimal getRecurringPrice(DateTime effectiveDate) {
            return recurringPrice;
        }

        @Override
        public Currency getCurrency() {
            return currency;
        }

        @Override
        public SubscriptionBaseTransitionType getTransitionType() {
            return type;
        }

        @Override
        public Long getTotalOrdering() {
            return totalOrdering;
        }

        @Override
        public DateTimeZone getTimeZone() {
            return DateTimeZone.UTC;
        }

        @Override
        public List<Usage> getUsages() {
            return Collections.emptyList();
        }

        @Override
        public int compareTo(final BillingEvent e1) {
            if (!getSubscription().getId().equals(e1.getSubscription().getId())) {
                // First order by subscription
                return getSubscription().getId().compareTo(e1.getSubscription().getId());
            } else {
                // subscriptions are the same
                if (!getEffectiveDate().equals(e1.getEffectiveDate())) {
                    // Secondly order by date
                    return getEffectiveDate().compareTo(e1.getEffectiveDate());
                } else {
                    // dates and subscriptions are the same
                    return getTotalOrdering().compareTo(e1.getTotalOrdering());
                }
            }
        }
    };
}
Also used : Account(org.killbill.billing.account.api.Account) Usage(org.killbill.billing.catalog.api.Usage) BillingEvent(org.killbill.billing.junction.BillingEvent) DateTime(org.joda.time.DateTime)

Aggregations

BillingEvent (org.killbill.billing.junction.BillingEvent)98 Test (org.testng.annotations.Test)82 DateTime (org.joda.time.DateTime)52 LocalDate (org.joda.time.LocalDate)49 Plan (org.killbill.billing.catalog.api.Plan)42 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)41 MockPlan (org.killbill.billing.catalog.MockPlan)40 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)40 BigDecimal (java.math.BigDecimal)36 ArrayList (java.util.ArrayList)36 UUID (java.util.UUID)35 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)35 BillingEventSet (org.killbill.billing.junction.BillingEventSet)33 Invoice (org.killbill.billing.invoice.api.Invoice)32 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)32 TreeSet (java.util.TreeSet)30 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)27 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)26 DefaultPrice (org.killbill.billing.catalog.DefaultPrice)25 MockInternationalPrice (org.killbill.billing.catalog.MockInternationalPrice)25