Beispiel #1
0
        public ActionResult RsmUser(int id)
        {
            var rsm = _userService.GetUser(id);
            ViewBag.UserName = rsm.Name;
            if (User.IsInRole("RSM"))
            {
                Session["BreadcrumbList"] = Utils.HtmlExtensions.SetBreadcrumbs((List<BreadcrumbModel>)Session["BreadcrumbList"], string.Format("/User/RsmUser/{0}", id), "Home");
            }
            else
            {
                Session["BreadcrumbList"] = Utils.HtmlExtensions.SetBreadcrumbs((List<BreadcrumbModel>)Session["BreadcrumbList"], string.Format("/User/RsmUser/{0}", id), "RSM");

            }
            var currentDate = DateTime.Now;
            var currentMonth = _masterService.FindAndCreateMonth(currentDate.ToString("MMMM"), currentDate.Year);
            DateTime currentmonth;
            DateTime nextMonth;
            var yearMonths = _masterService.FindMonths(year: currentDate.Year).ToList();
            var csmUsers = _userService.FindChilds(new List<int> { id });
            var csmIds = csmUsers.Select(x => x.Id).ToList();
            var products = _masterService.GetAllProducts().OrderBy(x => x.Id);
            var trainingLevels = Enumeration.GetAll<TrainingLevel>().ToList();
            var reportList = new List<ReportManpowerModel>();
            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 indexMonth = new DateTime(currentDate.Year, i, 15);
                var existmonth =
                    yearMonths.SingleOrDefault(x => x.Year == currentDate.Year && x.Name.Equals(indexMonth.ToString("MMMM")));
                if (existmonth == null)
                    existmonth = new Month { Name = indexMonth.ToString("MMMM"), Year = currentDate.Year };
                existMonths.Add(existmonth);
            } var janMonth = existMonths.Single(x => x.Name == "January");
            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 csm in csmUsers)
            {
                var dealerIds = _userDealerMapService.FindDealers(new List<int> { csm.Id }).Select(x => x.Id).ToList();
                var targets = _manpowerTargetService.FindDealerManpowerTargets(dealerIds, new List<int> { csm.Id }, currentMonth.Id).ToList();
                var manpowers =
                    _manpowerService.FindAllDealerManpowers(dealerIds, string.Empty).Where(
                        x => x.ObjectInfo.DeletedDate == null).Where(x => x.UserId == csm.Id).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 = csm.Name,
                    UserUrl = string.Format("/User/CsmUser/{0}", csm.Id),
                    Manpowers = targetList,
                    TrainingLevels = trainingLevelList
                });
            }
            var productStatList = new List<ProductStateModel>();
            csmIds = _userService.FindChilds(new List<int> { id }).Select(x => x.Id).ToList();
            int totalNonExistManpower = 0, totalEmployee = 0;
            int totalActuals = 0, months = 1;
            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);
                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 RsmUserViewModel
            {
                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 = "RSM";
            ViewBag.Id = id;
            ViewBag.Title = string.Format("{0}({1})", rsm.Name, rsm.Role);
            return View(model);
        }
Beispiel #2
0
 public ActionResult RsmUser(int id)
 {
     if (User.IsInRole("RSM")) {
         Session["BreadcrumbList"] = Utils.HtmlExtensions.SetBreadcrumbs((List<BreadcrumbModel>)Session["BreadcrumbList"], string.Format("/User/RsmUser/{0}", id), "Home");
     } else {
         Session["BreadcrumbList"] = Utils.HtmlExtensions.SetBreadcrumbs((List<BreadcrumbModel>)Session["BreadcrumbList"], string.Format("/User/RsmUser/{0}", id), "RSM");
         ViewBag.UserName = userService.GetUser(id).Name;
     }
     var currentDate = DateTime.Now;
     var currentMonth = masterService.FindAndCreateMonth(currentDate.ToString("MMMM"), currentDate.Year);
     var csmUsers = userService.FindUsers(x => x.ParentId == id);
     var products = masterService.GetAllProducts().OrderBy(x => x.Id);
     var trainingLevels = Enumeration.GetAll<TrainingLevel>();
     var reportList = new List<ReportManpowerModel>();
     foreach (var csm in csmUsers) {
         var dealerIds = userDealerMapService.FindUserDealerMaps(x => x.UserId == csm.Id).Select(x => x.DealerId);
         var targets = manpowerTargetService.FindDealerManpowerTargets(x => dealerIds.Contains(x.DealerId) && x.UserId == csm.Id && x.MonthId == currentMonth.Id);
         var manpowers = manpowerService.FindDealerManpowers(x => x.UserId == csm.Id && 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 = csm.Name,
             UserUrl = string.Format("/User/CsmUser/{0}", csm.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 csmIds = userService.FindUsers(x => x.ParentId == id).Select(x => x.Id);
         var manpowers = manpowerService.FindDealerManpowers(x => csmIds.Contains(x.UserId) && x.ProductId == product.Id);
         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 RsmUserViewModel {
         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 = "RSM";
     return View(model);
 }