using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/****************************************************************************************************************************
* Project Name    : TimeCheck                    Started Date  : 12-Aug-2007
* Module          : Leave Application            Last Modified : 14-Aug-2007
* Programmer      : Prabakaran.P
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Transactions_NumberofDaysCount : Base.BasePage
{
    string FromDate, ToDate, Fromsession, Tosession, LeaveTypeId, EmployeeId, EmployeeCodeId, gstrDateFormat, gstrSwipeCardNo, gstrDateOfJoining;
    DateTime YearSettingFromDate, YearSettingToDate;
    string gstrleavetypename;
    int testvalue;
    //int gintWeeklyOffHoliday = 0;
    double gintWeeklyOffHoliday;
    int StartYear, EndYear;
    int SuccessiveDays = 0;
    DataSet ds_LeavePolicyDetails = new DataSet();
    DataSet ds_EmployeeDetails = new DataSet();
    DataSet ds_WeeklyOff = new DataSet(); int intlevelid = 0;
    LeaveApplicationEventData objLeaveApplicationEventData = new LeaveApplicationEventData();
    ShiftExchangeEventData objShiftExchangeEventData = new ShiftExchangeEventData();
    HolidayEventData objHolidayEventData = new HolidayEventData();
    cls_Common objcls_Common = new cls_Common();
    int intApplicationYear = 0;
    MonthClosureEventData objMonthClosure = new MonthClosureEventData();
    PermissionEventData objPermissionEventData = new PermissionEventData();
    string selLeaveType = string.Empty;
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        if (Session["parentLevelId"] != null)
        {
            intlevelid = Convert.ToInt32(Session["parentLevelId"].ToString());
        }
        if (Request.QueryString["ApplicationYear"] != string.Empty && Request.QueryString["ApplicationYear"] != null)
        {
            intApplicationYear = Convert.ToInt32(Request.QueryString["ApplicationYear"].ToString());
        }
        //Checking and Convert to Date Format MM/dd/yyyy
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        FromDate = objcls_Common.DateConvertion(gstrDateFormat, Convert.ToString(Request.QueryString["FromDate"]));
        ToDate = objcls_Common.DateConvertion(gstrDateFormat, Convert.ToString(Convert.ToString(Request.QueryString["ToDate"])));
        if (DateDifference(DateTime.Parse(FromDate), DateTime.Parse(ToDate)) < 0)
        {
            Response.Clear();
            Response.Write("FromToDates");
            Response.End();
            return;
        }
        DataSet ds_Year = new DataSet();
        cls_Common objClassCommon = new cls_Common();
        ds_Year = objClassCommon.GetYearsetting();
        if (ds_Year.Tables[0].Rows.Count <= 0)
        {
        }
        else
        {
            StartYear = Convert.ToInt32(ds_Year.Tables[0].Rows[0]["StartYear"]);
            EndYear = Convert.ToInt32(ds_Year.Tables[0].Rows[0]["EndYear"]);
            YearSettingFromDate = Convert.ToDateTime(ds_Year.Tables[0].Rows[0]["YearSettingFromDate"]);
            YearSettingToDate = Convert.ToDateTime(ds_Year.Tables[0].Rows[0]["YearSettingToDate"]);
            ds_Year.Dispose();
        }
        if (Session["DaysForSuccessive"] != null)
        {
            SuccessiveDays = Convert.ToInt32(Session["DaysForSuccessive"]);
            YearSettingToDate = YearSettingToDate.AddDays(SuccessiveDays);
        }
        int LeaveStartYear = DateTime.Parse(FromDate).Year;
        int LeaveEndYear = DateTime.Parse(ToDate).Year;
        if (intApplicationYear == 1)
        {
            if ((Convert.ToDateTime(FromDate) >= YearSettingFromDate) && (Convert.ToDateTime(ToDate) <= YearSettingToDate))
            {
            }
            else
            {
                Response.Clear();
                Response.Write("InvalidYear");
                Response.End();
                return;
            }
        }
        else
        {
            if ((Convert.ToDateTime(FromDate) >= YearSettingFromDate) && (Convert.ToDateTime(ToDate) <= YearSettingToDate))
            {
            }
            else
            {
                Response.Clear();
                Response.Write("InvalidDate");
                Response.End();
                return;
            }
        }
        Fromsession = Convert.ToString(Request.QueryString["Fromsession"]);
        Tosession = Convert.ToString(Request.QueryString["Tosession"]);
        LeaveTypeId = Convert.ToString(Request.QueryString["LeaveTypeId"]);
        EmployeeId = Convert.ToString(Request.QueryString["EmployeeId"]);
        gstrleavetypename = Convert.ToString(Request.QueryString["leavetypename"]);
        ds_EmployeeDetails = objLeaveApplicationEventData.GetEmployeeDetails(Convert.ToInt32(EmployeeId));
        gstrDateOfJoining = ds_EmployeeDetails.Tables[0].Rows[0]["DateOfJoining"].ToString();
        EmployeeCodeId = ds_EmployeeDetails.Tables[0].Rows[0]["Employee_Code_Id"].ToString();
        gstrSwipeCardNo = ds_EmployeeDetails.Tables[0].Rows[0]["SwipeCardNo"].ToString();
        if (Convert.ToString(Request.QueryString["selLeaveType"]) != "")
            selLeaveType = Convert.ToString(Request.QueryString["selLeaveType"]);
        if (DateTime.Parse(gstrDateOfJoining) <= DateTime.Parse(FromDate))
        {
            //Leave dates Applicable
            DataSet dsRequiredPeriod = new DataSet();
            if (LeaveTypeId != "Others" && (gstrleavetypename != "ESI" || gstrleavetypename != "Comp Off" || gstrleavetypename != "Restricted Holiday") && gstrleavetypename != "Restricted Holiday" && gstrleavetypename != "LOP")
            {
                dsRequiredPeriod = objLeaveApplicationEventData.GetApplicableAfterDays(Convert.ToInt32(EmployeeId), Convert.ToInt32(LeaveTypeId), FromDate);
                if (dsRequiredPeriod.Tables.Count > 0)
                {
                    dsRequiredPeriod.Tables[0].TableName = "Details";
                    if (dsRequiredPeriod.Tables[0].Rows[0]["RemainingNoOfLeaves"].ToString() == "Y")
                    {
                    }
                    else
                    {
                        //Leave dates not applicable
                        Response.Clear();
                        Response.Write("ApplicableAfterDays");
                        Response.End();
                    }
                }
            }
        }
        else
        {
            //Leave dates not applicable
            Response.Clear();
            Response.Write("DateOfJoining");
            Response.End();
        }
        if (LeaveTypeId == "")
        {
            Response.Clear();
            Response.Write("NoLeaveTypeId");
            Response.End();
        }
        DataSet dsDateExistsYear = new DataSet();
        if (intApplicationYear != 0)
        {
            dsDateExistsYear = objLeaveApplicationEventData.GetGivenDatesExistsYearsettings(FromDate, ToDate, intApplicationYear);
            if (dsDateExistsYear.Tables.Count > 0)
            {
                if (dsDateExistsYear.Tables[0].Rows.Count > 0)
                {
                    if (dsDateExistsYear.Tables[0].Rows[0][0].ToString() == "wrong")
                    {
                        Response.Clear();
                        Response.Write("InvalidApplicationDate");
                        Response.End();
                        return;
                    }
                }
            }
        }
        if (gstrleavetypename == "Leave")
        {
            //Check Leave From Date and To Date falls between any Leave Policy with current year
            DataSet dsLeavePolicyDatesExists = new DataSet();
            dsLeavePolicyDatesExists = objLeaveApplicationEventData.GetLeavePolicyDatesExists(Convert.ToInt32(EmployeeId), FromDate, ToDate, intlevelid);
            if (dsLeavePolicyDatesExists.Tables.Count > 0)
            {
                if (dsLeavePolicyDatesExists.Tables[0].Rows.Count > 0)
                {
                    if (Convert.ToInt32(dsLeavePolicyDatesExists.Tables[0].Rows[0]["PolicyCount"]) == 0)
                    {
                        Response.Clear();
                        Response.Write("NoLeavePolicyDates");
                        Response.End();
                        return;
                    }
                    else if (Convert.ToInt32(dsLeavePolicyDatesExists.Tables[0].Rows[0]["PolicyCount"]) == 2)
                    {
                        Response.Clear();
                        Response.Write("ExceedSuccessiveDays");
                        Response.End();
                        return;
                    }
                }
            }
        }
        int ShiftCount = CheckShift();
        if (ShiftCount == 0)
        {
            Response.Clear();
            Response.Write("ShiftNotAllocated");
            Response.End();
        }
        testvalue = 0;
        CalculateNumberOfDaysApplied();
    }
    /// <summary>
    /// Calculate Number of Days Applied for Leave
    /// </summary>
    public void CalculateNumberOfDaysApplied()
    {
        double dDays = 0.0, dTempDays = 0.0;
        int iDays = DateDifference(DateTime.Parse(FromDate), DateTime.Parse(ToDate));
        DataSet dsEmp = new DataSet();
        dsEmp = objLeaveApplicationEventData.GetEmployeeLevel(Convert.ToInt32(EmployeeId));
        int iEmployeeLevelID = 0;
        if (dsEmp != null && dsEmp.Tables.Count != 0 && dsEmp.Tables[0].Rows.Count != 0)
        {
            if (dsEmp.Tables[0].Rows[0]["levelID"] != DBNull.Value)
            {
                iEmployeeLevelID = Convert.ToInt32(dsEmp.Tables[0].Rows[0]["levelID"]);
            }
        }
        //Select Company
        org_levelsEventData getOrg = new org_levelsEventData();
        getOrg.GetOrgParentLevels_FromID(iEmployeeLevelID);
        DataSet ds_getOrg = new DataSet();
        ds_getOrg = getOrg.GetOrgParentLevels();
        int iCompanyID = 0;
        if (ds_getOrg != null)
        {
            if (ds_getOrg.Tables[0].Rows.Count > 0)
            {
                iCompanyID = Convert.ToInt32(ds_getOrg.Tables[0].Rows[0]["LevelID"].ToString());
            }
        }
        ShiftSettingsEventData getShift = new ShiftSettingsEventData();
        DataSet dsShiftSett = new DataSet();
        dsShiftSett = getShift.GetShiftSettingsByComId(iCompanyID);
        //End of select Company
        if (iDays > 0)
        {
            dDays = Double.Parse(iDays.ToString());
            if ((Fromsession == "Full day") && (Tosession == "First half"))
            {
                dDays = dDays - 0.5;
            }
            else if ((Fromsession == "First half") && (Tosession == "First half"))
            {
                dDays = dDays - 0.5;
            }
            else if ((Fromsession == "First half") && (Tosession == "-Select-"))
            {
                dDays = dDays - 0.5;
            }
            else if ((Fromsession == "Second half") && (Tosession == "Full day"))
            {
                dDays = dDays - 0.5;
            }
            else if ((Fromsession == "Second half") && (Tosession == "-Select-"))
            {
                dDays = dDays - 0.5;
            }
            else if ((Fromsession == "Second half") && (Tosession == "First half"))
            {
                if (FromDate != ToDate)
                {
                    dDays = dDays - 1;
                }
                else
                {
                    dDays = dDays - 0.5;
                }
            }
            else if ((Fromsession == "Second half") && (Tosession == "Second half"))
            {
                dDays = dDays - 0.5;
            }
        }
        else
        {
            dDays = Double.Parse(iDays.ToString());
        }
        dTempDays = dDays;
        //Check the given From Date or To Data falls on Weekly Off
        int WeeklyOffCountFromDate = 0;
        int WeeklyOffCountToDate = 0;
        WeeklyOffCountFromDate = WeeklyOffChecking(DateTime.Parse(FromDate), DateTime.Parse(FromDate), EmployeeCodeId);
        WeeklyOffCountToDate = WeeklyOffChecking(DateTime.Parse(ToDate), DateTime.Parse(ToDate), EmployeeCodeId);
        if (Convert.ToString(Session["packageID"]) != "")
        {
            /// Mtts client requested us to allow to choose from or todate as weekly off for maternity leave
            /// on 04-May-2022 11:00. As per that we included this condition.
            if (Convert.ToInt32(Session["packageID"]) == 169 && selLeaveType == "ML")
            {
            }
            else
            {
                if (WeeklyOffCountFromDate > 0)
                {
                    Response.Clear();
                    Response.Write("WeeklyOffFromDate");
                    Response.End();
                }
                if (WeeklyOffCountToDate > 0)
                {
                    Response.Clear();
                    Response.Write("WeeklyOffToDate");
                    Response.End();
                }
            }
        }
        if (LeaveTypeId == "Others" && gstrleavetypename == "ESI")
        {
            Response.Clear();
            Response.Write(dDays + "," + -1);
            Response.End();
            return;
        }
        if (LeaveTypeId == "Others" && gstrleavetypename != "ESI")
        {
            //Check the Given date range have holidays
            int HolidayCount = HolidayChecking(DateTime.Parse(FromDate), DateTime.Parse(ToDate), EmployeeCodeId);
            if (HolidayCount > 0)
            {
                Response.Clear();
                Response.Write("Holiday");
                Response.End();
            }
            //Check the Given date range have Weekly Off
            int WeeklyOffCount = WeeklyOffChecking(DateTime.Parse(FromDate), DateTime.Parse(ToDate), EmployeeCodeId);
            if (WeeklyOffCount > 0)
            {
                Response.Clear();
                Response.Write("WeeklyOff");
                Response.End();
            }
            Response.Clear();
            Response.Write(dDays + "," + -1);
            Response.End();
            return;
        }
        //To get the Leave Policy Details Based on EmployeeId and Leave Type Id
        ds_LeavePolicyDetails = objLeaveApplicationEventData.GetLeavePolicyDetailsLeaveApplication(Convert.ToInt32(EmployeeId), Convert.ToInt32(LeaveTypeId), DateTime.Parse(FromDate), DateTime.Parse(ToDate));
        if (ds_LeavePolicyDetails.Tables[0].Rows.Count > 0)
        {
            //  declaration for After weekly off
            if (ds_LeavePolicyDetails.Tables[0].Rows[0]["Count_AfterWeeklyOff"].ToString() == "True")
            {
                string Type = "A";
                ds_WeeklyOff = objLeaveApplicationEventData.GetShiftIdBeforeAfterWeekOff(Convert.ToInt32(EmployeeId), DateTime.Parse(FromDate), DateTime.Parse(ToDate), Type);
                if (ds_WeeklyOff.Tables[0].Rows.Count > 0)
                {
                    if (ds_WeeklyOff.Tables[0].Rows[0]["ShiftId"] != DBNull.Value)
                    {
                        if (Convert.ToString(ds_WeeklyOff.Tables[0].Rows[0]["ShiftId"]) == "-1")
                        {
                            if (Fromsession == "First half")
                            {
                                dDays = dDays + 1;
                                testvalue = 1;
                            }
                        }
                    }
                }
                int count = 0;
                count = CheckAdditionalWeekOff(DateTime.Parse(FromDate).AddDays(-1), gstrSwipeCardNo);
                if (count == 1 && testvalue != 1)
                {
                    if (Fromsession == "First half")
                    {
                        dDays = dDays + 1;
                        testvalue = 1;
                    }
                }
                //Check Whether the date is holiday  
                DataTable dtHoliday = new DataTable();
                dtHoliday = objHolidayEventData.CheckDateForHoliday(DateTime.Parse(FromDate).AddDays(-1).ToShortDateString(), EmployeeCodeId);
                if (dtHoliday.Rows.Count > 0 && testvalue != 1)
                {
                    dDays = dDays + 1;
                    testvalue = 5;
                }
            }
            // declaration for Before weekly off
            if (ds_LeavePolicyDetails.Tables[0].Rows[0]["Count_BeforeWeeklyOff"].ToString() == "True")
            {
                string Type = "B";
                ds_WeeklyOff = objLeaveApplicationEventData.GetShiftIdBeforeAfterWeekOff(Convert.ToInt32(EmployeeId), DateTime.Parse(FromDate), DateTime.Parse(ToDate), Type);
                if (ds_WeeklyOff.Tables[0].Rows.Count > 0)
                {
                    if (ds_WeeklyOff.Tables[0].Rows[0]["ShiftId"] != DBNull.Value)
                    {
                        if (Convert.ToString(ds_WeeklyOff.Tables[0].Rows[0]["ShiftId"]) == "-1")
                        {
                            if (Tosession == "Second half")
                            {
                                dDays = dDays + 1;
                                testvalue = 2;
                            }
                        }
                    }
                }
                int count = 0;
                count = CheckAdditionalWeekOff(DateTime.Parse(FromDate).AddDays(1), gstrSwipeCardNo);
                if (count == 1 && testvalue != 2)
                {
                    if (Tosession == "Second half")
                    {
                        dDays = dDays + 1;
                        testvalue = 2;
                    }
                }
                //Check Whether the date is holiday
                DataTable dtHoliday = new DataTable();
                dtHoliday = objHolidayEventData.CheckDateForHoliday(DateTime.Parse(FromDate).AddDays(1).ToShortDateString(), EmployeeCodeId);
                if ((dtHoliday.Rows.Count > 0) && testvalue != 2)
                {
                    dDays = dDays + 1;
                    testvalue = 6;
                }
            }
            if (ds_LeavePolicyDetails.Tables[0].Rows[0]["Count_Intervening"].ToString() == "True")
            {
                //test value 4 for Intervening yes
                DateTime dtmFromDate = DateTime.Parse(FromDate);
                DateTime dtmToDate = DateTime.Parse(ToDate);
                for (DateTime dtmDate = dtmFromDate; dtmDate <= dtmToDate; dtmDate = dtmDate.AddDays(1))
                {
                    int intShiftId = 0;
                    DataTable dtTempShift = new DataTable();
                    dtTempShift = objShiftExchangeEventData.SelectTemporaryShiftId(EmployeeCodeId, dtmDate.Year.ToString(), dtmDate.Month.ToString(), dtmDate.Day.ToString());
                    DataTable dtActualShift = new DataTable();
                    dtActualShift = objShiftExchangeEventData.SelectPermanentShiftId(EmployeeCodeId, dtmDate.Year.ToString(), dtmDate.Month.ToString(), dtmDate.Day.ToString());
                    if (dtTempShift.Rows.Count > 0)
                    {
                        if (dtTempShift.Rows[0][0] != DBNull.Value)
                        {
                            intShiftId = int.Parse(dtTempShift.Rows[0][0].ToString());
                        }
                    }
                    if (intShiftId == 0)
                    {
                        if (dtActualShift.Rows.Count > 0)
                        {
                            if (dtActualShift.Rows[0][0] != DBNull.Value)
                            {
                                intShiftId = int.Parse(dtActualShift.Rows[0][0].ToString());
                            }
                        }
                    }
                    int count = 0;
                    count = CheckAdditionalWeekOff(dtmDate, gstrSwipeCardNo);
                    if (count == 1)
                    {
                        intShiftId = -1;
                    }
                    //Check Whether the date is holiday
                    DataTable dtHoliday = new DataTable();
                    dtHoliday = objHolidayEventData.CheckDateForHoliday(dtmDate.ToShortDateString(), EmployeeCodeId);
                    if (dtHoliday.Rows.Count > 0 || intShiftId == -1)
                    {
                        testvalue = 4;
                        gintWeeklyOffHoliday = gintWeeklyOffHoliday + 1;
                    }
                }
            }
            else
            {
                // Intervening weekly off Temporary Table
                DateTime dtmFromDate = DateTime.Parse(FromDate);
                DateTime dtmToDate = DateTime.Parse(ToDate);
                bool blnLeaveOnSameDay = false;
                if (dtmFromDate == dtmToDate)
                {
                    blnLeaveOnSameDay = true;
                }
                int LoopCount = 1;
                for (DateTime dtmDate = dtmFromDate; dtmDate <= dtmToDate; dtmDate = dtmDate.AddDays(1))
                {
                    int intShiftId = 0;
                    DataTable dtTempShift = new DataTable();
                    dtTempShift = objShiftExchangeEventData.SelectTemporaryShiftId(EmployeeCodeId, dtmDate.Year.ToString(), dtmDate.Month.ToString(), dtmDate.Day.ToString());
                    DataTable dtActualShift = new DataTable();
                    dtActualShift = objShiftExchangeEventData.SelectPermanentShiftId(EmployeeCodeId, dtmDate.Year.ToString(), dtmDate.Month.ToString(), dtmDate.Day.ToString());
                    if (dtTempShift.Rows.Count > 0)
                    {
                        if (dtTempShift.Rows[0][0] != DBNull.Value)
                        {
                            intShiftId = int.Parse(dtTempShift.Rows[0][0].ToString());
                        }
                    }
                    if (intShiftId == 0)
                    {
                        if (dtActualShift.Rows.Count > 0)
                        {
                            if (dtActualShift.Rows[0][0] != DBNull.Value)
                            {
                                intShiftId = int.Parse(dtActualShift.Rows[0][0].ToString());
                            }
                        }
                    }
                    int count = 0;
                    count = CheckAdditionalWeekOff(dtmDate, gstrSwipeCardNo);
                    if (count == 1)
                    {
                        intShiftId = -1;
                    }
                    //Check Whether the date is holiday
                    DataTable dtHoliday = new DataTable();
                    dtHoliday = objHolidayEventData.CheckDateForHoliday(dtmDate.ToShortDateString(), EmployeeCodeId);
                    if (dtHoliday.Rows.Count > 0 || intShiftId == -1)
                    {
                        if (dtHoliday.Rows.Count > 0)
                        {
                            if (blnLeaveOnSameDay == true)
                            {
                                dDays = dDays - 1;
                                gintWeeklyOffHoliday = gintWeeklyOffHoliday + 1;
                                testvalue = 3;
                            }
                            else
                            {
                                if (LoopCount == 1)
                                {
                                    if (Fromsession == "First half")
                                    {
                                        dDays = dDays - 1;
                                        gintWeeklyOffHoliday = gintWeeklyOffHoliday + 1;
                                        testvalue = 3;
                                    }
                                }
                                else if (dtmDate == dtmToDate)
                                {
                                    if (Fromsession == "First half" && Tosession == "Second half")
                                    {
                                        dDays = dDays - 1;
                                        gintWeeklyOffHoliday = gintWeeklyOffHoliday + 1;
                                        testvalue = 3;
                                    }
                                    else if (Tosession == "First half" || Tosession == "Second half")
                                    {
                                        dDays = dDays - 0.5;
                                        gintWeeklyOffHoliday = gintWeeklyOffHoliday + 0.5;
                                        testvalue = 3;
                                    }
                                }
                                else
                                {
                                    dDays = dDays - 1;
                                    gintWeeklyOffHoliday = gintWeeklyOffHoliday + 1;
                                    testvalue = 3;
                                }
                            }
                        }
                        else
                        {
                            dDays = dDays - 1;
                            gintWeeklyOffHoliday = gintWeeklyOffHoliday + 1;
                            testvalue = 3;
                        }
                    }
                    LoopCount = LoopCount + 1;
                }
            }
        }
        // Finally write as Text
        if (gintWeeklyOffHoliday < dTempDays)
        {
            Response.Clear();
            Response.Write(dDays + "," + testvalue + "," + gintWeeklyOffHoliday);
            Response.End();
        }
        else
        {
            Response.Clear();
            Response.Write("HolidayWeeklyOff");
            Response.End();
        }
    }
    /// <summary>
    /// Find the difference between From Date and To Date
    /// </summary>
    /// <param name="d1"></param>
    /// <param name="d2"></param>
    /// <returns></returns>
    public int DateDifference(DateTime d1, DateTime d2)
    {
        int Days;
        TimeSpan ts = d2.Subtract(d1);
        if (ts.Days >= 0)
        {
            Days = (ts.Days) + 1;
        }
        else
        {
            Days = ts.Days;
        }
        return Days;
    }
    public int HolidayChecking(DateTime Fromdate, DateTime ToDate, string EmployeeCodeId)
    {
        //Check Whether the date is holiday
        DataTable dtHoliday = new DataTable();
        int intHolidayCount = 0;
        for (DateTime dtmDate = Fromdate; dtmDate <= ToDate; dtmDate = dtmDate.AddDays(1))
        {
            dtHoliday = objHolidayEventData.CheckDateForHoliday(dtmDate.ToShortDateString(), EmployeeCodeId);
            if (dtHoliday.Rows.Count > 0)
            {
                intHolidayCount = 1;
            }
            dtHoliday.Clear();
        }
        return intHolidayCount;
    }
    public int WeeklyOffChecking(DateTime Fromdate, DateTime ToDate, string EmployeeCodeId)
    {
        // Intervening weekly off Temporary Table
        DateTime dtmFromDate = Fromdate;
        DateTime dtmToDate = ToDate;
        //DataTable declaration for Temporary and Actual Shifts
        DataTable dtTempShift = new DataTable();
        DataTable dtActualShift = new DataTable();
        int intWeeklyOffCount = 0;
        for (DateTime dtmDate = dtmFromDate; dtmDate <= dtmToDate; dtmDate = dtmDate.AddDays(1))
        {
            int intShiftId = 0;
            try
            {
                dtTempShift = objShiftExchangeEventData.SelectTemporaryShiftId(EmployeeCodeId, dtmDate.Year.ToString(), dtmDate.Month.ToString(), dtmDate.Day.ToString());
            }
            catch
            {
                //
            }
            try
            {
                dtActualShift = objShiftExchangeEventData.SelectPermanentShiftId(EmployeeCodeId, dtmDate.Year.ToString(), dtmDate.Month.ToString(), dtmDate.Day.ToString());
            }
            catch
            {
                //
            }
            if (dtTempShift.Rows.Count > 0)
            {
                if (dtTempShift.Rows[0][0] != DBNull.Value)
                {
                    intShiftId = int.Parse(dtTempShift.Rows[0][0].ToString());
                }
            }
            if (intShiftId == 0)
            {
                if (dtActualShift.Rows.Count > 0)
                {
                    if (dtActualShift.Rows[0][0] != DBNull.Value)
                    {
                        intShiftId = int.Parse(dtActualShift.Rows[0][0].ToString());
                    }
                }
            }
            int count = 0;
            count = CheckAdditionalWeekOff(dtmDate, gstrSwipeCardNo);
            if (count == 1)
            {
                intShiftId = -1;
            }
            if (intShiftId == -1)
            {
                intWeeklyOffCount = 1;
            }
        }
        return intWeeklyOffCount;
    }
    // Function to check Additional Weekly Off
    public int CheckAdditionalWeekOff(DateTime dtmDate, string EmployeeId)
    {
        DateTime startOfMonth;
        string strStartOfMonth;
        int DaysToAdd, NthWeek = 0, TargetDayOfWeek = 0;
        string flag = "NE";
        try
        {
            DataTable dtAdditionalWeekOff = new DataTable();
            dtAdditionalWeekOff = objMonthClosure.GetAdditionalWeekOff(EmployeeId, dtmDate.ToShortDateString());
            if (dtAdditionalWeekOff.Rows.Count > 0)
            {
                foreach (DataRow drRow in dtAdditionalWeekOff.Rows)
                {
                    if (drRow["Addl_WeekOff_Day"] != DBNull.Value && drRow["Addl_WeekOff_Day"].ToString() != string.Empty)
                    {
                        TargetDayOfWeek = GetWeekDay(drRow["Addl_WeekOff_Day"].ToString());
                    }
                    if (drRow["Addl_WeekOff_Week"] != DBNull.Value && drRow["Addl_WeekOff_Week"].ToString() != string.Empty)
                    {
                        string strTargetDayOfWeek = drRow["Addl_WeekOff_Week"].ToString();
                        if (strTargetDayOfWeek == "First Week")
                        {
                            NthWeek = 1;
                        }
                        else if (strTargetDayOfWeek == "Second Week")
                        {
                            NthWeek = 2;
                        }
                        else if (strTargetDayOfWeek == "Third Week")
                        {
                            NthWeek = 3;
                        }
                        else if (strTargetDayOfWeek == "Fourth Week")
                        {
                            NthWeek = 4;
                        }
                        else if (strTargetDayOfWeek == "Fifth Week")
                        {
                            NthWeek = 5;
                        }
                    }
                    if (NthWeek != 0 && TargetDayOfWeek != 0)
                    {
                        strStartOfMonth = dtmDate.Month + "/01/" + dtmDate.Year;
                        startOfMonth = DateTime.Parse(strStartOfMonth);
                        string WeekDayName = startOfMonth.DayOfWeek.ToString();
                        int WeekDay = GetWeekDay(WeekDayName);
                        DaysToAdd = (7 - (WeekDay + (7 - TargetDayOfWeek)) % 7) % 7;
                        DateTime dtmResult = startOfMonth.AddDays(DaysToAdd + ((NthWeek - 1) * 7));
                        if (dtmResult == dtmDate)
                        {
                            flag = "E";
                        }
                    }
                }
            }
            if (flag == "E")
            {
                return (1);
            }
            else
            {
                return (0);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public int GetWeekDay(string WeekDayName)
    {
        int intWeekDay = 0;
        switch (WeekDayName)
        {
            case "Sunday":
                intWeekDay = 1;
                break;
            case "Monday":
                intWeekDay = 2;
                break;
            case "Tuesday":
                intWeekDay = 3;
                break;
            case "Wednesday":
                intWeekDay = 4;
                break;
            case "Thursday":
                intWeekDay = 5;
                break;
            case "Friday":
                intWeekDay = 6;
                break;
            case "Saturday":
                intWeekDay = 7;
                break;
        }
        return (intWeekDay);
    }
    public int CheckShift()
    {
        DataSet dsShift = new DataSet();
        DateTime dtmFromDate = DateTime.Parse(FromDate);
        DateTime dtmToDate = DateTime.Parse(ToDate);
        //intShiftCount 1-Shift Allocated 0-Not Allocated 
        int intShiftCount = 1;
        for (DateTime dtmDate = dtmFromDate; dtmDate <= dtmToDate; dtmDate = dtmDate.AddDays(1))
        {
            int mon = dtmDate.Month;
            int dat = dtmDate.Day;
            int yea = dtmDate.Year;
            dsShift = objPermissionEventData.GetShiftID(Convert.ToString(EmployeeCodeId), yea, mon, dat);
            if (dsShift != null)
            {
                if (dsShift.Tables.Count > 0)
                {
                    if (dsShift.Tables[0].Rows.Count > 0)
                    {
                        if (dsShift.Tables[0].Rows[0][0] != DBNull.Value)
                        {
                            if (Convert.ToInt16(dsShift.Tables[0].Rows[0][0]) == 0)
                            {
                                intShiftCount = 0;
                            }
                        }
                        else
                        {
                            intShiftCount = 0;
                        }
                    }
                }
            }
        }
        return intShiftCount;
    }
}
