Search in sources :

Example 21 with RawUsageRecord

use of org.killbill.billing.usage.api.RawUsageRecord in project killbill by killbill.

the class TestContiguousIntervalConsumableInArrear method testComputeWithFractionOfCents.

@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/1124")
public void testComputeWithFractionOfCents() throws Exception {
    final LocalDate startDate = new LocalDate(2014, 03, 20);
    final LocalDate firstBCDDate = new LocalDate(2014, 04, 15);
    final List<RawUsageRecord> rawUsages = new ArrayList<RawUsageRecord>();
    rawUsages.add(new DefaultRawUsage(subscriptionId, startDate, "unit", 55L, "tracking-1"));
    final DefaultTieredBlock block = createDefaultTieredBlock("unit", 1, 100, new BigDecimal("0.067"));
    final DefaultTier tier = createDefaultTierWithBlocks(block);
    final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, TierBlockPolicy.ALL_TIERS, tier);
    final BillingEvent event1 = createMockBillingEvent(startDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList(), catalogEffectiveDate);
    final ContiguousIntervalUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, rawUsages, startDate.plusMonths(1), false, event1);
    final List<InvoiceItem> invoiceItems = new ArrayList<InvoiceItem>();
    final InvoiceItem ii1 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, usage.getName(), null, startDate, firstBCDDate, new BigDecimal("3.69"), currency);
    invoiceItems.add(ii1);
    final UsageInArrearItemsAndNextNotificationDate usageResult = intervalConsumableInArrear.computeMissingItemsAndNextNotificationDate(invoiceItems);
    // Nothing to bill
    Assert.assertTrue(usageResult.getInvoiceItems().isEmpty());
    checkTrackingIds(rawUsages, usageResult.getTrackingIds());
}
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) DefaultTieredBlock(org.killbill.billing.catalog.DefaultTieredBlock) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) RawUsageRecord(org.killbill.billing.usage.api.RawUsageRecord) 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)

Example 22 with RawUsageRecord

use of org.killbill.billing.usage.api.RawUsageRecord in project killbill by killbill.

the class TestContiguousIntervalConsumableInArrear method testGetRolledUpUsage.

@Test(groups = "fast")
public void testGetRolledUpUsage() throws Exception {
    final DefaultTieredBlock tieredBlock1 = createDefaultTieredBlock("unit", 100, 1000, BigDecimal.ONE);
    final DefaultTieredBlock tieredBlock2 = createDefaultTieredBlock("unit2", 10, 1000, BigDecimal.ONE);
    final DefaultTier tier = createDefaultTierWithBlocks(tieredBlock1, tieredBlock2);
    final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, TierBlockPolicy.ALL_TIERS, tier);
    final LocalDate t0 = new LocalDate(2015, 03, BCD);
    final BillingEvent eventT0 = createMockBillingEvent(t0.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList(), catalogEffectiveDate);
    final LocalDate t1 = new LocalDate(2015, 04, BCD);
    final BillingEvent eventT1 = createMockBillingEvent(t1.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList(), catalogEffectiveDate);
    final LocalDate t2 = new LocalDate(2015, 05, BCD);
    final BillingEvent eventT2 = createMockBillingEvent(t2.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList(), catalogEffectiveDate);
    final LocalDate t3 = new LocalDate(2015, 06, BCD);
    final BillingEvent eventT3 = createMockBillingEvent(t3.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList(), catalogEffectiveDate);
    final LocalDate targetDate = t3;
    // Prev t0
    final RawUsageRecord raw1 = new DefaultRawUsage(subscriptionId, new LocalDate(2015, 03, 01), "unit", 12L, "tracking-1");
    // t0 - t1
    final RawUsageRecord raw2 = new DefaultRawUsage(subscriptionId, new LocalDate(2015, 03, 15), "unit", 6L, "tracking-1");
    final RawUsageRecord raw3 = new DefaultRawUsage(subscriptionId, new LocalDate(2015, 03, 25), "unit", 4L, "tracking-1");
    // t1 - t2 nothing
    // t2 - t3
    final RawUsageRecord raw4 = new DefaultRawUsage(subscriptionId, new LocalDate(2015, 05, 15), "unit", 13L, "tracking-2");
    final RawUsageRecord oraw1 = new DefaultRawUsage(subscriptionId, new LocalDate(2015, 05, 21), "unit2", 21L, "tracking-2");
    final RawUsageRecord raw5 = new DefaultRawUsage(subscriptionId, new LocalDate(2015, 05, 31), "unit", 7L, "tracking-2");
    // after t3
    final RawUsageRecord raw6 = new DefaultRawUsage(subscriptionId, new LocalDate(2015, 06, 15), "unit", 100L, "tracking-3");
    final List<RawUsageRecord> rawUsageRecord = ImmutableList.of(raw1, raw2, raw3, raw4, oraw1, raw5, raw6);
    final ContiguousIntervalUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, rawUsageRecord, targetDate, true, eventT0, eventT1, eventT2, eventT3);
    final List<RolledUpUsageWithMetadata> unsortedRolledUpUsage = intervalConsumableInArrear.getRolledUpUsage().getUsage();
    Assert.assertEquals(unsortedRolledUpUsage.size(), 3);
    final List<RolledUpUsageWithMetadata> rolledUpUsage = TEST_ROLLED_UP_FIRST_USAGE_ORDERING.sortedCopy(unsortedRolledUpUsage);
    Assert.assertEquals(rolledUpUsage.get(0).getStart().compareTo(t0), 0);
    Assert.assertEquals(rolledUpUsage.get(0).getEnd().compareTo(t1), 0);
    Assert.assertEquals(rolledUpUsage.get(0).getRolledUpUnits().size(), 2);
    Assert.assertEquals(rolledUpUsage.get(0).getRolledUpUnits().get(0).getUnitType(), "unit");
    Assert.assertEquals(rolledUpUsage.get(0).getRolledUpUnits().get(0).getAmount(), (Long) 10L);
    Assert.assertEquals(rolledUpUsage.get(0).getRolledUpUnits().get(1).getUnitType(), "unit2");
    Assert.assertEquals(rolledUpUsage.get(0).getRolledUpUnits().get(1).getAmount(), (Long) 0L);
    Assert.assertEquals(rolledUpUsage.get(1).getStart().compareTo(t1), 0);
    Assert.assertEquals(rolledUpUsage.get(1).getEnd().compareTo(t2), 0);
    Assert.assertEquals(rolledUpUsage.get(1).getRolledUpUnits().size(), 2);
    Assert.assertEquals(rolledUpUsage.get(1).getRolledUpUnits().get(0).getUnitType(), "unit");
    Assert.assertEquals(rolledUpUsage.get(1).getRolledUpUnits().get(0).getAmount(), (Long) 0L);
    Assert.assertEquals(rolledUpUsage.get(1).getRolledUpUnits().get(1).getUnitType(), "unit2");
    Assert.assertEquals(rolledUpUsage.get(1).getRolledUpUnits().get(1).getAmount(), (Long) 0L);
    Assert.assertEquals(rolledUpUsage.get(2).getStart().compareTo(t2), 0);
    Assert.assertEquals(rolledUpUsage.get(2).getEnd().compareTo(t3), 0);
    Assert.assertEquals(rolledUpUsage.get(2).getRolledUpUnits().size(), 2);
    Assert.assertEquals(rolledUpUsage.get(2).getRolledUpUnits().get(0).getUnitType(), "unit");
    Assert.assertEquals(rolledUpUsage.get(2).getRolledUpUnits().get(0).getAmount(), (Long) 20L);
    Assert.assertEquals(rolledUpUsage.get(2).getRolledUpUnits().get(1).getUnitType(), "unit2");
    Assert.assertEquals(rolledUpUsage.get(2).getRolledUpUnits().get(1).getAmount(), (Long) 21L);
}
Also used : RawUsageRecord(org.killbill.billing.usage.api.RawUsageRecord) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) DefaultTieredBlock(org.killbill.billing.catalog.DefaultTieredBlock) DefaultTier(org.killbill.billing.catalog.DefaultTier) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 23 with RawUsageRecord

use of org.killbill.billing.usage.api.RawUsageRecord in project killbill by killbill.

the class DefaultInternalUserApi method getRawUsageForAccount.

@Override
public List<RawUsageRecord> getRawUsageForAccount(final LocalDate startDate, final LocalDate endDate, @Nullable final DryRunInfo dryRunInfo, final InternalTenantContext internalTenantContext) {
    log.info("GetRawUsageForAccount startDate='{}', endDate='{}'", startDate, endDate);
    final TenantContext tenantContext = internalCallContextFactory.createTenantContext(internalTenantContext);
    final TenantContext tenantContextWithHint = dryRunInfo != null ? new DryRunTenantContext(dryRunInfo, tenantContext) : tenantContext;
    final List<RawUsageRecord> resultFromPlugin = getAccountUsageFromPlugin(startDate, endDate, tenantContextWithHint);
    if (resultFromPlugin != null) {
        return resultFromPlugin;
    }
    final List<RolledUpUsageModelDao> usage = rolledUpUsageDao.getRawUsageForAccount(startDate, endDate, internalTenantContext);
    return ImmutableList.copyOf(Iterables.transform(usage, new Function<RolledUpUsageModelDao, RawUsageRecord>() {

        @Override
        public RawUsageRecord apply(final RolledUpUsageModelDao input) {
            return new DefaultRawUsage(input.getSubscriptionId(), input.getRecordDate(), input.getUnitType(), input.getAmount(), input.getTrackingId());
        }
    }));
}
Also used : Function(com.google.common.base.Function) RawUsageRecord(org.killbill.billing.usage.api.RawUsageRecord) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) DryRunTenantContext(org.killbill.billing.usage.DryRunTenantContext) TenantContext(org.killbill.billing.util.callcontext.TenantContext) DryRunTenantContext(org.killbill.billing.usage.DryRunTenantContext) RolledUpUsageModelDao(org.killbill.billing.usage.dao.RolledUpUsageModelDao)

Aggregations

RawUsageRecord (org.killbill.billing.usage.api.RawUsageRecord)23 LocalDate (org.joda.time.LocalDate)19 ArrayList (java.util.ArrayList)16 DefaultRawUsage (org.killbill.billing.usage.api.svcs.DefaultRawUsage)16 Test (org.testng.annotations.Test)15 BigDecimal (java.math.BigDecimal)11 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)11 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)11 UsageInvoiceItem (org.killbill.billing.invoice.model.UsageInvoiceItem)11 DefaultTier (org.killbill.billing.catalog.DefaultTier)8 DefaultUsage (org.killbill.billing.catalog.DefaultUsage)8 BillingEvent (org.killbill.billing.junction.BillingEvent)8 DefaultTieredBlock (org.killbill.billing.catalog.DefaultTieredBlock)7 UsageConsumableInArrearTierUnitAggregate (org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)4 TrackingRecordId (org.killbill.billing.invoice.generator.InvoiceWithMetadata.TrackingRecordId)4 UsageConsumableInArrearAggregate (org.killbill.billing.invoice.usage.details.UsageConsumableInArrearAggregate)4 RolledUpUnit (org.killbill.billing.usage.api.RolledUpUnit)4 HashMap (java.util.HashMap)3 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)3