use of org.killbill.billing.invoice.model.DefaultInvoice in project killbill by killbill.
the class TestDefaultInvoiceFormatter method testForDisplay.
// No Assertion, just to print the html
@Test(groups = "fast")
public void testForDisplay() throws Exception {
final FixedPriceInvoiceItem fixedItemBRL = new FixedPriceInvoiceItem(UUID.randomUUID(), UUID.randomUUID(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString(), null, new LocalDate(), new BigDecimal("1499.95"), Currency.BRL);
final Invoice invoice = new DefaultInvoice(UUID.randomUUID(), UUID.randomUUID(), new Integer(234), new LocalDate(), new LocalDate(), Currency.BRL, false, InvoiceStatus.COMMITTED);
invoice.addInvoiceItem(fixedItemBRL);
final String template = "<html>\n" + " <head>\n" + " <style type=\"text/css\">\n" + " #header td { width: 250px; }\n" + " #header .company_info { width: 450px; }\n" + " #header .label { text-align: right; font-weight: bold; }\n" + " #header .label_value { padding-left: 10px; }\n" + "\n" + " #invoice_items { margin-top: 50px; }\n" + " #invoice_items th { border-bottom: solid 2px black; }\n" + " #invoice_items td { width: 250px; }\n" + " #invoice_items td.amount { width: 125px; }\n" + " #invoice_items td.network { width: 350px; }\n" + " #invoice_items .amount { text-align: right; }\n" + " #invoice_items .label { text-align: right; font-weight: bold; }\n" + " </style>\n" + " </head>\n" + " <body>\n" + " <table id=\"header\">\n" + " <tr>\n" + " <td class=\"company_info\"/>\n" + " <td />\n" + " <td><h1>{{text.invoiceTitle}}</h1></td>\n" + " </tr>\n" + " <tr>\n" + " <td colspan=\"3\"><img src=\"http://static.foo.com/www/0/main/gfx/front/logo.png\"/></td>\n" + " </tr>\n" + " <tr>\n" + " <td class=\"company_info\" />\n" + " <td class=\"label\">{{text.invoiceDate}}</td>\n" + " <td class=\"label_value\">{{invoice.formattedInvoiceDate}}</td>\n" + " </tr>\n" + " <tr>\n" + " <td class=\"company_info\" />\n" + " <td class=\"label\">{{text.invoiceNumber}}</td>\n" + " <td class=\"label_value\">{{invoice.invoiceNumber}}</td>\n" + " </tr>\n" + " <tr>\n" + " <td class=\"company_info\">{{text.companyCountry}}</td>\n" + " <td colspan=\"2\" />\n" + " </tr>\n" + " <tr>\n" + " <td class=\"company_info\">{{text.companyUrl}}</td>\n" + " <td colspan=\"2\" />\n" + " </tr>\n" + " </table>\n" + "\n" + " <table id=\"invoice_items\">\n" + " <tr>\n" + " <th class=\"network\">{{text.invoiceItemBundleName}}</td>\n" + " <th>{{text.invoiceItemDescription}}</td>\n" + " <th>{{text.invoiceItemServicePeriod}}</td>\n" + " <th>{{text.invoiceItemAmount}}</td>\n" + " </tr>\n" + " {{#invoice.invoiceItems}}\n" + " <tr>\n" + " <td class=\"network\">{{description}}</td>\n" + " <td>{{planName}}</td>\n" + " <td>{{formattedStartDate}}{{#formattedEndDate}} - {{formattedEndDate}}{{/formattedEndDate}}</td>\n" + " <td class=\"amount\">{{formattedAmount}}</td>\n" + " </tr>\n" + " {{/invoice.invoiceItems}}\n" + " <tr>\n" + " <td colspan=\"4\" height=\"50px\"></td>\n" + " </tr>\n" + " <tr>\n" + " <td colspan=\"2\" />\n" + " <td class=\"label\">{{text.invoiceAmount}}</td>\n" + " <td class=\"amount\"><strong>{{invoice.formattedChargedAmount}}</strong></td>\n" + " </tr>\n" + " <tr>\n" + " <td colspan=\"2\" />\n" + " <td class=\"label\">{{text.invoiceAmountPaid}}" + " {{#invoice.processedCurrency}}" + " (*)" + " {{/invoice.processedCurrency}}\n" + " </td>\n" + " <td class=\"amount\"><strong>{{invoice.formattedPaidAmount}}</strong></td>\n" + " </tr>\n" + " <tr>\n" + " <td colspan=\"2\" />\n" + " <td class=\"label\">{{text.invoiceBalance}}</td>\n" + " <td class=\"amount\"><strong>{{invoice.formattedBalance}}</strong></td>\n" + " </tr>\n" + " </table>\n" + " {{#invoice.processedCurrency}}" + " {{text.processedPaymentCurrency}} {{invoice.processedCurrency}}." + " {{#invoice.processedPaymentRate}}\n" + " {{text.processedPaymentRate}} {{invoice.processedPaymentRate}}.\n" + " {{/invoice.processedPaymentRate}}" + " {{/invoice.processedCurrency}}" + " </body>\n" + "</html>\n";
final Map<String, Object> data = new HashMap<String, Object>();
final String bundlePath = "org/killbill/billing/util/template/translation/InvoiceTranslation";
final ResourceBundle bundle = resourceBundleFactory.createBundle(Locale.US, bundlePath, ResourceBundleType.INVOICE_TRANSLATION, internalCallContext);
final DefaultInvoiceTranslator translator = new DefaultInvoiceTranslator(bundle, null);
data.put("text", translator);
data.put("invoice", new DefaultInvoiceFormatter(config, invoice, Locale.US, currencyConversionApi, resourceBundleFactory, internalCallContext));
final String formattedText = templateEngine.executeTemplateText(template, data);
System.out.println(formattedText);
}
use of org.killbill.billing.invoice.model.DefaultInvoice in project killbill by killbill.
the class TestDefaultInvoiceGenerator method testRegressionFor170.
// Regression test for #170 (see https://github.com/killbill/killbill/pull/173)
@Test(groups = "fast")
public void testRegressionFor170() throws EntityPersistenceException, InvoiceApiException, CatalogApiException {
final UUID accountId = account.getId();
final Currency currency = Currency.USD;
final SubscriptionBase subscription = createSubscription();
final MockInternationalPrice recurringPrice = new MockInternationalPrice(new DefaultPrice(new BigDecimal("2.9500"), Currency.USD));
final MockPlanPhase phase = new MockPlanPhase(recurringPrice, null);
final Plan plan = new MockPlan(phase);
final LocalDate targetDate = new LocalDate(2013, 10, 30);
final Invoice existingInvoice = new DefaultInvoice(UUID.randomUUID(), accountId, null, clock.getUTCToday(), targetDate, currency, false, InvoiceStatus.COMMITTED);
// Set the existing recurring invoice item 2013/06/15 - 2013/07/15
final LocalDate startDate = new LocalDate(2013, 06, 15);
final LocalDate endDate = new LocalDate(2013, 07, 15);
final InvoiceItem recurringInvoiceItem = new RecurringInvoiceItem(existingInvoice.getId(), accountId, subscription.getBundleId(), subscription.getId(), null, plan.getName(), phase.getName(), null, startDate, endDate, recurringPrice.getPrice(currency), recurringPrice.getPrice(currency), Currency.USD);
existingInvoice.addInvoiceItem(recurringInvoiceItem);
// Set an existing repair item
final LocalDate repairStartDate = new LocalDate(2013, 06, 21);
final LocalDate repairEndDate = new LocalDate(2013, 06, 26);
final BigDecimal repairAmount = new BigDecimal("0.4900").negate();
final InvoiceItem repairItem = new RepairAdjInvoiceItem(existingInvoice.getId(), accountId, repairStartDate, repairEndDate, repairAmount, currency, recurringInvoiceItem.getId());
existingInvoice.addInvoiceItem(repairItem);
// Create the billing event associated with the subscription creation
//
// Note : this is the interesting part of the test; it does not provide the blocking billing events, which force invoice
// to un repair what was previously repaired.
final BillingEventSet events = new MockBillingEventSet();
final BillingEvent event = invoiceUtil.createMockBillingEvent(null, subscription, new DateTime("2013-06-15", DateTimeZone.UTC), plan, phase, null, recurringPrice.getPrice(currency), currency, BillingPeriod.MONTHLY, 15, BillingMode.IN_ADVANCE, "testEvent", 1L, SubscriptionBaseTransitionType.CREATE);
events.add(event);
final List<Invoice> existingInvoices = new LinkedList<Invoice>();
existingInvoices.add(existingInvoice);
// Generate a new invoice
final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, new AccountInvoices(null, null, existingInvoices), null, targetDate, currency, null, internalCallContext);
final Invoice invoice = invoiceWithMetadata.getInvoice();
assertEquals(invoice.getNumberOfItems(), 5);
assertEquals(invoice.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.RECURRING);
assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), new LocalDate(2013, 6, 21));
assertEquals(invoice.getInvoiceItems().get(0).getEndDate(), new LocalDate(2013, 6, 26));
assertTrue(repairAmount.negate().compareTo(invoice.getInvoiceItems().get(0).getAmount()) == 0);
assertEquals(invoice.getInvoiceItems().get(1).getInvoiceItemType(), InvoiceItemType.RECURRING);
assertEquals(invoice.getInvoiceItems().get(1).getStartDate(), new LocalDate(2013, 7, 15));
assertEquals(invoice.getInvoiceItems().get(1).getEndDate(), new LocalDate(2013, 8, 15));
assertEquals(invoice.getInvoiceItems().get(2).getInvoiceItemType(), InvoiceItemType.RECURRING);
assertEquals(invoice.getInvoiceItems().get(2).getStartDate(), new LocalDate(2013, 8, 15));
assertEquals(invoice.getInvoiceItems().get(2).getEndDate(), new LocalDate(2013, 9, 15));
assertEquals(invoice.getInvoiceItems().get(3).getInvoiceItemType(), InvoiceItemType.RECURRING);
assertEquals(invoice.getInvoiceItems().get(3).getStartDate(), new LocalDate(2013, 9, 15));
assertEquals(invoice.getInvoiceItems().get(3).getEndDate(), new LocalDate(2013, 10, 15));
assertEquals(invoice.getInvoiceItems().get(4).getInvoiceItemType(), InvoiceItemType.RECURRING);
assertEquals(invoice.getInvoiceItems().get(4).getStartDate(), new LocalDate(2013, 10, 15));
assertEquals(invoice.getInvoiceItems().get(4).getEndDate(), new LocalDate(2013, 11, 15));
// Add newly generated invoice to existing invoices
existingInvoices.add(invoice);
// Generate next invoice (no-op)
final InvoiceWithMetadata newInvoiceWithMetdata = generator.generateInvoice(account, events, new AccountInvoices(null, null, existingInvoices), null, targetDate, currency, null, internalCallContext);
final Invoice newInvoice = newInvoiceWithMetdata.getInvoice();
assertNull(newInvoice);
}
use of org.killbill.billing.invoice.model.DefaultInvoice in project killbill by killbill.
the class TestInvoiceDao method testAccountBalanceWithAllSortsOfThings.
@Test(groups = "slow")
public void testAccountBalanceWithAllSortsOfThings() 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 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 product", "test plan", "test phase A", null, 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 product", "test plan", "test phase B", null, 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 BigDecimal payment1 = amount1.add(rate1);
final InvoicePayment payment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, UUID.randomUUID(), 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 product", "test plan", "test phase B", null, 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 (along with CBA generated by the system)
final InvoicePayment refund = new DefaultInvoicePayment(UUID.randomUUID(), InvoicePaymentType.ATTEMPT, UUID.randomUUID(), invoice1.getId(), new DateTime(), rate2.negate(), Currency.USD, Currency.USD, null, payment.getId());
invoiceUtil.createPayment(refund, context);
final CreditBalanceAdjInvoiceItem cbaItem2 = new CreditBalanceAdjInvoiceItem(invoice1.getId(), accountId, new LocalDate(), rate2.negate(), Currency.USD);
invoiceUtil.createInvoiceItem(cbaItem2, context);
balance = invoiceDao.getAccountBalance(accountId, context);
assertEquals(balance.compareTo(BigDecimal.ZERO), 0);
cba = invoiceDao.getAccountCBA(accountId, context);
assertEquals(cba.compareTo(BigDecimal.ZERO), 0);
// NEXT RECURRING on invoice 2
final Invoice invoice2 = new DefaultInvoice(accountId, clock.getUTCToday(), targetDate1.plusMonths(1), Currency.USD);
invoiceUtil.createInvoice(invoice2, context);
final RecurringInvoiceItem nextItem = new RecurringInvoiceItem(invoice2.getId(), accountId, bundleId, UUID.randomUUID(), "test product", "test plan", "test bla", null, startDate.plusMonths(1), endDate.plusMonths(1), rate2, rate2, Currency.USD);
invoiceUtil.createInvoiceItem(nextItem, context);
balance = invoiceDao.getAccountBalance(accountId, context);
assertEquals(balance.compareTo(new BigDecimal("10.00")), 0);
cba = invoiceDao.getAccountCBA(accountId, context);
assertEquals(cba.compareTo(new BigDecimal("0.00")), 0);
// FINALLY ISSUE A CREDIT ADJ
final CreditAdjInvoiceItem creditItem = new CreditAdjInvoiceItem(invoice2.getId(), accountId, new LocalDate(), null, rate2.negate(), Currency.USD, null);
invoiceUtil.createInvoiceItem(creditItem, context);
balance = invoiceDao.getAccountBalance(accountId, context);
assertEquals(balance.compareTo(new BigDecimal("0.00")), 0);
cba = invoiceDao.getAccountCBA(accountId, context);
assertEquals(cba.compareTo(new BigDecimal("0.00")), 0);
}
use of org.killbill.billing.invoice.model.DefaultInvoice in project killbill by killbill.
the class TestInvoiceDao method testDeleteConsumedCredit.
@Test(groups = "slow")
public void testDeleteConsumedCredit() throws Exception {
final UUID accountId = account.getId();
final Invoice invoice = new DefaultInvoice(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
final RecurringInvoiceItem recurringItem = new RecurringInvoiceItem(invoice.getId(), accountId, UUID.randomUUID(), UUID.randomUUID(), "test product", "test plan", "test ZOO", null, clock.getUTCNow().plusMonths(-1).toLocalDate(), clock.getUTCNow().toLocalDate(), BigDecimal.TEN, BigDecimal.TEN, Currency.USD);
final CreditBalanceAdjInvoiceItem creditBalanceAdjInvoiceItem1 = new CreditBalanceAdjInvoiceItem(invoice.getId(), invoice.getAccountId(), invoice.getInvoiceDate(), BigDecimal.ONE.negate(), invoice.getCurrency());
invoiceUtil.createInvoice(invoice, context);
invoiceUtil.createInvoiceItem(recurringItem, context);
invoiceUtil.createInvoiceItem(creditBalanceAdjInvoiceItem1, context);
invoiceUtil.verifyInvoice(invoice.getId(), 9.00, -1.00, context);
// Delete the CBA on invoice 1
invoiceDao.deleteCBA(accountId, invoice.getId(), creditBalanceAdjInvoiceItem1.getId(), context);
final InvoiceModelDao res = invoiceDao.getById(invoice.getId(), context);
Assert.assertEquals(res.getInvoiceItems().size(), 2);
Assert.assertEquals(InvoiceModelDaoHelper.getRawBalanceForRegularInvoice(res).compareTo(BigDecimal.TEN), 0);
Assert.assertEquals(InvoiceModelDaoHelper.getCBAAmount(res).compareTo(BigDecimal.ZERO), 0);
final InvoiceItemModelDao cbaAdj = Iterables.tryFind(res.getInvoiceItems(), new Predicate<InvoiceItemModelDao>() {
@Override
public boolean apply(final InvoiceItemModelDao input) {
return input.getType() == InvoiceItemType.CBA_ADJ && input.getAmount().compareTo(BigDecimal.ZERO) == 0;
}
}).orNull();
Assert.assertNotNull(cbaAdj);
}
use of org.killbill.billing.invoice.model.DefaultInvoice in project killbill by killbill.
the class TestInvoiceDao method testAccountBalanceWithNoInvoiceItems.
@Test(groups = "slow")
public void testAccountBalanceWithNoInvoiceItems() 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 BigDecimal payment1 = new BigDecimal("48.0");
final InvoicePayment payment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, UUID.randomUUID(), invoice1.getId(), new DateTime(), payment1, Currency.USD, Currency.USD, null, true);
invoiceUtil.createPayment(payment, context);
final BigDecimal balance = invoiceDao.getAccountBalance(accountId, context);
assertEquals(balance.compareTo(BigDecimal.ZERO.subtract(payment1)), 0);
}
Aggregations