EchoTex_Payroll/HRM.DA/Service/Attendance/HolidayCalendarService.cs

634 lines
21 KiB
C#
Raw Normal View History

2024-10-14 10:01:49 +06:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using HRM.BO;
namespace HRM.DA
{
#region HolidayCalendar Service
[Serializable]
public class HolidayCalendarService : ServiceTemplate, IHolidayCalendarService
{
public HolidayCalendarService()
{
}
private void MapObject(HolidayCalendar oHolidayCalendar, DataReader oReader)
{
base.SetObjectID(oHolidayCalendar, (oReader.GetInt32("HolidayCalendarID").Value));
oHolidayCalendar.Type = (EnumHolidayType)oReader.GetInt32("Type").Value;
oHolidayCalendar.Description = oReader.GetString("Description");
oHolidayCalendar.HolidayDate = oReader.GetDateTime("HolidayDate").Value;
oHolidayCalendar.LocationID = oReader.GetInt32("LocationID");
oHolidayCalendar.WeeklyHolidayType = oReader.GetInt32("WeeklyHolidayType").GetValueOrDefault();
oHolidayCalendar.CreatedBy = oReader.GetInt32("CreatedBy", 0);
oHolidayCalendar.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
oHolidayCalendar.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
oHolidayCalendar.ModifiedDate = oReader.GetDateTime("ModifiedDate");
this.SetObjectState(oHolidayCalendar, Ease.Core.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
HolidayCalendar oHolidayCalendar = new HolidayCalendar();
MapObject(oHolidayCalendar, oReader);
return oHolidayCalendar as T;
}
protected HolidayCalendar CreateObject(DataReader oReader)
{
HolidayCalendar oHolidayCalendar = new HolidayCalendar();
MapObject(oHolidayCalendar, oReader);
return oHolidayCalendar;
}
#region Service implementation
public HolidayCalendar GetByDate(DateTime dDate)
{
HolidayCalendar oHolidayCalendar = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(HolidayCalendarDA.GetByDate(tc, dDate));
if (oreader.Read())
{
oHolidayCalendar = this.CreateObject<HolidayCalendar>(oreader);
}
oreader.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 oHolidayCalendar;
}
public HolidayCalendar Get(int id)
{
HolidayCalendar oHolidayCalendar = new HolidayCalendar();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(HolidayCalendarDA.Get(tc, id));
if (oreader.Read())
{
oHolidayCalendar = this.CreateObject<HolidayCalendar>(oreader);
}
oreader.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 oHolidayCalendar;
}
public List<HolidayCalendar> GetHoliDays(int nLocID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(HolidayCalendarDA.GetHoliDays(tc, nLocID));
var holidayCalendars = this.CreateObjects<HolidayCalendar>(dr);
dr.Close();
tc.End();
return holidayCalendars;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public List<HolidayCalendar> GetNationalHolidays()
{
List<HolidayCalendar> holidayCalendars = new List<HolidayCalendar>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(HolidayCalendarDA.GetNationalHolidays(tc));
holidayCalendars = this.CreateObjects<HolidayCalendar>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return holidayCalendars;
}
public List<HolidayCalendar> Get()
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(HolidayCalendarDA.Get(tc));
var holidayCalendars = this.CreateObjects<HolidayCalendar>(dr);
dr.Close();
tc.End();
return holidayCalendars;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public List<HolidayCalendar> GetWeeklyHoliDay()
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(HolidayCalendarDA.GetWeeklyHoliDay(tc));
var holidayCalendars = this.CreateObjects<HolidayCalendar>(dr);
dr.Close();
tc.End();
return holidayCalendars;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public List<HolidayCalendar> GetHoliDayByLocation(int nLocID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(HolidayCalendarDA.GetHoliDayByLocation(tc, nLocID));
var holidayCalendars = this.CreateObjects<HolidayCalendar>(dr);
dr.Close();
tc.End();
return holidayCalendars;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public List<HolidayCalendar> GetWeeklyAndLocHoliday(int? nLocID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(HolidayCalendarDA.GetWeeklyAndLocHoliday(tc, nLocID));
var holidayCalendars = this.CreateObjects<HolidayCalendar>(dr);
dr.Close();
tc.End();
return holidayCalendars;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public int GetTotalMonthlyHolidays(DateTime firstDateOfMonth, DateTime lastDateOfMonth)
{
int totalMonthlyHolidays = 0;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
totalMonthlyHolidays =
HolidayCalendarDA.GetTotalMonthlyHolidays(tc, firstDateOfMonth, lastDateOfMonth);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return totalMonthlyHolidays;
}
public int GetTotalMonthlyHolidays(int loactionNumber, DateTime firstDateOfMonth, DateTime lastDateOfMonth)
{
int totalMonthlyHolidays = 0;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
totalMonthlyHolidays =
HolidayCalendarDA.GetTotalMonthlyHolidays(tc, loactionNumber, firstDateOfMonth, lastDateOfMonth);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return totalMonthlyHolidays;
}
public double GetNoofHoliday()
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
double amount = HolidayCalendarDA.GetNoofHoliday(tc);
tc.End();
return amount;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed" + e.Message, e);
#endregion
}
}
public List<HolidayCalendar> GetbyMonthRange(int nLocationID, DateTime dStartDate, DateTime dEndDate)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr =
new DataReader(HolidayCalendarDA.GetbyMonthRange(tc, nLocationID, dStartDate, dEndDate));
var holidayCalendars = this.CreateObjects<HolidayCalendar>(dr);
dr.Close();
tc.End();
return holidayCalendars;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public List<HolidayCalendar> GetbyDateRange(DateTime dStartDate, DateTime dEndDate)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(HolidayCalendarDA.GetbyDateRange(tc, dStartDate, dEndDate));
List<HolidayCalendar> holidayCalendars = this.CreateObjects<HolidayCalendar>(dr);
//var holidayCalendars = this.CreateObjects<HolidayCalendar>(dr);
dr.Close();
tc.End();
return holidayCalendars;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public int Save(HolidayCalendar oHolidayCalendar)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oHolidayCalendar.IsNew)
{
int id = tc.GenerateID("HolidayCalendar", "HolidayCalendarID");
base.SetObjectID(oHolidayCalendar, (id));
HolidayCalendarDA.Insert(tc, oHolidayCalendar);
}
else
{
HolidayCalendarDA.Update(tc, oHolidayCalendar);
}
tc.End();
return oHolidayCalendar.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void Save(List<HolidayCalendar> oHolidayCalendars)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
int id = tc.GenerateID("HolidayCalendar", "HolidayCalendarID");
foreach(HolidayCalendar item in oHolidayCalendars)
{
if (item.IsNew)
{
id = id + 1;
base.SetObjectID(item, (id));
HolidayCalendarDA.Insert(tc, item);
}
else
{
HolidayCalendarDA.Update(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 Process(int payrollTypeID)
{
List<Location> _oLocs = new LocationService().GetAllLocation(payrollTypeID, EnumStatus.Regardless, string.Empty, string.Empty);
List<WeeklyHoliday> _oHolidays = new WeeklyHolidayService().Get(EnumStatus.Regardless);
List<AttnNationalHoliday> _oNationalHolidays = new AttnNationalHolidayService().Get(EnumStatus.Regardless, payrollTypeID);
List<HolidayCalendar> _oHolidayCalendars = new List<HolidayCalendar>();
if (_oHolidays.Count > 0)
RefreshHolidays(_oHolidays, _oHolidayCalendars, _oLocs);
if (_oNationalHolidays.Count > 0)
RefreshNationalHoliday(_oNationalHolidays, _oHolidayCalendars, _oLocs);
if (_oHolidayCalendars != null)
{
try
{
this.DeleteAll(DateTime.Today.Year);
this.Save(_oHolidayCalendars);
}
catch (Exception exp)
{
throw new Exception(exp.Message);
}
}
}
private void RefreshNationalHoliday(List<AttnNationalHoliday> _oNationalHolidays, List<HolidayCalendar> _oHolidayCalendars, List<Location> _oLocs)
{
DateTime dtNH = Ease.Core.Utility.Global.DateFunctions.FirstDateOfYear(DateTime.Today);
DateTime dtNH1 = Ease.Core.Utility.Global.DateFunctions.LastDateOfYear(DateTime.Today);
int day = (dtNH1 - dtNH).Days + 1;
for (int i = 1; i <= day; i++)
{
if (dtNH > dtNH1)
break;
else
{
List<AttnNationalHoliday> oNHolis = null;
oNHolis = _oNationalHolidays.Where(oH => dtNH >= oH.FromDate && dtNH <= oH.ToDate).ToList();
if (oNHolis != null && oNHolis.Count() > 0)
{
foreach (AttnNationalHoliday oNHoli in oNHolis)
{
HolidayCalendar _oHolidayCalendar = new HolidayCalendar();
if (oNHoli.LocationID != null && oNHoli.LocationID !=0)
{
Location oLoc = _oLocs.FirstOrDefault(x => x.ID == oNHoli.LocationID);
_oHolidayCalendar.LocationID = oLoc.ID;
}
_oHolidayCalendar.Type = EnumHolidayType.National;
_oHolidayCalendar.Description = oNHoli.Description;
_oHolidayCalendar.HolidayDate = dtNH;
_oHolidayCalendars.Add(_oHolidayCalendar);
}
}
}
dtNH = dtNH.AddDays(1);
}
}
private void RefreshHolidays(List<WeeklyHoliday> _oHolidays, List<HolidayCalendar> _oHolidayCalendars, List<Location> _oLocs)
{
if (_oHolidays != null && _oHolidays.Count > 0)
{
foreach (WeeklyHoliday hc in _oHolidays)
{
DateTime date = Ease.Core.Utility.Global.DateFunctions.FirstDateOfYear(DateTime.Today);
DateTime lastDateOfYear = Ease.Core.Utility.Global.DateFunctions.LastDateOfYear(DateTime.Today);
for (int i = 1; i <= 365; i++)
{
if (date > lastDateOfYear)
break;
if ((((HolidayDayOfWeek)hc.DateType).ToString() == date.ToString("dddd")))
{
HolidayCalendar _oHolidayCalendar = new HolidayCalendar();
_oHolidayCalendar.Type = EnumHolidayType.Weekend;
if (hc.LocationID != null && hc.LocationID != 0)
{
Location oLoc = _oLocs.FirstOrDefault(x => x.ID == hc.LocationID);
_oHolidayCalendar.LocationID = oLoc.ID;
}
_oHolidayCalendar.Description = "Weekly Holiday";
_oHolidayCalendar.HolidayDate = date;
_oHolidayCalendar.WeeklyHolidayType = (int)EnumWeeklyHolidayType.All;
_oHolidayCalendars.Add(_oHolidayCalendar);
}
date = date.AddDays(1);
}
}
}
}
public void Delete(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
HolidayCalendarDA.Delete(tc, 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 DeleteAll()
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
HolidayCalendarDA.DeleteAll(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 DeleteAll(int nYear)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
HolidayCalendarDA.DeleteAll(tc, nYear);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public List<HolidayCalendar> GetWeeklyAndLocHoliday(int nLocID)
{
throw new NotImplementedException();
}
#endregion
}
#endregion
}