Exemple #1
0
        public ActionResult RmUser(int id)
        {
            if (User.IsInRole("RM"))
            {
                Session["BreadcrumbList"] = Utils.HtmlExtensions.SetBreadcrumbs((List<BreadcrumbModel>)Session["BreadcrumbList"], string.Format("/User/RmUser/{0}", id), "Home");
            }
            else
            {
                var name = _userService.GetUser(id).UserRegionMaps.First().Region.Name;
                Session["BreadcrumbList"] = Utils.HtmlExtensions.SetBreadcrumbs((List<BreadcrumbModel>)Session["BreadcrumbList"], string.Format("/User/RmUser/{0}", id), name);
                ViewBag.UserName = name;
            }
            var currentDate = DateTime.Now;
            var currentMonth = _masterService.FindAndCreateMonth(currentDate.ToString("MMMM"), currentDate.Year);
            var rm = _userService.GetUser(id);
            var rsmUsers = _userService.FindChilds(new List<int> { id });
            var products = _masterService.GetAllProducts().OrderBy(x => x.Id).ToList();
            var trainingLevels = Enumeration.GetAll<TrainingLevel>().ToList();
            var reportList = new List<ReportManpowerModel>();
            var yearMonths = _masterService.FindMonths(year: currentDate.Year).ToList();
            var csmUsers = _userService.FindChilds(rsmUsers.Select(x => x.Id).ToList());
            var csmIds = csmUsers.Select(x => x.Id).ToList();
            var monthCharts = new Dictionary<string, GraphModel>();
            var existMonths = new List<Month>();
            var currentYear = new DateTime(currentDate.Year, 1, 1);
            var productMonthManPower = products.ToDictionary(x => x.Id, x => 0);
            var allProductMonthManPower = products.ToDictionary(x => x.Id, x => 0);
            var janMonthManpowers = products.ToDictionary(x => x.Id, x => 0);
            for (int i = 1; i < currentDate.Month + 1; i++)
            {
                var existmonth = new DateTime(currentDate.Year, i, 15);
                var month = yearMonths.SingleOrDefault(x => x.Name.Equals(existmonth.ToString("MMMM")) && x.Year == currentDate.Year);
                if (month == null)
                    month = new Month { Year = currentDate.Year, Name = existmonth.ToString("MMMM") };
                existMonths.Add(month);
            }
            var janMonth = existMonths.Single(x => x.Name == "January");
            DateTime currentmonth;
            DateTime nextMonth;
            foreach (var month in existMonths)
            {
                currentmonth = DateTime.Parse(string.Format("1 {0} {1}", month.Name, month.Year));
                nextMonth = currentmonth.AddMonths(1);
                if (month.Id != 0)
                {
                    var monthlyManPowerTarget = _manpowerTargetService.FindUserManpowerMonthTarget(csmIds, new List<int> { month.Id });
                    var totalManPower = _manpowerService.FindAllUserManpowers(csmIds, string.Empty).ToList();
                    var monthlyManPower =
                        totalManPower.Where(
                            x =>
                            x.ObjectInfo.CreatedDate.Ticks < nextMonth.Ticks &&
                            (x.Profile.DateOfLeaving == null ||
                             x.Profile.DateOfLeaving.Value.Ticks >= currentmonth.Ticks)).ToList();
                    var groupProductMonthManPower = monthlyManPower.GroupBy(x => x.ProductId).ToList();
                    foreach (var product in products)
                    {
                        var monthProductManpower = groupProductMonthManPower.SingleOrDefault(x => x.Key == product.Id);
                        if (month.Name != currentDate.ToString("MMMM"))
                        {
                            productMonthManPower[product.Id] = productMonthManPower[product.Id] + (monthProductManpower != null ? monthProductManpower.Count() : 0);
                        }
                        if (month.Name == "January")
                        {
                            janMonthManpowers[product.Id] = janMonthManpowers[product.Id] + (monthProductManpower != null ? monthProductManpower.Count() : 0);
                        }
                        allProductMonthManPower[product.Id] = allProductMonthManPower[product.Id] +
                                                               (monthProductManpower != null ? monthProductManpower.Count() : 0);
                    }
                    var manPoersIds = totalManPower.Where(x => x.Type == "DSE").Select(x => x.Id);
                    var monthlyActualSales = _targetService.FindMonthlyTarget(manPoersIds, month.Id);
                    var graphModel = new GraphModel
                    {
                        ActualManPower = monthlyManPower.Count(),
                        PlanManPower = monthlyManPowerTarget.Sum(x => x.Planned),
                        Retail = monthlyActualSales.Sum(x => x.Actual)
                    };
                    monthCharts.Add(month.Name, graphModel);
                }
                else
                {
                    monthCharts.Add(month.Name, new GraphModel());
                }
            }

            foreach (var rsm in rsmUsers)
            {
                csmIds = _userService.FindChilds(new List<int> { rsm.Id }).Select(x => x.Id).ToList();
                var dealerIds = _userDealerMapService.FindUserMaps(csmIds).Select(x => x.DealerId).ToList();
                var targets = _manpowerTargetService.FindDealerManpowerTargets(dealerIds, csmIds, currentMonth.Id).ToList();
                var manpowers =
                    _manpowerService.FindAllDealerManpowers(dealerIds, string.Empty).Where(
                        x => x.ObjectInfo.DeletedDate == null).ToList();
                var targetList = new List<TotalManpower>();
                foreach (var product in products)
                {
                    targetList.Add(new TotalManpower
                    {
                        Plan = targets.Where(x => x.ProductId == product.Id).Sum(x => x.Planned),
                        Actual = manpowers.Count(x => x.ProductId == product.Id)
                    });
                }
                var trainingLevelList = new List<ManpowerTrainingLevel>();
                foreach (var level in trainingLevels)
                {
                    trainingLevelList.Add(new ManpowerTrainingLevel
                    {
                        Level = level.Value,
                        LevelCount = _profileService.FindProfilesByManPowerLevel(manpowers.Select(x => x.Id), level.Value).Count()
                    });
                }
                reportList.Add(new ReportManpowerModel
                {
                    User = rsm.Name,
                    UserUrl = string.Format("/User/RsmUser/{0}", rsm.Id),
                    Manpowers = targetList,
                    TrainingLevels = trainingLevelList
                });
            }
            var productStatList = new List<ProductStateModel>();
            var rsmIds = _userService.FindChilds(new List<int> { id }).Select(x => x.Id).ToList();
            csmIds = _userService.FindChilds(rsmIds).Select(x => x.Id).ToList();
            int totalNonExistManpower = 0, totalEmployee = 0;
            int totalActuals = 0, months = 1;
            List<CompetencyProfileMap> totoalMaps = new List<CompetencyProfileMap>();
            foreach (var product in products)
            {
                var manpowers = _manpowerService.FindAllProductUserManpowers(csmIds, new List<int> { product.Id }).ToList();
                var productNonExitMnapowers = manpowers.Any() ? manpowers.Count(x => x.Profile.DateOfLeaving != null && x.Profile.DateOfLeaving.Value.Ticks > currentYear.Ticks) : 0;
                var noofManpowerProductMonth = allProductMonthManPower.Single(x => x.Key == product.Id).Value;
                totalEmployee = totalEmployee + noofManpowerProductMonth;
                totalNonExistManpower = totalNonExistManpower + productNonExitMnapowers;
                var dseManPowerIds = manpowers.Where(x => x.Type == "DSE").Select(x => x.Id).ToList();
                manpowers.RemoveAll(x => x.ObjectInfo.DeletedDate != null || x.Profile.DateOfLeaving != null);
                var manpowerIds = manpowers.Select(x => x.Id).ToList();
                var compmaps = _competencyProfileMapService.FindAllManpowerComap(manpowerIds).ToList();
                totoalMaps.AddRange(compmaps);
                var actualManpowers = manpowers.Any() ? manpowers.Where(x => compmaps.Any(y => y.DealerManpowerId == x.Id)).ToList() : null;
                List<Target> monthTargets;
                monthTargets = _targetService.FindMonthlyTarget(dseManPowerIds, janMonth.Id).ToList();
                //monthTargets.RemoveAll(x => x.MonthId == currentMonth.Id);
                var noOfMonths = monthTargets.GroupBy(x => x.MonthId).Count();
                var productActuals = monthTargets.Sum(x => x.Actual);
                totalActuals = totalActuals + productActuals;
                months = Math.Max(months, noOfMonths);
                productStatList.Add(new ProductStateModel
                {
                    Product = product.Name,
                    Competency = actualManpowers != null && actualManpowers.Any() ? actualManpowers.Sum(x => compmaps.Where(y => y.DealerManpowerId == x.Id).Average(y => y.Score)) / actualManpowers.Count() * 20 : 0,
                    Productivity = janMonthManpowers[product.Id] != 0 ? Math.Round((double)productActuals / janMonthManpowers[product.Id], 2) : 0,
                    Attrition = allProductMonthManPower.Single(x => x.Key == product.Id).Value != 0 ? (productNonExitMnapowers / (allProductMonthManPower.Single(x => x.Key == product.Id).Value / (double)currentDate.Month)) * 100 : 0,
                    TotalCompetency = actualManpowers != null && actualManpowers.Any() ? actualManpowers.Sum(x => compmaps.Where(y => y.DealerManpowerId == x.Id).Average(y => y.Score)) * 20 : 0,
                    TotalManpowers = actualManpowers != null && actualManpowers.Any() ? actualManpowers.Count() : 0 // used for division
                });
            }

            foreach (var productStatModel in productStatList)
            {
                productStatModel.GrossCompetency = productStatList.Any() && productStatList.Sum(x => x.TotalManpowers) != 0 ? productStatList.Sum(x => x.TotalCompetency) / productStatList.Sum(x => x.TotalManpowers) : 0;
            }
            var model = new RmUserViewModel
            {
                ReportManpower = new ReportManpowerViewModel
                {
                    ReportManpowers = reportList.OrderBy(x => x.User),
                    Products = products.Select(x => x.Name),
                    TrainingLevels = trainingLevels.Select(x => x.Value)
                },
                ProductStatistics = productStatList,
                MonthGaphModel = monthCharts,
                Attrition = totalEmployee != 0 ? (totalNonExistManpower / (totalEmployee / (double)currentDate.Month)) * 100 : 0,
                Productivity = totalActuals / (double)janMonthManpowers.Sum(x => x.Value),
            };
            ViewBag.List = Session["BreadcrumbList"];
            ViewBag.Role = "RM";
            ViewBag.Id = id;
            ViewBag.Title = string.Format("{0}({1})", rm.Name, rm.Role);
            return View(model);
        }
Exemple #2
0
 public ActionResult RmUser(int id)
 {
     if (User.IsInRole("RM")) {
         Session["BreadcrumbList"] = Utils.HtmlExtensions.SetBreadcrumbs((List<BreadcrumbModel>)Session["BreadcrumbList"], string.Format("/User/RmUser/{0}", id), "Home");
     } else {
         var name = userService.GetUser(id).UserRegionMaps.First().Region.Name;
         Session["BreadcrumbList"] = Utils.HtmlExtensions.SetBreadcrumbs((List<BreadcrumbModel>)Session["BreadcrumbList"], string.Format("/User/RmUser/{0}", id), name);
         ViewBag.UserName = name;
     }
     var currentDate = DateTime.Now;
     var currentMonth = masterService.FindAndCreateMonth(currentDate.ToString("MMMM"), currentDate.Year);
     var rsmUsers = userService.FindUsers(x => x.ParentId == id).ToList();
     var products = masterService.GetAllProducts().OrderBy(x => x.Id).ToList();
     var trainingLevels = Enumeration.GetAll<TrainingLevel>();
     var reportList = new List<ReportManpowerModel>();
     foreach (var rsm in rsmUsers) {
         var csmIds = userService.FindUsers(x => x.ParentId == rsm.Id).Select(x => x.Id).ToList();
         var dealerIds = userDealerMapService.FindUserDealerMaps(x => csmIds.Contains(x.UserId)).Select(x => x.DealerId).ToList();
         var targets = manpowerTargetService.FindDealerManpowerTargets(x => dealerIds.Contains(x.DealerId) && csmIds.Contains(x.UserId) && x.MonthId == currentMonth.Id).ToList();
         var manpowers = manpowerService.FindDealerManpowers(x => csmIds.Contains(x.UserId) && dealerIds.Contains(x.DealerId)).ToList();
         var targetList = new List<TotalManpower>();
         foreach (var product in products) {
             targetList.Add(new TotalManpower {
                 Plan = targets.Where(x => x.ProductId == product.Id).Sum(x => x.Planned),
                 Actual = manpowers.Count(x => x.ProductId == product.Id)
             });
         }
         var trainingLevelList = new List<ManpowerTrainingLevel>();
         foreach (var level in trainingLevels) {
             trainingLevelList.Add(new ManpowerTrainingLevel {
                 Level = level.Value,
                 LevelCount = manpowers.Count(x => x.Profile.TrainingLevel == level.Value)
             });
         }
         reportList.Add(new ReportManpowerModel {
             User = rsm.Name,
             UserUrl = string.Format("/User/RsmUser/{0}", rsm.Id),
             Manpowers = targetList,
             TrainingLevels = trainingLevelList
         });
     }
     var productStatList = new List<ProductStatModel>();
     foreach (var product in products) {
         var productVarientIds = masterService.FindProductVarient(x => x.ProductId == product.Id).Select(x => x.Id);
         var rsmIds = userService.FindUsers(x => x.ParentId == id).Select(x => x.Id).ToList();
         var csmIds = userService.FindUsers(x => rsmIds.Contains(x.ParentId)).Select(x => x.Id).ToList();
         var manpowers = manpowerService.FindDealerManpowers(x => csmIds.Contains(x.UserId) && x.ProductId == product.Id).ToList();
         var exitMnapowers = manpowers.Count(x => x.Profile.DateOfLeaving != null);
         var averageEmployee = masterService.FindAverageEmployee(currentDate.AddMonths(-1));
         var manpowerIds = manpowers.Select(x => x.Id);
         var actualManpowers = manpowers.Any() ? manpowers.Where(x => x.CompetencyProfileMaps.Any()) : null;
         var targets =
             targetService.FindTargets(
                 x => x.MonthId == currentMonth.Id && productVarientIds.Contains(x.ProductVarientId) && manpowerIds.Contains(x.DealerManpowerId));
         productStatList.Add(new ProductStatModel {
             Product = product.Name,
             Competency = actualManpowers != null && actualManpowers.Any() ? Math.Round(actualManpowers.Average(x => x.CompetencyProfileMaps.Average(y => y.Score)), 2) : 0,
             Productivity = targets.Any() ? Math.Round(targets.Average(x => x.Actual), 2) : 0,
             Attrition = exitMnapowers / (averageEmployee > 0 ? averageEmployee : 1)
         });
     }
     var model = new RmUserViewModel {
         ReportManpower = new ReportManpowerViewModel {
             ReportManpowers = reportList,
             Products = products.Select(x => x.Name),
             TrainingLevels = trainingLevels.Select(x => x.Value)
         },
         ProductStatistics = productStatList
     };
     ViewBag.List = Session["BreadcrumbList"];
     ViewBag.Role = "RM";
     return View(model);
 }