use of org.killbill.billing.invoice.dao.InvoiceModelDao in project killbill by killbill.
the class DefaultInvoiceUserApi method getInvoiceByNumber.
@Override
public Invoice getInvoiceByNumber(final Integer number, final TenantContext context) throws InvoiceApiException {
// The account record id will be populated in the DAO
final InternalTenantContext internalTenantContextWithoutAccountRecordId = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(context);
final InvoiceModelDao invoice = dao.getByNumber(number, internalTenantContextWithoutAccountRecordId);
final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(invoice.getAccountId(), context);
return new DefaultInvoice(invoice, getCatalogSafelyForPrettyNames(internalTenantContext));
}
use of org.killbill.billing.invoice.dao.InvoiceModelDao in project killbill by killbill.
the class TestIntegrationInvoiceWithRepairLogic method testWithPartialRepairAndExistingPartialTooLargeAdjustment.
@Test(groups = "slow")
public void testWithPartialRepairAndExistingPartialTooLargeAdjustment() throws Exception {
final LocalDate today = new LocalDate(2013, 7, 19);
// Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
clock.setDeltaFromReality(today.toDateTimeAtCurrentTime(DateTimeZone.UTC).getMillis() - clock.getUTCNow().getMillis());
final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(1));
final String productName = "Shotgun";
final BillingPeriod term = BillingPeriod.ANNUAL;
//
// CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE, NextEvent.BLOCK NextEvent.INVOICE
//
final DefaultEntitlement bpEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
assertNotNull(bpEntitlement);
assertEquals(invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext).size(), 1);
assertEquals(bpEntitlement.getSubscriptionBase().getCurrentPlan().getRecurringBillingPeriod(), BillingPeriod.ANNUAL);
// Move out of trials for interesting invoices adjustments
busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
clock.addDays(30);
assertListenerStatus();
List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
assertEquals(invoices.size(), 2);
ImmutableList<ExpectedInvoiceItemCheck> toBeChecked = ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2013, 8, 18), new LocalDate(2014, 8, 18), InvoiceItemType.RECURRING, new BigDecimal("2399.95")));
invoiceChecker.checkInvoice(invoices.get(1).getId(), callContext, toBeChecked);
clock.addMonths(6);
// Cancelation IMM -> partially repaired
busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CANCEL, NextEvent.INVOICE);
bpEntitlement.cancelEntitlementWithPolicyOverrideBillingPolicy(EntitlementActionPolicy.IMMEDIATE, BillingActionPolicy.IMMEDIATE, ImmutableList.<PluginProperty>of(), callContext);
assertListenerStatus();
// 1209.84
final InvoiceItem targetItem = invoices.get(1).getInvoiceItems().get(0);
final InvoiceItemModelDao adj = new InvoiceItemModelDao(clock.getUTCNow(), InvoiceItemType.ITEM_ADJ, targetItem.getInvoiceId(), targetItem.getAccountId(), null, null, null, targetItem.getProductName(), targetItem.getPlanName(), targetItem.getPhaseName(), targetItem.getUsageName(), targetItem.getCatalogEffectiveDate(), clock.getUTCToday(), clock.getUTCToday(), new BigDecimal("-1300.00"), null, targetItem.getCurrency(), targetItem.getId());
final InvoiceModelDao invoiceForAdj = new InvoiceModelDao(UUID.randomUUID(), clock.getUTCNow(), bpEntitlement.getAccountId(), null, clock.getUTCToday(), clock.getUTCToday(), Currency.USD, false, InvoiceStatus.COMMITTED, false);
invoiceForAdj.addInvoiceItem(adj);
busHandler.pushExpectedEvents(NextEvent.INVOICE_ADJUSTMENT);
insertInvoiceItems(invoiceForAdj);
assertListenerStatus();
// __PARK__ account
busHandler.pushExpectedEvents(NextEvent.TAG);
try {
invoiceUserApi.triggerInvoiceGeneration(account.getId(), clock.getUTCToday(), callContext);
fail("Should not have generated an extra invoice");
} catch (final InvoiceApiException e) {
assertListenerStatus();
assertTrue(e.getCause().getMessage().startsWith("Too many repairs for invoiceItemId"));
} finally {
assertListenerStatus();
}
}
use of org.killbill.billing.invoice.dao.InvoiceModelDao in project killbill by killbill.
the class TestIntegrationInvoiceWithRepairLogic method testWithFullRepairAndExistingPartialAdjustment.
@Test(groups = "slow")
public void testWithFullRepairAndExistingPartialAdjustment() throws Exception {
final LocalDate today = new LocalDate(2013, 7, 19);
// Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
clock.setDeltaFromReality(today.toDateTimeAtCurrentTime(DateTimeZone.UTC).getMillis() - clock.getUTCNow().getMillis());
final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(1));
final String productName = "Shotgun";
final BillingPeriod term = BillingPeriod.ANNUAL;
//
// CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE, NextEvent.BLOCK NextEvent.INVOICE
//
final DefaultEntitlement bpEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
assertNotNull(bpEntitlement);
assertEquals(invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext).size(), 1);
assertEquals(bpEntitlement.getSubscriptionBase().getCurrentPlan().getRecurringBillingPeriod(), BillingPeriod.ANNUAL);
// Move out of trials for interesting invoices adjustments
busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
clock.addDays(30);
assertListenerStatus();
List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
assertEquals(invoices.size(), 2);
ImmutableList<ExpectedInvoiceItemCheck> toBeChecked = ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2013, 8, 18), new LocalDate(2014, 8, 18), InvoiceItemType.RECURRING, new BigDecimal("2399.95")));
invoiceChecker.checkInvoice(invoices.get(1).getId(), callContext, toBeChecked);
// Cancelation SOT -> fully repaired
busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CANCEL, NextEvent.INVOICE);
bpEntitlement.cancelEntitlementWithPolicyOverrideBillingPolicy(EntitlementActionPolicy.IMMEDIATE, BillingActionPolicy.START_OF_TERM, ImmutableList.<PluginProperty>of(), callContext);
assertListenerStatus();
final InvoiceItem targetItem = invoices.get(1).getInvoiceItems().get(0);
final InvoiceItemModelDao adj = new InvoiceItemModelDao(clock.getUTCNow(), InvoiceItemType.ITEM_ADJ, targetItem.getInvoiceId(), targetItem.getAccountId(), null, null, null, targetItem.getProductName(), targetItem.getPlanName(), targetItem.getPhaseName(), targetItem.getUsageName(), targetItem.getCatalogEffectiveDate(), clock.getUTCToday(), clock.getUTCToday(), new BigDecimal("-1000.00"), null, targetItem.getCurrency(), targetItem.getId());
final InvoiceModelDao invoiceForAdj = new InvoiceModelDao(UUID.randomUUID(), clock.getUTCNow(), bpEntitlement.getAccountId(), null, clock.getUTCToday(), clock.getUTCToday(), Currency.USD, false, InvoiceStatus.COMMITTED, false);
invoiceForAdj.addInvoiceItem(adj);
busHandler.pushExpectedEvents(NextEvent.INVOICE_ADJUSTMENT);
insertInvoiceItems(invoiceForAdj);
assertListenerStatus();
// __PARK__ account
busHandler.pushExpectedEvents(NextEvent.TAG);
try {
invoiceUserApi.triggerInvoiceGeneration(account.getId(), clock.getUTCToday(), callContext);
fail("Should not have generated an extra invoice");
} catch (final InvoiceApiException e) {
assertListenerStatus();
assertTrue(e.getCause().getMessage().startsWith("Too many repairs for invoiceItemId"));
} finally {
assertListenerStatus();
}
}
use of org.killbill.billing.invoice.dao.InvoiceModelDao in project killbill by killbill.
the class TestIntegrationInvoiceWithRepairLogic method testRepairWithFullItemAdjustmentV2WithOldData.
//
// Same scenario as previous one but we insert a RECURRING for 2013-8-18 -> 2013-9-17 to simulate old full item adjustment behavior
//
@Test(groups = "slow")
public void testRepairWithFullItemAdjustmentV2WithOldData() throws Exception {
DefaultEntitlement bpEntitlement = setupTestRepairWithFullItemAdjustment();
final InvoiceModelDao invoiceForPreviousBehavior = new InvoiceModelDao(UUID.randomUUID(), clock.getUTCNow(), bpEntitlement.getAccountId(), null, new LocalDate(2013, 9, 17), new LocalDate(2013, 9, 17), Currency.USD, false, InvoiceStatus.COMMITTED, false);
invoiceForPreviousBehavior.addInvoiceItem(new InvoiceItemModelDao(UUID.randomUUID(), clock.getUTCNow(), InvoiceItemType.RECURRING, invoiceForPreviousBehavior.getId(), bpEntitlement.getAccountId(), null, null, bpEntitlement.getId(), "", "Shotgun", "shotgun-annual", "shotgun-annual-evergreen", null, null, new LocalDate(2013, 8, 18), new LocalDate(2013, 9, 17), new BigDecimal("197.26"), new BigDecimal("2399.95"), Currency.USD, null, null, null));
busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
insertInvoiceItems(invoiceForPreviousBehavior);
assertListenerStatus();
//
// Move clock to 2013-09-17
clock.addDays(30);
busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CANCEL, NextEvent.NULL_INVOICE);
bpEntitlement.cancelEntitlementWithPolicyOverrideBillingPolicy(EntitlementActionPolicy.IMMEDIATE, BillingActionPolicy.IMMEDIATE, ImmutableList.<PluginProperty>of(), callContext);
assertListenerStatus();
List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(bpEntitlement.getAccountId(), false, false, callContext);
assertEquals(invoices.size(), 3);
ImmutableList<ExpectedInvoiceItemCheck> toBeChecked = ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2013, 8, 18), new LocalDate(2014, 8, 18), InvoiceItemType.RECURRING, new BigDecimal("2399.95")), new ExpectedInvoiceItemCheck(new LocalDate(2013, 8, 18), new LocalDate(2013, 8, 18), InvoiceItemType.ITEM_ADJ, new BigDecimal("-2399.95")));
invoiceChecker.checkInvoice(invoices.get(1).getId(), callContext, toBeChecked);
// This is the invoice + item we inserted by hand to simulate the old data; we verify there is nothing more
toBeChecked = ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2013, 8, 18), new LocalDate(2013, 9, 17), InvoiceItemType.RECURRING, new BigDecimal("197.26")));
invoiceChecker.checkInvoice(invoices.get(2).getId(), callContext, toBeChecked);
final BigDecimal accountBalance = invoiceUserApi.getAccountBalance(bpEntitlement.getAccountId(), callContext);
assertEquals(accountBalance.compareTo(BigDecimal.ZERO), 0);
checkNoMoreInvoiceToGenerate(bpEntitlement.getAccountId());
}
use of org.killbill.billing.invoice.dao.InvoiceModelDao in project killbill by killbill.
the class TestIntegrationInvoiceWithRepairLogic method testOverlappingItems.
// This is a beatrix level test matching our invoice TestFixedAndRecurringInvoiceItemGenerator#testOverlappingItems
@Test(groups = "slow")
public void testOverlappingItems() throws Exception {
final DateTime initialDate = new DateTime(2016, 1, 1, 0, 0, 0, 0, testTimeZone);
final LocalDate startDate = initialDate.toLocalDate();
clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(1));
assertNotNull(account);
add_AUTO_INVOICING_OFF_Tag(account.getId(), ObjectType.ACCOUNT);
add_AUTO_PAY_OFF_Tag(account.getId(), ObjectType.ACCOUNT);
final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("pistol-monthly-notrial");
busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CREATE);
final UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec, null, null, null), null, startDate, startDate, false, false, ImmutableList.<PluginProperty>of(), callContext);
final Entitlement bpEntitlement = entitlementApi.getEntitlementForId(entitlementId, callContext);
assertListenerStatus();
final InvoiceModelDao existingBadInvoice = new InvoiceModelDao(UUID.randomUUID(), clock.getUTCNow(), account.getId(), null, startDate, startDate, Currency.USD, false, InvoiceStatus.COMMITTED, false);
final InvoiceItemModelDao wrongRecurring = new InvoiceItemModelDao(initialDate, InvoiceItemType.RECURRING, existingBadInvoice.getId(), existingBadInvoice.getAccountId(), bpEntitlement.getBundleId(), entitlementId, "", "Pistol", "pistol-monthly-notrial", "pistol-monthly-notrial-evergreen", null, null, new LocalDate(2016, 1, 1), new LocalDate(2016, 1, 30), new BigDecimal("18.66"), new BigDecimal("19.95"), account.getCurrency(), null);
existingBadInvoice.addInvoiceItem(wrongRecurring);
busHandler.pushExpectedEvents(NextEvent.INVOICE);
insertInvoiceItems(existingBadInvoice);
assertListenerStatus();
// Prior the change introduced in 6338109f8cdc7, this is what the code would have generated
final InvoiceModelDao prevBehaviorInvoice = new InvoiceModelDao(UUID.randomUUID(), clock.getUTCNow(), account.getId(), null, startDate, startDate, Currency.USD, false, InvoiceStatus.COMMITTED, false);
final InvoiceItemModelDao prevRecurring = new InvoiceItemModelDao(initialDate, InvoiceItemType.RECURRING, prevBehaviorInvoice.getId(), prevBehaviorInvoice.getAccountId(), bpEntitlement.getBundleId(), entitlementId, "", "Pistol", "pistol-monthly-notrial", "pistol-monthly-notrial-evergreen", null, null, new LocalDate(2016, 1, 1), new LocalDate(2016, 2, 1), new BigDecimal("19.85"), new BigDecimal("19.95"), account.getCurrency(), null);
final InvoiceItemModelDao prevRepair = new InvoiceItemModelDao(prevBehaviorInvoice.getCreatedDate(), InvoiceItemType.REPAIR_ADJ, prevBehaviorInvoice.getId(), prevBehaviorInvoice.getAccountId(), bpEntitlement.getBundleId(), bpEntitlement.getBaseEntitlementId(), null, null, null, null, null, null, new LocalDate(2016, 1, 1), new LocalDate(2016, 1, 30), new BigDecimal("-18.66"), new BigDecimal("19.95"), account.getCurrency(), wrongRecurring.getId());
prevBehaviorInvoice.addInvoiceItem(prevRecurring);
prevBehaviorInvoice.addInvoiceItem(prevRepair);
busHandler.pushExpectedEvents(NextEvent.INVOICE);
insertInvoiceItems(prevBehaviorInvoice);
assertListenerStatus();
busHandler.pushExpectedEvents(NextEvent.NULL_INVOICE);
remove_AUTO_INVOICING_OFF_Tag(account.getId(), ObjectType.ACCOUNT);
assertListenerStatus();
checkNoMoreInvoiceToGenerate(account);
}
Aggregations