@Override public int getNumberOfItems() { return invoice.getNumberOfItems(); }
private void logInvoiceWithItems(final ImmutableAccountData account, final Invoice invoice, final LocalDate targetDate, final Set<UUID> adjustedUniqueOtherInvoiceId, final boolean isRealInvoiceWithItems) { final StringBuilder tmp = new StringBuilder(); if (isRealInvoiceWithItems) { tmp.append(String.format("Generated invoiceId='%s', numberOfItems='%d', accountId='%s', targetDate='%s':", invoice.getId(), invoice.getNumberOfItems(), account.getId(), targetDate)); } else { final String adjustedInvoices = JOINER_COMMA.join(adjustedUniqueOtherInvoiceId.toArray(new UUID[adjustedUniqueOtherInvoiceId.size()])); tmp.append(String.format("Adjusting existing invoiceId='%s', numberOfItems='%d', accountId='%s', targetDate='%s':\n", adjustedInvoices, invoice.getNumberOfItems(), account.getId(), targetDate)); } for (final InvoiceItem item : invoice.getInvoiceItems()) { tmp.append(String.format("\n\t item = %s", item)); } log.info(tmp.toString()); }
private void testInvoiceGeneration(final UUID accountId, final BillingEventSet events, final List<Invoice> existingInvoices, final LocalDate targetDate, final int expectedNumberOfItems, final BigDecimal expectedAmount) throws InvoiceApiException { final Currency currency = Currency.USD; final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, existingInvoices, null, targetDate, currency, internalCallContext); final Invoice invoice = invoiceWithMetadata.getInvoice(); assertNotNull(invoice); assertEquals(invoice.getNumberOfItems(), expectedNumberOfItems); existingInvoices.add(invoice); distributeItems(existingInvoices); assertEquals(invoice.getBalance(), KillBillMoney.of(expectedAmount, invoice.getCurrency())); }
@Test(groups = "fast") public void testMixedModeLifeCycle() throws InvoiceApiException, CatalogApiException { // create a SubscriptionBase with a fixed price and recurring price final Plan plan1 = new MockPlan(); final BigDecimal monthlyRate = FIVE; final BigDecimal fixedCost = TEN; final PlanPhase phase1 = createMockMonthlyPlanPhase(monthlyRate, fixedCost, PhaseType.TRIAL); final BillingEventSet events = new MockBillingEventSet(); final UUID subscriptionId = UUID.randomUUID(); final UUID bundleId = UUID.randomUUID(); final LocalDate startDate = new LocalDate(2011, 1, 1); final BillingEvent event1 = createBillingEvent(subscriptionId, bundleId, startDate, plan1, phase1, 1); events.add(event1); // ensure both components are invoiced final InvoiceWithMetadata invoiceWithMetadata1 = generator.generateInvoice(account, events, null, null, startDate, Currency.USD, internalCallContext); final Invoice invoice1 = invoiceWithMetadata1.getInvoice(); assertNotNull(invoice1); assertEquals(invoice1.getNumberOfItems(), 2); assertEquals(invoice1.getBalance(), KillBillMoney.of(FIFTEEN, invoice1.getCurrency())); final List<Invoice> invoiceList = new ArrayList<Invoice>(); invoiceList.add(invoice1); // move forward in time one billing period final LocalDate currentDate = startDate.plusMonths(1); // ensure that only the recurring price is invoiced final InvoiceWithMetadata invoiceWithMetadata2 = generator.generateInvoice(account, events, invoiceList, null, currentDate, Currency.USD, internalCallContext); final Invoice invoice2 = invoiceWithMetadata2.getInvoice(); assertNotNull(invoice2); assertEquals(invoice2.getNumberOfItems(), 1); assertEquals(invoice2.getBalance(), KillBillMoney.of(FIVE, invoice2.getCurrency())); }
@Test(groups = "fast") public void testWithSingleMonthlyEvent() throws InvoiceApiException, CatalogApiException { final BillingEventSet events = new MockBillingEventSet(); final SubscriptionBase sub = createSubscription(); final LocalDate startDate = invoiceUtil.buildDate(2011, 9, 1); final Plan plan = new MockPlan(); final BigDecimal rate1 = TEN; final PlanPhase phase = createMockMonthlyPlanPhase(rate1); final BillingEvent event = createBillingEvent(sub.getId(), sub.getBundleId(), startDate, plan, phase, 1); events.add(event); final LocalDate targetDate = invoiceUtil.buildDate(2011, 10, 3); final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, null, targetDate, Currency.USD, internalCallContext); final Invoice invoice = invoiceWithMetadata.getInvoice(); assertNotNull(invoice); assertEquals(invoice.getNumberOfItems(), 2); assertEquals(invoice.getBalance(), KillBillMoney.of(TWENTY, invoice.getCurrency())); assertEquals(invoice.getInvoiceItems().get(0).getSubscriptionId(), sub.getId()); assertEquals(invoice.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.RECURRING); assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), new LocalDate(2011, 9, 1)); assertEquals(invoice.getInvoiceItems().get(0).getEndDate(), new LocalDate(2011, 10, 1)); assertEquals(invoice.getInvoiceItems().get(1).getInvoiceItemType(), InvoiceItemType.RECURRING); assertEquals(invoice.getInvoiceItems().get(1).getStartDate(), new LocalDate(2011, 10, 1)); assertEquals(invoice.getInvoiceItems().get(1).getEndDate(), new LocalDate(2011, 11, 1)); }
assertEquals(childInvoice.getNumberOfItems(), 2); assertEquals(childInvoice.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.EXTERNAL_CHARGE); assertEquals(childInvoice.getInvoiceItems().get(0).getAmount().compareTo(BigDecimal.valueOf(250)), 0); assertEquals(parentInvoice.getNumberOfItems(), 2); assertEquals(parentInvoice.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.CREDIT_ADJ); assertEquals(parentInvoice.getInvoiceItems().get(0).getAmount().compareTo(BigDecimal.valueOf(-250)), 0);
assertEquals(invoice1.getNumberOfItems(), 1); assertEquals(invoice1.getBalance(), KillBillMoney.of(fixedCost1, invoice1.getCurrency())); assertEquals(invoice2.getNumberOfItems(), 1); assertEquals(invoice2.getBalance(), KillBillMoney.of(fixedCost2, invoice2.getCurrency()));
@Test(groups = "fast") public void testWithSingleThirtyDaysEvent() throws InvoiceApiException, CatalogApiException { final BillingEventSet events = new MockBillingEventSet(); final SubscriptionBase sub = createSubscription(); final LocalDate startDate = invoiceUtil.buildDate(2011, 9, 1); final Plan plan = new MockPlan(); final BigDecimal rate1 = TEN; final PlanPhase phase = createMockThirtyDaysPlanPhase(rate1); final BillingEvent event = createBillingEvent(sub.getId(), sub.getBundleId(), startDate, plan, phase, 1); events.add(event); final LocalDate targetDate = invoiceUtil.buildDate(2011, 10, 3); final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, null, targetDate, Currency.USD, internalCallContext); final Invoice invoice = invoiceWithMetadata.getInvoice(); assertNotNull(invoice); assertEquals(invoice.getNumberOfItems(), 2); assertEquals(invoice.getBalance(), KillBillMoney.of(TWENTY, invoice.getCurrency())); assertEquals(invoice.getInvoiceItems().get(0).getSubscriptionId(), sub.getId()); assertEquals(invoice.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.RECURRING); assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), new LocalDate(2011, 9, 1)); assertEquals(invoice.getInvoiceItems().get(0).getEndDate(), new LocalDate(2011, 10, 1)); assertEquals(invoice.getInvoiceItems().get(1).getInvoiceItemType(), InvoiceItemType.RECURRING); assertEquals(invoice.getInvoiceItems().get(1).getStartDate(), new LocalDate(2011, 10, 1)); assertEquals(invoice.getInvoiceItems().get(1).getEndDate(), new LocalDate(2011, 10, 31)); }
@Test(groups = "fast") public void testSimpleWithSingleDiscountEvent() throws Exception { final UUID accountId = UUID.randomUUID(); final SubscriptionBase sub = createSubscription(); final Plan plan = new MockPlan("Plan with a single discount phase"); final PlanPhase phaseEvergreen = createMockMonthlyPlanPhase(EIGHT, PhaseType.DISCOUNT); final int bcdLocal = 16; final LocalDate startDate = invoiceUtil.buildDate(2012, 7, 16); final BillingEventSet events = new MockBillingEventSet(); events.add(createBillingEvent(sub.getId(), sub.getBundleId(), startDate, plan, phaseEvergreen, bcdLocal)); // Set a target date of today (start date) final LocalDate targetDate = startDate; final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, null, targetDate, Currency.USD, internalCallContext); final Invoice invoice = invoiceWithMetadata.getInvoice(); assertNotNull(invoice); assertEquals(invoice.getNumberOfItems(), 1); assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), invoiceUtil.buildDate(2012, 7, 16)); assertEquals(invoice.getInvoiceItems().get(0).getEndDate(), invoiceUtil.buildDate(2012, 8, 16)); }
final Invoice invoice1 = invoiceWithMetadata1.getInvoice(); assertNotNull(invoice1); assertEquals(invoice1.getNumberOfItems(), 1); assertEquals(invoice1.getBalance().compareTo(ZERO), 0); final Invoice invoice2 = invoiceWithMetadata2.getInvoice(); assertNotNull(invoice2); assertEquals(invoice2.getNumberOfItems(), 1); assertEquals(invoice2.getBalance().compareTo(cheapAmount), 0); final Invoice invoice3 = invoiceWithMetadata3.getInvoice(); assertNotNull(invoice3); assertEquals(invoice3.getNumberOfItems(), 1); assertEquals(invoice3.getBalance().compareTo(cheapAmount), 0);
assertEquals(childInvoice.getNumberOfItems(), 1); assertEquals(childInvoice.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.RECURRING); assertEquals(childInvoice.getInvoiceItems().get(0).getAmount().compareTo(BigDecimal.valueOf(29.95)), 0); assertEquals(parentInvoice.getNumberOfItems(), 1); assertEquals(parentInvoice.getStatus(), InvoiceStatus.COMMITTED); assertTrue(parentInvoice.isParentInvoice());
@Test(groups = "fast") public void testWithSingleMonthlyEventWithLeadingProRation() throws InvoiceApiException, CatalogApiException { final BillingEventSet events = new MockBillingEventSet(); final SubscriptionBase sub = createSubscription(); final LocalDate startDate = invoiceUtil.buildDate(2011, 9, 1); final Plan plan = new MockPlan(); final BigDecimal rate = TEN; final PlanPhase phase = createMockMonthlyPlanPhase(rate); final BillingEvent event = createBillingEvent(sub.getId(), sub.getBundleId(), startDate, plan, phase, 15); events.add(event); final LocalDate targetDate = invoiceUtil.buildDate(2011, 10, 3); final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, null, targetDate, Currency.USD, internalCallContext); final Invoice invoice = invoiceWithMetadata.getInvoice(); assertNotNull(invoice); assertEquals(invoice.getNumberOfItems(), 2); final BigDecimal expectedNumberOfBillingCycles; expectedNumberOfBillingCycles = ONE.add(FOURTEEN.divide(THIRTY_ONE, KillBillMoney.ROUNDING_METHOD)); final BigDecimal expectedAmount = KillBillMoney.of(expectedNumberOfBillingCycles.multiply(rate), invoice.getCurrency()); assertEquals(invoice.getBalance(), expectedAmount); }
final Invoice invoice1 = invoiceWithMetadata1.getInvoice(); assertNotNull(invoice1); assertEquals(invoice1.getNumberOfItems(), 1); final Invoice invoice2 = invoiceWithMetadata2.getInvoice(); assertNotNull(invoice2); assertEquals(invoice2.getNumberOfItems(), 1); final FixedPriceInvoiceItem item = (FixedPriceInvoiceItem) invoice2.getInvoiceItems().get(0); assertEquals(item.getStartDate(), changeDate.toLocalDate());
assertEquals(invoice.getNumberOfItems(), 1); assertEquals(invoice.getBalance(), KillBillMoney.of(rate, invoice.getCurrency())); assertEquals(invoice.getInvoiceItems().get(0).getSubscriptionId(), sub.getId());
@Test(groups = "fast") public void testSimpleWithTimeZone() throws InvoiceApiException, CatalogApiException { final SubscriptionBase sub = createSubscription(); final Plan plan = new MockPlan(); final BigDecimal rate = TEN; final PlanPhase phase = createMockMonthlyPlanPhase(rate); // Start date was the 16 local, but was the 17 UTC final int bcdLocal = 16; final LocalDate startDate = invoiceUtil.buildDate(2012, 7, bcdLocal); final BillingEventSet events = new MockBillingEventSet(); final BillingEvent event = createBillingEvent(sub.getId(), sub.getBundleId(), startDate, plan, phase, bcdLocal); events.add(event); // Target date is the next BCD, in local time final LocalDate targetDate = invoiceUtil.buildDate(2012, 8, bcdLocal); final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, null, targetDate, Currency.USD, internalCallContext); final Invoice invoice = invoiceWithMetadata.getInvoice(); assertNotNull(invoice); assertEquals(invoice.getNumberOfItems(), 2); assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), invoiceUtil.buildDate(2012, 7, 16)); assertEquals(invoice.getInvoiceItems().get(0).getEndDate(), invoiceUtil.buildDate(2012, 8, 16)); assertEquals(invoice.getInvoiceItems().get(1).getStartDate(), invoiceUtil.buildDate(2012, 8, 16)); assertEquals(invoice.getInvoiceItems().get(1).getEndDate(), invoiceUtil.buildDate(2012, 9, 16)); }
@Test(groups = "fast") public void testOnePlan_TwoMonthlyPhases_ChangeImmediate() throws InvoiceApiException, CatalogApiException { final BillingEventSet events = new MockBillingEventSet(); final Plan plan1 = new MockPlan(); final BigDecimal rate1 = FIVE; final PlanPhase phase1 = createMockMonthlyPlanPhase(rate1); final SubscriptionBase sub = createSubscription(); final BillingEvent event1 = createBillingEvent(sub.getId(), sub.getBundleId(), invoiceUtil.buildDate(2011, 9, 1), plan1, phase1, 1); events.add(event1); final BigDecimal rate2 = TEN; final PlanPhase phase2 = createMockMonthlyPlanPhase(rate2); final BillingEvent event2 = createBillingEvent(sub.getId(), sub.getBundleId(), invoiceUtil.buildDate(2011, 10, 15), plan1, phase2, 15); events.add(event2); final LocalDate targetDate = invoiceUtil.buildDate(2011, 12, 3); final UUID accountId = UUID.randomUUID(); final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, null, targetDate, Currency.USD, internalCallContext); final Invoice invoice = invoiceWithMetadata.getInvoice(); assertNotNull(invoice); assertEquals(invoice.getNumberOfItems(), 4); final BigDecimal numberOfCyclesEvent1; numberOfCyclesEvent1 = ONE.add(FOURTEEN.divide(THIRTY_ONE, KillBillMoney.ROUNDING_METHOD)); final BigDecimal numberOfCyclesEvent2 = TWO; BigDecimal expectedValue; expectedValue = numberOfCyclesEvent1.multiply(rate1); expectedValue = expectedValue.add(numberOfCyclesEvent2.multiply(rate2)); expectedValue = KillBillMoney.of(expectedValue, invoice.getCurrency()); assertEquals(invoice.getBalance(), expectedValue); }
final Invoice invoice = invoiceWithMetadata.getInvoice(); assertNotNull(invoice); assertEquals(invoice.getNumberOfItems(), 2); assertEquals(invoice.getBalance().compareTo(cheapAmount), 0);
assertEquals(invoice1.getNumberOfItems(), 1); final List<Invoice> invoices = new ArrayList<Invoice>(); invoices.add(invoice1); assertEquals(invoice2.getNumberOfItems(), 2); invoices.add(invoice2);
@Test(groups = "fast") public void testTwoMonthlySubscriptionsWithAlignedBillingDates() throws InvoiceApiException, CatalogApiException { final BillingEventSet events = new MockBillingEventSet(); final Plan plan1 = new MockPlan(); final BigDecimal rate1 = FIVE; final PlanPhase phase1 = createMockMonthlyPlanPhase(rate1); final Plan plan2 = new MockPlan(); final BigDecimal rate2 = TEN; final PlanPhase phase2 = createMockMonthlyPlanPhase(rate2); final SubscriptionBase sub = createSubscription(); final BillingEvent event1 = createBillingEvent(sub.getId(), sub.getBundleId(), invoiceUtil.buildDate(2011, 9, 1), plan1, phase1, 1); events.add(event1); final BillingEvent event2 = createBillingEvent(sub.getId(), sub.getBundleId(), invoiceUtil.buildDate(2011, 10, 1), plan2, phase2, 1); events.add(event2); final LocalDate targetDate = invoiceUtil.buildDate(2011, 10, 3); final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, null, targetDate, Currency.USD, internalCallContext); final Invoice invoice = invoiceWithMetadata.getInvoice(); assertNotNull(invoice); assertEquals(invoice.getNumberOfItems(), 2); assertEquals(invoice.getBalance(), KillBillMoney.of(rate1.add(rate2), invoice.getCurrency())); }
@Test(groups = "fast") public void testOnePlan_ThreeMonthlyPhases_ChangeEOT() throws InvoiceApiException, CatalogApiException { final BillingEventSet events = new MockBillingEventSet(); final Plan plan1 = new MockPlan(); final BigDecimal rate1 = FIVE; final PlanPhase phase1 = createMockMonthlyPlanPhase(rate1); final SubscriptionBase sub = createSubscription(); final BillingEvent event1 = createBillingEvent(sub.getId(), sub.getBundleId(), invoiceUtil.buildDate(2011, 9, 1), plan1, phase1, 1); events.add(event1); final BigDecimal rate2 = TEN; final PlanPhase phase2 = createMockMonthlyPlanPhase(rate2); final BillingEvent event2 = createBillingEvent(sub.getId(), sub.getBundleId(), invoiceUtil.buildDate(2011, 10, 1), plan1, phase2, 1); events.add(event2); final BigDecimal rate3 = THIRTY; final PlanPhase phase3 = createMockMonthlyPlanPhase(rate3); final BillingEvent event3 = createBillingEvent(sub.getId(), sub.getBundleId(), invoiceUtil.buildDate(2011, 11, 1), plan1, phase3, 1); events.add(event3); final LocalDate targetDate = invoiceUtil.buildDate(2011, 12, 3); final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, null, targetDate, Currency.USD, internalCallContext); final Invoice invoice = invoiceWithMetadata.getInvoice(); assertNotNull(invoice); assertEquals(invoice.getNumberOfItems(), 4); assertEquals(invoice.getBalance(), KillBillMoney.of(rate1.add(rate2).add(TWO.multiply(rate3)), invoice.getCurrency())); }