You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fineract.apache.org by ad...@apache.org on 2026/03/17 18:20:02 UTC

(fineract) branch develop updated: FINERACT-2421: Update `Transaction summary` reports

This is an automated email from the ASF dual-hosted git repository.

adamsaghy pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract.git


The following commit(s) were added to refs/heads/develop by this push:
     new 5add35ec31 FINERACT-2421: Update `Transaction summary` reports
5add35ec31 is described below

commit 5add35ec3129dc131cafe6bbb3a91e9b6f28f896
Author: Adam Saghy <ad...@gmail.com>
AuthorDate: Tue Mar 17 16:27:00 2026 +0100

    FINERACT-2421: Update `Transaction summary` reports
---
 .../db/changelog/tenant/changelog-tenant.xml       |    1 +
 ...ummary_reports_fix_after_originator_details.xml | 3264 ++++++++++++++++++++
 2 files changed, 3265 insertions(+)

diff --git a/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml b/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml
index 146469611c..5597a2e341 100644
--- a/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml
+++ b/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml
@@ -239,4 +239,5 @@
     <include file="parts/0218_standardize_character_set_and_collation.xml" relativeToChangelogFile="true" />
     <include file="parts/0219_remove_self_service_feature.xml" relativeToChangelogFile="true"/>
     <include file="parts/0220_add_login_retry_configuration.xml" relativeToChangelogFile="true" />
+    <include file="parts/0221_transaction_summary_reports_fix_after_originator_details.xml" relativeToChangelogFile="true" />
 </databaseChangeLog>
diff --git a/fineract-provider/src/main/resources/db/changelog/tenant/parts/0221_transaction_summary_reports_fix_after_originator_details.xml b/fineract-provider/src/main/resources/db/changelog/tenant/parts/0221_transaction_summary_reports_fix_after_originator_details.xml
new file mode 100644
index 0000000000..dceaa9e577
--- /dev/null
+++ b/fineract-provider/src/main/resources/db/changelog/tenant/parts/0221_transaction_summary_reports_fix_after_originator_details.xml
@@ -0,0 +1,3264 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements. See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership. The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License. You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied. See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
+                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd">
+    <changeSet author="fineract" id="1" context="postgresql">
+        <update tableName="stretchy_report">
+            <column name="report_sql"><![CDATA[
+WITH loan_originators AS (SELECT mlom.loan_id,
+             STRING_AGG(DISTINCT mlo.external_id, ', ' ORDER BY mlo.external_id) AS originator_external_ids
+      FROM m_loan_originator_mapping mlom
+               JOIN m_loan_originator mlo ON mlo.id = mlom.originator_id
+      GROUP BY mlom.loan_id),
+     slt_except_charge_adj_and_accrual AS (SELECT '${endDate}' AS transactiondate,
+          t.id,
+          l.name,
+          d.payment_type_id,
+          CASE
+              WHEN d.payment_type_id IS NULL AND t.classification_cv_id IS NOT NULL
+                  THEN (SELECT code_value FROM m_code_value WHERE id = t.classification_cv_id)
+              ELSE NULL END AS classification_name,
+          t.transaction_type_enum,
+          t.amount,
+          t.overpayment_portion_derived,
+          t.principal_portion_derived,
+          t.interest_portion_derived,
+          t.fee_charges_portion_derived,
+          t.penalty_charges_portion_derived,
+          e.status,
+          e.settlement_date,
+          e.owner_id,
+          m.charged_off_on_date,
+          t.transaction_date,
+          m.charge_off_reason_cv_id,
+          lo.originator_external_ids
+   FROM m_loan_transaction t
+            JOIN m_loan m ON m.id = t.loan_id
+            JOIN m_product_loan l ON l.id = m.product_id
+            LEFT JOIN m_payment_detail d ON d.id = t.payment_detail_id
+            LEFT JOIN m_external_asset_owner_transfer e
+                      ON e.loan_id = t.loan_id AND
+     e.settlement_date < '${endDate}' AND
+     e.effective_date_to >= '${endDate}'
+            LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+   WHERE t.submitted_on_date = '${endDate}'
+     AND t.transaction_type_enum not in (10, 26, 32, 34, 36, 39, 42, 43)
+     AND (t.office_id = ${officeId})),
+     slt_charge_adj AS (SELECT '${endDate}' AS transactiondate,
+           t.id,
+           l.name,
+           t.transaction_type_enum,
+           d.payment_type_id,
+           t.overpayment_portion_derived,
+           t.principal_portion_derived,
+           t.interest_portion_derived,
+           t.fee_charges_portion_derived,
+           t.penalty_charges_portion_derived,
+           t.amount,
+           e.status,
+           e.settlement_date,
+           e.owner_id,
+           m.charged_off_on_date,
+           t.transaction_date,
+           m.charge_off_reason_cv_id,
+           lo.originator_external_ids
+    FROM m_loan_transaction t
+             JOIN m_loan m ON m.id = t.loan_id
+             JOIN m_product_loan l ON l.id = m.product_id
+             LEFT JOIN m_payment_detail d ON d.id = t.payment_detail_id
+             LEFT JOIN m_external_asset_owner_transfer e
+   ON e.loan_id = t.loan_id AND e.settlement_date < '${endDate}' AND
+      e.effective_date_to >= '${endDate}'
+             LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+    WHERE t.submitted_on_date = '${endDate}'
+      AND t.transaction_type_enum = 26
+      AND (t.office_id = ${officeId})),
+     rlt_except_charge_adj_and_accrual AS (SELECT '${endDate}' AS transactiondate,
+          t.id,
+          l.name,
+          t.transaction_type_enum,
+          d.payment_type_id,
+          CASE
+              WHEN d.payment_type_id IS NULL AND t.classification_cv_id IS NOT NULL
+                  THEN (SELECT code_value FROM m_code_value WHERE id = t.classification_cv_id)
+              ELSE NULL END AS classification_name,
+          t.overpayment_portion_derived,
+          t.principal_portion_derived,
+          t.interest_portion_derived,
+          t.fee_charges_portion_derived,
+          t.penalty_charges_portion_derived,
+          t.amount,
+          e.status,
+          e.settlement_date,
+          e.owner_id,
+          m.charged_off_on_date,
+          t.transaction_date,
+          m.charge_off_reason_cv_id,
+          lo.originator_external_ids
+   FROM m_loan_transaction t
+            JOIN m_loan m ON m.id = t.loan_id
+            JOIN m_product_loan l ON l.id = m.product_id
+            LEFT JOIN m_payment_detail d ON d.id = t.payment_detail_id
+            LEFT JOIN m_external_asset_owner_transfer e
+                      ON e.loan_id = t.loan_id AND
+     e.settlement_date < '${endDate}' AND
+     e.effective_date_to >= '${endDate}'
+            LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+   WHERE t.reversed_on_date = '${endDate}'
+     AND t.transaction_type_enum not in (10, 26, 32, 34, 36, 39, 42, 43)
+     AND (t.office_id = ${officeId})),
+     rlt_charge_adj AS (SELECT '${endDate}' AS transactiondate,
+           t.id,
+           l.name,
+           t.transaction_type_enum,
+           d.payment_type_id,
+           t.overpayment_portion_derived,
+           t.principal_portion_derived,
+           t.interest_portion_derived,
+           t.fee_charges_portion_derived,
+           t.penalty_charges_portion_derived,
+           t.amount,
+           e.status,
+           e.settlement_date,
+           e.owner_id,
+           m.charged_off_on_date,
+           t.transaction_date,
+           m.charge_off_reason_cv_id,
+           lo.originator_external_ids
+    FROM m_loan_transaction t
+             JOIN m_loan m ON m.id = t.loan_id
+             JOIN m_product_loan l ON l.id = m.product_id
+             LEFT JOIN m_payment_detail d ON d.id = t.payment_detail_id
+             LEFT JOIN m_external_asset_owner_transfer e
+   ON e.loan_id = t.loan_id AND e.settlement_date < '${endDate}' AND
+      e.effective_date_to >= '${endDate}'
+             LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+    WHERE t.reversed_on_date = '${endDate}'
+      AND t.transaction_type_enum = 26
+      AND (t.office_id = ${officeId})),
+     slt_cap_income_amortization AS (SELECT '${endDate}' AS transactiondate,
+    t.id,
+    l.name,
+    t.transaction_type_enum,
+    d.payment_type_id,
+    CASE
+        WHEN d.payment_type_id IS NULL AND bt.classification_cv_id IS NOT NULL
+            THEN (SELECT code_value FROM m_code_value WHERE id = bt.classification_cv_id)
+        ELSE NULL END AS classification_name,
+    t.overpayment_portion_derived,
+    t.principal_portion_derived,
+    t.interest_portion_derived,
+    t.fee_charges_portion_derived,
+    t.penalty_charges_portion_derived,
+    t.amount,
+    e.status,
+    e.settlement_date,
+    e.owner_id,
+    m.charged_off_on_date,
+    t.transaction_date,
+    m.charge_off_reason_cv_id,
+    lo.originator_external_ids
+                 FROM m_loan_transaction t
+      JOIN m_loan m ON m.id = t.loan_id
+      JOIN m_product_loan l ON l.id = m.product_id
+      LEFT JOIN m_payment_detail d ON d.id = t.payment_detail_id
+      JOIN m_loan_amortization_allocation_mapping map
+           ON map.amortization_loan_transaction_id = t.id
+      JOIN m_loan_transaction bt ON bt.id = map.base_loan_transaction_id
+      LEFT JOIN m_external_asset_owner_transfer e ON e.loan_id = t.loan_id AND
+             e.settlement_date <
+             '${endDate}' AND
+             e.effective_date_to >=
+             '${endDate}'
+      LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+                 WHERE t.submitted_on_date = '${endDate}'
+                   AND t.is_reversed = false
+                   AND t.transaction_type_enum IN (36, 39, 42, 43)
+                   AND (t.office_id = ${officeId})),
+     rlt_cap_income_amortization AS (SELECT '${endDate}' AS transactiondate,
+    t.id,
+    l.name,
+    t.transaction_type_enum,
+    d.payment_type_id,
+    CASE
+        WHEN d.payment_type_id IS NULL AND bt.classification_cv_id IS NOT NULL
+            THEN (SELECT code_value FROM m_code_value WHERE id = bt.classification_cv_id)
+        ELSE NULL END AS classification_name,
+    t.overpayment_portion_derived,
+    t.principal_portion_derived,
+    t.interest_portion_derived,
+    t.fee_charges_portion_derived,
+    t.penalty_charges_portion_derived,
+    t.amount,
+    e.status,
+    e.settlement_date,
+    e.owner_id,
+    m.charged_off_on_date,
+    t.transaction_date,
+    m.charge_off_reason_cv_id,
+    lo.originator_external_ids
+                 FROM m_loan_transaction t
+      JOIN m_loan m ON m.id = t.loan_id
+      JOIN m_product_loan l ON l.id = m.product_id
+      LEFT JOIN m_payment_detail d ON d.id = t.payment_detail_id
+      JOIN m_loan_amortization_allocation_mapping map
+           ON map.amortization_loan_transaction_id = t.id
+      JOIN m_loan_transaction bt ON bt.id = map.base_loan_transaction_id
+      LEFT JOIN m_external_asset_owner_transfer e ON e.loan_id = t.loan_id AND
+             e.settlement_date <
+             '${endDate}' AND
+             e.effective_date_to >=
+             '${endDate}'
+      LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+                 WHERE t.reversed_on_date = '${endDate}'
+                   AND t.is_reversed = true
+                   AND t.transaction_type_enum IN (36, 39, 42, 43)
+                   AND (t.office_id = ${officeId})),
+     active_external_asset_owner_transfers AS (SELECT '${endDate}' AS transactiondate,
+              t.id,
+              p.name,
+              t.owner_id,
+              t.previous_owner_id,
+              dt.principal_outstanding_derived,
+              dt.interest_outstanding_derived,
+              dt.fee_charges_outstanding_derived,
+              dt.penalty_charges_outstanding_derived,
+              dt.total_overpaid_derived,
+              l.charged_off_on_date,
+              t.settlement_date,
+              l.charge_off_reason_cv_id,
+              lo.originator_external_ids
+       FROM m_external_asset_owner_transfer t
+                JOIN m_loan l ON l.id = t.loan_id
+                JOIN m_client c ON c.id = l.client_id
+                JOIN m_product_loan p ON p.id = l.product_id
+                JOIN m_external_asset_owner_transfer_details dt
+                     ON dt.asset_owner_transfer_id = t.id
+                LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+       WHERE t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE')
+         AND c.office_id = ${officeId}
+         AND t.settlement_date = '${endDate}'),
+     buyback_external_asset_owner_transfers AS (SELECT '${endDate}' AS transactiondate,
+               t.id,
+               p.name,
+               dt.principal_outstanding_derived,
+               dt.interest_outstanding_derived,
+               dt.fee_charges_outstanding_derived,
+               dt.penalty_charges_outstanding_derived,
+               dt.total_overpaid_derived,
+               l.charged_off_on_date,
+               t.settlement_date,
+               l.charge_off_reason_cv_id,
+               t.owner_id,
+               lo.originator_external_ids
+        FROM m_external_asset_owner_transfer t
+                 JOIN m_loan l ON l.id = t.loan_id
+                 JOIN m_client c ON c.id = l.client_id
+                 JOIN m_product_loan p ON p.id = l.product_id
+                 JOIN m_external_asset_owner_transfer_details dt
+                      ON dt.asset_owner_transfer_id = t.id
+                 LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+        WHERE t.status in ('BUYBACK', 'BUYBACK_INTERMEDIATE')
+          AND c.office_id = ${officeId}
+          AND t.settlement_date = '${endDate}')
+SELECT '${endDate}' AS TransactionDate,
+       a.product AS Product,
+       CASE
+           WHEN a.transaction_type = 9999 THEN 'Asset Transfer'
+           WHEN a.transaction_type = 99999 THEN 'Asset Buyback'
+           ELSE (SELECT enum_message_property
+                 FROM r_enum_value
+                 WHERE enum_name = 'transaction_type_enum'
+                   AND enum_id = a.transaction_type) END AS TransactionType_Name,
+       COALESCE((SELECT value FROM m_payment_type WHERE id = a.payment_type_id),
+                a.classification_name) AS PaymentType_Name,
+       a.chargetype AS chargetype,
+       a.reversal_indicator AS Reversed,
+       a.Allocation_Type AS Allocation_Type,
+       (SELECT code_value FROM m_code_value WHERE id = a.charge_off_reason_id) AS Chargeoff_ReasonCode,
+       CASE
+           WHEN a.transaction_type = 9999 THEN sum(a.amount) * + 1
+           WHEN a.transaction_type = 99999 THEN sum(a.amount) * - 1
+           WHEN a.transaction_type IN (2, 23, 21, 22, 24, 4, 5, 8, 6, 27, 9, 26, 28, 31, 33, 34, 37, 39, 41, 43) AND
+                a.reversal_indicator = false THEN sum(a.amount) * -1
+           WHEN a.transaction_type IN (2, 23, 21, 22, 24, 4, 5, 8, 6, 27, 9, 26, 28, 31, 33, 34, 37, 39, 41, 43) AND
+                a.reversal_indicator = true THEN sum(a.amount) * + 1
+           WHEN a.transaction_type IN (1, 10, 25, 20, 35, 36, 40, 42) AND a.reversal_indicator = false THEN sum(a.amount) * + 1
+           WHEN a.transaction_type IN (1, 10, 25, 20, 35, 36, 40, 42) AND a.reversal_indicator = true
+               THEN sum(a.amount) * -1 END AS Transaction_Amount,
+       (SELECT external_id
+        FROM m_external_asset_owner
+        WHERE id = a.asset_owner_id) AS Asset_owner_id,
+       (SELECT external_id
+        FROM m_external_asset_owner
+        WHERE id = a.from_asset_owner_id) AS From_asset_owner_id,
+       a.originator_external_ids
+FROM (SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Principal' AS Allocation_Type,
+             CASE
+                 WHEN t.transaction_type_enum in (1, 40, 41) THEN (CASE
+   WHEN t.amount is null THEN 0
+   WHEN t.overpayment_portion_derived is null THEN t.amount
+   WHEN t.overpayment_portion_derived is not null
+       THEN t.amount - t.overpayment_portion_derived
+   ELSE t.amount END)
+                 ELSE (CASE
+       WHEN t.principal_portion_derived is null THEN 0
+       ELSE t.principal_portion_derived end) END amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Interest' AS Allocation_Type,
+             CASE WHEN t.interest_portion_derived is null THEN 0 ELSE t.interest_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Fees' AS Allocation_Type,
+             CASE WHEN t.fee_charges_portion_derived is null THEN 0 ELSE t.fee_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Penalty' AS Allocation_Type,
+             CASE
+                 WHEN t.penalty_charges_portion_derived is null THEN 0
+                 ELSE t.penalty_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Unallocated Credit (UNC)' AS Allocation_Type,
+             CASE WHEN t.overpayment_portion_derived is null THEN 0 ELSE t.overpayment_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Fees' AS Allocation_Type,
+             CASE WHEN t.fee_charges_portion_derived is null THEN 0 ELSE t.fee_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Penalty' AS Allocation_Type,
+             CASE
+                 WHEN t.penalty_charges_portion_derived is null THEN 0
+                 ELSE t.penalty_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Interest' AS Allocation_Type,
+             CASE WHEN t.interest_portion_derived is null THEN 0 ELSE t.interest_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Principal' AS Allocation_Type,
+             CASE WHEN t.principal_portion_derived is null THEN 0 ELSE t.principal_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Unallocated Credit (UNC)' AS Allocation_Type,
+             CASE WHEN t.overpayment_portion_derived is null THEN 0 ELSE t.overpayment_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Fees' AS Allocation_Type,
+             CASE WHEN t.fee_charges_portion_derived is null THEN 0 ELSE t.fee_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Penalty' AS Allocation_Type,
+             CASE
+                 WHEN t.penalty_charges_portion_derived is null THEN 0
+                 ELSE t.penalty_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Interest' AS Allocation_Type,
+             CASE WHEN t.interest_portion_derived is null THEN 0 ELSE t.interest_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Principal' AS Allocation_Type,
+             CASE WHEN t.principal_portion_derived is null THEN 0 ELSE t.principal_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Unallocated Credit (UNC)' AS Allocation_Type,
+             CASE WHEN t.overpayment_portion_derived is null THEN 0 ELSE t.overpayment_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             l.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             mc.name AS chargetype,
+             false AS reversal_indicator,
+             'Fees' AS Allocation_Type,
+             CASE WHEN pd.amount is null THEN 0 ELSE pd.amount END AS amount,
+             CASE
+                 WHEN e.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND e.settlement_date < '${endDate}'
+ THEN e.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (m.charged_off_on_date <= t.transaction_date)
+ THEN m.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             lo.originator_external_ids
+      FROM m_loan_transaction t
+               JOIN m_loan m ON m.id = t.loan_id
+               JOIN m_product_loan l ON l.id = m.product_id
+               JOIN m_loan_charge_paid_by pd ON pd.loan_transaction_id = t.id
+               JOIN m_loan_charge c ON c.id = pd.loan_charge_id
+               JOIN m_charge mc ON mc.id = c.charge_id AND mc.is_penalty = false
+               LEFT JOIN m_external_asset_owner_transfer e
+     ON e.loan_id = t.loan_id AND e.settlement_date < '${endDate}' AND
+        e.effective_date_to >= '${endDate}'
+               LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+      WHERE t.submitted_on_date = '${endDate}'
+        AND t.transaction_type_enum = 10
+        AND t.is_reversed = false
+        AND (t.office_id = ${officeId})
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             l.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             mc.name AS chargetype,
+             false AS reversal_indicator,
+             'Penalty' AS Allocation_Type,
+             CASE WHEN pd.amount is null THEN 0 ELSE pd.amount END AS amount,
+             CASE
+                 WHEN e.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND e.settlement_date < '${endDate}'
+ THEN e.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (m.charged_off_on_date <= t.transaction_date)
+ THEN m.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             lo.originator_external_ids
+      FROM m_loan_transaction t
+               JOIN m_loan m ON m.id = t.loan_id
+               JOIN m_product_loan l ON l.id = m.product_id
+               JOIN m_loan_charge_paid_by pd ON pd.loan_transaction_id = t.id
+               JOIN m_loan_charge c ON c.id = pd.loan_charge_id
+               JOIN m_charge mc ON mc.id = c.charge_id AND mc.is_penalty = true
+               LEFT JOIN m_external_asset_owner_transfer e
+     ON e.loan_id = t.loan_id AND e.settlement_date < '${endDate}' AND
+        e.effective_date_to >= '${endDate}'
+               LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+      WHERE t.submitted_on_date = '${endDate}'
+        AND t.transaction_type_enum = 10
+        AND t.is_reversed = false
+        AND (t.office_id = ${officeId})
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             l.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Interest' AS Allocation_Type,
+             CASE WHEN t.interest_portion_derived is null THEN 0 ELSE t.interest_portion_derived END AS amount,
+             CASE
+                 WHEN e.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND e.settlement_date < '${endDate}'
+ THEN e.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (m.charged_off_on_date <= t.transaction_date)
+ THEN m.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             lo.originator_external_ids
+      FROM m_loan_transaction t
+               JOIN m_loan m ON m.id = t.loan_id
+               JOIN m_product_loan l ON l.id = m.product_id
+               LEFT JOIN m_external_asset_owner_transfer e
+     ON e.loan_id = t.loan_id AND e.settlement_date < '${endDate}' AND
+        e.effective_date_to >= '${endDate}'
+               LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+      WHERE t.submitted_on_date = '${endDate}'
+        AND t.transaction_type_enum in (10, 34)
+        AND t.is_reversed = false
+        AND (t.office_id = ${officeId})
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Fees' AS Allocation_Type,
+             CASE WHEN t.fee_charges_portion_derived is null THEN 0 ELSE t.fee_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Penalty' AS Allocation_Type,
+             CASE
+                 WHEN t.penalty_charges_portion_derived is null THEN 0
+                 ELSE t.penalty_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Interest' AS Allocation_Type,
+             CASE WHEN t.interest_portion_derived is null THEN 0 ELSE t.interest_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Principal' AS Allocation_Type,
+             CASE WHEN t.principal_portion_derived is null THEN 0 ELSE t.principal_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Unallocated Credit (UNC)' AS Allocation_Type,
+             CASE WHEN t.overpayment_portion_derived is null THEN 0 ELSE t.overpayment_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Principal' AS Allocation_Type,
+             CASE
+                 WHEN t.transaction_type_enum in (1, 40, 41) THEN (CASE
+   WHEN t.amount is null THEN 0
+   WHEN t.overpayment_portion_derived is null THEN t.amount
+   WHEN t.overpayment_portion_derived is not null
+       THEN t.amount - t.overpayment_portion_derived
+   ELSE t.amount END)
+                 ELSE (CASE
+       WHEN t.principal_portion_derived is null THEN 0
+       ELSE t.principal_portion_derived end) END amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Interest' AS Allocation_Type,
+             CASE WHEN t.interest_portion_derived is null THEN 0 ELSE t.interest_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Fees' AS Allocation_Type,
+             CASE WHEN t.fee_charges_portion_derived is null THEN 0 ELSE t.fee_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Penalty' AS Allocation_Type,
+             CASE
+                 WHEN t.penalty_charges_portion_derived is null THEN 0
+                 ELSE t.penalty_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Unallocated Credit (UNC)' AS Allocation_Type,
+             CASE WHEN t.overpayment_portion_derived is null THEN 0 ELSE t.overpayment_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             l.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             mc.name AS chargetype,
+             true AS reversal_indicator,
+             'Fees' AS Allocation_Type,
+             CASE WHEN pd.amount is null THEN 0 ELSE pd.amount END AS amount,
+             CASE
+                 WHEN e.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND e.settlement_date < '${endDate}'
+ THEN e.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (m.charged_off_on_date <= t.transaction_date)
+ THEN m.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             lo.originator_external_ids
+      FROM m_loan_transaction t
+               JOIN m_loan m ON m.id = t.loan_id
+               JOIN m_product_loan l ON l.id = m.product_id
+               JOIN m_loan_charge_paid_by pd ON pd.loan_transaction_id = t.id
+               JOIN m_loan_charge c ON c.id = pd.loan_charge_id
+               JOIN m_charge mc ON mc.id = c.charge_id AND mc.is_penalty = false
+               LEFT JOIN m_external_asset_owner_transfer e
+     ON e.loan_id = t.loan_id AND e.settlement_date < '${endDate}' AND
+        e.effective_date_to >= '${endDate}'
+               LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+      WHERE t.reversed_on_date = '${endDate}'
+        AND t.transaction_type_enum = 10
+        AND t.is_reversed = true
+        AND (t.office_id = ${officeId})
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             l.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             mc.name AS chargetype,
+             true AS reversal_indicator,
+             'Penalty' AS Allocation_Type,
+             CASE WHEN pd.amount is null THEN 0 ELSE pd.amount END AS amount,
+             CASE
+                 WHEN e.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND e.settlement_date < '${endDate}'
+ THEN e.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (m.charged_off_on_date <= t.transaction_date)
+ THEN m.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             lo.originator_external_ids
+      FROM m_loan_transaction t
+               JOIN m_loan m ON m.id = t.loan_id
+               JOIN m_product_loan l ON l.id = m.product_id
+               JOIN m_loan_charge_paid_by pd ON pd.loan_transaction_id = t.id
+               JOIN m_loan_charge c ON c.id = pd.loan_charge_id
+               JOIN m_charge mc ON mc.id = c.charge_id AND mc.is_penalty = true
+               LEFT JOIN m_external_asset_owner_transfer e
+     ON e.loan_id = t.loan_id AND e.settlement_date < '${endDate}' AND
+        e.effective_date_to >= '${endDate}'
+               LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+      WHERE t.reversed_on_date = '${endDate}'
+        AND t.transaction_type_enum = 10
+        AND t.is_reversed = true
+        AND (t.office_id = ${officeId})
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             l.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Interest' AS Allocation_Type,
+             CASE WHEN t.interest_portion_derived is null THEN 0 ELSE t.interest_portion_derived END AS amount,
+             CASE
+                 WHEN e.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND e.settlement_date < '${endDate}'
+ THEN e.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (m.charged_off_on_date <= t.transaction_date)
+ THEN m.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             lo.originator_external_ids
+      FROM m_loan_transaction t
+               JOIN m_loan m ON m.id = t.loan_id
+               JOIN m_product_loan l ON l.id = m.product_id
+               LEFT JOIN m_external_asset_owner_transfer e
+     ON e.loan_id = t.loan_id AND e.settlement_date < '${endDate}' AND
+        e.effective_date_to >= '${endDate}'
+               LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+      WHERE t.reversed_on_date = '${endDate}'
+        AND t.transaction_type_enum = 10
+        AND t.is_reversed = true
+        AND (t.office_id = ${officeId})
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Fees' AS Allocation_Type,
+             CASE WHEN t.fee_charges_portion_derived is null THEN 0 ELSE t.fee_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Penalty' AS Allocation_Type,
+             CASE
+                 WHEN t.penalty_charges_portion_derived is null THEN 0
+                 ELSE t.penalty_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Interest' AS Allocation_Type,
+             CASE WHEN t.interest_portion_derived is null THEN 0 ELSE t.interest_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Principal' AS Allocation_Type,
+             CASE WHEN t.principal_portion_derived is null THEN 0 ELSE t.principal_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Unallocated Credit (UNC)' AS Allocation_Type,
+             CASE WHEN t.overpayment_portion_derived is null THEN 0 ELSE t.overpayment_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+ THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null::bigint AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_charge_adj AS t
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             9999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Principal' AS Allocation_type,
+             t.principal_outstanding_derived AS amount,
+             t.owner_id AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.previous_owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM active_external_asset_owner_transfers AS t
+      WHERE t.principal_outstanding_derived > 0
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             9999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Interest' AS Allocation_type,
+             t.interest_outstanding_derived AS amount,
+             t.owner_id AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.previous_owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM active_external_asset_owner_transfers AS t
+      WHERE t.interest_outstanding_derived > 0
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             9999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Fees' AS Allocation_type,
+             t.fee_charges_outstanding_derived AS amount,
+             t.owner_id AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.previous_owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM active_external_asset_owner_transfers AS t
+      WHERE t.fee_charges_outstanding_derived > 0
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             9999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Penalty' AS Allocation_type,
+             t.penalty_charges_outstanding_derived AS amount,
+             t.owner_id AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.previous_owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM active_external_asset_owner_transfers AS t
+      WHERE t.penalty_charges_outstanding_derived > 0
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             9999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Unallocated Credit (UNC)' AS Allocation_type,
+             t.total_overpaid_derived AS amount,
+             t.owner_id AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.previous_owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM active_external_asset_owner_transfers AS t
+      WHERE t.total_overpaid_derived > 0
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             99999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Principal' AS Allocation_type,
+             t.principal_outstanding_derived AS amount,
+             null AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM buyback_external_asset_owner_transfers AS t
+      WHERE t.principal_outstanding_derived > 0
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             99999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Interest' AS Allocation_type,
+             t.interest_outstanding_derived AS amount,
+             null AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM buyback_external_asset_owner_transfers AS t
+      WHERE t.interest_outstanding_derived > 0
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             99999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Fees' AS Allocation_type,
+             t.fee_charges_outstanding_derived AS amount,
+             null AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM buyback_external_asset_owner_transfers AS t
+      WHERE t.fee_charges_outstanding_derived > 0
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             99999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Penalty' AS Allocation_type,
+             t.penalty_charges_outstanding_derived AS amount,
+             null AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM buyback_external_asset_owner_transfers AS t
+      WHERE t.penalty_charges_outstanding_derived > 0
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             99999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Unallocated Credit (UNC)' AS Allocation_type,
+             t.total_overpaid_derived * -1 AS amount,
+             null AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+ THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM buyback_external_asset_owner_transfers AS t
+      WHERE t.total_overpaid_derived > 0) a
+GROUP BY a.transactiondate, a.product, a.transaction_type, a.payment_type_id, a.classification_name, a.chargetype,
+         a.reversal_indicator, a.Allocation_Type, a.asset_owner_id, a.charge_off_reason_id, a.from_asset_owner_id,
+         a.originator_external_ids
+ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
+
+                                    ]]>
+            </column>
+            <where>report_name='Transaction Summary Report with Asset Owner'</where>
+        </update>
+    </changeSet>
+    <changeSet author="fineract" id="2" context="mysql">
+        <update tableName="stretchy_report">
+            <column name="report_sql"><![CDATA[
+
+WITH loan_originators AS (SELECT mlom.loan_id,
+              GROUP_CONCAT(DISTINCT mlo.external_id, ', ' ORDER BY mlo.external_id) AS originator_external_ids
+       FROM m_loan_originator_mapping mlom
+                JOIN m_loan_originator mlo ON mlo.id = mlom.originator_id
+       GROUP BY mlom.loan_id),
+     slt_except_charge_adj_and_accrual AS (SELECT '${endDate}' AS transactiondate,
+            t.id,
+            l.name,
+            d.payment_type_id,
+            CASE
+                WHEN d.payment_type_id IS NULL AND t.classification_cv_id IS NOT NULL
+                    THEN (SELECT code_value FROM m_code_value WHERE id = t.classification_cv_id)
+                ELSE NULL END AS classification_name,
+            t.transaction_type_enum,
+            t.amount,
+            t.overpayment_portion_derived,
+            t.principal_portion_derived,
+            t.interest_portion_derived,
+            t.fee_charges_portion_derived,
+            t.penalty_charges_portion_derived,
+            e.status,
+            e.settlement_date,
+            e.owner_id,
+            m.charged_off_on_date,
+            t.transaction_date,
+            m.charge_off_reason_cv_id,
+            lo.originator_external_ids
+     FROM m_loan_transaction t
+              JOIN m_loan m ON m.id = t.loan_id
+              JOIN m_product_loan l ON l.id = m.product_id
+              LEFT JOIN m_payment_detail d ON d.id = t.payment_detail_id
+              LEFT JOIN m_external_asset_owner_transfer e
+                        ON e.loan_id = t.loan_id AND
+        e.settlement_date < '${endDate}' AND
+        e.effective_date_to >= '${endDate}'
+              LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+     WHERE t.submitted_on_date = '${endDate}'
+       AND t.transaction_type_enum not in (10, 26, 32, 34, 36, 39, 42, 43)
+       AND (t.office_id = ${officeId})),
+     slt_charge_adj AS (SELECT '${endDate}' AS transactiondate,
+            t.id,
+            l.name,
+            t.transaction_type_enum,
+            d.payment_type_id,
+            t.overpayment_portion_derived,
+            t.principal_portion_derived,
+            t.interest_portion_derived,
+            t.fee_charges_portion_derived,
+            t.penalty_charges_portion_derived,
+            t.amount,
+            e.status,
+            e.settlement_date,
+            e.owner_id,
+            m.charged_off_on_date,
+            t.transaction_date,
+            m.charge_off_reason_cv_id,
+            lo.originator_external_ids
+     FROM m_loan_transaction t
+              JOIN m_loan m ON m.id = t.loan_id
+              JOIN m_product_loan l ON l.id = m.product_id
+              LEFT JOIN m_payment_detail d ON d.id = t.payment_detail_id
+              LEFT JOIN m_external_asset_owner_transfer e
+     ON e.loan_id = t.loan_id AND e.settlement_date < '${endDate}' AND
+        e.effective_date_to >= '${endDate}'
+              LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+     WHERE t.submitted_on_date = '${endDate}'
+       AND t.transaction_type_enum = 26
+       AND (t.office_id = ${officeId})),
+     rlt_except_charge_adj_and_accrual AS (SELECT '${endDate}' AS transactiondate,
+            t.id,
+            l.name,
+            t.transaction_type_enum,
+            d.payment_type_id,
+            CASE
+                WHEN d.payment_type_id IS NULL AND t.classification_cv_id IS NOT NULL
+                    THEN (SELECT code_value FROM m_code_value WHERE id = t.classification_cv_id)
+                ELSE NULL END AS classification_name,
+            t.overpayment_portion_derived,
+            t.principal_portion_derived,
+            t.interest_portion_derived,
+            t.fee_charges_portion_derived,
+            t.penalty_charges_portion_derived,
+            t.amount,
+            e.status,
+            e.settlement_date,
+            e.owner_id,
+            m.charged_off_on_date,
+            t.transaction_date,
+            m.charge_off_reason_cv_id,
+            lo.originator_external_ids
+     FROM m_loan_transaction t
+              JOIN m_loan m ON m.id = t.loan_id
+              JOIN m_product_loan l ON l.id = m.product_id
+              LEFT JOIN m_payment_detail d ON d.id = t.payment_detail_id
+              LEFT JOIN m_external_asset_owner_transfer e
+                        ON e.loan_id = t.loan_id AND
+        e.settlement_date < '${endDate}' AND
+        e.effective_date_to >= '${endDate}'
+              LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+     WHERE t.reversed_on_date = '${endDate}'
+       AND t.transaction_type_enum not in (10, 26, 32, 34, 36, 39, 42, 43)
+       AND (t.office_id = ${officeId})),
+     rlt_charge_adj AS (SELECT '${endDate}' AS transactiondate,
+            t.id,
+            l.name,
+            t.transaction_type_enum,
+            d.payment_type_id,
+            t.overpayment_portion_derived,
+            t.principal_portion_derived,
+            t.interest_portion_derived,
+            t.fee_charges_portion_derived,
+            t.penalty_charges_portion_derived,
+            t.amount,
+            e.status,
+            e.settlement_date,
+            e.owner_id,
+            m.charged_off_on_date,
+            t.transaction_date,
+            m.charge_off_reason_cv_id,
+            lo.originator_external_ids
+     FROM m_loan_transaction t
+              JOIN m_loan m ON m.id = t.loan_id
+              JOIN m_product_loan l ON l.id = m.product_id
+              LEFT JOIN m_payment_detail d ON d.id = t.payment_detail_id
+              LEFT JOIN m_external_asset_owner_transfer e
+     ON e.loan_id = t.loan_id AND e.settlement_date < '${endDate}' AND
+        e.effective_date_to >= '${endDate}'
+              LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+     WHERE t.reversed_on_date = '${endDate}'
+       AND t.transaction_type_enum = 26
+       AND (t.office_id = ${officeId})),
+     slt_cap_income_amortization AS (SELECT '${endDate}' AS transactiondate,
+      t.id,
+      l.name,
+      t.transaction_type_enum,
+      d.payment_type_id,
+      CASE
+          WHEN d.payment_type_id IS NULL AND bt.classification_cv_id IS NOT NULL
+              THEN (SELECT code_value FROM m_code_value WHERE id = bt.classification_cv_id)
+          ELSE NULL END AS classification_name,
+      t.overpayment_portion_derived,
+      t.principal_portion_derived,
+      t.interest_portion_derived,
+      t.fee_charges_portion_derived,
+      t.penalty_charges_portion_derived,
+      t.amount,
+      e.status,
+      e.settlement_date,
+      e.owner_id,
+      m.charged_off_on_date,
+      t.transaction_date,
+      m.charge_off_reason_cv_id,
+      lo.originator_external_ids
+                  FROM m_loan_transaction t
+        JOIN m_loan m ON m.id = t.loan_id
+        JOIN m_product_loan l ON l.id = m.product_id
+        LEFT JOIN m_payment_detail d ON d.id = t.payment_detail_id
+        JOIN m_loan_amortization_allocation_mapping map
+             ON map.amortization_loan_transaction_id = t.id
+        JOIN m_loan_transaction bt ON bt.id = map.base_loan_transaction_id
+        LEFT JOIN m_external_asset_owner_transfer e ON e.loan_id = t.loan_id AND
+                 e.settlement_date <
+                 '${endDate}' AND
+                 e.effective_date_to >=
+                 '${endDate}'
+        LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+                  WHERE t.submitted_on_date = '${endDate}'
+                    AND t.is_reversed = false
+                    AND t.transaction_type_enum IN (36, 39, 42, 43)
+                    AND (t.office_id = ${officeId})),
+     rlt_cap_income_amortization AS (SELECT '${endDate}' AS transactiondate,
+      t.id,
+      l.name,
+      t.transaction_type_enum,
+      d.payment_type_id,
+      CASE
+          WHEN d.payment_type_id IS NULL AND bt.classification_cv_id IS NOT NULL
+              THEN (SELECT code_value FROM m_code_value WHERE id = bt.classification_cv_id)
+          ELSE NULL END AS classification_name,
+      t.overpayment_portion_derived,
+      t.principal_portion_derived,
+      t.interest_portion_derived,
+      t.fee_charges_portion_derived,
+      t.penalty_charges_portion_derived,
+      t.amount,
+      e.status,
+      e.settlement_date,
+      e.owner_id,
+      m.charged_off_on_date,
+      t.transaction_date,
+      m.charge_off_reason_cv_id,
+      lo.originator_external_ids
+                  FROM m_loan_transaction t
+        JOIN m_loan m ON m.id = t.loan_id
+        JOIN m_product_loan l ON l.id = m.product_id
+        LEFT JOIN m_payment_detail d ON d.id = t.payment_detail_id
+        JOIN m_loan_amortization_allocation_mapping map
+             ON map.amortization_loan_transaction_id = t.id
+        JOIN m_loan_transaction bt ON bt.id = map.base_loan_transaction_id
+        LEFT JOIN m_external_asset_owner_transfer e ON e.loan_id = t.loan_id AND
+                 e.settlement_date <
+                 '${endDate}' AND
+                 e.effective_date_to >=
+                 '${endDate}'
+        LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+                  WHERE t.reversed_on_date = '${endDate}'
+                    AND t.is_reversed = true
+                    AND t.transaction_type_enum IN (36, 39, 42, 43)
+                    AND (t.office_id = ${officeId})),
+     active_external_asset_owner_transfers AS (SELECT '${endDate}' AS transactiondate,
+                t.id,
+                p.name,
+                t.owner_id,
+                t.previous_owner_id,
+                dt.principal_outstanding_derived,
+                dt.interest_outstanding_derived,
+                dt.fee_charges_outstanding_derived,
+                dt.penalty_charges_outstanding_derived,
+                dt.total_overpaid_derived,
+                l.charged_off_on_date,
+                t.settlement_date,
+                l.charge_off_reason_cv_id,
+                lo.originator_external_ids
+         FROM m_external_asset_owner_transfer t
+                  JOIN m_loan l ON l.id = t.loan_id
+                  JOIN m_client c ON c.id = l.client_id
+                  JOIN m_product_loan p ON p.id = l.product_id
+                  JOIN m_external_asset_owner_transfer_details dt
+                       ON dt.asset_owner_transfer_id = t.id
+                  LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+         WHERE t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE')
+           AND c.office_id = ${officeId}
+           AND t.settlement_date = '${endDate}'),
+     buyback_external_asset_owner_transfers AS (SELECT '${endDate}' AS transactiondate,
+                 t.id,
+                 p.name,
+                 dt.principal_outstanding_derived,
+                 dt.interest_outstanding_derived,
+                 dt.fee_charges_outstanding_derived,
+                 dt.penalty_charges_outstanding_derived,
+                 dt.total_overpaid_derived,
+                 l.charged_off_on_date,
+                 t.settlement_date,
+                 l.charge_off_reason_cv_id,
+                 t.owner_id,
+                 lo.originator_external_ids
+          FROM m_external_asset_owner_transfer t
+                   JOIN m_loan l ON l.id = t.loan_id
+                   JOIN m_client c ON c.id = l.client_id
+                   JOIN m_product_loan p ON p.id = l.product_id
+                   JOIN m_external_asset_owner_transfer_details dt
+                        ON dt.asset_owner_transfer_id = t.id
+                   LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+          WHERE t.status in ('BUYBACK', 'BUYBACK_INTERMEDIATE')
+            AND c.office_id = ${officeId}
+            AND t.settlement_date = '${endDate}')
+SELECT '${endDate}' AS TransactionDate,
+       a.product AS Product,
+       CASE
+           WHEN a.transaction_type = 9999 THEN 'Asset Transfer'
+           WHEN a.transaction_type = 99999 THEN 'Asset Buyback'
+           ELSE (SELECT enum_message_property
+                 FROM r_enum_value
+                 WHERE enum_name = 'transaction_type_enum'
+                   AND enum_id = a.transaction_type) END AS TransactionType_Name,
+       COALESCE((SELECT value FROM m_payment_type WHERE id = a.payment_type_id),
+                a.classification_name) AS PaymentType_Name,
+       a.chargetype AS chargetype,
+       a.reversal_indicator AS Reversed,
+       a.Allocation_Type AS Allocation_Type,
+       (SELECT code_value FROM m_code_value WHERE id = a.charge_off_reason_id) AS Chargeoff_ReasonCode,
+       CASE
+           WHEN a.transaction_type = 9999 THEN sum(a.amount) * + 1
+           WHEN a.transaction_type = 99999 THEN sum(a.amount) * - 1
+           WHEN a.transaction_type IN (2, 23, 21, 22, 24, 4, 5, 8, 6, 27, 9, 26, 28, 31, 33, 34, 37, 39, 41, 43) AND
+                a.reversal_indicator = false THEN sum(a.amount) * -1
+           WHEN a.transaction_type IN (2, 23, 21, 22, 24, 4, 5, 8, 6, 27, 9, 26, 28, 31, 33, 34, 37, 39, 41, 43) AND
+                a.reversal_indicator = true THEN sum(a.amount) * + 1
+           WHEN a.transaction_type IN (1, 10, 25, 20, 35, 36, 40, 42) AND a.reversal_indicator = false THEN sum(a.amount) * + 1
+           WHEN a.transaction_type IN (1, 10, 25, 20, 35, 36, 40, 42) AND a.reversal_indicator = true
+               THEN sum(a.amount) * -1 END AS Transaction_Amount,
+       (SELECT external_id
+        FROM m_external_asset_owner
+        WHERE id = a.asset_owner_id) AS Asset_owner_id,
+       (SELECT external_id
+        FROM m_external_asset_owner
+        WHERE id = a.from_asset_owner_id) AS From_asset_owner_id,
+       a.originator_external_ids AS Originator_External_Ids
+FROM (SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Principal' AS Allocation_Type,
+             CASE
+                 WHEN t.transaction_type_enum in (1, 40, 41) THEN (CASE
+      WHEN t.amount is null THEN 0
+      WHEN t.overpayment_portion_derived is null THEN t.amount
+      WHEN t.overpayment_portion_derived is not null
+          THEN t.amount - t.overpayment_portion_derived
+      ELSE t.amount END)
+                 ELSE (CASE
+        WHEN t.principal_portion_derived is null THEN 0
+        ELSE t.principal_portion_derived end) END amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Interest' AS Allocation_Type,
+             CASE WHEN t.interest_portion_derived is null THEN 0 ELSE t.interest_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null,
+             t.originator_external_ids AS from_asset_owner_id
+      FROM slt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Fees' AS Allocation_Type,
+             CASE WHEN t.fee_charges_portion_derived is null THEN 0 ELSE t.fee_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null,
+             t.originator_external_ids AS from_asset_owner_id
+      FROM slt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Penalty' AS Allocation_Type,
+             CASE
+                 WHEN t.penalty_charges_portion_derived is null THEN 0
+                 ELSE t.penalty_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null,
+             t.originator_external_ids AS from_asset_owner_id
+      FROM slt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Unallocated Credit (UNC)' AS Allocation_Type,
+             CASE WHEN t.overpayment_portion_derived is null THEN 0 ELSE t.overpayment_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null,
+             t.originator_external_ids AS from_asset_owner_id
+      FROM slt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Fees' AS Allocation_Type,
+             CASE WHEN t.fee_charges_portion_derived is null THEN 0 ELSE t.fee_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null,
+             t.originator_external_ids AS from_asset_owner_id
+      FROM slt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Penalty' AS Allocation_Type,
+             CASE
+                 WHEN t.penalty_charges_portion_derived is null THEN 0
+                 ELSE t.penalty_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null,
+             t.originator_external_ids AS from_asset_owner_id
+      FROM slt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Interest' AS Allocation_Type,
+             CASE WHEN t.interest_portion_derived is null THEN 0 ELSE t.interest_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null,
+             t.originator_external_ids AS from_asset_owner_id
+      FROM slt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Principal' AS Allocation_Type,
+             CASE WHEN t.principal_portion_derived is null THEN 0 ELSE t.principal_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null,
+             t.originator_external_ids AS from_asset_owner_id
+      FROM slt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Unallocated Credit (UNC)' AS Allocation_Type,
+             CASE WHEN t.overpayment_portion_derived is null THEN 0 ELSE t.overpayment_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null,
+             t.originator_external_ids AS from_asset_owner_id
+      FROM slt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Fees' AS Allocation_Type,
+             CASE WHEN t.fee_charges_portion_derived is null THEN 0 ELSE t.fee_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null,
+             t.originator_external_ids AS from_asset_owner_id
+      FROM rlt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Penalty' AS Allocation_Type,
+             CASE
+                 WHEN t.penalty_charges_portion_derived is null THEN 0
+                 ELSE t.penalty_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null,
+             t.originator_external_ids AS from_asset_owner_id
+      FROM rlt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Interest' AS Allocation_Type,
+             CASE WHEN t.interest_portion_derived is null THEN 0 ELSE t.interest_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null,
+             t.originator_external_ids AS from_asset_owner_id
+      FROM rlt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Principal' AS Allocation_Type,
+             CASE WHEN t.principal_portion_derived is null THEN 0 ELSE t.principal_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null,
+             t.originator_external_ids AS from_asset_owner_id
+      FROM rlt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Unallocated Credit (UNC)' AS Allocation_Type,
+             CASE WHEN t.overpayment_portion_derived is null THEN 0 ELSE t.overpayment_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.transaction_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null,
+             t.originator_external_ids AS from_asset_owner_id
+      FROM rlt_cap_income_amortization AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             l.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             mc.name AS chargetype,
+             false AS reversal_indicator,
+             'Fees' AS Allocation_Type,
+             CASE WHEN pd.amount is null THEN 0 ELSE pd.amount END AS amount,
+             CASE
+                 WHEN e.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND e.settlement_date < '${endDate}'
+  THEN e.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (m.charged_off_on_date <= t.transaction_date)
+  THEN m.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null,
+             lo.originator_external_ids AS from_asset_owner_id
+      FROM m_loan_transaction t
+               JOIN m_loan m ON m.id = t.loan_id
+               JOIN m_product_loan l ON l.id = m.product_id
+               JOIN m_loan_charge_paid_by pd ON pd.loan_transaction_id = t.id
+               JOIN m_loan_charge c ON c.id = pd.loan_charge_id
+               JOIN m_charge mc ON mc.id = c.charge_id AND mc.is_penalty = false
+               LEFT JOIN m_external_asset_owner_transfer e
+      ON e.loan_id = t.loan_id AND e.settlement_date < '${endDate}' AND
+         e.effective_date_to >= '${endDate}'
+               LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+      WHERE t.submitted_on_date = '${endDate}'
+        AND t.transaction_type_enum = 10
+        AND t.is_reversed = false
+        AND (t.office_id = ${officeId})
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             l.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             mc.name AS chargetype,
+             false AS reversal_indicator,
+             'Penalty' AS Allocation_Type,
+             CASE WHEN pd.amount is null THEN 0 ELSE pd.amount END AS amount,
+             CASE
+                 WHEN e.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND e.settlement_date < '${endDate}'
+  THEN e.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (m.charged_off_on_date <= t.transaction_date)
+  THEN m.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             lo.originator_external_ids AS from_asset_owner_id
+      FROM m_loan_transaction t
+               JOIN m_loan m ON m.id = t.loan_id
+               JOIN m_product_loan l ON l.id = m.product_id
+               JOIN m_loan_charge_paid_by pd ON pd.loan_transaction_id = t.id
+               JOIN m_loan_charge c ON c.id = pd.loan_charge_id
+               JOIN m_charge mc ON mc.id = c.charge_id AND mc.is_penalty = true
+               LEFT JOIN m_external_asset_owner_transfer e
+      ON e.loan_id = t.loan_id AND e.settlement_date < '${endDate}' AND
+         e.effective_date_to >= '${endDate}'
+               LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+      WHERE t.submitted_on_date = '${endDate}'
+        AND t.transaction_type_enum = 10
+        AND t.is_reversed = false
+        AND (t.office_id = ${officeId})
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             l.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Interest' AS Allocation_Type,
+             CASE WHEN t.interest_portion_derived is null THEN 0 ELSE t.interest_portion_derived END AS amount,
+             CASE
+                 WHEN e.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND e.settlement_date < '${endDate}'
+  THEN e.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (m.charged_off_on_date <= t.transaction_date)
+  THEN m.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             lo.originator_external_ids AS from_asset_owner_id
+      FROM m_loan_transaction t
+               JOIN m_loan m ON m.id = t.loan_id
+               JOIN m_product_loan l ON l.id = m.product_id
+               LEFT JOIN m_external_asset_owner_transfer e
+      ON e.loan_id = t.loan_id AND e.settlement_date < '${endDate}' AND
+         e.effective_date_to >= '${endDate}'
+               LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+      WHERE t.submitted_on_date = '${endDate}'
+        AND t.transaction_type_enum in (10, 34)
+        AND t.is_reversed = false
+        AND (t.office_id = ${officeId})
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Fees' AS Allocation_Type,
+             CASE WHEN t.fee_charges_portion_derived is null THEN 0 ELSE t.fee_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Penalty' AS Allocation_Type,
+             CASE
+                 WHEN t.penalty_charges_portion_derived is null THEN 0
+                 ELSE t.penalty_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Interest' AS Allocation_Type,
+             CASE WHEN t.interest_portion_derived is null THEN 0 ELSE t.interest_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Principal' AS Allocation_Type,
+             CASE WHEN t.principal_portion_derived is null THEN 0 ELSE t.principal_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Unallocated Credit (UNC)' AS Allocation_Type,
+             CASE WHEN t.overpayment_portion_derived is null THEN 0 ELSE t.overpayment_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM slt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Principal' AS Allocation_Type,
+             CASE
+                 WHEN t.transaction_type_enum in (1, 40, 41) THEN (CASE
+      WHEN t.amount is null THEN 0
+      WHEN t.overpayment_portion_derived is null THEN t.amount
+      WHEN t.overpayment_portion_derived is not null
+          THEN t.amount - t.overpayment_portion_derived
+      ELSE t.amount END)
+                 ELSE (CASE
+        WHEN t.principal_portion_derived is null THEN 0
+        ELSE t.principal_portion_derived end) END amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Interest' AS Allocation_Type,
+             CASE WHEN t.interest_portion_derived is null THEN 0 ELSE t.interest_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Fees' AS Allocation_Type,
+             CASE WHEN t.fee_charges_portion_derived is null THEN 0 ELSE t.fee_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Penalty' AS Allocation_Type,
+             CASE
+                 WHEN t.penalty_charges_portion_derived is null THEN 0
+                 ELSE t.penalty_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             t.classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Unallocated Credit (UNC)' AS Allocation_Type,
+             CASE WHEN t.overpayment_portion_derived is null THEN 0 ELSE t.overpayment_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_except_charge_adj_and_accrual AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             l.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             mc.name AS chargetype,
+             true AS reversal_indicator,
+             'Fees' AS Allocation_Type,
+             CASE WHEN pd.amount is null THEN 0 ELSE pd.amount END AS amount,
+             CASE
+                 WHEN e.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND e.settlement_date < '${endDate}'
+  THEN e.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (m.charged_off_on_date <= t.transaction_date)
+  THEN m.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             lo.originator_external_ids
+      FROM m_loan_transaction t
+               JOIN m_loan m ON m.id = t.loan_id
+               JOIN m_product_loan l ON l.id = m.product_id
+               JOIN m_loan_charge_paid_by pd ON pd.loan_transaction_id = t.id
+               JOIN m_loan_charge c ON c.id = pd.loan_charge_id
+               JOIN m_charge mc ON mc.id = c.charge_id AND mc.is_penalty = false
+               LEFT JOIN m_external_asset_owner_transfer e
+      ON e.loan_id = t.loan_id AND e.settlement_date < '${endDate}' AND
+         e.effective_date_to >= '${endDate}'
+               LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+      WHERE t.reversed_on_date = '${endDate}'
+        AND t.transaction_type_enum = 10
+        AND t.is_reversed = true
+        AND (t.office_id = ${officeId})
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             l.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             mc.name AS chargetype,
+             true AS reversal_indicator,
+             'Penalty' AS Allocation_Type,
+             CASE WHEN pd.amount is null THEN 0 ELSE pd.amount END AS amount,
+             CASE
+                 WHEN e.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND e.settlement_date < '${endDate}'
+  THEN e.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (m.charged_off_on_date <= t.transaction_date)
+  THEN m.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             lo.originator_external_ids
+      FROM m_loan_transaction t
+               JOIN m_loan m ON m.id = t.loan_id
+               JOIN m_product_loan l ON l.id = m.product_id
+               JOIN m_loan_charge_paid_by pd ON pd.loan_transaction_id = t.id
+               JOIN m_loan_charge c ON c.id = pd.loan_charge_id
+               JOIN m_charge mc ON mc.id = c.charge_id AND mc.is_penalty = true
+               LEFT JOIN m_external_asset_owner_transfer e
+      ON e.loan_id = t.loan_id AND e.settlement_date < '${endDate}' AND
+         e.effective_date_to >= '${endDate}'
+               LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+      WHERE t.reversed_on_date = '${endDate}'
+        AND t.transaction_type_enum = 10
+        AND t.is_reversed = true
+        AND (t.office_id = ${officeId})
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             l.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Interest' AS Allocation_Type,
+             CASE WHEN t.interest_portion_derived is null THEN 0 ELSE t.interest_portion_derived END AS amount,
+             CASE
+                 WHEN e.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND e.settlement_date < '${endDate}'
+  THEN e.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (m.charged_off_on_date <= t.transaction_date)
+  THEN m.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             lo.originator_external_ids
+      FROM m_loan_transaction t
+               JOIN m_loan m ON m.id = t.loan_id
+               JOIN m_product_loan l ON l.id = m.product_id
+               LEFT JOIN m_external_asset_owner_transfer e
+      ON e.loan_id = t.loan_id AND e.settlement_date < '${endDate}' AND
+         e.effective_date_to >= '${endDate}'
+               LEFT JOIN loan_originators lo ON lo.loan_id = t.loan_id
+      WHERE t.reversed_on_date = '${endDate}'
+        AND t.transaction_type_enum = 10
+        AND t.is_reversed = true
+        AND (t.office_id = ${officeId})
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Fees' AS Allocation_Type,
+             CASE WHEN t.fee_charges_portion_derived is null THEN 0 ELSE t.fee_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Penalty' AS Allocation_Type,
+             CASE
+                 WHEN t.penalty_charges_portion_derived is null THEN 0
+                 ELSE t.penalty_charges_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Interest' AS Allocation_Type,
+             CASE WHEN t.interest_portion_derived is null THEN 0 ELSE t.interest_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Principal' AS Allocation_Type,
+             CASE WHEN t.principal_portion_derived is null THEN 0 ELSE t.principal_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_charge_adj AS t
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             t.transaction_type_enum AS transaction_type,
+             t.payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             true AS reversal_indicator,
+             'Unallocated Credit (UNC)' AS Allocation_Type,
+             CASE WHEN t.overpayment_portion_derived is null THEN 0 ELSE t.overpayment_portion_derived END AS amount,
+             CASE
+                 WHEN t.status in ('ACTIVE', 'ACTIVE_INTERMEDIATE') AND t.settlement_date < '${endDate}'
+  THEN t.owner_id END AS asset_owner_id,
+             CASE
+                 WHEN t.transaction_type_enum = 27 OR (t.charged_off_on_date <= t.transaction_date)
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             null AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM rlt_charge_adj AS t
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             9999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Principal' AS Allocation_type,
+             t.principal_outstanding_derived AS amount,
+             t.owner_id AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.previous_owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM active_external_asset_owner_transfers AS t
+      WHERE t.principal_outstanding_derived > 0
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             9999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Interest' AS Allocation_type,
+             t.interest_outstanding_derived AS amount,
+             t.owner_id AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.previous_owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM active_external_asset_owner_transfers AS t
+      WHERE t.interest_outstanding_derived > 0
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             9999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Fees' AS Allocation_type,
+             t.fee_charges_outstanding_derived AS amount,
+             t.owner_id AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.previous_owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM active_external_asset_owner_transfers AS t
+      WHERE t.fee_charges_outstanding_derived > 0
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             9999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Penalty' AS Allocation_type,
+             t.penalty_charges_outstanding_derived AS amount,
+             t.owner_id AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.previous_owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM active_external_asset_owner_transfers AS t
+      WHERE t.penalty_charges_outstanding_derived > 0
+      UNION ALL
+      SELECT t.transactiondate,
+             t.id,
+             t.name AS product,
+             9999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Unallocated Credit (UNC)' AS Allocation_type,
+             t.total_overpaid_derived AS amount,
+             t.owner_id AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.previous_owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM active_external_asset_owner_transfers AS t
+      WHERE t.total_overpaid_derived > 0
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             99999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Principal' AS Allocation_type,
+             t.principal_outstanding_derived AS amount,
+             null AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM buyback_external_asset_owner_transfers AS t
+      WHERE t.principal_outstanding_derived > 0
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             99999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Interest' AS Allocation_type,
+             t.interest_outstanding_derived AS amount,
+             null AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM buyback_external_asset_owner_transfers AS t
+      WHERE t.interest_outstanding_derived > 0
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             99999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Fees' AS Allocation_type,
+             t.fee_charges_outstanding_derived AS amount,
+             null AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM buyback_external_asset_owner_transfers AS t
+      WHERE t.fee_charges_outstanding_derived > 0
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             99999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Penalty' AS Allocation_type,
+             t.penalty_charges_outstanding_derived AS amount,
+             null AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM buyback_external_asset_owner_transfers AS t
+      WHERE t.penalty_charges_outstanding_derived > 0
+      UNION ALL
+      SELECT '${endDate}' AS transactiondate,
+             t.id,
+             t.name AS product,
+             99999 AS transaction_type,
+             null AS payment_type_id,
+             null AS classification_name,
+             '' AS chargetype,
+             false AS reversal_indicator,
+             'Unallocated Credit (UNC)' AS Allocation_type,
+             t.total_overpaid_derived * -1 AS amount,
+             null AS asset_owner_id,
+             CASE
+                 WHEN t.charged_off_on_date <= t.settlement_date
+  THEN t.charge_off_reason_cv_id END AS charge_off_reason_id,
+             t.owner_id AS from_asset_owner_id,
+             t.originator_external_ids
+      FROM buyback_external_asset_owner_transfers AS t
+      WHERE t.total_overpaid_derived > 0) a
+GROUP BY a.transactiondate, a.product, a.transaction_type, a.payment_type_id, a.classification_name, a.chargetype,
+         a.reversal_indicator, a.Allocation_Type, a.asset_owner_id, a.charge_off_reason_id, a.from_asset_owner_id,
+         a.originator_external_ids
+ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
+
+                                    ]]>
+            </column>
+            <where>report_name='Transaction Summary Report with Asset Owner'</where>
+        </update>
+    </changeSet>
+
+    <!-- Transaction Summary Report -->
+    <changeSet author="fineract" id="3">
+        <update tableName="stretchy_report">
+            <column name="report_sql" value="SELECT
+            '${endDate}' AS TransactionDate,
+            a.product AS Product,
+            (
+              SELECT
+                enum_message_property
+              FROM
+                r_enum_value
+              WHERE
+                enum_name = 'transaction_type_enum'
+                and enum_id = a.transaction_type
+            ) TransactionType_Name,
+            (
+              select
+                value
+              from
+                m_payment_type
+              where
+                id = a.payment_type_id
+            ) as PaymentType_Name,
+            a.chargetype as chargetype,
+            a.reversal_indicator AS Reversed,
+            a.Allocation_Type AS Allocation_Type,
+            '' AS Chargeoff_ReasonCode,
+            case when a.transaction_type IN (2, 23, 21, 22, 24, 4, 5, 8, 6, 27, 9, 26, 41, 43)
+            AND a.reversal_indicator = false then sum(a.amount) * -1 when a.transaction_type IN (2, 23, 21, 22, 24, 4, 5, 8, 6, 27, 9, 26, 41, 43)
+            AND a.reversal_indicator = true then sum(a.amount) * + 1 when a.transaction_type IN (1, 10, 25, 20, 40, 42)
+            AND a.reversal_indicator = false then sum(a.amount) * + 1 when a.transaction_type IN (1, 10, 25, 20, 40, 42)
+            AND a.reversal_indicator = true then sum(a.amount) * -1 end AS Transaction_Amount
+          FROM
+            (
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                '' as chargetype,
+                false AS reversal_indicator,
+                'Principal' AS Allocation_Type,
+                CASE when t.transaction_type_enum in (1,40,41) then (
+                  case when t.amount is null then 0 else t.amount end
+                ) else (
+                  case when t.principal_portion_derived is null then 0 else t.principal_portion_derived end
+                ) end amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.submitted_on_date = '${endDate}'
+                and t.transaction_type_enum not in (10, 26)
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                '' as chargetype,
+                false AS reversal_indicator,
+                'Interest' AS Allocation_Type,
+                case when t.interest_portion_derived is null then 0 else t.interest_portion_derived end AS amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.submitted_on_date = '${endDate}'
+                and t.transaction_type_enum not in (10, 26)
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                '' as chargetype,
+                false AS reversal_indicator,
+                'Fees' AS Allocation_Type,
+                case when t.fee_charges_portion_derived is null then 0 else t.fee_charges_portion_derived end as amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.submitted_on_date = '${endDate}'
+                and t.transaction_type_enum not in (10, 26)
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                '' as chargetype,
+                false AS reversal_indicator,
+                'Penalty' AS Allocation_Type,
+                case when t.penalty_charges_portion_derived is null then 0 else t.penalty_charges_portion_derived end as amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.submitted_on_date = '${endDate}'
+                and t.transaction_type_enum not in (10, 26)
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                '' as chargetype,
+                false AS reversal_indicator,
+                'Unallocated Credit (UNC)' AS Allocation_Type,
+                case when t.overpayment_portion_derived is null then 0 else t.overpayment_portion_derived end as amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.submitted_on_date = '${endDate}'
+                and t.transaction_type_enum not in (10, 26)
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                null,
+                mc.name,
+                false AS reversal_indicator,
+                'Fees' AS Allocation_Type,
+                case when pd.amount is null then 0 else pd.amount end as amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                join m_loan_charge_paid_by pd on pd.loan_transaction_id = t.id
+                join m_loan_charge c on c.id = pd.loan_charge_id
+                join m_charge mc on mc.id = c.charge_id
+                and mc.is_penalty = false
+              WHERE
+                t.submitted_on_date = '${endDate}'
+                and t.transaction_type_enum = 10
+                and t.is_reversed = false
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                null,
+                mc.name,
+                false AS reversal_indicator,
+                'Penalty' AS Allocation_Type,
+                case when pd.amount is null then 0 else pd.amount end as amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                join m_loan_charge_paid_by pd on pd.loan_transaction_id = t.id
+                join m_loan_charge c on c.id = pd.loan_charge_id
+                join m_charge mc on mc.id = c.charge_id
+                and mc.is_penalty = true
+              WHERE
+                t.submitted_on_date = '${endDate}'
+                and t.transaction_type_enum = 10
+                and t.is_reversed = false
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                null,
+                '' as chargetype,
+                false AS reversal_indicator,
+                'Interest' AS Allocation_Type,
+                case when t.interest_portion_derived is null then 0 else t.interest_portion_derived end AS amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+              WHERE
+                t.submitted_on_date = '${endDate}'
+                and t.transaction_type_enum = 10
+                and t.is_reversed = false
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                mc.name,
+                false AS reversal_indicator,
+                'Fees' AS Allocation_Type,
+                case when pd.amount is null then 0 else pd.amount end as amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                join m_loan_charge_paid_by pd on pd.loan_transaction_id = t.id
+                join m_loan_charge c on c.id = pd.loan_charge_id
+                join m_charge mc on mc.id = c.charge_id
+                and mc.is_penalty = false
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.submitted_on_date = '${endDate}'
+                and t.transaction_type_enum = 26
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                mc.name,
+                false AS reversal_indicator,
+                'Penalty' AS Allocation_Type,
+                case when pd.amount is null then 0 else pd.amount end AS amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                join m_loan_charge_paid_by pd on pd.loan_transaction_id = t.id
+                join m_loan_charge c on c.id = pd.loan_charge_id
+                join m_charge mc on mc.id = c.charge_id
+                and mc.is_penalty = true
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.submitted_on_date = '${endDate}'
+                and t.transaction_type_enum = 26
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                '' as chargetype,
+                false AS reversal_indicator,
+                'Interest' AS Allocation_Type,
+                case when t.interest_portion_derived is null then 0 else t.interest_portion_derived end AS amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.submitted_on_date = '${endDate}'
+                and t.transaction_type_enum = 26
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                '' as chargetype,
+                false AS reversal_indicator,
+                'Principal' AS Allocation_Type,
+                case when t.principal_portion_derived is null then 0 else t.principal_portion_derived end AS amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.submitted_on_date = '${endDate}'
+                and t.transaction_type_enum = 26
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                '' as chargetype,
+                false AS reversal_indicator,
+                'Unallocated Credit (UNC)' AS Allocation_Type,
+                case when t.overpayment_portion_derived is null then 0 else t.overpayment_portion_derived end AS amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.submitted_on_date = '${endDate}'
+                and t.transaction_type_enum = 26
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                '' as chargetype,
+                true AS reversal_indicator,
+                'Principal' AS Allocation_Type,
+                CASE when t.transaction_type_enum in (1,40,41) then (
+                  case when t.amount is null then 0 else t.amount end
+                ) else (
+                  case when t.principal_portion_derived is null then 0 else t.principal_portion_derived end
+                ) end amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.reversed_on_date = '${endDate}'
+                and t.transaction_type_enum not in (10, 26)
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                '' as chargetype,
+                true AS reversal_indicator,
+                'Interest' AS Allocation_Type,
+                case when t.interest_portion_derived is null then 0 else t.interest_portion_derived end AS amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.reversed_on_date = '${endDate}'
+                and t.transaction_type_enum not in (10, 26)
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                '' as chargetype,
+                true AS reversal_indicator,
+                'Fees' AS Allocation_Type,
+                case when t.fee_charges_portion_derived is null then 0 else t.fee_charges_portion_derived end as amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.reversed_on_date = '${endDate}'
+                and t.transaction_type_enum not in (10, 26)
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                '' as chargetype,
+                true AS reversal_indicator,
+                'Penalty' AS Allocation_Type,
+                case when t.penalty_charges_portion_derived is null then 0 else t.penalty_charges_portion_derived end as amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.reversed_on_date = '${endDate}'
+                and t.transaction_type_enum not in (10, 26)
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                '' as chargetype,
+                true AS reversal_indicator,
+                'Unallocated Credit (UNC)' AS Allocation_Type,
+                case when t.overpayment_portion_derived is null then 0 else t.overpayment_portion_derived end as amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.reversed_on_date = '${endDate}'
+                and t.transaction_type_enum not in (10, 26)
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                null,
+                mc.name,
+                true AS reversal_indicator,
+                'Fees' AS Allocation_Type,
+                case when pd.amount is null then 0 else pd.amount end as amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                join m_loan_charge_paid_by pd on pd.loan_transaction_id = t.id
+                join m_loan_charge c on c.id = pd.loan_charge_id
+                join m_charge mc on mc.id = c.charge_id
+                and mc.is_penalty = false
+              WHERE
+                t.reversed_on_date = '${endDate}'
+                and t.transaction_type_enum = 10
+                and t.is_reversed = true
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                null,
+                mc.name,
+                true AS reversal_indicator,
+                'Penalty' AS Allocation_Type,
+                case when pd.amount is null then 0 else pd.amount end as amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                join m_loan_charge_paid_by pd on pd.loan_transaction_id = t.id
+                join m_loan_charge c on c.id = pd.loan_charge_id
+                join m_charge mc on mc.id = c.charge_id
+                and mc.is_penalty = true
+              WHERE
+                t.reversed_on_date = '${endDate}'
+                and t.transaction_type_enum = 10
+                and t.is_reversed = true
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                null,
+                '' as chargetype,
+                true AS reversal_indicator,
+                'Interest' AS Allocation_Type,
+                case when t.interest_portion_derived is null then 0 else t.interest_portion_derived end AS amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+              WHERE
+                t.reversed_on_date = '${endDate}'
+                and t.transaction_type_enum = 10
+                and t.is_reversed = true
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                mc.name,
+                true AS reversal_indicator,
+                'Fees' AS Allocation_Type,
+                case when pd.amount is null then 0 else pd.amount end as amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                join m_loan_charge_paid_by pd on pd.loan_transaction_id = t.id
+                join m_loan_charge c on c.id = pd.loan_charge_id
+                join m_charge mc on mc.id = c.charge_id
+                and mc.is_penalty = false
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.reversed_on_date = '${endDate}'
+                and t.transaction_type_enum = 26
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                mc.name,
+                true AS reversal_indicator,
+                'Penalty' AS Allocation_Type,
+                case when pd.amount is null then 0 else pd.amount end AS amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                join m_loan_charge_paid_by pd on pd.loan_transaction_id = t.id
+                join m_loan_charge c on c.id = pd.loan_charge_id
+                join m_charge mc on mc.id = c.charge_id
+                and mc.is_penalty = true
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.reversed_on_date = '${endDate}'
+                and t.transaction_type_enum = 26
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                '' as chargetype,
+                true AS reversal_indicator,
+                'Interest' AS Allocation_Type,
+                case when t.interest_portion_derived is null then 0 else t.interest_portion_derived end AS amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.reversed_on_date = '${endDate}'
+                and t.transaction_type_enum = 26
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                '' as chargetype,
+                true AS reversal_indicator,
+                'Principal' AS Allocation_Type,
+                case when t.principal_portion_derived is null then 0 else t.principal_portion_derived end AS amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.reversed_on_date = '${endDate}'
+                and t.transaction_type_enum = 26
+                and (t.office_id = ${officeId})
+              UNION ALL
+              SELECT
+                '${endDate}' AS transactiondate,
+                t.id,
+                l.name AS product,
+                t.transaction_type_enum AS transaction_type,
+                d.payment_type_id,
+                '' as chargetype,
+                true AS reversal_indicator,
+                'Unallocated Credit (UNC)' AS Allocation_Type,
+                case when t.overpayment_portion_derived is null then 0 else t.overpayment_portion_derived end AS amount
+              FROM
+                m_loan_transaction t
+                JOIN m_loan m ON m.id = t.loan_id
+                JOIN m_product_loan l ON l.id = m.product_id
+                left join m_payment_detail d on d.id = t.payment_detail_id
+              WHERE
+                t.reversed_on_date = '${endDate}'
+                and t.transaction_type_enum = 26
+                and (t.office_id = ${officeId})
+            ) a
+          GROUP BY
+            a.transactiondate,
+            a.product,
+            a.transaction_type,
+            a.payment_type_id,
+            a.chargetype,
+            a.reversal_indicator,
+            a.Allocation_Type
+          order by
+            1,
+            2,
+            3,
+            4,
+            5,
+            6,
+            7 "/>
+            <where>report_name='Transaction Summary Report'</where>
+        </update>
+    </changeSet>
+</databaseChangeLog>