Search in sources :

Example 6 with ItemAdjInvoiceItem

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);
}
Also used : FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) Test(org.testng.annotations.Test)

Example 7 with ItemAdjInvoiceItem

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);
}
Also used : FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) Test(org.testng.annotations.Test)

Example 8 with ItemAdjInvoiceItem

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);
}
Also used : FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) Test(org.testng.annotations.Test)

Example 9 with ItemAdjInvoiceItem

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());
}
Also used : ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) HashMap(java.util.HashMap) Currency(org.killbill.billing.catalog.api.Currency) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) UUID(java.util.UUID) BigDecimal(java.math.BigDecimal) Predicate(com.google.common.base.Predicate)

Example 10 with ItemAdjInvoiceItem

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);
}
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) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)15 BigDecimal (java.math.BigDecimal)14 LocalDate (org.joda.time.LocalDate)13 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)13 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)13 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)13 RepairAdjInvoiceItem (org.killbill.billing.invoice.model.RepairAdjInvoiceItem)13 Test (org.testng.annotations.Test)13 UUID (java.util.UUID)7 Invoice (org.killbill.billing.invoice.api.Invoice)7 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)7 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)6 BillingEventSet (org.killbill.billing.junction.BillingEventSet)6 LinkedList (java.util.LinkedList)5 SubscriptionFutureNotificationDates (org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates)5 MockPlan (org.killbill.billing.catalog.MockPlan)4 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)4 Plan (org.killbill.billing.catalog.api.Plan)4 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)4 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)4