Search in sources :

Example 16 with RecurringInvoiceItem

use of org.killbill.billing.invoice.model.RecurringInvoiceItem in project killbill by killbill.

the class TestFixedAndRecurringInvoiceItemGenerator method testOverlappingItemsWithRepair.

// Test fully repaired item get removed and instead we generate correct item
@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testOverlappingItemsWithRepair() 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 previous mis-bill: existing item is for [2016-01-01,2016-01-30], proposed will be for [2016-01-01,2016-02-01]
    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, event.getPlan().getName(), event.getPlanPhase().getName(), null, startDate, startDate.plusDays(29), amount, amount, account.getCurrency()));
    // But the system has already repaired it
    invoice.addInvoiceItem(new RepairAdjInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), startDate, startDate.plusDays(29), amount.negate(), account.getCurrency(), invoice.getInvoiceItems().get(0).getId()));
    existingInvoices.add(invoice);
    // We will generate the correct recurring item
    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();
    assertEquals(generatedItems.size(), 1);
    assertTrue(generatedItems.get(0) instanceof RecurringInvoiceItem);
    assertEquals(generatedItems.get(0).getStartDate(), new LocalDate("2016-01-01"));
    assertEquals(generatedItems.get(0).getEndDate(), new LocalDate("2016-02-01"));
    assertEquals(generatedItems.get(0).getAmount().compareTo(amount), 0);
}
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) 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 17 with RecurringInvoiceItem

use of org.killbill.billing.invoice.model.RecurringInvoiceItem in project killbill by killbill.

the class TestInvoiceWithMetadata method testWith$0RecurringItem.

@Test(groups = "fast")
public void testWith$0RecurringItem() {
    final LocalDate invoiceDate = new LocalDate(2016, 11, 15);
    final DefaultInvoice originalInvoice = new DefaultInvoice(account.getId(), invoiceDate, account.getCurrency());
    final Plan plan = new MockPlan("my-plan");
    final MockInternationalPrice price = new MockInternationalPrice(new DefaultPrice(BigDecimal.TEN, account.getCurrency()));
    final PlanPhase planPhase = new MockPlanPhase(price, null, BillingPeriod.MONTHLY, PhaseType.EVERGREEN);
    final BillingEvent event = invoiceUtil.createMockBillingEvent(account, subscription, invoiceDate.toDateTimeAtStartOfDay(), plan, planPhase, null, BigDecimal.ZERO, account.getCurrency(), planPhase.getRecurring().getBillingPeriod(), 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 1L, SubscriptionBaseTransitionType.CREATE);
    final InvoiceItem invoiceItem = new RecurringInvoiceItem(UUID.randomUUID(), invoiceDate.toDateTimeAtStartOfDay(), originalInvoice.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), null, event.getPlan().getName(), event.getPlanPhase().getName(), null, invoiceDate, invoiceDate.plusMonths(1), BigDecimal.ZERO, BigDecimal.ZERO, account.getCurrency());
    originalInvoice.addInvoiceItem(invoiceItem);
    final Map<UUID, SubscriptionFutureNotificationDates> perSubscriptionFutureNotificationDates = new HashMap<UUID, SubscriptionFutureNotificationDates>();
    final SubscriptionFutureNotificationDates subscriptionFutureNotificationDates = new SubscriptionFutureNotificationDates(BillingMode.IN_ADVANCE);
    subscriptionFutureNotificationDates.updateNextRecurringDateIfRequired(invoiceDate.plusMonths(1));
    perSubscriptionFutureNotificationDates.put(subscription.getId(), subscriptionFutureNotificationDates);
    final InvoiceWithMetadata invoiceWithMetadata = new InvoiceWithMetadata(originalInvoice, ImmutableSet.of(), perSubscriptionFutureNotificationDates, false, internalCallContext);
    // We generate an invoice with one item, invoicing for $0
    final Invoice resultingInvoice = invoiceWithMetadata.getInvoice();
    Assert.assertNotNull(resultingInvoice);
    Assert.assertEquals(resultingInvoice.getInvoiceItems().size(), 1);
    Assert.assertEquals(resultingInvoice.getInvoiceItems().get(0).getAmount().compareTo(BigDecimal.ZERO), 0);
    final Map<UUID, InvoiceWithMetadata.SubscriptionFutureNotificationDates> dateMap = invoiceWithMetadata.getPerSubscriptionFutureNotificationDates();
    final InvoiceWithMetadata.SubscriptionFutureNotificationDates futureNotificationDates = dateMap.get(subscription.getId());
    // We verify that we generated the future notification for a month ahead
    Assert.assertNotNull(futureNotificationDates.getNextRecurringDate());
    Assert.assertEquals(futureNotificationDates.getNextRecurringDate().compareTo(invoiceDate.plusMonths(1)), 0);
}
Also used : SubscriptionFutureNotificationDates(org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) 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) MockInternationalPrice(org.killbill.billing.catalog.MockInternationalPrice) SubscriptionFutureNotificationDates(org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates) MockPlan(org.killbill.billing.catalog.MockPlan) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) 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) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Test(org.testng.annotations.Test)

Example 18 with RecurringInvoiceItem

use of org.killbill.billing.invoice.model.RecurringInvoiceItem 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 RecurringInvoiceItem

use of org.killbill.billing.invoice.model.RecurringInvoiceItem in project killbill by killbill.

the class FixedAndRecurringInvoiceItemGenerator method processRecurringEvent.

// Turn a set of events into a list of invoice items. Note that the dates on the invoice items will be rounded (granularity of a day)
@VisibleForTesting
List<InvoiceItem> processRecurringEvent(final UUID invoiceId, final UUID accountId, final BillingEvent thisEvent, @Nullable final BillingEvent nextEvent, final LocalDate targetDate, final Currency currency, final InvoiceItemGeneratorLogger invoiceItemGeneratorLogger, final Map<UUID, SubscriptionFutureNotificationDates> perSubscriptionFutureNotificationDates, final InternalCallContext internalCallContext) throws InvoiceApiException {
    final List<InvoiceItem> items = new ArrayList<InvoiceItem>();
    final LocalDate thisEventEffectiveDate = internalCallContext.toLocalDate(thisEvent.getEffectiveDate());
    if (thisEventEffectiveDate.compareTo(targetDate) > 0) {
        return items;
    }
    // Handle recurring items
    final BillingPeriod billingPeriod = thisEvent.getBillingPeriod();
    if (billingPeriod != BillingPeriod.NO_BILLING_PERIOD) {
        final Plan currentPlan = thisEvent.getPlan();
        Preconditions.checkNotNull(currentPlan, "Unexpected null Plan name event = %s", thisEvent);
        final BillingMode recurringBillingMode = currentPlan.getRecurringBillingMode();
        final LocalDate startDate = thisEventEffectiveDate;
        if (!startDate.isAfter(targetDate)) {
            final LocalDate endDate = (nextEvent == null) ? null : internalCallContext.toLocalDate(nextEvent.getEffectiveDate());
            final int billCycleDayLocal = thisEvent.getBillCycleDayLocal();
            final RecurringInvoiceItemDataWithNextBillingCycleDate itemDataWithNextBillingCycleDate;
            try {
                itemDataWithNextBillingCycleDate = generateInvoiceItemData(startDate, endDate, targetDate, billCycleDayLocal, billingPeriod, recurringBillingMode);
            } catch (final InvalidDateSequenceException e) {
                throw new InvoiceApiException(ErrorCode.INVOICE_INVALID_DATE_SEQUENCE, startDate, endDate, targetDate);
            }
            for (final RecurringInvoiceItemData itemDatum : itemDataWithNextBillingCycleDate.getItemData()) {
                final BigDecimal rate = thisEvent.getRecurringPrice();
                if (rate != null) {
                    final BigDecimal amount = KillBillMoney.of(itemDatum.getNumberOfCycles().multiply(rate), currency);
                    final DateTime catalogEffectiveDate = thisEvent.getCatalogEffectiveDate() != null ? thisEvent.getCatalogEffectiveDate() : null;
                    final RecurringInvoiceItem recurringItem = new RecurringInvoiceItem(invoiceId, accountId, thisEvent.getBundleId(), thisEvent.getSubscriptionId(), currentPlan.getProduct().getName(), currentPlan.getName(), thisEvent.getPlanPhase().getName(), catalogEffectiveDate, itemDatum.getStartDate(), itemDatum.getEndDate(), amount, rate, currency);
                    items.add(recurringItem);
                }
            }
            updatePerSubscriptionNextNotificationDate(thisEvent.getSubscriptionId(), itemDataWithNextBillingCycleDate.getNextBillingCycleDate(), items, recurringBillingMode, perSubscriptionFutureNotificationDates);
        }
    } else {
        final SubscriptionFutureNotificationDates futureNotificationDates = perSubscriptionFutureNotificationDates.get(thisEvent.getSubscriptionId());
        if (futureNotificationDates != null) {
            futureNotificationDates.clearNextRecurringDate();
        }
    }
    // For debugging purposes
    invoiceItemGeneratorLogger.append(thisEvent, items);
    return items;
}
Also used : SubscriptionFutureNotificationDates(org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) InvoiceDateUtils.calculateProRationBeforeFirstBillingPeriod(org.killbill.billing.invoice.generator.InvoiceDateUtils.calculateProRationBeforeFirstBillingPeriod) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) ArrayList(java.util.ArrayList) RecurringInvoiceItemData(org.killbill.billing.invoice.model.RecurringInvoiceItemData) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) InvalidDateSequenceException(org.killbill.billing.invoice.model.InvalidDateSequenceException) RecurringInvoiceItemDataWithNextBillingCycleDate(org.killbill.billing.invoice.model.RecurringInvoiceItemDataWithNextBillingCycleDate) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) BillingMode(org.killbill.billing.catalog.api.BillingMode) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 20 with RecurringInvoiceItem

use of org.killbill.billing.invoice.model.RecurringInvoiceItem in project killbill by killbill.

the class TestInvoiceDao method testAccountBalanceWithRefundInternal.

private void testAccountBalanceWithRefundInternal(final boolean withAdjustment) throws InvoiceApiException, EntityPersistenceException {
    final UUID accountId = account.getId();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate targetDate1 = new LocalDate(2011, 10, 6);
    final Invoice invoice1 = new DefaultInvoice(accountId, clock.getUTCToday(), targetDate1, Currency.USD);
    invoiceUtil.createInvoice(invoice1, context);
    final LocalDate startDate = new LocalDate(2011, 3, 1);
    final LocalDate endDate = startDate.plusMonths(1);
    final BigDecimal rate1 = new BigDecimal("20.0");
    final BigDecimal refund1 = new BigDecimal("7.00");
    // Recurring item
    final RecurringInvoiceItem item2 = new RecurringInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test product", "test plan", "test phase B", null, startDate, endDate, rate1, rate1, Currency.USD);
    invoiceUtil.createInvoiceItem(item2, context);
    BigDecimal balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(new BigDecimal("20.00")), 0);
    // Pay the whole thing
    final UUID paymentId = UUID.randomUUID();
    final BigDecimal payment1 = rate1;
    final InvoicePayment payment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, paymentId, invoice1.getId(), new DateTime(), payment1, Currency.USD, Currency.USD, null, true);
    invoiceUtil.createPayment(payment, context);
    balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(new BigDecimal("0.00")), 0);
    invoiceDao.createRefund(paymentId, UUID.randomUUID(), refund1, withAdjustment, ImmutableMap.<UUID, BigDecimal>of(), UUID.randomUUID().toString(), true, context);
    balance = invoiceDao.getAccountBalance(accountId, context);
    if (withAdjustment) {
        assertEquals(balance.compareTo(BigDecimal.ZERO), 0);
    } else {
        assertEquals(balance.compareTo(new BigDecimal("7.00")), 0);
    }
}
Also used : InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment)

Aggregations

RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)98 Test (org.testng.annotations.Test)90 LocalDate (org.joda.time.LocalDate)89 BigDecimal (java.math.BigDecimal)82 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)73 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)69 RepairAdjInvoiceItem (org.killbill.billing.invoice.model.RepairAdjInvoiceItem)69 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)65 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)39 Invoice (org.killbill.billing.invoice.api.Invoice)38 UUID (java.util.UUID)34 MockPlan (org.killbill.billing.catalog.MockPlan)16 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)16 Plan (org.killbill.billing.catalog.api.Plan)16 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)16 AccountInvoices (org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices)16 BillingEventSet (org.killbill.billing.junction.BillingEventSet)16 HashMap (java.util.HashMap)15 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)15 BillingEvent (org.killbill.billing.junction.BillingEvent)15