Search in sources :

Example 31 with InvoiceModelDao

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

the class DefaultInvoiceUserApi method getInvoiceByNumber.

@Override
public Invoice getInvoiceByNumber(final Integer number, final TenantContext context) throws InvoiceApiException {
    // The account record id will be populated in the DAO
    final InternalTenantContext internalTenantContextWithoutAccountRecordId = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(context);
    final InvoiceModelDao invoice = dao.getByNumber(number, internalTenantContextWithoutAccountRecordId);
    final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(invoice.getAccountId(), context);
    return new DefaultInvoice(invoice, getCatalogSafelyForPrettyNames(internalTenantContext));
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice)

Example 32 with InvoiceModelDao

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

the class TestIntegrationInvoiceWithRepairLogic method testWithPartialRepairAndExistingPartialTooLargeAdjustment.

@Test(groups = "slow")
public void testWithPartialRepairAndExistingPartialTooLargeAdjustment() 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);
    clock.addMonths(6);
    // Cancelation IMM -> partially repaired
    busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CANCEL, NextEvent.INVOICE);
    bpEntitlement.cancelEntitlementWithPolicyOverrideBillingPolicy(EntitlementActionPolicy.IMMEDIATE, BillingActionPolicy.IMMEDIATE, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    // 1209.84
    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("-1300.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 33 with InvoiceModelDao

use of org.killbill.billing.invoice.dao.InvoiceModelDao 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 34 with InvoiceModelDao

use of org.killbill.billing.invoice.dao.InvoiceModelDao 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 35 with InvoiceModelDao

use of org.killbill.billing.invoice.dao.InvoiceModelDao 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)

Aggregations

InvoiceModelDao (org.killbill.billing.invoice.dao.InvoiceModelDao)40 InvoiceItemModelDao (org.killbill.billing.invoice.dao.InvoiceItemModelDao)23 LocalDate (org.joda.time.LocalDate)22 Invoice (org.killbill.billing.invoice.api.Invoice)21 BigDecimal (java.math.BigDecimal)18 Test (org.testng.annotations.Test)16 UUID (java.util.UUID)15 Account (org.killbill.billing.account.api.Account)12 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)12 DateTime (org.joda.time.DateTime)11 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)11 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)10 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)10 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)9 LinkedList (java.util.LinkedList)6 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)6 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)6 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)6 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)5 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)5