using System; using System.Collections.Generic; using System.Data; using System.Linq; using Ease.Core.Model; using Ease.Core.DataAccess; using Ease.Core.Utility; using HRM.BO; using NPOI.SS.Formula.Functions; using System.Security.Principal; using HRM.DA.Fund; using static iTextSharp.awt.geom.Point2D; using HRM.BO.Configuration; using Ease.Core; using System.Runtime.CompilerServices; using static iTextSharp.text.pdf.AcroFields; namespace HRM.DA { public class FMLoanService : ServiceTemplate, IFMLoanService //, IFASService { public FMLoanService() { } #region Child Schedule function private void MapLoanSchedule(FMLoanSchedule loanSchedule, DataReader dr) { base.SetObjectID(loanSchedule, dr.GetInt32("LoanScheduleID").Value); loanSchedule.ActivityID = dr.GetInt32("ActivityID") == null ? 0 : dr.GetInt32("ActivityID").Value; loanSchedule.CustomerID = dr.GetInt32("CustomerID") == null ? 0 : dr.GetInt32("CustomerID").Value; 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.FundTypeID = dr.GetInt32("FundTypeID").Value; base.SetObjectState(loanSchedule, Ease.Core.ObjectState.Saved); } private void MapLoanEarlySettlementAttachment(LoanEarlySettlementAttachment loanEarlySettlementAttachment, DataReader dr) { base.SetObjectID(loanEarlySettlementAttachment, dr.GetInt32("ID").Value); loanEarlySettlementAttachment.LoanId = dr.GetInt32("LoanId") == null ? 0 : dr.GetInt32("LoanId").Value; loanEarlySettlementAttachment.LoanEarlySettlementAttachmentDocType = (EnumDocumentType)dr.GetInt32("LoanEarlySettlementAttachmentDocType"); loanEarlySettlementAttachment.FileName = dr.GetString("FileName"); loanEarlySettlementAttachment.FileData = dr.GetLob("FileData"); loanEarlySettlementAttachment.FileDataString = dr.GetString("FileDataString"); loanEarlySettlementAttachment.uploadDate = dr.GetDateTime("UploadDate").Value; loanEarlySettlementAttachment.uploadBy = dr.GetInt32("UploadBy").Value; base.SetObjectState(loanEarlySettlementAttachment, Ease.Core.ObjectState.Saved); } private LoanEarlySettlementAttachment CreateLoanAttachment(DataReader dr) { LoanEarlySettlementAttachment loanEarlySettlementAttachment = new LoanEarlySettlementAttachment(); MapLoanEarlySettlementAttachment(loanEarlySettlementAttachment, dr); return loanEarlySettlementAttachment; } private FMLoanSchedule CreateSchedules(DataReader dr) { FMLoanSchedule loanSchedule = new FMLoanSchedule(); MapLoanSchedule(loanSchedule, dr); return loanSchedule; } private void FillSchedules(FMLoan parent, DataReader dr) { while (dr.Read()) { FMLoanSchedule item = this.CreateSchedules(dr); parent.LoanSchedules.Add(item); } } public int GetLoanAppEligibility(int MemberID) { int unpaidPayDateCount = 0; TransactionContext tc = null; try { tc = TransactionContext.Begin(true); unpaidPayDateCount = FMLoanDA.GetLoanAppEligibility(tc, MemberID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return unpaidPayDateCount; } public double GetLoanOustandingAmount(int MemberID) { double loanOutstandingAmount = 0; TransactionContext tc = null; try { tc = TransactionContext.Begin(true); loanOutstandingAmount = FMLoanDA.GetLoanOustandingAmount(tc, MemberID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return loanOutstandingAmount; } public FMLoan GetLoanByLoanApplicationID(int LoanApplicationID) { FMLoan loan = new FMLoan(); TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DataReader dr = new DataReader(FMLoanDA.GetLoanByLoanApplicationID(tc, LoanApplicationID)); if (dr.Read()) { loan = this.CreateObject(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 loan; } public void DepositLoanEarlySettlement(FMLoan oLoan) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); FMLoanDA.DepositLoanEarlySettlement(tc, oLoan); int attid = tc.GenerateID("FM_LoanEarlySettlementAttachment", "ID"); #region Insert Childs if (oLoan.LoanEarlySettlementAttachment != null) { if (oLoan.LoanEarlySettlementAttachment.FileData != null) { oLoan.LoanEarlySettlementAttachment.LoanId = oLoan.ID; oLoan.LoanEarlySettlementAttachment.uploadDate = DateTime.Now; oLoan.LoanEarlySettlementAttachment.uploadBy = oLoan.MemberID; base.SetObjectID(oLoan.LoanEarlySettlementAttachment, attid); LoanDA.InsertLoanEarlySettlementAttachment(tc, oLoan.LoanEarlySettlementAttachment); attid = attid + 1; } else { oLoan.LoanEarlySettlementAttachment.LoanId = oLoan.ID; oLoan.LoanEarlySettlementAttachment.FileData = null; oLoan.LoanEarlySettlementAttachment.FileName = null; base.SetObjectID(oLoan.LoanEarlySettlementAttachment, attid); LoanDA.InsertLoanEarlySettlementAttachment(tc, oLoan.LoanEarlySettlementAttachment); attid = attid + 1; } } #endregion 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 UpdateLoanEarlySettlement(FMLoan oLoan) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); FMLoanDA.UpdateLoanEarlySettlement(tc, oLoan); foreach (var item in oLoan.LoanSchedules) { if (item.PayDate == null && oLoan.SettleStatus == EnumLoanSettleStatus.Settled) { FMLoanDA.UpdateLoanSchedule(tc, item); } } int attid = tc.GenerateID("FM_LoanEarlySettlementAttachment", "ID"); #region Insert Childs if (oLoan.LoanEarlySettlementAttachment != null) { if (oLoan.LoanEarlySettlementAttachment.FileData != null) { oLoan.LoanEarlySettlementAttachment.LoanId = oLoan.ID; oLoan.LoanEarlySettlementAttachment.uploadDate = DateTime.Now; oLoan.LoanEarlySettlementAttachment.uploadBy = oLoan.MemberID; base.SetObjectID(oLoan.LoanEarlySettlementAttachment, attid); LoanDA.InsertLoanEarlySettlementAttachment(tc, oLoan.LoanEarlySettlementAttachment); attid = attid + 1; } else { oLoan.LoanEarlySettlementAttachment.LoanId = oLoan.ID; oLoan.LoanEarlySettlementAttachment.FileData = null; oLoan.LoanEarlySettlementAttachment.FileName = null; base.SetObjectID(oLoan.LoanEarlySettlementAttachment, attid); LoanDA.InsertLoanEarlySettlementAttachment(tc, oLoan.LoanEarlySettlementAttachment); attid = attid + 1; } } #endregion #endregion 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 SaveLoanEarlySettlementAttachment(TransactionContext tc, FMLoan oLoan) { try { int attid = tc.GenerateID("FM_LoanEarlySettlementAttachment", "ID"); #region Insert Childs if (oLoan.LoanEarlySettlementAttachment != null) { if (oLoan.LoanEarlySettlementAttachment.FileData != null) { oLoan.LoanEarlySettlementAttachment.LoanId = oLoan.ID; oLoan.LoanEarlySettlementAttachment.uploadDate = DateTime.Now; oLoan.LoanEarlySettlementAttachment.uploadBy = oLoan.MemberID; base.SetObjectID(oLoan.LoanEarlySettlementAttachment, attid); LoanDA.InsertLoanEarlySettlementAttachment(tc, oLoan.LoanEarlySettlementAttachment); } } #endregion } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void DoLoanEarlySettlement(FMLoan oLoan) { TransactionContext tc = null; FundType fundType = new FundTypeService().Get(oLoan.FundTypeID); try { List oPrincipalVoucherSetup = new ActivityVoucherSetupService().GetByProcessType(oLoan.FundTypeID , EnumFundProcessType.Loan_Early_Settlement); GLTran otran = new GLTran(); oLoan.LoanSchedules = new FMLoanScheduleService().GetByLoanID(oLoan.ID); if (oPrincipalVoucherSetup == null) { throw new Exception("Loan Early Settlment head not found."); } oLoan.LoanStatus = EnumFMLoanStatus.Close; oLoan.EarlySettleDate = DateTime.Today; oLoan.SettleStatus = EnumLoanSettleStatus.Settled; oLoan.ESPrincipalReceive = Math.Round(oLoan.TotalOutstandingPrincipal, 2); oLoan.ESIntrReceive = Math.Round(oLoan.TotalOutstandingInterest, 2); oLoan.ESServiceCharge = Math.Round(oLoan.ESServiceCharge, 2); tc = TransactionContext.Begin(true); FMLoanDA.DoLoanEarlySettlement(tc, oLoan); if (oLoan.LoanEarlySettlementAttachment != null) { SaveLoanEarlySettlementAttachment(tc, oLoan); } foreach (var item in oLoan.LoanSchedules) { if (item.PayDate == null) { item.IsCompleted = true; item.PayDate = DateTime.Today; item.IsSettle = (int)EnumYesNo.YES; if (oLoan.ESIntrReceive == 0) { item.InstallmentInterest = 0; item.InstallmentAmount = item.InstallmentPrincipal; } FMLoanDA.UpdateLoanSchedule(tc, item); } } if (GlobalFunctions.FirstDateOfMonth(fundType.CurrentMonth) > oLoan.VoucherDate || GlobalFunctions.LastDateOfMonth(fundType.CurrentMonth) < oLoan.VoucherDate) { throw new Exception("Voucher date must be in current month"); } #region Principal Voucher ActivityVoucherSetup pvprincipal = oPrincipalVoucherSetup.FirstOrDefault(x => x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Loan_Principal); if (pvprincipal == null) { throw new Exception("Principal head not found in Loan-Early-Settlment head."); } GL ogl = new GLService().Get(tc, pvprincipal.GLID); GLTranDetail oGTDetail = new GLTranDetail(); oGTDetail.GLID = ogl.ID; oGTDetail = new GLTranDetail(); oGTDetail.GLID = ogl.ID; oGTDetail.Amount = Math.Round(oLoan.TotalOutstandingPrincipal, 2); oGTDetail.Transide = pvprincipal.GLSide; if (ogl.UserObjectID != null) { UserRecord oRecord = new UserRecordService().GetByField1(oLoan.LoanNo, GlobalFunctions.Fund_UserObject_Loan, oLoan.FundTypeID); HRM.BO.GLTranUserRecord gltUR = new GLTranUserRecord(ogl.ID, 0, 0, oRecord.UserObjectID, oRecord.ID, oLoan.TotalOutstandingPrincipal); if (oGTDetail.GLTranUserRecords == null) oGTDetail.GLTranUserRecords = new List(); oGTDetail.GLTranUserRecords.Add(gltUR); } #endregion Principal Voucher #region Interest Voucher ActivityVoucherSetup interestSetup = oPrincipalVoucherSetup.FirstOrDefault(x => x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Loan_Interest); if (interestSetup == null) { throw new Exception("Interest head not found in Loan-Early-Settlment head."); } ogl = new GLService().Get(interestSetup.GLID); GLTranDetail gtInterest = new GLTranDetail(); gtInterest.GLID = ogl.ID; gtInterest.Amount = Math.Round(oLoan.TotalOutstandingInterest, 2); gtInterest.Transide = interestSetup.GLSide; #endregion Interest Voucher #region SettleCharge ActivityVoucherSetup settleChargeSetup = oPrincipalVoucherSetup.FirstOrDefault(x => x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Loan_Early_Settle_Charge); if (settleChargeSetup == null) { throw new Exception("Loan Early Settle Charge head not found in Loan-Early-Settlment head."); } ogl = new GLService().Get(settleChargeSetup.GLID); GLTranDetail gtSettleCharge = new GLTranDetail(); gtSettleCharge.GLID = ogl.ID; gtSettleCharge.Amount = Math.Round(oLoan.ESServiceCharge, 2); gtSettleCharge.Transide = settleChargeSetup.GLSide; #endregion SettleCharge #region Principal Contra ActivityVoucherSetup pricipalContra = oPrincipalVoucherSetup.FirstOrDefault(x => x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Bank || x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Contra); if (pricipalContra == null) { throw new Exception("Contra Head not found."); } ogl = new GLService().Get(pricipalContra.GLID); GLTranDetail pvcontra = new GLTranDetail(); pvcontra.GLID = ogl.ID; double bankAmount = Math.Round(oLoan.ESPrincipalReceive + oLoan.ESIntrReceive + oLoan.ESServiceCharge, 2); pvcontra.Amount = Math.Round(bankAmount, 2); pvcontra.Transide = pricipalContra.GLSide; #endregion Principal Contra otran.VoucherDate = oLoan.VoucherDate; otran.ChequeDate = oLoan.CheckDate; otran.CreatedDate = DateTime.Now; otran.CreatedBy = (int)oLoan.ModifiedBy; otran.ChequeNo = oLoan.CheckNo; otran.GLTranType = EnumGLTranType.LoanEarlySettlement; otran.FundTypeID = oLoan.FundTypeID; otran.Description = "Loan Early Settlement-" + oLoan.LoanNo; otran.GlTranDetails = new List(); otran.GlTranDetails.Add(oGTDetail); otran.GlTranDetails.Add(pvcontra); if (gtInterest.Amount > 0) { otran.GlTranDetails.Add(gtInterest); } if (gtSettleCharge.Amount > 0) { otran.GlTranDetails.Add(gtSettleCharge); } otran.DebitAmount = Math.Round(otran.GlTranDetails.Where(x => x.Transide == EnumTranSide.Debit).Sum(x => x.Amount), 2); otran.CreditAmount = Math.Round(otran.GlTranDetails.Where(x => x.Transide == EnumTranSide.Credit).Sum(x => x.Amount), 2); new GLTranService().Save(tc, otran); 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 SubmitLoanEarlySettlementRequest(FMLoan oLoan) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); FMLoanDA.UpdateLoanEarlySettlement(tc, oLoan); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public string SendMailToLoanEarlySettleApplicant(FMLoan oLoan, EmailSettings emailSettings) { var omember = new MemberService().Get(oLoan.MemberID); DataTable empInfo = new DataTable(); empInfo = new MemberService().GetDashboardMember(omember.EmpCode); string mailAddress = empInfo.Rows[0]["EMAILADDRESS"].ToString(); string EmployeeCode = empInfo.Rows[0]["EmpCode"].ToString(); string Name = empInfo.Rows[0]["Name"].ToString(); string error = string.Empty; string body = "Dear <>," + "

" + "A Loan Early Settlement Application has been submitted by You. The loan will be settled within 10 working days.

"; try { body = body.Replace("<>", Name); body = body.Replace("<>", EmployeeCode); //body = body.Replace(" <>", oLoanApplication.); //body = body.Replace("<>", Convert.ToDateTime(item["FROMDATE"].ToString()).ToString("dd MMM yyyy")); MailSender mailSender = new MailSender(); mailSender.AddTo(mailAddress); mailSender.Subject = "Loan Early Settlement Request"; mailSender.Body = "

" + body + "

"; mailSender.ThreadSendMail(emailSettings); } catch (Exception) { error = "Could Not Send Mail to " + Name + " [" + EmployeeCode + "]"; } return error; } public string SendMailToLoanEarlySettlementHR(FMLoan oLoan, EmailSettings emailSettings, int HrID) { FundEss _fundEss = new FundEss(); Member PFDepositCheckedBy = new MemberService().Get(HrID); var oApplicant = new MemberService().Get((int)oLoan.EarlySettleRequestBy); DataTable applicantInfo = new DataTable(); applicantInfo = new MemberService().GetDashboardMember(oApplicant.EmpCode); var oHr = new MemberService().Get(HrID); DataTable hrInfo = new DataTable(); hrInfo = new MemberService().GetDashboardMember(oHr.EmpCode); string mailAddress = hrInfo.Rows[0]["EMAILADDRESS"].ToString(); string EmployeeCode = hrInfo.Rows[0]["EmpCode"].ToString(); string Name = hrInfo.Rows[0]["Name"].ToString(); string error = string.Empty; string subject = string.Empty; string body = string.Empty; if (oLoan.SettleStatus == EnumLoanSettleStatus.Requested) { subject = "Loan Early Settlement Request"; body = "Dear <>," + "

" + "A Loan Early Settlement Application has been submitted by " + applicantInfo.Rows[0]["Name"].ToString() + " [" + applicantInfo.Rows[0]["EmpCode"].ToString() + "]
"; } else if (oLoan.SettleStatus == EnumLoanSettleStatus.Deposited) { subject = "Loan Early Settlement Deposit"; body = "Dear <>," + "

" + "Loan Early Settlement Amount has been Deposited by " + applicantInfo.Rows[0]["Name"].ToString() + " [" + applicantInfo.Rows[0]["EmpCode"].ToString() + "]
"; } try { body = body.Replace("<>", Name); body = body.Replace("<>", EmployeeCode); MailSender mailSender = new MailSender(); mailSender.AddTo(mailAddress); mailSender.Subject = subject; mailSender.Body = "

" + body + "

"; mailSender.ThreadSendMail(emailSettings); } catch (Exception) { error = "Could Not Send Mail to " + Name + " [" + EmployeeCode + "]"; } return error; } internal void LoadSchedule(TransactionContext tc, FMLoan loan) { try { if (loan != null && loan.ID != 0) { //Get LoanSchedule DataReader dr = new DataReader(FMLoanDA.GetSchedules(tc, loan.ID)); FillSchedules(loan, dr); dr.Close(); } } catch (Exception e) { throw new Exception("Error : " + e.Message); } } #region Child LoanCategory private void MapCategoryObject(FMLoanCategory loanCategory, DataReader dr) { base.SetObjectID(loanCategory, dr.GetInt32("LoanCategoryID").Value); loanCategory.DefaultInterestRate = dr.GetDouble("DefaultInterestRate").Value; loanCategory.LoanCategoryCode = dr.GetString("LoanCategoryCode"); loanCategory.Description = dr.GetString("Description"); loanCategory.IsActive = (EnumFMLoanCategoryStatus)dr.GetInt16("IsActive").Value; loanCategory.MaxAlowableFlatAmount = dr.GetDouble("MaxAlowableFlatAmount"); loanCategory.MaxInstallment = dr.GetInt32("MaxInstallment").Value; loanCategory.MinInstallment = dr.GetInt32("MinInstallment").Value; loanCategory.PercentageofFund = dr.GetDouble("PercentageofFund"); loanCategory.Version = dr.GetInt16("Version").Value; loanCategory.CreatedBy = dr.GetInt32("CreatedBy").Value; loanCategory.CreatedDate = dr.GetDateTime("CreatedDate").Value; loanCategory.ModifiedBy = dr.GetInt32("ModifiedBy"); loanCategory.ModifiedDate = dr.GetDateTime("ModifiedDate"); loanCategory.FundTypeID = dr.GetInt32("FundTypeID").Value; base.SetObjectState(loanCategory, Ease.Core.ObjectState.Saved); } private FMLoanCategory CreateCategoryObject(DataReader dr) { FMLoanCategory loanCategory = new FMLoanCategory(); MapCategoryObject(loanCategory, dr); return loanCategory; } internal void LoadCategory(TransactionContext tc, FMLoan loan) { try { if (loan != null && loan.LoanCategoryID != null && loan.LoanCategoryID != 0) { //Get LoanCategory FMLoanCategory loanCategory = null; DataReader dr = new DataReader(FMLoanDA.GetCategory(tc, loan.LoanCategoryID)); if (dr.Read()) { loanCategory = this.CreateCategoryObject(dr); } dr.Close(); } } catch (Exception e) { throw e; } } #endregion #region Child LoanCustomer //private void MapCustomerObject(Member loanCustomer, DataReader dr) //{ // base.SetObjectID(oMember, dr.GetInt32("MemberID").Value); // oMember.MemberID = dr.GetInt32("MemberID").Value; // oMember.EmpCode = dr.GetString("EmpCode"); // oMember.Name = dr.GetString("Name"); // oMember.FundTypeID = dr.GetInt32("FundTypeID").Value; // oMember.MembershipNo = dr.GetString("MembershipNo"); // oMember.MembershipDate = dr.GetDateTime("MembershipDate"); // oMember.FinalSettlementDate = dr.GetDateTime("FinalSettlementDate"); // oMember.DOB = dr.GetDateTime("DOB").HasValue ? dr.GetDateTime("DOB").Value : DateTime.MinValue; ; // oMember.DOJ = dr.GetDateTime("DOJ").HasValue ? dr.GetDateTime("DOJ").Value : DateTime.MinValue; // oMember.DOL = dr.GetDateTime("DOL"); // oMember.CompanyName = dr.GetString("CompanyName"); // oMember.Designation = dr.GetString("Designation") != null ? dr.GetString("Designation") : ""; // oMember.MemberStatus = (EnumEmpStatus)dr.GetInt16("Status").Value; // oMember.CreatedBy = dr.GetInt32("CreatedBy").Value; // oMember.CreatedDate = dr.GetDateTime("CreatedDate").Value; // oMember.ModifiedBy = dr.GetInt32("ModifiedBy", 0); // oMember.ModifiedDate = dr.GetDateTime("ModifiedDate"); // oMember.CurrentBasic = dr.GetInt32("CurrentBasic").Value; // oMember.Email = dr.GetString("Email") == null ? "" : dr.GetString("Email"); // oMember.IsProfitPartner = dr.GetBoolean("IsProfitPartner").Value; // base.SetObjectState(oMember, ObjectState.Saved); //} //private Member CreateCustomerObject(DataReader dr) //{ // Member loanCustomer = new Member(); // MapCustomerObject(loanCustomer, dr); // return loanCustomer; //} internal void LoadCustomer(TransactionContext tc, FMLoan loan) { try { if (loan != null && loan.ID != 0) { //Get LoanCustomer loan.Member = new MemberService().Get(tc, loan.MemberID); } } catch (Exception e) { throw e; } } #endregion #region parent Loan Section private void MapObject(FMLoan loan, DataReader dr) { base.SetObjectID(loan, dr.GetInt32("LoanID").Value); loan.ActivityID = (int)(dr.GetInt32("ActivityID") == null ? 0 : dr.GetInt32("ActivityID")); loan.CustomerID = dr.GetInt32("CustomerID").Value; loan.Description = dr.GetString("Description"); loan.InstallmentNo = dr.GetInt32("InstallmentNo").Value; loan.InterestRate = dr.GetDouble("InterestRate").Value; loan.IssueDate = dr.GetDateTime("IssueDate").Value; loan.LoanCategoryID = dr.GetInt32("LoanCategoryID").Value; loan.LoanNo = dr.GetString("LoanNo"); loan.Remarks = dr.GetString("Remarks"); loan.LoanAccNo = dr.GetString("LoanAccNo"); loan.MemberID = dr.GetInt32("MemberID").Value; loan.PrincipalAmount = dr.GetDouble("PrincipalAmount").Value; loan.LoanStatus = (EnumFMLoanStatus)dr.GetInt16("Status").Value; loan.LoanIssueStatus = dr.GetInt16("LoanIssueStatus") == null ? 0 : (EnumFMLoanIssueStatus)dr.GetInt32("LoanIssueStatus"); loan.Version = dr.GetInt32("Version") != null ? dr.GetInt32("Version").Value : 0; loan.CreatedBy = dr.GetInt32("CreatedBy").Value; loan.CreatedDate = dr.GetDateTime("CreatedDate").Value; loan.ModifiedBy = dr.GetInt32("ModifiedBy"); loan.ModifiedDate = dr.GetDateTime("ModifiedDate"); loan.FundTypeID = dr.GetInt32("FundTypeID").Value; loan.GLTranID = (int)(dr.GetInt32("GLTranID") == null ? 0 : dr.GetInt32("GLTranID")); loan.StartPayBackMonth = dr.GetDateTime("StartPayBackMonth") != null ? dr.GetDateTime("StartPayBackMonth").Value : DateTime.MinValue; loan.TransferLogIDinInt = dr.GetInt32("TransferLogID") != null ? dr.GetInt32("TransferLogID").Value : 0; loan.EarlySettleRequestDate = dr.GetDateTime("EarlySettleRequestDate"); loan.EarlySettleRequestBy = dr.GetInt32("EarlySettleRequestBy"); loan.SettleAmount = dr.GetDouble("SettleAmount"); loan.InstrumentNumber = dr.GetString("InstrumentNumber"); loan.InstrumentDate = dr.GetDateTime("InstrumentDate"); loan.InstrumentBank = dr.GetString("InstrumentBank"); loan.DepositAmount = dr.GetDouble("DepositAmount"); loan.DepositDate = dr.GetDateTime("DepositDate"); loan.SettleStatus = dr.GetInt16("SettleStatus") == null ? 0 : (EnumLoanSettleStatus)dr.GetInt16("SettleStatus"); loan.EarlySettleDate = dr.GetDateTime("EarlySettleDate"); loan.CheckNo = dr.GetString("CHECKNO"); loan.CheckDate = dr.GetDateTime("CHECKDATE"); loan.ESServiceCharge = dr.GetDouble("ESServiceCharge") == null ? 0 : dr.GetDouble("ESServiceCharge").Value; loan.ESPrincipalReceive = dr.GetDouble("ESPrincipalReceive") == null ? 0 : dr.GetDouble("ESPrincipalReceive").Value; loan.ESIntrReceive = dr.GetDouble("ESIntrReceive") == null ? 0 : dr.GetDouble("ESIntrReceive").Value; base.SetObjectState(loan, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader dr) { FMLoan loan = new FMLoan(); MapObject(loan, dr); return loan as T; } #endregion #region Service Implementation #region Insert public void SetLoanStatus() { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); FMLoanDA.SetLoanStatus(tc); 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 UpdateSchedules(List oloanSchedules, List otrans) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); foreach (FMLoanSchedule oitem in oloanSchedules) { FMLoanDA.UpdateLoanSchedule(tc, oitem); } //Ease.Core.DataAccess.TransactionContext coretc = Ease.Core.DataAccess.TransactionContext.SetConnection(tc.Connection, tc.Transaction); //EaseFAS.Service.GLTranService oservice = new EaseFAS.Service.GLTranService(); //oservice.Insert(coretc, otrans); 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 UpdateLoanPayDateSchedule(List oloanSchedules, List otrans) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); foreach (FMLoanSchedule oitem in oloanSchedules) { FMLoanDA.UpdateLoanPayDateSchedule(tc, oitem); if (oitem.IsCompleted) { FMLoanDA.UpdateLoanStatus(tc, oitem.LoanID); } } foreach (var item in otrans) { new GLTranService().Save(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 } } public void UpdateLoanIssueStatus(FMLoan oLoan) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); FMLoanDA.UpdateLoanIssueStatus(tc, oLoan); 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 SaveEarlySettlement(TransactionContext tc, FMLoan oloan) { try { if (!oloan.IsNew) { oloan.ModifiedBy = 0;//Current User ID oloan.ModifiedDate = DateTime.Today; FMLoanDA.UpdateLoanEarlySettle(tc, oloan); } foreach (var item in oloan.LoanSchedules) { //DateTime dTime = GlobalFunctionDA.ServerDate(tc).Date; if (oloan.InstallmentNo == item.SerialNo && item.PayDate.Value.Date == GlobalFunctionDA.ServerDate(tc).Date && item.InstallmentInterest == 0 && item.InstallmentAmount == item.EffectiveBalance) { item.ModifiedBy = 0;//Current User ID item.ModifiedDate = DateTime.Today; FMLoanDA.UpdateLoanPayDateSchedule(tc, item); } //else if (isEarlySettle && item.PayDate == null) //{ // FMLoanDA.DeleteLoanScheduleByID(tc, item.ID); //} } //if (isEarlySettle) //{ // int logId = tc.GenerateID("FM_LoanLog", "LogID"); // base.SetObjectID(loanLog, logId); // loanLog.CreatedBy = 0;//Current User ID // loanLog.CreatedDate = GlobalFunctionDA.ServerDate(tc); // FMLoanDA.Insert(tc, loanLog); //} } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void SaveForFinalSettlement(TransactionContext tc, FMLoan oloan) { try { oloan.LoanStatus = EnumFMLoanStatus.Close; FMLoanDA.UpdateLoanEarlySettle(tc, oloan); foreach (var item in oloan.LoanSchedules) { item.ModifiedBy = 0;//Current User ID item.ModifiedDate = DateTime.Today; item.PayDate = DateTime.Today; // this will be settlement date; FMLoanDA.UpdateLoanPayDateSchedule(tc, item); } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } #endregion #region GenerateLoanNo public string GenerateLoanNo(FMLoan loan) { string serial = string.Empty; string s = string.Empty; TransactionContext tc = null; try { s = "PFLOAN"; if (loan.MemberID >= 0) { Member member = new MemberService().Get(loan.MemberID); s = s + "//" + member.EmpCode.ToString() + "/"; } tc = TransactionContext.Begin(); int serialNo = FMLoanDA.GenerateLoanNo(tc, loan.MemberID, loan.LoanCategoryID); tc.End(); serialNo = serialNo + 1; serial = serialNo.ToString(); if (serial.Length == 1) { serial = "00" + serial; } if (serial.Length == 2) { serial = "0" + serial; } serial = s + serial; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return serial; } #endregion #region Delete public void Delete(int id) { FMLoan oloan = this.Get(id, true); if (oloan.LoanSchedules.Any(x => x.PayDate != null) == true) { throw new ServiceException("Cannot delete loan with paid installment."); } TransactionContext tc = null; try { tc = TransactionContext.Begin(true); FMLoanDA.Delete(tc, id); new FMLoanAttachmentService().DeleteByLoanID(tc, id); new GLTranService().Delete(tc, oloan.GLTranID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Delete " + e.Message); ; #endregion } } #endregion #region Get Loan public FMLoan Get(string LoanNo) { FMLoan loan = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(FMLoanDA.Get(tc, LoanNo)); if (dr.Read()) { loan = this.CreateObject(dr); } dr.Close(); LoadSchedule(tc, loan); LoadCategory(tc, loan); // LoadCustomer(tc, loan); tc.End(); //tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return loan; } public FMLoan Get(int loanID, bool isGetSchedule, bool isGetCategory, bool isGetCustomer) { FMLoan loan = new FMLoan(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(FMLoanDA.Get(tc, loanID)); if (dr.Read()) { loan = this.CreateObject(dr); } dr.Close(); if (isGetSchedule) { LoadSchedule(tc, loan); } if (isGetCategory) { LoadCategory(tc, loan); } if (isGetCustomer) { LoadCustomer(tc, loan); } tc.End(); //tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return loan; } public FMLoan Get(TransactionContext tc, int loanID, bool isGetSchedule, bool isGetCategory, bool isGetCustomer) { FMLoan loan = new FMLoan(); try { DataReader dr = new DataReader(FMLoanDA.Get(tc, loanID)); if (dr.Read()) { loan = this.CreateObject(dr); } dr.Close(); if (isGetSchedule) { LoadSchedule(tc, loan); } if (isGetCategory) { LoadCategory(tc, loan); } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return loan; } #endregion public bool GetIsUsedUserObjectName(FMLoanCategory loanCategory) { bool found = false; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dataReader = new DataReader(FMLoanDA.Get(tc, loanCategory)); if (dataReader.Read()) found = true; tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return found; } #region Get() public List GetESS(int runningLoan, bool isWithSchedule) { List loans = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DataReader dr = new DataReader(FMLoanDA.GetEss(tc, runningLoan)); loans = this.CreateObjects(dr); dr.Close(); if (isWithSchedule) { foreach (FMLoan loan in loans) { LoadSchedule(tc, loan); } } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return loans; } public List GetByFundType(int FundTypeID) { List loans = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(FMLoanDA.GetByFundType(tc, FundTypeID)); loans = this.CreateObjects(dr); dr.Close(); foreach (FMLoan loan in loans) { LoadSchedule(tc, loan); } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return loans; } public FMLoan Get(int loanid, bool isWithSchedule) { FMLoan loan = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DataReader dr = new DataReader(FMLoanDA.Get(tc, loanid)); if (dr.Read()) { loan = this.CreateObject(dr); } dr.Close(); if (isWithSchedule && loan != null) { LoadSchedule(tc, loan); if (loan.LoanSchedules != null) { foreach (FMLoanSchedule loanSchedule in loan.LoanSchedules) { if (loanSchedule.PayDate != null) { loan.TotalPaidInterest += loanSchedule.InstallmentInterest; loan.TotalPaidPrincipal += loanSchedule.InstallmentPrincipal; loan.TotalPaid += loanSchedule.InstallmentAmount; loan.TotalPaidInstallment++; } else { loan.TotalOutstandingPrincipal += loanSchedule.InstallmentPrincipal; loan.TotalOutstandingInterest += loanSchedule.InstallmentInterest; } loan.InstallmentAmount = loanSchedule.InstallmentAmount; loan.ToBeToatalInterest += loanSchedule.InstallmentInterest; loan.ToBeTotal += loanSchedule.InstallmentAmount; loan.ToBeTotalPrincipal += loanSchedule.InstallmentPrincipal; } } loan.InstallmentPrincipal = loan.LoanSchedules[0].InstallmentPrincipal; loan.InstallmentInterest = loan.LoanSchedules[0].InstallmentInterest; loan.TotalOutstanding = loan.ToBeTotal - loan.TotalPaid; loan.DisbursementMonth = loan.LoanSchedules[0].ScheduledPayDate; loan.LastSchedulePayDate = loan.LoanSchedules[loan.LoanSchedules.Count - 1].ScheduledPayDate; } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return loan; } public List GetNotSendLoan(int runningLoan, bool isWithSchedule) { List loans = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DataReader dr = new DataReader(FMLoanDA.GetNotSendLoan(tc, runningLoan)); loans = this.CreateObjects(dr); dr.Close(); if (isWithSchedule) { foreach (FMLoan loan in loans) { LoadSchedule(tc, loan); } } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return loans; } #endregion #region GetLoanMember public DataTable GetLoanMember(int loanCategoryID, string issueOperator, DateTime issueFromDate, DateTime issueToDate, string collectionOperator, DateTime collectionFromDate, DateTime collectionToDate, EnumLoanStatus status, int memberID, int custID, int loanID, int fundTypeID) { DataSet ds = new DataSet(); TransactionContext tc = null; try { string subQuery = string.Empty; if (loanCategoryID > 0) { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" cat.LoanCategoryID = %n", loanCategoryID); } if (issueOperator != string.Empty && issueOperator != "None" && issueOperator != "" && issueOperator != "<>") { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" issue.IssueDate " + issueOperator + " %d ", issueFromDate); } if (issueOperator == "<>") { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" issue.IssueDate Between %d AND %d ", issueFromDate, issueToDate); } if (collectionOperator != string.Empty && collectionOperator != "None" && collectionOperator != "" && collectionOperator != "<>") { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" issue.StartPayBackMonth " + collectionOperator + " %d ", collectionFromDate); } if (issueOperator == "<>") { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" issue.StartPayBackMonth Between %d AND %d ", collectionFromDate, collectionToDate); } string statusClause = string.Empty; if (status != EnumLoanStatus.All) { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" issue.Status = %n", status); } string memberClause = string.Empty; if (custID > 0) { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" cust.CustomerID = %n", custID); } tc = TransactionContext.Begin(true); ds = FMLoanDA.GetLoanMember(tc, subQuery, memberID, loanID, fundTypeID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); throw new ServiceException(e.Message, e); #endregion } return ds.Tables[0]; } #endregion #region GetLoanList public DataTable GetLoanList(EnumLoanStatus status, int memberID) { DataSet ds = new DataSet(); TransactionContext tc = null; try { string subQuery = string.Empty; string statusClause = string.Empty; if (status != EnumLoanStatus.All) { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" issue.Status = %n", status); } string memberClause = string.Empty; if (memberID > 0) { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" issue.MemberID = %n", memberID); } tc = TransactionContext.Begin(true); ds = FMLoanDA.GetLoanList(tc, subQuery); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); throw new ServiceException(e.Message, e); #endregion } return ds.Tables[0]; } public DataTable GetLoanList(int loanCategoryID, string issueOperator, DateTime issueFromDate, DateTime issueToDate, string collectionOperator, DateTime collectionFromDate, DateTime collectionToDate, EnumLoanStatus status, int memberID, int fundTypeID) { DataSet ds = new DataSet(); TransactionContext tc = null; try { string subQuery = string.Empty; if (loanCategoryID > 0) { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" cat.LoanCategoryID = %n", loanCategoryID); } if (issueOperator != string.Empty && issueOperator != "None" && issueOperator != "" && issueOperator != "<>") { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" issue.IssueDate " + issueOperator + " %d ", issueFromDate); } if (issueOperator == "<>") { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" issue.IssueDate Between %d AND %d ", issueFromDate, issueToDate); } if (collectionOperator != string.Empty && collectionOperator != "None" && collectionOperator != "" && collectionOperator != "<>") { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" issue.StartPayBackMonth " + collectionOperator + " %d ", collectionFromDate); } if (issueOperator == "<>") { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" issue.StartPayBackMonth Between %d AND %d ", collectionFromDate, collectionToDate); } string statusClause = string.Empty; if (status != EnumLoanStatus.All) { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" issue.Status = %n", status); } string memberClause = string.Empty; if (memberID > 0) { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" cust.MemberID = %n", memberID); } tc = TransactionContext.Begin(true); ds = FMLoanDA.GetLoanLists(tc, subQuery, fundTypeID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); throw new ServiceException(e.Message, e); #endregion } return ds.Tables[0]; } public DataTable GetLoanList(int fundTypeID, string memberID, int loanCategoryID, int loanStatusID, bool isIssueDate, DateTime issueFromDate, DateTime issueToDate) { DataSet ds = new DataSet(); TransactionContext tc = null; try { string subQuery = string.Empty; if (fundTypeID > 0) { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" issue.FundTypeID = %n", fundTypeID); } if (loanCategoryID > 0) { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" cat.LoanCategoryID = %n", loanCategoryID); } if (isIssueDate) { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" issue.IssueDate between %d and %d", issueFromDate, issueToDate); } if (loanStatusID > 0) { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" issue.Status = %n", loanStatusID); } string memberClause = string.Empty; if (memberID != string.Empty) { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" m.MemberID in (%q)", memberID); } tc = TransactionContext.Begin(true); ds = FMLoanDA.GetLoanList(tc, subQuery); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); throw new ServiceException(e.Message, e); #endregion } return ds.Tables[0]; } public List GetLoanObjects(int fundTypeID, string memberID, int loanStatusID) { List loans = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(true); loans = this.GetLoanObjects(tc, fundTypeID, memberID, loanStatusID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); throw new ServiceException(e.Message, e); #endregion } return loans; } public List GetLoanObjects(TransactionContext tc, int fundTypeID, string memberID, int loanStatusID) { List loans = new List(); try { string subQuery = string.Empty; if (fundTypeID > 0) { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" FundTypeID = %n", fundTypeID); } if (loanStatusID > 0) { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" Status = %n", loanStatusID); } string memberClause = string.Empty; if (memberID != string.Empty) { subQuery = SQLParser.TagSQL(subQuery) + SQLParser.MakeSQL(" MemberID in (%q)", memberID); } tc = TransactionContext.Begin(true); DataReader dr = new DataReader(FMLoanDA.GetLoanObjects(tc, subQuery)); loans = this.CreateObjects(dr); dr.Close(); foreach (var item in loans) { LoadSchedule(tc, item); LoadCategory(tc, item); } foreach (var loan in loans) { if (loan.LoanSchedules != null) { foreach (FMLoanSchedule loanSchedule in loan.LoanSchedules) { if (loanSchedule.PayDate != null) { loan.TotalPaidInterest += loanSchedule.InstallmentInterest; loan.TotalPaidPrincipal += loanSchedule.InstallmentPrincipal; loan.TotalPaid += loanSchedule.InstallmentAmount; loan.TotalPaidInstallment++; } else { loan.TotalOutstandingPrincipal += loanSchedule.InstallmentPrincipal; loan.TotalOutstandingInterest += loanSchedule.InstallmentInterest; } loan.InstallmentAmount = loanSchedule.InstallmentAmount; loan.ToBeToatalInterest += loanSchedule.InstallmentInterest; loan.ToBeTotal += loanSchedule.InstallmentAmount; loan.ToBeTotalPrincipal += loanSchedule.InstallmentPrincipal; } } loan.InstallmentPrincipal = loan.LoanSchedules[0].InstallmentAmount; loan.InstallmentInterest = loan.LoanSchedules[0].InstallmentInterest; loan.TotalOutstanding = loan.ToBeTotal - loan.TotalPaid; loan.DisbursementMonth = loan.LoanSchedules[0].ScheduledPayDate; loan.LastSchedulePayDate = loan.LoanSchedules[loan.LoanSchedules.Count - 1].ScheduledPayDate; } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); throw new ServiceException(e.Message, e); #endregion } return loans; } #endregion #region GetTable public DataTable GetTable(DateTime fromDate, DateTime toDate) { DataTable dTbl = new DataTable(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); dTbl = FMLoanDA.GetTable(tc, fromDate, 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 dTbl; } public DataTable GetTable(string smemberId, DateTime fromDate, DateTime toDate) { DataTable dTbl = new DataTable(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); dTbl = FMLoanDA.GetTable(tc, smemberId, fromDate, 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 dTbl; } public DataTable GetLoanInfoByMonth(DateTime scheduleMonth, int fundtypeid) { DataTable dtLoanScheduleInfo = new DataTable(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); dtLoanScheduleInfo = FMLoanDA.GetLoanInfoByMonth(tc, scheduleMonth, fundtypeid); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return dtLoanScheduleInfo; } public DataSet GetLoans() { DataTable dt = new DataTable(); DataSet ds = new DataSet(); TransactionContext tc = null; try { ConnectionContext cc = new ConnectionContext("serviceHandler1"); tc = TransactionContext.Begin(true, cc); ds = FMLoanDA.GetLoans(tc); tc.End(); } catch (Exception e) { throw new ServiceException("Failed to Get Members" + e.Message, e); } return ds; } #endregion #endregion internal void Delete(TransactionContext tc, Loan loan) { try { FMLoanDA.DeleteLoanLog(tc, loan.ID); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void Save(FMLoan loan) { FMLoan oloan = new FMLoan(); oloan = loan; string logstring = string.Empty; TransactionContext tc = null; FundType ofundtype = new FundTypeService().Get(loan.FundTypeID); var omemeber = new MemberService().Get(loan.MemberID); if (loan.IssueDate < ofundtype.fiscalYearStartDate || loan.IssueDate > ofundtype.fiscalYearEndDate) { throw new Exception("Loan issue date must be between current fiscal year."); } List activityVoucherSetup = new ActivityVoucherSetupService().GetByProcessType( loan.FundTypeID, EnumFundProcessType.Loan_Issue); //activityVoucherSetup = activityVoucherSetup.FindAll(x => x.ReferenceType == EnumProcessReferenceType.Loan && x.ReferenceID == loan.LoanCategoryID); //if (activityVoucherSetup.Count == 0) //{ // throw new ServiceException("There is no voucher setup found for the Loan Issue."); //} //if (activityVoucherSetup.Count > 2) //{ // throw new ServiceException(" There must be two item in setup."); //} try { List records = new UserRecordService().Get(GlobalFunctions.Fund_UserObject_Loan, loan.FundTypeID); tc = TransactionContext.Begin(true); if (oloan.IsNew) { oloan.Description = "Issue Loan"; int id = tc.GenerateID("FM_Loan", "LoanID"); base.SetObjectID(oloan, id); oloan.CreatedDate = DateTime.Now; FMLoanDA.Insert(tc, oloan); #region LoanAttachment if (oloan.LoanAttachment.FileData != null) { oloan.LoanAttachment.LoanID = oloan.ID; oloan.LoanAttachment.uploadDate = DateTime.Now; oloan.LoanAttachment.uploadBy = oloan.MemberID; new FMLoanAttachmentService().Save(tc, oloan.LoanAttachment); } #endregion LoanAttachment } else { FMLoanDA.Update(tc, oloan); } FMLoanDA.DeleteLoanSchedule(tc, oloan.ID, oloan.FundTypeID); foreach (var item in oloan.LoanSchedules) { item.FundTypeID = oloan.FundTypeID; item.CustomerID = oloan.MemberID; item.LoanID = oloan.ID; //item.PayDate = null; item.LoanCategoryID = oloan.LoanCategoryID; item.CreatedBy = oloan.CreatedBy; item.CreatedDate = oloan.CreatedDate; int id = tc.GenerateID("FM_LoanSchedule", "LoanScheduleID"); base.SetObjectID(item, id); FMLoanDA.Insert(tc, item); } if (loan.LoanApplicationID != null) { LoanApplicationDA.UpdateLoanIssueDate(tc, loan.LoanApplicationID ?? 0, loan.IssueDate); LoanApplicationDA.UpdateLoanStatus(tc, loan.LoanApplicationID ?? 0, EnumLoanWFStatus.Issued); } var newItem = records.FirstOrDefault(x => x.Field1 == loan.LoanNo); // create userRecord UserRecord oRecord = new UserRecord(); if (newItem == null) { oRecord.Field1 = loan.LoanNo; oRecord.Field2 = loan.Description + ", Loan amount:" + loan.PrincipalAmount; oRecord.FundTypeID = loan.FundTypeID; oRecord.UserObjectID = GlobalFunctions.Fund_UserObject_Loan; oRecord.CreatedBy = loan.CreatedBy; oRecord.CreatedDate = DateTime.Now; int id = tc.GenerateID("FM_USERRECORD", "USERRECORDID"); base.SetObjectID(oRecord, (id)); UserRecordDA.Insert(tc, oRecord); } else throw new Exception("This Certificate No is Already Available!"); GLTran otran = new GLTran(); otran.VoucherDate = loan.IssueDate; otran.ChequeDate = loan.CheckDate; otran.CreatedDate = DateTime.Now; otran.CreatedBy = loan.CreatedBy; otran.ChequeNo = loan.CheckNo; otran.GLTranType = EnumGLTranType.LoanIssue; otran.FundTypeID = loan.FundTypeID; otran.Description = "Loan Issue for Member :" + omemeber.EmpCode; // get the voucher setup of Loan // find the Bank GL from the Voucher setup ActivityVoucherSetup LoanbankGL = activityVoucherSetup.FirstOrDefault(x => x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Bank || x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Contra); if (LoanbankGL == null) { // if there is no bank GL throw exception throw new Exception("There is no bank GL in voucher setup"); } // Create a GL Tran Detail by this Bank GL and Amount will be Loan amount GL ogl = new GLService().Get(tc, LoanbankGL.GLID); GLTranDetail bankGL = new GLTranDetail(); bankGL.Transide = LoanbankGL.GLSide; bankGL.Amount = loan.PrincipalAmount; bankGL.GLID = ogl.ID; otran.CreditAmount = (double)loan.PrincipalAmount; otran.GlTranDetails = new List(); otran.GlTranDetails.Add(bankGL); ActivityVoucherSetup loanPrincipalSetup = activityVoucherSetup.Find(x => x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Loan_Principal && x.ReferenceType == EnumProcessReferenceType.Loan && x.ReferenceID == loan.LoanCategoryID); if (loanPrincipalSetup == null) { // if there is no GL throw exception throw new Exception("There is no Loan GL found in the setup"); } ogl = new GLService().Get(tc, loanPrincipalSetup.GLID); GLTranDetail loanGL = new GLTranDetail(); loanGL.Transide = loanPrincipalSetup.GLSide; loanGL.Amount = loan.PrincipalAmount; loanGL.GLID = ogl.ID; otran.DebitAmount = (double)loan.PrincipalAmount; otran.GlTranDetails.Add(loanGL); if (ogl.UserObjectID != null) { GLTranUserRecord orc = new GLTranUserRecord(); orc.Amount = loan.PrincipalAmount; orc.UserObject1ID = GlobalFunctions.Fund_UserObject_Loan; orc.GLID = loanGL.GLID; orc.UserRecord1ID = oRecord.ID; loanGL.GLTranUserRecords = new List(); loanGL.GLTranUserRecords.Add(orc); } new GLTranService().Save(tc, otran); oloan.GLTranID = otran.ID; FMLoanDA.UpdateGLTranID(tc, oloan); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public FMLoan GetByMemberID(int MemberID) { FMLoan loan = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(FMLoanDA.GetByMemberID(tc, MemberID)); if (dr.Read()) { loan = this.CreateObject(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 loan; } public List GetLoansByMemberID(int MemberID) { List loan = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(FMLoanDA.GetLoansByMemberID(tc, MemberID)); loan = this.CreateObjects(dr); dr.Close(); foreach (var item in loan) { LoadSchedule(tc, item); LoadCategory(tc, item); } //LoadCustomer(tc, loan); tc.End(); //tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return loan; } public void LoanOpeningSave(List oLoanIssues, List glTrans) { TransactionContext tc = null; try { foreach (FMLoan oloan in oLoanIssues) oloan.LoanNo = this.GenerateLoanNo(oloan); tc = TransactionContext.Begin(true); foreach (FMLoan oloan in oLoanIssues) { oloan.LoanAccNo = oloan.LoanNo; int id = tc.GenerateID("FM_Loan", "LoanID"); base.SetObjectID(oloan, id); FMLoanDA.Insert(tc, oloan); foreach (var item in oloan.LoanSchedules) { item.FundTypeID = oloan.FundTypeID; item.CustomerID = oloan.CustomerID; item.LoanID = oloan.ID; item.LoanCategoryID = oloan.LoanCategoryID; item.CreatedBy = oloan.CreatedBy; item.CreatedDate = oloan.CreatedDate; int newid = tc.GenerateID("FM_LoanSchedule", "LoanScheduleID"); base.SetObjectID(item, newid); FMLoanDA.Insert(tc, item); } UserRecord oRecord = new UserRecord(); oRecord.Field1 = oloan.LoanNo; oRecord.Field2 = oloan.Description + ", Loan amount:" + oloan.PrincipalAmount; oRecord.FundTypeID = oloan.FundTypeID; oRecord.UserObjectID = GlobalFunctions.Fund_UserObject_Loan; oRecord.CreatedBy = oloan.CreatedBy; oRecord.CreatedDate = DateTime.Now; int uid = tc.GenerateID("FM_USERRECORD", "USERRECORDID"); base.SetObjectID(oRecord, (uid)); UserRecordDA.Insert(tc, oRecord); foreach (GLTran otran in glTrans) { foreach (GLTranDetail detail in otran.GlTranDetails) { if (detail.GLTranUserRecords != null && detail.GLTranUserRecords.Count > 0) { var urItem = detail.GLTranUserRecords.FirstOrDefault(x => x.UserRecord1ID == oloan.MemberID); urItem.UserRecord1ID = uid; } } } } foreach (GLTran otran in glTrans) { new GLTranService().Save(tc, otran); } tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } } //public DataTable GetLoanInfoByMonth(DateTime scheduleMonth, int fundTypeID) //{ // DataTable dtLoanScheduleInfo = new DataTable(); // TransactionContext tc = null; // try // { // tc = TransactionContext.Begin(); // dtLoanScheduleInfo = FMLoanDA.GetLoanInfoByMonth(tc, scheduleMonth, fundTypeID); // tc.End(); // } // catch (Exception e) // { // #region Handle Exception // if (tc != null) // tc.HandleError(); // ExceptionLog.Write(e); // throw new ServiceException(e.Message, e); // #endregion // } // return dtLoanScheduleInfo; //} public FMLoan GetLoanForLoanApplicationDeposit(int MemberID, int InstallmentNo, double PrincipalAmount, DateTime issuefrom, DateTime issueToDate) { FMLoan loan = new FMLoan(); TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DataReader dr = new DataReader(FMLoanDA.GetLoanForLoanApplicationDeposit(tc, MemberID, InstallmentNo, PrincipalAmount, issuefrom, issueToDate)); loan = (dr.Read()) ? loan = this.CreateObject(dr) : null; dr.Close(); LoadSchedule(tc, loan); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return loan; } public List GetBySettlementStatus(DateTime fromDate, DateTime toDate, int settleStatus) { List loans = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DataReader dr = new DataReader(FMLoanDA.GetBySettleStatus(tc, fromDate, toDate, settleStatus)); loans = this.CreateObjects(dr); dr.Close(); LoanEarlySettlementAttachment attachment = new LoanEarlySettlementAttachment(); foreach (FMLoan loan in loans) { if (loan != null) { LoadSchedule(tc, loan); if (loan.LoanSchedules != null && loan.LoanSchedules.Count > 0) { foreach (FMLoanSchedule loanSchedule in loan.LoanSchedules) { if (loanSchedule.PayDate != null) { loan.TotalPaidInterest += loanSchedule.InstallmentInterest; loan.TotalPaidPrincipal += loanSchedule.InstallmentPrincipal; loan.TotalPaid += loanSchedule.InstallmentAmount; loan.TotalPaidInstallment++; } else { loan.TotalOutstandingPrincipal += loanSchedule.InstallmentPrincipal; loan.TotalOutstandingInterest += loanSchedule.InstallmentInterest; } loan.InstallmentAmount = loanSchedule.InstallmentAmount; loan.ToBeToatalInterest += loanSchedule.InstallmentInterest; loan.ToBeTotal += loanSchedule.InstallmentAmount; } } loan.InstallmentPrincipal = loan.LoanSchedules[0].InstallmentAmount; loan.InstallmentInterest = loan.LoanSchedules[0].InstallmentInterest; loan.TotalOutstanding = loan.ToBeTotal - loan.TotalPaid; loan.DisbursementMonth = loan.LoanSchedules[0].ScheduledPayDate; loan.LastSchedulePayDate = loan.LoanSchedules[loan.LoanSchedules.Count - 1].ScheduledPayDate; } loan.Member = new MemberService().Get(loan.MemberID); DataReader dr1 = new DataReader(FMLoanDA.GetLoanEarlySettlementAttachment(tc, loan.ID)); if (dr1.Read()) { attachment = this.CreateLoanAttachment(dr1); loan.LoanEarlySettlementAttachment = attachment; } dr1.Close(); } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); throw new ServiceException(e.Message, e); #endregion } return loans; } public void UpdateEarlySettlement(FMLoan oloan) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); FMLoanDA.UpdateLoanSettle(tc, oloan); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } } }