private void checkForTaxCodesInCustomFields(
      final Invoice invoice,
      final Collection<PluginProperty> properties,
      final TenantContext context) {
    final List<CustomField> customFields =
        killbillAPI
            .getCustomFieldUserApi()
            .getCustomFieldsForAccountType(
                invoice.getAccountId(), ObjectType.INVOICE_ITEM, context);
    if (customFields.isEmpty()) {
      return;
    }

    final Collection<UUID> invoiceItemIds = new HashSet<UUID>();
    for (final InvoiceItem invoiceItem : invoice.getInvoiceItems()) {
      invoiceItemIds.add(invoiceItem.getId());
    }

    final Iterable<CustomField> taxCodeCustomFieldsForInvoiceItems =
        Iterables.<CustomField>filter(
            customFields,
            new Predicate<CustomField>() {
              @Override
              public boolean apply(final CustomField customField) {
                return AvaTaxTaxCalculator.TAX_CODE.equals(customField.getFieldName())
                    && invoiceItemIds.contains(customField.getObjectId());
              }
            });
    for (final CustomField customField : taxCodeCustomFieldsForInvoiceItems) {
      final UUID invoiceItemId = customField.getObjectId();
      final String taxCode = customField.getFieldValue();
      addTaxCodeToInvoiceItem(invoiceItemId, taxCode, properties);
    }
  }
  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);
      }
    }
  }