// GET: PaymentConsole
        public ActionResult ShowPaymentList(PaymentQueryViewModel viewModel)
        {
            if (viewModel.KeyID != null)
            {
                viewModel.PaymentID = viewModel.DecryptKeyValue();
            }

            IQueryable <Payment> items = viewModel.InquirePayment(this, out string alertMessage);

            return(View("~/Views/PaymentConsole/Module/PaymentItemsList.cshtml", items));
        }
        public ActionResult InquirePayment(PaymentQueryViewModel viewModel)
        {
            if (viewModel.KeyID != null)
            {
                viewModel.PaymentID = viewModel.DecryptKeyValue();
            }

            viewModel.CustomQuery = viewModel.CustomQuery.GetEfficientString();
            //if (viewModel.CustomQuery != null)
            //{
            //    viewModel.ContractNo = viewModel.UserName = viewModel.InvoiceNo = viewModel.CustomQuery;
            //}

            if (viewModel.CustomQuery == null)
            {
                bool hasQuery = false;
                if (!viewModel.PayoffDateFrom.HasValue)
                {
                    ModelState.AddModelError("PayoffDateFrom", "請選擇查詢起日");
                }
                else
                {
                    hasQuery = true;
                }

                if (!viewModel.PayoffDateTo.HasValue)
                {
                    ModelState.AddModelError("PayoffDateTo", "請選擇查詢迄日");
                }
                else
                {
                    hasQuery = true;
                }

                if (!hasQuery)
                {
                    ModelState.AddModelError("CustomQuery", "請輸入學生姓名(暱稱) 或 合約編號 或 發票號碼");
                }
            }


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

            IQueryable <Payment> items = viewModel.InquirePayment(this, out string alertMessage);

            return(View("~/Views/PaymentConsole/Module/CustomPaymentList.cshtml", items));
        }
Пример #3
0
        public static async Task <IQueryable <Payment> > InquirePaymentAsync(this PaymentQueryViewModel queryViewModel, SampleController <UserProfile> controller)

        {
            var ModelState  = controller.ModelState;
            var ViewBag     = controller.ViewBag;
            var HttpContext = controller.HttpContext;
            var models      = controller.DataSource;

            ViewBag.ViewModel = queryViewModel;
            PaymentQueryViewModel viewModel = (PaymentQueryViewModel)queryViewModel.Duplicate();
            var profile = await HttpContext.GetUserAsync();

            return(viewModel.InquirePayment(models));
        }
Пример #4
0
        public static IQueryable <Payment> InquirePayment <TEntity>(this PaymentQueryViewModel queryViewModel, SampleController <TEntity> controller, out String alertMessage)
            where TEntity : class, new()
        {
            alertMessage = null;
            var ModelState  = controller.ModelState;
            var ViewBag     = controller.ViewBag;
            var HttpContext = controller.HttpContext;
            var models      = controller.DataSource;

            ViewBag.ViewModel = queryViewModel;
            PaymentQueryViewModel viewModel = (PaymentQueryViewModel)queryViewModel.Duplicate();
            var profile = HttpContext.GetUser();

            return(viewModel.InquirePayment(models));
        }
        public ActionResult RestoreCoachRevenueIndicator(MonthlyCoachRevenueIndicatorQueryViewModel viewModel, int[] exclusiveCoach = null)
        {
            ViewBag.ViewModel = viewModel;

            IQueryable <MonthlyIndicator> indicatorItems = models.GetTable <MonthlyIndicator>();

            if (viewModel.DateFrom.HasValue)
            {
                viewModel.DateFrom = viewModel.DateFrom.Value.FirstDayOfMonth();
                indicatorItems.Where(i => i.StartDate >= viewModel.DateFrom);
            }
            if (viewModel.DateTo.HasValue)
            {
                viewModel.DateTo = viewModel.DateTo.Value.FirstDayOfMonth().AddMonths(1);
                indicatorItems.Where(i => i.StartDate < viewModel.DateTo);
            }

            IQueryable <ServingCoach> coachItems = models.GetTable <ServingCoach>();

            if (viewModel.CoachID.HasValue)
            {
                coachItems = coachItems.Where(c => c.CoachID == viewModel.CoachID);
            }

            if (exclusiveCoach != null && exclusiveCoach.Length > 0)
            {
                coachItems = coachItems
                             .Where(c => !exclusiveCoach.Contains(c.CoachID))
                             .Where(c => c.CoachWorkplace.Any());
            }

            var forCoaches = coachItems.ToArray();

            foreach (MonthlyIndicator indicator in indicatorItems)
            {
                AchievementQueryViewModel queryModel = new AchievementQueryViewModel
                {
                    AchievementDateFrom = indicator.StartDate,
                    BypassCondition     = true,
                };

                IQueryable <V_Tuition> lessonItems = queryModel.InquireAchievement(models);
                lessonItems = lessonItems.Where(l => l.SettlementID.HasValue);
                IQueryable <V_Tuition> tuitionItems = lessonItems;

                PaymentQueryViewModel paymentQuery = new PaymentQueryViewModel
                {
                    PayoffDateFrom  = indicator.StartDate,
                    PayoffDateTo    = indicator.EndExclusiveDate.AddDays(-1),
                    BypassCondition = true,
                };

                IQueryable <Payment>            paymentItems     = paymentQuery.InquirePayment(models);
                IQueryable <TuitionAchievement> achievementItems = paymentItems.GetPaymentAchievement(models);

                CourseContractQueryViewModel contractQuery = new CourseContractQueryViewModel
                {
                    ContractQueryMode = Naming.ContractServiceMode.ContractOnly,
                    Status            = (int)Naming.CourseContractStatus.已生效,
                    EffectiveDateFrom = indicator.StartDate,
                    EffectiveDateTo   = indicator.EndExclusiveDate,
                };
                IQueryable <CourseContract> contractItems = contractQuery.InquireContract(models);

                void calcCoachAchievement(MonthlyCoachRevenueIndicator coachIndicator)
                {
                    int lessonAchievement, tuitionAchievement;

                    var coachTuitionItems     = tuitionItems.Where(t => t.AttendingCoach == coachIndicator.CoachID);
                    var coachAchievementItems = achievementItems.Where(t => t.CoachID == coachIndicator.CoachID);
                    var coachContractItems    = contractItems.Where(c => c.FitnessConsultant == coachIndicator.CoachID);

                    lessonAchievement  = coachTuitionItems.Where(t => BusinessConsoleExtensions.SessionScopeForAchievement.Contains(t.PriceStatus)).Sum(t => t.ListPrice * t.GroupingMemberCount * t.PercentageOfDiscount / 100) ?? 0;
                    lessonAchievement += (coachTuitionItems.Where(t => BusinessConsoleExtensions.SessionScopeForAchievement.Contains(t.ELStatus)).Sum(l => l.EnterpriseListPrice * l.GroupingMemberCount * l.PercentageOfDiscount / 100) ?? 0);
                    tuitionAchievement = coachAchievementItems.Sum(a => a.ShareAmount) ?? 0;

                    coachIndicator.ActualCompleteLessonCount = coachTuitionItems.Where(t => BusinessConsoleExtensions.SessionScopeForComleteLessonCount.Contains(t.PriceStatus)).Count()
                                                               + coachTuitionItems.Where(t => BusinessConsoleExtensions.SessionScopeForComleteLessonCount.Contains(t.ELStatus)).Count();
                    coachIndicator.ActualLessonAchievement        = lessonAchievement;
                    coachIndicator.ActualRenewContractAchievement = coachContractItems.Where(c => c.Renewal == true).Count();
                    coachIndicator.ActualNewContractAchievement   = coachContractItems.Count() - coachIndicator.ActualRenewContractAchievement;
                    coachIndicator.ActualCompleteTSCount          = coachTuitionItems.Where(t => t.PriceStatus == (int)Naming.LessonPriceStatus.體驗課程).Count()
                                                                    + coachTuitionItems.Where(t => t.ELStatus == (int)Naming.LessonPriceStatus.體驗課程).Count();
                    coachIndicator.ActualCompletePICount = coachTuitionItems.Where(t => t.PriceStatus == (int)Naming.LessonPriceStatus.自主訓練).Count()
                                                           + coachTuitionItems.Where(t => t.ELStatus == (int)Naming.LessonPriceStatus.自主訓練).Count();
                    coachIndicator.ATCount       = coachTuitionItems.Where(t => t.PriceStatus == (int)Naming.LessonPriceStatus.運動防護課程).Count();
                    coachIndicator.ATAchievement = coachTuitionItems.Where(t => t.PriceStatus == (int)Naming.LessonPriceStatus.運動防護課程).Sum(t => t.ListPrice) ?? 0;
                    coachIndicator.SRCount       = coachTuitionItems.Where(t => t.PriceStatus == (int)Naming.LessonPriceStatus.運動恢復課程).Count();
                    coachIndicator.SRAchievement = coachTuitionItems.Where(t => t.PriceStatus == (int)Naming.LessonPriceStatus.運動恢復課程).Sum(t => t.ListPrice) ?? 0;
                    coachIndicator.SDCount       = coachTuitionItems.Where(t => t.PriceStatus == (int)Naming.LessonPriceStatus.營養課程).Count();
                    coachIndicator.SDAchievement = coachTuitionItems.Where(t => t.PriceStatus == (int)Naming.LessonPriceStatus.營養課程).Sum(t => t.ListPrice) ?? 0;

                    models.SubmitChanges();
                }

                foreach (var coach in forCoaches)
                {
                    var item = models.GetTable <MonthlyCoachRevenueIndicator>()
                               .Where(c => c.PeriodID == indicator.PeriodID && c.CoachID == coach.CoachID).FirstOrDefault();

                    if (item == null)
                    {
                        var attendingLessons = models.GetTable <LessonTime>()
                                               .Where(l => l.AttendingCoach == coach.CoachID)
                                               .Where(l => l.ClassTime >= indicator.StartDate)
                                               .Where(l => l.ClassTime < indicator.EndExclusiveDate);

                        if (attendingLessons.Any())
                        {
                            item = new MonthlyCoachRevenueIndicator
                            {
                                PeriodID = indicator.PeriodID,
                                CoachID  = coach.CoachID,
                                BranchID = coach.CoachWorkplace.First().BranchID,
                                LevelID  = coach.LevelID,
                            };
                            models.GetTable <MonthlyCoachRevenueIndicator>().InsertOnSubmit(item);

                            item.AchievementGoal     = 0;
                            item.CompleteLessonsGoal = 0;
                            item.AverageLessonPrice  = indicator.CalculateAverageLessonPrice(models, coach.CoachID);
                            item.BRCount             = 0;
                            item.LessonTuitionGoal   = 0;

                            models.SubmitChanges();

                            calcCoachAchievement(item);
                        }
                    }
                }
            }

            return(Json(new { result = true }));
        }
Пример #6
0
        public static void UpdateMonthlyAchievement <TEntity>(this MonthlyIndicator item, ModelSource <TEntity> models, bool?forcedUpdate = null, bool?calcAverage = null)
            where TEntity : class, new()
        {
            AchievementQueryViewModel queryModel = new AchievementQueryViewModel
            {
                AchievementDateFrom = item.StartDate,
                BypassCondition     = true,
            };

            IQueryable <Payment> voidPayment = UpdateVoidShare(models, item.StartDate, item.EndExclusiveDate);

            IQueryable <V_Tuition>  lessonItems = queryModel.InquireAchievement(models);
            IQueryable <LessonTime> STItems     = models.GetTable <LessonTime>()
                                                  .Where(c => c.ClassTime >= item.StartDate)
                                                  .Where(c => c.ClassTime < item.StartDate.AddMonths(1))
                                                  .Where(t => t.TrainingBySelf == 2);

            if (forcedUpdate == true)
            {
                lessonItems = lessonItems.Where(l => l.SettlementID.HasValue);
            }
            else if (item.StartDate == DateTime.Today.FirstDayOfMonth())
            {
                lessonItems = lessonItems.Where(l => l.ClassTime < DateTime.Today);
            }
            else
            {
                lessonItems = lessonItems.Where(l => l.SettlementID.HasValue);
            }

            IQueryable <V_Tuition> tuitionItems = lessonItems;

            PaymentQueryViewModel paymentQuery = new PaymentQueryViewModel
            {
                PayoffDateFrom  = item.StartDate,
                PayoffDateTo    = item.EndExclusiveDate.AddDays(-1),
                BypassCondition = true,
            };

            IQueryable <Payment>            paymentItems     = paymentQuery.InquirePayment(models);
            IQueryable <TuitionAchievement> achievementItems = paymentItems.GetPaymentAchievement(models);

            CourseContractQueryViewModel contractQuery = new CourseContractQueryViewModel
            {
                ContractQueryMode = Naming.ContractServiceMode.ContractOnly,
                Status            = (int)Naming.CourseContractStatus.已生效,
                EffectiveDateFrom = item.StartDate,
                EffectiveDateTo   = item.EndExclusiveDate,
            };
            IQueryable <CourseContract> contractItems       = contractQuery.InquireContract(models);
            IQueryable <CourseContract> installmentItems    = contractItems.Where(c => c.Installment.HasValue);
            IQueryable <CourseContract> nonInstallmentItems = contractItems.Where(c => !c.Installment.HasValue);

            int lessonAchievement, tuitionAchievement;

            void calcHeadquarterAchievement()
            {
                var voidTuition = voidPayment
                                  .Join(models.GetTable <TuitionAchievement>(), p => p.PaymentID, t => t.InstallmentID, (p, t) => t);

                lessonAchievement  = tuitionItems.Where(t => SessionScopeForAchievement.Contains(t.PriceStatus)).Sum(t => t.ListPrice * t.GroupingMemberCount * t.PercentageOfDiscount / 100) ?? 0;
                lessonAchievement += (tuitionItems.Where(t => SessionScopeForAchievement.Contains(t.ELStatus)).Sum(l => l.EnterpriseListPrice * l.GroupingMemberCount * l.PercentageOfDiscount / 100) ?? 0);
                tuitionAchievement = achievementItems.Sum(a => a.ShareAmount) ?? 0;

                IQueryable <TuitionAchievement> newContractAchievementItems = paymentItems
                                                                              .Join(models.GetTable <ContractPayment>()
                                                                                    .Join(models.GetTable <CourseContract>()
                                                                                          .Where(t => !t.Installment.HasValue)
                                                                                          .Where(t => !t.Renewal.HasValue || t.Renewal != true),
                                                                                          c => c.ContractID, t => t.ContractID, (c, t) => c),
                                                                                    p => p.PaymentID, c => c.PaymentID, (p, c) => p)
                                                                              .GetPaymentAchievement(models);

                IQueryable <TuitionAchievement> renewContractAchievementItems = paymentItems
                                                                                .Join(models.GetTable <ContractPayment>()
                                                                                      .Join(models.GetTable <CourseContract>()
                                                                                            .Where(t => !t.Installment.HasValue)
                                                                                            .Where(t => t.Renewal == true),
                                                                                            c => c.ContractID, t => t.ContractID, (c, t) => c),
                                                                                      p => p.PaymentID, c => c.PaymentID, (p, c) => p)
                                                                                .GetPaymentAchievement(models);

                IQueryable <TuitionAchievement> installmentContractAchievementItems = paymentItems
                                                                                      .Join(models.GetTable <ContractPayment>()
                                                                                            .Join(models.GetTable <CourseContract>()
                                                                                                  .Where(t => t.Installment.HasValue),
                                                                                                  c => c.ContractID, t => t.ContractID, (c, t) => c),
                                                                                            p => p.PaymentID, c => c.PaymentID, (p, c) => p)
                                                                                      .GetPaymentAchievement(models);

                var revenueItem = item.MonthlyRevenueIndicator.Where(r => r.MonthlyRevenueGoal != null)
                                  .Select(r => r.MonthlyRevenueGoal)
                                  .FirstOrDefault();

                if (revenueItem != null)
                {
                    revenueItem.ActualCompleteLessonCount = tuitionItems.Where(t => SessionScopeForComleteLessonCount.Contains(t.PriceStatus)).Count()
                                                            + tuitionItems.Where(t => SessionScopeForComleteLessonCount.Contains(t.ELStatus)).Count();
                    revenueItem.ActualLessonAchievement = lessonAchievement;
                    revenueItem.ActualSharedAchievement = tuitionAchievement;
                    revenueItem.RenewContractCount      = nonInstallmentItems.Where(c => c.Renewal == true).Count();
                    revenueItem.NewContractCount        = nonInstallmentItems.Count() - revenueItem.RenewContractCount;
                    revenueItem.RenewContractSubtotal   = nonInstallmentItems.Where(c => c.Renewal == true).Sum(c => c.TotalCost) ?? 0;
                    revenueItem.NewContractSubtotal     = nonInstallmentItems.Sum(c => c.TotalCost) - revenueItem.RenewContractSubtotal;
                    revenueItem.InstallmentCount        = installmentItems.Count();
                    revenueItem.InstallmentSubtotal     = installmentItems.Sum(c => c.TotalCost) ?? 0;

                    revenueItem.ActualCompleteTSCount = tuitionItems.Where(t => t.PriceStatus == (int)Naming.LessonPriceStatus.體驗課程).Count()
                                                        + tuitionItems.Where(t => t.ELStatus == (int)Naming.LessonPriceStatus.體驗課程).Count();
                    revenueItem.ActualCompletePICount = tuitionItems.Where(t => t.PriceStatus == (int)Naming.LessonPriceStatus.自主訓練).Count()
                                                        + tuitionItems.Where(t => t.ELStatus == (int)Naming.LessonPriceStatus.自主訓練).Count();

                    revenueItem.NewContractAchievement   = newContractAchievementItems.Sum(t => t.ShareAmount) ?? 0;
                    revenueItem.RenewContractAchievement = renewContractAchievementItems.Sum(t => t.ShareAmount) ?? 0;
                    revenueItem.InstallmentAchievement   = installmentContractAchievementItems.Sum(t => t.ShareAmount) ?? 0;
                    revenueItem.VoidShare = voidTuition.Sum(t => t.VoidShare) ?? 0;

                    models.SubmitChanges();
                }
            }

            void calcBranchAchievement()
            {
                foreach (var branchIndicator in item.MonthlyBranchIndicator)
                {
                    var voidTuition = voidPayment
                                      .Join(models.GetTable <PaymentTransaction>().Where(t => t.BranchID == branchIndicator.BranchID), p => p.PaymentID, t => t.PaymentID, (p, t) => p)
                                      .Join(models.GetTable <TuitionAchievement>(), p => p.PaymentID, t => t.InstallmentID, (p, t) => t);

                    var branchTuitionItems        = tuitionItems.Where(t => t.CoachWorkPlace == branchIndicator.BranchID);
                    var branchAchievementItems    = achievementItems.Where(t => t.CoachWorkPlace == branchIndicator.BranchID);
                    var branchContractItems       = contractItems.Where(c => c.CourseContractExtension.BranchID == branchIndicator.BranchID);
                    var branchInstallmentItems    = installmentItems.Where(c => c.CourseContractExtension.BranchID == branchIndicator.BranchID);
                    var branchNonInstallmentItems = nonInstallmentItems.Where(c => c.CourseContractExtension.BranchID == branchIndicator.BranchID);

                    IQueryable <TuitionAchievement> branchNewContractAchievementItems = paymentItems
                                                                                        .Join(models.GetTable <ContractPayment>()
                                                                                              .Join(models.GetTable <CourseContract>()
                                                                                                    .Where(t => !t.Installment.HasValue)
                                                                                                    .Where(t => !t.Renewal.HasValue || t.Renewal != true),
                                                                                                    c => c.ContractID, t => t.ContractID, (c, t) => c),
                                                                                              p => p.PaymentID, c => c.PaymentID, (p, c) => p)
                                                                                        .GetPaymentAchievement(models)
                                                                                        .Where(t => t.CoachWorkPlace == branchIndicator.BranchID);

                    IQueryable <TuitionAchievement> branchRenewContractAchievementItems = paymentItems
                                                                                          .Join(models.GetTable <ContractPayment>()
                                                                                                .Join(models.GetTable <CourseContract>()
                                                                                                      .Where(t => !t.Installment.HasValue)
                                                                                                      .Where(t => t.Renewal == true),
                                                                                                      c => c.ContractID, t => t.ContractID, (c, t) => c),
                                                                                                p => p.PaymentID, c => c.PaymentID, (p, c) => p)
                                                                                          .GetPaymentAchievement(models)
                                                                                          .Where(t => t.CoachWorkPlace == branchIndicator.BranchID);

                    IQueryable <TuitionAchievement> branchInstallmentContractAchievementItems = paymentItems
                                                                                                .Join(models.GetTable <ContractPayment>()
                                                                                                      .Join(models.GetTable <CourseContract>()
                                                                                                            .Where(t => t.Installment.HasValue),
                                                                                                            c => c.ContractID, t => t.ContractID, (c, t) => c),
                                                                                                      p => p.PaymentID, c => c.PaymentID, (p, c) => p)
                                                                                                .GetPaymentAchievement(models)
                                                                                                .Where(t => t.CoachWorkPlace == branchIndicator.BranchID);

                    var revenueItem = branchIndicator.MonthlyBranchRevenueIndicator
                                      .Where(v => v.MonthlyBranchRevenueGoal != null)
                                      .FirstOrDefault()?.MonthlyBranchRevenueGoal;
                    if (revenueItem != null)
                    {
                        lessonAchievement  = branchTuitionItems.Where(t => SessionScopeForAchievement.Contains(t.PriceStatus)).Sum(t => t.ListPrice * t.GroupingMemberCount * t.PercentageOfDiscount / 100) ?? 0;
                        lessonAchievement += (branchTuitionItems.Where(t => SessionScopeForAchievement.Contains(t.ELStatus)).Sum(l => l.EnterpriseListPrice * l.GroupingMemberCount * l.PercentageOfDiscount / 100) ?? 0);
                        tuitionAchievement = branchAchievementItems.Sum(a => a.ShareAmount) ?? 0;

                        revenueItem.ActualCompleteLessonCount = branchTuitionItems.Where(t => SessionScopeForComleteLessonCount.Contains(t.PriceStatus)).Count()
                                                                + branchTuitionItems.Where(t => SessionScopeForComleteLessonCount.Contains(t.ELStatus)).Count();
                        revenueItem.ActualLessonAchievement = lessonAchievement;
                        revenueItem.ActualSharedAchievement = tuitionAchievement;
                        revenueItem.RenewContractCount      = branchNonInstallmentItems.Where(c => c.Renewal == true).Count();
                        revenueItem.NewContractCount        = branchNonInstallmentItems.Count() - revenueItem.RenewContractCount;
                        revenueItem.RenewContractSubtotal   = branchNonInstallmentItems.Where(c => c.Renewal == true).Sum(c => c.TotalCost) ?? 0;
                        revenueItem.NewContractSubtotal     = branchNonInstallmentItems.Sum(c => c.TotalCost) - revenueItem.RenewContractSubtotal;
                        revenueItem.InstallmentCount        = branchInstallmentItems.Count();
                        revenueItem.InstallmentSubtotal     = branchInstallmentItems.Sum(c => c.TotalCost) ?? 0;

                        revenueItem.ActualCompleteTSCount = branchTuitionItems.Where(t => t.PriceStatus == (int)Naming.LessonPriceStatus.體驗課程).Count()
                                                            + branchTuitionItems.Where(t => t.ELStatus == (int)Naming.LessonPriceStatus.體驗課程).Count();
                        revenueItem.ActualCompletePICount = branchTuitionItems.Where(t => t.PriceStatus == (int)Naming.LessonPriceStatus.自主訓練).Count()
                                                            + branchTuitionItems.Where(t => t.ELStatus == (int)Naming.LessonPriceStatus.自主訓練).Count();

                        revenueItem.NewContractAchievement   = branchNewContractAchievementItems.Sum(t => t.ShareAmount) ?? 0;
                        revenueItem.RenewContractAchievement = branchRenewContractAchievementItems.Sum(t => t.ShareAmount) ?? 0;
                        revenueItem.InstallmentAchievement   = branchInstallmentContractAchievementItems.Sum(t => t.ShareAmount) ?? 0;
                        revenueItem.VoidShare = voidTuition.Sum(t => t.VoidShare) ?? 0;

                        models.SubmitChanges();
                    }
                }
            }

            void calcCoachAchievement()
            {
                foreach (var coachIndicator in item.MonthlyCoachRevenueIndicator)
                {
                    var voidTuition = voidPayment
                                      .Join(models.GetTable <TuitionAchievement>()
                                            .Where(t => t.CoachID == coachIndicator.CoachID),
                                            p => p.PaymentID, t => t.InstallmentID, (p, t) => t);

                    var coachTuitionItems     = tuitionItems.Where(t => t.AttendingCoach == coachIndicator.CoachID);
                    var coachAchievementItems = achievementItems.Where(t => t.CoachID == coachIndicator.CoachID);
                    var coachContractItems    = contractItems.Where(c => c.FitnessConsultant == coachIndicator.CoachID);
                    IQueryable <CourseContract> coachInstallmentItems    = coachContractItems.Where(c => c.Installment.HasValue);
                    IQueryable <CourseContract> coachNonInstallmentItems = coachContractItems.Where(c => !c.Installment.HasValue);

                    var coachSTItems = STItems.Where(t => t.AttendingCoach == coachIndicator.CoachID);

                    IQueryable <TuitionAchievement> coachNewContractAchievementItems = paymentItems
                                                                                       .Join(models.GetTable <ContractPayment>()
                                                                                             .Join(models.GetTable <CourseContract>()
                                                                                                   .Where(t => !t.Installment.HasValue)
                                                                                                   .Where(t => !t.Renewal.HasValue || t.Renewal != true),
                                                                                                   c => c.ContractID, t => t.ContractID, (c, t) => c),
                                                                                             p => p.PaymentID, c => c.PaymentID, (p, c) => p)
                                                                                       .GetPaymentAchievement(models)
                                                                                       .Where(t => t.CoachID == coachIndicator.CoachID);

                    IQueryable <TuitionAchievement> coachRenewContractAchievementItems = paymentItems
                                                                                         .Join(models.GetTable <ContractPayment>()
                                                                                               .Join(models.GetTable <CourseContract>()
                                                                                                     .Where(t => !t.Installment.HasValue)
                                                                                                     .Where(t => t.Renewal == true),
                                                                                                     c => c.ContractID, t => t.ContractID, (c, t) => c),
                                                                                               p => p.PaymentID, c => c.PaymentID, (p, c) => p)
                                                                                         .GetPaymentAchievement(models)
                                                                                         .Where(t => t.CoachID == coachIndicator.CoachID);

                    IQueryable <TuitionAchievement> coachInstallmentContractAchievementItems = paymentItems
                                                                                               .Join(models.GetTable <ContractPayment>()
                                                                                                     .Join(models.GetTable <CourseContract>()
                                                                                                           .Where(t => t.Installment.HasValue),
                                                                                                           c => c.ContractID, t => t.ContractID, (c, t) => c),
                                                                                                     p => p.PaymentID, c => c.PaymentID, (p, c) => p)
                                                                                               .GetPaymentAchievement(models)
                                                                                               .Where(t => t.CoachID == coachIndicator.CoachID);


                    lessonAchievement  = coachTuitionItems.Where(t => SessionScopeForAchievement.Contains(t.PriceStatus)).Sum(t => t.ListPrice * t.GroupingMemberCount * t.PercentageOfDiscount / 100) ?? 0;
                    lessonAchievement += (coachTuitionItems.Where(t => SessionScopeForAchievement.Contains(t.ELStatus)).Sum(l => l.EnterpriseListPrice * l.GroupingMemberCount * l.PercentageOfDiscount / 100) ?? 0);
                    tuitionAchievement = coachAchievementItems.Sum(a => a.ShareAmount) ?? 0;

                    coachIndicator.ActualCompleteLessonCount = coachTuitionItems.Where(t => SessionScopeForComleteLessonCount.Contains(t.PriceStatus)).Count()
                                                               + coachTuitionItems.Where(t => SessionScopeForComleteLessonCount.Contains(t.ELStatus)).Count();
                    coachIndicator.ActualLessonAchievement        = lessonAchievement;
                    coachIndicator.ActualRenewContractAchievement = coachRenewContractAchievementItems.Sum(t => t.ShareAmount) ?? 0;
                    coachIndicator.ActualNewContractAchievement   = coachNewContractAchievementItems.Sum(t => t.ShareAmount) ?? 0;
                    coachIndicator.ActualCompleteTSCount          = coachTuitionItems.Where(t => t.PriceStatus == (int)Naming.LessonPriceStatus.體驗課程).Count()
                                                                    + coachTuitionItems.Where(t => t.ELStatus == (int)Naming.LessonPriceStatus.體驗課程).Count();
                    coachIndicator.ActualCompletePICount = coachTuitionItems.Where(t => t.PriceStatus == (int)Naming.LessonPriceStatus.自主訓練).Count()
                                                           + coachTuitionItems.Where(t => t.ELStatus == (int)Naming.LessonPriceStatus.自主訓練).Count();

                    coachIndicator.InstallmentAchievement = coachInstallmentContractAchievementItems.Sum(t => t.ShareAmount) ?? 0;
                    coachIndicator.RenewContractCount     = coachNonInstallmentItems.Where(t => t.Renewal == true).Count();
                    coachIndicator.NewContractCount       = coachNonInstallmentItems.Count() - coachIndicator.RenewContractCount;
                    coachIndicator.RenewContractSubtotal  = coachNonInstallmentItems.Where(c => c.Renewal == true).Sum(c => c.TotalCost) ?? 0;
                    coachIndicator.NewContractSubtotal    = coachNonInstallmentItems.Sum(c => c.TotalCost) - coachIndicator.RenewContractSubtotal;
                    coachIndicator.InstallmentCount       = coachInstallmentItems.Count();
                    coachIndicator.InstallmentSubtotal    = coachInstallmentItems.Sum(c => c.TotalCost) ?? 0;


                    if (calcAverage == true)
                    {
                        coachIndicator.AverageLessonPrice = item.CalculateAverageLessonPrice(models, coachIndicator.CoachID);
                        coachIndicator.LessonTuitionGoal  = coachIndicator.CompleteLessonsGoal * coachIndicator.AverageLessonPrice;
                    }

                    coachIndicator.STCount   = coachSTItems.Count();
                    coachIndicator.VoidShare = voidTuition.Sum(t => t.VoidShare) ?? 0;

                    models.SubmitChanges();
                }
            }

            calcHeadquarterAchievement();
            calcBranchAchievement();
            calcCoachAchievement();
        }