Esempio n. 1
0
 /* (non-Javadoc)
  * @see org.killbill.billing.catalog.IPriceList#findPlan(org.killbill.billing.catalog.api.IProduct, org.killbill.billing.catalog.api.BillingPeriod)
  */
 @Override
 public Collection<Plan> findPlans(final Product product, final BillingPeriod period) {
   final List<Plan> result = new ArrayList<Plan>(plans.size());
   for (final Plan cur : getPlans()) {
     if (cur.getProduct().equals(product)
         && (cur.getRecurringBillingPeriod() != null
             && cur.getRecurringBillingPeriod().equals(period))) {
       result.add(cur);
     }
   }
   return result;
 }
  private void checkForTaxCodesOnProducts(
      final Invoice invoice,
      final Collection<PluginProperty> properties,
      final TenantContext context) {
    final Map<String, String> planToProductCache = new HashMap<String, String>();
    final Map<String, String> productToTaxCodeCache = new HashMap<String, String>();

    for (final InvoiceItem invoiceItem : invoice.getInvoiceItems()) {
      final String planName = invoiceItem.getPlanName();
      if (planName == null) {
        continue;
      }

      if (planToProductCache.get(planName) == null) {
        try {
          final StaticCatalog catalog =
              killbillAPI.getCatalogUserApi().getCurrentCatalog(null, context);
          final Plan plan = catalog.findCurrentPlan(planName);
          planToProductCache.put(planName, plan.getProduct().getName());
        } catch (final CatalogApiException e) {
          continue;
        }
      }
      final String productName = planToProductCache.get(planName);
      if (productName == null) {
        continue;
      }

      if (productToTaxCodeCache.get(productName) == null) {
        try {
          final String taxCode = dao.getTaxCode(productName, context.getTenantId());
          productToTaxCodeCache.put(productName, taxCode);
        } catch (final SQLException e) {
          continue;
        }
      }

      final String taxCode = productToTaxCodeCache.get(productName);
      if (taxCode != null) {
        addTaxCodeToInvoiceItem(
            invoiceItem.getId(), productToTaxCodeCache.get(productName), properties);
      }
    }
  }
  @Override
  public SubscriptionBase createSubscription(
      final UUID bundleId,
      final PlanPhaseSpecifier spec,
      final DateTime requestedDateWithMs,
      final InternalCallContext context)
      throws SubscriptionBaseApiException {
    try {
      final String realPriceList =
          (spec.getPriceListName() == null)
              ? PriceListSet.DEFAULT_PRICELIST_NAME
              : spec.getPriceListName();
      final DateTime now = clock.getUTCNow();
      final DateTime requestedDate =
          (requestedDateWithMs != null) ? DefaultClock.truncateMs(requestedDateWithMs) : now;
      if (requestedDate.isAfter(now)) {
        throw new SubscriptionBaseApiException(
            ErrorCode.SUB_INVALID_REQUESTED_DATE, now.toString(), requestedDate.toString());
      }
      final DateTime effectiveDate = requestedDate;

      final Catalog catalog = catalogService.getFullCatalog();
      final Plan plan =
          catalog.findPlan(
              spec.getProductName(), spec.getBillingPeriod(), realPriceList, requestedDate);

      final PlanPhase phase = plan.getAllPhases()[0];
      if (phase == null) {
        throw new SubscriptionBaseError(
            String.format(
                "No initial PlanPhase for Product %s, term %s and set %s does not exist in the catalog",
                spec.getProductName(), spec.getBillingPeriod().toString(), realPriceList));
      }

      final SubscriptionBaseBundle bundle = dao.getSubscriptionBundleFromId(bundleId, context);
      if (bundle == null) {
        throw new SubscriptionBaseApiException(ErrorCode.SUB_CREATE_NO_BUNDLE, bundleId);
      }

      DateTime bundleStartDate = null;
      final DefaultSubscriptionBase baseSubscription =
          (DefaultSubscriptionBase) dao.getBaseSubscription(bundleId, context);
      switch (plan.getProduct().getCategory()) {
        case BASE:
          if (baseSubscription != null) {
            if (baseSubscription.getState() == EntitlementState.ACTIVE) {
              throw new SubscriptionBaseApiException(ErrorCode.SUB_CREATE_BP_EXISTS, bundleId);
            }
          }
          bundleStartDate = requestedDate;
          break;
        case ADD_ON:
          if (baseSubscription == null) {
            throw new SubscriptionBaseApiException(ErrorCode.SUB_CREATE_NO_BP, bundleId);
          }
          if (effectiveDate.isBefore(baseSubscription.getStartDate())) {
            throw new SubscriptionBaseApiException(
                ErrorCode.SUB_INVALID_REQUESTED_DATE,
                effectiveDate.toString(),
                baseSubscription.getStartDate().toString());
          }
          addonUtils.checkAddonCreationRights(baseSubscription, plan);
          bundleStartDate = baseSubscription.getStartDate();
          break;
        case STANDALONE:
          if (baseSubscription != null) {
            throw new SubscriptionBaseApiException(ErrorCode.SUB_CREATE_BP_EXISTS, bundleId);
          }
          // Not really but we don't care, there is no alignment for STANDALONE subscriptions
          bundleStartDate = requestedDate;
          break;
        default:
          throw new SubscriptionBaseError(
              String.format(
                  "Can't create subscription of type %s",
                  plan.getProduct().getCategory().toString()));
      }

      final UUID tenantId =
          nonEntityDao.retrieveIdFromObject(context.getTenantRecordId(), ObjectType.TENANT);
      return apiService.createPlan(
          new SubscriptionBuilder()
              .setId(UUID.randomUUID())
              .setBundleId(bundleId)
              .setCategory(plan.getProduct().getCategory())
              .setBundleStartDate(bundleStartDate)
              .setAlignStartDate(effectiveDate),
          plan,
          spec.getPhaseType(),
          realPriceList,
          requestedDate,
          effectiveDate,
          now,
          context.toCallContext(tenantId));
    } catch (CatalogApiException e) {
      throw new SubscriptionBaseApiException(e);
    }
  }