use of org.killbill.billing.invoice.model.ItemAdjInvoiceItem in project killbill by killbill.
the class TestSubscriptionItemTree method testRepairWithSmallItemAdjustment.
@Test(groups = "fast")
public void testRepairWithSmallItemAdjustment() {
final LocalDate startDate = new LocalDate(2014, 1, 1);
final LocalDate itemAdjDate = new LocalDate(2014, 1, 2);
final LocalDate endDate = new LocalDate(2014, 2, 1);
final LocalDate cancelDate = new LocalDate(2014, 1, 23);
final BigDecimal rate1 = new BigDecimal("12.00");
final BigDecimal amount1 = rate1;
final SubscriptionItemTree tree = new SubscriptionItemTree(subscriptionId, invoiceId);
final InvoiceItem initial = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, endDate, amount1, rate1, currency);
final InvoiceItem itemAdj = new ItemAdjInvoiceItem(initial, itemAdjDate, new BigDecimal("-2.00"), currency);
tree.addItem(initial);
tree.addItem(itemAdj);
tree.flatten(true);
final InvoiceItem proposed1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, cancelDate, amount1, rate1, currency);
tree.mergeProposedItem(proposed1);
tree.buildForMerge();
final List<InvoiceItem> expectedResult = Lists.newLinkedList();
final InvoiceItem repair1 = new RepairAdjInvoiceItem(invoiceId, accountId, cancelDate, endDate, new BigDecimal("-3.48"), currency, initial.getId());
expectedResult.add(repair1);
verifyResult(tree.getView(), expectedResult);
}
use of org.killbill.billing.invoice.model.ItemAdjInvoiceItem in project killbill by killbill.
the class TestSubscriptionItemTree method testPartialProRation.
@Test(groups = "fast")
public void testPartialProRation() {
final LocalDate startDate = new LocalDate(2014, 1, 1);
final LocalDate cancelDate = new LocalDate(2014, 1, 25);
final LocalDate endDate = new LocalDate(2014, 2, 1);
final BigDecimal monthlyRate1 = new BigDecimal("12.00");
final BigDecimal monthlyAmount1 = monthlyRate1;
final SubscriptionItemTree tree = new SubscriptionItemTree(subscriptionId, invoiceId);
final InvoiceItem existing1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, endDate, monthlyAmount1, monthlyRate1, currency);
tree.addItem(existing1);
// Partially item adjust the recurring item
final InvoiceItem existingItemAdj1 = new ItemAdjInvoiceItem(existing1, startDate, monthlyRate1.negate().add(BigDecimal.ONE), currency);
tree.addItem(existingItemAdj1);
tree.flatten(true);
final InvoiceItem proposed1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, cancelDate, monthlyAmount1, monthlyRate1, currency);
tree.mergeProposedItem(proposed1);
tree.buildForMerge();
final InvoiceItem repair = new RepairAdjInvoiceItem(invoiceId, accountId, cancelDate, endDate, BigDecimal.ONE.negate(), Currency.USD, existing1.getId());
final List<InvoiceItem> expectedResult = ImmutableList.<InvoiceItem>of(repair);
verifyResult(tree.getView(), expectedResult);
}
use of org.killbill.billing.invoice.model.ItemAdjInvoiceItem in project killbill by killbill.
the class TestSubscriptionItemTree method testRepairWithLargeItemAdjustment.
@Test(groups = "fast")
public void testRepairWithLargeItemAdjustment() {
final LocalDate startDate = new LocalDate(2014, 1, 1);
final LocalDate itemAdjDate = new LocalDate(2014, 1, 2);
final LocalDate endDate = new LocalDate(2014, 2, 1);
final LocalDate cancelDate = new LocalDate(2014, 1, 23);
final BigDecimal rate1 = new BigDecimal("12.00");
final BigDecimal amount1 = rate1;
final SubscriptionItemTree tree = new SubscriptionItemTree(subscriptionId, invoiceId);
final InvoiceItem initial = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, endDate, amount1, rate1, currency);
final InvoiceItem itemAdj = new ItemAdjInvoiceItem(initial, itemAdjDate, new BigDecimal("-10.00"), currency);
tree.addItem(initial);
tree.addItem(itemAdj);
tree.flatten(true);
final InvoiceItem proposed1 = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, cancelDate, amount1, rate1, currency);
tree.mergeProposedItem(proposed1);
tree.buildForMerge();
final List<InvoiceItem> expectedResult = Lists.newLinkedList();
final InvoiceItem repair1 = new RepairAdjInvoiceItem(invoiceId, accountId, cancelDate, endDate, new BigDecimal("-2.00"), currency, initial.getId());
expectedResult.add(repair1);
verifyResult(tree.getView(), expectedResult);
}
use of org.killbill.billing.invoice.model.ItemAdjInvoiceItem in project killbill by killbill.
the class InvoiceApiHelper method createAdjustmentItem.
/**
* Create an adjustment for a given invoice item. This just creates the object in memory, it doesn't write it to disk.
*
* @param invoiceToBeAdjusted the invoice
* @param invoiceItemId the invoice item id to adjust
* @param positiveAdjAmount the amount to adjust. Pass null to adjust the full amount of the original item
* @param currency the currency of the amount. Pass null to default to the original currency used
* @param effectiveDate adjustment effective date, in the account timezone
* @return the adjustment item
*/
public InvoiceItem createAdjustmentItem(final Invoice invoiceToBeAdjusted, final UUID invoiceItemId, @Nullable final BigDecimal positiveAdjAmount, @Nullable final Currency currency, final LocalDate effectiveDate, final String description, final InternalCallContext context) throws InvoiceApiException {
final InvoiceItem invoiceItemToBeAdjusted = Iterables.<InvoiceItem>tryFind(invoiceToBeAdjusted.getInvoiceItems(), new Predicate<InvoiceItem>() {
@Override
public boolean apply(final InvoiceItem input) {
return input.getId().equals(invoiceItemId);
}
}).orNull();
if (invoiceItemToBeAdjusted == null) {
throw new InvoiceApiException(ErrorCode.INVOICE_ITEM_NOT_FOUND, invoiceItemId);
}
// Check the specified currency matches the one of the existing invoice
final Currency currencyForAdjustment = MoreObjects.firstNonNull(currency, invoiceItemToBeAdjusted.getCurrency());
if (invoiceItemToBeAdjusted.getCurrency() != currencyForAdjustment) {
throw new InvoiceApiException(ErrorCode.CURRENCY_INVALID, currency, invoiceItemToBeAdjusted.getCurrency());
}
// Reuse the same logic we have for refund with item adjustment
final Map<UUID, BigDecimal> input = new HashMap<UUID, BigDecimal>();
input.put(invoiceItemId, positiveAdjAmount);
final Map<UUID, BigDecimal> output = dao.computeItemAdjustments(invoiceToBeAdjusted.getId().toString(), input, context);
// If we pass that stage, it means the validation succeeded so we just need to extract resulting amount and negate the result.
final BigDecimal amountToAdjust = output.get(invoiceItemId).negate();
// Finally, create the adjustment
return new ItemAdjInvoiceItem(UUIDs.randomUUID(), context.getCreatedDate(), invoiceItemToBeAdjusted.getInvoiceId(), invoiceItemToBeAdjusted.getAccountId(), effectiveDate, description, amountToAdjust, currencyForAdjustment, invoiceItemToBeAdjusted.getId());
}
use of org.killbill.billing.invoice.model.ItemAdjInvoiceItem in project killbill by killbill.
the class TestInvoiceDao method testRetrieveInvoiceItemsByParentInvoice.
@Test(groups = "slow")
public void testRetrieveInvoiceItemsByParentInvoice() throws InvoiceApiException, EntityPersistenceException {
final UUID childAccountId = account.getId();
final Invoice childInvoice = new DefaultInvoice(childAccountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
final UUID invoiceId = childInvoice.getId();
final UUID subscriptionId = UUID.randomUUID();
final UUID bundleId = UUID.randomUUID();
final LocalDate startDate = new LocalDate(2010, 1, 1);
final LocalDate endDate = new LocalDate(2010, 4, 1);
final InvoiceItem invoiceItem = new RecurringInvoiceItem(invoiceId, childAccountId, bundleId, subscriptionId, "test plan", "test phase", startDate, endDate, new BigDecimal("21.00"), new BigDecimal("7.00"), Currency.USD);
final InvoiceItem invoiceAdj = new ItemAdjInvoiceItem(invoiceItem, startDate, new BigDecimal("-5.00"), Currency.USD);
childInvoice.addInvoiceItem(invoiceItem);
childInvoice.addInvoiceItem(invoiceAdj);
invoiceUtil.createInvoice(childInvoice, context);
final UUID parentInvoiceId = UUID.randomUUID();
InvoiceParentChildModelDao invoiceRelation = new InvoiceParentChildModelDao(parentInvoiceId, childInvoice.getId(), childAccountId);
invoiceDao.createParentChildInvoiceRelation(invoiceRelation, context);
final List<InvoiceItemModelDao> invoiceItems = invoiceDao.getInvoiceItemsByParentInvoice(parentInvoiceId, context);
assertEquals(invoiceItems.size(), 2);
assertEquals(invoiceItems.get(0).getType(), InvoiceItemType.RECURRING);
assertEquals(invoiceItems.get(1).getType(), InvoiceItemType.ITEM_ADJ);
}
Aggregations