EchoTex_Payroll/HRM.DA/Service/Leave/LeaveProcessService.cs
2024-10-14 10:01:49 +06:00

2580 lines
112 KiB
C#

using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using HRM.BO;
using HRM.Service;
using Microsoft.AspNetCore.JsonPatch.Operations;
using NPOI.SS.Formula.Functions;
using static iTextSharp.text.pdf.AcroFields;
namespace HRM.DA
{
#region LeaveParameter Service
public class LeaveProcessService : ServiceTemplate, ILeaveProcessService
{
#region Private functions and declaration
#endregion
public LeaveProcessService()
{
}
#region Leave Process
private void MapObject(LeaveProcess oLeaveProcess, DataReader oReader)
{
base.SetObjectID(oLeaveProcess, oReader.GetInt32("ProcessId").Value);
oLeaveProcess.LeaveYearID = oReader.GetInt32("LEAVEYEARID").Value;
oLeaveProcess.ProcessDate = oReader.GetDateTime("ProcessDate").Value;
oLeaveProcess.IsYearEnd = oReader.GetBoolean("IsYearEnd").Value;
oLeaveProcess.ProcessYearDescription = oReader.GetString("ProcessYearDesc");
oLeaveProcess.PayrollTypeID = oReader.GetInt32("PayrollTypeID").Value;
this.SetObjectState(oLeaveProcess, Ease.Core.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
LeaveProcess oLeaveProcess = new LeaveProcess();
MapObject(oLeaveProcess, oReader);
return oLeaveProcess as T;
}
private LeaveProcess CreateObject(DataReader oReader)
{
LeaveProcess oLeaveProcess = new LeaveProcess();
MapObject(oLeaveProcess, oReader);
return oLeaveProcess;
}
#region Service implementation
public LeaveProcess Get(int id)
{
LeaveProcess oLeaveProcess = new LeaveProcess();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveProcessDA.Get(tc, id));
if (oreader.Read())
{
oLeaveProcess = CreateObject(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Leave Process:" + e.Message, e);
#endregion
}
return oLeaveProcess;
}
public LeaveProcess GetByYearID(int nProcessYear, int payrolltypeid)
{
LeaveProcess oLeaveProcess = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveProcessDA.GetByYearID(tc, nProcessYear, payrolltypeid));
if (oreader.Read())
{
oLeaveProcess = CreateObject(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Leave Process : " + e.Message, e);
#endregion
}
return oLeaveProcess;
}
public LeaveProcess Get(int nLeaveProcessYear, int nPayrollTypeid)
{
LeaveProcess oLeaveProcess = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader =
new DataReader(LeaveProcessDA.GetByYearANDPayrollID(tc, nLeaveProcessYear, nPayrollTypeid));
if (oreader.Read())
{
oLeaveProcess = CreateObject(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Leave Process : " + e.Message, e);
#endregion
}
return oLeaveProcess;
}
public List<LeaveProcess> GetbyPayrolltype(int payrolltypeid)
{
List<LeaveProcess> oLeaveProcesss = new List<LeaveProcess>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveProcessDA.Get(tc, payrolltypeid));
oLeaveProcesss = this.CreateObjects<LeaveProcess>(oreader);
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Leave Process: " + e.Message, e);
#endregion
}
return oLeaveProcesss;
}
public LeaveProcess GetLastProcess(int payrolltypeid)
{
LeaveProcess oLeaveProcess = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveProcessDA.GetLastProcess(tc, payrolltypeid));
if (oreader.Read())
{
oLeaveProcess = CreateObject(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Leave Process: " + e.Message, e);
#endregion
}
return oLeaveProcess;
}
public void UnDoYearEnd(int leaveyearid, int payrolltypeid)
{
LeaveYearService oLYearService = new LeaveYearService();
LeaveProcessService oEmpService = new LeaveProcessService();
LeaveProcess oprocess = this.Get(leaveyearid, payrolltypeid);
LeaveYear oyear = new LeaveYearService().Get(leaveyearid);
if (oprocess == null)
{
throw new Exception("There is no process to delete");
}
if (oprocess.ProcessDate.AddMonths(1) < DateTime.Today)
{
throw new Exception("Delete is not allowed after one month from the Last process.");
}
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveProcessDA.DeleteByLeaveYear(tc, leaveyearid, payrolltypeid);
oyear.IsCurrent = true;
oyear.IsEnded = false;
oLYearService.UpdateCurrYearStatus(tc, oyear);
oLYearService.UpdateYearEndValue(tc, oyear);
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 DoYearEnd(LeaveProcess oLeaveProcess)
{
if (this.IsProcessed(oLeaveProcess.LeaveYear.ID, oLeaveProcess.PayrollTypeID) == true)
{
throw new Exception("Selected Year already process, first Undo the process");
}
List<EmpLeaveStatus> empLeaveStatuses = this.GetLeaveBalance(oLeaveProcess.LeaveYearID, oLeaveProcess.PayrollTypeID, string.Empty);
//if (empLeaveStatuses != null)
//{
// foreach (EmpLeaveStatus item in empLeaveStatuses)
// {
// if (item.CFDays != 0 || item.EncashDays != 0)
// {
// item.YearEndBalance = item.YearEndBalance - (item.CFDays - item.EncashDays);
// }
// }
//}
LeaveYearService oLYearService = new LeaveYearService();
LeaveProcessService oEmpService = new LeaveProcessService();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveProcessDA.DeleteByProcessYearANDPayrollType(tc, oLeaveProcess.LeaveYearID,
oLeaveProcess.PayrollTypeID);
int id = tc.GenerateID("LeaveProcess", "ProcessID");
base.SetObjectID(oLeaveProcess, id);
LeaveProcessDA.Insert(tc, oLeaveProcess);
int statusid = LeaveProcessDA.GetNewDetailID(tc);
foreach (EmpLeaveStatus oItem in empLeaveStatuses)
{
if(oItem.EmpId == 1807)
{
}
if (oItem.LeaveParameter.CalculationType == EnumLeaveCalculationType.Monthly_Earn
&& oItem.EligibleParamDetail == null) // considering joining current year and will eligible next year.
{
Employee emp = new EmployeeService().Get(oItem.EmpId);
oItem.YearEndBalance = GetCurrentYearDays(oItem.LeaveParameter, emp, oLeaveProcess.LeaveYear.EndDate,
null, oItem.LeaveParameter.Details[0], oLeaveProcess.LeaveYear);
}
if (oItem.EligibleParamDetail != null)
{
if (oItem.EligibleParamDetail.MaxCF == 0) continue;
else
{
if(oItem.EligibleParamDetail.MaxCF > oItem.YearEndBalance)
{
oItem.CFDays = oItem.YearEndBalance;
}
else
{
oItem.CFDays = oItem.EligibleParamDetail.MaxCF;
}
}
}
if (oItem.IsNew == true)
{
this.SetObjectID(oItem, statusid);
oItem.ProcessId = oLeaveProcess.ID;
oItem.CreatedBy = oLeaveProcess.CreatedBy;
oItem.CreatedDate = oLeaveProcess.CreatedDate;
LeaveProcessDA.Insert(tc, oItem);
statusid = statusid + 1;
}
else
{
LeaveProcessDA.Update(tc, oItem);
}
}
oLeaveProcess.LeaveYear.IsCurrent = false;
oLeaveProcess.LeaveYear.IsEnded = true;
oLYearService.UpdateCurrYearStatus(tc, oLeaveProcess.LeaveYear);
oLYearService.UpdateYearEndValue(tc, oLeaveProcess.LeaveYear);
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);
LeaveProcessDA.Delete(tc, id);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Leave Process: " + e.Message, e);
#endregion
}
}
public void UpadteLeaveYearStatus(LeaveProcess oLeaveProcess)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveYearService oLYearService = new LeaveYearService();
oLeaveProcess.LeaveYear.IsCurrent = true;
oLYearService.UpdateCurrYearStatus(tc, oLeaveProcess.LeaveYear);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Leave Process: " + e.Message, e);
#endregion
}
}
public void DeleteByProcessYear(int nProcessYear)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveProcessDA.DeleteByProcessYear(tc, nProcessYear);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Leave Process: " + e.Message, e);
#endregion
}
}
public void Delete(int nProcessYear, int nPayrollTypeID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveProcessDA.DeleteByProcessYearANDPayrollType(tc, nProcessYear, nPayrollTypeID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Leave Process: " + e.Message, e);
#endregion
}
}
public bool IsProcessed(int nProcessYear, int payrolltypeid)
{
bool res = false;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
res = LeaveProcessDA.IsProcessed(tc, nProcessYear, payrolltypeid);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return res;
}
//public void DoYearEnd(LeaveProcess oProcess)
//{
// TransactionContext tc = null;
// try
// {
// LeaveYear olYear = oProcess.LeaveYear;
// olYear.IsEnded = true;
// oProcess.IsYearEnd = true;
// tc = TransactionContext.Begin(true);
// LeaveProcessDA.DoYearEnd(tc, oProcess);
// LeaveYearService oLS = new LeaveYearService();
// oLS.UpdateYearEndValue(tc, olYear);
// tc.End();
// }
// catch (Exception e)
// {
// #region Handle Exception
// if (tc != null)
// tc.HandleError();
// ExceptionLog.Write(e);
// throw new ServiceException("Failed to do Year End: reason:" + e.Message, e);
// #endregion
// }
//}
#endregion
#endregion
#region Private functions and declaration
private void MapEmpLeaveStatusObject(EmpLeaveStatus oEmpLeaveStatus, DataReader oReader)
{
base.SetObjectID(oEmpLeaveStatus, oReader.GetInt32("TranId").Value);
oEmpLeaveStatus.ProcessId = oReader.GetInt32("ProcessId").Value;
oEmpLeaveStatus.EmpId = oReader.GetInt32("EmpId").Value;
oEmpLeaveStatus.LeaveYearID = oReader.GetInt32("LEAVEYEARID").Value;
oEmpLeaveStatus.LeaveId = oReader.GetInt32("LeaveId").Value;
oEmpLeaveStatus.CarryFromPrvYear = oReader.GetDouble("FORBENIFITEDYEAR").Value;
oEmpLeaveStatus.CFDays = oReader.GetDouble("CFDays").Value;
oEmpLeaveStatus.EncashDays = oReader.GetDouble("EncashDays").Value;
oEmpLeaveStatus.EncashAmount = oReader.GetDouble("EncashAmount").Value;
oEmpLeaveStatus.NormalLeaveDays = oReader.GetDouble("NormalLeaveDays").Value;
oEmpLeaveStatus.YearEndBalance = oReader.GetDouble("YearEndBalance").Value;
oEmpLeaveStatus.LeaveAvailed = oReader.GetDouble("LEAVEAVAILED").Value;
oEmpLeaveStatus.OpeningBalance = oReader.GetDouble("OPENINGBALANCE").Value;
oEmpLeaveStatus.ForfitedDays = oReader.GetDouble("FORFITEDDAYS").Value;
oEmpLeaveStatus.LeaveNameView = oReader.GetString("Description", true, string.Empty);
this.SetObjectState(oEmpLeaveStatus, Ease.Core.ObjectState.Saved);
}
protected List<EmpLeaveStatus> CreateEmpLeaveStatusObjects(DataReader oReader)
{
List<EmpLeaveStatus> oEmpLeaveStatuss = new List<EmpLeaveStatus>();
while (oReader.Read())
{
EmpLeaveStatus oEmpLeaveStatus = new EmpLeaveStatus();
MapEmpLeaveStatusObject(oEmpLeaveStatus, oReader);
oEmpLeaveStatuss.Add(oEmpLeaveStatus);
}
return oEmpLeaveStatuss;
}
private EmpLeaveStatus CreateEmpLeaveStatusObject(DataReader oReader)
{
EmpLeaveStatus oEmpLeaveStatus = new EmpLeaveStatus();
MapEmpLeaveStatusObject(oEmpLeaveStatus, oReader);
return oEmpLeaveStatus;
}
#endregion
public EmpLeaveStatus GetProcessDetail(int id)
{
EmpLeaveStatus oEmpLeaveStatus = new EmpLeaveStatus();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveProcessDA.GetProcessDetail(tc, id));
if (oreader.Read())
{
oEmpLeaveStatus = this.CreateEmpLeaveStatusObject(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Employee Leave Status: " + e.Message, e);
#endregion
}
return oEmpLeaveStatus;
}
public void UpdateEncashAmount(List<EmpLeaveStatus> _oEmpLeaveStatus)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
foreach (EmpLeaveStatus oStatus in _oEmpLeaveStatus)
{
LeaveProcessDA.UpdateEncashAmount(tc, oStatus);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Employee Leave Status: " + e.Message, e);
#endregion
}
}
public List<EmpLeaveStatus> GetStatus(int empid, int leaveYearID)
{
List<EmpLeaveStatus> oEmpLeaveStatus = new List<EmpLeaveStatus>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveProcessDA.GetStatus(tc, empid, leaveYearID));
oEmpLeaveStatus = this.CreateEmpLeaveStatusObjects(oreader);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Employee Leave Status: " + e.Message, e);
#endregion
}
return oEmpLeaveStatus;
}
public EmpLeaveStatus GetStatus(int empid, int leaveID, int leaveYearID)
{
EmpLeaveStatus oEmpLeaveStatus = new EmpLeaveStatus();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveProcessDA.GetStatus(tc, empid, leaveID, leaveYearID));
if (oreader.Read())
{
oEmpLeaveStatus = this.CreateEmpLeaveStatusObject(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Employee Leave Status: " + e.Message, e);
#endregion
}
return oEmpLeaveStatus;
}
public EmpLeaveStatus GetByYear(int leaveyearID, int LeaveId, int EmpId)
{
EmpLeaveStatus oEmpLeaveStatus = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveProcessDA.GetByYear(tc, leaveyearID, LeaveId, EmpId));
if (oreader.Read())
{
oEmpLeaveStatus = this.CreateEmpLeaveStatusObject(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Employee Leave Status: " + e.Message, e);
#endregion
}
return oEmpLeaveStatus;
}
public List<EmpLeaveStatus> GetByYear(int leaveyearID)
{
List<EmpLeaveStatus> oEmpLeaveStatuss = new List<EmpLeaveStatus>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oReader = new DataReader(LeaveProcessDA.GetByYear(tc, leaveyearID));
oEmpLeaveStatuss = this.CreateEmpLeaveStatusObjects(oReader);
oReader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Employee Leave Status: " + e.Message, e);
#endregion
}
return oEmpLeaveStatuss;
}
public List<EmpLeaveStatus> GetByEmpIDs(string EmpIDs, int leaveyearID)
{
List<EmpLeaveStatus> oEmpLeaveStatuss = new List<EmpLeaveStatus>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oReader = new DataReader(LeaveProcessDA.GetByEmpIDs(tc, EmpIDs, leaveyearID));
oEmpLeaveStatuss = this.CreateEmpLeaveStatusObjects(oReader);
oReader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Employee Leave Status: " + e.Message, e);
#endregion
}
return oEmpLeaveStatuss;
}
public List<EmpLeaveStatus> GetProcessDetails(int nProcessId)
{
List<EmpLeaveStatus> oEmpLeaveStatuss = new List<EmpLeaveStatus>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oReader = new DataReader(LeaveProcessDA.GetByProcessId(tc, nProcessId));
oEmpLeaveStatuss = this.CreateEmpLeaveStatusObjects(oReader);
oReader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Employee Leave Status: " + e.Message, e);
#endregion
}
return oEmpLeaveStatuss;
}
public List<EmpLeaveStatus> GetAllStatus(int empId)
{
List<EmpLeaveStatus> oEmpLeaveStatuss = new List<EmpLeaveStatus>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oReader = new DataReader(LeaveProcessDA.GetAllStatus(tc, empId));
oEmpLeaveStatuss = this.CreateEmpLeaveStatusObjects(oReader);
oReader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Employee Leave Status: " + e.Message, e);
#endregion
}
return oEmpLeaveStatuss;
}
public int Save(EmpLeaveStatus oEmpLeaveStatus)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oEmpLeaveStatus.IsNew)
{
this.SetObjectID(oEmpLeaveStatus, LeaveProcessDA.GetNewDetailID(tc));
LeaveProcessDA.Insert(tc, oEmpLeaveStatus);
}
else
LeaveProcessDA.Update(tc, oEmpLeaveStatus);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Insert Employee Leave Status: " + e.Message, e);
#endregion
}
return oEmpLeaveStatus.ID;
}
public void SaveStatus(EmpLeaveStatus oEmpLeaveStatus)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
this.SetObjectID(oEmpLeaveStatus, LeaveProcessDA.GetNewDetailID(tc));
LeaveProcessDA.Insert(tc, oEmpLeaveStatus);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Insert Employee Leave Status: " + e.Message, e);
#endregion
}
}
public void SaveStatus(List<EmpLeaveStatus> oEmpLeaveStatus)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
int id = LeaveProcessDA.GetNewDetailID(tc);
foreach (EmpLeaveStatus oItem in oEmpLeaveStatus)
{
if (oItem.IsNew == true)
{
this.SetObjectID(oItem, id);
LeaveProcessDA.Insert(tc, oItem);
id = id + 1;
}
else
{
LeaveProcessDA.Update(tc, oItem);
}
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Insert Employee Leave Status: " + e.Message, e);
#endregion
}
}
internal void SaveAtProcessing(bool IsUpdate, int ProcessId, List<EmpLeaveStatus> oEmpLeaveStatuss,
TransactionContext tc)
{
if (oEmpLeaveStatuss != null)
{
foreach (EmpLeaveStatus oItem in oEmpLeaveStatuss)
{
if (IsUpdate) LeaveProcessDA.Update(tc, oItem);
else
{
this.SetObjectID(oItem, LeaveProcessDA.GetNewDetailID(tc));
oItem.ProcessId = ProcessId;
LeaveProcessDA.Insert(tc, oItem);
}
}
}
}
public void DeleteProcessDetail(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveProcessDA.DeleteProcessDetail(tc, id);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Employee Leave Status: " + e.Message, e);
#endregion
}
}
public void DeleteProcessDetailByID(int nProcessId)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveProcessDA.DeleteByProcessId(tc, nProcessId);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Employee Leave Status: " + e.Message, e);
#endregion
}
}
public void DeleteByPayrollType(int nProcessId, int nid, int nLeaveID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveProcessDA.DeleteByPayrollType(tc, nProcessId, nid, nLeaveID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Employee Leave Status: " + e.Message, e);
#endregion
}
}
public DataSet GetEmpAllLeaveRelatedInfo(int empID)
{
DataSet dsetLeaveInfo = new DataSet();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
dsetLeaveInfo = LeaveProcessDA.GetEmpAllLeaveRelatedInfo(tc, empID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return dsetLeaveInfo;
}
public List<EmpLeaveStatus> GetByYearType(int leaveYearID, int leaveId)
{
List<EmpLeaveStatus> oEmpLeaveStatuss = new List<EmpLeaveStatus>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oReader = new DataReader(LeaveProcessDA.GetByYearType(tc, leaveYearID, leaveId));
oEmpLeaveStatuss = this.CreateEmpLeaveStatusObjects(oReader);
oReader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Employee Leave Status: " + e.Message, e);
#endregion
}
return oEmpLeaveStatuss;
}
public List<LeaveProcess> Get()
{
throw new NotImplementedException();
}
public LeaveProcess GetLastProcess()
{
throw new NotImplementedException();
}
//public bool IsProcessed(int nProcessYear, int payrolltypeid)
//{
// TransactionContext tc = null;
// bool ispros = false;
// try
// {
// tc = TransactionContext.Begin();
// ispros = LeaveProcessDA.IsProcessed(tc, nProcessYear, payrolltypeid);
// tc.End();
// }
// catch (Exception e)
// {
// #region Handle Exception
// if (tc != null)
// tc.HandleError();
// ExceptionLog.Write(e);
// throw new ServiceException("Failed to process count: " + e.Message, e);
// #endregion
// }
// return ispros;
//}
private LeaveParameterDetail GetDeatilForEmployee(LeaveParameter leaveParam, Employee oEmp,
DateTime operationDate)
{
//#### change by Current date function
double y = Global.DateFunctions.DateDiff("Y",
oEmp.JoiningDate, operationDate);
//TimeSpan ts = operationDate - oEmp.JoiningDate;
//double y = Math.Round((double)ts.Days / 365, 2);
//int year = operationDate.Year - oEmp.JoiningDate.Year+1;
//int number=(int)y;
//double number1 =(double)(y-number);
//float f =(float)y;
int year = 0;
//if (y > (int)y)
// year = (int)y + 1;
//else
year = (int)y;
int maxYear = -1;
LeaveParameterDetail dtl = null;
foreach (LeaveParameterDetail detail in leaveParam.Details)
{
if (detail.Year <= year && detail.Year >= maxYear)
{
maxYear = detail.Year;
dtl = detail;
}
}
return dtl;
}
public List<EmpLeaveStatus> GetLeaveBalance(int leaveYearID, int payrollTypeID, string EmpIDsIn,
int? empID = null)
{
EmployeeService oservice = new EmployeeService();
List<Employee> oEmployees = new List<Employee>();
if (EmpIDsIn != string.Empty)
oEmployees = oservice.GetByEmpIDs(EmpIDsIn, payrollTypeID);
else if (empID != null)
{
var item = oservice.Get((int)empID);
if (item != null)
{
oEmployees.Add(item);
}
}
else
{
oEmployees = oservice.Get(EnumEmployeeStatus.Live, payrollTypeID);
}
LeaveYearService lservice = new LeaveYearService();
LeaveYear oCurrYear = lservice.Get(leaveYearID);
return GetLeaveBalance(oEmployees, oCurrYear, payrollTypeID);
}
public double GetSettlementEarnLeaveBalance(Employee emp, DateTime balanceDate)
{
EmployeeService oservice = new EmployeeService();
TimeSpan ts;
int month;
var item = emp;
List<Leave> oleaves = new LeaveService().Get(EnumStatus.Active, item.PayrollTypeID);
Leave earnedLeave = oleaves.FirstOrDefault(x => x.IsEarnedLeave == true);
if (earnedLeave == null) throw new Exception("There is no leave declared as annual/earned leave");
LeaveYearService lservice = new LeaveYearService();
LeaveYear oCurrYear = lservice.GetCurrentYear(emp.PayrollTypeID);
List<LeaveParameter> leaveParamss =
new LeaveParameterService().Get(EnumStatus.Regardless, item.PayrollTypeID, earnedLeave.ID, true);
List<LeaveEntry> oLs = null;
oLs = new LeaveEntryService().GetSumOfAvailedLeave(item.ID, oCurrYear.ID);
LeaveYear previousYear = new LeaveYearService().LastLeaveYear(oCurrYear);
List<EmpLeaveStatus> oPrevStatuses = null;
if (previousYear != null)
oPrevStatuses = this.GetStatus(emp.ID, previousYear.ID);
List<LeaveParameter> oAppLeaveParams =
new LeaveParameterService().GetApplicableParam(leaveParamss, oleaves,(int)item.GradeID, EnumLeaveparamOn.Grade, item);
LeaveParameterDetail oDetail = this.GetDeatilForEmployee(oAppLeaveParams[0], item, balanceDate);
double EDays = 0;
if (oPrevStatuses != null && oPrevStatuses.FindIndex(x => x.LeaveId == earnedLeave.ID) > -1)
{
EDays = oPrevStatuses[oPrevStatuses.FindIndex(x => x.LeaveId == earnedLeave.ID)].CFDays;
}
//if (emp.JoiningDate > oCurrYear.StartDate)
// ts = balanceDate - emp.JoiningDate.Date;
//else
// ts = balanceDate - new DateTime(oCurrYear.StartDate.Year, 1, 1);
//double nTempDays = (oDetail.MaxDays / 365) * ts.Days;
//EDays = Math.Round (nTempDays,0) ;
//new
DateTime YearStartingMonth= new DateTime(oCurrYear.StartDate.Year, 1, 1);
if (emp.JoiningDate > oCurrYear.StartDate)
month = (12 * (balanceDate.Year - emp.JoiningDate.Year) + (balanceDate.Month - emp.JoiningDate.Month)) + 1;
else
month = (12 * (balanceDate.Year - YearStartingMonth.Year) + (balanceDate.Month - YearStartingMonth.Month)) + 1;
double nTempDays = (oDetail.MaxDays / 12) * month;
EDays = Math.Round(nTempDays,MidpointRounding.AwayFromZero); // after decimal .5 will take upper value
/////
if (oLs != null && oLs.FindIndex(x => x.LeaveID == earnedLeave.ID) > -1)
{
EDays = EDays - oLs[oLs.FindIndex(x => x.LeaveID == earnedLeave.ID)].ApprovedTotalDays;
}
if(oPrevStatuses !=null)
{
var lval = oPrevStatuses.FirstOrDefault(x => x.LeaveId == earnedLeave.ID);
if (lval != null)
EDays = EDays/2 + lval.YearEndBalance; // as per Gov rule employee will get half of current year blance
}
return Math.Round(EDays, 0);
}
public List<EmpLeaveStatus> CurrentYearStatusForEncash(List<Employee> oEmployees, LeaveYear lYear, EnumLeaveStatus eStatus, DateTime operationDate, int payrolltypeId)
{
List<EmpLeaveStatus> oAllEmpsCurrYearBalance = new List<EmpLeaveStatus>();
try
{
//UpdateProgressStatus(EnumProcessStatus.Start);
//UpdateprocessStatus("Collecting Employee basic information....");
List<EmpLeaveStatus> oCurrYearBalance = null;
LeaveYear oCurrYear = lYear;
LeaveYear oPrvYear = new LeaveYearService().LastLeaveYear(oCurrYear);
List<EnmSetupManagerTranType> setupTypes = new SetupDetailService().GetTypes(EnumParameterSetup.Leave);
List<SetupDetail> setupDetails = new SetupDetailService().GetParameters(EnumParameterSetup.Leave);
List<LeaveParameter> leaveParamss = new LeaveParameterService().GetAllByPayrollTypeID(payrolltypeId);
List<LeaveParameterDetail> leaveParamDetails = new LeaveParameterService().GetDetails();
List<LeaveEntry> oLs = new LeaveEntryService().GetByLeaveYear(oCurrYear.ID);
List<LeaveYear> oLeaveYears = new LeaveYearService().Get();
List<EmpLeaveStatus> oPrevStatuses = GetAllPrvYearStatus(lYear,oLeaveYears);
List<LeaveException> oExceptions = new LeaveExceptionService().Get(operationDate, lYear.EndDate);
int nCount = 1;
foreach (Employee oEmp in oEmployees)
{
List<LeaveParameter> oAppLeaveParams = new LeaveParameterService().ApplicableParamsForReport(oEmp, setupTypes, setupDetails, leaveParamss);
//oCurrYearBalance = new EmpLeaveStatusService().CurrentYearEncashStatus(oEmp, operationDate, oAppLeaveParams, oCurrYear, oLs, eStatus, oPrevStatuses, leaveParamDetails, oExceptions);
oCurrYearBalance = CurrentYearEncashStatus(oEmp, operationDate, oAppLeaveParams, oCurrYear, oLs, eStatus, oPrevStatuses,
leaveParamDetails, oExceptions);
foreach (EmpLeaveStatus eSts in oCurrYearBalance)
oAllEmpsCurrYearBalance.Add(eSts);
}
//UpdateProgressStatus(EnumProcessStatus.End);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return oAllEmpsCurrYearBalance;
}
public static List<EmpLeaveStatus> GetAllPrvYearStatus(LeaveYear oCurrYear, List<LeaveYear> olYears)
{
LeaveYear oPrvYear = null;
foreach (LeaveYear oItem in olYears)
{
if (oItem.EndDate.AddDays(1) == oCurrYear.StartDate)
{
oPrvYear = oItem;
break;
}
}
if (oPrvYear == null)
return null;
return new LeaveProcessService().GetByYear(oPrvYear.ID);
}
// Dashboard
public List<EmpLeaveStatus> GetLeaveBalance(List<Employee> oEmployees, LeaveYear lYear, int payrollTypeID)
{
List<EmpLeaveStatus> oAllEmpsCurrYearBalance = null;
try
{
LeaveParameterService leaveParamService = new LeaveParameterService();
LeaveEntryService leaveEntryService = new LeaveEntryService();
List<EmpLeaveStatus> oCurrYearBalance = null;
DateTime operationDate = DateTime.Today;
LeaveYear oCurrYear = lYear;
LeaveYear previousYear = new LeaveYearService().GetLastLeaveYear(lYear, payrollTypeID);
oAllEmpsCurrYearBalance = new List<EmpLeaveStatus>();
List<LeaveParameter> leaveParamss =
leaveParamService.Get(EnumStatus.Regardless, payrollTypeID, null, true);
List<LeaveEntry> oLs = null;
if (oEmployees.Count == 1)
oLs = leaveEntryService.GetSumOfAvailedLeave(oEmployees[0].ID, oCurrYear.ID);
else oLs = leaveEntryService.GetSumOfAvailedLeave(oCurrYear.ID);
List<EmpLeaveStatus> oPrevStatuses = null;
if (oEmployees.Count == 1)
oPrevStatuses = this.GetStatus(oEmployees[0].ID, previousYear.ID);
else
oPrevStatuses = this.GetByYear(previousYear.ID);
List<LeaveException> oExceptions = new LeaveExceptionService().Get(lYear.StartDate, lYear.EndDate);
// List<DailyAttnProcess> _dailyAttnProcess = DailyAttnProcess.Get(oCurrYear.StartDate.Date, oCurrYear.EndDate.Date);
List<DailyAttnProcess> _dailyAttnProcessEmp = null;
LeaveService lService = new LeaveService();
List<Leave> _leaves = lService.GetAll();
//List<int, double> =
operationDate = operationDate > oCurrYear.EndDate ? oCurrYear.EndDate : operationDate;
DateTime from_Date = Ease.Core.Utility.Global.DateFunctions.FirstDateOfYear(operationDate);
DateTime to_Date = Ease.Core.Utility.Global.DateFunctions.LastDateOfYear(operationDate);
List<int> empIdIntList = new List<int>();
HashSet<int> uniqueLeaveIds = new HashSet<int>();
List<int> leaveIdIntList = new List<int>();
foreach (Employee oEmp in oEmployees)
{
empIdIntList.Add(oEmp.ID);
if (oEmp.GradeID == null) continue;
List<LeaveParameter> oAppLeaveParams =
leaveParamService.GetApplicableParam(leaveParamss, _leaves, (int)oEmp.GradeID, EnumLeaveparamOn.Grade, oEmp);
oAppLeaveParams.ForEach(x =>
{
x.Leave = _leaves.FirstOrDefault(l => l.ID == x.LeaveId);
});
foreach (LeaveParameter item in oAppLeaveParams)
{
if (uniqueLeaveIds.Add(item.ID))
{
leaveIdIntList.Add(item.ID);
}
}
}
string empIdString = string.Join(",", empIdIntList);
string leaveIdString = string.Join(",", leaveIdIntList);
DataTable EarnedLeaveOpeningdt = new DailyAttnProcessService().GetEarnedLeaveOpeningByEmployees(empIdString, leaveIdString, from_Date, to_Date);
foreach (Employee oEmp in oEmployees)
{
if (oEmp.GradeID == null) continue;
List<LeaveParameter> oAppLeaveParams =
leaveParamService.GetApplicableParam(leaveParamss, _leaves, (int)oEmp.GradeID, EnumLeaveparamOn.Grade, oEmp);
oAppLeaveParams.ForEach(x =>
{
x.Leave = _leaves.FirstOrDefault(l => l.ID == x.LeaveId);
});
oCurrYearBalance = this.CurrentYearStatus(oEmp, operationDate, oAppLeaveParams, oCurrYear, oLs,
oPrevStatuses, _leaves, _dailyAttnProcessEmp, oExceptions, EarnedLeaveOpeningdt);
foreach (EmpLeaveStatus eSts in oCurrYearBalance)
oAllEmpsCurrYearBalance.Add(eSts);
}
}
catch (Exception exp)
{
throw new Exception(exp.Message);
}
return oAllEmpsCurrYearBalance;
}
public List<EmpLeaveStatus> CurrentYearStatus(Employee oEmployee, DateTime operationDate,
List<LeaveParameter> oAppLeaveParams, LeaveYear oCurrYear, List<LeaveEntry> aviledLeaveSum,
List<EmpLeaveStatus> oPrevStatuses, List<Leave> _oLeaves, List<DailyAttnProcess> DaProcess, List<LeaveException> oExceptions, DataTable EarnedLeaveOpeningdt)
{
List<EmpLeaveStatus> oCurrYearBalance = null;
try
{
Leave oLeave = null;
bool isLateAttnAdjustFullyUsed = false;
//DateTime dCurrentDate = operationDate > oCurrYear.EndDate ? oCurrYear.EndDate : operationDate;
DateTime dCurrentDate = operationDate;
oCurrYearBalance = new List<EmpLeaveStatus>();
LeaveParameterDetail oDetail = null;
double lateAttendanceAdjustmentConsumeDays = 0; //GetLateAttendanceAdjustmentConsumeDays(DaProcess);
//double lateAttendanceAdjustmentCFDays = GetLateAttendanceAdjustmentCFDays(DaProcess);
double totalLeaveBalance = 0;
double leaveIndex = 0;
double totalAttendanceAdjustmentConsumeDays = lateAttendanceAdjustmentConsumeDays;
double remainingTotalAttnConsumedDays = totalAttendanceAdjustmentConsumeDays;
foreach (LeaveParameter oItem in oAppLeaveParams)
{
oLeave = _oLeaves.FirstOrDefault(o => o.ID == oItem.LeaveId);
if (oLeave == null) continue;
EmpLeaveStatus oPrevStatus = null;
EmpLeaveStatus oCurrStatus = new EmpLeaveStatus();
oCurrStatus.LeaveParameter = oItem;
oCurrStatus.LeaveNameView = oLeave.Description;
oCurrStatus.empNoView = oEmployee.EmployeeNo;
oCurrStatus.empNameView = oEmployee.Name;
oCurrStatus.LeaveNameView = oLeave.Description;
oCurrStatus.EmpId = oEmployee.ID;
oCurrStatus.LeaveId = oItem.LeaveId;
oCurrStatus.LeaveYearID = oCurrYear.ID;
if (oPrevStatuses != null)
if (oPrevStatuses != null)
oPrevStatus = oPrevStatuses.Find(delegate (EmpLeaveStatus el) { return el.LeaveId == oItem.LeaveId && el.EmpId == oEmployee.ID; });
LeaveException oLeaveEx = oExceptions.Find(delegate (LeaveException el) { return el.LeaveID == oItem.LeaveId && el.EmployeeID == oEmployee.ID; });
oDetail = this.GetDeatilForEmployee(oItem, oEmployee, operationDate);
if (oLeaveEx != null)
{
oCurrStatus.LeaveAvailed = new LeaveEntryService().GetLeaveExceptionAmtByType(oEmployee.ID, oItem.Leave.ID, oLeaveEx, EnumLeaveStatus.Approved); //10
oCurrStatus.OpeningBalance = oLeaveEx.OpeningBalance;
oCurrStatus.CFDays = oLeaveEx.CFDays;
//if (leaveEncashment != null && oItem.LeaveId == leaveEncashment.LeaveID.Integer)
// oCurrStatus.LeaveAvailed += leaveEncashment.EncashmentDays;
oCurrStatus.YearEndBalance = oCurrStatus.OpeningBalance - oCurrStatus.LeaveAvailed;
//oCurrStatus.CFDays = oCurrStatus.YearEndBalance;
}
else
{
if (oDetail != null) oCurrStatus.NormalLeaveDays = 0;
else
{
oCurrYearBalance.Add(oCurrStatus);
continue;
}
oCurrStatus.EligibleParamDetail = oDetail;
oCurrStatus.CurrentYearBalance = 0;
oCurrStatus.CurrentYearOpening = 0;
oCurrStatus.CFDays = oDetail.MaxCF;
oCurrStatus.EncashDays = oDetail.MaxEncash;
#region calculate the opening balance new
if (oPrevStatus == null)
oCurrStatus.CurrentYearOpening = 0;
else
oCurrStatus.CurrentYearOpening = oPrevStatus.YearEndBalance;
#endregion
#region calculate the current year balance
if (oItem.ApplicableFor == LeaveApplicableType.Confirmed)
{
if (oEmployee.IsConfirmed == true)
{
oCurrStatus.CurrentYearBalance =
Math.Round(
this.GetCurrentYearDaysMultipleEmp(oItem, oEmployee, dCurrentDate, oPrevStatus,
oDetail, oCurrYear, EarnedLeaveOpeningdt), oItem.Leave.BalanceRoundofDigit);
}
else oCurrStatus.CurrentYearBalance = 0;
}
else
{
oCurrStatus.CurrentYearBalance =
Math.Round(
this.GetCurrentYearDaysMultipleEmp(oItem, oEmployee, dCurrentDate, oPrevStatus, oDetail,
oCurrYear, EarnedLeaveOpeningdt), oItem.Leave.BalanceRoundofDigit);
}
#endregion
oCurrStatus.OpeningBalance = oCurrStatus.CurrentYearBalance + oCurrStatus.CurrentYearOpening;
// only for one-year completion month
// for example, yearly max days is 20. A employee join 20 Jan Last year, so 19 days carry forwarded from last year
// In current year 20 Jan, he will be eligible for 20 days. but system calculating = Carry forward (19 days)
// + current month (0, because current month not yet completed)= 19, but as per policy he will get 20 days from one year completion.
// so folloiwng loic is needed to that
if (oItem.CalculationType == EnumLeaveCalculationType.Monthly_Earn)
{
double months = Global.DateFunctions.DateDiff("M", oEmployee.JoiningDate, dCurrentDate);
if (oDetail.Year == 1 && months == 12)
if (oCurrStatus.OpeningBalance < oDetail.MaxDays) oCurrStatus.OpeningBalance = oDetail.MaxDays;
}
#region calculate leave availed in current year
if (aviledLeaveSum == null)
oCurrStatus.LeaveAvailed = 0;
else
{
LeaveEntry avl = aviledLeaveSum.FirstOrDefault(el =>
el.LeaveID == oItem.LeaveId && el.EmpID == oEmployee.ID);
if (avl != null)
oCurrStatus.LeaveAvailed = Convert.ToDouble(avl.ApprovedTotalDays);
}
#endregion calculate leave availed in current year
#region Calculate Year-End Balance
oCurrStatus.YearEndBalance = 0;
if (oItem.MaxAccumulatedDays > 0)
{
double maxaccumulationDays = oItem.MaxAccumulatedDays;
//if (oLeave.IsEarnedLeave && oEmployee.EmployeeNo.Substring(0,1).ToLower()=="s" ) // only for Synovia
//{
// TimeSpan ts = operationDate - oEmployee.JoiningDate;
// double y = Math.Round((double)ts.Days / 365, 2);
// if (y < 11)
// {
// maxaccumulationDays = 42;
// }
// else if (y < 26)
// {
// maxaccumulationDays = 56;
// }
// else
// {
// maxaccumulationDays = 70;
// }
//}
//oCurrStatus.OpeningBalance = oCurrStatus.OpeningBalance > oItem.MaxAccumulatedDays ? oItem.MaxAccumulatedDays : oCurrStatus.OpeningBalance;
oCurrStatus.OpeningBalance = oCurrStatus.OpeningBalance > maxaccumulationDays ? maxaccumulationDays : oCurrStatus.OpeningBalance;
oCurrStatus.OpeningBalance = Math.Round(oCurrStatus.OpeningBalance, oItem.Leave.BalanceRoundofDigit);
}
if (oLeave.IsBalanceCalculationNeeded == false)
{
oCurrStatus.NormalLeaveDays = 0;
oCurrStatus.OpeningBalance = 0;
}
else
{
if (oItem.CalculationType == EnumLeaveCalculationType.Hourly_365Day ||
oItem.CalculationType == EnumLeaveCalculationType.Hourly_Monthly)
{
double consumed = oCurrStatus.LeaveAvailed;
int fullpart = (int)consumed;
int fracpart3 = 0;
int openfracpart3 = 0;
int yeFrac = 0;
int yefullpart = 0;
string yeBalance;
if (consumed - fullpart > 0)
{
double fracpart = Math.Round(consumed - fullpart, oItem.Leave.BalanceRoundofDigit);
if (fracpart > .59)
{
fracpart = fracpart - .6;
fullpart = fullpart + 1;
string a = (fullpart + "." + (int)((decimal)fracpart * 100));
oCurrStatus.LeaveAvailed = Math.Round(Convert.ToDouble(a), oItem.Leave.BalanceRoundofDigit);
}
string ss = fracpart.ToString().Substring(2);
if (ss.Length == 1)
{
fracpart3 = Convert.ToInt16(ss + "0");
}
double opening = oCurrStatus.OpeningBalance;
int openfullpart = (int)opening;
if (opening - openfullpart > 0)
{
double openfracpart = Math.Round(opening - openfullpart, oItem.Leave.BalanceRoundofDigit);
string openss = openfracpart.ToString().Substring(2);
if (openss.Length == 1)
{
openfracpart3 = Convert.ToInt16(ss + "0");
}
if (openfracpart3 < fracpart3)
{
yeFrac = (openfracpart3 + 60) - fracpart3;
yefullpart = (openfullpart - 1) - fullpart;
}
else
{
yeFrac = openfracpart3 - fracpart3;
yefullpart = openfullpart - fullpart;
}
yeBalance = (yefullpart + "." + yeFrac);
oCurrStatus.YearEndBalance = Math.Round(Convert.ToDouble(yeBalance), oItem.Leave.BalanceRoundofDigit);
}
else
{
yeFrac = (int)(((decimal)0.60 - (decimal)fracpart) * 100);
yefullpart = (openfullpart - 1) - fullpart;
yeBalance = (yefullpart + "." + yeFrac);
oCurrStatus.YearEndBalance = Math.Round(Convert.ToDouble(yeBalance), oItem.Leave.BalanceRoundofDigit);
}
}
else
{
oCurrStatus.YearEndBalance =
Math.Round(
oCurrStatus.OpeningBalance -
(oCurrStatus.ForfitedDays + oCurrStatus.LeaveAvailed), oItem.Leave.BalanceRoundofDigit);
oCurrStatus.YearEndBalance = Math.Round(oCurrStatus.YearEndBalance, oItem.Leave.BalanceRoundofDigit);
}
}
else
{
oCurrStatus.YearEndBalance =
Math.Round(
oCurrStatus.OpeningBalance - (oCurrStatus.ForfitedDays + oCurrStatus.LeaveAvailed),
oItem.Leave.BalanceRoundofDigit);
oCurrStatus.YearEndBalance = Math.Round(oCurrStatus.YearEndBalance, oItem.Leave.BalanceRoundofDigit);
}
//if (oLeave.AutoLeaveReason == true)
//{
// if (!isLateAttnAdjustFullyUsed)
// {
// totalLeaveBalance = oCurrStatus.YearEndBalance;
// if (leaveIndex >= totalAttendanceAdjustmentConsumeDays)
// {
// isLateAttnAdjustFullyUsed = true;
// }
// if (totalLeaveBalance >= remainingTotalAttnConsumedDays && totalLeaveBalance != 0)
// {
// oCurrStatus.LateLeaveAdjustmentConsumed = remainingTotalAttnConsumedDays;
// remainingTotalAttnConsumedDays = 0;
// isLateAttnAdjustFullyUsed = true;
// }
// else if (totalLeaveBalance < remainingTotalAttnConsumedDays && totalLeaveBalance != 0)
// {
// remainingTotalAttnConsumedDays = remainingTotalAttnConsumedDays - totalLeaveBalance;
// leaveIndex += totalLeaveBalance;
// oCurrStatus.LateLeaveAdjustmentConsumed = totalLeaveBalance;
// }
// }
//}
}
#endregion
}
oCurrYearBalance.Add(oCurrStatus);
#endregion
}
return oCurrYearBalance;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public List<LeaveHistory> GetRecord(string empIds, DateTime fromDate, DateTime toDate, EnumLeaveStatus leaveStatus)
{
List<LeaveHistory> leaveHistories = new List<LeaveHistory>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(LeaveEntryDA.GetRecord(tc, empIds, fromDate, toDate, (int)leaveStatus));
while (dr.Read())
{
LeaveHistory leaveHistory = new LeaveHistory();
leaveHistory.TranID = dr.GetInt32("TranID").Value;
leaveHistory.Remarks = dr.GetString("Remarks");
if (dr.GetBoolean("isLFA") != null && dr.GetBoolean("isLFA") == true)
{
leaveHistory.Remarks = leaveHistory.Remarks + " LFA.";
}
if (dr.GetBoolean("isOnBehalf") != null && dr.GetBoolean("isOnBehalf") == true)
{
leaveHistory.Remarks = leaveHistory.Remarks + " On Behalf.";
}
leaveHistory.AprFromDate = Convert.ToDateTime(dr.GetString("AprFromDate")).ToString("dd MMM yyyy");
leaveHistory.AprToDate = Convert.ToDateTime(dr.GetString("AprToDate")).ToString("dd MMM yyyy");
leaveHistory.AprTotalDays = dr.GetInt32("AprTotalDays").Value;
leaveHistory.Description = dr.GetString("Description");
leaveHistory.AppLeaveDate = Convert.ToDateTime(dr.GetString("AppLeaveDate")).ToString("dd MMM yyyy");
short x = (short)dr.GetInt16("LeaveStatus");
switch (x)
{
case 0:
leaveHistory.LeaveStatus = "Drafted";
break;
case 1:
leaveHistory.LeaveStatus = "Waiting for Approval";
break;
case 2:
leaveHistory.LeaveStatus = "Approved";
break;
case 3:
leaveHistory.LeaveStatus = "reverted";
break;
case 4:
leaveHistory.LeaveStatus = "Declined";
break;
case 5:
leaveHistory.LeaveStatus = "Approved";
break;
case 6:
leaveHistory.LeaveStatus = "Approved";
break;
case 9:
leaveHistory.LeaveStatus = "Requested for Cancel";
break;
case 10:
leaveHistory.LeaveStatus = "Cancelled";
break;
}
leaveHistories.Add(leaveHistory);
}
dr.Close();
tc.End();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return leaveHistories;
}
private double GetCurrentYearDays(LeaveParameter param, Employee emp, DateTime dCurrentDate,
EmpLeaveStatus oPreviousStatus, LeaveParameterDetail oDetail, LeaveYear oCurrentYear)
{
double nTotalDays = 0;
TimeSpan ts = new TimeSpan();
double nTempDays = 0;
EnumLeaveCalculationType eType = param.CalculationType;
if (eType == EnumLeaveCalculationType.OneDay_On_18Day_Present)
{
//DateTime from_Date = Ease.Core.Utility.Global.DateFunctions.FirstDateOfYear(DateTime.Today);
//DateTime to_Date = Ease.Core.Utility.Global.DateFunctions.LastDateOfYear(DateTime.Today);
DateTime from_Date = Ease.Core.Utility.Global.DateFunctions.FirstDateOfYear(dCurrentDate);
DateTime to_Date = Ease.Core.Utility.Global.DateFunctions.LastDateOfYear(dCurrentDate);
DataSet ds = new DailyAttnProcessService().GetEarnedLeaveOpening(emp.ID, param.LeaveId, from_Date, to_Date);
// oAttnProcess = DailyAttnProcess.Get(emp.ID, PayrollPayrollGlobalFunctions.FirstDateOfYear(dCurrentDate), dCurrentDate);
// oPresentRecords = oAttnProcess.FindAll(delegate (DailyAttnProcess oitem) { return oitem.AttenType == EnumAttendanceType.Present; });
if (ds != null && ds.Tables.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
nTotalDays = Convert.ToDouble(dr[0].ToString());
}
}
}
else if (eType == EnumLeaveCalculationType.Hourly_365Day || eType == EnumLeaveCalculationType.Days_365)
{
if (emp.JoiningDate > oCurrentYear.StartDate)
ts = new DateTime(dCurrentDate.Year, 12, 31) - emp.JoiningDate;
else
ts = new DateTime(dCurrentDate.Year, 12, 31) - new DateTime(dCurrentDate.Year, 1, 1);
if (emp.JoiningDate > oCurrentYear.StartDate)
{
nTempDays = oDetail.MaxDays / 365;
}
else
{
nTempDays = oDetail.MaxDays / ts.Days;
}
nTotalDays = (double)nTempDays * ts.Days;
nTotalDays = Math.Floor(nTotalDays);
}
else if (eType == EnumLeaveCalculationType.Monthly_Earn)
{
if (emp.ID == 1807)
{
}
if (emp.JoiningDate > oCurrentYear.StartDate)
{
double fracMonth = 0.0;
DateTime startDate = emp.JoiningDate;
if (emp.JoiningDate != GlobalFunctions.FirstDateOfMonth(emp.JoiningDate))
{
fracMonth = GlobalFunctions.GetFractinalOfMonth(emp.JoiningDate);
startDate = GlobalFunctions.LastDateOfMonth(emp.JoiningDate).AddDays(1);
}
double months = 0;
if (dCurrentDate != oCurrentYear.EndDate) // consider year-end
{
months = Global.DateFunctions.DateDiff("M",
startDate,
GlobalFunctions.FirstDateOfMonth(dCurrentDate).AddDays(-1)) + 1;
}
else
{
months = Global.DateFunctions.DateDiff("M",
startDate, GlobalFunctions.FirstDateOfMonth(dCurrentDate)) + 1;
}
months = (double)oDetail.MaxDays / (double)12 * (months + fracMonth);
months = GlobalFunctions.Round(months);
nTotalDays = months;
}
else
{
double months = 12;
if (dCurrentDate != oCurrentYear.EndDate) // Year End
{
months = Global.DateFunctions.DateDiff("M", oCurrentYear.StartDate,
GlobalFunctions.FirstDateOfMonth(dCurrentDate).AddDays(-1)) + 1;
}
months = (double)oDetail.MaxDays / (double)12 * months;
months = GlobalFunctions.Round(months);
nTotalDays = months;
}
}
//else if (eType == EnumLeaveCalculationType.Monthly)
//{
// double months = 0;
// if (emp.JoiningDate > oCurrentYear.StartDate)
// {
// months = Global.DateFunctions.DateDiff("m", emp.JoiningDate, new DateTime(dCurrentDate.Year, 12, 31));
// nTempDays = oDetail.MaxDays / 12;
// nTotalDays = nTempDays * months;
// }
// else
// {
// nTempDays = oDetail.MaxDays;
// }
//}
else if (eType == EnumLeaveCalculationType.Yearly)
{
if (emp.JoiningDate > oCurrentYear.StartDate)
ts = new DateTime(dCurrentDate.Year, 12, 31) - emp.JoiningDate;
else
ts = new DateTime(dCurrentDate.Year, 12, 31) - new DateTime(dCurrentDate.Year, 1, 1);
nTempDays = oDetail.MaxDays / 12;
nTotalDays = (double)nTempDays * (ts.Days / 30);
nTotalDays = Math.Round(nTotalDays,param.Leave.BalanceRoundofDigit);
}
else if (eType == EnumLeaveCalculationType.Yearly_without_Prorated)
{
nTotalDays = Math.Round(oDetail.MaxDays, param.Leave.BalanceRoundofDigit);
}
nTotalDays = Math.Round(nTotalDays, param.Leave.BalanceRoundofDigit);
if (param.Leave.BalanceRoundofDigit == 1)
{
string stl = nTotalDays.ToString();
if (stl.Contains('.') == true)
{
string ntr = stl.Substring(stl.Length - 1, 1);
if (Convert.ToInt32(ntr) > 0 && Convert.ToInt32(ntr) < 6)
{
nTotalDays = Math.Floor(nTotalDays) + .5;
}
else if (Convert.ToInt32(ntr) > 5)
{
nTotalDays = Math.Floor(nTotalDays) + 1;
}
}
}
return Math.Round(nTotalDays, param.Leave.BalanceRoundofDigit);
}
private double GetCurrentYearDaysMultipleEmp(LeaveParameter param, Employee emp, DateTime dCurrentDate,
EmpLeaveStatus oPreviousStatus, LeaveParameterDetail oDetail, LeaveYear oCurrentYear, DataTable EarnedLeaveOpeningdt)
{
double nTotalDays = 0;
TimeSpan ts = new TimeSpan();
double nTempDays = 0;
EnumLeaveCalculationType eType = param.CalculationType;
if (eType == EnumLeaveCalculationType.OneDay_On_18Day_Present)
{
//DateTime from_Date = Ease.Core.Utility.Global.DateFunctions.FirstDateOfYear(DateTime.Today);
//DateTime to_Date = Ease.Core.Utility.Global.DateFunctions.LastDateOfYear(DateTime.Today);
//DateTime from_Date = Ease.Core.Utility.Global.DateFunctions.FirstDateOfYear(dCurrentDate);
//DateTime to_Date = Ease.Core.Utility.Global.DateFunctions.LastDateOfYear(dCurrentDate);
//DataSet ds = new DailyAttnProcessService().GetEarnedLeaveOpening(emp.ID, param.LeaveId, from_Date, to_Date);
// oAttnProcess = DailyAttnProcess.Get(emp.ID, PayrollPayrollGlobalFunctions.FirstDateOfYear(dCurrentDate), dCurrentDate);
// oPresentRecords = oAttnProcess.FindAll(delegate (DailyAttnProcess oitem) { return oitem.AttenType == EnumAttendanceType.Present; });
//if (ds != null && ds.Tables.Count > 0)
//{
// foreach (DataRow dr in ds.Tables[0].Rows)
// {
// nTotalDays = Convert.ToDouble(dr[0].ToString());
// }
//}
foreach (DataRow dr in EarnedLeaveOpeningdt.Rows)
{
if (Convert.ToInt32(dr["EMPLOYEEID"]) == emp.ID)
nTotalDays = Convert.ToDouble(dr[0].ToString());
}
}
else if (eType == EnumLeaveCalculationType.Hourly_365Day || eType == EnumLeaveCalculationType.Days_365)
{
if (emp.JoiningDate > oCurrentYear.StartDate)
ts = new DateTime(dCurrentDate.Year, 12, 31) - emp.JoiningDate;
else
ts = new DateTime(dCurrentDate.Year, 12, 31) - new DateTime(dCurrentDate.Year, 1, 1);
if (emp.JoiningDate > oCurrentYear.StartDate)
{
nTempDays = oDetail.MaxDays / 365;
}
else
{
nTempDays = oDetail.MaxDays / ts.Days;
}
nTotalDays = (double)nTempDays * ts.Days;
nTotalDays = Math.Floor(nTotalDays);
}
else if (eType == EnumLeaveCalculationType.Monthly_Earn)
{
if (emp.ID == 1807)
{
}
if (emp.JoiningDate > oCurrentYear.StartDate)
{
double fracMonth = 0.0;
DateTime startDate = emp.JoiningDate;
if (emp.JoiningDate != GlobalFunctions.FirstDateOfMonth(emp.JoiningDate))
{
fracMonth = GlobalFunctions.GetFractinalOfMonth(emp.JoiningDate);
startDate = GlobalFunctions.LastDateOfMonth(emp.JoiningDate).AddDays(1);
}
double months = 0;
if (dCurrentDate != oCurrentYear.EndDate) // consider year-end
{
months = Global.DateFunctions.DateDiff("M",
startDate,
GlobalFunctions.FirstDateOfMonth(dCurrentDate).AddDays(-1)) + 1;
}
else
{
months = Global.DateFunctions.DateDiff("M",
startDate, GlobalFunctions.FirstDateOfMonth(dCurrentDate)) + 1;
}
months = (double)oDetail.MaxDays / (double)12 * (months + fracMonth);
months = GlobalFunctions.Round(months);
nTotalDays = months;
}
else
{
double months = 12;
if (dCurrentDate != oCurrentYear.EndDate) // Year End
{
months = Global.DateFunctions.DateDiff("M", oCurrentYear.StartDate,
GlobalFunctions.FirstDateOfMonth(dCurrentDate).AddDays(-1)) + 1;
}
months = (double)oDetail.MaxDays / (double)12 * months;
months = GlobalFunctions.Round(months);
nTotalDays = months;
}
nTotalDays = Math.Round(nTotalDays, param.Leave.BalanceRoundofDigit);
if (param.Leave.BalanceRoundofDigit == 1)
{
string stl = nTotalDays.ToString();
if (stl.Contains('.') == true)
{
string ntr = stl.Substring(stl.Length - 1, 1);
if (Convert.ToInt32(ntr) > 0 && Convert.ToInt32(ntr) < 6)
{
nTotalDays = Math.Floor(nTotalDays) + .5;
}
else if (Convert.ToInt32(ntr) > 5)
{
nTotalDays = Math.Floor(nTotalDays) + 1;
}
}
}
}
//else if (eType == EnumLeaveCalculationType.Monthly)
//{
// double months = 0;
// if (emp.JoiningDate > oCurrentYear.StartDate)
// {
// months = Global.DateFunctions.DateDiff("m", emp.JoiningDate, new DateTime(dCurrentDate.Year, 12, 31));
// nTempDays = oDetail.MaxDays / 12;
// nTotalDays = nTempDays * months;
// }
// else
// {
// nTempDays = oDetail.MaxDays;
// }
//}
else if (eType == EnumLeaveCalculationType.Yearly)
{
if (emp.JoiningDate > oCurrentYear.StartDate)
ts = new DateTime(dCurrentDate.Year, 12, 31) - emp.JoiningDate;
else
ts = new DateTime(dCurrentDate.Year, 12, 31) - new DateTime(dCurrentDate.Year, 1, 1);
nTempDays = oDetail.MaxDays / 12;
nTotalDays = (double)nTempDays * (ts.Days / 30);
nTotalDays = Math.Round(nTotalDays, param.Leave.BalanceRoundofDigit);
}
else if (eType == EnumLeaveCalculationType.Yearly_without_Prorated)
{
nTotalDays = Math.Round(oDetail.MaxDays, param.Leave.BalanceRoundofDigit);
}
return Math.Round(nTotalDays, param.Leave.BalanceRoundofDigit);
}
public List<EmpLeaveStatus> CurrentYearStatus(Employee oEmployee, DateTime operationDate,
List<LeaveParameter> oAppLeaveParams, LeaveYear oCurrYear, List<LeaveEntry> aviledLeaveSum, List<EmpLeaveStatus> oPrevStatuses, List<LeaveParameterDetail> ParamDetails, List<Leave> _oLeaves, List<DailyAttnProcess> DaProcess)
{
List<EmpLeaveStatus> oCurrYearBalance = null;
try
{
Leave oLeave = null;
DateTime dCurrentDate;
if (oCurrYear != null)
{
dCurrentDate = operationDate > oCurrYear.EndDate ? oCurrYear.EndDate : operationDate;
}
else
{
dCurrentDate = operationDate;
}
oCurrYearBalance = new List<EmpLeaveStatus>();
LeaveParameterDetail oDetail = null;
double lateAttendanceAdjustmentConsumeDays = 0; //GetLateAttendanceAdjustmentConsumeDays(DaProcess);
ConfigurationManager oCon = new ConfigurationManager();
double totalAttendanceAdjustmentConsumeDays = lateAttendanceAdjustmentConsumeDays;
double remainingTotalAttnConsumedDays = totalAttendanceAdjustmentConsumeDays;
LeaveParameterService leaveParameterService = new LeaveParameterService();
List<LeaveException> oExceptions = new LeaveExceptionService().Get(oCurrYear.StartDate, oCurrYear.EndDate);
foreach (LeaveParameter oItem in oAppLeaveParams)
{
EmpLeaveStatus oPrevStatus = null;
EmpLeaveStatus oCurrStatus = new EmpLeaveStatus();
oCurrStatus.Leave = oItem.Leave;
oCurrStatus.EmpId = oEmployee.ID;
oLeave = _oLeaves.Where(o => o.ID == oItem.LeaveId).FirstOrDefault();
if (oLeave == null) continue;
oCurrStatus.LeaveId = oItem.LeaveId;
oCurrStatus.LeaveYearID = oCurrYear.ID;
if (oPrevStatuses != null)
oPrevStatus = oPrevStatuses.Find(delegate (EmpLeaveStatus el) { return el.LeaveId == oItem.LeaveId && el.EmpId == oEmployee.ID; });
LeaveException oLeaveEx = oExceptions.Find(delegate (LeaveException el) { return el.LeaveID == oItem.LeaveId && el.EmployeeID == oEmployee.ID; });
oDetail = leaveParameterService.GetApplicableForEmployee(oEmployee, operationDate, ParamDetails, oItem.ID);
if (oLeaveEx != null)
{
oCurrStatus.LeaveAvailed = new LeaveEntryService().GetLeaveExceptionAmtByType(oEmployee.ID, oItem.Leave.ID, oLeaveEx, EnumLeaveStatus.Approved); //10
oCurrStatus.OpeningBalance = oLeaveEx.OpeningBalance;
oCurrStatus.CFDays = oLeaveEx.CFDays;
//if (leaveEncashment != null && oItem.LeaveId == leaveEncashment.LeaveID.Integer)
// oCurrStatus.LeaveAvailed += leaveEncashment.EncashmentDays;
oCurrStatus.YearEndBalance = oCurrStatus.OpeningBalance - oCurrStatus.LeaveAvailed;
//oCurrStatus.CFDays = oCurrStatus.YearEndBalance;
}
else
{
if (oDetail != null) oCurrStatus.NormalLeaveDays = oDetail.MaxDays;
else continue;
oCurrStatus.CurrentYearBalance = 0;
oCurrStatus.CurrentYearOpening = 0;
#region calculate the opening balance new
if (oPrevStatus == null)
oCurrStatus.CurrentYearOpening = 0;
else
{
oCurrStatus.CurrentYearOpening = oPrevStatus.YearEndBalance;
// oCurrStatus.CurrentYearOpening = oPrevStatus.CFDays;
oCurrStatus.CFDays = oPrevStatus.CFDays;
}
#endregion
#region calculate the current year balance
if (oItem.ApplicableFor == LeaveApplicableType.Confirmed)
{
if (oEmployee.IsConfirmed == true)
{
oCurrStatus.CurrentYearBalance = Math.Round(GetCurrentYearDays(oItem , oEmployee, dCurrentDate, oPrevStatus, oDetail, oCurrYear), oItem.Leave.BalanceRoundofDigit);
}
else oCurrStatus.CurrentYearBalance = 0;
}
else
{
oCurrStatus.CurrentYearBalance = Math.Round(GetCurrentYearDays(oItem , oEmployee, dCurrentDate, oPrevStatus, oDetail, oCurrYear), oItem.Leave.BalanceRoundofDigit);
}
#endregion
oCurrStatus.OpeningBalance = oCurrStatus.CurrentYearBalance + oCurrStatus.CurrentYearOpening;
// only for one-year completion month
// for example, yearly max days is 20. A employee join 20 Jan Last year, so 19 days carry forwarded from last year
// In current year 20 Jan, he will be eligible for 20 days. but system calculating = Carry forward (19 days)
// + current month (0, because current month not yet completed)= 19, but as per policy he will get 20 days from one year completion.
// so folloiwng loic is needed to that
if (oItem.CalculationType == EnumLeaveCalculationType.Monthly_Earn)
{
double months = Global.DateFunctions.DateDiff("M", oEmployee.JoiningDate, dCurrentDate);
if (oDetail.Year == 1 && months == 12)
if (oCurrStatus.OpeningBalance < oDetail.MaxDays) oCurrStatus.OpeningBalance = oDetail.MaxDays;
}
#region calculate leave availed in current year
if (aviledLeaveSum == null)
oCurrStatus.LeaveAvailed = 0;
else
{
LeaveEntry avl = aviledLeaveSum.Find(delegate (LeaveEntry el) { return el.LeaveID == oItem.LeaveId && el.EmpID == oEmployee.ID; });
if (avl != null)
oCurrStatus.LeaveAvailed = Convert.ToDouble(avl.ApprovedTotalDays);
}
#endregion calculate leave availed in current year
#region Calculate Year-End Balance
oCurrStatus.YearEndBalance = 0;
if (oItem.MaxAccumulatedDays > 0)
{
oCurrStatus.OpeningBalance = oCurrStatus.OpeningBalance > oItem.MaxAccumulatedDays ? oItem.MaxAccumulatedDays : oCurrStatus.OpeningBalance;
oCurrStatus.OpeningBalance = Math.Round(oCurrStatus.OpeningBalance, oItem.Leave.BalanceRoundofDigit);
}
if (oLeave.IsBalanceCalculationNeeded == false)
{
oCurrStatus.NormalLeaveDays = 0;
oCurrStatus.OpeningBalance = 0;
}
else
{
if (oItem.CalculationType == EnumLeaveCalculationType.Hourly || oItem.CalculationType == EnumLeaveCalculationType.Hourly_Prorated)
{
double consumed = oCurrStatus.LeaveAvailed;
int fullpart = (int)consumed;
int fracpart3 = 0;
int openfracpart3 = 0;
int yeFrac = 0;
int yefullpart = 0;
string yeBalance;
if (consumed - fullpart > 0)
{
double fracpart = Math.Round(consumed - fullpart, oItem.Leave.BalanceRoundofDigit);
if (fracpart > .59)
{
fracpart = fracpart - .6;
fullpart = fullpart + 1;
string a = (fullpart + "." + (int)((decimal)fracpart * 100));
oCurrStatus.LeaveAvailed = Math.Round(Convert.ToDouble(a), oItem.Leave.BalanceRoundofDigit);
}
string ss = fracpart.ToString().Substring(2);
if (ss.Length == 1)
{
fracpart3 = Convert.ToInt16(ss + "0");
}
double opening = oCurrStatus.OpeningBalance;
int openfullpart = (int)opening;
if (opening - openfullpart > 0)
{
double openfracpart = Math.Round(opening - openfullpart, oItem.Leave.BalanceRoundofDigit);
string openss = openfracpart.ToString().Substring(2);
if (openss.Length == 1)
{
openfracpart3 = Convert.ToInt16(ss + "0");
}
if (openfracpart3 < fracpart3)
{
yeFrac = (openfracpart3 + 60) - fracpart3;
yefullpart = (openfullpart - 1) - fullpart;
}
else
{
yeFrac = openfracpart3 - fracpart3;
yefullpart = openfullpart - fullpart;
}
yeBalance = (yefullpart + "." + yeFrac);
oCurrStatus.YearEndBalance = Math.Round(Convert.ToDouble(yeBalance), oItem.Leave.BalanceRoundofDigit);
}
else
{
yeFrac = (int)(((decimal)0.60 - (decimal)fracpart) * 100);
yefullpart = (openfullpart - 1) - fullpart;
yeBalance = (yefullpart + "." + yeFrac);
oCurrStatus.YearEndBalance = Math.Round(Convert.ToDouble(yeBalance), oItem.Leave.BalanceRoundofDigit);
}
}
else
{
oCurrStatus.YearEndBalance = Math.Round(oCurrStatus.OpeningBalance - (oCurrStatus.ForfitedDays + oCurrStatus.LeaveAvailed), oItem.Leave.BalanceRoundofDigit);
oCurrStatus.YearEndBalance = Math.Round(oCurrStatus.YearEndBalance, oItem.Leave.BalanceRoundofDigit);
}
}
else
{
oCurrStatus.YearEndBalance = Math.Round(oCurrStatus.OpeningBalance - (oCurrStatus.ForfitedDays + oCurrStatus.LeaveAvailed), oItem.Leave.BalanceRoundofDigit);
oCurrStatus.YearEndBalance = Math.Round(oCurrStatus.YearEndBalance, oItem.Leave.BalanceRoundofDigit);
}
#region cmnt
//if (oLeave.AutoLeaveReason == true)
//{
// if (!isLateAttnAdjustFullyUsed)
// {
// totalLeaveBalance = oCurrStatus.YearEndBalance;
// if (leaveIndex >= totalAttendanceAdjustmentConsumeDays)
// {
// isLateAttnAdjustFullyUsed = true;
// }
// if (totalLeaveBalance >= remainingTotalAttnConsumedDays && totalLeaveBalance != 0)
// {
// oCurrStatus.LateLeaveAdjustmentConsumed = remainingTotalAttnConsumedDays;
// remainingTotalAttnConsumedDays = 0;
// isLateAttnAdjustFullyUsed = true;
// }
// else if (totalLeaveBalance < remainingTotalAttnConsumedDays && totalLeaveBalance != 0)
// {
// remainingTotalAttnConsumedDays = remainingTotalAttnConsumedDays - totalLeaveBalance;
// leaveIndex += totalLeaveBalance;
// oCurrStatus.LateLeaveAdjustmentConsumed = totalLeaveBalance;
// }
// }
//}
#endregion
}
#endregion Calculate Year-End Balance
}
if (oLeave.IsBalanceCalculationNeeded)
oCurrYearBalance.Add(oCurrStatus);
}
return oCurrYearBalance;
}
catch (Exception exp)
{
throw new Exception(exp.Message);
}
return oCurrYearBalance;
}
public List<EmpLeaveStatus> CurrentYearStatus(List<Employee> oEmployees, LeaveYear lYear, int PayrollTypeID)
{
List<EmpLeaveStatus> oAllEmpsCurrYearBalance = null;
try
{
List<EmpLeaveStatus> oCurrYearBalance = null;
DateTime operationDate = DateTime.Now; //GlobalFunctions.GetOperationDate();
LeaveYear oCurrYear = lYear;
oAllEmpsCurrYearBalance = new List<EmpLeaveStatus>();
LeaveYearService _leaveYearService = new LeaveYearService();
LeaveYear oPrvYear = _leaveYearService.GetLastLeaveYear(oCurrYear, PayrollTypeID);
List<EnmSetupManagerTranType> setupTypes = new SetupDetailService().GetTypes(EnumParameterSetup.Leave);
List<SetupDetail> setupDetails = new SetupDetailService().GetParameters(EnumParameterSetup.Leave);
List<LeaveParameter> leaveParamss =
new LeaveParameterService().Get(EnumStatus.Active, PayrollTypeID, null, true);
//List<LeaveParameter> leaveParamss = new LeaveParameterService().GetAllByPayrollTypeID(PayrollTypeID);
//leaveParamss = leaveParamss.OrderBy(o => o.Leave.Sequence).ToList();
List<LeaveParameterDetail> leaveParamDetails = new LeaveParameterService().GetDetails();
List<LeaveEntry> oLs = null;
if (oEmployees.Count == 1)
oLs = new LeaveEntryService().GetSumOfAvailedLeave(oEmployees[0].ID, oCurrYear.ID);
else oLs = new LeaveEntryService().GetSumOfAvailedLeave(oCurrYear.ID);
List<EmpLeaveStatus> oPrevStatuses = new LeaveProcessService().GetByYear(oPrvYear.ID);
List<DailyAttnProcess> _dailyAttnProcess = new List<DailyAttnProcess>(); //DailyAttnProcess.Get(oCurrYear.StartDate.Date, oCurrYear.EndDate.Date);
List<DailyAttnProcess> _dailyAttnProcessEmp = null;
List<Leave> _leaves = new LeaveService().Get(EnumStatus.Active, PayrollTypeID);
foreach (var parameter in leaveParamss)
{
parameter.Leave = _leaves.Find(x => x.ID == parameter.LeaveId);
}
foreach (Employee oEmp in oEmployees)
{
try
{
List<LeaveParameter> oAppLeaveParams = new LeaveParameterService().GetApplicableParam(leaveParamss, _leaves, (int)oEmp.GradeID, EnumLeaveparamOn.Grade, oEmp);
//List<LeaveParameter> oAppLeaveParams = LeaveParameterService.ApplicableParams(oEmp, setupTypes, setupDetails, leaveParamss);
//_dailyAttnProcessEmp = _dailyAttnProcess.Where(o => o.EmployeeID == oEmp.ID).ToList();
oCurrYearBalance = CurrentYearStatus(oEmp, operationDate, oAppLeaveParams, oCurrYear, oLs, oPrevStatuses, leaveParamDetails, _leaves, null);
foreach (EmpLeaveStatus eSts in oCurrYearBalance)
oAllEmpsCurrYearBalance.Add(eSts);
}
catch (Exception e)
{
//throw;
}
}
}
catch (Exception exp)
{
throw new Exception("Please try later. work in progress");
}
return oAllEmpsCurrYearBalance;
}
public List<LeaveDetailsMonthWiseDTO> GetMonthWiseLeaveRegister(string empIds, int leaveYearId, int leaveId, EnumLeaveStatus leaveStatus, DateTime leaveFromMonth, DateTime leaveToMonth)
{
List<LeaveDetailsMonthWiseDTO> leaveDetailsMonthWiseDTOs = new List<LeaveDetailsMonthWiseDTO>();
List<LeaveEntry> LeaveEntrys = new LeaveEntryService().Get(empIds, leaveYearId, leaveId, leaveStatus, leaveFromMonth, leaveToMonth);
List<Designation> designations = new DesignationService().GetAllDesignation(1, EnumStatus.Active, "", "");
List<Employee> employees = new EmployeeService().GetAllEmps();
Leave leave = null;
List<Leave> leaves = new LeaveService().GetAll();
foreach (LeaveEntry item in LeaveEntrys)
{
Employee employee = employees.Where(x => x.ID == item.EmpID).FirstOrDefault();
if (employee != null)
{
Designation designation = designations.Where(x => x.ID == employee.DesignationID).FirstOrDefault();
leave = leaves.FirstOrDefault(y => y.ID == item.LeaveID);
LeaveDetailsMonthWiseDTO leaveDetailsMonthWiseDTO = new LeaveDetailsMonthWiseDTO();
leaveDetailsMonthWiseDTO.EmpNo = employee.EmployeeNo;
leaveDetailsMonthWiseDTO.Name = employee.Name;
leaveDetailsMonthWiseDTO.JoiningDate = employee.JoiningDate.ToString("dd MMM yyyy");
leaveDetailsMonthWiseDTO.Designation = designation != null ? designation.Name : "";
//leaveDetailsMonthWiseDTO.Department = string.Empty;
//leaveDetailsMonthWiseDTO.CostCenter = string.Empty;
leaveDetailsMonthWiseDTO.LeaveName = leave != null ? leave.Description : string.Empty;
leaveDetailsMonthWiseDTO.FromDate = item.AppliedFromDate.ToString("dd MMM yyyy");
leaveDetailsMonthWiseDTO.ToDate = item.AppliedToDate.ToString("dd MMM yyyy");
leaveDetailsMonthWiseDTO.Consumed = item.AppliedTotalDays;
leaveDetailsMonthWiseDTO.isLFA = item.isLFA;
leaveDetailsMonthWiseDTO.isHalfDay = item.IsHalfday;
leaveDetailsMonthWiseDTO.Remarks = item.Remarks;
leaveDetailsMonthWiseDTOs.Add(leaveDetailsMonthWiseDTO);
}
}
return leaveDetailsMonthWiseDTOs;
}
public int Save(LeaveProcess oLeaveProcess)
{
LeaveYearService oLYearService = new LeaveYearService();
LeaveProcessService oEmpService = new LeaveProcessService();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveProcessDA.DeleteByProcessYearANDPayrollType(tc, oLeaveProcess.LeaveYearID, oLeaveProcess.PayrollTypeID);
int id = tc.GenerateID("LeaveProcess", "ProcessID");
base.SetObjectID(oLeaveProcess, id);
LeaveProcessDA.Insert(tc, oLeaveProcess);
oEmpService.SaveAtProcessing(false, oLeaveProcess.ID, oLeaveProcess.EmpLeaveStatuss, tc);
oLeaveProcess.LeaveYear.IsCurrent = false;
oLeaveProcess.LeaveYear.IsEnded = true;
oLYearService.UpdateCurrYearStatus(tc, oLeaveProcess.LeaveYear);
oLYearService.UpdateYearEndValue(tc, oLeaveProcess.LeaveYear);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oLeaveProcess.ID;
}
public int SaveLeaveProcess(LeaveProcess oLeaveProcess)
{
LeaveYearService oLYearService = new LeaveYearService();
LeaveProcessService oEmpService = new LeaveProcessService();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveProcessDA.DeleteByProcessYearANDPayrollType(tc, oLeaveProcess.LeaveYearID, oLeaveProcess.PayrollTypeID);
int id = tc.GenerateID("LeaveProcess", "ProcessID");
base.SetObjectID(oLeaveProcess, id);
LeaveProcessDA.Insert(tc, oLeaveProcess);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oLeaveProcess.ID;
}
//public void leaveYearEnd(LeaveYear curryear, int payrolltypeid)
//{
// if(this.IsProcessed(curryear.ID, payrolltypeid)==true)
// {
// throw new Exception("Selected Year already process, first Undo the process");
// }
// List<EmpLeaveStatus> empLeaveStatuses = this.GetLeaveBalance(curryear.ID, payrolltypeid, string.Empty);
// if(empLeaveStatuses!=null)
// {
// foreach(EmpLeaveStatus item in empLeaveStatuses)
// {
// if(item.CFDays !=0 || item.EncashDays !=0)
// {
// item.YearEndBalance = item.YearEndBalance - (item.CFDays - item.EncashDays);
// }
// }
// }
// this.SaveStatus(empLeaveStatuses);
// this.UpadteLeaveYearStatus()
//}
public static List<EmpLeaveStatus> CurrentYearEncashStatus(Employee oEmployee, DateTime operationDate,
List<LeaveParameter> oAppLeaveParams, LeaveYear oCurrYear, List<LeaveEntry> oLeaveEntries, EnumLeaveStatus enumLeaveStatus,
List<EmpLeaveStatus> oPrevStatuses, List<LeaveParameterDetail> ParamDetails, List<LeaveException> oExceptions)
{
try
{
DateTime dCurrentDate = oCurrYear.EndDate < operationDate ? oCurrYear.EndDate : operationDate;
DateTime from_Date = DateTime.MinValue;
DateTime to_Date = DateTime.MinValue;
List<EmpLeaveStatus> oCurrYearBalance = new List<EmpLeaveStatus>();
LeaveParameterDetail oDetail = null;
ConfigurationManager oCon = new ConfigurationManager();
//string Params = ConfigurationManager.GetStringValue("leave", "balance", EnumConfigurationType.Logic);
int Params = new SystemConfigarationService().GetconfigIntValue(EnumConfigurationType.Logic, "leave", "balance");
//added by sujon for adding leave encashment
//start
LeaveEncashment leaveEncashment = new LeaveEncashmentService().Get(oCurrYear.ID, oEmployee.ID);
//end
foreach (LeaveParameter oItem in oAppLeaveParams)
{
EmpLeaveStatus oPrevStatus = null;
EmpLeaveStatus oCurrStatus = new EmpLeaveStatus();
oCurrStatus.EmpId = oEmployee.ID;
oCurrStatus.LeaveId = oItem.LeaveId;
oCurrStatus.LeaveYearID = oCurrYear.ID;
//
int empId = oEmployee.ID;
from_Date = oEmployee.JoiningDate > operationDate ? oEmployee.JoiningDate : PayrollGlobalFunctions.FirstDateOfYear(operationDate);
to_Date = dCurrentDate;
List<LeaveEntry> les = new LeaveEntryService().GetByLeaveID(oItem.LeaveId, empId, from_Date, to_Date);
double countDays = 0;
foreach (LeaveEntry le in les)
{
countDays += le.AppliedTotalDays;
}
oCurrStatus.ApplyDays = countDays;
//
if (oPrevStatuses != null)
oPrevStatus = oPrevStatuses.Find(delegate (EmpLeaveStatus el) { return el.LeaveId == oItem.LeaveId && el.EmpId == oEmployee.ID; }); //EmpLeaveStatus.GetPrvYearStatus(oCurrYear.ID, oItem.LeaveId, oEmployee.ID);
LeaveException oLeaveEx = oExceptions.Find(delegate (LeaveException el) { return el.LeaveID == oItem.LeaveId && el.EmployeeID == oEmployee.ID; }); //EmpLeaveStatus.GetPrvYearStatus(oCurrYear.ID, oItem.LeaveId, oEmployee.ID);
oDetail = new LeaveParameterService().GetApplicableForEmployee2(oEmployee, operationDate, ParamDetails, oItem.ID);
if (oLeaveEx != null)
{
oCurrStatus.LeaveAvailed = new LeaveEntryService().GetLeaveExceptionAmtByType(oEmployee.ID, oItem.Leave.ID, oLeaveEx, EnumLeaveStatus.Approved); //10
oCurrStatus.OpeningBalance = oLeaveEx.OpeningBalance;
oCurrStatus.CFDays = oLeaveEx.CFDays;
if (leaveEncashment != null && oItem.LeaveId == leaveEncashment.LeaveID)
oCurrStatus.LeaveAvailed += leaveEncashment.EncashmentDays;
oCurrStatus.YearEndBalance = oCurrStatus.OpeningBalance - oCurrStatus.LeaveAvailed;
}
else
{
if (oDetail != null) oCurrStatus.NormalLeaveDays = 0;
else continue;
//oItem.Details = new List<LeaveParameterDetail>();
//oItem.Details.Add(oDetail);
oCurrStatus.CurrentYearBalance = 0;
oCurrStatus.CurrentYearOpening = 0;
#region calculate the opening balance new
if (oPrevStatus == null)
oCurrStatus.CurrentYearOpening = 0;
else
{
oCurrStatus.CurrentYearOpening = oPrevStatus.YearEndBalance;
oCurrStatus.CFDays = oPrevStatus.YearEndBalance;
}
#endregion
#region calculate the current year balance
if (oItem.ApplicableFor == LeaveApplicableType.Confirmed)
{
if (oEmployee.IsConfirmed == true)
{
oCurrStatus.CurrentYearBalance = Math.Round(new LeaveProcessService().GetCurrentYearDays(oItem , oEmployee, dCurrentDate, oPrevStatus, oDetail, oCurrYear), oItem.Leave.BalanceRoundofDigit);
}
else oCurrStatus.CurrentYearBalance = 0;
}
else
{
oCurrStatus.CurrentYearBalance = Math.Round(new LeaveProcessService().GetCurrentYearDays(oItem , oEmployee, dCurrentDate, oPrevStatus, oDetail, oCurrYear), oItem.Leave.BalanceRoundofDigit);
}
#endregion
oCurrStatus.OpeningBalance = oCurrStatus.CurrentYearBalance + oCurrStatus.CurrentYearOpening;
#region calculate leave availed in current year
if (oLeaveEntries == null)
oCurrStatus.LeaveAvailed = 0;
else
{
var itemsInCart = from o in oLeaveEntries
where o.EmpID == oEmployee.ID &&
o.LeaveID == oItem.LeaveId &&
(o.ApprovedFromDate.Date >= from_Date.Date && o.ApprovedToDate.Date <= to_Date.Date) &&
(o.LeaveStatus == enumLeaveStatus || o.LeaveStatus == EnumLeaveStatus.Cancel_Request)
select new { o.ApprovedTotalDays };
var sum = itemsInCart.ToList().Select(c => c.ApprovedTotalDays).Sum();
oCurrStatus.LeaveAvailed = Convert.ToDouble(sum);
}
#endregion calculate leave availed in current year
#region Calculate Year-End Balance
oCurrStatus.YearEndBalance = 0;
if (oItem.MaxAccumulatedDays > 0)
{
oCurrStatus.OpeningBalance = oCurrStatus.OpeningBalance > oItem.MaxAccumulatedDays ? oItem.MaxAccumulatedDays : oCurrStatus.OpeningBalance;
oCurrStatus.OpeningBalance = Math.Round(oCurrStatus.OpeningBalance, oItem.Leave.BalanceRoundofDigit);
}
// Added By Hassan 16 Jul 2016
oCurrStatus.CFDays = oCurrStatus.OpeningBalance != oCurrStatus.CurrentYearBalance ? oCurrStatus.OpeningBalance - oCurrStatus.CurrentYearBalance : 0;
if (oItem.Leave.IsBalanceCalculationNeeded == false)
{
oCurrStatus.NormalLeaveDays = 0;
oCurrStatus.OpeningBalance = 0;
}
else
{
if (leaveEncashment != null && oItem.LeaveId == leaveEncashment.LeaveID)
oCurrStatus.LeaveAvailed += leaveEncashment.EncashmentDays;
oCurrStatus.YearEndBalance = Math.Round(oCurrStatus.OpeningBalance - (oCurrStatus.ForfitedDays + oCurrStatus.LeaveAvailed), oItem.Leave.BalanceRoundofDigit);
oCurrStatus.YearEndBalance = Math.Round(oCurrStatus.YearEndBalance, oItem.Leave.BalanceRoundofDigit);
}
}
#endregion Calculate Year-End Balance
if (oItem.Leave.IsBalanceCalculationNeeded)
oCurrYearBalance.Add(oCurrStatus);
}
return oCurrYearBalance;
}
catch (Exception exp)
{
//MessageBox.Show(oEmployee.EmployeeNo);
throw new Exception(exp.Message);
}
return new List<EmpLeaveStatus>();
}
}
}