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 DepartmentDA internal class DepartmentDA { #region Constructor private DepartmentDA() { } #endregion #region Insert function internal static void Insert(TransactionContext tc, Department item) { tc.ExecuteNonQuery("INSERT INTO Department(DepartmentID, code, description, parentID, parentsID, CreatedBy, CreationDate, SequenceNo, Status,TIRE,rccode)" + " VALUES(%n, %s, %s, %n, %s, %n, %d, %n, %n,%n,%s)", item.ID.Integer, item.Code, item.Name, DataReader.GetNullValue(item.ParentID, IDType.Integer), item.ParentsID, item.CreatedBy.Integer, item.CreatedDate, item.Sequence, item.Status, item.Tier, item.RCCode); } #endregion #region Update function internal static void Update(TransactionContext tc, Department item) { tc.ExecuteNonQuery("UPDATE Department SET code=%s, description=%s, parentID=%n, parentsID=%s, ModifiedBy=%n, ModifiedDate=%d, SequenceNo=%n, Status=%n,TIRE=%n, rccode=%s" + " WHERE DepartmentID=%n", item.Code, item.Name, DataReader.GetNullValue(item.ParentID,IDType.Integer), item.ParentsID, item.ModifiedBy.Integer, item.ModifiedDate, item.Sequence, item.Status, item.Tier,item.RCCode, item.ID.Integer); } internal static void UpdateCode(TransactionContext tc, Department item, string sCode) { string sSQL = SQLParser.MakeSQL("UPDATE Department Set code = %s Where DepartmentID = %n", sCode, item.ID.Integer); tc.ExecuteNonQuery(sSQL); } #endregion #region Get Function internal static IDataReader Get(TransactionContext tc) { return tc.ExecuteReader("SELECT * FROM Department where status=1 Order by SequenceNo"); } internal static IDataReader GetBySalaryMonthAndRCCode(TransactionContext tc, DateTime salaryMonth) { return tc.ExecuteReader("SELECT * FROM DEPARTMENT WHERE RCCode IN (SELECT DISTINCT RCCode FROM SALARYMONTHLY WHERE SalaryMonth=%d and PayrollTypeID =1) order by SequenceNo", salaryMonth); } internal static IDataReader Get(TransactionContext tc, string sCode) { return tc.ExecuteReader("SELECT * FROM Department WHERE Code=%s", sCode); } internal static IDataReader Get(TransactionContext tc, ID nID) { return tc.ExecuteReader("SELECT * FROM Department WHERE DepartmentID=%n", nID.Integer); } internal static IDataReader GetChild(TransactionContext tc, ID nID) { return tc.ExecuteReader("SELECT * FROM Department WHERE PARENTID=%n order by DESCRIPTION", nID.Integer); } internal static IDataReader GetParents(TransactionContext tc,EnumStatus status) { if(EnumStatus.Active==status || EnumStatus.Inactive==status) { return tc.ExecuteReader("SELECT * FROM Department Where PARENTID IS NULL and Status=%n Order by SequenceNo",status); } else { return tc.ExecuteReader("SELECT * FROM Department Where PARENTID IS NULL Order by SequenceNo"); } } internal static IDataReader GetParentLessChilds(TransactionContext tc) { return tc.ExecuteReader("SELECT * FROM Department Where PARENTID IS NULL Order by DepartmentID"); } internal static IDataReader GetByTier(TransactionContext tc, int tier, int parentid) { if (parentid == -1) { return tc.ExecuteReader("SELECT * FROM Department Where Tire=%n Order by DepartmentID", tier); } else { return tc.ExecuteReader("SELECT * FROM Department Where Tire=%n and ParentID=%n Order by DepartmentID", tier, parentid); } } internal static IDataReader GetByTier(TransactionContext tc, int tier) { return tc.ExecuteReader("SELECT * FROM Department Where Tire=%n Order by DepartmentID", tier); } #endregion #region Delete function internal static void Delete(TransactionContext tc, ID nID) { tc.ExecuteNonQuery("UPDATE [Department] SET CreatedBy=%n,ModifiedBy=%n Where DepartmentID=%n", Payroll.BO.User.CurrentUser.ID.Integer, Payroll.BO.User.CurrentUser.ID.Integer, nID.Integer); tc.ExecuteNonQuery("DELETE FROM [Department] WHERE DepartmentID=%n", nID.Integer); } internal static DataSet GetForTree(TransactionContext tc) { return tc.ExecuteDataSet("SELECT * FROM Department Where Tire IN(1,2) Order by Description"); } #endregion internal static DataSet GetAllChilds(TransactionContext tc, ID parentID) { DataSet rootDataset = new DataSet(); DataSet tempdataset = new DataSet(); try { string query = SQLParser.MakeSQL(@"WITH cte AS ( SELECT pc.PARENTID as DeptParent, pc.* FROM DEpartment AS pc WHERE pc.PARENTID = %n UNION ALL SELECT %n as DeptParent, pc2.* FROM DEpartment pc2 JOIN cte c ON pc2.PARENTID = c.DepartmentID) SELECT * FROM CTE UNION SELECT dept.DEPARTMENTID as DeptParent, dept.* FROM DEpartment AS dept WHERE dept.DEPARTMENTID =%n ", parentID.Integer, parentID.Integer, parentID.Integer); tempdataset = tc.ExecuteDataSet(query); tempdataset.Tables[0].TableName = "Department"; rootDataset.Tables.Add(tempdataset.Tables[0].Copy()); } catch (Exception ex) { throw new Exception(ex.Message); } return rootDataset; } internal static DataSet GetRCCodeBySalaryMonth(TransactionContext tc, DateTime salaryMonth) { DataSet rootDataset = new DataSet(); DataSet tempdataset = new DataSet(); try { string query = SQLParser.MakeSQL(@"SELECT DISTINCT Rccode FROM SALARYMONTHLY WHERE SalaryMonth=%d and PAYROLLTYPEID=%n", salaryMonth, SystemInformation.CurrentSysInfo.PayrollTypeID.Integer); tempdataset = tc.ExecuteDataSet(query); tempdataset.Tables[0].TableName = "Rccode"; rootDataset.Tables.Add(tempdataset.Tables[0].Copy()); } catch (Exception ex) { throw new Exception(ex.Message); } return rootDataset; } internal static DataSet GetDepartmentWiseManpower(TransactionContext tc, string sParam) { DataSet rootDataset = new DataSet(); DataSet tempdataset = new DataSet(); try { string query = SQLParser.MakeSQL(@"SELECT d.[Code] Unit,tab1.Departmnt,tab1.Grade,tab1.Manpower FROM DEPARTMENT d, (SELECT d.PARENTID, d.[DESCRIPTION] Departmnt,g.[Code] Grade,COUNT(Employeeid) Manpower FROM EMPLOYEE e inner join department d on e.DepartmentID = d.DepartmentID inner join Grades g on e.GradeID = g.GradeID %q GROUP BY d.PARENTID,d.[DESCRIPTION],g.Code )tab1 WHERE d.DEPARTMENTID=tab1.PARENTID", sParam); tempdataset = tc.ExecuteDataSet(query); tempdataset.Tables[0].TableName = "Manpower"; rootDataset.Tables.Add(tempdataset.Tables[0].Copy()); } catch (Exception ex) { throw new Exception(ex.Message); } return rootDataset; } } #endregion }