Search in sources :

Example 26 with RecurringInvoiceItem

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

the class TestInvoiceDao method testWithFailedPaymentAttempt.

@Test(groups = "slow")
public void testWithFailedPaymentAttempt() throws Exception {
    final UUID accountId = account.getId();
    final Invoice invoice = new DefaultInvoice(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
    invoiceUtil.createInvoice(invoice, context);
    final UUID bundleId = UUID.randomUUID();
    final UUID subscriptionId = UUID.randomUUID();
    final RecurringInvoiceItem item1 = new RecurringInvoiceItem(invoice.getId(), accountId, bundleId, subscriptionId, "test product", "test plan", "test ZOO", null, clock.getUTCNow().plusMonths(-1).toLocalDate(), clock.getUTCNow().toLocalDate(), BigDecimal.TEN, BigDecimal.TEN, Currency.USD);
    invoiceUtil.createInvoiceItem(item1, context);
    final InvoiceModelDao retrievedInvoice = invoiceDao.getById(invoice.getId(), context);
    assertEquals(retrievedInvoice.getInvoicePayments().size(), 0);
    final UUID paymentId = UUID.randomUUID();
    final DefaultInvoicePayment defaultInvoicePayment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, paymentId, invoice.getId(), clock.getUTCNow().plusDays(12), BigDecimal.TEN, Currency.USD, Currency.USD, "cookie", false);
    invoiceDao.notifyOfPaymentCompletion(new InvoicePaymentModelDao(defaultInvoicePayment), UUID.randomUUID(), context);
    final InvoiceModelDao retrievedInvoice1 = invoiceDao.getById(invoice.getId(), context);
    assertEquals(retrievedInvoice1.getInvoicePayments().size(), 1);
    assertEquals(retrievedInvoice1.getInvoicePayments().get(0).getSuccess(), Boolean.FALSE);
    final DefaultInvoicePayment defaultInvoicePayment2 = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, paymentId, invoice.getId(), clock.getUTCNow().plusDays(12), BigDecimal.TEN, Currency.USD, Currency.USD, "cookie", true);
    invoiceDao.notifyOfPaymentCompletion(new InvoicePaymentModelDao(defaultInvoicePayment2), UUID.randomUUID(), context);
    final InvoiceModelDao retrievedInvoice2 = invoiceDao.getById(invoice.getId(), context);
    assertEquals(retrievedInvoice2.getInvoicePayments().size(), 1);
    assertEquals(retrievedInvoice2.getInvoicePayments().get(0).getSuccess(), Boolean.TRUE);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) Test(org.testng.annotations.Test)

Example 27 with RecurringInvoiceItem

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

the class TestInvoiceDao method testGetUnpaidInvoicesByAccountIdWithDraftInvoice.

@Test(groups = "slow")
public void testGetUnpaidInvoicesByAccountIdWithDraftInvoice() throws EntityPersistenceException, InvoiceApiException {
    final UUID accountId = account.getId();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate targetDate1 = new LocalDate(2011, 10, 6);
    final Invoice invoice1 = new DefaultInvoice(accountId, clock.getUTCToday(), targetDate1, Currency.USD);
    invoiceUtil.createInvoice(invoice1, context);
    final LocalDate startDate = new LocalDate(2011, 3, 1);
    final LocalDate endDate = startDate.plusMonths(1);
    final BigDecimal rate1 = new BigDecimal("17.0");
    final BigDecimal rate2 = new BigDecimal("42.0");
    final RecurringInvoiceItem item1 = new RecurringInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test product", "test plan", "test phase A", null, startDate, endDate, rate1, rate1, Currency.USD);
    invoiceUtil.createInvoiceItem(item1, context);
    final RecurringInvoiceItem item2 = new RecurringInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test product", "test plan", "test phase B", null, startDate, endDate, rate2, rate2, Currency.USD);
    invoiceUtil.createInvoiceItem(item2, context);
    LocalDate upToDate;
    Collection<InvoiceModelDao> invoices;
    upToDate = new LocalDate(2011, 1, 1);
    invoices = invoiceDao.getUnpaidInvoicesByAccountId(accountId, null, upToDate, context);
    assertEquals(invoices.size(), 0);
    upToDate = new LocalDate(2012, 1, 1);
    invoices = invoiceDao.getUnpaidInvoicesByAccountId(accountId, null, upToDate, context);
    assertEquals(invoices.size(), 1);
    List<InvoiceModelDao> allInvoicesByAccount = invoiceDao.getInvoicesByAccount(false, new LocalDate(2011, 1, 1), null, context);
    assertEquals(allInvoicesByAccount.size(), 1);
    // insert DRAFT invoice
    createCredit(accountId, new LocalDate(2011, 12, 31), BigDecimal.TEN, true);
    allInvoicesByAccount = invoiceDao.getInvoicesByAccount(false, new LocalDate(2011, 1, 1), null, context);
    assertEquals(allInvoicesByAccount.size(), 2);
    assertEquals(allInvoicesByAccount.get(0).getStatus(), InvoiceStatus.COMMITTED);
    assertEquals(allInvoicesByAccount.get(1).getStatus(), InvoiceStatus.DRAFT);
    upToDate = new LocalDate(2012, 1, 1);
    invoices = invoiceDao.getUnpaidInvoicesByAccountId(accountId, null, upToDate, context);
    assertEquals(invoices.size(), 1);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 28 with RecurringInvoiceItem

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

the class TestInvoiceDao method testGetByInvoiceItemId.

@Test(groups = "slow")
public void testGetByInvoiceItemId() throws EntityPersistenceException, InvoiceApiException {
    final Invoice invoice1 = new DefaultInvoice(account.getId(), clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
    invoiceUtil.createInvoice(invoice1, context);
    final UUID invoiceId1 = invoice1.getId();
    LocalDate startDate = new LocalDate(2011, 3, 1);
    LocalDate endDate = startDate.plusMonths(1);
    final RecurringInvoiceItem recurringItem1 = new RecurringInvoiceItem(invoiceId1, account.getId(), UUID.randomUUID(), UUID.randomUUID(), "test product", "test plan", "test A", null, startDate, endDate, BigDecimal.ONE, BigDecimal.ONE, Currency.USD);
    invoiceUtil.createInvoiceItem(recurringItem1, context);
    final InvoiceModelDao targetInvoice = invoiceDao.getByInvoiceItem(recurringItem1.getId(), internalCallContext);
    assertNotNull(targetInvoice);
    assertEquals(targetInvoice.getId(), invoiceId1);
    assertEquals(targetInvoice.getInvoiceItems().size(), 1);
    assertEquals(targetInvoice.getInvoiceItems().get(0).getId(), recurringItem1.getId());
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 29 with RecurringInvoiceItem

use of org.killbill.billing.invoice.model.RecurringInvoiceItem 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);
}
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) AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Product(org.killbill.billing.catalog.api.Product) ClockMock(org.killbill.clock.ClockMock) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) InvoiceWithMetadata(org.killbill.billing.invoice.generator.InvoiceWithMetadata) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) BillingEvent(org.killbill.billing.junction.BillingEvent) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Test(org.testng.annotations.Test)

Example 30 with RecurringInvoiceItem

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

the class TestSubscriptionItemTree method testRepairWithFullItemAdjustment.

@Test(groups = "fast")
public void testRepairWithFullItemAdjustment() {
    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 originalAdjusted = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, null, startDate, endDate, amount1, rate1, currency);
    final InvoiceItem itemAdj = new ItemAdjInvoiceItem(originalAdjusted, itemAdjDate, amount1.negate(), currency);
    // Simulate
    final InvoiceItem newItem = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, null, startDate, cancelDate, amount1, rate1, currency);
    tree.addItem(originalAdjusted);
    tree.addItem(itemAdj);
    tree.addItem(newItem);
    tree.flatten(true);
    tree.mergeProposedItem(new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, null, startDate, cancelDate, amount1, rate1, currency));
    tree.buildForMerge();
    final List<InvoiceItem> expectedResult = Lists.newLinkedList();
    verifyResult(tree.getView(), expectedResult);
}
Also used : FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)98 Test (org.testng.annotations.Test)90 LocalDate (org.joda.time.LocalDate)89 BigDecimal (java.math.BigDecimal)82 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)73 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)69 RepairAdjInvoiceItem (org.killbill.billing.invoice.model.RepairAdjInvoiceItem)69 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)65 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)39 Invoice (org.killbill.billing.invoice.api.Invoice)38 UUID (java.util.UUID)34 MockPlan (org.killbill.billing.catalog.MockPlan)16 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)16 Plan (org.killbill.billing.catalog.api.Plan)16 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)16 AccountInvoices (org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices)16 BillingEventSet (org.killbill.billing.junction.BillingEventSet)16 HashMap (java.util.HashMap)15 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)15 BillingEvent (org.killbill.billing.junction.BillingEvent)15