FINERACT-1805: Fix getting details of a share transactions
diff --git a/fineract-provider/src/main/java/org/apache/fineract/accounting/journalentry/service/JournalEntryReadPlatformServiceImpl.java b/fineract-provider/src/main/java/org/apache/fineract/accounting/journalentry/service/JournalEntryReadPlatformServiceImpl.java
index 88a826c..603f652 100644
--- a/fineract-provider/src/main/java/org/apache/fineract/accounting/journalentry/service/JournalEntryReadPlatformServiceImpl.java
+++ b/fineract-provider/src/main/java/org/apache/fineract/accounting/journalentry/service/JournalEntryReadPlatformServiceImpl.java
@@ -201,8 +201,11 @@
                     noteData = new NoteData(noteId, null, null, null, null, null, null, null, note, null, null, null, null, null, null);
                 }
                 Long transaction = null;
-                if (entityType != null) {
-                    transaction = Long.parseLong(transactionId.substring(1).trim());
+                if (entityType != null && transactionId != null) {
+                    String numericPart = transactionId.replaceAll("[^\\d]", "");
+                    if (!numericPart.isEmpty()) {
+                        transaction = Long.parseLong(numericPart);
+                    }
                 }
 
                 TransactionTypeEnumData transactionTypeEnumData = null;
diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/AccountingScenarioIntegrationTest.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/AccountingScenarioIntegrationTest.java
index c8c09f9..aca2e09 100644
--- a/integration-tests/src/test/java/org/apache/fineract/integrationtests/AccountingScenarioIntegrationTest.java
+++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/AccountingScenarioIntegrationTest.java
@@ -20,6 +20,7 @@
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
+import com.google.gson.Gson;
 import io.restassured.builder.RequestSpecBuilder;
 import io.restassured.builder.ResponseSpecBuilder;
 import io.restassured.http.ContentType;
@@ -42,7 +43,9 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 import java.util.TimeZone;
+import org.apache.fineract.client.models.GetJournalEntriesTransactionIdResponse;
 import org.apache.fineract.integrationtests.common.ClientHelper;
 import org.apache.fineract.integrationtests.common.CollateralManagementHelper;
 import org.apache.fineract.integrationtests.common.CommonConstants;
@@ -68,6 +71,10 @@
 import org.apache.fineract.integrationtests.common.savings.SavingsAccountHelper;
 import org.apache.fineract.integrationtests.common.savings.SavingsProductHelper;
 import org.apache.fineract.integrationtests.common.savings.SavingsStatusChecker;
+import org.apache.fineract.integrationtests.common.shares.ShareAccountHelper;
+import org.apache.fineract.integrationtests.common.shares.ShareAccountTransactionHelper;
+import org.apache.fineract.integrationtests.common.shares.ShareProductHelper;
+import org.apache.fineract.integrationtests.common.shares.ShareProductTransactionHelper;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -341,7 +348,7 @@
 
         this.savingsAccountHelper.addChargesForSavings(savingsID, withdrawalChargeId, false);
         ArrayList<HashMap> chargesPendingState = this.savingsAccountHelper.getSavingsCharges(savingsID);
-        Assertions.assertEquals(1, chargesPendingState.size());
+        assertEquals(1, chargesPendingState.size());
         HashMap savingsChargeForPay = chargesPendingState.get(0);
         HashMap paidCharge = this.savingsAccountHelper.getSavingsCharge(savingsID, (Integer) savingsChargeForPay.get("id"));
         Float chargeAmount = (Float) paidCharge.get("amount");
@@ -1115,4 +1122,71 @@
         return LocalDate.parse(dateAsString, Utils.dateFormatter);
     }
 
+    @Test
+    public void checkAccountingWithSharingFlow() {
+        this.savingsAccountHelper = new SavingsAccountHelper(requestSpec, responseSpec);
+
+        final Account assetAccount = this.accountHelper.createAssetAccount();
+        final Account incomeAccount = this.accountHelper.createIncomeAccount();
+        final Account equityAccount = this.accountHelper.createEquityAccount();
+        final Account liabilityAccount = this.accountHelper.createLiabilityAccount();
+
+        final Integer shareProductID = createSharesProduct(assetAccount, incomeAccount, equityAccount, liabilityAccount);
+
+        final Integer clientID = ClientHelper.createClient(requestSpec, responseSpec, DATE_OF_JOINING);
+        Assertions.assertNotNull(clientID);
+        final Integer savingsAccountId = SavingsAccountHelper.openSavingsAccount(requestSpec, responseSpec, clientID, "1000");
+        Assertions.assertNotNull(savingsAccountId);
+        final Integer shareAccountId = createShareAccount(clientID, shareProductID, savingsAccountId);
+        Assertions.assertNotNull(shareAccountId);
+        final Map<String, Object> shareAccountData = ShareAccountTransactionHelper.retrieveShareAccount(shareAccountId, requestSpec,
+                responseSpec);
+        Assertions.assertNotNull(shareAccountData);
+        // Approve share Account
+        final Map<String, Object> approveMap = new HashMap<>();
+        approveMap.put("note", "Share Account Approval Note");
+        approveMap.put("dateFormat", "dd MMMM yyyy");
+        approveMap.put("approvedDate", "01 Jan 2016");
+        approveMap.put("locale", "en");
+        final String approve = new Gson().toJson(approveMap);
+        ShareAccountTransactionHelper.postCommand("approve", shareAccountId, approve, requestSpec, responseSpec);
+        // Activate Share Account
+        final Map<String, Object> activateMap = new HashMap<>();
+        activateMap.put("dateFormat", "dd MMMM yyyy");
+        activateMap.put("activatedDate", "01 Jan 2016");
+        activateMap.put("locale", "en");
+        final String activateJson = new Gson().toJson(activateMap);
+        ShareAccountTransactionHelper.postCommand("activate", shareAccountId, activateJson, requestSpec, responseSpec);
+
+        // Checking sharing entries.
+        final JournalEntry[] assetAccountEntry = { new JournalEntry(Float.parseFloat("200"), JournalEntry.TransactionType.DEBIT) };
+        final JournalEntry[] liabilityAccountEntry = { new JournalEntry(Float.parseFloat("200"), JournalEntry.TransactionType.CREDIT) };
+        final JournalEntry[] checkJournalEntryForEquityAccount = {
+                new JournalEntry(Float.parseFloat("200"), JournalEntry.TransactionType.CREDIT) };
+        this.journalEntryHelper.checkJournalEntryForAssetAccount(assetAccount, "01 Jan 2016", assetAccountEntry);
+        this.journalEntryHelper.checkJournalEntryForLiabilityAccount(liabilityAccount, "01 Jan 2016", liabilityAccountEntry);
+        this.journalEntryHelper.checkJournalEntryForEquityAccount(equityAccount, "01 Jan 2016", checkJournalEntryForEquityAccount);
+
+        final String transactionId = this.journalEntryHelper.getJournalEntryTransactionIdByAccount(assetAccount, "01 Jan 2016",
+                assetAccountEntry);
+        Assertions.assertNotEquals("", transactionId);
+
+        final GetJournalEntriesTransactionIdResponse journalEntriesTransactionIdResponse = this.journalEntryHelper
+                .getJournalEntries(transactionId);
+        Assertions.assertNotNull(journalEntriesTransactionIdResponse);
+    }
+
+    public static Integer createSharesProduct(final Account... accounts) {
+        LOG.info("------------------------------CREATING NEW SHARE PRODUCT ---------------------------------------");
+        final String shareProductJSON = new ShareProductHelper().withCashBasedAccounting(accounts).build();
+        return ShareProductTransactionHelper.createShareProduct(shareProductJSON, requestSpec, responseSpec);
+    }
+
+    private Integer createShareAccount(final Integer clientId, final Integer productId, final Integer savingsAccountId) {
+        final String shareAccountJSON = new ShareAccountHelper().withClientId(String.valueOf(clientId))
+                .withProductId(String.valueOf(productId)).withExternalId("External1").withSavingsAccountId(String.valueOf(savingsAccountId))
+                .withSubmittedDate("01 Jan 2016").withApplicationDate("01 Jan 2016").withRequestedShares("100").build();
+        return ShareAccountTransactionHelper.createShareAccount(shareAccountJSON, requestSpec, responseSpec);
+    }
+
 }
diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/AccountHelper.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/AccountHelper.java
index cc98632..0d2a503 100644
--- a/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/AccountHelper.java
+++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/AccountHelper.java
@@ -69,6 +69,13 @@
         return new Account(accountID, Account.AccountType.LIABILITY);
     }
 
+    public Account createEquityAccount() {
+        final String equityAccountJSON = new GLAccountBuilder().withAccountTypeAsAsEquity().build();
+        final Integer accountID = Utils.performServerPost(this.requestSpec, this.responseSpec, CREATE_GL_ACCOUNT_URL, equityAccountJSON,
+                GL_ACCOUNT_ID_RESPONSE);
+        return new Account(accountID, Account.AccountType.EQUITY);
+    }
+
     public ArrayList getAccountingWithRunningBalances() {
         final String GET_RUNNING_BALANCE_URL = "/fineract-provider/api/v1/glaccounts?fetchRunningBalance=true";
         final ArrayList<HashMap> accountRunningBalance = Utils.performServerGet(this.requestSpec, this.responseSpec,
diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/JournalEntryHelper.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/JournalEntryHelper.java
index d1b771a..a464074 100644
--- a/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/JournalEntryHelper.java
+++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/JournalEntryHelper.java
@@ -61,6 +61,10 @@
         checkJournalEntry(null, liabilityAccount, date, accountEntries);
     }
 
+    public void checkJournalEntryForEquityAccount(final Account equityAccount, final String date, final JournalEntry... accountEntries) {
+        checkJournalEntry(null, equityAccount, date, accountEntries);
+    }
+
     public void checkJournalEntryForLiabilityAccount(final Integer officeId, final Account liabilityAccount, final String date,
             final JournalEntry... accountEntries) {
         checkJournalEntry(officeId, liabilityAccount, date, accountEntries);
@@ -72,6 +76,10 @@
 
     }
 
+    public String getJournalEntryTransactionIdByAccount(final Account account, final String date, final JournalEntry... accountEntries) {
+        return getJournalEntryTransactionId(account, date, accountEntries);
+    }
+
     private void checkJournalEntry(final Integer officeId, final Account account, final String date, final JournalEntry... accountEntries) {
         final String url = createURLForGettingAccountEntries(account, date, officeId);
         final ArrayList<HashMap> response = Utils.performServerGet(this.requestSpec, this.responseSpec, url, "pageItems");
@@ -89,6 +97,22 @@
         }
     }
 
+    private String getJournalEntryTransactionId(final Account account, final String date, final JournalEntry... accountEntries) {
+        final String url = createURLForGettingAccountEntries(account, date, null);
+        final ArrayList<HashMap> response = Utils.performServerGet(this.requestSpec, this.responseSpec, url, "pageItems");
+
+        for (JournalEntry entry : accountEntries) {
+            for (HashMap map : response) {
+                final HashMap entryType = (HashMap) map.get("entryType");
+                if (entry.getTransactionType().equals(entryType.get("value")) && entry.getTransactionAmount().equals(map.get("amount"))) {
+                    return map.get("transactionId").toString();
+                }
+            }
+        }
+
+        return "";
+    }
+
     private String createURLForGettingAccountEntries(final Account account, final String date, final Integer officeId) {
         String url = new String("/fineract-provider/api/v1/journalentries?glAccountId=" + account.getAccountID() + "&type="
                 + account.getAccountType() + "&fromDate=" + date + "&toDate=" + date + "&tenantIdentifier=default"
diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/shares/ShareProductHelper.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/shares/ShareProductHelper.java
index d806835..65d126f 100644
--- a/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/shares/ShareProductHelper.java
+++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/shares/ShareProductHelper.java
@@ -28,6 +28,7 @@
 import java.util.Locale;
 import java.util.Map;
 import org.apache.fineract.integrationtests.common.Utils;
+import org.apache.fineract.integrationtests.common.accounting.Account;
 import org.junit.jupiter.api.Assertions;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -62,6 +63,7 @@
 
     private List<Map<String, String>> charges = null;
     private List<Map<String, String>> marketPrices = null;
+    private Account[] accountList = null;
 
     public String build() {
         final HashMap<String, Object> map = new HashMap<>();
@@ -93,6 +95,10 @@
             map.put("marketPricePeriods", marketPrices);
         }
 
+        if (this.accountingRule.equals(CASH_BASED)) {
+            map.putAll(getAccountMappingForCashBased());
+        }
+
         String shareProductCreateJson = new Gson().toJson(map);
         LOG.info("{}", shareProductCreateJson);
         return shareProductCreateJson;
@@ -103,6 +109,12 @@
         return this;
     }
 
+    public ShareProductHelper withCashBasedAccounting(final Account[] account_list) {
+        this.accountingRule = CASH_BASED;
+        this.accountList = account_list;
+        return this;
+    }
+
     public ShareProductHelper withMarketPrice() {
         this.marketPrices = new ArrayList<>();
         LocalDate currentDate = Utils.getLocalDateOfTenant();
@@ -197,4 +209,29 @@
         // String>>)shareProductData.get("marketPricePeriods") ;
 
     }
+
+    private Map<String, String> getAccountMappingForCashBased() {
+        final Map<String, String> map = new HashMap<>();
+        if (accountList != null) {
+            for (int i = 0; i < this.accountList.length; i++) {
+                if (this.accountList[i].getAccountType().equals(Account.AccountType.ASSET)) {
+                    final String ID = this.accountList[i].getAccountID().toString();
+                    map.put("shareReferenceId", ID);
+                }
+                if (this.accountList[i].getAccountType().equals(Account.AccountType.LIABILITY)) {
+                    final String ID = this.accountList[i].getAccountID().toString();
+                    map.put("shareSuspenseId", ID);
+                }
+                if (this.accountList[i].getAccountType().equals(Account.AccountType.EQUITY)) {
+                    final String ID = this.accountList[i].getAccountID().toString();
+                    map.put("shareEquityId", ID);
+                }
+                if (this.accountList[i].getAccountType().equals(Account.AccountType.INCOME)) {
+                    final String ID = this.accountList[i].getAccountID().toString();
+                    map.put("incomeFromFeeAccountId", ID);
+                }
+            }
+        }
+        return map;
+    }
 }