EchoTex_Payroll/HRM.BO/Loan/LoanIssue.cs

1131 lines
43 KiB
C#
Raw Normal View History

2024-10-14 10:01:49 +06:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ease.Core;
using Ease.Core.Model;
using System.Data;
using Microsoft.VisualBasic;
namespace HRM.BO
{
#region LoanIssue
public class LoanIssue : AuditTrailBase
{
#region Constructor
//private List<LoanSchedule> _MonthlySchedules = null;
public LoanIssue()
{
//_MonthlySchedules = null;
//_loanID = null;
LoanNo = string.Empty;
//_employeeID = null;
LoanAmount = 0;
//IssueDate = DateTime.MinValue;
//_noOfInstallments = 0;
//_installmentPrincipal = 0;
//_interestRate = 0;
//_startPaybackMonthDate = DateTime.MinValue;
//_remainInstallment = 0;
//_remainInterest = 0;
//_remNoInstallment = 0;
//_employee = null;
//_loan = null;
//_wfStatus = enumwfStatus.Not_yet_Submitted;
//_AverageInterestRate = false;
//_settledPrincipal = 0;
//_settledInterest = 0;
//_isTopup = false;
}
#endregion
#region Properties
public int LoanID { get; set; }
public string LoanNo { get; set; }
public int EmployeeID { get; set; }
public double LoanAmount { get; set; }
public DateTime IssueDate { get; set; }
public int NoOfInstallments { get; set; }
public double InstallmentPrincipal { get; set; }
public double InterestRate { get; set; }
public DateTime StartPaybackMonthDate { get; set; }
public double RemainInstallment { get; set; }
public double RemainInterest { get; set; }
public double RemNoInstallment { get; set; }
public Employee Employee { get; set; }
public Loan loan { get; set; }
//#region employee : Employee
//private Employee _employee;
//public Employee Employee
//{
// get
// {
// if (_employeeID.Integer > 0 && _employee == null)
// {
// _employee = new Employee();
// _employee = Employee.Get(_employeeID);
// }
// return this._employee;
// }
// set
// {
// _employee = value;
// }
//}
//#endregion
public Loan LoanObj { get; set; }
//#region loan : Loan
//private Loan _loan;
//public Loan LoanObj
//{
// get
// {
// if (_loanID.Integer > 0 && _loan == null)
// {
// _loan = Loan.Get(_loanID);
// }
// return this._loan;
// }
// set
// {
// _loan = value;
// }
//}
//#endregion
public List<LoanSchedule> Schedules { get; set; }
//#region Schedule : Schedule
//private List<LoanSchedule> _Schedules;
//public List<LoanSchedule> Schedules
//{
// get
// {
// if (_Schedules == null && !ID.IsUnassigned && ID.Integer > 0)
// {
// _Schedules = LoanSchedule.GetByIssueID(this.ID);
// }
// if (_Schedules == null)
// {
// _Schedules = new List<LoanSchedule>();
// }
// return _Schedules;
// }
//}
//#endregion
public double SettledPrincipal { get; set; }
public double SettledInterest { get; set; }
public bool IsTopup { get; set; }
public bool AverageInterestRate { get; set; }
public EnumwfStatus WFStatus { get; set; }
public List<LoanEmployeeDoc> IssueDocks { get; set; }
//private List<LoanEmployeeDoc> _issueDocks;
//public List<LoanEmployeeDoc> IssueDocks
//{
// get
// {
// if (_issueDocks == null)
// _issueDocks = LoanEmployeeDoc.GetbyIssueId(this.ID);
// return _issueDocks;
// }
// set
// {
// _issueDocks = value;
// }
//}
//#region Service Factory ILoanIssueService : ILoanIssueService
//internal static ILoanIssueService Service
//{
// get { return Services.Factory.CreateService<ILoanIssueService>(typeof(ILoanIssueService)); }
//}
//#endregion
#endregion
public void PrepareEMISchedule(DateTime dMonth, EnumLoanFraction fractionate, double dPrincipal)
{
LoanSchedule oSchedule = null;
this.Schedules.Clear();
double nInterest = 0.0;
double nTotalInterest = 0.0;
double nInsPrinciple = 0.0;
double nBalance = 0.0;
double nTotalInstPrinciple = 0.0;
double nRate = this.InterestRate / 1200.0;
double nEffectiveBalance = this.LoanAmount;
double nInstAmount = Financial.Pmt(nRate, this.NoOfInstallments, -this.LoanAmount, 0, DueDate.EndOfPeriod);
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
nTotalInterest = 0;
nEffectiveBalance = this.LoanAmount;
nBalance = this.LoanAmount;
nInstAmount = Financial.Pmt(nRate, this.NoOfInstallments, -this.LoanAmount, 0, DueDate.EndOfPeriod);
nTotalInterest = 0;
nTotalInstPrinciple = 0;
for (int i = 0; i < this.NoOfInstallments; i++)
{
oSchedule = new LoanSchedule();
//nInterest = nEffectiveBalance * nRate;
//nTotalInterest = nTotalInterest + nInterest;
oSchedule.CalculatedDate = dMonth;
oSchedule.ScheduleNo = i + 1;
if (oSchedule.ScheduleNo < this.NoOfInstallments)
{
oSchedule.InstallmentPrincipal = dPrincipal;
nTotalInstPrinciple += oSchedule.InstallmentPrincipal;
}
else if (oSchedule.ScheduleNo == this.NoOfInstallments)
{
oSchedule.InstallmentPrincipal = this.LoanAmount - nTotalInstPrinciple;
}
oSchedule.OpeningBalance = nBalance;
oSchedule.ActualInterest = 0;
oSchedule.DueInstallmentDate = dMonth;
oSchedule.InstallmentInterest = 0;
nEffectiveBalance -= oSchedule.InstallmentPrincipal;
nBalance = nBalance - (oSchedule.InstallmentPrincipal);
oSchedule.ClosingBalance = nBalance;
this.Schedules.Add(oSchedule);
dMonth = dMonth.AddMonths(1);
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
}
//if (fractionate != EnumLoanFraction.WithFraction)
//{
// AdjustSchedele(fractionate);
//}
}
public List<LoanSchedule> PrepareEMISchedule(LoanIssue loanIssue)
{
LoanSchedule oSchedule = null;
this.Schedules = new List<LoanSchedule>();
EnumLoanFraction fractionate = EnumLoanFraction.FractionLastMonth;
DateTime dMonth = loanIssue.StartPaybackMonthDate;
double nInterest = 0.0;
double nTotalInterest = 0.0;
double nInsPrinciple = 0.0;
double nBalance = 0.0;
double nTotalInstPrinciple = 0.0;
double nRate = loanIssue.InterestRate / 1200.0;
double nEffectiveBalance = loanIssue.LoanAmount;
double nInstAmount = Financial.Pmt(nRate, loanIssue.NoOfInstallments, -loanIssue.LoanAmount, 0, DueDate.EndOfPeriod);
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
nTotalInterest = 0;
for (int i = 0; i < loanIssue.NoOfInstallments; i++)
{
nInterest = nEffectiveBalance * nRate;
nTotalInterest = nTotalInterest + nInterest;
if (i == loanIssue.NoOfInstallments - 1)
{
nInsPrinciple = loanIssue.LoanAmount - nTotalInstPrinciple;
}
else
{
nInsPrinciple = nInstAmount - nInterest;
}
nEffectiveBalance = nEffectiveBalance - nInsPrinciple;
nTotalInstPrinciple += nInsPrinciple;
}
nBalance = loanIssue.LoanAmount + nTotalInterest;
nEffectiveBalance = loanIssue.LoanAmount;
nInstAmount = Financial.Pmt(nRate, loanIssue.NoOfInstallments, -loanIssue.LoanAmount, 0, DueDate.EndOfPeriod);
nTotalInterest = 0;
nTotalInstPrinciple = 0;
for (int i = 0; i < loanIssue.NoOfInstallments; i++)
{
oSchedule = new LoanSchedule();
nInterest = nEffectiveBalance * nRate;
nTotalInterest = nTotalInterest + nInterest;
oSchedule.CalculatedDate = dMonth;
oSchedule.ScheduleNo = i + 1;
if (i == loanIssue.NoOfInstallments - 1)
{
oSchedule.InstallmentPrincipal = loanIssue.LoanAmount - nTotalInstPrinciple;
}
else
{
oSchedule.InstallmentPrincipal = nInstAmount - nInterest;
}
oSchedule.OpeningBalance = nBalance;
oSchedule.ActualInterest = nInterest;
oSchedule.DueInstallmentDate = dMonth;
oSchedule.InstallmentInterest = nInterest;
nEffectiveBalance -= oSchedule.InstallmentPrincipal;
nBalance = nBalance - (oSchedule.InstallmentPrincipal + oSchedule.InstallmentInterest);
nTotalInstPrinciple += oSchedule.InstallmentPrincipal;
oSchedule.ClosingBalance = nBalance;
loanIssue.Schedules.Add(oSchedule);
dMonth = dMonth.AddMonths(1);
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
}
if (fractionate != EnumLoanFraction.WithFraction)
{
AdjustSchedele(loanIssue, fractionate);
}
return loanIssue.Schedules;
}
public void PrepareEMISchedule(DateTime dMonth, EnumLoanFraction fractionate, int nSequenceNo)
{
LoanSchedule oSchedule = null;
double nInterest = 0.0;
double nTotalInterest = 0.0;
double nInsPrinciple = 0.0;
double nBalance = 0.0;
double nTotalInstPrinciple = 0.0;
double nRate = this.InterestRate / 1200.0;
double nEffectiveBalance = this.LoanAmount;
double nInstAmount = Financial.Pmt(nRate, this.NoOfInstallments, -this.LoanAmount, 0, DueDate.EndOfPeriod);
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
nTotalInterest = 0;
for (int i = nSequenceNo - 1; i < this.NoOfInstallments; i++)
{
nInterest = nEffectiveBalance * nRate;
nTotalInterest = nTotalInterest + nInterest;
if (i == this.NoOfInstallments - 1)
{
nInsPrinciple = this.LoanAmount - nTotalInstPrinciple;
}
else
{
nInsPrinciple = nInstAmount - nInterest;
}
nEffectiveBalance = nEffectiveBalance - nInsPrinciple;
nTotalInstPrinciple += nInsPrinciple;
}
nBalance = Math.Round( this.LoanAmount + nTotalInterest, 2);
nEffectiveBalance = this.LoanAmount;
nInstAmount = Financial.Pmt(nRate, this.NoOfInstallments, -this.LoanAmount, 0, DueDate.EndOfPeriod);
nTotalInterest = 0;
nTotalInstPrinciple = 0;
for (int i = nSequenceNo - 1; i < this.NoOfInstallments; i++)
{
oSchedule = new LoanSchedule();
nInterest = nEffectiveBalance * nRate;
nTotalInterest = nTotalInterest + nInterest;
oSchedule.CalculatedDate = dMonth;
oSchedule.ScheduleNo = i + 1;
if (i == this.NoOfInstallments - 1)
{
oSchedule.InstallmentPrincipal = this.LoanAmount - nTotalInstPrinciple;
}
else
{
oSchedule.InstallmentPrincipal = nInstAmount - nInterest;
}
oSchedule.OpeningBalance = Math.Round( nBalance,2);
oSchedule.ActualInterest = nInterest;
oSchedule.DueInstallmentDate = dMonth;
oSchedule.InstallmentInterest = nInterest;
nEffectiveBalance -= oSchedule.InstallmentPrincipal;
nBalance = nBalance - (oSchedule.InstallmentPrincipal + oSchedule.InstallmentInterest);
nTotalInstPrinciple += oSchedule.InstallmentPrincipal;
oSchedule.ClosingBalance = nBalance;
this.Schedules.Add(oSchedule);
dMonth = dMonth.AddMonths(1);
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
}
if (fractionate != EnumLoanFraction.WithFraction)
{
AdjustSchedele(this, fractionate);
}
}
public void PrepareLMIDSchedule(DateTime dMonth, EnumLoanFraction fractionate)
{
LoanSchedule oSchedule = null;
this.Schedules.Clear();
double nTotalInterest = 0.0;
double nBalance = 0.0;
double nInstPrinciple = 0.0;
double nTotalInstPrinciple = 0.0;
int i = 0;
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
nTotalInterest = this.LoanAmount * (this.InterestRate / 100.0) * ((double)this.NoOfInstallments / 12.0);
nInstPrinciple = GlobalFunctions.Round(this.LoanAmount / this.NoOfInstallments);
nBalance = this.LoanAmount + nTotalInterest;
for (i = 0; i < this.NoOfInstallments; i++)
{
oSchedule = new LoanSchedule();
oSchedule.CalculatedDate = dMonth;
oSchedule.ScheduleNo = i + 1;
if (i == this.NoOfInstallments - 1)
{
oSchedule.InstallmentPrincipal = this.LoanAmount - nTotalInstPrinciple;
}
else
{
oSchedule.InstallmentPrincipal = nInstPrinciple;
}
oSchedule.OpeningBalance = nBalance;
oSchedule.ActualInterest = 0;
oSchedule.DueInstallmentDate = dMonth;
oSchedule.InstallmentInterest = 0;
nBalance = nBalance - (oSchedule.InstallmentPrincipal + oSchedule.InstallmentInterest);
nTotalInstPrinciple += oSchedule.InstallmentPrincipal;
oSchedule.ClosingBalance = nBalance;
this.Schedules.Add(oSchedule);
dMonth = dMonth.AddMonths(1);
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
}
while (nTotalInterest > 0)
{
oSchedule = new LoanSchedule();
oSchedule.CalculatedDate = dMonth;
oSchedule.ScheduleNo = i++;
oSchedule.InstallmentPrincipal = 0;
oSchedule.OpeningBalance = nBalance;
oSchedule.InstallmentInterest = nTotalInterest > nInstPrinciple ? nInstPrinciple : nTotalInterest;
nBalance = nBalance - oSchedule.InstallmentInterest;
oSchedule.ClosingBalance = nBalance;
oSchedule.DueInstallmentDate = dMonth;
nTotalInterest = nTotalInterest - nInstPrinciple;
dMonth = dMonth.AddMonths(1);
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
this.Schedules.Add(oSchedule);
}
if (fractionate != EnumLoanFraction.WithFraction)
{
AdjustSchedele(this, fractionate);
}
}
public void PrepareLMIDSchedule(DateTime dMonth, EnumLoanFraction fractionate, int nSequenceNo)
{
LoanSchedule oSchedule = null;
this.Schedules.Clear();
double nTotalInterest = 0.0;
double nBalance = 0.0;
double nInstPrinciple = 0.0;
double nTotalInstPrinciple = 0.0;
int i = nSequenceNo - 1;
dMonth = GlobalFunctions.LastDateOfMonth(dMonth); ;
nTotalInterest = this.LoanAmount * (this.InterestRate / 100.0) * ((double)this.NoOfInstallments / 12.0);
nInstPrinciple = GlobalFunctions.Round(this.LoanAmount / this.NoOfInstallments);
nBalance = this.LoanAmount + nTotalInterest;
for (i = nSequenceNo - 1; i < this.NoOfInstallments; i++)
{
oSchedule = new LoanSchedule();
oSchedule.CalculatedDate = dMonth;
oSchedule.ScheduleNo = i + 1;
if (i == this.NoOfInstallments - 1)
{
oSchedule.InstallmentPrincipal = this.LoanAmount - nTotalInstPrinciple;
}
else
{
oSchedule.InstallmentPrincipal = nInstPrinciple;
}
oSchedule.OpeningBalance = nBalance;
oSchedule.ActualInterest = 0;
oSchedule.DueInstallmentDate = dMonth;
oSchedule.InstallmentInterest = 0;
nBalance = nBalance - (oSchedule.InstallmentPrincipal + oSchedule.InstallmentInterest);
nTotalInstPrinciple += oSchedule.InstallmentPrincipal;
oSchedule.ClosingBalance = nBalance;
this.Schedules.Add(oSchedule);
dMonth = dMonth.AddMonths(1);
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
}
while (nTotalInterest > 0)
{
oSchedule = new LoanSchedule();
oSchedule.CalculatedDate = dMonth;
oSchedule.ScheduleNo = i++;
oSchedule.InstallmentPrincipal = 0;
oSchedule.OpeningBalance = nBalance;
oSchedule.InstallmentInterest = nTotalInterest > nInstPrinciple ? nInstPrinciple : nTotalInterest;
nBalance = nBalance - oSchedule.InstallmentInterest;
oSchedule.ClosingBalance = nBalance;
oSchedule.DueInstallmentDate = dMonth;
nTotalInterest = nTotalInterest - nInstPrinciple;
dMonth = dMonth.AddMonths(1);
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
this.Schedules.Add(oSchedule);
}
if (fractionate != EnumLoanFraction.WithFraction)
{
AdjustSchedele(this, fractionate);
}
}
public void PrepareL2MIDSchedule(DateTime dMonth, EnumLoanFraction fractionate)
{
LoanSchedule oSchedule = null;
this.Schedules.Clear();
double nTotalInterest = 0.0;
double nBalance = 0.0;
double nInstPrinciple = 0.0;
double nInstPrincipleRounded = 0.0;
double nTotalInstPrinciple = 0.0;
long nTotalFractionValue = 0;
int i = 0;
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
nTotalInterest = this.LoanAmount * (this.InterestRate / 100.0) * ((double)this.NoOfInstallments / 12.0);
nInstPrinciple = GlobalFunctions.Round(this.LoanAmount / this.NoOfInstallments);
nBalance = this.LoanAmount + nTotalInterest;
if (nInstPrinciple % 10 > 0)
{
nInstPrincipleRounded = nInstPrinciple + (10 - nInstPrinciple % 10);
}
else
{
nInstPrincipleRounded = nInstPrinciple;
}
this.InstallmentPrincipal = nInstPrincipleRounded;
for (i = 0; i <= this.NoOfInstallments; i++)
{
oSchedule = new LoanSchedule();
oSchedule.CalculatedDate = dMonth;
oSchedule.ScheduleNo = i + 1;
if (i == this.NoOfInstallments)
{
if (this.NoOfInstallments <= 12)
{
oSchedule.InstallmentInterest = nTotalInterest + nTotalFractionValue;
oSchedule.InstallmentPrincipal = 0.0;
}
else
{
oSchedule.InstallmentInterest = (nTotalInterest + nTotalFractionValue) / 2;
oSchedule.InstallmentPrincipal = 0.0;
}
}
else if (i == this.NoOfInstallments - 1)
{
oSchedule.InstallmentInterest = 0.0;
oSchedule.InstallmentPrincipal = nBalance - nTotalInterest;
}
else
{
oSchedule.InstallmentInterest = 0.0;
oSchedule.InstallmentPrincipal = nInstPrincipleRounded;
}
oSchedule.OpeningBalance = nBalance;
oSchedule.ActualInterest = 0;
oSchedule.DueInstallmentDate = dMonth;
nBalance = nBalance - (oSchedule.InstallmentPrincipal + oSchedule.InstallmentInterest);
nTotalInstPrinciple += oSchedule.InstallmentPrincipal;
oSchedule.ClosingBalance = nBalance;
if (i >= this.NoOfInstallments && nTotalInterest <= 0)
{ }
else
{
this.Schedules.Add(oSchedule);
}
dMonth = dMonth.AddMonths(1);
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
if (i == this.NoOfInstallments && this.NoOfInstallments >= 13 && nTotalInterest > 0)
{
oSchedule = new LoanSchedule();
oSchedule.CalculatedDate = dMonth;
oSchedule.ScheduleNo = i + 1;
oSchedule.InstallmentInterest = (nTotalInterest + nTotalFractionValue) / 2;
oSchedule.InstallmentPrincipal = 0.0;
oSchedule.OpeningBalance = nBalance;
oSchedule.ActualInterest = 0;
oSchedule.DueInstallmentDate = dMonth;
nBalance = nBalance - (oSchedule.InstallmentPrincipal + oSchedule.InstallmentInterest);
nTotalInstPrinciple += oSchedule.InstallmentPrincipal;
oSchedule.ClosingBalance = nBalance;
this.Schedules.Add(oSchedule);
dMonth = dMonth.AddMonths(1);
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
}
}
}
public List<LoanSchedule> PrepareSIDSchedule(LoanIssue issue)
{
LoanSchedule oSchedule = null;
EnumLoanFraction fractionate = EnumLoanFraction.WithFraction;
DateTime dMonth = issue.StartPaybackMonthDate;
issue.Schedules.Clear();
int i = 0;
double nInterest = 0.0;
double nBalance = 0.0;
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
nInterest = issue.LoanAmount * issue.InterestRate / 1200 * issue.NoOfInstallments;
nBalance = issue.LoanAmount + nInterest;
while (Math.Round(nBalance) > 0)
{
oSchedule = new LoanSchedule();
oSchedule.CalculatedDate = dMonth;
oSchedule.ScheduleNo = ++i;
oSchedule.InstallmentInterest = nInterest / issue.NoOfInstallments;
oSchedule.InstallmentPrincipal = issue.LoanAmount / issue.NoOfInstallments;
oSchedule.OpeningBalance = nBalance;
oSchedule.ActualInterest = oSchedule.InstallmentInterest;
oSchedule.DueInstallmentDate = dMonth;
nBalance = nBalance - (oSchedule.InstallmentPrincipal + oSchedule.InstallmentInterest);
oSchedule.ClosingBalance = nBalance;
issue.Schedules.Add(oSchedule);
dMonth = dMonth.AddMonths(1);
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
}
if (fractionate != EnumLoanFraction.WithFraction)
{
AdjustSchedele( this, fractionate);
}
return issue.Schedules;
}
public void PrepareBankSchedule(DateTime dMonth, EnumLoanFraction fractionate)
{
LoanSchedule oSchedule = null;
double nInterest = 0.0, nBalance = 0.0, nTotalInterest = 0.0, nInstPrinciple = 0.0;
int nDays = 0;
this.Schedules.Clear();
DateTime dNextMonth = new DateTime(this.StartPaybackMonthDate.Year, this.StartPaybackMonthDate.Month, 25);
dMonth = GlobalFunctions.LastDateOfMonth(dMonth);
nInstPrinciple = this.LoanAmount / this.NoOfInstallments;
nInstPrinciple = GlobalFunctions.Round(nInstPrinciple);
nBalance = this.LoanAmount;
for (int i = 1; i <= this.NoOfInstallments; i++)
{
oSchedule = new LoanSchedule();
if (i == 1)
{
nInterest = GlobalFunctions.Round(((this.LoanAmount * this.InterestRate)) / 100 / 12);
oSchedule.CalculatedDate = dNextMonth;
}
else
{
nInterest = GlobalFunctions.Round((nBalance * this.InterestRate) / 100 / 12);
dNextMonth = dNextMonth.AddMonths(1);
oSchedule.CalculatedDate = dNextMonth;
}
nTotalInterest += nInterest;
oSchedule.ScheduleNo = i;
if (i == this.NoOfInstallments)
nInstPrinciple = nBalance;
oSchedule.InstallmentInterest = nInterest;
oSchedule.InstallmentPrincipal = nInstPrinciple;
oSchedule.OpeningBalance = nBalance;
oSchedule.ActualInterest = oSchedule.InstallmentInterest;
oSchedule.DueInstallmentDate = GlobalFunctions.LastDateOfMonth(dNextMonth);
oSchedule.Days = nDays;
nBalance = nBalance - (oSchedule.InstallmentPrincipal);
oSchedule.ClosingBalance = nBalance;
this.Schedules.Add(oSchedule);
}
}
public List<LoanSchedule> PreparePFLoanSchedule(LoanIssue loanIssue)
{
LoanSchedule oSchedule = null;
double nInterest = 0.0, nBalance = 0.0, nTotalInterest = 0.0, nInstPrinciple = 0.0;
int nDays = 0;
loanIssue.Schedules.Clear();
DateTime dMonth = GlobalFunctions.LastDateOfMonth(loanIssue.StartPaybackMonthDate);
nInstPrinciple = loanIssue.LoanAmount / loanIssue.NoOfInstallments;
nInstPrinciple = GlobalFunctions.Round(nInstPrinciple);
nBalance = loanIssue.LoanAmount;
for (int i = 1; i <= loanIssue.NoOfInstallments; i++)
{
oSchedule = new LoanSchedule();
if (i == 1)
{
oSchedule.CalculatedDate = dMonth;
}
else
{
dMonth = dMonth.AddMonths(1);
oSchedule.CalculatedDate = dMonth;
}
nTotalInterest += nInterest;
oSchedule.ScheduleNo = i;
//if (i == loanIssue.NoOfInstallments && loanIssue.loan.calculationType != EnumLoanFraction.RemoveFraction)
// nInstPrinciple = nBalance;
oSchedule.InstallmentInterest = 0;
oSchedule.InstallmentPrincipal = nInstPrinciple;
oSchedule.OpeningBalance = nBalance;
oSchedule.ActualInterest = 0;
oSchedule.DueInstallmentDate = GlobalFunctions.LastDateOfMonth(dMonth);
oSchedule.Days = nDays;
nBalance = nBalance - (oSchedule.InstallmentPrincipal);
if (nBalance < 0) nBalance = 0;
oSchedule.ClosingBalance = nBalance;
loanIssue.Schedules.Add(oSchedule);
}
if (loanIssue.loan.calculationType != EnumLoanFraction.WithFraction)
{
AdjustSchedele(loanIssue, loanIssue.loan.calculationType);
}
dMonth = dMonth.AddMonths(1);
oSchedule = new LoanSchedule();
oSchedule.CalculatedDate = dMonth;
nTotalInterest += nInterest;
oSchedule.ScheduleNo = loanIssue.Schedules.Count+1;
oSchedule.InstallmentInterest = nInstPrinciple;
oSchedule.InstallmentPrincipal = 0;
oSchedule.OpeningBalance = 0;
oSchedule.ActualInterest = 0;
oSchedule.DueInstallmentDate = GlobalFunctions.LastDateOfMonth(dMonth);
oSchedule.Days = nDays;
nBalance = nBalance - (oSchedule.InstallmentPrincipal);
oSchedule.ClosingBalance = 0;
loanIssue.Schedules.Add(oSchedule);
//if(loanIssue.Schedules.Count >13)
//{
// dMonth = dMonth.AddMonths(1);
// oSchedule = new LoanSchedule();
// oSchedule.CalculatedDate = dMonth;
// nTotalInterest += nInterest;
// oSchedule.ScheduleNo = loanIssue.Schedules.Count + 1;
// oSchedule.InstallmentInterest = nInstPrinciple;
// oSchedule.InstallmentPrincipal =0 ;
// oSchedule.OpeningBalance = 0;
// oSchedule.ActualInterest = 0;
// oSchedule.DueInstallmentDate = GlobalFunctions.LastDateOfMonth(dMonth);
// oSchedule.Days = nDays;
// nBalance = nBalance - (oSchedule.InstallmentPrincipal);
// oSchedule.ClosingBalance = 0;
// loanIssue.Schedules.Add(oSchedule);
//}
return loanIssue.Schedules;
}
public List<LoanSchedule> PrepareFlatAmountSchedule(LoanIssue loanIssue)
{
LoanSchedule oSchedule = null;
double nInterest = 0.0, nBalance = 0.0, nTotalInterest = 0.0, nInstPrinciple = 0.0;
int nDays = 0;
loanIssue.Schedules.Clear();
DateTime dMonth = GlobalFunctions.LastDateOfMonth(loanIssue.StartPaybackMonthDate);
nTotalInterest = loanIssue.LoanAmount * loanIssue.InterestRate/100;
nBalance = loanIssue.LoanAmount + nTotalInterest;//+ (loanIssue.LoanAmount * loanIssue.InterestRate /100);
nInstPrinciple = loanIssue.LoanAmount / loanIssue.NoOfInstallments;
nInterest = nTotalInterest/ loanIssue.NoOfInstallments;
//nInstPrinciple = GlobalFunctions.Round(nInstPrinciple);
for (int i = 1; i <= loanIssue.NoOfInstallments; i++)
{
oSchedule = new LoanSchedule();
if (i == 1)
{
oSchedule.CalculatedDate = dMonth;
}
else
{
dMonth = dMonth.AddMonths(1);
oSchedule.CalculatedDate = dMonth;
}
// nTotalInterest += nInterest;
oSchedule.ScheduleNo = i;
//if (i == loanIssue.NoOfInstallments && loanIssue.loan.calculationType != EnumLoanFraction.RemoveFraction)
// nInstPrinciple = nBalance;
oSchedule.InstallmentInterest = nInterest;
oSchedule.InstallmentPrincipal = nInstPrinciple;
oSchedule.OpeningBalance = nBalance;
oSchedule.ActualInterest = nInterest;
oSchedule.DueInstallmentDate = GlobalFunctions.LastDateOfMonth(dMonth);
oSchedule.Days = nDays;
nBalance = nBalance - (oSchedule.InstallmentPrincipal + nInterest);
if (nBalance < 0) nBalance = 0;
oSchedule.ClosingBalance = nBalance;
loanIssue.Schedules.Add(oSchedule);
}
if (loanIssue.loan.calculationType != EnumLoanFraction.WithFraction)
{
AdjustSchedele(loanIssue, loanIssue.loan.calculationType);
}
//dMonth = dMonth.AddMonths(1);
//oSchedule = new LoanSchedule();
//oSchedule.CalculatedDate = dMonth;
//nTotalInterest += nInterest;
//oSchedule.ScheduleNo = loanIssue.Schedules.Count + 1;
//oSchedule.InstallmentInterest = nInstPrinciple;
//oSchedule.InstallmentPrincipal = 0;
//oSchedule.OpeningBalance = 0;
//oSchedule.ActualInterest = 0;
//oSchedule.DueInstallmentDate = GlobalFunctions.LastDateOfMonth(dMonth);
//oSchedule.Days = nDays;
//nBalance = nBalance - (oSchedule.InstallmentPrincipal);
//oSchedule.ClosingBalance = 0;
//loanIssue.Schedules.Add(oSchedule);
//if(loanIssue.Schedules.Count >13)
//{
// dMonth = dMonth.AddMonths(1);
// oSchedule = new LoanSchedule();
// oSchedule.CalculatedDate = dMonth;
// nTotalInterest += nInterest;
// oSchedule.ScheduleNo = loanIssue.Schedules.Count + 1;
// oSchedule.InstallmentInterest = nInstPrinciple;
// oSchedule.InstallmentPrincipal =0 ;
// oSchedule.OpeningBalance = 0;
// oSchedule.ActualInterest = 0;
// oSchedule.DueInstallmentDate = GlobalFunctions.LastDateOfMonth(dMonth);
// oSchedule.Days = nDays;
// nBalance = nBalance - (oSchedule.InstallmentPrincipal);
// oSchedule.ClosingBalance = 0;
// loanIssue.Schedules.Add(oSchedule);
//}
return loanIssue.Schedules;
}
private static bool IsEven(int nperiodic)
{
return nperiodic % 2 == 0;
}
private static bool IsOdd(int nperiodic)
{
return nperiodic % 2 != 0;
}
private void AdjustSchedele(LoanIssue oloanIssue, EnumLoanFraction fractionate)
{
if (fractionate == EnumLoanFraction.WithFraction) return;
double nFraction = 0.0;
foreach (LoanSchedule item in oloanIssue.Schedules)
{
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.Schedules.Count - 1; i++)
{
if (oloanIssue.Schedules[i - 1].Installment != oloanIssue.Schedules[i].Installment)
{
nequal = oloanIssue.Schedules[i - 1].Installment - oloanIssue.Schedules[i].Installment;
oloanIssue.Schedules[i].InstallmentPrincipal = oloanIssue.Schedules[i].InstallmentPrincipal + nequal;
nFraction = nFraction - nequal;
}
}
nFraction = GlobalFunctions.Round(nFraction);
if (nFraction != 0 && oloanIssue.Schedules.Count > 0)
{
if (fractionate == EnumLoanFraction.FractionFirstMonth)
{
if (oloanIssue.InterestRate == 0)
oloanIssue.Schedules[0].InstallmentPrincipal += nFraction;
else oloanIssue.Schedules[0].InstallmentInterest += nFraction;
}
else if (fractionate == EnumLoanFraction.FractionLastMonth)
{
if (oloanIssue.InterestRate == 0)
oloanIssue.Schedules[oloanIssue.Schedules.Count - 1].InstallmentPrincipal += nFraction;
else oloanIssue.Schedules[oloanIssue.Schedules.Count - 1].InstallmentInterest += nFraction;
}
for (int i = 0; i < oloanIssue.Schedules.Count; i++)
{
// if (i == 9) return;
oloanIssue.Schedules[i].ClosingBalance = oloanIssue.Schedules[i].OpeningBalance - (oloanIssue.Schedules[i].InstallmentPrincipal + oloanIssue.Schedules[i].InstallmentInterest);
if (i < oloanIssue.Schedules.Count - 1)
{
oloanIssue.Schedules[i + 1].OpeningBalance = oloanIssue.Schedules[i].ClosingBalance;
}
}
}
foreach (LoanSchedule item in oloanIssue.Schedules)
{
item.OpeningBalance = Math.Round(item.OpeningBalance, 2);
item.Installment = item.InstallmentPrincipal + item.InstallmentInterest;
item.ClosingBalance = Math.Round( item.OpeningBalance - (item.InstallmentPrincipal + item.InstallmentInterest),2);
}
}
//public static LoanIssue GetByLoanAndEmployeeIdAndLoanNumber(ID LoanId, ID EmployeeId, string loanNumber)
//{
// return LoanIssue.Service.GetByLoanAndEmployeeIdAndLoanNumber(LoanId, EmployeeId, loanNumber);
//}
//public static List<LoanIssue> GetByEmployeeIdAndLoanNumber(ID id, string loanNumber)
//{
// List<LoanIssue> allIssuedLoans = new List<LoanIssue>();
// allIssuedLoans = LoanIssue.Service.GetByEmployeeIdAndLoanNumber(id, loanNumber);
// return allIssuedLoans;
//}
//public static LoanIssue GetExistingAllLoan(ID nLoanID, ID nEmpID)
//{
// LoanIssue oLoanIssue = null;
// #region Cache Header
// oLoanIssue = (LoanIssue)_cache["GetExistingLoan", nLoanID, nEmpID];
// if (oLoanIssue != null)
// return oLoanIssue;
// #endregion
// oLoanIssue = LoanIssue.Service.GetExistingAllLoan(nLoanID, nEmpID);
// #region Cache Footer
// _cache.Add(oLoanIssue, "GetExistingLoan", nLoanID, nEmpID);
// #endregion
// return oLoanIssue;
//}
//#endregion
//public string WFDesciption()
//{
// string sDescription;
// sDescription = this.ObjectDescription;
// return sDescription;
//}
//public int GetStatusbyWf(enumwfStatus wfstatus)
//{
// return (int)this.WFStatus;
//}
//public string ObjectDescription
//{
// get
// {
// string str = "";
// str = " Employee (" + this.Employee.EmployeeNo + ")" + this.Employee.Name
// + " applied " + this.LoanObj.Name + " Loan. ";
// return str;
// }
//}
//public ID SetupID
//{
// get { return ID.FromInteger(2); }
//}
//public ID ObjectID
//{
// get { return this.ID; }
//}
//public List<PendingJobDetail> GetDetails()
//{
// List<PendingJobDetail> opJobsDetail = new List<PendingJobDetail>();
// opJobsDetail.Add(new PendingJobDetail() { Type = "Applier:", Value = this.Employee.Name });
// opJobsDetail.Add(new PendingJobDetail() { Type = "Loan Type:", Value = this.LoanObj.Name });
// opJobsDetail.Add(new PendingJobDetail() { Type = "Loan Amount:", Value = this.LoanAmount.ToString("#,###") });
// opJobsDetail.Add(new PendingJobDetail() { Type = "Issue Date:", Value = this.IssueDate.ToString("dd MMM yyyy") });
// opJobsDetail.Add(new PendingJobDetail() { Type = "No of Installments:", Value = this.NoOfInstallments.ToString() });
// opJobsDetail.Add(new PendingJobDetail() { Type = "Installment Principle:", Value = this.InstallmentPrincipal.ToString("#,###") });
// opJobsDetail.Add(new PendingJobDetail() { Type = "Interest Rate:", Value = this.InterestRate.ToString("#,###") });
// return opJobsDetail;
//}
//public void PrepareManualSchedule(DataTable dt, EnumLoanFraction _Fractionate)
//{
// LoanSchedule oSchedule = null;
// this.Schedules.Clear();
// int i = 0;
// foreach (DataRow dr in dt.Rows)
// {
// oSchedule = new LoanSchedule();
// oSchedule.CalculatedDate = Ease.CoreV35.Utility.Global.DateFunctions.PayrollLastDateOfMonth(Convert.ToDateTime(dr["Date"]));
// oSchedule.ScheduleNo = i + 1;
// oSchedule.InstallmentPrincipal = Convert.ToDouble(dr["Principal"]);
// oSchedule.OpeningBalance = Convert.ToDouble(dr["Opening"]);
// oSchedule.ActualInterest = 0;
// oSchedule.DueInstallmentDate = oSchedule.CalculatedDate;
// oSchedule.InstallmentInterest = 0;
// oSchedule.ClosingBalance = Convert.ToDouble(dr["Closing"]);
// this.Schedules.Add(oSchedule);
// }
// if (_Fractionate != EnumLoanFraction.WithFraction)
// {
// AdjustSchedele(_Fractionate);
// }
//}
}
#endregion
#region ILoanIssue Service
public interface ILoanIssueService
{
LoanIssue Get(int id);
List<LoanIssue> Get();
DataSet GetIssuedLoanIDs(int EmpID);
DataSet RemainingLoanforPayslip(string EmpId);
LoanIssue GetExistingLoan(int nLoanID, int nEmpID);
LoanIssue GetExistingAllLoan(int nLoanID, int nEmpID);
List<LoanIssue> GetExistingLoan(int nEmpID);
List<LoanIssue> GetExistingLoan(string nEmpID);
List<LoanIssue> GetByDueInstallmentDate(DateTime dateTime, string empID);
List<LoanIssue> Get(DateTime dateTime);
List<LoanIssue> Get(DateTime dateTime, DateTime dateTime2);
List<LoanIssue> Get(string employeeNo);
List<LoanIssue> GetByEmpIDs(string employeeID);
List<LoanIssue> GetByLoanID(int nLoanID);
List<LoanIssue> GetByEmployeeIdAndLoanNumber(int id, string loanNumber);
LoanIssue GetByLoanAndEmployeeIdAndLoanNumber(int LoanId, int EmployeeId, string loanNumber);
DataSet GetIssuedLoans(int EmpID);
DataTable GetLoanSettlementData(int empId, int loanId);
public DataTable GetLoanListByEmployee(int empId);
List<LoanIssue> GetByDueInstallmentDate(DateTime dateTime, int payrollTypeID);
List<LoanIssue> GetUnPaidLoanWithSchedule(DateTime dateTime);
DataSet GetDueLoans(string sEmpID, DateTime todate);
DataSet GetMonthWiseLoans(DateTime todate, string locationIds);
void UpdateAvgIntStatus(List<LoanIssue> list);
int Save(LoanIssue item);
void SaveAll(List<LoanIssue> oItems);
int Save2(LoanIssue item);
void Delete(int id);
}
#endregion
}