Esempio n. 1
0
 public async Task <ApprovalRegRespObj> CustomerTransaction([FromBody] CustomerTransactionObj entity)
 {
     try
     {
         return(_repo.CustomerTransaction(entity));
     }
     catch (Exception ex)
     {
         var errorCode = ErrorID.Generate(5);
         _logger.Error($"ErrorID : {errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}");
         return(new ApprovalRegRespObj
         {
             Status = new APIResponseStatus {
                 IsSuccessful = false, Message = new APIResponseMessage {
                     FriendlyMessage = "Error Occurred", TechnicalMessage = ex?.Message, MessageId = errorCode
                 }
             }
         }); throw;
     }
 }
        public async Task <ActionResult <LoanApplicationRespObj> > SubmitLoanForCreditAppraisal([FromQuery] LoanApplicationSearchObj model)
        {
            try
            {
                var response = await _repo.SubmitLoanForCreditAppraisal(model.LoanApplicationId);

                if (response.Status.IsSuccessful)
                {
                    var companyList = await _serverRequest.GetAllCompanyAsync();

                    var loanObj     = _context.credit_loanapplication.Find(model.LoanApplicationId);
                    var customerObj = _context.credit_loancustomer.Find(loanObj.CustomerId);

                    var productFeeList = _context.credit_productfee.Where(x => x.ProductId == loanObj.ApprovedProductId && x.Deleted == false).ToList();
                    foreach (var item in productFeeList)
                    {
                        decimal productamount = 0;
                        var     fee           = _context.credit_fee.FirstOrDefault(x => x.FeeId == item.FeeId && x.IsIntegral == false && x.Deleted == false);
                        if (item.ProductFeeType == 2)//Percentage
                        {
                            productamount = (loanObj.ApprovedAmount * Convert.ToDecimal(item.ProductAmount)) / 100;
                        }
                        else///Fixed
                        {
                            productamount = Convert.ToDecimal(item.ProductAmount);
                        }
                        if (fee != null)
                        {
                            CustomerTransactionObj customerTrans = new CustomerTransactionObj
                            {
                                CasaAccountNumber = customerObj.CASAAccountNumber,
                                Description       = "Payment of Non Integral Fee",
                                TransactionDate   = DateTime.Now,
                                ValueDate         = DateTime.Now,
                                TransactionType   = "Debit",
                                CreditAmount      = 0,
                                DebitAmount       = productamount,
                                Beneficiary       = companyList.companyStructures.FirstOrDefault(x => x.companyStructureId == loanObj.CompanyId)?.name,
                                ReferenceNo       = loanObj.ApplicationRefNumber,
                            };
                            _customerTrans.CustomerTransaction(customerTrans);
                        }
                    }
                }
                return(new LoanApplicationRespObj
                {
                    ResponseId = response.ResponseId,
                    Status = response.Status
                });
            }
            catch (Exception ex)
            {
                var errorCode = ErrorID.Generate(5);
                _logger.Error($"ErrorID : {errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}");
                return(new LoanApplicationRespObj
                {
                    Status = new APIResponseStatus {
                        IsSuccessful = false, Message = new APIResponseMessage {
                            FriendlyMessage = "Error Occurred", TechnicalMessage = ex?.Message, MessageId = errorCode
                        }
                    }
                });
            }
        }
Esempio n. 3
0
        public async Task <ActionResult <ApprovalRegRespObj> > GoForApproval([FromBody] ApprovalObj entity)
        {
            using (var _trans = _dataContext.Database.BeginTransaction())
            {
                try
                {
                    var currentUserId = _httpContextAccessor.HttpContext.User?.FindFirst(x => x.Type == "userId").Value;
                    var user          = await _serverRequest.UserDataAsync();

                    var loan = _dataContext.credit_loan.Find(entity.TargetId);

                    var req = new IndentityServerApprovalCommand
                    {
                        ApprovalComment = entity.Comment,
                        ApprovalStatus  = entity.ApprovalStatusId,
                        TargetId        = entity.TargetId,
                        WorkflowToken   = loan.WorkflowToken,
                        ReferredStaffId = entity.ReferredStaffId
                    };

                    var previousDetails = _dataContext.cor_approvaldetail.Where(x => x.WorkflowToken.Contains(loan.WorkflowToken) && x.TargetId == entity.TargetId).ToList();
                    var lastDate        = loan.CreatedOn;
                    if (previousDetails.Count() > 0)
                    {
                        lastDate = previousDetails.OrderByDescending(x => x.ApprovalDetailId).FirstOrDefault().Date;
                    }
                    if (previousDetails.Count() > 0)
                    {
                        lastDate = previousDetails.OrderByDescending(x => x.ApprovalDetailId).FirstOrDefault().Date;
                    }
                    var details = new cor_approvaldetail
                    {
                        Comment       = entity.Comment,
                        Date          = DateTime.Now,
                        ArrivalDate   = previousDetails.Count() > 0 ? lastDate : loan.CreatedOn,
                        StatusId      = entity.ApprovalStatusId,
                        TargetId      = entity.TargetId,
                        StaffId       = user.StaffId,
                        WorkflowToken = loan.WorkflowToken
                    };

                    var result = await _serverRequest.StaffApprovalRequestAsync(req);

                    if (!result.IsSuccessStatusCode)
                    {
                        return(new ApprovalRegRespObj
                        {
                            Status = new APIResponseStatus
                            {
                                Message = new APIResponseMessage {
                                    FriendlyMessage = result.ReasonPhrase
                                }
                            }
                        });
                    }

                    var stringData = await result.Content.ReadAsStringAsync();

                    var response = JsonConvert.DeserializeObject <ApprovalRegRespObj>(stringData);

                    if (!response.Status.IsSuccessful)
                    {
                        return(new ApprovalRegRespObj
                        {
                            Status = response.Status
                        });
                    }

                    if (response.ResponseId == (int)ApprovalStatus.Processing)
                    {
                        loan.ApprovalStatusId = (int)ApprovalStatus.Processing;
                        await _dataContext.cor_approvaldetail.AddAsync(details);

                        await _dataContext.SaveChangesAsync();

                        _trans.Commit();
                        return(new ApprovalRegRespObj
                        {
                            ResponseId = (int)ApprovalStatus.Processing,
                            Status = new APIResponseStatus {
                                IsSuccessful = true, Message = response.Status.Message
                            }
                        });
                    }


                    if (response.ResponseId == (int)ApprovalStatus.Revert)
                    {
                        loan.ApprovalStatusId = (int)ApprovalStatus.Revert;
                        await _dataContext.cor_approvaldetail.AddAsync(details);

                        await _dataContext.SaveChangesAsync();

                        _trans.Commit();
                        return(new ApprovalRegRespObj
                        {
                            ResponseId = (int)ApprovalStatus.Revert,
                            Status = new APIResponseStatus {
                                IsSuccessful = true, Message = response.Status.Message
                            }
                        });
                    }

                    if (response.ResponseId == (int)ApprovalStatus.Approved)
                    {
                        var applicationResponse = _repo.DisburseLoan(entity.TargetId, user.StaffId, user.UserName, entity.Comment);
                        var customer            = _dataContext.credit_loancustomer.Find(loan.CustomerId);
                        await _serverRequest.SendMail(new MailObj
                        {
                            fromAddresses = new List <FromAddress> {
                            },
                            toAddresses   = new List <ToAddress>
                            {
                                new ToAddress {
                                    address = customer.Email, name = customer.FirstName
                                }
                            },
                            subject = "Loan Successfully Approved",
                            content = $"Hi {customer.FirstName}, <br> Your loan application has been finally approved. <br/> Loan Amount : {loan.PrincipalAmount}",
                            sendIt  = true,
                        });

                        //Update CustomerTransaction
                        if (applicationResponse.DisbursementEntry != null && applicationResponse.IntegralFeeEntry != null)
                        {
                            _customerTrans.CustomerTransaction(applicationResponse.DisbursementEntry);
                            _customerTrans.CustomerTransaction(applicationResponse.IntegralFeeEntry);
                        }

                        //Generate Schedule
                        if (applicationResponse.loanPayment != null && applicationResponse.AnyIdentifier > 0)
                        {
                            await _schedule.AddLoanSchedule(applicationResponse.AnyIdentifier, applicationResponse.loanPayment);
                        }

                        //Update IFRS
                        _ifrs.UpdateScoreCardHistoryByLoanDisbursement(entity.TargetId, user.UserId);

                        //Pay with Flutterwave
                        if (applicationResponse.FlutterObj != null)
                        {
                            var flutter = _serverRequest.GetFlutterWaveKeys().Result;
                            if (flutter.keys.useFlutterWave)
                            {
                                var res = _flutter.createBulkTransfer(applicationResponse.FlutterObj).Result;
                                loan.ApprovalStatusId = (int)ApprovalStatus.Approved;
                                await _dataContext.cor_approvaldetail.AddAsync(details);

                                await _dataContext.SaveChangesAsync();

                                _trans.Commit();
                                if (res.status.ToLower().Trim() != "success")
                                {
                                    return(new ApprovalRegRespObj
                                    {
                                        ResponseId = (int)ApprovalStatus.Revert,
                                        Status = new APIResponseStatus {
                                            IsSuccessful = true, Message = new APIResponseMessage {
                                                FriendlyMessage = "Loan disbursed successfully but transfer creation failed"
                                            }
                                        }
                                    });
                                }
                                else if (res.status.ToLower().Trim() == "success")
                                {
                                    return(new ApprovalRegRespObj
                                    {
                                        ResponseId = (int)ApprovalStatus.Revert,
                                        Status = new APIResponseStatus {
                                            IsSuccessful = true, Message = new APIResponseMessage {
                                                FriendlyMessage = "Loan disbursed successfully and transfer creation successful"
                                            }
                                        }
                                    });
                                }
                            }
                        }
                        loan.ApprovalStatusId = (int)ApprovalStatus.Approved;
                        await _dataContext.cor_approvaldetail.AddAsync(details);

                        await _dataContext.SaveChangesAsync();

                        _trans.Commit();
                        return(new ApprovalRegRespObj
                        {
                            ResponseId = (int)ApprovalStatus.Approved,
                            Status = new APIResponseStatus {
                                IsSuccessful = true, Message = response.Status.Message
                            }
                        });
                    }

                    if (response.ResponseId == (int)ApprovalStatus.Disapproved)
                    {
                        loan.ApprovalStatusId = (int)ApprovalStatus.Disapproved;
                        await _dataContext.cor_approvaldetail.AddAsync(details);

                        await _dataContext.SaveChangesAsync();

                        _trans.Commit();
                        return(new ApprovalRegRespObj
                        {
                            ResponseId = (int)ApprovalStatus.Disapproved,
                            Status = new APIResponseStatus {
                                IsSuccessful = true, Message = response.Status.Message
                            }
                        });
                    }

                    return(new ApprovalRegRespObj
                    {
                        Status = response.Status
                    });
                }
                catch (Exception ex)
                {
                    _trans.Rollback();
                    var errorCode = ErrorID.Generate(5);
                    _logger.Error($"ErrorID : {errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}");
                    return(new ApprovalRegRespObj
                    {
                        Status = new APIResponseStatus {
                            IsSuccessful = false, Message = new APIResponseMessage {
                                FriendlyMessage = "Error Occurred", TechnicalMessage = ex?.Message, MessageId = errorCode
                            }
                        }
                    });
                }
                finally { _trans.Dispose(); }
            }
        }