Search in sources :

Example 1 with InvoiceItemCatalogBase

use of org.killbill.billing.invoice.model.InvoiceItemCatalogBase in project killbill by killbill.

the class InvoicePluginDispatcher method validateAndSanitizeInvoiceItemFromPlugin.

private InvoiceItem validateAndSanitizeInvoiceItemFromPlugin(final UUID originalInvoiceId, final Map<UUID, InvoiceItem> invoiceItemsByItemId, final InvoiceItem additionalInvoiceItem, final InvoicePluginApi invoicePlugin) throws InvoiceApiException {
    final InvoiceItem existingItem = invoiceItemsByItemId.get(additionalInvoiceItem.getId());
    if (!ALLOWED_INVOICE_ITEM_TYPES.contains(additionalInvoiceItem.getInvoiceItemType()) && existingItem == null) {
        log.warn("Ignoring invoice item of type {} from InvoicePlugin {}: {}", additionalInvoiceItem.getInvoiceItemType(), invoicePlugin, additionalInvoiceItem);
        throw new InvoiceApiException(ErrorCode.INVOICE_ITEM_TYPE_INVALID, additionalInvoiceItem.getInvoiceItemType());
    }
    final UUID invoiceId = MoreObjects.firstNonNull(mutableField("invoiceId", existingItem != null ? existingItem.getInvoiceId() : null, additionalInvoiceItem.getInvoiceId(), invoicePlugin), originalInvoiceId);
    final UUID additionalInvoiceId = MoreObjects.firstNonNull(additionalInvoiceItem.getId(), UUIDs.randomUUID());
    final InvoiceItemCatalogBase tmp = new InvoiceItemCatalogBase(additionalInvoiceId, mutableField("createdDate", existingItem != null ? existingItem.getCreatedDate() : null, additionalInvoiceItem.getCreatedDate(), invoicePlugin), invoiceId, immutableField("accountId", existingItem, existingItem != null ? existingItem.getAccountId() : null, additionalInvoiceItem.getAccountId(), invoicePlugin), immutableField("bundleId", existingItem, existingItem != null ? existingItem.getBundleId() : null, additionalInvoiceItem.getBundleId(), invoicePlugin), immutableField("subscriptionId", existingItem, existingItem != null ? existingItem.getSubscriptionId() : null, additionalInvoiceItem.getSubscriptionId(), invoicePlugin), mutableField("description", existingItem != null ? existingItem.getDescription() : null, additionalInvoiceItem.getDescription(), invoicePlugin), immutableField("productName", existingItem, existingItem != null ? existingItem.getProductName() : null, additionalInvoiceItem.getProductName(), invoicePlugin), immutableField("planName", existingItem, existingItem != null ? existingItem.getPlanName() : null, additionalInvoiceItem.getPlanName(), invoicePlugin), immutableField("phaseName", existingItem, existingItem != null ? existingItem.getPhaseName() : null, additionalInvoiceItem.getPhaseName(), invoicePlugin), immutableField("usageName", existingItem, existingItem != null ? existingItem.getUsageName() : null, additionalInvoiceItem.getUsageName(), invoicePlugin), immutableField("catalogEffectiveDate", existingItem, existingItem != null ? existingItem.getCatalogEffectiveDate() : null, additionalInvoiceItem.getCatalogEffectiveDate(), invoicePlugin), mutableField("prettyProductName", existingItem != null ? existingItem.getPrettyProductName() : null, additionalInvoiceItem.getPrettyProductName(), invoicePlugin), mutableField("prettyPlanName", existingItem != null ? existingItem.getPrettyPlanName() : null, additionalInvoiceItem.getPrettyPlanName(), invoicePlugin), mutableField("prettyPhaseName", existingItem != null ? existingItem.getPrettyPhaseName() : null, additionalInvoiceItem.getPrettyPhaseName(), invoicePlugin), mutableField("prettyUsageName", existingItem != null ? existingItem.getPrettyUsageName() : null, additionalInvoiceItem.getPrettyUsageName(), invoicePlugin), immutableField("startDate", existingItem, existingItem != null ? existingItem.getStartDate() : null, additionalInvoiceItem.getStartDate(), invoicePlugin), immutableField("endDate", existingItem, existingItem != null ? existingItem.getEndDate() : null, additionalInvoiceItem.getEndDate(), invoicePlugin), mutableField("amount", existingItem != null ? existingItem.getAmount() : null, additionalInvoiceItem.getAmount(), invoicePlugin), immutableField("rate", existingItem, existingItem != null ? existingItem.getRate() : null, additionalInvoiceItem.getRate(), invoicePlugin), immutableField("currency", existingItem, existingItem != null ? existingItem.getCurrency() : null, additionalInvoiceItem.getCurrency(), invoicePlugin), immutableField("linkedItemId", existingItem, existingItem != null ? existingItem.getLinkedItemId() : null, additionalInvoiceItem.getLinkedItemId(), invoicePlugin), immutableField("quantity", existingItem, existingItem != null ? existingItem.getQuantity() : null, additionalInvoiceItem.getQuantity(), invoicePlugin), mutableField("itemDetails", existingItem != null ? existingItem.getItemDetails() : null, additionalInvoiceItem.getItemDetails(), invoicePlugin), immutableField("invoiceItemType", existingItem, existingItem != null ? existingItem.getInvoiceItemType() : null, additionalInvoiceItem.getInvoiceItemType(), invoicePlugin));
    switch(tmp.getInvoiceItemType()) {
        case RECURRING:
            return new RecurringInvoiceItem(tmp);
        case USAGE:
            return new UsageInvoiceItem(tmp);
        case FIXED:
            return new FixedPriceInvoiceItem(tmp);
        case TAX:
            return new TaxInvoiceItem(tmp);
        case EXTERNAL_CHARGE:
            return new ExternalChargeInvoiceItem(tmp);
        default:
            // As long as there is no explicit cast to the expected type this works
            return tmp;
    }
}
Also used : TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) UUID(java.util.UUID) InvoiceItemCatalogBase(org.killbill.billing.invoice.model.InvoiceItemCatalogBase) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem)

Example 2 with InvoiceItemCatalogBase

use of org.killbill.billing.invoice.model.InvoiceItemCatalogBase in project killbill by killbill.

the class InvoiceApiHelper method createAdjustmentItem.

/**
 * Create an adjustment for a given invoice item. This just creates the object in memory, it doesn't write it to disk.
 *
 * @param invoiceToBeAdjusted the invoice
 * @param invoiceItemId       the invoice item id to adjust
 * @param positiveAdjAmount   the amount to adjust. Pass null to adjust the full amount of the original item
 * @param currency            the currency of the amount. Pass null to default to the original currency used
 * @param effectiveDate       adjustment effective date, in the account timezone
 * @return the adjustment item
 */
public InvoiceItem createAdjustmentItem(final Invoice invoiceToBeAdjusted, final UUID invoiceItemId, @Nullable final BigDecimal positiveAdjAmount, @Nullable final Currency currency, final LocalDate effectiveDate, final String description, @Nullable final String itemDetails, final InternalCallContext context) throws InvoiceApiException {
    final InvoiceItem invoiceItemToBeAdjusted = Iterables.<InvoiceItem>tryFind(invoiceToBeAdjusted.getInvoiceItems(), new Predicate<InvoiceItem>() {

        @Override
        public boolean apply(final InvoiceItem input) {
            return input.getId().equals(invoiceItemId);
        }
    }).orNull();
    if (invoiceItemToBeAdjusted == null) {
        throw new InvoiceApiException(ErrorCode.INVOICE_ITEM_NOT_FOUND, invoiceItemId);
    }
    // Check the specified currency matches the one of the existing invoice
    final Currency currencyForAdjustment = MoreObjects.firstNonNull(currency, invoiceItemToBeAdjusted.getCurrency());
    if (invoiceItemToBeAdjusted.getCurrency() != currencyForAdjustment) {
        throw new InvoiceApiException(ErrorCode.CURRENCY_INVALID, currency, invoiceItemToBeAdjusted.getCurrency());
    }
    // Reuse the same logic we have for refund with item adjustment
    final Map<UUID, BigDecimal> input = new HashMap<UUID, BigDecimal>();
    input.put(invoiceItemId, positiveAdjAmount);
    final Map<UUID, BigDecimal> output = dao.computeItemAdjustments(invoiceToBeAdjusted.getId().toString(), input, context);
    // Nothing to adjust
    if (output.get(invoiceItemId) == null) {
        return null;
    }
    // If we pass that stage, it means the validation succeeded so we just need to extract resulting amount and negate the result.
    final BigDecimal amountToAdjust = output.get(invoiceItemId).negate();
    return new InvoiceItemCatalogBase(UUIDs.randomUUID(), context.getCreatedDate(), invoiceItemToBeAdjusted.getInvoiceId(), invoiceItemToBeAdjusted.getAccountId(), null, null, description, invoiceItemToBeAdjusted.getProductName(), invoiceItemToBeAdjusted.getPlanName(), invoiceItemToBeAdjusted.getPhaseName(), invoiceItemToBeAdjusted.getUsageName(), invoiceItemToBeAdjusted.getCatalogEffectiveDate(), effectiveDate, effectiveDate, amountToAdjust, null, currencyForAdjustment, invoiceItemToBeAdjusted.getId(), null, itemDetails, InvoiceItemType.ITEM_ADJ);
}
Also used : HashMap(java.util.HashMap) Currency(org.killbill.billing.catalog.api.Currency) UUID(java.util.UUID) InvoiceItemCatalogBase(org.killbill.billing.invoice.model.InvoiceItemCatalogBase) BigDecimal(java.math.BigDecimal) Predicate(com.google.common.base.Predicate)

Aggregations

UUID (java.util.UUID)2 InvoiceItemCatalogBase (org.killbill.billing.invoice.model.InvoiceItemCatalogBase)2 Predicate (com.google.common.base.Predicate)1 BigDecimal (java.math.BigDecimal)1 HashMap (java.util.HashMap)1 Currency (org.killbill.billing.catalog.api.Currency)1 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)1 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)1 ExternalChargeInvoiceItem (org.killbill.billing.invoice.model.ExternalChargeInvoiceItem)1 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)1 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)1 TaxInvoiceItem (org.killbill.billing.invoice.model.TaxInvoiceItem)1 UsageInvoiceItem (org.killbill.billing.invoice.model.UsageInvoiceItem)1