use of org.killbill.billing.invoice.model.DefaultInvoice in project killbill by killbill.
the class TestInvoiceDao method testAccountBalanceWithRefundInternal.
private void testAccountBalanceWithRefundInternal(final boolean withAdjustment) 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 rate1 = new BigDecimal("20.0");
final BigDecimal refund1 = new BigDecimal("7.00");
// Recurring item
final RecurringInvoiceItem item2 = new RecurringInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test product", "test plan", "test phase B", null, startDate, endDate, rate1, rate1, Currency.USD);
invoiceUtil.createInvoiceItem(item2, context);
BigDecimal balance = invoiceDao.getAccountBalance(accountId, context);
assertEquals(balance.compareTo(new BigDecimal("20.00")), 0);
// Pay the whole thing
final UUID paymentId = UUID.randomUUID();
final BigDecimal payment1 = 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);
invoiceDao.createRefund(paymentId, UUID.randomUUID(), refund1, withAdjustment, ImmutableMap.<UUID, BigDecimal>of(), UUID.randomUUID().toString(), true, context);
balance = invoiceDao.getAccountBalance(accountId, context);
if (withAdjustment) {
assertEquals(balance.compareTo(BigDecimal.ZERO), 0);
} else {
assertEquals(balance.compareTo(new BigDecimal("7.00")), 0);
}
}
use of org.killbill.billing.invoice.model.DefaultInvoice in project killbill by killbill.
the class TestInvoiceDao method testGetUnpaidInvoicesByAccountId.
@Test(groups = "slow")
public void testGetUnpaidInvoicesByAccountId() 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 product", "test plan", "test phase A", null, startDate, endDate, rate1, rate1, Currency.USD);
invoiceUtil.createInvoiceItem(item1, context);
final RecurringInvoiceItem item2 = new RecurringInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test product", "test plan", "test phase B", null, 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, null, upToDate, context);
assertEquals(invoices.size(), 0);
upToDate = new LocalDate(2012, 1, 1);
invoices = invoiceDao.getUnpaidInvoicesByAccountId(accountId, null, upToDate, context);
assertEquals(invoices.size(), 1);
final LocalDate targetDate2 = new LocalDate(2011, 7, 1);
final Invoice invoice2 = new DefaultInvoice(accountId, clock.getUTCToday(), targetDate2, Currency.USD);
invoiceUtil.createInvoice(invoice2, context);
final LocalDate startDate2 = new LocalDate(2011, 6, 1);
final LocalDate endDate2 = startDate2.plusMonths(3);
final BigDecimal rate3 = new BigDecimal("21.0");
final RecurringInvoiceItem item3 = new RecurringInvoiceItem(invoice2.getId(), accountId, bundleId, UUID.randomUUID(), "test product", "test plan", "test phase C", null, startDate2, endDate2, rate3, rate3, Currency.USD);
invoiceUtil.createInvoiceItem(item3, context);
upToDate = new LocalDate(2011, 1, 1);
invoices = invoiceDao.getUnpaidInvoicesByAccountId(accountId, null, upToDate, context);
assertEquals(invoices.size(), 0);
upToDate = new LocalDate(2012, 1, 1);
invoices = invoiceDao.getUnpaidInvoicesByAccountId(accountId, null, upToDate, context);
assertEquals(invoices.size(), 2);
}
use of org.killbill.billing.invoice.model.DefaultInvoice 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(false, new LocalDate(2011, 1, 1), null, context);
assertEquals(invoices.size(), 2);
invoices = invoiceDao.getInvoicesByAccount(false, new LocalDate(2011, 10, 6), null, context);
assertEquals(invoices.size(), 2);
invoices = invoiceDao.getInvoicesByAccount(false, new LocalDate(2011, 10, 11), null, context);
assertEquals(invoices.size(), 1);
invoices = invoiceDao.getInvoicesByAccount(false, new LocalDate(2011, 12, 6), null, context);
assertEquals(invoices.size(), 1);
invoices = invoiceDao.getInvoicesByAccount(false, new LocalDate(2012, 1, 1), null, context);
assertEquals(invoices.size(), 0);
}
use of org.killbill.billing.invoice.model.DefaultInvoice 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, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), null, 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), UUID.randomUUID(), 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, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), null, 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, UUID.randomUUID(), new BigDecimal("10.0"), false, ImmutableMap.<UUID, BigDecimal>of(), UUID.randomUUID().toString(), true, 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);
}
use of org.killbill.billing.invoice.model.DefaultInvoice in project killbill by killbill.
the class TestInvoiceDao method testAccountBalanceWithCredit.
@Test(groups = "slow")
public void testAccountBalanceWithCredit() 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 RecurringInvoiceItem item1 = new RecurringInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test product", "test plan", "test phase A", null, startDate, endDate, rate1, rate1, Currency.USD);
invoiceUtil.createInvoiceItem(item1, context);
final CreditAdjInvoiceItem creditItem = new CreditAdjInvoiceItem(invoice1.getId(), accountId, new LocalDate(), null, rate1.negate(), Currency.USD, null);
invoiceUtil.createInvoiceItem(creditItem, context);
final BigDecimal balance = invoiceDao.getAccountBalance(accountId, context);
assertEquals(balance.compareTo(BigDecimal.ZERO), 0);
}
Aggregations