using System; using System.Collections.Generic; using System.Linq; using System.Text; using Ease.CoreV35; using Ease.CoreV35.Model; using Ease.CoreV35.Caching; using System.Data.Linq.Mapping; using Microsoft.VisualBasic; using System.Data; namespace Payroll.BO { #region LoanIssue [Serializable] public class LoanIssue : AuditTrailBase, IworkflowInterface { #region Cache Store private static Cache _cache = new Cache(typeof(LoanIssue)); #endregion #region Constructor private ObjectsTemplate _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; } #endregion #region Properties #region LoanID : ID private ID _loanID; public ID LoanID { get { return _loanID; } set { base.OnPropertyChange("LoanID", _loanID, value); _loanID = value; } } #endregion #region loanNo : string private string _loanNo; public string LoanNo { get { return _loanNo; } set { base.OnPropertyChange("loanNo", _loanNo, value); _loanNo = value; } } #endregion #region EmployeeID : ID private ID _employeeID; public ID EmployeeID { get { return _employeeID; } set { base.OnPropertyChange("EmployeeID", _employeeID, value); _employeeID = value; } } #endregion #region loanAmount : double private double _loanAmount; public double LoanAmount { get { return _loanAmount; } set { base.OnPropertyChange("loanAmount", _loanAmount, value); _loanAmount = value; } } #endregion #region issueDate : DateTime private DateTime _issueDate; public DateTime IssueDate { get { return _issueDate; } set { base.OnPropertyChange("issueDate", _issueDate, value); _issueDate = value; } } #endregion #region noOfInstallments : int private int _noOfInstallments; public int NoOfInstallments { get { return _noOfInstallments; } set { base.OnPropertyChange("noOfInstallments", _noOfInstallments, value); _noOfInstallments = value; } } #endregion #region installmentPrincipal : double private double _installmentPrincipal; public double InstallmentPrincipal { get { return _installmentPrincipal; } set { base.OnPropertyChange("installmentPrincipal", _installmentPrincipal, value); _installmentPrincipal = value; } } #endregion #region interestRate : double private double _interestRate; public double InterestRate { get { return _interestRate; } set { base.OnPropertyChange("interestRate", _interestRate, value); _interestRate = value; } } #endregion #region startPaybackMonthDate : DateTime private DateTime _startPaybackMonthDate; public DateTime StartPaybackMonthDate { get { return _startPaybackMonthDate; } set { base.OnPropertyChange("startPaybackMonthDate", _startPaybackMonthDate, value); _startPaybackMonthDate = value; } } #endregion #region remainInstallment : double private double _remainInstallment; public double RemainInstallment { get { return _remainInstallment; } set { base.OnPropertyChange("remainInstallment", _remainInstallment, value); _remainInstallment = value; } } #endregion #region remainInterest : double private double _remainInterest; public double RemainInterest { get { return _remainInterest; } set { base.OnPropertyChange("remainInterest", _remainInterest, value); _remainInterest = value; } } #endregion #region remNoInstallment : double private double _remNoInstallment; public double RemNoInstallment { get { return _remNoInstallment; } set { base.OnPropertyChange("remNoInstallment", _remNoInstallment, value); _remNoInstallment = value; } } #endregion #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 #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 #region Schedule : Schedule private ObjectsTemplate _Schedules; public ObjectsTemplate Schedules { get { if (_Schedules == null && !ID.IsUnassigned && ID.Integer > 0) { _Schedules = LoanSchedule.GetByIssueID(this.ID); } if (_Schedules == null) { _Schedules = new ObjectsTemplate(); } return _Schedules; } } #endregion #region AverageInterestRate : bool private bool _AverageInterestRate; public bool AverageInterestRate { get { return _AverageInterestRate; } set { base.OnPropertyChange("AverageInterestRate", _AverageInterestRate, value); _AverageInterestRate = value; } } #endregion private enumwfStatus _wfStatus; public enumwfStatus WFStatus { get { return _wfStatus; } set { _wfStatus = value; } } private ObjectsTemplate _issueDocks; public ObjectsTemplate 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(typeof(ILoanIssueService)); } } #endregion #endregion #region Functions public ObjectsTemplate MonthlySchedules(DateTime dateTime) { if (_MonthlySchedules == null && !this.ID.IsUnassigned && this.ID.Integer > 0) { _MonthlySchedules = LoanSchedule.GetByIssueIDAndMonth(this.ID, dateTime); } return _MonthlySchedules; } public static LoanIssue Get(ID nID) { LoanIssue oLoanIssue = null; #region Cache Header oLoanIssue = (LoanIssue)_cache["Get", nID]; if (oLoanIssue != null) return oLoanIssue; #endregion oLoanIssue = LoanIssue.Service.Get(nID); #region Cache Footer _cache.Add(oLoanIssue, "Get", nID); #endregion return oLoanIssue; } public static LoanIssue GetExistingLoan(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.GetExistingLoan(nLoanID, nEmpID); #region Cache Footer _cache.Add(oLoanIssue, "GetExistingLoan", nLoanID, nEmpID); #endregion return oLoanIssue; } public static ObjectsTemplate GetExistingLoan(ID nEmpID) { #region Cache Header ObjectsTemplate loanIssues = _cache["GetExistingLoan", nEmpID] as ObjectsTemplate; if (loanIssues != null) return loanIssues; #endregion try { loanIssues = Service.GetExistingLoan(nEmpID); } catch (ServiceException e) { throw new Exception(e.Message, e); } #region Cache Footer _cache.Add(loanIssues, "GetExistingLoan", nEmpID); #endregion return loanIssues; } public static ObjectsTemplate GetAllExistingLoan(string LoanIDs) { ObjectsTemplate loanIssues = null; try { loanIssues = Service.GetAllExistingLoan(LoanIDs); } catch (ServiceException e) { throw new Exception(e.Message, e); } return loanIssues; } public static ObjectsTemplate GetAllExistingLoan(int EmpID, string sLaonIDs) { ObjectsTemplate loanIssues = null; try { loanIssues = Service.GetAllExistingLoan(EmpID, sLaonIDs); } catch (Exception e) { throw new Exception(e.Message, e); } return loanIssues; } public static ObjectsTemplate GetAllExistingLoan(string LoanIDs, DateTime month) { ObjectsTemplate loanIssues = null; try { loanIssues = Service.GetAllExistingLoan(LoanIDs, month); } catch (ServiceException e) { throw new Exception(e.Message, e); } return loanIssues; } public static ObjectsTemplate GetByDueInstallmentDate(DateTime dateTime, string empID) { #region Cache Header ObjectsTemplate loanIssues = _cache["Get", dateTime, empID] as ObjectsTemplate; if (loanIssues != null) return loanIssues; #endregion try { loanIssues = Service.GetByDueInstallmentDate(dateTime, empID); } catch (ServiceException e) { throw new Exception(e.Message, e); } #region Cache Footer _cache.Add(loanIssues, "Get", dateTime, empID); #endregion return loanIssues; } public static ObjectsTemplate GetByDueInstallmentDate(DateTime dateTime) { #region Cache Header ObjectsTemplate loanIssues = _cache["Get", dateTime] as ObjectsTemplate; if (loanIssues != null) return loanIssues; #endregion try { loanIssues = Service.GetByDueInstallmentDate(dateTime); } catch (ServiceException e) { throw new Exception(e.Message, e); } #region Cache Footer _cache.Add(loanIssues, "Get", dateTime); #endregion return loanIssues; } public static ObjectsTemplate Get(DateTime dateTime) { #region Cache Header ObjectsTemplate loanIssues = _cache["Get", dateTime] as ObjectsTemplate; if (loanIssues != null) return loanIssues; #endregion try { loanIssues = Service.Get(dateTime); } catch (ServiceException e) { throw new Exception(e.Message, e); } #region Cache Footer _cache.Add(loanIssues, "Get", dateTime); #endregion return loanIssues; } public static ObjectsTemplate Get(DateTime dateTime, DateTime dateTime2) { #region Cache Header ObjectsTemplate loanIssues = _cache["Get", dateTime, dateTime2] as ObjectsTemplate; if (loanIssues != null) return loanIssues; #endregion try { loanIssues = Service.Get(dateTime, dateTime2); } catch (ServiceException e) { throw new Exception(e.Message, e); } #region Cache Footer _cache.Add(loanIssues, "Get", dateTime, dateTime2); #endregion return loanIssues; } public static ObjectsTemplate Get(string employeeNo) { #region Cache Header ObjectsTemplate loanIssues = _cache["Get", employeeNo] as ObjectsTemplate; if (loanIssues != null) return loanIssues; #endregion try { loanIssues = Service.Get(employeeNo); } catch (ServiceException e) { throw new Exception(e.Message, e); } #region Cache Footer _cache.Add(loanIssues, "Get", employeeNo); #endregion return loanIssues; } public static ObjectsTemplate GetByLoanID(ID nLoanID) { #region Cache Header ObjectsTemplate loanIssues = _cache["Get", nLoanID] as ObjectsTemplate; if (loanIssues != null) return loanIssues; #endregion try { loanIssues = Service.GetByLoanID(nLoanID); } catch (ServiceException e) { throw new Exception(e.Message, e); } #region Cache Footer _cache.Add(loanIssues, "Get", nLoanID); #endregion return loanIssues; } public static ObjectsTemplate Get() { #region Cache Header ObjectsTemplate loanIssues = _cache["Get"] as ObjectsTemplate; if (loanIssues != null) return loanIssues; #endregion try { loanIssues = Service.Get(); } catch (ServiceException e) { throw new Exception(e.Message, e); } #region Cache Footer _cache.Add(loanIssues, "Get"); #endregion return loanIssues; } public static DataSet GetIssuedLoanIDs(int EmpID) { DataSet ds = null; try { ds = Service.GetIssuedLoanIDs(EmpID); } catch (Exception e) { throw new Exception(e.Message, e); } return ds; } public static DataSet GetIssuedLoans(int EmpID) { DataSet ds = null; try { ds = Service.GetIssuedLoans(EmpID); } catch (Exception e) { throw new Exception(e.Message, e); } return ds; } public bool IsPaymentStarted() { bool paymentStarted = false; foreach (LoanSchedule schedule in this.Schedules) { if (schedule.PaymentDate != null) { paymentStarted = true; break; } } return paymentStarted; } public ID Save() { base.SetAuditTrailProperties(); if (!this.ID.IsUnassigned) { if (this.IssueDocks != null) { foreach (LoanEmployeeDoc item in this.IssueDocks) { item.CreatedBy = this.CreatedBy; item.CreatedDate = this.CreatedDate; } } } return LoanIssue.Service.Save(this); } public ID Save2() { base.SetAuditTrailProperties(); if (!this.ID.IsUnassigned) { if (this.IssueDocks != null) { foreach (LoanEmployeeDoc item in this.IssueDocks) { item.CreatedBy = this.CreatedBy; item.CreatedDate = this.CreatedDate; } } } return LoanIssue.Service.Save2(this); } public void Delete() { LoanIssue.Service.Delete(ID); } public void UpdateAvgIntStatus(ObjectsTemplate lis) { LoanIssue.Service.UpdateAvgIntStatus(lis); } public void PrepareEMISchedule(DateTime dMonth, EnumLoanFraction fractionate) { 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 = Ease.CoreV35.Utility.Global.DateFunctions.LastDateOfMonth(dMonth); nTotalInterest = 0; for (int i = 0; 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 = this.LoanAmount + nTotalInterest; nEffectiveBalance = 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 (i == this.NoOfInstallments - 1) { oSchedule.InstallmentPrincipal = this.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; this.Schedules.Add(oSchedule); dMonth = dMonth.AddMonths(1); dMonth = Ease.CoreV35.Utility.Global.DateFunctions.LastDateOfMonth(dMonth); } if (fractionate != EnumLoanFraction.WithFraction) { AdjustSchedele(fractionate); } } public void PrepareEMISchedule(DateTime dMonth, EnumLoanFraction fractionate, int nSequenceNo) { 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 = Ease.CoreV35.Utility.Global.DateFunctions.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 = this.LoanAmount + nTotalInterest; 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 = 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; this.Schedules.Add(oSchedule); dMonth = dMonth.AddMonths(1); dMonth = Ease.CoreV35.Utility.Global.DateFunctions.LastDateOfMonth(dMonth); } if (fractionate != EnumLoanFraction.WithFraction) { AdjustSchedele(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 = Ease.CoreV35.Utility.Global.DateFunctions.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 = Ease.CoreV35.Utility.Global.DateFunctions.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 = Ease.CoreV35.Utility.Global.DateFunctions.LastDateOfMonth(dMonth); this.Schedules.Add(oSchedule); } if (fractionate != EnumLoanFraction.WithFraction) { AdjustSchedele(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 = Ease.CoreV35.Utility.Global.DateFunctions.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 = Ease.CoreV35.Utility.Global.DateFunctions.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 = Ease.CoreV35.Utility.Global.DateFunctions.LastDateOfMonth(dMonth); this.Schedules.Add(oSchedule); } if (fractionate != EnumLoanFraction.WithFraction) { AdjustSchedele(fractionate); } } public void PrepareSIDSchedule(DateTime dMonth, EnumLoanFraction fractionate) { LoanSchedule oSchedule = null; this.Schedules.Clear(); int i = 0; double nInterest = 0.0; double nBalance = 0.0; dMonth = Ease.CoreV35.Utility.Global.DateFunctions.LastDateOfMonth(dMonth); nInterest = this.LoanAmount * this.InterestRate / 1200 * this.NoOfInstallments; nBalance = this.LoanAmount + nInterest; while (Math.Round(nBalance) > 0) { oSchedule = new LoanSchedule(); oSchedule.CalculatedDate = dMonth; oSchedule.ScheduleNo = ++i; oSchedule.InstallmentInterest = nInterest / this.NoOfInstallments; oSchedule.InstallmentPrincipal = this.LoanAmount / this.NoOfInstallments; oSchedule.OpeningBalance = nBalance; oSchedule.ActualInterest = oSchedule.InstallmentInterest; oSchedule.DueInstallmentDate = dMonth; nBalance = nBalance - (oSchedule.InstallmentPrincipal + oSchedule.InstallmentInterest); oSchedule.ClosingBalance = nBalance; this.Schedules.Add(oSchedule); dMonth = dMonth.AddMonths(1); dMonth = Ease.CoreV35.Utility.Global.DateFunctions.LastDateOfMonth(dMonth); } if (fractionate != EnumLoanFraction.WithFraction) { AdjustSchedele(fractionate); } } public void PrepareSIDSchedule(DateTime dMonth, EnumLoanFraction fractionate, int nSequenceNo) { LoanSchedule oSchedule = null; this.Schedules.Clear(); int i = nSequenceNo - 1; double nInterest = 0.0; double nBalance = 0.0; dMonth = Ease.CoreV35.Utility.Global.DateFunctions.LastDateOfMonth(dMonth); nInterest = this.LoanAmount * this.InterestRate / 1200 * this.NoOfInstallments; nBalance = this.LoanAmount + nInterest; while (Math.Round(nBalance) > 0) { oSchedule = new LoanSchedule(); oSchedule.CalculatedDate = dMonth; oSchedule.ScheduleNo = ++i; oSchedule.InstallmentInterest = nInterest / this.NoOfInstallments; oSchedule.InstallmentPrincipal = this.LoanAmount / this.NoOfInstallments; oSchedule.OpeningBalance = nBalance; oSchedule.ActualInterest = oSchedule.InstallmentInterest; oSchedule.DueInstallmentDate = dMonth; nBalance = nBalance - (oSchedule.InstallmentPrincipal + oSchedule.InstallmentInterest); oSchedule.ClosingBalance = nBalance; this.Schedules.Add(oSchedule); dMonth = dMonth.AddMonths(1); dMonth = Ease.CoreV35.Utility.Global.DateFunctions.LastDateOfMonth(dMonth); } if (fractionate != EnumLoanFraction.WithFraction) { AdjustSchedele(fractionate); } } public void PrepareBankSchedule(DateTime dMonth, EnumLoanFraction fractionate) { LoanSchedule oSchedule = null; double nInterest = 0.0, nBalance = 0.0, nTotalInterest = 0.0, nInstPrinciple = 0.0, nAvgInterest = 0.0; int nDays = 0; this.Schedules.Clear(); DateTime dNextMonth = new DateTime(this.StartPaybackMonthDate.Year, this.StartPaybackMonthDate.Month, 25); //int CheckDay = Ease.CoreV35.Utility.Global.DateFunctions.DateDiff("d", dMonth, Ease.CoreV35.Utility.Global.DateFunctions.LastDateOfMonth(dMonth)); //if (dMonth.Day > 25) //{ // dNextMonth = Ease.CoreV35.Utility.Global.DateFunctions.DateAdd("m", 1, dNextMonth);//this.StartPaybackMonthDate.AddMonths(1); //} //dMonth = Ease.CoreV35.Utility.Global.DateFunctions.LastDateOfMonth(dMonth); nInstPrinciple = GlobalFunctions.Round(this.LoanAmount / this.NoOfInstallments); nBalance = this.LoanAmount; for (int i = 1; i <= this.NoOfInstallments; i++) { oSchedule = new LoanSchedule(); if (i == 1) { TimeSpan t = dNextMonth.Subtract(dNextMonth.TimeOfDay) - this.IssueDate.Subtract(this.IssueDate.TimeOfDay); nDays = t.Days; //nDays = Ease.CoreV35.Utility.Global.DateFunctions.DateDiff("d", this.IssueDate, dNextMonth); nInterest = GlobalFunctions.Round((nDays * (this.LoanAmount * this.InterestRate)) / 36500); nBalance = this.LoanAmount - nInstPrinciple; //GlobalFunctions.Round(((this.LoanAmount * this.InterestRate))/100 / 12); oSchedule.CalculatedDate = dNextMonth; } else { DateTime dAddNextMonth = dNextMonth.AddMonths(1); TimeSpan t = dAddNextMonth.Subtract(dAddNextMonth.TimeOfDay) - dNextMonth.Subtract(dNextMonth.TimeOfDay); nDays = t.Days; //nDays = Ease.CoreV35.Utility.Global.DateFunctions.DateDiff("d", dNextMonth,dAddNextMonth); nInterest = GlobalFunctions.Round((nDays * (nBalance * this.InterestRate)) / 36500); dNextMonth = dNextMonth.AddMonths(1); nBalance = nBalance - nInstPrinciple; oSchedule.CalculatedDate = dNextMonth; } nTotalInterest += nInterest; oSchedule.ScheduleNo = i; //if (i ==this.NoOfInstallments) // nInstPrinciple = nBalance ; oSchedule.ActualInterest = nInterest; oSchedule.InstallmentPrincipal = nInstPrinciple; oSchedule.OpeningBalance = nBalance; oSchedule.InstallmentInterest = nInterest; oSchedule.DueInstallmentDate = Ease.CoreV35.Utility.Global.DateFunctions.LastDateOfMonth(dNextMonth); oSchedule.Days = nDays; oSchedule.ClosingBalance = nBalance; oSchedule.InterestRate = this.InterestRate; this.Schedules.Add(oSchedule); } nAvgInterest = nTotalInterest / this.NoOfInstallments; foreach (LoanSchedule oItem in this.Schedules) { oItem.InstallmentInterest = nAvgInterest; } //if (fractionate != EnumLoanFraction.WithFraction) //{ // AdjustSchedele(fractionate); //} } public void PrepareBankSchedule(DateTime dMonth, EnumLoanFraction fractionate, int nSequenceNo, int nRemainingInstallment, ObjectsTemplate oItems) { string sFormat = "0"; LoanSchedule oSchedule = null; double nInterest = 0.0, nBalance = 0.0, nTotalInterest = 0.0, nInstPrinciple = 0.0, nAvgInterest = 0.0; int nDays = 0; this.Schedules.Clear(); //this.NoOfInstallments = nRemainingInstallment; DateTime dNextMonth = new DateTime(this.StartPaybackMonthDate.Year, this.StartPaybackMonthDate.Month, 25); if (dMonth.Day > 25) { dNextMonth = Ease.CoreV35.Utility.Global.DateFunctions.DateAdd("m", 1, dNextMonth);//this.StartPaybackMonthDate.AddMonths(1); } nInstPrinciple = GlobalFunctions.Round(this.LoanAmount / nRemainingInstallment); nBalance = this.LoanAmount; for (int i = nSequenceNo; i <= this.NoOfInstallments; i++) { oSchedule = new LoanSchedule(); if (i == 1) { TimeSpan t = dNextMonth.Subtract(dNextMonth.TimeOfDay) - this.IssueDate.Subtract(this.IssueDate.TimeOfDay); nDays = t.Days; nInterest = GlobalFunctions.Round((nDays * (this.LoanAmount * this.InterestRate)) / 36500); nBalance = this.LoanAmount - nInstPrinciple; oSchedule.CalculatedDate = dNextMonth; } else { DateTime dAddNextMonth = dNextMonth.AddMonths(1); TimeSpan t = dAddNextMonth.Subtract(dAddNextMonth.TimeOfDay) - dNextMonth.Subtract(dNextMonth.TimeOfDay); nDays = t.Days; nInterest = GlobalFunctions.Round((nDays * (nBalance * this.InterestRate)) / 36500); dNextMonth = dNextMonth.AddMonths(1); nBalance = nBalance - nInstPrinciple; oSchedule.CalculatedDate = dNextMonth; } nTotalInterest += nInterest; oSchedule.ScheduleNo = i; oSchedule.ActualInterest = Math.Round(nInterest, 0); oSchedule.InstallmentPrincipal = Math.Round(nInstPrinciple); oSchedule.OpeningBalance = Math.Round(nBalance); oSchedule.InstallmentInterest = Math.Round(nInterest); oSchedule.DueInstallmentDate = Ease.CoreV35.Utility.Global.DateFunctions.LastDateOfMonth(dNextMonth); oSchedule.Days = nDays; oSchedule.ClosingBalance = nBalance; oSchedule.InterestRate = this.InterestRate; this.Schedules.Add(oSchedule); } ObjectsTemplate oNewSchedules = new ObjectsTemplate(); foreach (LoanSchedule oItem in this.Schedules) { LoanSchedule item = oItems.Find(delegate(LoanSchedule ls) { return oItem.CalculatedDate.Month == ls.CalculatedDate.Month && oItem.CalculatedDate.Year == ls.CalculatedDate.Year; }); if (item == null) oNewSchedules.Add(oItem); } this.Schedules.Clear(); foreach (LoanSchedule oItem in oNewSchedules) { this.Schedules.Add(oItem); } nAvgInterest = nTotalInterest / nRemainingInstallment; //Need to change foreach (LoanSchedule oItem in this.Schedules) { oItem.InstallmentInterest = nAvgInterest; } } public void PrepareHalfYearlySchedule(DateTime dMonth, EnumLoanFraction fractionate) { LoanSchedule oSchedule = null; double nInterest = 0.0, nBalance = 0.0, nTotalInterest = 0.0, nInstPrinciple = 0.0, nAvgInterest = 0.0, nMainAvgInterest = 0.0; int nDays = 0; this.Schedules.Clear(); DateTime dNextMonth = new DateTime(this.StartPaybackMonthDate.Year, this.StartPaybackMonthDate.Month, 25); //if (dMonth.Day > 25) //{ // dNextMonth = Ease.CoreV35.Utility.Global.DateFunctions.DateAdd("m", 1, dNextMonth); //} nInstPrinciple = GlobalFunctions.Round(this.LoanAmount / this.NoOfInstallments); nBalance = this.LoanAmount; int nInstallmentCount = 1; double nPartialInterest = 0; for (int i = 1; i <= this.NoOfInstallments; i++) { oSchedule = new LoanSchedule(); if (i == 1) { TimeSpan t = dNextMonth.Subtract(dNextMonth.TimeOfDay) - this.IssueDate.Subtract(this.IssueDate.TimeOfDay); nDays = t.Days; //nInterest = GlobalFunctions.Round((nDays * (this.LoanAmount * this.InterestRate)) / 36500); nBalance = this.LoanAmount - nInstPrinciple; oSchedule.CalculatedDate = dNextMonth; } else { DateTime dAddNextMonth = dNextMonth.AddMonths(1); TimeSpan t = dAddNextMonth.Subtract(dAddNextMonth.TimeOfDay) - dNextMonth.Subtract(dNextMonth.TimeOfDay); nDays = t.Days; //nInterest = GlobalFunctions.Round((nDays * (nBalance * this.InterestRate)) / 36500); dNextMonth = dNextMonth.AddMonths(1); if (i == this.NoOfInstallments && nBalance < nInstPrinciple) { nInstPrinciple = nBalance; nBalance = nInstPrinciple - nBalance; } else if (i == this.NoOfInstallments && nBalance > nInstPrinciple) { nInstPrinciple = nBalance; nBalance = 0; } else nBalance = nBalance - nInstPrinciple; oSchedule.CalculatedDate = dNextMonth; } //nTotalInterest += nInterest; // nPartialInterest += nInterest; oSchedule.ScheduleNo = i; oSchedule.InstallmentPrincipal = nInstPrinciple; oSchedule.OpeningBalance = nBalance; //oSchedule.ActualInterest = nInterest; //if (nInstallmentCount % 6 == 0 || nInstallmentCount == this.NoOfInstallments) //{ // oSchedule.InstallmentInterest = nPartialInterest; // nPartialInterest = 0; //} //else //{ oSchedule.InstallmentInterest = 0; //} oSchedule.DueInstallmentDate = Ease.CoreV35.Utility.Global.DateFunctions.LastDateOfMonth(dNextMonth); oSchedule.Days = nDays; oSchedule.ClosingBalance = nBalance; oSchedule.InterestRate = this.InterestRate; this.Schedules.Add(oSchedule); nInstallmentCount++; } nInstallmentCount = 1; nMainAvgInterest = (this.LoanAmount * this.InterestRate) / 100; nAvgInterest = nMainAvgInterest / 2; //double nInterestPeriod = this.NoOfInstallments / 6; //if (this.NoOfInstallments % 6 != 0) //{ // nInterestPeriod += 1; //} //nAvgInterest = nAvgInterest / nInterestPeriod; int nperiodic = 1; foreach (LoanSchedule oItem in this.Schedules) { if (nInstallmentCount % 6 == 0 || nInstallmentCount == this.NoOfInstallments) { if (this.NoOfInstallments <= 6) { if (nInstallmentCount == this.NoOfInstallments) { oItem.InstallmentInterest = nMainAvgInterest; break; } } else if((nperiodic >= 3 && (this.NoOfInstallments - nInstallmentCount==0))) { if(IsEven(nperiodic)) { oItem.InstallmentInterest = nAvgInterest; break; } else if (IsOdd(nperiodic)) { oItem.InstallmentInterest = nMainAvgInterest; break; } } else { oItem.InstallmentInterest = nAvgInterest; nperiodic++; } } nInstallmentCount++; } } private static bool IsEven(int nperiodic) { return nperiodic % 2 == 0; } private static bool IsOdd(int nperiodic) { return nperiodic % 2 != 0; } private void AdjustSchedele(EnumLoanFraction fractionate) { if (fractionate == EnumLoanFraction.WithFraction) return; double nFraction = 0.0; foreach (LoanSchedule item in this.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 <= this.Schedules.Count - 1; i++) { if (this.Schedules[i - 1].Installment != this.Schedules[i].Installment) { nequal = this.Schedules[i - 1].Installment - this.Schedules[i].Installment; this.Schedules[i].InstallmentPrincipal = this.Schedules[i].InstallmentPrincipal + nequal; nFraction = nFraction - nequal; } } nFraction = GlobalFunctions.Round(nFraction); if (nFraction != 0 && this.Schedules.Count > 0) { if (fractionate == EnumLoanFraction.FractionFirstMonth) { if (this.InterestRate == 0) this.Schedules[0].InstallmentPrincipal += nFraction; else this.Schedules[0].InstallmentInterest += nFraction; } else { if (this.InterestRate == 0) this.Schedules[this.Schedules.Count - 1].InstallmentPrincipal += nFraction; else this.Schedules[this.Schedules.Count - 1].InstallmentInterest += nFraction; } for (int i = 0; i < this.Schedules.Count; i++) { // if (i == 9) return; this.Schedules[i].ClosingBalance = this.Schedules[i].OpeningBalance - (this.Schedules[i].InstallmentPrincipal + this.Schedules[i].InstallmentInterest); if (i < this.Schedules.Count - 1) { this.Schedules[i + 1].OpeningBalance = this.Schedules[i].ClosingBalance; } } } } public static LoanIssue GetByLoanAndEmployeeIdAndLoanNumber(ID LoanId, ID EmployeeId, string loanNumber) { return LoanIssue.Service.GetByLoanAndEmployeeIdAndLoanNumber(LoanId, EmployeeId, loanNumber); } public static ObjectsTemplate GetByEmployeeIdAndLoanNumber(ID id, string loanNumber) { ObjectsTemplate allIssuedLoans = new ObjectsTemplate(); 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 = "Employee (" + Employee.EmployeeNo + ") " + Employee.Name // + " applied " + this.Leave.Description + " From: " + this.AppliedFromDate.ToString("dd MMM yyyy") // + " To: " + this.AppliedToDate.ToString("dd MMM yyyy") // + " Total: " + this.ApprovedTotalDays.ToString() // + " Reason: " + this.Remarks; 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; } } } #endregion #region ILoanIssue Service public interface ILoanIssueService { LoanIssue Get(ID id); ObjectsTemplate Get(); DataSet GetIssuedLoanIDs(int EmpID); ID Save(LoanIssue item); ID Save2(LoanIssue item); void Delete(ID id); LoanIssue GetExistingLoan(ID nLoanID, ID nEmpID); LoanIssue GetExistingAllLoan(ID nLoanID, ID nEmpID); ObjectsTemplate GetExistingLoan(ID nEmpID); ObjectsTemplate GetByDueInstallmentDate(DateTime dateTime, string empID); ObjectsTemplate Get(DateTime dateTime); ObjectsTemplate Get(DateTime dateTime, DateTime dateTime2); ObjectsTemplate Get(string employeeNo); ObjectsTemplate GetByLoanID(ID nLoanID); ObjectsTemplate GetByEmployeeIdAndLoanNumber(ID id, string loanNumber); LoanIssue GetByLoanAndEmployeeIdAndLoanNumber(ID LoanId, ID EmployeeId, string loanNumber); DataSet GetIssuedLoans(int EmpID); ObjectsTemplate GetByDueInstallmentDate(DateTime dateTime); ObjectsTemplate GetAllExistingLoan(string LoanIDs); ObjectsTemplate GetAllExistingLoan(int EmpID, string sLaonIDs); ObjectsTemplate GetAllExistingLoan(string LoanIDs, DateTime month); void UpdateAvgIntStatus(ObjectsTemplate lis); } #endregion }