use of org.killbill.billing.usage.api.RolledUpUsage in project killbill by killbill.
the class ContiguousIntervalUsageInArrear method computeMissingItemsAndNextNotificationDate.
/**
* Compute the missing usage invoice items based on what should be billed and what has been billed ($ amount comparison).
*
* @param existingUsage existing on disk usage items for the subscription
* @throws CatalogApiException
*/
public UsageInArrearItemsAndNextNotificationDate computeMissingItemsAndNextNotificationDate(final List<InvoiceItem> existingUsage) throws CatalogApiException {
Preconditions.checkState(isBuilt.get());
if (transitionTimes.size() < 2) {
return new UsageInArrearItemsAndNextNotificationDate(ImmutableList.<InvoiceItem>of(), computeNextNotificationDate());
}
final List<InvoiceItem> result = Lists.newLinkedList();
// We start by generating 'marker' USAGE items with $0 that will allow to correctly insert the next notification for when there is no USAGE to bill.
// Those will be removed by the invoicing code later so as to not end up with superfluous $0 items
LocalDate prevDate = null;
for (final LocalDate curDate : transitionTimes) {
if (prevDate != null) {
final InvoiceItem item = new UsageInvoiceItem(invoiceId, accountId, getBundleId(), getSubscriptionId(), getPlanName(), getPhaseName(), usage.getName(), prevDate, curDate, BigDecimal.ZERO, getCurrency());
result.add(item);
}
prevDate = curDate;
}
final List<RolledUpUsage> allUsage = getRolledUpUsage();
for (final RolledUpUsage ru : allUsage) {
BigDecimal toBeBilledUsage = BigDecimal.ZERO;
if (usage.getUsageType() == UsageType.CAPACITY) {
toBeBilledUsage = computeToBeBilledCapacityInArrear(ru.getRolledUpUnits());
} else /* UsageType.CONSUMABLE */
{
// Compute total price amount that should be billed for that period of time (and usage section) across unitTypes.
for (final RolledUpUnit cur : ru.getRolledUpUnits()) {
if (!unitTypes.contains(cur.getUnitType())) {
log.warn("ContiguousIntervalConsumableInArrear is skipping unitType " + cur.getUnitType());
continue;
}
final BigDecimal toBeBilledForUnit = computeToBeBilledConsumableInArrear(cur);
toBeBilledUsage = toBeBilledUsage.add(toBeBilledForUnit);
}
}
// Retrieves current price amount billed for that period of time (and usage section)
final Iterable<InvoiceItem> billedItems = getBilledItems(ru.getStart(), ru.getEnd(), existingUsage);
final BigDecimal billedUsage = computeBilledUsage(billedItems);
// Compare the two and add the missing piece if required.
if (!billedItems.iterator().hasNext() || billedUsage.compareTo(toBeBilledUsage) < 0) {
final BigDecimal amountToBill = toBeBilledUsage.subtract(billedUsage);
if (amountToBill.compareTo(BigDecimal.ZERO) > 0) {
final InvoiceItem item = new UsageInvoiceItem(invoiceId, accountId, getBundleId(), getSubscriptionId(), getPlanName(), getPhaseName(), usage.getName(), ru.getStart(), ru.getEnd(), amountToBill, getCurrency());
result.add(item);
}
}
}
final LocalDate nextNotificationdate = computeNextNotificationDate();
return new UsageInArrearItemsAndNextNotificationDate(result, nextNotificationdate);
}
use of org.killbill.billing.usage.api.RolledUpUsage in project killbill by killbill.
the class UsageResource method getUsage.
@TimedResource
@GET
@Path("/{subscriptionId:" + UUID_PATTERN + "}/{unitType}")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve usage for a subscription and unit type", response = RolledUpUsageJson.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Missing start date or end date") })
public Response getUsage(@PathParam("subscriptionId") final String subscriptionId, @PathParam("unitType") final String unitType, @QueryParam(QUERY_START_DATE) final String startDate, @QueryParam(QUERY_END_DATE) final String endDate, @javax.ws.rs.core.Context final HttpServletRequest request) {
if (startDate == null || endDate == null) {
return Response.status(Status.BAD_REQUEST).build();
}
final TenantContext tenantContext = context.createContext(request);
final LocalDate usageStartDate = LOCAL_DATE_FORMATTER.parseLocalDate(startDate);
final LocalDate usageEndDate = LOCAL_DATE_FORMATTER.parseLocalDate(endDate);
final RolledUpUsage usage = usageUserApi.getUsageForSubscription(UUID.fromString(subscriptionId), unitType, usageStartDate, usageEndDate, tenantContext);
final RolledUpUsageJson result = new RolledUpUsageJson(usage);
return Response.status(Status.OK).entity(result).build();
}
use of org.killbill.billing.usage.api.RolledUpUsage in project killbill by killbill.
the class UsageResource method getAllUsage.
@TimedResource
@GET
@Path("/{subscriptionId:" + UUID_PATTERN + "}")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve usage for a subscription", response = RolledUpUsageJson.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Missing start date or end date") })
public Response getAllUsage(@PathParam("subscriptionId") final String subscriptionId, @QueryParam(QUERY_START_DATE) final String startDate, @QueryParam(QUERY_END_DATE) final String endDate, @javax.ws.rs.core.Context final HttpServletRequest request) {
if (startDate == null || endDate == null) {
return Response.status(Status.BAD_REQUEST).build();
}
final TenantContext tenantContext = context.createContext(request);
final LocalDate usageStartDate = LOCAL_DATE_FORMATTER.parseLocalDate(startDate);
final LocalDate usageEndDate = LOCAL_DATE_FORMATTER.parseLocalDate(endDate);
// The current JAXRS API only allows to look for one transition
final List<LocalDate> startEndDate = ImmutableList.<LocalDate>builder().add(usageStartDate).add(usageEndDate).build();
final List<RolledUpUsage> usage = usageUserApi.getAllUsageForSubscription(UUID.fromString(subscriptionId), startEndDate, tenantContext);
final RolledUpUsageJson result = new RolledUpUsageJson(usage.get(0));
return Response.status(Status.OK).entity(result).build();
}
use of org.killbill.billing.usage.api.RolledUpUsage in project killbill by killbill.
the class DefaultUsageUserApi method getAllUsageForSubscription.
@Override
public List<RolledUpUsage> getAllUsageForSubscription(final UUID subscriptionId, final List<LocalDate> transitionTimes, final TenantContext tenantContext) {
final InternalTenantContext internalCallContext = internalCallContextFactory.createInternalTenantContext(subscriptionId, ObjectType.SUBSCRIPTION, tenantContext);
List<RolledUpUsage> result = new ArrayList<RolledUpUsage>();
LocalDate prevDate = null;
for (LocalDate curDate : transitionTimes) {
if (prevDate != null) {
final List<RolledUpUsageModelDao> usageForSubscription = rolledUpUsageDao.getAllUsageForSubscription(subscriptionId, prevDate, curDate, internalCallContext);
final List<RolledUpUnit> rolledUpAmount = getRolledUpUnits(usageForSubscription);
result.add(new DefaultRolledUpUsage(subscriptionId, prevDate, curDate, rolledUpAmount));
}
prevDate = curDate;
}
return result;
}
use of org.killbill.billing.usage.api.RolledUpUsage in project killbill by killbill.
the class ContiguousIntervalUsageInArrear method getRolledUpUsage.
@VisibleForTesting
List<RolledUpUsage> getRolledUpUsage() {
final Iterator<RawUsage> rawUsageIterator = rawSubscriptionUsage.iterator();
if (!rawUsageIterator.hasNext()) {
return ImmutableList.of();
}
final List<RolledUpUsage> result = new ArrayList<RolledUpUsage>();
//
// Skip all items before our first transition date
//
// prevRawUsage keeps track of first unconsumed raw usage element
RawUsage prevRawUsage = null;
while (rawUsageIterator.hasNext()) {
final RawUsage curRawUsage = rawUsageIterator.next();
if (curRawUsage.getDate().compareTo(transitionTimes.get(0)) >= 0) {
prevRawUsage = curRawUsage;
break;
}
}
// Optimize path where all raw usage items are outside or our transitionTimes range
if (prevRawUsage.getDate().compareTo(transitionTimes.get(transitionTimes.size() - 1)) >= 0) {
return ImmutableList.of();
}
//
// Loop through each interval [prevDate, curDate) and consume as many rawSubscriptionUsage elements within that range
// to create one RolledUpUsage per interval. If an interval does not have any rawSubscriptionUsage element, there will be no
// matching RolledUpUsage for that interval, and we'll detect that in the 'computeMissingItems' logic
//
LocalDate prevDate = null;
for (final LocalDate curDate : transitionTimes) {
if (prevDate != null) {
// Allocate new perRangeUnitToAmount for this interval and populate with rawSubscriptionUsage items
final Map<String, Long> perRangeUnitToAmount = new HashMap<String, Long>();
// Start consuming prevRawUsage element if it exists and falls into the range
if (prevRawUsage != null) {
if (prevRawUsage.getDate().compareTo(prevDate) >= 0 && prevRawUsage.getDate().compareTo(curDate) < 0) {
final Long currentAmount = perRangeUnitToAmount.get(prevRawUsage.getUnitType());
final Long updatedAmount = computeUpdatedAmount(currentAmount, prevRawUsage.getAmount());
perRangeUnitToAmount.put(prevRawUsage.getUnitType(), updatedAmount);
prevRawUsage = null;
}
}
//
if (prevRawUsage == null) {
while (rawUsageIterator.hasNext()) {
final RawUsage curRawUsage = rawUsageIterator.next();
if (curRawUsage.getDate().compareTo(curDate) >= 0) {
prevRawUsage = curRawUsage;
break;
}
final Long currentAmount = perRangeUnitToAmount.get(curRawUsage.getUnitType());
final Long updatedAmount = computeUpdatedAmount(currentAmount, curRawUsage.getAmount());
perRangeUnitToAmount.put(curRawUsage.getUnitType(), updatedAmount);
}
}
// If we did find some usage for that date range, let's populate the result
if (!perRangeUnitToAmount.isEmpty()) {
final List<RolledUpUnit> rolledUpUnits = new ArrayList<RolledUpUnit>(perRangeUnitToAmount.size());
for (final String unitType : perRangeUnitToAmount.keySet()) {
rolledUpUnits.add(new DefaultRolledUpUnit(unitType, perRangeUnitToAmount.get(unitType)));
}
result.add(new DefaultRolledUpUsage(getSubscriptionId(), prevDate, curDate, rolledUpUnits));
}
}
prevDate = curDate;
}
return result;
}
Aggregations