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); }
public ActionResult Dealer(int dealerId, int csmId) { Session["BreadcrumbList"] = Utils.HtmlExtensions.SetBreadcrumbs((List<BreadcrumbModel>)Session["BreadcrumbList"], string.Format("/Dealer/Dealer?dealerId={0}&csmId={1}", dealerId, csmId), "Dealer"); var currentDate = DateTime.Now; var currentMonth = _masterService.FindAndCreateMonth(currentDate.ToString("MMMM"), currentDate.Year); var allManpower = manpowerService.FindAllDealerManpowers(new List<int> { dealerId }, new List<int> { csmId }); var excludeManpower = allManpower.Where(x => x.ObjectInfo.DeletedDate != null && x.Profile.DateOfLeaving == null); allManpower = allManpower.Except(excludeManpower); var manpowerUsers = allManpower.Where(x => x.DealerId == dealerId && x.UserId == csmId && x.ObjectInfo.DeletedDate == null).ToList(); var yearMonths = _masterService.FindMonths(year: currentDate.Year); var existMonths = new List<Month>(); var currentYear = new DateTime(currentDate.Year, 1, 1); var products = _masterService.GetAllProducts().OrderBy(x => x.Id).ToList(); 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 month = new DateTime(currentDate.Year, i, 15); var existMonth = yearMonths.SingleOrDefault(x => x.Name.Equals(month.ToString("MMMM")) && x.Year == month.Year); if (existMonth == null) existMonth = new Month { Name = month.ToString("MMMM"), Year = month.Year }; existMonths.Add(existMonth); } var janMonth = existMonths.Single(x => x.Name == "January"); var monthCharts = new Dictionary<string, GraphModel>(); foreach (var yearMonth in existMonths) { var currentmonth = DateTime.Parse(string.Format("1 {0} {1}", yearMonth.Name, yearMonth.Year)); var nextMonth = currentmonth.AddMonths(1); if (yearMonth.Id != 0) { var planManPowerTarget = manpowerTargetService.FindDealerManpowerTargets( x => x.UserId == csmId && x.MonthId == yearMonth.Id && x.DealerId == dealerId).Sum(x => x.Planned); var manpowers1 = allManpower.Where( x => x.ObjectInfo.CreatedDate.Ticks < nextMonth.Ticks && (x.Profile.DateOfLeaving == null || x.Profile.DateOfLeaving.Value.Ticks >= currentmonth.Ticks)) .ToList(); var groupProductMonthManPower = manpowers1.GroupBy(x => x.ProductId); foreach (var product in products) { var monthProductManpower = groupProductMonthManPower.SingleOrDefault(x => x.Key == product.Id); if (yearMonth.Name != currentDate.ToString("MMMM")) { productMonthManPower[product.Id] = productMonthManPower[product.Id] + (monthProductManpower != null ? monthProductManpower.Count() : 0); } if (yearMonth.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 = allManpower.Where(x => x.Type == "DSE").Select(x => x.Id); var monthlyActualSales = targetService.FindMonthlyTarget(manPoersIds, yearMonth.Id); var graphModel = new GraphModel { ActualManPower = manpowers1.Count, PlanManPower = planManPowerTarget, Retail = monthlyActualSales.Sum(x => x.Actual) }; monthCharts.Add(yearMonth.Name, graphModel); } else { monthCharts.Add(yearMonth.Name, new GraphModel()); } } var trainingLevels = Enumeration.GetAll<TrainingLevel>().ToList(); var reportList = new List<ReportManpowerModel>(); var targets = manpowerTargetService.FindDealerManpowerTargets(x => x.DealerId == dealerId && x.UserId == csmId).ToList(); var targetList = products.Select(product => new TotalManpower { Plan = targets.Where(x => x.ProductId == product.Id && x.MonthId == currentMonth.Id).Sum(x => x.Planned), Actual = manpowerUsers.Count(x => x.ProductId == product.Id) }).ToList(); var trainingLevelList = trainingLevels.Select(level => new ManpowerTrainingLevel { Level = level.Value, LevelCount = manpowerUsers.Count(x => x.Profile.TrainingLevel == level.Value) }).ToList(); reportList.Add(new ReportManpowerModel { Manpowers = targetList, TrainingLevels = trainingLevelList }); var productStatList = new List<ProductStateModel>(); int totalNonExistManpower = 0, totalEmployee = 0, totalProductiveEmployee = 0; int totalActuals = 0, months = 1; foreach (var product in products) { var manpowers = manpowerService.FindAllDealerManpowers(x => x.UserId == csmId && x.DealerId == dealerId && x.ProductId == 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 actualManpowers = manpowers.Any() ? manpowers.Where(x => x.CompetencyProfileMaps.Any()) : null; List<Repository.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 => x.CompetencyProfileMaps.Average(y => y.Score)) / actualManpowers.Count() * 20 : 0, Productivity = noOfMonths != 0 && 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 => x.CompetencyProfileMaps.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.Sum(x => x.TotalManpowers) != 0 ? productStatList.Sum(x => x.TotalCompetency) / productStatList.Sum(x => x.TotalManpowers) : 0; } var model = new CsmUserViewModel { ReportManpower = new ReportManpowerViewModel { ReportManpowers = reportList, Products = products.Select(x => x.Name), TrainingLevels = trainingLevels.Select(x => x.Value) }, ProductStatistics = productStatList, MonthGaphModel = monthCharts, CsmId = csmId, Attrition = totalEmployee != 0 ? (totalNonExistManpower / (totalEmployee / (double)currentDate.Month)) * 100 : 0, Productivity = totalActuals / (double)janMonthManpowers.Sum(x => x.Value) }; ViewBag.DealerId = dealerId; ViewBag.CsmId = csmId; ViewBag.List = Session["BreadcrumbList"]; var dealer = _masterService.GetDealer(dealerId); ViewBag.UserName = dealer.Name; ViewBag.Title = string.Format("{0}({1})", dealer.Name, "Dealer"); return View(model); }