public ActionResult Actuals(int dealerId, int monthId = 0) { Session["BreadcrumbList"] = Utils.HtmlExtensions.SetBreadcrumbs((List<BreadcrumbModel>)Session["BreadcrumbList"], string.Format("/Dealer/Actuals?dealerId={0}&monthId={1}", dealerId, monthId), "Actual"); var now = DateTime.Now; var year = now.Year; var actualStatus = targetService.GetActualStatus(); _masterService.FindAndCreateMonth(now.ToString("MMMM"), year); var monthList = _masterService.GetAllFinancialMonths(now.ToString("MMMM"), now.Year).ToDictionary(x => x.Id, y => string.Format("{0}-{1}", y.Name, y.Year)); Month month = null; if (monthId == 0) { month = _masterService.FindAndCreateMonth(now.ToString("MMMM"), year); } else { month = _masterService.GetMonth(monthId); } var csm = userService.GetUserByUserName(User.Identity.Name); var hasReport1 = _retailService.HasReportFile(dealerId, month.Id, csm.Id, "DMSReport"); var hasReport2 = _retailService.HasReportFile(dealerId, month.Id, csm.Id, "Directbillingreport"); var actualModels = new List<ActualModel>(); var manpowers = manpowerService.FindAllDealerManpowers(new List<int> { dealerId }, new List<int> { csm.Id }, x => x.Product).OrderBy(y => y.Name).ToList(); var manpowerIds = manpowers.Select(x => x.Id); var allVarients = _masterService.GetAllProductVarients(); var products = _masterService.FindProducts(x => x.ProductVarients.Count > 0 && !x.IsCommon).OrderBy(x => x.Name); var monthData = targetService.FindTargets(manpowerIds, new List<int> { month.Id }); foreach (var manpower in manpowers) { var isEdit = manpower.Product.IsCommon; var actualModel = new ActualModel() { ManpowerId = manpower.Id, Manpower = manpower.Name, MonthId = month.Id, Designation = manpower.Type }; var manpowerProductVarientIds = allVarients.Where(x => x.ProductId == manpower.ProductId).Select(x => x.Id); var actuals = new List<Actual>(); bool hastarget; hastarget = monthData != null && monthData.Where(x => x.DealerManpowerId == manpower.Id).Any(y => (y.Target1 + y.Target2 > 0)); if (!hastarget && manpower.ObjectInfo.DeletedDate != null) { continue; } foreach (var varient in allVarients) { Repository.Target motnhTempData; if (isEdit || manpower.Type.ToLower() == "dsm") { motnhTempData = monthData.SingleOrDefault(x => x.ProductVarientId == varient.Id && x.DealerManpowerId == manpower.Id); actuals.Add(new Actual { Id = motnhTempData != null ? motnhTempData.Id : 0, ActualRetail = motnhTempData != null ? motnhTempData.Actual : 0, ProductVarientId = varient.Id, IsEditable = manpower.Type.ToLower() != "dsm" && (manpowerProductVarientIds.Contains(varient.Id) || isEdit) && (motnhTempData != null && hastarget) && hasReport1, }); } else if (manpowerProductVarientIds.Contains(varient.Id)) { motnhTempData = monthData.SingleOrDefault(x => x.ProductVarientId == varient.Id && x.DealerManpowerId == manpower.Id); actuals.Add(new Actual { Id = motnhTempData != null ? motnhTempData.Id : 0, ActualRetail = motnhTempData != null ? motnhTempData.Actual : 0, ProductVarientId = varient.Id, IsEditable = motnhTempData != null && hastarget && hasReport1, }); } } actualModel.Actuals = actuals; actualModels.Add(actualModel); } var model = new ActualViewModel { CsmId = csm.Id, MonthId = month.Id, Months = monthList, HasReport1 = hasReport1, HasReport2 = hasReport2, MonthName = month.Name, DealerId = dealerId, ActualModels = actualModels.OrderBy(x => x.Designation).ThenBy(x => x.Manpower), ProductVarients = products.Select( x => new ProductVarients { Product = x.Name, VarientsDic = x.ProductVarients.OrderBy(y => y.Name).ToDictionary(y => y.Id, y => y.Name) }). ToList(), TargetStatus = actualStatus }; ViewBag.List = Session["BreadcrumbList"]; return View("Actuals", model); }
public ActionResult SaveActuals(ActualViewModel actualViewModel) { var actualStatus = targetService.GetActualStatus(); if (!actualStatus) return RedirectToAction("Actuals", new { actualViewModel.DealerId, actualViewModel.MonthId }); var csmId = actualViewModel.CsmId; var monthId = actualViewModel.MonthId; var dealerId = actualViewModel.DealerId; var targets = new List<Repository.Target>(); if (actualViewModel.ActualModels == null) actualViewModel.ActualModels = new List<ActualModel>(); foreach (var actualModel in actualViewModel.ActualModels) { if (actualModel.Actuals == null) actualModel.Actuals = new List<Actual>(); foreach (var actual in actualModel.Actuals) { var target = new Repository.Target(); target.MonthId = monthId; target.DealerManpowerId = actualModel.ManpowerId; target.ProductVarientId = actual.ProductVarientId; target.Actual = actual.ActualRetail; target.Id = actual.Id; targets.Add(target); } } targetService.SaveActuals(targets); // update dsm var dsmdsemaps = dsmDseTargetMapService.FindDsmDseTargetMaps(csmId, monthId, dealerId, targets.Select(x => x.DealerManpowerId).ToList()); foreach (var dsmdsemap in dsmdsemaps) { targetService.UpdateDsmTarget(dsmdsemap, actualViewModel.CsmId, actualViewModel.MonthId); } return RedirectToAction("Actuals", new { dealerId, monthId }); }