Search in sources :

Example 1 with UsageInArrearItemsAndNextNotificationDate

use of org.killbill.billing.invoice.usage.ContiguousIntervalUsageInArrear.UsageInArrearItemsAndNextNotificationDate in project killbill by killbill.

the class SubscriptionUsageInArrear method computeMissingUsageInvoiceItems.

/**
     * Based on billing events, (@code existingUsage} and targetDate, figure out what remains to be billed.
     *
     * @param existingUsage the existing on disk usage items.
     * @throws CatalogApiException
     */
public SubscriptionUsageInArrearItemsAndNextNotificationDate computeMissingUsageInvoiceItems(final List<InvoiceItem> existingUsage, final InvoiceItemGeneratorLogger invoiceItemGeneratorLogger) throws CatalogApiException {
    final SubscriptionUsageInArrearItemsAndNextNotificationDate result = new SubscriptionUsageInArrearItemsAndNextNotificationDate();
    final List<ContiguousIntervalUsageInArrear> billingEventTransitionTimePeriods = computeInArrearUsageInterval();
    for (final ContiguousIntervalUsageInArrear usageInterval : billingEventTransitionTimePeriods) {
        final UsageInArrearItemsAndNextNotificationDate newItemsAndDate = usageInterval.computeMissingItemsAndNextNotificationDate(existingUsage);
        // For debugging purposes
        invoiceItemGeneratorLogger.append(usageInterval, newItemsAndDate.getInvoiceItems());
        result.addUsageInArrearItemsAndNextNotificationDate(usageInterval.getUsage().getName(), newItemsAndDate);
    }
    return result;
}
Also used : UsageInArrearItemsAndNextNotificationDate(org.killbill.billing.invoice.usage.ContiguousIntervalUsageInArrear.UsageInArrearItemsAndNextNotificationDate)

Example 2 with UsageInArrearItemsAndNextNotificationDate

use of org.killbill.billing.invoice.usage.ContiguousIntervalUsageInArrear.UsageInArrearItemsAndNextNotificationDate in project killbill by killbill.

the class TestContiguousIntervalCapacityInArrear method testComputeMissingItems.

@Test(groups = "fast")
public void testComputeMissingItems() throws CatalogApiException {
    final LocalDate startDate = new LocalDate(2014, 03, 20);
    final LocalDate firstBCDDate = new LocalDate(2014, 04, 15);
    final LocalDate endDate = new LocalDate(2014, 05, 15);
    // 2 items for startDate - firstBCDDate
    final List<RawUsage> rawUsages = new ArrayList<RawUsage>();
    rawUsages.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "unit", 130L));
    rawUsages.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "unit", 271L));
    // 1 items for firstBCDDate - endDate
    rawUsages.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 04, 15), "unit", 199L));
    final DefaultUnit unit = new DefaultUnit().setName("unit");
    final DefaultLimit limit = new DefaultLimit().setUnit(unit).setMax((double) -1);
    final DefaultTier tier = createDefaultTierWithLimits(BigDecimal.TEN, limit);
    final DefaultUsage usage = createCapacityInArrearUsage(usageName, BillingPeriod.MONTHLY, tier);
    final LocalDate targetDate = endDate;
    final BillingEvent event1 = createMockBillingEvent(startDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList());
    final BillingEvent event2 = createMockBillingEvent(endDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList());
    final ContiguousIntervalUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, rawUsages, targetDate, true, event1, event2);
    final List<InvoiceItem> invoiceItems = new ArrayList<InvoiceItem>();
    final InvoiceItem ii1 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), startDate, firstBCDDate, BigDecimal.ONE, currency);
    invoiceItems.add(ii1);
    final InvoiceItem ii2 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), firstBCDDate, endDate, BigDecimal.ONE, currency);
    invoiceItems.add(ii2);
    final UsageInArrearItemsAndNextNotificationDate usageResult = intervalConsumableInArrear.computeMissingItemsAndNextNotificationDate(invoiceItems);
    final List<InvoiceItem> rawResults = usageResult.getInvoiceItems();
    assertEquals(rawResults.size(), 4);
    final List<InvoiceItem> result = ImmutableList.copyOf(Iterables.filter(rawResults, new Predicate<InvoiceItem>() {

        @Override
        public boolean apply(final InvoiceItem input) {
            return input.getAmount().compareTo(BigDecimal.ZERO) > 0;
        }
    }));
    assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("9.0")), 0, String.format("%s != 9.0", result.get(0).getAmount()));
    assertEquals(result.get(0).getCurrency(), Currency.BTC);
    assertEquals(result.get(0).getAccountId(), accountId);
    assertEquals(result.get(0).getBundleId(), bundleId);
    assertEquals(result.get(0).getSubscriptionId(), subscriptionId);
    assertEquals(result.get(0).getPlanName(), planName);
    assertEquals(result.get(0).getPhaseName(), phaseName);
    assertEquals(result.get(0).getUsageName(), usage.getName());
    assertTrue(result.get(0).getStartDate().compareTo(startDate) == 0);
    assertTrue(result.get(0).getEndDate().compareTo(firstBCDDate) == 0);
    assertEquals(result.get(1).getAmount().compareTo(new BigDecimal("9.0")), 0, String.format("%s != 9.0", result.get(0).getAmount()));
    assertEquals(result.get(1).getCurrency(), Currency.BTC);
    assertEquals(result.get(1).getAccountId(), accountId);
    assertEquals(result.get(1).getBundleId(), bundleId);
    assertEquals(result.get(1).getSubscriptionId(), subscriptionId);
    assertEquals(result.get(1).getPlanName(), planName);
    assertEquals(result.get(1).getPhaseName(), phaseName);
    assertEquals(result.get(1).getUsageName(), usage.getName());
    assertTrue(result.get(1).getStartDate().compareTo(firstBCDDate) == 0);
    assertTrue(result.get(1).getEndDate().compareTo(endDate) == 0);
}
Also used : DefaultLimit(org.killbill.billing.catalog.DefaultLimit) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) ArrayList(java.util.ArrayList) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) RawUsage(org.killbill.billing.usage.RawUsage) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Predicate(com.google.common.base.Predicate) UsageInArrearItemsAndNextNotificationDate(org.killbill.billing.invoice.usage.ContiguousIntervalUsageInArrear.UsageInArrearItemsAndNextNotificationDate) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) DefaultUnit(org.killbill.billing.catalog.DefaultUnit) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultTier(org.killbill.billing.catalog.DefaultTier) Test(org.testng.annotations.Test)

Example 3 with UsageInArrearItemsAndNextNotificationDate

use of org.killbill.billing.invoice.usage.ContiguousIntervalUsageInArrear.UsageInArrearItemsAndNextNotificationDate in project killbill by killbill.

the class TestContiguousIntervalConsumableInArrear method testComputeMissingItems.

@Test(groups = "fast")
public void testComputeMissingItems() throws CatalogApiException {
    final LocalDate startDate = new LocalDate(2014, 03, 20);
    final LocalDate firstBCDDate = new LocalDate(2014, 04, 15);
    final LocalDate endDate = new LocalDate(2014, 05, 15);
    // 2 items for startDate - firstBCDDate
    final List<RawUsage> rawUsages = new ArrayList<RawUsage>();
    rawUsages.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "unit", 130L));
    rawUsages.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "unit", 271L));
    // 1 items for firstBCDDate - endDate
    rawUsages.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 04, 15), "unit", 199L));
    final DefaultTieredBlock block = createDefaultTieredBlock("unit", 100, 10, BigDecimal.ONE);
    final DefaultTier tier = createDefaultTierWithBlocks(block);
    final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, tier);
    final LocalDate targetDate = endDate;
    final BillingEvent event1 = createMockBillingEvent(startDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList());
    final BillingEvent event2 = createMockBillingEvent(endDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList());
    final ContiguousIntervalUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, rawUsages, targetDate, true, event1, event2);
    final List<InvoiceItem> invoiceItems = new ArrayList<InvoiceItem>();
    final InvoiceItem ii1 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), startDate, firstBCDDate, BigDecimal.ONE, currency);
    invoiceItems.add(ii1);
    final InvoiceItem ii2 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), firstBCDDate, endDate, BigDecimal.ONE, currency);
    invoiceItems.add(ii2);
    final UsageInArrearItemsAndNextNotificationDate usageResult = intervalConsumableInArrear.computeMissingItemsAndNextNotificationDate(invoiceItems);
    final List<InvoiceItem> rawResults = usageResult.getInvoiceItems();
    assertEquals(rawResults.size(), 4);
    final List<InvoiceItem> result = ImmutableList.copyOf(Iterables.filter(rawResults, new Predicate<InvoiceItem>() {

        @Override
        public boolean apply(final InvoiceItem input) {
            return input.getAmount().compareTo(BigDecimal.ZERO) > 0;
        }
    }));
    // Invoiced for 1 BTC and used 130 + 271 = 401 => 5 blocks => 5 BTC so remaining piece should be 4 BTC
    assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("4.0")), 0, String.format("%s != 4.0", result.get(0).getAmount()));
    assertEquals(result.get(0).getCurrency(), Currency.BTC);
    assertEquals(result.get(0).getAccountId(), accountId);
    assertEquals(result.get(0).getBundleId(), bundleId);
    assertEquals(result.get(0).getSubscriptionId(), subscriptionId);
    assertEquals(result.get(0).getPlanName(), planName);
    assertEquals(result.get(0).getPhaseName(), phaseName);
    assertEquals(result.get(0).getUsageName(), usage.getName());
    assertTrue(result.get(0).getStartDate().compareTo(startDate) == 0);
    assertTrue(result.get(0).getEndDate().compareTo(firstBCDDate) == 0);
    // Invoiced for 1 BTC and used 199  => 2 blocks => 2 BTC so remaining piece should be 1 BTC
    assertEquals(result.get(1).getAmount().compareTo(new BigDecimal("1.0")), 0, String.format("%s != 1.0", result.get(0).getAmount()));
    assertEquals(result.get(1).getCurrency(), Currency.BTC);
    assertEquals(result.get(1).getAccountId(), accountId);
    assertEquals(result.get(1).getBundleId(), bundleId);
    assertEquals(result.get(1).getSubscriptionId(), subscriptionId);
    assertEquals(result.get(1).getPlanName(), planName);
    assertEquals(result.get(1).getPhaseName(), phaseName);
    assertEquals(result.get(1).getUsageName(), usage.getName());
    assertTrue(result.get(1).getStartDate().compareTo(firstBCDDate) == 0);
    assertTrue(result.get(1).getEndDate().compareTo(endDate) == 0);
}
Also used : FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) ArrayList(java.util.ArrayList) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) RawUsage(org.killbill.billing.usage.RawUsage) DefaultTieredBlock(org.killbill.billing.catalog.DefaultTieredBlock) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Predicate(com.google.common.base.Predicate) UsageInArrearItemsAndNextNotificationDate(org.killbill.billing.invoice.usage.ContiguousIntervalUsageInArrear.UsageInArrearItemsAndNextNotificationDate) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultTier(org.killbill.billing.catalog.DefaultTier) Test(org.testng.annotations.Test)

Aggregations

UsageInArrearItemsAndNextNotificationDate (org.killbill.billing.invoice.usage.ContiguousIntervalUsageInArrear.UsageInArrearItemsAndNextNotificationDate)3 Predicate (com.google.common.base.Predicate)2 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 LocalDate (org.joda.time.LocalDate)2 DefaultTier (org.killbill.billing.catalog.DefaultTier)2 DefaultUsage (org.killbill.billing.catalog.DefaultUsage)2 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)2 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)2 UsageInvoiceItem (org.killbill.billing.invoice.model.UsageInvoiceItem)2 BillingEvent (org.killbill.billing.junction.BillingEvent)2 RawUsage (org.killbill.billing.usage.RawUsage)2 DefaultRawUsage (org.killbill.billing.usage.api.svcs.DefaultRawUsage)2 Test (org.testng.annotations.Test)2 DefaultLimit (org.killbill.billing.catalog.DefaultLimit)1 DefaultTieredBlock (org.killbill.billing.catalog.DefaultTieredBlock)1 DefaultUnit (org.killbill.billing.catalog.DefaultUnit)1