use of org.killbill.billing.invoice.api.Invoice in project killbill by killbill.
the class TestInvoiceDao method testGetUnpaidInvoicesByAccountIdWithDraftInvoice.
@Test(groups = "slow")
public void testGetUnpaidInvoicesByAccountIdWithDraftInvoice() throws 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 rate1 = new BigDecimal("17.0");
final BigDecimal rate2 = new BigDecimal("42.0");
final RecurringInvoiceItem item1 = new RecurringInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test plan", "test phase A", startDate, endDate, rate1, rate1, Currency.USD);
invoiceUtil.createInvoiceItem(item1, context);
final RecurringInvoiceItem item2 = new RecurringInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test plan", "test phase B", startDate, endDate, rate2, rate2, Currency.USD);
invoiceUtil.createInvoiceItem(item2, context);
LocalDate upToDate;
Collection<InvoiceModelDao> invoices;
upToDate = new LocalDate(2011, 1, 1);
invoices = invoiceDao.getUnpaidInvoicesByAccountId(accountId, upToDate, context);
assertEquals(invoices.size(), 0);
upToDate = new LocalDate(2012, 1, 1);
invoices = invoiceDao.getUnpaidInvoicesByAccountId(accountId, upToDate, context);
assertEquals(invoices.size(), 1);
List<InvoiceModelDao> allInvoicesByAccount = invoiceDao.getInvoicesByAccount(new LocalDate(2011, 1, 1), context);
assertEquals(allInvoicesByAccount.size(), 1);
// insert DRAFT invoice
createCredit(accountId, new LocalDate(2011, 12, 31), BigDecimal.TEN, true);
allInvoicesByAccount = invoiceDao.getInvoicesByAccount(new LocalDate(2011, 1, 1), context);
assertEquals(allInvoicesByAccount.size(), 2);
assertEquals(allInvoicesByAccount.get(0).getStatus(), InvoiceStatus.COMMITTED);
assertEquals(allInvoicesByAccount.get(1).getStatus(), InvoiceStatus.DRAFT);
upToDate = new LocalDate(2012, 1, 1);
invoices = invoiceDao.getUnpaidInvoicesByAccountId(accountId, upToDate, context);
assertEquals(invoices.size(), 1);
}
use of org.killbill.billing.invoice.api.Invoice 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);
}
use of org.killbill.billing.invoice.api.Invoice in project killbill by killbill.
the class TestInvoiceDao method testGetInvoicesForAccountAfterDate.
@Test(groups = "slow")
public void testGetInvoicesForAccountAfterDate() throws EntityPersistenceException {
final UUID accountId = account.getId();
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 targetDate2 = new LocalDate(2011, 12, 6);
final Invoice invoice2 = new DefaultInvoice(accountId, clock.getUTCToday(), targetDate2, Currency.USD);
invoiceUtil.createInvoice(invoice2, context);
List<InvoiceModelDao> invoices;
invoices = invoiceDao.getInvoicesByAccount(new LocalDate(2011, 1, 1), context);
assertEquals(invoices.size(), 2);
invoices = invoiceDao.getInvoicesByAccount(new LocalDate(2011, 10, 6), context);
assertEquals(invoices.size(), 2);
invoices = invoiceDao.getInvoicesByAccount(new LocalDate(2011, 10, 11), context);
assertEquals(invoices.size(), 1);
invoices = invoiceDao.getInvoicesByAccount(new LocalDate(2011, 12, 6), context);
assertEquals(invoices.size(), 1);
invoices = invoiceDao.getInvoicesByAccount(new LocalDate(2012, 1, 1), context);
assertEquals(invoices.size(), 0);
}
use of org.killbill.billing.invoice.api.Invoice 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);
}
use of org.killbill.billing.invoice.api.Invoice in project killbill by killbill.
the class TestInvoiceDao method testDeleteCBANotConsumed.
@Test(groups = "slow")
public void testDeleteCBANotConsumed() 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);
// Verify scenario - no CBA should have been used
Assert.assertEquals(invoiceDao.getAccountCBA(accountId, context).doubleValue(), 10.00);
invoiceUtil.verifyInvoice(invoice1.getId(), 10.00, 10.00, context);
// Delete the CBA on invoice 1
invoiceDao.deleteCBA(accountId, invoice1.getId(), creditBalanceAdjInvoiceItem1.getId(), context);
// Verify the result
Assert.assertEquals(invoiceDao.getAccountCBA(accountId, context).doubleValue(), 0.00);
invoiceUtil.verifyInvoice(invoice1.getId(), 0.00, 0.00, context);
}
Aggregations