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.Globalization;
using System.Threading;
using System.Reflection;
using System.Resources;
using System.IO;
/****************************************************************************************************************************
* Project Name    : TimeCheck                  Started Date  : 18-July-2007
* Module          : Payroll Process            Last Modified : 29-Aug-2007
* Programmer      : Kumuthini.L
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Transactions_Transaction_PayrollProcessGrid : Base.BasePage
{
    #region VariableDeclaration
    ResourceManager rm;
    CultureInfo ci;
    MonthClosureEventData objMonthClosureEventData = new MonthClosureEventData();
    EmployeeEventData objEmployeeEventData = new EmployeeEventData();
    MonthClosureEventData objMonthClosure = new MonthClosureEventData();
    Rpt_Common rptcommon = new Rpt_Common();
    int gintMonClosureId;
    string gstrDateFormat = string.Empty;
    string StartDate = string.Empty;
    string EndDate = string.Empty;
    string strLevels = string.Empty;
    string gstrEmployeeId = string.Empty;
    string gstrDate = string.Empty;
    int gintUserId = 0, MonthCloseID = 0;
    string source_Language;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        grv_Discrepancies.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        // Get the culture info to set the 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"]);
        LoadStrings(ci);
        StartDate = Convert.ToString(Request.QueryString["StartDate"]);
        EndDate = Convert.ToString(Request.QueryString["EndDate"]);
        strLevels = Convert.ToString(Request.QueryString["LevelID"]);
        gintMonClosureId = Convert.ToInt32(Request.QueryString["MonClosureId"]);
        Session.Add("SessiongintMonClosureId", gintMonClosureId);
        gintUserId = Int32.Parse(Session["EmpAutoID"].ToString());
        MonthCloseID = gintMonClosureId;
        DataTable dtDiscrepancies = new DataTable();
        dtDiscrepancies = objMonthClosureEventData.GetDiscrepanciesForPayroll(StartDate, EndDate, strLevels, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(strLevels));
    }
    #endregion
    #region ButtonEvents
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Transactions/TA_UI_Transactions_MonthClosureView.aspx?MonthClose_Id=" + gintMonClosureId);
    }
    protected void btn_IndividualReprocess_Click(object sender, EventArgs e)
    {
        PayrollProcess("Process");
    }
    #endregion
    #region UserDefinedFunctions
    private void LoadStrings(CultureInfo ci)
    {
        //lbl_Header.Text = rm.GetString("PayrollProcess_Discrepancies", ci);
        btnBack.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        grv_Discrepancies.Columns[1].Caption = rm.GetString("email_EmployeeId", ci);
        grv_Discrepancies.Columns[2].Caption = rm.GetString("Caption_InDeviceId", ci);
        grv_Discrepancies.Columns[3].Caption = rm.GetString("ManualTimeEntry_InDate", ci);
        grv_Discrepancies.Columns[4].Caption = rm.GetString("ManualTimeEntry_In_Time", ci);
        grv_Discrepancies.Columns[5].Caption = rm.GetString("Caption_OutDeviceId", ci);
        grv_Discrepancies.Columns[6].Caption = rm.GetString("ManualTimeEntry_OutDate", ci);
        grv_Discrepancies.Columns[7].Caption = rm.GetString("ManualTimeEntry_Out_Time", ci);
        grv_Discrepancies.Columns[8].Caption = rm.GetString("Caption_ClockDate", ci);
        btn_IndividualReprocess.Text = rm.GetString("Caption_CONTINUEPROCESS", ci);
    }
    public void PayrollProcess(string inType)
    {
        bool blnErrorOccurred = false;
        try
        {
            string strStartDate, strEndDate;
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strStartDate = StartDate;
            strEndDate = EndDate;
            DateTime dtmStartDate = DateTime.Parse(strStartDate);
            DateTime dtmEndDate = DateTime.Parse(strEndDate);
            DataSet dsEmpDetails = new DataSet();
            DataTable dtPayroll = new DataTable();
            DataSet dsProcess = new DataSet();
            DataTable dtProcess = new DataTable("ReprocessEmployees");
            DataColumn dcProcess = new DataColumn();
            dcProcess.DataType = System.Type.GetType("System.String");
            dcProcess.ColumnName = "EmployeeId";
            dtProcess.Columns.Add(dcProcess);
            dsProcess.Tables.Add(dtProcess);
            StringWriter strWriteXML = new StringWriter();
            dsProcess.Tables[0].WriteXml(strWriteXML);
            int intProcessedCount = 0;
            intProcessedCount = objMonthClosure.CheckMonthClosureProcessFlag(MonthCloseID);
            dsEmpDetails = objEmployeeEventData.Select_EmployeeDetailsForMonthClosure(strEndDate, strLevels, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(strLevels), MonthCloseID, gintUserId, intProcessedCount, inType, strWriteXML.ToString(), "", "");
            if (inType == "Process")
            {
                objMonthClosure.UpdateMonthClosureProcessFlag(MonthCloseID);
            }
        }
        catch (Exception ex)
        {
            blnErrorOccurred = true;
            //lblMessage.Text = string.Empty;
            string strErrorMessage = "<script language='javascript' type='text/javascript'>";
            strErrorMessage += "alert('" + ex.Message + "');";
            strErrorMessage += "</script>";
            this.RegisterStartupScript("setScript", strErrorMessage);
        }
        if (blnErrorOccurred == false)
        {
            string strScript = string.Empty;
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("PayrollProcess_Status", ci) + "');";
            strScript = strScript + "location.replace('TA_UI_Transactions_MonthClosureView.aspx?MonthClose_Id=" + MonthCloseID + "');";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("AlertPAdd9962878766", strScript);
        }
    }
    // Function to Convert hh:mm format to Total Minutes
    public double ConvertHoursMinsToMinutes(string inHoursMins)
    {
        int intTotalMinutes = 0, intHours = 0, intMinutes = 0;
        string[] HoursMins = inHoursMins.Split(':');
        intHours = Int16.Parse(HoursMins[0]);
        intMinutes = Int16.Parse(HoursMins[1]);
        intTotalMinutes = (intHours * 60) + intMinutes;
        return (double.Parse(intTotalMinutes.ToString()));
    }
    // Function to Convert Total Minutes to hh:mm format
    public string TimeFormat(double TotMins)
    {
        string strHrsMins = string.Empty;
        double dHrs = Math.Floor(TotMins / 60);
        double dMins = TotMins % 60;
        int iHrs = Convert.ToInt32(dHrs);
        int iMins = Convert.ToInt32(dMins);
        if (Convert.ToInt32(dMins) >= 60)
        {
            iMins = Convert.ToInt32(dMins) % 60;
            iHrs += Convert.ToInt32(dMins) / 60;
        }
        strHrsMins = iHrs.ToString("00") + ":" + iMins.ToString("00");
        return (strHrsMins);
    }
    #endregion
    protected void grv_Discrepancies_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == DevExpress.Web.GridViewRowType.Data)
        {
            source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
            rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            string Outdate = Convert.ToString(e.GetValue("clk_outDate1"));
            //if (Outdate != string.Empty && Outdate != null)
            //{
            //    string date1 = (Convert.ToDateTime(Outdate)).ToString(Session["MDateFormat"].ToString());
            //    e.Row.Cells[5].Text = date1;
            //}
            //string InDate = Convert.ToString(e.GetValue("clk_InDate1"));
            //if (InDate != string.Empty && InDate != null)
            //{
            //    string date2 = (Convert.ToDateTime(InDate)).ToString(Session["MDateFormat"].ToString());
            //    e.Row.Cells[2].Text = date2;
            //}
            //string ClockDate = Convert.ToString(e.GetValue("clk_Date1"));
            //if (ClockDate != string.Empty && ClockDate != null)
            //{
            //    string date3 = (Convert.ToDateTime(ClockDate)).ToString(Session["MDateFormat"].ToString());
            //    e.Row.Cells[7].Text = date3;
            //}
        }
    }
}
