killbill-memoizeit

analytics: fix various bugs in SqlDao layer Signed-off-by:

4/12/2013 4:06:17 PM

Changes

Details

diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/AnalyticsDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/AnalyticsDao.java
index 6d2a163..7f61fe2 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/AnalyticsDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/AnalyticsDao.java
@@ -16,6 +16,7 @@
 
 package com.ning.billing.osgi.bundles.analytics.dao;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
@@ -105,7 +106,12 @@ public class AnalyticsDao {
         final Long accountRecordId = getAccountRecordId(accountId, context);
         final Long tenantRecordId = getTenantRecordId(context);
 
-        final List<BusinessInvoiceItemBaseModelDao> businessInvoiceItemModelDaos = sqlDao.getInvoiceItemsByAccountRecordId(accountRecordId, tenantRecordId, context);
+        final List<BusinessInvoiceItemBaseModelDao> businessInvoiceItemModelDaos = new ArrayList<BusinessInvoiceItemBaseModelDao>();
+        businessInvoiceItemModelDaos.addAll(sqlDao.getInvoiceAdjustmentsByAccountRecordId(accountRecordId, tenantRecordId, context));
+        businessInvoiceItemModelDaos.addAll(sqlDao.getInvoiceItemsByAccountRecordId(accountRecordId, tenantRecordId, context));
+        businessInvoiceItemModelDaos.addAll(sqlDao.getInvoiceItemAdjustmentsByAccountRecordId(accountRecordId, tenantRecordId, context));
+        businessInvoiceItemModelDaos.addAll(sqlDao.getInvoiceItemCreditsByAccountRecordId(accountRecordId, tenantRecordId, context));
+
         final Map<UUID, List<BusinessInvoiceItemBaseModelDao>> itemsPerInvoice = new LinkedHashMap<UUID, List<BusinessInvoiceItemBaseModelDao>>();
         for (final BusinessInvoiceItemBaseModelDao businessInvoiceModelDao : businessInvoiceItemModelDaos) {
             if (itemsPerInvoice.get(businessInvoiceModelDao.getInvoiceId()) == null) {
@@ -127,8 +133,12 @@ public class AnalyticsDao {
         final Long accountRecordId = getAccountRecordId(accountId, context);
         final Long tenantRecordId = getTenantRecordId(context);
 
-        final List<BusinessInvoicePaymentBaseModelDao> businessInvoicePaymentBaseModelDaos = sqlDao.getInvoicePaymentsByAccountRecordId(accountRecordId, tenantRecordId, context);
-        return Lists.transform(businessInvoicePaymentBaseModelDaos, new Function<BusinessInvoicePaymentBaseModelDao, BusinessInvoicePayment>() {
+        final List<BusinessInvoicePaymentBaseModelDao> businessInvoicePaymentModelDaos = new ArrayList<BusinessInvoicePaymentBaseModelDao>();
+        businessInvoicePaymentModelDaos.addAll(sqlDao.getInvoicePaymentsByAccountRecordId(accountRecordId, tenantRecordId, context));
+        businessInvoicePaymentModelDaos.addAll(sqlDao.getInvoicePaymentRefundsByAccountRecordId(accountRecordId, tenantRecordId, context));
+        businessInvoicePaymentModelDaos.addAll(sqlDao.getInvoicePaymentChargebacksByAccountRecordId(accountRecordId, tenantRecordId, context));
+
+        return Lists.transform(businessInvoicePaymentModelDaos, new Function<BusinessInvoicePaymentBaseModelDao, BusinessInvoicePayment>() {
             @Override
             public BusinessInvoicePayment apply(final BusinessInvoicePaymentBaseModelDao input) {
                 return new BusinessInvoicePayment(input);
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/BusinessAnalyticsSqlDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/BusinessAnalyticsSqlDao.java
index 5ba5502..1b61b0b 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/BusinessAnalyticsSqlDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/BusinessAnalyticsSqlDao.java
@@ -28,11 +28,16 @@ import com.ning.billing.commons.jdbi.binder.SmartBindBean;
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessAccountFieldModelDao;
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessAccountModelDao;
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessAccountTagModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoiceAdjustmentModelDao;
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoiceFieldModelDao;
-import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoiceItemBaseModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoiceItemAdjustmentModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoiceItemCreditModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoiceItemModelDao;
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoiceModelDao;
-import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoicePaymentBaseModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoicePaymentChargebackModelDao;
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoicePaymentFieldModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoicePaymentModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoicePaymentRefundModelDao;
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoicePaymentTagModelDao;
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoiceTagModelDao;
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessModelDaoBase;
@@ -77,14 +82,39 @@ public interface BusinessAnalyticsSqlDao extends Transactional<BusinessAnalytics
                                                                       final TenantContext tenantContext);
 
     @SqlQuery
-    public List<BusinessInvoiceItemBaseModelDao> getInvoiceItemsByAccountRecordId(@Bind("accountRecordId") final Long accountRecordId,
-                                                                                  @Bind("tenantRecordId") final Long tenantRecordId,
-                                                                                  final TenantContext tenantContext);
+    public List<BusinessInvoiceAdjustmentModelDao> getInvoiceAdjustmentsByAccountRecordId(@Bind("accountRecordId") final Long accountRecordId,
+                                                                                          @Bind("tenantRecordId") final Long tenantRecordId,
+                                                                                          final TenantContext tenantContext);
+
+    @SqlQuery
+    public List<BusinessInvoiceItemModelDao> getInvoiceItemsByAccountRecordId(@Bind("accountRecordId") final Long accountRecordId,
+                                                                              @Bind("tenantRecordId") final Long tenantRecordId,
+                                                                              final TenantContext tenantContext);
+
+    @SqlQuery
+    public List<BusinessInvoiceItemAdjustmentModelDao> getInvoiceItemAdjustmentsByAccountRecordId(@Bind("accountRecordId") final Long accountRecordId,
+                                                                                                  @Bind("tenantRecordId") final Long tenantRecordId,
+                                                                                                  final TenantContext tenantContext);
+
+    @SqlQuery
+    public List<BusinessInvoiceItemCreditModelDao> getInvoiceItemCreditsByAccountRecordId(@Bind("accountRecordId") final Long accountRecordId,
+                                                                                          @Bind("tenantRecordId") final Long tenantRecordId,
+                                                                                          final TenantContext tenantContext);
+
+    @SqlQuery
+    public List<BusinessInvoicePaymentModelDao> getInvoicePaymentsByAccountRecordId(@Bind("accountRecordId") final Long accountRecordId,
+                                                                                    @Bind("tenantRecordId") final Long tenantRecordId,
+                                                                                    final TenantContext tenantContext);
+
+    @SqlQuery
+    public List<BusinessInvoicePaymentRefundModelDao> getInvoicePaymentRefundsByAccountRecordId(@Bind("accountRecordId") final Long accountRecordId,
+                                                                                                @Bind("tenantRecordId") final Long tenantRecordId,
+                                                                                                final TenantContext tenantContext);
 
     @SqlQuery
-    public List<BusinessInvoicePaymentBaseModelDao> getInvoicePaymentsByAccountRecordId(@Bind("accountRecordId") final Long accountRecordId,
-                                                                                        @Bind("tenantRecordId") final Long tenantRecordId,
-                                                                                        final TenantContext tenantContext);
+    public List<BusinessInvoicePaymentChargebackModelDao> getInvoicePaymentChargebacksByAccountRecordId(@Bind("accountRecordId") final Long accountRecordId,
+                                                                                                        @Bind("tenantRecordId") final Long tenantRecordId,
+                                                                                                        final TenantContext tenantContext);
 
     @SqlQuery
     public List<BusinessAccountFieldModelDao> getAccountFieldsByAccountRecordId(@Bind("accountRecordId") final Long accountRecordId,
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/BusinessDBIProvider.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/BusinessDBIProvider.java
index f6718ac..75b8ab9 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/BusinessDBIProvider.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/BusinessDBIProvider.java
@@ -36,10 +36,12 @@ import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoiceItemMode
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoiceModelDao;
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoicePaymentChargebackModelDao;
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoicePaymentFieldModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoicePaymentModelDao;
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoicePaymentRefundModelDao;
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoicePaymentTagModelDao;
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoiceTagModelDao;
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessOverdueStatusModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessSubscriptionTransitionModelDao;
 
 import com.google.common.base.CaseFormat;
 
@@ -61,12 +63,12 @@ public class BusinessDBIProvider {
         dbi.registerMapper(new LowerToCamelBeanMapperFactory(BusinessInvoiceModelDao.class));
         dbi.registerMapper(new LowerToCamelBeanMapperFactory(BusinessInvoicePaymentChargebackModelDao.class));
         dbi.registerMapper(new LowerToCamelBeanMapperFactory(BusinessInvoicePaymentFieldModelDao.class));
-        dbi.registerMapper(new LowerToCamelBeanMapperFactory(BusinessInvoicePaymentDao.class));
+        dbi.registerMapper(new LowerToCamelBeanMapperFactory(BusinessInvoicePaymentModelDao.class));
         dbi.registerMapper(new LowerToCamelBeanMapperFactory(BusinessInvoicePaymentRefundModelDao.class));
         dbi.registerMapper(new LowerToCamelBeanMapperFactory(BusinessInvoicePaymentTagModelDao.class));
         dbi.registerMapper(new LowerToCamelBeanMapperFactory(BusinessInvoiceTagModelDao.class));
         dbi.registerMapper(new LowerToCamelBeanMapperFactory(BusinessOverdueStatusModelDao.class));
-        dbi.registerMapper(new LowerToCamelBeanMapperFactory(BusinessSubscriptionTransitionDao.class));
+        dbi.registerMapper(new LowerToCamelBeanMapperFactory(BusinessSubscriptionTransitionModelDao.class));
 
         dbi.setStatementLocator(new AnalyticsStatementLocator());
 
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceItemBaseModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceItemBaseModelDao.java
index 33ca531..cd70b84 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceItemBaseModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceItemBaseModelDao.java
@@ -449,7 +449,7 @@ public abstract class BusinessInvoiceItemBaseModelDao extends BusinessModelDaoBa
 
         final BusinessInvoiceItemBaseModelDao that = (BusinessInvoiceItemBaseModelDao) o;
 
-        if (amount != null ? !amount.equals(that.amount) : that.amount != null) {
+        if (amount != null ? (amount.compareTo(that.amount) != 0) : that.amount != null) {
             return false;
         }
         if (billingPeriod != null ? !billingPeriod.equals(that.billingPeriod) : that.billingPeriod != null) {
@@ -458,31 +458,31 @@ public abstract class BusinessInvoiceItemBaseModelDao extends BusinessModelDaoBa
         if (bundleExternalKey != null ? !bundleExternalKey.equals(that.bundleExternalKey) : that.bundleExternalKey != null) {
             return false;
         }
-        if (currency != that.currency) {
+        if (currency != null ? !currency.equals(that.currency) : that.currency != null) {
             return false;
         }
-        if (endDate != null ? !endDate.equals(that.endDate) : that.endDate != null) {
+        if (endDate != null ? (endDate.compareTo(that.endDate) != 0) : that.endDate != null) {
             return false;
         }
-        if (invoiceAmountCharged != null ? !invoiceAmountCharged.equals(that.invoiceAmountCharged) : that.invoiceAmountCharged != null) {
+        if (invoiceAmountCharged != null ? (invoiceAmountCharged.compareTo(that.invoiceAmountCharged) != 0) : that.invoiceAmountCharged != null) {
             return false;
         }
-        if (invoiceAmountCredited != null ? !invoiceAmountCredited.equals(that.invoiceAmountCredited) : that.invoiceAmountCredited != null) {
+        if (invoiceAmountCredited != null ? (invoiceAmountCredited.compareTo(that.invoiceAmountCredited) != 0) : that.invoiceAmountCredited != null) {
             return false;
         }
-        if (invoiceAmountPaid != null ? !invoiceAmountPaid.equals(that.invoiceAmountPaid) : that.invoiceAmountPaid != null) {
+        if (invoiceAmountPaid != null ? (invoiceAmountPaid.compareTo(that.invoiceAmountPaid) != 0) : that.invoiceAmountPaid != null) {
             return false;
         }
-        if (invoiceBalance != null ? !invoiceBalance.equals(that.invoiceBalance) : that.invoiceBalance != null) {
+        if (invoiceBalance != null ? (invoiceBalance.compareTo(that.invoiceBalance) != 0) : that.invoiceBalance != null) {
             return false;
         }
-        if (invoiceCreatedDate != null ? !invoiceCreatedDate.equals(that.invoiceCreatedDate) : that.invoiceCreatedDate != null) {
+        if (invoiceCreatedDate != null ? (invoiceCreatedDate.compareTo(that.invoiceCreatedDate) != 0) : that.invoiceCreatedDate != null) {
             return false;
         }
         if (invoiceCurrency != null ? !invoiceCurrency.equals(that.invoiceCurrency) : that.invoiceCurrency != null) {
             return false;
         }
-        if (invoiceDate != null ? !invoiceDate.equals(that.invoiceDate) : that.invoiceDate != null) {
+        if (invoiceDate != null ? (invoiceDate.compareTo(that.invoiceDate) != 0) : that.invoiceDate != null) {
             return false;
         }
         if (invoiceId != null ? !invoiceId.equals(that.invoiceId) : that.invoiceId != null) {
@@ -494,10 +494,10 @@ public abstract class BusinessInvoiceItemBaseModelDao extends BusinessModelDaoBa
         if (invoiceNumber != null ? !invoiceNumber.equals(that.invoiceNumber) : that.invoiceNumber != null) {
             return false;
         }
-        if (invoiceOriginalAmountCharged != null ? !invoiceOriginalAmountCharged.equals(that.invoiceOriginalAmountCharged) : that.invoiceOriginalAmountCharged != null) {
+        if (invoiceOriginalAmountCharged != null ? (invoiceOriginalAmountCharged.compareTo(that.invoiceOriginalAmountCharged) != 0) : that.invoiceOriginalAmountCharged != null) {
             return false;
         }
-        if (invoiceTargetDate != null ? !invoiceTargetDate.equals(that.invoiceTargetDate) : that.invoiceTargetDate != null) {
+        if (invoiceTargetDate != null ? (invoiceTargetDate.compareTo(that.invoiceTargetDate) != 0) : that.invoiceTargetDate != null) {
             return false;
         }
         if (itemId != null ? !itemId.equals(that.itemId) : that.itemId != null) {
@@ -530,7 +530,7 @@ public abstract class BusinessInvoiceItemBaseModelDao extends BusinessModelDaoBa
         if (slug != null ? !slug.equals(that.slug) : that.slug != null) {
             return false;
         }
-        if (startDate != null ? !startDate.equals(that.startDate) : that.startDate != null) {
+        if (startDate != null ? (startDate.compareTo(that.startDate) != 0) : that.startDate != null) {
             return false;
         }
 
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceModelDao.java
index a7c8c75..8cf25fb 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceModelDao.java
@@ -202,22 +202,22 @@ public class BusinessInvoiceModelDao extends BusinessModelDaoBase {
 
         final BusinessInvoiceModelDao that = (BusinessInvoiceModelDao) o;
 
-        if (amountCharged != null ? !amountCharged.equals(that.amountCharged) : that.amountCharged != null) {
+        if (amountCharged != null ? (amountCharged.compareTo(that.amountCharged) != 0) : that.amountCharged != null) {
             return false;
         }
-        if (amountCredited != null ? !amountCredited.equals(that.amountCredited) : that.amountCredited != null) {
+        if (amountCredited != null ? (amountCredited.compareTo(that.amountCredited) != 0) : that.amountCredited != null) {
             return false;
         }
-        if (amountPaid != null ? !amountPaid.equals(that.amountPaid) : that.amountPaid != null) {
+        if (amountPaid != null ? (amountPaid.compareTo(that.amountPaid) != 0) : that.amountPaid != null) {
             return false;
         }
-        if (balance != null ? !balance.equals(that.balance) : that.balance != null) {
+        if (balance != null ? (balance.compareTo(that.balance) != 0) : that.balance != null) {
             return false;
         }
         if (currency != null ? !currency.equals(that.currency) : that.currency != null) {
             return false;
         }
-        if (invoiceDate != null ? !invoiceDate.equals(that.invoiceDate) : that.invoiceDate != null) {
+        if (invoiceDate != null ? (invoiceDate.compareTo(that.invoiceDate) != 0) : that.invoiceDate != null) {
             return false;
         }
         if (invoiceId != null ? !invoiceId.equals(that.invoiceId) : that.invoiceId != null) {
@@ -229,10 +229,10 @@ public class BusinessInvoiceModelDao extends BusinessModelDaoBase {
         if (invoiceRecordId != null ? !invoiceRecordId.equals(that.invoiceRecordId) : that.invoiceRecordId != null) {
             return false;
         }
-        if (originalAmountCharged != null ? !originalAmountCharged.equals(that.originalAmountCharged) : that.originalAmountCharged != null) {
+        if (originalAmountCharged != null ? (originalAmountCharged.compareTo(that.originalAmountCharged) != 0) : that.originalAmountCharged != null) {
             return false;
         }
-        if (targetDate != null ? !targetDate.equals(that.targetDate) : that.targetDate != null) {
+        if (targetDate != null ? (targetDate.compareTo(that.targetDate) != 0) : that.targetDate != null) {
             return false;
         }
 
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentBaseModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentBaseModelDao.java
index 85b7230..5a3523b 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentBaseModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentBaseModelDao.java
@@ -512,31 +512,31 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
 
         final BusinessInvoicePaymentBaseModelDao that = (BusinessInvoicePaymentBaseModelDao) o;
 
-        if (amount != null ? !amount.equals(that.amount) : that.amount != null) {
+        if (amount != null ? (amount.compareTo(that.amount) != 0) : that.amount != null) {
             return false;
         }
         if (currency != null ? !currency.equals(that.currency) : that.currency != null) {
             return false;
         }
-        if (invoiceAmountCharged != null ? !invoiceAmountCharged.equals(that.invoiceAmountCharged) : that.invoiceAmountCharged != null) {
+        if (invoiceAmountCharged != null ? (invoiceAmountCharged.compareTo(that.invoiceAmountCharged) != 0) : that.invoiceAmountCharged != null) {
             return false;
         }
-        if (invoiceAmountCredited != null ? !invoiceAmountCredited.equals(that.invoiceAmountCredited) : that.invoiceAmountCredited != null) {
+        if (invoiceAmountCredited != null ? (invoiceAmountCredited.compareTo(that.invoiceAmountCredited) != 0) : that.invoiceAmountCredited != null) {
             return false;
         }
-        if (invoiceAmountPaid != null ? !invoiceAmountPaid.equals(that.invoiceAmountPaid) : that.invoiceAmountPaid != null) {
+        if (invoiceAmountPaid != null ? (invoiceAmountPaid.compareTo(that.invoiceAmountPaid) != 0) : that.invoiceAmountPaid != null) {
             return false;
         }
-        if (invoiceBalance != null ? !invoiceBalance.equals(that.invoiceBalance) : that.invoiceBalance != null) {
+        if (invoiceBalance != null ? (invoiceBalance.compareTo(that.invoiceBalance) != 0) : that.invoiceBalance != null) {
             return false;
         }
-        if (invoiceCreatedDate != null ? !invoiceCreatedDate.equals(that.invoiceCreatedDate) : that.invoiceCreatedDate != null) {
+        if (invoiceCreatedDate != null ? (invoiceCreatedDate.compareTo(that.invoiceCreatedDate) != 0) : that.invoiceCreatedDate != null) {
             return false;
         }
         if (invoiceCurrency != null ? !invoiceCurrency.equals(that.invoiceCurrency) : that.invoiceCurrency != null) {
             return false;
         }
-        if (invoiceDate != null ? !invoiceDate.equals(that.invoiceDate) : that.invoiceDate != null) {
+        if (invoiceDate != null ? (invoiceDate.compareTo(that.invoiceDate) != 0) : that.invoiceDate != null) {
             return false;
         }
         if (invoiceId != null ? !invoiceId.equals(that.invoiceId) : that.invoiceId != null) {
@@ -545,7 +545,7 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
         if (invoiceNumber != null ? !invoiceNumber.equals(that.invoiceNumber) : that.invoiceNumber != null) {
             return false;
         }
-        if (invoiceOriginalAmountCharged != null ? !invoiceOriginalAmountCharged.equals(that.invoiceOriginalAmountCharged) : that.invoiceOriginalAmountCharged != null) {
+        if (invoiceOriginalAmountCharged != null ? (invoiceOriginalAmountCharged.compareTo(that.invoiceOriginalAmountCharged) != 0) : that.invoiceOriginalAmountCharged != null) {
             return false;
         }
         if (invoicePaymentId != null ? !invoicePaymentId.equals(that.invoicePaymentId) : that.invoicePaymentId != null) {
@@ -557,7 +557,7 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
         if (invoicePaymentType != null ? !invoicePaymentType.equals(that.invoicePaymentType) : that.invoicePaymentType != null) {
             return false;
         }
-        if (invoiceTargetDate != null ? !invoiceTargetDate.equals(that.invoiceTargetDate) : that.invoiceTargetDate != null) {
+        if (invoiceTargetDate != null ? (invoiceTargetDate.compareTo(that.invoiceTargetDate) != 0) : that.invoiceTargetDate != null) {
             return false;
         }
         if (linkedInvoicePaymentId != null ? !linkedInvoicePaymentId.equals(that.linkedInvoicePaymentId) : that.linkedInvoicePaymentId != null) {
@@ -566,10 +566,10 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
         if (paymentNumber != null ? !paymentNumber.equals(that.paymentNumber) : that.paymentNumber != null) {
             return false;
         }
-        if (pluginCreatedDate != null ? !pluginCreatedDate.equals(that.pluginCreatedDate) : that.pluginCreatedDate != null) {
+        if (pluginCreatedDate != null ? (pluginCreatedDate.compareTo(that.pluginCreatedDate) != 0) : that.pluginCreatedDate != null) {
             return false;
         }
-        if (pluginEffectiveDate != null ? !pluginEffectiveDate.equals(that.pluginEffectiveDate) : that.pluginEffectiveDate != null) {
+        if (pluginEffectiveDate != null ? (pluginEffectiveDate.compareTo(that.pluginEffectiveDate) != 0) : that.pluginEffectiveDate != null) {
             return false;
         }
         if (pluginFirstReferenceId != null ? !pluginFirstReferenceId.equals(that.pluginFirstReferenceId) : that.pluginFirstReferenceId != null) {
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessOverdueStatusModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessOverdueStatusModelDao.java
index 02e6d82..7abf1cf 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessOverdueStatusModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessOverdueStatusModelDao.java
@@ -166,10 +166,10 @@ public class BusinessOverdueStatusModelDao extends BusinessModelDaoBase {
         if (bundleId != null ? !bundleId.equals(that.bundleId) : that.bundleId != null) {
             return false;
         }
-        if (endDate != null ? !endDate.equals(that.endDate) : that.endDate != null) {
+        if (endDate != null ? (endDate.compareTo(that.endDate) != 0) : that.endDate != null) {
             return false;
         }
-        if (startDate != null ? !startDate.equals(that.startDate) : that.startDate != null) {
+        if (startDate != null ? (startDate.compareTo(that.startDate) != 0) : that.startDate != null) {
             return false;
         }
         if (status != null ? !status.equals(that.status) : that.status != null) {
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessSubscriptionTransitionModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessSubscriptionTransitionModelDao.java
index 92032b6..1567f9b 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessSubscriptionTransitionModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessSubscriptionTransitionModelDao.java
@@ -394,16 +394,16 @@ public class BusinessSubscriptionTransitionModelDao extends BusinessModelDaoBase
         if (nextCurrency != null ? !nextCurrency.equals(that.nextCurrency) : that.nextCurrency != null) {
             return false;
         }
-        if (nextEndDate != null ? !nextEndDate.equals(that.nextEndDate) : that.nextEndDate != null) {
+        if (nextEndDate != null ? (nextEndDate.compareTo(that.nextEndDate) != 0) : that.nextEndDate != null) {
             return false;
         }
-        if (nextMrr != null ? !nextMrr.equals(that.nextMrr) : that.nextMrr != null) {
+        if (nextMrr != null ? (nextMrr.compareTo(that.nextMrr) != 0) : that.nextMrr != null) {
             return false;
         }
         if (nextPhase != null ? !nextPhase.equals(that.nextPhase) : that.nextPhase != null) {
             return false;
         }
-        if (nextPrice != null ? !nextPrice.equals(that.nextPrice) : that.nextPrice != null) {
+        if (nextPrice != null ? (nextPrice.compareTo(that.nextPrice) != 0) : that.nextPrice != null) {
             return false;
         }
         if (nextPriceList != null ? !nextPriceList.equals(that.nextPriceList) : that.nextPriceList != null) {
@@ -421,7 +421,7 @@ public class BusinessSubscriptionTransitionModelDao extends BusinessModelDaoBase
         if (nextSlug != null ? !nextSlug.equals(that.nextSlug) : that.nextSlug != null) {
             return false;
         }
-        if (nextStartDate != null ? !nextStartDate.equals(that.nextStartDate) : that.nextStartDate != null) {
+        if (nextStartDate != null ? (nextStartDate.compareTo(that.nextStartDate) != 0) : that.nextStartDate != null) {
             return false;
         }
         if (nextState != null ? !nextState.equals(that.nextState) : that.nextState != null) {
@@ -436,13 +436,13 @@ public class BusinessSubscriptionTransitionModelDao extends BusinessModelDaoBase
         if (prevCurrency != null ? !prevCurrency.equals(that.prevCurrency) : that.prevCurrency != null) {
             return false;
         }
-        if (prevMrr != null ? !prevMrr.equals(that.prevMrr) : that.prevMrr != null) {
+        if (prevMrr != null ? (prevMrr.compareTo(that.prevMrr) != 0) : that.prevMrr != null) {
             return false;
         }
         if (prevPhase != null ? !prevPhase.equals(that.prevPhase) : that.prevPhase != null) {
             return false;
         }
-        if (prevPrice != null ? !prevPrice.equals(that.prevPrice) : that.prevPrice != null) {
+        if (prevPrice != null ? (prevPrice.compareTo(that.prevPrice) != 0) : that.prevPrice != null) {
             return false;
         }
         if (prevPriceList != null ? !prevPriceList.equals(that.prevPriceList) : that.prevPriceList != null) {
@@ -460,13 +460,13 @@ public class BusinessSubscriptionTransitionModelDao extends BusinessModelDaoBase
         if (prevSlug != null ? !prevSlug.equals(that.prevSlug) : that.prevSlug != null) {
             return false;
         }
-        if (prevStartDate != null ? !prevStartDate.equals(that.prevStartDate) : that.prevStartDate != null) {
+        if (prevStartDate != null ? (prevStartDate.compareTo(that.prevStartDate) != 0) : that.prevStartDate != null) {
             return false;
         }
         if (prevState != null ? !prevState.equals(that.prevState) : that.prevState != null) {
             return false;
         }
-        if (requestedTimestamp != null ? !requestedTimestamp.equals(that.requestedTimestamp) : that.requestedTimestamp != null) {
+        if (requestedTimestamp != null ? (requestedTimestamp.compareTo(that.requestedTimestamp) != 0) : that.requestedTimestamp != null) {
             return false;
         }
         if (subscriptionEventRecordId != null ? !subscriptionEventRecordId.equals(that.subscriptionEventRecordId) : that.subscriptionEventRecordId != null) {
diff --git a/osgi-bundles/bundles/analytics/src/main/resources/com/ning/billing/osgi/bundles/analytics/dao/BusinessAnalyticsSqlDao.sql.stg b/osgi-bundles/bundles/analytics/src/main/resources/com/ning/billing/osgi/bundles/analytics/dao/BusinessAnalyticsSqlDao.sql.stg
index 0f40ef3..8d829d4 100644
--- a/osgi-bundles/bundles/analytics/src/main/resources/com/ning/billing/osgi/bundles/analytics/dao/BusinessAnalyticsSqlDao.sql.stg
+++ b/osgi-bundles/bundles/analytics/src/main/resources/com/ning/billing/osgi/bundles/analytics/dao/BusinessAnalyticsSqlDao.sql.stg
@@ -183,6 +183,7 @@ insert into bin (
 , account_name
 , account_external_key
 , account_record_id
+, tenant_record_id
 , report_group
 ) values (
   :invoiceRecordId
@@ -204,6 +205,7 @@ insert into bin (
 , :accountName
 , :accountExternalKey
 , :accountRecordId
+, :tenantRecordId
 , :reportGroup
 );
 >>
@@ -556,6 +558,27 @@ insert into bip (
 , linked_invoice_payment_id
 , amount
 , currency
+, plugin_created_date
+, plugin_effective_date
+, plugin_status
+, plugin_gateway_error
+, plugin_gateway_error_code
+, plugin_first_reference_id
+, plugin_second_reference_id
+, plugin_pm_id
+, plugin_pm_is_default
+, plugin_pm_type
+, plugin_pm_cc_name
+, plugin_pm_cc_type
+, plugin_pm_cc_expiration_month
+, plugin_pm_cc_expiration_year
+, plugin_pm_cc_last_4
+, plugin_pm_address1
+, plugin_pm_address2
+, plugin_pm_city
+, plugin_pm_state
+, plugin_pm_zip
+, plugin_pm_country
 , created_date
 , created_by
 , created_reason_code
@@ -585,6 +608,27 @@ insert into bip (
 , :linkedInvoicePaymentId
 , :amount
 , :currency
+, :pluginCreatedDate
+, :pluginEffectiveDate
+, :pluginStatus
+, :pluginGatewayError
+, :pluginGatewayErrorCode
+, :pluginFirstReferenceId
+, :pluginSecondReferenceId
+, :pluginPmId
+, :pluginPmIsDefault
+, :pluginPmType
+, :pluginPmCcName
+, :pluginPmCcType
+, :pluginPmCcExpirationMonth
+, :pluginPmCcExpirationYear
+, :pluginPmCcLast4
+, :pluginPmAddress1
+, :pluginPmAddress2
+, :pluginPmCity
+, :pluginPmState
+, :pluginPmZip
+, :pluginPmCountry
 , :createdDate
 , :createdBy
 , :createdReasonCode
@@ -618,6 +662,27 @@ insert into bipr (
 , linked_invoice_payment_id
 , amount
 , currency
+, plugin_created_date
+, plugin_effective_date
+, plugin_status
+, plugin_gateway_error
+, plugin_gateway_error_code
+, plugin_first_reference_id
+, plugin_second_reference_id
+, plugin_pm_id
+, plugin_pm_is_default
+, plugin_pm_type
+, plugin_pm_cc_name
+, plugin_pm_cc_type
+, plugin_pm_cc_expiration_month
+, plugin_pm_cc_expiration_year
+, plugin_pm_cc_last_4
+, plugin_pm_address1
+, plugin_pm_address2
+, plugin_pm_city
+, plugin_pm_state
+, plugin_pm_zip
+, plugin_pm_country
 , created_date
 , created_by
 , created_reason_code
@@ -647,6 +712,27 @@ insert into bipr (
 , :linkedInvoicePaymentId
 , :amount
 , :currency
+, :pluginCreatedDate
+, :pluginEffectiveDate
+, :pluginStatus
+, :pluginGatewayError
+, :pluginGatewayErrorCode
+, :pluginFirstReferenceId
+, :pluginSecondReferenceId
+, :pluginPmId
+, :pluginPmIsDefault
+, :pluginPmType
+, :pluginPmCcName
+, :pluginPmCcType
+, :pluginPmCcExpirationMonth
+, :pluginPmCcExpirationYear
+, :pluginPmCcLast4
+, :pluginPmAddress1
+, :pluginPmAddress2
+, :pluginPmCity
+, :pluginPmState
+, :pluginPmZip
+, :pluginPmCountry
 , :createdDate
 , :createdBy
 , :createdReasonCode
@@ -680,6 +766,27 @@ insert into bipc (
 , linked_invoice_payment_id
 , amount
 , currency
+, plugin_created_date
+, plugin_effective_date
+, plugin_status
+, plugin_gateway_error
+, plugin_gateway_error_code
+, plugin_first_reference_id
+, plugin_second_reference_id
+, plugin_pm_id
+, plugin_pm_is_default
+, plugin_pm_type
+, plugin_pm_cc_name
+, plugin_pm_cc_type
+, plugin_pm_cc_expiration_month
+, plugin_pm_cc_expiration_year
+, plugin_pm_cc_last_4
+, plugin_pm_address1
+, plugin_pm_address2
+, plugin_pm_city
+, plugin_pm_state
+, plugin_pm_zip
+, plugin_pm_country
 , created_date
 , created_by
 , created_reason_code
@@ -709,6 +816,27 @@ insert into bipc (
 , :linkedInvoicePaymentId
 , :amount
 , :currency
+, :pluginCreatedDate
+, :pluginEffectiveDate
+, :pluginStatus
+, :pluginGatewayError
+, :pluginGatewayErrorCode
+, :pluginFirstReferenceId
+, :pluginSecondReferenceId
+, :pluginPmId
+, :pluginPmIsDefault
+, :pluginPmType
+, :pluginPmCcName
+, :pluginPmCcType
+, :pluginPmCcExpirationMonth
+, :pluginPmCcExpirationYear
+, :pluginPmCcLast4
+, :pluginPmAddress1
+, :pluginPmAddress2
+, :pluginPmCity
+, :pluginPmState
+, :pluginPmZip
+, :pluginPmCountry
 , :createdDate
 , :createdBy
 , :createdReasonCode
@@ -922,7 +1050,7 @@ insert into bin_fields (
 
 createBipFields() ::= <<
 insert into bip_fields (
-, custom_field_record_id
+  custom_field_record_id
 , invoice_payment_id
 , name
 , value
@@ -937,7 +1065,7 @@ insert into bip_fields (
 , tenant_record_id
 , report_group
 ) values (
-, :customFieldRecordId
+  :customFieldRecordId
 , :invoicePaymentId
 , :name
 , :value
@@ -991,22 +1119,37 @@ getInvoicesByAccountRecordId() ::= <<
 ;
 >>
 
-getInvoiceItemsByAccountRecordId() ::= <<
+getInvoiceAdjustmentsByAccountRecordId() ::= <<
 <SELECT_STAR_FROM_TABLE("bia")>
-union all
+;
+>>
+
+getInvoiceItemsByAccountRecordId() ::= <<
 <SELECT_STAR_FROM_TABLE("bii")>
-union all
+;
+>>
+
+getInvoiceItemAdjustmentsByAccountRecordId() ::= <<
 <SELECT_STAR_FROM_TABLE("biia")>
-union all
+;
+>>
+
+getInvoiceItemCreditsByAccountRecordId() ::= <<
 <SELECT_STAR_FROM_TABLE("biic")>
 ;
 >>
 
 getInvoicePaymentsByAccountRecordId() ::= <<
 <SELECT_STAR_FROM_TABLE("bip")>
-union all
+;
+>>
+
+getInvoicePaymentRefundsByAccountRecordId() ::= <<
 <SELECT_STAR_FROM_TABLE("bipr")>
-union all
+;
+>>
+
+getInvoicePaymentChargebacksByAccountRecordId() ::= <<
 <SELECT_STAR_FROM_TABLE("bipc")>
 ;
 >>
diff --git a/osgi-bundles/bundles/analytics/src/main/resources/com/ning/billing/osgi/bundles/analytics/ddl.sql b/osgi-bundles/bundles/analytics/src/main/resources/com/ning/billing/osgi/bundles/analytics/ddl.sql
index ad682ef..b1bbbf3 100644
--- a/osgi-bundles/bundles/analytics/src/main/resources/com/ning/billing/osgi/bundles/analytics/ddl.sql
+++ b/osgi-bundles/bundles/analytics/src/main/resources/com/ning/billing/osgi/bundles/analytics/ddl.sql
@@ -139,7 +139,7 @@ create table bia (
 , item_id char(36) not null
 , invoice_id char(36) not null
 , invoice_number bigint default null
-, invoice_created_date date not null
+, invoice_created_date datetime not null
 , invoice_date date not null
 , invoice_target_date date not null
 , invoice_currency char(50) not null
@@ -189,7 +189,7 @@ create table bii (
 , item_id char(36) not null
 , invoice_id char(36) not null
 , invoice_number bigint default null
-, invoice_created_date date not null
+, invoice_created_date datetime not null
 , invoice_date date not null
 , invoice_target_date date not null
 , invoice_currency char(50) not null
@@ -239,7 +239,7 @@ create table biia (
 , item_id char(36) not null
 , invoice_id char(36) not null
 , invoice_number bigint default null
-, invoice_created_date date not null
+, invoice_created_date datetime not null
 , invoice_date date not null
 , invoice_target_date date not null
 , invoice_currency char(50) not null
@@ -289,7 +289,7 @@ create table biic (
 , item_id char(36) not null
 , invoice_id char(36) not null
 , invoice_number bigint default null
-, invoice_created_date date not null
+, invoice_created_date datetime not null
 , invoice_date date not null
 , invoice_target_date date not null
 , invoice_currency char(50) not null
@@ -338,7 +338,7 @@ create table bip (
 , invoice_payment_id char(36) not null
 , invoice_id char(36) not null
 , invoice_number bigint default null
-, invoice_created_date date not null
+, invoice_created_date datetime not null
 , invoice_date date not null
 , invoice_target_date date not null
 , invoice_currency char(50) not null
@@ -399,7 +399,7 @@ create table bipr (
 , invoice_payment_id char(36) not null
 , invoice_id char(36) not null
 , invoice_number bigint default null
-, invoice_created_date date not null
+, invoice_created_date datetime not null
 , invoice_date date not null
 , invoice_target_date date not null
 , invoice_currency char(50) not null
@@ -460,7 +460,7 @@ create table bipc (
 , invoice_payment_id char(36) not null
 , invoice_id char(36) not null
 , invoice_number bigint default null
-, invoice_created_date date not null
+, invoice_created_date datetime not null
 , invoice_date date not null
 , invoice_target_date date not null
 , invoice_currency char(50) not null
diff --git a/osgi-bundles/bundles/analytics/src/test/java/com/ning/billing/osgi/bundles/analytics/AnalyticsTestSuiteWithEmbeddedDB.java b/osgi-bundles/bundles/analytics/src/test/java/com/ning/billing/osgi/bundles/analytics/AnalyticsTestSuiteWithEmbeddedDB.java
index 9f867fe..ef5d1d2 100644
--- a/osgi-bundles/bundles/analytics/src/test/java/com/ning/billing/osgi/bundles/analytics/AnalyticsTestSuiteWithEmbeddedDB.java
+++ b/osgi-bundles/bundles/analytics/src/test/java/com/ning/billing/osgi/bundles/analytics/AnalyticsTestSuiteWithEmbeddedDB.java
@@ -50,6 +50,9 @@ public abstract class AnalyticsTestSuiteWithEmbeddedDB extends AnalyticsTestSuit
         embeddedDB = new MySQLEmbeddedDB();
         embeddedDB.initialize();
         embeddedDB.start();
+
+        final String ddl = toString(Resources.getResource("com/ning/billing/osgi/bundles/analytics/ddl.sql").openStream());
+        embeddedDB.executeScript(ddl);
     }
 
     @BeforeMethod(groups = "slow")
@@ -59,8 +62,7 @@ public abstract class AnalyticsTestSuiteWithEmbeddedDB extends AnalyticsTestSuit
 
         killbillDataSource = new AnalyticsOSGIKillbillDataSource();
 
-        final String ddl = toString(Resources.getResource("com/ning/billing/osgi/bundles/analytics/ddl.sql").openStream());
-        embeddedDB.executeScript(ddl);
+        embeddedDB.cleanupAllTables();
 
         dbi = BusinessDBIProvider.get(embeddedDB.getDataSource());
         analyticsSqlDao = dbi.onDemand(BusinessAnalyticsSqlDao.class);
diff --git a/osgi-bundles/bundles/analytics/src/test/java/com/ning/billing/osgi/bundles/analytics/dao/TestBusinessAnalyticsSqlDao.java b/osgi-bundles/bundles/analytics/src/test/java/com/ning/billing/osgi/bundles/analytics/dao/TestBusinessAnalyticsSqlDao.java
index dcb30df..00ecab6 100644
--- a/osgi-bundles/bundles/analytics/src/test/java/com/ning/billing/osgi/bundles/analytics/dao/TestBusinessAnalyticsSqlDao.java
+++ b/osgi-bundles/bundles/analytics/src/test/java/com/ning/billing/osgi/bundles/analytics/dao/TestBusinessAnalyticsSqlDao.java
@@ -18,16 +18,36 @@ package com.ning.billing.osgi.bundles.analytics.dao;
 
 import java.math.BigDecimal;
 
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
+import com.ning.billing.catalog.api.Currency;
+import com.ning.billing.entitlement.api.user.Subscription.SubscriptionState;
 import com.ning.billing.osgi.bundles.analytics.AnalyticsTestSuiteWithEmbeddedDB;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessAccountFieldModelDao;
 import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessAccountModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessAccountTagModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessFieldModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoiceFieldModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoiceItemBaseModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoiceModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoicePaymentBaseModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoicePaymentFieldModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoicePaymentModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoicePaymentTagModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessInvoiceTagModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessOverdueStatusModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessSubscription;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessSubscriptionEvent;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessSubscriptionTransitionModelDao;
+import com.ning.billing.osgi.bundles.analytics.dao.model.BusinessTagModelDao;
 
 public class TestBusinessAnalyticsSqlDao extends AnalyticsTestSuiteWithEmbeddedDB {
 
     @Test(groups = "slow")
-    public void testSqlDao() throws Exception {
+    public void testSqlDaoForAccount() throws Exception {
         final BusinessAccountModelDao accountModelDao = new BusinessAccountModelDao(account,
                                                                                     accountRecordId,
                                                                                     new BigDecimal("1.2345"),
@@ -37,20 +57,18 @@ public class TestBusinessAnalyticsSqlDao extends AnalyticsTestSuiteWithEmbeddedD
                                                                                     tenantRecordId,
                                                                                     reportGroup);
 
+        // Check the record doesn't exist yet
         Assert.assertNull(analyticsSqlDao.getAccountByAccountRecordId(accountModelDao.getAccountRecordId(),
                                                                       accountModelDao.getTenantRecordId(),
                                                                       callContext));
 
+        // Create and check we can retrieve it
         analyticsSqlDao.create(accountModelDao.getTableName(), accountModelDao, callContext);
-        final BusinessAccountModelDao newBusinessAccountModelDao = analyticsSqlDao.getAccountByAccountRecordId(accountModelDao.getAccountRecordId(),
-                                                                                                               accountModelDao.getTenantRecordId(),
-                                                                                                               callContext);
-        Assert.assertEquals(newBusinessAccountModelDao, accountModelDao);
-       /*
         Assert.assertEquals(analyticsSqlDao.getAccountByAccountRecordId(accountModelDao.getAccountRecordId(),
                                                                         accountModelDao.getTenantRecordId(),
                                                                         callContext), accountModelDao);
-*/
+
+        // Delete and verify it doesn't exist anymore
         analyticsSqlDao.deleteByAccountRecordId(accountModelDao.getTableName(),
                                                 accountModelDao.getAccountRecordId(),
                                                 accountModelDao.getTenantRecordId(),
@@ -59,4 +77,275 @@ public class TestBusinessAnalyticsSqlDao extends AnalyticsTestSuiteWithEmbeddedD
                                                                       accountModelDao.getTenantRecordId(),
                                                                       callContext));
     }
+
+    @Test(groups = "slow")
+    public void testSqlDaoForAccountField() throws Exception {
+        final BusinessFieldModelDao businessFieldModelDao = new BusinessAccountFieldModelDao(account,
+                                                                                             accountRecordId,
+                                                                                             customField,
+                                                                                             fieldRecordId,
+                                                                                             auditLog,
+                                                                                             tenantRecordId,
+                                                                                             reportGroup);
+        // Check the record doesn't exist yet
+        Assert.assertEquals(analyticsSqlDao.getAccountFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+
+        // Create and check we can retrieve it
+        analyticsSqlDao.create(businessFieldModelDao.getTableName(), businessFieldModelDao, callContext);
+        Assert.assertEquals(analyticsSqlDao.getAccountFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 1);
+        Assert.assertEquals(analyticsSqlDao.getAccountFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).get(0), businessFieldModelDao);
+
+        // Delete and verify it doesn't exist anymore
+        analyticsSqlDao.deleteByAccountRecordId(businessFieldModelDao.getTableName(), accountRecordId, tenantRecordId, callContext);
+        Assert.assertEquals(analyticsSqlDao.getAccountFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+    }
+
+    @Test(groups = "slow")
+    public void testSqlDaoForInvoiceField() throws Exception {
+        final BusinessFieldModelDao businessFieldModelDao = new BusinessInvoiceFieldModelDao(account,
+                                                                                             accountRecordId,
+                                                                                             customField,
+                                                                                             fieldRecordId,
+                                                                                             auditLog,
+                                                                                             tenantRecordId,
+                                                                                             reportGroup);
+        // Check the record doesn't exist yet
+        Assert.assertEquals(analyticsSqlDao.getInvoiceFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+
+        // Create and check we can retrieve it
+        analyticsSqlDao.create(businessFieldModelDao.getTableName(), businessFieldModelDao, callContext);
+        Assert.assertEquals(analyticsSqlDao.getInvoiceFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 1);
+        Assert.assertEquals(analyticsSqlDao.getInvoiceFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).get(0), businessFieldModelDao);
+
+        // Delete and verify it doesn't exist anymore
+        analyticsSqlDao.deleteByAccountRecordId(businessFieldModelDao.getTableName(), accountRecordId, tenantRecordId, callContext);
+        Assert.assertEquals(analyticsSqlDao.getInvoiceFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+    }
+
+    @Test(groups = "slow")
+    public void testSqlDaoForInvoicePaymentField() throws Exception {
+        final BusinessFieldModelDao businessFieldModelDao = new BusinessInvoicePaymentFieldModelDao(account,
+                                                                                                    accountRecordId,
+                                                                                                    customField,
+                                                                                                    fieldRecordId,
+                                                                                                    auditLog,
+                                                                                                    tenantRecordId,
+                                                                                                    reportGroup);
+        // Check the record doesn't exist yet
+        Assert.assertEquals(analyticsSqlDao.getInvoicePaymentFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+
+        // Create and check we can retrieve it
+        analyticsSqlDao.create(businessFieldModelDao.getTableName(), businessFieldModelDao, callContext);
+        Assert.assertEquals(analyticsSqlDao.getInvoicePaymentFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 1);
+        Assert.assertEquals(analyticsSqlDao.getInvoicePaymentFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).get(0), businessFieldModelDao);
+
+        // Delete and verify it doesn't exist anymore
+        analyticsSqlDao.deleteByAccountRecordId(businessFieldModelDao.getTableName(), accountRecordId, tenantRecordId, callContext);
+        Assert.assertEquals(analyticsSqlDao.getInvoicePaymentFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+    }
+
+    @Test(groups = "slow")
+    public void testSqlDaoForInvoice() throws Exception {
+        final BusinessInvoiceModelDao businessInvoiceModelDao = new BusinessInvoiceModelDao(account,
+                                                                                            accountRecordId,
+                                                                                            invoice,
+                                                                                            invoiceRecordId,
+                                                                                            auditLog,
+                                                                                            tenantRecordId,
+                                                                                            reportGroup);
+        // Check the record doesn't exist yet
+        Assert.assertEquals(analyticsSqlDao.getInvoicesByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+
+        // Create and check we can retrieve it
+        analyticsSqlDao.create(businessInvoiceModelDao.getTableName(), businessInvoiceModelDao, callContext);
+        Assert.assertEquals(analyticsSqlDao.getInvoicesByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 1);
+        Assert.assertEquals(analyticsSqlDao.getInvoicesByAccountRecordId(accountRecordId, tenantRecordId, callContext).get(0), businessInvoiceModelDao);
+
+        // Delete and verify it doesn't exist anymore
+        analyticsSqlDao.deleteByAccountRecordId(businessInvoiceModelDao.getTableName(), accountRecordId, tenantRecordId, callContext);
+        Assert.assertEquals(analyticsSqlDao.getInvoicesByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+    }
+
+    @Test(groups = "slow")
+    public void testSqlDaoForInvoiceItem() throws Exception {
+        final BusinessInvoiceItemBaseModelDao businessInvoiceItemModelDao = BusinessInvoiceItemBaseModelDao.create(account,
+                                                                                                                   accountRecordId,
+                                                                                                                   invoice,
+                                                                                                                   invoiceItem,
+                                                                                                                   recognizable,
+                                                                                                                   // ITEM_ADJ
+                                                                                                                   invoiceItemType,
+                                                                                                                   invoiceItemRecordId,
+                                                                                                                   secondInvoiceItemRecordId,
+                                                                                                                   bundle,
+                                                                                                                   plan,
+                                                                                                                   phase,
+                                                                                                                   auditLog,
+                                                                                                                   tenantRecordId,
+                                                                                                                   reportGroup);
+        // Check the record doesn't exist yet
+        Assert.assertEquals(analyticsSqlDao.getInvoiceItemAdjustmentsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+
+        // Create and check we can retrieve it
+        analyticsSqlDao.create(businessInvoiceItemModelDao.getTableName(), businessInvoiceItemModelDao, callContext);
+        Assert.assertEquals(analyticsSqlDao.getInvoiceItemAdjustmentsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 1);
+        Assert.assertEquals(analyticsSqlDao.getInvoiceItemAdjustmentsByAccountRecordId(accountRecordId, tenantRecordId, callContext).get(0), businessInvoiceItemModelDao);
+
+        // Delete and verify it doesn't exist anymore
+        analyticsSqlDao.deleteByAccountRecordId(businessInvoiceItemModelDao.getTableName(), accountRecordId, tenantRecordId, callContext);
+        Assert.assertEquals(analyticsSqlDao.getInvoiceItemAdjustmentsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+    }
+
+    @Test(groups = "slow")
+    public void testSqlDaoForInvoicePayment() throws Exception {
+        final BusinessInvoicePaymentBaseModelDao businessInvoicePaymentModelDao = BusinessInvoicePaymentModelDao.create(account,
+                                                                                                                        accountRecordId,
+                                                                                                                        invoice,
+                                                                                                                        invoicePayment,
+                                                                                                                        invoicePaymentRecordId,
+                                                                                                                        payment,
+                                                                                                                        refund,
+                                                                                                                        paymentMethod,
+                                                                                                                        auditLog,
+                                                                                                                        tenantRecordId,
+                                                                                                                        reportGroup);
+        // Check the record doesn't exist yet
+        Assert.assertEquals(analyticsSqlDao.getInvoicePaymentsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+
+        // Create and check we can retrieve it
+        analyticsSqlDao.create(businessInvoicePaymentModelDao.getTableName(), businessInvoicePaymentModelDao, callContext);
+        Assert.assertEquals(analyticsSqlDao.getInvoicePaymentsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 1);
+        Assert.assertEquals(analyticsSqlDao.getInvoicePaymentsByAccountRecordId(accountRecordId, tenantRecordId, callContext).get(0), businessInvoicePaymentModelDao);
+
+        // Delete and verify it doesn't exist anymore
+        analyticsSqlDao.deleteByAccountRecordId(businessInvoicePaymentModelDao.getTableName(), accountRecordId, tenantRecordId, callContext);
+        Assert.assertEquals(analyticsSqlDao.getInvoicePaymentsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+    }
+
+    @Test(groups = "slow")
+    public void testSqlDaoForOverdueStatus() throws Exception {
+        final DateTime endDate = new DateTime(2005, 6, 5, 4, 5, 6, DateTimeZone.UTC);
+        final BusinessOverdueStatusModelDao businessOverdueStatusModelDao = new BusinessOverdueStatusModelDao(account,
+                                                                                                              accountRecordId,
+                                                                                                              bundle,
+                                                                                                              blockingState,
+                                                                                                              blockingStateRecordId,
+                                                                                                              endDate,
+                                                                                                              auditLog,
+                                                                                                              tenantRecordId,
+                                                                                                              reportGroup);
+        // Check the record doesn't exist yet
+        Assert.assertEquals(analyticsSqlDao.getInvoicePaymentsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+
+        // Create and check we can retrieve it
+        analyticsSqlDao.create(businessOverdueStatusModelDao.getTableName(), businessOverdueStatusModelDao, callContext);
+        Assert.assertEquals(analyticsSqlDao.getOverdueStatusesByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 1);
+        Assert.assertEquals(analyticsSqlDao.getOverdueStatusesByAccountRecordId(accountRecordId, tenantRecordId, callContext).get(0), businessOverdueStatusModelDao);
+
+        // Delete and verify it doesn't exist anymore
+        analyticsSqlDao.deleteByAccountRecordId(businessOverdueStatusModelDao.getTableName(), accountRecordId, tenantRecordId, callContext);
+        Assert.assertEquals(analyticsSqlDao.getOverdueStatusesByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+    }
+
+    @Test(groups = "slow")
+    public void testSqlDaoForSubscriptionTransition() throws Exception {
+        final DateTime startDate = new DateTime(2012, 6, 5, 4, 3, 12, DateTimeZone.UTC);
+        final DateTime requestedTimestamp = new DateTime(2012, 7, 21, 10, 10, 10, DateTimeZone.UTC);
+
+        final BusinessSubscriptionEvent event = BusinessSubscriptionEvent.valueOf("ADD_BASE");
+        final BusinessSubscription previousSubscription = null;
+        final BusinessSubscription nextSubscription = new BusinessSubscription(null, null, null, Currency.GBP, startDate, SubscriptionState.ACTIVE);
+        final BusinessSubscriptionTransitionModelDao businessSubscriptionTransitionModelDao = new BusinessSubscriptionTransitionModelDao(account,
+                                                                                                                                         accountRecordId,
+                                                                                                                                         bundle,
+                                                                                                                                         subscriptionTransition,
+                                                                                                                                         subscriptionEventRecordId,
+                                                                                                                                         requestedTimestamp,
+                                                                                                                                         event,
+                                                                                                                                         previousSubscription,
+                                                                                                                                         nextSubscription,
+                                                                                                                                         auditLog,
+                                                                                                                                         tenantRecordId,
+                                                                                                                                         reportGroup);
+        // Check the record doesn't exist yet
+        Assert.assertEquals(analyticsSqlDao.getSubscriptionTransitionsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+
+        // Create and check we can retrieve it
+        analyticsSqlDao.create(businessSubscriptionTransitionModelDao.getTableName(), businessSubscriptionTransitionModelDao, callContext);
+        Assert.assertEquals(analyticsSqlDao.getSubscriptionTransitionsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 1);
+        Assert.assertEquals(analyticsSqlDao.getSubscriptionTransitionsByAccountRecordId(accountRecordId, tenantRecordId, callContext).get(0), businessSubscriptionTransitionModelDao);
+
+        // Delete and verify it doesn't exist anymore
+        analyticsSqlDao.deleteByAccountRecordId(businessSubscriptionTransitionModelDao.getTableName(), accountRecordId, tenantRecordId, callContext);
+        Assert.assertEquals(analyticsSqlDao.getSubscriptionTransitionsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+    }
+
+    @Test(groups = "slow")
+    public void testSqlDaoForAccountTag() throws Exception {
+        final BusinessTagModelDao businessTagModelDao = new BusinessAccountTagModelDao(account,
+                                                                                       accountRecordId,
+                                                                                       tag,
+                                                                                       tagRecordId,
+                                                                                       tagDefinition,
+                                                                                       auditLog,
+                                                                                       tenantRecordId,
+                                                                                       reportGroup);
+        // Check the record doesn't exist yet
+        Assert.assertEquals(analyticsSqlDao.getAccountTagsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+
+        // Create and check we can retrieve it
+        analyticsSqlDao.create(businessTagModelDao.getTableName(), businessTagModelDao, callContext);
+        Assert.assertEquals(analyticsSqlDao.getAccountTagsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 1);
+        Assert.assertEquals(analyticsSqlDao.getAccountTagsByAccountRecordId(accountRecordId, tenantRecordId, callContext).get(0), businessTagModelDao);
+
+        // Delete and verify it doesn't exist anymore
+        analyticsSqlDao.deleteByAccountRecordId(businessTagModelDao.getTableName(), accountRecordId, tenantRecordId, callContext);
+        Assert.assertEquals(analyticsSqlDao.getAccountTagsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+    }
+
+    @Test(groups = "slow")
+    public void testSqlDaoForInvoiceTag() throws Exception {
+        final BusinessTagModelDao businessTagModelDao = new BusinessInvoiceTagModelDao(account,
+                                                                                       accountRecordId,
+                                                                                       tag,
+                                                                                       tagRecordId,
+                                                                                       tagDefinition,
+                                                                                       auditLog,
+                                                                                       tenantRecordId,
+                                                                                       reportGroup);
+        // Check the record doesn't exist yet
+        Assert.assertEquals(analyticsSqlDao.getInvoiceTagsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+
+        // Create and check we can retrieve it
+        analyticsSqlDao.create(businessTagModelDao.getTableName(), businessTagModelDao, callContext);
+        Assert.assertEquals(analyticsSqlDao.getInvoiceTagsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 1);
+        Assert.assertEquals(analyticsSqlDao.getInvoiceTagsByAccountRecordId(accountRecordId, tenantRecordId, callContext).get(0), businessTagModelDao);
+
+        // Delete and verify it doesn't exist anymore
+        analyticsSqlDao.deleteByAccountRecordId(businessTagModelDao.getTableName(), accountRecordId, tenantRecordId, callContext);
+        Assert.assertEquals(analyticsSqlDao.getInvoiceTagsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+    }
+
+    @Test(groups = "slow")
+    public void testSqlDaoForInvoicePaymentTag() throws Exception {
+        final BusinessTagModelDao businessTagModelDao = new BusinessInvoicePaymentTagModelDao(account,
+                                                                                              accountRecordId,
+                                                                                              tag,
+                                                                                              tagRecordId,
+                                                                                              tagDefinition,
+                                                                                              auditLog,
+                                                                                              tenantRecordId,
+                                                                                              reportGroup);
+        // Check the record doesn't exist yet
+        Assert.assertEquals(analyticsSqlDao.getInvoicePaymentTagsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+
+        // Create and check we can retrieve it
+        analyticsSqlDao.create(businessTagModelDao.getTableName(), businessTagModelDao, callContext);
+        Assert.assertEquals(analyticsSqlDao.getInvoicePaymentTagsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 1);
+        Assert.assertEquals(analyticsSqlDao.getInvoicePaymentTagsByAccountRecordId(accountRecordId, tenantRecordId, callContext).get(0), businessTagModelDao);
+
+        // Delete and verify it doesn't exist anymore
+        analyticsSqlDao.deleteByAccountRecordId(businessTagModelDao.getTableName(), accountRecordId, tenantRecordId, callContext);
+        Assert.assertEquals(analyticsSqlDao.getInvoicePaymentTagsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
+    }
 }