using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Text;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.Util;
using System.IO;
using System.Web;
/********************************************************
 * Modified By      :   K.Gopinath
 * Modified Date    :   12-Dec-2013
 * Descriptions     :   Filter Movement on punches except the First In and Last out.
 *********************************************************/
public partial class Reports_TA_UI_Report_MovementsEntry : Base.BasePage
{
    #region Variable Declaration
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    string sv;
    HeaderInformation hdrTable = new HeaderInformation();
    GetReports rptEmp = new GetReports();
    Rpt_Common rptcommon = new Rpt_Common();
    cls_Common objCommon = new cls_Common();
    PunchTypesEventdata objPunchtype = new PunchTypesEventdata();
    EmployeeEventData objEmployee = new EmployeeEventData();
    string strFromdate, gstrDateFormat, strTodate, strInTime, strOutTime;
    ResourceManager rm;
    CultureInfo ci;
    int Cellvalcount = 0;
    int intIndividualUser = 0;
    string reportName, SheetName = "";
    int intFilter = 0;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!Page.IsPostBack)
        {
            base.CheckPages(0); 
            objCommon.menu("Attendance Reports", "41");
        }
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        if (!IsPostBack)
        {
            LoadStrings(ci);
            string strSetAlert = "";
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
            strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_EnterDate", ci) + "','" + rm.GetString("Alert_EnterToDate", ci) + "','" + rm.GetString("Alert_EnterFromTime", ci) + "','" + rm.GetString("Alert_EnterToTime", ci) + "');";
            strSetAlert = strSetAlert + "SetAlertCheckDate('" + rm.GetString("Alert_Validmonthdatefourdigit", ci) + "','" + rm.GetString("Alert_Validdatemonthfourdigit", ci) + "','" + rm.GetString("Alert_Invalidyearentry", ci) + "','" + rm.GetString("Alert_Invalidmonthentry", ci) + "','" + rm.GetString("Alert_Invaliddateentry", ci) + "','" + rm.GetString("Alert_IncorrectdateformatDDMONYYYY", ci) + "','" + rm.GetString("Alert_Entertime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "','" + rm.GetString("Alert_Invalidtimeformat1", ci) + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMM", ci) + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMMTT", ci) + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
            UserRights();
            if (Request.QueryString["Source"] != null)
            {
                if (Convert.ToString(Request.QueryString["Source"]) == "Email")
                {
                    divdateTime.Visible = false;
                    divTime.Visible = false;
                    trView.Visible = false;
                    trSave.Visible = true;
                }
            }
            else
            {
                divdateTime.Visible = true;
                divTime.Visible = true;
                trView.Visible = true;
                trSave.Visible = false;
            }
            if (Request.QueryString["ScheduleId"] != null)
            {
                string strGet = hdnReportFilters.Value;
                string strScript = "<script language='javascript' type='text/javascript'>";
                strScript += "GethiddenValue();";
                strScript += " </script>";
                this.RegisterStartupScript("setScript", strScript);
            }
            if (Session["parentLevelId"] != null || Convert.ToInt16(Session["parentLevelId"]) != 0)
            {
                Session["parentLevelId"] = null;
            }
            Hidden1.Value = Convert.ToString(Session["IDateFormat"]);
            Hidden2.Value = Convert.ToString(Session["ITimeFormat"]);
            BindingEmployeeCategoryandType();
            FillDesignation();
            BindPunchTypes();
            loadRepottype();
            ddlDesignation.PreRender += LB_PreRender;
            txtFromDate.Text = (Convert.ToDateTime(objCommon.CurrentMonthStartDate())).ToString(Convert.ToString(Session["MDateFormat"]));
            txtToDate.Text = (Convert.ToDateTime(System.DateTime.Now.ToShortDateString())).ToString(Convert.ToString(Session["MDateFormat"]));
            fillShift();
            if (Request.QueryString["ScheduleId"] != null)
            {
                DataSet ds = new DataSet();
                ds = rptEmp.GetEmailSchedulerFilterDetails(Convert.ToInt32(Request.QueryString["ScheduleId"]));
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        txtEmployeeName.Text = Convert.ToString(ds.Tables[0].Rows[0]["EmployeeName"]);
                        ddlEmpCatagory.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["EmployeeCategory"]);
                        ddlDesignation.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["DesignationID"]);
                        txtFromTime.Text = Convert.ToString(ds.Tables[0].Rows[0]["InTime"]);
                        txtToTime.Text = Convert.ToString(ds.Tables[0].Rows[0]["OutTime"]);
                        ddlreportType.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["employeeType"]);
                        txtEmployeeId.Text = Convert.ToString(ds.Tables[0].Rows[0]["EmployeeID"]);
                        ddlShift.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["shift"]);
                        ddlGender.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["Gender"]);
                        ddlEmpType.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["EmployeeType"]);
                        Session["EMailParentId"] = Convert.ToString(ds.Tables[0].Rows[0]["ParentID"]);
                        ddlreportType.SelectedIndex = Convert.ToInt32(ds.Tables[0].Rows[0]["AllSwipesReportType"]);
                        if (Convert.ToInt32(ddlreportType.SelectedIndex) != 0)
                            ddlreportType_SelectedIndexChanged(sender, e);
                        else
                        {
                            foreach (ListItem list in ddlPunchTypes.Items)
                            {
                                if (list.Text == Convert.ToString(ds.Tables[0].Rows[0]["punchTypeValue"]))
                                {
                                    sv = list.Value;
                                    break;
                                }
                            }
                            ddlPunchTypes.SelectedValue = sv;
                        }
                    }
                }
            }
            else
            {
                if (Convert.ToString(Session["hdnReportFilters"]) != string.Empty)
                {
                    string strhdnParameters = Convert.ToString(Session["hdnReportFilters"]);
                    string[] strArray = new string[50];
                    string[] strArray1 = new string[50];
                    strArray = strhdnParameters.Split(',');
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        string[] strTemp = new string[5];
                        strTemp = strArray[i].Split('=');
                        strArray1[i] = strTemp[1];
                    }
                    ddlShift.SelectedValue = Convert.ToString(strArray1[3]);
                    txtEmployeeName.Text = Convert.ToString(strArray1[4]).Replace("~", ",");
                    txtEmployeeId.Text = Convert.ToString(strArray1[2]).Replace("~", ",");
                    ddlEmpCatagory.SelectedValue = Convert.ToString(strArray1[6]);
                    ddlEmpType.SelectedValue = Convert.ToString(strArray1[7]);
                    ddlDesignation.SelectedValue = Convert.ToString(strArray1[12]);
                    ddlGender.SelectedValue = Convert.ToString(strArray1[13]);
                    ddlreportType.SelectedIndex = Convert.ToInt32(strArray1[16]);
                    if (Convert.ToInt32(ddlreportType.SelectedIndex) != 0)
                        ddlreportType_SelectedIndexChanged(sender, e);
                    else
                    {
                        foreach (ListItem list in ddlPunchTypes.Items)
                        {
                            if (list.Text == strArray1[17])
                            {
                                sv = list.Value;
                                break;
                            }
                        }
                        ddlPunchTypes.SelectedValue = sv;
                    }
                    Session["EMailParentId"] = Convert.ToInt16(strArray1[11]);
                }
            }
        }
        else
        {
            if (Request.QueryString["Source"] != null)
            {
                if (Convert.ToString(Request.QueryString["Source"]) == "Email")
                {
                    divdateTime.Visible = false;
                    divTime.Visible = false;
                    trView.Visible = false;
                    trSave.Visible = true;
                }
            }
            else
            {
                divdateTime.Visible = true;
                divTime.Visible = true;
                trView.Visible = true;
                trSave.Visible = false;
            }
        }
        intIndividualUser = objCheckUser.CheckIndividualUser(Convert.ToString(Session["UserGroupName"]));
        if (intIndividualUser >= 1)
        {
            PlaceHolderMovementsEntry.Controls.Clear();
            divEmployeeDetails.Visible = false;
        }
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        strFromdate = txtFromDate.Text;
        strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
        strTodate = txtToDate.Text;
        strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
        GetTimes();
        if (Convert.ToString(Page.MasterPageFile) == "TA_Popup_Report_MasterPage.master")
        {
            conHeader.Visible = false;
        }
        HndleCntrls_CustmrWise();
    }
    private void HndleCntrls_CustmrWise()
    {
        if (Convert.ToString(Session["packageID"]) != "")
        {
            if (Convert.ToString(Session["packageID"]) != "153")// 153- Veolia Client
            {
                Rptypelbl1.Style.Add("display", "none");
                Rptypelbl2.Style.Add("display", "none");
            }
        }
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Request.QueryString["Source"] != null)
        {
            this.Page.MasterPageFile = "TA_Popup_Report_MasterPage.master";
        }
    }
    #endregion
    #region User Defined Function
    /// <summary>
    /// Fills the designation in the drop down
    /// </summary>
    private void FillDesignation()
    {
        DataSet ds_designation = new DataSet();
        ds_designation = objEmployee.select_CommonDesignation();
        if (ds_designation != null)
        {
            if (ds_designation.Tables[0].Rows.Count > 0)
            {
                ddlDesignation.DataSource = ds_designation;
                ddlDesignation.DataTextField = "TitleName";
                ddlDesignation.DataValueField = "TitleId";
                ddlDesignation.DataBind();
            }
            ds_designation.Dispose();
        }
    }
    /// <summary>
    /// Bindings the employee category and type.
    /// </summary>
    private void BindingEmployeeCategoryandType()
    {
        DataSet dsCategory = new DataSet();
        dsCategory = objEmployee.SelectCategoryDetailsEmployee("");
        if (dsCategory != null)
        {
            if (dsCategory.Tables[0].Rows.Count > 0)
            {
                ddlEmpCatagory.DataSource = dsCategory;
                ddlEmpCatagory.DataTextField = "CategoryName";
                ddlEmpCatagory.DataValueField = "CategoryId";
                ddlEmpCatagory.DataBind();
            }
            dsCategory.Dispose();
        }
        DataSet dsType = new DataSet();
        dsType = objEmployee.SelectTypeDetailsEmployee("");
        if (dsType != null)
        {
            if (dsType.Tables[0].Rows.Count > 0)
            {
                ddlEmpType.DataSource = dsType;
                ddlEmpType.DataTextField = "TypeName";
                ddlEmpType.DataValueField = "TypeId";
                ddlEmpType.DataBind();
            }
            dsType.Dispose();
        }
    }
    /// <summary>
    /// To fill shift in the drop down
    /// </summary>
    public void fillShift()
    {
        ShiftEvenData getShift = new ShiftEvenData();
        DataSet ds = new DataSet();
        string strusergroup = string.Empty;
        ds = getShift.SelectShift(Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["LevelRights"]), 0);
        foreach (DataRow drRow in ds.Tables[0].Rows)
        {
            switch (Convert.ToString(drRow["Shift_Name"]))
            {
                case "No Shift":
                    drRow["Shift_Name"] = rm.GetString("Caption_NoShift", ci);
                    break;
            }
        }
        if (ds.Tables[0].Rows.Count > 0)
        {
            this.ddlShift.DataSource = ds;
            this.ddlShift.DataTextField = "Shift_Name";
            this.ddlShift.DataValueField = "Shift_Id";
            this.ddlShift.DataBind();
        }
        ListItem lstItem = new ListItem();
        lstItem.Value = "0";
        lstItem.Text = rm.GetString("ExportData_All", ci);
        ddlShift.Items.Insert(0, lstItem);
    }
    /// <summary>
    /// Validate the FromDate
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void ValidateFromDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            strFromdate = txtFromDate.Text;
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
            DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date);
            DateTime dtFromDate = DateTime.Parse(strFromdate);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day <= 0)
                e.IsValid = true;
            else
            {
                base.Message(rm.GetString("Alert_EnterValidFromDate", ci));
                e.IsValid = false;
            }
        }
        catch
        {
        }
    }
    /// <summary>
    /// Validate the ToDate
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void ValidateToDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strTodate = txtToDate.Text;
            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
            DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date);
            DateTime dtFromDate = DateTime.Parse(strTodate);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day <= 0)
                e.IsValid = true;
            else
            {
                base.Message(rm.GetString("Alert_EnterValidToDate", ci));
                e.IsValid = false;
            }
        }
        catch
        { }
    }
    /// <summary>
    /// Compare ToDate with FromDate
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void CompareDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            strFromdate = txtFromDate.Text;
            strTodate = txtToDate.Text;
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
            DateTime dtToDate = DateTime.Parse(strTodate);
            DateTime dtFromDate = DateTime.Parse(strFromdate);
            TimeSpan diff = dtToDate.Subtract(dtFromDate);
            int day = diff.Days;
            if (day >= 0)
                e.IsValid = true;
            else
            {
                base.Message(rm.GetString("Msg_SelectvaliddatecomparewithFromdate", ci));
                e.IsValid = false;
            }
        }
        catch
        { }
    }
    /// <summary>
    /// Getting Information from resource file and display it in label
    /// </summary>
    /// <param name="ci"></param>
    private void LoadStrings(CultureInfo ci)
    {
        lblFilter.Text = rm.GetString("lblFilter", ci);
        btn_View.Text = "&nbsp;&nbsp;" + rm.GetString("btn_View", ci);
        lblAllSwipesReport.Text = rm.GetString("Reports_AllSwipes_AllSwipesReport", ci);
        lblShift.Text = rm.GetString("Menu_Reports_Shift", ci);
        lblEmployeeName.Text = rm.GetString("email_EmployeeName", ci);
        lblEmployeeId.Text = rm.GetString("email_EmployeeId", ci);
        lblEmpCategory.Text = rm.GetString("Master_Category", ci);
        lblEmpType.Text = rm.GetString("Master_Type", ci);
        lblTimePeriod.Text = rm.GetString("Reports_TimePeriod", ci);
        lblFrom.Text = rm.GetString("email_FromDate", ci);
        lblTo.Text = rm.GetString("email_ToDate", ci);
        lblFromTime.Text = rm.GetString("AllSwipes_FromTime", ci);
        lblToTime.Text = rm.GetString("AllSwipes_ToTime", ci);
        RequiredFieldValidator1.ErrorMessage = rm.GetString("Alert_SelectShift", ci);
        reqFromDate.ErrorMessage = rm.GetString("Alert_PleaseselectFromDate", ci);
        reqToDate.ErrorMessage = rm.GetString("Alert_PleaseselectToDate", ci);
        ddlEmpCatagory.Items[0].Text = rm.GetString("Select", ci);
        ddlEmpType.Items[0].Text = rm.GetString("Select", ci);
        lblDesignation.Text = rm.GetString("EmployeeMaster_Designation", ci);
        lblGender.Text = rm.GetString("EmployeeMaster_Gender", ci);
        ddlGender.Items[0].Text = rm.GetString("Caption_Both", ci);
        ddlGender.Items[1].Text = rm.GetString("rbtn_EmployeeMaster_Male", ci);
        ddlGender.Items[2].Text = rm.GetString("rbtn_EmployeeMaster_Female", ci);
        ddlDesignation.Items[0].Text = rm.GetString("ExportData_All", ci);
        btnSave.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btnBack.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Cancel", ci);
        lblDateFilter.Text = rm.GetString("lbl_DateFilter", ci);
        btn_Export.Text = "&nbsp;&nbsp;" + rm.GetString("btn_ExporttoExcel", ci);
        rbCustom.Text = rm.GetString("rb_Custom", ci);
        rbpreviousMonth.Text = rm.GetString("rb_PreviousMonth", ci);
        rbpreviousweek.Text = rm.GetString("rb_PreviousWeek", ci);
        rbThisMonth.Text = rm.GetString("rb_ThisMonth", ci);
        rbThisweek.Text = rm.GetString("rb_ThisWeek", ci);
        lbl_ReportType.Text = rm.GetString("EmployeeInOutReportType", ci);
        lblPunchtypes.Text = rm.GetString("Master_Punch", ci);

    }
    private void loadRepottype()
    {
        try
        {
            ddlreportType.Items.Clear();
            ddlreportType.Items.Add(new ListItem(rm.GetString("AllPunches", ci), "All"));
            ddlreportType.Items.Add(new ListItem(rm.GetString("FirstandLastPunches", ci), "First&LastPunchs"));
            ddlreportType.Items.Add(new ListItem(rm.GetString("WithMatchedPunches", ci), "MatchedPunchYes"));
            ddlreportType.Items.Add(new ListItem(rm.GetString("WithoutMatchedPunches", ci), "MatchedPunchNo"));
            ddlreportType.Items.Add(new ListItem(rm.GetString("OtherPunches", ci), "OtherPunches"));
        }
        catch (Exception ex)
        {
            throw;
        }
    }
    /// <summary>
    /// To check rights for the logged in user
    /// </summary>
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(41, Convert.ToInt32(Session["userGroup_Id"]), "viewPrv");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] == DBNull.Value || Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) == 1)
            {
                btn_View.Visible = true;
            }
            else
            {
                btn_View.Visible = false;
            }
        }
        else
        {
            Response.Redirect("ReportsLandingPage.aspx");
        }
    }
    private void BindPunchTypes()
    {
        DataSet dsPunchtypes = new DataSet();
        dsPunchtypes = objPunchtype.SelectPunchTypeDetailsForMovementsEntry();
        if (dsPunchtypes.Tables.Count > 0)
        {
            if (dsPunchtypes.Tables[0].Rows.Count > 0)
            {
                ddlPunchTypes.DataSource = dsPunchtypes;
                ddlPunchTypes.DataTextField = "PunchType";
                ddlPunchTypes.DataValueField = "DataValueField";
                ddlPunchTypes.DataBind();
            }
        }
        ListItem lstItem = new ListItem();
        lstItem.Value = "0";
        lstItem.Text = rm.GetString("ExportData_All", ci);
        ddlPunchTypes.Items.Insert(0, lstItem);
    }
    /// <summary>
    /// To apply tooltip for the designation in the drop down
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void LB_PreRender(object sender, System.EventArgs e)
    {
    }
    private void GetTimes()
    {
        if (txtFromTime.Text != string.Empty)
        {
            strFromdate = txtFromDate.Text;
            strInTime = txtFromTime.Text;
            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
        }
        else
        {
            strFromdate = txtFromDate.Text;
            strInTime = "12:00:00 AM";
            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
        }
        if (txtToTime.Text != string.Empty)
        {
            strTodate = txtToDate.Text;
            strOutTime = txtToTime.Text;
            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
        }
        else
        {
            strTodate = txtToDate.Text;
            strOutTime = "11:59:59 PM";
            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
        }
    }
    #endregion
    #region Button Event
    protected void btn_View_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid == true)
            {
                if (txtFromTime.Text != "" && txtToTime.Text != "")
                {
                    if (Convert.ToDateTime(txtFromTime.Text) > Convert.ToDateTime(txtToTime.Text))
                    {
                        base.Message(rm.GetString("Alert_FromTimeGreater", ci));
                        return;
                    }
                }
                if (ddlreportType.SelectedIndex == 0)
                    intFilter = 0;
                else if (ddlreportType.SelectedIndex == 1)
                    intFilter = 1;
                else if (ddlreportType.SelectedIndex == 2)
                    intFilter = 2;
                else if (ddlreportType.SelectedIndex == 3)
                    intFilter = 3;
                else if (ddlreportType.SelectedIndex == 4)
                    intFilter = 4;
                string dtFrom = strFromdate;
                string dtTo = strTodate;
                string dtIntime = strInTime;
                string dtOutTime = strOutTime;
                int shft = 0;
                string Level = string.Empty;
                string employeeName = string.Empty;
                string empName = string.Empty;
                string empId = string.Empty;
                shft = Convert.ToInt16(this.ddlShift.SelectedValue);
                if (intIndividualUser >= 1)
                {
                    empName = string.Empty;
                    Level = string.Empty;
                    empId = Convert.ToString(Session["EmployeeCodeId"]);
                }
                else
                {
                    empName = Convert.ToString(txtEmployeeName.Text);
                    empId = Convert.ToString(txtEmployeeId.Text.Trim());
                    employeeName = this.txtEmployeeName.Text.Replace("'", "''");
                    Level = rptcommon.getNodes(Convert.ToInt16(Session["parentLevelId"]), Convert.ToString(Session["LevelRights"]));
                }
                string punchTypeValue = "All";
                if (ddlPunchTypes.Visible == true)
                {
                    punchTypeValue = Convert.ToString(ddlPunchTypes.SelectedItem.Text);
                }
                StringBuilder strScript = new StringBuilder();
                strScript.Append("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
                strScript.Append("<meta http-equiv='X-UA-Compatible' content='IE=edge' />");
                strScript.Append("<script language='javascript'>");
                DataSet dsEmp = new DataSet();
                string strEmpAutoId = "0";
                if (Session["ReportsBasedOn"] != null)
                {
                    if (Convert.ToString(Session["ReportsBasedOn"]) == "False")
                    {
                        strEmpAutoId = "0";
                    }
                    else
                    {
                        strEmpAutoId = Convert.ToString(Session["EmpAutoID"]);
                    }
                }
                string reportName = "AllSwipes";
                if (Convert.ToInt16(ddlreportType.SelectedIndex) == 0 || Convert.ToInt16(ddlreportType.SelectedIndex) == 1 || Convert.ToInt16(ddlreportType.SelectedIndex) == 3)
                {
                    dsEmp = rptEmp.SelectMovementsEntryReport(empId, Level, dtFrom, dtTo, employeeName, shft, Convert.ToInt32(ddlEmpCatagory.SelectedValue), Convert.ToInt32(ddlEmpType.SelectedValue), Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), empId, Convert.ToInt16(Session["parentLevelId"]), Convert.ToInt32(ddlDesignation.SelectedValue), ddlGender.SelectedValue, dtIntime, dtOutTime, Convert.ToInt16(ddlreportType.SelectedIndex), punchTypeValue, Convert.ToInt64(strEmpAutoId), 0);
                    reportName = "AllSwipes";
                }
                else
                {
                    dsEmp = rptEmp.SelectMatchedMovementsEntryReport(empId, Level, dtFrom, dtTo, employeeName, shft, Convert.ToInt32(ddlEmpCatagory.SelectedValue), Convert.ToInt32(ddlEmpType.SelectedValue), Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), empId, Convert.ToInt16(Session["parentLevelId"]), Convert.ToInt32(ddlDesignation.SelectedValue), ddlGender.SelectedValue, dtIntime, dtOutTime, Convert.ToInt16(ddlreportType.SelectedIndex), Convert.ToInt64(strEmpAutoId), 0);
                    reportName = "MatchedAllSwipes";
                }
                if (dsEmp.Tables["Table"].Rows.Count == 0)
                {
                    string strMsg;
                    strMsg = "<script language='javascript'>";
                    strMsg = strMsg + "alert('" + rm.GetString("Alert_Thereisnorecord", ci) + "');window.close();";
                    strMsg = strMsg + "</script>";
                    Page.RegisterStartupScript("Display Message", strMsg);
                    return;
                }
                else
                {
                    Session["AllSwipesData"] = dsEmp;
                    strScript.Append("window.open('TA_UI_Report_ReportViewer.aspx?ReportName=" + reportName + "&punchTypeValue=" + punchTypeValue
                           + "&dtFrom=" + dtFrom
                           + "&dtTo=" + dtTo
                           + "&InTime=" + dtIntime
                           + "&OutTime=" + dtOutTime
                           + "&empid=" + empId
                           + "&shft=" + shft
                           + "&EmpName=" + employeeName
                           + "&Level=" + Level
                           + "&EmpCategory=" + Convert.ToInt32(ddlEmpCatagory.SelectedValue)
                           + "&EmpType=" + Convert.ToInt32(ddlEmpType.SelectedValue)
                           + "&ParentLevelId=" + Convert.ToInt16(Session["parentLevelId"])
                           + "&UserGroupName=" + Convert.ToString(Session["UserGroupName"])
                           + "&GradeLevel=" + Convert.ToString(Session["GradeLevel"])
                           + "&SwipeCard=" + empId
                           + "&Designation=" + Convert.ToInt32(ddlDesignation.SelectedValue)
                           + "&Gender=" + ddlGender.SelectedValue
                           + "&rptType=" + ((rptType1.Checked) ? 0 : 1)
                           + "&AllSwipesReportType=" + intFilter + "','','left=10px, top=60px, width=1000px, height=600px, scrollbars=yes, status=no, resizable=yes');");
                }
                strScript.Append("</script>");
                Response.Write(strScript);
            }
        }
        catch
        { }
    }
    protected void btn_Export_Click(object sender, EventArgs e)
    {
        try
        {
            DataSet dsEmp = new DataSet();
            string strEmpAutoId = "0";
            if (Page.IsValid == true)
            {
                if (Session["ReportsBasedOn"] != null)
                {
                    if (Convert.ToString(Session["ReportsBasedOn"]) == "False")
                        strEmpAutoId = "0";
                    else
                        strEmpAutoId = Convert.ToString(Session["EmpAutoID"]);
                }
                string dtFrom = "", dtTo = "";
                if (txtFromDate.Text != "")
                    dtFrom = strFromdate;
                if (txtToDate.Text != "")
                    dtTo = strTodate;
                string dtIntime = strInTime;
                string dtOutTime = strOutTime;
                string empName = "", empId = "";
                string employeeName = string.Empty;
                string Level = string.Empty;
                if (intIndividualUser >= 1)
                {
                    empName = string.Empty;
                    Level = string.Empty;
                    empId = Convert.ToString(Session["EmployeeCodeId"]);
                }
                else
                {
                    empName = Convert.ToString(txtEmployeeName.Text);
                    empId = Convert.ToString(txtEmployeeId.Text).Trim();
                    employeeName = this.txtEmployeeName.Text.Replace("'", "''");
                    Level = rptcommon.getNodes(Convert.ToInt16(Session["parentLevelId"]), Convert.ToString(Session["LevelRights"]));
                }
                string punchTypeValue = "All";
                if (ddlPunchTypes.Visible == true)
                    punchTypeValue = Convert.ToString(ddlPunchTypes.SelectedItem.Text);
                int shft = 0;
                shft = Convert.ToInt16(this.ddlShift.SelectedValue);
                switch (ddlreportType.SelectedIndex)
                {
                    case 0:
                        dsEmp = rptEmp.SelectMovementsEntryReport(empId, Level, dtFrom, dtTo, employeeName, shft, Convert.ToInt32(ddlEmpCatagory.SelectedValue), Convert.ToInt32(ddlEmpType.SelectedValue), Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), empId, Convert.ToInt16(Session["parentLevelId"]), Convert.ToInt32(ddlDesignation.SelectedValue), Convert.ToString(ddlGender.SelectedValue), dtIntime, dtOutTime, intFilter, punchTypeValue, Convert.ToInt64(strEmpAutoId), 1);
                        break;
                    case 1:
                        dsEmp = rptEmp.SelectMovementsEntryReport(empId, Level, dtFrom, dtTo, employeeName, shft, Convert.ToInt32(ddlEmpCatagory.SelectedValue), Convert.ToInt32(ddlEmpType.SelectedValue), Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), empId, Convert.ToInt16(Session["parentLevelId"]), Convert.ToInt32(ddlDesignation.SelectedValue), Convert.ToString(ddlGender.SelectedValue), dtIntime, dtOutTime, intFilter, punchTypeValue, Convert.ToInt64(strEmpAutoId), 1);
                        break;
                    case 2:
                        dsEmp = rptEmp.SelectMatchedMovementsEntryReport(empId, Level, dtFrom, dtTo, employeeName, shft, Convert.ToInt32(ddlEmpCatagory.SelectedValue), Convert.ToInt32(ddlEmpType.SelectedValue), Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), empId, Convert.ToInt16(Session["parentLevelId"]), Convert.ToInt32(ddlDesignation.SelectedValue), Convert.ToString(ddlGender.SelectedValue), dtIntime, dtOutTime, intFilter, Convert.ToInt64(strEmpAutoId), 1);
                        break;
                    case 3:
                        dsEmp = rptEmp.SelectMovementsEntryReport(empId, Level, dtFrom, dtTo, employeeName, shft, Convert.ToInt32(ddlEmpCatagory.SelectedValue), Convert.ToInt32(ddlEmpType.SelectedValue), Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), empId, Convert.ToInt16(Session["parentLevelId"]), Convert.ToInt32(ddlDesignation.SelectedValue), Convert.ToString(ddlGender.SelectedValue), dtIntime, dtOutTime, intFilter, punchTypeValue, Convert.ToInt64(strEmpAutoId), 1);
                        break;
                    case 4:
                        dsEmp = rptEmp.SelectMatchedMovementsEntryReport(empId, Level, dtFrom, dtTo, employeeName, shft, Convert.ToInt32(ddlEmpCatagory.SelectedValue), Convert.ToInt32(ddlEmpType.SelectedValue), Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), empId, Convert.ToInt16(Session["parentLevelId"]), Convert.ToInt32(ddlDesignation.SelectedValue), Convert.ToString(ddlGender.SelectedValue), dtIntime, dtOutTime, intFilter, Convert.ToInt64(strEmpAutoId), 1);
                        break;
                }
                if (dsEmp.Tables["Table"] == null || dsEmp.Tables["Table"].Rows.Count == 0)
                {
                    Message(rm.GetString("Alert_Thereisnorecord", ci));
                    return;
                }
                else
                {
                    Session["Export_ExcelData"] = dsEmp;
                    Response.Redirect("Excel_Export.aspx?ReportName=All Swipes&FrmDate=" + Convert.ToString(txtFromDate.Text) + "&toDate=" + Convert.ToString(txtToDate.Text) + "");
                    //ExportExcel(dsEmp);
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid == true)
            {
                if (txtFromTime.Text != "" && txtToTime.Text != "")
                {
                    if (Convert.ToDateTime(txtFromTime.Text) > Convert.ToDateTime(txtToTime.Text))
                    {
                        base.Message(rm.GetString("Alert_FromTimeGreater", ci));
                        return;
                    }
                }
                string dtFrom = strFromdate;
                string dtTo = strTodate; string dtIntime = strInTime;
                string dtOutTime = strOutTime;
                int shft = 0;
                string Level = string.Empty;
                string employeeName = string.Empty;
                string empName = string.Empty;
                string empId = string.Empty;
                shft = Convert.ToInt16(this.ddlShift.SelectedValue);
                if (intIndividualUser >= 1)
                {
                    empName = string.Empty;
                    Level = string.Empty;
                    empId = Convert.ToString(Session["EmployeeCodeId"]);
                }
                else
                {
                    empName = Convert.ToString(txtEmployeeName.Text);
                    empId = Convert.ToString(txtEmployeeId.Text.Trim());
                    employeeName = this.txtEmployeeName.Text.Replace("'", "''");
                    Level = rptcommon.getNodes(Convert.ToInt16(Session["parentLevelId"]), Convert.ToString(Session["LevelRights"]));
                    if (Level.Contains(","))
                    {
                        Level = Level.Replace(",", "~");
                    }
                    if (employeeName.Contains(","))
                    {
                        employeeName = employeeName.Replace(",", "~");
                    }
                    if (empId.Contains(","))
                    {
                        empId = empId.Replace(",", "~");
                    }
                }
                StringBuilder strFilters = new StringBuilder();
                if (ddlreportType.SelectedIndex == 1 || ddlreportType.SelectedIndex == 3 || ddlreportType.SelectedIndex == 2 || ddlreportType.SelectedIndex == 4)
                {
                    strFilters.Append("@fromDate=" + " Duration_From " + ",");
                    strFilters.Append("@ToDate=" + " Duration_To " + ",");
                    strFilters.Append("@EmployeeID=" + empId + ",");
                    strFilters.Append("@shift=" + shft + ",");
                    strFilters.Append("@EmployeeName=" + employeeName + ",");
                    strFilters.Append("@LevelID=" + Level + ",");
                    strFilters.Append("@employeeCategory=" + Convert.ToInt32(ddlEmpCatagory.SelectedValue) + ",");
                    strFilters.Append("@employeeType=" + Convert.ToInt32(ddlEmpType.SelectedValue) + ",");
                    strFilters.Append("@UserGroupName=" + Convert.ToString(Session["UserGroupName"]) + ",");
                    strFilters.Append("@GradeLevel=" + Convert.ToString(Session["GradeLevel"]) + ",");
                    strFilters.Append("@EmpSwipeCard=" + empId + ",");
                    strFilters.Append("@ParentId=" + Convert.ToInt32(Session["parentLevelId"]) + ",");
                    strFilters.Append("@DesignationID=" + Convert.ToInt32(ddlDesignation.SelectedValue) + ",");
                    strFilters.Append("@Gender=" + ddlGender.SelectedValue + ",");
                    strFilters.Append("@InTime=" + dtIntime + ",");
                    strFilters.Append("@OutTime=" + dtOutTime + ",");
                    strFilters.Append("@AllSwipesReportType=" + ddlreportType.SelectedIndex + ",");
                    strFilters.Append("@EmpAutoId=0" + ",");
                    strFilters.Append("@ExportType=0");
                }
                else
                {
                    strFilters.Append("@fromDate=" + " Duration_From " + ",");
                    strFilters.Append("@ToDate=" + " Duration_To " + ",");
                    strFilters.Append("@EmployeeID=" + empId + ",");
                    strFilters.Append("@shift=" + shft + ",");
                    strFilters.Append("@EmployeeName=" + employeeName + ",");
                    strFilters.Append("@LevelID=" + Level + ",");
                    strFilters.Append("@employeeCategory=" + Convert.ToInt32(ddlEmpCatagory.SelectedValue) + ",");
                    strFilters.Append("@employeeType=" + Convert.ToInt32(ddlEmpType.SelectedValue) + ",");
                    strFilters.Append("@UserGroupName=" + Convert.ToString(Session["UserGroupName"]) + ",");
                    strFilters.Append("@GradeLevel=" + Convert.ToString(Session["GradeLevel"]) + ",");
                    strFilters.Append("@EmpSwipeCard=" + empId + ",");
                    strFilters.Append("@ParentId=" + Convert.ToInt32(Session["parentLevelId"]) + ",");
                    strFilters.Append("@DesignationID=" + Convert.ToInt32(ddlDesignation.SelectedValue) + ",");
                    strFilters.Append("@Gender=" + ddlGender.SelectedValue + ",");
                    strFilters.Append("@InTime=" + dtIntime + ",");
                    strFilters.Append("@OutTime=" + dtOutTime + ",");
                    strFilters.Append("@AllSwipesReportType=" + ddlreportType.SelectedIndex + ",");
                    strFilters.Append("@punchTypeValue=" + ddlPunchTypes.SelectedItem.Text + ",");
                    strFilters.Append("@EmpAutoId=0" + ",");
                    strFilters.Append("@ExportType=0");
                }
                Session["hdnReportFilters"] = string.Empty;
                Session["hdnReportFilters"] = strFilters;
                string strScript = "<script language=\"javascript\">";
                strScript = strScript + "{window.top.parent.document.getElementById('ctl00_BodyContentPH_hdnFilters').value='" + strFilters + "'; window.top.parent.document.getElementById('ctl00_BodyContentPH_hdnCompanyId').value='" + Convert.ToInt32(Session["parentLevelId"]) + "';}";
                strScript = strScript + "parent.$('#myModalReport').modal('hide');";
                strScript = strScript + "</script>";
                Response.Write(strScript);
            }
        }
        catch
        {
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string strMsg;
        strMsg = "<script language='javascript'>";
        strMsg = strMsg + "parent.$('#myModalReport').modal('hide');";
        strMsg = strMsg + "</script>";
        Page.RegisterStartupScript("close report window", strMsg);
        return;
    }
    #endregion
    protected void ddlreportType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlreportType.SelectedValue != "All")
        {
            ddlPunchTypes.Enabled = false;
            lblPunchtypes.Enabled = false;
            ddlPunchTypes.Items.Clear();
            BindPunchTypes();
        }
        else
        {
            ddlPunchTypes.Enabled = true;
            lblPunchtypes.Enabled = true;
            ddlPunchTypes.Items.Clear();
            BindPunchTypes();
        }
    }
    #region Export Excel
    protected void ExportExcel(DataSet dsEmp)
    {
        DataTable dtLevelName = new DataTable();
        if (Convert.ToInt16(Session["parentLevelId"]) != 0)
        {
            dtLevelName = rptEmp.getHeader(Convert.ToInt16(Session["parentLevelId"]));
        }
        string NewTimeFormat = string.Empty;
        string TimeFormat = Convert.ToString(Session["ITimeFormat"]);
        if (TimeFormat == "HH:mm")
        {
            NewTimeFormat = "HH:mm";
        }
        else
        {
            NewTimeFormat = "hh:mm tt";
        }
        if (ddlreportType.SelectedIndex == 0)
        {
            SheetName = "All Swipes Report";
            reportName = "AllSwipesReport";
        }
        else if (ddlreportType.SelectedIndex == 1)
        {
            SheetName = "All Swipes First & Last Punch Details";
            reportName = "AllSwipesFirst&LastPunchDetails";
        }
        else if (ddlreportType.SelectedIndex == 2)
        {
            SheetName = "All Swipes Report With Matched Punches";
            reportName = "AllSwipesReportWithMatchedPunches";
        }
        else if (ddlreportType.SelectedIndex == 3)
        {
            SheetName = "All Swipes Report Without Matched Punches";
            reportName = "AllSwipesReportWithoutMatchedPunches";
        }
        else if (ddlreportType.SelectedIndex == 4)
        {
            SheetName = "Other All Swipes Details";
            reportName = "OtherAllSwipesDetails";
        }
        HSSFWorkbook workbook = new HSSFWorkbook();
        ISheet sheet = workbook.CreateSheet(SheetName);
        int rowIndex = 0;
        NPOI.SS.UserModel.IRow row = sheet.CreateRow(rowIndex);
        HSSFFont hFont = (HSSFFont)workbook.CreateFont();
        hFont.Boldweight = (short)FontBoldWeight.Bold;
        hFont.Color = HSSFColor.Maroon.Index;
        hFont.FontHeightInPoints = 10;
        HSSFCellStyle hStyle = (HSSFCellStyle)workbook.CreateCellStyle();
        hStyle.SetFont(hFont);
        hStyle.FillForegroundColor = IndexedColors.White.Index;
        hStyle.FillPattern = FillPattern.SolidForeground;
        hStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
        hStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
        hStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
        hStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
        HSSFCellStyle MainHStyle = (HSSFCellStyle)workbook.CreateCellStyle();
        MainHStyle.SetFont(hFont);
        MainHStyle.Alignment = HorizontalAlignment.Center;
        HSSFCellStyle IStyle = (HSSFCellStyle)workbook.CreateCellStyle();
        IStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
        IStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
        IStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
        IStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
        if (dtLevelName.Rows.Count > 0)
        {
            row.CreateCell(0).SetCellValue(Convert.ToString(dtLevelName.Rows[0][0]));
            sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 0, Cellvalcount));
            row.GetCell(0).CellStyle = MainHStyle;
            rowIndex++;
            row = sheet.CreateRow(rowIndex);
        }
        row.CreateCell(0).SetCellValue(SheetName + " From - " + Convert.ToString(txtFromDate.Text) + " - " + " To - " + Convert.ToString(txtToDate.Text));
        row.GetCell(0).CellStyle = MainHStyle;
        sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 0, Cellvalcount));
        rowIndex++;
        row = sheet.CreateRow(rowIndex);
        rowIndex++;
        row = sheet.CreateRow(rowIndex);
        if (ddlreportType.SelectedIndex == 0)
        {
            row.CreateCell(0).SetCellValue("Sl.No");
            row.CreateCell(1).SetCellValue("Employee Id");
            row.CreateCell(2).SetCellValue("Employee Name");
            row.CreateCell(3).SetCellValue("Designation");
            row.CreateCell(4).SetCellValue("Level Name");
            row.CreateCell(5).SetCellValue("Gender");
            row.CreateCell(6).SetCellValue("Shift Name");
            row.CreateCell(7).SetCellValue("Date");
            row.CreateCell(8).SetCellValue("In Reader ID");
            row.CreateCell(9).SetCellValue("In Punch Type");
            row.CreateCell(10).SetCellValue("In Date");
            row.CreateCell(11).SetCellValue("In Time");
            row.CreateCell(12).SetCellValue("Out Reader ID");
            row.CreateCell(13).SetCellValue("Out Punch Type");
            row.CreateCell(14).SetCellValue("Out Date");
            row.CreateCell(15).SetCellValue("Out Time");
            row.CreateCell(16).SetCellValue("Remarks");
            for (int i = 0; i <= 16; i++)
            {
                row.GetCell(i).CellStyle = hStyle;
            }
            rowIndex++;
            string strPre = string.Empty;
            Int32 SlNo = 0;
            foreach (DataRow DR in dsEmp.Tables[0].Rows)
            {
                row = sheet.CreateRow(rowIndex);
                if (strPre == string.Empty || strPre != Convert.ToString(DR["Employee Id"]))
                {
                    SlNo++;
                    row.CreateCell(0).SetCellValue(SlNo);
                }
                else
                {
                    row.CreateCell(0).SetCellValue("");
                }
                row.CreateCell(1).SetCellValue(Convert.ToString(DR["Employee Id"]));
                row.CreateCell(2).SetCellValue(Convert.ToString(DR["Employee Name"]));
                row.CreateCell(3).SetCellValue(Convert.ToString(DR["Designation"]));
                row.CreateCell(4).SetCellValue(Convert.ToString(DR["Level Name"]));
                row.CreateCell(5).SetCellValue(Convert.ToString(DR["Gender"]));
                row.CreateCell(6).SetCellValue(Convert.ToString(DR["Shift_ShortName"]));
                row.CreateCell(7).SetCellValue(Convert.ToString(DR["Date"]));
                row.CreateCell(8).SetCellValue(Convert.ToString(DR["In Reader ID"]));
                row.CreateCell(9).SetCellValue(Convert.ToString(DR["In Punch Type"]));
                row.CreateCell(10).SetCellValue(Convert.ToString(DR["In Date"]));
                if (Convert.ToString(DR["In Time"]) == String.Empty)
                    row.CreateCell(11).SetCellValue(Convert.ToString(DR["In Time"]));
                else
                {
                    row.CreateCell(11).SetCellValue((Convert.ToDateTime(Convert.ToString(DR["In Time"]))).ToString(NewTimeFormat));
                }
                row.CreateCell(12).SetCellValue(Convert.ToString(DR["Out Reader ID"]));
                row.CreateCell(13).SetCellValue(Convert.ToString(DR["Out Punch Type"]));
                row.CreateCell(14).SetCellValue(Convert.ToString(DR["Out Date"]));
                if (Convert.ToString(DR["Out Time"]) == String.Empty)
                    row.CreateCell(15).SetCellValue(Convert.ToString(DR["Out Time"]));
                else
                {
                    row.CreateCell(15).SetCellValue((Convert.ToDateTime(Convert.ToString(DR["Out Time"]))).ToString(NewTimeFormat));
                }
                row.CreateCell(16).SetCellValue(Convert.ToString(DR["Remarks"]));
                strPre = Convert.ToString(DR["Employee Id"]);
                for (int i = 0; i <= 16; i++)
                {
                    row.GetCell(i).CellStyle = IStyle;
                    sheet.AutoSizeColumn(i);
                }
                rowIndex++;
            }
        }
        else if (ddlreportType.SelectedIndex == 1)
        {
            row.CreateCell(0).SetCellValue("Sl.No");
            row.CreateCell(1).SetCellValue("Employee Id");
            row.CreateCell(2).SetCellValue("Employee Name");
            row.CreateCell(3).SetCellValue("Designation");
            row.CreateCell(4).SetCellValue("Level Name");
            row.CreateCell(5).SetCellValue("Gender");
            row.CreateCell(6).SetCellValue("Shift Name");
            row.CreateCell(7).SetCellValue("Date");
            row.CreateCell(8).SetCellValue("Reader ID");
            row.CreateCell(9).SetCellValue("First Punch");
            row.CreateCell(10).SetCellValue("Reader ID");
            row.CreateCell(11).SetCellValue("Last Punch");
            for (int i = 0; i <= 11; i++)
            {
                row.GetCell(i).CellStyle = hStyle;
            }
            rowIndex++;
            string strPre = string.Empty;
            Int32 SlNo = 0;
            foreach (DataRow DR in dsEmp.Tables[0].Rows)
            {
                row = sheet.CreateRow(rowIndex);
                if (strPre == string.Empty || strPre != Convert.ToString(DR["Employee Id"]))
                {
                    SlNo++;
                    row.CreateCell(0).SetCellValue(SlNo);
                }
                else
                {
                    row.CreateCell(0).SetCellValue("");
                }
                row.CreateCell(1).SetCellValue(Convert.ToString(DR["Employee Id"]));
                row.CreateCell(2).SetCellValue(Convert.ToString(DR["Employee Name"]));
                row.CreateCell(3).SetCellValue(Convert.ToString(DR["Designation"]));
                row.CreateCell(4).SetCellValue(Convert.ToString(DR["Level Name"]));
                row.CreateCell(5).SetCellValue(Convert.ToString(DR["Gender"]));
                row.CreateCell(6).SetCellValue(Convert.ToString(DR["Shift_ShortName"]));
                row.CreateCell(7).SetCellValue(Convert.ToString(DR["Date"]));
                row.CreateCell(8).SetCellValue(Convert.ToString(DR["In Reader ID"]));
                if (Convert.ToString(DR["InTime"]) == string.Empty || Convert.ToString(DR["InTime"]) != "0" && Convert.ToString(DR["Clk_InPunchType"]) == string.Empty)
                {
                    row.CreateCell(9).SetCellValue("");
                }
                else if (Convert.ToString(DR["InTime"]) != string.Empty && Convert.ToString(DR["Clk_InPunchType"]) == string.Empty)
                {
                    row.CreateCell(9).SetCellValue((Convert.ToDateTime(Convert.ToString(DR["InTime"]))).ToString(NewTimeFormat));
                }
                else
                {
                    row.CreateCell(9).SetCellValue((Convert.ToDateTime(Convert.ToString(DR["InTime"]))).ToString(NewTimeFormat) + " ( " + Convert.ToString(DR["Clk_InPunchType"]) + " ) ");
                }
                row.CreateCell(10).SetCellValue(Convert.ToString(DR["Out Reader ID"]));
                if (Convert.ToString(DR["OutTime"]) == string.Empty || Convert.ToString(DR["OutTime"]) != "0" && Convert.ToString(DR["Clk_OutPunchType"]) == string.Empty)
                {
                    row.CreateCell(11).SetCellValue("");
                }
                else if (Convert.ToString(DR["OutTime"]) != string.Empty && Convert.ToString(DR["Clk_OutPunchType"]) == string.Empty)
                {
                    row.CreateCell(11).SetCellValue((Convert.ToDateTime(Convert.ToString(DR["OutTime"]))).ToString(NewTimeFormat));
                }
                else
                {
                    row.CreateCell(11).SetCellValue((Convert.ToDateTime(Convert.ToString(DR["OutTime"]))).ToString(NewTimeFormat) + " ( " + Convert.ToString(DR["Clk_OutPunchType"]) + " ) ");
                }
                strPre = Convert.ToString(DR["Employee Id"]);
                for (int i = 0; i <= 11; i++)
                {
                    row.GetCell(i).CellStyle = IStyle;
                    sheet.AutoSizeColumn(i);
                }
                rowIndex++;
            }
        }
        else if (ddlreportType.SelectedIndex == 2 || ddlreportType.SelectedIndex == 4)
        {
            row.CreateCell(0).SetCellValue("Sl.No");
            row.CreateCell(1).SetCellValue("Employee Id");
            row.CreateCell(2).SetCellValue("Employee Name");
            row.CreateCell(3).SetCellValue("Designation");
            row.CreateCell(4).SetCellValue("Level Name");
            row.CreateCell(5).SetCellValue("Gender");
            row.CreateCell(6).SetCellValue("Shift Name");
            row.CreateCell(7).SetCellValue("Shift Date");
            row.CreateCell(8).SetCellValue("Punch Type");
            row.CreateCell(9).SetCellValue("Date");
            row.CreateCell(10).SetCellValue("Time");
            row.CreateCell(11).SetCellValue("Punch Type");
            row.CreateCell(12).SetCellValue("Date");
            row.CreateCell(13).SetCellValue("Time");
            for (int i = 0; i <= 13; i++)
            {
                row.GetCell(i).CellStyle = hStyle;
            }
            rowIndex++;
            string strPre = string.Empty;
            Int32 SlNo = 0;
            foreach (DataRow DR in dsEmp.Tables[0].Rows)
            {
                row = sheet.CreateRow(rowIndex);
                if (strPre == string.Empty || strPre != Convert.ToString(DR["Employee Id"]))
                {
                    SlNo++;
                    row.CreateCell(0).SetCellValue(SlNo);
                }
                else
                {
                    row.CreateCell(0).SetCellValue("");
                }
                row.CreateCell(1).SetCellValue(Convert.ToString(DR["Employee Id"]));
                row.CreateCell(2).SetCellValue(Convert.ToString(DR["Employee Name"]));
                row.CreateCell(3).SetCellValue(Convert.ToString(DR["Designation"]));
                row.CreateCell(4).SetCellValue(Convert.ToString(DR["Level Name"]));
                row.CreateCell(5).SetCellValue(Convert.ToString(DR["Gender"]));
                row.CreateCell(6).SetCellValue(Convert.ToString(DR["Shift_ShortName"]));
                row.CreateCell(7).SetCellValue(Convert.ToString(DR["Date"]));
                row.CreateCell(8).SetCellValue(Convert.ToString(DR["In Punch Type"]));
                row.CreateCell(9).SetCellValue(Convert.ToString(DR["In Date"]));
                if (Convert.ToString(DR["In Time"]) == String.Empty)
                    row.CreateCell(10).SetCellValue(Convert.ToString(DR["In Time"]));
                else
                {
                    row.CreateCell(10).SetCellValue((Convert.ToDateTime(Convert.ToString(DR["In Time"]))).ToString(NewTimeFormat));
                }
                row.CreateCell(11).SetCellValue(Convert.ToString(DR["Out Punch Type"]));
                row.CreateCell(12).SetCellValue(Convert.ToString(DR["Out Date"]));
                if (Convert.ToString(DR["Out Time"]) == String.Empty)
                    row.CreateCell(13).SetCellValue(Convert.ToString(DR["Out Time"]));
                else
                {
                    row.CreateCell(13).SetCellValue((Convert.ToDateTime(Convert.ToString(DR["Out Time"]))).ToString(NewTimeFormat));
                }
                strPre = Convert.ToString(DR["Employee Id"]);
                for (int i = 0; i <= 13; i++)
                {
                    row.GetCell(i).CellStyle = IStyle;
                    sheet.AutoSizeColumn(i);
                }
                rowIndex++;
            }
        }
        else if (ddlreportType.SelectedIndex == 3)
        {
            row.CreateCell(0).SetCellValue("Sl.No");
            row.CreateCell(1).SetCellValue("Employee Id");
            row.CreateCell(2).SetCellValue("Employee Name");
            row.CreateCell(3).SetCellValue("Designation");
            row.CreateCell(4).SetCellValue("Level Name");
            row.CreateCell(5).SetCellValue("Gender");
            row.CreateCell(6).SetCellValue("Shift Name");
            row.CreateCell(7).SetCellValue("Date");
            row.CreateCell(8).SetCellValue("In Reader ID");
            row.CreateCell(9).SetCellValue("In Punch Type");
            row.CreateCell(10).SetCellValue("In Date");
            row.CreateCell(11).SetCellValue("InTime");
            row.CreateCell(12).SetCellValue("Out Reader ID");
            row.CreateCell(13).SetCellValue("Out Punch Type");
            row.CreateCell(14).SetCellValue("Out Date");
            row.CreateCell(15).SetCellValue("Out Time");
            for (int i = 0; i <= 15; i++)
            {
                row.GetCell(i).CellStyle = hStyle;
            }
            rowIndex++;
            string strPre = string.Empty;
            Int32 SlNo = 0;
            foreach (DataRow DR in dsEmp.Tables[0].Rows)
            {
                row = sheet.CreateRow(rowIndex);
                if (strPre == string.Empty || strPre != Convert.ToString(DR["Employee Id"]))
                {
                    SlNo++;
                    row.CreateCell(0).SetCellValue(SlNo);
                }
                else
                {
                    row.CreateCell(0).SetCellValue("");
                }
                row.CreateCell(1).SetCellValue(Convert.ToString(DR["Employee Id"]));
                row.CreateCell(2).SetCellValue(Convert.ToString(DR["Employee Name"]));
                row.CreateCell(3).SetCellValue(Convert.ToString(DR["Designation"]));
                row.CreateCell(4).SetCellValue(Convert.ToString(DR["Level Name"]));
                row.CreateCell(5).SetCellValue(Convert.ToString(DR["Gender"]));
                row.CreateCell(6).SetCellValue(Convert.ToString(DR["Shift_ShortName"]));
                row.CreateCell(7).SetCellValue(Convert.ToString(DR["Date"]));
                row.CreateCell(8).SetCellValue(Convert.ToString(DR["In Reader ID"]));
                row.CreateCell(9).SetCellValue(Convert.ToString(DR["In Punch Type"]));
                row.CreateCell(10).SetCellValue(Convert.ToString(DR["In Date"]));
                if (Convert.ToString(DR["InTime"]) == String.Empty)
                    row.CreateCell(11).SetCellValue(Convert.ToString(DR["InTime"]));
                else
                {
                    row.CreateCell(11).SetCellValue((Convert.ToDateTime(Convert.ToString(DR["InTime"]))).ToString(NewTimeFormat));
                }
                row.CreateCell(12).SetCellValue(Convert.ToString(DR["Out Reader ID"]));
                row.CreateCell(13).SetCellValue(Convert.ToString(DR["Out Punch Type"]));
                row.CreateCell(14).SetCellValue(Convert.ToString(DR["Out Date"]));
                if (Convert.ToString(DR["Out Time"]) == String.Empty)
                    row.CreateCell(15).SetCellValue(Convert.ToString(DR["Out Time"]));
                else
                {
                    row.CreateCell(15).SetCellValue((Convert.ToDateTime(Convert.ToString(DR["Out Time"]))).ToString(NewTimeFormat));
                }
                strPre = Convert.ToString(DR["Employee Id"]);
                for (int i = 0; i <= 15; i++)
                {
                    row.GetCell(i).CellStyle = IStyle;
                    sheet.AutoSizeColumn(i);
                }
                rowIndex++;
            }
        }
        ISheet sheet1 = workbook.CreateSheet("Legends");
        sheet1.IsPrintGridlines = true;
        sheet1.VerticallyCenter = true;
        int rowIndex1 = 0;
        NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(rowIndex1);
        hFont = (HSSFFont)workbook.CreateFont();
        hFont.Boldweight = (short)FontBoldWeight.Bold;
        hFont.Color = HSSFColor.Red.Index;
        hFont.FontHeightInPoints = 10;
        hStyle = (HSSFCellStyle)workbook.CreateCellStyle();
        hStyle.SetFont(hFont);
        hStyle.FillForegroundColor = IndexedColors.White.Index;
        hStyle.FillPattern = FillPattern.SolidForeground;
        HSSFCellStyle hStyle1 = (HSSFCellStyle)workbook.CreateCellStyle();
        hStyle1.SetFont(hFont);
        hStyle1.FillForegroundColor = IndexedColors.Pink.Index;
        hStyle1.FillPattern = FillPattern.SolidForeground;
        HSSFFont hFont1 = (HSSFFont)workbook.CreateFont();
        hFont1.Boldweight = (short)FontBoldWeight.Bold;
        hFont1.Color = HSSFColor.Pink.Index;
        hFont1.FontHeightInPoints = 10;
        HSSFCellStyle hStyle2 = (HSSFCellStyle)workbook.CreateCellStyle();
        hStyle2.SetFont(hFont1);
        hStyle2.FillForegroundColor = IndexedColors.White.Index;
        hStyle2.FillPattern = FillPattern.SolidForeground;
        row1.CreateCell(0).SetCellValue("");
        row1.CreateCell(1).SetCellValue("F_In     - Free Scan In Punch");
        sheet1.AddMergedRegion(new CellRangeAddress(rowIndex1, rowIndex1, 1, 5));
        row1.GetCell(1).CellStyle = hStyle;
        rowIndex1++;
        row1 = sheet1.CreateRow(rowIndex1);
        row1.CreateCell(0).SetCellValue("");
        row1.CreateCell(1).SetCellValue("F_Out  - Free Scan Out Punch");
        sheet1.AddMergedRegion(new CellRangeAddress(rowIndex1, rowIndex1, 1, 5));
        row1.GetCell(1).CellStyle = hStyle;
        rowIndex1++;
        row1 = sheet1.CreateRow(rowIndex1);
        row1.CreateCell(0).SetCellValue("");
        row1.GetCell(0).CellStyle = hStyle1;
        row1.CreateCell(1).SetCellValue("NOS Records");
        sheet1.AddMergedRegion(new CellRangeAddress(rowIndex1, rowIndex1, 1, 5));
        row1.GetCell(1).CellStyle = hStyle2;
        rowIndex1++;
        using (MemoryStream exportData = new MemoryStream())
        {
            workbook.Write(exportData);
            string saveAsFileName = string.Format(reportName + "-{0:d}.xls", DateTime.Now).Replace("/", "-");
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
            Response.Clear();
            Response.BinaryWrite(exportData.GetBuffer());
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
    }
    #endregion
}