You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fineract.apache.org by "adamsaghy (via GitHub)" <gi...@apache.org> on 2023/05/26 14:42:37 UTC

[GitHub] [fineract] adamsaghy opened a new pull request, #3207: FINERACT-1926: Asset transfer to external owner COB business step

adamsaghy opened a new pull request, #3207:
URL: https://github.com/apache/fineract/pull/3207

   ## Description
   
   Describe the changes made and why they were made.
   
   Ignore if these details are present on the associated [Apache Fineract JIRA ticket](https://github.com/apache/fineract/pull/1284).
   
   
   ## Checklist
   
   Please make sure these boxes are checked before submitting your pull request - thanks!
   
   - [ ] Write the commit message as per https://github.com/apache/fineract/#pull-requests
   
   - [ ] Acknowledge that we will not review PRs that are not passing the build _("green")_ - it is your responsibility to get a proposed PR to pass the build, not primarily the project's maintainers.
   
   - [ ] Create/update unit or integration tests for verifying the changes made.
   
   - [ ] Follow coding conventions at https://cwiki.apache.org/confluence/display/FINERACT/Coding+Conventions.
   
   - [ ] Add required Swagger annotation and update API documentation at fineract-provider/src/main/resources/static/legacy-docs/apiLive.htm with details of any API changes
   
   - [ ] Submission is not a "code dump".  (Large changes can be made "in repository" via a branch.  Ask on the developer mailing list for guidance, if required.)
   
   FYI our guidelines for code reviews are at https://cwiki.apache.org/confluence/display/FINERACT/Code+Review+Guide.
   


-- 
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: commits-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [fineract] adamsaghy merged pull request #3207: FINERACT-1926: Asset transfer to external owner COB business step

Posted by "adamsaghy (via GitHub)" <gi...@apache.org>.
adamsaghy merged PR #3207:
URL: https://github.com/apache/fineract/pull/3207


-- 
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: commits-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [fineract] galovics commented on a diff in pull request #3207: FINERACT-1926: Asset transfer to external owner COB business step

Posted by "galovics (via GitHub)" <gi...@apache.org>.
galovics commented on code in PR #3207:
URL: https://github.com/apache/fineract/pull/3207#discussion_r1210122162


##########
fineract-investor/src/main/java/org/apache/fineract/investor/cob/loan/LoanAccountOwnerTransferBusinessStep.java:
##########
@@ -0,0 +1,180 @@
+/**
+ * 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.investor.cob.loan;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.List;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.cob.loan.LoanCOBBusinessStep;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.investor.data.ExternalTransferStatus;
+import org.apache.fineract.investor.data.ExternalTransferSubStatus;
+import org.apache.fineract.investor.domain.ExternalAssetOwnerTransfer;
+import org.apache.fineract.investor.service.ExternalAssetOwnersReadService;
+import org.apache.fineract.investor.service.ExternalAssetOwnersWriteService;
+import org.apache.fineract.portfolio.loanaccount.domain.Loan;
+import org.springframework.stereotype.Component;
+
+@Component
+@RequiredArgsConstructor
+@Slf4j
+public class LoanAccountOwnerTransferBusinessStep implements LoanCOBBusinessStep {
+
+    private final ExternalAssetOwnersReadService externalAssetOwnersReadService;
+    private final ExternalAssetOwnersWriteService externalAssetOwnersWriteService;
+
+    @Override
+    public Loan execute(Loan loan) {
+        Long loanId = loan.getId();
+        log.debug("start processing loan ownership transfer business step for loan with Id [{}]", loanId);
+
+        LocalDate settlementDate = DateUtils.getBusinessLocalDate();
+        List<ExternalAssetOwnerTransfer> transferDataList = externalAssetOwnersReadService.retrieveTransfers(loanId, settlementDate,
+                List.of(ExternalTransferStatus.PENDING, ExternalTransferStatus.ACTIVE, ExternalTransferStatus.BUYBACK));
+        int size = transferDataList.size();
+
+        if (size > 2) {
+            throw new IllegalStateException(

Review Comment:
   I understand the purpose of this, but don't you think it'd be better to catch this when submitting a transfer?



##########
fineract-investor/src/main/java/org/apache/fineract/investor/service/ExternalAssetOwnersWriteService.java:
##########
@@ -20,10 +20,19 @@
 
 import org.apache.fineract.infrastructure.core.api.JsonCommand;
 import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
+import org.apache.fineract.investor.domain.ExternalAssetOwnerTransfer;
+import org.apache.fineract.investor.domain.ExternalAssetOwnerTransferLoanMapping;
 
 public interface ExternalAssetOwnersWriteService {
 
     CommandProcessingResult saleLoanByLoanId(JsonCommand command);
 
-    CommandProcessingResult buyBackLoanByLoanId(JsonCommand command);
+    CommandProcessingResult buybackLoanByLoanId(JsonCommand command);
+
+    ExternalAssetOwnerTransfer save(ExternalAssetOwnerTransfer externalAssetOwnerTransfer);

Review Comment:
   Same as above for the read service on boundaries.



##########
fineract-investor/src/main/java/org/apache/fineract/investor/service/ExternalAssetOwnersWriteService.java:
##########
@@ -20,10 +20,19 @@
 
 import org.apache.fineract.infrastructure.core.api.JsonCommand;
 import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
+import org.apache.fineract.investor.domain.ExternalAssetOwnerTransfer;
+import org.apache.fineract.investor.domain.ExternalAssetOwnerTransferLoanMapping;
 
 public interface ExternalAssetOwnersWriteService {
 
     CommandProcessingResult saleLoanByLoanId(JsonCommand command);
 
-    CommandProcessingResult buyBackLoanByLoanId(JsonCommand command);
+    CommandProcessingResult buybackLoanByLoanId(JsonCommand command);
+
+    ExternalAssetOwnerTransfer save(ExternalAssetOwnerTransfer externalAssetOwnerTransfer);
+
+    void removeActiveMapping(Long loanId, ExternalAssetOwnerTransfer externalAssetOwnerTransfer);

Review Comment:
   Bonus: here as well since the parameter is an entity.



##########
fineract-investor/src/main/java/org/apache/fineract/investor/service/ExternalAssetOwnersReadServiceImpl.java:
##########
@@ -48,4 +52,16 @@ public List<ExternalTransferData> retrieveTransferData(Long loanId, String exter
         }
         return result.stream().map(mapper::mapTransfer).toList();
     }
+
+    @Override

Review Comment:
   Let's not let entities past through the transactional boundary (i.e. the service is annotated with Transactional so anything that's coming out of that will be detached) and could cause maintenance issues in the future (just like for the majority of fineract today).



##########
fineract-investor/src/main/java/org/apache/fineract/investor/service/ExternalAssetOwnersWriteServiceImpl.java:
##########
@@ -91,6 +94,25 @@ public CommandProcessingResult buyBackLoanByLoanId(JsonCommand command) {
         return buildResponseData(savedExternalAssetOwnerTransfer);
     }
 
+    @Override
+    @Transactional

Review Comment:
   Let's simply annotate the class instead.



##########
fineract-investor/src/main/java/org/apache/fineract/investor/service/ExternalAssetOwnersWriteService.java:
##########
@@ -20,10 +20,19 @@
 
 import org.apache.fineract.infrastructure.core.api.JsonCommand;
 import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
+import org.apache.fineract.investor.domain.ExternalAssetOwnerTransfer;
+import org.apache.fineract.investor.domain.ExternalAssetOwnerTransferLoanMapping;
 
 public interface ExternalAssetOwnersWriteService {
 
     CommandProcessingResult saleLoanByLoanId(JsonCommand command);
 
-    CommandProcessingResult buyBackLoanByLoanId(JsonCommand command);
+    CommandProcessingResult buybackLoanByLoanId(JsonCommand command);
+
+    ExternalAssetOwnerTransfer save(ExternalAssetOwnerTransfer externalAssetOwnerTransfer);
+
+    void removeActiveMapping(Long loanId, ExternalAssetOwnerTransfer externalAssetOwnerTransfer);
+
+    ExternalAssetOwnerTransferLoanMapping createActiveMapping(Long loanId, ExternalAssetOwnerTransfer externalAssetOwnerTransfer);

Review Comment:
   Same as above for the read service on boundaries.



-- 
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: commits-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [fineract] adamsaghy commented on pull request #3207: FINERACT-1926: Asset transfer to external owner COB business step

Posted by "adamsaghy (via GitHub)" <gi...@apache.org>.
adamsaghy commented on PR #3207:
URL: https://github.com/apache/fineract/pull/3207#issuecomment-1568689797

   > LGTM from functional/use cases perspective.
   > Is adding this business step to cob job a separate change. (liquibase script to add step to cob, integrating/configuring this step with fineract-provider)
   
   It will not be added by default.


-- 
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: commits-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [fineract] taskain7 commented on a diff in pull request #3207: FINERACT-1926: Asset transfer to external owner COB business step

Posted by "taskain7 (via GitHub)" <gi...@apache.org>.
taskain7 commented on code in PR #3207:
URL: https://github.com/apache/fineract/pull/3207#discussion_r1210098560


##########
fineract-investor/src/main/java/org/apache/fineract/investor/cob/loan/LoanAccountOwnerTransferBusinessStep.java:
##########
@@ -0,0 +1,180 @@
+/**
+ * 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.investor.cob.loan;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.List;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.cob.loan.LoanCOBBusinessStep;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.investor.data.ExternalTransferStatus;
+import org.apache.fineract.investor.data.ExternalTransferSubStatus;
+import org.apache.fineract.investor.domain.ExternalAssetOwnerTransfer;
+import org.apache.fineract.investor.service.ExternalAssetOwnersReadService;
+import org.apache.fineract.investor.service.ExternalAssetOwnersWriteService;
+import org.apache.fineract.portfolio.loanaccount.domain.Loan;
+import org.springframework.stereotype.Component;
+
+@Component
+@RequiredArgsConstructor
+@Slf4j
+public class LoanAccountOwnerTransferBusinessStep implements LoanCOBBusinessStep {
+
+    private final ExternalAssetOwnersReadService externalAssetOwnersReadService;
+    private final ExternalAssetOwnersWriteService externalAssetOwnersWriteService;
+
+    @Override
+    public Loan execute(Loan loan) {
+        Long loanId = loan.getId();
+        log.debug("start processing loan ownership transfer business step for loan with Id [{}]", loanId);
+
+        LocalDate settlementDate = DateUtils.getBusinessLocalDate();
+        List<ExternalAssetOwnerTransfer> transferDataList = externalAssetOwnersReadService.retrieveTransfers(loanId, settlementDate,
+                List.of(ExternalTransferStatus.PENDING, ExternalTransferStatus.ACTIVE, ExternalTransferStatus.BUYBACK));
+        int size = transferDataList.size();
+
+        if (size > 2) {
+            throw new IllegalStateException(
+                    String.format("Found too many owner transfers(%s) by the settlement date(%s)", size, settlementDate));
+        } else if (size == 2) {
+            ExternalTransferStatus firstTransferStatus = transferDataList.get(0).getStatus();
+            ExternalTransferStatus secondTransferStatus = transferDataList.get(0).getStatus();
+
+            if (!ExternalTransferStatus.BUYBACK.equals(secondTransferStatus)) {
+                throw new IllegalStateException(String.format("Illegal transfer found. Expected %s, found: %s",
+                        ExternalTransferStatus.BUYBACK, secondTransferStatus));
+            }
+
+            switch (firstTransferStatus) {
+                case PENDING -> handleSameDaySaleAndBuyback(settlementDate, transferDataList);
+                case ACTIVE -> handleBuyback(loan, settlementDate, transferDataList);
+                default -> throw new IllegalStateException(String.format("Illegal transfer found. Expected %s or %s, found: %s",
+                        ExternalTransferStatus.PENDING, ExternalTransferStatus.ACTIVE, firstTransferStatus));
+            }
+        } else if (size == 1) {
+            handleSale(loan, settlementDate, transferDataList.get(0));
+        }
+
+        log.debug("end processing loan ownership transfer business step for loan Id [{}]", loan.getId());
+        return loan;
+    }
+
+    private void handleSale(final Loan loan, final LocalDate settlementDate, final ExternalAssetOwnerTransfer externalAssetOwnerTransfer) {
+        ExternalAssetOwnerTransfer newExternalAssetOwnerTransfer = sellAsset(loan, settlementDate, externalAssetOwnerTransfer);
+        // TODO: trigger asset loan transfer executed event
+    }
+
+    private void handleBuyback(final Loan loan, final LocalDate settlementDate,
+            final List<ExternalAssetOwnerTransfer> externalAssetOwnerTransferList) {
+        ExternalAssetOwnerTransfer newExternalAssetOwnerTransfer = buybackAsset(loan, settlementDate, externalAssetOwnerTransferList);
+        // TODO: trigger asset loan transfer executed event
+    }
+
+    private ExternalAssetOwnerTransfer buybackAsset(final Loan loan, final LocalDate settlementDate,
+            List<ExternalAssetOwnerTransfer> externalAssetOwnerTransferList) {
+        ExternalAssetOwnerTransfer saleExternalAssetOwnerTransfer = externalAssetOwnerTransferList.get(0);
+        ExternalAssetOwnerTransfer buybackExternalAssetOwnerTransfer = externalAssetOwnerTransferList.get(1);
+        saleExternalAssetOwnerTransfer.setEffectiveDateTo(settlementDate);
+        externalAssetOwnersWriteService.save(saleExternalAssetOwnerTransfer);
+        externalAssetOwnersWriteService.removeActiveMapping(loan.getId(), buybackExternalAssetOwnerTransfer);
+        // TODO: create asset ownership accounting entries
+        // TODO: create asset ownership transaction entries
+        return buybackExternalAssetOwnerTransfer;
+    }
+
+    private ExternalAssetOwnerTransfer sellAsset(final Loan loan, final LocalDate settlementDate,
+            final ExternalAssetOwnerTransfer externalAssetOwnerTransfer) {
+        ExternalAssetOwnerTransfer newExternalAssetOwnerTransfer;
+        if (isTransferable(loan)) {
+            newExternalAssetOwnerTransfer = createActiveEntry(settlementDate, externalAssetOwnerTransfer);
+            externalAssetOwnersWriteService.createActiveMapping(loan.getId(), newExternalAssetOwnerTransfer);
+            // TODO: create asset ownership accounting entries
+            // TODO: create asset ownership transaction entries
+        } else {
+            ExternalTransferSubStatus subStatus = ExternalTransferSubStatus.BALANCE_ZERO;
+            if (loan.getTotalOverpaid().compareTo(BigDecimal.ZERO) > 0) {
+                subStatus = ExternalTransferSubStatus.BALANCE_NEGATIVE;
+            }
+            newExternalAssetOwnerTransfer = createNewEntry(settlementDate, externalAssetOwnerTransfer, ExternalTransferStatus.DECLINED,
+                    subStatus, settlementDate, settlementDate);
+        }
+        return newExternalAssetOwnerTransfer;
+    }
+
+    private boolean isTransferable(final Loan loan) {
+        return loan.getLoanSummary().getTotalOutstanding().compareTo(BigDecimal.ZERO) > 0;

Review Comment:
   I think we should also check if the loan is in active status.
   Or is the positive totalOutstanding implicitly means that the loan is active?



##########
fineract-investor/src/main/java/org/apache/fineract/investor/cob/loan/LoanAccountOwnerTransferBusinessStep.java:
##########
@@ -0,0 +1,180 @@
+/**
+ * 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.investor.cob.loan;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.List;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.cob.loan.LoanCOBBusinessStep;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.investor.data.ExternalTransferStatus;
+import org.apache.fineract.investor.data.ExternalTransferSubStatus;
+import org.apache.fineract.investor.domain.ExternalAssetOwnerTransfer;
+import org.apache.fineract.investor.service.ExternalAssetOwnersReadService;
+import org.apache.fineract.investor.service.ExternalAssetOwnersWriteService;
+import org.apache.fineract.portfolio.loanaccount.domain.Loan;
+import org.springframework.stereotype.Component;
+
+@Component
+@RequiredArgsConstructor
+@Slf4j
+public class LoanAccountOwnerTransferBusinessStep implements LoanCOBBusinessStep {
+
+    private final ExternalAssetOwnersReadService externalAssetOwnersReadService;
+    private final ExternalAssetOwnersWriteService externalAssetOwnersWriteService;
+
+    @Override
+    public Loan execute(Loan loan) {
+        Long loanId = loan.getId();
+        log.debug("start processing loan ownership transfer business step for loan with Id [{}]", loanId);
+
+        LocalDate settlementDate = DateUtils.getBusinessLocalDate();
+        List<ExternalAssetOwnerTransfer> transferDataList = externalAssetOwnersReadService.retrieveTransfers(loanId, settlementDate,
+                List.of(ExternalTransferStatus.PENDING, ExternalTransferStatus.ACTIVE, ExternalTransferStatus.BUYBACK));
+        int size = transferDataList.size();
+
+        if (size > 2) {
+            throw new IllegalStateException(
+                    String.format("Found too many owner transfers(%s) by the settlement date(%s)", size, settlementDate));
+        } else if (size == 2) {
+            ExternalTransferStatus firstTransferStatus = transferDataList.get(0).getStatus();
+            ExternalTransferStatus secondTransferStatus = transferDataList.get(0).getStatus();

Review Comment:
   this is also the first element of the list



##########
fineract-investor/src/main/java/org/apache/fineract/investor/cob/loan/LoanAccountOwnerTransferBusinessStep.java:
##########
@@ -0,0 +1,180 @@
+/**
+ * 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.investor.cob.loan;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.List;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.cob.loan.LoanCOBBusinessStep;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.investor.data.ExternalTransferStatus;
+import org.apache.fineract.investor.data.ExternalTransferSubStatus;
+import org.apache.fineract.investor.domain.ExternalAssetOwnerTransfer;
+import org.apache.fineract.investor.service.ExternalAssetOwnersReadService;
+import org.apache.fineract.investor.service.ExternalAssetOwnersWriteService;
+import org.apache.fineract.portfolio.loanaccount.domain.Loan;
+import org.springframework.stereotype.Component;
+
+@Component
+@RequiredArgsConstructor
+@Slf4j
+public class LoanAccountOwnerTransferBusinessStep implements LoanCOBBusinessStep {
+
+    private final ExternalAssetOwnersReadService externalAssetOwnersReadService;
+    private final ExternalAssetOwnersWriteService externalAssetOwnersWriteService;
+
+    @Override
+    public Loan execute(Loan loan) {
+        Long loanId = loan.getId();
+        log.debug("start processing loan ownership transfer business step for loan with Id [{}]", loanId);
+
+        LocalDate settlementDate = DateUtils.getBusinessLocalDate();
+        List<ExternalAssetOwnerTransfer> transferDataList = externalAssetOwnersReadService.retrieveTransfers(loanId, settlementDate,
+                List.of(ExternalTransferStatus.PENDING, ExternalTransferStatus.ACTIVE, ExternalTransferStatus.BUYBACK));
+        int size = transferDataList.size();
+
+        if (size > 2) {
+            throw new IllegalStateException(
+                    String.format("Found too many owner transfers(%s) by the settlement date(%s)", size, settlementDate));
+        } else if (size == 2) {
+            ExternalTransferStatus firstTransferStatus = transferDataList.get(0).getStatus();
+            ExternalTransferStatus secondTransferStatus = transferDataList.get(0).getStatus();
+
+            if (!ExternalTransferStatus.BUYBACK.equals(secondTransferStatus)) {
+                throw new IllegalStateException(String.format("Illegal transfer found. Expected %s, found: %s",
+                        ExternalTransferStatus.BUYBACK, secondTransferStatus));
+            }
+
+            switch (firstTransferStatus) {
+                case PENDING -> handleSameDaySaleAndBuyback(settlementDate, transferDataList);
+                case ACTIVE -> handleBuyback(loan, settlementDate, transferDataList);
+                default -> throw new IllegalStateException(String.format("Illegal transfer found. Expected %s or %s, found: %s",
+                        ExternalTransferStatus.PENDING, ExternalTransferStatus.ACTIVE, firstTransferStatus));
+            }
+        } else if (size == 1) {
+            handleSale(loan, settlementDate, transferDataList.get(0));
+        }
+
+        log.debug("end processing loan ownership transfer business step for loan Id [{}]", loan.getId());
+        return loan;
+    }
+
+    private void handleSale(final Loan loan, final LocalDate settlementDate, final ExternalAssetOwnerTransfer externalAssetOwnerTransfer) {
+        ExternalAssetOwnerTransfer newExternalAssetOwnerTransfer = sellAsset(loan, settlementDate, externalAssetOwnerTransfer);
+        // TODO: trigger asset loan transfer executed event
+    }
+
+    private void handleBuyback(final Loan loan, final LocalDate settlementDate,
+            final List<ExternalAssetOwnerTransfer> externalAssetOwnerTransferList) {
+        ExternalAssetOwnerTransfer newExternalAssetOwnerTransfer = buybackAsset(loan, settlementDate, externalAssetOwnerTransferList);
+        // TODO: trigger asset loan transfer executed event
+    }
+
+    private ExternalAssetOwnerTransfer buybackAsset(final Loan loan, final LocalDate settlementDate,
+            List<ExternalAssetOwnerTransfer> externalAssetOwnerTransferList) {
+        ExternalAssetOwnerTransfer saleExternalAssetOwnerTransfer = externalAssetOwnerTransferList.get(0);
+        ExternalAssetOwnerTransfer buybackExternalAssetOwnerTransfer = externalAssetOwnerTransferList.get(1);
+        saleExternalAssetOwnerTransfer.setEffectiveDateTo(settlementDate);
+        externalAssetOwnersWriteService.save(saleExternalAssetOwnerTransfer);
+        externalAssetOwnersWriteService.removeActiveMapping(loan.getId(), buybackExternalAssetOwnerTransfer);
+        // TODO: create asset ownership accounting entries
+        // TODO: create asset ownership transaction entries
+        return buybackExternalAssetOwnerTransfer;
+    }
+
+    private ExternalAssetOwnerTransfer sellAsset(final Loan loan, final LocalDate settlementDate,
+            final ExternalAssetOwnerTransfer externalAssetOwnerTransfer) {
+        ExternalAssetOwnerTransfer newExternalAssetOwnerTransfer;
+        if (isTransferable(loan)) {
+            newExternalAssetOwnerTransfer = createActiveEntry(settlementDate, externalAssetOwnerTransfer);
+            externalAssetOwnersWriteService.createActiveMapping(loan.getId(), newExternalAssetOwnerTransfer);
+            // TODO: create asset ownership accounting entries
+            // TODO: create asset ownership transaction entries
+        } else {
+            ExternalTransferSubStatus subStatus = ExternalTransferSubStatus.BALANCE_ZERO;
+            if (loan.getTotalOverpaid().compareTo(BigDecimal.ZERO) > 0) {
+                subStatus = ExternalTransferSubStatus.BALANCE_NEGATIVE;
+            }
+            newExternalAssetOwnerTransfer = createNewEntry(settlementDate, externalAssetOwnerTransfer, ExternalTransferStatus.DECLINED,
+                    subStatus, settlementDate, settlementDate);
+        }
+        return newExternalAssetOwnerTransfer;
+    }
+
+    private boolean isTransferable(final Loan loan) {
+        return loan.getLoanSummary().getTotalOutstanding().compareTo(BigDecimal.ZERO) > 0;
+    }
+
+    private void handleSameDaySaleAndBuyback(final LocalDate settlementDate, final List<ExternalAssetOwnerTransfer> transferDataList) {
+        if (ExternalTransferStatus.CANCELLED.equals(transferDataList.get(0).getStatus())
+                && ExternalTransferStatus.BUYBACK.equals(transferDataList.get(1).getStatus())) {
+            ExternalAssetOwnerTransfer cancelledPendingTransfer = cancelTransfer(settlementDate, transferDataList.get(0));
+            ExternalAssetOwnerTransfer cancelledBuybackTransfer = cancelTransfer(settlementDate, transferDataList.get(1));
+            // TODO: trigger asset loan transfer cancel events
+        } else {
+            throw new IllegalStateException(String.format("Invalid combination of transfers: %s, %s", transferDataList.get(0).getStatus(),
+                    transferDataList.get(1).getStatus()));
+        }
+    }
+
+    private ExternalAssetOwnerTransfer cancelTransfer(final LocalDate settlementDate,
+            final ExternalAssetOwnerTransfer externalAssetOwnerTransfer) {
+        return createNewEntry(settlementDate, externalAssetOwnerTransfer, ExternalTransferStatus.CANCELLED,
+                ExternalTransferSubStatus.SAMEDAY_TRANSFERS, settlementDate, settlementDate);
+    }
+
+    private ExternalAssetOwnerTransfer createNewEntry(final LocalDate settlementDate,
+            final ExternalAssetOwnerTransfer externalAssetOwnerTransfer, final ExternalTransferStatus status,
+            final ExternalTransferSubStatus subStatus, final LocalDate effectiveDateFrom, final LocalDate effectiveDateTo) {
+        ExternalAssetOwnerTransfer newExternalAssetOwnerTransfer = new ExternalAssetOwnerTransfer();
+        newExternalAssetOwnerTransfer.setOwner(externalAssetOwnerTransfer.getOwner());
+        newExternalAssetOwnerTransfer.setExternalId(externalAssetOwnerTransfer.getExternalId());
+        newExternalAssetOwnerTransfer.setStatus(status);
+        newExternalAssetOwnerTransfer.setSubStatus(subStatus);
+        newExternalAssetOwnerTransfer.setSettlementDate(settlementDate);
+        newExternalAssetOwnerTransfer.setLoanId(externalAssetOwnerTransfer.getLoanId());
+        newExternalAssetOwnerTransfer.setExternalLoanId(externalAssetOwnerTransfer.getExternalLoanId());
+        newExternalAssetOwnerTransfer.setPurchasePriceRatio(externalAssetOwnerTransfer.getPurchasePriceRatio());
+        newExternalAssetOwnerTransfer.setEffectiveDateFrom(effectiveDateFrom);
+        newExternalAssetOwnerTransfer.setEffectiveDateTo(effectiveDateTo);
+
+        externalAssetOwnerTransfer.setEffectiveDateTo(settlementDate);
+        externalAssetOwnersWriteService.save(externalAssetOwnerTransfer);
+        return externalAssetOwnersWriteService.save(newExternalAssetOwnerTransfer);
+    }
+
+    private ExternalAssetOwnerTransfer createActiveEntry(final LocalDate settlementDate,
+            final ExternalAssetOwnerTransfer externalAssetOwnerTransfer) {
+        LocalDate effectiveFrom = settlementDate.plusDays(1);
+        LocalDate openEnded = LocalDate.of(9999, 12, 31);

Review Comment:
   We use this multiple times, it would be a good idea to extract it to a global constant



##########
fineract-investor/src/main/java/org/apache/fineract/investor/cob/loan/LoanAccountOwnerTransferBusinessStep.java:
##########
@@ -0,0 +1,180 @@
+/**
+ * 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.investor.cob.loan;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.List;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.cob.loan.LoanCOBBusinessStep;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.investor.data.ExternalTransferStatus;
+import org.apache.fineract.investor.data.ExternalTransferSubStatus;
+import org.apache.fineract.investor.domain.ExternalAssetOwnerTransfer;
+import org.apache.fineract.investor.service.ExternalAssetOwnersReadService;
+import org.apache.fineract.investor.service.ExternalAssetOwnersWriteService;
+import org.apache.fineract.portfolio.loanaccount.domain.Loan;
+import org.springframework.stereotype.Component;
+
+@Component
+@RequiredArgsConstructor
+@Slf4j
+public class LoanAccountOwnerTransferBusinessStep implements LoanCOBBusinessStep {
+
+    private final ExternalAssetOwnersReadService externalAssetOwnersReadService;
+    private final ExternalAssetOwnersWriteService externalAssetOwnersWriteService;
+
+    @Override
+    public Loan execute(Loan loan) {

Review Comment:
   could you write some unit tests for this, to cover the use-cases?



-- 
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: commits-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [fineract] adamsaghy commented on a diff in pull request #3207: FINERACT-1926: Asset transfer to external owner COB business step

Posted by "adamsaghy (via GitHub)" <gi...@apache.org>.
adamsaghy commented on code in PR #3207:
URL: https://github.com/apache/fineract/pull/3207#discussion_r1210121147


##########
fineract-investor/src/main/java/org/apache/fineract/investor/cob/loan/LoanAccountOwnerTransferBusinessStep.java:
##########
@@ -0,0 +1,180 @@
+/**
+ * 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.investor.cob.loan;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.List;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.cob.loan.LoanCOBBusinessStep;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.investor.data.ExternalTransferStatus;
+import org.apache.fineract.investor.data.ExternalTransferSubStatus;
+import org.apache.fineract.investor.domain.ExternalAssetOwnerTransfer;
+import org.apache.fineract.investor.service.ExternalAssetOwnersReadService;
+import org.apache.fineract.investor.service.ExternalAssetOwnersWriteService;
+import org.apache.fineract.portfolio.loanaccount.domain.Loan;
+import org.springframework.stereotype.Component;
+
+@Component
+@RequiredArgsConstructor
+@Slf4j
+public class LoanAccountOwnerTransferBusinessStep implements LoanCOBBusinessStep {
+
+    private final ExternalAssetOwnersReadService externalAssetOwnersReadService;
+    private final ExternalAssetOwnersWriteService externalAssetOwnersWriteService;
+
+    @Override
+    public Loan execute(Loan loan) {
+        Long loanId = loan.getId();
+        log.debug("start processing loan ownership transfer business step for loan with Id [{}]", loanId);
+
+        LocalDate settlementDate = DateUtils.getBusinessLocalDate();
+        List<ExternalAssetOwnerTransfer> transferDataList = externalAssetOwnersReadService.retrieveTransfers(loanId, settlementDate,
+                List.of(ExternalTransferStatus.PENDING, ExternalTransferStatus.ACTIVE, ExternalTransferStatus.BUYBACK));
+        int size = transferDataList.size();
+
+        if (size > 2) {
+            throw new IllegalStateException(
+                    String.format("Found too many owner transfers(%s) by the settlement date(%s)", size, settlementDate));
+        } else if (size == 2) {
+            ExternalTransferStatus firstTransferStatus = transferDataList.get(0).getStatus();
+            ExternalTransferStatus secondTransferStatus = transferDataList.get(0).getStatus();
+
+            if (!ExternalTransferStatus.BUYBACK.equals(secondTransferStatus)) {
+                throw new IllegalStateException(String.format("Illegal transfer found. Expected %s, found: %s",
+                        ExternalTransferStatus.BUYBACK, secondTransferStatus));
+            }
+
+            switch (firstTransferStatus) {
+                case PENDING -> handleSameDaySaleAndBuyback(settlementDate, transferDataList);
+                case ACTIVE -> handleBuyback(loan, settlementDate, transferDataList);
+                default -> throw new IllegalStateException(String.format("Illegal transfer found. Expected %s or %s, found: %s",
+                        ExternalTransferStatus.PENDING, ExternalTransferStatus.ACTIVE, firstTransferStatus));
+            }
+        } else if (size == 1) {
+            handleSale(loan, settlementDate, transferDataList.get(0));
+        }
+
+        log.debug("end processing loan ownership transfer business step for loan Id [{}]", loan.getId());
+        return loan;
+    }
+
+    private void handleSale(final Loan loan, final LocalDate settlementDate, final ExternalAssetOwnerTransfer externalAssetOwnerTransfer) {
+        ExternalAssetOwnerTransfer newExternalAssetOwnerTransfer = sellAsset(loan, settlementDate, externalAssetOwnerTransfer);
+        // TODO: trigger asset loan transfer executed event
+    }
+
+    private void handleBuyback(final Loan loan, final LocalDate settlementDate,
+            final List<ExternalAssetOwnerTransfer> externalAssetOwnerTransferList) {
+        ExternalAssetOwnerTransfer newExternalAssetOwnerTransfer = buybackAsset(loan, settlementDate, externalAssetOwnerTransferList);
+        // TODO: trigger asset loan transfer executed event
+    }
+
+    private ExternalAssetOwnerTransfer buybackAsset(final Loan loan, final LocalDate settlementDate,
+            List<ExternalAssetOwnerTransfer> externalAssetOwnerTransferList) {
+        ExternalAssetOwnerTransfer saleExternalAssetOwnerTransfer = externalAssetOwnerTransferList.get(0);
+        ExternalAssetOwnerTransfer buybackExternalAssetOwnerTransfer = externalAssetOwnerTransferList.get(1);
+        saleExternalAssetOwnerTransfer.setEffectiveDateTo(settlementDate);
+        externalAssetOwnersWriteService.save(saleExternalAssetOwnerTransfer);
+        externalAssetOwnersWriteService.removeActiveMapping(loan.getId(), buybackExternalAssetOwnerTransfer);
+        // TODO: create asset ownership accounting entries
+        // TODO: create asset ownership transaction entries
+        return buybackExternalAssetOwnerTransfer;
+    }
+
+    private ExternalAssetOwnerTransfer sellAsset(final Loan loan, final LocalDate settlementDate,
+            final ExternalAssetOwnerTransfer externalAssetOwnerTransfer) {
+        ExternalAssetOwnerTransfer newExternalAssetOwnerTransfer;
+        if (isTransferable(loan)) {
+            newExternalAssetOwnerTransfer = createActiveEntry(settlementDate, externalAssetOwnerTransfer);
+            externalAssetOwnersWriteService.createActiveMapping(loan.getId(), newExternalAssetOwnerTransfer);
+            // TODO: create asset ownership accounting entries
+            // TODO: create asset ownership transaction entries
+        } else {
+            ExternalTransferSubStatus subStatus = ExternalTransferSubStatus.BALANCE_ZERO;
+            if (loan.getTotalOverpaid().compareTo(BigDecimal.ZERO) > 0) {
+                subStatus = ExternalTransferSubStatus.BALANCE_NEGATIVE;
+            }
+            newExternalAssetOwnerTransfer = createNewEntry(settlementDate, externalAssetOwnerTransfer, ExternalTransferStatus.DECLINED,
+                    subStatus, settlementDate, settlementDate);
+        }
+        return newExternalAssetOwnerTransfer;
+    }
+
+    private boolean isTransferable(final Loan loan) {
+        return loan.getLoanSummary().getTotalOutstanding().compareTo(BigDecimal.ZERO) > 0;

Review Comment:
   it means that, yeah...



-- 
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: commits-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org