using Ease.Core.DataAccess; using Ease.Core.Model; using Ease.Core.Utility; using HRM.BO; using HRM.BO.Configuration; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Data; using System.IO; namespace HRM.DA { public class NewInvestmentService : ServiceTemplate, INewInvestmentService { public NewInvestmentService() { } #region MapObject For NewInvestment private void MapObject(NewInvestment newInvestment, DataReader oReader) { newInvestment.NewInvestmentID = oReader.GetInt32("NewInvestmentID").Value; newInvestment.TranMonth = oReader.GetDateTime("TranMonth").GetValueOrDefault(); newInvestment.Balance = oReader.GetDouble("Balance").GetValueOrDefault(); newInvestment.ApproverOne = oReader.GetInt32("ApproverOne").Value; newInvestment.ApproverOneRemarks = oReader.GetString("ApproverOneRemarks"); newInvestment.ApproverOneDate = oReader.GetDateTime("ApproverOneDate"); newInvestment.ApproverTwo = oReader.GetInt32("ApproverTwo").Value; newInvestment.ApproverTwoRemarks = oReader.GetString("ApproverTwoRemarks"); newInvestment.ApproverOneDate = oReader.GetDateTime("ApproverOneDate"); } protected override T CreateObject(DataReader oReader) { NewInvestment oNewInvestment = new NewInvestment(); MapObject(oNewInvestment, oReader); return oNewInvestment as T; } #endregion MapObject For NewInvestment #region MapObject For Child NewInvestMentInBank private void MapNewInvestmentInBank(NewInvestmentInBank newInvestmentInBank, DataReader dr) { base.SetObjectID(newInvestmentInBank, dr.GetInt32("NewInvestmentInBankID").Value); newInvestmentInBank.BankID = dr.GetInt32("BankID").Value; newInvestmentInBank.BankName = dr.GetString("BankName"); newInvestmentInBank.NewInvestmentID = dr.GetInt32("NewInvestmentID").Value; newInvestmentInBank.Amount = dr.GetDouble("Amount").Value; newInvestmentInBank.Ratio = dr.GetDouble("Ratio").Value; newInvestmentInBank.RatingID = dr.GetInt32("Rating").Value; newInvestmentInBank.ThreeMonthRate = dr.GetDouble("ThreeMonthRate").Value; newInvestmentInBank.SixMonthRate = dr.GetDouble("SixMonthRate").Value; newInvestmentInBank.TwelveMonthRate = dr.GetDouble("TwelveMonthRate").Value; newInvestmentInBank.ThreeYearRate = dr.GetDouble("ThreeYearRate").Value; newInvestmentInBank.FiveYearRate = dr.GetDouble("FiveYearRate").Value; } private NewInvestmentInBank CreateNewInvestmentInBanks(DataReader dr) { NewInvestmentInBank newInvestmentInBank = new NewInvestmentInBank(); MapNewInvestmentInBank(newInvestmentInBank, dr); return newInvestmentInBank; } private void FillNewInvestmentInBanks(NewInvestment parent, DataReader dr) { while (dr.Read()) { NewInvestmentInBank item = this.CreateNewInvestmentInBanks(dr); parent.NewInvestmentInBanks.Add(item); } } internal void LoadNewInvestmentInBank(TransactionContext tc, NewInvestment newInvestment) { try { if (newInvestment != null && newInvestment.ID != 0) { //Get LoanSchedule DataReader dr = new DataReader(NewInvestmentDA.GetNewInvestmentInBanks(tc, newInvestment.ID)); FillNewInvestmentInBanks(newInvestment, dr); dr.Close(); } } catch (Exception e) { throw new ServiceException(e.Message, e); } } #endregion #region Service Implemantation public NewInvestment Get(int id) { NewInvestment oNewInvestment = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(NewInvestmentDA.Get(tc, id)); if (oreader.Read()) { oNewInvestment = this.CreateObject(oreader); } oreader.Close(); LoadNewInvestmentInBank(tc, oNewInvestment); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oNewInvestment; } public NewInvestment GetByTranMonth(DateTime tranMonth) { NewInvestment oNewInvestment = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(NewInvestmentDA.Get(tc, tranMonth)); if (oreader.Read()) { oNewInvestment = this.CreateObject(oreader); } oreader.Close(); if(oNewInvestment != null) { LoadNewInvestmentInBank(tc, oNewInvestment); } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oNewInvestment; } public NewInvestment GetByMemberID(int MemberID) { NewInvestment oNewInvestment = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(NewInvestmentDA.GetByMemberID(tc, MemberID)); if (oreader.Read()) { oNewInvestment = this.CreateObject(oreader); } oreader.Close(); LoadNewInvestmentInBank(tc, oNewInvestment); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oNewInvestment; } public List GetAll() { List NewInvestmentList = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(NewInvestmentDA.GetAll(tc)); NewInvestmentList = 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 NewInvestmentList; } //public NewInvestment GetFromConfig(int currentUserID, DateTime date) //{ // NewInvestment newInvestment; // List _banks; // TransactionContext tc = null; // try // { // var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json"); // IConfiguration Configuration = builder.Build(); // newInvestment = new NewInvestment(); // Member approverOneId = new MemberService().Get(Configuration.GetSection("Approver")["ApproverOneId"]); // Member approverTwoId = new MemberService().Get(Configuration.GetSection("Approver")["ApproverOneId"]); // //NewInvestment // newInvestment.ApproverOne = approverOneId.ID; // newInvestment.ApproverTwo = approverTwoId.ID; // newInvestment.CreatedDate = DateTime.Now; // newInvestment.CreatedBy = currentUserID; // GL gl; // //NewInvestmentInBanks // _banks = new FundBankService().Get(); // foreach (FundBank oItem in _banks) // { // //Rating rating = Rating.Get(oItem.RatingID); // string Balance = Configuration.GetSection("Approver")["Balance"]; // gl = new GLService().Get(Balance); // decimal balance = 0; // if (gl != null) // { // balance = Math.Round(Math.Abs(new GLTranService().GetOpeningBalance(gl.ID, date)), 2); // newInvestment.Balance = (double)balance; // } // NewInvestmentInBank newInvestmentInBank = new NewInvestmentInBank(); // newInvestmentInBank.BankID = oItem.ID; // newInvestmentInBank.Amount = (double)balance * 0.7; // newInvestmentInBank.Ratio = oItem.Ratio; // newInvestmentInBank.RatingID = 0; // newInvestmentInBank.BankName = oItem.Name; // newInvestmentInBank.ThreeMonthRate = oItem.ThreeMonthInterest; // newInvestmentInBank.SixMonthRate = oItem.SixMonthInterest; // newInvestmentInBank.TwelveMonthRate = oItem.OneYearInterest; // newInvestmentInBank.ThreeYearRate = oItem.ThreeMonthInterest; // newInvestmentInBank.FiveYearRate = oItem.FiveYearInterest; // //newInvestmentInBanks.Add(newInvestmentInBank); // newInvestment.NewInvestmentInBanks.Add(newInvestmentInBank); // } // } // catch (Exception e) // { // #region Handle Exception // if (tc != null) // tc.HandleError(); // ExceptionLog.Write(e); // throw new ServiceException(e.Message, e); // #endregion // } // return newInvestment; //} #region Send Mail public string SendMailToHr(EmailSettings emailSettings, int HrID) { 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 body = "Dear <>," + "

" + "Your New Investment Request has been Approved.
" + "Please go to " + emailSettings.WebAddress + " for details."; try { body = body.Replace("<>", Name); body = body.Replace("<>", EmployeeCode); MailSender mailSender = new MailSender(); mailSender.AddTo(mailAddress); mailSender.Subject = "New Investment Request"; mailSender.Body = "

" + body + "

"; mailSender.ThreadSendMail(emailSettings); } catch (Exception) { error = "Could Not Send Mail!"; } return error; } #endregion public NewInvestment GetNewInvestmentById(int NewInvestmentId) { TransactionContext tc = null; try { var oNewInvestment = new NewInvestment(); tc = TransactionContext.Begin(); DataReader oreader = new DataReader(NewInvestmentDA.GetNewInvestmentById(tc, NewInvestmentId)); if (oreader.Read()) { oNewInvestment = this.CreateObject(oreader); } oreader.Close(); LoadNewInvestmentInBank(tc, oNewInvestment); tc.End(); return oNewInvestment; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public int UpdateNewInvestmentApproval(NewInvestment oNewInvestment) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); NewInvestmentDA.UpdateNewInvestmentApproval(tc, oNewInvestment); return oNewInvestment.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } } public void Save(NewInvestment newInvestment) { NewInvestment oNewInvestment = new NewInvestment(); oNewInvestment = newInvestment; List oNewInvestmentInBanks = oNewInvestment.NewInvestmentInBanks; TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oNewInvestment.IsNew) { int id = tc.GenerateID("NewInvestment", "NewInvestmentID"); base.SetObjectID(oNewInvestment, id); NewInvestmentDA.Insert(tc, oNewInvestment); NewInvestmentDA.DeleteNewInvestMentInBank(tc, id); foreach (NewInvestmentInBank oitem in oNewInvestmentInBanks) { int iD = tc.GenerateID("NewInvestmentInBank", "NewInvestmentInBankID"); base.SetObjectID(oitem, iD); oitem.NewInvestmentID = id; NewInvestmentDA.InsertNewInvestMentInBank(tc, oitem); } } else { NewInvestmentDA.Update(tc, oNewInvestment); NewInvestmentDA.DeleteNewInvestMentInBank(tc, oNewInvestment.NewInvestmentID); foreach (NewInvestmentInBank oitem in oNewInvestmentInBanks) { NewInvestmentDA.InsertNewInvestMentInBank(tc, oitem); } } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } } public void Delete(int id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); NewInvestmentDA.Delete(tc, id); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } } public NewInvestment GetFromConfig(int currentUserID, DateTime date) { throw new NotImplementedException(); } #endregion Service Implemantation } }