@Override
  public BillingEventSet getBillingEventsForAccountAndUpdateAccountBCD(
      final UUID accountId, final InternalCallContext context) {
    final List<SubscriptionBaseBundle> bundles =
        subscriptionApi.getBundlesForAccount(accountId, context);
    final DefaultBillingEventSet result = new DefaultBillingEventSet();

    try {
      final Account account = accountApi.getAccountById(accountId, context);

      // Check to see if billing is off for the account
      final List<Tag> accountTags = tagApi.getTags(accountId, ObjectType.ACCOUNT, context);
      final boolean found_AUTO_INVOICING_OFF = is_AUTO_INVOICING_OFF(accountTags);
      if (found_AUTO_INVOICING_OFF) {
        result.setAccountAutoInvoiceIsOff(true);
        return result; // billing is off, we are done
      }

      addBillingEventsForBundles(bundles, account, context, result);
    } catch (AccountApiException e) {
      log.warn("Failed while getting BillingEvent", e);
    }

    // Pretty-print the events, before and after the blocking calculator does its magic
    final StringBuilder logStringBuilder =
        new StringBuilder("Computed billing events for accountId ").append(accountId);
    eventsToString(logStringBuilder, result, "\nBilling Events Raw");
    blockCalculator.insertBlockingEvents(result, context);
    eventsToString(logStringBuilder, result, "\nBilling Events After Blocking");
    log.info(logStringBuilder.toString());

    return result;
  }
  private void addBillingEventsForSubscription(
      final List<SubscriptionBase> subscriptions,
      final SubscriptionBaseBundle bundle,
      final Account account,
      final InternalCallContext context,
      final DefaultBillingEventSet result) {

    boolean updatedAccountBCD = false;
    for (final SubscriptionBase subscription : subscriptions) {
      for (final EffectiveSubscriptionInternalEvent transition :
          subscriptionApi.getBillingTransitions(subscription, context)) {
        try {
          final int bcdLocal =
              bcdCalculator.calculateBcd(bundle, subscription, transition, account, context);

          if (account.getBillCycleDayLocal() == 0 && !updatedAccountBCD) {
            final MutableAccountData modifiedData = account.toMutableAccountData();
            modifiedData.setBillCycleDayLocal(bcdLocal);
            accountApi.updateAccount(account.getExternalKey(), modifiedData, context);
            updatedAccountBCD = true;
          }

          final BillingEvent event =
              new DefaultBillingEvent(
                  account,
                  transition,
                  subscription,
                  bcdLocal,
                  account.getCurrency(),
                  catalogService.getFullCatalog());
          result.add(event);
        } catch (CatalogApiException e) {
          log.error(
              "Failing to identify catalog components while creating BillingEvent from transition: "
                  + transition.getId().toString(),
              e);
        } catch (Exception e) {
          log.warn("Failed while getting BillingEvent", e);
        }
      }
    }
  }
  private void addBillingEventsForBundles(
      final List<SubscriptionBaseBundle> bundles,
      final Account account,
      final InternalCallContext context,
      final DefaultBillingEventSet result) {
    for (final SubscriptionBaseBundle bundle : bundles) {
      final List<SubscriptionBase> subscriptions =
          subscriptionApi.getSubscriptionsForBundle(bundle.getId(), context);

      // Check if billing is off for the bundle
      final List<Tag> bundleTags = tagApi.getTags(bundle.getId(), ObjectType.BUNDLE, context);
      boolean found_AUTO_INVOICING_OFF = is_AUTO_INVOICING_OFF(bundleTags);
      if (found_AUTO_INVOICING_OFF) {
        for (final SubscriptionBase subscription :
            subscriptions) { // billing is off so list sub ids in set to be excluded
          result.getSubscriptionIdsWithAutoInvoiceOff().add(subscription.getId());
        }
      } else { // billing is not off
        addBillingEventsForSubscription(subscriptions, bundle, account, context, result);
      }
    }
  }