'ASP.NET Core - How to Add condition to Loan Repayment Schedule
I am currently working on Loan Repayment Schedule using ASP.NET Core-5.
There are two (2) models for the Loan Repayment Schedule. One Loan has Many LoanDetail (RepaymentSchedule):
Model:
public class Loan
{
public long Id { get; set; }
public long CustomerId { get; set; }
public DateTime? DebitDate { get; set; } = null;
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public PaymentFrequency? PaymentFrequency { get; set; } //1=Monthly, 2=Quarterly
public int? PaymentCount { get; set; }
[ForeignKey("CustomerId")]
public virtual Customer Customer { get; set; }
public virtual ICollection<LoanDetail> LoanDetails { get; set; }
}
public class LoanDetail
{
public long? LoanId { get; set; }
public DateTime? DueDate { get; set; }
[Display(Name = "Payable Amount")]
public decimal? PayableAmount { get; set; }
[ForeignKey("LoanId")]
public virtual Loan Loan { get; set; }
}
Dto
public class LoanCreateDto
{
public long CustomerId { get; set; }
[Required(ErrorMessage = "Start Date is Required")]
public DateTime StartDate { get; set; }
[Required(ErrorMessage = "End Date is Required")]
[DateGreaterThanAttribute(otherPropertyName = "StartDate", ErrorMessage = "End Date must be greater than Start Date")]
[JsonProperty(PropertyName = "EndDate")]
public DateTime EndDate { get; set; }
public DateTime? DebitDate { get; set; }
[Range(1,2)]
[Required(ErrorMessage = "Payment Frequency is Required")]
public PaymentFrequency? PaymentFrequency { get; set; } //1=Monthly, 2=Quarterly
[Required(ErrorMessage = "Amount is Required")]
[Range(1000, 99999999999999999999999999999999.99, ErrorMessage = "Please enter a value greater than {1000}")]
[JsonProperty(PropertyName = "Amount")]
public decimal Amount { get; set; }
}
ConstantHelper is used to Calculate the PayableAmount and For PaymentFrequency, 1=Monthly, 2=Quarterly
public static class ConstantHelper
{
public static int GetTotalMonth(DateTime startDate, DateTime endDate)
{
int totalMonth = 12 * (startDate.Year - endDate.Year) + startDate.Month - endDate.Month;
return Convert.ToInt32(Math.Abs(totalMonth));
}
public static int GetTotalQuarter(DateTime startDate, DateTime endDate)
{
int firstQuarter = getQuarter(startDate);
int secondQuarter = getQuarter(endDate);
return 1 + Math.Abs(firstQuarter - secondQuarter);
}
private static int getQuarter(DateTime date)
{
return (date.Year * 4) + ((date.Month - 1) / 3);
}
}
Customers take loan and is scheduled based on PaymentFrequency.
service
public async Task<GenericResponseDto<LoanListDto>> CreateLoanAsync(LoanCreateDto requestDto)
{
var response = new GenericResponseDto<LoanListDto>();
try
{
var loan = _mapper.Map<Loan>(requestDto);
switch ((byte?)requestDto.PaymentFrequency)
{
case 1:
numberOfTimes = ConstantHelper.GetTotalMonth(loan.StartDate, loan.EndDate);
default:;
case 2:
numberOfTimes = ConstantHelper.GetTotalQuarter(loan.StartDate, loan.EndDate);
break;
}
_context.loans.Add(loan);
await _context.SaveChangesAsync();
DateTime dueDate = loan.StartDate;
decimal repaymentAmount = Convert.ToDecimal(loan.Amount / loan.PaymentCount);
for (int i = 1; i <= (int)loan.PaymentCount; i++)
{
switch(loan.PaymentFrequency)
{
case Infrastructure.Helpers.EnumList.PaymentFrequency.Monthly:
if(i != 1)
{
// Reset Date
dueDate = dueDate.AddMonths(1);
}
break;
case Infrastructure.Helpers.EnumList.PaymentFrequency.Quarterly:
if (i != 1)
{
// Reset Date
dueDate = dueDate.AddMonths(3);
}
break;
}
var loanDetail = new LoanDetail();
loanDetail.loanId = loan.Id;
loanDetail.PayableAmount = repaymentAmount;
loanDetail.DueDate = dueDate;
_context.loan_details.Add(loanDetail);
await _context.SaveChangesAsync();
}
response.StatusCode = 201;
response.Message = "Successfully Created Loan and Schedules";
response.Result = _mapper.Map<LoanListDto>(loan);
}
catch (Exception ex)
{
response.Error = new ErrorResponseDto()
{
ErrorCode = 500,
Message = ex.Message
};
}
return response;
}
DebitDate is only applicable when the PaymentFrequency is 1 (Monthly).
I want to add a condition here: dueDate = dueDate.AddMonths(1)
For the Monthly option, a customer is given the flexibility to select the date in the month when he wants to make his repayment usingDebitDate.
So in Loan Model, If DebitDate = null, the service should utilize dueDate.AddMonths(1) for the DueDate. But if DebitDate is not null, it should use the DebitDate Entered in Loan Model to calculate the Monthly DueDate in LoanDetail for the Loan Repayment Schedule.
How do I achieve this?
Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
