Search in sources :

Example 36 with Currency

use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.

the class TestDefaultAccountUserApiWithMocks method testCreateAccount.

@Test(groups = "fast", description = "Test Account create API")
public void testCreateAccount() throws Exception {
    final UUID id = UUID.randomUUID();
    final String externalKey = UUID.randomUUID().toString();
    final String email = UUID.randomUUID().toString();
    final String name = UUID.randomUUID().toString();
    final Integer firstNameLength = Integer.MAX_VALUE;
    final Currency currency = Currency.BRL;
    final Integer billCycleDay = Integer.MAX_VALUE;
    final UUID paymentMethodId = UUID.randomUUID();
    final DateTime referenceTime = clock.getUTCNow();
    final DateTimeZone timeZone = DateTimeZone.UTC;
    final String locale = UUID.randomUUID().toString();
    final String address1 = UUID.randomUUID().toString();
    final String address2 = UUID.randomUUID().toString();
    final String companyName = UUID.randomUUID().toString();
    final String city = UUID.randomUUID().toString();
    final String stateOrProvince = UUID.randomUUID().toString();
    final String country = UUID.randomUUID().toString();
    final String postalCode = UUID.randomUUID().toString();
    final String phone = UUID.randomUUID().toString();
    final String notes = UUID.randomUUID().toString();
    final Boolean isMigrated = true;
    final AccountData data = new DefaultAccount(id, externalKey, email, name, firstNameLength, currency, null, false, billCycleDay, paymentMethodId, referenceTime, timeZone, locale, address1, address2, companyName, city, stateOrProvince, country, postalCode, phone, notes, isMigrated);
    accountUserApi.createAccount(data, callContext);
    final AccountModelDao account = accountDao.getAccountByKey(externalKey, tenantContext);
    Assert.assertEquals(account.getExternalKey(), externalKey);
    Assert.assertEquals(account.getEmail(), email);
    Assert.assertEquals(account.getName(), name);
    Assert.assertEquals(account.getFirstNameLength(), firstNameLength);
    Assert.assertEquals(account.getCurrency(), currency);
    Assert.assertEquals(account.getBillingCycleDayLocal(), (Integer) billCycleDay);
    Assert.assertEquals(account.getPaymentMethodId(), paymentMethodId);
    Assert.assertEquals(account.getTimeZone(), timeZone);
    Assert.assertEquals(account.getLocale(), locale);
    Assert.assertEquals(account.getAddress1(), address1);
    Assert.assertEquals(account.getAddress2(), address2);
    Assert.assertEquals(account.getCompanyName(), companyName);
    Assert.assertEquals(account.getCity(), city);
    Assert.assertEquals(account.getStateOrProvince(), stateOrProvince);
    Assert.assertEquals(account.getCountry(), country);
    Assert.assertEquals(account.getPostalCode(), postalCode);
    Assert.assertEquals(account.getPhone(), phone);
    Assert.assertEquals(account.getMigrated(), isMigrated);
}
Also used : AccountModelDao(org.killbill.billing.account.dao.AccountModelDao) DefaultAccount(org.killbill.billing.account.api.DefaultAccount) AccountData(org.killbill.billing.account.api.AccountData) Currency(org.killbill.billing.catalog.api.Currency) UUID(java.util.UUID) DateTime(org.joda.time.DateTime) DateTimeZone(org.joda.time.DateTimeZone) Test(org.testng.annotations.Test)

Example 37 with Currency

use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.

the class DefaultInvoiceFormatter method getFormattedAmountByLocaleAndInvoiceCurrency.

// Returns the formatted amount with the correct currency symbol that is get from the invoice currency.
private String getFormattedAmountByLocaleAndInvoiceCurrency(final BigDecimal amount) {
    final String invoiceCurrencyCode = invoice.getCurrency().toString();
    final CurrencyUnit currencyUnit = CurrencyUnit.of(invoiceCurrencyCode);
    final DecimalFormat numberFormatter = (DecimalFormat) DecimalFormat.getCurrencyInstance(locale);
    final DecimalFormatSymbols dfs = numberFormatter.getDecimalFormatSymbols();
    dfs.setInternationalCurrencySymbol(currencyUnit.getCode());
    try {
        Currency currency = Currency.fromCode(invoiceCurrencyCode);
        dfs.setCurrencySymbol(currency.getSymbol());
    } catch (final IllegalArgumentException e) {
        dfs.setCurrencySymbol(currencyUnit.getSymbol(locale));
    }
    numberFormatter.setDecimalFormatSymbols(dfs);
    numberFormatter.setMinimumFractionDigits(currencyUnit.getDecimalPlaces());
    numberFormatter.setMaximumFractionDigits(currencyUnit.getDecimalPlaces());
    return numberFormatter.format(amount.doubleValue());
}
Also used : CurrencyUnit(org.joda.money.CurrencyUnit) DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) Currency(org.killbill.billing.catalog.api.Currency)

Example 38 with Currency

use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.

the class InvoiceItemFactory method fromModelDaoWithCatalog.

public static InvoiceItem fromModelDaoWithCatalog(final InvoiceItemModelDao invoiceItemModelDao, @Nullable final VersionedCatalog catalog) {
    if (invoiceItemModelDao == null) {
        return null;
    }
    final UUID id = invoiceItemModelDao.getId();
    final DateTime createdDate = invoiceItemModelDao.getCreatedDate();
    final UUID invoiceId = invoiceItemModelDao.getInvoiceId();
    final UUID accountId = invoiceItemModelDao.getAccountId();
    final UUID childAccountId = invoiceItemModelDao.getChildAccountId();
    final UUID bundleId = invoiceItemModelDao.getBundleId();
    final UUID subscriptionId = invoiceItemModelDao.getSubscriptionId();
    final String productName = invoiceItemModelDao.getProductName();
    final String planName = invoiceItemModelDao.getPlanName();
    final String phaseName = invoiceItemModelDao.getPhaseName();
    final String usageName = invoiceItemModelDao.getUsageName();
    final String description = invoiceItemModelDao.getDescription();
    final LocalDate startDate = invoiceItemModelDao.getStartDate();
    final LocalDate endDate = invoiceItemModelDao.getEndDate();
    final BigDecimal amount = invoiceItemModelDao.getAmount();
    final BigDecimal rate = invoiceItemModelDao.getRate();
    final Currency currency = invoiceItemModelDao.getCurrency();
    final UUID linkedItemId = invoiceItemModelDao.getLinkedItemId();
    final Integer quantity = invoiceItemModelDao.getQuantity();
    final String itemDetails = invoiceItemModelDao.getItemDetails();
    final InvoiceItemType type = invoiceItemModelDao.getType();
    final String[] prettyNames = computePrettyName(type, createdDate, productName, planName, phaseName, usageName, catalog);
    String prettyProductName = prettyNames[0];
    String prettyPlanName = prettyNames[1];
    String prettyPlanPhaseName = prettyNames[2];
    String prettyUsageName = prettyNames[3];
    final DateTime catalogEffectiveDate = invoiceItemModelDao.getCatalogEffectiveDate() != null ? invoiceItemModelDao.getCatalogEffectiveDate() : null;
    final InvoiceItem item;
    switch(type) {
        case EXTERNAL_CHARGE:
            item = new ExternalChargeInvoiceItem(id, createdDate, invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, prettyProductName, prettyPlanName, prettyPlanPhaseName, description, startDate, endDate, amount, rate, currency, linkedItemId, quantity, itemDetails);
            break;
        case FIXED:
            item = new FixedPriceInvoiceItem(id, createdDate, invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, catalogEffectiveDate, prettyProductName, prettyPlanName, prettyPlanPhaseName, description, startDate, amount, currency, quantity, itemDetails);
            break;
        case RECURRING:
            item = new RecurringInvoiceItem(id, createdDate, invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, catalogEffectiveDate, prettyProductName, prettyPlanName, prettyPlanPhaseName, description, startDate, endDate, amount, rate, currency, quantity, itemDetails);
            break;
        case CBA_ADJ:
            item = new CreditBalanceAdjInvoiceItem(id, createdDate, invoiceId, accountId, startDate, linkedItemId, description, amount, currency);
            break;
        case CREDIT_ADJ:
            item = new CreditAdjInvoiceItem(id, createdDate, invoiceId, accountId, startDate, description, amount, rate, currency, quantity, itemDetails);
            break;
        case REPAIR_ADJ:
            item = new RepairAdjInvoiceItem(id, createdDate, invoiceId, accountId, startDate, endDate, description, amount, currency, linkedItemId);
            break;
        case ITEM_ADJ:
            item = new ItemAdjInvoiceItem(id, createdDate, invoiceId, accountId, startDate, description, amount, rate, currency, linkedItemId, quantity, itemDetails);
            break;
        case USAGE:
            item = new UsageInvoiceItem(id, createdDate, invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, usageName, catalogEffectiveDate, prettyProductName, prettyPlanName, prettyPlanPhaseName, prettyUsageName, startDate, endDate, description, amount, rate, currency, quantity, itemDetails);
            break;
        case TAX:
            item = new TaxInvoiceItem(id, createdDate, invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, usageName, catalogEffectiveDate, prettyProductName, prettyPlanName, prettyPlanPhaseName, prettyUsageName, startDate, endDate, description, amount, currency, linkedItemId, itemDetails);
            break;
        case PARENT_SUMMARY:
            item = new ParentInvoiceItem(id, createdDate, invoiceId, accountId, childAccountId, amount, currency, description);
            break;
        default:
            throw new RuntimeException("Unexpected type of event item " + type);
    }
    return item;
}
Also used : InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Currency(org.killbill.billing.catalog.api.Currency) UUID(java.util.UUID) InvoiceItemType(org.killbill.billing.invoice.api.InvoiceItemType)

Example 39 with Currency

use of org.killbill.billing.catalog.api.Currency 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)

Example 40 with Currency

use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.

the class InvoiceDaoHelper 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 invoiceId         the invoice id
 * @param invoiceItemId     the invoice item id to adjust
 * @param effectiveDate     adjustment effective date, in the account timezone
 * @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
 * @return the adjustment item
 */
public InvoiceItemModelDao createAdjustmentItem(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final UUID invoiceId, final UUID invoiceItemId, final BigDecimal positiveAdjAmount, final Currency currency, final LocalDate effectiveDate, final InternalCallContext context) throws InvoiceApiException {
    // First, retrieve the invoice item in question
    final InvoiceItemSqlDao invoiceItemSqlDao = entitySqlDaoWrapperFactory.become(InvoiceItemSqlDao.class);
    final InvoiceItemModelDao invoiceItemToBeAdjusted = invoiceItemSqlDao.getById(invoiceItemId.toString(), context);
    if (invoiceItemToBeAdjusted == null) {
        throw new InvoiceApiException(ErrorCode.INVOICE_ITEM_NOT_FOUND, invoiceItemId);
    }
    // Validate the invoice it belongs to
    if (!invoiceItemToBeAdjusted.getInvoiceId().equals(invoiceId)) {
        throw new InvoiceApiException(ErrorCode.INVOICE_INVALID_FOR_INVOICE_ITEM_ADJUSTMENT, invoiceItemId, invoiceId);
    }
    // Retrieve the amount and currency if needed
    final BigDecimal amountToAdjust = MoreObjects.firstNonNull(positiveAdjAmount, invoiceItemToBeAdjusted.getAmount());
    // TODO - should we enforce the currency (and respect the original one) here if the amount passed was null?
    final Currency currencyForAdjustment = MoreObjects.firstNonNull(currency, invoiceItemToBeAdjusted.getCurrency());
    // Note! The amount is negated here!
    return new InvoiceItemModelDao(context.getCreatedDate(), InvoiceItemType.ITEM_ADJ, invoiceItemToBeAdjusted.getInvoiceId(), invoiceItemToBeAdjusted.getAccountId(), null, null, null, invoiceItemToBeAdjusted.getProductName(), invoiceItemToBeAdjusted.getPlanName(), invoiceItemToBeAdjusted.getPhaseName(), invoiceItemToBeAdjusted.getUsageName(), invoiceItemToBeAdjusted.getCatalogEffectiveDate(), effectiveDate, effectiveDate, amountToAdjust.negate(), null, currencyForAdjustment, invoiceItemToBeAdjusted.getId());
}
Also used : InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) Currency(org.killbill.billing.catalog.api.Currency) BigDecimal(java.math.BigDecimal)

Aggregations

Currency (org.killbill.billing.catalog.api.Currency)65 BigDecimal (java.math.BigDecimal)36 UUID (java.util.UUID)31 DateTime (org.joda.time.DateTime)21 Test (org.testng.annotations.Test)20 LocalDate (org.joda.time.LocalDate)18 PluginProperty (org.killbill.billing.payment.api.PluginProperty)16 Invoice (org.killbill.billing.invoice.api.Invoice)15 MockPlan (org.killbill.billing.catalog.MockPlan)13 Plan (org.killbill.billing.catalog.api.Plan)13 Account (org.killbill.billing.account.api.Account)12 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)12 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)12 PaymentOptions (org.killbill.billing.payment.api.PaymentOptions)12 CallContext (org.killbill.billing.util.callcontext.CallContext)12 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)11 Payment (org.killbill.billing.payment.api.Payment)11 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)11 AccountInvoices (org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices)9 BillingEventSet (org.killbill.billing.junction.BillingEventSet)9