Ejemplo n.º 1
0
        protected void BtnRegisterClick(object sender, EventArgs e)
        {
            string dOP = ((TextBox)(DtDOP.Controls[0])).Text;
            string dOJ = ((TextBox)(DtDoj.Controls[0])).Text;

            // string dOB = ((TextBox)(DtDOB.Controls[0])).Text;

            if (SPContext.Current != null)
            {
                if (txtfristname.Text == "")
                {
                    LblError.Text  = "All Fields are required field.";
                    LblError.Text += "<br/>";
                    return;
                }
                if (txtlastname.Text == "")
                {
                    LblError.Text  = "All Fields are required field.";
                    LblError.Text += "<br/>";
                    return;
                }
                if (txtempid.Text == "")
                {
                    LblError.Text  = "All Fields are required field.";
                    LblError.Text += "<br/>";
                    return;
                }
                if (DdlDep.SelectedIndex < 1)
                {
                    LblError.Text  = "All Fields are required field.";
                    LblError.Text += "<br/>";
                    return;
                }
                if (DdlDesignation.SelectedIndex < 1)
                {
                    LblError.Text  = "All Fields are required field.";
                    LblError.Text += "<br/>";
                    return;
                }

                if (DdlEmptype.SelectedIndex < 1)
                {
                    LblError.Text  = "All Fields are required field.";
                    LblError.Text += "<br/>";
                    return;
                }

                if (string.IsNullOrEmpty(dOJ))
                {
                    LblError.Text  = "All Fields are required field.";
                    LblError.Text += "<br/>";
                    return;
                }
                if (!ValidateDate(dOJ, "MM/dd/yyyy"))
                {
                    LblError.Text  = "Date format should be in 'MM/DD/YYYY' format.";
                    LblError.Text += "<br/>";
                    DtDoj.ClearSelection();
                    DtDoj.Focus();
                    return;
                }
                else if (DateTime.Parse(dOJ) > DateTime.Now)
                {
                    LblError.Text  = "DOJ should be Less than or equal to today.";
                    LblError.Text += "<br/>";
                    DtDoj.ClearSelection();
                    DtDoj.Focus();
                    return;
                }
                if (DdlEmptype.SelectedItem.Value.Trim() == "Permanent")
                {
                    if (string.IsNullOrEmpty(dOP))
                    {
                        LblError.Text  = "All Fields are required field.";
                        LblError.Text += "<br/>";
                        divdop.Visible = true;
                        return;
                    }
                    else if (!ValidateDate(dOP, "MM/dd/yyyy"))
                    {
                        LblError.Text  = "Date format should be in 'MM/DD/YYYY' format.";
                        LblError.Text += "<br/>";
                        divdop.Visible = true;
                        DtDOP.ClearSelection();
                        DtDOP.Focus();
                        return;
                    }
                    else if (DateTime.Parse(dOP) > DateTime.Now)
                    {
                        LblError.Text  = "DOP should be Less than or equal to today.";
                        LblError.Text += "<br/>";
                        divdop.Visible = true;
                        DtDOP.ClearSelection();
                        DtDOP.Focus();
                        return;
                    }
                    else if (DateTime.Parse(dOP) < DateTime.Parse(dOJ))
                    {
                        LblError.Text  = "DOP should be greater than or equal to DOJ.";
                        LblError.Text += "<br/>";
                        return;
                    }
                }
                try
                {
                    using (var site = new SPSite(SPContext.Current.Site.Url))
                    {
                        using (var web = site.OpenWeb())
                        {
                            var list = web.Lists.TryGetList(Utilities.EmployeeScreen);
                            web.AllowUnsafeUpdates = true;

                            var newItem  = list.Items.Add();
                            var empdupID = txtempid.Text;
                            var query    = new SPQuery
                            {
                                Query = @"<Where>
                                                            <Eq>
                                                       <order><FieldRef Name='Title' />
                                                                     <Value Type='Text'></order>" +
                                        empdupID + @"</Value>
                                                            </Eq>
                                                     </Where>"
                            };
                            var collection = list.GetItems(query);
                            if (collection.Count == 0)
                            {
                                newItem[Utilities.EmployeeId] = txtempid.Text;
                                int iPeopleTeam = peoplepickeremp.ResolvedEntities.Count;
                                if (iPeopleTeam > 0)
                                {
                                    string u = string.Empty;

                                    for (int k = 0; k < peoplepickeremp.ResolvedEntities.Count; k++)
                                    {
                                        PickerEntity selectedEntity0 = (PickerEntity)peoplepickeremp.ResolvedEntities[k];
                                        SPUser       user            = SPContext.Current.Web.EnsureUser(selectedEntity0.Key);
                                        u += user + ";";
                                    }

                                    newItem[Utilities.EmployeeName] = UserValueCollection(web, u);
                                }
                                //newItem[Utilities.EmployeeName] = SPContext.Current.Web.AllUsers[peoplepickeremp.Accounts[0].ToString()];
                                newItem[Utilities.FirstName] = txtfristname.Text;
                                newItem[Utilities.LastName]  = txtlastname.Text;
                                newItem["Employee Type"]     = DdlEmptype.SelectedValue;
                                newItem["Department"]        = DdlDep.SelectedValue;
                                newItem["Designation"]       = DdlDesignation.SelectedValue;
                                //newItem[Utilities.Email] = Txtmail.Text;
                                //newItem[Utilities.Mobile] = TxtContact.Text;
                                newItem[Utilities.DateofJoin] = DateTime.Parse(GetFormatedDate(dOJ));// DtDoj.SelectedDate;
                                //newItem[Utilities.DOB] = DateTime.Parse(GetFormatedDate(dOB)); //DtDOB.SelectedDate;
                                if (DdlEmptype.SelectedItem.Text.Trim() == "Permanent")
                                {
                                    newItem["Date of Permanent"] = DateTime.Parse(GetFormatedDate(dOP));//DtDOP.SelectedDate;
                                }
                                var entity = new PickerEntity
                                {
                                    DisplayText =
                                        new SPFieldUserValue(web, ddlReportingTo.SelectedItem.Value)
                                        .
                                        User.
                                        LoginName
                                };
                                newItem[Utilities.Manager] = SPContext.Current.Web.AllUsers[entity.DisplayText];
                                newItem["Status"]          = "Active";
                                newItem.Update();
                                AddUsertoEmployeeGroup();
                                var leavelist = web.Lists.TryGetList(Utilities.LeaveDays);
                                var field     = leavelist.Fields["Employee Type"];

                                var leaveDaysQuery = new SPQuery
                                {
                                    Query =
                                        @"<Where>
                                                <Eq>
                                                    <FieldRef Name='" + field.InternalName + @"' />
                                                    <Value Type='Lookup'>" + DdlEmptype.SelectedItem.Text + @"</Value>
                                                </Eq>
                                          </Where>"
                                };
                                var leaveDayscollection = leavelist.GetItems(leaveDaysQuery);


                                foreach (SPListItem leaveType in leaveDayscollection)
                                {
                                    var empLeavelist = web.Lists.TryGetList(Utilities.EmployeeLeaves);
                                    var empLeaveItem = empLeavelist.Items.Add();
                                    var emptype      = new SPFieldLookupValue(leaveType["Employee Type"].ToString());
                                    var lveType      = new SPFieldLookupValue(leaveType["Leave Type"].ToString());

                                    empLeaveItem["Employee ID"]          = txtempid.Text;
                                    empLeaveItem[Utilities.EmployeeName] =
                                        web.AllUsers[peoplepickeremp.Accounts[0].ToString()];
                                    empLeaveItem["Leave Type"] = leaveType["Leave Type"].ToString();

                                    if (lveType.LookupValue == "Comp off")
                                    {
                                        empLeaveItem["Leave Balance"] = 0;
                                    }
                                    else if (lveType.LookupValue == "LOP")
                                    {
                                        empLeaveItem["Leave Balance"] = 0;
                                    }
                                    else if (lveType.LookupValue == "Optional")
                                    {
                                        empLeaveItem["Leave Balance"] =
                                            decimal.Parse(leaveType["Leave Days"].ToString());
                                    }
                                    else
                                    {
                                        if (emptype.LookupValue.Trim() == "Permanent")
                                        {
                                            int     dopMonth  = DateTime.Parse(GetFormatedDate(dOP)).Month;// DtDOP.SelectedDate.Month;
                                            int     monthDiff = GetMonthDifference(dopMonth);
                                            decimal leaves    = decimal.Parse(leaveType["Leave Days"].ToString()) *
                                                                monthDiff;
                                            if (leaves % 1 == 0)
                                            {
                                                int noOfleaves = Convert.ToInt16(leaves);
                                                empLeaveItem["Leave Balance"] = noOfleaves;
                                            }
                                            else
                                            {
                                                empLeaveItem["Leave Balance"] = leaves;
                                            }
                                        }
                                        else
                                        {
                                            if (lveType.LookupValue.Trim() != "Casual Leave" && lveType.LookupValue.Trim() != "Sick Leave")
                                            {
                                                decimal  leaves   = decimal.Parse(leaveType["Leave Days"].ToString());
                                                DateTime today    = DateTime.Today;
                                                DateTime JoinDate = DtDoj.SelectedDate;
                                                if (JoinDate.Year == today.Year)
                                                {
                                                    if (JoinDate.Month == today.Month)
                                                    {
                                                        if (today.Month != 4)
                                                        {
                                                            if (leaves % 1 == 0)
                                                            {
                                                                int noOfleaves = Convert.ToInt16(leaves);
                                                                empLeaveItem["Leave Balance"] = noOfleaves;
                                                            }
                                                            else
                                                            {
                                                                empLeaveItem["Leave Balance"] = leaves;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            empLeaveItem["Leave Balance"] = 0;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (JoinDate.Day <= 15)
                                                        {
                                                            if (JoinDate.Month != 4)
                                                            {
                                                                empLeaveItem["Leave Balance"] = today.Subtract(JoinDate).Days / (365 / 12) + 1;
                                                            }
                                                            else
                                                            {
                                                                //    empLeaveItem["Leave Balance"] = today.Subtract(JoinDate).Days / (365 / 12) + 2;
                                                                empLeaveItem["Leave Balance"] = 0;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            empLeaveItem["Leave Balance"] = today.Subtract(JoinDate).Days / (365 / 12) + 1;
                                                        }
                                                    }
                                                }

                                                else
                                                {
                                                    if (JoinDate.Day < 15)
                                                    {
                                                        empLeaveItem["Leave Balance"] = today.Subtract(JoinDate).Days / (365 / 12) + 2;
                                                    }
                                                    else
                                                    {
                                                        empLeaveItem["Leave Balance"] = today.Subtract(JoinDate).Days / (365 / 12) + 1;
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                empLeaveItem["Leave Balance"] = 0;
                                            }
                                        }
                                    }

                                    empLeaveItem["Leave utilized"]    = 0;
                                    empLeaveItem["Leave Requested"]   = 0;
                                    empLeaveItem["Reporting Manager"] = web.AllUsers[entity.DisplayText];
                                    empLeaveItem[Utilities.Year]      = hdnCurrentYear.Value;
                                    empLeaveItem["Employee Type"]     = DdlEmptype.SelectedValue;
                                    empLeaveItem["Employee Status"]   = "Active";//This new line add now(5/4/17)
                                    empLeaveItem.Update();
                                }

                                Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup(); SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, '1');</script>");

                                //Context.Response.Write("<script type='text/javascript'>window.Location = window.Location;</script>");
                                Context.Response.Flush();
                                Context.Response.End();

                                //Response.Redirect(SPContext.Current.Web.Url);

                                web.AllowUnsafeUpdates = false;
                            }

                            else
                            {
                                LblError.Text  = "Employee id is already exists.";
                                LblError.Text += "<br/>";
                            }
                        }
                    }
                }

                catch (Exception ex)
                {
                    LblError.Text = ex.Message;
                }
                // }
            }
        }
Ejemplo n.º 2
0
        protected void BtnUpdateClick(object sender, EventArgs e)
        {
            string dOP = ((TextBox)(DtDOP.Controls[0])).Text;
            string dOJ = ((TextBox)(DtDoj.Controls[0])).Text;

            if (string.IsNullOrEmpty(dOJ))
            {
                LblError.Text  = "Please Enter the valid DOJ";
                LblError.Text += "<br/>";
                // divdop.Visible = true;
                return;
            }
            if (!ValidateDate(dOJ, "MM/dd/yyyy"))
            {
                LblError.Text  = "Date format should be in 'MM/DD/YYYY' format.";
                LblError.Text += "<br/>";
                DtDoj.ClearSelection();
                DtDoj.Focus();
                return;
            }
            else if (DateTime.Parse(dOJ) > DateTime.Now)
            {
                LblError.Text  = "DOJ should be Less than or equal to today.";
                LblError.Text += "<br/>";
                divdop.Visible = true;
                DtDoj.ClearSelection();
                DtDoj.Focus();
                return;
            }
            if (DdlEmptype.SelectedItem.Value.Trim() == "Permanent")
            {
                if (string.IsNullOrEmpty(dOP))
                {
                    LblError.Text  = "Please Enter the valid DOP.";
                    LblError.Text += "<br/>";
                    divdop.Visible = true;
                    return;
                }
                else if (!ValidateDate(dOP, "MM/dd/yyyy"))
                {
                    LblError.Text  = "Date format should be in 'MM/DD/YYYY' format.";
                    LblError.Text += "<br/>";
                    divdop.Visible = true;
                    DtDOP.ClearSelection();
                    DtDOP.Focus();
                    return;
                }
                else if (DateTime.Parse(dOP) > DateTime.Now)
                {
                    LblError.Text  = "DOP should be Less than or equal to today.";
                    LblError.Text += "<br/>";
                    divdop.Visible = true;
                    DtDOP.ClearSelection();
                    DtDOP.Focus();
                    return;
                }
                else if (DateTime.Parse(dOP) < DateTime.Parse(dOJ))
                {
                    LblError.Text  = "DOP should be greater than or equal to DOJ.";
                    LblError.Text += "<br/>";
                    return;
                }
            }

            try
            {
                using (var site = new SPSite(SPContext.Current.Site.Url))
                {
                    using (var web = site.OpenWeb())
                    {
                        web.AllowUnsafeUpdates = true;
                        var empid   = empSPid.Value;
                        var emplist = web.Lists.TryGetList("Employee Screen");
                        var empitem = emplist.GetItemById(Convert.ToInt16(empid));

                        empitem["First Name"]         = txtfristname.Text;
                        empitem["Last Name"]          = txtlastname.Text;
                        empitem["Employee Type"]      = DdlEmptype.SelectedItem.Text;
                        empitem["Department"]         = DdlDep.SelectedItem.Text;
                        empitem["Designation"]        = DdlDesignation.SelectedItem.Text;
                        empitem[Utilities.DateofJoin] = DateTime.Parse(GetFormatedDate(dOJ));// DtDoj.SelectedDate;
                        if (DdlEmptype.SelectedItem.Text.Trim() == "Permanent")
                        {
                            empitem["Date of Permanent"] = DateTime.Parse(GetFormatedDate(dOP));//DtDOP.SelectedDate;
                        }
                        empitem["OldManager"] = empitem[Utilities.Manager];
                        PickerEntity entity = new PickerEntity();
                        entity.DisplayText         = new SPFieldUserValue(web, ddlReportingTo.SelectedItem.Value).User.LoginName;
                        empitem[Utilities.Manager] = web.AllUsers[entity.DisplayText];
                        empitem.Update();

                        var leavelist      = web.Lists.TryGetList(Utilities.LeaveDays);
                        var field          = leavelist.Fields["Employee Type"];
                        var leaveDaysQuery = new SPQuery
                        {
                            Query =
                                @"<Where>
                                                <Eq>
                                                    <FieldRef Name='" +
                                field.InternalName + @"' />
                                                    <Value Type='Lookup'>" +
                                DdlEmptype.SelectedItem.Text + @"</Value>
                                                    </Eq>
                                                     </Where>"
                        };
                        var leaveDayscollection = leavelist.GetItems(leaveDaysQuery);

                        foreach (SPListItem leavedaItem in leaveDayscollection)
                        {
                            var emptype           = new SPFieldLookupValue(leavedaItem["Employee Type"].ToString());
                            var leaveType         = new SPFieldLookupValue(leavedaItem["Leave Type"].ToString());
                            var employeeleavelist = web.Lists.TryGetList(Utilities.EmployeeLeaves);

                            var lveType = new SPFieldLookupValue(leavedaItem["Leave Type"].ToString());

                            var empleaveCollection = GetListItemCollection(employeeleavelist, "Employee ID",
                                                                           txtempid.Text, "Leave Type",
                                                                           leaveType.LookupValue, "Year",
                                                                           hdnCurrentYear.Value);

                            if (empleaveCollection.Count == 0)
                            {
                                var empLeaveItem = employeeleavelist.Items.Add();

                                empLeaveItem["Employee ID"]          = txtempid.Text;
                                empLeaveItem[Utilities.EmployeeName] = web.AllUsers[txtempusername.Text];
                                empLeaveItem["Leave Type"]           = leavedaItem["Leave Type"].ToString();
                                if (lveType.LookupValue == "Comp off")
                                {
                                    empLeaveItem["Leave Balance"] = 0;
                                }
                                else if (lveType.LookupValue == "Optional")
                                {
                                    empLeaveItem["Leave Balance"] =
                                        decimal.Parse(leavedaItem["Leave Days"].ToString());
                                }
                                else if (lveType.LookupValue == "LOP")
                                {
                                    empLeaveItem["Leave Balance"] = 0;
                                }
                                else
                                {
                                    if (emptype.LookupValue.Trim() == "Permanent")
                                    {
                                        int     dopMonth  = DateTime.Parse(GetFormatedDate(dOP)).Month;// DtDOP.SelectedDate.Month;
                                        int     monthDiff = GetMonthDifference(dopMonth);
                                        decimal leaves    = decimal.Parse(leavedaItem["Leave Days"].ToString()) *
                                                            monthDiff;
                                        if (leaves % 1 == 0)
                                        {
                                            int noOfleaves = Convert.ToInt16(leaves);
                                            empLeaveItem["Leave Balance"] = noOfleaves;
                                        }
                                        else
                                        {
                                            empLeaveItem["Leave Balance"] = leaves;
                                        }
                                    }
                                    else
                                    {
                                        decimal leaves = decimal.Parse(leavedaItem["Leave Days"].ToString()) *
                                                         1;
                                        if (leaves % 1 == 0)
                                        {
                                            int noOfleaves = Convert.ToInt16(leaves);
                                            empLeaveItem["Leave Balance"] = noOfleaves;
                                        }
                                        else
                                        {
                                            empLeaveItem["Leave Balance"] = leaves;
                                        }
                                    }
                                }
                                empLeaveItem["Leave utilized"]    = 0;
                                empLeaveItem["Leave Requested"]   = 0;
                                empLeaveItem["Reporting Manager"] = web.AllUsers[entity.DisplayText];
                                empLeaveItem[Utilities.Year]      = hdnCurrentYear.Value;
                                empLeaveItem["Employee Type"]     = DdlEmptype.SelectedValue;
                                empLeaveItem["Employee Status"]   = "Active";
                                empLeaveItem.Update();
                            }
                            else
                            {
                                if (hdnCurrentEmpType.Value.Trim() != "Permanent")
                                {
                                    foreach (SPListItem empleaveItem in empleaveCollection)
                                    {
                                        if (chkPrePL.Checked)
                                        {
                                            if (lveType.LookupValue != "Comp off" &&
                                                lveType.LookupValue != "Optional" && lveType.LookupValue != "LOP")
                                            {
                                                if (emptype.LookupValue.Trim() == "Permanent")
                                                {
                                                    int dopMonth = DateTime.Parse(GetFormatedDate(dOP)).Month;
                                                    //DtDOP.SelectedDate.Month;
                                                    int     monthDiff = GetMonthDifference(dopMonth) - 1;
                                                    decimal leaves    =
                                                        decimal.Parse(empleaveItem["Leave Balance"].ToString()) +
                                                        decimal.Parse(leavedaItem["Leave Days"].ToString()) *
                                                        monthDiff;
                                                    if (leaves % 1 == 0)
                                                    {
                                                        int noOfleaves = Convert.ToInt16(leaves);
                                                        empleaveItem["Leave Balance"] = noOfleaves;
                                                    }
                                                    else
                                                    {
                                                        empleaveItem["Leave Balance"] = leaves;
                                                    }
                                                }
                                                else
                                                {
                                                    decimal leaves =
                                                        decimal.Parse(empleaveItem["Leave Balance"].ToString()) +
                                                        decimal.Parse(leavedaItem["Leave Days"].ToString());
                                                    if (leaves % 1 == 0)
                                                    {
                                                        int noOfleaves = Convert.ToInt16(leaves);
                                                        empleaveItem["Leave Balance"] = noOfleaves;
                                                    }
                                                    else
                                                    {
                                                        empleaveItem["Leave Balance"] = leaves;
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (lveType.LookupValue != "Comp off" &&
                                                lveType.LookupValue != "Optional" && lveType.LookupValue != "LOP")
                                            {
                                                if (emptype.LookupValue.Trim() == "Permanent")
                                                {
                                                    int dopMonth = DateTime.Parse(GetFormatedDate(dOP)).Month;
                                                    // DtDOP.SelectedDate.Month;
                                                    int     monthDiff = GetMonthDifference(dopMonth);
                                                    decimal leaves    =
                                                        decimal.Parse(leavedaItem["Leave Days"].ToString()) *
                                                        monthDiff;
                                                    //if (lveType.LookupValue == "Paid Leave")
                                                    //    leaves = leaves + 1;
                                                    if (leaves % 1 == 0)
                                                    {
                                                        int noOfleaves = Convert.ToInt16(leaves);
                                                        empleaveItem["Leave Balance"] = noOfleaves;
                                                    }
                                                    else
                                                    {
                                                        empleaveItem["Leave Balance"] = leaves;
                                                    }
                                                }
                                                else
                                                {
                                                    decimal leaves =
                                                        decimal.Parse(leavedaItem["Leave Days"].ToString());
                                                    //if (lveType.LookupValue == "Paid Leave")
                                                    //    leaves = leaves + 1;
                                                    if (leaves % 1 == 0)
                                                    {
                                                        int noOfleaves = Convert.ToInt16(leaves);
                                                        empleaveItem["Leave Balance"] = noOfleaves;
                                                    }
                                                    else
                                                    {
                                                        empleaveItem["Leave Balance"] = leaves;
                                                    }
                                                }
                                            }
                                        }
                                        empleaveItem[Utilities.Year]  = hdnCurrentYear.Value;
                                        empleaveItem["Employee Type"] = DdlEmptype.SelectedValue;

                                        empleaveItem.Update();
                                    }
                                }
                            }
                        }

                        Context.Response.Write(
                            "<script type='text/javascript'>window.frameElement.commitPopup(); SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, '1');</script>");
                    }
                }
            }
            catch (Exception ex)
            {
                LblError.Text = ex.Message;
            }
        }