rrpawar96 commented on a change in pull request #2002:
URL: https://github.com/apache/fineract/pull/2002#discussion_r776685328



##########
File path: 
fineract-provider/src/main/java/org/apache/fineract/portfolio/client/domain/AccountNumberGenerator.java
##########
@@ -143,17 +166,60 @@ private String generateAccountNumber(Map<String, String> 
propertyMap, AccountNum
             }
             if 
(accountNumberPrefixType.getValue().equals(AccountNumberPrefixType.PREFIX_SHORT_NAME.getValue()))
 {
                 Integer prefixLength = prefix.length();
-                Integer numberLength = accountMaxLength - prefixLength;
-                accountNumber = StringUtils.leftPad(propertyMap.get(ID), 
numberLength, '0');
+
+                if (randomAccountNumber.isEnabled()) {
+                    accountNumber = accountNumber.substring(prefixLength);
+                } else {
+                    Integer numberLength = accountMaxLength - prefixLength;
+                    accountNumber = StringUtils.leftPad(propertyMap.get(ID), 
numberLength, '0');
+                }
             } else {
                 accountNumber = StringUtils.leftPad(accountNumber, 
Integer.valueOf(propertyMap.get(ID).length()), '0');
             }
 
             accountNumber = StringUtils.overlay(accountNumber, prefix, 0, 0);
+
+            if (randomAccountNumber.isEnabled()) {
+                randomNumberValidation(propertyMap, accountNumberFormat, 
accountNumber);
+            }
+        }
+        return accountNumber;
+    }
+
+    private String randomNumberGenerator(int accountMaxLength, Map<String, 
String> propertyMap) {
+        String randomNumber = RandomStringUtils.random(accountMaxLength, 
false, true);
+
+        BigInteger accNumber = new BigInteger(randomNumber);
+        if (accNumber.equals(BigInteger.ZERO)) { // to avoid account no. 00 in 
randomisation
+            randomNumber = randomNumberGenerator(accountMaxLength, 
propertyMap);
         }
+
+        String accountNumber = randomNumber.substring(0, accountMaxLength);
         return accountNumber;
     }
 
+    private void randomNumberValidation(Map<String, String> propertyMap, 
AccountNumberFormat accountNumberFormat, String accountNumber) {
+
+        String entityType = propertyMap.get(ENTITY_TYPE);
+        if (entityType.equals("client")) { // avoid duplication it will loop 
until it finds new random account no.
+
+            Client client = 
this.clientRepository.getClientByAccountNumber(accountNumber);
+            if (client != null) {
+                accountNumber = generateAccountNumber(propertyMap, 
accountNumberFormat);

Review comment:
       @ptuomola, comment has been addressed. I have tested it, it is working 
fine, if the randomNumberConflict is true then it will re-call the function and 
create a random number until it finds a new one and stores it in the 
accountNumber variable.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to