Search in sources :

Example 11 with Currency

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

the class DefaultInvoiceUserApi method insertItems.

private List<InvoiceItem> insertItems(final UUID accountId, final LocalDate effectiveDate, final InvoiceItemType itemType, final Iterable<InvoiceItem> inputItems, final boolean autoCommit, final LinkedList<PluginProperty> properties, final CallContext context) throws InvoiceApiException {
    final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(accountId, context);
    ImmutableAccountData accountData;
    try {
        accountData = accountUserApi.getImmutableAccountDataById(accountId, internalTenantContext);
    } catch (AccountApiException e) {
        throw new InvoiceApiException(e);
    }
    final Currency accountCurrency = accountData.getCurrency();
    final WithAccountLock withAccountLock = new WithAccountLock() {

        @Override
        public Iterable<DefaultInvoice> prepareInvoices() throws InvoiceApiException {
            final LocalDate invoiceDate = internalTenantContext.toLocalDate(context.getCreatedDate());
            final Map<UUID, DefaultInvoice> newAndExistingInvoices = new HashMap<UUID, DefaultInvoice>();
            UUID newInvoiceId = null;
            for (final InvoiceItem inputItem : inputItems) {
                if (inputItem.getAmount() == null || inputItem.getAmount().compareTo(BigDecimal.ZERO) < 0) {
                    if (itemType == InvoiceItemType.EXTERNAL_CHARGE) {
                        throw new InvoiceApiException(ErrorCode.EXTERNAL_CHARGE_AMOUNT_INVALID, inputItem.getAmount());
                    } else if (itemType == InvoiceItemType.CREDIT_ADJ) {
                        throw new InvoiceApiException(ErrorCode.CREDIT_AMOUNT_INVALID, inputItem.getAmount());
                    }
                }
                if (inputItem.getCurrency() != null && !inputItem.getCurrency().equals(accountCurrency)) {
                    throw new InvoiceApiException(ErrorCode.CURRENCY_INVALID, inputItem.getCurrency(), accountCurrency);
                }
                final UUID invoiceIdForItem = inputItem.getInvoiceId();
                final Invoice curInvoiceForItem;
                if (invoiceIdForItem == null) {
                    final Currency currency = inputItem.getCurrency();
                    final InvoiceStatus status = autoCommit ? InvoiceStatus.COMMITTED : InvoiceStatus.DRAFT;
                    if (newInvoiceId == null) {
                        final DefaultInvoice newInvoiceForItems = new DefaultInvoice(accountId, invoiceDate, effectiveDate, currency, status);
                        newInvoiceId = newInvoiceForItems.getId();
                        newAndExistingInvoices.put(newInvoiceId, newInvoiceForItems);
                    }
                    curInvoiceForItem = newAndExistingInvoices.get(newInvoiceId);
                } else {
                    if (newAndExistingInvoices.get(invoiceIdForItem) == null) {
                        final DefaultInvoice existingInvoiceForExternalCharge = getInvoiceInternal(invoiceIdForItem, context);
                        switch(existingInvoiceForExternalCharge.getStatus()) {
                            case COMMITTED:
                                throw new InvoiceApiException(ErrorCode.INVOICE_ALREADY_COMMITTED, existingInvoiceForExternalCharge.getId());
                            case VOID:
                                // TODO Add missing error https://github.com/killbill/killbill/issues/1501
                                throw new IllegalStateException(String.format("Cannot add credit or external charge for invoice id %s because it is in \" + InvoiceStatus.VOID + \" status\"", existingInvoiceForExternalCharge.getId()));
                            case DRAFT:
                            default:
                                break;
                        }
                        newAndExistingInvoices.put(invoiceIdForItem, existingInvoiceForExternalCharge);
                    }
                    curInvoiceForItem = newAndExistingInvoices.get(invoiceIdForItem);
                }
                final InvoiceItem newInvoiceItem;
                switch(itemType) {
                    case EXTERNAL_CHARGE:
                        newInvoiceItem = new ExternalChargeInvoiceItem(UUIDs.randomUUID(), context.getCreatedDate(), curInvoiceForItem.getId(), accountId, inputItem.getBundleId(), inputItem.getSubscriptionId(), inputItem.getProductName(), inputItem.getPlanName(), inputItem.getPhaseName(), inputItem.getPrettyProductName(), inputItem.getPrettyPlanName(), inputItem.getPrettyPhaseName(), inputItem.getDescription(), MoreObjects.firstNonNull(inputItem.getStartDate(), effectiveDate), inputItem.getEndDate(), inputItem.getAmount(), inputItem.getRate(), accountCurrency, inputItem.getLinkedItemId(), inputItem.getQuantity(), inputItem.getItemDetails());
                        break;
                    case CREDIT_ADJ:
                        newInvoiceItem = new CreditAdjInvoiceItem(UUIDs.randomUUID(), context.getCreatedDate(), curInvoiceForItem.getId(), accountId, effectiveDate, inputItem.getDescription(), // Note! The amount is negated here!
                        inputItem.getAmount().negate(), inputItem.getRate(), inputItem.getCurrency(), inputItem.getQuantity(), inputItem.getItemDetails());
                        break;
                    case TAX:
                        newInvoiceItem = new TaxInvoiceItem(UUIDs.randomUUID(), curInvoiceForItem.getId(), accountId, inputItem.getBundleId(), inputItem.getDescription(), MoreObjects.firstNonNull(inputItem.getStartDate(), effectiveDate), inputItem.getAmount(), accountCurrency);
                        break;
                    default:
                        throw new IllegalStateException(String.format("Unsupported to add item of type '%s'", itemType));
                }
                curInvoiceForItem.addInvoiceItem(newInvoiceItem);
            }
            return newAndExistingInvoices.values();
        }
    };
    return invoiceApiHelper.dispatchToInvoicePluginsAndInsertItems(accountId, false, withAccountLock, properties, context);
}
Also used : ImmutableAccountData(org.killbill.billing.account.api.ImmutableAccountData) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) Invoice(org.killbill.billing.invoice.api.Invoice) HtmlInvoice(org.killbill.billing.invoice.template.HtmlInvoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) HashMap(java.util.HashMap) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) WithAccountLock(org.killbill.billing.invoice.api.WithAccountLock) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) LocalDate(org.joda.time.LocalDate) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) Currency(org.killbill.billing.catalog.api.Currency) AccountApiException(org.killbill.billing.account.api.AccountApiException) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) InvoiceStatus(org.killbill.billing.invoice.api.InvoiceStatus)

Example 12 with Currency

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

the class TestInvoiceItemJsonSimple method testJson.

@Test(groups = "fast")
public void testJson() throws Exception {
    final UUID invoiceItemId = UUID.randomUUID();
    final UUID invoiceId = UUID.randomUUID();
    final UUID linkedInvoiceItemId = UUID.randomUUID();
    final UUID accountId = UUID.randomUUID();
    final UUID childAccountId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final UUID subscriptionId = UUID.randomUUID();
    final String productName = UUID.randomUUID().toString();
    final String planName = UUID.randomUUID().toString();
    final String phaseName = UUID.randomUUID().toString();
    final String usageName = UUID.randomUUID().toString();
    final InvoiceItemType type = InvoiceItemType.FIXED;
    final String description = UUID.randomUUID().toString();
    final LocalDate startDate = clock.getUTCToday();
    final LocalDate endDate = clock.getUTCToday();
    final BigDecimal amount = BigDecimal.TEN;
    final Currency currency = Currency.MXN;
    final List<AuditLogJson> auditLogs = createAuditLogsJson(clock.getUTCNow());
    final InvoiceItemJson invoiceItemJson = new InvoiceItemJson(invoiceItemId, invoiceId, linkedInvoiceItemId, accountId, childAccountId, bundleId, subscriptionId, productName, planName, phaseName, usageName, null, null, null, null, type, description, startDate, endDate, amount, null, currency, null, null, new DateTime(), null, auditLogs);
    Assert.assertEquals(invoiceItemJson.getInvoiceItemId(), invoiceItemId);
    Assert.assertEquals(invoiceItemJson.getInvoiceId(), invoiceId);
    Assert.assertEquals(invoiceItemJson.getLinkedInvoiceItemId(), linkedInvoiceItemId);
    Assert.assertEquals(invoiceItemJson.getAccountId(), accountId);
    Assert.assertEquals(invoiceItemJson.getChildAccountId(), childAccountId);
    Assert.assertEquals(invoiceItemJson.getBundleId(), bundleId);
    Assert.assertEquals(invoiceItemJson.getSubscriptionId(), subscriptionId);
    Assert.assertEquals(invoiceItemJson.getProductName(), productName);
    Assert.assertEquals(invoiceItemJson.getPlanName(), planName);
    Assert.assertEquals(invoiceItemJson.getPhaseName(), phaseName);
    Assert.assertEquals(invoiceItemJson.getUsageName(), usageName);
    Assert.assertEquals(invoiceItemJson.getItemType(), type);
    Assert.assertEquals(invoiceItemJson.getDescription(), description);
    Assert.assertEquals(invoiceItemJson.getStartDate(), startDate);
    Assert.assertEquals(invoiceItemJson.getEndDate(), endDate);
    Assert.assertEquals(invoiceItemJson.getAmount(), amount);
    Assert.assertEquals(invoiceItemJson.getCurrency(), currency);
    Assert.assertEquals(invoiceItemJson.getAuditLogs(), auditLogs);
    final String asJson = mapper.writeValueAsString(invoiceItemJson);
    final InvoiceItemJson fromJson = mapper.readValue(asJson, InvoiceItemJson.class);
    Assert.assertEquals(fromJson, invoiceItemJson);
}
Also used : Currency(org.killbill.billing.catalog.api.Currency) UUID(java.util.UUID) InvoiceItemType(org.killbill.billing.invoice.api.InvoiceItemType) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 13 with Currency

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

the class BlockingCalculator method createNewDisableEvent.

protected BillingEvent createNewDisableEvent(final DateTime disabledDurationStart, final BillingEvent previousEvent) {
    final int billCycleDay = previousEvent.getBillCycleDayLocal();
    final DateTime effectiveDate = disabledDurationStart;
    final PlanPhase planPhase = previousEvent.getPlanPhase();
    final Plan plan = previousEvent.getPlan();
    // Make sure to set the fixed price to null and the billing period to NO_BILLING_PERIOD,
    // which makes invoice disregard this event
    final BigDecimal fixedPrice = null;
    final BigDecimal recurringPrice = null;
    final BillingPeriod billingPeriod = BillingPeriod.NO_BILLING_PERIOD;
    final Currency currency = previousEvent.getCurrency();
    final String description = "";
    final SubscriptionBaseTransitionType type = SubscriptionBaseTransitionType.START_BILLING_DISABLED;
    final Long totalOrdering = globaltotalOrder.getAndIncrement();
    return new DefaultBillingEvent(previousEvent.getSubscriptionId(), previousEvent.getBundleId(), effectiveDate, plan, planPhase, fixedPrice, recurringPrice, ImmutableList.of(), currency, billingPeriod, billCycleDay, description, totalOrdering, type, true);
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) Currency(org.killbill.billing.catalog.api.Currency) AtomicLong(java.util.concurrent.atomic.AtomicLong) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) SubscriptionBaseTransitionType(org.killbill.billing.subscription.api.SubscriptionBaseTransitionType) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal)

Example 14 with Currency

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

the class BlockingCalculator method createNewReenableEvent.

protected BillingEvent createNewReenableEvent(final DateTime odEventTime, final BillingEvent previousEvent) throws CatalogApiException {
    // All fields are populated with the event state from before the blocking period, for invoice to resume invoicing
    final int billCycleDay = previousEvent.getBillCycleDayLocal();
    final DateTime effectiveDate = odEventTime;
    final PlanPhase planPhase = previousEvent.getPlanPhase();
    final BigDecimal fixedPrice = previousEvent.getFixedPrice();
    final BigDecimal recurringPrice = previousEvent.getRecurringPrice();
    final List<Usage> usages = previousEvent.getUsages();
    final Plan plan = previousEvent.getPlan();
    final Currency currency = previousEvent.getCurrency();
    final String description = "";
    final BillingPeriod billingPeriod = previousEvent.getBillingPeriod();
    final SubscriptionBaseTransitionType type = SubscriptionBaseTransitionType.END_BILLING_DISABLED;
    final Long totalOrdering = globaltotalOrder.getAndIncrement();
    return new DefaultBillingEvent(previousEvent.getSubscriptionId(), previousEvent.getBundleId(), effectiveDate, plan, planPhase, fixedPrice, recurringPrice, usages, currency, billingPeriod, billCycleDay, description, totalOrdering, type, false);
}
Also used : Usage(org.killbill.billing.catalog.api.Usage) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) SubscriptionBaseTransitionType(org.killbill.billing.subscription.api.SubscriptionBaseTransitionType) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Currency(org.killbill.billing.catalog.api.Currency) AtomicLong(java.util.concurrent.atomic.AtomicLong) PlanPhase(org.killbill.billing.catalog.api.PlanPhase)

Example 15 with Currency

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

the class TestBlockingCalculator method createRealEvent.

private BillingEvent createRealEvent(final SubscriptionBase subscription, final DateTime effectiveDate, final SubscriptionBaseTransitionType type, final Long totalOrdering) {
    try {
        final Integer billCycleDay = 1;
        final Plan plan = new MockPlan();
        final Currency currency = Currency.USD;
        final String description = "";
        final BillingPeriod billingPeriod = BillingPeriod.MONTHLY;
        final PlanPhase planPhase = Mockito.mock(PlanPhase.class);
        final InternationalPrice internationalPrice = Mockito.mock(InternationalPrice.class);
        Mockito.when(internationalPrice.getPrice(Mockito.<Currency>any())).thenReturn(BigDecimal.TEN);
        final Recurring recurring = Mockito.mock(Recurring.class);
        Mockito.when(recurring.getRecurringPrice()).thenReturn(internationalPrice);
        Mockito.when(planPhase.getRecurring()).thenReturn(recurring);
        Mockito.when(planPhase.getUsages()).thenReturn(new DefaultUsage[0]);
        final BigDecimal fixedPrice = BigDecimal.TEN;
        final BigDecimal recurringPrice = BigDecimal.TEN;
        return new DefaultBillingEvent(subscription.getId(), subscription.getBundleId(), effectiveDate, plan, planPhase, fixedPrice, recurringPrice, ImmutableList.of(), currency, billingPeriod, billCycleDay, description, totalOrdering, type, false);
    } catch (final CatalogApiException e) {
        Assert.fail("", e);
    }
    throw new IllegalStateException();
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) InternationalPrice(org.killbill.billing.catalog.api.InternationalPrice) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) BigDecimal(java.math.BigDecimal) Recurring(org.killbill.billing.catalog.api.Recurring) MockPlan(org.killbill.billing.catalog.MockPlan) Currency(org.killbill.billing.catalog.api.Currency) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) PlanPhase(org.killbill.billing.catalog.api.PlanPhase)

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