HRM-10/HRM.DA/Service/Fund/Loan/FmLoanScheduleService.cs
Mohaimen Hasan 838b8d4480 first commit
2026-11-03 13:06:00 +06:00

987 lines
39 KiB
C#

using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using HRM.BO;
using Microsoft.VisualBasic;
using static iTextSharp.text.pdf.AcroFields;
namespace HRM.DA
{
public class FMLoanScheduleService : ServiceTemplate , IFMLoanScheduleService
{
public FMLoanScheduleService()
{
}
private void MapObject(FMLoanSchedule loanSchedule, DataReader dr)
{
base.SetObjectID(loanSchedule, dr.GetInt32("LoanScheduleID").Value);
// loanSchedule.ActivityID = dr.GetInt32("ActivityID").Value;
loanSchedule.CustomerID = dr.GetInt32("CustomerID",0);
loanSchedule.EffectiveBalance = dr.GetDouble("EffectiveBalance").Value;
loanSchedule.InstallmentAmount = dr.GetDouble("InstallmentAmount").Value;
loanSchedule.InstallmentInterest = dr.GetDouble("InstallmentInterest").Value;
loanSchedule.InstallmentPrincipal = dr.GetDouble("InstallmentPrincipal").Value;
loanSchedule.LoanCategoryID = dr.GetInt32("LoanCategoryID").Value;
loanSchedule.LoanID = dr.GetInt32("LoanID").Value;
loanSchedule.PayDate = dr.GetDateTime("PayDate");
loanSchedule.ScheduledPayDate = dr.GetDateTime("ScheduledPayDate").Value;
loanSchedule.SerialNo = dr.GetInt32("SerialNo").Value;
loanSchedule.CreatedBy = dr.GetInt32("CreatedBy").Value;
loanSchedule.CreatedDate = dr.GetDateTime("CreatedDate").Value;
loanSchedule.ModifiedBy = dr.GetInt32("ModifiedBy");
loanSchedule.ModifiedDate = dr.GetDateTime("ModifiedDate");
loanSchedule.IsSettle = dr.GetInt32("isSettle") == null? 0 : dr.GetInt32("isSettle").Value;
base.SetObjectState(loanSchedule, Ease.Core.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader dr)
{
FMLoanSchedule loanSchedule = new FMLoanSchedule();
MapObject(loanSchedule, dr);
return loanSchedule as T;
}
protected FMLoanSchedule CreateObject(DataReader oReader)
{
FMLoanSchedule loanSchedule = new FMLoanSchedule();
MapObject(loanSchedule, oReader);
return loanSchedule;
}
#region Service Implementation
#region Insert
public void Save(FMLoanSchedule loanSchedule)
{
FMLoanSchedule oloanSchedule = new FMLoanSchedule();
oloanSchedule = loanSchedule;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oloanSchedule.IsNew)
{
int id = tc.GenerateID("FM_Loan", "LoanID");
base.SetObjectID(oloanSchedule, id);
FMLoanScheduleDA.Insert(tc, oloanSchedule);
}
else
{
FMLoanScheduleDA.Update(tc, oloanSchedule);
}
tc.End();
//return oLoanTransaction.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
#endregion
#region Delete
public void Delete(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
FMLoanScheduleDA.Delete(tc, id);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
#endregion
#region Get(By loanScheduleID)
public FMLoanSchedule Get(int loanScheduleID)
{
FMLoanSchedule loanSchedule = new FMLoanSchedule();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(FMLoanScheduleDA.Get(tc, loanScheduleID));
if (dr.Read())
{
loanSchedule = this.CreateObject<FMLoanSchedule>(dr);
}
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return loanSchedule;
}
#endregion
public List<FMLoanSchedule> GetByLoanID(int loanScheduleID)
{
List<FMLoanSchedule> loanSchedule = new List<FMLoanSchedule>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(FMLoanScheduleDA.GetByLoanID(tc, loanScheduleID));
loanSchedule = CreateObjects<FMLoanSchedule>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return loanSchedule;
}
#region Get()
//public List<FMLoanSchedule> Get()
//{
// List<FMLoanSchedule> loanSchedules = new List<FMLoanSchedule>();
// TransactionContext tc = null;
// try
// {
// tc = TransactionContext.Begin();
// DataReader dr = new DataReader(FMLoanScheduleDA.Get(tc));
// loanSchedules = this.CreateObjects<FMLoanSchedule>(dr);
// dr.Close();
// tc.End();
// }
// catch (Exception e)
// {
// #region Handle Exception
// if (tc != null)
// tc.HandleError();
// ExceptionLog.Write(e);
// throw new ServiceException(e.Message, e);
// #endregion
// }
// return loanSchedules;
//}
public List<FMLoanSchedule> Get(DateTime scheuleMonth, int fundtypeid)
{
List<FMLoanSchedule> loanSchedules = new List<FMLoanSchedule>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(FMLoanScheduleDA.Get(tc, scheuleMonth, fundtypeid));
loanSchedules = this.CreateObjects<FMLoanSchedule>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return loanSchedules;
}
#endregion
#region GetTable
public DataTable GetTable()
{
DataTable dTbl = new DataTable("LoanSchedules");
//TransactionContext tc = null;
//try
//{
// tc = TransactionContext.Begin();
// IDataReader ir = LoanScheduleDA.Get(tc);
// dTbl.Load(ir);
// ir.Close();
// tc.End();
//}
//catch (Exception e)
//{
// #region Handle Exception
// if (tc != null)
// tc.HandleError();
// ExceptionLog.Write(e);
// throw new ServiceException(e.Message, e);
// #endregion
//}
return dTbl;
}
#endregion
public bool GetSchedule(DateTime month)
{
bool exists = false;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
exists = FMLoanScheduleDA.GetSchedule(tc, month);
tc.End();
}
catch (Exception e)
{
throw e;
}
return exists;
}
//public List<FMLoanSchedule> CreateSchedule(int noOfInstallment, double interestRate, double loanAmount, DateTime payBackMonth, DateTime issueDate)
//{
// List<FMLoanSchedule> oFMLoanSchedule = new List<FMLoanSchedule>();
// try
// {
// FMLoanSchedule oSchedule = null;
// double nInterest = 0.0;
// double nTotalInterest = 0.0;
// double nInsPrinciple = 0.0;
// double nBalance = 0.0;
// double nTotalInstPrinciple = 0.0;
// double nRate = interestRate / 1200.0;
// double nEffectiveBalance = loanAmount;
// double nInstAmount = 0.0;
// DateTime dNextMonth = new DateTime(payBackMonth.Year, payBackMonth.Month, 25);
// //nInstAmount = Financial.Pmt(nRate, noOfInstallment, -loanAmount, 0, DueDate.EndOfPeriod);//DueDate.EndOfPeriod
// nInsPrinciple = Math.Round(nEffectiveBalance / noOfInstallment);
// nInterest = Math.Round(nEffectiveBalance * nRate);
// nInstAmount = Math.Round(nInsPrinciple + nInterest);
// issueDate = GlobalFunctions.LastDateOfMonth(issueDate);
// for (int i = 0; i < noOfInstallment; i++)
// {
// nEffectiveBalance = Math.Round(nEffectiveBalance - nInsPrinciple);
// nTotalInstPrinciple += nInsPrinciple;
// }
// nBalance = loanAmount + nTotalInterest;
// nEffectiveBalance = loanAmount;
// double dPrevInstallmentAmount = 0;
// nTotalInterest = 0;
// nTotalInstPrinciple = 0;
// for (int i = 0; i < noOfInstallment; i++)
// {
// oSchedule = new FMLoanSchedule();
// nTotalInterest = nTotalInterest + nInterest;
// oSchedule.SerialNo = i + 1;
// if (oSchedule.SerialNo == 1)
// {
// dNextMonth = new DateTime(payBackMonth.Year, payBackMonth.Month, 25);
// }
// else
// {
// dNextMonth = dNextMonth.AddMonths(1);
// }
// oSchedule.InstallmentPrincipal = nInsPrinciple;
// oSchedule.ScheduledPayDate = GlobalFunctions.LastDateOfMonth(dNextMonth);
// oSchedule.InstallmentInterest = nInterest;
// nEffectiveBalance -= oSchedule.InstallmentPrincipal;
// if (i > 0)
// {
// nBalance = loanAmount - (dPrevInstallmentAmount);
// oSchedule.EffectiveBalance = nBalance;
// }
// else
// {
// oSchedule.EffectiveBalance = loanAmount;
// }
// dPrevInstallmentAmount = dPrevInstallmentAmount + oSchedule.InstallmentPrincipal;
// nTotalInstPrinciple += oSchedule.InstallmentPrincipal;
// oSchedule.InstallmentAmount = oSchedule.InstallmentPrincipal + oSchedule.InstallmentInterest;
// oFMLoanSchedule.Add(oSchedule);
// }
// int count = oFMLoanSchedule.Count;
// return oFMLoanSchedule;
// }
// catch (ServiceException e)
// {
// throw new Exception(e.Message, e);
// }
//}
public List<FMLoanSchedule> CreateSchedule(FMLoan data)
{
FMLoanCategory ocat = new FMLoanCategoryService().GetbyID(data.LoanCategoryID);
if (ocat.calculationMethod == EnumLoanCalMethod.EMI)
return this.EMICalculation(data, ocat);
else if(ocat.calculationMethod == EnumLoanCalMethod.EMI_With_EqualPrincipal)
return this.PrepareEMIScheduleForFixedPrincipalEMI(data, ocat);
else return null;
}
public List<FMLoanSchedule> EMICalculation(FMLoan data, FMLoanCategory ocat)
{
try
{
EnumLoanFraction fractionate = ocat.fractionCaltype;
List<FMLoanSchedule> oFMLoanSchedule = new List<FMLoanSchedule>();
double monthlyInterestRate = data.InterestRate / 1200;
double emi = Financial.Pmt(monthlyInterestRate, data.InstallmentNo, -data.PrincipalAmount, 0, 0);
double remaningPrincipal = data.PrincipalAmount;
DateTime dNextMonth = new DateTime(data.StartPayBackMonth.Year, data.StartPayBackMonth.Month, 25);
if (ocat.fractionCaltype == EnumLoanFraction.RemoveFraction)
emi = Math.Round(emi, 0);
else
emi = Math.Round(emi, 2);
double FractionAmount = 0;
if (fractionate == EnumLoanFraction.FractionFirstMonth ||
fractionate == EnumLoanFraction.FractionLastMonth)
{
FractionAmount = Math.Round((emi - (double) Math.Floor(emi)) * (double) data.InstallmentNo, 0);
emi = Math.Floor(emi);
}
for (int i = 1; i <= data.InstallmentNo; i++)
{
FMLoanSchedule schedule = new FMLoanSchedule();
schedule.SerialNo = i;
schedule.EffectiveBalance = Math.Round(Financial.PV(monthlyInterestRate, data.InstallmentNo - i, -emi), 0);
schedule.InstallmentAmount = emi;
schedule.InstallmentInterest = Math.Round(emi - (remaningPrincipal - schedule.EffectiveBalance), 0);
schedule.InstallmentPrincipal = Math.Round(Math.Abs(emi - schedule.InstallmentInterest), 0);
remaningPrincipal = Math.Round(remaningPrincipal + schedule.InstallmentInterest - emi, 0);
if (schedule.SerialNo > 1)
dNextMonth = dNextMonth.AddMonths(1);
schedule.ScheduledPayDate = GlobalFunctions.LastDateOfMonth(dNextMonth);
oFMLoanSchedule.Add(schedule);
}
if (fractionate == EnumLoanFraction.FractionFirstMonth)
{
oFMLoanSchedule[0].InstallmentInterest = oFMLoanSchedule[0].InstallmentInterest + FractionAmount;
oFMLoanSchedule[0].InstallmentAmount =
oFMLoanSchedule[0].InstallmentInterest + oFMLoanSchedule[0].InstallmentPrincipal;
}
else if (fractionate == EnumLoanFraction.FractionLastMonth)
{
oFMLoanSchedule[oFMLoanSchedule.Count - 1].InstallmentInterest = oFMLoanSchedule[oFMLoanSchedule.Count - 1].InstallmentInterest + FractionAmount;
oFMLoanSchedule[oFMLoanSchedule.Count - 1].InstallmentAmount =
oFMLoanSchedule[oFMLoanSchedule.Count - 1].InstallmentInterest + oFMLoanSchedule[oFMLoanSchedule.Count - 1].InstallmentPrincipal;
}
return oFMLoanSchedule;
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
}
public List<FMLoanSchedule> PrepareEMIScheduleForFixedPrincipalEMI(FMLoan loanIssue, FMLoanCategory ocat)
{
loanIssue.LoanSchedules = new List<FMLoanSchedule>();
FMLoanSchedule oSchedule = null;
EnumLoanFraction fractionate = ocat.fractionCaltype;
DateTime dMonth = GlobalFunctions.LastDateOfMonth(loanIssue.StartPayBackMonth);
decimal monthlyInterestRate = (decimal)loanIssue.InterestRate / 1200m;
decimal loanAmount = (decimal)loanIssue.PrincipalAmount;
int totalInstallments = loanIssue.InstallmentNo;
int roundingPrecision = (fractionate == EnumLoanFraction.WithFraction) ? 2 : 0;
decimal fixedPrincipal = Math.Round(loanAmount / totalInstallments, roundingPrecision);
decimal openingBalance = loanAmount;
decimal totalPrincipalPaid = 0;
decimal totalInterestPaid = 0;
for (int i = 0; i < totalInstallments; i++)
{
oSchedule = new FMLoanSchedule();
oSchedule.SerialNo = i + 1;
oSchedule.ScheduledPayDate = dMonth;
oSchedule.OpeningBalance = (double)Math.Round(openingBalance, roundingPrecision);
decimal interest = Math.Round(openingBalance * monthlyInterestRate, roundingPrecision);
decimal principal;
if (i == totalInstallments - 1)
{
principal = openingBalance;
interest = Math.Round(openingBalance * monthlyInterestRate, roundingPrecision);
}
else
{
principal = fixedPrincipal;
}
if ((totalPrincipalPaid + principal) > loanAmount)
{
principal = loanAmount - totalPrincipalPaid;
}
openingBalance -= principal;
oSchedule.InstallmentPrincipal = (double)principal;
oSchedule.InstallmentInterest = (double)interest;
decimal installmentAmount = principal + interest;
oSchedule.InstallmentAmount = (double)Math.Round(installmentAmount, roundingPrecision);
oSchedule.ClosingBalance = (i == totalInstallments - 1) ? 0 : (double)Math.Round(openingBalance, roundingPrecision);
totalPrincipalPaid += principal;
totalInterestPaid += interest;
loanIssue.LoanSchedules.Add(oSchedule);
dMonth = GlobalFunctions.LastDateOfMonth(dMonth.AddMonths(1));
}
if (fractionate != EnumLoanFraction.WithFraction)
{
loanIssue.LoanSchedules = AdjustSchedeleForFixedPrincipalEMI(loanIssue, fractionate);
}
if (loanIssue.LoanSchedules.Count > 0)
{
loanIssue.LoanSchedules[loanIssue.LoanSchedules.Count - 1].ClosingBalance = 0;
}
return loanIssue.LoanSchedules;
}
private List<FMLoanSchedule> AdjustSchedeleForFixedPrincipalEMI(FMLoan oloanIssue, EnumLoanFraction fractionate)
{
if (fractionate == EnumLoanFraction.WithFraction) return oloanIssue.LoanSchedules;
double nFraction = 0.0;
foreach (FMLoanSchedule item in oloanIssue.LoanSchedules)
{
nFraction += item.InstallmentInterest - Math.Floor(item.InstallmentInterest);
nFraction += item.InstallmentPrincipal - Math.Floor(item.InstallmentPrincipal);
item.InstallmentPrincipal -= item.InstallmentPrincipal - Math.Floor(item.InstallmentPrincipal);
item.InstallmentInterest -= item.InstallmentInterest - Math.Floor(item.InstallmentInterest);
}
double nequal = 0;
//for (int i = 1; i <= oloanIssue.LoanSchedules.Count - 1; i++)
//{
// if (oloanIssue.LoanSchedules[i - 1].InstallmentAmount != oloanIssue.LoanSchedules[i].InstallmentAmount)
// {
// nequal = oloanIssue.LoanSchedules[i - 1].InstallmentAmount - oloanIssue.LoanSchedules[i].InstallmentAmount;
// oloanIssue.LoanSchedules[i].InstallmentPrincipal = oloanIssue.LoanSchedules[i].InstallmentPrincipal + nequal;
// nFraction = nFraction - nequal;
// }
//}
nFraction = GlobalFunctions.Round(nFraction);
if (nFraction != 0 && oloanIssue.LoanSchedules.Count > 0)
{
if (fractionate == EnumLoanFraction.FractionFirstMonth)
{
if (oloanIssue.InterestRate == 0)
oloanIssue.LoanSchedules[0].InstallmentPrincipal += nFraction;
else oloanIssue.LoanSchedules[0].InstallmentInterest += nFraction;
}
else if (fractionate == EnumLoanFraction.FractionLastMonth)
{
if (oloanIssue.InterestRate == 0)
oloanIssue.LoanSchedules[oloanIssue.LoanSchedules.Count - 1].InstallmentPrincipal += nFraction;
else oloanIssue.LoanSchedules[oloanIssue.LoanSchedules.Count - 1].InstallmentInterest += nFraction;
}
for (int i = 0; i < oloanIssue.LoanSchedules.Count; i++)
{
oloanIssue.LoanSchedules[i].ClosingBalance = oloanIssue.LoanSchedules[i].OpeningBalance - (oloanIssue.LoanSchedules[i].InstallmentPrincipal);
if (i < oloanIssue.LoanSchedules.Count - 1)
{
oloanIssue.LoanSchedules[i + 1].OpeningBalance = oloanIssue.LoanSchedules[i].ClosingBalance;
}
}
}
foreach (FMLoanSchedule item in oloanIssue.LoanSchedules)
{
item.OpeningBalance = Math.Round(item.OpeningBalance, 2);
item.InstallmentAmount = item.InstallmentPrincipal + item.InstallmentInterest;
item.EffectiveBalance = Math.Round(item.OpeningBalance - (item.InstallmentPrincipal), 2);
if (item.SerialNo == oloanIssue.LoanSchedules.Count)
{
item.ClosingBalance = 0;
}
}
return oloanIssue.LoanSchedules;
}
//public List<FMLoanSchedule> PrepareEMISchedule(FMLoan loanIssue, FMLoanCategory ocat)
//{
// try
// {
// EnumLoanFraction fractionate = ocat.fractionCaltype;
// List<FMLoanSchedule> loanSchedules = new List<FMLoanSchedule>();
// DateTime dMonth = loanIssue.StartPayBackMonth;
// double nRate = loanIssue.InterestRate / 1200.0;
// double nInstAmount = Financial.Pmt(nRate, loanIssue.InstallmentNo, -loanIssue.PrincipalAmount, 0, DueDate.EndOfPeriod);
// double FractionAmount = 0;
// if (fractionate == EnumLoanFraction.RemoveFraction)
// {
// nInstAmount = Math.Round(nInstAmount, 0);
// }
// else if (fractionate == EnumLoanFraction.FractionFirstMonth || fractionate == EnumLoanFraction.FractionLastMonth)
// {
// FractionAmount = Math.Round((nInstAmount - Math.Floor(nInstAmount)) * loanIssue.InstallmentNo, 0);
// nInstAmount = Math.Floor(nInstAmount);
// }
// else
// {
// nInstAmount = Math.Round(nInstAmount, 2);
// }
// double nEffectiveBalance = loanIssue.PrincipalAmount;
// double nTotalInstPrinciple = 0.0;
// for (int i = 0; i < loanIssue.InstallmentNo; i++)
// {
// var oSchedule = new FMLoanSchedule();
// double nInterest = Math.Round(nEffectiveBalance * nRate, 2);
// double nInsPrinciple;
// if (i == loanIssue.InstallmentNo - 1)
// {
// nInsPrinciple = nEffectiveBalance;
// oSchedule.InstallmentAmount = nInsPrinciple + nInterest;
// }
// else
// {
// nInsPrinciple = nInstAmount - nInterest;
// oSchedule.InstallmentAmount = nInstAmount;
// }
// oSchedule.SerialNo = i + 1;
// oSchedule.ScheduledPayDate = GlobalFunctions.LastDateOfMonth(dMonth);
// oSchedule.OpeningBalance = nEffectiveBalance;
// oSchedule.InstallmentInterest = nInterest;
// oSchedule.InstallmentPrincipal = nInsPrinciple;
// nEffectiveBalance -= nInsPrinciple;
// nTotalInstPrinciple += nInsPrinciple;
// oSchedule.ClosingBalance = nEffectiveBalance;
// loanSchedules.Add(oSchedule);
// dMonth = dMonth.AddMonths(1);
// }
// if (fractionate == EnumLoanFraction.FractionFirstMonth && loanSchedules.Count > 0)
// {
// loanSchedules[0].InstallmentInterest += FractionAmount;
// loanSchedules[0].InstallmentAmount += FractionAmount;
// }
// else if (fractionate == EnumLoanFraction.FractionLastMonth && loanSchedules.Count > 0)
// {
// int lastIndex = loanSchedules.Count - 1;
// loanSchedules[lastIndex].InstallmentInterest += FractionAmount;
// loanSchedules[lastIndex].InstallmentAmount += FractionAmount;
// }
// loanIssue.LoanSchedules = loanSchedules;
// return loanIssue.LoanSchedules;
// }
// catch (Exception ex)
// {
// throw new Exception("Error occurred while preparing EMI schedule: " + ex.Message, ex);
// }
//}
//public List<FMLoanSchedule> CreateSchedule(FMLoan data)
//{
// try
// {
// FMLoanCategory ocat = new FMLoanCategoryService().GetbyID(data.LoanCategoryID);
// EnumLoanFraction fractionate = ocat.fractionCaltype;
// List<FMLoanSchedule> oFMLoanSchedule = new List<FMLoanSchedule>();
// double monthlyInterestRate = data.InterestRate / 1200;
// double emi = Financial.Pmt(monthlyInterestRate, data.InstallmentNo, -data.PrincipalAmount, 0, 0);
// double remaningPrincipal = data.PrincipalAmount;
// double extraAmount = 0.0;
// double principal = 0.0;
// DateTime dNextMonth = new DateTime(data.StartPayBackMonth.Year, data.StartPayBackMonth.Month, 25);
// if (ocat.fractionCaltype == EnumLoanFraction.RemoveFraction)
// emi = Math.Round(emi, 0);
// else
// emi = Math.Round(emi, 2);
// for (int i = 1; i <= data.InstallmentNo; i++)
// {
// FMLoanSchedule schedule = new FMLoanSchedule();
// schedule.SerialNo = i;
// double interest = Math.Round(remaningPrincipal * monthlyInterestRate, 0);
// principal = 0.0;
// if (i < data.InstallmentNo)
// {
// principal = emi - interest;
// }
// else
// {
// principal = remaningPrincipal;
// emi = principal + interest + extraAmount;
// }
// remaningPrincipal = Math.Round(remaningPrincipal - principal, 2);
// principal = Math.Round(principal, 2);
// interest = Math.Round(interest, 2);
// double installmentAmount = Math.Round(principal + interest, 2);
// extraAmount += (principal + interest) - Math.Round(emi, 2);
// schedule.InstallmentAmount = installmentAmount;
// schedule.InstallmentPrincipal = principal;
// schedule.InstallmentInterest = interest;
// schedule.EffectiveBalance = Math.Round(remaningPrincipal, 2);
// if (schedule.SerialNo > 1)
// {
// dNextMonth = dNextMonth.AddMonths(1);
// }
// schedule.ScheduledPayDate = GlobalFunctions.LastDateOfMonth(dNextMonth);
// oFMLoanSchedule.Add(schedule);
// }
// if (fractionate != EnumLoanFraction.WithFraction)
// {
// data.LoanSchedules = oFMLoanSchedule;
// AdjustSchedele(data, fractionate);
// }
// return oFMLoanSchedule;
// }
// catch (Exception e)
// {
// throw new Exception(e.Message, e);
// }
//}
private void AdjustSchedele(FMLoan oloanIssue, EnumLoanFraction fractionate)
{
if (fractionate == EnumLoanFraction.WithFraction) return;
if(fractionate == EnumLoanFraction.RemoveFraction)
{
foreach (FMLoanSchedule item in oloanIssue.LoanSchedules)
{
item.InstallmentPrincipal = Math.Round( item.InstallmentPrincipal,0);
item.InstallmentInterest = Math.Round(item.InstallmentInterest, 0);
item.EffectiveBalance = Math.Round(item.EffectiveBalance, 0);
item.OpeningBalance = Math.Round(item.OpeningBalance, 0);
item.InstallmentAmount = item.InstallmentPrincipal + item.InstallmentInterest;
item.ClosingBalance = Math.Round(item.OpeningBalance - (item.InstallmentPrincipal + item.InstallmentInterest), 0);
}
return;
}
double nFraction = 0.0;
foreach (FMLoanSchedule item in oloanIssue.LoanSchedules)
{
nFraction += item.InstallmentInterest - Math.Floor(item.InstallmentInterest);
nFraction += item.InstallmentPrincipal - Math.Floor(item.InstallmentPrincipal);
item.InstallmentPrincipal -= item.InstallmentPrincipal - Math.Floor(item.InstallmentPrincipal);
item.InstallmentInterest -= item.InstallmentInterest - Math.Floor(item.InstallmentInterest);
item.InstallmentPrincipal = Math.Round(item.InstallmentPrincipal, 0);
item.InstallmentInterest = Math.Round(item.InstallmentInterest,0);
}
double nequal = 0;
for (int i = 1; i <= oloanIssue.LoanSchedules.Count - 1; i++)
{
if (oloanIssue.LoanSchedules[i - 1].InstallmentAmount != oloanIssue.LoanSchedules[i].InstallmentAmount)
{
nequal = oloanIssue.LoanSchedules[i - 1].InstallmentAmount - oloanIssue.LoanSchedules[i].InstallmentAmount;
oloanIssue.LoanSchedules[i].InstallmentPrincipal = oloanIssue.LoanSchedules[i].InstallmentPrincipal + nequal;
oloanIssue.LoanSchedules[i].InstallmentPrincipal = Math.Round(oloanIssue.LoanSchedules[i].InstallmentPrincipal, 0);
nFraction = nFraction - nequal;
}
}
nFraction = GlobalFunctions.Round(nFraction,0);
if (nFraction != 0 && oloanIssue.LoanSchedules.Count > 0)
{
if (fractionate == EnumLoanFraction.FractionFirstMonth)
{
if (oloanIssue.InterestRate == 0)
oloanIssue.LoanSchedules[0].InstallmentPrincipal += nFraction;
else oloanIssue.LoanSchedules[0].InstallmentInterest += nFraction;
}
else if (fractionate == EnumLoanFraction.FractionLastMonth)
{
if (oloanIssue.InterestRate == 0)
oloanIssue.LoanSchedules[oloanIssue.LoanSchedules.Count - 1].InstallmentPrincipal += nFraction;
else oloanIssue.LoanSchedules[oloanIssue.LoanSchedules.Count - 1].InstallmentInterest += nFraction;
}
for (int i = 0; i < oloanIssue.LoanSchedules.Count; i++)
{
// if (i == 9) return;
oloanIssue.LoanSchedules[i].ClosingBalance = oloanIssue.LoanSchedules[i].OpeningBalance - (oloanIssue.LoanSchedules[i].InstallmentPrincipal + oloanIssue.LoanSchedules[i].InstallmentInterest);
if (i < oloanIssue.LoanSchedules.Count - 1)
{
oloanIssue.LoanSchedules[i + 1].OpeningBalance = oloanIssue.LoanSchedules[i].ClosingBalance;
}
}
}
double remaningPrincipal = oloanIssue.PrincipalAmount;
int cnt = 0;
foreach (FMLoanSchedule item in oloanIssue.LoanSchedules)
{
cnt = cnt + 1;
remaningPrincipal = Math.Round(remaningPrincipal - item.InstallmentPrincipal, 0);
if(oloanIssue.LoanSchedules.Count == cnt)
item.EffectiveBalance = 0;
else item.EffectiveBalance = remaningPrincipal;
item.OpeningBalance = Math.Round(item.OpeningBalance, 0);
item.InstallmentAmount =Math.Round( item.InstallmentPrincipal + item.InstallmentInterest,0);
item.ClosingBalance = Math.Round(item.OpeningBalance - (item.InstallmentPrincipal + item.InstallmentInterest), 0);
}
}
//public List<FMLoanSchedule> CreateSchedule(int noOfInstallment, double interestRate, double loanAmount, DateTime payBackMonth, DateTime issueDate)
//{
// List<FMLoanSchedule> oFMLoanSchedule = new List<FMLoanSchedule>();
// try
// {
// FMLoanSchedule oSchedule = null;
// oFMLoanSchedule.Clear();
// double nInterest = 0.0;
// double nTotalInterest = 0.0;
// double nInsPrinciple = 0.0;
// double nBalance = 0.0;
// double nTotalInstPrinciple = 0.0;
// double nRate = interestRate / 1200.0;
// double extra = 0.0;
// double nEffectiveBalance = loanAmount;
// double nInstAmount = Financial.Pmt(nRate, noOfInstallment, -loanAmount, 0, 0);//DueDate.EndOfPeriod
// DateTime dNextMonth = new DateTime(payBackMonth.Year, payBackMonth.Month, 25);
// issueDate = GlobalFunctions.LastDateOfMonth(issueDate);
// nTotalInterest = 0;
// for (int i = 0; i < noOfInstallment; i++)
// {
// nInterest = (nEffectiveBalance * nRate);
// nTotalInterest = nTotalInterest + nInterest;
// if (i == noOfInstallment - 1)
// {
// nInsPrinciple = loanAmount - nTotalInstPrinciple;
// }
// else
// {
// nInsPrinciple = nInstAmount - nInterest;
// }
// nEffectiveBalance = nEffectiveBalance - nInsPrinciple;
// nTotalInstPrinciple += nInsPrinciple;
// }
// nBalance = loanAmount + nTotalInterest;
// nEffectiveBalance = loanAmount;
// double dPrevInstallmentAmount = 0;
// nTotalInterest = 0;
// nTotalInstPrinciple = 0;
// for (int i = 0; i < noOfInstallment; i++)
// {
// oSchedule = new FMLoanSchedule();
// nInterest = nEffectiveBalance * nRate;
// nInterest = ((nEffectiveBalance * (interestRate / 1200.0)));
// nTotalInterest = nTotalInterest + nInterest;
// oSchedule.SerialNo = i + 1;
// if (oSchedule.SerialNo == 1)
// {
// dNextMonth = new DateTime(payBackMonth.Year, payBackMonth.Month, 25);
// }
// else
// {
// dNextMonth = dNextMonth.AddMonths(1);
// }
// if (i == noOfInstallment - 1)
// {
// oSchedule.InstallmentPrincipal = loanAmount - nTotalInstPrinciple;
// }
// else
// {
// oSchedule.InstallmentPrincipal = Math.Round(nInstAmount - nInterest);
// }
// oSchedule.ScheduledPayDate = GlobalFunctions.LastDateOfMonth(dNextMonth);
// oSchedule.InstallmentInterest = Math.Round(nInterest);
// if (i > 0)
// {
// nBalance = loanAmount - (dPrevInstallmentAmount);//(oSchedule.InstallmentPrincipal + oSchedule.InstallmentInterest);
// oSchedule.EffectiveBalance = Math.Round(nBalance);
// }
// else
// {
// oSchedule.EffectiveBalance = Math.Round(nEffectiveBalance);
// }
// nEffectiveBalance -= oSchedule.InstallmentPrincipal;
// dPrevInstallmentAmount = dPrevInstallmentAmount + oSchedule.InstallmentPrincipal;
// nTotalInstPrinciple += oSchedule.InstallmentPrincipal;
// double totalPayAmount = oSchedule.InstallmentInterest + oSchedule.InstallmentPrincipal;
// if (totalPayAmount > Math.Round(nInstAmount))
// {
// extra += totalPayAmount - Math.Round(nInstAmount);
// }
// else if (totalPayAmount < Math.Round(nInstAmount))
// {
// extra -=Math.Round(nInstAmount) - totalPayAmount;
// }
// if(oSchedule.SerialNo==noOfInstallment&& extra!=0)
// nInstAmount = nInstAmount + extra;
// oSchedule.InstallmentAmount = Math.Round(nInstAmount);
// oFMLoanSchedule.Add(oSchedule);
// }
// int count = oFMLoanSchedule.Count;
// return oFMLoanSchedule;
// }
// catch (ServiceException e)
// {
// throw new Exception(e.Message, e);
// }
//}
#endregion
public void UpdateSchedules(List<FMLoanSchedule> oLoanSchedules)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
foreach (FMLoanSchedule item in oLoanSchedules)
{
FMLoanScheduleDA.Update(tc, item);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
}
}