using Ease.Core.DataAccess; using Ease.Core.Model; using Ease.Core.Utility; using System; using System.Collections.Generic; using System.Data; using HRM.BO; using Org.BouncyCastle.Ocsp; namespace HRM.DA { #region LoanIssue Service public class LoanIssueService : ServiceTemplate, ILoanIssueService { #region Private functions and declaration #endregion public LoanIssueService() { } private void MapObject(LoanIssue oLoanIssue, DataReader oReader) { base.SetObjectID(oLoanIssue, oReader.GetInt32("LoanIssueID").Value); oLoanIssue.LoanID = oReader.GetString("loanID") == null ? 0 : oReader.GetInt32("loanID").Value; oLoanIssue.LoanNo = oReader.GetString("loanNo"); oLoanIssue.EmployeeID = oReader.GetString("EmployeeID") == null ? 0 : oReader.GetInt32("EmployeeID").Value; oLoanIssue.LoanAmount = oReader.GetDouble("loanAmount").Value; oLoanIssue.SettledPrincipal = oReader.GetDouble("SettledPrincipal").Value; oLoanIssue.SettledInterest = oReader.GetDouble("SettledInterest").Value; oLoanIssue.IssueDate = oReader.GetDateTime("issueDate").Value; oLoanIssue.NoOfInstallments = oReader.GetInt32("noOfInstallments").Value; oLoanIssue.InstallmentPrincipal = oReader.GetDouble("installmentPrincipal").Value; oLoanIssue.InterestRate = oReader.GetDouble("interestRate").Value; oLoanIssue.StartPaybackMonthDate = oReader.GetDateTime("startPaybackMonthDate").Value; oLoanIssue.WFStatus = (EnumwfStatus)oReader.GetInt32("WFStatus").Value; oLoanIssue.IsTopup = oReader.GetBoolean("IsTopup").GetValueOrDefault(); oLoanIssue.CreatedBy = oReader.GetString("CreatedBy") == null ? 0 : oReader.GetInt32("CreatedBy").Value; oLoanIssue.CreatedDate = oReader.GetDateTime("CreationDate").Value; oLoanIssue.ModifiedBy = oReader.GetString("ModifiedBy") == null ? 0 : oReader.GetInt32("ModifiedBy").Value; ; oLoanIssue.ModifiedDate = oReader.GetDateTime("ModifiedDate"); oLoanIssue.AverageInterestRate = oReader.GetBoolean("AverageInterestRate").GetValueOrDefault(); this.SetObjectState(oLoanIssue, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { LoanIssue oLoanIssue = new LoanIssue(); MapObject(oLoanIssue, oReader); return oLoanIssue as T; } protected LoanIssue CreateObject(DataReader oReader) { LoanIssue oLoanIssue = new LoanIssue(); MapObject(oLoanIssue, oReader); return oLoanIssue; } #region Service implementation public LoanIssue Get(int id) { LoanIssue oLoanIssue = new LoanIssue(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LoanIssueDA.Get(tc, id)); if (oreader.Read()) { oLoanIssue = this.CreateObject(oreader); } oreader.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 oLoanIssue; } public LoanIssue GetExistingLoan(int nLoanID, int nEmpID) { LoanIssue oLoanIssue = new LoanIssue(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LoanIssueDA.GetExistingLoan(tc, nLoanID, nEmpID)); if (oreader.Read()) { oLoanIssue = this.CreateObject(oreader); } oreader.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 oLoanIssue; } public DataSet GetDueLoans(string sEmpID, DateTime todate) { DataSet oGrossPay = new DataSet(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); oGrossPay = LoanIssueDA.GetDueLoans(tc, sEmpID, todate); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oGrossPay; } public DataSet GetMonthWiseLoans(DateTime todate, string locationIds) { DataSet oGrossPay = new DataSet(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); oGrossPay = LoanIssueDA.GetMonthWiseLoans(tc, todate, locationIds); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oGrossPay; } public LoanIssue GetExistingAllLoan(int nLoanID, int nEmpID) { LoanIssue oLoanIssue = new LoanIssue(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LoanIssueDA.GetExistingAllLoan(tc, nLoanID, nEmpID)); if (oreader.Read()) { oLoanIssue = this.CreateObject(oreader); } oreader.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 oLoanIssue; } public List GetExistingLoan(int nEmpID) { List loanIssues = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LoanIssueDA.GetExistingLoan(tc, nEmpID)); loanIssues = this.CreateObjects(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 loanIssues; } public List GetExistingLoan(string nEmpID) { List loanIssues = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LoanIssueDA.GetExistingLoan(tc, nEmpID)); loanIssues = this.CreateObjects(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 loanIssues; } public List GetByDueInstallmentDate(DateTime dateTime, string sEmpID) { List loanIssues = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LoanIssueDA.GetByDueInstallmentDate(tc, dateTime, sEmpID)); loanIssues = this.CreateObjects(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 loanIssues; } public DataTable GetLoanListByEmployee(int empId) { DataTable dt = new DataTable(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); dt = LoanIssueDA.GetLoanListByEmployee(tc, empId); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return dt; } public DataTable GetLoanSettlementData(int empId, int loanId) { DataTable settlementData = new DataTable(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); settlementData = LoanIssueDA.GetLoanSettlementData(tc, empId, loanId); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return settlementData; } public List GetByDueInstallmentDate(DateTime dateTime, int payrollTypeID) { List loanIssues = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LoanIssueDA.GetByDueInstallmentDate(tc, dateTime, payrollTypeID)); loanIssues = this.CreateObjects(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 loanIssues; } public List Get(DateTime dateTime) { List loanIssues = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LoanIssueDA.Get(tc, dateTime)); loanIssues = this.CreateObjects(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 loanIssues; } public List GetUnPaidLoanWithSchedule(DateTime dateTime) { List loanIssues = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LoanIssueDA.Get(tc, dateTime)); loanIssues = this.CreateObjects(dr); dr.Close(); if (loanIssues != null && loanIssues.Count > 0) { LoanScheduleService osda = new LoanScheduleService(); List schedules = osda.GetUnpaidSechedule(dateTime); foreach (LoanIssue issue in loanIssues) { //issue.Schedules = schedules.FindAll(delegate(LoanSchedule fd) { return fd.LoanIssueID == issue.ID; }); foreach (LoanSchedule osch in schedules) { if (osch.LoanIssueID == issue.ID) { issue.Schedules.Add(osch); break; } } } } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return loanIssues; } public List Get(DateTime dateTime, DateTime dateTime2) { List loanIssues = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LoanIssueDA.Get(tc, dateTime, dateTime2)); loanIssues = this.CreateObjects(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 loanIssues; } public List Get(string employeeNo) { List loanIssues = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LoanIssueDA.Get(tc, employeeNo)); loanIssues = this.CreateObjects(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 loanIssues; } public List GetByEmpIDs(string employeeID) { List loanIssues = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LoanIssueDA.GetByEmpIDs(tc, employeeID)); loanIssues = this.CreateObjects(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 loanIssues; } public List GetByLoanID(int nLoanID) { List loanIssues = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LoanIssueDA.GetByLoanID(tc, nLoanID)); loanIssues = this.CreateObjects(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 loanIssues; } public DataSet GetIssuedLoanIDs(int EmpID) { DataSet loanIDs = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); loanIDs = LoanIssueDA.GetIssuedLoanIDs(tc, EmpID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return loanIDs; } public DataSet RemainingLoanforPayslip(string empId) { DataSet rlfpayslip = new DataSet(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); rlfpayslip = LoanIssueDA.RemainingLoanforPayslip(tc, empId); } catch (Exception ex) { throw new Exception(ex.Message); } return rlfpayslip; } public DataSet GetIssuedLoans(int EmpID) { DataSet loanIDs = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); loanIDs = LoanIssueDA.GetIssuedLoans(tc, EmpID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return loanIDs; } public List Get() { List loanIssues = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LoanIssueDA.Get(tc)); loanIssues = this.CreateObjects(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 loanIssues; } public string GenerateLoanNo(int employeeid) { TransactionContext tc = null; int? nLoanID = 0; string sLoanNo = string.Empty; try { tc = TransactionContext.Begin(); Employee oemp = new EmployeeService().Get(employeeid); DataReader dr = new DataReader(LoanIssueDA.GetMaxLoanIssueID(tc, employeeid)); if (dr.Read()) { nLoanID = dr.GetInt32(0).HasValue ? dr.GetInt32(0).Value : 0; } dr.Close(); if (nLoanID == null) { nLoanID = 0; } dr = new DataReader(LoanIssueDA.GetLoanNo(tc, employeeid, nLoanID)); if (dr.Read()) { sLoanNo = dr.GetString(0); } nLoanID = 0; if (sLoanNo.Length > 3) { nLoanID = Convert.ToInt32(sLoanNo.Substring(sLoanNo.Length - 3, 3)); } dr.Close(); nLoanID = nLoanID + 1; //sLoanName sLoanNo = "Loan" + "//" + oemp.EmployeeNo + "/" + nLoanID.ToString().PadLeft(3, '0'); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return sLoanNo; } public void SaveAll(List oItems) { TransactionContext tc = null; int id = 0; int nCou = 0; try { foreach (LoanIssue oLoanIssue in oItems) { if (oLoanIssue.LoanNo == string.Empty) { oLoanIssue.LoanNo = this.GenerateLoanNo(oLoanIssue.EmployeeID); } } tc = TransactionContext.Begin(true); id = tc.GenerateID("LoanIssue", "LoanIssueID"); int id2 = tc.GenerateID("LOANSCHEDULE", "LoanScheduleID"); foreach (LoanIssue oLoanIssue in oItems) { LoanIssueDA.Delete(tc, oLoanIssue.LoanID, oLoanIssue.EmployeeID, oLoanIssue.StartPaybackMonthDate); //if (oLoanIssue.IsNew) //{ base.SetObjectID(oLoanIssue, id); oLoanIssue.CreatedBy = oLoanIssue.CreatedBy; oLoanIssue.CreatedDate = DateTime.Today; LoanIssueDA.Insert(tc, oLoanIssue); //} //else //{ // LoanIssueDA.Update(tc, oLoanIssue); //} //LoanScheduleDA.Delete(tc, oLoanIssue.ID); foreach (LoanSchedule schedule in oLoanIssue.Schedules) { schedule.LoanIssueID = oLoanIssue.ID; schedule.EmployeeID = oLoanIssue.EmployeeID; base.SetObjectID(schedule, id2); LoanScheduleDA.Insert(tc, schedule); id2++; } id++; //nCou++; } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public int Save(LoanIssue oLoanIssue) { TransactionContext tc = null; int id = 0; try { if(oLoanIssue.LoanNo == string.Empty) { oLoanIssue.LoanNo = this.GenerateLoanNo(oLoanIssue.EmployeeID); } tc = TransactionContext.Begin(true); if (oLoanIssue.IsNew) { id = tc.GenerateID("LoanIssue", "LoanIssueID"); base.SetObjectID(oLoanIssue, id); oLoanIssue.CreatedBy = oLoanIssue.CreatedBy; oLoanIssue.CreatedDate = DateTime.Today; LoanIssueDA.Insert(tc, oLoanIssue); } else { LoanIssueDA.Update(tc, oLoanIssue); } LoanScheduleDA.Delete(tc, oLoanIssue.ID); foreach (LoanSchedule schedule in oLoanIssue.Schedules) { schedule.LoanIssueID = oLoanIssue.ID; schedule.EmployeeID = oLoanIssue.EmployeeID; id = tc.GenerateID("LOANSCHEDULE", "LoanScheduleID"); base.SetObjectID(schedule, id); LoanScheduleDA.Insert(tc, schedule); } if (oLoanIssue.IssueDocks != null) { LoanEmployeeDocService dlsrv = new LoanEmployeeDocService(); dlsrv.DeletebyIssueid(tc, oLoanIssue.ID); foreach (LoanEmployeeDoc oitem in oLoanIssue.IssueDocks) { LoanEmployeeDocService srv = new LoanEmployeeDocService(); oitem.LoanIssueID = oLoanIssue.ID; srv.Save(tc, oitem); } } tc.End(); return oLoanIssue.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public int Save2(LoanIssue oLoanIssue) { TransactionContext tc = null; int id = 0; try { tc = TransactionContext.Begin(true); if (oLoanIssue.IsNew) { id = tc.GenerateID("LoanIssue", "LoanIssueID"); base.SetObjectID(oLoanIssue, id); LoanIssueDA.Insert(tc, oLoanIssue); } else { LoanIssueDA.Update(tc, oLoanIssue); } LoanScheduleDA.Delete2(tc, oLoanIssue.ID); foreach (LoanSchedule schedule in oLoanIssue.Schedules) { schedule.LoanIssueID = oLoanIssue.ID; schedule.EmployeeID = oLoanIssue.EmployeeID; this.SetObjectID(schedule, LoanScheduleDA.GetNewID(tc, "LOANSCHEDULE", "LoanScheduleID")); LoanScheduleDA.Insert(tc, schedule); } LoanEmployeeDocService dlsrv = new LoanEmployeeDocService(); dlsrv.DeletebyIssueid(tc, oLoanIssue.ID); foreach (LoanEmployeeDoc oitem in oLoanIssue.IssueDocks) { LoanEmployeeDocService srv = new LoanEmployeeDocService(); oitem.LoanIssueID = oLoanIssue.ID; srv.Save(tc, oitem); } tc.End(); return oLoanIssue.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void Delete(int id) { List< LoanSchedule> shedules = new LoanScheduleService().GetByIssueID(id); if(shedules.FindIndex(x=>x.PaymentDate != null) >-1) { throw new Exception("Payment started, Delete is not allowed."); } TransactionContext tc = null; try { tc = TransactionContext.Begin(true); LoanScheduleDA.Delete(tc, id); LoanIssueDA.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 } } public void UpdateAvgIntStatus(List lis) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); foreach (LoanIssue li in lis) { LoanIssueDA.UpdateAvgIntStatus(tc, li); } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public LoanIssue GetByLoanAndEmployeeIdAndLoanNumber(int LoanId, int EmployeeId, string loanNumber) { TransactionContext tc = null; List allLoans = new List(); try { tc = TransactionContext.Begin(true); DataReader dr = new DataReader(LoanIssueDA.GetByLoanAndEmployeeIdAndLoanNumber(tc, LoanId, EmployeeId, loanNumber)); allLoans = this.CreateObjects(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 } if (allLoans.Count == 1) { return allLoans[0]; } else return null; } public List GetByEmployeeIdAndLoanNumber(int id, string loanNumber) { TransactionContext tc = null; List allLoans; try { tc = TransactionContext.Begin(true); DataReader dr = new DataReader(LoanIssueDA.GetByEmployeeIdAndLoanNumber(id, loanNumber, tc)); allLoans = this.CreateObjects(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 allLoans; } #endregion } #endregion }