using System; using Payroll.BO; using System.Data; using System.Linq; using Ease.CoreV35.Model; using System.Data.SqlClient; using Ease.CoreV35.DataAccess; using System.Collections.Generic; using Ease.CoreV35.DataAccess.SQL; namespace Payroll.Service { #region EmpLifeCycleDA internal class EmpLifeCycleDA { #region Constructor private EmpLifeCycleDA() { } #endregion #region Insert function internal static void Insert(TransactionContext tc, EmpLifeCycle item) { tc.ExecuteNonQuery("INSERT INTO EmpLifeCycle(EmpLifeCycleID,EffectDate,NodeID,GrossSalary,BasicSalary,GradeID,CategoryID,CompanyID,FunctionID,DesignationID,LocationID,DepartmentID,IsConfirm,pFMemberType,IsDiscontinue,IsContinue,EmployeeID,StatusDetailID,Remarks,Description,Status,CreatedBy, CreationDate, SequenceNo,SalaryMonth,IsCurrentMonthIncluded,CostCenterID,GradeSalaryAssesmentID,EmployeeCCID,Role,DesignationText)" + " VALUES( %n , %D , %n , %n , %n , %n , %n , %n , %n , %n , %n , %n , %b , %n , %b , %b , %n , %n , %s , %s , %n , %n , %D , %n , %D , %b , %n , %n , %n ,%n , %s)", item.ID.Integer, item.EffectDate, DataReader.GetNullValue(item.NodeID, IDType.Integer), item.GrossSalary, item.BasicSalary, DataReader.GetNullValue(item.GradeID, IDType.Integer), DataReader.GetNullValue(item.CategoryID, IDType.Integer), DataReader.GetNullValue(item.CompanyID, IDType.Integer), DataReader.GetNullValue(item.FunctionID, IDType.Integer), DataReader.GetNullValue(item.DesignationID, IDType.Integer), DataReader.GetNullValue(item.LocationID, IDType.Integer), DataReader.GetNullValue(item.DepartmentID, IDType.Integer), item.IsConfirm, item.PFMemberType, item.IsDiscontinue, item.IsContinue, item.EmployeeID.Integer, DataReader.GetNullValue(item.StatusDetailID,IDType.Integer), item.Remarks, item.Description, item.Status, DataReader.GetNullValue(item.CreatedBy,IDType.Integer) ,DataReader.GetNullValue(item.CreatedDate) , item.Sequence, item.SalaryMonth, item.IsCurrentMonthIncluded, DataReader.GetNullValue(item.CostCenterID, IDType.Integer), DataReader.GetNullValue(item.GradeSalaryAssesmentID, IDType.Integer), DataReader.GetNullValue(item.EmployeeCCID, IDType.Integer),(int)item.Role,item.DesignationText); //1 //2 //3 //4 //5 //6 //7 //8 //9 //10 //11 //12 //13 //14 //15 //16 //17 //18 //19 //20 //21 //21 //22 //23 //24 //25 } #endregion #region Update function internal static void Update(TransactionContext tc, EmpLifeCycle item) { //tc.ExecuteNonQuery("UPDATE EmpLifeCycle SET Code=%s, Name=%s, ACCOUNTNOFORMAT=%s, ModifiedBy=%n, ModifiedDate=%d, SequenceNo=%n, Status=%n" + //"WHERE EmpLifeCycleID=%n", item.Code, item.Name, item.Accountingformat, item.ModifiedBy.Integer, item.ModifiedDate, item.Sequence, item.Status,item.ID.Integer); } #endregion #region Get Function internal static IDataReader Get(TransactionContext tc, ID lifecycleID) { return tc.ExecuteReader("SELECT * FROM EmpLifeCycle where EmpLifeCycleID=%n order by SequenceNo", lifecycleID.Integer); } internal static IDataReader GetEmpID(TransactionContext tc, ID nEmpID, DateTime effectDate) { return tc.ExecuteReader("SELECT * FROM EmpLifeCycle where employeeid=%n AND EffectDate=%d order by SequenceNo", nEmpID.Integer, effectDate); } internal static IDataReader GetLastPosition(TransactionContext tc, int nEmpID) { return tc.ExecuteReader("select * from EmpLifeCycle where employeeid=%n and sequenceno=(select max(sequenceno) from EmpLifeCycle where employeeid=%n AND EffectDate<=%d)", nEmpID,nEmpID, DateTime.Today); } internal static IDataReader Get(TransactionContext tc, EnumStatus status, ID PayrolltypeID) { if(EnumStatus.Active==status) { return tc.ExecuteReader("SELECT * FROM EmpLifeCycle where Status=%n order by CreationDate", status); } else { return tc.ExecuteReader("SELECT * FROM EmpLifeCycle where EmployeeID in(select EmployeeID from EMPLOYEE where PAYROLLTYPEID=%n) order by CreationDate", PayrolltypeID.Integer); } } internal static IDataReader GetEmpID(TransactionContext tc, ID employeeID) { return tc.ExecuteReader("SELECT * FROM EmpLifeCycle Where EmployeeID=%n order by CreationDate DESC", employeeID.Integer); } //internal static IDataReader Get(TransactionContext tc, DateTime fromEffectDate, DateTime toEffectDate, int eventID) //{ // string sql = string.Empty; // sql = SQLParser.MakeSQL("Select * from EmpLifeCycle Where EffectDate between %d and %d AND StatusDetailID =%n order by EffectDate",fromEffectDate,toEffectDate, eventID); // return tc.ExecuteReader(sql); //} internal static IDataReader GetNotYetProcessUptoToday(TransactionContext tc) { string sql = SQLParser.MakeSQL("Select * from EmpLifeCycle where IsProcessed =%b and EffectDate <=%d", false, DateTime.Today); return tc.ExecuteReader(sql); } #endregion #region Delete function internal static void Delete(TransactionContext tc, EmpLifeCycle oEmpLifeCycle) { tc.ExecuteNonQuery("DELETE FROM EmpLifeCycle Where EmpLifeCycleID=%n", oEmpLifeCycle.ID.Integer); } internal static void DeleteByGradeSalaryAssesmentID(TransactionContext tc, EmpLifeCycle oEmpLifeCycle) { tc.ExecuteNonQuery("DELETE FROM EmpLifeCycle Where EmployeeID=%n AND GradeSalaryAssesmentID=%n", oEmpLifeCycle.EmployeeID.Integer,oEmpLifeCycle.GradeSalaryAssesmentID.Integer); } internal static object GetNodeId(TransactionContext tc, ID EmployeeId) { object ovalue = tc.ExecuteScalar("select NodeID from EmpLifeCycle where EmpLifeCycleID=( " + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and NodeID is Not Null)", EmployeeId.Integer); if (ovalue != DBNull.Value && ovalue!=null) return Convert.ToInt32(ovalue); else return null; } public static void UpdateEmployee(TransactionContext tc, EmpLifeCycle oItem) { // Gross Salary object ovalue; if (oItem.GrossSalary != null) { ovalue = tc.ExecuteScalar("select GrossSalary from EmpLifeCycle where EmpLifeCycleID=( " + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and GrossSalary is Not Null)", oItem.EmployeeID.Integer); if (ovalue != DBNull.Value) { if (ovalue != null && ovalue != null) tc.ExecuteNonQuery("UPDATE Employee SET GrossSalary= %n WHERE EMPLOYEEID=%n", Convert.ToDouble(ovalue), oItem.EmployeeID.Integer); } } if (oItem.BasicSalary != null) { //Basic Salary ovalue = tc.ExecuteScalar("select BasicSalary from EmpLifeCycle where EmpLifeCycleID=( " + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and BasicSalary is Not Null)", oItem.EmployeeID.Integer); if (ovalue != DBNull.Value) { if (ovalue != null && ovalue != null) tc.ExecuteNonQuery("UPDATE Employee SET BasicSalary= %n WHERE EMPLOYEEID=%n", Convert.ToDouble(ovalue), oItem.EmployeeID.Integer); } } ////Category //tc.ExecuteNonQuery("Update Employee Set Employee.BasicSalary =(select BasicSalary from EmpLifeCycle where EmpLifeCycleID=( " // + " select Max(BasicSalary) from EmpLifeCycle where employeeid=%n and BasicSalary is Not Null))", oItem.EmployeeID.Integer); //GradeID if (oItem.GradeID != null && oItem.GradeID.IsUnassigned ==false) tc.ExecuteNonQuery("Update Employee Set Employee.GradeID =(select GradeID from EmpLifeCycle where EmpLifeCycleID=( " + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and GradeID is Not Null)) WHERE Employee.EmployeeID=%n", oItem.EmployeeID.Integer, oItem.EmployeeID.Integer); //Company if (oItem.CompanyID != null && oItem.CompanyID.IsUnassigned == false) tc.ExecuteNonQuery("Update Employee Set Employee.Companyid =(select Companyid from EmpLifeCycle where EmpLifeCycleID=( " + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and Companyid is Not Null)) WHERE Employee.EmployeeID=%n", oItem.EmployeeID.Integer, oItem.EmployeeID.Integer); //Functionid if (oItem.FunctionID != null && oItem.FunctionID.IsUnassigned == false) tc.ExecuteNonQuery("Update Employee Set Employee.Functionid =(select Functionid from EmpLifeCycle where EmpLifeCycleID=( " + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and Functionid is Not Null)) WHERE Employee.EmployeeID=%n", oItem.EmployeeID.Integer, oItem.EmployeeID.Integer); //Designationid if (oItem.DesignationID != null && oItem.DesignationID.IsUnassigned == false) tc.ExecuteNonQuery("Update Employee Set Employee.DesignationId =(select DesignationId from EmpLifeCycle where EmpLifeCycleID=( " + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and DesignationId is Not Null)) WHERE Employee.EmployeeID=%n", oItem.EmployeeID.Integer, oItem.EmployeeID.Integer); //LocationID if (oItem.LocationID != null && oItem.LocationID.IsUnassigned == false) tc.ExecuteNonQuery("Update Employee Set Employee.LocationID =(select LocationID from EmpLifeCycle where EmpLifeCycleID=( " + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and LocationID is Not Null)) WHERE Employee.EmployeeID=%n", oItem.EmployeeID.Integer, oItem.EmployeeID.Integer); //Departmentid if (oItem.DepartmentID != null && oItem.DepartmentID.IsUnassigned == false) tc.ExecuteNonQuery("Update Employee Set Employee.Departmentid =(select Departmentid from EmpLifeCycle where EmpLifeCycleID=( " + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and Departmentid is Not Null)) WHERE Employee.EmployeeID=%n", oItem.EmployeeID.Integer, oItem.EmployeeID.Integer); //IsConfirmed if (oItem.IsConfirm != null) { ovalue = tc.ExecuteScalar("select IsConfirm from EmpLifeCycle where EmpLifeCycleID=( " + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and IsConfirm is Not Null)", oItem.EmployeeID.Integer); if (ovalue != DBNull.Value && ovalue != null) { bool isconfirm = Convert.ToBoolean(ovalue); tc.ExecuteNonQuery("UPDATE Employee SET ISCONFIRMED= %b WHERE EMPLOYEEID=%n", isconfirm, oItem.EmployeeID.Integer); } else tc.ExecuteNonQuery("UPDATE Employee SET ISCONFIRMED= %b, dateOfConfirmation=null WHERE EMPLOYEEID=%n", false , oItem.EmployeeID.Integer); } if (oItem.PFMemberType != null) { ovalue = tc.ExecuteScalar("select pFMemberType from EmpLifeCycle where EmpLifeCycleID=( " + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and PFMemberType is Not Null)", oItem.EmployeeID.Integer); if (ovalue != DBNull.Value && ovalue != null) tc.ExecuteNonQuery("UPDATE Employee SET PFMemberType=1 WHERE EMPLOYEEID=%n", oItem.EmployeeID.Integer); } if (oItem.IsContinue != null || oItem.IsDiscontinue != null) { int nContinue = 0; int nDiscontinue = 0; ovalue = tc.ExecuteScalar(" select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and IsContinue is Not Null", oItem.EmployeeID.Integer); if (ovalue != DBNull.Value && ovalue != null) nContinue = Convert.ToInt32(ovalue); ovalue = tc.ExecuteScalar(" select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and IsDiscontinue is Not Null", oItem.EmployeeID.Integer); if (ovalue != DBNull.Value && ovalue != null) nDiscontinue = Convert.ToInt32(ovalue); if (nContinue > 0 || nDiscontinue > 0) { if (nContinue > nDiscontinue) { EmployeeDA.DoContinue(tc, oItem.EmployeeID); } else { ovalue = tc.ExecuteScalar(" select effectDate from EmpLifeCycle where EmpLifeCycleId=%n ", nDiscontinue); DateTime effectDate = Convert.ToDateTime(ovalue); HREmployeeDA.UpdateEndofContact(tc, oItem.EmployeeID, effectDate); ovalue = tc.ExecuteScalar(" select IsCurrentMonthIncluded from EmpLifeCycle where EmpLifeCycleId=%n ", nDiscontinue); bool iscurrMonth = false; if (ovalue != DBNull.Value) iscurrMonth = Convert.ToBoolean(ovalue); ovalue = tc.ExecuteScalar(" select Status from EmpLifeCycle where EmpLifeCycleId=%n ", nDiscontinue); int nStatus = Convert.ToInt32(ovalue); if (iscurrMonth == true) HREmployeeDA.UpdatemiddleOfMonthDiscontinue(tc, oItem.EmployeeID, (EnumEmployeeStatus)nStatus, effectDate); } } else { tc.ExecuteNonQuery("UPDATE Employee SET Status=%n, ENDOFCONTRACTDATE=null WHERE EMPLOYEEID=%n", EnumEmployeeStatus.Waitingforjoin, oItem.EmployeeID.Integer); } } } internal static object GetNodeAssignDate(TransactionContext tc, ID EmployeeId) { object ovalue = tc.ExecuteScalar("select EffectDate from EmpLifeCycle where EmpLifeCycleID=( " + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and NodeID is Not Null)", EmployeeId.Integer); if (ovalue != DBNull.Value) return Convert.ToDateTime(ovalue); else return null; } internal static object GetCrgId(TransactionContext tc, ID EmployeeId) { object ovalue = tc.ExecuteScalar("select CostCenterID from EmpLifeCycle where EmpLifeCycleID=( " + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and CostCenterID is Not Null)", EmployeeId.Integer); if (ovalue != DBNull.Value) if(ovalue==null)return null; else return Convert.ToInt32(ovalue); else return null; } internal static int GetPrvRCId(TransactionContext tc, ID EmployeeId) { //object ovalue = tc.ExecuteScalar("select CostCenterID from EmpLifeCycle where EmpLifeCycleID=( " // + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and CostCenterID is Not Null)", EmployeeId.Integer); //if (ovalue != DBNull.Value) // if (ovalue == null) return 0; // else return Convert.ToInt32(ovalue); //else return 0; string sql = SQLParser.MakeSQL(@"select CostCenterID from EmpLifeCycle where sequenceno =(select Max(sequenceno) from EmpLifeCycle where employeeid = %n ) - 1 AND employeeid = %n", EmployeeId.Integer, EmployeeId.Integer); object ovalue = tc.ExecuteScalar(sql); if (ovalue != DBNull.Value) { if (ovalue == null) return 0; else return Convert.ToInt32(ovalue); } else return 0; } internal static int GetPrvGradeId(TransactionContext tc, ID EmployeeId,ID gradeID) { //object ovalue = tc.ExecuteScalar("select GradeID from EmpLifeCycle where EmpLifeCycleID=( " // + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and GradeID=%n)", EmployeeId.Integer,gradeID.Integer); //if (ovalue != DBNull.Value) // if (ovalue == null) return 0; // else return Convert.ToInt32(ovalue); //else return 0; string sql = SQLParser.MakeSQL(@"select GradeID from EmpLifeCycle where sequenceno =(select Max(sequenceno) from EmpLifeCycle where employeeid = %n ) - 1 AND employeeid = %n", EmployeeId.Integer, EmployeeId.Integer); object ovalue = tc.ExecuteScalar(sql); if (ovalue != DBNull.Value) { if (ovalue == null) return 0; else return Convert.ToInt32(ovalue); } else return 0; } internal static int GetPrvLocationId(TransactionContext tc, ID EmployeeId, ID locationID) { //object ovalue = tc.ExecuteScalar("select LocationID from EmpLifeCycle where EmpLifeCycleID=( " // + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and LocationID=%n)", EmployeeId.Integer, locationID.Integer); //if (ovalue != DBNull.Value) // if (ovalue == null) return 0; // else return Convert.ToInt32(ovalue); //else return 0; string sql = SQLParser.MakeSQL(@"select LocationID from EmpLifeCycle where sequenceno =(select Max(sequenceno) from EmpLifeCycle where employeeid = %n ) - 1 AND employeeid = %n", EmployeeId.Integer, EmployeeId.Integer); object ovalue = tc.ExecuteScalar(sql); if (ovalue != DBNull.Value) { if (ovalue == null) return 0; else return Convert.ToInt32(ovalue); } else return 0; } internal static int GetPrvDesigId(TransactionContext tc, ID EmployeeId, ID designationID) { object ovalue = tc.ExecuteScalar("select LocationID from EmpLifeCycle where EmpLifeCycleID=( " + " select Max(EmpLifeCycleId) from EmpLifeCycle where employeeid=%n and LocationID=%n)", EmployeeId.Integer, designationID.Integer); if (ovalue != DBNull.Value) if (ovalue == null) return 0; else return Convert.ToInt32(ovalue); else return 0; } #endregion internal static IDataReader Get(TransactionContext tc, int p, DateTime startDate, DateTime endDate) { string sql = SQLParser.MakeSQL("Select * from EmpLifeCycle where StatusDetailID =%n and EffectDate between %d and %d ", p,startDate,endDate); return tc.ExecuteReader(sql); } internal static IDataReader GetByDate(TransactionContext tc, DateTime startDate, DateTime endDate, ID PayrolltypeID) { string sql = SQLParser.MakeSQL("Select * from EmpLifeCycle where salarymonth between %d and %d AND EmployeeID in(select EmployeeID from EMPLOYEE where PAYROLLTYPEID=%n) order by CreationDate DESC", startDate, endDate, PayrolltypeID.Integer); return tc.ExecuteReader(sql); } internal static IDataReader GetByCreateDate(TransactionContext tc, DateTime startDate, DateTime endDate, int payrollTypeID) { string sql = SQLParser.MakeSQL("Select * from EmpLifeCycle where CREATIONDATE between %d and %d AND EmployeeID in(select EmployeeID from EMPLOYEE where PAYROLLTYPEID=%n) order by EffectDate ", startDate, endDate, payrollTypeID); return tc.ExecuteReader(sql); } internal static IDataReader GetByDate(TransactionContext tc, DateTime startDate, DateTime endDate, ID PayrolltypeID,bool approveID) { string sql = ""; if(approveID) sql = SQLParser.MakeSQL(@"Select * from EmpLifeCycle el where salarymonth between %d and %d AND EmployeeID in(select EmployeeID from EMPLOYEE where PAYROLLTYPEID=%n) AND EmplifecycleID IN(SELECT afd.ObjectID FROM ApproveFinantialData afd WHERE afd.EmployeeID=el.employeeid AND afd.FinanatialDataType=3 AND afd.SalaryMonth between %d and %d )", startDate, endDate, PayrolltypeID.Integer, startDate, endDate); else sql = SQLParser.MakeSQL(@"Select * from EmpLifeCycle el where salarymonth between %d and %d AND EmployeeID in(select EmployeeID from EMPLOYEE where PAYROLLTYPEID=%n) AND EmplifecycleID not IN(SELECT afd.ObjectID FROM ApproveFinantialData afd WHERE afd.EmployeeID=el.employeeid AND afd.FinanatialDataType=3 AND afd.SalaryMonth between %d and %d )", startDate, endDate, PayrolltypeID.Integer, startDate, endDate); return tc.ExecuteReader(sql); } internal static int? EmpPrevDesg(TransactionContext tc, ID employeeID) { string sql = SQLParser.MakeSQL(@"select DesignationID from EmpLifeCycle where sequenceno =(select Max(sequenceno) from EmpLifeCycle where employeeid = %n ) - 1 AND employeeid = %n",employeeID.Integer,employeeID.Integer); object ovalue = tc.ExecuteScalar(sql); if (ovalue != DBNull.Value) { if (ovalue == null) return null; else return Convert.ToInt32(ovalue); } else return null; } } #endregion }