Search in sources :

Example 1 with DefaultInvoicePayment

use of org.killbill.billing.invoice.model.DefaultInvoicePayment in project killbill by killbill.

the class TestInvoiceDao method testRefundedInvoiceWithInvoiceItemAdjustmentWithRepair.

@Test(groups = "slow")
public void testRefundedInvoiceWithInvoiceItemAdjustmentWithRepair() throws InvoiceApiException, EntityPersistenceException {
    final UUID accountId = account.getId();
    final UUID subscriptionId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate startDate = new LocalDate(2010, 1, 1);
    ((ClockMock) clock).setDay(startDate);
    final LocalDate recuringStartDate = clock.getUTCNow().plusDays(30).toLocalDate();
    final LocalDate recuringEndDate = recuringStartDate.plusMonths(1);
    final LocalDate targetDate = recuringStartDate.plusDays(1);
    // FIRST CREATE INITIAL INVOICE WITH ONE RECURRING ITEM
    final Invoice invoice = new DefaultInvoice(accountId, targetDate, targetDate, Currency.USD);
    final UUID invoiceId = invoice.getId();
    final InvoiceItem invoiceItem = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, "test-plan", "test-phase-rec", recuringStartDate, recuringEndDate, new BigDecimal("239.00"), new BigDecimal("239.00"), Currency.USD);
    invoice.addInvoiceItem(invoiceItem);
    invoiceUtil.createInvoice(invoice, context);
    ((ClockMock) clock).addDays(1);
    // SECOND CREATE THE PAYMENT
    final BigDecimal paymentAmount = new BigDecimal("239.00");
    final UUID paymentId = UUID.randomUUID();
    final DefaultInvoicePayment defaultInvoicePayment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, paymentId, invoiceId, clock.getUTCNow(), paymentAmount, Currency.USD, Currency.USD, "cookie", true);
    invoiceDao.notifyOfPaymentCompletion(new InvoicePaymentModelDao(defaultInvoicePayment), context);
    // AND THEN THIRD THE REFUND
    final Map<UUID, BigDecimal> invoiceItemMap = new HashMap<UUID, BigDecimal>();
    invoiceItemMap.put(invoiceItem.getId(), new BigDecimal("239.00"));
    invoiceDao.createRefund(paymentId, paymentAmount, true, invoiceItemMap, UUID.randomUUID().toString(), context);
    final InvoiceModelDao savedInvoice = invoiceDao.getById(invoiceId, context);
    assertNotNull(savedInvoice);
    assertEquals(savedInvoice.getInvoiceItems().size(), 2);
    final List<Invoice> invoices = new ArrayList<Invoice>();
    invoices.add(new DefaultInvoice(savedInvoice));
    // NOW COMPUTE A DIFFERENT ITEM TO TRIGGER REPAIR
    final BillingEventSet events = new MockBillingEventSet();
    final SubscriptionBase subscription = getZombieSubscription(subscriptionId);
    final Plan plan = Mockito.mock(Plan.class);
    Mockito.when(plan.getName()).thenReturn("plan");
    final PlanPhase phase1 = Mockito.mock(PlanPhase.class);
    Mockito.when(phase1.getName()).thenReturn("plan-phase1");
    final BillingEvent event1 = invoiceUtil.createMockBillingEvent(null, subscription, recuringStartDate.toDateTimeAtStartOfDay(), plan, phase1, null, TEN, Currency.USD, BillingPeriod.MONTHLY, 31, BillingMode.IN_ADVANCE, "new-event", 1L, SubscriptionBaseTransitionType.CREATE);
    events.add(event1);
    final InvoiceWithMetadata newInvoiceWithMetadata = generator.generateInvoice(account, events, invoices, targetDate, Currency.USD, context);
    final Invoice newInvoice = newInvoiceWithMetadata.getInvoice();
    invoiceUtil.createInvoice(newInvoice, context);
    // VERIFY THAT WE STILL HAVE ONLY 2 ITEMS, MEANING THERE WERE NO REPAIR AND NO CBA GENERATED
    final Invoice firstInvoice = new DefaultInvoice(invoiceDao.getById(invoiceId, context));
    assertNotNull(firstInvoice);
    assertEquals(firstInvoice.getInvoiceItems().size(), 2);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ClockMock(org.killbill.clock.ClockMock) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) InvoiceWithMetadata(org.killbill.billing.invoice.generator.InvoiceWithMetadata) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) BillingEvent(org.killbill.billing.junction.BillingEvent) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Test(org.testng.annotations.Test)

Example 2 with DefaultInvoicePayment

use of org.killbill.billing.invoice.model.DefaultInvoicePayment in project killbill by killbill.

the class TestInvoiceDao method testRefundWithRepairAndInvoiceItemAdjustmentInternal.

private void testRefundWithRepairAndInvoiceItemAdjustmentInternal(final BigDecimal refundAmount) throws InvoiceApiException, EntityPersistenceException {
    final UUID accountId = account.getId();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate targetDate1 = new LocalDate(2011, 10, 6);
    final Invoice invoice = new DefaultInvoice(accountId, clock.getUTCToday(), targetDate1, Currency.USD);
    invoiceUtil.createInvoice(invoice, context);
    final LocalDate startDate = new LocalDate(2011, 3, 1);
    final LocalDate endDate = startDate.plusMonths(1);
    final BigDecimal amount = new BigDecimal("20.0");
    // Recurring item
    final RecurringInvoiceItem item2 = new RecurringInvoiceItem(invoice.getId(), accountId, bundleId, UUID.randomUUID(), "test plan", "test phase B", startDate, endDate, amount, amount, Currency.USD);
    invoiceUtil.createInvoiceItem(item2, context);
    BigDecimal balancePriorRefund = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balancePriorRefund.compareTo(new BigDecimal("20.00")), 0);
    // Pay the whole thing
    final UUID paymentId = UUID.randomUUID();
    final BigDecimal payment1 = amount;
    final InvoicePayment payment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, paymentId, invoice.getId(), new DateTime(), payment1, Currency.USD, Currency.USD, null, true);
    invoiceUtil.createPayment(payment, context);
    balancePriorRefund = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balancePriorRefund.compareTo(new BigDecimal("0.00")), 0);
    // Repair the item (And add CBA item that should be generated)
    final InvoiceItem repairItem = new RepairAdjInvoiceItem(invoice.getId(), accountId, startDate, endDate, amount.negate(), Currency.USD, item2.getId());
    invoiceUtil.createInvoiceItem(repairItem, context);
    final InvoiceItem cbaItem = new CreditBalanceAdjInvoiceItem(invoice.getId(), accountId, startDate, amount, Currency.USD);
    invoiceUtil.createInvoiceItem(cbaItem, context);
    final Map<UUID, BigDecimal> itemAdjustment = new HashMap<UUID, BigDecimal>();
    // PAss a null value to let invoice calculate the amount to adjust
    itemAdjustment.put(item2.getId(), null);
    invoiceDao.createRefund(paymentId, refundAmount, true, itemAdjustment, UUID.randomUUID().toString(), context);
    balancePriorRefund = invoiceDao.getAccountBalance(accountId, context);
    final boolean partialRefund = refundAmount.compareTo(amount) < 0;
    final BigDecimal cba = invoiceDao.getAccountCBA(accountId, context);
    final InvoiceModelDao savedInvoice = invoiceDao.getById(invoice.getId(), context);
    final BigDecimal expectedCba = balancePriorRefund.compareTo(BigDecimal.ZERO) < 0 ? balancePriorRefund.negate() : BigDecimal.ZERO;
    assertEquals(cba.compareTo(expectedCba), 0);
    // Let's re-calculate them from invoice
    final BigDecimal balanceAfterRefund = invoiceDao.getAccountBalance(accountId, context);
    final BigDecimal cbaAfterRefund = invoiceDao.getAccountCBA(accountId, context);
    if (partialRefund) {
        // IB = 20 (rec) - 20 (repair) + 20 (cba) - (20 -7) = 7;  AB = IB - CBA = 7 - 20 = -13
        assertEquals(balancePriorRefund.compareTo(new BigDecimal("-13.0")), 0);
        assertEquals(savedInvoice.getInvoiceItems().size(), 4);
        assertEquals(balanceAfterRefund.compareTo(new BigDecimal("-13.0")), 0);
        assertEquals(cbaAfterRefund.compareTo(expectedCba), 0);
    } else {
        assertEquals(balancePriorRefund.compareTo(new BigDecimal("0.0")), 0);
        assertEquals(savedInvoice.getInvoiceItems().size(), 4);
        assertEquals(balanceAfterRefund.compareTo(BigDecimal.ZERO), 0);
        assertEquals(cbaAfterRefund.compareTo(expectedCba), 0);
    }
}
Also used : InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) HashMap(java.util.HashMap) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem)

Example 3 with DefaultInvoicePayment

use of org.killbill.billing.invoice.model.DefaultInvoicePayment in project killbill by killbill.

the class TestInvoiceDao method testWithFailedPaymentAttempt.

@Test(groups = "slow")
public void testWithFailedPaymentAttempt() throws Exception {
    final UUID accountId = account.getId();
    final Invoice invoice = new DefaultInvoice(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
    invoiceUtil.createInvoice(invoice, context);
    final UUID bundleId = UUID.randomUUID();
    final UUID subscriptionId = UUID.randomUUID();
    final RecurringInvoiceItem item1 = new RecurringInvoiceItem(invoice.getId(), accountId, bundleId, subscriptionId, "test plan", "test ZOO", clock.getUTCNow().plusMonths(-1).toLocalDate(), clock.getUTCNow().toLocalDate(), BigDecimal.TEN, BigDecimal.TEN, Currency.USD);
    invoiceUtil.createInvoiceItem(item1, context);
    final InvoiceModelDao retrievedInvoice = invoiceDao.getById(invoice.getId(), context);
    assertEquals(retrievedInvoice.getInvoicePayments().size(), 0);
    final UUID paymentId = UUID.randomUUID();
    final DefaultInvoicePayment defaultInvoicePayment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, paymentId, invoice.getId(), clock.getUTCNow().plusDays(12), BigDecimal.TEN, Currency.USD, Currency.USD, "cookie", false);
    invoiceDao.notifyOfPaymentCompletion(new InvoicePaymentModelDao(defaultInvoicePayment), context);
    final InvoiceModelDao retrievedInvoice1 = invoiceDao.getById(invoice.getId(), context);
    assertEquals(retrievedInvoice1.getInvoicePayments().size(), 1);
    assertEquals(retrievedInvoice1.getInvoicePayments().get(0).getSuccess(), Boolean.FALSE);
    final DefaultInvoicePayment defaultInvoicePayment2 = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, paymentId, invoice.getId(), clock.getUTCNow().plusDays(12), BigDecimal.TEN, Currency.USD, Currency.USD, "cookie", true);
    invoiceDao.notifyOfPaymentCompletion(new InvoicePaymentModelDao(defaultInvoicePayment2), context);
    final InvoiceModelDao retrievedInvoice2 = invoiceDao.getById(invoice.getId(), context);
    assertEquals(retrievedInvoice2.getInvoicePayments().size(), 1);
    assertEquals(retrievedInvoice2.getInvoicePayments().get(0).getSuccess(), Boolean.TRUE);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) Test(org.testng.annotations.Test)

Example 4 with DefaultInvoicePayment

use of org.killbill.billing.invoice.model.DefaultInvoicePayment in project killbill by killbill.

the class TestInvoiceDao method testAccountBalanceWithRefundAndCBAInternal.

private void testAccountBalanceWithRefundAndCBAInternal(final boolean withAdjustment, final BigDecimal refundAmount, final BigDecimal expectedFinalBalance) throws InvoiceApiException, EntityPersistenceException {
    final UUID accountId = account.getId();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate targetDate1 = new LocalDate(2011, 10, 6);
    final Invoice invoice1 = new DefaultInvoice(accountId, clock.getUTCToday(), targetDate1, Currency.USD);
    invoiceUtil.createInvoice(invoice1, context);
    final LocalDate startDate = new LocalDate(2011, 3, 1);
    final LocalDate endDate = startDate.plusMonths(1);
    final BigDecimal amount1 = new BigDecimal("5.0");
    final BigDecimal rate1 = new BigDecimal("20.0");
    final BigDecimal rate2 = new BigDecimal("10.0");
    // Fixed Item
    final FixedPriceInvoiceItem item1 = new FixedPriceInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test plan", "test phase A", startDate, amount1, Currency.USD);
    invoiceUtil.createInvoiceItem(item1, context);
    BigDecimal balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(new BigDecimal("5.00")), 0);
    // Recurring item
    final RecurringInvoiceItem item2 = new RecurringInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test plan", "test phase B", startDate, endDate, rate1, rate1, Currency.USD);
    invoiceUtil.createInvoiceItem(item2, context);
    balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(new BigDecimal("25.00")), 0);
    // Pay the whole thing
    final UUID paymentId = UUID.randomUUID();
    final BigDecimal payment1 = amount1.add(rate1);
    final InvoicePayment payment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, paymentId, invoice1.getId(), new DateTime(), payment1, Currency.USD, Currency.USD, null, true);
    invoiceUtil.createPayment(payment, context);
    balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(new BigDecimal("0.00")), 0);
    // Repair previous item with rate 2
    final RepairAdjInvoiceItem item2Repair = new RepairAdjInvoiceItem(invoice1.getId(), accountId, startDate, endDate, rate1.negate(), Currency.USD, item2.getId());
    final RecurringInvoiceItem item2Replace = new RecurringInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test plan", "test phase B", startDate, endDate, rate2, rate2, Currency.USD);
    invoiceUtil.createInvoiceItem(item2Repair, context);
    invoiceUtil.createInvoiceItem(item2Replace, context);
    balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(new BigDecimal("-10.00")), 0);
    // CBA
    final CreditBalanceAdjInvoiceItem cbaItem = new CreditBalanceAdjInvoiceItem(invoice1.getId(), accountId, new LocalDate(), balance.negate(), Currency.USD);
    invoiceUtil.createInvoiceItem(cbaItem, context);
    balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(new BigDecimal("-10.00")), 0);
    BigDecimal cba = invoiceDao.getAccountCBA(accountId, context);
    assertEquals(cba.compareTo(new BigDecimal("10.00")), 0);
    // PARTIAL REFUND on the payment
    final Map<UUID, BigDecimal> invoiceItemIdsWithAmounts = new HashMap<UUID, BigDecimal>();
    if (withAdjustment) {
        invoiceItemIdsWithAmounts.put(item2Replace.getId(), refundAmount);
    }
    invoiceDao.createRefund(paymentId, refundAmount, withAdjustment, invoiceItemIdsWithAmounts, UUID.randomUUID().toString(), context);
    balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(expectedFinalBalance), 0);
    cba = invoiceDao.getAccountCBA(accountId, context);
    final BigDecimal expectedCba = balance.compareTo(BigDecimal.ZERO) < 0 ? balance.negate() : BigDecimal.ZERO;
    assertEquals(cba.compareTo(expectedCba), 0);
}
Also used : InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) HashMap(java.util.HashMap) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem)

Example 5 with DefaultInvoicePayment

use of org.killbill.billing.invoice.model.DefaultInvoicePayment in project killbill by killbill.

the class TestInvoiceDao method testRefundWithCBAPartiallyConsumed.

@Test(groups = "slow")
public void testRefundWithCBAPartiallyConsumed() throws Exception {
    final UUID accountId = account.getId();
    // Create invoice 1
    // Scenario: single item with payment
    // * $10 item
    // Then, a repair occur:
    // * $-10 repair
    // * $10 generated CBA due to the repair (assume previous payment)
    final Invoice invoice1 = new DefaultInvoice(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
    final InvoiceItem fixedItem1 = new FixedPriceInvoiceItem(invoice1.getId(), invoice1.getAccountId(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), clock.getUTCToday(), BigDecimal.TEN, Currency.USD);
    final RepairAdjInvoiceItem repairAdjInvoiceItem = new RepairAdjInvoiceItem(fixedItem1.getInvoiceId(), fixedItem1.getAccountId(), fixedItem1.getStartDate(), fixedItem1.getEndDate(), fixedItem1.getAmount().negate(), fixedItem1.getCurrency(), fixedItem1.getId());
    final CreditBalanceAdjInvoiceItem creditBalanceAdjInvoiceItem1 = new CreditBalanceAdjInvoiceItem(fixedItem1.getInvoiceId(), fixedItem1.getAccountId(), fixedItem1.getStartDate(), fixedItem1.getAmount(), fixedItem1.getCurrency());
    invoiceUtil.createInvoice(invoice1, context);
    invoiceUtil.createInvoiceItem(fixedItem1, context);
    invoiceUtil.createInvoiceItem(repairAdjInvoiceItem, context);
    invoiceUtil.createInvoiceItem(creditBalanceAdjInvoiceItem1, context);
    final UUID paymentId = UUID.randomUUID();
    final DefaultInvoicePayment defaultInvoicePayment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, paymentId, invoice1.getId(), clock.getUTCNow().plusDays(12), new BigDecimal("10.0"), Currency.USD, Currency.USD, "cookie", true);
    invoiceDao.notifyOfPaymentCompletion(new InvoicePaymentModelDao(defaultInvoicePayment), context);
    // Create invoice 2
    // Scenario: single item
    // * $5 item
    // * $-5 CBA used
    final DefaultInvoice invoice2 = new DefaultInvoice(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
    final InvoiceItem fixedItem2 = new FixedPriceInvoiceItem(invoice2.getId(), invoice1.getAccountId(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), clock.getUTCToday(), new BigDecimal("5"), Currency.USD);
    final CreditBalanceAdjInvoiceItem creditBalanceAdjInvoiceItem2 = new CreditBalanceAdjInvoiceItem(fixedItem2.getInvoiceId(), fixedItem2.getAccountId(), fixedItem2.getStartDate(), fixedItem2.getAmount().negate(), fixedItem2.getCurrency());
    invoiceUtil.createInvoice(invoice2, context);
    invoiceUtil.createInvoiceItem(fixedItem2, context);
    invoiceUtil.createInvoiceItem(creditBalanceAdjInvoiceItem2, context);
    // Verify scenario - half of the CBA should have been used
    Assert.assertEquals(invoiceDao.getAccountCBA(accountId, context).doubleValue(), 5.00);
    invoiceUtil.verifyInvoice(invoice1.getId(), 0.00, 10.00, context);
    invoiceUtil.verifyInvoice(invoice2.getId(), 0.00, -5.00, context);
    // Refund Payment before we can deleted CBA
    invoiceDao.createRefund(paymentId, new BigDecimal("10.0"), false, ImmutableMap.<UUID, BigDecimal>of(), UUID.randomUUID().toString(), context);
    // Verify all three invoices were affected
    Assert.assertEquals(invoiceDao.getAccountCBA(accountId, context).doubleValue(), 0.00);
    invoiceUtil.verifyInvoice(invoice1.getId(), 5.00, 5.00, context);
    invoiceUtil.verifyInvoice(invoice2.getId(), 0.00, -5.00, context);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) BigDecimal(java.math.BigDecimal) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) Test(org.testng.annotations.Test)

Aggregations

DefaultInvoicePayment (org.killbill.billing.invoice.model.DefaultInvoicePayment)17 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)14 Invoice (org.killbill.billing.invoice.api.Invoice)13 UUID (java.util.UUID)12 BigDecimal (java.math.BigDecimal)11 LocalDate (org.joda.time.LocalDate)10 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)10 Test (org.testng.annotations.Test)10 InvoicePayment (org.killbill.billing.invoice.api.InvoicePayment)8 CreditBalanceAdjInvoiceItem (org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem)8 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)8 RepairAdjInvoiceItem (org.killbill.billing.invoice.model.RepairAdjInvoiceItem)8 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)7 CreditAdjInvoiceItem (org.killbill.billing.invoice.model.CreditAdjInvoiceItem)7 DateTime (org.joda.time.DateTime)6 ExternalChargeInvoiceItem (org.killbill.billing.invoice.model.ExternalChargeInvoiceItem)5 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)5 ParentInvoiceItem (org.killbill.billing.invoice.model.ParentInvoiceItem)5 InvoicePaymentModelDao (org.killbill.billing.invoice.dao.InvoicePaymentModelDao)4 HashMap (java.util.HashMap)3