use of org.killbill.billing.invoice.dao.InvoiceModelDao in project killbill by killbill.
the class TestIntegrationInvoiceWithRepairLogic method testWithSuperflousRepairedItems.
@Test(groups = "slow")
public void testWithSuperflousRepairedItems() throws Exception {
// We take april as it has 30 days (easier to play with BCD)
final LocalDate today = new LocalDate(2012, 4, 1);
final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(1));
// Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
clock.setDay(today);
final String productName = "Shotgun";
final BillingPeriod term = BillingPeriod.MONTHLY;
final String pricelistName = PriceListSet.DEFAULT_PRICELIST_NAME;
//
// CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE NextEvent.INVOICE
//
final DefaultEntitlement bpEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.INVOICE, NextEvent.BLOCK);
assertNotNull(bpEntitlement);
List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
assertEquals(invoices.size(), 1);
ImmutableList<ExpectedInvoiceItemCheck> toBeChecked = ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, BigDecimal.ZERO));
invoiceChecker.checkInvoice(invoices.get(0).getId(), callContext, toBeChecked);
//
// Check we get the first invoice at the phase event
//
busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
// Move the clock to 2012-05-02
clock.addDays(31);
assertListenerStatus();
invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
assertEquals(invoices.size(), 2);
toBeChecked = ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, BigDecimal.ZERO));
invoiceChecker.checkInvoice(invoices.get(0).getId(), callContext, toBeChecked);
toBeChecked = ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("249.95")));
final Invoice lastInvoice = invoices.get(1);
invoiceChecker.checkInvoice(lastInvoice.getId(), callContext, toBeChecked);
//
// Let's add a bunch of items by hand to pretend we have lots of cancelling items that should be cleaned (data issue after a potential invoice bug)
//
// We test both full and partial repair.
/*
final UUID id, @Nullable final DateTime createdDate, final UUID accountId,
@Nullable final Integer invoiceNumber, final LocalDate invoiceDate, final LocalDate targetDate,
final Currency currency, final boolean migrated, final InvoiceStatus status, final boolean isParentInvoice
*/
final InvoiceModelDao shellInvoice = new InvoiceModelDao(UUID.randomUUID(), lastInvoice.getCreatedDate(), lastInvoice.getAccountId(), null, lastInvoice.getInvoiceDate(), lastInvoice.getTargetDate(), lastInvoice.getCurrency(), false, InvoiceStatus.COMMITTED, false);
final InvoiceItemModelDao recurring1 = new InvoiceItemModelDao(lastInvoice.getCreatedDate(), InvoiceItemType.RECURRING, lastInvoice.getId(), lastInvoice.getAccountId(), bpEntitlement.getBundleId(), bpEntitlement.getBaseEntitlementId(), "", "Shotgun", "shotgun-monthly", "shotgun-monthly-evergreen", null, null, new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), new BigDecimal("249.95"), new BigDecimal("249.95"), account.getCurrency(), null);
final InvoiceItemModelDao repair1 = new InvoiceItemModelDao(lastInvoice.getCreatedDate(), InvoiceItemType.REPAIR_ADJ, lastInvoice.getId(), lastInvoice.getAccountId(), bpEntitlement.getBundleId(), bpEntitlement.getBaseEntitlementId(), null, null, null, null, null, null, new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), new BigDecimal("-249.95"), new BigDecimal("-249.95"), account.getCurrency(), recurring1.getId());
final InvoiceItemModelDao recurring2 = new InvoiceItemModelDao(lastInvoice.getCreatedDate(), InvoiceItemType.RECURRING, lastInvoice.getId(), lastInvoice.getAccountId(), bpEntitlement.getBundleId(), bpEntitlement.getBaseEntitlementId(), "", "Shotgun", "shotgun-monthly", "shotgun-monthly-evergreen", null, null, new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), new BigDecimal("249.95"), new BigDecimal("249.95"), account.getCurrency(), null);
final InvoiceItemModelDao repair21 = new InvoiceItemModelDao(lastInvoice.getCreatedDate(), InvoiceItemType.REPAIR_ADJ, lastInvoice.getId(), lastInvoice.getAccountId(), bpEntitlement.getBundleId(), bpEntitlement.getBaseEntitlementId(), null, null, null, null, null, null, new LocalDate(2012, 5, 1), new LocalDate(2012, 5, 13), new BigDecimal("-100.95"), new BigDecimal("-100.95"), account.getCurrency(), recurring2.getId());
final InvoiceItemModelDao repair22 = new InvoiceItemModelDao(lastInvoice.getCreatedDate(), InvoiceItemType.REPAIR_ADJ, lastInvoice.getId(), lastInvoice.getAccountId(), bpEntitlement.getBundleId(), bpEntitlement.getBaseEntitlementId(), null, null, null, null, null, null, new LocalDate(2012, 5, 13), new LocalDate(2012, 5, 22), new BigDecimal("-100"), new BigDecimal("-100"), account.getCurrency(), recurring2.getId());
final InvoiceItemModelDao repair23 = new InvoiceItemModelDao(lastInvoice.getCreatedDate(), InvoiceItemType.REPAIR_ADJ, lastInvoice.getId(), lastInvoice.getAccountId(), bpEntitlement.getBundleId(), bpEntitlement.getBaseEntitlementId(), null, null, null, null, null, null, new LocalDate(2012, 5, 22), new LocalDate(2012, 6, 1), new BigDecimal("-49"), new BigDecimal("-49"), account.getCurrency(), recurring2.getId());
final InvoiceItemModelDao recurring3 = new InvoiceItemModelDao(lastInvoice.getCreatedDate(), InvoiceItemType.RECURRING, lastInvoice.getId(), lastInvoice.getAccountId(), bpEntitlement.getBundleId(), bpEntitlement.getBaseEntitlementId(), "", "Shotgun", "shotgun-monthly", "shotgun-monthly-evergreen", null, null, new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), new BigDecimal("249.95"), new BigDecimal("249.95"), account.getCurrency(), null);
final InvoiceItemModelDao repair3 = new InvoiceItemModelDao(lastInvoice.getCreatedDate(), InvoiceItemType.REPAIR_ADJ, lastInvoice.getId(), lastInvoice.getAccountId(), bpEntitlement.getBundleId(), bpEntitlement.getBaseEntitlementId(), null, null, null, null, null, null, new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), new BigDecimal("-249.95"), new BigDecimal("-249.95"), account.getCurrency(), recurring3.getId());
List<InvoiceItemModelDao> newItems = new ArrayList<InvoiceItemModelDao>();
newItems.add(recurring1);
newItems.add(repair1);
newItems.add(recurring2);
newItems.add(repair21);
newItems.add(repair22);
newItems.add(repair23);
newItems.add(recurring3);
newItems.add(repair3);
shellInvoice.addInvoiceItems(newItems);
invoiceDao.createInvoice(shellInvoice, null, ImmutableSet.of(), new FutureAccountNotifications(), null, internalCallContext);
// Move ahead one month, verify nothing from previous data was generated
busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
clock.addMonths(1);
assertListenerStatus();
invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
assertEquals(invoices.size(), 3);
toBeChecked = ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2012, 6, 1), new LocalDate(2012, 7, 1), InvoiceItemType.RECURRING, new BigDecimal("249.95")));
invoiceChecker.checkInvoice(invoices.get(2).getId(), callContext, toBeChecked);
}
use of org.killbill.billing.invoice.dao.InvoiceModelDao in project killbill by killbill.
the class TestDefaultInvoiceFormatter method testProcessedCurrencyExists.
@Test(groups = "fast")
public void testProcessedCurrencyExists() throws Exception {
// Use InvoiceModelDao to build the invoice to be able to set the processedCurrency (No suitable CTOR for DefaultInvoice on purpose)
final InvoiceModelDao invoiceModelDao = new InvoiceModelDao(UUID.randomUUID(), new LocalDate(), new LocalDate(), Currency.BRL, false);
invoiceModelDao.setProcessedCurrency(Currency.USD);
final Invoice invoice = new DefaultInvoice(invoiceModelDao);
checkOutput(invoice, "{{#invoice.processedCurrency}}" + "<tr>\n" + " <td class=\"processedCurrency\"><strong>{{invoice.processedCurrency}}</strong></td>\n" + "</tr>\n" + "{{/invoice.processedCurrency}}", "<tr>\n" + " <td class=\"processedCurrency\"><strong>USD</strong></td>\n" + "</tr>\n", Locale.US);
}
use of org.killbill.billing.invoice.dao.InvoiceModelDao in project killbill by killbill.
the class TestWithInvoiceHardening method testFixParkedAccountByVoidingInvoices.
//
// Complicated scenario where we want to test that given some bad data
// 1/ leading the account to be parked and
// 2/ leading the system to have both generated and used some credit
// we are able using our 'void' and credit 'deletion api' to come back to a good state, i.e
// credit was reclaimed, and account is back into a good state
//
@Test(groups = "slow")
public void testFixParkedAccountByVoidingInvoices() throws Exception {
final DateTimeZone testTimeZone = DateTimeZone.UTC;
final DateTime initialDate = new DateTime(2019, 4, 27, 0, 13, 42, 0, testTimeZone);
clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(0));
assertNotNull(account);
add_AUTO_PAY_OFF_Tag(account.getId(), ObjectType.ACCOUNT);
add_AUTO_INVOICING_OFF_Tag(account.getId(), ObjectType.ACCOUNT);
busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Blowdart", BillingPeriod.MONTHLY, "notrial", null);
UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), "Something", null, null, false, true, ImmutableList.<PluginProperty>of(), callContext);
assertListenerStatus();
final BlockingState blockingState2 = new DefaultBlockingState(entitlementId, BlockingStateType.SUBSCRIPTION, "SOMETHING_BLOCK", "company.a.b.c", true, true, true, null);
subscriptionApi.addBlockingState(blockingState2, new LocalDate(2019, 5, 17), ImmutableList.<PluginProperty>of(), callContext);
// 2019-05-17
busHandler.pushExpectedEvents(NextEvent.BLOCK);
clock.addDays(20);
assertListenerStatus();
//
// Simulate some bad data on disk
//
// Invoice 1: RECURRING 2019-4-27 -> 2019-5-27
final InvoiceModelDao firstInvoice = new InvoiceModelDao(UUID.randomUUID(), clock.getUTCNow(), account.getId(), null, new LocalDate(2019, 4, 27), new LocalDate(2019, 4, 27), account.getCurrency(), false, InvoiceStatus.COMMITTED, false);
final UUID initialRecuringItemId = UUID.randomUUID();
firstInvoice.addInvoiceItem(new InvoiceItemModelDao(initialRecuringItemId, clock.getUTCNow(), InvoiceItemType.RECURRING, firstInvoice.getId(), account.getId(), null, null, entitlementId, "", "Blowdart", "blowdart-monthly-notrial", "blowdart-monthly-notrial-evergreen", null, null, new LocalDate(2019, 4, 27), new LocalDate(2019, 5, 27), new BigDecimal("29.95"), new BigDecimal("29.95"), account.getCurrency(), null, null, null));
insertInvoiceItems(firstInvoice);
// Invoice 2: REPAIR_ADJ 2019-5-3 -> 2019-5-27 => Simulate the BLOCK billing on 2019-5-3
final InvoiceModelDao secondInvoice = new InvoiceModelDao(UUID.randomUUID(), clock.getUTCNow(), account.getId(), null, new LocalDate(2019, 5, 3), new LocalDate(2019, 5, 3), account.getCurrency(), false, InvoiceStatus.COMMITTED, false);
secondInvoice.addInvoiceItem(new InvoiceItemModelDao(UUID.randomUUID(), clock.getUTCNow(), InvoiceItemType.REPAIR_ADJ, secondInvoice.getId(), account.getId(), null, null, entitlementId, "", null, null, null, null, null, new LocalDate(2019, 5, 3), new LocalDate(2019, 5, 27), new BigDecimal("-23.96"), null, account.getCurrency(), initialRecuringItemId, null, null));
insertInvoiceItems(secondInvoice);
// Invoice 3: REPAIR_ADJ 2019-4-27 -> 2019-5-03 => Simulate the UNBLOCK billing on 2019-5-3 and RECURRING 2019-4-27 -> 2019-5-27
// Original initialRecuringItemId is now fully repaired and we have re-invoiced for the full period
//
final InvoiceModelDao thirdInvoice = new InvoiceModelDao(UUID.randomUUID(), clock.getUTCNow(), account.getId(), null, new LocalDate(2019, 5, 3), new LocalDate(2019, 5, 3), account.getCurrency(), false, InvoiceStatus.COMMITTED, false);
final UUID secondRecurringItemId = UUID.randomUUID();
thirdInvoice.addInvoiceItem(new InvoiceItemModelDao(secondRecurringItemId, clock.getUTCNow(), InvoiceItemType.RECURRING, thirdInvoice.getId(), account.getId(), null, null, entitlementId, "", "Blowdart", "blowdart-monthly-notrial", "blowdart-monthly-notrial-evergreen", null, null, new LocalDate(2019, 4, 27), new LocalDate(2019, 5, 27), new BigDecimal("29.95"), new BigDecimal("29.95"), account.getCurrency(), null, null, null));
thirdInvoice.addInvoiceItem(new InvoiceItemModelDao(UUID.randomUUID(), clock.getUTCNow(), InvoiceItemType.REPAIR_ADJ, thirdInvoice.getId(), account.getId(), null, null, entitlementId, "", null, null, null, null, null, new LocalDate(2019, 4, 27), new LocalDate(2019, 5, 03), new BigDecimal("-5.99"), null, account.getCurrency(), initialRecuringItemId, null, null));
insertInvoiceItems(thirdInvoice);
// Invoice 4: REPAIR_ADJ 2019-5-17 -> 2019-5-27 => Simulate the BLOCK billing on 2019-5-17
final InvoiceModelDao fourthInvoice = new InvoiceModelDao(UUID.randomUUID(), clock.getUTCNow(), account.getId(), null, new LocalDate(2019, 5, 17), new LocalDate(2019, 5, 17), account.getCurrency(), false, InvoiceStatus.COMMITTED, false);
fourthInvoice.addInvoiceItem(new InvoiceItemModelDao(UUID.randomUUID(), clock.getUTCNow(), InvoiceItemType.REPAIR_ADJ, fourthInvoice.getId(), account.getId(), null, null, entitlementId, "", null, null, null, null, null, new LocalDate(2019, 5, 17), new LocalDate(2019, 5, 27), new BigDecimal("-9.98"), null, account.getCurrency(), secondRecurringItemId, null, null));
insertInvoiceItems(fourthInvoice);
// Invoice 5: RECURRING 2019-4-17 -> 2019-5-17
final InvoiceModelDao fifthInvoice = new InvoiceModelDao(UUID.randomUUID(), clock.getUTCNow(), account.getId(), null, new LocalDate(2019, 5, 17), new LocalDate(2019, 5, 17), account.getCurrency(), false, InvoiceStatus.COMMITTED, false);
final UUID thirdRecurringItemId = UUID.randomUUID();
fifthInvoice.addInvoiceItem(new InvoiceItemModelDao(thirdRecurringItemId, clock.getUTCNow(), InvoiceItemType.RECURRING, fifthInvoice.getId(), account.getId(), null, null, entitlementId, "", "Blowdart", "blowdart-monthly-notrial", "blowdart-monthly-notrial-evergreen", null, null, new LocalDate(2019, 4, 27), new LocalDate(2019, 5, 17), new BigDecimal("5.99"), new BigDecimal("29.95"), account.getCurrency(), null, null, null));
insertInvoiceItems(fifthInvoice);
// Trigger IllegalStateException: Double billing detected
busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE, NextEvent.INVOICE, NextEvent.INVOICE, NextEvent.INVOICE, NextEvent.TAG);
tagUserApi.removeTag(account.getId(), ObjectType.ACCOUNT, ControlTagType.AUTO_INVOICING_OFF.getId(), callContext);
busHandler.waitAndIgnoreEvents(3000);
// <! end of setup>
//
// We need to first VOID invoices that have REPAIR items otherwsie VOID operation on invoices being repaired would fail
// However, in order to VOID such REPAIR invoices for which (system) credit was generated, we need to ensure there is enough
// credit available on the account, hence the step of manually reclaiming credit before each operation.
//
deleteUsedCredit(account.getId(), new BigDecimal("-23.96"));
busHandler.pushExpectedEvents(NextEvent.INVOICE_ADJUSTMENT);
invoiceUserApi.voidInvoice(secondInvoice.getId(), callContext);
assertListenerStatus();
deleteUsedCredit(account.getId(), new BigDecimal("-5.99"));
deleteUsedCredit(account.getId(), new BigDecimal("-3.99"));
busHandler.pushExpectedEvents(NextEvent.INVOICE_ADJUSTMENT);
invoiceUserApi.voidInvoice(fourthInvoice.getId(), callContext);
assertListenerStatus();
busHandler.pushExpectedEvents(NextEvent.INVOICE_ADJUSTMENT);
invoiceUserApi.voidInvoice(thirdInvoice.getId(), callContext);
assertListenerStatus();
busHandler.pushExpectedEvents(NextEvent.INVOICE_ADJUSTMENT);
invoiceUserApi.voidInvoice(firstInvoice.getId(), callContext);
assertListenerStatus();
// This remove the __PARK__ tag and fixes the state !
busHandler.pushExpectedEvents(NextEvent.TAG, NextEvent.NULL_INVOICE);
try {
invoiceUserApi.triggerInvoiceGeneration(account.getId(), new LocalDate(2019, 5, 17), callContext);
fail();
} catch (final InvoiceApiException e) {
assertEquals(e.getCode(), INVOICE_NOTHING_TO_DO.getCode());
} finally {
assertListenerStatus();
}
}
use of org.killbill.billing.invoice.dao.InvoiceModelDao in project killbill by killbill.
the class TestInvoiceDispatcher method testDryRunInvoice.
@Test(groups = "slow")
public void testDryRunInvoice() throws InvoiceApiException, AccountApiException, CatalogApiException, SubscriptionBaseApiException {
final UUID accountId = account.getId();
final BillingEventSet events = new MockBillingEventSet();
final Plan plan = MockPlan.createBicycleNoTrialEvergreen1USD();
final PlanPhase planPhase = MockPlanPhase.create1USDMonthlyEvergreen();
final DateTime effectiveDate = clock.getUTCNow().minusDays(1);
final Currency currency = Currency.USD;
final BigDecimal fixedPrice = null;
events.add(invoiceUtil.createMockBillingEvent(account, subscription, effectiveDate, plan, planPhase, fixedPrice, BigDecimal.ONE, currency, BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "", 1L, SubscriptionBaseTransitionType.CREATE));
Mockito.when(billingApi.getBillingEventsForAccountAndUpdateAccountBCD(Mockito.<UUID>any(), Mockito.<DryRunArguments>any(), Mockito.<LocalDate>any(), Mockito.<InternalCallContext>any())).thenReturn(events);
final LocalDate target = internalCallContext.toLocalDate(effectiveDate);
final InvoiceDispatcher dispatcher = new InvoiceDispatcher(generator, accountApi, billingApi, subscriptionApi, invoiceDao, internalCallContextFactory, invoicePluginDispatcher, locker, bus, notificationQueueService, invoiceConfig, clock, invoiceOptimizer, parkedAccountsManager);
Invoice invoice = dispatcher.processAccountFromNotificationOrBusEvent(accountId, target, new DryRunFutureDateArguments(), false, context);
Assert.assertNotNull(invoice);
List<InvoiceModelDao> invoices = invoiceDao.getInvoicesByAccount(false, context);
Assert.assertEquals(invoices.size(), 0);
// Try it again to double check
invoice = dispatcher.processAccountFromNotificationOrBusEvent(accountId, target, new DryRunFutureDateArguments(), false, context);
Assert.assertNotNull(invoice);
invoices = invoiceDao.getInvoicesByAccount(false, context);
Assert.assertEquals(invoices.size(), 0);
// This time no dry run
invoice = dispatcher.processAccountFromNotificationOrBusEvent(accountId, target, null, false, context);
Assert.assertNotNull(invoice);
invoices = invoiceDao.getInvoicesByAccount(false, context);
Assert.assertEquals(invoices.size(), 1);
}
use of org.killbill.billing.invoice.dao.InvoiceModelDao in project killbill by killbill.
the class TestDefaultInvoiceMigrationApi method createAndCheckMigrationInvoice.
private UUID createAndCheckMigrationInvoice(final UUID accountId) throws InvoiceApiException {
final InvoiceItem recurringItem = new RecurringInvoiceItem(null, accountId, null, null, "productName", "planName", "phaseName", null, new LocalDate(2016, 2, 1), new LocalDate(2016, 3, 1), MIGRATION_INVOICE_AMOUNT, MIGRATION_INVOICE_AMOUNT, MIGRATION_INVOICE_CURRENCY);
final UUID migrationInvoiceId = invoiceUserApi.createMigrationInvoice(accountId, date_migrated, ImmutableList.of(recurringItem), callContext);
Assert.assertNotNull(migrationInvoiceId);
// Double check it was created and values are correct
final InvoiceModelDao invoice = invoiceDao.getById(migrationInvoiceId, internalCallContext);
Assert.assertNotNull(invoice);
Assert.assertEquals(invoice.getAccountId(), accountId);
// temp to avoid tz test artifact
Assert.assertEquals(invoice.getTargetDate().compareTo(date_migrated), 0);
// Assert.assertEquals(invoice.getTargetDate(),now);
Assert.assertEquals(invoice.getInvoiceItems().size(), 1);
Assert.assertEquals(invoice.getInvoiceItems().get(0).getAmount().compareTo(MIGRATION_INVOICE_AMOUNT), 0);
Assert.assertEquals(invoice.getInvoiceItems().get(0).getType(), InvoiceItemType.RECURRING);
Assert.assertEquals(InvoiceModelDaoHelper.getRawBalanceForRegularInvoice(invoice).compareTo(BigDecimal.ZERO), 0);
Assert.assertEquals(invoice.getCurrency(), MIGRATION_INVOICE_CURRENCY);
Assert.assertTrue(invoice.isMigrated());
return migrationInvoiceId;
}
Aggregations