Search in sources :

Example 6 with InvoiceItemModelDao

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

the class DefaultInvoiceUserApi method createMigrationInvoice.

@Override
public UUID createMigrationInvoice(final UUID accountId, final LocalDate targetDate, final Iterable<InvoiceItem> items, final CallContext context) {
    final InternalCallContext internalCallContext = internalCallContextFactory.createInternalCallContext(accountId, context);
    final LocalDate invoiceDate = internalCallContext.toLocalDate(internalCallContext.getCreatedDate());
    final InvoiceModelDao migrationInvoice = new InvoiceModelDao(accountId, invoiceDate, targetDate, items.iterator().next().getCurrency(), true);
    final List<InvoiceItemModelDao> itemModelDaos = ImmutableList.copyOf(Iterables.transform(items, new Function<InvoiceItem, InvoiceItemModelDao>() {

        @Override
        public InvoiceItemModelDao apply(final InvoiceItem input) {
            return new InvoiceItemModelDao(internalCallContext.getCreatedDate(), input.getInvoiceItemType(), migrationInvoice.getId(), accountId, input.getBundleId(), input.getSubscriptionId(), input.getDescription(), input.getPlanName(), input.getPhaseName(), input.getUsageName(), input.getStartDate(), input.getEndDate(), input.getAmount(), input.getRate(), input.getCurrency(), input.getLinkedItemId());
        }
    }));
    migrationInvoice.addInvoiceItems(itemModelDaos);
    dao.createInvoices(ImmutableList.<InvoiceModelDao>of(migrationInvoice), internalCallContext);
    return migrationInvoice.getId();
}
Also used : Function(com.google.common.base.Function) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) LocalDate(org.joda.time.LocalDate)

Example 7 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));
        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);
    Mockito.when(invoice.getInvoiceDate()).thenReturn(clock.getUTCToday());
    Mockito.when(invoice.getTargetDate()).thenReturn(clock.getUTCToday());
    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), 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) BigDecimal(java.math.BigDecimal)

Example 8 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, 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, 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-monthly", "shotgun-monthly-evergreen", 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, 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-monthly", "shotgun-monthly-evergreen", 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, 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, 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, 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-monthly", "shotgun-monthly-evergreen", 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, 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, new FutureAccountNotifications(new HashMap<UUID, List<SubscriptionNotification>>()), 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, 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) HashMap(java.util.HashMap) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) FutureAccountNotifications(org.killbill.billing.invoice.InvoiceDispatcher.FutureAccountNotifications) ArrayList(java.util.ArrayList) SubscriptionNotification(org.killbill.billing.invoice.InvoiceDispatcher.FutureAccountNotifications.SubscriptionNotification) 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 9 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 10 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)

Aggregations

InvoiceItemModelDao (org.killbill.billing.invoice.dao.InvoiceItemModelDao)10 InvoiceModelDao (org.killbill.billing.invoice.dao.InvoiceModelDao)10 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)6 BigDecimal (java.math.BigDecimal)5 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)5 Invoice (org.killbill.billing.invoice.api.Invoice)5 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)5 LocalDate (org.joda.time.LocalDate)4 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)4 Function (com.google.common.base.Function)3 ArrayList (java.util.ArrayList)3 UUID (java.util.UUID)3 AccountApiException (org.killbill.billing.account.api.AccountApiException)3 ParentInvoiceItem (org.killbill.billing.invoice.model.ParentInvoiceItem)3 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)3 Account (org.killbill.billing.account.api.Account)2 Currency (org.killbill.billing.catalog.api.Currency)2 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)2 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)2 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)2