use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.
the class InvoiceDispatcher method processAccountWithLockAndInputTargetDate.
private Invoice processAccountWithLockAndInputTargetDate(final UUID accountId, final LocalDate targetDate, final BillingEventSet billingEvents, final boolean isDryRun, final InternalCallContext context) throws InvoiceApiException {
try {
final ImmutableAccountData account = accountApi.getImmutableAccountDataById(accountId, context);
final List<Invoice> invoices = billingEvents.isAccountAutoInvoiceOff() ? ImmutableList.<Invoice>of() : ImmutableList.<Invoice>copyOf(Collections2.transform(invoiceDao.getInvoicesByAccount(context), new Function<InvoiceModelDao, Invoice>() {
@Override
public Invoice apply(final InvoiceModelDao input) {
return new DefaultInvoice(input);
}
}));
final Currency targetCurrency = account.getCurrency();
final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, billingEvents, invoices, targetDate, targetCurrency, context);
final DefaultInvoice invoice = invoiceWithMetadata.getInvoice();
// Compute future notifications
final FutureAccountNotifications futureAccountNotifications = createNextFutureNotificationDate(invoiceWithMetadata, context);
//
if (invoice == null) {
if (isDryRun) {
log.info("Generated null dryRun invoice for accountId='{}', targetDate='{}'", accountId, targetDate);
} else {
log.info("Generated null invoice for accountId='{}', targetDate='{}'", accountId, targetDate);
final BusInternalEvent event = new DefaultNullInvoiceEvent(accountId, clock.getUTCToday(), context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken());
commitInvoiceAndSetFutureNotifications(account, null, futureAccountNotifications, context);
postEvent(event);
}
return null;
}
// Generate missing credit (> 0 for generation and < 0 for use) prior we call the plugin
final InvoiceItem cbaItemPreInvoicePlugins = computeCBAOnExistingInvoice(invoice, context);
DefaultInvoice tmpInvoiceForInvoicePlugins = invoice;
if (cbaItemPreInvoicePlugins != null) {
tmpInvoiceForInvoicePlugins = (DefaultInvoice) tmpInvoiceForInvoicePlugins.clone();
tmpInvoiceForInvoicePlugins.addInvoiceItem(cbaItemPreInvoicePlugins);
}
//
// Ask external invoice plugins if additional items (tax, etc) shall be added to the invoice
//
final CallContext callContext = buildCallContext(context);
final List<InvoiceItem> additionalInvoiceItemsFromPlugins = invoicePluginDispatcher.getAdditionalInvoiceItems(tmpInvoiceForInvoicePlugins, isDryRun, callContext);
if (additionalInvoiceItemsFromPlugins.isEmpty()) {
// PERF: avoid re-computing the CBA if no change was made
if (cbaItemPreInvoicePlugins != null) {
invoice.addInvoiceItem(cbaItemPreInvoicePlugins);
}
} else {
invoice.addInvoiceItems(additionalInvoiceItemsFromPlugins);
// Use credit after we call the plugin (https://github.com/killbill/killbill/issues/637)
final InvoiceItem cbaItemPostInvoicePlugins = computeCBAOnExistingInvoice(invoice, context);
if (cbaItemPostInvoicePlugins != null) {
invoice.addInvoiceItem(cbaItemPostInvoicePlugins);
}
}
if (!isDryRun) {
// Compute whether this is a new invoice object (or just some adjustments on an existing invoice), and extract invoiceIds for later use
final Set<UUID> uniqueInvoiceIds = getUniqueInvoiceIds(invoice);
final boolean isRealInvoiceWithItems = uniqueInvoiceIds.remove(invoice.getId());
final Set<UUID> adjustedUniqueOtherInvoiceId = uniqueInvoiceIds;
logInvoiceWithItems(account, invoice, targetDate, adjustedUniqueOtherInvoiceId, isRealInvoiceWithItems);
// Transformation to Invoice -> InvoiceModelDao
final InvoiceModelDao invoiceModelDao = new InvoiceModelDao(invoice);
final List<InvoiceItemModelDao> invoiceItemModelDaos = transformToInvoiceModelDao(invoice.getInvoiceItems());
invoiceModelDao.addInvoiceItems(invoiceItemModelDaos);
// Commit invoice on disk
final boolean isThereAnyItemsLeft = commitInvoiceAndSetFutureNotifications(account, invoiceModelDao, futureAccountNotifications, context);
final boolean isRealInvoiceWithNonEmptyItems = isThereAnyItemsLeft ? isRealInvoiceWithItems : false;
setChargedThroughDates(invoice.getInvoiceItems(FixedPriceInvoiceItem.class), invoice.getInvoiceItems(RecurringInvoiceItem.class), context);
if (InvoiceStatus.COMMITTED.equals(invoice.getStatus())) {
notifyAccountIfEnabled(account, invoice, isRealInvoiceWithNonEmptyItems, context);
}
}
return invoice;
} catch (final AccountApiException e) {
log.error("Failed handling SubscriptionBase change.", e);
return null;
} catch (final SubscriptionBaseApiException e) {
log.error("Failed handling SubscriptionBase change.", e);
return null;
}
}
use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.
the class TestBillingEventSetJson method testTestBillingEventSetJsonSerialization.
@Test(groups = "fast")
public void testTestBillingEventSetJsonSerialization() throws Exception {
final LocalDate startDate = new LocalDate(2019, 9, 26);
final UUID subscriptionId = UUID.randomUUID();
final UUID bundleId = UUID.randomUUID();
final Currency currency = Currency.USD;
final SubscriptionBase sub = Mockito.mock(SubscriptionBase.class);
Mockito.when(sub.getId()).thenReturn(subscriptionId);
Mockito.when(sub.getBundleId()).thenReturn(bundleId);
final MockBillingEventSet billingEventSet = new MockBillingEventSet();
final Plan plan = new MockPlan("Test");
final MockInternationalPrice recurringPrice = new MockInternationalPrice(new DefaultPrice(THIRTEEN, Currency.USD));
final PlanPhase planPhase = new MockPlanPhase(recurringPrice, null, BillingPeriod.MONTHLY, PhaseType.EVERGREEN);
final DateTime effectiveDate1 = startDate.toDateTimeAtStartOfDay();
final BillingEvent event1 = invoiceUtil.createMockBillingEvent(null, sub, effectiveDate1, plan, planPhase, planPhase.getFixed().getPrice() == null ? null : planPhase.getFixed().getPrice().getPrice(currency), planPhase.getRecurring().getRecurringPrice() == null ? null : planPhase.getRecurring().getRecurringPrice().getPrice(currency), currency, planPhase.getRecurring().getBillingPeriod(), 1, BillingMode.IN_ADVANCE, "Test", 1, SubscriptionBaseTransitionType.CREATE);
billingEventSet.add(event1);
final DateTime effectiveDate2 = startDate.toDateTimeAtStartOfDay().plusDays(10);
final BillingEvent event2 = invoiceUtil.createMockBillingEvent(null, sub, effectiveDate2, plan, planPhase, planPhase.getFixed().getPrice() == null ? null : planPhase.getFixed().getPrice().getPrice(currency), planPhase.getRecurring().getRecurringPrice() == null ? null : planPhase.getRecurring().getRecurringPrice().getPrice(currency), currency, planPhase.getRecurring().getBillingPeriod(), 1, BillingMode.IN_ADVANCE, "Test", 1, SubscriptionBaseTransitionType.CHANGE);
billingEventSet.add(event2);
final BillingEventSetJson json = new BillingEventSetJson(billingEventSet);
final String asJson = testMapper.writeValueAsString(json);
final BillingEventSetJson res = testMapper.readValue(asJson, BillingEventSetJson.class);
assertFalse(res.isAutoInvoiceOff());
assertFalse(res.isAutoInvoiceDraft());
assertFalse(res.isAutoInvoiceReuseDraft());
assertEquals(res.getSubscriptionEvents().size(), 1);
final SubscriptionBillingEventJson res1 = res.getSubscriptionEvents().get(0);
assertEquals(res1.getSubscriptionId(), subscriptionId);
assertEquals(res1.getEvents().size(), 2);
final BillingEventJson billingEvent1 = res1.getEvents().get(0);
assertEquals(billingEvent1.getBcdLocal(), 1);
assertEquals(billingEvent1.getPlanName(), "Test");
assertEquals(billingEvent1.getPhaseName(), "Test-evergreen");
assertEquals(billingEvent1.getEffDate().compareTo(effectiveDate1), 0);
assertNull(billingEvent1.getFixedPrice());
assertEquals(billingEvent1.getRecurringPrice().compareTo(new BigDecimal("13.0")), 0);
assertEquals(billingEvent1.getTransitionType(), SubscriptionBaseTransitionType.CREATE);
// Our Mock implementation returns null
assertNull(billingEvent1.getCatalogEffDt());
}
use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.
the class TestDefaultInvoiceGenerator method testInvoiceGeneration.
private void testInvoiceGeneration(final UUID accountId, final BillingEventSet events, final List<Invoice> existingInvoices, final LocalDate targetDate, final int expectedNumberOfItems, final BigDecimal expectedAmount) throws InvoiceApiException {
final Currency currency = Currency.USD;
final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, new AccountInvoices(null, null, existingInvoices), null, targetDate, currency, null, internalCallContext);
final Invoice invoice = invoiceWithMetadata.getInvoice();
assertNotNull(invoice);
assertEquals(invoice.getNumberOfItems(), expectedNumberOfItems);
existingInvoices.add(invoice);
distributeItems(existingInvoices);
assertEquals(invoice.getBalance(), KillBillMoney.of(expectedAmount, invoice.getCurrency()));
}
use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.
the class AccountTestUtils method createAccountData.
private static MutableAccountData createAccountData(final int billCycleDayLocal, final String phone) {
final String externalKey = UUID.randomUUID().toString();
final String email = UUID.randomUUID().toString().substring(0, 4) + '@' + UUID.randomUUID().toString().substring(0, 4);
final String name = UUID.randomUUID().toString();
final String locale = Locale.GERMANY.toString();
final DateTimeZone timeZone = DateTimeZone.forID("America/Los_Angeles");
final int firstNameLength = name.length();
final Currency currency = Currency.MXN;
final UUID paymentMethodId = UUID.randomUUID();
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 = Locale.GERMANY.getCountry();
final String postalCode = UUID.randomUUID().toString().substring(0, 4);
final String notes = UUID.randomUUID().toString();
return new DefaultMutableAccountData(externalKey, email, name, firstNameLength, currency, null, false, billCycleDayLocal, paymentMethodId, null, timeZone, locale, address1, address2, companyName, city, stateOrProvince, country, postalCode, phone, notes, false);
}
use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.
the class TestDefaultAccount method testBCDMerges.
@Test(groups = "fast", description = "Test Account BCD merge")
public void testBCDMerges() throws Exception {
final UUID accountId = UUID.randomUUID();
final Currency currency = Currency.BRL;
final String externalKey = UUID.randomUUID().toString();
final AccountData accountDataWithNullBCD = getAccountData(null, currency, externalKey);
final Account accountWithNullBCD = new DefaultAccount(accountId, accountDataWithNullBCD);
// Null BCD -> 0 BCD
Assert.assertEquals(accountWithNullBCD.getBillCycleDayLocal(), (Integer) 0);
final DefaultMutableAccountData accountDataWithZeroBCD = new DefaultMutableAccountData(accountDataWithNullBCD);
accountDataWithZeroBCD.setBillCycleDayLocal(0);
final Account accountWithZeroBCD = new DefaultAccount(accountId, accountDataWithZeroBCD);
// Null BCD and 0 BCD -> 0 BCD
Assert.assertEquals(accountWithNullBCD.mergeWithDelegate(accountWithZeroBCD).getBillCycleDayLocal(), (Integer) 0);
final DefaultMutableAccountData accountDataWithRealBCD = new DefaultMutableAccountData(accountDataWithNullBCD);
accountDataWithRealBCD.setBillCycleDayLocal(12);
final Account accountWithRealBCD = new DefaultAccount(accountId, accountDataWithRealBCD);
// Null BCD and real BCD -> real BCD
Assert.assertEquals(accountWithNullBCD.mergeWithDelegate(accountWithRealBCD).getBillCycleDayLocal(), (Integer) 12);
final DefaultMutableAccountData accountDataWithAnotherRealBCD = new DefaultMutableAccountData(accountDataWithNullBCD);
accountDataWithAnotherRealBCD.setBillCycleDayLocal(20);
final Account accountWithAnotherBCD = new DefaultAccount(accountId, accountDataWithAnotherRealBCD);
// Same BCD
Assert.assertEquals(accountWithAnotherBCD.mergeWithDelegate(accountWithAnotherBCD).getBillCycleDayLocal(), (Integer) 20);
try {
// Different BCD
Assert.assertEquals(accountWithAnotherBCD.mergeWithDelegate(accountWithRealBCD).getBillCycleDayLocal(), (Integer) 20);
Assert.fail();
} catch (final IllegalArgumentException e) {
Assert.assertTrue(true);
}
}
Aggregations