Ejemplo n.º 1
0
        public ActionResult HqUser(int id)
        {
            var hq = _userService.GetUser(id);
            if (User.IsInRole("HQ") || User.IsInRole("HQR"))
            {
                Session["BreadcrumbList"] = Utils.HtmlExtensions.SetBreadcrumbs((List<BreadcrumbModel>)Session["BreadcrumbList"], string.Format("/User/HqUser/{0}", id), "Home");
            }
            var currentDate = DateTime.Now;
            var currentMonth = _masterService.FindAndCreateMonth(currentDate.ToString("MMMM"), currentDate.Year);
            DateTime currentmonth;
            DateTime nextMonth;
            IEnumerable<User> rmUsers;
            List<Product> products;
            var trainingLevels = Enumeration.GetAll<TrainingLevel>().ToList();
            var reportList = new List<ReportManpowerModel>();
            IEnumerable<int> totalDealerIds;
            var yearMonths = _masterService.FindMonths(year: currentDate.Year).ToList();
            var currentYear = new DateTime(currentDate.Year, 1, 1);
            rmUsers = _userService.FindUsersByRole("RM").ToList();
            var rsm = _userService.FindChilds(rmUsers.Select(x => x.Id).ToList());
            products = _masterService.GetAllProducts().OrderBy(x => x.Id).ToList();
            var csmUsers = _userService.FindChilds(rsm.Select(x => x.Id).ToList());
            totalDealerIds = _userDealerMapService.FindUserMaps(csmUsers.Select(x => x.Id)).Select(x => x.DealerId).ToList();
            var productMonthManPower = products.ToDictionary(x => x.Id, x => 0);
            var allProductMonthManPower = products.ToDictionary(x => x.Id, x => 0);
            var tillMonths = new List<Month>();
            var janMonthManpowers = products.ToDictionary(x => x.Id, x => 0);
            for (int i = 1; i <= currentDate.Month; i++)
            {
                var month = new DateTime(2013, i, 15);
                var existMonth = yearMonths.SingleOrDefault(x => x.Name.Equals(month.ToString("MMMM")));
                if (existMonth == null)
                    existMonth = new Month { Year = currentDate.Year, Name = month.ToString("MMMM") };
                tillMonths.Add(existMonth);
            }
            var janMonth = tillMonths.Single(x => x.Name == "January");
            var monthCharts = new Dictionary<string, GraphModel>();

            foreach (var month in tillMonths)
            {
                currentmonth = DateTime.Parse(string.Format("1 {0} {1}", month.Name, month.Year));
                nextMonth = currentmonth.AddMonths(1);
                if (month.Id != 0)
                {
                    var monthlyManPowerTarget = _manpowerTargetService.FindDealerManpowerMonthTarget(totalDealerIds, new List<int> { month.Id });
                    var totalManPower = _manpowerService.FindAllDealerManpowers(totalDealerIds, 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 rm in rmUsers)
            {
                var rsmIds = _userService.FindChilds(new List<int> { rm.Id }).Select(x => x.Id).ToList();
                var csmIds = _userService.FindChilds(rsmIds).Select(x => x.Id).ToList();
                var dealerIds = _userDealerMapService.FindDealers(csmIds).Select(x => x.Id).ToList();
                var targets = _manpowerTargetService.FindDealerManpowerMonthTarget(dealerIds, new List<int> { 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 =
                        string.Format("{0} ({1})", rm.UserRegionMaps.First().Region.Name, rm.Name),
                    UserUrl = string.Format("/User/RmUser/{0}", rm.Id),
                    Manpowers = targetList,
                    TrainingLevels = trainingLevelList
                });
            }
            var productStatList = new List<ProductStateModel>();
            var rmIds = rmUsers.Select(x => x.Id).ToList();
            var prsmIds = _userService.FindUsers(x => rmIds.Contains(x.ParentId)).Select(x => x.Id).ToList();
            var pcsmIds = _userService.FindUsers(x => prsmIds.Contains(x.ParentId)).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(pcsmIds, 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);
                var competency = actualManpowers != null && actualManpowers.Any()
                                     ? actualManpowers.Sum(
                                     x => compmaps.Where(y => y.DealerManpowerId == x.Id).Average(y => y.Score)) : 0;
                productStatList.Add(new ProductStateModel
                {
                    Product = product.Name,
                    Competency = actualManpowers != null && actualManpowers.Count() != 0 ? competency / actualManpowers.Count() * 20 : 0,
                    Productivity = janMonthManpowers[product.Id] != 0 ? Math.Round((double)productActuals / janMonthManpowers[product.Id], 2) : 0,
                    Attrition = allProductMonthManPower[product.Id] != 0 ? (productNonExitMnapowers / (allProductMonthManPower[product.Id] / (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 HqUserViewModel
            {
                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 = "HQ";
            ViewBag.Id = id;
            ViewBag.Title = string.Format("{0}({1})", hq.Name, hq.Role);
            return View(model);
        }
Ejemplo n.º 2
0
        public ActionResult HqUser(int id)
        {
            if (User.IsInRole("HQ")) {
                Session["BreadcrumbList"] = Utils.HtmlExtensions.SetBreadcrumbs((List<BreadcrumbModel>)Session["BreadcrumbList"], string.Format("/User/HqUser/{0}", id), "Home");
            }
            var currentDate = DateTime.Now;
            var currentMonth = masterService.FindAndCreateMonth(currentDate.ToString("MMMM"), currentDate.Year);
            var rmUsers = userService.FindUsers(x => x.ParentId == id || x.Role.ToLower() == "rm").ToList();
            var products = masterService.GetAllProducts().OrderBy(x => x.Id);
            var trainingLevels = Enumeration.GetAll<TrainingLevel>();
            var reportList = new List<ReportManpowerModel>();

            foreach (var rm in rmUsers) {
                var rsmIds = userService.FindUsers(x => x.ParentId == rm.Id).Select(x => x.Id).ToList();
                var csmIds = userService.FindUsers(x => rsmIds.Contains(x.ParentId)).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 = string.Format("{0} ({1})", rm.UserRegionMaps.First().Region.Name, rm.Name),
                    UserUrl = string.Format("/User/RmUser/{0}", rm.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).ToList();
                var rmIds = rmUsers.Select(x => x.Id).ToList();
                var rsmIds = userService.FindUsers(x => rmIds.Contains(x.ParentId)).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.Any() ? manpowers.Count(x => x.Profile.DateOfLeaving != null) : 0;
                var averageEmployee = masterService.FindAverageEmployee(currentDate.AddMonths(-1));
                var manpowerIds = manpowers.Select(x => x.Id).ToList();
                var actualManpowers = manpowers.Any() ? manpowers.Where(x => x.CompetencyProfileMaps.Any()).ToList() : null;
                var targets =
                    targetService.FindTargets(
                        x => x.MonthId == currentMonth.Id && productVarientIds.Contains(x.ProductVarientId) && manpowerIds.Contains(x.DealerManpowerId)).ToList();
                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 HqUserViewModel {
                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 = "HQ";
            return View(model);
        }