public ActionResult CommitSingleCoachBonus(CoachBonusViewModel viewModel)
        {
            ViewResult         result = (ViewResult)ProcessBonus(viewModel);
            CoachMonthlySalary item   = result.Model as CoachMonthlySalary;

            if (item == null)
            {
                return(result);
            }

            if (!viewModel.SpecialBonus.HasValue && !viewModel.ManagerBonus.HasValue)
            {
                ModelState.AddModelError("ManagerBonus", "請輸入管理獎金");
                ModelState.AddModelError("SpecialBonus", "請輸特別獎金");
            }

            if (!ModelState.IsValid)
            {
                ViewBag.ModelState = ModelState;
                return(View(ConsoleHomeController.InputErrorView));
            }

            item.SpecialBonus = viewModel.SpecialBonus;
            item.ManagerBonus = viewModel.ManagerBonus;

            models.SubmitChanges();

            return(Json(new { result = true }));
        }
        public ActionResult EditCoachBonus(CoachBonusViewModel viewModel)
        {
            ViewResult         result = (ViewResult)ProcessBonus(viewModel);
            CoachMonthlySalary item   = result.Model as CoachMonthlySalary;

            if (item == null)
            {
                return(result);
            }

            viewModel.SpecialBonus = item.SpecialBonus;
            viewModel.ManagerBonus = item.ManagerBonus;

            return(View("~/Views/BonusCredit/BonusModal/EditCoachBonus.cshtml", item));
        }
        public ActionResult ClearCoachBonus(CoachBonusViewModel viewModel)
        {
            ViewResult         result = (ViewResult)ProcessBonus(viewModel);
            CoachMonthlySalary item   = result.Model as CoachMonthlySalary;

            if (item == null)
            {
                return(result);
            }

            item.SpecialBonus = (int?)null;
            item.ManagerBonus = (int?)null;

            models.SubmitChanges();

            return(Json(new { result = true }));
        }
Beispiel #4
0
        public static void ExecuteLessonPerformanceSettlement <TEntity>(this ModelSource <TEntity> models, DateTime startDate, DateTime endExclusiveDate)
            where TEntity : class, new()
        {
            var settlement = models.GetTable <Settlement>().Where(s => startDate <= s.SettlementDate && endExclusiveDate > s.SettlementDate).FirstOrDefault();

            if (settlement == null)
            {
                return;
            }

            LessonTimeAchievementHelper <TEntity> helper = new LessonTimeAchievementHelper <TEntity>(models)
            {
                LessonItems = models.GetTable <V_Tuition>().Where(l => l.ClassTime >= settlement.StartDate && l.ClassTime < settlement.EndExclusiveDate)
            };

            foreach (var item in helper.SettlementFullAchievement)
            {
                models.ExecuteCommand("update LessonTimeSettlement set SettlementStatus = {0},SettlementID = {1} where LessonID={2}", (int)Naming.LessonSettlementStatus.FullAchievement, settlement.SettlementID, item.LessonID);
            }

            foreach (var item in helper.SettlementHalfAchievement)
            {
                models.ExecuteCommand("update LessonTimeSettlement set SettlementStatus = {0},SettlementID = {1} where LessonID={2}", (int)Naming.LessonSettlementStatus.HalfAchievement, settlement.SettlementID, item.LessonID);
            }

            foreach (var item in helper.SettlementVainAchievement)
            {
                models.ExecuteCommand("update LessonTimeSettlement set SettlementID = {0} where LessonID={1}", settlement.SettlementID, item.LessonID);
            }

            var coachItems     = models.PromptEffectiveCoach();
            var salaryTable    = models.GetTable <CoachMonthlySalary>();
            var countableItems = helper.PerformanceCountableLesson;

            var paymentItems = models.GetTable <Payment>().Where(p => p.PayoffDate >= settlement.StartDate && p.PayoffDate < settlement.EndExclusiveDate);
            //.FilterByEffective();
            IQueryable <TuitionAchievement> achievementItems = paymentItems.GetPaymentAchievement(models);

            foreach (var coach in coachItems)
            {
                var lessonItem = countableItems.Where(l => l.AttendingCoach == coach.CoachID).FirstOrDefault();
                var salary     = salaryTable.Where(s => s.CoachID == coach.CoachID && s.SettlementID == settlement.SettlementID).FirstOrDefault();
                if (salary == null)
                {
                    salary = new CoachMonthlySalary
                    {
                        CoachID      = coach.CoachID,
                        SettlementID = settlement.SettlementID,
                    };
                    salaryTable.InsertOnSubmit(salary);
                }

                if (coach.CoachWorkplace.Count == 1)
                {
                    salary.WorkPlace = coach.CoachWorkplace.First().BranchID;
                }

                if (lessonItem != null)
                {
                    salary.LevelID    = lessonItem.ProfessionalLevelID;
                    salary.GradeIndex = lessonItem.MarkedGradeIndex ?? 0;
                }
                else
                {
                    salary.LevelID    = coach.LevelID ?? 0;
                    salary.GradeIndex = coach.ProfessionalLevel?.GradeIndex ?? 0;
                }

                var performanceItems = achievementItems.Where(t => t.CoachID == coach.CoachID);
                salary.PerformanceAchievement = performanceItems.Sum(t => t.ShareAmount) ?? 0;

                models.SubmitChanges();

                void calcCoachAchievement()
                {
                    var attendingLessons = countableItems.Where(l => l.AttendingCoach == coach.CoachID);

                    foreach (var g in attendingLessons.GroupBy(l => l.BranchID))
                    {
                        var branchBonus = salary.CoachBranchMonthlyBonus.Where(b => b.BranchID == g.Key).FirstOrDefault();
                        if (branchBonus == null)
                        {
                            branchBonus = new CoachBranchMonthlyBonus
                            {
                                CoachMonthlySalary = salary,
                                BranchID           = g.Key.Value
                            };
                            salary.CoachBranchMonthlyBonus.Add(branchBonus);
                        }

                        LessonTimeAchievementHelper <TEntity> branchHelper = new LessonTimeAchievementHelper <TEntity>(models)
                        {
                            LessonItems = attendingLessons.Where(l => l.BranchID == g.Key)
                        };

                        branchBonus.AchievementAttendanceCount = branchHelper.LessonItems.Count()
                                                                 - branchHelper.SettlementPILesson.Count() / 2m;

                        branchBonus.Tuition = branchHelper.LessonItems.CalcTuition(models);

                        branchBonus.AttendanceBonus = branchHelper.LessonItems.CalcTuitionShare(models);

                        models.SubmitChanges();
                    }

                    var     attendanceCount = salary.CoachBranchMonthlyBonus.Sum(b => b.AchievementAttendanceCount);
                    decimal shareRatio      = 3.5m;

                    for (int i = 0; i < PerformanceAchievementIndex.Length; i++)
                    {
                        if (salary.PerformanceAchievement >= PerformanceAchievementIndex[i])
                        {
                            shareRatio += ShareRatioIncrementForPerformance[i];
                            break;
                        }
                    }
                    for (int i = 0; i < AttendingLessonIndex.Length; i++)
                    {
                        if (attendanceCount >= AttendingLessonIndex[i])
                        {
                            shareRatio += ShareRatioIncrementForAttendance[i];
                            break;
                        }
                    }

                    salary.AchievementShareRatio = shareRatio;
                    models.SubmitChanges();
                };

                BranchStore branch;
                if (coach.UserProfile.IsOfficer())
                {
                    foreach (var g in countableItems.GroupBy(l => l.BranchID))
                    {
                        var branchBonus = salary.CoachBranchMonthlyBonus.Where(b => b.BranchID == g.Key).FirstOrDefault();
                        if (branchBonus == null)
                        {
                            branchBonus = new CoachBranchMonthlyBonus
                            {
                                CoachMonthlySalary = salary,
                                BranchID           = g.Key.Value
                            };
                            salary.CoachBranchMonthlyBonus.Add(branchBonus);
                        }

                        branchBonus.BranchTotalAttendanceCount = g.Count();
                        branchBonus.BranchTotalTuition         = g.CalcTuition(models);
                        models.SubmitChanges();
                    }

                    calcCoachAchievement();
                }
                else if ((branch = models.GetTable <BranchStore>().Where(b => b.ManagerID == coach.CoachID || b.ViceManagerID == coach.CoachID).FirstOrDefault()) != null)
                {
                    var branchBonus = salary.CoachBranchMonthlyBonus.Where(b => b.BranchID == branch.BranchID).FirstOrDefault();
                    if (branchBonus == null)
                    {
                        branchBonus = new CoachBranchMonthlyBonus
                        {
                            CoachMonthlySalary = salary,
                            BranchID           = branch.BranchID
                        };
                        salary.CoachBranchMonthlyBonus.Add(branchBonus);
                    }
                    var branchItems = countableItems.Where(w => w.WorkPlace == branch.BranchID);
                    branchBonus.BranchTotalAttendanceCount = branchItems.Count();
                    branchBonus.BranchTotalTuition         = branchItems.CalcTuition(models);
                    models.SubmitChanges();

                    calcCoachAchievement();
                }
                else
                {
                    calcCoachAchievement();
                }
            }
        }