Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,5 @@
<include file="parts/0207_add_allow_full_term_for_tranche.xml" relativeToChangelogFile="true" />
<include file="parts/0208_trial_balance_summary_with_asset_owner_journal_entry_aggregation_fix.xml" relativeToChangelogFile="true" />
<include file="parts/0209_transaction_summary_with_asset_owner_and_from_asset_owner_id_for_asset_sales.xml" relativeToChangelogFile="true" />
<include file="parts/0210_trial_balance_summary_fix_external_owners_and_aggregation.xml" relativeToChangelogFile="true" />
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
<?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="trial-balance-summary-with-asset-owner-update-5">
<update tableName="stretchy_report">
<column name="report_sql">
<![CDATA[
WITH retained_earning AS (
SELECT DISTINCT
'${endDate}' AS postingdate,
lp.name AS product,
gl_code AS glacct,
COALESCE((SELECT name FROM acc_gl_account WHERE gl_code = e.gl_code), '') AS description,
COALESCE(e.owner_external_id, 'self') AS assetowner,
SUM(opening_balance_amount) AS beginningbalance,
0 AS debitmovement,
0 AS creditmovement,
SUM(opening_balance_amount) AS endingbalance
FROM acc_gl_journal_entry_annual_summary e, m_product_loan lp
WHERE e.office_id = ${officeId}
AND lp.id = product_id
AND EXTRACT(YEAR FROM e.year_end_date) < EXTRACT(YEAR FROM CAST('${endDate}' AS DATE))
GROUP BY gl_code, lp.name, office_id, owner_external_id
),
aggregated_date AS (
SELECT MAX(aggregated_on_date_to) AS latest
FROM m_journal_entry_aggregation_tracking
WHERE aggregated_on_date_to < '${endDate}'
),
summary_snapshot_baseline_data AS (
SELECT
lp.NAME AS productname,
acc_gl_account.gl_code AS glcode,
acc_gl_account.NAME AS glname,
CASE WHEN ags.external_owner_id IS NULL THEN 0 ELSE ags.external_owner_id END AS assetowner,
SUM(ags.debit_amount) AS debitamount,
SUM(ags.credit_amount) AS creditamount
FROM acc_gl_account
JOIN m_journal_entry_aggregation_summary ags ON acc_gl_account.id = ags.gl_account_id
JOIN m_product_loan lp ON lp.id = ags.product_id
WHERE ags.entity_type_enum = 1
AND ags.manual_entry = FALSE
AND ags.aggregated_on_date <= (SELECT latest FROM aggregated_date)
AND (ags.office_id = ${officeId})
GROUP BY productname, glcode, glname, assetowner
),
post_snapshot_delta_data AS (
SELECT
lp.NAME AS productname,
acc_gl_account.gl_code AS glcode,
acc_gl_account.NAME AS glname,
CASE WHEN aw.owner_id IS NULL THEN 0 ELSE aw.owner_id END AS assetowner,
SUM(CASE WHEN acc_gl_journal_entry.type_enum = 2 THEN amount ELSE 0 END) AS debitamount,
SUM(CASE WHEN acc_gl_journal_entry.type_enum = 1 THEN amount ELSE 0 END) AS creditamount
FROM acc_gl_account
JOIN acc_gl_journal_entry ON acc_gl_account.id = acc_gl_journal_entry.account_id
JOIN m_loan m ON m.id = acc_gl_journal_entry.entity_id
JOIN m_product_loan lp ON lp.id = m.product_id
LEFT JOIN m_external_asset_owner_journal_entry_mapping aw ON aw.journal_entry_id = acc_gl_journal_entry.id
WHERE acc_gl_journal_entry.entity_type_enum = 1
AND acc_gl_journal_entry.manual_entry = FALSE
AND (
(SELECT latest FROM aggregated_date) IS NULL
OR acc_gl_journal_entry.submitted_on_date > (SELECT latest FROM aggregated_date)
)
AND acc_gl_journal_entry.submitted_on_date < '${endDate}'
AND (acc_gl_journal_entry.office_id = ${officeId})
GROUP BY productname, glcode, glname, assetowner
),
merged_historical_data AS (
SELECT
COALESCE(s.productname, p.productname) AS productname,
COALESCE(s.glcode, p.glcode) AS glcode,
COALESCE(s.glname, p.glname) AS glname,
COALESCE(s.assetowner, p.assetowner, 0) AS assetowner,
COALESCE(s.debitamount, 0) + COALESCE(p.debitamount, 0) AS debitamount,
COALESCE(s.creditamount, 0) + COALESCE(p.creditamount, 0) AS creditamount
FROM summary_snapshot_baseline_data s
LEFT JOIN post_snapshot_delta_data p
ON s.glcode = p.glcode
AND s.productname = p.productname
AND s.assetowner = p.assetowner

UNION ALL

SELECT
p.productname AS productname,
p.glcode AS glcode,
p.glname AS glname,
COALESCE(p.assetowner, 0) AS assetowner,
COALESCE(p.debitamount, 0) AS debitamount,
COALESCE(p.creditamount, 0) AS creditamount
FROM post_snapshot_delta_data p
LEFT JOIN summary_snapshot_baseline_data s
ON s.glcode = p.glcode
AND s.productname = p.productname
AND s.assetowner = p.assetowner
WHERE s.glcode IS NULL
),
current_cob_data AS (
SELECT
lp.name AS productname,
account_id,
acc_gl_account.gl_code AS glcode,
acc_gl_account.name AS glname,
CASE WHEN aw.owner_id IS NULL THEN 0 ELSE aw.owner_id END AS assetowner,
SUM(CASE WHEN acc_gl_journal_entry.type_enum = 2 THEN amount ELSE 0 END) AS debitamount,
SUM(CASE WHEN acc_gl_journal_entry.type_enum = 1 THEN amount ELSE 0 END) AS creditamount
FROM acc_gl_journal_entry
JOIN acc_gl_account ON acc_gl_account.id = acc_gl_journal_entry.account_id
JOIN m_loan m ON m.id = acc_gl_journal_entry.entity_id
JOIN m_product_loan lp ON lp.id = m.product_id
LEFT JOIN m_external_asset_owner_journal_entry_mapping aw ON aw.journal_entry_id = acc_gl_journal_entry.id
WHERE acc_gl_journal_entry.entity_type_enum = 1
AND acc_gl_journal_entry.manual_entry = FALSE
AND acc_gl_journal_entry.submitted_on_date = '${endDate}'
AND (acc_gl_journal_entry.office_id = ${officeId})
GROUP BY productname, account_id, glcode, glname, assetowner
)

SELECT *
FROM (
SELECT *
FROM retained_earning
WHERE glacct = (SELECT gl_code FROM acc_gl_account WHERE name = 'Retained Earnings Prior Year')

UNION

SELECT
txnreport.postingdate,
txnreport.product,
txnreport.glacct,
txnreport.description,
txnreport.assetowner,
(COALESCE(txnreport.beginningbalance, 0) + COALESCE(summary.beginningbalance, 0)) AS beginningbalance,
txnreport.debitmovement AS debitmovement,
txnreport.creditmovement AS creditmovement,
(COALESCE(txnreport.endingbalance, 0) + COALESCE(summary.beginningbalance, 0)) AS endingbalance
FROM (
SELECT *
FROM (
SELECT DISTINCT
'${endDate}' AS postingdate,
loan.pname AS product,
loan.gl_code AS glacct,
loan.glname AS description,
COALESCE((SELECT external_id FROM m_external_asset_owner WHERE id = loan.assetowner), 'self') AS assetowner,
loan.openingbalance AS beginningbalance,
(loan.debitamount * 1) AS debitmovement,
(loan.creditamount * -1) AS creditmovement,
(loan.openingbalance + loan.debitamount - loan.creditamount) AS endingbalance
FROM (
SELECT DISTINCT
g.pname AS pname,
g.gl_code AS gl_code,
g.glname AS glname,
COALESCE(mh.assetowner, c.assetowner, 0) AS assetowner,
COALESCE(mh.debitamount, 0) - COALESCE(mh.creditamount, 0) AS openingbalance,
COALESCE(c.debitamount, 0) AS debitamount,
COALESCE(c.creditamount, 0) AS creditamount
FROM (
SELECT DISTINCT ag.gl_code, ag.id, pl.NAME AS pname, ag.NAME AS glname
FROM acc_gl_account ag
JOIN acc_product_mapping am ON am.gl_account_id = ag.id AND am.product_type = 1
JOIN m_product_loan pl ON pl.id = am.product_id
) g
LEFT JOIN merged_historical_data mh
ON g.gl_code = mh.glcode
AND mh.productname = g.pname
LEFT JOIN current_cob_data c
ON g.gl_code = c.glcode
AND c.productname = g.pname
AND mh.assetowner = c.assetowner

UNION ALL

SELECT DISTINCT
c.productname AS pname,
c.glcode AS gl_code,
c.glname AS glname,
COALESCE(c.assetowner, 0) AS assetowner,
0 AS openingbalance,
COALESCE(c.debitamount, 0) AS debitamount,
COALESCE(c.creditamount, 0) AS creditamount
FROM current_cob_data c
LEFT JOIN (
SELECT g3.gl_code, g3.pname, mh.assetowner
FROM (
SELECT DISTINCT ag.gl_code, pl.NAME AS pname
FROM acc_gl_account ag
JOIN acc_product_mapping am ON am.gl_account_id = ag.id AND am.product_type = 1
JOIN m_product_loan pl ON pl.id = am.product_id
) g3
LEFT JOIN merged_historical_data mh
ON g3.gl_code = mh.glcode
AND mh.productname = g3.pname
) matched
ON matched.gl_code = c.glcode
AND matched.pname = c.productname
AND matched.assetowner = c.assetowner
WHERE matched.gl_code IS NULL
) loan
) a
) AS txnreport
LEFT JOIN retained_earning summary
ON txnreport.glacct = summary.glacct
AND txnreport.assetowner = summary.assetowner
AND summary.product = txnreport.product
) report
WHERE report.endingbalance != 0
OR report.debitmovement != 0
OR report.creditmovement != 0
ORDER BY glacct

]]>
</column>
<where>report_name='Trial Balance Summary Report with Asset Owner'</where>
</update>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ private Account createAccount(String name, String glCode, String type) {
return new Account(account.getId().intValue(), getAccountType(type));
}

public String getGlCode(Account account) {
GetGLAccountsResponse response = ok(
() -> fineractClient.generalLedgerAccount().retreiveAccount(account.getAccountID().longValue(), Collections.emptyMap()));
return response.getGlCode();
}

private Integer getAccountTypeId(String type) {
return switch (type) {
case "ASSET" -> 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* 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.
*/
package org.apache.fineract.integrationtests.client.feign.helpers;

import static org.apache.fineract.client.feign.util.FeignCalls.ok;

import java.time.Duration;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import org.apache.fineract.client.feign.FineractFeignClient;
import org.apache.fineract.client.feign.util.FeignCalls;
import org.apache.fineract.client.models.ExecuteJobRequest;
import org.apache.fineract.client.models.GetJobsResponse;
import org.apache.fineract.client.models.JobDetailHistoryData;
import org.awaitility.Awaitility;

public class FeignSchedulerHelper {

private final FineractFeignClient fineractClient;

public FeignSchedulerHelper(FineractFeignClient fineractClient) {
this.fineractClient = fineractClient;
}

public void stopScheduler() {
FeignCalls.executeVoid(() -> fineractClient.scheduler().changeSchedulerStatus("stop"));
}

public void startScheduler() {
FeignCalls.executeVoid(() -> fineractClient.scheduler().changeSchedulerStatus("start"));
}

public void executeAndAwaitJob(String jobDisplayName) {
stopScheduler();

List<GetJobsResponse> allJobs = ok(() -> fineractClient.schedulerJob().retrieveAll8());
GetJobsResponse targetJob = allJobs.stream().filter(j -> jobDisplayName.equals(j.getDisplayName())).findFirst()
.orElseThrow(() -> new RuntimeException("Job not found: " + jobDisplayName));

Instant beforeExecuteTime = Instant.now().truncatedTo(ChronoUnit.SECONDS);
FeignCalls.executeVoid(() -> fineractClient.schedulerJob().executeJob(targetJob.getJobId(), "executeJob", new ExecuteJobRequest()));

Awaitility.await().atMost(Duration.ofMinutes(2)).pollInterval(Duration.ofSeconds(1)).pollDelay(Duration.ofSeconds(1)).until(() -> {
GetJobsResponse job = ok(() -> fineractClient.schedulerJob().retrieveOne5(targetJob.getJobId()));
JobDetailHistoryData history = job.getLastRunHistory();
if (history == null || history.getJobRunStartTime() == null) {
return false;
}
OffsetDateTime startTime = history.getJobRunStartTime();
if (startTime.toInstant().isBefore(beforeExecuteTime)) {
return false;
}
return history.getJobRunEndTime() != null && !history.getJobRunEndTime().toInstant().isBefore(startTime.toInstant());
});
}
}
Loading
Loading