private CsmCompetenceModel RsmDealerCompetence(int id) { var csmCompetenceModel = new CsmCompetenceModel(); csmCompetenceModel.ProductCompetency = new Dictionary<int, decimal>(); csmCompetenceModel.HeadCompetency = new Dictionary<int, decimal>(); var totalcompetency = new Dictionary<int, List<CompetencyProfileMap>>(); var cSmHeads = _masterService.GetAllCompetenceHeader().ToList(); var rsm = _userService.GetUser(id); var csms = _userService.FindChilds(new List<int> { rsm.Id }); var csmIds = csms.Select(x => x.Id).ToList(); var dealers = _userDealerMapServiceService.FindDealers(csmIds); var products = _masterService.GetAllProducts().ToList(); foreach (var product in products) { csmCompetenceModel.ProductCompetency.Add(product.Id, 0); totalcompetency.Add(product.Id, new List<CompetencyProfileMap>()); } decimal totalCompetence = 0, totalManpower = 0; var delaerCsmCompetenceModels = new List<DealerCsmcompetenceModel>(); List<ProductHeadCompetency> productHeadCompetencies = new List<ProductHeadCompetency>(); foreach (var dealer in dealers) { var delaerCsmCompetenceModel = new DealerCsmcompetenceModel(); delaerCsmCompetenceModel.ProductCompetency = new Dictionary<int, decimal>(); var totalUserHeadcompetency = new Dictionary<int, List<CompetencyProfileMap>>(); delaerCsmCompetenceModel.HeadCompetency = new Dictionary<int, decimal>(); foreach (var cSmHead in cSmHeads) { totalUserHeadcompetency.Add(cSmHead.Id, new List<CompetencyProfileMap>()); delaerCsmCompetenceModel.HeadCompetency.Add(cSmHead.Id, 0); } var dealerManpowers = _dealerManpowerService.FindAllDealerManpowers(new List<int> { dealer.Id }, csmIds).Where(x => x.CompetencyProfileMaps != null && x.ObjectInfo.DeletedDate == null).ToList(); var competenceCountCsmModels = new List<CompetenceCountCsmModel>(); decimal totalProductCompetence = 0, totalProductManpower = 0; foreach (var product in products) { List<CompetencyProfileMap> productCompetencyProfileMaps = new List<CompetencyProfileMap>(); foreach (var csmHead in cSmHeads) { var dlrMan = dealerManpowers.Where(x => x.ProductId == product.Id); var manpowerIds = dlrMan.Select(x => x.Id).ToList(); var compmaps = _competencyProfileMapService.FindManpowerHeadComap(manpowerIds, csmHead.Id).ToList(); var productHeadCompetencie = productHeadCompetencies.SingleOrDefault(x => x.HeadId == csmHead.Id && x.ProductId == product.Id); if (productHeadCompetencie == null) { productHeadCompetencies.Add(new ProductHeadCompetency { HeadId = csmHead.Id, ProductId = product.Id, CompetencyProfileMaps = compmaps }); } else { productHeadCompetencie.CompetencyProfileMaps.AddRange(compmaps); } productCompetencyProfileMaps.AddRange(compmaps); totalUserHeadcompetency[csmHead.Id].AddRange(compmaps); var groupedCompetency = compmaps.GroupBy(x => x.DealerManpowerId).ToList(); competenceCountCsmModels.Add(new CompetenceCountCsmModel { CompetenceId = csmHead.Id, ProductId = product.Id, NoOfCompetence = compmaps.Any() ? ((int)groupedCompetency.Sum(x => compmaps.Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) / groupedCompetency.Count() * 20) : 0, }); } var groupedProductCompetency = productCompetencyProfileMaps.GroupBy(x => x.DealerManpowerId).ToList(); delaerCsmCompetenceModel.ProductCompetency.Add(product.Id, groupedProductCompetency.Any() ? (decimal)(groupedProductCompetency.Sum(x => productCompetencyProfileMaps.Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) / groupedProductCompetency.Count() * 20) : 0); totalProductManpower = totalProductManpower + groupedProductCompetency.Count(); totalProductCompetence = totalProductCompetence + (groupedProductCompetency.Any() ? (decimal)(groupedProductCompetency.Sum(x => productCompetencyProfileMaps.Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) * 20) : 0); totalcompetency[product.Id].AddRange(productCompetencyProfileMaps); } foreach (var headcompetency in totalUserHeadcompetency) { var groupedCompetency = headcompetency.Value.GroupBy(x => x.DealerManpowerId).ToList(); delaerCsmCompetenceModel.HeadCompetency[headcompetency.Key] = headcompetency.Value.Any() ? ((decimal)groupedCompetency.Sum(x => headcompetency.Value.Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) / groupedCompetency.Count() * 20) : 0; } totalCompetence = totalCompetence + totalProductCompetence; totalManpower = totalManpower + totalProductManpower; delaerCsmCompetenceModel.Totalcompetency = totalProductManpower != 0 ? totalProductCompetence / totalProductManpower : 0; delaerCsmCompetenceModel.DealerName = dealer.Name; delaerCsmCompetenceModel.CompetenceCountCsmModels = competenceCountCsmModels; delaerCsmCompetenceModels.Add(delaerCsmCompetenceModel); } csmCompetenceModel.DealerCsmcompetenceModel = new DealerCsmcompetenceModel(); csmCompetenceModel.DealerCsmcompetenceModel.HeadCompetency = new Dictionary<int, decimal>(); csmCompetenceModel.DealerCsmcompetenceModel.CompetenceCountCsmModels = new List<CompetenceCountCsmModel>(); foreach (var product in products) { var productCompetency = totalcompetency[product.Id].GroupBy(x => x.DealerManpowerId).ToList(); csmCompetenceModel.ProductCompetency[product.Id] = productCompetency.Any() ? (decimal)(productCompetency.Sum(x => totalcompetency[product.Id].Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) / productCompetency.Count() * 20) : 0; foreach (var cSmHead in cSmHeads) { var headCompetency = productHeadCompetencies.SingleOrDefault(x => x.HeadId == cSmHead.Id && x.ProductId == product.Id); decimal comp; if (headCompetency != null) { var groupHeadComp = headCompetency.CompetencyProfileMaps.GroupBy(x => x.DealerManpowerId).ToList(); comp = groupHeadComp.Any() ? (decimal)(groupHeadComp.Sum(x => headCompetency.CompetencyProfileMaps.Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) / groupHeadComp.Count() * 20) : 0; } else comp = 0; csmCompetenceModel.DealerCsmcompetenceModel.CompetenceCountCsmModels.Add(new CompetenceCountCsmModel { CompetenceId = cSmHead.Id, NoOfCompetence = comp, ProductId = product.Id }); } } foreach (var cSmHead in cSmHeads) { var headCompetency = productHeadCompetencies.Where(x => x.HeadId == cSmHead.Id).SelectMany(x => x.CompetencyProfileMaps).ToList(); decimal comp = 0; if (headCompetency.Any()) { var groupHeadComp = headCompetency.GroupBy(x => x.DealerManpowerId).ToList(); comp = groupHeadComp.Any() ? (decimal)(groupHeadComp.Sum(x => headCompetency.Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) / groupHeadComp.Count() * 20) : 0; } csmCompetenceModel.HeadCompetency.Add(cSmHead.Id, comp); } csmCompetenceModel.Id = rsm.Id; csmCompetenceModel.TotalCompetency = totalManpower != 0 ? totalCompetence / totalManpower : 0; csmCompetenceModel.Name = rsm.Name; csmCompetenceModel.DelaerCsmCompetenceModels = delaerCsmCompetenceModels.OrderBy(x => x.DealerName); csmCompetenceModel.competence = cSmHeads.ToDictionary(x => x.Id, x => x.Category); csmCompetenceModel.Products = products.ToDictionary(x => x.Id, x => x.Name); csmCompetenceModel.Role = "RSM"; return csmCompetenceModel; }
public ActionResult PdfDseCompetence(int csmId, int dealerId) { var csmCompetenceModel = new CsmCompetenceModel(); var dlr = _userDealerMapServiceService.FindDealerByID(dealerId).FirstOrDefault(); var dealerManpowers = dlr.DealerManpowers.Where(x => x.CompetencyProfileMaps != null && x.DealerId == dlr.Id && x.UserId == csmId); var products = _masterService.GetAllProducts().ToList(); var cSmHeads = _masterService.GetAllCompetenceHeader().ToList(); var delaerCsmCompetenceModels = new List<DealerCsmcompetenceModel>(); decimal totalCompetence = 0, totalManpower = 0; foreach (var dealerManpower in dealerManpowers) { var delaerCsmCompetenceModel = new DealerCsmcompetenceModel(); delaerCsmCompetenceModel.ProductCompetency = new Dictionary<int, decimal>(); var competenceCountCsmModels = new List<CompetenceCountCsmModel>(); decimal totalProductCompetence = 0, totalProductManpower = 0; IDictionary<int, List<CompetencyProfileMap>> dealerHeadCompetency = new Dictionary<int, List<CompetencyProfileMap>>(); foreach (var cSmHead in cSmHeads) { dealerHeadCompetency.Add(cSmHead.Id, new List<CompetencyProfileMap>()); } foreach (var product in products) { List<CompetencyProfileMap> productCompetencyProfileMaps = new List<CompetencyProfileMap>(); foreach (var csmHead in cSmHeads) { var manpowerIds = dealerManpower.Id; if (dealerManpower.ProductId == product.Id) { var compmaps = _competencyProfileMapService.FindManpowerHeadComap(manpowerIds, csmHead.Id).ToList(); dealerHeadCompetency[csmHead.Id].AddRange(compmaps); productCompetencyProfileMaps.AddRange(compmaps); var groupedCompetency = compmaps.GroupBy(x => x.DealerManpowerId).ToList(); competenceCountCsmModels.Add(new CompetenceCountCsmModel { CompetenceId = csmHead.Id, ProductId = product.Id, NoOfCompetence = compmaps.Any() ? ((decimal)groupedCompetency.Sum(x => compmaps.Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) / groupedCompetency.Count() * 20) : 0, }); } else { competenceCountCsmModels.Add(new CompetenceCountCsmModel { CompetenceId = csmHead.Id, ProductId = product.Id, NoOfCompetence = 0, }); } } var groupedProductCompetency = productCompetencyProfileMaps.GroupBy(x => x.DealerManpowerId).ToList(); delaerCsmCompetenceModel.ProductCompetency.Add(product.Id, groupedProductCompetency.Any() ? (decimal)(groupedProductCompetency.Sum(x => productCompetencyProfileMaps.Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) / groupedProductCompetency.Count() * 20) : 0); totalProductManpower = totalProductManpower + groupedProductCompetency.Count(); totalProductCompetence = totalProductCompetence + (groupedProductCompetency.Any() ? (decimal)(groupedProductCompetency.Sum(x => productCompetencyProfileMaps.Where( y => y.DealerManpowerId == x.Key).Average(y => y.Score)) * 20) : 0); } delaerCsmCompetenceModel.HeadCompetency = new Dictionary<int, decimal>(); foreach (var temp in dealerHeadCompetency) { var groupedCompetency = temp.Value.GroupBy(x => x.DealerManpowerId).ToList(); var comp = temp.Value != null && temp.Value.Any() ? ((decimal)groupedCompetency.Sum(x => temp.Value.Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) / groupedCompetency.Count() * 20) : 0; delaerCsmCompetenceModel.HeadCompetency.Add(temp.Key, comp); } totalCompetence = totalCompetence + totalProductCompetence; totalManpower = totalManpower + totalProductManpower; delaerCsmCompetenceModel.DealerId = dealerManpower.Id; delaerCsmCompetenceModel.DealerName = dealerManpower.Name; delaerCsmCompetenceModel.CompetenceCountCsmModels = competenceCountCsmModels; delaerCsmCompetenceModels.Add(delaerCsmCompetenceModel); } csmCompetenceModel.Id = csmId; csmCompetenceModel.DlId = dealerId; csmCompetenceModel.Name = dlr.Name; csmCompetenceModel.TotalCompetency = totalManpower != 0 ? totalCompetence / totalManpower : 0; csmCompetenceModel.DelaerCsmCompetenceModels = delaerCsmCompetenceModels.OrderBy(x => x.DealerName); csmCompetenceModel.competence = cSmHeads.ToDictionary(x => x.Id, x => x.Category); csmCompetenceModel.Products = products.ToDictionary(x => x.Id, x => x.Name); var viewString = RenderViewToString("PdfDseCompetence", csmCompetenceModel); var absolutePath = WkhtmlWrapper.CreatePdf(viewString, Server.MapPath("/tmp"), FileService.CreateTempFilePath("pdf"), "Portrait", WkhtmlWrapper.PaperSize.A4); var fileContents = FileService.ReadBytesFromAbsolutePath(absolutePath); FileService.DeleteFile(absolutePath); return File(fileContents, "application/pdf", "Dse-Competence.pdf"); }
private CsmCompetenceModel CsmCompetence(int csmId) { var csmCompetenceModel = new CsmCompetenceModel(); var csm = _userService.GetUser(csmId); var dealers = csm.UserDealerMaps.Select(x => x.Dealer); var products = _masterService.GetAllProducts().ToList(); var cSmHeads = _masterService.GetAllCompetenceHeader().ToList(); var delaerCsmCompetenceModels = new List<DealerCsmcompetenceModel>(); decimal totalCompetence = 0, totalManpower = 0; foreach (var dealer in dealers) { var delaerCsmCompetenceModel = new DealerCsmcompetenceModel(); delaerCsmCompetenceModel.ProductCompetency = new Dictionary<int, decimal>(); var dealerManpowers = dealer.DealerManpowers.Where(x => x.CompetencyProfileMaps != null && x.DealerId == dealer.Id && x.UserId == csmId).ToList(); var competenceCountCsmModels = new List<CompetenceCountCsmModel>(); decimal totalProductCompetence = 0, totalProductManpower = 0; IDictionary<int, List<CompetencyProfileMap>> dealerHeadCompetency = new Dictionary<int, List<CompetencyProfileMap>>(); foreach (var cSmHead in cSmHeads) { dealerHeadCompetency.Add(cSmHead.Id, new List<CompetencyProfileMap>()); } foreach (var product in products) { List<CompetencyProfileMap> productCompetencyProfileMaps = new List<CompetencyProfileMap>(); foreach (var csmHead in cSmHeads) { var dlrMan = dealerManpowers.Where(x => x.ProductId == product.Id); var manpowerIds = dlrMan.Select(x => x.Id).ToList(); var compmaps = _competencyProfileMapService.FindManpowerHeadComap(manpowerIds, csmHead.Id).ToList(); dealerHeadCompetency[csmHead.Id].AddRange(compmaps); productCompetencyProfileMaps.AddRange(compmaps); var groupedCompetency = compmaps.GroupBy(x => x.DealerManpowerId).ToList(); competenceCountCsmModels.Add(new CompetenceCountCsmModel { CompetenceId = csmHead.Id, ProductId = product.Id, NoOfCompetence = compmaps.Any() ? ((decimal)groupedCompetency.Sum(x => compmaps.Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) / groupedCompetency.Count() * 20) : 0, }); } var groupedProductCompetency = productCompetencyProfileMaps.GroupBy(x => x.DealerManpowerId).ToList(); delaerCsmCompetenceModel.ProductCompetency.Add(product.Id, groupedProductCompetency.Any() ? (decimal)(groupedProductCompetency.Sum(x => productCompetencyProfileMaps.Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) / groupedProductCompetency.Count() * 20) : 0); totalProductManpower = totalProductManpower + groupedProductCompetency.Count(); totalProductCompetence = totalProductCompetence + (groupedProductCompetency.Any() ? (decimal)(groupedProductCompetency.Sum(x => productCompetencyProfileMaps.Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) * 20) : 0); } delaerCsmCompetenceModel.HeadCompetency = new Dictionary<int, decimal>(); foreach (var temp in dealerHeadCompetency) { var groupedCompetency = temp.Value.GroupBy(x => x.DealerManpowerId).ToList(); var comp = temp.Value != null && temp.Value.Any() ? ((decimal)groupedCompetency.Sum(x => temp.Value.Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) / groupedCompetency.Count() * 20) : 0; delaerCsmCompetenceModel.HeadCompetency.Add(temp.Key, comp); } totalCompetence = totalCompetence + totalProductCompetence; totalManpower = totalManpower + totalProductManpower; delaerCsmCompetenceModel.DealerId = dealer.Id; delaerCsmCompetenceModel.DealerName = dealer.Name; delaerCsmCompetenceModel.CompetenceCountCsmModels = competenceCountCsmModels; delaerCsmCompetenceModels.Add(delaerCsmCompetenceModel); } csmCompetenceModel.Id = csmId; csmCompetenceModel.Name = csm.Name; csmCompetenceModel.TotalCompetency = totalManpower != 0 ? totalCompetence / totalManpower : 0; csmCompetenceModel.DelaerCsmCompetenceModels = delaerCsmCompetenceModels.OrderBy(x => x.DealerName); csmCompetenceModel.competence = cSmHeads.ToDictionary(x => x.Id, x => x.Category); csmCompetenceModel.Products = products.ToDictionary(x => x.Id, x => x.Name); return csmCompetenceModel; }
public ActionResult ExcelDseCompetence(int csmId, int dealerId) { var csmCompetenceModel = new CsmCompetenceModel(); var dlr = _userDealerMapServiceService.FindDealerByID(dealerId).FirstOrDefault(); var dealerManpowers = dlr.DealerManpowers.Where(x => x.CompetencyProfileMaps != null && x.DealerId == dlr.Id && x.UserId == csmId); var products = _masterService.GetAllProducts().ToList(); var cSmHeads = _masterService.GetAllCompetenceHeader().ToList(); var delaerCsmCompetenceModels = new List<DealerCsmcompetenceModel>(); decimal totalCompetence = 0, totalManpower = 0; foreach (var dealerManpower in dealerManpowers) { var delaerCsmCompetenceModel = new DealerCsmcompetenceModel(); delaerCsmCompetenceModel.ProductCompetency = new Dictionary<int, decimal>(); var competenceCountCsmModels = new List<CompetenceCountCsmModel>(); decimal totalProductCompetence = 0, totalProductManpower = 0; IDictionary<int, List<CompetencyProfileMap>> dealerHeadCompetency = new Dictionary<int, List<CompetencyProfileMap>>(); foreach (var cSmHead in cSmHeads) { dealerHeadCompetency.Add(cSmHead.Id, new List<CompetencyProfileMap>()); } foreach (var product in products) { List<CompetencyProfileMap> productCompetencyProfileMaps = new List<CompetencyProfileMap>(); foreach (var csmHead in cSmHeads) { var manpowerIds = dealerManpower.Id; if (dealerManpower.ProductId == product.Id) { var compmaps = _competencyProfileMapService.FindManpowerHeadComap(manpowerIds, csmHead.Id).ToList(); dealerHeadCompetency[csmHead.Id].AddRange(compmaps); productCompetencyProfileMaps.AddRange(compmaps); var groupedCompetency = compmaps.GroupBy(x => x.DealerManpowerId).ToList(); competenceCountCsmModels.Add(new CompetenceCountCsmModel { CompetenceId = csmHead.Id, ProductId = product.Id, NoOfCompetence = compmaps.Any() ? ((decimal)groupedCompetency.Sum(x => compmaps.Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) / groupedCompetency.Count() * 20) : 0, }); } else { competenceCountCsmModels.Add(new CompetenceCountCsmModel { CompetenceId = csmHead.Id, ProductId = product.Id, NoOfCompetence = 0, }); } } var groupedProductCompetency = productCompetencyProfileMaps.GroupBy(x => x.DealerManpowerId).ToList(); delaerCsmCompetenceModel.ProductCompetency.Add(product.Id, groupedProductCompetency.Any() ? (decimal)(groupedProductCompetency.Sum(x => productCompetencyProfileMaps.Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) / groupedProductCompetency.Count() * 20) : 0); totalProductManpower = totalProductManpower + groupedProductCompetency.Count(); totalProductCompetence = totalProductCompetence + (groupedProductCompetency.Any() ? (decimal)(groupedProductCompetency.Sum(x => productCompetencyProfileMaps.Where( y => y.DealerManpowerId == x.Key).Average(y => y.Score)) * 20) : 0); } delaerCsmCompetenceModel.HeadCompetency = new Dictionary<int, decimal>(); foreach (var temp in dealerHeadCompetency) { var groupedCompetency = temp.Value.GroupBy(x => x.DealerManpowerId).ToList(); var comp = temp.Value != null && temp.Value.Any() ? ((decimal)groupedCompetency.Sum(x => temp.Value.Where(y => y.DealerManpowerId == x.Key).Average(y => y.Score)) / groupedCompetency.Count() * 20) : 0; delaerCsmCompetenceModel.HeadCompetency.Add(temp.Key, comp); } totalCompetence = totalCompetence + totalProductCompetence; totalManpower = totalManpower + totalProductManpower; delaerCsmCompetenceModel.DealerId = dealerManpower.Id; delaerCsmCompetenceModel.DealerName = dealerManpower.Name; delaerCsmCompetenceModel.CompetenceCountCsmModels = competenceCountCsmModels; delaerCsmCompetenceModels.Add(delaerCsmCompetenceModel); } csmCompetenceModel.Id = csmId; csmCompetenceModel.DlId = dealerId; csmCompetenceModel.Name = dlr.Name; csmCompetenceModel.TotalCompetency = totalManpower != 0 ? totalCompetence / totalManpower : 0; csmCompetenceModel.DelaerCsmCompetenceModels = delaerCsmCompetenceModels.OrderBy(x => x.DealerName); csmCompetenceModel.competence = cSmHeads.ToDictionary(x => x.Id, x => x.Category); csmCompetenceModel.Products = products.ToDictionary(x => x.Id, x => x.Name); var tempPath = FileService.GetDownloadFilePath("xlsx"); var excelFilePath = FileService.ConvertToAbsolute(tempPath); var converter = new CsmCompetenceConverter(); converter.GenerateExcel(excelFilePath, csmCompetenceModel); var fileContent = FileService.ReadBytesFromAbsolutePath(excelFilePath); FileService.DeleteFile(excelFilePath); return File(fileContent, "application/octet-stream", "Dse_Competence-Analysis.xlsx"); }