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


namespace HRM.DA
{
    #region EmployeeUnAuthorizeLeaveDA

    internal class EmployeeUnAuthorizeLeaveDA
    {
        #region Constructor

        private EmployeeUnAuthorizeLeaveDA()
        {
        }

        #endregion

        #region Insert function

        internal static void Insert(TransactionContext tc, EmployeeUnAuthorizeLeave item)
        {
            tc.ExecuteNonQuery(
                "INSERT INTO MONTHLYLEAVEENTRY(EmpLeaveID, EMPLOYEEID, LEAVEID, MONTHDATE, LEAVEDAY, CreatedBy, LEAVEMONTH, FROMDATE, TODATE, CreationDate, Type, ReferenceID, ParamID, IsLateAttendanceRelated, payrolltypeid)" +
                " VALUES(%n, %n, %n, %d, %n, %n, %d, %d, %d, %d, %n, %n, %n, %n, %n)", item.ID, item.EmployeeID,
                item.UnAuthorizeleaveID, item.MonthDate, item.LeaveDays, DataReader.GetNullValue(item.CreatedBy),
                item.LeaveMonth
                , item.FromDate, item.ToDate, item.CreatedDate, (int)item.Type,
                DataReader.GetNullValue(item.ReferenceID), item.ParamID, item.IsLateAttendanceRelated, item.PayrollTypeID);
        }

        #endregion

        #region Update function

        internal static void Update(TransactionContext tc, EmployeeUnAuthorizeLeave item)
        {
            tc.ExecuteNonQuery(
                "UPDATE MONTHLYLEAVEENTRY SET EMPLOYEEID=%n, LEAVEID=%n, MONTHDATE=%d, LEAVEDAY=%n, LEAVEMONTH=%d, FROMDATE=%d, TODATE=%d, ModifiedDate=%d, Type=%n, ReferenceID=%n, ModifiedBy=%n, ParamID = %n, IsLateAttendanceRelated=%n" +
                " WHERE EmpLeaveID=%n", item.EmployeeID, item.UnAuthorizeleaveID, item.MonthDate, item.LeaveDays,
                item.LeaveMonth, item.FromDate, item.ToDate, item.ModifiedDate, item.Type, item.ReferenceID,
                item.ModifiedBy, item.ParamID, item.IsLateAttendanceRelated, item.ID);
        }

        #endregion

        #region Get Function

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

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

        internal static IDataReader GetByEmployeeAfter(TransactionContext tc, int nID, EnumLeaveEntryType type,
            DateTime lastmonth)
        {
            return tc.ExecuteReader("SELECT * FROM MONTHLYLEAVEENTRY WHERE EmployeeID=%n AND MONTHDATE>%d AND Type=%n",
                nID, lastmonth, type);
        }

        internal static IDataReader GetByEmployeeID(TransactionContext tc, int employeeID)
        {
            return tc.ExecuteReader("SELECT * FROM MONTHLYLEAVEENTRY WHERE EmployeeID=%n ", employeeID);
        }

        internal static IDataReader GetBySalaryMonth(TransactionContext tc, DateTime salaryMonth)
        {
            return tc.ExecuteReader("SELECT * FROM MONTHLYLEAVEENTRY WHERE MONTHDATE=%d ", salaryMonth);
        }


        internal static IDataReader GetDeductedLeave(TransactionContext tc, int nEmpID, int nLeaveID,
            EnumLeaveEntryType type, DateTime nextPayProcessDate)
        {
            DateTime dDate = nextPayProcessDate;
            return tc.ExecuteReader(
                "SELECT * FROM MONTHLYLEAVEENTRY ML WHERE EmployeeID=%n AND MONTHDATE<%d AND Type=%n AND LeaveID=%n" +
                "AND EmpLeaveID NOT IN ( " +
                "Select MLP.ReferenceID From MONTHLYLEAVEENTRY MLP " +
                "WHERE MLP.EmployeeID=%n AND MLP.ReferenceID > 0 AND MLP.ReferenceID = ML.EmpLeaveID AND MLP.LeaveID=%n " +
                "Group By MLP.ReferenceID Having Sum(MLP.LeaveDay) >= ML.LeaveDay) ", nEmpID, dDate, type, nLeaveID,
                nEmpID, nLeaveID);
        }

        internal static IDataReader GetByEmployee(TransactionContext tc, int nID, DateTime dMonth,
            EnumLeaveEntryType type, int payrolltypeid)
        {
            return tc.ExecuteReader(
                @"SELECT el.*, e.EmployeeNo, e.Name as EmployeeName, l.leaveDesc as LeaveName FROM MONTHLYLEAVEENTRY el, Employee e, LEAVESUSPENSETYPE l 
                    WHERE e.EmployeeID =el.EmployeeID and l.LeaveID = el.LEAVEID and e.PayrollTypeID =%n and
                    el.EmployeeID=%n AND el.MONTHDATE=%d and el.Type=%n", payrolltypeid, nID, dMonth, type);
        }

        internal static IDataReader Get(TransactionContext tc, int payrolltypeid , int? empid, DateTime? dMonth)
        {
            string sqlClauseEmployee = string.Empty;
            string sqlClauseMonth = string.Empty;

            if (empid !=null)
            {
                sqlClauseEmployee = SQLParser.MakeSQL(" and el.EmployeeID = %n", empid);
            }

            if (dMonth !=null)
            {
                sqlClauseMonth = SQLParser.MakeSQL(" and el.MONTHDATE=%d", GlobalFunctions.LastDateOfMonth( (DateTime) dMonth));
            }

            string sql = SQLParser.MakeSQL(
                @"SELECT el.*, e.EmployeeNo, e.Name as EmployeeName, l.leaveDesc as LeaveName FROM MONTHLYLEAVEENTRY el, Employee e, LEAVESUSPENSETYPE l 
                    WHERE e.EmployeeID =el.EmployeeID and l.LeaveID = el.LEAVEID and e.PayrollTypeID =%n 
                     %q %q", payrolltypeid, sqlClauseEmployee, sqlClauseMonth);

            return tc.ExecuteReader(sql);
        }


        internal static int GetAdjustedDays(TransactionContext tc, int nID)
        {
            object value =
                tc.ExecuteScalar("SELECT Sum(LeaveDay) FROM MONTHLYLEAVEENTRY Where ReferenceID = %n AND Type= %n", nID,
                    EnumLeaveEntryType.PaidLeave);
            if (value == DBNull.Value)
                return 0;
            else return Convert.ToInt32(value);
        }

        internal static int GetAdjustedDays(TransactionContext tc, int nID, DateTime month)
        {
            object value =
                tc.ExecuteScalar(
                    "SELECT Sum(LeaveDay) FROM MONTHLYLEAVEENTRY Where ReferenceID = %n AND Type= %n  AND MonthDate=%d",
                    nID, EnumLeaveEntryType.PaidLeave, month);
            if (value == DBNull.Value)
                return 0;
            else return Convert.ToInt32(value);
        }

        internal static IDataReader Get(TransactionContext tc, EnumLeaveEntryType type, DateTime month)
        {
            return tc.ExecuteReader(
                "SELECT * FROM MONTHLYLEAVEENTRY WHERE MONTHDATE=%d AND Type=%n Order By EmployeeID,MONTHDATE", month,
                type);
        }

        internal static IDataReader Get(TransactionContext tc, DateTime fromDate, DateTime todate,
            EnumLeaveEntryType type)
        {
            return tc.ExecuteReader("SELECT * FROM MONTHLYLEAVEENTRY WHERE MONTHDATE Between %d AND %d "
                                    + " AND Type=%n Order By EmployeeID, MONTHDATE",
                fromDate, todate, type);
        }

        internal static IDataReader GetByEmployeeAfter(TransactionContext tc, int nEmpID, int nLeaveID,
            EnumLeaveEntryType type, DateTime LastPayProcessMonth)
        {
            return tc.ExecuteReader(
                @"SELECT * FROM MONTHLYLEAVEENTRY WHERE EmployeeID=%n AND LeaveID=%n AND MONTHDATE>%d AND Type=%n",
                nEmpID, nLeaveID, LastPayProcessMonth, type);
        }

        internal static IDataReader GetByEmployee(TransactionContext tc, int empid, int leaveid,  DateTime leaveMonth,
            EnumLeaveEntryType type)
        {
            return tc.ExecuteReader("SELECT * FROM MONTHLYLEAVEENTRY WHERE EmployeeID=%n AND LEAVEID=%n AND LeaveMonth=%d and Type=%n",
                empid, leaveid , leaveMonth, type);
        }

        internal static DataSet GetUnAuthorizeLeave(TransactionContext tc, DateTime date)
        {
            DataSet unAuthoLeave = new DataSet();
            try
            {
                string sql = SQLParser.MakeSQL("select EmployeeID,COUNT(EmployeeID) TotalAbsent from DailyAttnProcess "
                                               + "where AttnDate between %d AND %d AND AttnType=%n group by EmployeeID order by EmployeeID ",
                    PayrollGlobalFunctions.PayrollFirstDateOfMonth(date),
                    PayrollGlobalFunctions.PayrollLastDateOfMonth(date), EnumAttendanceType.Absent);

                unAuthoLeave = tc.ExecuteDataSet(sql);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return unAuthoLeave;
        }

        #endregion

        #region Delete function

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

        internal static void Delete(TransactionContext tc, DateTime dateTime, int nLeaveID, int nEmpID)
        {
            tc.ExecuteNonQuery("DELETE FROM MONTHLYLEAVEENTRY WHERE MONTHDATE=%d AND LEAVEID=%n AND EMPLOYEEID=%n",
                dateTime, nLeaveID, nEmpID);
        }

        internal static void Delete(TransactionContext tc, DateTime month, DateTime fromDate, DateTime toDate,
            int nLeaveID, int nEmpID)
        {
            tc.ExecuteNonQuery(
                "DELETE FROM MONTHLYLEAVEENTRY WHERE MONTHDATE=%d AND FromDate=%d and ToDate=%d and LEAVEID=%n AND EMPLOYEEID=%n",
                month, fromDate, toDate, nLeaveID, nEmpID);
        }

        internal static bool IsExist(TransactionContext tc, DateTime dateTime, int nLeaveID, int nEmpID)
        {
            bool Exist = false;
            Object obj =
                tc.ExecuteScalar(
                    "select Count(*) FROM MONTHLYLEAVEENTRY WHERE MONTHDATE=%d AND LEAVEID=%n AND EMPLOYEEID=%n",
                    dateTime, nLeaveID, nEmpID);
            Exist = Convert.ToInt32(obj) > 0 ? true : false;
            return Exist;
        }

        internal static bool IsExist(TransactionContext tc, DateTime Month, DateTime FromDate, DateTime todate,
            int nLeaveID, int nEmpID)
        {
            bool Exist = false;
            Object obj =
                tc.ExecuteScalar(
                    "select Count(*) FROM MONTHLYLEAVEENTRY WHERE MONTHDATE=%d and FromDate=%d and ToDate=%d AND LEAVEID=%n AND EMPLOYEEID=%n",
                    Month, FromDate, todate, nLeaveID, nEmpID);
            Exist = Convert.ToInt32(obj) > 0 ? true : false;
            return Exist;
        }

        internal static void Delete(TransactionContext tc, DateTime month, int nEmpID)
        {
            tc.ExecuteNonQuery("DELETE FROM MONTHLYLEAVEENTRY WHERE MONTHDATE=%d AND EMPLOYEEID=%n", month, nEmpID);
        }

        internal static void Delete(TransactionContext tc, DateTime dateTime)
        {
            string sql = SQLParser.MakeSQL("DELETE FROM MONTHLYLEAVEENTRY WHERE MONTHDATE between %d AND %d ",
                PayrollGlobalFunctions.PayrollFirstDateOfMonth(dateTime),
                PayrollGlobalFunctions.PayrollLastDateOfMonth(dateTime));
            tc.ExecuteNonQuery(sql);
        }

        #endregion
    }

    #endregion
}