using HRM.BO;
using Ease.Core.DataAccess;
using System;
using System.Data;


namespace HRM.DA
{
    #region OTProcessDA

    internal class OTProcessDA
    {
        #region Constructor

        private OTProcessDA()
        {
        }

        #endregion

        #region Insert function

        internal static void Insert(TransactionContext tc, OTProcess item)
        {
            tc.ExecuteNonQuery(
                "INSERT INTO OTProcess(ProcessId, ProcessMonth, empID, Amount, TermID, Hours, CreatedBy, CreationDate,TermParameterID,EmpOverTimeID,ArrearAmount,OTMonth,PayrollTypeID)" +
                " VALUES(%n, %d, %n, %n, %n, %n, %n, %D,%n,%n,%n,%d,%n)", item.ID, item.MonthDate, item.EmployeeID,
                item.Amount, item.TermID, item.TotalHour, item.CreatedBy, item.CreatedDate, item.TermParameterID,
                item.EmpOverTimeID, item.ArrearAmount, item.OTMonth, item.PayrollTypeID);
        }

        #endregion

        #region Update function

        internal static void Update(TransactionContext tc, OTProcess item)
        {
            tc.ExecuteNonQuery(
                "UPDATE OTProcess SET month=%d, empID=%n, amount=%n, termID=%n, Hour=%n, ModifiedBy=%n, ModifiedDate=%d,TermParameterID=%n,EmpOverTimeID=%n,ArrearAmount=%n,OTMonth=%d,PayrollTypeID=%n" +
                " WHERE ProcessId=%n", item.MonthDate, item.EmployeeID, item.Amount, item.TermID, item.TotalHour,
                item.ModifiedBy, item.ModifiedDate, item.TermParameterID, item.EmpOverTimeID, item.ArrearAmount,
                item.OTMonth, item.PayrollTypeID, item.ID);
        }

        #endregion

        #region Get Function

        internal static IDataReader Get(TransactionContext tc)
        {
            return tc.ExecuteReader("SELECT * FROM OTProcess");
        }

        internal static DataSet GetCCWiseOTSummary(TransactionContext tc, DateTime dateTime, string sEmpID)
        {
            DataSet oOTMonthlySheets = new DataSet();
            try
            {
                string sSQL = SQLParser.MakeSQL(@"SELECT cc.DESCRIPTION cc, 'OT' Item, SUM(ot.Amount) Amount
                                                FROM SALARYMONTHLY sm, salaryempcostcenter sc,crg cc, EMPLOYEE emp, OTProcess ot
                                                WHERE emp.EMPLOYEEID = sm.EMPLOYEEID
                                                AND  sm.SALARYMONTHLYID=sc.SALARYMONTHLYID
                                                AND sc.COSTCENTERID=cc.CRGID
                                                AND ot.ProcessMonth =%d  
                                                AND sm.EmployeeId = ot.EmpID    
                                                AND sm.SalaryMonth=%d
                                                AND ot.EmpID IN (%q)
                                                GROUP BY cc.DESCRIPTION", dateTime, dateTime, sEmpID);
                oOTMonthlySheets = tc.ExecuteDataSet(sSQL);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return oOTMonthlySheets;
        }

        internal static DataSet GetOTProcessData(TransactionContext tc, DateTime Month, string sEmpID)
        {
            DataSet oOTMonthlySheets = new DataSet();
            try
            {
                string sql = SQLParser.MakeSQL(
                    "SELECT E.EMPLOYEENO,E.NAME EmpName,E.BasicSalary,T.Name TermName,OTP.Hours,OTP.Amount,OTP.ProcessMonth, OTP.OTMonth, OTP.TermID SeqNo"
                    + " FROM EMPLOYEE E,OTProcess OTP,Term T"
                    + " WHERE E.EMPLOYEEID=OTP.EmpID"
                    + " AND OTP.EmpID IN(%q)"
                    + " AND T.TermID=OTP.TermID And OTP.ProcessMonth=%d order by E.EmployeeNo", sEmpID, Month);
                oOTMonthlySheets = tc.ExecuteDataSet(sql);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return oOTMonthlySheets;
        }

        internal static DataSet GetOTMonthRangeData(TransactionContext tc, DateTime FDate, DateTime TDate)
        {
            DataSet oOTMonthlySheets = new DataSet();
            try
            {
                oOTMonthlySheets = tc.ExecuteDataSet(
                    "SELECT Loc.DESCRIPTION Location,Dep.DESCRIPTION, Emp.EMPLOYEENO,Emp.NAME,RTrim(LTrim(DateName(MM,Pro.ProcessMonth))) OTMonth,Pro.Hours"
                    + " FROM EMPLOYEE Emp,OTProcess Pro,LOCATION Loc,Department Dep"
                    + " WHERE Emp.EMPLOYEEID=Pro.EmpID"
                    + " AND Emp.DEPARTMENTID=Dep.DEPARTMENTID"
                    + " and Emp.LOCATIONID=Loc.LOCATIONID"
                    + " AND Pro.ProcessMonth BETWEEN %d AND %d"
                    + " GROUP BY Loc.DESCRIPTION,Dep.DESCRIPTION, Emp.EMPLOYEENO,Emp.NAME,Pro.ProcessMonth,Pro.Hours  order by Month(Pro.ProcessMonth)",
                    FDate, TDate);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return oOTMonthlySheets;
        }

        internal static DataSet GetBranchWiseOT(TransactionContext tc, DateTime Month)
        {
            DataSet oOTMonthlySheets = new DataSet();
            try
            {
                oOTMonthlySheets = tc.ExecuteDataSet(
                    "Select Loc.DESCRIPTION Location,Dep.DESCRIPTION Department,RTrim(LTrim(DateName(MM,Pro.ProcessMonth))) + ' ' + RTrim(LTrim(Year(Pro.ProcessMonth))) Month,Sum(Pro.Hours ) OTHours"
                    + " from EMPLOYEE Emp,LOCATION Loc,DEPARTMENT Dep,OTProcess Pro"
                    + " where Emp.EMPLOYEEID=Pro.EmpID"
                    + " and Emp.LOCATIONID=Loc.LOCATIONID"
                    + " and Emp.DEPARTMENTID=Dep.DEPARTMENTID"
                    + " and Pro.ProcessMonth=%d"
                    + " GROUP BY Loc.DESCRIPTION,Dep.DESCRIPTION,RTrim(LTrim(DateName(MM,Pro.ProcessMonth))) + ' ' + RTrim(LTrim(Year(Pro.ProcessMonth)))",
                    Month);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return oOTMonthlySheets;
        }

        internal static DataSet GetLocationWiseOT(TransactionContext tc, DateTime FDate, DateTime TDate)
        {
            DataSet oOTMonthlySheets = new DataSet();
            try
            {
                oOTMonthlySheets = tc.ExecuteDataSet(
                    "Select Loc.DESCRIPTION Location,Dep.DESCRIPTION Department,DateName(MM,Pro.ProcessMonth) Month,Year(Pro.ProcessMonth) Year,Month(Pro.ProcessMonth),SUM(Pro.Hours) OTHours"
                    + " from EMPLOYEE Emp,LOCATION Loc,DEPARTMENT Dep,OTProcess Pro"
                    + " where Emp.EMPLOYEEID=Pro.EmpID"
                    + " and Emp.LOCATIONID=Loc.LOCATIONID"
                    + " and Emp.DEPARTMENTID=Dep.DEPARTMENTID"
                    + " and Pro.ProcessMonth between %d and %d"
                    + " group by Loc.DESCRIPTION,Dep.DESCRIPTION,DateName(MM,Pro.ProcessMonth),Year(Pro.ProcessMonth),Month(Pro.ProcessMonth)"
                    + " order By Loc.DESCRIPTION,Dep.DESCRIPTION,Month(Pro.ProcessMonth)", FDate, TDate);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return oOTMonthlySheets;
        }

        internal static IDataReader Get(TransactionContext tc, int nID)
        {
            return tc.ExecuteReader("SELECT * FROM OTProcess WHERE ProcessId=%n", nID);
        }

        internal static IDataReader IsProcessed(TransactionContext tc, DateTime dMonthDate, int payrollTypeID)
        {
            return tc.ExecuteReader("SELECT Count(*) FROM OTProcess WHERE ProcessMonth=%d AND PayrollTypeID=%n",
                dMonthDate, payrollTypeID);
        }

        internal static IDataReader Get(TransactionContext tc, DateTime dMonthDate)
        {
            return tc.ExecuteReader("SELECT * FROM OTProcess WHERE ProcessMonth=%d", dMonthDate);
        }

        internal static IDataReader GetOTProcessDataByMonthAndDeptID(TransactionContext tc, DateTime dMonthDate,
            string deptID)
        {
            return tc.ExecuteReader(
                "SELECT ot.* FROM OTProcess ot Inner join Employee e on e.EmployeeID=ot.EmpID inner join Department d on d.DepartmentID=e.DepartmentID WHERE d.DepartmentID in (%q) and ot.ProcessMonth=%d",
                deptID, dMonthDate);
        }

        internal static IDataReader GetbyOtMonth(TransactionContext tc, string sEmpID, string sDates)
        {
            return tc.ExecuteReader("Select * from OTProcess Where EmpID in (%q) AND OTMonth in (%q) " +
                                    "AND TermparameterID in  (Select TermParameterID from TermParameter TP Where Type in (3,4)) Order By EmpID, OTMonth",
                sEmpID, sDates);
        }

        internal static DataSet GetCostCenterWiseOT(TransactionContext tc, DateTime dateTime, string sEmpID)
        {
            DataSet oOTMonthlySheets = new DataSet();
            try
            {
                string sSQL = SQLParser.MakeSQL(@"SELECT   e.EmployeeNo, e.Name EmpName,e.ACCOUNTNO,D.NAME Designation, 
                                                Sum(ot.Amount) Amount ,CC.DESCRIPTION CostCenter
                                                FROM Employee e, OTProcess ot,CRG CC,EMPCOSTCENTER empcc,Designation D
                                                WHERE ot.ProcessMonth =%d 
                                                AND e.EmployeeId IN(%q) 
                                                AND e.EmployeeId = ot.EmpID 
                                                AND e.EMPLOYEEID = empcc.EMPLOYEEID
                                                AND CC.CRGID = empcc.COSTCENTERID
                                                AND empcc.CurrentCC=1 
                                                AND e.DESIGNATIONID = D.DESIGNATIONID 
                                                GROUP BY  e.EmployeeNo, e.Name,e.ACCOUNTNO,CC.DESCRIPTION,D.NAME
                                                ORDER BY  e.EmployeeNo", dateTime, sEmpID);
                oOTMonthlySheets = tc.ExecuteDataSet(sSQL);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return oOTMonthlySheets;
        }

        #endregion

        #region Delete function

        internal static void Delete(TransactionContext tc, int nID)
        {
            tc.ExecuteNonQuery("DELETE FROM OTProcess WHERE ProcessId=%n", nID);
        }

        internal static void Delete(TransactionContext tc, DateTime dMonthDate, int nPayrollTypeID)
        {
            tc.ExecuteNonQuery("DELETE FROM OTProcess WHERE ProcessMonth=%d AND PayrollTypeID=%n", dMonthDate,
                nPayrollTypeID);
        }

        #endregion
    }

    #endregion
}