use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices in project killbill by killbill.
the class TestInvoiceDao method testRefundedInvoiceWithInvoiceItemAdjustmentWithRepair.
@Test(groups = "slow")
public void testRefundedInvoiceWithInvoiceItemAdjustmentWithRepair() throws InvoiceApiException, EntityPersistenceException {
final UUID accountId = account.getId();
final UUID subscriptionId = UUID.randomUUID();
final UUID bundleId = UUID.randomUUID();
final LocalDate startDate = new LocalDate(2010, 1, 1);
((ClockMock) clock).setDay(startDate);
final LocalDate recuringStartDate = clock.getUTCNow().plusDays(30).toLocalDate();
final LocalDate recuringEndDate = recuringStartDate.plusMonths(1);
final LocalDate targetDate = recuringStartDate.plusDays(1);
// FIRST CREATE INITIAL INVOICE WITH ONE RECURRING ITEM
final Invoice invoice = new DefaultInvoice(accountId, targetDate, targetDate, Currency.USD);
final UUID invoiceId = invoice.getId();
final InvoiceItem invoiceItem = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, "test product", "test-plan", "test-phase-rec", null, recuringStartDate, recuringEndDate, new BigDecimal("239.00"), new BigDecimal("239.00"), Currency.USD);
invoice.addInvoiceItem(invoiceItem);
invoiceUtil.createInvoice(invoice, context);
((ClockMock) clock).addDays(1);
// SECOND CREATE THE PAYMENT
final BigDecimal paymentAmount = new BigDecimal("239.00");
final UUID paymentId = UUID.randomUUID();
final DefaultInvoicePayment defaultInvoicePayment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, paymentId, invoiceId, clock.getUTCNow(), paymentAmount, Currency.USD, Currency.USD, "cookie", true);
invoiceDao.notifyOfPaymentCompletion(new InvoicePaymentModelDao(defaultInvoicePayment), UUID.randomUUID(), context);
// AND THEN THIRD THE REFUND
final Map<UUID, BigDecimal> invoiceItemMap = new HashMap<UUID, BigDecimal>();
invoiceItemMap.put(invoiceItem.getId(), new BigDecimal("239.00"));
invoiceDao.createRefund(paymentId, UUID.randomUUID(), paymentAmount, true, invoiceItemMap, UUID.randomUUID().toString(), true, context);
final InvoiceModelDao savedInvoice = invoiceDao.getById(invoiceId, context);
assertNotNull(savedInvoice);
assertEquals(savedInvoice.getInvoiceItems().size(), 2);
final List<Invoice> invoices = new ArrayList<Invoice>();
invoices.add(new DefaultInvoice(savedInvoice));
// NOW COMPUTE A DIFFERENT ITEM TO TRIGGER REPAIR
final BillingEventSet events = new MockBillingEventSet();
final SubscriptionBase subscription = getZombieSubscription(subscriptionId);
final Product product = Mockito.mock(Product.class);
Mockito.when(product.getName()).thenReturn("product");
final Plan plan = Mockito.mock(Plan.class);
Mockito.when(plan.getName()).thenReturn("plan");
Mockito.when(plan.getProduct()).thenReturn(product);
Mockito.when(plan.getRecurringBillingMode()).thenReturn(BillingMode.IN_ADVANCE);
final PlanPhase phase1 = Mockito.mock(PlanPhase.class);
Mockito.when(phase1.getName()).thenReturn("plan-phase1");
final BillingEvent event1 = invoiceUtil.createMockBillingEvent(null, subscription, recuringStartDate.toDateTimeAtStartOfDay(), plan, phase1, null, TEN, Currency.USD, BillingPeriod.MONTHLY, 31, BillingMode.IN_ADVANCE, "new-event", 1L, SubscriptionBaseTransitionType.CREATE);
events.add(event1);
final InvoiceWithMetadata newInvoiceWithMetadata = generator.generateInvoice(account, events, new AccountInvoices(null, null, invoices), null, targetDate, Currency.USD, null, context);
final Invoice newInvoice = newInvoiceWithMetadata.getInvoice();
invoiceUtil.createInvoice(newInvoice, context);
// VERIFY THAT WE STILL HAVE ONLY 2 ITEMS, MEANING THERE WERE NO REPAIR AND NO CBA GENERATED
final Invoice firstInvoice = new DefaultInvoice(invoiceDao.getById(invoiceId, context));
assertNotNull(firstInvoice);
assertEquals(firstInvoice.getInvoiceItems().size(), 2);
}
use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices in project killbill by killbill.
the class TestFixedAndRecurringInvoiceItemGenerator method testOverlappingItemsWithTooManyRepairs.
// Also a case we REPAIR amount does not match the dates and the current logic is to assert on the dates
@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testOverlappingItemsWithTooManyRepairs() throws InvoiceApiException {
final LocalDate startDate = new LocalDate("2016-01-01");
final BillingEventSet events = new MockBillingEventSet();
final BigDecimal amount = BigDecimal.TEN;
final MockInternationalPrice price = new MockInternationalPrice(new DefaultPrice(amount, account.getCurrency()));
final Plan plan = new MockPlan("my-plan");
final PlanPhase planPhase = new MockPlanPhase(price, null, BillingPeriod.MONTHLY, PhaseType.EVERGREEN);
final BillingEvent event = invoiceUtil.createMockBillingEvent(account, subscription, startDate.toDateTimeAtStartOfDay(), plan, planPhase, null, amount, account.getCurrency(), BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 1L, SubscriptionBaseTransitionType.CREATE);
events.add(event);
// Simulate a previous mis-bill: existing item is for [2016-01-01,2016-01-30], proposed will be for [2016-01-01,2016-02-01]
final List<Invoice> existingInvoices = new LinkedList<Invoice>();
final Invoice invoice = new DefaultInvoice(account.getId(), clock.getUTCToday(), startDate, account.getCurrency());
invoice.addInvoiceItem(new RecurringInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), null, event.getPlan().getName(), event.getPlanPhase().getName(), null, startDate, startDate.plusDays(29), amount, amount, account.getCurrency()));
// But the system has already repaired it
invoice.addInvoiceItem(new RepairAdjInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), startDate, startDate.plusDays(29), // Note! The amount will not matter
amount.negate(), account.getCurrency(), invoice.getInvoiceItems().get(0).getId()));
// Twice!
invoice.addInvoiceItem(new RepairAdjInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), startDate, startDate.plusDays(29), // Note! The amount will not matter
BigDecimal.ONE.negate(), account.getCurrency(), invoice.getInvoiceItems().get(0).getId()));
existingInvoices.add(invoice);
try {
fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, new AccountInvoices(null, null, existingInvoices), startDate, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), null, internalCallContext).getItems();
fail();
} catch (final InvoiceApiException e) {
assertEquals(e.getCode(), ErrorCode.UNEXPECTED_ERROR.getCode());
assertTrue(e.getCause().getMessage().startsWith("Too many repairs"));
}
}
use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices in project killbill by killbill.
the class TestDefaultInvoiceGenerator method testAutoInvoiceOffWithCredits.
@Test(groups = "fast")
public void testAutoInvoiceOffWithCredits() throws CatalogApiException, InvoiceApiException {
final Currency currency = Currency.USD;
final List<Invoice> invoices = new ArrayList<Invoice>();
final MockBillingEventSet eventSet = new MockBillingEventSet();
final UUID accountId = UUID.randomUUID();
final UUID bundleId = UUID.randomUUID();
final LocalDate startDate = new LocalDate(2012, 1, 1);
// add first SubscriptionBase creation event
final UUID subscriptionId1 = UUID.randomUUID();
final Plan plan1 = new MockPlan();
final PlanPhase plan1phase1 = createMockMonthlyPlanPhase(FIFTEEN, null, PhaseType.DISCOUNT);
final BillingEvent subscription1creation = createBillingEvent(subscriptionId1, bundleId, startDate, plan1, plan1phase1, 1);
eventSet.add(subscription1creation);
// add second SubscriptionBase creation event
final UUID subscriptionId2 = UUID.randomUUID();
final Plan plan2 = new MockPlan();
final PlanPhase plan2phase1 = createMockMonthlyPlanPhase(TWELVE, null, PhaseType.EVERGREEN);
eventSet.add(createBillingEvent(subscriptionId2, bundleId, startDate, plan2, plan2phase1, 1));
// generate the first invoice
final InvoiceWithMetadata invoiceWithMetadata1 = generator.generateInvoice(account, eventSet, new AccountInvoices(null, null, invoices), null, startDate, currency, null, internalCallContext);
final Invoice invoice1 = invoiceWithMetadata1.getInvoice();
assertNotNull(invoice1);
assertTrue(invoice1.getBalance().compareTo(FIFTEEN.add(TWELVE)) == 0);
invoices.add(invoice1);
// set auto invoice off for first SubscriptionBase (i.e. remove event from BillingEventSet and add SubscriptionBase id to the list
// generate invoice
eventSet.remove(subscription1creation);
eventSet.addSubscriptionWithAutoInvoiceOff(subscriptionId1);
final LocalDate targetDate2 = startDate.plusMonths(1);
final InvoiceWithMetadata invoiceWithMetadata2 = generator.generateInvoice(account, eventSet, new AccountInvoices(null, null, invoices), null, targetDate2, currency, null, internalCallContext);
final Invoice invoice2 = invoiceWithMetadata2.getInvoice();
assertNotNull(invoice2);
assertTrue(invoice2.getBalance().compareTo(TWELVE) == 0);
invoices.add(invoice2);
final LocalDate targetDate3 = targetDate2.plusMonths(1);
eventSet.clearSubscriptionsWithAutoInvoiceOff();
eventSet.add(subscription1creation);
final InvoiceWithMetadata invoiceWithMetadata3 = generator.generateInvoice(account, eventSet, new AccountInvoices(null, null, invoices), null, targetDate3, currency, null, internalCallContext);
final Invoice invoice3 = invoiceWithMetadata3.getInvoice();
assertNotNull(invoice3);
assertTrue(invoice3.getBalance().compareTo(FIFTEEN.multiply(TWO).add(TWELVE)) == 0);
}
use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices in project killbill by killbill.
the class TestDefaultInvoiceGenerator method testSingleEventWithExistingInvoice.
@Test(groups = "fast")
public void testSingleEventWithExistingInvoice() throws InvoiceApiException, CatalogApiException {
final BillingEventSet events = new MockBillingEventSet();
final SubscriptionBase sub = createSubscription();
final LocalDate startDate = invoiceUtil.buildDate(2011, 9, 1);
final Plan plan1 = new MockPlan();
final BigDecimal rate = FIVE;
final PlanPhase phase1 = createMockMonthlyPlanPhase(rate);
final BillingEvent event1 = createBillingEvent(sub.getId(), sub.getBundleId(), startDate, plan1, phase1, 1);
events.add(event1);
LocalDate targetDate = invoiceUtil.buildDate(2011, 12, 1);
final InvoiceWithMetadata invoiceWithMetadata1 = generator.generateInvoice(account, events, new AccountInvoices(), null, targetDate, Currency.USD, null, internalCallContext);
final List<Invoice> existingInvoices = new ArrayList<Invoice>();
final Invoice invoice1 = invoiceWithMetadata1.getInvoice();
existingInvoices.add(invoice1);
targetDate = invoiceUtil.buildDate(2011, 12, 3);
final InvoiceWithMetadata invoiceWithMetadata2 = generator.generateInvoice(account, events, new AccountInvoices(null, null, existingInvoices), null, targetDate, Currency.USD, null, internalCallContext);
final Invoice invoice2 = invoiceWithMetadata2.getInvoice();
assertNull(invoice2);
}
use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices in project killbill by killbill.
the class TestDefaultInvoiceGenerator method testWithSingleMonthlyEventWithLeadingProRation.
@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, new AccountInvoices(), null, targetDate, Currency.USD, null, 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);
}
Aggregations