Example #1
0
        public ActionResult ProjectEdit(long id)
        {
            ViewBag.Message = TempData["message"] ?? "";
            // int ProjectId = Convert.ToInt16(Session["ProjectId"]);
            ViewBag.GrantList        = DropdownList.GrantList();
            ViewBag.ThemeList        = DropdownList.ThemeList();
            ViewBag.RoleList         = DropdownList.RoleList();
            ViewBag.projectList      = DropdownList.ProjectListAdmin();
            ViewBag.ResearchList     = DropdownList.ResearchList();
            ViewBag.ResearchTypeList = DropdownList.ResearchTypeList();
            ViewBag.costList         = DropdownList.CostCenterList();
            ProjectListEdit GetDetails = DB.Database.SqlQuery <ProjectListEdit>(
                @"exec " + Constants.P_GetProject_Details_Edit + " @ProjectId",
                new object[] {
                new SqlParameter("@ProjectId", id)
            }).ToList().FirstOrDefault();

            ViewBag.ProjectId = id;
            ViewBag.GrantID   = GetDetails.ProjectGrant;
            //ViewBag.ResearchList = DropdownList.ResearchList();
            //ViewBag.ResearchTypeList = DropdownList.ResearchTypeList();
            ViewBag.ReasearchID = GetDetails.ResearchArea;
            ViewBag.TypeID      = GetDetails.TypeofResearch;
            return(View(GetDetails));
        }
Example #2
0
        public ActionResult UpdateProject(ProjectListEdit EPL)
        {
            try
            {
                List <KeyValuePair <KeyValuePair <long, string>, KeyValuePair <decimal, int> > > empPercentageStatus = new List <KeyValuePair <KeyValuePair <long, string>, KeyValuePair <decimal, int> > >();
                if (!string.IsNullOrEmpty(EPL.ProjectMembers))
                {
                    string[] empList  = EPL.ProjectMembers.Split(',');
                    string[] empNames = EPL.ProjectMembersNames.Split(',');
                    for (int i = 0; i < empList.Length; i++)
                    {
                        long       empId              = Convert.ToInt64(empList[i]);
                        string     empName            = empNames[i];
                        List <int> totalInvolvement   = DB.Employee.Where(x => x.EmployeeID == empId).Select(x => x.TotalInvolvement ?? 100).ToList();
                        decimal?   currentInvolvement = (from x in DB.ProjectEmployee
                                                         join y in DB.ProjectMaster on x.ProjectID equals y.ProjectID
                                                         where x.EmployeeID == empId && (x.EndDate >= DateTime.Now || !x.EndDate.HasValue) && x.ProjectID != EPL.ProjectID
                                                         select x.InvPercentage).Sum();

                        // = currentInvolvement + EPL.InvPercentage;
                        if (currentInvolvement + EPL.InvPercentage > totalInvolvement[0])
                        {
                            empPercentageStatus.Add(new KeyValuePair <KeyValuePair <long, string>, KeyValuePair <decimal, int> >(new KeyValuePair <long, string>(empId, empName), new KeyValuePair <decimal, int>(currentInvolvement.Value, totalInvolvement[0])));
                        }
                    }
                }
                StringBuilder errorMessage = new StringBuilder();
                if (empPercentageStatus.Any())
                {
                    foreach (KeyValuePair <KeyValuePair <long, string>, KeyValuePair <decimal, int> > item in empPercentageStatus)
                    {
                        errorMessage.Append($"Emplyee {item.Key.Value} has exceeded the maximum allocation , current allocation is {item.Value.Key} , maximum allocation is {item.Value.Value} and Available allocation is { item.Value.Value - item.Value.Key } line line");
                    }

                    ViewBag.Message = errorMessage.ToString();
                }
                if (EPL.Theme.HasValue)
                {
                    var eligibleThemeGrants = ConfigurationManager.AppSettings["ThemeGrandCodes"].ToString().Split(',').ToList();
                    var themeMstType        = Convert.ToInt32(ConfigurationManager.AppSettings["TypeGrant"]);
                    var themes = DB.MasterData.Where(x => x.MstTypeID == themeMstType && eligibleThemeGrants.Contains(x.MstCode)).Select(x => x.MstID);
                    if (!themes.Contains(EPL.ProjectGrant))
                    {
                        EPL.Theme = null;
                    }
                }

                if (EPL.IsRDProject == 1)
                {
                    IEnumerable <EmployeeProjectList> GetDetails = DB.Database.SqlQuery <EmployeeProjectList>(
                        @"exec " + Constants.P_UpdateProjectList + " @ProjectName,@ProjectId,@ProjectCode,@StartDate,@EndDate,@InternalOrder,@ProjectGrant,@IsRDProject,@Theme,@ResearchArea,@TypeofResearch,@CostCenter,@ProjectDesc",
                        new object[] {
                        new SqlParameter("@ProjectName", EPL.ProjectName),
                        new SqlParameter("@ProjectId", EPL.ProjectID),
                        new SqlParameter("@ProjectCode", EPL.ProjectCode),
                        new SqlParameter("@StartDate", EPL.StartDate),
                        new SqlParameter("@EndDate", EPL.EndDate),
                        new SqlParameter("@InternalOrder", EPL.InternalOrder),
                        new SqlParameter("@ProjectGrant", EPL.ProjectGrant),
                        new SqlParameter("@ResearchArea", EPL.ResearchArea),
                        new SqlParameter("@TypeofResearch", EPL.TypeofResearch),
                        new SqlParameter("@IsRDProject", EPL.IsRDProject),
                        new SqlParameter("@CostCenter", EPL.CostCentre),
                        new SqlParameter("@ProjectDesc", EPL.ProjectDesc ?? ""),
                        new SqlParameter("@Theme", (object)EPL.Theme ?? DBNull.Value),
                    }).ToList().Distinct();
                }
                else
                {
                    IEnumerable <EmployeeProjectList> GetDetails = DB.Database.SqlQuery <EmployeeProjectList>(
                        @"exec " + Constants.P_UpdateProjectList + " @ProjectName,@ProjectId,@ProjectCode,@StartDate,@EndDate,@InternalOrder,@ProjectGrant,@IsRDProject,@Theme,null,null,@CostCenter,@ProjectDesc",
                        new object[] {
                        new SqlParameter("@ProjectName", EPL.ProjectName),
                        new SqlParameter("@ProjectId", EPL.ProjectID),
                        new SqlParameter("@ProjectCode", EPL.ProjectCode),
                        new SqlParameter("@StartDate", EPL.StartDate),
                        new SqlParameter("@EndDate", EPL.EndDate),
                        new SqlParameter("@InternalOrder", EPL.InternalOrder),
                        new SqlParameter("@ProjectGrant", EPL.ProjectGrant),
                        new SqlParameter("@IsRDProject", EPL.IsRDProject),
                        new SqlParameter("@Theme", (object)EPL.Theme ?? DBNull.Value),
                        new SqlParameter("@CostCenter", EPL.CostCentre),
                        new SqlParameter("@ProjectDesc", EPL.ProjectDesc ?? ""),
                    }).ToList().Distinct();
                }


                // long projectID = Convert.ToInt64(Session["ProjectId"]);

                List <ProjectEmployeesModel> projectMembers = DB.ProjectEmployee.Where(x => x.ProjectID == EPL.ProjectID).ToList();

                foreach (ProjectEmployeesModel item in projectMembers)
                {
                    if (item.EndDate > EPL.EndDate)
                    {
                        item.EndDate         = EPL.EndDate;
                        DB.Entry(item).State = System.Data.Entity.EntityState.Modified;
                        DB.SaveChanges();
                    }
                }


                if (!string.IsNullOrEmpty(EPL.ProjectMembers))
                {
                    int      i     = 0;
                    string[] names = EPL.ProjectMembersNames.Split(',');
                    foreach (string item in EPL.ProjectMembers.Split(','))
                    {
                        long id = Convert.ToInt64(item);

                        if (!DB.ProjectEmployee.Any(x => x.ProjectID == EPL.ProjectID && x.EmployeeID == id) && !empPercentageStatus.Any(x => x.Key.Key == id))
                        {
                            EmployeeModel refRole = DB.Employee.FirstOrDefault(x => x.EmployeeID.ToString() == item);
                            List <EmployeeProjectList> GetDetails2 = DB.Database.SqlQuery <EmployeeProjectList>(
                                @"exec " + Constants.P_InsertProjectEmployee + " @ProjectId,@EmployeeID,@CheckRole,@InvPercentage,@RefRole,@StartDate,@EndDate,@Grant,@IsActive,@CreatedBy",
                                new object[] {
                                new SqlParameter("@ProjectId", EPL.ProjectID),
                                new SqlParameter("@EmployeeID", item),
                                new SqlParameter("@CheckRole", EPL.CheckRole),
                                new SqlParameter("@InvPercentage", EPL.InvPercentage),
                                new SqlParameter("@RefRole", EPL.IsRDProject == 1 ? (refRole.RoleID ?? 0) : 0),
                                new SqlParameter("@StartDate", EPL.MemberStartDate.HasValue ? EPL.MemberStartDate.Value : EPL.StartDate),
                                new SqlParameter("@EndDate", EPL.MemberEndDate.HasValue ? EPL.MemberEndDate.Value : EPL.EndDate),
                                new SqlParameter("@Grant", Convert.ToInt64(EPL.ProjectGrant)),
                                new SqlParameter("@IsActive", 1),
                                new SqlParameter("@CreatedBy", Session["EmployeeId"].ToString())
                            }).ToList();

                            if (EPL.IsRDProject == 1 && refRole.RoleID == null)
                            {
                                ViewBag.Message = ViewBag.Message + $"line Employee { SCTimeSheet.Models.Common.GetName(refRole.EmpFirstName, refRole.EmpLastName, refRole.EmpMiddleName) } Role is blank, please modify manually or contact administrator";
                            }

                            if (!string.IsNullOrEmpty(refRole.Email))
                            {
                                if (!DB.User.Any(x => x.Email == refRole.Email))
                                {
                                    UserModel user = new UserModel
                                    {
                                        Email       = refRole.Email,
                                        CreatedBy   = 1,
                                        CreatedDate = DateTime.Now,
                                        IsActive    = true,
                                        Password    = "******",
                                        RoleID      = EPL.CheckRole ? Convert.ToInt64(ReadConfig.GetValue("RolePM")) : Convert.ToInt64(ReadConfig.GetValue("RoleEmployee"))
                                    };
                                    DB.User.Add(user);
                                    DB.SaveChanges();

                                    refRole.UserID = user.UserID;
                                    DB.Employee.Attach(refRole);
                                    DB.Entry(refRole).State = System.Data.Entity.EntityState.Modified;
                                    DB.SaveChanges();
                                }
                            }
                            else
                            {
                                ViewBag.Message = ViewBag.Message + $"line Employee { SCTimeSheet.Models.Common.GetName(refRole.EmpFirstName, refRole.EmpLastName, refRole.EmpMiddleName) } email is blank, please contact administrator";
                            }
                        }
                        else
                        {
                            if (!empPercentageStatus.Any(x => x.Key.Key == id))
                            {
                                ViewBag.Message = ViewBag.Message + $"line { names[i] } already exist";
                            }
                        }
                        i++;
                    }
                }

                // return RedirectToAction("Index", "ProjectMain");
                ViewBag.Message = ViewBag.Message + $"line {EPL.ProjectName } is updated successfully";

                GetDefaults();
                ViewBag.ProjectId = EPL.ProjectID;
                ViewBag.GrantID   = EPL.ProjectGrant;

                ViewBag.ReasearchID = EPL.ResearchArea;
                ViewBag.TypeID      = EPL.TypeofResearch;

                //EPL.ProjectMembers = string.Join(",", empPercentageStatus.Select(x => x.Key.Key));
                //EPL.ProjectMembersNames = string.Join(",", empPercentageStatus.Select(x => x.Key.Value));
                EPL.ProjectMembers      = "";
                EPL.ProjectMembersNames = "";
                EPL.EmpSearchText       = "";
                EPL.CheckRole           = false;
                EPL.InvPercentage       = null;
                EPL.StartDate           = DateTime.Now;
                EPL.EndDate             = DateTime.Now;
                EPL.RefRole             = 0;
                return(View("ProjectEdit", EPL));
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog(ex);
                throw ex;
            }
        }
        public ActionResult UpdateProject(ProjectListEdit EPL)
        {
            try
            {
                JavaScriptSerializer json_serializer = new JavaScriptSerializer();
                var modifiedOrNewEmpList             = json_serializer.Deserialize <EmployeeProjectList[]>(EPL.EmployeeProjectList);

                StringBuilder errorMessage = new StringBuilder();

                if (EPL.Theme.HasValue)
                {
                    var eligibleThemeGrants = ConfigurationManager.AppSettings["ThemeGrandCodes"].ToString().Split(',').ToList();
                    var themeMstType        = Convert.ToInt32(ConfigurationManager.AppSettings["TypeGrant"]);
                    var themes = DB.MasterData.Where(x => x.MstTypeID == themeMstType && eligibleThemeGrants.Contains(x.MstCode)).Select(x => x.MstID);
                    if (!themes.Contains(EPL.ProjectGrant))
                    {
                        EPL.Theme = null;
                    }
                }

                if (EPL.IsRDProject == 1)
                {
                    IEnumerable <EmployeeProjectList> GetDetails = DB.Database.SqlQuery <EmployeeProjectList>(
                        @"exec " + Constants.P_UpdateProjectList + " @ProjectName,@ProjectId,@ProjectCode,@StartDate,@EndDate,@InternalOrder,@ProjectGrant,@IsRDProject,@Theme,@ResearchArea,@TypeofResearch,@CostCenter,@ProjectDesc",
                        new object[] {
                        new SqlParameter("@ProjectName", EPL.ProjectName),
                        new SqlParameter("@ProjectId", EPL.ProjectID),
                        new SqlParameter("@ProjectCode", EPL.ProjectCode),
                        new SqlParameter("@StartDate", EPL.StartDate),
                        new SqlParameter("@EndDate", EPL.EndDate),
                        new SqlParameter("@InternalOrder", EPL.InternalOrder),
                        new SqlParameter("@ProjectGrant", EPL.ProjectGrant),
                        new SqlParameter("@ResearchArea", EPL.ResearchArea),
                        new SqlParameter("@TypeofResearch", EPL.TypeofResearch),
                        new SqlParameter("@IsRDProject", EPL.IsRDProject),
                        new SqlParameter("@CostCenter", EPL.CostCentre),
                        new SqlParameter("@ProjectDesc", EPL.ProjectDesc ?? ""),
                        new SqlParameter("@Theme", (object)EPL.Theme ?? DBNull.Value),
                    }).ToList().Distinct();
                }
                else
                {
                    IEnumerable <EmployeeProjectList> GetDetails = DB.Database.SqlQuery <EmployeeProjectList>(
                        @"exec " + Constants.P_UpdateProjectList + " @ProjectName,@ProjectId,@ProjectCode,@StartDate,@EndDate,@InternalOrder,@ProjectGrant,@IsRDProject,@Theme,null,null,@CostCenter,@ProjectDesc",
                        new object[] {
                        new SqlParameter("@ProjectName", EPL.ProjectName),
                        new SqlParameter("@ProjectId", EPL.ProjectID),
                        new SqlParameter("@ProjectCode", EPL.ProjectCode),
                        new SqlParameter("@StartDate", EPL.StartDate),
                        new SqlParameter("@EndDate", EPL.EndDate),
                        new SqlParameter("@InternalOrder", EPL.InternalOrder),
                        new SqlParameter("@ProjectGrant", EPL.ProjectGrant),
                        new SqlParameter("@IsRDProject", EPL.IsRDProject),
                        new SqlParameter("@Theme", (object)EPL.Theme ?? DBNull.Value),
                        new SqlParameter("@CostCenter", EPL.CostCentre),
                        new SqlParameter("@ProjectDesc", EPL.ProjectDesc ?? ""),
                    }).ToList().Distinct();
                }
                List <ProjectEmployeesModel> projectMembers = DB.ProjectEmployee.Where(x => x.ProjectID == EPL.ProjectID).ToList();

                foreach (ProjectEmployeesModel item in projectMembers)
                {
                    if (item.EndDate > EPL.EndDate)
                    {
                        item.EndDate         = EPL.EndDate;
                        DB.Entry(item).State = System.Data.Entity.EntityState.Modified;
                        DB.SaveChanges();
                    }
                }
                foreach (var item in modifiedOrNewEmpList)
                {
                    if (!DB.ProjectEmployee.Any(x => x.ProjectID == EPL.ProjectID && x.EmployeeID == item.EmployeeID))
                    {
                        EmployeeModel refRole = DB.Employee.FirstOrDefault(x => x.EmployeeID == item.EmployeeID);
                        List <EmployeeProjectList> GetDetails2 = DB.Database.SqlQuery <EmployeeProjectList>(
                            @"exec " + Constants.P_InsertProjectEmployee + " @ProjectId,@EmployeeID,@CheckRole,@InvPercentage,@RefRole,@StartDate,@EndDate,@Grant,@IsActive,@CreatedBy",
                            new object[] {
                            new SqlParameter("@ProjectId", EPL.ProjectID),
                            new SqlParameter("@EmployeeID", item.EmployeeID),
                            new SqlParameter("@CheckRole", item.IsManager),
                            new SqlParameter("@InvPercentage", item.InvPercentage),
                            new SqlParameter("@RefRole", EPL.IsRDProject == 1 ? (refRole.RoleID ?? 0) : 0),
                            new SqlParameter("@StartDate", item.StartDate),
                            new SqlParameter("@EndDate", item.EndDate),
                            new SqlParameter("@Grant", Convert.ToInt64(EPL.ProjectGrant)),
                            new SqlParameter("@IsActive", 1),
                            new SqlParameter("@CreatedBy", Session["EmployeeId"].ToString())
                        }).ToList();

                        if (EPL.IsRDProject == 1 && refRole.RoleID == null)
                        {
                            ViewBag.Message = ViewBag.Message + $"line Employee { SCTimeSheet.Models.Common.GetName(refRole.EmpFirstName, refRole.EmpLastName, refRole.EmpMiddleName) } Role is blank, please modify manually or contact administrator";
                        }

                        if (!string.IsNullOrEmpty(refRole.Email))
                        {
                            if (!DB.User.Any(x => x.Email == refRole.Email))
                            {
                                UserModel user = new UserModel
                                {
                                    Email       = refRole.Email,
                                    CreatedBy   = 1,
                                    CreatedDate = DateTime.Now,
                                    IsActive    = true,
                                    Password    = "******",
                                    RoleID      = item.IsManager ? Convert.ToInt64(ReadConfig.GetValue("RolePM")) : Convert.ToInt64(ReadConfig.GetValue("RoleEmployee"))
                                };
                                DB.User.Add(user);
                                DB.SaveChanges();

                                refRole.UserID = user.UserID;
                                DB.Employee.Attach(refRole);
                                DB.Entry(refRole).State = System.Data.Entity.EntityState.Modified;
                                DB.SaveChanges();
                            }
                        }
                        else
                        {
                            ViewBag.Message = ViewBag.Message + $"line Employee { SCTimeSheet.Models.Common.GetName(refRole.EmpFirstName, refRole.EmpLastName, refRole.EmpMiddleName) } email is blank, please contact administrator";
                        }
                    }
                    else
                    {
                        var emp = DB.ProjectEmployee.FirstOrDefault(x => x.ProjectID == EPL.ProjectID && x.EmployeeID == item.EmployeeID);
                        emp.InvPercentage   = item.InvPercentage;
                        emp.StartDate       = item.StartDate;
                        emp.EndDate         = item.EndDate;
                        emp.CheckRole       = item.IsManager;
                        emp.RefRole         = item.EmpRole;
                        DB.Entry(emp).State = System.Data.Entity.EntityState.Modified;
                        DB.SaveChanges();
                    }
                }
                // return RedirectToAction("Index", "ProjectMain");
                ViewBag.Message = ViewBag.Message + $"line {EPL.ProjectName } is updated successfully";

                GetDefaults();
                ViewBag.ProjectId = EPL.ProjectID;
                ViewBag.GrantID   = EPL.ProjectGrant;

                ViewBag.ReasearchID = EPL.ResearchArea;
                ViewBag.TypeID      = EPL.TypeofResearch;

                //EPL.ProjectMembers = string.Join(",", empPercentageStatus.Select(x => x.Key.Key));
                //EPL.ProjectMembersNames = string.Join(",", empPercentageStatus.Select(x => x.Key.Value));
                EPL.EmpSearchText = "";
                return(View("ProjectEdit", EPL));
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog(ex);
                throw ex;
            }
        }