protected void GeneratePayrollReport()
    {
        DataRow[] foundEmpSalHeadRow;
        string    strEmpID         = "";
        string    strGenerateValue = "";

        int     inGrossHeadCount   = 0;
        bool    EmpGrossColAdded   = false;
        int     inBenefitHeadCount = 0;
        int     inDeductCount      = 0;
        decimal dclSalHeadAmt      = 0;

        switch (ddlGeneratefor.SelectedValue.ToString())
        {
        case "O":
            strGenerateValue    = ddlGenerateValue.SelectedValue.ToString();
            lblGenerateFor.Text = ddlGenerateValue.SelectedItem.Text.Trim();
            break;

        case "B":
            strGenerateValue    = ddlBank.SelectedValue.ToString();
            lblGenerateFor.Text = ddlBank.SelectedItem.Text.Trim();
            break;

        case "E":
            strGenerateValue = txtTextValue.Text.Trim();
            break;
        }
        DataTable dtSalaryHead = objPayrollMgr.SelectTotalSalHeadWithSeq(0);
        DataTable dtHeadCount  = objPayRptMgr.GetHeadCount();

        DataRow[] founHCRows = dtHeadCount.Select("DISPLAYTYPE='B'");
        inBenefitHeadCount = Convert.ToInt32(founHCRows[0]["HEADCOUNT"]);
        founHCRows         = null;
        founHCRows         = dtHeadCount.Select("DISPLAYTYPE='D'");
        inDeductCount      = Convert.ToInt32(founHCRows[0]["HEADCOUNT"]);

        dtGrossSalHead = objPayrollMgr.SelectGrossSalHead(0);
        dtEmpPayroll   = objPayRptMgr.GetPayrollDatAForExcel(ddlGeneratefor.SelectedValue.ToString(), strGenerateValue,
                                                             ddlMonth.SelectedValue.ToString(), ddlYear.SelectedValue.ToString(), ddlBank.SelectedValue.Trim(),
                                                             chkBonus.Checked == true ? "B" : "S", ddlGroup.SelectedValue.Trim());
        this.InitializeSummaryTable(dtSalaryHead.Rows.Count + 14);


        int i = 4;
        int j = 1;

        foreach (DataRow dEmpRow in dtEmpPayroll.Rows)
        {
            dclEmpBenefits = 0;
            dclEmpDeduct   = 0;
            dclTotalSalary = 0;
            this.GetEmpBenefitsAmount(dtSalaryHead, dEmpRow["EMPID"].ToString().Trim(), dEmpRow["GROSSAMNT"].ToString());
            i = 9;
            if (strEmpID == dEmpRow["EMPID"].ToString().Trim())
            {
                continue;
            }
            DataRow nRow = dtPayrollSummary.NewRow();
            nRow[0] = Convert.ToString(j);
            nRow[1] = dEmpRow["EMPID"].ToString().Trim();
            nRow[2] = dEmpRow["FULLNAME"].ToString().Trim();
            nRow[3] = "";
            nRow[4] = dEmpRow["JoiningDate"].ToString().Trim();
            nRow[5] = dEmpRow["DOB"].ToString().Trim();
            nRow[6] = dEmpRow["DEPTNAME"].ToString().Trim();
            nRow[7] = dEmpRow["JOBTITLE"].ToString().Trim();
            nRow[8] = dEmpRow["GradeName"].ToString().Trim();
            foreach (DataRow dSalRow in dtSalaryHead.Rows)
            {
                if (i - 9 == dtGrossSalHead.Rows.Count)
                {
                    nRow[i] = Common.RoundDecimal(dEmpRow["GROSSAMNT"].ToString(), 0);
                    i++;
                }
                if ((i - 9) - dtGrossSalHead.Rows.Count == inBenefitHeadCount + 1)
                {
                    nRow[i] = dclEmpBenefits.ToString();
                    i++;
                }
                if ((i - 9) - dtGrossSalHead.Rows.Count == inBenefitHeadCount + 2)
                {
                    nRow[i] = dclTotalSalary.ToString();
                    i++;

                    dclSalHeadAmt = 0;
                    dclSalHeadAmt = this.GetSalHeadAmt(dEmpRow["EMPID"].ToString().Trim(), dSalRow["SHEADID"].ToString().Trim());
                    if (dSalRow["DISPLAYTYPE"].ToString().Trim() == "D")
                    {
                        if (dclSalHeadAmt > 0)
                        {
                            dclSalHeadAmt = dclSalHeadAmt * -1;
                        }
                    }

                    nRow[i] = dclSalHeadAmt.ToString();
                    i++;
                }
                else
                {
                    dclSalHeadAmt = 0;
                    dclSalHeadAmt = this.GetSalHeadAmt(dEmpRow["EMPID"].ToString().Trim(), dSalRow["SHEADID"].ToString().Trim());
                    if (dSalRow["DISPLAYTYPE"].ToString().Trim() == "D")
                    {
                        if (dclSalHeadAmt > 0)
                        {
                            dclSalHeadAmt = dclSalHeadAmt * -1;
                        }
                    }

                    nRow[i] = dclSalHeadAmt.ToString();
                    i++;
                }
            }


            nRow[i] = dclEmpDeduct.ToString();
            i++;
            //}

            nRow[i] = Common.RoundDecimal(dEmpRow["NETPAY"].ToString(), 0);

            dtPayrollSummary.Rows.Add(nRow);
            dtPayrollSummary.AcceptChanges();
            strEmpID = dEmpRow["EMPID"].ToString().Trim();
            j++;
        }

        grPayroll.DataSource = dtPayrollSummary;
        grPayroll.DataBind();
        if (dtPayrollSummary.Rows.Count > 0)
        {
            this.FormatGridView(dtSalaryHead, inBenefitHeadCount);
            this.GetSummaryTotal();
            if (ddlGeneratefor.SelectedValue.Trim() == "E")
            {
                lblGenerateFor.Text = grPayroll.Rows[0].Cells[2].Text.Trim() + " [" + grPayroll.Rows[0].Cells[1].Text.Trim() + "] ";
            }
            lblPayrollMonth.Text = "Salary for the month of " + ddlMonth.SelectedItem.Text + " " + ddlYear.SelectedItem.Text;

            lblPreparedBy.Text   = dtEmpPayroll.Rows[0]["PREPAREDBY"].ToString().Trim();
            lblPreparedDate.Text = string.IsNullOrEmpty(dtEmpPayroll.Rows[0]["PREPARINGDATE"].ToString().Trim()) == false?Common.DisplayDateTime(dtEmpPayroll.Rows[0]["PREPARINGDATE"].ToString().Trim()) : "";

            lblReviewedBy.Text = dtEmpPayroll.Rows[0]["REVIEWEDBY"].ToString().Trim();
            lblReviewDate.Text = string.IsNullOrEmpty(dtEmpPayroll.Rows[0]["REVIEWDATE"].ToString().Trim()) == false?Common.DisplayDateTime(dtEmpPayroll.Rows[0]["REVIEWDATE"].ToString().Trim()) : "";

            lblCheckedBy.Text = dtEmpPayroll.Rows[0]["CHECKEDBY"].ToString().Trim();
            lblCheckDate.Text = string.IsNullOrEmpty(dtEmpPayroll.Rows[0]["CHECKDATE"].ToString().Trim()) == false?Common.DisplayDateTime(dtEmpPayroll.Rows[0]["CHECKDATE"].ToString().Trim()) : "";

            lblApprovedBy.Text  = dtEmpPayroll.Rows[0]["APPROVEDBY"].ToString().Trim();
            lblApproveDate.Text = string.IsNullOrEmpty(dtEmpPayroll.Rows[0]["APPROVINGDATE"].ToString().Trim()) == false?Common.DisplayDateTime(dtEmpPayroll.Rows[0]["APPROVINGDATE"].ToString().Trim()) : "";

            lblDisburseBy.Text   = dtEmpPayroll.Rows[0]["DISBURSEBY"].ToString().Trim();
            lblDisburseDate.Text = string.IsNullOrEmpty(dtEmpPayroll.Rows[0]["DISBURSINGDATE"].ToString().Trim()) == false?Common.DisplayDateTime(dtEmpPayroll.Rows[0]["DISBURSINGDATE"].ToString().Trim()) : "";
        }
        else
        {
            lblGenerateFor.Text  = "";
            lblPayrollMonth.Text = "";
            lblPreparedBy.Text   = "";
            lblPreparedDate.Text = "";
            lblReviewedBy.Text   = "";
            lblReviewDate.Text   = "";
            lblCheckedBy.Text    = "";
            lblCheckDate.Text    = "";
            lblApprovedBy.Text   = "";
            lblApproveDate.Text  = "";
            lblDisburseBy.Text   = "";
            lblDisburseDate.Text = "";
        }
    }