killbill-memoizeit

Changes

Details

diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/api/BusinessInvoicePayment.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/api/BusinessInvoicePayment.java
index 1b05d1a..e2b2d82 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/api/BusinessInvoicePayment.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/api/BusinessInvoicePayment.java
@@ -39,10 +39,13 @@ public class BusinessInvoicePayment extends BusinessEntityBase {
     private final BigDecimal invoiceOriginalAmountCharged;
     private final BigDecimal invoiceAmountCredited;
     private final String invoicePaymentType;
+    private final UUID paymentId;
+    private final UUID refundId;
     private final Long paymentNumber;
     private final UUID linkedInvoicePaymentId;
     private final BigDecimal amount;
     private final String currency;
+    private final String pluginName;
     private final DateTime pluginCreatedDate;
     private final DateTime pluginEffectiveDate;
     private final String pluginStatus;
@@ -87,10 +90,13 @@ public class BusinessInvoicePayment extends BusinessEntityBase {
         this.invoiceOriginalAmountCharged = businessInvoicePaymentBaseModelDao.getInvoiceOriginalAmountCharged();
         this.invoiceAmountCredited = businessInvoicePaymentBaseModelDao.getInvoiceAmountCredited();
         this.invoicePaymentType = businessInvoicePaymentBaseModelDao.getInvoicePaymentType();
+        this.paymentId = businessInvoicePaymentBaseModelDao.getPaymentId();
+        this.refundId = businessInvoicePaymentBaseModelDao.getRefundId();
         this.paymentNumber = businessInvoicePaymentBaseModelDao.getPaymentNumber();
         this.linkedInvoicePaymentId = businessInvoicePaymentBaseModelDao.getLinkedInvoicePaymentId();
         this.amount = businessInvoicePaymentBaseModelDao.getAmount();
         this.currency = businessInvoicePaymentBaseModelDao.getCurrency();
+        this.pluginName = businessInvoicePaymentBaseModelDao.getPluginName();
         this.pluginCreatedDate = businessInvoicePaymentBaseModelDao.getPluginCreatedDate();
         this.pluginEffectiveDate = businessInvoicePaymentBaseModelDao.getPluginEffectiveDate();
         this.pluginStatus = businessInvoicePaymentBaseModelDao.getPluginStatus();
@@ -166,6 +172,14 @@ public class BusinessInvoicePayment extends BusinessEntityBase {
         return invoicePaymentType;
     }
 
+    public UUID getPaymentId() {
+        return paymentId;
+    }
+
+    public UUID getRefundId() {
+        return refundId;
+    }
+
     public Long getPaymentNumber() {
         return paymentNumber;
     }
@@ -182,6 +196,10 @@ public class BusinessInvoicePayment extends BusinessEntityBase {
         return currency;
     }
 
+    public String getPluginName() {
+        return pluginName;
+    }
+
     public DateTime getPluginCreatedDate() {
         return pluginCreatedDate;
     }
@@ -282,10 +300,13 @@ public class BusinessInvoicePayment extends BusinessEntityBase {
         sb.append(", invoiceOriginalAmountCharged=").append(invoiceOriginalAmountCharged);
         sb.append(", invoiceAmountCredited=").append(invoiceAmountCredited);
         sb.append(", invoicePaymentType='").append(invoicePaymentType).append('\'');
+        sb.append(", paymentId=").append(paymentId);
+        sb.append(", refundId=").append(refundId);
         sb.append(", paymentNumber=").append(paymentNumber);
         sb.append(", linkedInvoicePaymentId=").append(linkedInvoicePaymentId);
         sb.append(", amount=").append(amount);
         sb.append(", currency='").append(currency).append('\'');
+        sb.append(", pluginName='").append(pluginName).append('\'');
         sb.append(", pluginCreatedDate=").append(pluginCreatedDate);
         sb.append(", pluginEffectiveDate=").append(pluginEffectiveDate);
         sb.append(", pluginStatus='").append(pluginStatus).append('\'');
@@ -373,9 +394,18 @@ public class BusinessInvoicePayment extends BusinessEntityBase {
         if (linkedInvoicePaymentId != null ? !linkedInvoicePaymentId.equals(that.linkedInvoicePaymentId) : that.linkedInvoicePaymentId != null) {
             return false;
         }
+        if (paymentId != null ? !paymentId.equals(that.paymentId) : that.paymentId != null) {
+            return false;
+        }
+        if (refundId != null ? !refundId.equals(that.refundId) : that.refundId != null) {
+            return false;
+        }
         if (paymentNumber != null ? !paymentNumber.equals(that.paymentNumber) : that.paymentNumber != null) {
             return false;
         }
+        if (pluginName != null ? !pluginName.equals(that.pluginName) : that.pluginName != null) {
+            return false;
+        }
         if (pluginCreatedDate != null ? !pluginCreatedDate.equals(that.pluginCreatedDate) : that.pluginCreatedDate != null) {
             return false;
         }
@@ -459,10 +489,13 @@ public class BusinessInvoicePayment extends BusinessEntityBase {
         result = 31 * result + (invoiceOriginalAmountCharged != null ? invoiceOriginalAmountCharged.hashCode() : 0);
         result = 31 * result + (invoiceAmountCredited != null ? invoiceAmountCredited.hashCode() : 0);
         result = 31 * result + (invoicePaymentType != null ? invoicePaymentType.hashCode() : 0);
+        result = 31 * result + (paymentId != null ? paymentId.hashCode() : 0);
+        result = 31 * result + (refundId != null ? refundId.hashCode() : 0);
         result = 31 * result + (paymentNumber != null ? paymentNumber.hashCode() : 0);
         result = 31 * result + (linkedInvoicePaymentId != null ? linkedInvoicePaymentId.hashCode() : 0);
         result = 31 * result + (amount != null ? amount.hashCode() : 0);
         result = 31 * result + (currency != null ? currency.hashCode() : 0);
+        result = 31 * result + (pluginName != null ? pluginName.hashCode() : 0);
         result = 31 * result + (pluginCreatedDate != null ? pluginCreatedDate.hashCode() : 0);
         result = 31 * result + (pluginEffectiveDate != null ? pluginEffectiveDate.hashCode() : 0);
         result = 31 * result + (pluginStatus != null ? pluginStatus.hashCode() : 0);
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/BusinessAnalyticsBase.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/BusinessAnalyticsBase.java
index 98df258..5d5589f 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/BusinessAnalyticsBase.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/BusinessAnalyticsBase.java
@@ -69,7 +69,12 @@ import com.ning.billing.util.tag.TagDefinition;
 import com.ning.killbill.osgi.libs.killbill.OSGIKillbillAPI;
 import com.ning.killbill.osgi.libs.killbill.OSGIKillbillLogService;
 
-// Wrapper around Kill Bill APIs
+/**
+ * Wrapper around Kill Bill APIs
+ * <p/>
+ * Note: the code is merciful in case audit logs cannot be retrieved. This is because the auditing code
+ * is fairly recent, and we want this plugin to support early versions of Kill Bill (with non audited data).
+ */
 public abstract class BusinessAnalyticsBase {
 
     protected final OSGIKillbillLogService logService;
@@ -84,9 +89,16 @@ public abstract class BusinessAnalyticsBase {
     // TENANT
     //
 
+    private static final long INTERNAL_TENANT_RECORD_ID = 0L;
+
     protected Long getTenantRecordId(final TenantContext context) throws AnalyticsRefreshException {
-        final RecordIdApi recordIdUserApi = getRecordIdUserApi();
-        return recordIdUserApi.getRecordId(context.getTenantId(), ObjectType.TENANT, context);
+        // See convention in InternalCallContextFactory
+        if (context.getTenantId() == null) {
+            return INTERNAL_TENANT_RECORD_ID;
+        } else {
+            final RecordIdApi recordIdUserApi = getRecordIdUserApi();
+            return recordIdUserApi.getRecordId(context.getTenantId(), ObjectType.TENANT, context);
+        }
     }
 
     //
@@ -112,7 +124,8 @@ public abstract class BusinessAnalyticsBase {
             }
         }
 
-        throw new AnalyticsRefreshException("Unable to find Account creation audit log for id " + accountId);
+        logService.log(LogService.LOG_WARNING, "Unable to find Account creation audit log for id " + accountId);
+        return null;
     }
 
     protected Long getAccountRecordId(final UUID accountId, final TenantContext context) throws AnalyticsRefreshException {
@@ -182,7 +195,8 @@ public abstract class BusinessAnalyticsBase {
             }
         }
 
-        throw new AnalyticsRefreshException("Unable to find Bundle creation audit log for id " + bundleId);
+        logService.log(LogService.LOG_WARNING, "Unable to find Bundle creation audit log for id " + bundleId);
+        return null;
     }
 
     protected Subscription getSubscription(final UUID subscriptionId, final TenantContext context) throws AnalyticsRefreshException {
@@ -204,7 +218,8 @@ public abstract class BusinessAnalyticsBase {
             }
         }
 
-        throw new AnalyticsRefreshException("Unable to find Subscription event creation audit log for id " + subscriptionEventId);
+        logService.log(LogService.LOG_WARNING, "Unable to find Subscription event creation audit log for id " + subscriptionEventId);
+        return null;
     }
 
     protected Long getSubscriptionEventRecordId(final UUID subscriptionEventId, final TenantContext context) throws AnalyticsRefreshException {
@@ -233,7 +248,8 @@ public abstract class BusinessAnalyticsBase {
             }
         }
 
-        throw new AnalyticsRefreshException("Unable to find Blocking state creation audit log for id " + blockingStateId);
+        logService.log(LogService.LOG_WARNING, "Unable to find Blocking state creation audit log for id " + blockingStateId);
+        return null;
     }
 
     protected Long getBlockingStateRecordId(final UUID blockingStateId, final TenantContext context) throws AnalyticsRefreshException {
@@ -263,7 +279,8 @@ public abstract class BusinessAnalyticsBase {
             }
         }
 
-        throw new AnalyticsRefreshException("Unable to find Invoice creation audit log for id " + invoiceId);
+        logService.log(LogService.LOG_WARNING, "Unable to find Invoice creation audit log for id " + invoiceId);
+        return null;
     }
 
     protected Long getInvoiceRecordId(final UUID invoiceId, final TenantContext context) throws AnalyticsRefreshException {
@@ -279,7 +296,8 @@ public abstract class BusinessAnalyticsBase {
             }
         }
 
-        throw new AnalyticsRefreshException("Unable to find Invoice item creation audit log for id " + invoiceItemId);
+        logService.log(LogService.LOG_WARNING, "Unable to find Invoice item creation audit log for id " + invoiceItemId);
+        return null;
     }
 
     protected Long getInvoiceItemRecordId(final UUID invoiceItemId, final TenantContext context) throws AnalyticsRefreshException {
@@ -353,7 +371,8 @@ public abstract class BusinessAnalyticsBase {
             }
         }
 
-        throw new AnalyticsRefreshException("Unable to find Invoice payment creation audit log for id " + invoicePaymentId);
+        logService.log(LogService.LOG_WARNING, "Unable to find Invoice payment creation audit log for id " + invoicePaymentId);
+        return null;
     }
 
     protected Long getInvoicePaymentRecordId(final UUID invoicePaymentId, final TenantContext context) throws AnalyticsRefreshException {
@@ -397,26 +416,14 @@ public abstract class BusinessAnalyticsBase {
         }
     }
 
-    protected Collection<PaymentMethod> getAccountPaymentMethods(final UUID accountId, final TenantContext context) throws AnalyticsRefreshException {
-        final Account account = getAccount(accountId, context);
-
-        final PaymentApi paymentApi = getPaymentUserApi();
-        try {
-            return paymentApi.getPaymentMethods(account, true, context);
-        } catch (PaymentApiException e) {
-            logService.log(LogService.LOG_WARNING, "Error retrieving payment methods for account id " + accountId, e);
-            throw new AnalyticsRefreshException(e);
-        }
-    }
-
     protected PaymentMethod getPaymentMethod(final UUID paymentMethodId, final TenantContext context) throws AnalyticsRefreshException {
         final PaymentApi paymentApi = getPaymentUserApi();
 
         try {
             return paymentApi.getPaymentMethodById(paymentMethodId, true, true, context);
         } catch (PaymentApiException e) {
-            logService.log(LogService.LOG_WARNING, "Error retrieving payment method for id " + paymentMethodId, e);
-            throw new AnalyticsRefreshException(e);
+            logService.log(LogService.LOG_INFO, "Error retrieving payment method for id " + paymentMethodId + " (already deleted?)", e);
+            return null;
         }
     }
 
@@ -437,7 +444,8 @@ public abstract class BusinessAnalyticsBase {
             }
         }
 
-        throw new AnalyticsRefreshException("Unable to find Field creation audit log for id " + fieldId);
+        logService.log(LogService.LOG_WARNING, "Unable to find Field creation audit log for id " + fieldId);
+        return null;
     }
 
     protected Long getFieldRecordId(final UUID fieldId, final TenantContext context) throws AnalyticsRefreshException {
@@ -473,7 +481,8 @@ public abstract class BusinessAnalyticsBase {
             }
         }
 
-        throw new AnalyticsRefreshException("Unable to find Tag creation audit log for id " + tagId);
+        logService.log(LogService.LOG_WARNING, "Unable to find Tag creation audit log for id " + tagId);
+        return null;
     }
 
     protected Long getTagRecordId(final UUID tagId, final TenantContext context) throws AnalyticsRefreshException {
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessAccountFieldModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessAccountFieldModelDao.java
index 2ff0a94..42a1024 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessAccountFieldModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessAccountFieldModelDao.java
@@ -30,7 +30,7 @@ public class BusinessAccountFieldModelDao extends BusinessFieldModelDao {
                                         final Long accountRecordId,
                                         final CustomField customField,
                                         final Long customFieldRecordId,
-                                        final AuditLog creationAuditLog,
+                                        @Nullable final AuditLog creationAuditLog,
                                         final Long tenantRecordId,
                                         @Nullable final ReportGroup reportGroup) {
         super(account,
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessAccountModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessAccountModelDao.java
index 234114f..9dea26d 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessAccountModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessAccountModelDao.java
@@ -133,7 +133,7 @@ public class BusinessAccountModelDao extends BusinessModelDaoBase {
                                    @Nullable final Invoice lastInvoice,
                                    @Nullable final Payment lastPayment,
                                    final Integer nbActiveBundles,
-                                   final AuditLog creationAuditLog,
+                                   @Nullable final AuditLog creationAuditLog,
                                    final Long tenantRecordId,
                                    @Nullable final ReportGroup reportGroup) {
         this(account.getEmail(),
@@ -160,9 +160,9 @@ public class BusinessAccountModelDao extends BusinessModelDaoBase {
              lastPayment == null ? null : lastPayment.getPaymentStatus().toString(),
              nbActiveBundles,
              account.getCreatedDate(),
-             creationAuditLog.getUserName(),
-             creationAuditLog.getReasonCode(),
-             creationAuditLog.getComment(),
+             creationAuditLog != null ? creationAuditLog.getUserName() : null,
+             creationAuditLog != null ? creationAuditLog.getReasonCode() : null,
+             creationAuditLog != null ? creationAuditLog.getComment() : null,
              account.getId(),
              account.getName(),
              account.getExternalKey(),
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessAccountTagModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessAccountTagModelDao.java
index 55ef70d..219310e 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessAccountTagModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessAccountTagModelDao.java
@@ -32,7 +32,7 @@ public class BusinessAccountTagModelDao extends BusinessTagModelDao {
                                       final Tag tag,
                                       final Long tagRecordId,
                                       final TagDefinition tagDefinition,
-                                      final AuditLog creationAuditLog,
+                                      @Nullable final AuditLog creationAuditLog,
                                       final Long tenantRecordId,
                                       @Nullable final ReportGroup reportGroup) {
         super(account,
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessBundleSummaryModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessBundleSummaryModelDao.java
index 535d09c..fc7e2a4 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessBundleSummaryModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessBundleSummaryModelDao.java
@@ -109,7 +109,7 @@ public class BusinessBundleSummaryModelDao extends BusinessModelDaoBase {
                                          final Long bundleRecordId,
                                          final Integer bundleAccountRank,
                                          final BusinessSubscriptionTransitionModelDao bst,
-                                         final AuditLog creationAuditLog,
+                                         @Nullable final AuditLog creationAuditLog,
                                          final Long tenantRecordId,
                                          @Nullable final ReportGroup reportGroup) {
         this(bundleRecordId,
@@ -119,9 +119,9 @@ public class BusinessBundleSummaryModelDao extends BusinessModelDaoBase {
              bundleAccountRank,
              bst,
              bundle.getCreatedDate(),
-             creationAuditLog.getUserName(),
-             creationAuditLog.getReasonCode(),
-             creationAuditLog.getComment(),
+             creationAuditLog != null ? creationAuditLog.getUserName() : null,
+             creationAuditLog != null ? creationAuditLog.getReasonCode() : null,
+             creationAuditLog != null ? creationAuditLog.getComment() : null,
              account.getId(),
              account.getName(),
              account.getExternalKey(),
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessFieldModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessFieldModelDao.java
index a3b2904..9e71e90 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessFieldModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessFieldModelDao.java
@@ -43,7 +43,7 @@ public abstract class BusinessFieldModelDao extends BusinessModelDaoBase {
                                                final Long accountRecordId,
                                                final CustomField customField,
                                                final Long customFieldRecordId,
-                                               final AuditLog creationAuditLog,
+                                               @Nullable final AuditLog creationAuditLog,
                                                final Long tenantRecordId,
                                                @Nullable final ReportGroup reportGroup) {
         if (ObjectType.ACCOUNT.equals(customField.getObjectType())) {
@@ -110,16 +110,16 @@ public abstract class BusinessFieldModelDao extends BusinessModelDaoBase {
                                  final Long accountRecordId,
                                  final CustomField customField,
                                  final Long customFieldRecordId,
-                                 final AuditLog creationAuditLog,
+                                 @Nullable final AuditLog creationAuditLog,
                                  final Long tenantRecordId,
                                  @Nullable final ReportGroup reportGroup) {
         this(customFieldRecordId,
              customField.getFieldName(),
              customField.getFieldValue(),
              customField.getCreatedDate(),
-             creationAuditLog.getUserName(),
-             creationAuditLog.getReasonCode(),
-             creationAuditLog.getComment(),
+             creationAuditLog != null ? creationAuditLog.getUserName() : null,
+             creationAuditLog != null ? creationAuditLog.getReasonCode() : null,
+             creationAuditLog != null ? creationAuditLog.getComment() : null,
              account.getId(),
              account.getName(),
              account.getExternalKey(),
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceAdjustmentModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceAdjustmentModelDao.java
index 2114226..8c64231 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceAdjustmentModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceAdjustmentModelDao.java
@@ -40,7 +40,7 @@ public class BusinessInvoiceAdjustmentModelDao extends BusinessInvoiceItemBaseMo
                                              @Nullable final SubscriptionBundle bundle,
                                              @Nullable final Plan plan,
                                              @Nullable final PlanPhase planPhase,
-                                             final AuditLog creationAuditLog,
+                                             @Nullable final AuditLog creationAuditLog,
                                              final Long tenantRecordId,
                                              @Nullable final ReportGroup reportGroup) {
         super(account,
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceFieldModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceFieldModelDao.java
index 4b4a137..e68c344 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceFieldModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceFieldModelDao.java
@@ -34,7 +34,7 @@ public class BusinessInvoiceFieldModelDao extends BusinessFieldModelDao {
                                         final Long accountRecordId,
                                         final CustomField customField,
                                         final Long customFieldRecordId,
-                                        final AuditLog creationAuditLog,
+                                        @Nullable final AuditLog creationAuditLog,
                                         final Long tenantRecordId,
                                         @Nullable final ReportGroup reportGroup) {
         super(account,
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceItemAdjustmentModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceItemAdjustmentModelDao.java
index bbc2af6..fca1e6a 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceItemAdjustmentModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceItemAdjustmentModelDao.java
@@ -40,7 +40,7 @@ public class BusinessInvoiceItemAdjustmentModelDao extends BusinessInvoiceItemBa
                                                  @Nullable final SubscriptionBundle bundle,
                                                  @Nullable final Plan plan,
                                                  @Nullable final PlanPhase planPhase,
-                                                 final AuditLog creationAuditLog,
+                                                 @Nullable final AuditLog creationAuditLog,
                                                  final Long tenantRecordId,
                                                  @Nullable final ReportGroup reportGroup) {
         super(account,
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 cd70b84..f0fcef2 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
@@ -88,7 +88,7 @@ public abstract class BusinessInvoiceItemBaseModelDao extends BusinessModelDaoBa
                                                          @Nullable final SubscriptionBundle bundle,
                                                          @Nullable final Plan plan,
                                                          @Nullable final PlanPhase planPhase,
-                                                         final AuditLog creationAuditLog,
+                                                         @Nullable final AuditLog creationAuditLog,
                                                          final Long tenantRecordId,
                                                          @Nullable final ReportGroup reportGroup) {
         if (BusinessInvoiceItemType.INVOICE_ADJUSTMENT.equals(businessInvoiceItemType)) {
@@ -243,7 +243,7 @@ public abstract class BusinessInvoiceItemBaseModelDao extends BusinessModelDaoBa
                                            @Nullable final SubscriptionBundle bundle,
                                            @Nullable final Plan plan,
                                            @Nullable final PlanPhase planPhase,
-                                           final AuditLog creationAuditLog,
+                                           @Nullable final AuditLog creationAuditLog,
                                            final Long tenantRecordId,
                                            @Nullable final ReportGroup reportGroup) {
         this(invoiceItemRecordId,
@@ -276,9 +276,9 @@ public abstract class BusinessInvoiceItemBaseModelDao extends BusinessModelDaoBa
              invoiceItem.getCurrency() == null ? null : invoiceItem.getCurrency().toString(),
              invoiceItem.getLinkedItemId(),
              invoiceItem.getCreatedDate(),
-             creationAuditLog.getUserName(),
-             creationAuditLog.getReasonCode(),
-             creationAuditLog.getComment(),
+             creationAuditLog != null ? creationAuditLog.getUserName() : null,
+             creationAuditLog != null ? creationAuditLog.getReasonCode() : null,
+             creationAuditLog != null ? creationAuditLog.getComment() : null,
              account.getId(),
              account.getName(),
              account.getExternalKey(),
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceItemCreditModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceItemCreditModelDao.java
index bf26129..f9557d2 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceItemCreditModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceItemCreditModelDao.java
@@ -40,7 +40,7 @@ public class BusinessInvoiceItemCreditModelDao extends BusinessInvoiceItemBaseMo
                                              @Nullable final SubscriptionBundle bundle,
                                              @Nullable final Plan plan,
                                              @Nullable final PlanPhase planPhase,
-                                             final AuditLog creationAuditLogs,
+                                             @Nullable final AuditLog creationAuditLogs,
                                              final Long tenantRecordId,
                                              @Nullable final ReportGroup reportGroup) {
         super(account,
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceItemModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceItemModelDao.java
index 1e5602a..bd7832c 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceItemModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceItemModelDao.java
@@ -40,7 +40,7 @@ public class BusinessInvoiceItemModelDao extends BusinessInvoiceItemBaseModelDao
                                        @Nullable final SubscriptionBundle bundle,
                                        @Nullable final Plan plan,
                                        @Nullable final PlanPhase planPhase,
-                                       final AuditLog creationAuditLog,
+                                       @Nullable final AuditLog creationAuditLog,
                                        final Long tenantRecordId,
                                        @Nullable final ReportGroup reportGroup) {
         super(account,
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 8cf25fb..650a86c 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
@@ -94,7 +94,7 @@ public class BusinessInvoiceModelDao extends BusinessModelDaoBase {
                                    final Long accountRecordId,
                                    final Invoice invoice,
                                    final Long invoiceRecordId,
-                                   final AuditLog creationAuditLog,
+                                   @Nullable final AuditLog creationAuditLog,
                                    final Long tenantRecordId,
                                    @Nullable final ReportGroup reportGroup) {
         this(invoiceRecordId,
@@ -109,9 +109,9 @@ public class BusinessInvoiceModelDao extends BusinessModelDaoBase {
              invoice.getOriginalChargedAmount(),
              invoice.getCreditAdjAmount(),
              invoice.getCreatedDate(),
-             creationAuditLog.getUserName(),
-             creationAuditLog.getReasonCode(),
-             creationAuditLog.getComment(),
+             creationAuditLog != null ? creationAuditLog.getUserName() : null,
+             creationAuditLog != null ? creationAuditLog.getReasonCode() : null,
+             creationAuditLog != null ? creationAuditLog.getComment() : null,
              account.getId(),
              account.getName(),
              account.getExternalKey(),
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 5a3523b..402c98d 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
@@ -33,8 +33,13 @@ import com.ning.billing.payment.api.PaymentMethod;
 import com.ning.billing.payment.api.Refund;
 import com.ning.billing.util.audit.AuditLog;
 
+import com.google.common.annotations.VisibleForTesting;
+
 public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDaoBase {
 
+    @VisibleForTesting
+    static final String DEFAULT_PLUGIN_NAME = "__UNKNOWN__";
+
     protected static final String INVOICE_PAYMENTS_TABLE_NAME = "bip";
     protected static final String INVOICE_PAYMENT_REFUNDS_TABLE_NAME = "bipr";
     protected static final String INVOICE_PAYMENT_CHARGEBACKS_TABLE_NAME = "bipc";
@@ -55,10 +60,13 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
     private BigDecimal invoiceOriginalAmountCharged;
     private BigDecimal invoiceAmountCredited;
     private String invoicePaymentType;
+    private UUID paymentId;
+    private UUID refundId;
     private Long paymentNumber;
     private UUID linkedInvoicePaymentId;
     private BigDecimal amount;
     private String currency;
+    private String pluginName;
     private DateTime pluginCreatedDate;
     private DateTime pluginEffectiveDate;
     private String pluginStatus;
@@ -88,8 +96,8 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
                                                             final Long invoicePaymentRecordId,
                                                             final Payment payment,
                                                             final Refund refund,
-                                                            final PaymentMethod paymentMethod,
-                                                            final AuditLog creationAuditLog,
+                                                            @Nullable final PaymentMethod paymentMethod,
+                                                            @Nullable final AuditLog creationAuditLog,
                                                             final Long tenantRecordId,
                                                             @Nullable final ReportGroup reportGroup) {
         if (invoicePayment.getType().equals(InvoicePaymentType.REFUND)) {
@@ -147,10 +155,13 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
                                               final BigDecimal invoiceOriginalAmountCharged,
                                               final BigDecimal invoiceAmountCredited,
                                               final String invoicePaymentType,
+                                              final UUID paymentId,
+                                              final UUID refundId,
                                               final Long paymentNumber,
                                               final UUID linkedInvoicePaymentId,
                                               final BigDecimal amount,
                                               final String currency,
+                                              final String pluginName,
                                               final DateTime pluginCreatedDate,
                                               final DateTime pluginEffectiveDate,
                                               final String pluginStatus,
@@ -206,10 +217,13 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
         this.invoiceOriginalAmountCharged = invoiceOriginalAmountCharged;
         this.invoiceAmountCredited = invoiceAmountCredited;
         this.invoicePaymentType = invoicePaymentType;
+        this.paymentId = paymentId;
+        this.refundId = refundId;
         this.paymentNumber = paymentNumber;
         this.linkedInvoicePaymentId = linkedInvoicePaymentId;
         this.amount = amount;
         this.currency = currency;
+        this.pluginName = pluginName;
         this.pluginCreatedDate = pluginCreatedDate;
         this.pluginEffectiveDate = pluginEffectiveDate;
         this.pluginStatus = pluginStatus;
@@ -240,8 +254,8 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
                                                  final Long invoicePaymentRecordId,
                                                  final Payment payment,
                                                  @Nullable final Refund refund,
-                                                 final PaymentMethod paymentMethod,
-                                                 final AuditLog creationAuditLog,
+                                                 @Nullable final PaymentMethod paymentMethod,
+                                                 @Nullable final AuditLog creationAuditLog,
                                                  final Long tenantRecordId,
                                                  @Nullable final ReportGroup reportGroup) {
         this(invoicePaymentRecordId,
@@ -258,10 +272,13 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
              invoice.getOriginalChargedAmount(),
              invoice.getCreditAdjAmount(),
              invoicePayment.getType().toString(),
+             payment.getId(),
+             refund != null ? refund.getId() : null,
              payment.getPaymentNumber() == null ? null : payment.getPaymentNumber().longValue(),
              invoicePayment.getLinkedInvoicePaymentId(),
              invoicePayment.getAmount(),
              invoicePayment.getCurrency() == null ? null : invoicePayment.getCurrency().toString(),
+             paymentMethod != null ? paymentMethod.getPluginName() : DEFAULT_PLUGIN_NAME,
              refund != null ? (refund.getPluginDetail() != null ? refund.getPluginDetail().getCreatedDate() : null) : (payment.getPaymentInfoPlugin() != null ? payment.getPaymentInfoPlugin().getCreatedDate() : null),
              refund != null ? (refund.getPluginDetail() != null ? refund.getPluginDetail().getEffectiveDate() : null) : (payment.getPaymentInfoPlugin() != null ? payment.getPaymentInfoPlugin().getEffectiveDate() : null),
              refund != null ? (refund.getPluginDetail() != null ? refund.getPluginDetail().getStatus().toString() : null) : (payment.getPaymentInfoPlugin() != null ? payment.getPaymentInfoPlugin().getStatus().toString() : null),
@@ -269,24 +286,24 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
              refund != null ? (refund.getPluginDetail() != null ? refund.getPluginDetail().getGatewayErrorCode() : null) : (payment.getPaymentInfoPlugin() != null ? payment.getPaymentInfoPlugin().getGatewayErrorCode() : null),
              refund != null ? (refund.getPluginDetail() != null ? refund.getPluginDetail().getReferenceId() : null) : (payment.getPaymentInfoPlugin() != null ? payment.getPaymentInfoPlugin().getFirstPaymentReferenceId() : null),
              refund != null ? null : (payment.getPaymentInfoPlugin() != null ? payment.getPaymentInfoPlugin().getSecondPaymentReferenceId() : null),
-             paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getExternalPaymentMethodId() : null,
-             paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().isDefaultPaymentMethod() : null,
-             paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getType() : null,
-             paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getCCName() : null,
-             paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getCCType() : null,
-             paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getCCExprirationMonth() : null,
-             paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getCCExprirationYear() : null,
-             paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getCCLast4() : null,
-             paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getAddress1() : null,
-             paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getAddress2() : null,
-             paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getCity() : null,
-             paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getState() : null,
-             paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getZip() : null,
-             paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getCountry() : null,
+             paymentMethod != null ? (paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getExternalPaymentMethodId() : null) : null,
+             paymentMethod != null ? (paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().isDefaultPaymentMethod() : null) : null,
+             paymentMethod != null ? (paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getType() : null) : null,
+             paymentMethod != null ? (paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getCCName() : null) : null,
+             paymentMethod != null ? (paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getCCType() : null) : null,
+             paymentMethod != null ? (paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getCCExprirationMonth() : null) : null,
+             paymentMethod != null ? (paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getCCExprirationYear() : null) : null,
+             paymentMethod != null ? (paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getCCLast4() : null) : null,
+             paymentMethod != null ? (paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getAddress1() : null) : null,
+             paymentMethod != null ? (paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getAddress2() : null) : null,
+             paymentMethod != null ? (paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getCity() : null) : null,
+             paymentMethod != null ? (paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getState() : null) : null,
+             paymentMethod != null ? (paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getZip() : null) : null,
+             paymentMethod != null ? (paymentMethod.getPluginDetail() != null ? paymentMethod.getPluginDetail().getCountry() : null) : null,
              invoicePayment.getCreatedDate(),
-             creationAuditLog.getUserName(),
-             creationAuditLog.getReasonCode(),
-             creationAuditLog.getComment(),
+             creationAuditLog != null ? creationAuditLog.getUserName() : null,
+             creationAuditLog != null ? creationAuditLog.getReasonCode() : null,
+             creationAuditLog != null ? creationAuditLog.getComment() : null,
              account.getId(),
              account.getName(),
              account.getExternalKey(),
@@ -351,6 +368,14 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
         return invoicePaymentType;
     }
 
+    public UUID getPaymentId() {
+        return paymentId;
+    }
+
+    public UUID getRefundId() {
+        return refundId;
+    }
+
     public Long getPaymentNumber() {
         return paymentNumber;
     }
@@ -367,6 +392,10 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
         return currency;
     }
 
+    public String getPluginName() {
+        return pluginName;
+    }
+
     public DateTime getPluginCreatedDate() {
         return pluginCreatedDate;
     }
@@ -453,9 +482,8 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
 
     @Override
     public String toString() {
-        final StringBuilder sb = new StringBuilder();
-        sb.append("BusinessInvoicePaymentBaseModelDao");
-        sb.append("{invoicePaymentRecordId=").append(invoicePaymentRecordId);
+        final StringBuilder sb = new StringBuilder("BusinessInvoicePaymentBaseModelDao{");
+        sb.append("invoicePaymentRecordId=").append(invoicePaymentRecordId);
         sb.append(", invoicePaymentId=").append(invoicePaymentId);
         sb.append(", invoiceId=").append(invoiceId);
         sb.append(", invoiceNumber=").append(invoiceNumber);
@@ -469,10 +497,13 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
         sb.append(", invoiceOriginalAmountCharged=").append(invoiceOriginalAmountCharged);
         sb.append(", invoiceAmountCredited=").append(invoiceAmountCredited);
         sb.append(", invoicePaymentType='").append(invoicePaymentType).append('\'');
+        sb.append(", paymentId=").append(paymentId);
+        sb.append(", refundId=").append(refundId);
         sb.append(", paymentNumber=").append(paymentNumber);
         sb.append(", linkedInvoicePaymentId=").append(linkedInvoicePaymentId);
         sb.append(", amount=").append(amount);
         sb.append(", currency='").append(currency).append('\'');
+        sb.append(", pluginName='").append(pluginName).append('\'');
         sb.append(", pluginCreatedDate=").append(pluginCreatedDate);
         sb.append(", pluginEffectiveDate=").append(pluginEffectiveDate);
         sb.append(", pluginStatus='").append(pluginStatus).append('\'');
@@ -563,6 +594,12 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
         if (linkedInvoicePaymentId != null ? !linkedInvoicePaymentId.equals(that.linkedInvoicePaymentId) : that.linkedInvoicePaymentId != null) {
             return false;
         }
+        if (paymentId != null ? !paymentId.equals(that.paymentId) : that.paymentId != null) {
+            return false;
+        }
+        if (refundId != null ? !refundId.equals(that.refundId) : that.refundId != null) {
+            return false;
+        }
         if (paymentNumber != null ? !paymentNumber.equals(that.paymentNumber) : that.paymentNumber != null) {
             return false;
         }
@@ -581,6 +618,9 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
         if (pluginGatewayErrorCode != null ? !pluginGatewayErrorCode.equals(that.pluginGatewayErrorCode) : that.pluginGatewayErrorCode != null) {
             return false;
         }
+        if (pluginName != null ? !pluginName.equals(that.pluginName) : that.pluginName != null) {
+            return false;
+        }
         if (pluginPmAddress1 != null ? !pluginPmAddress1.equals(that.pluginPmAddress1) : that.pluginPmAddress1 != null) {
             return false;
         }
@@ -650,10 +690,13 @@ public abstract class BusinessInvoicePaymentBaseModelDao extends BusinessModelDa
         result = 31 * result + (invoiceOriginalAmountCharged != null ? invoiceOriginalAmountCharged.hashCode() : 0);
         result = 31 * result + (invoiceAmountCredited != null ? invoiceAmountCredited.hashCode() : 0);
         result = 31 * result + (invoicePaymentType != null ? invoicePaymentType.hashCode() : 0);
+        result = 31 * result + (paymentId != null ? paymentId.hashCode() : 0);
+        result = 31 * result + (refundId != null ? refundId.hashCode() : 0);
         result = 31 * result + (paymentNumber != null ? paymentNumber.hashCode() : 0);
         result = 31 * result + (linkedInvoicePaymentId != null ? linkedInvoicePaymentId.hashCode() : 0);
         result = 31 * result + (amount != null ? amount.hashCode() : 0);
         result = 31 * result + (currency != null ? currency.hashCode() : 0);
+        result = 31 * result + (pluginName != null ? pluginName.hashCode() : 0);
         result = 31 * result + (pluginCreatedDate != null ? pluginCreatedDate.hashCode() : 0);
         result = 31 * result + (pluginEffectiveDate != null ? pluginEffectiveDate.hashCode() : 0);
         result = 31 * result + (pluginStatus != null ? pluginStatus.hashCode() : 0);
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentChargebackModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentChargebackModelDao.java
index d1a434a..42003e8 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentChargebackModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentChargebackModelDao.java
@@ -37,8 +37,8 @@ public class BusinessInvoicePaymentChargebackModelDao extends BusinessInvoicePay
                                                     final Long invoicePaymentRecordId,
                                                     final Payment payment,
                                                     final Refund refund,
-                                                    final PaymentMethod paymentMethod,
-                                                    final AuditLog creationAuditLog,
+                                                    @Nullable final PaymentMethod paymentMethod,
+                                                    @Nullable final AuditLog creationAuditLog,
                                                     final Long tenantRecordId,
                                                     @Nullable final ReportGroup reportGroup) {
         super(account,
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentFieldModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentFieldModelDao.java
index 2cd2b7b..687ccb5 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentFieldModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentFieldModelDao.java
@@ -34,7 +34,7 @@ public class BusinessInvoicePaymentFieldModelDao extends BusinessFieldModelDao {
                                                final Long accountRecordId,
                                                final CustomField customField,
                                                final Long customFieldRecordId,
-                                               final AuditLog creationAuditLog,
+                                               @Nullable final AuditLog creationAuditLog,
                                                final Long tenantRecordId,
                                                @Nullable final ReportGroup reportGroup) {
         super(account,
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentModelDao.java
index 23ea91f..b058c73 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentModelDao.java
@@ -37,8 +37,8 @@ public class BusinessInvoicePaymentModelDao extends BusinessInvoicePaymentBaseMo
                                           final Long invoicePaymentRecordId,
                                           final Payment payment,
                                           final Refund refund,
-                                          final PaymentMethod paymentMethod,
-                                          final AuditLog creationAuditLog,
+                                          @Nullable final PaymentMethod paymentMethod,
+                                          @Nullable final AuditLog creationAuditLog,
                                           final Long tenantRecordId,
                                           @Nullable final ReportGroup reportGroup) {
         super(account,
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentRefundModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentRefundModelDao.java
index ab367e0..6ebc393 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentRefundModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentRefundModelDao.java
@@ -37,8 +37,8 @@ public class BusinessInvoicePaymentRefundModelDao extends BusinessInvoicePayment
                                                 final Long invoicePaymentRecordId,
                                                 final Payment payment,
                                                 final Refund refund,
-                                                final PaymentMethod paymentMethod,
-                                                final AuditLog creationAuditLog,
+                                                @Nullable final PaymentMethod paymentMethod,
+                                                @Nullable final AuditLog creationAuditLog,
                                                 final Long tenantRecordId,
                                                 @Nullable final ReportGroup reportGroup) {
         super(account,
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentTagModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentTagModelDao.java
index 3ed4207..108e48e 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentTagModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoicePaymentTagModelDao.java
@@ -36,7 +36,7 @@ public class BusinessInvoicePaymentTagModelDao extends BusinessTagModelDao {
                                              final Tag tag,
                                              final Long tagRecordId,
                                              final TagDefinition tagDefinition,
-                                             final AuditLog creationAuditLog,
+                                             @Nullable final AuditLog creationAuditLog,
                                              final Long tenantRecordId,
                                              @Nullable final ReportGroup reportGroup) {
         super(account,
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceTagModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceTagModelDao.java
index b695760..0ab543b 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceTagModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessInvoiceTagModelDao.java
@@ -36,7 +36,7 @@ public class BusinessInvoiceTagModelDao extends BusinessTagModelDao {
                                       final Tag tag,
                                       final Long tagRecordId,
                                       final TagDefinition tagDefinition,
-                                      final AuditLog creationAuditLog,
+                                      @Nullable final AuditLog creationAuditLog,
                                       final Long tenantRecordId,
                                       @Nullable final ReportGroup reportGroup) {
         super(account,
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 7abf1cf..59af1d0 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
@@ -79,7 +79,7 @@ public class BusinessOverdueStatusModelDao extends BusinessModelDaoBase {
                                          final BlockingState blockingState,
                                          final Long blockingStateRecordId,
                                          final DateTime endDate,
-                                         final AuditLog creationAuditLog,
+                                         @Nullable final AuditLog creationAuditLog,
                                          final Long tenantRecordId,
                                          @Nullable final ReportGroup reportGroup) {
         this(blockingStateRecordId,
@@ -89,9 +89,9 @@ public class BusinessOverdueStatusModelDao extends BusinessModelDaoBase {
              blockingState.getTimestamp(),
              endDate,
              blockingState.getCreatedDate(),
-             creationAuditLog.getUserName(),
-             creationAuditLog.getReasonCode(),
-             creationAuditLog.getComment(),
+             creationAuditLog != null ? creationAuditLog.getUserName() : null,
+             creationAuditLog != null ? creationAuditLog.getReasonCode() : null,
+             creationAuditLog != null ? creationAuditLog.getComment() : null,
              account.getId(),
              account.getName(),
              account.getExternalKey(),
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 346f837..f12cc58 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
@@ -164,7 +164,7 @@ public class BusinessSubscriptionTransitionModelDao extends BusinessModelDaoBase
                                                   final BusinessSubscriptionEvent event,
                                                   @Nullable final BusinessSubscription previousSubscription,
                                                   final BusinessSubscription nextSubscription,
-                                                  final AuditLog creationAuditLog,
+                                                  @Nullable final AuditLog creationAuditLog,
                                                   final Long tenantRecordId,
                                                   @Nullable final ReportGroup reportGroup) {
         this(subscriptionEventRecordId,
@@ -176,9 +176,9 @@ public class BusinessSubscriptionTransitionModelDao extends BusinessModelDaoBase
              previousSubscription,
              nextSubscription,
              transition.getNextEventCreatedDate(),
-             creationAuditLog.getUserName(),
-             creationAuditLog.getReasonCode(),
-             creationAuditLog.getComment(),
+             creationAuditLog != null ? creationAuditLog.getUserName() : null,
+             creationAuditLog != null ? creationAuditLog.getReasonCode() : null,
+             creationAuditLog != null ? creationAuditLog.getComment() : null,
              account.getId(),
              account.getName(),
              account.getExternalKey(),
diff --git a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessTagModelDao.java b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessTagModelDao.java
index 4aa7378..8a4868d 100644
--- a/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessTagModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/main/java/com/ning/billing/osgi/bundles/analytics/dao/model/BusinessTagModelDao.java
@@ -44,7 +44,7 @@ public abstract class BusinessTagModelDao extends BusinessModelDaoBase {
                                              final Tag tag,
                                              final Long tagRecordId,
                                              final TagDefinition tagDefinition,
-                                             final AuditLog creationAuditLog,
+                                             @Nullable final AuditLog creationAuditLog,
                                              final Long tenantRecordId,
                                              @Nullable final ReportGroup reportGroup) {
         if (ObjectType.ACCOUNT.equals(tag.getObjectType())) {
@@ -113,15 +113,15 @@ public abstract class BusinessTagModelDao extends BusinessModelDaoBase {
                                final Tag tag,
                                final Long tagRecordId,
                                final TagDefinition tagDefinition,
-                               final AuditLog creationAuditLog,
+                               @Nullable final AuditLog creationAuditLog,
                                final Long tenantRecordId,
                                @Nullable final ReportGroup reportGroup) {
         this(tagRecordId,
              tagDefinition.getName(),
              tag.getCreatedDate(),
-             creationAuditLog.getUserName(),
-             creationAuditLog.getReasonCode(),
-             creationAuditLog.getComment(),
+             creationAuditLog != null ? creationAuditLog.getUserName() : null,
+             creationAuditLog != null ? creationAuditLog.getReasonCode() : null,
+             creationAuditLog != null ? creationAuditLog.getComment() : null,
              account.getId(),
              account.getName(),
              account.getExternalKey(),
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 4184f2e..e5a2153 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
@@ -620,10 +620,12 @@ insert into bip (
 , invoice_original_amount_charged
 , invoice_amount_credited
 , invoice_payment_type
+, payment_id
 , payment_number
 , linked_invoice_payment_id
 , amount
 , currency
+, plugin_name
 , plugin_created_date
 , plugin_effective_date
 , plugin_status
@@ -670,10 +672,12 @@ insert into bip (
 , :invoiceOriginalAmountCharged
 , :invoiceAmountCredited
 , :invoicePaymentType
+, :paymentId
 , :paymentNumber
 , :linkedInvoicePaymentId
 , :amount
 , :currency
+, :pluginName
 , :pluginCreatedDate
 , :pluginEffectiveDate
 , :pluginStatus
@@ -724,10 +728,13 @@ insert into bipr (
 , invoice_original_amount_charged
 , invoice_amount_credited
 , invoice_payment_type
+, payment_id
+, refund_id
 , payment_number
 , linked_invoice_payment_id
 , amount
 , currency
+, plugin_name
 , plugin_created_date
 , plugin_effective_date
 , plugin_status
@@ -774,10 +781,13 @@ insert into bipr (
 , :invoiceOriginalAmountCharged
 , :invoiceAmountCredited
 , :invoicePaymentType
+, :paymentId
+, :refundId
 , :paymentNumber
 , :linkedInvoicePaymentId
 , :amount
 , :currency
+, :pluginName
 , :pluginCreatedDate
 , :pluginEffectiveDate
 , :pluginStatus
@@ -828,10 +838,12 @@ insert into bipc (
 , invoice_original_amount_charged
 , invoice_amount_credited
 , invoice_payment_type
+, payment_id
 , payment_number
 , linked_invoice_payment_id
 , amount
 , currency
+, plugin_name
 , plugin_created_date
 , plugin_effective_date
 , plugin_status
@@ -878,10 +890,12 @@ insert into bipc (
 , :invoiceOriginalAmountCharged
 , :invoiceAmountCredited
 , :invoicePaymentType
+, :paymentId
 , :paymentNumber
 , :linkedInvoicePaymentId
 , :amount
 , :currency
+, :pluginName
 , :pluginCreatedDate
 , :pluginEffectiveDate
 , :pluginStatus
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 2e30a2f..7eca803 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
@@ -5,11 +5,11 @@ drop table if exists bst;
 create table bst (
   record_id int(11) unsigned not null auto_increment
 , subscription_event_record_id int(11) unsigned default null
-, bundle_id char(36) not null
-, bundle_external_key varchar(50) not null
-, subscription_id char(36) not null
-, requested_timestamp datetime not null
-, event varchar(50) not null
+, bundle_id char(36) default null
+, bundle_external_key varchar(50) default null
+, subscription_id char(36) default null
+, requested_timestamp datetime default null
+, event varchar(50) default null
 , prev_product_name varchar(50) default null
 , prev_product_type varchar(50) default null
 , prev_product_category varchar(50) default null
@@ -37,13 +37,13 @@ create table bst (
 , next_business_active bool default true
 , next_start_date datetime default null
 , next_end_date datetime default null
-, created_date datetime not null
-, created_by varchar(50) not null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -59,10 +59,10 @@ drop table if exists bbs;
 create table bbs (
   record_id int(11) unsigned not null auto_increment
 , bundle_record_id int(11) unsigned default null
-, bundle_id char(36) not null
-, bundle_external_key varchar(50) not null
-, subscription_id char(36) not null
-, bundle_account_rank int(11) not null
+, bundle_id char(36) default null
+, bundle_external_key varchar(50) default null
+, subscription_id char(36) default null
+, bundle_account_rank int(11) default null
 , charged_through_date datetime default null
 , current_product_name varchar(50) default null
 , current_product_type varchar(50) default null
@@ -78,13 +78,13 @@ create table bbs (
 , current_business_active bool default true
 , current_start_date datetime default null
 , current_end_date datetime default null
-, created_date datetime not null
-, created_by varchar(50) not null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -99,8 +99,8 @@ create index bbs_tenant_account_record_id on bbs(tenant_record_id, account_recor
 drop table if exists bac;
 create table bac (
   record_id int(11) unsigned not null auto_increment
-, email varchar(128) not null
-, first_name_length int not null
+, email varchar(128) default null
+, first_name_length int(11) default null
 , currency char(3) default null
 , billing_cycle_day_local int default null
 , payment_method_id char(36) default null
@@ -115,20 +115,20 @@ create table bac (
 , postal_code varchar(16) default null
 , phone varchar(25) default null
 , migrated bool default false
-, notified_for_invoices boolean not null
+, notified_for_invoices boolean default null
 , balance numeric(10, 4) default 0
 , last_invoice_date date default null
 , last_payment_date datetime default null
 , last_payment_status varchar(255) default null
 , nb_active_bundles int(11) default 0
-, created_date datetime not null
-, created_by varchar(50) not null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, updated_date datetime not null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, updated_date datetime default null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -144,23 +144,23 @@ drop table if exists bin;
 create table bin (
   record_id int(11) unsigned not null auto_increment
 , invoice_record_id int(11) unsigned default null
-, invoice_id char(36) not null
+, invoice_id char(36) default null
 , invoice_number bigint default null
-, invoice_date date not null
-, target_date date not null
-, currency char(50) not null
+, invoice_date date default null
+, target_date date default null
+, currency char(50) default null
 , balance numeric(10, 4) default 0
 , amount_paid numeric(10, 4) default 0
 , amount_charged numeric(10, 4) default 0
 , original_amount_charged numeric(10, 4) default 0
 , amount_credited numeric(10, 4) default 0
-, created_date datetime not null
-, created_by varchar(50) not null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -178,19 +178,19 @@ create table bia (
   record_id int(11) unsigned not null auto_increment
 , invoice_item_record_id int(11) unsigned default null
 , second_invoice_item_record_id int(11) unsigned default null
-, item_id char(36) not null
-, invoice_id char(36) not null
+, item_id char(36) default null
+, invoice_id char(36) default null
 , invoice_number bigint default null
-, invoice_created_date datetime not null
-, invoice_date date not null
-, invoice_target_date date not null
-, invoice_currency char(50) not null
+, invoice_created_date datetime default null
+, invoice_date date default null
+, invoice_target_date date default null
+, invoice_currency char(50) default null
 , invoice_balance numeric(10, 4) default 0
 , invoice_amount_paid numeric(10, 4) default 0
 , invoice_amount_charged numeric(10, 4) default 0
 , invoice_original_amount_charged numeric(10, 4) default 0
 , invoice_amount_credited numeric(10, 4) default 0
-, item_type char(50) not null
+, item_type char(50) default null
 , revenue_recognizable bool default true
 , bundle_external_key varchar(50) default null
 , product_name varchar(50) default null
@@ -204,13 +204,13 @@ create table bia (
 , amount numeric(10, 4) default 0
 , currency char(50) default null
 , linked_item_id char(36) default null
-, created_date datetime not null
-, created_by varchar(50) not null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -228,19 +228,19 @@ create table bii (
   record_id int(11) unsigned not null auto_increment
 , invoice_item_record_id int(11) unsigned default null
 , second_invoice_item_record_id int(11) unsigned default null
-, item_id char(36) not null
-, invoice_id char(36) not null
+, item_id char(36) default null
+, invoice_id char(36) default null
 , invoice_number bigint default null
-, invoice_created_date datetime not null
-, invoice_date date not null
-, invoice_target_date date not null
-, invoice_currency char(50) not null
+, invoice_created_date datetime default null
+, invoice_date date default null
+, invoice_target_date date default null
+, invoice_currency char(50) default null
 , invoice_balance numeric(10, 4) default 0
 , invoice_amount_paid numeric(10, 4) default 0
 , invoice_amount_charged numeric(10, 4) default 0
 , invoice_original_amount_charged numeric(10, 4) default 0
 , invoice_amount_credited numeric(10, 4) default 0
-, item_type char(50) not null
+, item_type char(50) default null
 , revenue_recognizable bool default true
 , bundle_external_key varchar(50) default null
 , product_name varchar(50) default null
@@ -254,13 +254,13 @@ create table bii (
 , amount numeric(10, 4) default 0
 , currency char(50) default null
 , linked_item_id char(36) default null
-, created_date datetime not null
-, created_by varchar(50) not null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -278,19 +278,19 @@ create table biia (
   record_id int(11) unsigned not null auto_increment
 , invoice_item_record_id int(11) unsigned default null
 , second_invoice_item_record_id int(11) unsigned default null
-, item_id char(36) not null
-, invoice_id char(36) not null
+, item_id char(36) default null
+, invoice_id char(36) default null
 , invoice_number bigint default null
-, invoice_created_date datetime not null
-, invoice_date date not null
-, invoice_target_date date not null
-, invoice_currency char(50) not null
+, invoice_created_date datetime default null
+, invoice_date date default null
+, invoice_target_date date default null
+, invoice_currency char(50) default null
 , invoice_balance numeric(10, 4) default 0
 , invoice_amount_paid numeric(10, 4) default 0
 , invoice_amount_charged numeric(10, 4) default 0
 , invoice_original_amount_charged numeric(10, 4) default 0
 , invoice_amount_credited numeric(10, 4) default 0
-, item_type char(50) not null
+, item_type char(50) default null
 , revenue_recognizable bool default true
 , bundle_external_key varchar(50) default null
 , product_name varchar(50) default null
@@ -304,13 +304,13 @@ create table biia (
 , amount numeric(10, 4) default 0
 , currency char(50) default null
 , linked_item_id char(36) default null
-, created_date datetime not null
-, created_by varchar(50) not null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -328,19 +328,19 @@ create table biic (
   record_id int(11) unsigned not null auto_increment
 , invoice_item_record_id int(11) unsigned default null
 , second_invoice_item_record_id int(11) unsigned default null
-, item_id char(36) not null
-, invoice_id char(36) not null
+, item_id char(36) default null
+, invoice_id char(36) default null
 , invoice_number bigint default null
-, invoice_created_date datetime not null
-, invoice_date date not null
-, invoice_target_date date not null
-, invoice_currency char(50) not null
+, invoice_created_date datetime default null
+, invoice_date date default null
+, invoice_target_date date default null
+, invoice_currency char(50) default null
 , invoice_balance numeric(10, 4) default 0
 , invoice_amount_paid numeric(10, 4) default 0
 , invoice_amount_charged numeric(10, 4) default 0
 , invoice_original_amount_charged numeric(10, 4) default 0
 , invoice_amount_credited numeric(10, 4) default 0
-, item_type char(50) not null
+, item_type char(50) default null
 , revenue_recognizable bool default true
 , bundle_external_key varchar(50) default null
 , product_name varchar(50) default null
@@ -354,13 +354,13 @@ create table biic (
 , amount numeric(10, 4) default 0
 , currency char(50) default null
 , linked_item_id char(36) default null
-, created_date datetime not null
-, created_by varchar(50) not null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -377,23 +377,25 @@ drop table if exists bip;
 create table bip (
   record_id int(11) unsigned not null auto_increment
 , invoice_payment_record_id int(11) unsigned default null
-, invoice_payment_id char(36) not null
-, invoice_id char(36) not null
+, invoice_payment_id char(36) default null
+, invoice_id char(36) default null
 , invoice_number bigint default null
-, invoice_created_date datetime not null
-, invoice_date date not null
-, invoice_target_date date not null
-, invoice_currency char(50) not null
+, invoice_created_date datetime default null
+, invoice_date date default null
+, invoice_target_date date default null
+, invoice_currency char(50) default null
 , invoice_balance numeric(10, 4) default 0
 , invoice_amount_paid numeric(10, 4) default 0
 , invoice_amount_charged numeric(10, 4) default 0
 , invoice_original_amount_charged numeric(10, 4) default 0
 , invoice_amount_credited numeric(10, 4) default 0
 , invoice_payment_type varchar(50) default null
+, payment_id char(36) default null
 , payment_number bigint default null
 , linked_invoice_payment_id char(36) default null
 , amount numeric(10, 4) default 0
 , currency char(50) default null
+, plugin_name varchar(255) default null
 , plugin_created_date datetime default null
 , plugin_effective_date datetime default null
 , plugin_status varchar(255) default null
@@ -415,13 +417,13 @@ create table bip (
 , plugin_pm_state varchar(255) default null
 , plugin_pm_zip varchar(255) default null
 , plugin_pm_country varchar(255) default null
-, created_date datetime not null
-, created_by varchar(50) not null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -438,23 +440,26 @@ drop table if exists bipr;
 create table bipr (
   record_id int(11) unsigned not null auto_increment
 , invoice_payment_record_id int(11) unsigned default null
-, invoice_payment_id char(36) not null
-, invoice_id char(36) not null
+, invoice_payment_id char(36) default null
+, invoice_id char(36) default null
 , invoice_number bigint default null
-, invoice_created_date datetime not null
-, invoice_date date not null
-, invoice_target_date date not null
-, invoice_currency char(50) not null
+, invoice_created_date datetime default null
+, invoice_date date default null
+, invoice_target_date date default null
+, invoice_currency char(50) default null
 , invoice_balance numeric(10, 4) default 0
 , invoice_amount_paid numeric(10, 4) default 0
 , invoice_amount_charged numeric(10, 4) default 0
 , invoice_original_amount_charged numeric(10, 4) default 0
 , invoice_amount_credited numeric(10, 4) default 0
 , invoice_payment_type varchar(50) default null
+, payment_id char(36) default null
+, refund_id char(36) default null
 , payment_number bigint default null
 , linked_invoice_payment_id char(36) default null
 , amount numeric(10, 4) default 0
 , currency char(50) default null
+, plugin_name varchar(255) default null
 , plugin_created_date datetime default null
 , plugin_effective_date datetime default null
 , plugin_status varchar(255) default null
@@ -476,13 +481,13 @@ create table bipr (
 , plugin_pm_state varchar(255) default null
 , plugin_pm_zip varchar(255) default null
 , plugin_pm_country varchar(255) default null
-, created_date datetime not null
-, created_by varchar(50) not null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -499,23 +504,25 @@ drop table if exists bipc;
 create table bipc (
   record_id int(11) unsigned not null auto_increment
 , invoice_payment_record_id int(11) unsigned default null
-, invoice_payment_id char(36) not null
-, invoice_id char(36) not null
+, invoice_payment_id char(36) default null
+, invoice_id char(36) default null
 , invoice_number bigint default null
-, invoice_created_date datetime not null
-, invoice_date date not null
-, invoice_target_date date not null
-, invoice_currency char(50) not null
+, invoice_created_date datetime default null
+, invoice_date date default null
+, invoice_target_date date default null
+, invoice_currency char(50) default null
 , invoice_balance numeric(10, 4) default 0
 , invoice_amount_paid numeric(10, 4) default 0
 , invoice_amount_charged numeric(10, 4) default 0
 , invoice_original_amount_charged numeric(10, 4) default 0
 , invoice_amount_credited numeric(10, 4) default 0
 , invoice_payment_type varchar(50) default null
+, payment_id char(36) default null
 , payment_number bigint default null
 , linked_invoice_payment_id char(36) default null
 , amount numeric(10, 4) default 0
 , currency char(50) default null
+, plugin_name varchar(255) default null
 , plugin_created_date datetime default null
 , plugin_effective_date datetime default null
 , plugin_status varchar(255) default null
@@ -537,13 +544,13 @@ create table bipc (
 , plugin_pm_state varchar(255) default null
 , plugin_pm_zip varchar(255) default null
 , plugin_pm_country varchar(255) default null
-, created_date datetime not null
-, created_by varchar(50) not null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -559,18 +566,18 @@ drop table if exists bos;
 create table bos (
   record_id int(11) unsigned not null auto_increment
 , blocking_state_record_id int(11) unsigned default null
-, bundle_id char(36) not null
-, bundle_external_key varchar(50) not null
-, status varchar(50) not null
+, bundle_id char(36) default null
+, bundle_external_key varchar(50) default null
+, status varchar(50) default null
 , start_date datetime default null
 , end_date datetime default null
-, created_date datetime not null
-, created_by varchar(50) not null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -584,14 +591,14 @@ drop table if exists bac_tags;
 create table bac_tags (
   record_id int(11) unsigned not null auto_increment
 , tag_record_id int(11) unsigned default null
-, name varchar(50) not null
-, created_date datetime not null
-, created_by varchar(50) not null
+, name varchar(50) default null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -605,15 +612,15 @@ drop table if exists bin_tags;
 create table bin_tags (
   record_id int(11) unsigned not null auto_increment
 , tag_record_id int(11) unsigned default null
-, invoice_id char(36) not null
-, name varchar(50) not null
-, created_date datetime not null
-, created_by varchar(50) not null
+, invoice_id char(36) default null
+, name varchar(50) default null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -627,15 +634,15 @@ drop table if exists bip_tags;
 create table bip_tags (
   record_id int(11) unsigned not null auto_increment
 , tag_record_id int(11) unsigned default null
-, invoice_payment_id char(36) not null
-, name varchar(50) not null
-, created_date datetime not null
-, created_by varchar(50) not null
+, invoice_payment_id char(36) default null
+, name varchar(50) default null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -649,15 +656,15 @@ drop table if exists bac_fields;
 create table bac_fields (
   record_id int(11) unsigned not null auto_increment
 , custom_field_record_id int(11) unsigned default null
-, name varchar(50) not null
+, name varchar(50) default null
 , value varchar(255) default null
-, created_date datetime not null
-, created_by varchar(50) not null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -671,16 +678,16 @@ drop table if exists bin_fields;
 create table bin_fields (
   record_id int(11) unsigned not null auto_increment
 , custom_field_record_id int(11) unsigned default null
-, invoice_id char(36) not null
-, name varchar(50) not null
+, invoice_id char(36) default null
+, name varchar(50) default null
 , value varchar(255) default null
-, created_date datetime not null
-, created_by varchar(50) not null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
@@ -694,16 +701,16 @@ drop table if exists bip_fields;
 create table bip_fields (
   record_id int(11) unsigned not null auto_increment
 , custom_field_record_id int(11) unsigned default null
-, invoice_payment_id char(36) not null
-, name varchar(50) not null
+, invoice_payment_id char(36) default null
+, name varchar(50) default null
 , value varchar(255) default null
-, created_date datetime not null
-, created_by varchar(50) not null
+, created_date datetime default null
+, created_by varchar(50) default null
 , created_reason_code varchar(255) default null
 , created_comments varchar(255) default null
-, account_id char(36) not null
-, account_name varchar(100) not null
-, account_external_key varchar(50) not null
+, account_id char(36) default null
+, account_name varchar(100) default null
+, account_external_key varchar(50) default null
 , account_record_id int(11) unsigned default null
 , tenant_record_id int(11) unsigned default null
 , report_group enum('default', 'test', 'partner') not null
diff --git a/osgi-bundles/bundles/analytics/src/main/resources/sanity/sanity.sql b/osgi-bundles/bundles/analytics/src/main/resources/sanity/sanity.sql
new file mode 100644
index 0000000..a0d011e
--- /dev/null
+++ b/osgi-bundles/bundles/analytics/src/main/resources/sanity/sanity.sql
@@ -0,0 +1,622 @@
+-- bac
+
+-- A1a
+select a.updated_date, bac.updated_date
+from accounts a
+ left outer join bac on a.id = bac.account_id
+where a.record_id != bac.account_record_id
+      or ( coalesce(a.id , '') != coalesce(bac.account_id ,''))
+      or a.external_key !=  bac.account_external_key
+      or ( coalesce(a.email , '') != coalesce(bac.email ,''))
+      or ( coalesce(a.name , '') != coalesce(bac.account_name ,''))
+      or ( coalesce(a.first_name_length , '') != coalesce(bac.first_name_length ,''))
+      or ( coalesce(a.currency , '') != coalesce(bac.currency ,''))
+      or ( coalesce(a.billing_cycle_day_local , '') != coalesce(bac.billing_cycle_day_local ,''))
+      or ( coalesce(a.payment_method_id , '') != coalesce(bac.payment_method_id ,''))
+      or ( coalesce(a.time_zone , '') != coalesce(bac.time_zone ,''))
+      or ( coalesce(a.locale , '') != coalesce(bac.locale ,''))
+      or ( coalesce(a.address1 , '') != coalesce(bac.address1 ,''))
+      or ( coalesce(a.address2 , '') != coalesce(bac.address2 ,''))
+      or ( coalesce(a.company_name , '') != coalesce(bac.company_name ,''))
+      or ( coalesce(a.city , '') != coalesce(bac.city ,''))
+      or ( coalesce(a.state_or_province , '') != coalesce(bac.state_or_province ,''))
+      or ( coalesce(a.country , '') != coalesce(bac.country ,''))
+      or ( coalesce(a.postal_code , '') != coalesce(bac.postal_code ,''))
+      or ( coalesce(a.phone , '') != coalesce(bac.phone ,''))
+      or ( coalesce(a.migrated , '') != coalesce(bac.migrated ,''))
+      or ( coalesce(a.is_notified_for_invoices , '') != coalesce(bac.notified_for_invoices ,''))
+      or a.created_date  != bac.created_date
+      or a.created_by != bac.created_by
+      -- RI-1967 or a.updated_date != bac.updated_date
+      -- or a.updated_by != bac.updated_by
+      -- RI-1966 or a.tenant_record_id != bac.tenant_record_id
+
+-- A1b
+select a.updated_date, bac.updated_date
+from  bac
+ left outer join accounts a on a.id = bac.account_id
+where a.record_id != bac.account_record_id
+      or ( coalesce(a.id , '') != coalesce(bac.account_id ,''))
+      or a.external_key , '') != bac.account_external_key
+      or ( coalesce(a.email , '') != coalesce(bac.email ,''))
+      or ( coalesce(a.name , '') != coalesce(bac.account_name ,''))
+      or ( coalesce(a.first_name_length , '') != coalesce(bac.first_name_length ,''))
+      or ( coalesce(a.currency , '') != coalesce(bac.currency ,''))
+      or ( coalesce(a.billing_cycle_day_local , '') != coalesce(bac.billing_cycle_day_local ,''))
+      or ( coalesce(a.payment_method_id , '') != coalesce(bac.payment_method_id ,''))
+      or ( coalesce(a.time_zone , '') != coalesce(bac.time_zone ,''))
+      or ( coalesce(a.locale , '') != coalesce(bac.locale ,''))
+      or ( coalesce(a.address1 , '') != coalesce(bac.address1 ,''))
+      or ( coalesce(a.address2 , '') != coalesce(bac.address2 ,''))
+      or ( coalesce(a.company_name , '') != coalesce(bac.company_name ,''))
+      or ( coalesce(a.city , '') != coalesce(bac.city ,''))
+      or ( coalesce(a.state_or_province , '') != coalesce(bac.state_or_province ,''))
+      or ( coalesce(a.country , '') != coalesce(bac.country ,''))
+      or ( coalesce(a.postal_code , '') != coalesce(bac.postal_code ,''))
+      or ( coalesce(a.phone , '') != coalesce(bac.phone ,''))
+      or ( coalesce(a.migrated , '') != coalesce(bac.migrated ,''))
+      or ( coalesce(a.is_notified_for_invoices , '') != coalesce(bac.notified_for_invoices ,''))
+      or ( coalesce(a.created_date, '')  != ( coalesce(bac.created_date,''))
+      or ( coalesce(a.created_by != ( coalesce(bac.created_by,''))
+      -- RI-1967 or ( coalesce(a.updated_date, '') != ( coalesce(bac.updated_date,''))
+      -- or ( coalesce(a.updated_b, '')y != ( coalesce(bac.updated_by,''))
+      -- RI-1966 or ( coalesce(a.tenant_record_id, '') != ( coalesce(bac.tenant_record_id,''))
+
+*****
+
+-- bia
+
+-- B1a
+-- this will find things it thinks should be in bia but it's correct that they're not there
+select *
+from invoice_items ii
+     left outer join bia b on ii.id = b.item_id
+where ii.type in ('CREDIT_ADJ')
+      and (( coalesce(ii.record_id, '') != coalesce(b.invoice_item_record_id,''))
+      or ( coalesce(ii.id, '') != coalesce(b.item_id,''))
+      or ( coalesce(ii.type, '') != coalesce(b.item_type,''))
+      or ( coalesce(ii.invoice_id, '') != coalesce(b.invoice_id,''))
+      or ( coalesce(ii.account_id , '')!= coalesce(b.account_id,''))
+      or ( coalesce(ii.phase_name, '') != coalesce(b.slug,''))
+      or ( coalesce(ii.start_date, '') != coalesce(b.start_date,''))
+      -- old trials or ( coalesce(ii.end_date, '') != coalesce(b.end_date,''))
+      or ( coalesce(ii.amount, '') != coalesce(b.amount,''))
+      or ( coalesce(ii.currency, '') != coalesce(b.currency,''))
+      or ( coalesce(ii.linked_item_id, '') != coalesce(b.linked_item_id,''))
+      or ( coalesce(ii.created_by, '') != coalesce(b.created_by,''))
+      or ( coalesce(ii.created_date, '') != coalesce(b.created_date,''))
+      or ( coalesce(ii.account_record_id, '') != coalesce(b.account_record_id,''))
+      -- RI-1966 or ( coalesce(ii.tenant_record_id, '') != coalesce(b.tenant_record_id,''))
+      )
+
+-- B1b
+select *
+from bia b
+     left outer join invoice_items ii on ii.id = b.item_id
+where ( coalesce(ii.record_id, '') != coalesce(b.invoice_item_record_id,''))
+      or ( coalesce(ii.id, '') != coalesce(b.item_id,''))
+      or ( coalesce(ii.type, '') != coalesce(b.item_type,''))
+      or ( coalesce(ii.invoice_id, '') != coalesce(b.invoice_id,''))
+      or ( coalesce(ii.account_id , '')!= coalesce(b.account_id,''))
+      or ( coalesce(ii.phase_name, '') != coalesce(b.slug,''))
+      or ( coalesce(ii.start_date, '') != coalesce(b.start_date,''))
+      -- old trials or ( coalesce(ii.end_date, '') != coalesce(b.end_date,''))
+      or ( coalesce(ii.amount, '') != coalesce(b.amount,''))
+      or ( coalesce(ii.currency, '') != coalesce(b.currency,''))
+      or ( coalesce(ii.linked_item_id, '') != coalesce(b.linked_item_id,''))
+      or ( coalesce(ii.created_by, '') != coalesce(b.created_by,''))
+      or ( coalesce(ii.created_date, '') != coalesce(b.created_date,''))
+      or ( coalesce(ii.account_record_id, '') != coalesce(b.account_record_id,''))
+      -- RI-1966 or ( coalesce(ii.tenant_record_id, '') != coalesce(b.tenant_record_id,''))
+      or ii.type not in ('CREDIT_ADJ')
+
+-- B2
+select *
+from bia b
+     left outer join accounts a on a.id = b.account_id
+where coalesce(a.record_id )!=  coalesce(b.account_record_id ,'')
+      or coalesce(a.id , '') != coalesce(b.account_id ,'')
+      or coalesce(a.external_key, '') != coalesce(b.account_external_key,'')
+      or coalesce(a.name , '') != coalesce(b.account_name ,'')
+
+-- B3
+select *
+from bia b
+     left outer join invoices i on i.id = b.invoice_id
+where coalesce(i.RECORD_ID, 'NULL') != coalesce(b.invoice_number,'NULL')
+      or coalesce(i.created_date, 'NULL') != coalesce(b.invoice_created_date,'NULL')
+      or coalesce(i.invoice_date, 'NULL') != coalesce(b.invoice_date,'NULL')
+      or coalesce(i.target_date, 'NULL') != coalesce(b.invoice_target_date,'NULL')
+      or coalesce(i.currency, 'NULL') != coalesce(b.invoice_currency,'NULL')
+
+-- B4
+select *
+from bia b
+     left outer join invoice_items ii on ii.id = b.item_id
+     left outer join bundles bndl on ii.bundle_id = bndl.id
+where coalesce(bndl.external_key, 'NULL') != coalesce(b.bundle_external_key,'NULL')
+
+-- B5
+
+select *
+from bia b
+     left outer join bin on b.invoice_id = bin.invoice_id
+where b.invoice_balance != bin.balance
+      or b.invoice_amount_paid != bin.amount_paid
+      or b.invoice_amount_charged != bin.amount_charged
+      or b.invoice_original_amount_charged != bin.original_amount_charged
+      or b.invoice_amount_credited != bin.amount_credited
+
+*****
+
+-- bii
+
+-- C1a
+select *
+from invoice_items ii
+     left outer join bii on ii.id = bii.item_id
+where ii.type in ('FIXED','RECURRING','EXTERNAL_CHARGE')
+      and (( coalesce(ii.record_id, '') != coalesce(bii.invoice_item_record_id,''))
+      or ( coalesce(ii.id, '') != coalesce(bii.item_id,''))
+      or ( coalesce(ii.type, '') != coalesce(bii.item_type,''))
+      or ( coalesce(ii.invoice_id, '') != coalesce(bii.invoice_id,''))
+      or ( coalesce(ii.account_id , '')!= coalesce(bii.account_id,''))
+      or ( coalesce(ii.phase_name, '') != coalesce(bii.slug,''))
+      or ( coalesce(ii.start_date, '') != coalesce(bii.start_date,''))
+      -- old trials or ( coalesce(ii.end_date, '') != coalesce(bii.end_date,''))
+      or ( coalesce(ii.amount, '') != coalesce(bii.amount,''))
+      or ( coalesce(ii.currency, '') != coalesce(bii.currency,''))
+      or ( coalesce(ii.linked_item_id, '') != coalesce(bii.linked_item_id,''))
+      or ( coalesce(ii.created_by, '') != coalesce(bii.created_by,''))
+      or ( coalesce(ii.created_date, '') != coalesce(bii.created_date,''))
+      or ( coalesce(ii.account_record_id, '') != coalesce(bii.account_record_id,''))
+      -- RI-1966 or ( coalesce(ii.tenant_record_id, '') != coalesce(bii.tenant_record_id,''))
+      )
+
+-- C1b
+select *
+from bii
+     left outer join invoice_items ii on ii.id = bii.item_id
+where ( coalesce(ii.record_id, '') != coalesce(bii.invoice_item_record_id,''))
+      or ( coalesce(ii.id, '') != coalesce(bii.item_id,''))
+      or ( coalesce(ii.type, '') != coalesce(bii.item_type,''))
+      or ( coalesce(ii.invoice_id, '') != coalesce(bii.invoice_id,''))
+      or ( coalesce(ii.account_id , '')!= coalesce(bii.account_id,''))
+      or ( coalesce(ii.phase_name, '') != coalesce(bii.slug,''))
+      or ( coalesce(ii.start_date, '') != coalesce(bii.start_date,''))
+      -- old trials or ( coalesce(ii.end_date, '') != coalesce(bii.end_date,''))
+      or ( coalesce(ii.amount, '') != coalesce(bii.amount,''))
+      or ( coalesce(ii.currency, '') != coalesce(bii.currency,''))
+      or ( coalesce(ii.linked_item_id, '') != coalesce(bii.linked_item_id,''))
+      or ( coalesce(ii.created_by, '') != coalesce(bii.created_by,''))
+      or ( coalesce(ii.created_date, '') != coalesce(bii.created_date,''))
+      or ( coalesce(ii.account_record_id, '') != coalesce(bii.account_record_id,''))
+      -- RI-1966 or ( coalesce(ii.tenant_record_id, '') != coalesce(bii.tenant_record_id,''))
+      or ii.type not in ('FIXED','RECURRING','EXTERNAL_CHARGE')
+
+-- C2
+select *
+from bii b
+     left outer join accounts a on a.id = b.account_id
+where coalesce(a.record_id )!=  coalesce(b.account_record_id ,'')
+      or coalesce(a.id , '') != coalesce(b.account_id ,'')
+      or coalesce(a.external_key, '') != coalesce(b.account_external_key,'')
+      or coalesce(a.name , '') != coalesce(b.account_name ,'')
+
+-- C3
+select *
+from bii b
+     left outer join invoices i on i.id = b.invoice_id
+where coalesce(i.RECORD_ID, 'NULL') != coalesce(b.invoice_number,'NULL')
+      or coalesce(i.created_date, 'NULL') != coalesce(b.invoice_created_date,'NULL')
+      or coalesce(i.invoice_date, 'NULL') != coalesce(b.invoice_date,'NULL')
+      or coalesce(i.target_date, 'NULL') != coalesce(b.invoice_target_date,'NULL')
+      or coalesce(i.currency, 'NULL') != coalesce(b.invoice_currency,'NULL')
+
+-- C4
+select *
+from bii b
+     left outer join invoice_items ii on ii.id = b.item_id
+     left outer join bundles bndl on ii.bundle_id = bndl.id
+where coalesce(bndl.external_key, 'NULL') != coalesce(b.bundle_external_key,'NULL')
+
+-- C5
+select *
+from bii b
+     left outer join bin on b.invoice_id = bin.invoice_id
+where b.invoice_balance != bin.balance
+      or b.invoice_amount_paid != bin.amount_paid
+      or b.invoice_amount_charged != bin.amount_charged
+      or b.invoice_original_amount_charged != bin.original_amount_charged
+      or b.invoice_amount_credited != bin.amount_credited
+
+*****
+
+-- biia
+
+-- D1a
+select *
+from invoice_items ii
+     left outer join biia b on ii.id = b.item_id
+where ii.type in ('ITEM_ADJ')
+      and (( coalesce(ii.record_id, '') != coalesce(b.invoice_item_record_id,''))
+      or ( coalesce(ii.id, '') != coalesce(b.item_id,''))
+      or ( coalesce(ii.type, '') != coalesce(b.item_type,''))
+      or ( coalesce(ii.invoice_id, '') != coalesce(b.invoice_id,''))
+      or ( coalesce(ii.account_id , '')!= coalesce(b.account_id,''))
+      or ( coalesce(ii.phase_name, '') != coalesce(b.slug,''))
+      or ( coalesce(ii.start_date, '') != coalesce(b.start_date,''))
+      -- old trials or ( coalesce(ii.end_date, '') != coalesce(b.end_date,''))
+      or ( coalesce(ii.amount, '') != coalesce(b.amount,''))
+      or ( coalesce(ii.currency, '') != coalesce(b.currency,''))
+      or ( coalesce(ii.linked_item_id, '') != coalesce(b.linked_item_id,''))
+      or ( coalesce(ii.created_by, '') != coalesce(b.created_by,''))
+      or ( coalesce(ii.created_date, '') != coalesce(b.created_date,''))
+      or ( coalesce(ii.account_record_id, '') != coalesce(b.account_record_id,''))
+      -- RI-1966 or ( coalesce(ii.tenant_record_id, '') != coalesce(b.tenant_record_id,''))
+      )
+
+-- D1b
+select *
+from biia b
+     left outer join invoice_items ii on ii.id = b.item_id
+where ( coalesce(ii.record_id, '') != coalesce(b.invoice_item_record_id,''))
+      or ( coalesce(ii.id, '') != coalesce(b.item_id,''))
+      or ( coalesce(ii.type, '') != coalesce(b.item_type,''))
+      or ( coalesce(ii.invoice_id, '') != coalesce(b.invoice_id,''))
+      or ( coalesce(ii.account_id , '')!= coalesce(b.account_id,''))
+      or ( coalesce(ii.phase_name, '') != coalesce(b.slug,''))
+      or ( coalesce(ii.start_date, '') != coalesce(b.start_date,''))
+      -- old trials or ( coalesce(ii.end_date, '') != coalesce(b.end_date,''))
+      or ( coalesce(ii.amount, '') != coalesce(b.amount,''))
+      or ( coalesce(ii.currency, '') != coalesce(b.currency,''))
+      or ( coalesce(ii.linked_item_id, '') != coalesce(b.linked_item_id,''))
+      or ( coalesce(ii.created_by, '') != coalesce(b.created_by,''))
+      or ( coalesce(ii.created_date, '') != coalesce(b.created_date,''))
+      or ( coalesce(ii.account_record_id, '') != coalesce(b.account_record_id,''))
+      -- RI-1966 or ( coalesce(ii.tenant_record_id, '') != coalesce(b.tenant_record_id,''))
+      or ii.type not in ('ITEM_ADJ')
+
+-- D2
+select *
+from biia b
+     left outer join accounts a on a.id = b.account_id
+where coalesce(a.record_id )!=  coalesce(b.account_record_id ,'')
+      or coalesce(a.id , '') != coalesce(b.account_id ,'')
+      or coalesce(a.external_key, '') != coalesce(b.account_external_key,'')
+      or coalesce(a.name , '') != coalesce(b.account_name ,'')
+
+-- D3
+select *
+from biia b
+     left outer join invoices i on i.id = b.invoice_id
+where coalesce(i.RECORD_ID, 'NULL') != coalesce(b.invoice_number,'NULL')
+      or coalesce(i.created_date, 'NULL') != coalesce(b.invoice_created_date,'NULL')
+      or coalesce(i.invoice_date, 'NULL') != coalesce(b.invoice_date,'NULL')
+      or coalesce(i.target_date, 'NULL') != coalesce(b.invoice_target_date,'NULL')
+      or coalesce(i.currency, 'NULL') != coalesce(b.invoice_currency,'NULL')
+
+
+-- D4
+select *
+from biia b
+     left outer join invoice_items ii on ii.id = b.item_id
+     left outer join bundles bndl on ii.bundle_id = bndl.id
+where coalesce(bndl.external_key, 'NULL') != coalesce(b.bundle_external_key,'NULL')
+
+-- D5
+select *
+from biia b
+     left outer join bin on b.invoice_id = bin.invoice_id
+where b.invoice_balance != bin.balance
+      or b.invoice_amount_paid != bin.amount_paid
+      or b.invoice_amount_charged != bin.amount_charged
+      or b.invoice_original_amount_charged != bin.original_amount_charged
+      or b.invoice_amount_credited != bin.amount_credited
+****
+
+-- biic
+
+-- E1a
+select *
+from invoice_items ii
+     left outer join biic b on ii.id = b.item_id
+where ii.type in ('CBA_ADJ')
+      and (( coalesce(ii.record_id, '') != coalesce(b.invoice_item_record_id,''))
+      or ( coalesce(ii.id, '') != coalesce(b.item_id,''))
+      or ( coalesce(ii.type, '') != coalesce(b.item_type,''))
+      or ( coalesce(ii.invoice_id, '') != coalesce(b.invoice_id,''))
+      or ( coalesce(ii.account_id , '')!= coalesce(b.account_id,''))
+      or ( coalesce(ii.phase_name, '') != coalesce(b.slug,''))
+      or ( coalesce(ii.start_date, '') != coalesce(b.start_date,''))
+      -- old trials or ( coalesce(ii.end_date, '') != coalesce(b.end_date,''))
+      or ( coalesce(ii.amount, '') != coalesce(b.amount,''))
+      or ( coalesce(ii.currency, '') != coalesce(b.currency,''))
+      or ( coalesce(ii.linked_item_id, '') != coalesce(b.linked_item_id,''))
+      or ( coalesce(ii.created_by, '') != coalesce(b.created_by,''))
+      or ( coalesce(ii.created_date, '') != coalesce(b.created_date,''))
+      or ( coalesce(ii.account_record_id, '') != coalesce(b.account_record_id,''))
+      -- RI-1966 or ( coalesce(ii.tenant_record_id, '') != coalesce(b.tenant_record_id,''))
+      )
+
+-- E1b
+select *
+from biic b
+     left outer join invoice_items ii on ii.id = b.item_id
+where ( coalesce(ii.record_id, '') != coalesce(b.invoice_item_record_id,''))
+      or ( coalesce(ii.id, '') != coalesce(b.item_id,''))
+      or ( coalesce(ii.type, '') != coalesce(b.item_type,''))
+      or ( coalesce(ii.invoice_id, '') != coalesce(b.invoice_id,''))
+      or ( coalesce(ii.account_id , '')!= coalesce(b.account_id,''))
+      or ( coalesce(ii.phase_name, '') != coalesce(b.slug,''))
+      or ( coalesce(ii.start_date, '') != coalesce(b.start_date,''))
+      -- old trials or ( coalesce(ii.end_date, '') != coalesce(b.end_date,''))
+      or ( coalesce(ii.amount, '') != coalesce(b.amount,''))
+      or ( coalesce(ii.currency, '') != coalesce(b.currency,''))
+      or ( coalesce(ii.linked_item_id, '') != coalesce(b.linked_item_id,''))
+      or ( coalesce(ii.created_by, '') != coalesce(b.created_by,''))
+      or ( coalesce(ii.created_date, '') != coalesce(b.created_date,''))
+      or ( coalesce(ii.account_record_id, '') != coalesce(b.account_record_id,''))
+      -- RI-1966 or ( coalesce(ii.tenant_record_id, '') != coalesce(b.tenant_record_id,''))
+      or ii.type not in ('CBA_ADJ')
+
+-- E2
+select *
+from biic b
+     left outer join accounts a on a.id = b.account_id
+where coalesce(a.record_id )!=  coalesce(b.account_record_id ,'')
+      or coalesce(a.id , '') != coalesce(b.account_id ,'')
+      or coalesce(a.external_key, '') != coalesce(b.account_external_key,'')
+      or coalesce(a.name , '') != coalesce(b.account_name ,'')
+
+-- E3
+select *
+from biic b
+     left outer join invoices i on i.id = b.invoice_id
+where coalesce(i.RECORD_ID, 'NULL') != coalesce(b.invoice_number,'NULL')
+      or coalesce(i.created_date, 'NULL') != coalesce(b.invoice_created_date,'NULL')
+      or coalesce(i.invoice_date, 'NULL') != coalesce(b.invoice_date,'NULL')
+      or coalesce(i.target_date, 'NULL') != coalesce(b.invoice_target_date,'NULL')
+      or coalesce(i.currency, 'NULL') != coalesce(b.invoice_currency,'NULL')
+
+-- E4
+select *
+from biic b
+     left outer join invoice_items ii on ii.id = b.item_id
+     left outer join bundles bndl on ii.bundle_id = bndl.id
+where coalesce(bndl.external_key, 'NULL') != coalesce(b.bundle_external_key,'NULL')
+
+-- E5
+select *
+from biic b
+     left outer join bin on b.invoice_id = bin.invoice_id
+where b.invoice_balance != bin.balance
+      or b.invoice_amount_paid != bin.amount_paid
+      or b.invoice_amount_charged != bin.amount_charged
+      or b.invoice_original_amount_charged != bin.original_amount_charged
+      or b.invoice_amount_credited != bin.amount_credited
+
+****
+
+-- bin
+
+-- F1a
+select *
+from invoices i
+     left outer join bin on i.id = bin.invoice_id
+where coalesce(i.record_id, '') != coalesce(bin.invoice_record_id,'')
+      or coalesce(i.record_id, '') != coalesce(bin.invoice_number,'')
+      or coalesce(i.id, '') != coalesce(bin.invoice_id,'')
+      or ( coalesce(i.account_id, '') != coalesce(bin.account_id,''))
+      or ( coalesce(i.invoice_date, '') != coalesce(bin.invoice_date,''))
+      or ( coalesce(i.target_date, '') != coalesce(bin.target_date,''))
+      or ( coalesce(i.currency, '') != coalesce(bin.currency,''))
+      or ( coalesce(i.created_by, '') != coalesce(bin.created_by,''))
+      or ( coalesce(i.created_date, '') != coalesce( bin.created_date,''))
+      or ( coalesce(i.account_record_id, '') != coalesce(bin.account_record_id,''))
+      -- RI-1966 or ( coalesce(i.tenant_record_id, '') != coalesce(bin.tenant_record_id,''))
+
+-- F1b
+select *
+from bin
+     left outer join invoices i on i.id = bin.invoice_id
+where ( coalesce(i.record_id, '') != coalesce(bin.invoice_record_id,''))
+      or ( coalesce(i.id, '') != coalesce(bin.invoice_id,''))
+      or ( coalesce(i.account_id, '') != coalesce(bin.account_id,''))
+      or ( coalesce(i.invoice_date, '') != coalesce(bin.invoice_date,''))
+      or ( coalesce(i.target_date, '') != coalesce(bin.target_date,''))
+      or ( coalesce(i.currency, '') != coalesce(bin.currency,''))
+      or ( coalesce(i.created_by, '') != coalesce(bin.created_by,''))
+      or ( coalesce(i.created_date, '') != coalesce(bin.created_date,''))
+      or ( coalesce(i.account_record_id, '') != coalesce(bin.account_record_id,''))
+      -- RI-1966or ( coalesce(i.tenant_record_id, '') != coalesce(bin.tenant_record_id,''))
+
+-- F2
+select *
+from bin b
+     left outer join accounts a on a.id = b.account_id
+where coalesce(a.record_id )!=  coalesce(b.account_record_id ,'')
+      or coalesce(a.id , '') != coalesce(b.account_id ,'')
+      or coalesce(a.external_key, '') != coalesce(b.account_external_key,'')
+      or coalesce(a.name , '') != coalesce(b.account_name ,'')
+
+
+*****
+
+-- bip
+
+-- G1a
+select *
+from invoice_payments ip
+     left outer join bip on ip.id = bip.invoice_payment_id
+where ( coalesce(ip.RECORD_ID, 'NULL') != coalesce(bip.invoice_payment_record_id,'NULL')
+      or  coalesce(ip.ID, 'NULL') != coalesce(bip.invoice_payment_id,'NULL')
+      or  coalesce(ip.invoice_id, 'NULL') != coalesce(bip.invoice_id,'NULL')
+      or  coalesce(ip.type, 'NULL') != coalesce(bip.invoice_payment_type,'NULL')
+      or  coalesce(ip.linked_invoice_payment_id, 'NULL') != coalesce(bip.linked_invoice_payment_id,'NULL')
+      or  coalesce(ip.amount, 'NULL') != coalesce(bip.amount,'NULL')
+      or  coalesce(ip.currency, 'NULL') != coalesce(bip.currency,'NULL')
+      or  coalesce(ip.created_date, 'NULL') != coalesce(bip.created_date,'NULL')
+      or  coalesce(ip.created_by, 'NULL') != coalesce(bip.created_by,'NULL')
+      or  coalesce(ip.account_record_id, 'NULL') != coalesce(bip.account_record_id,'NULL')
+      -- or  coalesce(ip.tenant_record_id, 'NULL') != coalesce(bip.tenant_record_id,'NULL')
+     )
+      and ip.type = 'ATTEMPT'
+
+-- G1b
+
+select *
+from bip
+     left outer join invoice_payments ip on ip.id = bip.invoice_payment_id
+where coalesce(ip.RECORD_ID, 'NULL') != coalesce(bip.invoice_payment_record_id,'NULL')
+      or  coalesce(ip.ID, 'NULL') != coalesce(bip.invoice_payment_id,'NULL')
+      or  coalesce(ip.invoice_id, 'NULL') != coalesce(bip.invoice_id,'NULL')
+      or  coalesce(ip.type, 'NULL') != coalesce(bip.invoice_payment_type,'NULL')
+      or  coalesce(ip.linked_invoice_payment_id, 'NULL') != coalesce(bip.linked_invoice_payment_id,'NULL')
+      or  coalesce(ip.amount, 'NULL') != coalesce(bip.amount,'NULL')
+      or  coalesce(ip.currency, 'NULL') != coalesce(bip.currency,'NULL')
+      or  coalesce(ip.created_date, 'NULL') != coalesce(bip.created_date,'NULL')
+      or  coalesce(ip.created_by, 'NULL') != coalesce(bip.created_by,'NULL')
+      or  coalesce(ip.account_record_id, 'NULL') != coalesce(bip.account_record_id,'NULL')
+      -- or  coalesce(ip.tenant_record_id, 'NULL') != coalesce(bip.tenant_record_id,'NULL')
+      or  bip.invoice_payment_type != 'ATTEMPT'
+
+-- G2
+select *
+from bip b
+     left outer join accounts a on a.id = b.account_id
+where coalesce(a.record_id )!=  coalesce(b.account_record_id ,'')
+      or coalesce(a.external_key, '') != coalesce(b.account_external_key,'')
+      or coalesce(a.name , '') != coalesce(b.account_name ,'')
+
+-- G3
+select *
+from bip b
+     left outer join invoices i on i.id = b.invoice_id
+where coalesce(i.RECORD_ID, 'NULL') != coalesce(b.invoice_number,'NULL')
+      or coalesce(i.created_date, 'NULL') != coalesce(b.invoice_created_date,'NULL')
+      or coalesce(i.invoice_date, 'NULL') != coalesce(b.invoice_date,'NULL')
+      or coalesce(i.target_date, 'NULL') != coalesce(b.invoice_target_date,'NULL')
+      or coalesce(i.currency, 'NULL') != coalesce(b.invoice_currency,'NULL')
+
+-- G4
+select *
+from bip b
+     left outer join bin on b.invoice_id = bin.invoice_id
+where b.invoice_balance != bin.balance
+      or b.invoice_amount_paid != bin.amount_paid
+      or b.invoice_amount_charged != bin.amount_charged
+      or b.invoice_original_amount_charged != bin.original_amount_charged
+      or b.invoice_amount_credited != bin.amount_credited
+
+-- G5
+select *
+from bip
+     left outer join invoice_payments ip on bip.invoice_payment_id = ip.id
+     left outer join payments p on ip.payment_id = p.id
+where coalesce(p.RECORD_ID, 'NULL') != coalesce(bip.payment_number,'NULL')
+
+*****
+
+-- bipr
+-- H1a
+select *
+from invoice_payments ip
+     left outer join bipr on ip.id = bipr.invoice_payment_id
+where ( coalesce(ip.RECORD_ID, 'NULL') != coalesce(bipr.invoice_payment_record_id,'NULL')
+      or  coalesce(ip.ID, 'NULL') != coalesce(bipr.invoice_payment_id,'NULL')
+      or  coalesce(ip.invoice_id, 'NULL') != coalesce(bipr.invoice_id,'NULL')
+      or  coalesce(ip.type, 'NULL') != coalesce(bipr.invoice_payment_type,'NULL')
+      or  coalesce(ip.linked_invoice_payment_id, 'NULL') != coalesce(bipr.linked_invoice_payment_id,'NULL')
+      or  coalesce(ip.amount, 'NULL') != coalesce(bipr.amount,'NULL')
+      or  coalesce(ip.currency, 'NULL') != coalesce(bipr.currency,'NULL')
+      or  coalesce(ip.created_date, 'NULL') != coalesce(bipr.created_date,'NULL')
+      or  coalesce(ip.created_by, 'NULL') != coalesce(bipr.created_by,'NULL')
+      or  coalesce(ip.account_record_id, 'NULL') != coalesce(bipr.account_record_id,'NULL')
+      -- or  coalesce(ip.tenant_record_id, 'NULL') != coalesce(bipr.tenant_record_id,'NULL')
+     )
+      and ip.type = 'REFUND' -- ?
+
+-- H1b
+
+select *
+from bipr
+     left outer join invoice_payments ip on ip.id = bipr.invoice_payment_id
+where coalesce(ip.RECORD_ID, 'NULL') != coalesce(bipr.invoice_payment_record_id,'NULL')
+      or  coalesce(ip.ID, 'NULL') != coalesce(bipr.invoice_payment_id,'NULL')
+      or  coalesce(ip.invoice_id, 'NULL') != coalesce(bipr.invoice_id,'NULL')
+      or  coalesce(ip.type, 'NULL') != coalesce(bipr.invoice_payment_type,'NULL')
+      or  coalesce(ip.linked_invoice_payment_id, 'NULL') != coalesce(bipr.linked_invoice_payment_id,'NULL')
+      or  coalesce(ip.amount, 'NULL') != coalesce(bipr.amount,'NULL')
+      or  coalesce(ip.currency, 'NULL') != coalesce(bipr.currency,'NULL')
+      or  coalesce(ip.created_date, 'NULL') != coalesce(bipr.created_date,'NULL')
+      or  coalesce(ip.created_by, 'NULL') != coalesce(bipr.created_by,'NULL')
+      or  coalesce(ip.account_record_id, 'NULL') != coalesce(bipr.account_record_id,'NULL')
+      -- or  coalesce(ip.tenant_record_id, 'NULL') != coalesce(bipr.tenant_record_id,'NULL')
+      or  bipr.invoice_payment_type != 'REFUND' -- ?
+
+-- H2
+select *
+from bipr b
+     left outer join accounts a on a.id = b.account_id
+where coalesce(a.record_id )!=  coalesce(b.account_record_id ,'')
+      or coalesce(a.external_key, '') != coalesce(b.account_external_key,'')
+      or coalesce(a.name , '') != coalesce(b.account_name ,'')
+
+-- H3
+select *
+from bipr b
+     left outer join invoices i on i.id = b.invoice_id
+where coalesce(i.RECORD_ID, 'NULL') != coalesce(b.invoice_number,'NULL')
+      or coalesce(i.created_date, 'NULL') != coalesce(b.invoice_created_date,'NULL')
+      or coalesce(i.invoice_date, 'NULL') != coalesce(b.invoice_date,'NULL')
+      or coalesce(i.target_date, 'NULL') != coalesce(b.invoice_target_date,'NULL')
+      or coalesce(i.currency, 'NULL') != coalesce(b.invoice_currency,'NULL')
+
+-- H4
+select *
+from bipr b
+     left outer join bin on b.invoice_id = bin.invoice_id
+where b.invoice_balance != bin.balance
+      or b.invoice_amount_paid != bin.amount_paid
+      or b.invoice_amount_charged != bin.amount_charged
+      or b.invoice_original_amount_charged != bin.original_amount_charged
+      or b.invoice_amount_credited != bin.amount_credited
+
+-- H5
+select *
+from bip
+     left outer join invoice_payments ip on bip.invoice_payment_id = ip.id
+     left outer join payments p on ip.payment_id = p.id
+where coalesce(p.RECORD_ID, 'NULL') != coalesce(bip.payment_number,'NULL')
+
+
+*****
+
+-- bos
+
+*****
+
+-- bst
+
+select se.plan_name, se.phase_name, bst.next_slug
+from bst
+     left outer join subscription_events se on bst.subscription_event_record_id = se.record_id
+where 1=0
+      or se.requested_date != bst.requested_timestamp
+      or se.effective_date != bst.next_start_date
+      or se.subscription_id!= bst.subscription_id
+      or se.phase_name!= bst.next_slug
+      or se.price_list_name!= bst.next_price_list
+      or se.created_by!= bst.created_by
+      or se.created_date!= bst.created_date
+      or se.account_record_id!= bst.account_record_id
+      -- RI-1966 or se.tenant_record_id!= bst.tenant_record_id
+
+select *
+from bst  b
+     left outer join accounts a on a.id = b.account_id
+where coalesce(a.record_id )!=  coalesce(b.account_record_id ,'')
+      or coalesce(a.id , '') != coalesce(b.account_id ,'')
+      or coalesce(a.external_key, '') != coalesce(b.account_external_key,'')
+      or coalesce(a.name , '') != coalesce(b.account_name ,'')
diff --git a/osgi-bundles/bundles/analytics/src/test/java/com/ning/billing/osgi/bundles/analytics/dao/model/TestBusinessInvoicePaymentModelDao.java b/osgi-bundles/bundles/analytics/src/test/java/com/ning/billing/osgi/bundles/analytics/dao/model/TestBusinessInvoicePaymentModelDao.java
index b4dd1cb..51118ca 100644
--- a/osgi-bundles/bundles/analytics/src/test/java/com/ning/billing/osgi/bundles/analytics/dao/model/TestBusinessInvoicePaymentModelDao.java
+++ b/osgi-bundles/bundles/analytics/src/test/java/com/ning/billing/osgi/bundles/analytics/dao/model/TestBusinessInvoicePaymentModelDao.java
@@ -24,6 +24,60 @@ import com.ning.billing.osgi.bundles.analytics.AnalyticsTestSuiteNoDB;
 public class TestBusinessInvoicePaymentModelDao extends AnalyticsTestSuiteNoDB {
 
     @Test(groups = "fast")
+    public void testConstructorWithNullPaymentMethod() throws Exception {
+        final BusinessInvoicePaymentModelDao invoicePaymentModelDao = new BusinessInvoicePaymentModelDao(account,
+                                                                                                         accountRecordId,
+                                                                                                         invoice,
+                                                                                                         invoicePayment,
+                                                                                                         invoicePaymentRecordId,
+                                                                                                         payment,
+                                                                                                         refund,
+                                                                                                         null,
+                                                                                                         auditLog,
+                                                                                                         tenantRecordId,
+                                                                                                         reportGroup);
+        verifyCommonFields(invoicePaymentModelDao);
+        Assert.assertEquals(invoicePaymentModelDao.getPluginName(), BusinessInvoicePaymentBaseModelDao.DEFAULT_PLUGIN_NAME);
+        Assert.assertNull(invoicePaymentModelDao.getPluginCreatedDate());
+        Assert.assertNull(invoicePaymentModelDao.getPluginEffectiveDate());
+        Assert.assertNull(invoicePaymentModelDao.getPluginStatus());
+        Assert.assertNull(invoicePaymentModelDao.getPluginGatewayError());
+        Assert.assertNull(invoicePaymentModelDao.getPluginGatewayErrorCode());
+        Assert.assertNull(invoicePaymentModelDao.getPluginFirstReferenceId());
+        Assert.assertNull(invoicePaymentModelDao.getPluginSecondReferenceId());
+        Assert.assertNull(invoicePaymentModelDao.getPluginPmId());
+        Assert.assertNull(invoicePaymentModelDao.getPluginPmIsDefault());
+        Assert.assertNull(invoicePaymentModelDao.getPluginPmType());
+        Assert.assertNull(invoicePaymentModelDao.getPluginPmCcName());
+        Assert.assertNull(invoicePaymentModelDao.getPluginPmCcType());
+        Assert.assertNull(invoicePaymentModelDao.getPluginPmCcExpirationMonth());
+        Assert.assertNull(invoicePaymentModelDao.getPluginPmCcExpirationYear());
+        Assert.assertNull(invoicePaymentModelDao.getPluginPmCcLast4());
+        Assert.assertNull(invoicePaymentModelDao.getPluginPmAddress1());
+        Assert.assertNull(invoicePaymentModelDao.getPluginPmAddress2());
+        Assert.assertNull(invoicePaymentModelDao.getPluginPmCity());
+        Assert.assertNull(invoicePaymentModelDao.getPluginPmState());
+        Assert.assertNull(invoicePaymentModelDao.getPluginPmZip());
+        Assert.assertNull(invoicePaymentModelDao.getPluginPmCountry());
+    }
+
+    @Test(groups = "fast")
+    public void testConstructorWithNullRefund() throws Exception {
+        final BusinessInvoicePaymentModelDao invoicePaymentModelDao = new BusinessInvoicePaymentModelDao(account,
+                                                                                                         accountRecordId,
+                                                                                                         invoice,
+                                                                                                         invoicePayment,
+                                                                                                         invoicePaymentRecordId,
+                                                                                                         payment,
+                                                                                                         null,
+                                                                                                         paymentMethod,
+                                                                                                         auditLog,
+                                                                                                         tenantRecordId,
+                                                                                                         reportGroup);
+        verifyCommonFields(invoicePaymentModelDao);
+    }
+
+    @Test(groups = "fast")
     public void testConstructor() throws Exception {
         final BusinessInvoicePaymentModelDao invoicePaymentModelDao = new BusinessInvoicePaymentModelDao(account,
                                                                                                          accountRecordId,
@@ -36,6 +90,10 @@ public class TestBusinessInvoicePaymentModelDao extends AnalyticsTestSuiteNoDB {
                                                                                                          auditLog,
                                                                                                          tenantRecordId,
                                                                                                          reportGroup);
+        verifyCommonFields(invoicePaymentModelDao);
+    }
+
+    private void verifyCommonFields(final BusinessInvoicePaymentModelDao invoicePaymentModelDao) {
         verifyBusinessModelDaoBase(invoicePaymentModelDao, accountRecordId, tenantRecordId);
         Assert.assertEquals(invoicePaymentModelDao.getCreatedDate(), invoicePayment.getCreatedDate());
         Assert.assertEquals(invoicePaymentModelDao.getInvoicePaymentRecordId(), invoicePaymentRecordId);
@@ -52,7 +110,7 @@ public class TestBusinessInvoicePaymentModelDao extends AnalyticsTestSuiteNoDB {
         Assert.assertEquals(invoicePaymentModelDao.getInvoiceOriginalAmountCharged(), invoice.getOriginalChargedAmount());
         Assert.assertEquals(invoicePaymentModelDao.getInvoiceAmountCredited(), invoice.getCreditAdjAmount());
         Assert.assertEquals(invoicePaymentModelDao.getInvoicePaymentType(), invoicePayment.getType().toString());
-        //Assert.assertEquals(invoicePaymentModelDao.getPaymentNumber(), /* TODO */);
+        Assert.assertEquals(invoicePaymentModelDao.getPaymentNumber(), (Long) payment.getPaymentNumber().longValue());
         Assert.assertEquals(invoicePaymentModelDao.getLinkedInvoicePaymentId(), invoicePayment.getLinkedInvoicePaymentId());
         Assert.assertEquals(invoicePaymentModelDao.getAmount(), invoicePayment.getAmount());
         Assert.assertEquals(invoicePaymentModelDao.getCurrency(), invoicePayment.getCurrency().toString());
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 55b7496..abd47c7 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
@@ -20,11 +20,13 @@ import java.math.BigDecimal;
 
 import org.joda.time.DateTime;
 import org.joda.time.DateTimeZone;
+import org.mockito.Mockito;
 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.invoice.api.InvoicePayment.InvoicePaymentType;
 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;
@@ -206,7 +208,7 @@ public class TestBusinessAnalyticsSqlDao extends AnalyticsTestSuiteWithEmbeddedD
                                                                                                                         invoicePayment,
                                                                                                                         invoicePaymentRecordId,
                                                                                                                         payment,
-                                                                                                                        refund,
+                                                                                                                        null,
                                                                                                                         paymentMethod,
                                                                                                                         auditLog,
                                                                                                                         tenantRecordId,
@@ -225,6 +227,33 @@ public class TestBusinessAnalyticsSqlDao extends AnalyticsTestSuiteWithEmbeddedD
     }
 
     @Test(groups = "slow")
+    public void testSqlDaoForInvoicePaymentRefund() throws Exception {
+        Mockito.when(invoicePayment.getType()).thenReturn(InvoicePaymentType.REFUND);
+        final BusinessInvoicePaymentBaseModelDao businessInvoicePaymentRefundModelDao = 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(businessInvoicePaymentRefundModelDao.getTableName(), businessInvoicePaymentRefundModelDao, callContext);
+        Assert.assertEquals(analyticsSqlDao.getInvoicePaymentRefundsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 1);
+        Assert.assertEquals(analyticsSqlDao.getInvoicePaymentRefundsByAccountRecordId(accountRecordId, tenantRecordId, callContext).get(0), businessInvoicePaymentRefundModelDao);
+
+        // Delete and verify it doesn't exist anymore
+        analyticsSqlDao.deleteByAccountRecordId(businessInvoicePaymentRefundModelDao.getTableName(), accountRecordId, tenantRecordId, callContext);
+        Assert.assertEquals(analyticsSqlDao.getInvoicePaymentRefundsByAccountRecordId(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,
diff --git a/util/src/main/java/com/ning/billing/util/callcontext/InternalCallContextFactory.java b/util/src/main/java/com/ning/billing/util/callcontext/InternalCallContextFactory.java
index 450d29b..4c03231 100644
--- a/util/src/main/java/com/ning/billing/util/callcontext/InternalCallContextFactory.java
+++ b/util/src/main/java/com/ning/billing/util/callcontext/InternalCallContextFactory.java
@@ -220,6 +220,7 @@ public class InternalCallContextFactory {
 
     private Long getTenantRecordId(final TenantContext context) {
         // Default to single default tenant (e.g. single tenant mode)
+        // TODO Extract this convention (e.g. BusinessAnalyticsBase needs to know about it)
         if (context.getTenantId() == null) {
             return INTERNAL_TENANT_RECORD_ID;
         } else {