static BigDecimal getMrrFromBillingPeriod(final BillingPeriod period, final BigDecimal price) { if (period == null || period.getNumberOfMonths() == 0) { return BigDecimal.ZERO; } return price.divide(BigDecimal.valueOf(period.getNumberOfMonths()), Rounder.SCALE, RoundingMode.HALF_UP); }
@Override public int hashCode() { int result = productName != null ? productName.hashCode() : 0; result = 31 * result + (planName != null ? planName.hashCode() : 0); result = 31 * result + (billingPeriod != null ? billingPeriod.hashCode() : 0); result = 31 * result + (priceListName != null ? priceListName.hashCode() : 0); result = 31 * result + (finalPhasePrice != null ? finalPhasePrice.hashCode() : 0); return result; } }
private int findNumberOfPlans(final Product product, final BillingPeriod period) { int count = 0; for (final DefaultPlan cur : getPlans()) { if (cur.getProduct().equals(product) && (cur.getBillingPeriod() == null || cur.getBillingPeriod().equals(period))) { count++; } } return count; }
@Test(groups = "fast") public void testJson() throws Exception { final String someUUID = UUID.randomUUID().toString(); final UUID bundleId = UUID.randomUUID(); final String externalKey = UUID.randomUUID().toString(); final List<AuditLogJson> auditLogs = createAuditLogsJson(clock.getUTCNow()); EventSubscriptionJson event = new EventSubscriptionJson(someUUID, BillingPeriod.NO_BILLING_PERIOD.toString(), new LocalDate(), new LocalDate(), "product", "priceList", "eventType", "phase", null); final SubscriptionJson subscription = new SubscriptionJson(someUUID, someUUID, someUUID, externalKey, new LocalDate(), someUUID, someUUID, someUUID, someUUID, new LocalDate(), new LocalDate(), new LocalDate(), new LocalDate(), ImmutableList.<EventSubscriptionJson>of(event), null, null, auditLogs); final BundleJson bundleJson = new BundleJson(someUUID, bundleId.toString(), externalKey, ImmutableList.<SubscriptionJson>of(subscription), auditLogs); Assert.assertEquals(bundleJson.getBundleId(), bundleId.toString()); Assert.assertEquals(bundleJson.getExternalKey(), externalKey); Assert.assertEquals(bundleJson.getSubscriptions().size(), 1); Assert.assertEquals(bundleJson.getAuditLogs(), auditLogs); final String asJson = mapper.writeValueAsString(bundleJson); final BundleJson fromJson = mapper.readValue(asJson, BundleJson.class); Assert.assertEquals(fromJson, bundleJson); } }
@Override public Response doOperation(final CallContext ctx) throws EntitlementApiException, InterruptedException, TimeoutException, AccountApiException { final UUID uuid = UUID.fromString(subscriptionId); final Entitlement current = entitlementApi.getEntitlementForId(uuid, callContext); final LocalDate inputLocalDate = toLocalDate(current.getAccountId(), requestedDate, callContext); final Entitlement newEntitlement; if (requestedDate == null && policyString == null) { newEntitlement = current.changePlan(entitlement.getProductName(), BillingPeriod.valueOf(entitlement.getBillingPeriod()), entitlement.getPriceList(), ctx); } else if (policyString == null) { newEntitlement = current.changePlanWithDate(entitlement.getProductName(), BillingPeriod.valueOf(entitlement.getBillingPeriod()), entitlement.getPriceList(), inputLocalDate, ctx); } else { final BillingActionPolicy policy = BillingActionPolicy.valueOf(policyString.toUpperCase()); newEntitlement = current.changePlanOverrideBillingPolicy(entitlement.getProductName(), BillingPeriod.valueOf(entitlement.getBillingPeriod()), entitlement.getPriceList(), inputLocalDate, policy, ctx); } isImmediateOp = newEntitlement.getLastActiveProduct().getName().equals(entitlement.getProductName()) && newEntitlement.getLastActivePlan().getBillingPeriod() == BillingPeriod.valueOf(entitlement.getBillingPeriod()) && newEntitlement.getLastActivePriceList().getName().equals(entitlement.getPriceList()); return Response.status(Status.OK).build(); }
@Override public List<Listing> getAvailableAddonListings(final String baseProductName) { final List<Listing> availAddons = new ArrayList<Listing>(); try { Product product = findCurrentProduct(baseProductName); if ( product != null ) { for ( Product availAddon : product.getAvailable() ) { for ( BillingPeriod billingPeriod : BillingPeriod.values()) { for( PriceList priceList : getPriceLists().getAllPriceLists()) { Plan addonInList = priceList.findPlan(availAddon, billingPeriod); if ( (addonInList != null) ) { availAddons.add(new DefaultListing(addonInList, priceList)); } } } } } } catch (CatalogApiException e) { // No such product - just return an empty list } return availAddons; }
@Override public DefaultPlan findCurrentPlan(final String productName, final BillingPeriod period, final String priceListName) throws CatalogApiException { if (productName == null) { throw new CatalogApiException(ErrorCode.CAT_NULL_PRODUCT_NAME); } if (priceLists == null) { throw new CatalogApiException(ErrorCode.CAT_PRICE_LIST_NOT_FOUND, priceListName); } final Product product = findCurrentProduct(productName); final DefaultPlan result = priceLists.getPlanFrom(priceListName, product, period); if (result == null) { final String periodString = (period == null) ? "NULL" : period.toString(); throw new CatalogApiException(ErrorCode.CAT_PLAN_NOT_FOUND, productName, periodString, priceListName); } return result; }
@Override public Entitlement doOperation(final CallContext ctx) throws InterruptedException, TimeoutException, EntitlementApiException { final PlanPhaseSpecifier spec = new PlanPhaseSpecifier(entitlement.getProductName(), ProductCategory.valueOf(entitlement.getProductCategory()), BillingPeriod.valueOf(entitlement.getBillingPeriod()), entitlement.getPriceList(), null); final UUID accountId = entitlement.getAccountId() != null ? UUID.fromString(entitlement.getAccountId()) : null; final LocalDate inputLocalDate = toLocalDate(accountId, requestedDate, callContext); final UUID bundleId = entitlement.getBundleId() != null ? UUID.fromString(entitlement.getBundleId()) : null; return (entitlement.getProductCategory().equals(ProductCategory.ADD_ON.toString())) ? entitlementApi.addEntitlement(bundleId, spec, inputLocalDate, callContext) : entitlementApi.createBaseEntitlement(accountId, spec, entitlement.getExternalKey(), inputLocalDate, callContext); }
public LocalDate getFutureBillingDateFor(int nbPeriod) { final int numberOfMonthsPerBillingPeriod = billingPeriod.getNumberOfMonths(); LocalDate proposedDate = firstBillingCycleDate.plusMonths((nbPeriod) * numberOfMonthsPerBillingPeriod); return alignProposedBillCycleDate(proposedDate); }
final List<AuditLogJson> auditLogs = createAuditLogsJson(clock.getUTCNow()); final EventSubscriptionJson newEvent = new EventSubscriptionJson(eventId.toString(), BillingPeriod.NO_BILLING_PERIOD.toString(), requestedDate.toLocalDate(), effectiveDate.toLocalDate(),
@Override public int hashCode() { int result = id != null ? id.hashCode() : 0; result = 31 * result + (entitlementId != null ? entitlementId.hashCode() : 0); result = 31 * result + (effectiveDate != null ? effectiveDate.hashCode() : 0); result = 31 * result + (requestedDate != null ? requestedDate.hashCode() : 0); result = 31 * result + (eventType != null ? eventType.hashCode() : 0); result = 31 * result + (isBlockingEntitlement ? 1 : 0); result = 31 * result + (isBlockingBilling ? 1 : 0); result = 31 * result + (serviceName != null ? serviceName.hashCode() : 0); result = 31 * result + (serviceStateName != null ? serviceStateName.hashCode() : 0); result = 31 * result + (prevProduct != null ? prevProduct.hashCode() : 0); result = 31 * result + (prevPlan != null ? prevPlan.hashCode() : 0); result = 31 * result + (prevPlanPhase != null ? prevPlanPhase.hashCode() : 0); result = 31 * result + (prevPriceList != null ? prevPriceList.hashCode() : 0); result = 31 * result + (prevBillingPeriod != null ? prevBillingPeriod.hashCode() : 0); result = 31 * result + (nextProduct != null ? nextProduct.hashCode() : 0); result = 31 * result + (nextPlan != null ? nextPlan.hashCode() : 0); result = 31 * result + (nextPlanPhase != null ? nextPlanPhase.hashCode() : 0); result = 31 * result + (nextPriceList != null ? nextPriceList.hashCode() : 0); result = 31 * result + (nextBillingPeriod != null ? nextBillingPeriod.hashCode() : 0); result = 31 * result + (createdDate != null ? createdDate.hashCode() : 0); return result; } }
@Override public DefaultPlan findPlan(final Product product, final BillingPeriod period) { for (final DefaultPlan cur : getPlans()) { if (cur.getProduct().equals(product) && (cur.getBillingPeriod() == null || cur.getBillingPeriod().equals(period))) { return cur; } } return null; }
public static int calculateNumberOfWholeBillingPeriods(final LocalDate startDate, final LocalDate endDate, final BillingPeriod billingPeriod) { final int numberOfMonths = Months.monthsBetween(startDate, endDate).getMonths(); final int numberOfMonthsInPeriod = billingPeriod.getNumberOfMonths(); return numberOfMonths / numberOfMonthsInPeriod; }
@Test(groups = "fast") public void testJson() throws Exception { final String productName = UUID.randomUUID().toString(); final String planName = UUID.randomUUID().toString(); final BillingPeriod billingPeriod = BillingPeriod.ANNUAL; final String priceListName = UUID.randomUUID().toString(); final PlanDetailJson planDetailJason = new PlanDetailJson(productName, planName, billingPeriod, priceListName, null); Assert.assertEquals(planDetailJason.getProductName(), productName); Assert.assertEquals(planDetailJason.getPlanName(), planName); Assert.assertEquals(planDetailJason.getBillingPeriod(), billingPeriod); Assert.assertEquals(planDetailJason.getPriceListName(), priceListName); Assert.assertEquals(planDetailJason.getFinalPhasePrice(), null); final String asJson = mapper.writeValueAsString(planDetailJason); Assert.assertEquals(asJson, "{\"productName\":\"" + planDetailJason.getProductName() + "\"," + "\"planName\":\"" + planDetailJason.getPlanName() + "\"," + "\"billingPeriod\":\"" + planDetailJason.getBillingPeriod().toString() + "\"," + "\"priceListName\":\"" + planDetailJason.getPriceListName() + "\"," + "\"finalPhasePrice\":null}"); final PlanDetailJson fromJson = mapper.readValue(asJson, PlanDetailJson.class); Assert.assertEquals(fromJson, planDetailJason); }
@Override public int hashCode() { int result = account != null ? account.hashCode() : 0; result = 31 * result + billCycleDayLocal; result = 31 * result + (subscription != null ? subscription.hashCode() : 0); result = 31 * result + (effectiveDate != null ? effectiveDate.hashCode() : 0); result = 31 * result + (planPhase != null ? planPhase.hashCode() : 0); result = 31 * result + (plan != null ? plan.hashCode() : 0); result = 31 * result + (fixedPrice != null ? fixedPrice.hashCode() : 0); result = 31 * result + (recurringPrice != null ? recurringPrice.hashCode() : 0); result = 31 * result + (currency != null ? currency.hashCode() : 0); result = 31 * result + (description != null ? description.hashCode() : 0); result = 31 * result + (billingModeType != null ? billingModeType.hashCode() : 0); result = 31 * result + (billingPeriod != null ? billingPeriod.hashCode() : 0); result = 31 * result + (type != null ? type.hashCode() : 0); result = 31 * result + (totalOrdering != null ? totalOrdering.hashCode() : 0); result = 31 * result + (timeZone != null ? timeZone.hashCode() : 0); return result; }
public T getResult(final PlanPhaseSpecifier from, final PlanSpecifier to, final StandaloneCatalog catalog) throws CatalogApiException { if ( (phaseType == null || from.getPhaseType() == phaseType) && (fromProduct == null || fromProduct.equals(catalog.findCurrentProduct(from.getProductName()))) && (fromProductCategory == null || fromProductCategory.equals(from.getProductCategory())) && (fromBillingPeriod == null || fromBillingPeriod.equals(from.getBillingPeriod())) && (toProduct == null || toProduct.equals(catalog.findCurrentProduct(to.getProductName()))) && (toProductCategory == null || toProductCategory.equals(to.getProductCategory())) && (toBillingPeriod == null || toBillingPeriod.equals(to.getBillingPeriod())) && (fromPriceList == null || fromPriceList.equals(catalog.findCurrentPriceList(from.getPriceListName()))) && (toPriceList == null || toPriceList.equals(catalog.findCurrentPriceList(to.getPriceListName()))) ) { return getResult(); } return null; }
public static BigDecimal calculateProRationAfterLastBillingCycleDate(final LocalDate endDate, final LocalDate previousBillThroughDate, final BillingPeriod billingPeriod) { // Note: assumption is that previousBillThroughDate is correctly aligned with the billing cycle day final LocalDate nextBillThroughDate = previousBillThroughDate.plusMonths(billingPeriod.getNumberOfMonths()); return calculateProrationBetweenDates(previousBillThroughDate, endDate, previousBillThroughDate, nextBillThroughDate); }
public BusinessInvoiceItemModelDao(@Nullable final String externalKey, final InvoiceItem invoiceItem, @Nullable final Plan plan, @Nullable final PlanPhase planPhase) { this(invoiceItem.getAmount(), planPhase != null ? planPhase.getBillingPeriod().toString() : null, invoiceItem.getCreatedDate(), invoiceItem.getCurrency(), /* Populate end date for fixed items for convenience (null in invoice_items table) */ (invoiceItem.getEndDate() == null && planPhase != null) ? invoiceItem.getStartDate().plus(planPhase.getDuration().toJodaPeriod()) : invoiceItem.getEndDate(), externalKey, invoiceItem.getInvoiceId(), invoiceItem.getId(), invoiceItem.getLinkedItemId(), invoiceItem.getInvoiceItemType().toString(), planPhase != null ? planPhase.getPhaseType().toString() : null, plan != null ? plan.getProduct().getCategory().toString() : null, plan != null ? plan.getProduct().getName() : null, plan != null ? plan.getProduct().getCatalogName() : null, planPhase != null ? planPhase.getName() : null, invoiceItem.getStartDate(), invoiceItem.getUpdatedDate()); }
protected boolean satisfiesCase(final PlanSpecifier planPhase, final StandaloneCatalog c) throws CatalogApiException { return (getProduct() == null || getProduct().equals(c.findCurrentProduct(planPhase.getProductName()))) && (getProductCategory() == null || getProductCategory().equals(planPhase.getProductCategory())) && (getBillingPeriod() == null || getBillingPeriod().equals(planPhase.getBillingPeriod())) && (getPriceList() == null || getPriceList().equals(c.findCurrentPriceList(planPhase.getPriceListName()))); }
public static BigDecimal calculateProRationBeforeFirstBillingPeriod(final LocalDate startDate, final LocalDate nextBillingCycleDate, final BillingPeriod billingPeriod) { final LocalDate previousBillingCycleDate = nextBillingCycleDate.plusMonths(-billingPeriod.getNumberOfMonths()); return calculateProrationBetweenDates(startDate, nextBillingCycleDate, previousBillingCycleDate, nextBillingCycleDate); }