CEL_Payroll/Payroll.BO/Leave/LeaveParameter.cs
2024-09-17 14:30:13 +06:00

703 lines
26 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ease.CoreV35;
using Ease.CoreV35.Model;
using Ease.CoreV35.Caching;
using System.Data.Linq.Mapping;
using System.Data;
namespace Payroll.BO
{
#region LeaveParameter
[Serializable]
public class LeaveParameter : AuditTrailBase, ISetupManager
{
#region Cache Store
private static Cache _cache = new Cache(typeof(LeaveParameter));
#endregion
#region Constructor
public LeaveParameter()
{
_nLeaveId = 0;
_nMaxAccumulatedDays = 0.0;
_bIsActive = true;
_nForfitedMonth = 0;
_nAllowAdvance = false;
_nIgnoreHoliday = true;
_CalculationType = EnumLeaveCalculationType.Daily;
_nApplicableFor = 0;
_manager = null;
_bIsForfited = false;
_nApplicableFor = LeaveApplicableType.Regardless;
_PayrollTypeID = 0;
_nIsMonthlyBalance = false;
}
#endregion
#region Properties
private int _PayrollTypeID;
public int PayrollTypeID
{
get { return _PayrollTypeID; }
set { _PayrollTypeID = value; }
}
private Leave _oLeave;
public Leave Leave
{
get
{
if (_oLeave == null)
{
_oLeave = new Leave();
_oLeave = (Leave)_oLeave.Get(ID.FromInteger(_nLeaveId));
}
return _oLeave;
}
}
#region Leave Parameter Details
private ObjectsTemplate<LeaveParameterDetail> _oDetails;
public ObjectsTemplate<LeaveParameterDetail> Details
{
get
{
if (_oDetails == null || _oDetails.Count == 0)
{
_oDetails = new ObjectsTemplate<LeaveParameterDetail>();
if (!this.IsNew)
_oDetails = LeaveParameter.Service.GetDetails(this.ID);
}
return _oDetails;
}
set
{
_oDetails = value;
}
}
private ObjectsTemplate<LeaveParameterDetail> _oDetails2;
public ObjectsTemplate<LeaveParameterDetail> Details2
{
get
{
if (_oDetails2 == null || _oDetails2.Count == 0)
{
_oDetails2 = new ObjectsTemplate<LeaveParameterDetail>();
//if (!this.IsNew)
// _oDetails2 = LeaveParameter.Service.GetDetails(this.ID);
}
return _oDetails2;
}
set
{
_oDetails2 = value;
}
}
public LeaveParameterDetail GetDetail(int paramID)
{
LeaveParameterDetail oLeaveDetail = null;
#region Cache Header
oLeaveDetail = (LeaveParameterDetail)_cache["Get", paramID];
if (oLeaveDetail != null)
return oLeaveDetail;
#endregion
oLeaveDetail = LeaveParameter.Service.GetDetail(paramID);
#region Cache Footer
_cache.Add(oLeaveDetail, "Get", paramID);
#endregion
return oLeaveDetail;
}
#endregion
#region LeaveId
private int _nLeaveId;
public int LeaveId
{
get { return _nLeaveId; }
set { _nLeaveId = value; }
}
#endregion
#region MaxAccumulatedDays
private double _nMaxAccumulatedDays;
public double MaxAccumulatedDays
{
get { return _nMaxAccumulatedDays; }
set { _nMaxAccumulatedDays = value; }
}
#endregion
#region Included SetupManager
private SetupManager _manager;
public SetupManager Manager
{
get
{
return _manager;
}
set { _manager = value; }
}
#endregion
#region IsActive
private bool _bIsActive;
public bool IsActive
{
get { return _bIsActive; }
set { _bIsActive = value; }
}
#endregion
#region ForfitedMonth
private int _nForfitedMonth;
public int ForfitedMonth
{
get { return _nForfitedMonth; }
set { _nForfitedMonth = value; }
}
#endregion
#region IsForfited
private bool _bIsForfited;
public bool IsForfited
{
get { return _bIsForfited; }
set { _bIsForfited = value; }
}
#endregion
#region AllowAdvance
private bool _nAllowAdvance;
public bool AllowAdvance
{
get { return _nAllowAdvance; }
set { _nAllowAdvance = value; }
}
#endregion
#region IgnoreHoliday
private bool _nIgnoreHoliday;
public bool IgnoreHoliday
{
get { return _nIgnoreHoliday; }
set { _nIgnoreHoliday = value; }
}
#endregion
#region _CalculationType
private EnumLeaveCalculationType _CalculationType;
public EnumLeaveCalculationType CalculationType
{
get { return _CalculationType; }
set { _CalculationType = value; }
}
#endregion
#region MonthlyBalance
private bool _nIsMonthlyBalance;
public bool IsMonthlyBalance
{
get { return _nIsMonthlyBalance; }
set { _nIsMonthlyBalance = value; }
}
#endregion
#region LeaveApplicableType
private LeaveApplicableType _nApplicableFor;
public LeaveApplicableType ApplicableFor
{
get { return _nApplicableFor; }
set { _nApplicableFor = value; }
}
#endregion
#endregion
public ObjectsTemplate<LeaveParameterDetail> GetAllDetails()
{
ObjectsTemplate<LeaveParameterDetail> Details = new ObjectsTemplate<LeaveParameterDetail>();
Details = LeaveParameter.Service.GetDetails();
return Details;
}
public LeaveParameter Get(ID nLeaveParamId)
{
LeaveParameter oLeaveParameter = null;
#region Cache Header
oLeaveParameter = (LeaveParameter)_cache["Get", nLeaveParamId];
if (oLeaveParameter != null)
return oLeaveParameter;
#endregion
oLeaveParameter = LeaveParameter.Service.Get(nLeaveParamId);
#region Cache Footer
_cache.Add(oLeaveParameter, "Get", nLeaveParamId);
#endregion
return oLeaveParameter;
}
public LeaveParameterDetail GetApplicableForEmployee(Employee oEmp, DateTime operationDate, List<LeaveParameterDetail> ParamDetails)
{
//#### change by Current date function
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
List<LeaveParameterDetail> oDetails = new List<LeaveParameterDetail>();
oDetails = ParamDetails.FindAll(delegate(LeaveParameterDetail lpd) { return lpd.LeaveParamId == this.ID.Integer; });
year = (int)y;
int maxYear = -1;
LeaveParameterDetail dtl = null;
//foreach (LeaveParameterDetail detail in this.Details)
foreach (LeaveParameterDetail detail in oDetails)
{
if (detail.Year <= year && detail.Year >= maxYear)
{
maxYear = detail.Year;
dtl = detail;
}
}
return dtl;
}
public LeaveParameterDetail GetApplicableForEmployee2(Employee oEmp, DateTime operationDate,List<LeaveParameterDetail> oAllDetails,ID nParamID)
{
//#### change by Current date function
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;
List<LeaveParameterDetail> oDetails = oAllDetails.FindAll(delegate(LeaveParameterDetail lpd) { return lpd.LeaveParamId ==Convert.ToInt32(nParamID.Integer.ToString()); });
foreach (LeaveParameterDetail detail in oDetails)
{
if (detail.Year <= year && detail.Year >= maxYear)
{
maxYear = detail.Year;
dtl = detail;
}
}
return dtl;
}
public LeaveParameter GetByLeaveID(int leaveID)
{
LeaveParameter oLeaveParameter = null;
oLeaveParameter = LeaveParameter.Service.GetByLeaveID(leaveID);
return oLeaveParameter;
}
public ID Save()
{
this.SetAuditTrailProperties();
return LeaveParameter.Service.Save(this);
}
public void Delete()
{
LeaveParameter.Service.Delete(this.ID);
}
public static ObjectsTemplate<Leave> GetApplicableLeaves(Employee oEmp)
{
ObjectsTemplate<LeaveParameter> oLeaveParams = Get(true);
if (oLeaveParams == null) return null;
ObjectsTemplate<LeaveParameter> oAppParams = LeaveParameter.ApplicableParams(oEmp);
if (oAppParams == null) return null;
ObjectsTemplate<Leave> oLeaves = new ObjectsTemplate<Leave>();
foreach (LeaveParameter param in oAppParams)
{
oLeaves.Add(param.Leave);
}
return oLeaves;
}
public static ObjectsTemplate<LeaveParameter> ApplicableParamsForReport(Employee oEmp, List<EnmSetupManagerTranType> setupTypes, ObjectsTemplate<SetupDetail> details, ObjectsTemplate<LeaveParameter> leaveParamss)
{
List<int> leaveParamIds = SetupManager.ApplicableParametersForReport(oEmp, setupTypes, details);
//ObjectsTemplate<LeaveParameter> leaveParamss = LeaveParameter.Get();
ObjectsTemplate<LeaveParameter> leaveParams = new ObjectsTemplate<LeaveParameter>();
foreach (LeaveParameter lparam in leaveParamss)
{
if (lparam.Leave.ApplicableFor != (EnumGender)oEmp.Gender && lparam.Leave.ApplicableFor != EnumGender.Other && lparam.Leave.ApplicableFor != EnumGender.None)
{
continue;
}
bool found = false;
foreach (int id in leaveParamIds)
{
if (id == lparam.ID.Integer)
{
found = true;
break;
}
}
if (found)
{
switch (lparam.ApplicableFor)
{
case LeaveApplicableType.Probetionary:
if (!oEmp.IsConfirmed)
{
leaveParams.Add(lparam);
}
break;
case LeaveApplicableType.Confirmed:
if (oEmp.IsConfirmed)
{
leaveParams.Add(lparam);
}
break;
case LeaveApplicableType.Regardless:
leaveParams.Add(lparam);
break;
default:
break;
}
}
}
return leaveParams;
}
public LeaveParameterDetail GetApplicableForEmployeeForReport(Employee oEmp, DateTime operationDate, List<LeaveParameterDetail> oDetails)
{
//#### change by Current date function
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 oDetails)
{
if (detail.Year <= year && detail.Year >= maxYear)
{
maxYear = detail.Year;
dtl = detail;
}
}
return dtl;
}
public static LeaveParameter GetLeaveParam(Employee oEmp, int nLeaveID)
{
ObjectsTemplate<LeaveParameter> oLeaveParams = LeaveParameter.Get(true);
if (oLeaveParams == null) return null;
ObjectsTemplate<LeaveParameter> oAppParams = ApplicableParams(oEmp);
if (oAppParams == null) return null;
foreach (LeaveParameter param in oAppParams)
{
if (param.LeaveId == nLeaveID)
{
return param;
}
}
return null;
}
public LeaveParameterDetail GetDetailForEmp(ID EmpId)
{
Employee oEmp = Employee.Service.Get(EmpId);
LeaveParameterDetail oretVal = null;
return oretVal;
}
#region Service Factory
internal static ILeaveParameterService Service
{
get { return Services.Factory.CreateService<ILeaveParameterService>(typeof(ILeaveParameterService)); }
}
#endregion
#region Functions
public static ObjectsTemplate<LeaveParameter> ApplicableParams(Employee oEmp)
{
List<int> leaveParamIds = SetupManager.ApplicableParameters(oEmp, EnumParameterSetup.Leave);
ObjectsTemplate<LeaveParameter> leaveParamss = LeaveParameter.Get();
ObjectsTemplate<LeaveParameter> leaveParams = new ObjectsTemplate<LeaveParameter>();
foreach (LeaveParameter lparam in leaveParamss)
{
if (lparam.Leave.ApplicableFor != (EnumGender)oEmp.Gender && lparam.Leave.ApplicableFor != EnumGender.Other && lparam.Leave.ApplicableFor != EnumGender.None)
{
continue;
}
bool found = false;
foreach (int id in leaveParamIds)
{
if (id == lparam.ID.Integer)
{
found = true;
break;
}
}
if (found)
{
switch (lparam.ApplicableFor)
{
case LeaveApplicableType.Probetionary:
if (!oEmp.IsConfirmed)
{
leaveParams.Add(lparam);
}
break;
case LeaveApplicableType.Confirmed:
if (oEmp.IsConfirmed)
{
leaveParams.Add(lparam);
}
break;
case LeaveApplicableType.Regardless:
leaveParams.Add(lparam);
break;
default:
break;
}
}
}
return leaveParams;
}
public LeaveParameterDetail GetApplicableForEmployee(Employee oEmp, LeaveYear lyear)
{
int month = Ease.CoreV35.Utility.Global.DateFunctions.DateDiff("m", oEmp.JoiningDate, SystemInformation.GetServerDate());
int year = month / 12;
int maxYear = -1;
LeaveParameterDetail dtl = null;
foreach (LeaveParameterDetail detail in this.Details)
{
if (detail.Year <= year && detail.Year >= maxYear)
{
maxYear = detail.Year;
dtl = detail;
}
}
if (dtl == null) return null;
month = month - (maxYear * 12);
if (month >= 12) month = Ease.CoreV35.Utility.Global.DateFunctions.DateDiff("m", lyear.StartDate, SystemInformation.GetServerDate());
dtl.MonthPassed = month;
month = month + Ease.CoreV35.Utility.Global.DateFunctions.DateDiff("m", SystemInformation.GetServerDate(), lyear.EndDate);
month = month + 1;
dtl.EligibleMonth = month;
dtl.MaxDays = Math.Round((dtl.MaxDays / 12) * month);
return dtl;
}
public static ObjectsTemplate<LeaveParameter> Get()
{
ObjectsTemplate<LeaveParameter> oLeaveParameters = null;
oLeaveParameters = LeaveParameter.Service.Get();
#region Cache Footer
_cache.Add(oLeaveParameters, "Get");
#endregion
return oLeaveParameters;
}
public static ObjectsTemplate<LeaveParameter> GetByLeaveId(ID leaveId)
{
#region Cache Header
ObjectsTemplate<LeaveParameter> oLeaveParameters = _cache["GetByLeaveId", leaveId] as ObjectsTemplate<LeaveParameter>;
if (oLeaveParameters != null)
return oLeaveParameters;
#endregion
oLeaveParameters = LeaveParameter.Service.GetbyLeaveID(leaveId);
#region Cache Footer
_cache.Add(oLeaveParameters, "GetByLeaveId", leaveId);
#endregion
return oLeaveParameters;
}
public static ObjectsTemplate<Leave> GetApplicableLeave(Employee employee)
{
ObjectsTemplate<LeaveParameter> oLeaveParams = LeaveParameter.Get(true);
if (oLeaveParams == null) return null;
ObjectsTemplate<LeaveParameter> oAppParams = new ObjectsTemplate<LeaveParameter>();
oAppParams = ApplicableParams(employee);
if (oAppParams == null) return null;
ObjectsTemplate<Leave> oLeaves = new ObjectsTemplate<Leave>();
foreach (LeaveParameter param in oAppParams)
{
oLeaves.Add(param.Leave);
}
return oLeaves;
}
public static ObjectsTemplate<LeaveParameter> Get(bool isActive)
{
#region Cache Header
ObjectsTemplate<LeaveParameter> oLeaveParameters = _cache["Get"] as ObjectsTemplate<LeaveParameter>;
if (oLeaveParameters != null)
return oLeaveParameters;
#endregion
oLeaveParameters = LeaveParameter.Service.Get(isActive, SystemInformation.CurrentSysInfo.PayrollTypeID);
#region Cache Footer
_cache.Add(oLeaveParameters, "Get", isActive);
#endregion
return oLeaveParameters;
}
#endregion
#region ISetupManager Members
private ObjectsTemplate<SetupDetail> _setupDetails;
public ObjectsTemplate<SetupDetail> SetupDetails
{
get
{
return _setupDetails;
}
set
{
_setupDetails = value;
}
}
public EmployeeSetupParameter ApplicableParameters(Employee employee)
{
EmployeeSetupParameter empParameter = new EmployeeSetupParameter(EnumParameterSetup.Leave);
empParameter.Employee = employee;
empParameter.ParameterIDs = SetupManager.ApplicableParameters(employee, EnumParameterSetup.Leave);
return empParameter;
}
public static List<EmployeeSetupParameter> GetParameters(ObjectsTemplate<Employee> employees)
{
List<EmployeeSetupParameter> empParametes = new List<EmployeeSetupParameter>();
string Params = ConfigurationManager.GetStringValue("leave", "setupparameter", EnumConfigurationType.Logic);
ConfigReader oreader = new ConfigReader(Params);
DataSet setupDetails = SetupDetail.GetParameters(EnumParameterSetup.Leave, oreader.SQLRelationToEmp, Employee.GetInEmpSQL(employees));
if (setupDetails.Tables[0] == null) return empParametes;
int empId = -1;
EmployeeSetupParameter empprm = null;
ObjectsTemplate<LeaveParameter> allParameters = LeaveParameter.Get();
ObjectsTemplate<LeaveParameter> allLeave = LeaveParameter.Get();
int nparameterID = 0;
DateTime serverDate = SystemInformation.GetServerDate();
foreach (DataRow orow in setupDetails.Tables[0].Rows)
{
if (empId != Convert.ToInt32(orow["EmployeeID"]))
{
empId = Convert.ToInt32(orow["EmployeeID"]);
if (empprm != null && empprm.ParameterIDs.Count > 0) empParametes.Add(empprm);
empprm = new EmployeeSetupParameter(EnumParameterSetup.Leave);
empprm.Employee = employees.GetItem(ID.FromInteger(empId));
nparameterID = Convert.ToInt32(orow["SetupID"]);
}
else nparameterID = Convert.ToInt32(orow["SetupID"]);
List<LeaveParameter> lparameters = allParameters.FindAll(delegate(LeaveParameter param) { return param.LeaveId == nparameterID; });
foreach (LeaveParameter param in lparameters)
{
if (param.ApplicableFor == LeaveApplicableType.Confirmed &&
empprm.Employee.IsConfirmed == false) continue;
if (param.ApplicableFor == LeaveApplicableType.Probetionary &&
empprm.Employee.IsConfirmed == true) continue;
int month = Ease.CoreV35.Utility.Global.DateFunctions.DateDiff("m", empprm.Employee.JoiningDate, serverDate);
int year = (month / 12) + 1;
int maxYear = -1;
bool isValidParameter = false;
foreach (LeaveParameterDetail detail in param.Details)
{
if (detail.Year <= year && detail.Year >= maxYear)
{
maxYear = detail.Year;
empprm.ParameterIDs.Add(nparameterID);
isValidParameter = true;
break;
}
}
}
}
if (empprm != null && empprm.ParameterIDs.Count > 0) empParametes.Add(empprm);
return empParametes;
}
public List<EmployeeSetupParameter> ApplicableParameters(ObjectsTemplate<Employee> employees)
{
ObjectsTemplate<SetupDetail> details = SetupDetail.GetParameters(EnumParameterSetup.Leave);
List<EnmSetupManagerTranType> types = SetupDetail.GetTypes(EnumParameterSetup.Leave);
List<EmployeeSetupParameter> empParametes = new List<EmployeeSetupParameter>();
foreach (Employee emp in employees)
{
EmployeeSetupParameter empparameter = new EmployeeSetupParameter(EnumParameterSetup.Leave);
empparameter.Employee = emp;
empparameter.ParameterIDs = SetupManager.ApplicableParameters(emp, EnumParameterSetup.Leave,
types, details);
empParametes.Add(empparameter);
}
return empParametes;
}
public void FillMe(ID id)
{
LeaveParameter tpm = new LeaveParameter();
tpm = tpm.Get(id);
this.LeaveId = tpm.LeaveId;
this.MaxAccumulatedDays = tpm.MaxAccumulatedDays;
this.IsActive = tpm.IsActive;
this.ForfitedMonth = tpm.ForfitedMonth;
this.IgnoreHoliday = tpm.IgnoreHoliday;
this.CalculationType = tpm.CalculationType;
this.ApplicableFor = tpm.ApplicableFor;
this.IsForfited = tpm.IsForfited;
this.SetID(tpm.ID);
this.SetState(ObjectState.Saved);
}
#endregion
}
#endregion
#region ILeaveParameter Service
public interface ILeaveParameterService
{
LeaveParameter Get(ID leaveParameterId);
ObjectsTemplate<LeaveParameter> Get();
ObjectsTemplate<LeaveParameter> Get(bool isActive, ID PayrolltypeID);
ObjectsTemplate<LeaveParameter> GetbyLeaveID(ID leaveId);
ObjectsTemplate<LeaveParameterDetail> GetDetails(ID leaveParamID);
LeaveParameterDetail GetDetail(int paramID);
LeaveParameter GetByLeaveID(int leaveID);
ID Save(LeaveParameter oLeaveParameter);
ObjectsTemplate<LeaveParameterDetail> GetDetails();
void Delete(ID leaveParamID);
}
#endregion
}