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;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                                         Started Date  : 24-July-2007
* Module          : Payroll Process                                                   Last Modified : 05-Aug-2007
* Programmer      : Kumuthini.L
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          : \\windev\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
****************************************************************************************************************************/
public partial class Transactions_TA_UI_Transactions_MonthClosureAdd : Base.BasePage
{
    ResourceManager rm;
    CultureInfo ci;
    // Create object for the Class MonthClosureEventData to use the methods that are used to interact with the Payroll Process 
    // related Tables in the Timecheck Database
    MonthClosureEventData objMonthClosure = new MonthClosureEventData();
    // Create object for the Class EmployeeEventData to use the methods that are used to interact with the Employee Details 
    // related Tables in the Timecheck Database
    EmployeeEventData objEmployee = new EmployeeEventData();
    cls_Common objClassCommon = new cls_Common();
    #region variable declaration

    // Global Variable Declarations 

    int gintUserId;
    string strMonth = "";
    int intYear = 0;
    string strStartDate = "";
    string strEndDate = "";
    string strClosingDate = "";
    string gstrUserGroupName = string.Empty;
    string gstrDateFormat = string.Empty;

    DataSet ds_Year = new DataSet();

    #endregion
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking and reset date format
        base.Page_Load(sender, e);
        //Session.LCID = 1033;
        //get the culture info to set the language
        hdn_DateFormat.Value = Convert.ToString(Session["IDateFormat"]);
        if (Session["Session_ActiveLanguage"] == null)
        {
            Response.Redirect("../index.aspx");
        }
        else
        {
            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;
            LoadStrings(ci);
        }
        ds_Year = objClassCommon.GetYearsetting();
        if (ds_Year.Tables[0].Rows.Count <= 0)
        {
            string strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Msg_DefineYearSettinginOrganizationSettings", ci) + "');location.replace('../Organization Settings/Organization_YearSetting.aspx')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("AlertLeavePolicyYear", strScript);
        }
        else
        {
            ViewState["YearSettings_Id"] = ds_Year.Tables[0].Rows[0]["YearSettings_Id"].ToString();
            ds_Year.Dispose();
        }
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        GC.Collect();
        // Call the Javascript Function dateValidation() to validate the Date before saving the Details 
        btn_Save.Attributes.Add("onclick", "return dateValidation();");
        // Get the Employee Id who have logged in from the Session Variable and store in Variable (i.e., gintUserId) for Audit Trail
        gintUserId = Int32.Parse(Session["EmpAutoID"].ToString());
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        // If Page not PostBack
        if (!IsPostBack)
        {
            objClassCommon.menu("ExportTran", "27");
            // Fill Company Dropdown
            CompanyBinding();
            // Function call to fill years in the Year DropDown Lists
            FillYearDropdown();
        }
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        //Added this arguments ,'" + rm.GetString("Alert_Are_you_sure_to_Save", ci) + "' in alert message. It was missing here by MAnjula.P on 10-Feb-2014
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_SelectCompany", ci) + "','" + rm.GetString("Alert_SelectMonth", ci) + "','" + rm.GetString("Alert_SelectYear", ci) + "','" + rm.GetString("Alert_SelectStartDate", ci) + "','" + rm.GetString("Alert_SelectEndDate") + "','" + rm.GetString("Alert_SelectClosedDate") + "','" + rm.GetString("Alert_SelectCorrectYearinStartdate") + "','" + rm.GetString("Alert_SelectCorrectMonthinStartdate") + "','" + rm.GetString("Alert_SelectCorrectYearinEnddate") + "','" + rm.GetString("Alert_SelectCorrectMonthinEnddate") + "','" + rm.GetString("Alert_Are_you_sure_to_Save", ci) + "','" + rm.GetString("Alert_EnterStartDate") + "','" + rm.GetString("Alert_EnterEndDate") + "','" + rm.GetString("Alert_EnterClosedDate") + "');";
        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") + "','" + rm.GetString("Alert_Invalidtimeformat1") + "','" + rm.GetString("Alert_PleaseentervalidDateandTime") + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMM") + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMMTT") + "','" + rm.GetString("Alert_PleaseentervalidDateandTime") + "','" + rm.GetString("Alert_Invalidvalueforhours") + "','" + rm.GetString("Alert_Invalidvalueforminites") + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
    }
    // Used to fill all available companies in the Company Dropdown
    private void CompanyBinding()
    {
        DataSet dsCompany = new DataSet();
        ShiftEvenData objShift = new ShiftEvenData();
        dsCompany = objShift.SelectCompany(gstrUserGroupName, Convert.ToString(Session["LevelRights"]), 0);
        if (dsCompany != null)
        {
            if (dsCompany.Tables[0].Rows.Count > 0)
            {
                ddlCompany.DataSource = dsCompany;
                ddlCompany.DataTextField = "CompanyName";
                ddlCompany.DataValueField = "CompanyId";
                ddlCompany.DataBind();
                foreach (ListItem item in ddlCompany.Items)
                {
                    item.Attributes.Add("title", item.Text);
                }
            }
        }
    }
    // Save Button - Click Event
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        int intOrg_LevelId = 0;
        if (ddlCompany.SelectedValue != "0")
        {
            intOrg_LevelId = Convert.ToInt32(ddlCompany.SelectedValue);
        }
        else
        {
            lbl_Error.Text = rm.GetString("Alert_Select_Company", ci);
            return;
        }
        // Get the Payroll Process Details from the Page
        //strMonth = ddl_Month.SelectedItem.Text.ToString();
        switch (ddl_Month.SelectedValue)
        {
            case "01":
                strMonth = "January";
                break;
            case "02":
                strMonth = "February";
                break;
            case "03":
                strMonth = "March";
                break;
            case "04":
                strMonth = "April";
                break;
            case "05":
                strMonth = "May";
                break;
            case "06":
                strMonth = "June";
                break;
            case "07":
                strMonth = "July";
                break;
            case "08":
                strMonth = "August";
                break;
            case "09":
                strMonth = "September";
                break;
            case "10":
                strMonth = "October";
                break;
            case "11":
                strMonth = "November";
                break;
            case "12":
                strMonth = "December";
                break;
        }
        intYear = Convert.ToInt32(ddl_Year.Text);
        strStartDate = txt_StartDate.Text;
        strEndDate = txt_EndDate.Text;
        //strClosingDate = txt_ClosedDate.Text;
        strClosingDate = txt_EndDate.Text;
        int count = 0;
        //if (Convert.ToString(Session["IDateFormat"]) == "DD/MM/YYYY")
        //{
        //    strStartDate = strStartDate.Substring(3, 2) + "/" + strStartDate.Substring(0, 2) + "/" + strStartDate.Substring(6, 4);
        //    strEndDate = strEndDate.Substring(3, 2) + "/" + strEndDate.Substring(0, 2) + "/" + strEndDate.Substring(6, 4);
        //    strClosingDate = strClosingDate.Substring(3, 2) + "/" + strClosingDate.Substring(0, 2) + "/" + strClosingDate.Substring(6, 4);
        //}
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        strStartDate = objClassCommon.DateConvertion(gstrDateFormat, txt_StartDate.Text);
        strEndDate = objClassCommon.DateConvertion(gstrDateFormat, txt_EndDate.Text);
        // strClosingDate = objClassCommon.DateConvertion(gstrDateFormat, txt_ClosedDate.Text);
        strClosingDate = objClassCommon.DateConvertion(gstrDateFormat, txt_EndDate.Text);
        // Get the Current Date
        DateTime dtmCurrentDate = DateTime.Parse(DateTime.Now.ToShortDateString());
        // Parse the Start Date to DateTime Format
        DateTime dtStartDate = DateTime.Parse(strStartDate);
        // Parse the End Date to DateTime Format
        DateTime dtEndDate = DateTime.Parse(strEndDate);
        DateTime dtCloDate = DateTime.Parse(strClosingDate);
        int Days = 0;
        Days = dtmCurrentDate.CompareTo(dtStartDate);
        // If Difference between Current Date and Start Date is lesser than 0
        if (Days < 0)
        {
            //lbl_Error.Text = "Start Date should not be greater than Current Date";
            base.Message(rm.GetString("Msg_StartDateshouldnotbegreaterthanCurrentDate", ci));
            txt_StartDate.Focus();
            return;
        }
        Days = 0;
        Days = dtmCurrentDate.CompareTo(dtEndDate);
        // If Difference between Current Date and End Date is lesser than 0
        if (Days < 0)
        {
            //lbl_Error.Text = "End Date should not be greater than Current Date";
            base.Message(rm.GetString("Msg_EndDateshouldnotbegreaterthanCurrentDate", ci));
            txt_EndDate.Focus();
            return;
        }
        // Compare End Date with Start Date
        Days = 0;
        Days = dtEndDate.CompareTo(dtStartDate);
        // If Difference between End Date and Start Date is lesser than 0
        if (Days < 0)
        {
            //lbl_Error.Text = "End Date should not be less than the Start Date";
            base.Message(rm.GetString("Msg_EndDateshouldnotbelessthantheStartDate", ci));
            txt_StartDate.Focus();
            return;
        }
        int StartDayDiff = dtCloDate.CompareTo(dtEndDate);
        int EndDayDiff = dtCloDate.CompareTo(dtStartDate);
        //if (StartDayDiff < 0)
        //{
        //    //lbl_Error.Text = "Closed Date should not be less than the End Date";
        //    base.Message(rm.GetString("Msg_ClosedDateshouldnotbelessthantheEndDate", ci));
        //    txt_ClosedDate.Focus();
        //    return;
        //}
        try
        {
            // Check for Duplicate
            //count = objMonthClosure.CheckMonthClosure_Insert(intOrg_LevelId, strMonth, intYear);
            count = objMonthClosure.CheckMonthClosure_Insert(intOrg_LevelId, strStartDate, strEndDate);
            // If Record does not Exists
            if (count == 0)
            {
                // Add the Payroll Process Details 
                objMonthClosure.InsertMonthClosureDetails(intOrg_LevelId, strMonth, intYear, strStartDate, strEndDate, strClosingDate, gintUserId, DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss tt"), Convert.ToInt32(ViewState["YearSettings_Id"]));
                //Response.Redirect("TA_UI_Transactions_MonthClosure.aspx");
                base.MessageWithRedirect(rm.GetString("Msg_MonthClosureAddedSuccessfully", ci), "TA_UI_Transactions_MonthClosure.aspx");
            }
            else
            {
                //lbl_Error.Text = "Month Closure Duration Already Exists";
                base.Message(rm.GetString("Msg_MonthClosureDurationAlreadyExists", ci));
            }
        }
        catch (Exception ex)
        {
            //
            Response.Write(ex.ToString());
        }
    }
    // Cancel Button - Click Event
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Transactions_MonthClosure.aspx");
    }
    // **********************************************************************************************************************
    // Function Name : FillYearDropdown
    // Purpose :
    //     The FillYearDropdown Method is used to fill the years in the Year DropDown List. 
    // **********************************************************************************************************************
    private void FillYearDropdown()
    {
        // Get the Current Year
        int year = System.DateTime.Now.Year;
        year = year - 1;
        // Insert the year starting from Current Year upto 10 years ahead in the Year DropDown List
        for (int i = 1; i <= 10; i++)
        {
            ddl_Year.Items.Add(year.ToString());
            year = year + 1;
        }
        // Insert Default Item -Select- in the Year DropDown List
        //ddl_Year.Items.Insert(0, "-Select-");
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("UserGroups_MonthClosure", ci);
        lbl_ClosedDate.Text = rm.GetString("PayrollProcess_ClosedDate", ci);
        lbl_EndDate.Text = rm.GetString("PayrollProcess_EndDate", ci);
        lbl_Month.Text = rm.GetString("PayrollProcess_Month", ci);
        lbl_StartDate.Text = rm.GetString("PayrollProcess_StartDate", ci);
        lbl_Year.Text = rm.GetString("PayrollProcess_Year", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        RequiredFieldValidator7.ErrorMessage = rm.GetString("Alert_SelectMonth", ci);
        RequiredFieldValidator8.ErrorMessage = rm.GetString("Alert_SelectYear", ci);
        lblCompany.Text = rm.GetString("EntranceMaster_Company", ci);
        btn_Save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_Cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        ddl_Month.Items[0].Text = rm.GetString("Select", ci);
        ddl_Month.Items[1].Text = rm.GetString("January", ci);
        ddl_Month.Items[2].Text = rm.GetString("February", ci);
        ddl_Month.Items[3].Text = rm.GetString("March", ci);
        ddl_Month.Items[4].Text = rm.GetString("April", ci);
        ddl_Month.Items[5].Text = rm.GetString("May", ci);
        ddl_Month.Items[6].Text = rm.GetString("June", ci);
        ddl_Month.Items[7].Text = rm.GetString("July", ci);
        ddl_Month.Items[8].Text = rm.GetString("August", ci);
        ddl_Month.Items[9].Text = rm.GetString("September", ci);
        ddl_Month.Items[10].Text = rm.GetString("October", ci);
        ddl_Month.Items[11].Text = rm.GetString("November", ci);
        ddl_Month.Items[12].Text = rm.GetString("December", ci);
        ddlCompany.Items[0].Text = rm.GetString("Select", ci);
        ddl_Year.Items[0].Text = rm.GetString("Select", ci);
    }
}
