Esempio n. 1
0
        public async Task<ActionResult> ConfirmSlots(List<RegistrationFinancialViewModel> registrations)
        {
            var ident = HttpContext.User.Identity as ClaimsIdentity;
            var userId = ident.GetUserId();
            var user = await userManager.FindByIdAsync(userId);
            if (user == null)
            {
                String returnUrl = null;
                if (HttpContext.Request != null)
                {
                    returnUrl = HttpContext.Request.RawUrl;
                }
                return RedirectToAction("Login", "User", new { Area = "", returnUrl = returnUrl });
            }
            var tournamentId = user.CurrentTournamentId;
            var currentTournamentId = (Guid)(tournamentId == null ? Guid.Empty : tournamentId);

            List<PaymentViewModel> changes = new List<PaymentViewModel>();

            // Check Model
            foreach (var registration in registrations)
            {
                var oldRegistration = registrationManager.GetRegistration(currentTournamentId, registration.OrganizationId);

                if (oldRegistration.AdjudicatorsPaid != registration.AdjudicatorsPaid
                    || oldRegistration.TeamsPaid != registration.TeamsPaid)
                {

                    if (oldRegistration.TeamsPaid > registration.TeamsPaid)
                    {
                        ModelState.AddModelError("[" + registrations.IndexOf(registration) + "].TeamsPaid", Resources.TournamentManagement.Finance.Strings.ErrorTeamsPaidConfirmedNewBelowOld);
                    }


                    if (oldRegistration.TeamsGranted < registration.TeamsPaid)
                    {
                        ModelState.AddModelError("[" + registrations.IndexOf(registration) + "].TeamsPaid", Resources.TournamentManagement.Finance.Strings.ErrorTeamsPaidConfirmedOverGranted);
                    }

                    if (oldRegistration.AdjudicatorsPaid > registration.AdjudicatorsPaid)
                    {
                        ModelState.AddModelError("[" + registrations.IndexOf(registration) + "].AdjudicatorsPaid", Resources.TournamentManagement.Finance.Strings.ErrorAdjudicatorsPaidNewBelowOld);
                    }

                    if (oldRegistration.AdjudicatorsGranted < registration.AdjudicatorsPaid)
                    {
                        ModelState.AddModelError("[" + registrations.IndexOf(registration) + "].AdjudicatorsPaid", Resources.TournamentManagement.Finance.Strings.ErrorAdjudicatorsPaidConfirmedOverGranted);
                    }

                    if (ModelState.IsValid)
                    {
                        PaymentViewModel paymentViewModel = new PaymentViewModel
                        {
                            OrganizationId = oldRegistration.OrganizationId,
                            Balance = bookingManager.GetBalance(oldRegistration.OrganizationId, oldRegistration.TournamentId),
                            TeamsGranted = oldRegistration.TeamsGranted,
                            TeamsPaidOld = oldRegistration.TeamsPaid,
                            TeamsPaid = registration.TeamsPaid,
                            AdjudicatorsGranted = oldRegistration.AdjudicatorsGranted,
                            AdjudicatorsPaidOld = oldRegistration.AdjudicatorsPaid,
                            AdjudicatorsPaid = registration.AdjudicatorsPaid
                        };

                        if (oldRegistration.Organization != null)
                        {
                            paymentViewModel.OrganizationName = oldRegistration.Organization.Name;
                        }

                        changes.Add(paymentViewModel);
                    }

                }
            }

            if (ModelState.IsValid && changes.Count > 0)
            {
                ModelState.Clear();
                return View("UpdateConfirmedSlots", changes);
            }
            ModelState.AddModelError("", Resources.Strings.ValidationErrorCheckBelow);
            return View(registrations);
        }
Esempio n. 2
0
        private async Task ValidatePayment(PostPaymentsViewModel payments)
        {
            var ident = HttpContext.User.Identity as ClaimsIdentity;
            var tournamentId = (await (userManager.FindByIdAsync(ident.GetUserId()))).CurrentTournamentId;
            var currentTournamentId = (Guid)(tournamentId == null ? Guid.Empty : tournamentId);

            // Check for empty search term

            if (String.IsNullOrWhiteSpace(payments.SearchTerm))
            {
                ModelState.AddModelError("SearchTerm", Resources.TournamentManagement.Finance.Strings.ErrorSearchTermMissing);
                return;
            }

            // Find registration by booking code or organization name

            var registration = registrationManager.GetRegistration(payments.SearchTerm, currentTournamentId);
            if (!String.IsNullOrWhiteSpace(payments.SearchTerm) && registration == null)
            {
                registration = registrationManager.FindRegistrationByOrganizationName(currentTournamentId, payments.SearchTerm);
            }

            // Registration found

            if (registration != null)
            {

                // Check for duplicate

                var duplicate = payments.Payments.FirstOrDefault(p => p.OrganizationId == registration.OrganizationId);

                if (duplicate != null)
                {
                    ModelState.AddModelError(
                        "SearchTerm",
                        String.Format(
                            Resources.TournamentManagement.Finance.Strings.ErrorDuplicatePayment,
                            registration.Organization.Name
                        )
                    );
                    return;
                }

                // Calculate paid slots

                var slotsPaidResult = paymentManager.CalculatePaidSlots(currentTournamentId, registration.OrganizationId, payments.Value);


                // Build view model and add it to the to the main view model

                var viewModel = new PaymentViewModel
                {
                    AdjudicatorsPaid = slotsPaidResult.Adjudicators,
                    Balance = bookingManager.GetBalance(registration.OrganizationId, currentTournamentId),
                    Date = payments.Date,
                    Note = payments.Note,
                    Value = payments.Value,
                    OrganizationId = registration.OrganizationId,
                    OrganizationName = registration.Organization.Name,
                    TeamsPaidOld = registration.TeamsPaid,
                    TeamsPaid = slotsPaidResult.Teams,
                    TeamsGranted = registration.TeamsGranted,
                    AdjudicatorsGranted = registration.AdjudicatorsGranted
                };

                if (payments.Payments == null)
                {
                    payments.Payments = new List<PaymentViewModel>();
                }
                payments.Payments.Add(viewModel);

                // Reset input fields

                ModelState.Clear();
                payments.SearchTerm = String.Empty;
                payments.Value = 0;
                payments.Note = Resources.TournamentManagement.Finance.Strings.PaymentReceivedNote;

                //payments.Payments.Insert(0, viewModel);


                //// renumber model state entries

                //Regex rgxIndexPrefix = new Regex(@"^Payments\[\d+\]\.", RegexOptions.IgnoreCase);
                //Regex rgxDigits = new Regex(@"\d+");

                //ModelStateDictionary newModelState = new ModelStateDictionary();
                //foreach (var item in ModelState) {
                //    var indexPrefix = rgxIndexPrefix.Match(item.Key);
                //    String newKey = item.Key;
                //    if (indexPrefix.Success) {
                //        var indexString = rgxDigits.Match(indexPrefix.Value).Value;
                //        var index = int.Parse(indexString);
                //        index++;
                //        newKey = rgxIndexPrefix.Replace(item.Key, "Payments[" + index + "].");
                //    }
                //    newModelState.Add(newKey, item.Value);
                //}
                //ModelState.Clear();

                //foreach (var item in newModelState) {
                //    ModelState.Add(item);
                //}
            }
            else
            {
                ModelState.AddModelError("SearchTerm", Resources.TournamentManagement.Finance.Strings.ErrorSearchTermNotFound);
            }
        }