using System; using System.Data; using Ease.Core.Model; using Ease.Core.DataAccess; using Ease.Core; using System.Collections.Generic; using Ease.Core.Utility; using HRM.BO; using System.Linq; using Microsoft.Data.SqlClient; namespace HRM.DA { #region EmployeeUnAuthorizeLeave Service public class EmployeeUnAuthorizeLeaveService : ServiceTemplate, IEmployeeUnAuthorizeLeaveService { #region Private functions and declaration #endregion public EmployeeUnAuthorizeLeaveService() { } private void MapObject(EmployeeUnAuthorizeLeave oEmployeeUnAuthorizeLeave, DataReader oReader) { base.SetObjectID(oEmployeeUnAuthorizeLeave, (oReader.GetInt32("EmpLeaveID", 0))); oEmployeeUnAuthorizeLeave.EmployeeID = oReader.GetInt32("EmployeeID", 0); oEmployeeUnAuthorizeLeave.UnAuthorizeleaveID = oReader.GetInt32("LeaveID", 0); oEmployeeUnAuthorizeLeave.MonthDate = oReader.GetDateTime("MonthDate").Value; oEmployeeUnAuthorizeLeave.LeaveDays = oReader.GetDouble("LEAVEDAY").Value; oEmployeeUnAuthorizeLeave.LeaveMonth = oReader.GetDateTime("LEAVEMONTH").Value; //oEmployeeUnAuthorizeLeave.IsLateAttendanceRelated = oReader.GetBoolean("IsLateAttendanceRelated", false); oEmployeeUnAuthorizeLeave.FromDate = oReader.GetDateTime("FROMDATE").Value; oEmployeeUnAuthorizeLeave.ToDate = oReader.GetDateTime("TODATE").Value; oEmployeeUnAuthorizeLeave.CreatedBy = oReader.GetInt32("CreatedBy", true, 0); oEmployeeUnAuthorizeLeave.CreatedDate = oReader.GetDateTime("CreatedDate", true, DateTime.MinValue); //.HasValue ? oReader.GetDateTime("CreatedDate").Value : DateTime.MinValue; oEmployeeUnAuthorizeLeave.ModifiedBy = oReader.GetInt32("ModifiedBy", 0); oEmployeeUnAuthorizeLeave.ModifiedDate = oReader.GetDateTime("ModifiedDate", true, DateTime.MinValue); //.HasValue ? oReader.GetDateTime("ModifiedDate").Value : (DateTime?)null; oEmployeeUnAuthorizeLeave.Type = (EnumLeaveEntryType)oReader.GetInt32("Type").Value; oEmployeeUnAuthorizeLeave.ReferenceID = oReader.GetInt32("ReferenceID", 0); oEmployeeUnAuthorizeLeave.ParamID = oReader.GetInt32("ParamID", 0); oEmployeeUnAuthorizeLeave.employeeNoView = oReader.GetString("EmployeeNo", true, string.Empty); oEmployeeUnAuthorizeLeave.employeeNameView = oReader.GetString("EmployeeName", true, string.Empty); oEmployeeUnAuthorizeLeave.leaveNameView = oReader.GetString("leaveName", true, string.Empty); this.SetObjectState(oEmployeeUnAuthorizeLeave, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { EmployeeUnAuthorizeLeave oEmployeeUnAuthorizeLeave = new EmployeeUnAuthorizeLeave(); MapObject(oEmployeeUnAuthorizeLeave, oReader); return oEmployeeUnAuthorizeLeave as T; } protected EmployeeUnAuthorizeLeave CreateObject(DataReader oReader) { EmployeeUnAuthorizeLeave oEmployeeUnAuthorizeLeave = new EmployeeUnAuthorizeLeave(); MapObject(oEmployeeUnAuthorizeLeave, oReader); return oEmployeeUnAuthorizeLeave; } #region Service implementation public EmployeeUnAuthorizeLeave Get(int id) { EmployeeUnAuthorizeLeave oEmployeeUnAuthorizeLeave = new EmployeeUnAuthorizeLeave(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(EmployeeUnAuthorizeLeaveDA.Get(tc, id)); if (oreader.Read()) { oEmployeeUnAuthorizeLeave = this.CreateObject(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 oEmployeeUnAuthorizeLeave; } public List Get(EnumLeaveEntryType type, DateTime month) { List employeeUnAuthorizeLeaves = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(EmployeeUnAuthorizeLeaveDA.Get(tc, type, month)); employeeUnAuthorizeLeaves = this.CreateObjects(dr); dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return employeeUnAuthorizeLeaves; } public List Get(DateTime fromDate, DateTime toDate, EnumLeaveEntryType type) { List employeeUnAuthorizeLeaves = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(EmployeeUnAuthorizeLeaveDA.Get(tc, fromDate, toDate, type)); employeeUnAuthorizeLeaves = this.CreateObjects(dr); dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return employeeUnAuthorizeLeaves; } public List Get() { List employeeUnAuthorizeLeaves = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(EmployeeUnAuthorizeLeaveDA.Get(tc)); employeeUnAuthorizeLeaves = this.CreateObjects(dr); dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return employeeUnAuthorizeLeaves; } public List GetByEmployeeAfter(int nEmpID, EnumLeaveEntryType type, DateTime lastmonth) { List employeeUnAuthorizeLeaves = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(EmployeeUnAuthorizeLeaveDA.GetByEmployeeAfter(tc, nEmpID, type, lastmonth)); employeeUnAuthorizeLeaves = this.CreateObjects(dr); dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return employeeUnAuthorizeLeaves; } public List GetByEmployeeID(int nEmpID) { List employeeUnAuthorizeLeaves = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(EmployeeUnAuthorizeLeaveDA.GetByEmployeeID(tc, nEmpID)); employeeUnAuthorizeLeaves = this.CreateObjects(dr); dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return employeeUnAuthorizeLeaves; } public List GetBySalaryMonth(DateTime salaryMonth) { List employeeUnAuthorizeLeaves = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(EmployeeUnAuthorizeLeaveDA.GetBySalaryMonth(tc, salaryMonth)); employeeUnAuthorizeLeaves = this.CreateObjects(dr); dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return employeeUnAuthorizeLeaves; } //public EmployeeUnAuthorizeLeave GetLeaveTypeByEmployee(int nEmpID, DateTime dMonth, EnumLeaveEntryType type) //{ // EmployeeUnAuthorizeLeave employeeUnAuthorizeLeave = null; // TransactionContext tc = null; // try // { // tc = TransactionContext.Begin(); // DataReader oreader = // new DataReader(EmployeeUnAuthorizeLeaveDA.GetLeaveTypeByEmployee(tc, nEmpID, dMonth, type)); // if (oreader.Read()) // { // employeeUnAuthorizeLeave = this.CreateObject(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 employeeUnAuthorizeLeave; //} public List GetByEmployee(int nEmpID, int leaveid, DateTime leaveMonth, EnumLeaveEntryType type) { List items = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(EmployeeUnAuthorizeLeaveDA.GetByEmployee(tc, nEmpID, leaveid, leaveMonth, type)); items = this.CreateObjects(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 items; } public int AdjustedDays(int nID) { int nAjustedDays = 0; TransactionContext tc = null; try { tc = TransactionContext.Begin(); nAjustedDays = EmployeeUnAuthorizeLeaveDA.GetAdjustedDays(tc, nID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return nAjustedDays; } public int AdjustedDays(int nID, DateTime month) { int nAjustedDays = 0; TransactionContext tc = null; try { tc = TransactionContext.Begin(); nAjustedDays = EmployeeUnAuthorizeLeaveDA.GetAdjustedDays(tc, nID, month); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return nAjustedDays; } //public List GetDeductedLeaves(int nEmpID, int nLeaveID, EnumLeaveEntryType type, // DateTime nextPayProcessDate) //{ // List employeeUnAuthorizeLeaves = null; // TransactionContext tc = null; // try // { // tc = TransactionContext.Begin(); // DataReader dr = // new DataReader( // EmployeeUnAuthorizeLeaveDA.GetDeductedLeave(tc, nEmpID, nLeaveID, type, nextPayProcessDate)); // employeeUnAuthorizeLeaves = this.CreateObjects(dr); // dr.Close(); // tc.End(); // } // catch (Exception e) // { // #region Handle Exception // if (tc != null) // tc.HandleError(); // ExceptionLog.Write(e); // throw new ServiceException(e.Message, e); // #endregion // } // return employeeUnAuthorizeLeaves; //} public List Get(int payrollTypeID, DateTime? salaryMonth, int? empid) { List employeeUnAuthorizeLeaves = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(EmployeeUnAuthorizeLeaveDA.Get(tc, payrollTypeID, empid, salaryMonth)); employeeUnAuthorizeLeaves = this.CreateObjects(dr); dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return employeeUnAuthorizeLeaves; } public List GetByEmployee(int nEmpID, DateTime dMonth, EnumLeaveEntryType type, int payrolltypeid) { List employeeUnAuthorizeLeaves = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(EmployeeUnAuthorizeLeaveDA.GetByEmployee(tc, nEmpID, dMonth, type, payrolltypeid)); employeeUnAuthorizeLeaves = this.CreateObjects(dr); dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return employeeUnAuthorizeLeaves; } //public List GetByEmployeeAfter(int nEmpID, int nLeaveID, EnumLeaveEntryType type, // DateTime LastPayProcessMonth) //{ // List employeeUnAuthorizeLeaves = null; // TransactionContext tc = null; // try // { // tc = TransactionContext.Begin(); // DataReader dr = // new DataReader(EmployeeUnAuthorizeLeaveDA.GetByEmployeeAfter(tc, nEmpID, nLeaveID, type, // LastPayProcessMonth)); // employeeUnAuthorizeLeaves = this.CreateObjects(dr); // dr.Close(); // tc.End(); // } // catch (Exception e) // { // #region Handle Exception // if (tc != null) // tc.HandleError(); // ExceptionLog.Write(e); // throw new ServiceException(e.Message, e); // #endregion // } // return employeeUnAuthorizeLeaves; //} public int Save(EmployeeUnAuthorizeLeave oEmployeeUnAuthorizeLeave) { if(oEmployeeUnAuthorizeLeave.Type == EnumLeaveEntryType.PaidLeave) { oEmployeeUnAuthorizeLeave.LeaveMonth = GlobalFunctions.LastDateOfMonth(oEmployeeUnAuthorizeLeave.LeaveMonth); List items = this.GetByEmployee(oEmployeeUnAuthorizeLeave.EmployeeID, oEmployeeUnAuthorizeLeave.UnAuthorizeleaveID, oEmployeeUnAuthorizeLeave.LeaveMonth, EnumLeaveEntryType.Normal); int sumLeave = Convert.ToInt32( items.Sum(x => x.LeaveDays)); if (sumLeave ==0) { throw new Exception("There was no LWP on that month."); } if (sumLeave < oEmployeeUnAuthorizeLeave.LeaveDays) { throw new Exception("Adjust-Days is greater to leave-days of that perticular month"); } } TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oEmployeeUnAuthorizeLeave.IsNew) { int id = tc.GenerateID("MONTHLYLEAVEENTRY", "EmpLeaveID"); base.SetObjectID(oEmployeeUnAuthorizeLeave, (id)); EmployeeUnAuthorizeLeaveDA.Insert(tc, oEmployeeUnAuthorizeLeave); } else { EmployeeUnAuthorizeLeaveDA.Update(tc, oEmployeeUnAuthorizeLeave); } tc.End(); return oEmployeeUnAuthorizeLeave.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(TransactionContext tc, EmployeeUnAuthorizeLeave oEmployeeUnAuthorizeLeave) { try { int id = tc.GenerateID("MONTHLYLEAVEENTRY", "EmpLeaveID"); base.SetObjectID(oEmployeeUnAuthorizeLeave, (id)); oEmployeeUnAuthorizeLeave.CreatedBy = oEmployeeUnAuthorizeLeave.CreatedBy; oEmployeeUnAuthorizeLeave.CreatedDate = DateTime.Today; EmployeeUnAuthorizeLeaveDA.Insert(tc, oEmployeeUnAuthorizeLeave); } 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); EmployeeUnAuthorizeLeaveDA.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(List empLeaves) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); foreach (EmployeeUnAuthorizeLeave item in empLeaves) { EmployeeUnAuthorizeLeaveDA.Delete(tc, item.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 SaveAuthorizeLeave(int employeeId, DateTime month, List empLeaves) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); EmployeeUnAuthorizeLeaveDA.Delete(tc, month, employeeId); foreach (EmployeeUnAuthorizeLeave leave in empLeaves) { leave.Type = EnumLeaveEntryType.PaidLeave; leave.EmployeeID = employeeId; leave.MonthDate = month; this.Save(tc, leave); } 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 bulkSave(TransactionContext tc, List empLeaves) { try { int id = tc.GenerateID("MONTHLYLEAVEENTRY", "EMPLEAVEID"); DataTable oitem = new DataTable("EMPOVERTIME"); oitem.Columns.Add(new DataColumn("EMPLEAVEID", typeof(int))); oitem.Columns.Add(new DataColumn("EMPLOYEEID", typeof(int))); oitem.Columns.Add(new DataColumn("LEAVEID", typeof(int))); oitem.Columns.Add(new DataColumn("MONTHDATE", typeof(DateTime))); // NULL oitem.Columns.Add(new DataColumn("LEAVEDAY", typeof(double))); // NULL oitem.Columns.Add(new DataColumn("CREATEDBY", typeof(int))); oitem.Columns.Add(new DataColumn("LEAVEMONTH", typeof(DateTime))); oitem.Columns.Add(new DataColumn("FROMDATE", typeof(DateTime))); oitem.Columns.Add(new DataColumn("TODATE", typeof(DateTime))); oitem.Columns.Add(new DataColumn("CREATIONDATE", typeof(DateTime))); // NULL oitem.Columns.Add(new DataColumn("MODIFIEDBY", typeof(int))); // NULL, oitem.Columns.Add(new DataColumn("MODIFIEDDATE", typeof(DateTime))); // NULL, oitem.Columns.Add(new DataColumn("TYPE", typeof(int))); // NULL, oitem.Columns.Add(new DataColumn("REFERENCEID", typeof(int))); // NULL, oitem.Columns.Add(new DataColumn("PARAMID", typeof(int))); // NULL, oitem.Columns.Add(new DataColumn("ISLATEATTENDANCERELATED", typeof(int))); oitem.Columns.Add(new DataColumn("PAYROLLTYPEID", typeof(int))); // NULL, foreach (EmployeeUnAuthorizeLeave item in empLeaves) { id = id + 1; item.ID = id; oitem.Rows.Add( id, item.EmployeeID, item.UnAuthorizeleaveID, item.MonthDate, item.LeaveDays, item.CreatedBy, item.LeaveMonth, item.FromDate, item.ToDate, item.CreatedDate, item.ModifiedBy, item.ModifiedDate, item.Type, item.ReferenceID, item.ParamID, item.IsLateAttendanceRelated, item.PayrollTypeID ); } using (SqlBulkCopy bulkCopy = new SqlBulkCopy((SqlConnection)tc.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)tc.Transaction)) { bulkCopy.BulkCopyTimeout = 6000; // in seconds bulkCopy.ColumnMappings.Add("EMPLEAVEID", "EMPLEAVEID"); bulkCopy.ColumnMappings.Add("EMPLOYEEID", "EMPLOYEEID"); bulkCopy.ColumnMappings.Add("LEAVEID", "LEAVEID"); bulkCopy.ColumnMappings.Add("MONTHDATE", "MONTHDATE"); bulkCopy.ColumnMappings.Add("LEAVEDAY", "LEAVEDAY"); bulkCopy.ColumnMappings.Add("CREATEDBY", "CREATEDBY"); bulkCopy.ColumnMappings.Add("LEAVEMONTH", "LEAVEMONTH"); bulkCopy.ColumnMappings.Add("FROMDATE", "FROMDATE"); bulkCopy.ColumnMappings.Add("TODATE", "TODATE"); bulkCopy.ColumnMappings.Add("CREATIONDATE", "CREATIONDATE"); bulkCopy.ColumnMappings.Add("MODIFIEDBY", "MODIFIEDBY"); bulkCopy.ColumnMappings.Add("MODIFIEDDATE", "MODIFIEDDATE"); bulkCopy.ColumnMappings.Add("TYPE", "TYPE"); bulkCopy.ColumnMappings.Add("REFERENCEID", "REFERENCEID"); bulkCopy.ColumnMappings.Add("PARAMID", "PARAMID"); bulkCopy.ColumnMappings.Add("ISLATEATTENDANCERELATED", "ISLATEATTENDANCERELATED"); bulkCopy.ColumnMappings.Add("PAYROLLTYPEID", "PAYROLLTYPEID"); bulkCopy.DestinationTableName = "MONTHLYLEAVEENTRY"; bulkCopy.WriteToServer(oitem); } } 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 empLeaves, DateTime NextPayprocessDate) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); EmployeeUnAuthorizeLeaveDA.Delete(tc, NextPayprocessDate); foreach (EmployeeUnAuthorizeLeave leave in empLeaves) { this.Save(tc, leave); } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public DataSet GetUnAuthorizeLeave(DateTime date) { DataSet unAuthoLeaves = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); unAuthoLeaves = EmployeeUnAuthorizeLeaveDA.GetUnAuthorizeLeave(tc, date); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return unAuthoLeaves; } public void Save(List empLeaves) { throw new NotImplementedException(); } public List GetByEmployee(int nEmpID, EnumLeaveEntryType type) { throw new NotImplementedException(); } public List GetByEmployee(int nEmpID, int nLeaveID, EnumLeaveEntryType type) { throw new NotImplementedException(); } #endregion } #endregion }