public IHttpActionResult PayInstallment(LoanInstallmentDto installmentDto) { var result = _loanService.PayInstallment(installmentDto); if (result.Success) { return(Ok(result)); } else { return(BadRequest(result.Message)); } }
public ResultDto PayInstallment(LoanInstallmentDto installmentDto) { var result = new ResultDto(); var loan = _loanRepository.GetSingle(installmentDto.LoanId, t => t.BankAccount); if (!loan.Repayment && loan.InstallmentsLeft > 0) { decimal penalty = 0m; if (loan.NextInstallmentDate <= DateTime.Today) { penalty = loan.InstallmentAmount * 1.10m - loan.InstallmentAmount; loan.LoanAmountLeft += penalty; } var installment = new LoanInstallment() { InstallmentAmount = loan.InstallmentAmount + penalty, LoanId = loan.Id, PaidOn = DateTime.Now }; var isValid = _bankAccountService.TakeCash(installment.InstallmentAmount, loan.BankAccountId); if (isValid) { loan.InstallmentsLeft--; loan.LoanAmountLeft -= installment.InstallmentAmount; loan.NextInstallmentDate = loan.NextInstallmentDate.Value.AddDays(1); if (loan.InstallmentsLeft == 0 && loan.LoanAmountLeft == 0) { loan.Repayment = true; loan.RepaymentDate = DateTime.Now; } _loanRepository.Update(loan); _loanInstallmentRepository.Create(installment); result.Success = true; result.Message = "Payment has been successful!"; } else { result.Message = "There was a problem with paying installment"; } } else { result.Message = "You cannot pay the installment for this loan. Check date of your next installment or contact support."; } return(result); }