Exemple #1
0
        public ActionResult CommitCoachRevenueIndicator(MonthlyCoachRevenueIndicatorQueryViewModel viewModel)
        {
            ViewResult       result    = (ViewResult)LoadMonthlyIndicator(viewModel);
            MonthlyIndicator indicator = result.Model as MonthlyIndicator;

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

            var coach = models.GetTable <ServingCoach>().Where(c => c.CoachID == viewModel.CoachID).FirstOrDefault();

            if (coach == null)
            {
                return(View("~/Views/ConsoleHome/Shared/JsAlert.cshtml", model: "請選擇教練!!"));
            }

            if (!viewModel.BranchID.HasValue)
            {
                return(View("~/Views/ConsoleHome/Shared/JsAlert.cshtml", model: "請選擇分店!!"));
            }

            if (!viewModel.AchievementGoal.HasValue)
            {
                ModelState.AddModelError("AchievementGoal", "請輸收款業績");
            }

            if (viewModel.DataOperation != Naming.DataOperationMode.Create)
            {
                if (!viewModel.AverageLessonPrice.HasValue)
                {
                    ModelState.AddModelError("AverageLessonPrice", "請輸平均單價");
                }
            }

            if (!viewModel.CompleteLessonsGoal.HasValue)
            {
                ModelState.AddModelError("CompleteLessonsGoal", "請輸入課數");
            }

            if (!viewModel.BRCount.HasValue)
            {
                ModelState.AddModelError("BRCount", "請輸BR堂數");
            }

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

            var item = models.GetTable <MonthlyCoachRevenueIndicator>()
                       .Where(c => c.PeriodID == viewModel.PeriodID && c.CoachID == viewModel.CoachID).FirstOrDefault();

            if (item == null)
            {
                item = new MonthlyCoachRevenueIndicator
                {
                    PeriodID = indicator.PeriodID,
                    CoachID  = viewModel.CoachID.Value,
                    BranchID = viewModel.BranchID,
                    LevelID  = coach.LevelID,
                };
                models.GetTable <MonthlyCoachRevenueIndicator>().InsertOnSubmit(item);
            }

            item.AchievementGoal     = viewModel.AchievementGoal;
            item.CompleteLessonsGoal = viewModel.CompleteLessonsGoal;
            if (viewModel.DataOperation == Naming.DataOperationMode.Create)
            {
                item.AverageLessonPrice = indicator.CalculateAverageLessonPrice(models, viewModel.CoachID);;
            }
            else
            {
                item.AverageLessonPrice = viewModel.AverageLessonPrice;
            }
            item.BRCount           = viewModel.BRCount;
            item.LessonTuitionGoal = item.AverageLessonPrice * item.CompleteLessonsGoal;

            models.SubmitChanges();

            indicator.UpdateMonthlyAchievementGoal(models);

            return(Json(new { result = true }, JsonRequestBehavior.AllowGet));
        }
Exemple #2
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();
        }