@Override public DateTimeZone getAccountTimeZone() { return account.getTimeZone(); }
private LocalDate toLocalDate(final Account account, final DateTime inputDate, final TenantContext context) { if (account == null && inputDate == null) { // We have no inputDate and so accountTimeZone so we default to LocalDate as seen in UTC return new LocalDate(clock.getUTCNow(), DateTimeZone.UTC); } else if (account == null && inputDate != null) { // We were given a date but can't get timezone, default in UTC return new LocalDate(inputDate, DateTimeZone.UTC); } else if (account != null && inputDate == null) { // We have no inputDate but for accountTimeZone so default to LocalDate as seen in account timezone return new LocalDate(clock.getUTCNow(), account.getTimeZone()); } else { // Precise LocalDate as requested return new LocalDate(inputDate, account.getTimeZone()); } }
private void computeStateForEntitlement() { // Current state for the ENTITLEMENT_SERVICE_NAME is set to cancelled if (entitlementEffectiveEndDate != null && entitlementEffectiveEndDate.compareTo(new LocalDate(utcNow, account.getTimeZone())) <= 0) { entitlementState = EntitlementState.CANCELLED; } else { // Gather states across all services and check if one of them is set to 'blockEntitlement' entitlementState = (blockingAggregator != null && blockingAggregator.isBlockEntitlement() ? EntitlementState.BLOCKED : EntitlementState.ACTIVE); } }
private void computeEntitlementEffectiveEndDate() { LocalDate result = null; BlockingState lastEntry; lastEntry = (!subscriptionEntitlementStates.isEmpty()) ? subscriptionEntitlementStates.get(subscriptionEntitlementStates.size() - 1) : null; if (lastEntry != null && DefaultEntitlementApi.ENT_STATE_CANCELLED.equals(lastEntry.getStateName())) { result = new LocalDate(lastEntry.getEffectiveDate(), account.getTimeZone()); } lastEntry = (!bundleEntitlementStates.isEmpty()) ? bundleEntitlementStates.get(bundleEntitlementStates.size() - 1) : null; if (lastEntry != null && DefaultEntitlementApi.ENT_STATE_CANCELLED.equals(lastEntry.getStateName())) { final LocalDate localDate = new LocalDate(lastEntry.getEffectiveDate(), account.getTimeZone()); result = ((result == null) || (localDate.compareTo(result) < 0)) ? localDate : result; } lastEntry = (!accountEntitlementStates.isEmpty()) ? accountEntitlementStates.get(accountEntitlementStates.size() - 1) : null; if (lastEntry != null && DefaultEntitlementApi.ENT_STATE_CANCELLED.equals(lastEntry.getStateName())) { final LocalDate localDate = new LocalDate(lastEntry.getEffectiveDate(), account.getTimeZone()); result = ((result == null) || (localDate.compareTo(result) < 0)) ? localDate : result; } entitlementEffectiveEndDate = result; }
private Account createAccount(final int billCycleDay) throws AccountApiException { final Account account = Mockito.mock(Account.class); Mockito.when(account.getBillCycleDayLocal()).thenReturn(billCycleDay); Mockito.when(account.getCurrency()).thenReturn(Currency.USD); Mockito.when(account.getId()).thenReturn(UUID.randomUUID()); Mockito.when(account.getTimeZone()).thenReturn(DateTimeZone.UTC); Mockito.when(accountInternalApi.getAccountById(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(account); return account; }
@Override @BeforeMethod(groups = "fast") public void beforeMethod() throws Exception { super.beforeMethod(); final Account account = Mockito.mock(Account.class); Mockito.when(account.getTimeZone()).thenReturn(DateTimeZone.UTC); Mockito.when(accountApi.getAccountById(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(account); }
public TestDateConversion() throws AccountApiException { super(null, null, null, null, Mockito.mock(AccountUserApi.class), new ClockMock(), null); final Account account = Mockito.mock(Account.class); Mockito.when(account.getTimeZone()).thenReturn(dateTimeZone); Mockito.when(accountUserApi.getAccountById(accountId, null)).thenReturn(account); }
public BillingState calculateBillingState(final Account account, final InternalTenantContext context) throws OverdueException { final SortedSet<Invoice> unpaidInvoices = unpaidInvoicesForAccount(account.getId(), account.getTimeZone(), context); final int numberOfUnpaidInvoices = unpaidInvoices.size(); final BigDecimal unpaidInvoiceBalance = sumBalance(unpaidInvoices); LocalDate dateOfEarliestUnpaidInvoice = null; UUID idOfEarliestUnpaidInvoice = null; final Invoice invoice = earliest(unpaidInvoices); if (invoice != null) { dateOfEarliestUnpaidInvoice = invoice.getInvoiceDate(); idOfEarliestUnpaidInvoice = invoice.getId(); } final PaymentResponse responseForLastFailedPayment = PaymentResponse.INSUFFICIENT_FUNDS; //TODO MDW final Tag[] tags = new Tag[]{}; //TODO MDW return new BillingState(account.getId(), numberOfUnpaidInvoices, unpaidInvoiceBalance, dateOfEarliestUnpaidInvoice, account.getTimeZone(), idOfEarliestUnpaidInvoice, responseForLastFailedPayment, tags); }
private void verifyBCDCalculation(final DateTimeZone accountTimeZone, final DateTime startDateUTC, final int bcdLocal) throws AccountApiException, CatalogApiException { final BillCycleDayCalculator billCycleDayCalculator = new BillCycleDayCalculator(Mockito.mock(CatalogService.class), Mockito.mock(SubscriptionBaseInternalApi.class)); final SubscriptionBase subscription = Mockito.mock(SubscriptionBase.class); Mockito.when(subscription.getStartDate()).thenReturn(startDateUTC); final Plan plan = Mockito.mock(Plan.class); Mockito.when(plan.dateOfFirstRecurringNonZeroCharge(startDateUTC, null)).thenReturn(startDateUTC); final Account account = Mockito.mock(Account.class); Mockito.when(account.getTimeZone()).thenReturn(accountTimeZone); final Integer bcd = billCycleDayCalculator.calculateBcdFromSubscription(subscription, plan, account, Mockito.mock(Catalog.class), internalCallContext); Assert.assertEquals(bcd, (Integer) bcdLocal); } }
public DateTime fromNowAndReferenceTime(final DateTime referenceDateTime, final InternalTenantContext callContext) throws EntitlementApiException { try { final Account account = accountApi.getAccountByRecordId(callContext.getAccountRecordId(), callContext); return fromNowAndReferenceTime(referenceDateTime, account.getTimeZone()); } catch (AccountApiException e) { throw new EntitlementApiException(e); } }
public DateTime fromLocalDateAndReferenceTime(final LocalDate requestedDate, final DateTime referenceDateTime, final InternalTenantContext callContext) throws EntitlementApiException { try { final Account account = accountApi.getAccountByRecordId(callContext.getAccountRecordId(), callContext); return fromLocalDateAndReferenceTime(requestedDate, referenceDateTime, account.getTimeZone()); } catch (AccountApiException e) { throw new EntitlementApiException(e); } }
@POST @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) public Response createCredit(final CreditJson json, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final HttpServletRequest request, @javax.ws.rs.core.Context final UriInfo uriInfo) throws AccountApiException, InvoiceApiException { final CallContext callContext = context.createContext(createdBy, reason, comment, request); final Account account = accountUserApi.getAccountById(UUID.fromString(json.getAccountId()), callContext); final LocalDate effectiveDate = new LocalDate(clock.getUTCNow(), account.getTimeZone()); final InvoiceItem credit; if (json.getInvoiceId() != null) { // Apply an invoice level credit credit = invoiceUserApi.insertCreditForInvoice(account.getId(), UUID.fromString(json.getInvoiceId()), json.getCreditAmount(), effectiveDate, account.getCurrency(), callContext); } else { // Apply a account level credit credit = invoiceUserApi.insertCredit(account.getId(), json.getCreditAmount(), effectiveDate, account.getCurrency(), callContext); } return uriBuilder.buildResponse(uriInfo, CreditResource.class, "getCredit", credit.getId()); }
@Test(groups = "fast") public void testCalculateBCDForAOWithBPCancelledBundleAligned() throws Exception { final DateTimeZone accountTimeZone = DateTimeZone.UTC; final DateTime bpStartDateUTC = new DateTime(2012, 7, 16, 21, 0, 0, DateTimeZone.UTC); final int expectedBCDUTC = 16; // Create a Bundle associated with a subscription final SubscriptionBaseBundle bundle = Mockito.mock(SubscriptionBaseBundle.class); final SubscriptionBase subscription = Mockito.mock(SubscriptionBase.class); Mockito.when(subscription.getStartDate()).thenReturn(bpStartDateUTC); // subscription.getCurrentPlan() will return null as expected (cancelled BP) Mockito.when(subscriptionInternalApi.getBaseSubscription(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(subscription); // Create a the base plan associated with that subscription final Plan plan = Mockito.mock(Plan.class); Mockito.when(plan.dateOfFirstRecurringNonZeroCharge(bpStartDateUTC, null)).thenReturn(bpStartDateUTC); final Catalog catalog = Mockito.mock(Catalog.class); Mockito.when(catalog.findPlan(Mockito.anyString(), Mockito.<DateTime>any(), Mockito.<DateTime>any())).thenReturn(plan); Mockito.when(subscription.getLastActivePlan()).thenReturn(plan); final Account account = Mockito.mock(Account.class); Mockito.when(account.getTimeZone()).thenReturn(accountTimeZone); final Integer billCycleDayLocal = billCycleDayCalculator.calculateBcdForAlignment(BillingAlignment.BUNDLE, bundle, subscription, account, catalog, null, internalCallContext); Assert.assertEquals(billCycleDayLocal, (Integer) expectedBCDUTC); }
final int bcdLocal = date.toDateTime(account.getTimeZone()).getDayOfMonth(); log.info("Calculated BCD: subscription id {}, subscription start {}, timezone {}, bcd UTC {}, bcd local {}", subscription.getId(), date.toDateTimeISO(), account.getTimeZone(), bcdUTC, bcdLocal);
public Account createAccount(final LocalDate dateOfLastUnPaidInvoice) throws SubscriptionBaseApiException, AccountApiException { final UUID accountId = UUID.randomUUID(); final Account account = Mockito.mock(Account.class); Mockito.when(account.getId()).thenReturn(accountId); Mockito.when(account.getTimeZone()).thenReturn(DateTimeZone.UTC); Mockito.when(accountInternalApi.getAccountById(Mockito.eq(account.getId()), Mockito.<InternalTenantContext>any())).thenReturn(account); final Invoice invoice = Mockito.mock(Invoice.class); Mockito.when(invoice.getInvoiceDate()).thenReturn(dateOfLastUnPaidInvoice); Mockito.when(invoice.getBalance()).thenReturn(BigDecimal.TEN); Mockito.when(invoice.getId()).thenReturn(UUID.randomUUID()); final InvoiceItem item = Mockito.mock(InvoiceItem.class); final List<InvoiceItem> items = new ArrayList<InvoiceItem>(); items.add(item); Mockito.when(invoice.getInvoiceItems()).thenReturn(items); final List<Invoice> invoices = new ArrayList<Invoice>(); invoices.add(invoice); Mockito.when(invoiceInternalApi.getUnpaidInvoicesByAccountId(Mockito.<UUID>any(), Mockito.<LocalDate>any(), Mockito.<InternalTenantContext>any())).thenReturn(invoices); return account; }
@Override public Invoice triggerInvoiceGeneration(final UUID accountId, final LocalDate targetDate, final boolean dryRun, final CallContext context) throws InvoiceApiException { final InternalCallContext internalContext = internalCallContextFactory.createInternalCallContext(accountId, context); final Account account; try { account = accountUserApi.getAccountById(accountId, internalContext); } catch (AccountApiException e) { throw new InvoiceApiException(e, ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_ID, e.toString()); } final DateTime processingDateTime = targetDate.toDateTimeAtCurrentTime(account.getTimeZone()); final Invoice result = dispatcher.processAccount(accountId, processingDateTime, dryRun, internalContext); if (result == null) { throw new InvoiceApiException(ErrorCode.INVOICE_NOTHING_TO_DO, accountId, targetDate); } else { return result; } }
for (final Entitlement cur : toBeCancelled) { try { cur.cancelEntitlementWithDateOverrideBillingPolicy(new LocalDate(clock.getUTCNow(), account.getTimeZone()), actionPolicy, context.toCallContext(tenantId)); } catch (EntitlementApiException e) {
public AccountJson(final Account account, final BigDecimal accountBalance, final BigDecimal accountCBA, @Nullable final AccountAuditLogs accountAuditLogs) { super(toAuditLogJson(accountAuditLogs == null ? null : accountAuditLogs.getAuditLogsForAccount())); this.accountCBA = accountCBA; this.accountBalance = accountBalance; this.accountId = account.getId().toString(); this.externalKey = account.getExternalKey(); this.name = account.getName(); this.firstNameLength = account.getFirstNameLength(); this.email = account.getEmail(); this.billCycleDayLocal = account.getBillCycleDayLocal(); this.currency = account.getCurrency() != null ? account.getCurrency().toString() : null; this.paymentMethodId = account.getPaymentMethodId() != null ? account.getPaymentMethodId().toString() : null; this.timeZone = account.getTimeZone().toString(); this.address1 = account.getAddress1(); this.address2 = account.getAddress2(); this.postalCode = account.getPostalCode(); this.company = account.getCompanyName(); this.city = account.getCity(); this.state = account.getStateOrProvince(); this.country = account.getCountry(); this.locale = account.getLocale(); this.phone = account.getPhone(); this.isMigrated = account.isMigrated(); this.isNotifiedForInvoices = account.isNotifiedForInvoices(); }
final DateTime effectiveDate = dateHelper.fromLocalDateAndReferenceTime(localEffectiveDate, baseSubscription.getStartDate(), contextWithValidAccountRecordId); if (!dateHelper.isBeforeOrEqualsToday(effectiveDate, account.getTimeZone())) { recordPauseResumeNotificationEntry(baseSubscription.getId(), bundleId, effectiveDate, true, contextWithValidAccountRecordId); return;
public DefaultBillingEvent(final Account account, final EffectiveSubscriptionInternalEvent transition, final SubscriptionBase subscription, final int billCycleDayLocal, final Currency currency, final Catalog catalog) throws CatalogApiException { this.account = account; this.billCycleDayLocal = billCycleDayLocal; this.subscription = subscription; effectiveDate = transition.getEffectiveTransitionTime(); final String planPhaseName = (transition.getTransitionType() != SubscriptionBaseTransitionType.CANCEL) ? transition.getNextPhase() : transition.getPreviousPhase(); planPhase = (planPhaseName != null) ? catalog.findPhase(planPhaseName, transition.getEffectiveTransitionTime(), transition.getSubscriptionStartDate()) : null; final String planName = (transition.getTransitionType() != SubscriptionBaseTransitionType.CANCEL) ? transition.getNextPlan() : transition.getPreviousPlan(); plan = (planName != null) ? catalog.findPlan(planName, transition.getEffectiveTransitionTime(), transition.getSubscriptionStartDate()) : null; final String nextPhaseName = transition.getNextPhase(); final PlanPhase nextPhase = (nextPhaseName != null) ? catalog.findPhase(nextPhaseName, transition.getEffectiveTransitionTime(), transition.getSubscriptionStartDate()) : null; final String prevPhaseName = transition.getPreviousPhase(); final PlanPhase prevPhase = (prevPhaseName != null) ? catalog.findPhase(prevPhaseName, transition.getEffectiveTransitionTime(), transition.getSubscriptionStartDate()) : null; fixedPrice = (nextPhase != null && nextPhase.getFixedPrice() != null) ? nextPhase.getFixedPrice().getPrice(currency) : null; recurringPrice = (nextPhase != null && nextPhase.getRecurringPrice() != null) ? nextPhase.getRecurringPrice().getPrice(currency) : null; this.currency = currency; description = transition.getTransitionType().toString(); billingModeType = BillingModeType.IN_ADVANCE; billingPeriod = (transition.getTransitionType() != SubscriptionBaseTransitionType.CANCEL) ? nextPhase.getBillingPeriod() : prevPhase.getBillingPeriod(); type = transition.getTransitionType(); totalOrdering = transition.getTotalOrdering(); timeZone = account.getTimeZone(); }