Exemplo n.º 1
0
        public ActionResult Targets(int id)
        {
            Session["BreadcrumbList"] = Utils.HtmlExtensions.SetBreadcrumbs((List<BreadcrumbModel>)Session["BreadcrumbList"], string.Format("/Dealer/Targets/{0}", id), "Target");
            var date = DateTime.Now;
            var month = masterService.FindAndCreateMonth(date.ToString("MMMM"), date.Year);
            date = DateTime.Now.AddMonths(-1);
            var previousMonth = masterService.FindAndCreateMonth(date.ToString("MMMM"), date.Year);
            var csm = userService.GetUserByUserName(User.Identity.Name);
            var manpowers = manpowerService.FindDealerManpowers(x => x.UserId == csm.Id && x.DealerId == id).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.Id);
            var data = targetService.FindTargets(x => manpowerIds.Contains(x.DealerManpowerId) && x.MonthId == month.Id);
            var previousTargets =
                targetService.FindTargets(x => manpowerIds.Contains(x.DealerManpowerId) && x.MonthId == previousMonth.Id);
            var targetList = new List<TargetModel>();

            foreach (var manpower in manpowers) {
                var isEdit = manpower.Product.IsCommon;
                var targetModel = new TargetModel { ManpowerId = manpower.Id, Manpower = manpower.Name, MonthId = month.Id, Designation = manpower.Type };
                var targetPlanList = new List<TargetPlanModel>();
                var manpowerProductVarientIds = allVarients.Where(x => x.ProductId == manpower.ProductId).Select(x => x.Id);
                foreach (var varient in allVarients) {
                    var tempData = data.Where(x => x.ProductVarientId == varient.Id && x.DealerManpowerId == manpower.Id);
                    var preTempData =
                        previousTargets.Where(x => x.ProductVarientId == varient.Id && x.DealerManpowerId == manpower.Id);
                    targetPlanList.Add(new TargetPlanModel {
                        Id = tempData.Any() ? tempData.First().Id : 0,
                        Target1 = tempData.Any() ? tempData.Sum(x => x.Target1) : 0,
                        Target2 = tempData.Any() ? tempData.Sum(x => x.Target2) : 0,
                        Actual = preTempData.Any() ? preTempData.Sum(x => x.Actual) : 0,
                        ProductVarientId = varient.Id,
                        preTarget1 = preTempData.Any() ? preTempData.Sum(x => x.Target1) : 0,
                        preTarget2 = preTempData.Any() ? preTempData.Sum(x => x.Target2) : 0,
                        IsEditable = manpower.Type.ToLower() != "dsm" && (manpowerProductVarientIds.Contains(varient.Id) || isEdit)
                    });
                }
                targetModel.Targets = targetPlanList;
                targetList.Add(targetModel);
            }
            var model = new TargetViewModel {
                CsmId = csm.Id,
                MonthId = month.Id,
                DealerId = id,
                Targets = targetList.OrderBy(x => x.Designation).ThenBy(x => x.Manpower),
                ProductVarients = products.Select(
                    x =>
                    new ProductVarients {
                        Product = x.Name,
                        Varients = x.ProductVarients.OrderBy(y => y.Name).Select(y => y.Name).ToList()
                    }).
                    ToList(),
            };

            ViewBag.Month = string.Format("{0} - {1}", month.Name, month.Year);
            ViewBag.List = Session["BreadcrumbList"];
            return View(model);
        }
Exemplo n.º 2
0
 public ActionResult Targets(int id)
 {
     Session["BreadcrumbList"] = Utils.HtmlExtensions.SetBreadcrumbs((List<BreadcrumbModel>)Session["BreadcrumbList"], string.Format("/Dealer/Targets/{0}", id), "Target");
     var now = DateTime.Now;
     var targetStatus = targetService.GetTargetStatus();
     var currentMonth = _masterService.FindAndCreateMonth(now.ToString("MMMM"), now.Year);
     var year = now.ToString("yyyy");
     now = now.AddMonths(-1);
     var previousMonth = _masterService.FindAndCreateMonth(now.ToString("MMMM"), now.Year);
     var csm = userService.GetUserByUserName(User.Identity.Name);
     var hasTarget = _retailService.HasReportFile(id, currentMonth.Id, csm.Id, "Targetsdocument");
     var manpowers = manpowerService.FindDealerManpowers(id, csm.Id).ToList();
     var manpowerIds = manpowers.Select(x => x.Id).ToList();
     var allVarients = _masterService.GetAllProductVarients().ToList();
     var products = _masterService.FindProducts(x => x.ProductVarients.Count > 0 && !x.IsCommon).OrderBy(x => x.Id).ToList();
     var currentMonthTarget = targetService.FindDealerMonthTarget(manpowerIds, new List<int> { currentMonth.Id }).ToList();
     var previousMonthTargets = targetService.FindDealerMonthTarget(manpowerIds, new List<int> { previousMonth.Id }).ToList();
     var targetList = new List<TargetModel>();
     var dealer = _masterService.GetDealer(id);
     foreach (var manpower in manpowers)
     {
         var isEdit = manpower.Product.IsCommon;
         var targetModel = new TargetModel { ManpowerId = manpower.Id, Manpower = manpower.Name, MonthId = currentMonth.Id, Designation = manpower.Type };
         var targetPlanList = new List<TargetPlanModel>();
         YearTarget yerarTarget = targetService.GetYearTarget(year, manpower.Id);
         targetModel.YearTarget1 = yerarTarget.Target1;
         targetModel.YearTarget2 = yerarTarget.Target2;
         IEnumerable<int> manpowerProductVarientIds;
         if (isEdit)
         {
             manpowerProductVarientIds = allVarients.Select(x => x.Id);
         }
         else
         {
             manpowerProductVarientIds = allVarients.Where(x => x.ProductId == manpower.ProductId).Select(x => x.Id);
         }
         foreach (var varientId in manpowerProductVarientIds)
         {
             var motnhTempData = currentMonthTarget.Where(x => x.ProductVarientId == varientId && x.DealerManpowerId == manpower.Id);
             var preTempData = previousMonthTargets.Where(x => x.ProductVarientId == varientId && x.DealerManpowerId == manpower.Id);
             targetPlanList.Add(new TargetPlanModel
             {
                 Id = motnhTempData.Any() ? motnhTempData.First().Id : 0,
                 Target1 = motnhTempData.Any() ? motnhTempData.Sum(x => x.Target1) : 0,
                 Target2 = motnhTempData.Any() ? motnhTempData.Sum(x => x.Target2) : 0,
                 Actual = preTempData.Any() ? preTempData.Sum(x => x.Actual) : 0,
                 ProductVarientId = varientId,
                 preTarget1 = preTempData.Any() ? preTempData.Sum(x => x.Target1) : 0,
                 preTarget2 = preTempData.Any() ? preTempData.Sum(x => x.Target2) : 0,
                 IsEditable = manpower.Type.ToLower() != "dsm" && (manpowerProductVarientIds.Contains(varientId) || isEdit) && hasTarget
             });
         }
         targetModel.Targets = targetPlanList;
         targetList.Add(targetModel);
     }
     var model = new TargetViewModel
     {
         CsmId = csm.Id,
         MonthId = currentMonth.Id,
         DealerId = id,
         Targets = targetList.OrderBy(x => x.Designation).ThenBy(x => x.Manpower),
         ProductVarients = products.Select(
             x =>
             new ProductVarients
             {
                 Product = x.Name,
                 Varients = x.ProductVarients.OrderBy(y => y.Name).Select(y => y.Name).ToList(),
                 VarientsDic = x.ProductVarients.OrderBy(y => y.Name).ToDictionary(y => y.Id, y => y.Name)
             }).
             ToList(),
         TargetStatus = targetStatus
     };
     ViewBag.Month = string.Format("{0} - {1}", currentMonth.Name, currentMonth.Year);
     ViewBag.Year = currentMonth.Year;
     ViewBag.List = Session["BreadcrumbList"];
     ViewBag.hasTarget = hasTarget;
     ViewBag.DealerName = dealer.Name;
     return View(model);
 }
Exemplo n.º 3
0
        public ActionResult SaveTargets(TargetViewModel model)
        {
            foreach (var targetModel in model.Targets) {
                var manpowerId = targetModel.ManpowerId;
                var monthId = targetModel.MonthId;
                foreach (var plan in targetModel.Targets) {
                    var targetPlan = TargetModel.ToDomainModel(plan);
                    targetPlan.MonthId = monthId;
                    targetPlan.DealerManpowerId = manpowerId;
                    if (targetPlan.Id > 0) {
                        targetService.UpdateTarget(targetPlan);
                    } else {
                        targetService.AddTarget(new[] { targetPlan });
                    }

                }
            }
            var dseList = model.Targets.Where(x => x.Designation == "DSE").Select(x => new DsmDseTargetMap { UserId = model.CsmId, MonthId = model.MonthId, DseId = x.ManpowerId });
            var dsmDseMapList = dsmDseTargetMapService.GetDsmDseTargetMap(dseList);
            foreach (var dsmDseTargetMap in dsmDseMapList) {
                targetService.UpdateDsmTarget(dsmDseTargetMap.DsmId, dsmDseTargetMap.UserId, dsmDseTargetMap.MonthId);
            }
            return Json(new { success = true }, JsonRequestBehavior.AllowGet);
        }
Exemplo n.º 4
0
 public ActionResult SaveTargets(TargetViewModel model)
 {
     var targetStatus = targetService.GetTargetStatus();
     if (!targetStatus)
         return RedirectToAction("Targets", new { id = model.DealerId });
     var yearTargets = new List<YearTarget>();
     var monthsTarget = new List<Repository.Target>();
     foreach (var targetModel in model.Targets)
     {
         var manpowerId = targetModel.ManpowerId;
         var monthId = model.MonthId;
         var yearTargetPlan = TargetModel.ToYearDomainModel(targetModel);
         yearTargetPlan.DealerManpowerId = targetModel.ManpowerId;
         yearTargets.Add(yearTargetPlan);
         if (targetModel.Targets != null)
             foreach (var plan in targetModel.Targets)
             {
                 var targetPlan = TargetModel.ToDomainModel(plan);
                 targetPlan.MonthId = monthId;
                 targetPlan.DealerManpowerId = manpowerId;
                 monthsTarget.Add(targetPlan);
             }
     }
     targetService.SaveTargets(monthsTarget);
     targetService.UpdateYearTarget(yearTargets);
     var dseList = model.Targets.Where(x => x.Designation == "DSE").Select(x => new DsmDseTargetMap { UserId = model.CsmId, MonthId = model.MonthId, DseId = x.ManpowerId });
     var dsmDseMapList = dsmDseTargetMapService.GetDsmDseTargetMap(dseList);
     foreach (var dsmDseTargetMap in dsmDseMapList)
     {
         targetService.UpdateDsmTarget(dsmDseTargetMap.DsmId, dsmDseTargetMap.UserId, dsmDseTargetMap.MonthId);
     }
     return RedirectToAction("Targets", new { id = model.DealerId });
 }