|
| 1 | +<?xml version="1.0" encoding="UTF-8"?> |
| 2 | +<!-- |
| 3 | +
|
| 4 | + Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + or more contributor license agreements. See the NOTICE file |
| 6 | + distributed with this work for additional information |
| 7 | + regarding copyright ownership. The ASF licenses this file |
| 8 | + to you under the Apache License, Version 2.0 (the |
| 9 | + "License"); you may not use this file except in compliance |
| 10 | + with the License. You may obtain a copy of the License at |
| 11 | +
|
| 12 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +
|
| 14 | + Unless required by applicable law or agreed to in writing, |
| 15 | + software distributed under the License is distributed on an |
| 16 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + KIND, either express or implied. See the License for the |
| 18 | + specific language governing permissions and limitations |
| 19 | + under the License. |
| 20 | +
|
| 21 | +--> |
| 22 | +<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" |
| 23 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 24 | + xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd"> |
| 25 | + |
| 26 | + <changeSet author="fineract" id="trial-balance-summary-with-asset-owner-update-4"> |
| 27 | + <update tableName="stretchy_report"> |
| 28 | + <column name="report_sql"> |
| 29 | + <![CDATA[ |
| 30 | +WITH retained_earning AS ( |
| 31 | + SELECT DISTINCT |
| 32 | + '${endDate}' AS postingdate, |
| 33 | + lp.name AS product, |
| 34 | + gl_code AS glacct, |
| 35 | + COALESCE((SELECT name FROM acc_gl_account WHERE gl_code = e.gl_code), '') AS description, |
| 36 | + COALESCE(e.owner_external_id, 'self') AS assetowner, |
| 37 | + SUM(opening_balance_amount) AS beginningbalance, |
| 38 | + 0 AS debitmovement, |
| 39 | + 0 AS creditmovement, |
| 40 | + SUM(opening_balance_amount) AS endingbalance |
| 41 | + FROM acc_gl_journal_entry_annual_summary e, m_product_loan lp |
| 42 | + WHERE e.office_id = ${officeId} |
| 43 | + AND lp.id = product_id |
| 44 | + AND EXTRACT(YEAR FROM e.year_end_date) < EXTRACT(YEAR FROM CAST('${endDate}' AS DATE)) |
| 45 | + GROUP BY gl_code, lp.name, office_id, owner_external_id |
| 46 | +), |
| 47 | +aggregated_date AS ( |
| 48 | + SELECT MAX(aggregated_on_date_to) AS latest |
| 49 | + FROM m_journal_entry_aggregation_tracking |
| 50 | + WHERE aggregated_on_date_to < '${endDate}' |
| 51 | +), |
| 52 | +summary_snapshot_baseline_data AS ( |
| 53 | + SELECT |
| 54 | + lp.NAME AS productname, |
| 55 | + acc_gl_account.gl_code AS glcode, |
| 56 | + acc_gl_account.NAME AS glname, |
| 57 | + CASE WHEN ags.external_owner_id IS NULL THEN 0 ELSE ags.external_owner_id END AS assetowner, |
| 58 | + SUM(ags.debit_amount) AS debitamount, |
| 59 | + SUM(ags.credit_amount) AS creditamount |
| 60 | + FROM acc_gl_account |
| 61 | + JOIN m_journal_entry_aggregation_summary ags ON acc_gl_account.id = ags.gl_account_id |
| 62 | + JOIN m_product_loan lp ON lp.id = ags.product_id |
| 63 | + WHERE ags.entity_type_enum = 1 |
| 64 | + AND ags.manual_entry = FALSE |
| 65 | + AND ags.aggregated_on_date <= (SELECT latest FROM aggregated_date) |
| 66 | + AND (ags.office_id = ${officeId}) |
| 67 | + GROUP BY productname, glcode, glname, assetowner |
| 68 | +), |
| 69 | +post_snapshot_delta_data AS ( |
| 70 | + SELECT |
| 71 | + lp.NAME AS productname, |
| 72 | + acc_gl_account.gl_code AS glcode, |
| 73 | + acc_gl_account.NAME AS glname, |
| 74 | + CASE WHEN aw.owner_id IS NULL THEN 0 ELSE aw.owner_id END AS assetowner, |
| 75 | + SUM(CASE WHEN acc_gl_journal_entry.type_enum = 2 THEN amount ELSE 0 END) AS debitamount, |
| 76 | + SUM(CASE WHEN acc_gl_journal_entry.type_enum = 1 THEN amount ELSE 0 END) AS creditamount |
| 77 | + FROM acc_gl_account |
| 78 | + JOIN acc_gl_journal_entry ON acc_gl_account.id = acc_gl_journal_entry.account_id |
| 79 | + JOIN m_loan m ON m.id = acc_gl_journal_entry.entity_id |
| 80 | + JOIN m_product_loan lp ON lp.id = m.product_id |
| 81 | + LEFT JOIN m_external_asset_owner_journal_entry_mapping aw ON aw.journal_entry_id = acc_gl_journal_entry.id |
| 82 | + WHERE acc_gl_journal_entry.entity_type_enum = 1 |
| 83 | + AND acc_gl_journal_entry.manual_entry = FALSE |
| 84 | + AND ( |
| 85 | + (SELECT latest FROM aggregated_date) IS NULL |
| 86 | + OR acc_gl_journal_entry.submitted_on_date > (SELECT latest FROM aggregated_date) |
| 87 | + ) |
| 88 | + AND acc_gl_journal_entry.submitted_on_date < '${endDate}' |
| 89 | + AND (acc_gl_journal_entry.office_id = ${officeId}) |
| 90 | + GROUP BY productname, glcode, glname, assetowner |
| 91 | +), |
| 92 | +
|
| 93 | +/* |
| 94 | + * merged_historical_data: |
| 95 | + * Replaces FULL OUTER JOIN with: |
| 96 | + * baseline LEFT JOIN delta |
| 97 | + * UNION ALL |
| 98 | + * delta LEFT JOIN baseline (anti-join) |
| 99 | + */ |
| 100 | +merged_historical_data AS ( |
| 101 | + SELECT |
| 102 | + COALESCE(s.productname, p.productname) AS productname, |
| 103 | + COALESCE(s.glcode, p.glcode) AS glcode, |
| 104 | + COALESCE(s.glname, p.glname) AS glname, |
| 105 | + COALESCE(s.assetowner, p.assetowner, 0) AS assetowner, |
| 106 | + COALESCE(s.debitamount, 0) + COALESCE(p.debitamount, 0) AS debitamount, |
| 107 | + COALESCE(s.creditamount, 0) + COALESCE(p.creditamount, 0) AS creditamount |
| 108 | + FROM summary_snapshot_baseline_data s |
| 109 | + LEFT JOIN post_snapshot_delta_data p |
| 110 | + ON s.glcode = p.glcode |
| 111 | + AND s.productname = p.productname |
| 112 | + AND s.assetowner = p.assetowner |
| 113 | +
|
| 114 | + UNION ALL |
| 115 | +
|
| 116 | + SELECT |
| 117 | + p.productname AS productname, |
| 118 | + p.glcode AS glcode, |
| 119 | + p.glname AS glname, |
| 120 | + COALESCE(p.assetowner, 0) AS assetowner, |
| 121 | + COALESCE(p.debitamount, 0) AS debitamount, |
| 122 | + COALESCE(p.creditamount, 0) AS creditamount |
| 123 | + FROM post_snapshot_delta_data p |
| 124 | + LEFT JOIN summary_snapshot_baseline_data s |
| 125 | + ON s.glcode = p.glcode |
| 126 | + AND s.productname = p.productname |
| 127 | + AND s.assetowner = p.assetowner |
| 128 | + WHERE s.glcode IS NULL |
| 129 | +), |
| 130 | +current_cob_data AS ( |
| 131 | + SELECT |
| 132 | + lp.name AS productname, |
| 133 | + account_id, |
| 134 | + acc_gl_account.gl_code AS glcode, |
| 135 | + acc_gl_account.name AS glname, |
| 136 | + CASE WHEN aw.owner_id IS NULL THEN 0 ELSE aw.owner_id END AS assetowner, |
| 137 | + SUM(CASE WHEN acc_gl_journal_entry.type_enum = 2 THEN amount ELSE 0 END) AS debitamount, |
| 138 | + SUM(CASE WHEN acc_gl_journal_entry.type_enum = 1 THEN amount ELSE 0 END) AS creditamount |
| 139 | + FROM acc_gl_journal_entry |
| 140 | + JOIN acc_gl_account ON acc_gl_account.id = acc_gl_journal_entry.account_id |
| 141 | + JOIN m_loan m ON m.id = acc_gl_journal_entry.entity_id |
| 142 | + JOIN m_product_loan lp ON lp.id = m.product_id |
| 143 | + LEFT JOIN m_external_asset_owner_journal_entry_mapping aw ON aw.journal_entry_id = acc_gl_journal_entry.id |
| 144 | + WHERE acc_gl_journal_entry.entity_type_enum = 1 |
| 145 | + AND acc_gl_journal_entry.manual_entry = FALSE |
| 146 | + AND acc_gl_journal_entry.submitted_on_date = '${endDate}' |
| 147 | + AND (acc_gl_journal_entry.office_id = ${officeId}) |
| 148 | + GROUP BY productname, account_id, glcode, glname, assetowner |
| 149 | +) |
| 150 | +
|
| 151 | +SELECT * |
| 152 | +FROM ( |
| 153 | + SELECT * |
| 154 | + FROM retained_earning |
| 155 | + WHERE glacct = (SELECT gl_code FROM acc_gl_account WHERE name = 'Retained Earnings Prior Year') |
| 156 | +
|
| 157 | + UNION |
| 158 | +
|
| 159 | + SELECT |
| 160 | + txnreport.postingdate, |
| 161 | + txnreport.product, |
| 162 | + txnreport.glacct, |
| 163 | + txnreport.description, |
| 164 | + txnreport.assetowner, |
| 165 | + (COALESCE(txnreport.beginningbalance, 0) + COALESCE(summary.beginningbalance, 0)) AS beginningbalance, |
| 166 | + txnreport.debitmovement AS debitmovement, |
| 167 | + txnreport.creditmovement AS creditmovement, |
| 168 | + (COALESCE(txnreport.endingbalance, 0) + COALESCE(summary.beginningbalance, 0)) AS endingbalance |
| 169 | + FROM ( |
| 170 | + SELECT * |
| 171 | + FROM ( |
| 172 | + SELECT DISTINCT |
| 173 | + '${endDate}' AS postingdate, |
| 174 | + loan.pname AS product, |
| 175 | + loan.gl_code AS glacct, |
| 176 | + loan.glname AS description, |
| 177 | + COALESCE((SELECT external_id FROM m_external_asset_owner WHERE id = loan.assetowner), 'self') AS assetowner, |
| 178 | + loan.openingbalance AS beginningbalance, |
| 179 | + (loan.debitamount * 1) AS debitmovement, |
| 180 | + (loan.creditamount * -1) AS creditmovement, |
| 181 | + (loan.openingbalance + loan.debitamount - loan.creditamount) AS endingbalance |
| 182 | + FROM ( |
| 183 | + /* |
| 184 | + * This replaces: |
| 185 | + * g LEFT JOIN merged_historical_data |
| 186 | + * FULL OUTER JOIN current_cob_data |
| 187 | + * with a UNION ALL of: |
| 188 | + * (1) g LEFT JOIN mh LEFT JOIN c |
| 189 | + * (2) c-only rows (anti-join against g) |
| 190 | + */ |
| 191 | + SELECT DISTINCT |
| 192 | + g.pname AS pname, |
| 193 | + g.gl_code AS gl_code, |
| 194 | + g.glname AS glname, |
| 195 | + COALESCE(mh.assetowner, c.assetowner, 0) AS assetowner, |
| 196 | + COALESCE(mh.debitamount, 0) - COALESCE(mh.creditamount, 0) AS openingbalance, |
| 197 | + COALESCE(c.debitamount, 0) AS debitamount, |
| 198 | + COALESCE(c.creditamount, 0) AS creditamount |
| 199 | + FROM ( |
| 200 | + SELECT DISTINCT ag.gl_code, ag.id, pl.NAME AS pname, ag.NAME AS glname |
| 201 | + FROM acc_gl_account ag |
| 202 | + JOIN acc_product_mapping am ON am.gl_account_id = ag.id AND am.product_type = 1 |
| 203 | + JOIN m_product_loan pl ON pl.id = am.product_id |
| 204 | + ) g |
| 205 | + LEFT JOIN merged_historical_data mh |
| 206 | + ON g.gl_code = mh.glcode |
| 207 | + AND mh.productname = g.pname |
| 208 | + LEFT JOIN current_cob_data c |
| 209 | + ON g.gl_code = c.glcode |
| 210 | + AND c.productname = g.pname |
| 211 | + AND COALESCE(mh.assetowner, 0) = COALESCE(c.assetowner, 0) |
| 212 | +
|
| 213 | + UNION ALL |
| 214 | +
|
| 215 | + SELECT DISTINCT |
| 216 | + c.productname AS pname, |
| 217 | + c.glcode AS gl_code, |
| 218 | + c.glname AS glname, |
| 219 | + COALESCE(c.assetowner, 0) AS assetowner, |
| 220 | + COALESCE(mh.debitamount, 0) - COALESCE(mh.creditamount, 0) AS openingbalance, |
| 221 | + COALESCE(c.debitamount, 0) AS debitamount, |
| 222 | + COALESCE(c.creditamount, 0) AS creditamount |
| 223 | + FROM current_cob_data c |
| 224 | + LEFT JOIN ( |
| 225 | + SELECT DISTINCT ag.gl_code, pl.NAME AS pname |
| 226 | + FROM acc_gl_account ag |
| 227 | + JOIN acc_product_mapping am ON am.gl_account_id = ag.id AND am.product_type = 1 |
| 228 | + JOIN m_product_loan pl ON pl.id = am.product_id |
| 229 | + ) g2 |
| 230 | + ON g2.gl_code = c.glcode |
| 231 | + AND g2.pname = c.productname |
| 232 | + LEFT JOIN merged_historical_data mh |
| 233 | + ON mh.glcode = c.glcode |
| 234 | + AND mh.productname = c.productname |
| 235 | + AND COALESCE(mh.assetowner, 0) = COALESCE(c.assetowner, 0) |
| 236 | + WHERE g2.gl_code IS NULL |
| 237 | + ) loan |
| 238 | + ) a |
| 239 | + ) AS txnreport |
| 240 | + LEFT JOIN retained_earning summary |
| 241 | + ON txnreport.glacct = summary.glacct |
| 242 | + AND txnreport.assetowner = summary.assetowner |
| 243 | + AND summary.product = txnreport.product |
| 244 | +) report |
| 245 | +WHERE report.endingbalance != 0 |
| 246 | + OR report.debitmovement != 0 |
| 247 | + OR report.creditmovement != 0 |
| 248 | +ORDER BY glacct |
| 249 | +
|
| 250 | + ]]> |
| 251 | + </column> |
| 252 | + <where>report_name='Trial Balance Summary Report with Asset Owner'</where> |
| 253 | + </update> |
| 254 | + </changeSet> |
| 255 | +</databaseChangeLog> |
0 commit comments