Search in sources :

Example 21 with InvoiceItemModelDao

use of org.killbill.billing.invoice.dao.InvoiceItemModelDao in project killbill by killbill.

the class TestIntegrationInvoiceWithRepairLogic method testWithFullRepairAndExistingPartialAdjustment.

@Test(groups = "slow")
public void testWithFullRepairAndExistingPartialAdjustment() throws Exception {
    final LocalDate today = new LocalDate(2013, 7, 19);
    // Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
    clock.setDeltaFromReality(today.toDateTimeAtCurrentTime(DateTimeZone.UTC).getMillis() - clock.getUTCNow().getMillis());
    final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(1));
    final String productName = "Shotgun";
    final BillingPeriod term = BillingPeriod.ANNUAL;
    // 
    // CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE, NextEvent.BLOCK NextEvent.INVOICE
    // 
    final DefaultEntitlement bpEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    assertNotNull(bpEntitlement);
    assertEquals(invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext).size(), 1);
    assertEquals(bpEntitlement.getSubscriptionBase().getCurrentPlan().getRecurringBillingPeriod(), BillingPeriod.ANNUAL);
    // Move out of trials for interesting invoices adjustments
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(30);
    assertListenerStatus();
    List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
    assertEquals(invoices.size(), 2);
    ImmutableList<ExpectedInvoiceItemCheck> toBeChecked = ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2013, 8, 18), new LocalDate(2014, 8, 18), InvoiceItemType.RECURRING, new BigDecimal("2399.95")));
    invoiceChecker.checkInvoice(invoices.get(1).getId(), callContext, toBeChecked);
    // Cancelation SOT -> fully repaired
    busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CANCEL, NextEvent.INVOICE);
    bpEntitlement.cancelEntitlementWithPolicyOverrideBillingPolicy(EntitlementActionPolicy.IMMEDIATE, BillingActionPolicy.START_OF_TERM, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    final InvoiceItem targetItem = invoices.get(1).getInvoiceItems().get(0);
    final InvoiceItemModelDao adj = new InvoiceItemModelDao(clock.getUTCNow(), InvoiceItemType.ITEM_ADJ, targetItem.getInvoiceId(), targetItem.getAccountId(), null, null, null, targetItem.getProductName(), targetItem.getPlanName(), targetItem.getPhaseName(), targetItem.getUsageName(), targetItem.getCatalogEffectiveDate(), clock.getUTCToday(), clock.getUTCToday(), new BigDecimal("-1000.00"), null, targetItem.getCurrency(), targetItem.getId());
    final InvoiceModelDao invoiceForAdj = new InvoiceModelDao(UUID.randomUUID(), clock.getUTCNow(), bpEntitlement.getAccountId(), null, clock.getUTCToday(), clock.getUTCToday(), Currency.USD, false, InvoiceStatus.COMMITTED, false);
    invoiceForAdj.addInvoiceItem(adj);
    busHandler.pushExpectedEvents(NextEvent.INVOICE_ADJUSTMENT);
    insertInvoiceItems(invoiceForAdj);
    assertListenerStatus();
    // __PARK__ account
    busHandler.pushExpectedEvents(NextEvent.TAG);
    try {
        invoiceUserApi.triggerInvoiceGeneration(account.getId(), clock.getUTCToday(), callContext);
        fail("Should not have generated an extra invoice");
    } catch (final InvoiceApiException e) {
        assertListenerStatus();
        assertTrue(e.getCause().getMessage().startsWith("Too many repairs for invoiceItemId"));
    } finally {
        assertListenerStatus();
    }
}
Also used : Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) LocalDate(org.joda.time.LocalDate) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) BigDecimal(java.math.BigDecimal) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao) Test(org.testng.annotations.Test)

Example 22 with InvoiceItemModelDao

use of org.killbill.billing.invoice.dao.InvoiceItemModelDao in project killbill by killbill.

the class TestIntegrationInvoiceWithRepairLogic method testRepairWithFullItemAdjustmentV2WithOldData.

// 
// Same scenario as previous one but we insert a RECURRING for 2013-8-18 -> 2013-9-17 to simulate old full item adjustment behavior
// 
@Test(groups = "slow")
public void testRepairWithFullItemAdjustmentV2WithOldData() throws Exception {
    DefaultEntitlement bpEntitlement = setupTestRepairWithFullItemAdjustment();
    final InvoiceModelDao invoiceForPreviousBehavior = new InvoiceModelDao(UUID.randomUUID(), clock.getUTCNow(), bpEntitlement.getAccountId(), null, new LocalDate(2013, 9, 17), new LocalDate(2013, 9, 17), Currency.USD, false, InvoiceStatus.COMMITTED, false);
    invoiceForPreviousBehavior.addInvoiceItem(new InvoiceItemModelDao(UUID.randomUUID(), clock.getUTCNow(), InvoiceItemType.RECURRING, invoiceForPreviousBehavior.getId(), bpEntitlement.getAccountId(), null, null, bpEntitlement.getId(), "", "Shotgun", "shotgun-annual", "shotgun-annual-evergreen", null, null, new LocalDate(2013, 8, 18), new LocalDate(2013, 9, 17), new BigDecimal("197.26"), new BigDecimal("2399.95"), Currency.USD, null, null, null));
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    insertInvoiceItems(invoiceForPreviousBehavior);
    assertListenerStatus();
    // 
    // Move clock to 2013-09-17
    clock.addDays(30);
    busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CANCEL, NextEvent.NULL_INVOICE);
    bpEntitlement.cancelEntitlementWithPolicyOverrideBillingPolicy(EntitlementActionPolicy.IMMEDIATE, BillingActionPolicy.IMMEDIATE, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(bpEntitlement.getAccountId(), false, false, callContext);
    assertEquals(invoices.size(), 3);
    ImmutableList<ExpectedInvoiceItemCheck> toBeChecked = ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2013, 8, 18), new LocalDate(2014, 8, 18), InvoiceItemType.RECURRING, new BigDecimal("2399.95")), new ExpectedInvoiceItemCheck(new LocalDate(2013, 8, 18), new LocalDate(2013, 8, 18), InvoiceItemType.ITEM_ADJ, new BigDecimal("-2399.95")));
    invoiceChecker.checkInvoice(invoices.get(1).getId(), callContext, toBeChecked);
    // This is the invoice + item we inserted by hand to simulate the old data; we verify there is nothing more
    toBeChecked = ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2013, 8, 18), new LocalDate(2013, 9, 17), InvoiceItemType.RECURRING, new BigDecimal("197.26")));
    invoiceChecker.checkInvoice(invoices.get(2).getId(), callContext, toBeChecked);
    final BigDecimal accountBalance = invoiceUserApi.getAccountBalance(bpEntitlement.getAccountId(), callContext);
    assertEquals(accountBalance.compareTo(BigDecimal.ZERO), 0);
    checkNoMoreInvoiceToGenerate(bpEntitlement.getAccountId());
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) LocalDate(org.joda.time.LocalDate) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 23 with InvoiceItemModelDao

use of org.killbill.billing.invoice.dao.InvoiceItemModelDao in project killbill by killbill.

the class TestIntegrationInvoiceWithRepairLogic method testOverlappingItems.

// This is a beatrix level test matching our invoice TestFixedAndRecurringInvoiceItemGenerator#testOverlappingItems
@Test(groups = "slow")
public void testOverlappingItems() throws Exception {
    final DateTime initialDate = new DateTime(2016, 1, 1, 0, 0, 0, 0, testTimeZone);
    final LocalDate startDate = initialDate.toLocalDate();
    clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
    final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(1));
    assertNotNull(account);
    add_AUTO_INVOICING_OFF_Tag(account.getId(), ObjectType.ACCOUNT);
    add_AUTO_PAY_OFF_Tag(account.getId(), ObjectType.ACCOUNT);
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("pistol-monthly-notrial");
    busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CREATE);
    final UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec, null, null, null), null, startDate, startDate, false, false, ImmutableList.<PluginProperty>of(), callContext);
    final Entitlement bpEntitlement = entitlementApi.getEntitlementForId(entitlementId, callContext);
    assertListenerStatus();
    final InvoiceModelDao existingBadInvoice = new InvoiceModelDao(UUID.randomUUID(), clock.getUTCNow(), account.getId(), null, startDate, startDate, Currency.USD, false, InvoiceStatus.COMMITTED, false);
    final InvoiceItemModelDao wrongRecurring = new InvoiceItemModelDao(initialDate, InvoiceItemType.RECURRING, existingBadInvoice.getId(), existingBadInvoice.getAccountId(), bpEntitlement.getBundleId(), entitlementId, "", "Pistol", "pistol-monthly-notrial", "pistol-monthly-notrial-evergreen", null, null, new LocalDate(2016, 1, 1), new LocalDate(2016, 1, 30), new BigDecimal("18.66"), new BigDecimal("19.95"), account.getCurrency(), null);
    existingBadInvoice.addInvoiceItem(wrongRecurring);
    busHandler.pushExpectedEvents(NextEvent.INVOICE);
    insertInvoiceItems(existingBadInvoice);
    assertListenerStatus();
    // Prior the change introduced in 6338109f8cdc7, this is what the code would have generated
    final InvoiceModelDao prevBehaviorInvoice = new InvoiceModelDao(UUID.randomUUID(), clock.getUTCNow(), account.getId(), null, startDate, startDate, Currency.USD, false, InvoiceStatus.COMMITTED, false);
    final InvoiceItemModelDao prevRecurring = new InvoiceItemModelDao(initialDate, InvoiceItemType.RECURRING, prevBehaviorInvoice.getId(), prevBehaviorInvoice.getAccountId(), bpEntitlement.getBundleId(), entitlementId, "", "Pistol", "pistol-monthly-notrial", "pistol-monthly-notrial-evergreen", null, null, new LocalDate(2016, 1, 1), new LocalDate(2016, 2, 1), new BigDecimal("19.85"), new BigDecimal("19.95"), account.getCurrency(), null);
    final InvoiceItemModelDao prevRepair = new InvoiceItemModelDao(prevBehaviorInvoice.getCreatedDate(), InvoiceItemType.REPAIR_ADJ, prevBehaviorInvoice.getId(), prevBehaviorInvoice.getAccountId(), bpEntitlement.getBundleId(), bpEntitlement.getBaseEntitlementId(), null, null, null, null, null, null, new LocalDate(2016, 1, 1), new LocalDate(2016, 1, 30), new BigDecimal("-18.66"), new BigDecimal("19.95"), account.getCurrency(), wrongRecurring.getId());
    prevBehaviorInvoice.addInvoiceItem(prevRecurring);
    prevBehaviorInvoice.addInvoiceItem(prevRepair);
    busHandler.pushExpectedEvents(NextEvent.INVOICE);
    insertInvoiceItems(prevBehaviorInvoice);
    assertListenerStatus();
    busHandler.pushExpectedEvents(NextEvent.NULL_INVOICE);
    remove_AUTO_INVOICING_OFF_Tag(account.getId(), ObjectType.ACCOUNT);
    assertListenerStatus();
    checkNoMoreInvoiceToGenerate(account);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) Account(org.killbill.billing.account.api.Account) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) UUID(java.util.UUID) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 24 with InvoiceItemModelDao

use of org.killbill.billing.invoice.dao.InvoiceItemModelDao 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);
}
Also used : Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) FutureAccountNotifications(org.killbill.billing.invoice.InvoiceDispatcher.FutureAccountNotifications) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) BigDecimal(java.math.BigDecimal) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao) Test(org.testng.annotations.Test)

Example 25 with InvoiceItemModelDao

use of org.killbill.billing.invoice.dao.InvoiceItemModelDao 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();
    }
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) BlockingState(org.killbill.billing.entitlement.api.BlockingState) LocalDate(org.joda.time.LocalDate) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao) UUID(java.util.UUID) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) Test(org.testng.annotations.Test)

Aggregations

InvoiceItemModelDao (org.killbill.billing.invoice.dao.InvoiceItemModelDao)25 InvoiceModelDao (org.killbill.billing.invoice.dao.InvoiceModelDao)23 LocalDate (org.joda.time.LocalDate)18 BigDecimal (java.math.BigDecimal)17 Invoice (org.killbill.billing.invoice.api.Invoice)13 Test (org.testng.annotations.Test)13 Account (org.killbill.billing.account.api.Account)12 UUID (java.util.UUID)10 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)10 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)10 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)10 DateTime (org.joda.time.DateTime)9 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)7 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)6 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)5 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)5 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)5 Function (com.google.common.base.Function)4 AccountApiException (org.killbill.billing.account.api.AccountApiException)4 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)4