use of org.killbill.billing.junction.BillingEvent 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-plan", "test-phase-rec", 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), 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, paymentAmount, true, invoiceItemMap, UUID.randomUUID().toString(), 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 Plan plan = Mockito.mock(Plan.class);
Mockito.when(plan.getName()).thenReturn("plan");
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, invoices, targetDate, Currency.USD, 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.junction.BillingEvent in project killbill by killbill.
the class TestFixedAndRecurringInvoiceItemGenerator method testOverlappingItemsWithRepair.
@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testOverlappingItemsWithRepair() 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(), event.getPlan().getName(), event.getPlanPhase().getName(), 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
BigDecimal.ONE.negate(), account.getCurrency(), invoice.getInvoiceItems().get(0).getId()));
existingInvoices.add(invoice);
// We will generate the correct recurring item
final List<InvoiceItem> generatedItems = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices, startDate, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), internalCallContext);
assertEquals(generatedItems.size(), 1);
assertTrue(generatedItems.get(0) instanceof RecurringInvoiceItem);
assertEquals(generatedItems.get(0).getStartDate(), new LocalDate("2016-01-01"));
assertEquals(generatedItems.get(0).getEndDate(), new LocalDate("2016-02-01"));
assertEquals(generatedItems.get(0).getAmount().compareTo(amount), 0);
}
use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.
the class TestFixedAndRecurringInvoiceItemGenerator method testItemPartiallyRepairedAndInvalidAdjustment.
@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testItemPartiallyRepairedAndInvalidAdjustment() 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 event1 = 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(event1);
final BillingEvent event2 = invoiceUtil.createMockBillingEvent(account, subscription, startDate.plusDays(1).toDateTimeAtStartOfDay(), plan, planPhase, null, null, Currency.USD, BillingPeriod.NO_BILLING_PERIOD, 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 2L, SubscriptionBaseTransitionType.CANCEL);
events.add(event2);
// Subscription incorrectly invoiced
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(), plan.getName(), planPhase.getName(), startDate, startDate.plusMonths(1), amount, amount, account.getCurrency()));
// Repaired by the system
invoice.addInvoiceItem(new RepairAdjInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), startDate.plusDays(1), startDate.plusMonths(1), new BigDecimal("9.68").negate(), account.getCurrency(), invoice.getInvoiceItems().get(0).getId()));
// Invalid adjustment (too much)
invoice.addInvoiceItem(new ItemAdjInvoiceItem(invoice.getInvoiceItems().get(0), startDate, new BigDecimal("9.68").negate(), account.getCurrency()));
existingInvoices.add(invoice);
try {
final List<InvoiceItem> generatedItems = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices, startDate, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), internalCallContext);
fail();
} catch (final InvoiceApiException e) {
assertEquals(e.getCode(), ErrorCode.UNEXPECTED_ERROR.getCode());
assertTrue(e.getCause().getMessage().endsWith("overly repaired"));
}
}
use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.
the class TestFixedAndRecurringInvoiceItemGenerator method testProcessFixedBillingEventsWithCancellationOnSameDay.
@Test(groups = "fast")
public void testProcessFixedBillingEventsWithCancellationOnSameDay() throws InvoiceApiException {
final LocalDate targetDate = new LocalDate("2016-01-08");
final UUID invoiceId = UUID.randomUUID();
final BillingEventSet events = new MockBillingEventSet();
final BigDecimal fixedPriceAmount = BigDecimal.TEN;
final MockInternationalPrice fixedPrice = new MockInternationalPrice(new DefaultPrice(fixedPriceAmount, Currency.USD));
final Plan plan = new MockPlan("my-plan");
final PlanPhase phase = new MockPlanPhase(null, fixedPrice, BillingPeriod.NO_BILLING_PERIOD, PhaseType.TRIAL);
final BillingEvent event1 = invoiceUtil.createMockBillingEvent(account, subscription, new DateTime("2016-01-08"), plan, phase, fixedPriceAmount, null, Currency.USD, BillingPeriod.NO_BILLING_PERIOD, 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 1L, SubscriptionBaseTransitionType.CREATE);
events.add(event1);
final BillingEvent event2 = invoiceUtil.createMockBillingEvent(account, subscription, new DateTime("2016-01-08"), plan, phase, null, null, Currency.USD, BillingPeriod.NO_BILLING_PERIOD, 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 2L, SubscriptionBaseTransitionType.CANCEL);
events.add(event2);
final List<InvoiceItem> proposedItems = new ArrayList<InvoiceItem>();
fixedAndRecurringInvoiceItemGenerator.processFixedBillingEvents(invoiceId, account.getId(), events, targetDate, Currency.USD, proposedItems, internalCallContext);
assertTrue(proposedItems.isEmpty());
}
use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.
the class TestFixedAndRecurringInvoiceItemGenerator method testProcessFixedBillingEventsWithMultipleChangeOnSameDay.
@Test(groups = "fast")
public void testProcessFixedBillingEventsWithMultipleChangeOnSameDay() throws InvoiceApiException {
final LocalDate targetDate = new LocalDate("2016-01-08");
final UUID invoiceId = UUID.randomUUID();
final BillingEventSet events = new MockBillingEventSet();
final BigDecimal fixedPriceAmount1 = BigDecimal.TEN;
final MockInternationalPrice fixedPrice1 = new MockInternationalPrice(new DefaultPrice(fixedPriceAmount1, Currency.USD));
final Plan plan1 = new MockPlan("my-plan1");
final PlanPhase planPhase1 = new MockPlanPhase(null, fixedPrice1, BillingPeriod.NO_BILLING_PERIOD, PhaseType.TRIAL);
final BillingEvent event1 = invoiceUtil.createMockBillingEvent(account, subscription, new DateTime("2016-01-08"), plan1, planPhase1, fixedPriceAmount1, null, Currency.USD, BillingPeriod.NO_BILLING_PERIOD, 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 1L, SubscriptionBaseTransitionType.CREATE);
events.add(event1);
final BigDecimal fixedPriceAmount2 = null;
final MockInternationalPrice fixedPrice2 = new MockInternationalPrice(new DefaultPrice(fixedPriceAmount2, Currency.USD));
final Plan plan2 = new MockPlan("my-plan2");
final PlanPhase planPhase2 = new MockPlanPhase(null, fixedPrice2, BillingPeriod.NO_BILLING_PERIOD, PhaseType.TRIAL);
final BillingEvent event2 = invoiceUtil.createMockBillingEvent(account, subscription, new DateTime("2016-01-08"), plan2, planPhase2, fixedPriceAmount2, null, Currency.USD, BillingPeriod.NO_BILLING_PERIOD, 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 2L, SubscriptionBaseTransitionType.CHANGE);
events.add(event2);
final BigDecimal fixedPriceAmount3 = BigDecimal.ONE;
final MockInternationalPrice fixedPrice3 = new MockInternationalPrice(new DefaultPrice(fixedPriceAmount3, Currency.USD));
final Plan plan3 = new MockPlan("my-plan3");
final PlanPhase planPhase3 = new MockPlanPhase(null, fixedPrice3, BillingPeriod.NO_BILLING_PERIOD, PhaseType.TRIAL);
final BillingEvent event3 = invoiceUtil.createMockBillingEvent(account, subscription, new DateTime("2016-01-08"), plan3, planPhase3, fixedPriceAmount3, null, Currency.USD, BillingPeriod.NO_BILLING_PERIOD, 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 3L, SubscriptionBaseTransitionType.CHANGE);
events.add(event3);
final List<InvoiceItem> proposedItems = new ArrayList<InvoiceItem>();
fixedAndRecurringInvoiceItemGenerator.processFixedBillingEvents(invoiceId, account.getId(), events, targetDate, Currency.USD, proposedItems, internalCallContext);
assertEquals(proposedItems.size(), 1);
assertEquals(proposedItems.get(0).getInvoiceItemType(), InvoiceItemType.FIXED);
assertEquals(proposedItems.get(0).getAmount().compareTo(fixedPriceAmount3), 0);
}
Aggregations