using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Globalization;
using System.Threading;
using System.Reflection;
using System.Resources;
/****************************************************************************************************************************
* Project Name    : TimeCheck                         Started Date  : 12-Aug-2007
* Module          : Leave Sanction                    Last Modified : 14-Aug-2007
* Programmer      : Prabakaran.P
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Transactions_TA_UI_Transactions_LeaveSanction : Base.BasePage
{
    #region Variable Declaration
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    LeaveApplicationEventData objLeaveApplicationEventData = new LeaveApplicationEventData();
    cls_Common objClassCommon = new cls_Common();
    DateTime CurrentDate;
    int month;
    int year;
    int date;
    int TotalDays;
    DataSet dsEmployeeGroup = new DataSet();
    string gstrDateFormat, strFromDateTime, strToDateTime;
    int payrollConfig = 0;
    #endregion
    #region PageLoad Events
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!Page.IsPostBack)
        {
            base.CheckPages(0);
            //Todo:need to add this line into new file.
            Hidden1.Value = Convert.ToString(Session["IDateFormat"]);
            payrollConfig = objClassCommon.CheckPageExistsInLicense(185);
            if (payrollConfig == 0)
            {
                objClassCommon.menu("LeaveTransaction", "248");
            }
            else
            {
                objClassCommon.menu("LeaveTransaction", "24");
            }
        }
        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"]);
        LoadStrings(ci);
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        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_SelectStatus", 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 (!IsPostBack)
        {
            txtFromDate.Text = (Convert.ToDateTime(CurrentMonthStartDate().ToString())).ToString(Session["MDateFormat"].ToString());
            txtToDate.Text = (Convert.ToDateTime(NextMonthEndDate())).ToString(Session["MDateFormat"].ToString());
        }
    }
    #endregion
    #region User Defined Functions

    public int bindLeaveSanctionDetails(string LevelId, string Status, string From_Date, string To_Date, string strSwipeCardNo, string strEmployeeName, string strEmployeeCodeId)
    {
        DataTable dtLeaveTransactionView = new DataTable();
        int intCount = 0;
        //To check the user is in SuperAdmin Group
        if (Convert.ToString(Session["UserGroupName"]) == "SuperAdmin" || Convert.ToString(Session["HasSpecialRights"]) == "True")
        {
            dtLeaveTransactionView = objLeaveApplicationEventData.GetLeaveSanctionDetailsForSuperAdmin(Convert.ToInt32(Session["EmpAutoID"]),LevelId, Status, From_Date, To_Date, strSwipeCardNo, strEmployeeName, strEmployeeCodeId, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(Session["parentLevelId"]), 0, 0);
            intCount = dtLeaveTransactionView.Rows.Count;
        }
        else
        {
            dtLeaveTransactionView = objLeaveApplicationEventData.GetLeaveSanctionDetails(Convert.ToInt32(Session["EmpAutoId"]), LevelId, Status, Convert.ToDateTime(From_Date), Convert.ToDateTime(To_Date), strSwipeCardNo, strEmployeeName, strEmployeeCodeId, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(Session["parentLevelId"]), 0, 0);
            intCount = dtLeaveTransactionView.Rows.Count;
        }
        return intCount;
    }

    public int bindLeaveSanctionDetailsForBulkApproval(string LevelId, string Status, string From_Date, string To_Date, string strSwipeCardNo, string strEmployeeName, string strEmployeeCodeId)
    {
        DataTable dtLeaveTransactionView = new DataTable();
        int intCount = 0;
        if (Convert.ToString(Session["UserGroupName"]) == "SuperAdmin" || Convert.ToString(Session["HasSpecialRights"]) == "True")
        {
            dtLeaveTransactionView = objLeaveApplicationEventData.GetLeaveSanctionDetailsForSuperAdmin(Convert.ToInt32(Session["EmpAutoID"]), LevelId, Status, From_Date, To_Date, strSwipeCardNo, strEmployeeName, strEmployeeCodeId, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(LevelId),0,0);
            intCount = dtLeaveTransactionView.Rows.Count;
        }
        else
        {
            dtLeaveTransactionView = objLeaveApplicationEventData.GetLeaveSanctionDetails(Convert.ToInt32(Session["EmpAutoId"]), LevelId, Session["LeaveSanctionStatus"].ToString(), Convert.ToDateTime(From_Date), Convert.ToDateTime(To_Date), strSwipeCardNo, strEmployeeName, strEmployeeCodeId, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(LevelId), 0, 0);
            intCount = dtLeaveTransactionView.Rows.Count;
        }
        return intCount;
    }
    public DateTime CurrentMonthStartDate()
    {
        CurrentDate = (DateTime.Now);
        CurrentDate = CurrentDate.AddDays(-(CurrentDate.AddDays(-1).Day));
        CurrentDate = CurrentDate.AddMonths(-1);
        return CurrentDate;
    }
    public DateTime NextMonthEndDate()
    {
        CurrentDate = (DateTime.Now);
        CurrentDate = CurrentDate.AddMonths(1);
        month = CurrentDate.Month;
        year = CurrentDate.Year;
        TotalDays = System.DateTime.DaysInMonth(year, month);
        date = CurrentDate.Day;
        date = TotalDays - date;
        CurrentDate = CurrentDate.AddDays(date);
        return CurrentDate;
    }
    public void CompareDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strFromDateTime = objClassCommon.DateTimeConvertion((gstrDateFormat), txtFromDate.Text, "D");
            strToDateTime = objClassCommon.DateTimeConvertion((gstrDateFormat), txtToDate.Text, "D");
            DateTime dtToDate = DateTime.Parse(strToDateTime);
            DateTime dtFromDate = DateTime.Parse(strFromDateTime);
            TimeSpan diff = dtToDate.Subtract(dtFromDate);
            int day = diff.Days;
            if (day >= 0)
            {
                e.IsValid = true;
            }
            else
            {
                base.Message(rm.GetString("Msg_GrearterThanFrmDate", ci));
                e.IsValid = false;
            }
        }
        catch
        {
        }
    }
    public void CompareCurrentdate(object s, ServerValidateEventArgs e)
    {
        try
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strFromDateTime = objClassCommon.DateTimeConvertion((gstrDateFormat), txtFromDate.Text, "D");
            DateTime dtCurrentDate = DateTime.Now;
            DateTime dtFromDate = DateTime.Parse(strFromDateTime);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day >= 0)
                e.IsValid = true;
            else
                e.IsValid = false;
        }
        catch
        {
        }
    }
    public void CompareTodate(object s, ServerValidateEventArgs e)
    {
        try
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strToDateTime = objClassCommon.DateTimeConvertion((gstrDateFormat), txtToDate.Text, "D");
            DateTime dtCurrentDate = DateTime.Now;
            DateTime dtFromDate = DateTime.Parse(strToDateTime);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day >= 0)
                e.IsValid = true;
            else
                e.IsValid = false;
        }
        catch
        {
        }
    }
    public int getParentCompany(int iEmployeeLevelID)
    {
        org_levelsEventData getOrg = new org_levelsEventData();
        getOrg.GetOrgParentLevels_FromID(iEmployeeLevelID);
        DataSet ds_getOrg = new DataSet();
        ds_getOrg = getOrg.GetOrgParentLevels();
        int iCompanyID = 0;
        if (ds_getOrg != null)
        {
            if (ds_getOrg.Tables[0].Rows.Count > 0)
            {
                iCompanyID = Convert.ToInt32(ds_getOrg.Tables[0].Rows[0]["LevelID"].ToString());
            }
        }
        return iCompanyID;
    }
    public string GetApprovalStatus(int intCompanyId, string strModule)
    {
        int approvalSettingsPageExists = 0;
        //Checking grade module is available or not and changed the id value from 110 to 228
        approvalSettingsPageExists = objClassCommon.CheckPageExistsInLicense(228);
        string strStatus = string.Empty;
        if (approvalSettingsPageExists == 1)
        {
            DataSet dsApprovalSettings = new DataSet();
            ShiftEvenData objShift = new ShiftEvenData();
            dsApprovalSettings = objShift.SelectApprovalSettings(intCompanyId);
            if (dsApprovalSettings != null)
            {
                if (dsApprovalSettings.Tables.Count > 0)
                {
                    if (dsApprovalSettings.Tables[0].Rows.Count > 0)
                    {
                        if (Convert.ToBoolean(dsApprovalSettings.Tables[0].Rows[0][strModule]) == false)
                        {
                            strStatus = "ApprovalNotRequired";
                        }
                        else
                        {
                            strStatus = "ApprovalRequired";
                        }
                    }
                    else
                    {
                        strStatus = "NotDefined";
                    }
                }
            }
        }
        else
        {
            strStatus = "ApprovalRequired";
        }
        return strStatus;
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_From.Text = rm.GetString("email_FromDate", ci);
        lbl_Status.Text = rm.GetString("email_Status", ci);
        lbl_To.Text = rm.GetString("email_ToDate", ci);
        cus_ComDate.ErrorMessage = rm.GetString("Msg_GrearterThanFrmDate", ci);
        lbl_Header.Text = rm.GetString("Menu_Transactions_LeaveSanction", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        btn_View.Text = "&nbsp;&nbsp;" + rm.GetString("btn_View", ci);
        lbl_EmployeeName.Text = rm.GetString("email_EmployeeName", ci);
        lbl_EmployeeId.Text = rm.GetString("email_EmployeeId", ci);
        lblEmpSwipe.Text = rm.GetString("TerminalDataType_Card_No", ci);
        ddlStatus.Items[0].Text = rm.GetString("ExportData_All", ci);
        ddlStatus.Items[1].Text = rm.GetString("btn_Approved", ci);
        ddlStatus.Items[2].Text = rm.GetString("btn_Forwarded", ci);
        ddlStatus.Items[3].Text = rm.GetString("btn_Pending", ci);
        ddlStatus.Items[4].Text = rm.GetString("btn_Reject", ci);
        lblFilter.Text = rm.GetString("lblFilter", ci);

    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(24, Convert.ToInt32(Session["userGroup_Id"]), "viewPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("TransactionLandingPage.aspx");
        }
        else
        {
        }
    }
    #endregion
    #region Button Events
    protected void btn_View_Click(object sender, EventArgs e)
    {
        if (Page.IsValid == true)
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            string strScript = string.Empty;
            int LevelId = Convert.ToInt32(Session["parentLevelId"]);
            int intCompanyId;
            string strStatus = string.Empty;
            string strModule = string.Empty;
            string strSwipeCardNo = string.Empty;
            string strEmployeeName = string.Empty;
            string strEmployeeCodeId = string.Empty;
            strSwipeCardNo = txtSwipeCard.Text;
            strEmployeeName = txt_EmpName.Text.Trim().Replace("'", "''");
            strEmployeeCodeId = txtEmployeeId.Text.Trim().Replace("'", "''");
            Session["strSwipeCardNo"] = null;
            Session["strEmployeeName"] = null;
            Session["strEmployeeCodeId"] = null;
            Session["LeaveFrom_Date"] = null;
            Session["LeaveTo_Date"] = null;
            Session["LeaveSanctionStatus"] = Convert.ToString(ddlStatus.SelectedValue);
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strModule = "LeaveSanction";
            if (Session["parentLevelId"] == null || Convert.ToInt32(Session["parentLevelId"]) == 0)
            {
                strStatus = rm.GetString("Alert_SelectCompany", ci);
                strScript = strScript + "alert('" + strStatus + "');";
                strScript = strScript + "</script>";
                Page.RegisterStartupScript("AlertManualApproval", strScript);
                return;
            }
            intCompanyId = getParentCompany(Convert.ToInt32(Session["parentLevelId"]));
            strStatus = GetApprovalStatus(intCompanyId, strModule);
            if (strStatus == "ApprovalNotRequired")
            {
                strStatus = rm.GetString("Alert_ApprovalNotRequiredforthiscompany", ci);
                strScript = strScript + "alert('" + strStatus + "');";
                strScript = strScript + "</script>";
                Page.RegisterStartupScript("AlertManualApproval", strScript);
                return;
            }
            else if (strStatus == "ApprovalRequired")
            {
            }
            else if (strStatus == "NotDefined")
            {
                strStatus = rm.GetString("Alert_ApprovalSettingsnotdefinedforthiscompany", ci);
                strScript = strScript + "alert('" + strStatus + "');";
                strScript = strScript + "</script>";
                Page.RegisterStartupScript("AlertManualApproval", strScript);
                return;
            }
            ;
            if (ddlStatus.SelectedItem.Text.ToString() == rm.GetString("ExportData_All", ci) || ddlStatus.SelectedItem.Text.ToString() == rm.GetString("btn_Approved", ci) || ddlStatus.SelectedItem.Text.ToString() == rm.GetString("btn_Reject", ci))
            {
                int intCount = 0;
                intCount = bindLeaveSanctionDetails(Convert.ToString(LevelId), ddlStatus.SelectedValue, objClassCommon.DateConvertion(gstrDateFormat, txtFromDate.Text), objClassCommon.DateConvertion(gstrDateFormat, txtToDate.Text), strSwipeCardNo, strEmployeeName, strEmployeeCodeId);
                if (intCount != 0)
                {
                    Session["LevelId"] = LevelId;
                    Response.Redirect("../Transactions/TA_UI_Transactions_LeaveSanctionView.aspx?LevelId=" + LevelId + "&Status=" + ddlStatus.SelectedValue + "&From_Date=" + txtFromDate.Text + "&To_Date=" + txtToDate.Text + "&strSwipeCardNo=" + strSwipeCardNo + "&strEmployeeName=" + strEmployeeName + "&strEmployeeCodeId=" + strEmployeeCodeId);

                }
                else
                {
                    strStatus = rm.GetString("Alert_Thereisnorecord", ci);
                    strScript = strScript + "alert('" + strStatus + "');";
                    strScript = strScript + "</script>";
                    Page.RegisterStartupScript("AlertManualApproval", strScript);
                    return;
                }

            }
            else if (ddlStatus.SelectedItem.Text.ToString() == rm.GetString("btn_Forwarded", ci) || ddlStatus.SelectedItem.Text.ToString() == rm.GetString("btn_Pending", ci))
            {
                int intCount = 0;
                intCount = bindLeaveSanctionDetailsForBulkApproval(Convert.ToString(LevelId), ddlStatus.SelectedValue, objClassCommon.DateConvertion(gstrDateFormat, txtFromDate.Text), objClassCommon.DateConvertion(gstrDateFormat, txtToDate.Text), strSwipeCardNo, strEmployeeName, strEmployeeCodeId);
                if (intCount != 0)
                {
                    Session["LevelId"] = LevelId;
                    Response.Redirect("../Transactions/TA_UI_Transactions_LeaveSanctionViewForBulkApproval.aspx?LevelId=" + LevelId + "&Status=" + ddlStatus.SelectedValue + "&From_Date=" + txtFromDate.Text + "&To_Date=" + txtToDate.Text + "&strSwipeCardNo=" + strSwipeCardNo + "&strEmployeeName=" + strEmployeeName + "&strEmployeeCodeId=" + strEmployeeCodeId);

                }
                else
                {
                    strStatus = rm.GetString("Alert_Thereisnorecord", ci);
                    strScript = strScript + "alert('" + strStatus + "');";
                    strScript = strScript + "</script>";
                    Page.RegisterStartupScript("AlertManualApproval", strScript);
                    return;
                }
            }
        }
    }
    #endregion
}
