Search in sources :

Example 6 with TaxInvoiceItem

use of org.killbill.billing.invoice.model.TaxInvoiceItem in project killbill by killbill.

the class InvoicePluginDispatcher method validateAndSanitizeInvoiceItemFromPlugin.

private InvoiceItem validateAndSanitizeInvoiceItemFromPlugin(final UUID originalInvoiceId, final Map<UUID, InvoiceItem> invoiceItemsByItemId, final InvoiceItem additionalInvoiceItem, final InvoicePluginApi invoicePlugin) throws InvoiceApiException {
    final InvoiceItem existingItem = invoiceItemsByItemId.get(additionalInvoiceItem.getId());
    if (!ALLOWED_INVOICE_ITEM_TYPES.contains(additionalInvoiceItem.getInvoiceItemType()) && existingItem == null) {
        log.warn("Ignoring invoice item of type {} from InvoicePlugin {}: {}", additionalInvoiceItem.getInvoiceItemType(), invoicePlugin, additionalInvoiceItem);
        throw new InvoiceApiException(ErrorCode.INVOICE_ITEM_TYPE_INVALID, additionalInvoiceItem.getInvoiceItemType());
    }
    final UUID invoiceId = MoreObjects.firstNonNull(mutableField("invoiceId", existingItem != null ? existingItem.getInvoiceId() : null, additionalInvoiceItem.getInvoiceId(), invoicePlugin), originalInvoiceId);
    final UUID additionalInvoiceId = MoreObjects.firstNonNull(additionalInvoiceItem.getId(), UUIDs.randomUUID());
    final InvoiceItemCatalogBase tmp = new InvoiceItemCatalogBase(additionalInvoiceId, mutableField("createdDate", existingItem != null ? existingItem.getCreatedDate() : null, additionalInvoiceItem.getCreatedDate(), invoicePlugin), invoiceId, immutableField("accountId", existingItem, existingItem != null ? existingItem.getAccountId() : null, additionalInvoiceItem.getAccountId(), invoicePlugin), immutableField("bundleId", existingItem, existingItem != null ? existingItem.getBundleId() : null, additionalInvoiceItem.getBundleId(), invoicePlugin), immutableField("subscriptionId", existingItem, existingItem != null ? existingItem.getSubscriptionId() : null, additionalInvoiceItem.getSubscriptionId(), invoicePlugin), mutableField("description", existingItem != null ? existingItem.getDescription() : null, additionalInvoiceItem.getDescription(), invoicePlugin), immutableField("productName", existingItem, existingItem != null ? existingItem.getProductName() : null, additionalInvoiceItem.getProductName(), invoicePlugin), immutableField("planName", existingItem, existingItem != null ? existingItem.getPlanName() : null, additionalInvoiceItem.getPlanName(), invoicePlugin), immutableField("phaseName", existingItem, existingItem != null ? existingItem.getPhaseName() : null, additionalInvoiceItem.getPhaseName(), invoicePlugin), immutableField("usageName", existingItem, existingItem != null ? existingItem.getUsageName() : null, additionalInvoiceItem.getUsageName(), invoicePlugin), immutableField("catalogEffectiveDate", existingItem, existingItem != null ? existingItem.getCatalogEffectiveDate() : null, additionalInvoiceItem.getCatalogEffectiveDate(), invoicePlugin), mutableField("prettyProductName", existingItem != null ? existingItem.getPrettyProductName() : null, additionalInvoiceItem.getPrettyProductName(), invoicePlugin), mutableField("prettyPlanName", existingItem != null ? existingItem.getPrettyPlanName() : null, additionalInvoiceItem.getPrettyPlanName(), invoicePlugin), mutableField("prettyPhaseName", existingItem != null ? existingItem.getPrettyPhaseName() : null, additionalInvoiceItem.getPrettyPhaseName(), invoicePlugin), mutableField("prettyUsageName", existingItem != null ? existingItem.getPrettyUsageName() : null, additionalInvoiceItem.getPrettyUsageName(), invoicePlugin), immutableField("startDate", existingItem, existingItem != null ? existingItem.getStartDate() : null, additionalInvoiceItem.getStartDate(), invoicePlugin), immutableField("endDate", existingItem, existingItem != null ? existingItem.getEndDate() : null, additionalInvoiceItem.getEndDate(), invoicePlugin), mutableField("amount", existingItem != null ? existingItem.getAmount() : null, additionalInvoiceItem.getAmount(), invoicePlugin), immutableField("rate", existingItem, existingItem != null ? existingItem.getRate() : null, additionalInvoiceItem.getRate(), invoicePlugin), immutableField("currency", existingItem, existingItem != null ? existingItem.getCurrency() : null, additionalInvoiceItem.getCurrency(), invoicePlugin), immutableField("linkedItemId", existingItem, existingItem != null ? existingItem.getLinkedItemId() : null, additionalInvoiceItem.getLinkedItemId(), invoicePlugin), immutableField("quantity", existingItem, existingItem != null ? existingItem.getQuantity() : null, additionalInvoiceItem.getQuantity(), invoicePlugin), mutableField("itemDetails", existingItem != null ? existingItem.getItemDetails() : null, additionalInvoiceItem.getItemDetails(), invoicePlugin), immutableField("invoiceItemType", existingItem, existingItem != null ? existingItem.getInvoiceItemType() : null, additionalInvoiceItem.getInvoiceItemType(), invoicePlugin));
    switch(tmp.getInvoiceItemType()) {
        case RECURRING:
            return new RecurringInvoiceItem(tmp);
        case USAGE:
            return new UsageInvoiceItem(tmp);
        case FIXED:
            return new FixedPriceInvoiceItem(tmp);
        case TAX:
            return new TaxInvoiceItem(tmp);
        case EXTERNAL_CHARGE:
            return new ExternalChargeInvoiceItem(tmp);
        default:
            // As long as there is no explicit cast to the expected type this works
            return tmp;
    }
}
Also used : TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) UUID(java.util.UUID) InvoiceItemCatalogBase(org.killbill.billing.invoice.model.InvoiceItemCatalogBase) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem)

Example 7 with TaxInvoiceItem

use of org.killbill.billing.invoice.model.TaxInvoiceItem in project killbill by killbill.

the class TestWithTaxItems method testDryRunTaxItemsWithCredits.

@Test(groups = "slow", description = "https://github.com/killbill/killbill/issues/637")
public void testDryRunTaxItemsWithCredits() throws Exception {
    // We take april as it has 30 days (easier to play with BCD)
    // Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
    clock.setDay(new LocalDate(2012, 4, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    // Create original subscription (Trial PHASE) -> $0 invoice.
    final DefaultEntitlement bpSubscription = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Pistol", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    subscriptionChecker.checkSubscriptionCreated(bpSubscription.getId(), internalCallContext);
    busHandler.pushExpectedEvents(NextEvent.INVOICE);
    final InvoiceItem inputCredit = new CreditAdjInvoiceItem(null, account.getId(), clock.getUTCToday(), "VIP", new BigDecimal("100"), account.getCurrency(), null);
    invoiceUserApi.insertCredits(account.getId(), clock.getUTCToday(), ImmutableList.of(inputCredit), true, null, callContext);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 4, 1), InvoiceItemType.CBA_ADJ, new BigDecimal("100")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 4, 1), InvoiceItemType.CREDIT_ADJ, new BigDecimal("-100")));
    // Make sure TestInvoicePluginApi will return an additional TAX item
    testInvoicePluginApi.addTaxItem(new TaxInvoiceItem(UUID.randomUUID(), null, account.getId(), null, "Tax Item", new LocalDate(2012, 4, 1), BigDecimal.ONE, account.getCurrency()));
    // Verify dry-run scenario
    final Invoice dryRunInvoice = invoiceUserApi.triggerDryRunInvoiceGeneration(account.getId(), new LocalDate(2012, 5, 1), new TestDryRunArguments(DryRunType.TARGET_DATE), callContext);
    invoiceChecker.checkInvoiceNoAudits(dryRunInvoice, ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("29.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.TAX, new BigDecimal("1.0")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 4, 1), InvoiceItemType.CBA_ADJ, new BigDecimal("-30.95"))));
    // Make sure TestInvoicePluginApi will return an additional TAX item
    testInvoicePluginApi.addTaxItem(new TaxInvoiceItem(UUID.randomUUID(), null, account.getId(), null, "Tax Item", new LocalDate(2012, 5, 1), BigDecimal.ONE, account.getCurrency()));
    // Move to Evergreen PHASE to verify non-dry-run scenario
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE);
    clock.addDays(30);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 3, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("29.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), null, InvoiceItemType.TAX, new BigDecimal("1.0")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 5, 1), InvoiceItemType.CBA_ADJ, new BigDecimal("-30.95")));
}
Also used : Account(org.killbill.billing.account.api.Account) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) Invoice(org.killbill.billing.invoice.api.Invoice) AccountData(org.killbill.billing.account.api.AccountData) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) LocalDate(org.joda.time.LocalDate) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 8 with TaxInvoiceItem

use of org.killbill.billing.invoice.model.TaxInvoiceItem in project killbill by killbill.

the class TestInvoiceItemDao method testTaxInvoiceSqlDao.

@Test(groups = "slow")
public void testTaxInvoiceSqlDao() throws Exception {
    final UUID invoiceId = UUID.randomUUID();
    final UUID accountId = account.getId();
    final UUID bundleId = UUID.randomUUID();
    final UUID linkedItemId = UUID.randomUUID();
    final String description = UUID.randomUUID().toString();
    final LocalDate startDate = new LocalDate(2012, 4, 1);
    final LocalDate endDate = new LocalDate(2012, 5, 1);
    final InvoiceItem taxInvoiceItem = new TaxInvoiceItem(UUID.randomUUID(), null, invoiceId, accountId, bundleId, null, null, null, null, null, null, null, null, null, null, startDate, endDate, description, TEN, Currency.USD, linkedItemId, null);
    invoiceUtil.createInvoiceItem(taxInvoiceItem, context);
    final InvoiceItemModelDao savedItem = invoiceUtil.getInvoiceItemById(taxInvoiceItem.getId(), context);
    assertSameInvoiceItem(taxInvoiceItem, savedItem);
}
Also used : TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 9 with TaxInvoiceItem

use of org.killbill.billing.invoice.model.TaxInvoiceItem in project killbill by killbill.

the class TestDefaultInvoiceUserApi method testAddTaxItems.

@Test(groups = "slow")
public void testAddTaxItems() throws Exception {
    final Account account = invoiceUtil.createAccount(callContext);
    final UUID accountId = account.getId();
    final UUID invoiceId = invoiceUtil.generateRegularInvoice(account, null, callContext);
    final BigDecimal taxItemAmount = BigDecimal.TEN;
    final UUID bundleId = UUID.randomUUID();
    final InvoiceItem taxItem = new TaxInvoiceItem(null, accountId, bundleId, UUID.randomUUID().toString(), clock.getUTCToday(), taxItemAmount, accountCurrency);
    final List<InvoiceItem> resultTaxInvoiceItems = invoiceUserApi.insertTaxItems(accountId, clock.getUTCToday(), ImmutableList.<InvoiceItem>of(taxItem), true, ImmutableList.of(), callContext);
    Assert.assertEquals(resultTaxInvoiceItems.size(), 1);
    Assert.assertEquals(resultTaxInvoiceItems.get(0).getAmount().compareTo(taxItemAmount), 0);
    Assert.assertEquals(resultTaxInvoiceItems.get(0).getBundleId(), bundleId);
}
Also used : Account(org.killbill.billing.account.api.Account) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) UUID(java.util.UUID) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

ExternalChargeInvoiceItem (org.killbill.billing.invoice.model.ExternalChargeInvoiceItem)9 TaxInvoiceItem (org.killbill.billing.invoice.model.TaxInvoiceItem)9 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)8 LocalDate (org.joda.time.LocalDate)7 Test (org.testng.annotations.Test)7 BigDecimal (java.math.BigDecimal)6 UUID (java.util.UUID)6 Account (org.killbill.billing.account.api.Account)6 CreditAdjInvoiceItem (org.killbill.billing.invoice.model.CreditAdjInvoiceItem)6 AccountData (org.killbill.billing.account.api.AccountData)5 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)5 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)5 Invoice (org.killbill.billing.invoice.api.Invoice)5 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)2 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)2 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)2 Predicate (com.google.common.base.Predicate)1 HashMap (java.util.HashMap)1 AccountApiException (org.killbill.billing.account.api.AccountApiException)1 ImmutableAccountData (org.killbill.billing.account.api.ImmutableAccountData)1