HRM-10/HRM.DA/Service/Fund/Investment/InvestmentService.cs
Mohaimen Hasan 838b8d4480 first commit
2026-11-03 13:06:00 +06:00

1704 lines
73 KiB
C#

using System;
using System.Data;
using Ease.Core.Model;
using Ease.Core.DataAccess;
using Ease.Core;
using System.Collections.Generic;
using Ease.Core.Utility;
using HRM.BO;
using HRM.DA;
using System.Data.Common;
using System.Linq;
namespace HRM.DA.Fund
{
[Serializable]
public class InvestmentService : ServiceTemplate, IInvestmentService
{
#region Private functions and declaration
public InvestmentService()
{
}
private void MapObject(InvestmentInfo oInvestment, DataReader oReader)
{
base.SetObjectID(oInvestment, oReader.GetInt32("ID").Value);
oInvestment.AccountNo = oReader.GetString("AccountNo");
oInvestment.BankID = oReader.GetInt32("BankID").Value;
oInvestment.BranchID = oReader.GetInt32("BranchID").Value;
oInvestment.CategoryID = oReader.GetInt32("CategoryID").Value;
oInvestment.FundTypeID = oReader.GetInt32("FundTypeID").Value;
oInvestment.CertificateNo = oReader.GetString("CertificateNo");
oInvestment.Amount = oReader.GetDouble("Amount").Value;
oInvestment.TotalProAccruedAmount = oReader.GetDouble("TotalProAccruedAmount");
oInvestment.LastProAccruedDate = oReader.GetDateTime("LastProAccruedDate");
oInvestment.CurrentStatus = (EnumInvestmentStatus)oReader.GetInt32("CurrentStatus");
oInvestment.Description = oReader.GetString("Description");
oInvestment.TenureType = (EnumTenureType)oReader.GetInt32("TenureType").Value;
oInvestment.IssueDate = oReader.GetDateTime("IssueDate").Value;
oInvestment.VoucherDate = oReader.GetDateTime("VoucherDate") == null ? DateTime.Now : oReader.GetDateTime("VoucherDate").Value;
oInvestment.MatureDate = oReader.GetDateTime("MatureDate").Value;
oInvestment.RefID = oReader.GetInt32("RefID");
oInvestment.Tenure = oReader.GetInt32("Tenure").Value;
oInvestment.CalculationBase = oReader.GetInt32("CalculationBase");
oInvestment.EncashmentDate = oReader.GetDateTime("EncashmentDate");
oInvestment.RenewalDate = oReader.GetDateTime("RenewalDate");
oInvestment.UserRecordID = oReader.GetInt32("UserRecordID");
oInvestment.GLTranID = oReader.GetInt32("GLTranID") == null ? 0 : oReader.GetInt32("GLTranID").Value;
oInvestment.EncashGLTranID = oReader.GetInt32("ENCASHGLTRANID") == null ? 0 : oReader.GetInt32("ENCASHGLTRANID").Value;
oInvestment.CreatedBy = oReader.GetInt32("CreatedBy").Value;
oInvestment.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
oInvestment.ModifiedBy = oReader.GetInt32("ModifiedBy");
oInvestment.ModifiedDate = oReader.GetDateTime("ModifiedDate");
base.SetObjectState(oInvestment, ObjectState.Saved);
}
private InvestmentInfo CreateObject(DataReader oReader)
{
InvestmentInfo oInvestment = new InvestmentInfo();
MapObject(oInvestment, oReader);
return oInvestment;
}
private List<InvestmentInfo> CreateObjects(IDataReader oReader)
{
List<InvestmentInfo> oInvestmentInfos = new List<InvestmentInfo>();
DataReader oreader = new DataReader(oReader);
while (oReader.Read())
{
InvestmentInfo oItem = CreateObject(oreader);
oInvestmentInfos.Add(oItem);
}
return oInvestmentInfos;
}
#region InvestmentSchedule child load
private void MapInvestmentSchedule(InvestmentSchedule investSchedule, DataReader dr)
{
base.SetObjectID(investSchedule, dr.GetInt32("ID").Value);
investSchedule.InvestmentID = dr.GetInt32("InvestmentID").Value;
investSchedule.FundTypeID = dr.GetInt32("FundTypeID").Value;
investSchedule.FromDate = dr.GetDateTime("FromDate").Value;
investSchedule.Percentage = dr.GetFloat("Percentage").Value;
investSchedule.RecalculationNeeded = dr.GetBoolean("RecalculationNeeded").Value;
investSchedule.TillDate = dr.GetDateTime("TillDate").Value;
investSchedule.TranDate = dr.GetDateTime("TranDate").Value;
investSchedule.CreatedBy = dr.GetInt32("CreatedBy").Value;
investSchedule.CreatedDate = dr.GetDateTime("CreatedDate").Value;
investSchedule.ModifiedBy = dr.GetInt32("ModifiedBy");
investSchedule.ModifiedDate = dr.GetDateTime("ModifiedDate");
base.SetObjectState(investSchedule, ObjectState.Saved);
}
private void MapInvestmentSignatory(InvestmentSignatory investSignatory, DataReader dr)
{
base.SetObjectID(investSignatory, dr.GetInt32("ID").Value);
investSignatory.InvestmentID = dr.GetInt32("InvestmentID").Value;
investSignatory.FundTypeID = dr.GetInt32("FundTypeID").Value;
investSignatory.TrustyID = dr.GetInt32("TrustyID").Value;
investSignatory.CreatedBy = dr.GetInt32("CreatedBy").Value;
investSignatory.CreatedDate = dr.GetDateTime("CreatedDate").Value;
investSignatory.ModifiedBy = dr.GetInt32("ModifiedBy");
investSignatory.ModifiedDate = dr.GetDateTime("ModifiedDate");
base.SetObjectState(investSignatory, ObjectState.Saved);
}
private InvestmentSchedule CreateInvestmentSchedules(DataReader dr)
{
InvestmentSchedule investSchedule = new InvestmentSchedule();
MapInvestmentSchedule(investSchedule, dr);
return investSchedule;
}
private InvestmentSignatory CreateInvestmentSignatorys(DataReader dr)
{
InvestmentSignatory investSignatory = new InvestmentSignatory();
MapInvestmentSignatory(investSignatory, dr);
return investSignatory;
}
private void FillInvestmentSchedules(InvestmentInfo parent, DataReader dr)
{
while (dr.Read())
{
InvestmentSchedule item = this.CreateInvestmentSchedules(dr);
parent.InvestmentSchedules.Add(item);
}
}
private void FillInvestmentSignatorys(InvestmentInfo parent, DataReader dr)
{
while (dr.Read())
{
InvestmentSignatory item = this.CreateInvestmentSignatorys(dr);
parent.InvestmentSignatorys.Add(item);
}
}
private void LoadChildren(TransactionContext tc, InvestmentInfo investment)
{
try
{
if (investment != null && investment.ID != 0)
{
DataReader dr = new DataReader(InvestmentScheduleDA.GetByParent(tc, investment.ID));
FillInvestmentSchedules(investment, dr);
dr.Close();
dr = new DataReader(InvestmentDA.GetByParentSinatory(tc, investment.ID));
FillInvestmentSignatorys(investment, dr);
dr.Close();
investment.InvestmentTrans = new InvestmentTranService().Get(tc, investment);
}
}
catch (Exception e)
{
throw e;
}
}
private void LoadChildren(TransactionContext tc, InvestmentInfo investment, DateTime processMonth)
{
try
{
if (investment != null && investment.ID != 0)
{
DataReader dr = new DataReader(InvestmentScheduleDA.GetByParent(tc, investment.ID, processMonth));
FillInvestmentSchedules(investment, dr);
dr.Close();
dr = new DataReader(InvestmentDA.GetByParentSinatory(tc, investment.ID));
FillInvestmentSignatorys(investment, dr);
dr.Close();
investment.InvestmentTrans = new InvestmentTranService().Get(tc, investment);
}
}
catch (Exception e)
{
throw e;
}
}
#endregion InvestmentSchedule child load
#endregion
#region Service implementation
#region Get By InvestmentInfoID
public InvestmentInfo Get(int investmentInfoID)
{
InvestmentInfo investment = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(InvestmentDA.Get(tc, investmentInfoID));
if (dr.Read())
{
investment = this.CreateObject<InvestmentInfo>(dr);
}
dr.Close();
LoadChildren(tc, investment);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
#region Cache Footer
#endregion
return investment;
}
#endregion
public InvestmentInfo GetbyGLTranId(TransactionContext tc, int investmentInfoID)
{
InvestmentInfo investment = null;
try
{
DataReader dr = new DataReader(InvestmentDA.GetbyGLTranID(tc, investmentInfoID));
if (dr.Read())
{
investment = this.CreateObject<InvestmentInfo>(dr);
}
dr.Close();
LoadChildren(tc, investment);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
#region Cache Footer
#endregion
return investment;
}
public DataTable GetPFInvestment(DateTime Fromdate, DateTime toDate)
{
DataTable dTbl = new DataTable("PFInvestment");
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
IDataReader ir = InvestmentDA.GetPFInvestment(tc, Fromdate, toDate);
dTbl.Load(ir);
ir.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 dTbl;
}
public List<InvestmentInfo> GetInvestments(DateTime fromdate, DateTime toDate, int fundTypeID)
{
List<InvestmentInfo> items = new List<InvestmentInfo>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
IDataReader dataReader = InvestmentDA.GetInvestments(tc, fromdate, toDate, fundTypeID);
items = CreateObjects(dataReader);
dataReader.Close();
tc.End();
dataReader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
#region Cache Footer
#endregion
return items;
}
public DataSet GetIndvInvestment(int investmentID, DateTime fromDate, DateTime toDate)
{
DataSet dTbl = new DataSet("IndividualInvestment");
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
dTbl = InvestmentDA.GetIndvInvestment(tc, investmentID, 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 GetInvestmentBSP(DateTime Fromdate, DateTime toDate)
{
DataTable dTbl = new DataTable("Investment");
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
IDataReader ir = InvestmentDA.GetInvestmentBSP(tc, Fromdate, toDate);
dTbl.Load(ir);
ir.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 dTbl;
}
public DataSet GetStatementInvestment(string ssta1glID, DateTime dtFirst, DateTime dtLast)
{
DataSet dTbl = new DataSet("StatementInvestment");
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
dTbl = InvestmentDA.GetStatementInvestment(tc, ssta1glID, dtFirst, dtLast);
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 List<InvestmentInfo> Getbyfundtype(int fundtypeid)
{
List<InvestmentInfo> items = new List<InvestmentInfo>();
List<InvestmentInfo> itemsRead = new List<InvestmentInfo>();
items = new List<InvestmentInfo>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dataReader = new DataReader(InvestmentDA.Getbyfundtype(tc, fundtypeid));
while (dataReader.Read())
{
InvestmentInfo item = this.CreateObject(dataReader);
LoadChildren(tc, item);
itemsRead.Add(item);
}
dataReader.Close();
foreach (InvestmentInfo item in itemsRead)
{
LoadChildren(tc, item);
items.Add(item);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
#region Cache Footer
#endregion
return items;
}
public bool GetIsUsedUserObjectName(int categoryid)
{
bool found = false;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dataReader = new DataReader(InvestmentDA.Get(tc, categoryid));
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;
}
public List<InvestmentInfo> GetBySearch(string sSearch, DateTime fromDate, DateTime toDate, int fundtypeid)
{
List<InvestmentInfo> oInvestmentInfos = new List<InvestmentInfo>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
IDataReader oReader = InvestmentDA.GetBySearch(tc, sSearch, fromDate, toDate, fundtypeid);
oInvestmentInfos = CreateObjects(oReader);
oReader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message);
#endregion
}
return oInvestmentInfos;
}
public List<InvestmentInfo> GetBySearch(string sSearch, int fundTypeID)
{
List<InvestmentInfo> oInvestmentInfos = new List<InvestmentInfo>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
IDataReader oReader = InvestmentDA.GetBySearch(tc, sSearch, fundTypeID);
oInvestmentInfos = CreateObjects(oReader);
oReader.Close();
foreach (InvestmentInfo o in oInvestmentInfos)
{
LoadChildren(tc, o);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message);
#endregion
}
return oInvestmentInfos;
}
public List<InvestmentInfo> GetByMatureDateAndStatus(string sSearch, int fundTypeID)
{
List<InvestmentInfo> oInvestmentInfos = new List<InvestmentInfo>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
IDataReader oReader = InvestmentDA.GetByMatureDateAndStatus(tc, sSearch, fundTypeID);
oInvestmentInfos = CreateObjects(oReader);
oReader.Close();
foreach (InvestmentInfo o in oInvestmentInfos)
{
LoadChildren(tc, o);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message);
#endregion
}
return oInvestmentInfos;
}
public List<InvestmentInfo> GetAccrualInfo(string sSearch, int fundTypeID, DateTime processMonth)
{
List<InvestmentInfo> oInvestmentInfos = new List<InvestmentInfo>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
IDataReader oReader = InvestmentDA.GetBySearch(tc, sSearch, fundTypeID);
oInvestmentInfos = CreateObjects(oReader);
oReader.Close();
foreach (InvestmentInfo o in oInvestmentInfos)
{
LoadChildren(tc, o, processMonth);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message);
#endregion
}
return oInvestmentInfos;
}
public decimal GetAmountBySearch(string sSearch)
{
decimal amount = 0;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
amount = InvestmentDA.GetAmountBySearch(tc, sSearch);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message);
#endregion
}
return amount;
}
protected override T CreateObject<T>(DataReader dataReader)
{
InvestmentInfo investment = new InvestmentInfo();
MapObject(investment, dataReader);
return investment as T;
}
public void Save(InvestmentInfo investment)
{
TransactionContext tc = null;
InvestmentCategory investmentCategory = new InvestmentCategoryService().Get(investment.CategoryID);
List<ActivityVoucherSetup> activityVoucherSetup = new ActivityVoucherSetupService().GetByProcessType(investment.FundTypeID, EnumFundProcessType.Investment_Purchase); // fundProcessType
try
{
tc = TransactionContext.Begin(true);
int userRecordID = 0;
if (investment.IsNew)
{
int ID = tc.GenerateID("FM_Investment", "ID");
base.SetObjectID(investment, (ID));
InvestmentDA.Insert(tc, investment);
//Delete Children
InvestmentDA.DeleteSingnatoryByParent(tc, investment.ID);
InvestmentScheduleDA.DeleteByParent(tc, investment.ID);
InvestmentTranDA.DeleteByParent(tc, investment.ID);
InvestmentLogDA.DeleteByParent(tc, investment.ID);
foreach (InvestmentSchedule item in investment.InvestmentSchedules)
{
item.InvestmentID = investment.ID;
int id = tc.GenerateID("FM_InvestmentSchedule", "ID");
base.SetObjectID(item, (id));
InvestmentScheduleDA.Insert(tc, item);
}
foreach (InvestmentSignatory item in investment.InvestmentSignatorys)
{
item.InvestmentID = investment.ID;
int id = tc.GenerateID("FM_InvestmentSignatory", "ID");
base.SetObjectID(item, (id));
InvestmentDA.Insert(tc, item);
}
}
else
{
InvestmentDA.Update(tc, investment);
}
List<UserRecord> records = new UserRecordService().Get(investment.FundTypeID, GlobalFunctions.Fund_UserObject_Investment);
var newItem = records.FirstOrDefault(x => x.Field1 == investment.CertificateNo);
// create userRecord
UserRecord oRecord = new UserRecord();
if (newItem == null)
{
oRecord.Field1 = investment.CertificateNo;
oRecord.Field2 = "Date: " + investment.VoucherDate.ToString("dd MMM yyyy") + " - " + investmentCategory.Name + " - Investment Purchase, Amount: " + investment.Amount + ". Certificate No: " + investment.CertificateNo;
oRecord.FundTypeID = investment.FundTypeID;
oRecord.UserObjectID = GlobalFunctions.Fund_UserObject_Investment;
oRecord.CreatedBy = investment.CreatedBy;
oRecord.CreatedDate = DateTime.Now;
userRecordID = tc.GenerateID("FM_USERRECORD", "USERRECORDID");
base.SetObjectID(oRecord, (userRecordID));
UserRecordDA.Insert(tc, oRecord);
}
else throw new Exception("This Certificate No is Already Available!");
GLTran otran = new GLTran();
otran.VoucherDate = investment.VoucherDate;
otran.ChequeDate = investment.VoucherDate;
otran.CreatedDate = DateTime.Now;
otran.CreatedBy = investment.CreatedBy;
otran.ChequeNo = investment.CertificateNo;
otran.GLTranType = EnumGLTranType.InvestmentPurchase;
otran.FundTypeID = investment.FundTypeID;
otran.Description = "Date: " + investment.VoucherDate.ToString("dd MMM yyyy") + " - " + investmentCategory.Name + " - Investment Purchase, Amount: " + investment.Amount + ". Certificate No: " + investment.CertificateNo;
ActivityVoucherSetup bankVS = activityVoucherSetup.FirstOrDefault(x => x.FundProcessType == EnumFundProcessType.Investment_Purchase
&& x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Bank); // EPF
if (bankVS == null)
{
throw new Exception("There is no bank GL in the voucher setup.");
}
GL ogl = new GLService().Get(tc, bankVS.GLID);
GLTranDetail bankGL = new GLTranDetail();
bankGL.Transide = (EnumTranSide)bankVS.GLSide;
bankGL.Amount = investment.Amount;
bankGL.GLID = ogl.ID;
otran.GlTranDetails = new List<GLTranDetail>();
otran.GlTranDetails.Add(bankGL);
ActivityVoucherSetup investmentVS = activityVoucherSetup.FirstOrDefault(x => x.FundProcessType == EnumFundProcessType.Investment_Purchase && x.ReferenceType
== EnumProcessReferenceType.Investment && x.ReferenceID == investment.CategoryID && x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Investment_Principal);
if (investmentVS == null)
{
// if there is no Investment GL throw exception
throw new Exception("There is no Investment GL found in the setup");
}
ogl = new GLService().Get(tc, investmentVS.GLID);
GLTranDetail InvestmentkGL = new GLTranDetail();
InvestmentkGL.Transide = (EnumTranSide)investmentVS.GLSide;
InvestmentkGL.Amount = investment.Amount;
InvestmentkGL.GLID = ogl.ID;
GLTranUserRecord orc = new GLTranUserRecord();
orc.Amount = investment.Amount;
orc.UserObject1ID = GlobalFunctions.Fund_UserObject_Investment;
orc.GLID = InvestmentkGL.GLID;
orc.UserRecord1ID = oRecord.ID;
InvestmentkGL.GLTranUserRecords = new List<GLTranUserRecord>();
InvestmentkGL.GLTranUserRecords.Add(orc);
otran.GlTranDetails.Add(InvestmentkGL);
otran.DebitAmount = (double)investment.Amount;
otran.CreditAmount = (double)investment.Amount;
int GLTranID = new GLTranService().Save(tc, otran);
if( GLTranID < 0)
{
throw new Exception("Failed to Create Voucher!");
}
InvestmentDA.UpdateInvestmentGLAndUserRecord(tc, investment.ID, GLTranID, userRecordID);
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 ReschduleInterestRate(InvestmentInfo investment, bool isCollectionModified)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (!investment.IsNew)
{
investment.ModifiedBy = investment.ModifiedBy;
investment.ModifiedDate = DateTime.Now;
InvestmentDA.Update(tc, investment);
}
if (isCollectionModified)
{
InvestmentScheduleDA.DeleteByParent(tc, investment.ID);
foreach (InvestmentSchedule item in investment.InvestmentSchedules)
{
item.ModifiedBy = investment.ModifiedBy;
item.ModifiedDate = DateTime.Now;
item.InvestmentID = investment.ID;
int id = tc.GenerateID("InvestmentSchedule", "ID");
base.SetObjectID(item, (id));
InvestmentScheduleDA.Insert(tc, item);
}
foreach (InvestmentLog item in investment.InvestmentLogs)
{
item.InvestmentID = investment.ID;
int id = tc.GenerateID("InvestmentLog", "ID");
base.SetObjectID(item, (id));
InvestmentLogDA.Insert(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 UpdateAccruedAndStatus(InvestmentInfo investment, TransactionContext tc)
{
try
{
InvestmentDA.UpdateAccruedAndStatus(tc, investment);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void UpdateInvestmentGLTranID(TransactionContext tc, int investmentID, int GLTranID)
{
try
{
InvestmentDA.UpdateInvestmentGLTranID(tc, investmentID, GLTranID);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void UpdateEncashAndStatus(InvestmentInfo investment, TransactionContext tc)
{
try
{
InvestmentDA.UpdateEncashAndStatus(tc, investment);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void UpdateRenewalAndStatus(InvestmentInfo investment, TransactionContext tc)
{
try
{
InvestmentDA.UpdateRenewalAndStatus(tc, investment);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void SaveMonthlyBenifitsAccrual(InvestmentInfo investment)
{
List<ActivityVoucherSetup> vsProfit = new ActivityVoucherSetupService().GetByProcessType(
investment.FundTypeID, EnumFundProcessType.Monthly_Investment_Accrual);
List<ActivityVoucherSetup> vsAIT = new ActivityVoucherSetupService().GetByProcessType(
investment.FundTypeID, EnumFundProcessType.Investment_Purchase_AIT);
InvestmentCategory investmentCategory = new InvestmentCategoryService().Get(investment.CategoryID);
TransactionContext tc = null;
List<GLTran> otrans = new List<GLTran>();
InvestmentInfo oldinvestment = new InvestmentService().Get(investment.ID);
investment.AccrualInterestAIT = Math.Round(investment.AccrualInterestAIT, 2);
investment.AccrualInterest = Math.Round(investment.AccrualInterest, 2);
try
{
tc = TransactionContext.Begin(true);
InvestmentTran tmepItem = new InvestmentTran();
tmepItem.InvestmentAIT = investment.AccrualInterestAIT;
tmepItem.PrincipalAmount = investment.AccrualInterest;
tmepItem.InvestmentID = investment.ID;
tmepItem.FundTypeID = investment.FundTypeID;
tmepItem.TranTypeID = EnumInvestmentActivities.InvestMonthlyAccrualInterest;
tmepItem.TranDate = investment.VoucherDate;
tmepItem.CreatedBy = investment.CreatedBy;
tmepItem.CreatedDate = investment.CreatedDate;
int idx = tc.GenerateID("FM_InvestmentTran", "TRANID");
base.SetObjectID(tmepItem, (idx));
InvestmentTranDA.Insert(tc, tmepItem);
investment.AccrualInterestAIT = investment.AccrualInterestAIT + oldinvestment.AccrualInterestAIT;
double amount = oldinvestment.TotalProAccruedAmount == null ? 0 : (double)oldinvestment.TotalProAccruedAmount;
investment.TotalProAccruedAmount = investment.AccrualInterest + amount;
UpdateAccruedAndStatus(investment, tc);
//SaveVoucher 1
//vsProfit.FindAll(x => x.ReferenceType == EnumProcessReferenceType.Investment
//&& x.ReferenceID == investment.CategoryID);
if (vsProfit.Count == 0)
{
throw new ServiceException(" There is no voucher setup found for the investment accruall.");
}
GLTran otranIncome = new GLTran();
otranIncome.VoucherDate = investment.VoucherDate;
otranIncome.ChequeDate = investment.VoucherDate;
otranIncome.FundTypeID = investment.FundTypeID;
otranIncome.CreatedDate = DateTime.Now;
otranIncome.CreatedBy = investment.CreatedBy;
otranIncome.ChequeNo = investment.CertificateNo;
otranIncome.GLTranType = EnumGLTranType.MonthlyInvestmentAccrual;
otranIncome.DebitAmount = (double)tmepItem.PrincipalAmount;
otranIncome.CreditAmount = (double)tmepItem.PrincipalAmount;
otranIncome.Description = investment.Description;
otranIncome.Description = "Date: " + investment.VoucherDate.ToString("dd MMM yyyy") + " - " + investmentCategory.Name + " - Investment Accrual (Income), Amount: " + tmepItem.PrincipalAmount + ". Certificate No: " + investment.CertificateNo;
ActivityVoucherSetup IncomeVS = vsProfit.FirstOrDefault(x => x.ReferenceID == investment.CategoryID &&
x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Investment_Income);
if (IncomeVS == null)
{
throw new ServiceException(" There is no voucher setup found for the investment income.");
}
GL ogl = new GLService().Get(tc, IncomeVS.GLID);
GLTranDetail IncomeGL = new GLTranDetail();
IncomeGL.Transide = IncomeVS.GLSide;
IncomeGL.Amount = tmepItem.PrincipalAmount;
IncomeGL.GLID = ogl.ID;
otranIncome.GlTranDetails = new List<GLTranDetail>();
otranIncome.GlTranDetails.Add(IncomeGL);
double contracmount = tmepItem.PrincipalAmount;
ActivityVoucherSetup IncomeAITVS = vsProfit.FirstOrDefault(x => x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Investment_AIT);
if (IncomeAITVS == null)
{
throw new ServiceException(" There is no voucher setup found for the investment AIT.");
}
if (IncomeAITVS != null)
{
ogl = new GLService().Get(tc, IncomeAITVS.GLID);
GLTranDetail IncomeAITGL = new GLTranDetail();
IncomeAITGL.Transide = IncomeAITVS.GLSide;
IncomeAITGL.Amount = Convert.ToDouble(tmepItem.InvestmentAIT);
IncomeAITGL.GLID = ogl.ID;
contracmount = contracmount - (double)tmepItem.InvestmentAIT;
otranIncome.GlTranDetails.Add(IncomeAITGL);
}
ActivityVoucherSetup ContraIncomeVS = vsProfit.FirstOrDefault(x => x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Contra
&& x.ReferenceID == investment.CategoryID);
if (ContraIncomeVS == null)
{
throw new ServiceException("There is no voucher setup found for the investment contra.");
}
ogl = new GLService().Get(tc, ContraIncomeVS.GLID);
GLTranDetail ContraIncomeGL = new GLTranDetail();
ContraIncomeGL.Transide = ContraIncomeVS.GLSide;
ContraIncomeGL.Amount = contracmount;
ContraIncomeGL.GLID = ogl.ID;
otranIncome.GlTranDetails.Add(ContraIncomeGL);
otrans.Add(otranIncome);
if (vsAIT != null && vsAIT.Count > 0)
{
vsAIT = vsAIT.FindAll(x => x.ReferenceType == EnumProcessReferenceType.Investment);
if (vsAIT.Count == 0)
{
throw new ServiceException(" There is no voucher setup found for the investment purchase AIT.");
}
var AITExpenceVS = vsAIT.FirstOrDefault(x => x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Investment_AIT);
if (AITExpenceVS == null)
{
throw new ServiceException(" There is no voucher setup found for the investment AIT Expense.");
}
GLTran oAittran = new GLTran();
oAittran.VoucherDate = investment.VoucherDate;
oAittran.ChequeDate = investment.VoucherDate;
oAittran.FundTypeID = investment.FundTypeID;
oAittran.CreatedDate = DateTime.Now;
oAittran.CreatedBy = investment.CreatedBy;
oAittran.ChequeNo = investment.CertificateNo;
oAittran.GLTranType = EnumGLTranType.MonthlyInvestmentAccrualAIT;
oAittran.DebitAmount = (double)tmepItem.InvestmentAIT;
oAittran.CreditAmount = (double)tmepItem.InvestmentAIT;
oAittran.Description = "Date: " + investment.VoucherDate.ToString("dd MMM yyyy") + " - " + investmentCategory.Name + " - Investment Accrual (AIT), Amount: " + tmepItem.InvestmentAIT + ". Certificate No: " + investment.CertificateNo;
ogl = new GLService().Get(tc, AITExpenceVS.GLID);
GLTranDetail aitExpenceGL = new GLTranDetail();
aitExpenceGL.Transide = AITExpenceVS.GLSide;
aitExpenceGL.Amount = Convert.ToDouble(tmepItem.InvestmentAIT);
aitExpenceGL.GLID = ogl.ID;
oAittran.GlTranDetails = new List<GLTranDetail>();
oAittran.GlTranDetails.Add(aitExpenceGL);
var AITContraVS = vsAIT.FirstOrDefault(x => x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Contra ||
x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Bank);
if (AITContraVS == null)
{
throw new ServiceException(" There is no voucher setup found for the investment AIT Contra.");
}
ogl = new GLService().Get(tc, AITContraVS.GLID);
GLTranDetail aitContralGL = new GLTranDetail();
aitContralGL.Transide = AITContraVS.GLSide;
aitContralGL.Amount = Convert.ToDouble(tmepItem.InvestmentAIT);
aitContralGL.GLID = ogl.ID;
oAittran.GlTranDetails.Add(aitContralGL);
otrans.Add(oAittran);
}
int InterestGlTranID = 0, AITGLTranID = 0;
foreach (var item in otrans)
{
item.CreditAmount = item.GlTranDetails.Where(x => x.Transide == EnumTranSide.Credit).Sum(x => x.Amount);
item.DebitAmount = item.GlTranDetails.Where(x => x.Transide == EnumTranSide.Debit).Sum(x => x.Amount);
int glTranID = new GLTranService().Save(tc, item);
if(item.GLTranType == EnumGLTranType.MonthlyInvestmentAccrual)
InterestGlTranID = glTranID;
if(item.GLTranType == EnumGLTranType.MonthlyInvestmentAccrualAIT)
AITGLTranID = glTranID;
//UpdateInvestmentGLTranID(tc, investment.ID, glTranID);
}
InvestmentTranDA.UpdateInvestmentTranGLTranID(tc, InterestGlTranID, AITGLTranID, tmepItem.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 SaveInvestmentEncashment(InvestmentInfo investment)
{
TransactionContext tc = null;
List<ActivityVoucherSetup> activityVoucherSetup = new ActivityVoucherSetupService().GetByProcessType
(investment.FundTypeID, EnumFundProcessType.Investment_Encashment);
UserRecord oRecord = new UserRecordService().GetByField1(investment.CertificateNo, GlobalFunctions.Fund_UserObject_Investment, investment.FundTypeID);
InvestmentCategory investmentCategory = new InvestmentCategoryService().Get(investment.CategoryID);
try
{
tc = TransactionContext.Begin(true);
UpdateEncashAndStatus(investment, tc);
InvestmentTran tmepItem = new InvestmentTran();
tmepItem.InvestmentAIT = investment.AccrualInterestAIT;
tmepItem.PrincipalAmount = investment.AccrualInterest;
tmepItem.InvestmentID = investment.ID;
tmepItem.FundTypeID = investment.FundTypeID;
tmepItem.TranTypeID = EnumInvestmentActivities.InvestmentEncashment;
tmepItem.TranDate = investment.VoucherDate;
tmepItem.CreatedDate= investment.CreatedDate;
tmepItem.CreatedBy = investment.CreatedBy;
int idx = tc.GenerateID("FM_InvestmentTran", "TRANID");
base.SetObjectID(tmepItem, (idx));
InvestmentTranDA.Insert(tc, tmepItem);
// Creating Bank Amount GLTranDetail
ActivityVoucherSetup invsetup1 = activityVoucherSetup.FirstOrDefault(x => x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Bank ||
x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Contra);
if (invsetup1 == null)
{
throw new Exception("Bank/Contra GL head not found in the voucher setup.");
}
GL ogl = new GLService().Get(tc, invsetup1.GLID);
GLTranDetail investGL1 = new GLTranDetail();
investGL1.Transide = invsetup1.GLSide;
investGL1.Amount = investment.Amount + investment.AccrualInterest;
investGL1.GLID = ogl.ID;
// Creating Principal Amount GLTranDetail
ActivityVoucherSetup invsetup2 = activityVoucherSetup.Find(x => x.ReferenceID == investment.CategoryID &&
x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Investment_Principal);
if (invsetup2 == null)
{
throw new Exception("Investment Principal GL head not found in the voucher setup.");
}
ogl = new GLService().Get(tc, invsetup2.GLID);
GLTranDetail investGL2 = new GLTranDetail();
investGL2.Transide = invsetup2.GLSide;
investGL2.Amount = investment.Amount;
investGL2.GLID = ogl.ID;
GLTranUserRecord orc = new GLTranUserRecord();
orc.Amount = investment.Amount;
orc.UserObject1ID = GlobalFunctions.Fund_UserObject_Investment;
orc.GLID = investGL2.GLID;
orc.UserRecord1ID = oRecord.ID;
investGL2.GLTranUserRecords = new List<GLTranUserRecord>();
investGL2.GLTranUserRecords.Add(orc);
// Creating Accrual Interest Tran Detail
ActivityVoucherSetup invsetupforBank = activityVoucherSetup.Find(x => x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Investment_Income);
if (invsetup1 == null)
{
throw new Exception("Investment Bank not found in the voucher setup.");
}
ogl = new GLService().Get(tc, invsetupforBank.GLID);
GLTranDetail investGL3 = new GLTranDetail();
investGL3.Transide = invsetupforBank.GLSide;
investGL3.Amount = investment.AccrualInterest;
investGL3.GLID = ogl.ID;
//Creating Voucher with these three GL Tran Detail
GLTran otran = new GLTran();
// Own Setup of GL Tran
otran.FundTypeID = investment.FundTypeID;
otran.VoucherDate = investment.VoucherDate;
otran.ChequeDate = investment.VoucherDate;
otran.Description = "Date: " + investment.VoucherDate.ToString("dd MMM yyyy") + " - " + investmentCategory.Name + " - Investment Encashment , Amount: " + (investment.AccrualInterest + investment.Amount) + ". Certificate No: " + investment.CertificateNo;
otran.CreatedDate = DateTime.Now;
otran.CreatedBy = investment.CreatedBy;
otran.GLTranType = EnumGLTranType.InvestmentEncashment;
otran.CreditAmount = (double)(investment.AccrualInterest + investment.Amount);
otran.DebitAmount = (double)(investment.AccrualInterest + investment.Amount);
// Adding Details to Tran
otran.GlTranDetails = new List<GLTranDetail>();
otran.GlTranDetails.Add(investGL1);
otran.GlTranDetails.Add(investGL2);
otran.GlTranDetails.Add(investGL3);
int gltranID = new GLTranService().Save(tc, otran);
InvestmentDA.UpdateInvestmentEncashGLTranID(tc, investment.ID, gltranID);
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 SaveInvestmentLiquidation(InvestmentInfo investment)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
UpdateAccruedAndStatus(investment, 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 SaveInvestmentRenewal(InvestmentInfo investment)
{
InvestmentInfo oldInvestment = new InvestmentService().Get((int)investment.RefID);
UserRecord oldRecord = new UserRecordService().GetByField1(oldInvestment.CertificateNo, GlobalFunctions.Fund_UserObject_Investment, investment.FundTypeID);
TransactionContext tc = null;
List<ActivityVoucherSetup> activityVoucherSetup = new ActivityVoucherSetupService().GetByProcessType
(investment.FundTypeID, EnumFundProcessType.Investment_Renewal);
InvestmentCategory investmentCategory = new InvestmentCategoryService().Get(investment.CategoryID);
investment.TotalProAccruedAmount = 0;
investment.ID = 0;
try
{
tc = TransactionContext.Begin(true);
int userRecordID = 0;
if (!investment.IsNew)
{
InvestmentDA.Update(tc, investment);
}
else
{
int idx = tc.GenerateID("FM_Investment", "ID");
base.SetObjectID(investment, (idx));
InvestmentDA.Insert(tc, investment);
//Delete Children
InvestmentDA.DeleteSingnatoryByParent(tc, investment.ID);
InvestmentScheduleDA.DeleteByParent(tc, investment.ID);
InvestmentTranDA.DeleteByParent(tc, investment.ID);
InvestmentLogDA.DeleteByParent(tc, investment.ID);
}
foreach (InvestmentSchedule item in investment.InvestmentSchedules)
{
item.InvestmentID = investment.ID;
int idx = tc.GenerateID("FM_InvestmentSchedule", "ID");
base.SetObjectID(item, (idx));
InvestmentScheduleDA.Insert(tc, item);
}
foreach (InvestmentSignatory item in investment.InvestmentSignatorys)
{
item.InvestmentID = investment.ID;
int idx = tc.GenerateID("FM_InvestmentSignatory", "ID");
base.SetObjectID(item, (idx));
InvestmentDA.Insert(tc, item);
}
oldInvestment.CurrentStatus = EnumInvestmentStatus.Renewal;
UpdateRenewalAndStatus(oldInvestment, tc);
activityVoucherSetup = activityVoucherSetup.FindAll(x => x.ReferenceType == EnumProcessReferenceType.Investment
&& x.ReferenceID == investment.CategoryID);
if (activityVoucherSetup.Count == 0)
{
throw new ServiceException(" There is no voucher setup found for the investment purchase.");
}
if (activityVoucherSetup.Count > 3)
{
throw new ServiceException(" There must be two item in investment purchase setup.");
}
// Creating Principal Amount GLTranDetail
ActivityVoucherSetup invsetup1 = activityVoucherSetup.FirstOrDefault(x => x.ReferenceID == investment.CategoryID &&
x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Investment_Principal);
if (invsetup1 == null)
{
throw new ServiceException(" There is no voucher setup found for the investment principle.");
}
GL ogl = new GLService().Get(tc, invsetup1.GLID);
GLTranDetail investGL1 = new GLTranDetail();
investGL1.Transide = EnumTranSide.Debit;
investGL1.Amount = Math.Round(investment.Amount,2);
investGL1.GLID = ogl.ID;
UserRecord oRecord = new UserRecord();
oRecord.Field1 = investment.CertificateNo;
oRecord.Field2 = investment.Description + ", Purchase amount:" + investment.Amount;
oRecord.FundTypeID = investment.FundTypeID;
oRecord.UserObjectID = GlobalFunctions.Fund_UserObject_Investment;
oRecord.CreatedBy = investment.CreatedBy;
oRecord.CreatedDate = DateTime.Now;
userRecordID = tc.GenerateID("FM_USERRECORD", "USERRECORDID");
base.SetObjectID(oRecord, (userRecordID));
UserRecordDA.Insert(tc, oRecord);
GLTranUserRecord ngltrur = new GLTranUserRecord();
ngltrur.UserObject1ID = GlobalFunctions.Fund_UserObject_Investment;
ngltrur.UserRecord1ID = oRecord.ID;
ngltrur.Amount = investment.Amount;
investGL1.GLTranUserRecords = new List<GLTranUserRecord>();
investGL1.GLTranUserRecords.Add(ngltrur);
ActivityVoucherSetup invsetup2 = activityVoucherSetup.FirstOrDefault(x => x.ReferenceID == investment.CategoryID &&
x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Investment_Principal);
if (invsetup2 == null)
{
throw new Exception("There is no voucher setup found for the investment principle");
}
ogl = new GLService().Get(tc, invsetup2.GLID);
GLTranDetail investGL2 = new GLTranDetail();
investGL2.Transide = EnumTranSide.Credit;
investGL2.Amount = Math.Round(oldInvestment.Amount,2);
investGL2.GLID = ogl.ID;
GLTranUserRecord gltnew = new GLTranUserRecord();
gltnew.UserObject1ID = (int)EnumFundProcessType.Investment_Encashment;
gltnew.UserRecord1ID = oldRecord.ID;
gltnew.Amount = Math.Round(investGL2.Amount, 2);
investGL2.GLTranUserRecords = new List<GLTranUserRecord>();
investGL2.GLTranUserRecords.Add(gltnew);
// Creating Bank GL Tran Detail
ActivityVoucherSetup invsetupforBank = activityVoucherSetup.FirstOrDefault(x => x.VouhcerSetupItemType == EnumVouhcerSetupItemType.Investment_Income);
if (invsetupforBank == null)
{
throw new Exception("There is no voucher setup found for the investment bank");
}
ogl = new GLService().Get(tc, invsetupforBank.GLID);
GLTranDetail bankGL = new GLTranDetail();
bankGL.Transide = invsetupforBank.GLSide;
bankGL.Amount = Math.Round(Convert.ToDouble(oldInvestment.TotalProAccruedAmount),2);
bankGL.GLID = ogl.ID;
//Creating Voucher with these three GL Tran Detail
GLTran otran = new GLTran();
otran.FundTypeID = investment.FundTypeID;
otran.VoucherDate = investment.VoucherDate;
otran.Description = "Date: " + investment.VoucherDate.ToString("dd MMM yyyy") + " - " + investmentCategory.Name + " - Investment Renewed , Amount: " + investGL1.Amount + ". Certificate No: " + investment.CertificateNo;
otran.CreatedDate = DateTime.Now;
otran.CreatedBy = investment.CreatedBy;
otran.GLTranType = EnumGLTranType.InvestmentRenewal;
// Adding Details to Tran
otran.GlTranDetails = new List<GLTranDetail>();
otran.GlTranDetails.Add(bankGL);
otran.GlTranDetails.Add(investGL1);
otran.GlTranDetails.Add(investGL2);
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);
if (otran.DebitAmount != otran.CreditAmount)
{
otran.GlTranDetails.Remove(bankGL);
double extraAmount =Math.Round(investGL1.Amount - investGL2.Amount, 2);
if(extraAmount < 0) {
throw new Exception("Input amount is less!");
}
bankGL.Amount = extraAmount;
otran.GlTranDetails.Add(bankGL);
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);
}
int glTranID = new GLTranService().Save(tc, otran);
if(glTranID < 0)
{
throw new Exception("Failed to Save Voucher!");
}
InvestmentDA.UpdateInvestmentGLAndUserRecord(tc, investment.ID, glTranID, userRecordID);
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 Delete(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
InvestmentDA.Delete(tc, id);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to GetInvestment", e);
#endregion
}
}
public InvestmentInfo GetMonthlyInterestAmount(InvestmentInfo investmentInfo, DateTime dLastDateOfProcessMonth)
{
InvestmentCategory ocat = new InvestmentCategoryService().Get(investmentInfo.CategoryID);
if(ocat.Name.ToUpper() =="BSP")
return GetBSPtMonthlyInterestAmount(investmentInfo, dLastDateOfProcessMonth);
double interestAmount = 0;
double dTaxAmount = 0;
int daysOfMonth = 0;
DateTime dFirstDateOfMonth = GlobalFunctions.FirstDateOfMonth(dLastDateOfProcessMonth);
DateTime dLastDateOfMonth = GlobalFunctions.LastDateOfMonth(dLastDateOfProcessMonth);
daysOfMonth = GlobalFunctions.DateDiff("D", dFirstDateOfMonth, dLastDateOfMonth) + 1;
int basement365 = 365;
int indexNumber = 0;
try
{
foreach (InvestmentSchedule investmentSchedule in investmentInfo.InvestmentSchedules)
{
if (investmentSchedule.FromDate <= dLastDateOfProcessMonth && investmentSchedule.TillDate >= dFirstDateOfMonth)
{
if (investmentSchedule.FromDate.Year == dLastDateOfProcessMonth.Year ||
investmentInfo.LastProAccruedDate?.Year == dLastDateOfProcessMonth.Year
|| investmentSchedule.TillDate.Year == dLastDateOfProcessMonth.Year)
{
if (investmentInfo.TenureType == EnumTenureType.Day)
{
if (investmentInfo.IssueDate.Month == dLastDateOfProcessMonth.Month)
{
int days = GlobalFunctions.DateDiff("D", investmentInfo.IssueDate, dLastDateOfProcessMonth) + 1;
interestAmount = (investmentInfo.Amount * investmentInfo.InvestmentSchedules[indexNumber].Percentage * days) / (basement365 * 100);
dTaxAmount += (interestAmount * investmentInfo.InvestmentCategoryObj.InterestRate) / 100;
investmentInfo.AccrualInterestAIT = Math.Round(dTaxAmount);
}
else if (investmentInfo.MatureDate.Month == dLastDateOfProcessMonth.Month)
{
int days = 0;
if (investmentInfo.LastProAccruedDate == null || investmentInfo.LastProAccruedDate == DateTime.MinValue)
{
days = GlobalFunctions.DateDiff("D", investmentSchedule.FromDate, investmentSchedule.TillDate) + 1;
}
else if (investmentInfo.LastProAccruedDate != null && investmentInfo.LastProAccruedDate != DateTime.MinValue)
{
days = GlobalFunctions.DateDiff("D", investmentInfo.LastProAccruedDate.Value, investmentInfo.MatureDate);
}
interestAmount = (investmentInfo.Amount * investmentInfo.InvestmentSchedules[indexNumber].Percentage * days) / (basement365 * 100);
dTaxAmount += (interestAmount * investmentInfo.InvestmentCategoryObj.InterestRate) / 100;
investmentInfo.AccrualInterestAIT = Math.Round(dTaxAmount);
}
else
{
int days = 0;
if (investmentInfo.LastProAccruedDate == null || investmentInfo.LastProAccruedDate == DateTime.MinValue)
{
days = GlobalFunctions.DateDiff("D", investmentSchedule.FromDate, dLastDateOfProcessMonth) + 1;
}
else if (investmentInfo.LastProAccruedDate != null && investmentInfo.LastProAccruedDate != DateTime.MinValue)
{
days = GlobalFunctions.DateDiff("D", investmentInfo.LastProAccruedDate.Value, dLastDateOfProcessMonth);
}
interestAmount = (investmentInfo.Amount * investmentInfo.InvestmentSchedules[indexNumber].Percentage * days) / (basement365 * 100);
dTaxAmount += (interestAmount * investmentInfo.InvestmentCategoryObj.InterestRate) / 100;
investmentInfo.AccrualInterestAIT = Math.Round(dTaxAmount);
}
}
else if (investmentInfo.TenureType == EnumTenureType.Month || investmentInfo.TenureType == EnumTenureType.Month_Plus_1Day)
{
if (investmentInfo.IssueDate.Month == dLastDateOfProcessMonth.Month
&& investmentInfo.IssueDate.Year == dLastDateOfMonth.Year)
{
int days = GlobalFunctions.DateDiff("D", investmentInfo.IssueDate, dLastDateOfProcessMonth) + 1;
interestAmount += (investmentInfo.Amount * investmentInfo.InvestmentSchedules[indexNumber].Percentage * days) / (basement365 * 100);
dTaxAmount += (interestAmount * investmentInfo.InvestmentCategoryObj.InterestRate) / 100;
investmentInfo.AccrualInterestAIT = Math.Round(dTaxAmount);
}
else if (investmentInfo.MatureDate.Month == dLastDateOfProcessMonth.Month
&& investmentInfo.MatureDate.Year == dLastDateOfMonth.Year)
{
int days = 0;
if (investmentInfo.LastProAccruedDate == null || investmentInfo.LastProAccruedDate == DateTime.MinValue)
{
days = GlobalFunctions.DateDiff("D", investmentSchedule.FromDate, investmentSchedule.TillDate) + 1;
}
else if (investmentInfo.LastProAccruedDate != null && investmentInfo.LastProAccruedDate != DateTime.MinValue && investmentInfo.TenureType == EnumTenureType.Month)
{
days = GlobalFunctions.DateDiff("D", investmentInfo.LastProAccruedDate.Value, investmentInfo.MatureDate) + 1;
}
else if (investmentInfo.LastProAccruedDate != null && investmentInfo.LastProAccruedDate != DateTime.MinValue && investmentInfo.TenureType == EnumTenureType.Month_Plus_1Day)
{
days = GlobalFunctions.DateDiff("D", investmentInfo.LastProAccruedDate.Value, investmentInfo.MatureDate);
}
interestAmount += (investmentInfo.Amount * Math.Round(investmentInfo.InvestmentSchedules[indexNumber].Percentage, 3) * days) / (basement365 * 100);
dTaxAmount += (interestAmount * investmentInfo.InvestmentCategoryObj.InterestRate) / 100;
investmentInfo.AccrualInterestAIT = Math.Round(dTaxAmount);
}
else
{
int days = 0;
double newInterest = 0;
double dInterestAmount = 0;
if (investmentInfo.LastProAccruedDate == null || investmentInfo.LastProAccruedDate == DateTime.MinValue)
{
if (investmentSchedule.TillDate < dLastDateOfProcessMonth)
{
days = GlobalFunctions.DateDiff("D", investmentSchedule.FromDate, investmentSchedule.TillDate) + 1;
}
else if (investmentSchedule.TillDate == dLastDateOfProcessMonth && investmentInfo.TenureType == EnumTenureType.Month_Plus_1Day)
{
days = GlobalFunctions.DateDiff("D", investmentSchedule.FromDate, dLastDateOfProcessMonth) + 1;
}
else
{
days = GlobalFunctions.DateDiff("D", investmentSchedule.FromDate, dLastDateOfProcessMonth) + 1;
}
interestAmount += (investmentInfo.Amount * investmentInfo.InvestmentSchedules[indexNumber].Percentage * days) / (basement365 * 100);
}
else if (investmentInfo.LastProAccruedDate != null &&
investmentInfo.LastProAccruedDate != DateTime.MinValue)
{
days = GlobalFunctions.DateDiff("D", investmentInfo.LastProAccruedDate.Value, dLastDateOfProcessMonth);
interestAmount += (investmentInfo.Amount * investmentInfo.InvestmentSchedules[indexNumber].Percentage * days) / (basement365 * 100);
if (investmentSchedule.FromDate.Month == dLastDateOfProcessMonth.Month
&& investmentSchedule.FromDate > GlobalFunctions.FirstDateOfMonth(dLastDateOfProcessMonth))
{
interestAmount = 0;
days = (dLastDateOfProcessMonth - investmentSchedule.FromDate).Days + 1;
interestAmount += (investmentInfo.Amount * investmentInfo.InvestmentSchedules[indexNumber].Percentage * days) / (basement365 * 100);
days = (investmentSchedule.FromDate - GlobalFunctions.FirstDateOfMonth(dLastDateOfProcessMonth)).Days;
interestAmount += (investmentInfo.Amount * investmentInfo.InvestmentSchedules[indexNumber - 1].Percentage * days) / (basement365 * 100);
}
}
if (investmentInfo.IssueDate.Month == dLastDateOfProcessMonth.Month &&
investmentInfo.IssueDate.Year != dLastDateOfMonth.Year)
{
if (indexNumber > 0)
{
newInterest = investmentInfo.InvestmentSchedules[indexNumber].Percentage -
investmentInfo.InvestmentSchedules[indexNumber - 1].Percentage;
dInterestAmount = ((investmentInfo.Amount * newInterest) / 100) * indexNumber;
interestAmount += dInterestAmount;
}
dTaxAmount = (interestAmount * investmentInfo.InvestmentCategoryObj.InterestRate) / 100;
investmentInfo.AccrualInterestAIT = Math.Round(dTaxAmount);
}
else
{
dTaxAmount += (interestAmount * investmentInfo.InvestmentCategoryObj.InterestRate) / 100;
investmentInfo.AccrualInterestAIT = Math.Round(dTaxAmount);
}
}
}
}
}
indexNumber++;
}
}
catch (Exception ex)
{
throw new ServiceException(ex.Message);
}
investmentInfo.AccrualInterest = Math.Round(interestAmount);
return investmentInfo;
}
public InvestmentInfo GetBSPtMonthlyInterestAmount(InvestmentInfo investmentInfo, DateTime dProcessMonth)
{
double interestAmount = 0;
int daysOfMonth = 0;
DateTime dFirstDateOfMonth = GlobalFunctions.FirstDateOfMonth(dProcessMonth);
DateTime dLastDateOfMonth = GlobalFunctions.LastDateOfMonth(dProcessMonth);
InvestmentCategory ocat = new InvestmentCategoryService().Get(investmentInfo.CategoryID);
daysOfMonth = dProcessMonth.Day;
int basement365 = 365;
double interestRate = 0;
var matchingItems = investmentInfo.InvestmentSchedules
.Where(item => dLastDateOfMonth >= item.FromDate && dFirstDateOfMonth <= item.TillDate)
.ToList();
if(matchingItems.Count == 0)
{
throw new ServiceException("No Investment Schedule found in this current month for this investment");
}
interestRate = matchingItems[matchingItems.Count-1].Percentage;
DateTime date;
if (dProcessMonth > investmentInfo.MatureDate)
{
date = investmentInfo.MatureDate;
}
else
{
date = dProcessMonth;
}
foreach (InvestmentSchedule investmentSchedule in investmentInfo.InvestmentSchedules)
{
if (investmentSchedule.TillDate <= date)
{
interestAmount = interestAmount + (investmentInfo.Amount * interestRate) / 100;
}
else
{
//DateTime fromDate = investmentInfo.LastProAccruedDate == null ? investmentSchedule.FromDate :
//(DateTime)investmentInfo.LastProAccruedDate;
DateTime fromDate = investmentSchedule.FromDate ;
// fromDate = fromDate.AddDays(1);
double days = (date - fromDate).TotalDays + 1;
if (days > 0)
{
interestAmount = interestAmount +(investmentInfo.Amount * interestRate * days) / (basement365 * 100);
}
}
}
investmentInfo.TotalProAccruedAmount = investmentInfo.TotalProAccruedAmount
== null ? 0 : investmentInfo.TotalProAccruedAmount;
investmentInfo.AccrualInterest = (interestAmount -(double) investmentInfo.TotalProAccruedAmount);
investmentInfo.AccrualInterest = investmentInfo.AccrualInterest <0? 0: Math.Round(investmentInfo.AccrualInterest);
investmentInfo.AccrualInterestAIT = Math.Round(Math.Round(investmentInfo.AccrualInterest) * ocat.InterestRate / 100);
return investmentInfo;
}
#endregion
}
}