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);
}
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);
}
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);
}
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);
}
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();
}
Aggregations