use of org.killbill.billing.catalog.api.Usage in project killbill by killbill.
the class InvoiceItemFactory method computePrettyName.
//
// Returns an array of string for 'pretty' names [prettyPlanName, prettyPlanPhaseName, prettyUsageName]
//
private static String[] computePrettyName(final InvoiceItemType type, final DateTime transitionDate, @Nullable final String productName, @Nullable final String planName, @Nullable final String phaseName, @Nullable final String usageName, @Nullable final VersionedCatalog catalog) {
final String[] result = new String[4];
final boolean computePrettyName = catalog != null && (type == InvoiceItemType.FIXED || type == InvoiceItemType.RECURRING || type == InvoiceItemType.TAX || type == InvoiceItemType.USAGE);
String prettyProductName = null;
String prettyPlanName = null;
String prettyPlanPhaseName = null;
String prettyUsageName = null;
// Trying to optimize safe default behavior by checking both input and each intermediary null result at each step -- and doing poor on the Cyclomatic complexity
try {
if (computePrettyName) {
if (planName != null) {
// We are simply looking for the most recent version of a catalog version having such plan
// Finding the right entry would require some expensive operations which are probably not worth it for this use case.
Plan plan = null;
final List<StaticCatalog> versions = catalog.getVersions();
for (int i = versions.size() - 1; i >= 0; i--) {
final StaticCatalog curVersion = versions.get(i);
try {
plan = curVersion.findPlan(planName);
} catch (final CatalogApiException e) {
if (e.getCode() != ErrorCode.CAT_NO_SUCH_PLAN.getCode()) {
throw e;
}
}
if (plan != null) {
break;
}
}
if (plan != null) {
prettyPlanName = plan.getPrettyName();
if (productName != null) {
Preconditions.checkState(plan.getProduct().getName().equals(productName));
prettyProductName = plan.getProduct().getPrettyName();
}
if (phaseName != null) {
final PlanPhase planPhase = plan.findPhase(phaseName);
if (planPhase != null) {
prettyPlanPhaseName = planPhase.getPrettyName();
if (usageName != null) {
Usage usage = null;
for (Usage cur : planPhase.getUsages()) {
if (cur.getName().equals(usageName)) {
usage = cur;
break;
}
}
if (usage != null) {
prettyUsageName = usage.getPrettyName();
}
}
}
}
}
}
}
} catch (final CatalogApiException e) {
log.warn("Failed to compute invoice pretty names:", e.getMessage());
} finally {
result[0] = prettyProductName;
result[1] = prettyPlanName;
result[2] = prettyPlanPhaseName;
result[3] = prettyUsageName;
}
return result;
}
use of org.killbill.billing.catalog.api.Usage in project killbill by killbill.
the class TestContiguousIntervalConsumableInArrear method testBilledDetailsForUnitType.
@Test(groups = "fast")
public void testBilledDetailsForUnitType() throws Exception {
final LocalDate startDate = new LocalDate(2014, 03, 20);
final LocalDate targetDate = startDate.plusDays(1);
final DefaultTieredBlock block = createDefaultTieredBlock("unit", 100, 1000, BigDecimal.ONE);
final DefaultTier tier = createDefaultTierWithBlocks(block);
final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, TierBlockPolicy.ALL_TIERS, tier);
final ContiguousIntervalConsumableUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, ImmutableList.<RawUsageRecord>of(), targetDate, false, createMockBillingEvent(targetDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList(), catalogEffectiveDate));
final UsageConsumableInArrearTierUnitAggregate detail1 = new UsageConsumableInArrearTierUnitAggregate(3, "FOO", new BigDecimal("0.50"), 1L, 700L);
final UsageConsumableInArrearTierUnitAggregate detail2 = new UsageConsumableInArrearTierUnitAggregate(2, "FOO", BigDecimal.ONE, 1L, 500L);
final UsageConsumableInArrearTierUnitAggregate detail3 = new UsageConsumableInArrearTierUnitAggregate(1, "FOO", BigDecimal.TEN, 1L, 10L);
final UsageConsumableInArrearTierUnitAggregate detail4 = new UsageConsumableInArrearTierUnitAggregate(2, "FOO", BigDecimal.ONE, 1L, 50L);
final UsageConsumableInArrearTierUnitAggregate detail5 = new UsageConsumableInArrearTierUnitAggregate(1, "FOO", BigDecimal.TEN, 1L, 100L);
final List<UsageConsumableInArrearTierUnitAggregate> existingUsage = ImmutableList.of(detail1, detail2, detail3, detail4, detail5);
final UsageConsumableInArrearAggregate usageConsumableInArrearDetail = new UsageConsumableInArrearAggregate(existingUsage);
final String existingUsageJson = objectMapper.writeValueAsString(usageConsumableInArrearDetail);
final List<InvoiceItem> existingItems = new ArrayList<InvoiceItem>();
final InvoiceItem ii1 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, usageName, null, new LocalDate(2014, 03, 20), new LocalDate(2014, 04, 15), new BigDecimal("570.00"), null, currency, null, existingUsageJson);
existingItems.add(ii1);
final List<UsageConsumableInArrearTierUnitAggregate> aggregateDetails = intervalConsumableInArrear.getBilledDetailsForUnitType(existingItems, "FOO");
assertEquals(aggregateDetails.size(), 3);
assertEquals(aggregateDetails.get(0).getTier(), 1);
assertEquals(aggregateDetails.get(0).getQuantity().intValue(), 110);
assertEquals(aggregateDetails.get(1).getTier(), 2);
assertEquals(aggregateDetails.get(1).getQuantity().intValue(), 550);
assertEquals(aggregateDetails.get(2).getTier(), 3);
assertEquals(aggregateDetails.get(2).getQuantity().intValue(), 700);
}
use of org.killbill.billing.catalog.api.Usage in project killbill by killbill.
the class TestContiguousIntervalConsumableInArrear method testComputeBilledUsageWithUnlimitedMaxWith_ALL_TIERS.
@Test(groups = "fast")
public void testComputeBilledUsageWithUnlimitedMaxWith_ALL_TIERS() throws Exception {
final DefaultTieredBlock block1 = createDefaultTieredBlock("unit", 100, 10, BigDecimal.ONE);
final DefaultTier tier1 = createDefaultTierWithBlocks(block1);
final DefaultTieredBlock block2 = createDefaultTieredBlock("unit", 1000, -1, BigDecimal.ONE);
final DefaultTier tier2 = createDefaultTierWithBlocks(block2);
final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, TierBlockPolicy.ALL_TIERS, tier1, tier2);
final LocalDate targetDate = new LocalDate(2014, 03, 20);
final ContiguousIntervalConsumableUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, ImmutableList.<RawUsageRecord>of(), targetDate, false, createMockBillingEvent(targetDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList(), catalogEffectiveDate));
List<UsageConsumableInArrearTierUnitAggregate> result = intervalConsumableInArrear.computeToBeBilledConsumableInArrear(null, null, new DefaultRolledUpUnit("unit", 5325L), ImmutableList.<UsageConsumableInArrearTierUnitAggregate>of());
assertEquals(result.size(), 2);
// 5000 = 1000 (tier1) + 4325 (tier2) => 10 + 5 = 15
assertEquals(result.get(0).getAmount(), new BigDecimal("10"));
assertEquals(result.get(1).getAmount(), new BigDecimal("5"));
}
use of org.killbill.billing.catalog.api.Usage in project killbill by killbill.
the class TestRawUsageOptimizer method testWithNoItems.
@Test(groups = "fast")
public void testWithNoItems() {
final LocalDate firstEventStartDate = new LocalDate(2014, 03, 15);
final List<InvoiceItem> invoiceItems = new ArrayList<InvoiceItem>();
final Map<String, Usage> knownUsage = new HashMap<String, Usage>();
final DefaultTieredBlock block = createDefaultTieredBlock("unit", 100, 1000, BigDecimal.ONE);
final DefaultTier tier = createDefaultTierWithBlocks(block);
final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, TierBlockPolicy.ALL_TIERS, tier);
knownUsage.put(usageName, usage);
final LocalDate result = rawUsageOptimizer.getOptimizedRawUsageStartDate(firstEventStartDate, null, invoiceItems, knownUsage, internalCallContext);
Assert.assertEquals(result.compareTo(firstEventStartDate), 0);
}
use of org.killbill.billing.catalog.api.Usage in project killbill by killbill.
the class TestRawUsageOptimizer method testWithOneMonthlyAndOneNonActiveAnnualUsageSectionAndEnoughUsageItems.
@Test(groups = "fast")
public void testWithOneMonthlyAndOneNonActiveAnnualUsageSectionAndEnoughUsageItems() {
final LocalDate firstEventStartDate = new LocalDate(2014, 03, 15);
final List<InvoiceItem> invoiceItems = new ArrayList<InvoiceItem>();
for (int i = 0; i < 5; i++) {
invoiceItems.add(createUsageItem(firstEventStartDate.plusMonths(i)));
}
final LocalDate targetDate = invoiceItems.get(invoiceItems.size() - 1).getEndDate();
final Map<String, Usage> knownUsage = new HashMap<String, Usage>();
final DefaultTieredBlock block = createDefaultTieredBlock("unit", 100, 1000, BigDecimal.ONE);
final DefaultTier tier = createDefaultTierWithBlocks(block);
final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, TierBlockPolicy.ALL_TIERS, tier);
knownUsage.put(usageName, usage);
final DefaultTieredBlock block2 = createDefaultTieredBlock("unit2", 10, 10000, BigDecimal.TEN);
final DefaultTier tier2 = createDefaultTierWithBlocks(block2);
final DefaultUsage usage2 = createConsumableInArrearUsage("usageName2", BillingPeriod.ANNUAL, TierBlockPolicy.ALL_TIERS, tier2);
knownUsage.put("usageName2", usage2);
final LocalDate result = rawUsageOptimizer.getOptimizedRawUsageStartDate(firstEventStartDate, targetDate, invoiceItems, knownUsage, internalCallContext);
// The same reasoning applies as previously because there is no usage items against the annual and
// so, the largest endDate for ii is 2014-08-15, and by default org.killbill.invoice.readMaxRawUsagePreviousPeriod == 2 => targetDate => 2014-06-15
Assert.assertEquals(result.compareTo(new LocalDate(2014, 06, 15)), 0, "142 got " + result);
}
Aggregations