Search in sources :

Example 71 with InvoiceItem

use of org.killbill.billing.invoice.api.InvoiceItem in project killbill by killbill.

the class TestDefaultInvoiceUserApi method testOriginalAmountCharged.

@Test(groups = "slow")
public void testOriginalAmountCharged() throws Exception {
    final Invoice initialInvoice = invoiceUserApi.getInvoice(invoiceId, callContext);
    final BigDecimal originalAmountCharged = initialInvoice.getOriginalChargedAmount();
    final BigDecimal amountCharged = initialInvoice.getChargedAmount();
    Assert.assertEquals(originalAmountCharged.compareTo(amountCharged), 0);
    ((ClockMock) clock).addDays(1);
    // Sleep at least one sec to make sure created_date for the external charge is different than the created date for the invoice itself
    CallContext newCallContextLater = new DefaultCallContext(callContext.getTenantId(), callContext.getUserName(), callContext.getCallOrigin(), callContext.getUserType(), callContext.getUserToken(), clock);
    // Post an external charge
    final BigDecimal externalChargeAmount = BigDecimal.TEN;
    final InvoiceItem externalCharge = new ExternalChargeInvoiceItem(invoiceId, accountId, null, UUID.randomUUID().toString(), clock.getUTCToday(), externalChargeAmount, accountCurrency);
    final InvoiceItem externalChargeInvoiceItem = invoiceUserApi.insertExternalCharges(accountId, clock.getUTCToday(), ImmutableList.<InvoiceItem>of(externalCharge), true, newCallContextLater).get(0);
    final Invoice newInvoice = invoiceUserApi.getInvoice(invoiceId, callContext);
    final BigDecimal newOriginalAmountCharged = newInvoice.getOriginalChargedAmount();
    final BigDecimal newAmountCharged = newInvoice.getChargedAmount();
    final BigDecimal expectedChargedAmount = newInvoice.getOriginalChargedAmount().add(externalChargeInvoiceItem.getAmount());
    Assert.assertEquals(originalAmountCharged.compareTo(newOriginalAmountCharged), 0);
    Assert.assertEquals(newAmountCharged.compareTo(expectedChargedAmount), 0);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) DefaultCallContext(org.killbill.billing.callcontext.DefaultCallContext) ClockMock(org.killbill.clock.ClockMock) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CallContext(org.killbill.billing.util.callcontext.CallContext) DefaultCallContext(org.killbill.billing.callcontext.DefaultCallContext) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 72 with InvoiceItem

use of org.killbill.billing.invoice.api.InvoiceItem in project killbill by killbill.

the class TestDefaultInvoiceUserApi method testAdjustFullInvoiceItem.

@Test(groups = "slow")
public void testAdjustFullInvoiceItem() throws Exception {
    final InvoiceItem invoiceItem = invoiceUserApi.getInvoice(invoiceId, callContext).getInvoiceItems().get(0);
    // Verify we picked a non zero item
    Assert.assertEquals(invoiceItem.getAmount().compareTo(BigDecimal.ZERO), 1);
    // Verify the initial invoice balance
    final BigDecimal invoiceBalance = invoiceUserApi.getInvoice(invoiceId, callContext).getBalance();
    Assert.assertEquals(invoiceBalance.compareTo(BigDecimal.ZERO), 1);
    // Verify the initial account balance
    final BigDecimal accountBalance = invoiceUserApi.getAccountBalance(accountId, callContext);
    Assert.assertEquals(accountBalance, invoiceBalance);
    // Adjust the invoice for the full amount
    final InvoiceItem adjInvoiceItem = invoiceUserApi.insertInvoiceItemAdjustment(accountId, invoiceId, invoiceItem.getId(), clock.getUTCToday(), null, callContext);
    Assert.assertEquals(adjInvoiceItem.getInvoiceId(), invoiceId);
    Assert.assertEquals(adjInvoiceItem.getInvoiceItemType(), InvoiceItemType.ITEM_ADJ);
    Assert.assertEquals(adjInvoiceItem.getAccountId(), accountId);
    Assert.assertEquals(adjInvoiceItem.getAmount(), invoiceItem.getAmount().negate());
    Assert.assertEquals(adjInvoiceItem.getCurrency(), accountCurrency);
    Assert.assertEquals(adjInvoiceItem.getLinkedItemId(), invoiceItem.getId());
    // Verify the adjusted invoice balance
    final BigDecimal adjustedInvoiceBalance = invoiceUserApi.getInvoice(invoiceId, callContext).getBalance();
    verifyAdjustedInvoiceBalance(invoiceBalance, invoiceItem.getAmount(), invoiceItem.getCurrency(), adjustedInvoiceBalance);
    // Verify the adjusted account balance
    final BigDecimal adjustedAccountBalance = invoiceUserApi.getAccountBalance(accountId, callContext);
    Assert.assertEquals(adjustedAccountBalance, adjustedInvoiceBalance);
}
Also used : InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 73 with InvoiceItem

use of org.killbill.billing.invoice.api.InvoiceItem in project killbill by killbill.

the class TestHtmlInvoiceGenerator method createInvoice.

private Invoice createInvoice() {
    final LocalDate startDate = new LocalDate(new DateTime().minusMonths(1), DateTimeZone.UTC);
    final LocalDate endDate = new LocalDate(DateTimeZone.UTC);
    final BigDecimal price1 = new BigDecimal("29.95");
    final BigDecimal price2 = new BigDecimal("59.95");
    final Invoice dummyInvoice = Mockito.mock(Invoice.class);
    Mockito.when(dummyInvoice.getInvoiceDate()).thenReturn(startDate);
    Mockito.when(dummyInvoice.getInvoiceNumber()).thenReturn(42);
    Mockito.when(dummyInvoice.getCurrency()).thenReturn(Currency.USD);
    Mockito.when(dummyInvoice.getChargedAmount()).thenReturn(price1.add(price2));
    Mockito.when(dummyInvoice.getPaidAmount()).thenReturn(BigDecimal.ZERO);
    Mockito.when(dummyInvoice.getBalance()).thenReturn(price1.add(price2));
    final List<InvoiceItem> items = new ArrayList<InvoiceItem>();
    items.add(createInvoiceItem(price1, "Domain 1", startDate, endDate, "ning-plus"));
    items.add(createInvoiceItem(price2, "Domain 2", startDate, endDate, "ning-pro"));
    Mockito.when(dummyInvoice.getInvoiceItems()).thenReturn(items);
    return dummyInvoice;
}
Also used : HtmlInvoice(org.killbill.billing.invoice.template.HtmlInvoice) Invoice(org.killbill.billing.invoice.api.Invoice) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal)

Example 74 with InvoiceItem

use of org.killbill.billing.invoice.api.InvoiceItem in project killbill by killbill.

the class TestHtmlInvoiceGenerator method createInvoiceItem.

private InvoiceItem createInvoiceItem(final BigDecimal amount, final String networkName, final LocalDate startDate, final LocalDate endDate, final String planName) {
    final InvoiceItem item = Mockito.mock(InvoiceItem.class);
    Mockito.when(item.getAmount()).thenReturn(amount);
    Mockito.when(item.getStartDate()).thenReturn(startDate);
    Mockito.when(item.getEndDate()).thenReturn(endDate);
    Mockito.when(item.getPlanName()).thenReturn(planName);
    Mockito.when(item.getDescription()).thenReturn(networkName);
    return item;
}
Also used : InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem)

Example 75 with InvoiceItem

use of org.killbill.billing.invoice.api.InvoiceItem in project killbill by killbill.

the class TestInvoiceDispatcher method testWithOverdueEvents.

@Test(groups = "slow")
public void testWithOverdueEvents() throws Exception {
    final BillingEventSet events = new MockBillingEventSet();
    // Initial trial
    final MockPlan bicycleTrialEvergreen1USD = MockPlan.createBicycleTrialEvergreen1USD();
    events.add(invoiceUtil.createMockBillingEvent(account, subscription, new DateTime("2012-05-01T00:03:42.000Z"), bicycleTrialEvergreen1USD, new MockPlanPhase(bicycleTrialEvergreen1USD, PhaseType.TRIAL), BigDecimal.ZERO, null, account.getCurrency(), BillingPeriod.NO_BILLING_PERIOD, 31, BillingMode.IN_ADVANCE, "CREATE", 1L, SubscriptionBaseTransitionType.CREATE));
    // Phase change to evergreen
    events.add(invoiceUtil.createMockBillingEvent(account, subscription, new DateTime("2012-05-31T00:03:42.000Z"), bicycleTrialEvergreen1USD, new MockPlanPhase(bicycleTrialEvergreen1USD, PhaseType.EVERGREEN), null, new BigDecimal("249.95"), account.getCurrency(), BillingPeriod.MONTHLY, 31, BillingMode.IN_ADVANCE, "PHASE", 2L, SubscriptionBaseTransitionType.PHASE));
    // Overdue period
    events.add(invoiceUtil.createMockBillingEvent(account, subscription, new DateTime("2012-07-15T00:00:00.000Z"), bicycleTrialEvergreen1USD, new MockPlanPhase(bicycleTrialEvergreen1USD, PhaseType.EVERGREEN), null, null, account.getCurrency(), BillingPeriod.NO_BILLING_PERIOD, 31, BillingMode.IN_ADVANCE, "", 0L, SubscriptionBaseTransitionType.START_BILLING_DISABLED));
    events.add(invoiceUtil.createMockBillingEvent(account, subscription, new DateTime("2012-07-25T00:00:00.000Z"), bicycleTrialEvergreen1USD, new MockPlanPhase(bicycleTrialEvergreen1USD, PhaseType.EVERGREEN), null, new BigDecimal("249.95"), account.getCurrency(), BillingPeriod.MONTHLY, 31, BillingMode.IN_ADVANCE, "", 1L, SubscriptionBaseTransitionType.END_BILLING_DISABLED));
    // Upgrade after the overdue period
    final MockPlan jetTrialEvergreen1000USD = MockPlan.createJetTrialEvergreen1000USD();
    events.add(invoiceUtil.createMockBillingEvent(account, subscription, new DateTime("2012-07-25T00:04:00.000Z"), jetTrialEvergreen1000USD, new MockPlanPhase(jetTrialEvergreen1000USD, PhaseType.EVERGREEN), null, new BigDecimal("1000"), account.getCurrency(), BillingPeriod.MONTHLY, 31, BillingMode.IN_ADVANCE, "CHANGE", 3L, SubscriptionBaseTransitionType.CHANGE));
    Mockito.when(billingApi.getBillingEventsForAccountAndUpdateAccountBCD(Mockito.<UUID>any(), Mockito.<DryRunArguments>any(), Mockito.<InternalCallContext>any())).thenReturn(events);
    final InvoiceNotifier invoiceNotifier = new NullInvoiceNotifier();
    final InvoiceDispatcher dispatcher = new InvoiceDispatcher(generator, accountApi, billingApi, subscriptionApi, invoiceDao, internalCallContextFactory, invoiceNotifier, invoicePluginDispatcher, locker, busService.getBus(), null, invoiceConfig, clock, parkedAccountsManager);
    final Invoice invoice = dispatcher.processAccountFromNotificationOrBusEvent(account.getId(), new LocalDate("2012-07-30"), null, context);
    Assert.assertNotNull(invoice);
    final List<InvoiceItem> invoiceItems = invoice.getInvoiceItems();
    Assert.assertEquals(invoiceItems.size(), 4);
    Assert.assertEquals(invoiceItems.get(0).getInvoiceItemType(), InvoiceItemType.FIXED);
    Assert.assertEquals(invoiceItems.get(0).getStartDate(), new LocalDate("2012-05-01"));
    Assert.assertNull(invoiceItems.get(0).getEndDate());
    Assert.assertEquals(invoiceItems.get(0).getAmount().compareTo(BigDecimal.ZERO), 0);
    Assert.assertNull(invoiceItems.get(0).getRate());
    Assert.assertEquals(invoiceItems.get(1).getInvoiceItemType(), InvoiceItemType.RECURRING);
    Assert.assertEquals(invoiceItems.get(1).getStartDate(), new LocalDate("2012-05-31"));
    Assert.assertEquals(invoiceItems.get(1).getEndDate(), new LocalDate("2012-06-30"));
    Assert.assertEquals(invoiceItems.get(1).getAmount(), new BigDecimal("249.95"));
    Assert.assertEquals(invoiceItems.get(1).getRate(), new BigDecimal("249.95"));
    Assert.assertEquals(invoiceItems.get(2).getInvoiceItemType(), InvoiceItemType.RECURRING);
    Assert.assertEquals(invoiceItems.get(2).getStartDate(), new LocalDate("2012-06-30"));
    Assert.assertEquals(invoiceItems.get(2).getEndDate(), new LocalDate("2012-07-15"));
    Assert.assertEquals(invoiceItems.get(2).getAmount(), new BigDecimal("124.98"));
    Assert.assertEquals(invoiceItems.get(2).getRate(), new BigDecimal("249.95"));
    Assert.assertEquals(invoiceItems.get(3).getInvoiceItemType(), InvoiceItemType.RECURRING);
    Assert.assertEquals(invoiceItems.get(3).getStartDate(), new LocalDate("2012-07-25"));
    Assert.assertEquals(invoiceItems.get(3).getEndDate(), new LocalDate("2012-07-31"));
    Assert.assertEquals(invoiceItems.get(3).getAmount(), new BigDecimal("193.55"));
    Assert.assertEquals(invoiceItems.get(3).getRate(), new BigDecimal("1000"));
    // Verify common fields
    for (final InvoiceItem item : invoiceItems) {
        Assert.assertEquals(item.getAccountId(), account.getId());
        Assert.assertEquals(item.getBundleId(), subscription.getBundleId());
        Assert.assertEquals(item.getCurrency(), account.getCurrency());
        Assert.assertEquals(item.getInvoiceId(), invoice.getId());
        Assert.assertNull(item.getLinkedItemId());
        Assert.assertEquals(item.getSubscriptionId(), subscription.getId());
    }
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) NullInvoiceNotifier(org.killbill.billing.invoice.notification.NullInvoiceNotifier) InvoiceNotifier(org.killbill.billing.invoice.api.InvoiceNotifier) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) NullInvoiceNotifier(org.killbill.billing.invoice.notification.NullInvoiceNotifier) MockPlan(org.killbill.billing.catalog.MockPlan) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) BillingEventSet(org.killbill.billing.junction.BillingEventSet) Test(org.testng.annotations.Test)

Aggregations

InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)168 LocalDate (org.joda.time.LocalDate)118 Test (org.testng.annotations.Test)109 BigDecimal (java.math.BigDecimal)103 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)97 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)92 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)79 RepairAdjInvoiceItem (org.killbill.billing.invoice.model.RepairAdjInvoiceItem)76 UUID (java.util.UUID)68 Invoice (org.killbill.billing.invoice.api.Invoice)57 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)36 ExternalChargeInvoiceItem (org.killbill.billing.invoice.model.ExternalChargeInvoiceItem)32 BillingEvent (org.killbill.billing.junction.BillingEvent)27 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)25 ArrayList (java.util.ArrayList)23 DateTime (org.joda.time.DateTime)23 MockPlan (org.killbill.billing.catalog.MockPlan)23 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)23 Plan (org.killbill.billing.catalog.api.Plan)22 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)21