private CsmAttritionModel StatewiseAttrition(int rmId) { var rm = _userService.GetUser(rmId); var regionmaps = rm.UserRegionMaps.ToList(); var regions = regionmaps.Select(x => x.Region); var states = regions.SelectMany(x => x.States).ToList(); var rsms = _userService.FindUsers(x => x.ParentId == rmId); var csms = _userService.FindUsers(x => rsms.Any(y => y.Id == x.ParentId)); var userDealerMaps = csms.SelectMany(x => x.UserDealerMaps); var dealers = userDealerMaps.Select(x => x.Dealer).Distinct().ToList(); var products = _masterService.GetAllProducts().ToList(); var attHeads = _masterService.GetAllAttritionHeaders().ToList(); var currentDate = DateTime.Now; var currentYear = new DateTime(currentDate.Year, 1, 1); var totalAttritionEmployee = attHeads.ToDictionary(x => x.Id, x => 0); int csmTotalEmployee = 0; var delaerCsmattritionModels = new List<DelaerCsmattritionModel>(); int maintotalemployee = 0; int maintotalNonExistManpower = 0; foreach (var state in states) { var stateDealers = dealers.Where(x => x.StateId == state.Id).ToList(); var delaerCsmattritionModel = new DelaerCsmattritionModel(); var dealerManpowers = _dealerManpowerService.FindAllDealerManpowers(stateDealers.Select(x => x.Id), string.Empty).ToList(); var manPowerProfiles = _profileService.FindAllProfiles(dealerManpowers.Select(x => x.Id)); var productNonExitMnapowers = dealerManpowers.Any() ? manPowerProfiles.Count(x => x.DateOfLeaving != null && x.DateOfLeaving.Value.Ticks > currentYear.Ticks) : 0; int totalemployee = 0; int totalNonExistManpower = 0; int totalEmployee1 = 0; int totalEmployee2 = 0; foreach (var manpower in dealerManpowers) { if (manpower.ObjectInfo.CreatedDate.Ticks < currentYear.Ticks && manpower.ObjectInfo.DeletedDate == null) totalemployee = totalemployee + currentDate.Month; else if (manpower.ObjectInfo.CreatedDate.Ticks < currentYear.Ticks && manpower.ObjectInfo.DeletedDate != null && manpower.ObjectInfo.DeletedDate.Value.Ticks > currentYear.Ticks) totalemployee = totalemployee + manpower.ObjectInfo.DeletedDate.Value.Month; else if (manpower.ObjectInfo.CreatedDate.Ticks > currentYear.Ticks && manpower.ObjectInfo.DeletedDate == null) totalemployee = totalemployee + currentDate.Month - manpower.ObjectInfo.CreatedDate.Month + 1; else if (manpower.ObjectInfo.CreatedDate.Ticks > currentYear.Ticks && manpower.ObjectInfo.DeletedDate != null) totalemployee = totalemployee + manpower.ObjectInfo.DeletedDate.Value.Month - manpower.ObjectInfo.CreatedDate.Month + 1; } foreach (var manpower1 in dealerManpowers) { if (manpower1.ObjectInfo.CreatedDate.Ticks < currentYear.Ticks && manpower1.Profile.DateOfLeaving == null) totalEmployee2 = totalEmployee2 + currentDate.Month; else if (manpower1.ObjectInfo.CreatedDate.Ticks < currentYear.Ticks && manpower1.Profile.DateOfLeaving != null && manpower1.Profile.DateOfLeaving.Value.Ticks > currentYear.Ticks) totalEmployee2 = totalEmployee2 + manpower1.Profile.DateOfLeaving.Value.Month; else if (manpower1.ObjectInfo.CreatedDate.Ticks > currentYear.Ticks && manpower1.Profile.DateOfLeaving == null) totalEmployee2 = totalEmployee2 + currentDate.Month - manpower1.ObjectInfo.CreatedDate.Month + 1; else if (manpower1.ObjectInfo.CreatedDate.Ticks > currentYear.Ticks && manpower1.Profile.DateOfLeaving != null) totalEmployee2 = totalEmployee2 + manpower1.Profile.DateOfLeaving.Value.Month - manpower1.ObjectInfo.CreatedDate.Month + 1; } csmTotalEmployee = csmTotalEmployee + totalemployee; totalEmployee1 = totalEmployee1 + totalEmployee2; totalNonExistManpower = totalNonExistManpower + productNonExitMnapowers; maintotalemployee = maintotalemployee + totalEmployee1; maintotalNonExistManpower = maintotalNonExistManpower + totalNonExistManpower; var attritionRate = new Dictionary<int, decimal>(); var attritionCountCsmModels = new List<AttritionCountCsmModel>(); foreach (var attHead in attHeads) { var attheadManPowerLeft = dealerManpowers.Count( x => x.AttritionProfileMap != null && x.AttritionProfileMap.Attrition.AttritionHead.Id == attHead.Id && x.Profile.DateOfLeaving != null && x.Profile.DateOfLeaving.Value.Ticks > currentYear.Ticks); totalAttritionEmployee[attHead.Id] = totalAttritionEmployee[attHead.Id] + attheadManPowerLeft; var attRate = totalemployee != 0 ? (attheadManPowerLeft / (totalemployee / (double)currentDate.Month)) : 0; attritionRate.Add(attHead.Id, (decimal)attRate); } foreach (var product in products) { foreach (var atthead in attHeads) { var count = dealerManpowers.Count( x => x.ProductId == product.Id && x.AttritionProfileMap != null && x.AttritionProfileMap.Attrition.AttritionHead.Id == atthead.Id && x.Profile.DateOfLeaving != null && x.Profile.DateOfLeaving.Value.Ticks > currentYear.Ticks); var attritionCountCsmModel = new AttritionCountCsmModel(); attritionCountCsmModel.AttrionId = atthead.Id; attritionCountCsmModel.ProductId = product.Id; attritionCountCsmModel.NoOfAttriton = count; attritionCountCsmModels.Add(attritionCountCsmModel); } } delaerCsmattritionModel.DealerName = state.Name; delaerCsmattritionModel.AttritionCountCsmModels = attritionCountCsmModels; delaerCsmattritionModel.TotalAttration = Convert.ToDouble(totalEmployee1 != 0 ? (totalNonExistManpower / (totalEmployee1 / (double)currentDate.Month)) * 100 : 0); delaerCsmattritionModels.Add(delaerCsmattritionModel); delaerCsmattritionModel.AttritionRate = attritionRate; } var csmAttritionModel = new CsmAttritionModel(); csmAttritionModel.Id = rm.Id; csmAttritionModel.Name = rm.Name; csmAttritionModel.DelaerCsmattritionModels = delaerCsmattritionModels.OrderBy(x => x.DealerName); csmAttritionModel.Attritions = attHeads.ToDictionary(x => x.Id, x => x.Category); csmAttritionModel.Products = products.ToDictionary(x => x.Id, x => x.Name); csmAttritionModel.Role = "RM"; csmAttritionModel.FinalTotalAttrition = Convert.ToDouble(maintotalemployee != 0 ? (maintotalNonExistManpower / (maintotalemployee / (double)currentDate.Month)) * 100 : 0); csmAttritionModel.TotalAttritionRate = totalAttritionEmployee.ToDictionary(x => x.Key, x => csmTotalEmployee != 0 ? (x.Value / (csmTotalEmployee / (decimal)currentDate.Month)) : 0); return csmAttritionModel; }
private CsmAttritionModel CsmAttritions(int csmId) { var csm = _userService.GetUser(csmId); var dealers = csm.UserDealerMaps.Select(x => x.Dealer).ToList(); var currentDate = DateTime.Now; var currentYear = new DateTime(currentDate.Year, 1, 1); var attHeads = _masterService.GetAllAttritionHeaders().ToList(); var delaerCsmattritionModels = new List<DelaerCsmattritionModel>(); int maintotalemployee = 0; int maintotalNonExistManpower = 0; var products = _masterService.GetAllProducts().OrderBy(x => x.Id).ToList(); var productMonthManPower = products.ToDictionary(x => x.Id, x => 0); foreach (var dealer in dealers) { var allProductMonthManPower = products.ToDictionary(x => x.Id, x => 0); var allManpower = _manpowerService.FindAllDealerManpowers(new List<int> { dealer.Id }, new List<int> { csmId }).ToList(); var yearMonths = _masterService.FindMonths(year: currentDate.Year).ToList(); var existMonths = new List<Month>(); 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); } 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 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).ToList(); 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); } allProductMonthManPower[product.Id] = allProductMonthManPower[product.Id] + (monthProductManpower != null ? monthProductManpower.Count() : 0); } } } var delaerCsmattritionModel = new DelaerCsmattritionModel(); int totalNonExistManpower = 0, totalEmployee = 0; int productNonExitMnapowers1 = 0; List<DealerManpower> manpowers; var productNonExitMnapowers = 0; foreach (var product in products) { manpowers = _manpowerService.FindAllDealerManpowers(x => x.UserId == csmId && x.DealerId == dealer.Id && x.ProductId == product.Id).ToList(); 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; productNonExitMnapowers1 = productNonExitMnapowers1 + productNonExitMnapowers; } totalNonExistManpower = totalNonExistManpower + productNonExitMnapowers1; var attritionCountCsmModels = new List<AttritionCountCsmModel>(); var attritionRate = new Dictionary<int, decimal>(); foreach (var attHead in attHeads) { var attheadManPowerLeft = allManpower.Count( x => x.AttritionProfileMap != null && x.AttritionProfileMap.Attrition.AttritionHead.Id == attHead.Id && x.Profile.DateOfLeaving != null && x.Profile.DateOfLeaving.Value.Ticks > currentYear.Ticks); var attRate = totalEmployee != 0 ? (attheadManPowerLeft / (totalEmployee / (double)currentDate.Month)) : 0; attritionRate.Add(attHead.Id, (decimal)attRate); } foreach (var product in products) { foreach (var atthead in attHeads) { var count = allManpower.Count( x => x.ProductId == product.Id && x.AttritionProfileMap != null && x.AttritionProfileMap.Attrition.AttritionHead.Id == atthead.Id && x.Profile.DateOfLeaving != null && x.Profile.DateOfLeaving.Value.Ticks > currentYear.Ticks); var attritionCountCsmModel = new AttritionCountCsmModel(); attritionCountCsmModel.AttrionId = atthead.Id; attritionCountCsmModel.ProductId = product.Id; attritionCountCsmModel.NoOfAttriton = count; attritionCountCsmModels.Add(attritionCountCsmModel); } } delaerCsmattritionModel.DealerId = dealer.Id; delaerCsmattritionModel.DealerName = dealer.Name; delaerCsmattritionModel.AttritionRate = attritionRate; delaerCsmattritionModel.TotalAttration = Convert.ToDouble(totalEmployee != 0 ? (totalNonExistManpower / (totalEmployee / (double)currentDate.Month)) * 100 : 0); delaerCsmattritionModel.AttritionCountCsmModels = attritionCountCsmModels; delaerCsmattritionModels.Add(delaerCsmattritionModel); } var csmAttritionModel = new CsmAttritionModel(); csmAttritionModel.Id = csmId; csmAttritionModel.Name = csm.Name; csmAttritionModel.DelaerCsmattritionModels = delaerCsmattritionModels.OrderBy(x => x.DealerName); csmAttritionModel.FinalTotalAttrition = Convert.ToDouble(maintotalemployee != 0 ? (maintotalNonExistManpower / (maintotalemployee / (double)currentDate.Month)) * 100 : 0); csmAttritionModel.Attritions = attHeads.ToDictionary(x => x.Id, x => x.Category); csmAttritionModel.Products = products.ToDictionary(x => x.Id, x => x.Name); return csmAttritionModel; }