Search in sources :

Example 16 with InvoiceItemModelDao

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

the class InvoiceApiHelper method dispatchToInvoicePluginsAndInsertItems.

public List<InvoiceItem> dispatchToInvoicePluginsAndInsertItems(final UUID accountId, final boolean isDryRun, final WithAccountLock withAccountLock, final CallContext context) throws InvoiceApiException {
    GlobalLock lock = null;
    try {
        lock = locker.lockWithNumberOfTries(LockerType.ACCNT_INV_PAY.toString(), accountId.toString(), invoiceConfig.getMaxGlobalLockRetries());
        final Iterable<Invoice> invoicesForPlugins = withAccountLock.prepareInvoices();
        final List<InvoiceModelDao> invoiceModelDaos = new LinkedList<InvoiceModelDao>();
        for (final Invoice invoiceForPlugin : invoicesForPlugins) {
            // Call plugin
            final List<InvoiceItem> additionalInvoiceItems = invoicePluginDispatcher.getAdditionalInvoiceItems(invoiceForPlugin, isDryRun, context);
            invoiceForPlugin.addInvoiceItems(additionalInvoiceItems);
            // Transformation to InvoiceModelDao
            final InvoiceModelDao invoiceModelDao = new InvoiceModelDao(invoiceForPlugin);
            final List<InvoiceItem> invoiceItems = invoiceForPlugin.getInvoiceItems();
            final List<InvoiceItemModelDao> invoiceItemModelDaos = toInvoiceItemModelDao(invoiceItems);
            invoiceModelDao.addInvoiceItems(invoiceItemModelDaos);
            // Keep track of modified invoices
            invoiceModelDaos.add(invoiceModelDao);
        }
        final InternalCallContext internalCallContext = internalCallContextFactory.createInternalCallContext(accountId, context);
        final List<InvoiceItemModelDao> createdInvoiceItems = dao.createInvoices(invoiceModelDaos, internalCallContext);
        return fromInvoiceItemModelDao(createdInvoiceItems);
    } catch (final LockFailedException e) {
        log.warn("Failed to process invoice items for accountId='{}'", accountId.toString(), e);
        return ImmutableList.<InvoiceItem>of();
    } finally {
        if (lock != null) {
            lock.release();
        }
    }
}
Also used : GlobalLock(org.killbill.commons.locker.GlobalLock) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) LockFailedException(org.killbill.commons.locker.LockFailedException) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) LinkedList(java.util.LinkedList)

Example 17 with InvoiceItemModelDao

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

the class InvoiceTestUtils method createAndPersistInvoice.

public static Invoice createAndPersistInvoice(final TestInvoiceHelper testInvoiceHelper, final InvoiceDao invoiceDao, final Clock clock, final Iterable<BigDecimal> amounts, final Currency currency, final InternalCallContext internalCallContext) throws EntityPersistenceException {
    final Invoice invoice = Mockito.mock(Invoice.class);
    final UUID invoiceId = UUID.randomUUID();
    final UUID accountId;
    try {
        final Account account = testInvoiceHelper.createAccount(internalCallContext.toCallContext(null, null));
        accountId = account.getId();
    } catch (final AccountApiException e) {
        Assert.fail(e.getMessage());
        return null;
    }
    Mockito.when(invoice.getId()).thenReturn(invoiceId);
    Mockito.when(invoice.getAccountId()).thenReturn(accountId);
    final LocalDate today = clock.getUTCToday();
    Mockito.when(invoice.getInvoiceDate()).thenReturn(today);
    Mockito.when(invoice.getTargetDate()).thenReturn(today);
    Mockito.when(invoice.getCurrency()).thenReturn(currency);
    Mockito.when(invoice.isMigrationInvoice()).thenReturn(false);
    Mockito.when(invoice.getStatus()).thenReturn(InvoiceStatus.COMMITTED);
    final List<InvoiceItem> invoiceItems = new ArrayList<InvoiceItem>();
    final List<InvoiceItemModelDao> invoiceModelItems = new ArrayList<InvoiceItemModelDao>();
    for (final BigDecimal amount : amounts) {
        final InvoiceItem invoiceItem = createInvoiceItem(clock, invoiceId, accountId, amount, currency);
        invoiceModelItems.add(new InvoiceItemModelDao(invoiceItem));
        invoiceItems.add(invoiceItem);
    }
    Mockito.when(invoice.getInvoiceItems()).thenReturn(invoiceItems);
    final InvoiceModelDao invoiceModelDao = new InvoiceModelDao(invoice);
    invoiceModelDao.addInvoiceItems(invoiceModelItems);
    invoiceDao.createInvoices(ImmutableList.<InvoiceModelDao>of(invoiceModelDao), null, ImmutableSet.of(), internalCallContext);
    return invoice;
}
Also used : Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) AccountApiException(org.killbill.billing.account.api.AccountApiException) ArrayList(java.util.ArrayList) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal)

Example 18 with InvoiceItemModelDao

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

the class TestInvoiceHelper method createInvoice.

public void createInvoice(final Invoice invoice, final InternalCallContext internalCallContext) throws EntityPersistenceException {
    final InvoiceModelDao invoiceModelDao = new InvoiceModelDao(invoice);
    final List<InvoiceItemModelDao> invoiceItemModelDaos = ImmutableList.<InvoiceItemModelDao>copyOf(Collections2.transform(invoice.getInvoiceItems(), new Function<InvoiceItem, InvoiceItemModelDao>() {

        @Override
        public InvoiceItemModelDao apply(final InvoiceItem input) {
            return new InvoiceItemModelDao(input);
        }
    }));
    invoiceSqlDao.create(invoiceModelDao, internalCallContext);
    for (final InvoiceItem invoiceItem : invoice.getInvoiceItems()) {
        createInvoiceItem(invoiceItem, internalCallContext);
    }
}
Also used : Function(com.google.common.base.Function) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao)

Example 19 with InvoiceItemModelDao

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

the class DefaultInvoiceUserApi method getInvoiceItemsByParentInvoice.

@Override
public List<InvoiceItem> getInvoiceItemsByParentInvoice(final UUID parentInvoiceId, final TenantContext context) throws InvoiceApiException {
    final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(parentInvoiceId, ObjectType.INVOICE, context);
    final VersionedCatalog catalog = getCatalogSafelyForPrettyNames(internalTenantContext);
    return ImmutableList.copyOf(Collections2.transform(dao.getInvoiceItemsByParentInvoice(parentInvoiceId, internalTenantContext), new Function<InvoiceItemModelDao, InvoiceItem>() {

        @Override
        public InvoiceItem apply(final InvoiceItemModelDao input) {
            return InvoiceItemFactory.fromModelDaoWithCatalog(input, catalog);
        }
    }));
}
Also used : VersionedCatalog(org.killbill.billing.catalog.api.VersionedCatalog) Function(com.google.common.base.Function) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao)

Example 20 with InvoiceItemModelDao

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

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